| 1 |
from ._sha3_cffi import ffi as _ffi, lib as _lib |
| 2 |
import codecs |
| 3 |
|
| 4 |
SHA3_MAX_DIGESTSIZE = 64 |
| 5 |
SHA3_LANESIZE = (20 * 8) |
| 6 |
|
| 7 |
class Immutable(type): |
| 8 |
def __init__(cls, name, bases, dct): |
| 9 |
type.__setattr__(cls,"attr",set(dct.keys())) |
| 10 |
type.__init__(cls, name, bases, dct) |
| 11 |
|
| 12 |
def __setattr__(cls, name, value): |
| 13 |
|
| 14 |
qualname = '.'.join([cls.__module__, cls.__name__]) |
| 15 |
raise TypeError(f"cannot set '{name}' attribute of immutable type '{qualname}'") |
| 16 |
|
| 17 |
|
| 18 |
class _sha3(metaclass=Immutable): |
| 19 |
_keccak_init = None |
| 20 |
|
| 21 |
def __new__(cls, string=None, usedforsecurity=True): |
| 22 |
self = super().__new__(cls) |
| 23 |
self._hash_state = _ffi.new("Keccak_HashInstance*") |
| 24 |
|
| 25 |
assert self._keccak_init is not None |
| 26 |
self._keccak_init() |
| 27 |
|
| 28 |
if string: |
| 29 |
self.update(string) |
| 30 |
return self |
| 31 |
|
| 32 |
def update(self, string): |
| 33 |
if isinstance(string, memoryview): |
| 34 |
buf = string.tobytes() |
| 35 |
else: |
| 36 |
buf = _ffi.from_buffer(string) |
| 37 |
res = _lib.Keccak_HashUpdate(self._hash_state, buf, len(buf) * 8) |
| 38 |
|
| 39 |
def digest(self): |
| 40 |
digest = _ffi.new("char[]", |
| 41 |
SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE) |
| 42 |
state_copy = _ffi.new("Keccak_HashInstance*") |
| 43 |
_ffi.memmove(state_copy, self._hash_state, |
| 44 |
_ffi.sizeof("Keccak_HashInstance")) |
| 45 |
if _lib.Keccak_HashFinal(state_copy, digest) != _lib.SUCCESS: |
| 46 |
raise RuntimeError("internal error in SHA3 Final()") |
| 47 |
return _ffi.unpack(digest, self._hash_state.fixedOutputLength // 8) |
| 48 |
|
| 49 |
def hexdigest(self): |
| 50 |
return codecs.encode(self.digest(), 'hex').decode() |
| 51 |
|
| 52 |
def copy(self): |
| 53 |
copy = super().__new__(type(self)) |
| 54 |
copy._hash_state = _ffi.new("Keccak_HashInstance*") |
| 55 |
_ffi.memmove(copy._hash_state, self._hash_state, |
| 56 |
_ffi.sizeof("Keccak_HashInstance")) |
| 57 |
return copy |
| 58 |
|
| 59 |
@property |
| 60 |
def digest_size(self): |
| 61 |
return self._hash_state.fixedOutputLength // 8 |
| 62 |
|
| 63 |
@property |
| 64 |
def block_size(self): |
| 65 |
return self._hash_state.sponge.rate // 8 |
| 66 |
|
| 67 |
@property |
| 68 |
def _capacity_bits(self): |
| 69 |
return 1600 - self._hash_state.sponge.rate |
| 70 |
|
| 71 |
@property |
| 72 |
def _rate_bits(self): |
| 73 |
return self._hash_state.sponge.rate |
| 74 |
|
| 75 |
@property |
| 76 |
def _suffix(self): |
| 77 |
return bytes([self._hash_state.delimitedSuffix]) |
| 78 |
|
| 79 |
|
| 80 |
class _shake(_sha3): |
| 81 |
def digest(self, length): |
| 82 |
if length >= (1 << 29): |
| 83 |
raise ValueError("length is too large") |
| 84 |
if length < 0: |
| 85 |
raise ValueError("value must be positive") |
| 86 |
|
| 87 |
|
| 88 |
digest = _ffi.new("char[]", length + SHA3_LANESIZE) |
| 89 |
|
| 90 |
state_copy = _ffi.new("Keccak_HashInstance*") |
| 91 |
_ffi.memmove(state_copy, self._hash_state, |
| 92 |
_ffi.sizeof("Keccak_HashInstance")) |
| 93 |
if _lib.Keccak_HashFinal(state_copy, digest) != _lib.SUCCESS: |
| 94 |
raise RuntimeError("internal error in SHA3 Final()") |
| 95 |
if _lib.Keccak_HashSqueeze(state_copy, digest, length * 8) != _lib.SUCCESS: |
| 96 |
raise RuntimeError("internal error in SHA3 Squeeze()") |
| 97 |
return _ffi.unpack(digest, length) |
| 98 |
|
| 99 |
def hexdigest(self, length): |
| 100 |
return codecs.encode(self.digest(length), 'hex').decode() |
| 101 |
|
| 102 |
|
| 103 |
class sha3_224(_sha3): |
| 104 |
name = "sha3_224" |
| 105 |
def _keccak_init(self): |
| 106 |
return _lib.Keccak_HashInitialize_SHA3_224(self._hash_state) |
| 107 |
|
| 108 |
class sha3_256(_sha3): |
| 109 |
name = "sha3_256" |
| 110 |
def _keccak_init(self): |
| 111 |
return _lib.Keccak_HashInitialize_SHA3_256(self._hash_state) |
| 112 |
|
| 113 |
class sha3_384(_sha3): |
| 114 |
name = "sha3_384" |
| 115 |
def _keccak_init(self): |
| 116 |
return _lib.Keccak_HashInitialize_SHA3_384(self._hash_state) |
| 117 |
|
| 118 |
class sha3_512(_sha3): |
| 119 |
name = "sha3_512" |
| 120 |
def _keccak_init(self): |
| 121 |
return _lib.Keccak_HashInitialize_SHA3_512(self._hash_state) |
| 122 |
|
| 123 |
class shake_128(_shake): |
| 124 |
name = "shake_128" |
| 125 |
def _keccak_init(self): |
| 126 |
return _lib.Keccak_HashInitialize_SHAKE128(self._hash_state) |
| 127 |
|
| 128 |
class shake_256(_shake): |
| 129 |
name = "shake_256" |
| 130 |
def _keccak_init(self): |
| 131 |
return _lib.Keccak_HashInitialize_SHAKE256(self._hash_state) |
| 132 |
|