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 ;-) */ |
143
|
31 int sha224_init(hash_state * md) |
3
|
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; |
143
|
45 return CRYPT_OK; |
3
|
46 } |
|
47 |
|
48 int sha224_done(hash_state * md, unsigned char *hash) |
|
49 { |
|
50 unsigned char buf[32]; |
|
51 int err; |
|
52 |
|
53 err = sha256_done(md, buf); |
143
|
54 XMEMCPY(hash, buf, 28); |
3
|
55 #ifdef CLEAN_STACK |
|
56 zeromem(buf, sizeof(buf)); |
|
57 #endif |
|
58 return err; |
|
59 } |
|
60 |
|
61 int sha224_test(void) |
|
62 { |
|
63 #ifndef LTC_TEST |
|
64 return CRYPT_NOP; |
|
65 #else |
|
66 static const struct { |
|
67 char *msg; |
|
68 unsigned char hash[28]; |
|
69 } tests[] = { |
|
70 { "abc", |
|
71 { 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, |
|
72 0x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, |
|
73 0x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd, |
|
74 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 } |
|
75 }, |
|
76 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
|
77 { 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, |
|
78 0xcc, 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89, |
|
79 0x01, 0x50, 0xb0, 0xc6, 0x45, 0x5c, 0xb4, |
|
80 0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25, 0x25 } |
|
81 }, |
|
82 }; |
|
83 |
|
84 int i; |
|
85 unsigned char tmp[28]; |
|
86 hash_state md; |
|
87 |
|
88 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { |
|
89 sha224_init(&md); |
|
90 sha224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); |
|
91 sha224_done(&md, tmp); |
|
92 if (memcmp(tmp, tests[i].hash, 28) != 0) { |
|
93 return CRYPT_FAIL_TESTVECTOR; |
|
94 } |
|
95 } |
|
96 return CRYPT_OK; |
|
97 #endif |
|
98 } |
|
99 |