Skip to content

Commit

Permalink
Fix carry overflow in Stribog in 512-bit addition
Browse files Browse the repository at this point in the history
* cipher/stribog.c (transform_bits): properly calculate carry flag
* tests/basic.c (check_digests): add two more test cases
--

Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  • Loading branch information
lumag authored and jkivilin committed May 6, 2019
1 parent 71b0eb3 commit da6cd4f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
12 changes: 7 additions & 5 deletions cipher/stribog.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ static void
transform_bits (STRIBOG_CONTEXT *hd, const unsigned char *data, unsigned count)
{
u64 M[8];
u64 l;
u64 l, cf;
int i;

for (i = 0; i < 8; i++)
Expand All @@ -1243,11 +1243,13 @@ transform_bits (STRIBOG_CONTEXT *hd, const unsigned char *data, unsigned count)
}

hd->Sigma[0] += M[0];
cf = 0;
for (i = 1; i < 8; i++)
if (hd->Sigma[i-1] < M[i-1])
hd->Sigma[i] += M[i] + 1;
else
hd->Sigma[i] += M[i];
{
if (hd->Sigma[i-1] != M[i-1])
cf = (hd->Sigma[i-1] < M[i-1]);
hd->Sigma[i] += M[i] + cf;
}
}

static unsigned int
Expand Down
27 changes: 27 additions & 0 deletions tests/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -9168,6 +9168,33 @@ check_digests (void)
"\x20\xc8\xe3\xee\xf0\xe5\xe2\xfb",
"\x9d\xd2\xfe\x4e\x90\x40\x9e\x5d\xa8\x7f\x53\x97\x6d\x74\x05\xb0"
"\xc0\xca\xc6\x28\xfc\x66\x9a\x74\x1d\x50\x06\x3c\x55\x7e\x8f\x50" },
/* Special tests for carry flag in addition */
{ GCRY_MD_STRIBOG512,
"\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
"\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
"\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
"\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
"\x16\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
"\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
"\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
"\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x16",
"\x8b\x06\xf4\x1e\x59\x90\x7d\x96\x36\xe8\x92\xca\xf5\x94\x2f\xcd"
"\xfb\x71\xfa\x31\x16\x9a\x5e\x70\xf0\xed\xb8\x73\x66\x4d\xf4\x1c"
"\x2c\xce\x6e\x06\xdc\x67\x55\xd1\x5a\x61\xcd\xeb\x92\xbd\x60\x7c"
"\xc4\xaa\xca\x67\x32\xbf\x35\x68\xa2\x3a\x21\x0d\xd5\x20\xfd\x41" },
{ GCRY_MD_STRIBOG512,
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff",
"\x90\xa1\x61\xd1\x2a\xd3\x09\x49\x8d\x3f\xe5\xd4\x82\x02\xd8\xa4"
"\xe9\xc4\x06\xd6\xa2\x64\xae\xab\x25\x8a\xc5\xec\xc3\x7a\x79\x62"
"\xaa\xf9\x58\x7a\x5a\xbb\x09\xb6\xbb\x81\xec\x4b\x37\x52\xa3\xff"
"\x5a\x83\x8e\xf1\x75\xbe\x57\x72\x05\x6b\xc5\xfe\x54\xfc\xfc\x7e" },
#include "./sha3-224.h"
#include "./sha3-256.h"
#include "./sha3-384.h"
Expand Down

0 comments on commit da6cd4f

Please sign in to comment.