Skip to content

Commit

Permalink
Padding byte is passed to digest/squeeze
Browse files Browse the repository at this point in the history
  • Loading branch information
Legrandin committed Nov 20, 2021
1 parent 6c25e34 commit 8005ed6
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 49 deletions.
12 changes: 7 additions & 5 deletions lib/Crypto/Hash/SHA3_224.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
c_uint8_ptr)
c_uint8_ptr, c_ubyte)

from Crypto.Hash.keccak import _raw_keccak_lib

Expand Down Expand Up @@ -52,12 +52,12 @@ class SHA3_224_Hash(object):
def __init__(self, data, update_after_digest):
self._update_after_digest = update_after_digest
self._digest_done = False
self._padding = 0x06

state = VoidPointer()
result = _raw_keccak_lib.keccak_init(state.address_of(),
c_size_t(self.digest_size * 2),
0x06,
24)
c_ubyte(24))
if result:
raise ValueError("Error %d while instantiating SHA-3/224"
% result)
Expand All @@ -78,7 +78,8 @@ def update(self, data):

result = _raw_keccak_lib.keccak_absorb(self._state.get(),
c_uint8_ptr(data),
c_size_t(len(data)))
c_size_t(len(data))
)
if result:
raise ValueError("Error %d while updating SHA-3/224"
% result)
Expand All @@ -97,7 +98,8 @@ def digest(self):
bfr = create_string_buffer(self.digest_size)
result = _raw_keccak_lib.keccak_digest(self._state.get(),
bfr,
c_size_t(self.digest_size))
c_size_t(self.digest_size),
c_ubyte(self._padding))
if result:
raise ValueError("Error %d while instantiating SHA-3/224"
% result)
Expand Down
12 changes: 7 additions & 5 deletions lib/Crypto/Hash/SHA3_256.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
c_uint8_ptr)
c_uint8_ptr, c_ubyte)

from Crypto.Hash.keccak import _raw_keccak_lib

Expand Down Expand Up @@ -52,12 +52,12 @@ class SHA3_256_Hash(object):
def __init__(self, data, update_after_digest):
self._update_after_digest = update_after_digest
self._digest_done = False
self._padding = 0x06

state = VoidPointer()
result = _raw_keccak_lib.keccak_init(state.address_of(),
c_size_t(self.digest_size * 2),
0x06,
24)
c_ubyte(24))
if result:
raise ValueError("Error %d while instantiating SHA-3/256"
% result)
Expand All @@ -78,7 +78,8 @@ def update(self, data):

result = _raw_keccak_lib.keccak_absorb(self._state.get(),
c_uint8_ptr(data),
c_size_t(len(data)))
c_size_t(len(data))
)
if result:
raise ValueError("Error %d while updating SHA-3/256"
% result)
Expand All @@ -97,7 +98,8 @@ def digest(self):
bfr = create_string_buffer(self.digest_size)
result = _raw_keccak_lib.keccak_digest(self._state.get(),
bfr,
c_size_t(self.digest_size))
c_size_t(self.digest_size),
c_ubyte(self._padding))
if result:
raise ValueError("Error %d while instantiating SHA-3/256"
% result)
Expand Down
9 changes: 5 additions & 4 deletions lib/Crypto/Hash/SHA3_384.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
c_uint8_ptr)
c_uint8_ptr, c_ubyte)

from Crypto.Hash.keccak import _raw_keccak_lib

Expand Down Expand Up @@ -52,12 +52,12 @@ class SHA3_384_Hash(object):
def __init__(self, data, update_after_digest):
self._update_after_digest = update_after_digest
self._digest_done = False
self._padding = 0x06

state = VoidPointer()
result = _raw_keccak_lib.keccak_init(state.address_of(),
c_size_t(self.digest_size * 2),
0x06,
24)
c_ubyte(24))
if result:
raise ValueError("Error %d while instantiating SHA-3/384"
% result)
Expand Down Expand Up @@ -97,7 +97,8 @@ def digest(self):
bfr = create_string_buffer(self.digest_size)
result = _raw_keccak_lib.keccak_digest(self._state.get(),
bfr,
c_size_t(self.digest_size))
c_size_t(self.digest_size),
c_ubyte(self._padding))
if result:
raise ValueError("Error %d while instantiating SHA-3/384"
% result)
Expand Down
9 changes: 5 additions & 4 deletions lib/Crypto/Hash/SHA3_512.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
c_uint8_ptr)
c_uint8_ptr, c_ubyte)

from Crypto.Hash.keccak import _raw_keccak_lib

Expand Down Expand Up @@ -52,12 +52,12 @@ class SHA3_512_Hash(object):
def __init__(self, data, update_after_digest):
self._update_after_digest = update_after_digest
self._digest_done = False
self._padding = 0x06

state = VoidPointer()
result = _raw_keccak_lib.keccak_init(state.address_of(),
c_size_t(self.digest_size * 2),
0x06,
24)
c_ubyte(24))
if result:
raise ValueError("Error %d while instantiating SHA-3/512"
% result)
Expand Down Expand Up @@ -98,7 +98,8 @@ def digest(self):
bfr = create_string_buffer(self.digest_size)
result = _raw_keccak_lib.keccak_digest(self._state.get(),
bfr,
c_size_t(self.digest_size))
c_size_t(self.digest_size),
c_ubyte(self._padding))
if result:
raise ValueError("Error %d while instantiating SHA-3/512"
% result)
Expand Down
9 changes: 5 additions & 4 deletions lib/Crypto/Hash/SHAKE128.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
c_uint8_ptr)
c_uint8_ptr, c_ubyte)

from Crypto.Hash.keccak import _raw_keccak_lib

Expand All @@ -54,14 +54,14 @@ def __init__(self, data=None):
state = VoidPointer()
result = _raw_keccak_lib.keccak_init(state.address_of(),
c_size_t(32),
0x1F,
24)
c_ubyte(24))
if result:
raise ValueError("Error %d while instantiating SHAKE128"
% result)
self._state = SmartPointer(state.get(),
_raw_keccak_lib.keccak_destroy)
self._is_squeezing = False
self._padding = 0x1F
if data:
self.update(data)

Expand Down Expand Up @@ -102,7 +102,8 @@ def read(self, length):
bfr = create_string_buffer(length)
result = _raw_keccak_lib.keccak_squeeze(self._state.get(),
bfr,
c_size_t(length))
c_size_t(length),
c_ubyte(self._padding))
if result:
raise ValueError("Error %d while extracting from SHAKE128"
% result)
Expand Down
10 changes: 6 additions & 4 deletions lib/Crypto/Hash/SHAKE256.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
c_uint8_ptr)
c_uint8_ptr, c_ubyte)

from Crypto.Hash.keccak import _raw_keccak_lib

Expand All @@ -54,14 +54,15 @@ def __init__(self, data=None):
state = VoidPointer()
result = _raw_keccak_lib.keccak_init(state.address_of(),
c_size_t(64),
0x1F,
24)
c_ubyte(24))
if result:
raise ValueError("Error %d while instantiating SHAKE256"
% result)
self._state = SmartPointer(state.get(),
_raw_keccak_lib.keccak_destroy)
self._is_squeezing = False
self._padding = 0x1F

if data:
self.update(data)

Expand Down Expand Up @@ -102,7 +103,8 @@ def read(self, length):
bfr = create_string_buffer(length)
result = _raw_keccak_lib.keccak_squeeze(self._state.get(),
bfr,
c_size_t(length))
c_size_t(length),
c_ubyte(self._padding))
if result:
raise ValueError("Error %d while extracting from SHAKE256"
% result)
Expand Down
2 changes: 1 addition & 1 deletion lib/Crypto/Hash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
__all__ = ['HMAC', 'MD2', 'MD4', 'MD5', 'RIPEMD160', 'SHA1',
'SHA224', 'SHA256', 'SHA384', 'SHA512', 'CMAC', 'Poly1305',
'cSHAKE128', 'cSHAKE256', 'KMAC128', 'KMAC256',
'TupleHash128', 'TupleHash256']
'TupleHash128', 'TupleHash256', 'KangarooTwelve']
12 changes: 6 additions & 6 deletions lib/Crypto/Hash/cSHAKE128.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from Crypto.Util._raw_api import (VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
c_uint8_ptr)
c_uint8_ptr, c_ubyte)

from Crypto.Util.number import long_to_bytes

Expand Down Expand Up @@ -96,15 +96,14 @@ def __init__(self, data, custom, capacity, function):
if custom or function:
prefix_unpad = _encode_str(function) + _encode_str(custom)
prefix = _bytepad(prefix_unpad, (1600 - capacity)//8)
pad = 0x04
self._padding = 0x04
else:
prefix = None
pad = 0x1F # for SHAKE
self._padding = 0x1F # for SHAKE

result = _raw_keccak_lib.keccak_init(state.address_of(),
c_size_t(capacity//8),
pad,
24)
c_ubyte(24))
if result:
raise ValueError("Error %d while instantiating cSHAKE"
% result)
Expand Down Expand Up @@ -155,7 +154,8 @@ def read(self, length):
bfr = create_string_buffer(length)
result = _raw_keccak_lib.keccak_squeeze(self._state.get(),
bfr,
c_size_t(length))
c_size_t(length),
c_ubyte(self._padding))
if result:
raise ValueError("Error %d while extracting from %s"
% (result, self.name))
Expand Down
18 changes: 11 additions & 7 deletions lib/Crypto/Hash/keccak.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,25 @@
VoidPointer, SmartPointer,
create_string_buffer,
get_raw_buffer, c_size_t,
c_uint8_ptr)
c_uint8_ptr, c_ubyte)

_raw_keccak_lib = load_pycryptodome_raw_lib("Crypto.Hash._keccak",
"""
int keccak_init(void **state,
size_t capacity_bytes,
uint8_t padding_byte,
uint8_t rounds);
int keccak_destroy(void *state);
int keccak_absorb(void *state,
const uint8_t *in,
size_t len);
int keccak_squeeze(const void *state,
uint8_t *out,
size_t len);
int keccak_digest(void *state, uint8_t *digest, size_t len);
size_t len,
uint8_t padding);
int keccak_digest(void *state,
uint8_t *digest,
size_t len,
uint8_t padding);
int keccak_copy(const void *src, void *dst);
""")

Expand All @@ -68,12 +71,12 @@ def __init__(self, data, digest_bytes, update_after_digest):

self._update_after_digest = update_after_digest
self._digest_done = False
self._padding = 0x01

state = VoidPointer()
result = _raw_keccak_lib.keccak_init(state.address_of(),
c_size_t(self.digest_size * 2),
0x01,
24)
c_ubyte(24))
if result:
raise ValueError("Error %d while instantiating keccak" % result)
self._state = SmartPointer(state.get(),
Expand Down Expand Up @@ -110,7 +113,8 @@ def digest(self):
bfr = create_string_buffer(self.digest_size)
result = _raw_keccak_lib.keccak_digest(self._state.get(),
bfr,
c_size_t(self.digest_size))
c_size_t(self.digest_size),
c_ubyte(self._padding))
if result:
raise ValueError("Error %d while squeezing keccak" % result)

Expand Down
Loading

0 comments on commit 8005ed6

Please sign in to comment.