Skip to content

Commit 13887ed

Browse files
committed
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>
1 parent 1f38ad8 commit 13887ed

2 files changed

Lines changed: 14 additions & 20 deletions

File tree

‎crypto/sha512_generic.c‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ sha512_init(struct shash_desc *desc)
144144
sctx->state[5] = SHA512_H5;
145145
sctx->state[6] = SHA512_H6;
146146
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;
148148

149149
return 0;
150150
}
@@ -161,7 +161,7 @@ sha384_init(struct shash_desc *desc)
161161
sctx->state[5] = SHA384_H5;
162162
sctx->state[6] = SHA384_H6;
163163
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;
165165

166166
return 0;
167167
}
@@ -174,15 +174,11 @@ sha512_update(struct shash_desc *desc, const u8 *data, unsigned int len)
174174
unsigned int i, index, part_len;
175175

176176
/* Compute number of bytes mod 128 */
177-
index = (unsigned int)((sctx->count[0] >> 3) & 0x7F);
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-
}
177+
index = sctx->count[0] & 0x7f;
178+
179+
/* Update number of bytes */
180+
if (!(sctx->count[0] += len))
181+
sctx->count[1]++;
186182

187183
part_len = 128 - index;
188184

@@ -211,18 +207,16 @@ sha512_final(struct shash_desc *desc, u8 *hash)
211207
struct sha512_state *sctx = shash_desc_ctx(desc);
212208
static u8 padding[128] = { 0x80, };
213209
__be64 *dst = (__be64 *)hash;
214-
__be32 bits[4];
210+
__be64 bits[2];
215211
unsigned int index, pad_len;
216212
int i;
217213

218214
/* 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);
223217

224218
/* Pad out to 112 mod 128. */
225-
index = (sctx->count[0] >> 3) & 0x7f;
219+
index = sctx->count[0] & 0x7f;
226220
pad_len = (index < 112) ? (112 - index) : ((128+112) - index);
227221
sha512_update(desc, padding, pad_len);
228222

‎include/crypto/sha.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ struct sha256_state {
7777
};
7878

7979
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];
8383
};
8484

8585
#endif

0 commit comments

Comments
 (0)