comparison libtomcrypt/src/hashes/sha2/sha384.c @ 302:973fccb59ea4 ucc-axis-hack

propagate from branch 'au.asn.ucc.matt.dropbear' (head 11034278bd1917bebcbdc69cf53b1891ce9db121) to branch 'au.asn.ucc.matt.dropbear.ucc-axis-hack' (head 10a1f614fec73d0820c3f61160d9db409b9beb46)
author Matt Johnston <matt@ucc.asn.au>
date Sat, 25 Mar 2006 12:59:58 +0000
parents 1b9e69c058d2
children 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
299:740e782679be 302:973fccb59ea4
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 @param sha384.c
13 SHA384 hash included in sha512.c, Tom St Denis
14 */
15
16 const struct ltc_hash_descriptor sha384_desc =
17 {
18 "sha384",
19 4,
20 48,
21 128,
22
23 /* OID */
24 { 2, 16, 840, 1, 101, 3, 4, 2, 2, },
25 9,
26
27 &sha384_init,
28 &sha512_process,
29 &sha384_done,
30 &sha384_test
31 };
32
33 /**
34 Initialize the hash state
35 @param md The hash state you wish to initialize
36 @return CRYPT_OK if successful
37 */
38 int sha384_init(hash_state * md)
39 {
40 LTC_ARGCHK(md != NULL);
41
42 md->sha512.curlen = 0;
43 md->sha512.length = 0;
44 md->sha512.state[0] = CONST64(0xcbbb9d5dc1059ed8);
45 md->sha512.state[1] = CONST64(0x629a292a367cd507);
46 md->sha512.state[2] = CONST64(0x9159015a3070dd17);
47 md->sha512.state[3] = CONST64(0x152fecd8f70e5939);
48 md->sha512.state[4] = CONST64(0x67332667ffc00b31);
49 md->sha512.state[5] = CONST64(0x8eb44a8768581511);
50 md->sha512.state[6] = CONST64(0xdb0c2e0d64f98fa7);
51 md->sha512.state[7] = CONST64(0x47b5481dbefa4fa4);
52 return CRYPT_OK;
53 }
54
55 /**
56 Terminate the hash to get the digest
57 @param md The hash state
58 @param out [out] The destination of the hash (48 bytes)
59 @return CRYPT_OK if successful
60 */
61 int sha384_done(hash_state * md, unsigned char *out)
62 {
63 unsigned char buf[64];
64
65 LTC_ARGCHK(md != NULL);
66 LTC_ARGCHK(out != NULL);
67
68 if (md->sha512.curlen >= sizeof(md->sha512.buf)) {
69 return CRYPT_INVALID_ARG;
70 }
71
72 sha512_done(md, buf);
73 XMEMCPY(out, buf, 48);
74 #ifdef LTC_CLEAN_STACK
75 zeromem(buf, sizeof(buf));
76 #endif
77 return CRYPT_OK;
78 }
79
80 /**
81 Self-test the hash
82 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
83 */
84 int sha384_test(void)
85 {
86 #ifndef LTC_TEST
87 return CRYPT_NOP;
88 #else
89 static const struct {
90 char *msg;
91 unsigned char hash[48];
92 } tests[] = {
93 { "abc",
94 { 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
95 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
96 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
97 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
98 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
99 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7 }
100 },
101 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
102 { 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8,
103 0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47,
104 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,
105 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12,
106 0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9,
107 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39 }
108 },
109 };
110
111 int i;
112 unsigned char tmp[48];
113 hash_state md;
114
115 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
116 sha384_init(&md);
117 sha384_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
118 sha384_done(&md, tmp);
119 if (memcmp(tmp, tests[i].hash, 48) != 0) {
120 return CRYPT_FAIL_TESTVECTOR;
121 }
122 }
123 return CRYPT_OK;
124 #endif
125 }
126
127
128
129
130
131
132 /* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha2/sha384.c,v $ */
133 /* $Revision: 1.5 $ */
134 /* $Date: 2005/05/23 02:42:07 $ */