class _Hmac(_Hash): """ A **PEP 247: Cryptographic Hash Functions** compliant **Keyed Hash Function Interface**. """ digest_size = None _native_type = "Hmac *" _native_size = _ffi.sizeof("Hmac") def __init__(self, key, string=None): # pylint: disable=W0231 key = t2b(key) self._native_object = _ffi.new(self._native_type) ret = self._init(self._type, key) if ret < 0: # pragma: no cover raise WolfCryptError("Hmac init error (%d)" % ret) if string: self.update(string) @classmethod def new(cls, key, string=None): # pylint: disable=W0221 """ Creates a new hashing object and returns it. **key** is a required parameter containing a string giving the key to use. The optional **string** parameter, if supplied, will be immediately hashed into the object's starting state, as if obj.update(string) was called. """ return cls(key, string) def _init(self, hmac, key): if _lib.wc_HmacInit(self._native_object, _ffi.NULL, -2) != 0: raise WolfCryptError("wc_HmacInit error") # If the key isn't set, don't call wc_HmacSetKey. This can happen, # for example, when the HMAC object is being copied. See the copy # function of _Hash. ret = 0 if len(key) > 0: ret = _lib.wc_HmacSetKey(self._native_object, hmac, key, len(key)) if ret < 0: err_str = "no error description found" try: err_str = _ffi.string( _lib.wc_GetErrorString(ret)).decode() except: pass raise WolfCryptError( "wc_HmacSetKey returned {}: {}".format(ret, err_str)) return ret def _update(self, data): return _lib.wc_HmacUpdate(self._native_object, data, len(data)) def _final(self, obj, ret): return _lib.wc_HmacFinal(obj, ret)
class Sha(_Hash): """ **SHA-1** is a cryptographic hash function standardized by **NIST**. It produces an [ **160-bit | 20 bytes** ] message digest. """ digest_size = 20 _native_type = "Sha *" _native_size = _ffi.sizeof("Sha") def _init(self): return _lib.wc_InitSha(self._native_object) def _update(self, data): return _lib.wc_ShaUpdate(self._native_object, data, len(data)) def _final(self, obj, ret): return _lib.wc_ShaFinal(obj, ret)
class Sha512(_Hash): """ **SHA-512** is a cryptographic hash function from the **SHA-2 family** and is standardized by **NIST**. It produces a [ **512-bit | 64 bytes** ] message digest. """ digest_size = 64 _native_type = "Sha512 *" _native_size = _ffi.sizeof("Sha512") def _init(self): return _lib.wc_InitSha512(self._native_object) def _update(self, data): return _lib.wc_Sha512Update(self._native_object, data, len(data)) def _final(self, obj, ret): return _lib.wc_Sha512Final(obj, ret)
class Sha384(_Hash): """ **SHA-384** is a cryptographic hash function from the **SHA-2 family** and is standardized by **NIST**. It produces a [ **384-bit | 48 bytes** ] message digest. """ digest_size = 48 _native_type = "wc_Sha384 *" _native_size = _ffi.sizeof("wc_Sha384") def _init(self): return _lib.wc_InitSha384(self._native_object) def _update(self, data): return _lib.wc_Sha384Update(self._native_object, data, len(data)) def _final(self, obj, ret): return _lib.wc_Sha384Final(obj, ret)
class _Hmac(_Hash): """ A **PEP 247: Cryptographic Hash Functions** compliant **Keyed Hash Function Interface**. """ digest_size = None _native_type = "Hmac *" _native_size = _ffi.sizeof("Hmac") def __init__(self, key, string=None): key = t2b(key) self._native_object = _ffi.new(self._native_type) ret = self._init(self._type, key) if ret < 0: raise WolfCryptError("Hmac init error (%d)" % ret) if (string): self.update(string) @classmethod def new(cls, key, string=None): """ Creates a new hashing object and returns it. **key** is a required parameter containing a string giving the key to use. The optional **string** parameter, if supplied, will be immediately hashed into the object's starting state, as if obj.update(string) was called. """ return cls(key, string) def _init(self, type, key): return _lib.wc_HmacSetKey(self._native_object, type, key, len(key)) def _update(self, data): return _lib.wc_HmacUpdate(self._native_object, data, len(data)) def _final(self, obj, ret): return _lib.wc_HmacFinal(obj, ret)
class Sha3(_Hash): """ **SHA3 ** is a cryptographic hash function family standardized by **NIST**. It produces from [ **224-bit | 28 bytes** ] up to [ **512-bit | 64 bytes] message digests. Using SHA3-384 by default, unless a different digest size is passed through __init__. """ _native_type = "wc_Sha3 *" _native_size = _ffi.sizeof("wc_Sha3") SHA3_224_DIGEST_SIZE = 28 SHA3_256_DIGEST_SIZE = 32 SHA3_384_DIGEST_SIZE = 48 SHA3_512_DIGEST_SIZE = 64 def __init__(self): # pylint: disable=W0231 self._native_object = _ffi.new(self._native_type) self.digest_size = SHA3_384_DIGEST_SIZE ret = self._init() if ret < 0: # pragma: no cover raise WolfCryptError("Sha3 init error (%d)" % ret) def __init__(self, string, size=SHA3_384_DIGEST_SIZE): # pylint: disable=W0231 self._native_object = _ffi.new(self._native_type) self.digest_size = size ret = self._init() if ret < 0: # pragma: no cover raise WolfCryptError("Sha3 init error (%d)" % ret) if string: self.update(string) def _init(self): if (self.digest_size != Sha3.SHA3_224_DIGEST_SIZE and self.digest_size != Sha3.SHA3_256_DIGEST_SIZE and self.digest_size != Sha3.SHA3_384_DIGEST_SIZE and self.digest_size != Sha3.SHA3_512_DIGEST_SIZE): return -1 if self.digest_size == Sha3.SHA3_224_DIGEST_SIZE: return _lib.wc_InitSha3_224(self._native_object, _ffi.NULL, 0) if self.digest_size == Sha3.SHA3_256_DIGEST_SIZE: return _lib.wc_InitSha3_256(self._native_object, _ffi.NULL, 0) if self.digest_size == Sha3.SHA3_384_DIGEST_SIZE: return _lib.wc_InitSha3_384(self._native_object, _ffi.NULL, 0) if self.digest_size == Sha3.SHA3_512_DIGEST_SIZE: return _lib.wc_InitSha3_512(self._native_object, _ffi.NULL, 0) def _update(self, data): if self.digest_size == Sha3.SHA3_224_DIGEST_SIZE: return _lib.wc_Sha3_224_Update(self._native_object, data, len(data)) if self.digest_size == Sha3.SHA3_256_DIGEST_SIZE: return _lib.wc_Sha3_256_Update(self._native_object, data, len(data)) if self.digest_size == Sha3.SHA3_384_DIGEST_SIZE: return _lib.wc_Sha3_384_Update(self._native_object, data, len(data)) if self.digest_size == Sha3.SHA3_512_DIGEST_SIZE: return _lib.wc_Sha3_512_Update(self._native_object, data, len(data)) def _final(self, obj, ret): if self.digest_size == Sha3.SHA3_224_DIGEST_SIZE: return _lib.wc_Sha3_224_Final(obj, ret) if self.digest_size == Sha3.SHA3_256_DIGEST_SIZE: return _lib.wc_Sha3_256_Final(obj, ret) if self.digest_size == Sha3.SHA3_384_DIGEST_SIZE: return _lib.wc_Sha3_384_Final(obj, ret) if self.digest_size == Sha3.SHA3_512_DIGEST_SIZE: return _lib.wc_Sha3_512_Final(obj, ret)