Skip to content

Commit cd12fb9

Browse files
Jonathan Lynchherbertx
authored andcommitted
[CRYPTO] sha256-generic: Extend sha256_generic.c to support SHA-224
Resubmitting this patch which extends sha256_generic.c to support SHA-224 as described in FIPS 180-2 and RFC 3874. HMAC-SHA-224 as described in RFC4231 is then supported through the hmac interface. Patch includes test vectors for SHA-224 and HMAC-SHA-224. SHA-224 chould be chosen as a hash algorithm when 112 bits of security strength is required. Patch generated against the 2.6.24-rc1 kernel and tested against 2.6.24-rc1-git14 which includes fix for scatter gather implementation for HMAC. Signed-off-by: Jonathan Lynch <jonathan.lynch@intel.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent cd7c3bf commit cd12fb9

5 files changed

Lines changed: 241 additions & 12 deletions

File tree

‎crypto/Kconfig‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,17 @@ config CRYPTO_SHA1
9191
SHA-1 secure hash standard (FIPS 180-1/DFIPS 180-2).
9292

9393
config CRYPTO_SHA256
94-
tristate "SHA256 digest algorithm"
94+
tristate "SHA224 and SHA256 digest algorithm"
9595
select CRYPTO_ALGAPI
9696
help
9797
SHA256 secure hash standard (DFIPS 180-2).
9898

9999
This version of SHA implements a 256 bit hash with 128 bits of
100100
security against collision attacks.
101101

102+
This code also includes SHA-224, a 224 bit hash with 112 bits
103+
of security against collision attacks.
104+
102105
config CRYPTO_SHA512
103106
tristate "SHA384 and SHA512 digest algorithms"
104107
select CRYPTO_ALGAPI

‎crypto/sha256_generic.c‎

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
1010
* Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
1111
* Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
12+
* SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
1213
*
1314
* This program is free software; you can redistribute it and/or modify it
1415
* under the terms of the GNU General Public License as published by the Free
@@ -218,6 +219,22 @@ static void sha256_transform(u32 *state, const u8 *input)
218219
memset(W, 0, 64 * sizeof(u32));
219220
}
220221

222+
223+
static void sha224_init(struct crypto_tfm *tfm)
224+
{
225+
struct sha256_ctx *sctx = crypto_tfm_ctx(tfm);
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] = 0;
235+
sctx->count[1] = 0;
236+
}
237+
221238
static void sha256_init(struct crypto_tfm *tfm)
222239
{
223240
struct sha256_ctx *sctx = crypto_tfm_ctx(tfm);
@@ -294,37 +311,76 @@ static void sha256_final(struct crypto_tfm *tfm, u8 *out)
294311
memset(sctx, 0, sizeof(*sctx));
295312
}
296313

314+
static void sha224_final(struct crypto_tfm *tfm, u8 *hash)
315+
{
316+
u8 D[SHA256_DIGEST_SIZE];
317+
318+
sha256_final(tfm, D);
319+
320+
memcpy(hash, D, SHA224_DIGEST_SIZE);
321+
memset(D, 0, SHA256_DIGEST_SIZE);
322+
}
297323

298-
static struct crypto_alg alg = {
324+
static struct crypto_alg sha256 = {
299325
.cra_name = "sha256",
300326
.cra_driver_name= "sha256-generic",
301327
.cra_flags = CRYPTO_ALG_TYPE_DIGEST,
302328
.cra_blocksize = SHA256_BLOCK_SIZE,
303329
.cra_ctxsize = sizeof(struct sha256_ctx),
304330
.cra_module = THIS_MODULE,
305331
.cra_alignmask = 3,
306-
.cra_list = LIST_HEAD_INIT(alg.cra_list),
332+
.cra_list = LIST_HEAD_INIT(sha256.cra_list),
307333
.cra_u = { .digest = {
308334
.dia_digestsize = SHA256_DIGEST_SIZE,
309-
.dia_init = sha256_init,
310-
.dia_update = sha256_update,
311-
.dia_final = sha256_final } }
335+
.dia_init = sha256_init,
336+
.dia_update = sha256_update,
337+
.dia_final = sha256_final } }
338+
};
339+
340+
static struct crypto_alg sha224 = {
341+
.cra_name = "sha224",
342+
.cra_driver_name = "sha224-generic",
343+
.cra_flags = CRYPTO_ALG_TYPE_DIGEST,
344+
.cra_blocksize = SHA224_BLOCK_SIZE,
345+
.cra_ctxsize = sizeof(struct sha256_ctx),
346+
.cra_module = THIS_MODULE,
347+
.cra_alignmask = 3,
348+
.cra_list = LIST_HEAD_INIT(sha224.cra_list),
349+
.cra_u = { .digest = {
350+
.dia_digestsize = SHA224_DIGEST_SIZE,
351+
.dia_init = sha224_init,
352+
.dia_update = sha256_update,
353+
.dia_final = sha224_final } }
312354
};
313355

314356
static int __init init(void)
315357
{
316-
return crypto_register_alg(&alg);
358+
int ret = 0;
359+
360+
ret = crypto_register_alg(&sha224);
361+
362+
if (ret < 0)
363+
return ret;
364+
365+
ret = crypto_register_alg(&sha256);
366+
367+
if (ret < 0)
368+
crypto_unregister_alg(&sha224);
369+
370+
return ret;
317371
}
318372

319373
static void __exit fini(void)
320374
{
321-
crypto_unregister_alg(&alg);
375+
crypto_unregister_alg(&sha224);
376+
crypto_unregister_alg(&sha256);
322377
}
323378

324379
module_init(init);
325380
module_exit(fini);
326381

327382
MODULE_LICENSE("GPL");
328-
MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm");
383+
MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm");
329384

385+
MODULE_ALIAS("sha224");
330386
MODULE_ALIAS("sha256");

‎crypto/tcrypt.c‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Software Foundation; either version 2 of the License, or (at your option)
1313
* any later version.
1414
*
15+
* 2007-11-06 Added SHA-224 and SHA-224-HMAC tests
1516
* 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
1617
* 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
1718
* 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
@@ -74,8 +75,9 @@ static char *xbuf;
7475
static char *tvmem;
7576

7677
static char *check[] = {
77-
"des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
78-
"twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
78+
"des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
79+
"blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
80+
"cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
7981
"arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
8082
"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
8183
"camellia", "seed", NULL
@@ -918,6 +920,8 @@ static void do_test(void)
918920

919921
test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
920922

923+
test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
924+
921925
test_hash("sha256", sha256_tv_template, SHA256_TEST_VECTORS);
922926

923927
//BLOWFISH
@@ -1067,6 +1071,8 @@ static void do_test(void)
10671071
HMAC_MD5_TEST_VECTORS);
10681072
test_hash("hmac(sha1)", hmac_sha1_tv_template,
10691073
HMAC_SHA1_TEST_VECTORS);
1074+
test_hash("hmac(sha224)", hmac_sha224_tv_template,
1075+
HMAC_SHA224_TEST_VECTORS);
10701076
test_hash("hmac(sha256)", hmac_sha256_tv_template,
10711077
HMAC_SHA256_TEST_VECTORS);
10721078
test_hash("hmac(sha384)", hmac_sha384_tv_template,
@@ -1299,6 +1305,9 @@ static void do_test(void)
12991305
camellia_cbc_dec_tv_template,
13001306
CAMELLIA_CBC_DEC_TEST_VECTORS);
13011307
break;
1308+
case 33:
1309+
test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
1310+
break;
13021311

13031312
case 100:
13041313
test_hash("hmac(md5)", hmac_md5_tv_template,
@@ -1324,7 +1333,10 @@ static void do_test(void)
13241333
test_hash("hmac(sha512)", hmac_sha512_tv_template,
13251334
HMAC_SHA512_TEST_VECTORS);
13261335
break;
1327-
1336+
case 105:
1337+
test_hash("hmac(sha224)", hmac_sha224_tv_template,
1338+
HMAC_SHA224_TEST_VECTORS);
1339+
break;
13281340

13291341
case 200:
13301342
test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
@@ -1459,6 +1471,10 @@ static void do_test(void)
14591471
test_hash_speed("tgr192", sec, generic_hash_speed_template);
14601472
if (mode > 300 && mode < 400) break;
14611473

1474+
case 313:
1475+
test_hash_speed("sha224", sec, generic_hash_speed_template);
1476+
if (mode > 300 && mode < 400) break;
1477+
14621478
case 399:
14631479
break;
14641480

‎crypto/tcrypt.h‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,33 @@ static struct hash_testvec sha1_tv_template[] = {
173173
}
174174
};
175175

176+
177+
/*
178+
* SHA224 test vectors from from FIPS PUB 180-2
179+
*/
180+
#define SHA224_TEST_VECTORS 2
181+
182+
static struct hash_testvec sha224_tv_template[] = {
183+
{
184+
.plaintext = "abc",
185+
.psize = 3,
186+
.digest = { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22,
187+
0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3,
188+
0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7,
189+
0xE3, 0x6C, 0x9D, 0xA7},
190+
}, {
191+
.plaintext =
192+
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
193+
.psize = 56,
194+
.digest = { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC,
195+
0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50,
196+
0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19,
197+
0x52, 0x52, 0x25, 0x25 },
198+
.np = 2,
199+
.tap = { 28, 28 }
200+
}
201+
};
202+
176203
/*
177204
* SHA256 test vectors from from NIST
178205
*/
@@ -817,6 +844,121 @@ static struct hash_testvec hmac_sha1_tv_template[] = {
817844
},
818845
};
819846

847+
848+
/*
849+
* SHA224 HMAC test vectors from RFC4231
850+
*/
851+
#define HMAC_SHA224_TEST_VECTORS 4
852+
853+
static struct hash_testvec hmac_sha224_tv_template[] = {
854+
{
855+
.key = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
856+
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
857+
0x0b, 0x0b, 0x0b, 0x0b },
858+
.ksize = 20,
859+
/* ("Hi There") */
860+
.plaintext = { 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 },
861+
.psize = 8,
862+
.digest = { 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
863+
0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
864+
0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
865+
0x53, 0x68, 0x4b, 0x22},
866+
}, {
867+
.key = { 0x4a, 0x65, 0x66, 0x65 }, /* ("Jefe") */
868+
.ksize = 4,
869+
/* ("what do ya want for nothing?") */
870+
.plaintext = { 0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20,
871+
0x79, 0x61, 0x20, 0x77, 0x61, 0x6e, 0x74, 0x20,
872+
0x66, 0x6f, 0x72, 0x20, 0x6e, 0x6f, 0x74, 0x68,
873+
0x69, 0x6e, 0x67, 0x3f },
874+
.psize = 28,
875+
.digest = { 0xa3, 0x0e, 0x01, 0x09, 0x8b, 0xc6, 0xdb, 0xbf,
876+
0x45, 0x69, 0x0f, 0x3a, 0x7e, 0x9e, 0x6d, 0x0f,
877+
0x8b, 0xbe, 0xa2, 0xa3, 0x9e, 0x61, 0x48, 0x00,
878+
0x8f, 0xd0, 0x5e, 0x44 },
879+
.np = 4,
880+
.tap = { 7, 7, 7, 7 }
881+
}, {
882+
.key = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
883+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
884+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
885+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
886+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
887+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
888+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
889+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
890+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
891+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
892+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
893+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
894+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
895+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
896+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
897+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
898+
0xaa, 0xaa, 0xaa },
899+
.ksize = 131,
900+
/* ("Test Using Larger Than Block-Size Key - Hash Key First") */
901+
.plaintext = { 0x54, 0x65, 0x73, 0x74, 0x20, 0x55, 0x73, 0x69,
902+
0x6e, 0x67, 0x20, 0x4c, 0x61, 0x72, 0x67, 0x65,
903+
0x72, 0x20, 0x54, 0x68, 0x61, 0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0xaa, 0xaa, 0xaa, 0xaa,
927+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
928+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
929+
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
930+
0xaa, 0xaa, 0xaa },
931+
.ksize = 131,
932+
/* ("This is a test using a larger than block-size key and a")
933+
(" larger than block-size data. The key needs to be")
934+
(" hashed before being used by the HMAC algorithm.") */
935+
.plaintext = { 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
936+
0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x75,
937+
0x73, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x6c,
938+
0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x74, 0x68,
939+
0x61, 0x6e, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b,
940+
0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6b, 0x65,
941+
0x79, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x61, 0x20,
942+
0x6c, 0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x74,
943+
0x68, 0x61, 0x6e, 0x20, 0x62, 0x6c, 0x6f, 0x63,
944+
0x6b, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x64,
945+
0x61, 0x74, 0x61, 0x2e, 0x20, 0x54, 0x68, 0x65,
946+
0x20, 0x6b, 0x65, 0x79, 0x20, 0x6e, 0x65, 0x65,
947+
0x64, 0x73, 0x20, 0x74, 0x6f, 0x20, 0x62, 0x65,
948+
0x20, 0x68, 0x61, 0x73, 0x68, 0x65, 0x64, 0x20,
949+
0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x20, 0x62,
950+
0x65, 0x69, 0x6e, 0x67, 0x20, 0x75, 0x73, 0x65,
951+
0x64, 0x20, 0x62, 0x79, 0x20, 0x74, 0x68, 0x65,
952+
0x20, 0x48, 0x4d, 0x41, 0x43, 0x20, 0x61, 0x6c,
953+
0x67, 0x6f, 0x72, 0x69, 0x74, 0x68, 0x6d, 0x2e },
954+
.psize = 152,
955+
.digest = { 0x3a, 0x85, 0x41, 0x66, 0xac, 0x5d, 0x9f, 0x02,
956+
0x3f, 0x54, 0xd5, 0x17, 0xd0, 0xb3, 0x9d, 0xbd,
957+
0x94, 0x67, 0x70, 0xdb, 0x9c, 0x2b, 0x95, 0xc9,
958+
0xf6, 0xf5, 0x65, 0xd1 },
959+
},
960+
};
961+
820962
/*
821963
* HMAC-SHA256 test vectors from
822964
* draft-ietf-ipsec-ciph-sha-256-01.txt

‎include/crypto/sha.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#define SHA1_DIGEST_SIZE 20
99
#define SHA1_BLOCK_SIZE 64
1010

11+
#define SHA224_DIGEST_SIZE 28
12+
#define SHA224_BLOCK_SIZE 64
13+
1114
#define SHA256_DIGEST_SIZE 32
1215
#define SHA256_BLOCK_SIZE 64
1316

@@ -23,6 +26,15 @@
2326
#define SHA1_H3 0x10325476UL
2427
#define SHA1_H4 0xc3d2e1f0UL
2528

29+
#define SHA224_H0 0xc1059ed8UL
30+
#define SHA224_H1 0x367cd507UL
31+
#define SHA224_H2 0x3070dd17UL
32+
#define SHA224_H3 0xf70e5939UL
33+
#define SHA224_H4 0xffc00b31UL
34+
#define SHA224_H5 0x68581511UL
35+
#define SHA224_H6 0x64f98fa7UL
36+
#define SHA224_H7 0xbefa4fa4UL
37+
2638
#define SHA256_H0 0x6a09e667UL
2739
#define SHA256_H1 0xbb67ae85UL
2840
#define SHA256_H2 0x3c6ef372UL

0 commit comments

Comments
 (0)