Commit 13887ed6888dad1608eb9530ebd83b6ba29db577

Authored by Herbert Xu
1 parent 1f38ad8389

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>

Showing 2 changed files with 14 additions and 20 deletions Side-by-side Diff

crypto/sha512_generic.c
... ... @@ -144,7 +144,7 @@
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,7 +161,7 @@
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 }
168 168  
... ... @@ -174,15 +174,11 @@
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  
189 185  
190 186  
... ... @@ -211,18 +207,16 @@
211 207 struct sha512_state *sctx = shash_desc_ctx(desc);
212 208 static u8 padding[128] = { 0x80, };
213 209 __be64 *dst = (__be64 *)hash;
214   - __be32 bits[4];
  210 + __be64 bits[2];
215 211 unsigned int index, pad_len;
216 212 int i;
217 213  
218 214 /* Save number of bits */
219   - bits[3] = cpu_to_be32(sctx->count[0]);
220   - bits[2] = cpu_to_be32(sctx->count[1]);
221   - bits[1] = cpu_to_be32(sctx->count[2]);
222   - bits[0] = cpu_to_be32(sctx->count[3]);
  215 + bits[1] = cpu_to_be64(sctx->count[0] << 3);
  216 + bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61);
223 217  
224 218 /* Pad out to 112 mod 128. */
225   - index = (sctx->count[0] >> 3) & 0x7f;
  219 + index = sctx->count[0] & 0x7f;
226 220 pad_len = (index < 112) ? (112 - index) : ((128+112) - index);
227 221 sha512_update(desc, padding, pad_len);
228 222  
include/crypto/sha.h
... ... @@ -77,9 +77,9 @@
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