polhenarejos / pycvc

Card Verifiable Certificates (CVC) tools for Python
https://www.picokeys.com
GNU General Public License v3.0
13 stars 3 forks source link

R and S components of ECDSA signature should be encoded to fixed number of bytes #8

Closed rathorearvind19 closed 9 months ago

rathorearvind19 commented 10 months ago

I've run into a certificate which doesn't have correctly encoded signature for ECC algorithms. The problem is that the r and s components of ECDSA signature are encoded to minimum number of bytes required to fit those integers. However, they should be encoded to fixed number of bytes (derived based on the curve size, e.g. 32 bytes for P256, 28 bytes for P224, 24 bytes for P192, and so on). Could you please look into it and fix the issue?

From certificates.py:

    def sign(self, key, scheme):
        h,p = get_hash_padding(scheme)
        if (isinstance(key, ec.EllipticCurvePrivateKey)):
            signature = key.sign(self.__a.encode(), ec.ECDSA(h))
            r,s = utils.decode_dss_signature(signature)
            n = math.ceil(key.curve.key_size / 8)
            _**signature = r.to_bytes(n, 'big') + s.to_bytes(n, 'big')**_
        elif (isinstance(key, rsa.RSAPrivateKey)):
            signature = key.sign(self.__a.encode(), p, h)
        elif (isinstance(key, (ed25519.Ed25519PrivateKey, ed448.Ed448PrivateKey))):
            signature = key.sign(self.__a.encode())
        self.__a = self.__a.add_tag(0x5f37, bytearray(signature))
        return self
polhenarejos commented 9 months ago

r and s are converted to a fixed number of bytes n, which is the size of the key in octets (not the minimum size of each), making r and s of equal length. Can you provide an example of the problem you are experiencing?

rathorearvind19 commented 9 months ago

Ok. I see that it's correctly handled now. I referred to to_bytes function in utils by mistake.

Will check why I was getting incorrectly formatted signature in one of the certs I generated.

Closing the issue.