def __init__(self): self.native_object = _ffi.new("RsaKey *") ret = _lib.wc_InitRsaKey(self.native_object, _ffi.NULL) if ret < 0: raise WolfCryptError("Invalid key error (%d)" % ret) self._random = Random()
def __init__(self): self.native_object = _ffi.new("RsaKey *") ret = _lib.wc_InitRsaKey(self.native_object, _ffi.NULL) if ret < 0: # pragma: no cover raise WolfCryptError("Invalid key error (%d)" % ret) self._random = Random() ret = _lib.wc_RsaSetRNG(self.native_object, self._random.native_object) if ret < 0: # pragma: no cover raise WolfCryptError("Key initialization error (%d)" % ret)
def make_key(cls, size, rng=Random()): """ Generates a new key pair of desired length **size**. """ ecc = cls() ret = _lib.wc_ecc_make_key(rng.native_object, size, ecc.native_object) if ret < 0: raise WolfCryptError("Key generation error (%d)" % ret) return ecc
def make_key(cls, size, rng=Random()): """ Generates a new key pair of desired length **size**. """ rsa = cls(None) if rsa == None: # pragma: no cover raise WolfCryptError("Invalid key error (%d)" % ret) ret = _lib.wc_MakeRsaKey(rsa.native_object, size, 65537, rng.native_object) if ret < 0: raise WolfCryptError("Key generation error (%d)" % ret) rsa.output_size = _lib.wc_RsaEncryptSize(rsa.native_object) rsa.size = size if rsa.output_size <= 0: # pragma: no cover raise WolfCryptError("Invalid key size error (%d)" % ret) return rsa
def sign(self, plaintext, rng=Random()): """ Signs **plaintext**, using the private key data in the object. Returns the signature. """ plaintext = t2b(plaintext) signature = _ffi.new("byte[%d]" % self.max_signature_size) signature_size = _ffi.new("word32[1]") signature_size[0] = self.max_signature_size ret = _lib.wc_ecc_sign_hash(plaintext, len(plaintext), signature, signature_size, rng.native_object, self.native_object) if ret != 0: # pragma: no cover raise WolfCryptError("Signature error (%d)" % ret) return _ffi.buffer(signature, signature_size[0])[:]
def sign_raw(self, plaintext, rng=Random()): """ Signs **plaintext**, using the private key data in the object. Returns the signature in its two raw components r, s """ plaintext = t2b(plaintext) R = _ffi.new("mp_int[1]"); S = _ffi.new("mp_int[1]"); R_bin = _ffi.new("unsigned char[%d]" % self.size ) S_bin = _ffi.new("unsigned char[%d]" % self.size ) ret = _lib.mp_init(R) if ret != 0: # pragma: no cover raise WolfCryptError("wolfCrypt error (%d)" % ret) ret = _lib.mp_init(S) if ret != 0: # pragma: no cover raise WolfCryptError("wolfCrypt error (%d)" % ret) ret = _lib.wc_ecc_sign_hash_ex(plaintext, len(plaintext), rng.native_object, self.native_object, R, S) if ret != 0: # pragma: no cover raise WolfCryptError("Signature error (%d)" % ret) ret = _lib.mp_to_unsigned_bin(R, R_bin) if ret != 0: # pragma: no cover raise WolfCryptError("wolfCrypt error (%d)" % ret) ret = _lib.mp_to_unsigned_bin(S, S_bin) if ret != 0: # pragma: no cover raise WolfCryptError("wolfCrypt error (%d)" % ret) return _ffi.buffer(R_bin, self.size)[:], _ffi.buffer(S_bin, self.size)[:]
def rng(): return Random()