Skip to content

Commit 7d2f5b0

Browse files
jwrdegoedeherbertx
authored andcommitted
crypto: sha256 - Add sha224 support to sha256 library code
Add sha224 support to the lib/crypto/sha256 library code. This will allow us to replace both the sha256 and sha224 parts of crypto/sha256_generic.c when we remove the code duplication in further patches in this series. Suggested-by: Eric Biggers <ebiggers@kernel.org> Signed-off-by: Hans de Goede <hdegoede@redhat.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 01d3aee commit 7d2f5b0

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎include/crypto/sha256.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ extern int sha256_update(struct sha256_state *sctx, const u8 *input,
2626
unsigned int length);
2727
extern int sha256_final(struct sha256_state *sctx, u8 *hash);
2828

29+
extern int sha224_init(struct sha256_state *sctx);
30+
extern int sha224_update(struct sha256_state *sctx, const u8 *input,
31+
unsigned int length);
32+
extern int sha224_final(struct sha256_state *sctx, u8 *hash);
33+
2934
#endif /* SHA256_H */

‎lib/crypto/sha256.c‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,22 @@ int sha256_init(struct sha256_state *sctx)
221221
}
222222
EXPORT_SYMBOL(sha256_init);
223223

224+
int sha224_init(struct sha256_state *sctx)
225+
{
226+
sctx->state[0] = SHA224_H0;
227+
sctx->state[1] = SHA224_H1;
228+
sctx->state[2] = SHA224_H2;
229+
sctx->state[3] = SHA224_H3;
230+
sctx->state[4] = SHA224_H4;
231+
sctx->state[5] = SHA224_H5;
232+
sctx->state[6] = SHA224_H6;
233+
sctx->state[7] = SHA224_H7;
234+
sctx->count = 0;
235+
236+
return 0;
237+
}
238+
EXPORT_SYMBOL(sha224_init);
239+
224240
int sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len)
225241
{
226242
unsigned int partial, done;
@@ -252,7 +268,13 @@ int sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len)
252268
}
253269
EXPORT_SYMBOL(sha256_update);
254270

255-
int sha256_final(struct sha256_state *sctx, u8 *out)
271+
int sha224_update(struct sha256_state *sctx, const u8 *data, unsigned int len)
272+
{
273+
return sha256_update(sctx, data, len);
274+
}
275+
EXPORT_SYMBOL(sha224_update);
276+
277+
static int __sha256_final(struct sha256_state *sctx, u8 *out, int digest_words)
256278
{
257279
__be32 *dst = (__be32 *)out;
258280
__be64 bits;
@@ -272,12 +294,23 @@ int sha256_final(struct sha256_state *sctx, u8 *out)
272294
sha256_update(sctx, (const u8 *)&bits, sizeof(bits));
273295

274296
/* Store state in digest */
275-
for (i = 0; i < 8; i++)
297+
for (i = 0; i < digest_words; i++)
276298
put_unaligned_be32(sctx->state[i], &dst[i]);
277299

278300
/* Zeroize sensitive information. */
279301
memset(sctx, 0, sizeof(*sctx));
280302

281303
return 0;
282304
}
305+
306+
int sha256_final(struct sha256_state *sctx, u8 *out)
307+
{
308+
return __sha256_final(sctx, out, 8);
309+
}
283310
EXPORT_SYMBOL(sha256_final);
311+
312+
int sha224_final(struct sha256_state *sctx, u8 *out)
313+
{
314+
return __sha256_final(sctx, out, 7);
315+
}
316+
EXPORT_SYMBOL(sha224_final);

0 commit comments

Comments
 (0)