Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: sha512_generic - Use 64-bit counters

This patch replaces the 32-bit counters in sha512_generic with
64-bit counters. It also switches the bit count to the simpler
byte count.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

+13 -19
+10 -16
crypto/sha512_generic.c
··· 144 144 sctx->state[5] = SHA512_H5; 145 145 sctx->state[6] = SHA512_H6; 146 146 sctx->state[7] = SHA512_H7; 147 - sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0; 147 + sctx->count[0] = sctx->count[1] = 0; 148 148 149 149 return 0; 150 150 } ··· 161 161 sctx->state[5] = SHA384_H5; 162 162 sctx->state[6] = SHA384_H6; 163 163 sctx->state[7] = SHA384_H7; 164 - sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0; 164 + sctx->count[0] = sctx->count[1] = 0; 165 165 166 166 return 0; 167 167 } ··· 174 174 unsigned int i, index, part_len; 175 175 176 176 /* Compute number of bytes mod 128 */ 177 - index = (unsigned int)((sctx->count[0] >> 3) & 0x7F); 177 + index = sctx->count[0] & 0x7f; 178 178 179 - /* Update number of bits */ 180 - if ((sctx->count[0] += (len << 3)) < (len << 3)) { 181 - if ((sctx->count[1] += 1) < 1) 182 - if ((sctx->count[2] += 1) < 1) 183 - sctx->count[3]++; 184 - sctx->count[1] += (len >> 29); 185 - } 179 + /* Update number of bytes */ 180 + if (!(sctx->count[0] += len)) 181 + sctx->count[1]++; 186 182 187 183 part_len = 128 - index; 188 184 ··· 207 211 struct sha512_state *sctx = shash_desc_ctx(desc); 208 212 static u8 padding[128] = { 0x80, }; 209 213 __be64 *dst = (__be64 *)hash; 210 - __be32 bits[4]; 214 + __be64 bits[2]; 211 215 unsigned int index, pad_len; 212 216 int i; 213 217 214 218 /* Save number of bits */ 215 - bits[3] = cpu_to_be32(sctx->count[0]); 216 - bits[2] = cpu_to_be32(sctx->count[1]); 217 - bits[1] = cpu_to_be32(sctx->count[2]); 218 - bits[0] = cpu_to_be32(sctx->count[3]); 219 + bits[1] = cpu_to_be64(sctx->count[0] << 3); 220 + bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61); 219 221 220 222 /* Pad out to 112 mod 128. */ 221 - index = (sctx->count[0] >> 3) & 0x7f; 223 + index = sctx->count[0] & 0x7f; 222 224 pad_len = (index < 112) ? (112 - index) : ((128+112) - index); 223 225 sha512_update(desc, padding, pad_len); 224 226
+3 -3
include/crypto/sha.h
··· 77 77 }; 78 78 79 79 struct sha512_state { 80 - u64 state[8]; 81 - u32 count[4]; 82 - u8 buf[128]; 80 + u64 count[2]; 81 + u64 state[SHA512_DIGEST_SIZE / 8]; 82 + u8 buf[SHA512_BLOCK_SIZE]; 83 83 }; 84 84 85 85 #endif