From: Andrey Rys Date: Wed, 27 Feb 2019 08:54:12 +0000 (+0700) Subject: ctr_add: replace dead with working code. X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=0f0e095f408d4d867a056dffc9d20d853d761489;p=tfcrypt.git ctr_add: replace dead with working code. ctr_add came directly from 2012, and it was always broken. The original purpose is to add two very large integers. Still, there is no much to worry about: Threefish 64 bit integers make it very hard to trigger the old buggy code, and it was only used to rewind the counter. But it is time to finally fix it. --- diff --git a/VERSION b/VERSION index b4de394..48082f7 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -11 +12 diff --git a/tfctrapi.c b/tfctrapi.c index fcd14ee..2845b2d 100644 --- a/tfctrapi.c +++ b/tfctrapi.c @@ -8,7 +8,7 @@ void tf_ctr_set(void *ctr, const void *sctr, size_t sctrsz) memset(usctr, 0, TF_BLOCK_SIZE); memcpy(usctr, sctr, sctrsz > TF_BLOCK_SIZE ? TF_BLOCK_SIZE : sctrsz); - ctr_add(uctr, usctr, TF_NR_BLOCK_UNITS); + ctr_add(uctr, TF_NR_BLOCK_UNITS, usctr, TF_NR_BLOCK_UNITS); data_to_words(uctr, TF_BLOCK_SIZE); memset(usctr, 0, TF_BLOCK_SIZE); } diff --git a/tfdef.h b/tfdef.h index 5450271..463e5fb 100644 --- a/tfdef.h +++ b/tfdef.h @@ -87,31 +87,26 @@ static inline void data_to_words(void *p, size_t l) #endif } -static inline void ctr_inc(TF_UNIT_TYPE *x, size_t l) +static inline void ctr_inc(TF_UNIT_TYPE *x, size_t xl) { - size_t i; + size_t z; - for (i = 0; i < l; i++) { - x[i] = ((x[i] + (TF_UNIT_TYPE)1) & ((TF_UNIT_TYPE)~0)); - if (x[i]) break; + for (z = 0; z < xl; z++) { + x[z] = ((x[z] + (TF_UNIT_TYPE)1) & ((TF_UNIT_TYPE)~0)); + if (x[z]) break; } } -static inline void ctr_add(TF_UNIT_TYPE *x, const TF_UNIT_TYPE *y, size_t l) +static inline void ctr_add(TF_UNIT_TYPE *x, size_t xl, const TF_UNIT_TYPE *y, size_t yl) { - size_t i, f = 0; + size_t z, cf; TF_UNIT_TYPE t; - for (i = 0; i < l; i++) { - t = x[i]; - x[i] += y[i]; x[i] &= ((TF_UNIT_TYPE)~0); - if (x[i] < t) { -_again: f++; - t = x[f-i]; - x[f-i]++; - if (x[f-i] < t) goto _again; - else f = 0; - } + for (z = 0, cf = 0; z < xl; z++) { + t = x[z] + (z >= yl ? (TF_UNIT_TYPE)0 : y[z]) + cf; + if (cf) cf = (x[z] >= t ? 1 : 0); + else cf = (x[z] > t ? 1 : 0); + x[z] = t; } }