3
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 |
|
12 /* SHA-224 new NIST standard based off of SHA-256 truncated to 224 bits */ |
|
13 const struct _hash_descriptor sha224_desc = |
|
14 { |
|
15 "sha224", |
|
16 10, |
|
17 28, |
|
18 64, |
15
|
19 |
|
20 /* DER identifier (not supported) */ |
|
21 { 0x00 }, |
|
22 0, |
|
23 |
3
|
24 &sha224_init, |
|
25 &sha256_process, |
|
26 &sha224_done, |
|
27 &sha224_test |
|
28 }; |
|
29 |
|
30 /* init the sha256 er... sha224 state ;-) */ |
|
31 void sha224_init(hash_state * md) |
|
32 { |
|
33 _ARGCHK(md != NULL); |
|
34 |
|
35 md->sha256.curlen = 0; |
|
36 md->sha256.length = 0; |
|
37 md->sha256.state[0] = 0xc1059ed8UL; |
|
38 md->sha256.state[1] = 0x367cd507UL; |
|
39 md->sha256.state[2] = 0x3070dd17UL; |
|
40 md->sha256.state[3] = 0xf70e5939UL; |
|
41 md->sha256.state[4] = 0xffc00b31UL; |
|
42 md->sha256.state[5] = 0x68581511UL; |
|
43 md->sha256.state[6] = 0x64f98fa7UL; |
|
44 md->sha256.state[7] = 0xbefa4fa4UL; |
|
45 } |
|
46 |
|
47 int sha224_done(hash_state * md, unsigned char *hash) |
|
48 { |
|
49 unsigned char buf[32]; |
|
50 int err; |
|
51 |
|
52 err = sha256_done(md, buf); |
|
53 memcpy(hash, buf, 28); |
|
54 #ifdef CLEAN_STACK |
|
55 zeromem(buf, sizeof(buf)); |
|
56 #endif |
|
57 return err; |
|
58 } |
|
59 |
|
60 int sha224_test(void) |
|
61 { |
|
62 #ifndef LTC_TEST |
|
63 return CRYPT_NOP; |
|
64 #else |
|
65 static const struct { |
|
66 char *msg; |
|
67 unsigned char hash[28]; |
|
68 } tests[] = { |
|
69 { "abc", |
|
70 { 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, |
|
71 0x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, |
|
72 0x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd, |
|
73 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 } |
|
74 }, |
|
75 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
|
76 { 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, |
|
77 0xcc, 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89, |
|
78 0x01, 0x50, 0xb0, 0xc6, 0x45, 0x5c, 0xb4, |
|
79 0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25, 0x25 } |
|
80 }, |
|
81 }; |
|
82 |
|
83 int i; |
|
84 unsigned char tmp[28]; |
|
85 hash_state md; |
|
86 |
|
87 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { |
|
88 sha224_init(&md); |
|
89 sha224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); |
|
90 sha224_done(&md, tmp); |
|
91 if (memcmp(tmp, tests[i].hash, 28) != 0) { |
|
92 return CRYPT_FAIL_TESTVECTOR; |
|
93 } |
|
94 } |
|
95 return CRYPT_OK; |
|
96 #endif |
|
97 } |
|
98 |