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