Skip to content

Commit 1712c0e

Browse files
authored
Merge pull request #205 from libtom/pr/shake-be-fix
SHAKE (SHA3 related) big endian fix
2 parents 67ca1c0 + 4e66160 commit 1712c0e

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/hashes/sha3.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -231,33 +231,29 @@ int sha3_process(hash_state *md, const unsigned char *in, unsigned long inlen)
231231

232232
int sha3_done(hash_state *md, unsigned char *hash)
233233
{
234+
unsigned i;
235+
234236
LTC_ARGCHK(md != NULL);
235237
LTC_ARGCHK(hash != NULL);
236238

237239
md->sha3.s[md->sha3.word_index] ^= (md->sha3.saved ^ (CONST64(0x06) << (md->sha3.byte_index * 8)));
238240
md->sha3.s[SHA3_KECCAK_SPONGE_WORDS - md->sha3.capacity_words - 1] ^= CONST64(0x8000000000000000);
239241
keccakf(md->sha3.s);
240242

241-
#ifndef ENDIAN_LITTLE
242-
{
243-
unsigned i;
244-
for(i = 0; i < SHA3_KECCAK_SPONGE_WORDS; i++) {
245-
const ulong32 t1 = (ulong32)(md->sha3.s[i] & CONST64(0xFFFFFFFF));
246-
const ulong32 t2 = (ulong32)(md->sha3.s[i] >> 32);
247-
STORE32L(t1, md->sha3.sb + i * 8);
248-
STORE32L(t2, md->sha3.sb + i * 8 + 4);
249-
}
243+
/* store sha3.s[] as little-endian bytes into sha3.sb */
244+
for(i = 0; i < SHA3_KECCAK_SPONGE_WORDS; i++) {
245+
STORE64L(md->sha3.s[i], md->sha3.sb + i * 8);
250246
}
251-
#endif
252247

253248
XMEMCPY(hash, md->sha3.sb, md->sha3.capacity_words * 4);
254249
return CRYPT_OK;
255250
}
256251

257252
int sha3_shake_done(hash_state *md, unsigned char *out, unsigned long outlen)
258253
{
259-
unsigned long i = 0;
260-
/* sha3_shake_done can be called many times */
254+
/* IMPORTANT NOTE: sha3_shake_done can be called many times */
255+
unsigned long idx;
256+
unsigned i;
261257

262258
if (outlen == 0) return CRYPT_OK; /* nothing to do */
263259
LTC_ARGCHK(md != NULL);
@@ -268,16 +264,24 @@ int sha3_shake_done(hash_state *md, unsigned char *out, unsigned long outlen)
268264
md->sha3.s[md->sha3.word_index] ^= (md->sha3.saved ^ (CONST64(0x1F) << (md->sha3.byte_index * 8)));
269265
md->sha3.s[SHA3_KECCAK_SPONGE_WORDS - md->sha3.capacity_words - 1] ^= CONST64(0x8000000000000000);
270266
keccakf(md->sha3.s);
267+
/* store sha3.s[] as little-endian bytes into sha3.sb */
268+
for(i = 0; i < SHA3_KECCAK_SPONGE_WORDS; i++) {
269+
STORE64L(md->sha3.s[i], md->sha3.sb + i * 8);
270+
}
271271
md->sha3.byte_index = 0;
272272
md->sha3.xof_flag = 1;
273273
}
274274

275-
while (i < outlen) {
275+
for (idx = 0; idx < outlen; idx++) {
276276
if(md->sha3.byte_index >= (SHA3_KECCAK_SPONGE_WORDS - md->sha3.capacity_words) * 8) {
277277
keccakf(md->sha3.s);
278+
/* store sha3.s[] as little-endian bytes into sha3.sb */
279+
for(i = 0; i < SHA3_KECCAK_SPONGE_WORDS; i++) {
280+
STORE64L(md->sha3.s[i], md->sha3.sb + i * 8);
281+
}
278282
md->sha3.byte_index = 0;
279283
}
280-
out[i++] = md->sha3.sb[md->sha3.byte_index++];
284+
out[idx] = md->sha3.sb[md->sha3.byte_index++];
281285
}
282286
return CRYPT_OK;
283287
}

src/headers/tomcrypt_hash.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#ifdef LTC_SHA3
33
struct sha3_state {
44
ulong64 saved; /* the portion of the input message that we didn't consume yet */
5-
union { ulong64 s[25]; unsigned char sb[25 * 8]; };
5+
ulong64 s[25];
6+
unsigned char sb[25 * 8]; /* used for storing `ulong64 s[25]` as little-endian bytes */
67
unsigned short byte_index; /* 0..7--the next byte after the set one (starts from 0; 0--none are buffered) */
78
unsigned short word_index; /* 0..24--the next word to integrate input (starts from 0) */
89
unsigned short capacity_words; /* the double size of the hash output in words (e.g. 16 for Keccak 512) */

0 commit comments

Comments
 (0)