From 63e16138badac42aed5269456877bd67ef9b79c1 Mon Sep 17 00:00:00 2001 From: Richard Basch Date: Wed, 29 Nov 1995 06:08:41 +0000 Subject: [PATCH] Corrected a bug in the wrap-around carry (found bug by comparing test results with another implementation). git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@7141 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/crypto/des/u_nfold.c | 40 +++++++++++++++--------------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/lib/crypto/des/u_nfold.c b/src/lib/crypto/des/u_nfold.c index 4311b9185..63eb0e29a 100644 --- a/src/lib/crypto/des/u_nfold.c +++ b/src/lib/crypto/des/u_nfold.c @@ -66,40 +66,32 @@ mit_des_n_fold(inbuf, inlen, outbuf, outlen) #endif do { - int i, j; + int j; register unsigned int k; - if ((bytes % inlen) == 0) { - /* Rotate input */ - k = ((bytes/inlen) * ROTATE_VALUE) % (inlen*8); - - for (j = (k+7)/8; j < inlen + (k+7)/8; j++) - tempbuf[j % inlen] = - ((inbuf[((8*j-k)/8)%inlen] << ((8-(k&7))&7)) + - ((k&7) ? (inbuf[((8*j-k)/8 +1)%inlen] >> (k&7)) : 0)) - & 0xff; - } - + /* Rotate input */ + k = ((bytes/inlen) * ROTATE_VALUE) % (inlen*8); + for (j = (k+7)/8; j < inlen + (k+7)/8; j++) + tempbuf[j % inlen] = + ((inbuf[((8*j-k)/8)%inlen] << ((8-(k&7))&7)) + + ((k&7) ? (inbuf[((8*j-k)/8 +1)%inlen] >> (k&7)) : 0)) + & 0xff; - i = min(outlen - (bytes % outlen), inlen - (bytes % inlen)); - - j = i; - k = 0; - while (j--) { - k += outbuf[(bytes+j) % outlen] + tempbuf[(bytes+j) % inlen]; + for (k=0, j=inlen; j--; ) { + k += outbuf[(bytes+j) % outlen] + tempbuf[j]; outbuf[(bytes+j) % outlen] = k & 0xff; k >>= 8; } - - j = outlen-1; + j = bytes % outlen; while (k) { + if (j-- == 0) + j += outlen; k += outbuf[j]; - outbuf[j--] = k & 0xff; + outbuf[j] = k & 0xff; k >>= 8; } - - bytes += i; - } while (((bytes % inlen) != 0) || ((bytes % outlen) != 0)); + bytes += inlen; + } while (bytes % outlen); return 0; } -- 2.26.2