1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 /**
11 @param sha384.c
12 LTC_SHA384 hash included in sha512.c, Tom St Denis
13 */
14
15 #include "tomcrypt_private.h"
16
17 #if defined(LTC_SHA384) && defined(LTC_SHA512)
18
19 const struct ltc_hash_descriptor sha384_desc =
20 {
21 "sha384",
22 4,
23 48,
24 128,
25
26 /* OID */
27 { 2, 16, 840, 1, 101, 3, 4, 2, 2, },
28 9,
29
30 &sha384_init,
31 &sha512_process,
32 &sha384_done,
33 &sha384_test,
34 NULL
35 };
36
37 /**
38 Initialize the hash state
39 @param md The hash state you wish to initialize
40 @return CRYPT_OK if successful
41 */
sha384_init(hash_state * md)42 int sha384_init(hash_state * md)
43 {
44 LTC_ARGCHK(md != NULL);
45
46 md->sha512.curlen = 0;
47 md->sha512.length = 0;
48 md->sha512.state[0] = CONST64(0xcbbb9d5dc1059ed8);
49 md->sha512.state[1] = CONST64(0x629a292a367cd507);
50 md->sha512.state[2] = CONST64(0x9159015a3070dd17);
51 md->sha512.state[3] = CONST64(0x152fecd8f70e5939);
52 md->sha512.state[4] = CONST64(0x67332667ffc00b31);
53 md->sha512.state[5] = CONST64(0x8eb44a8768581511);
54 md->sha512.state[6] = CONST64(0xdb0c2e0d64f98fa7);
55 md->sha512.state[7] = CONST64(0x47b5481dbefa4fa4);
56 return CRYPT_OK;
57 }
58
59 /**
60 Terminate the hash to get the digest
61 @param md The hash state
62 @param out [out] The destination of the hash (48 bytes)
63 @return CRYPT_OK if successful
64 */
sha384_done(hash_state * md,unsigned char * out)65 int sha384_done(hash_state * md, unsigned char *out)
66 {
67 unsigned char buf[64];
68
69 LTC_ARGCHK(md != NULL);
70 LTC_ARGCHK(out != NULL);
71
72 if (md->sha512.curlen >= sizeof(md->sha512.buf)) {
73 return CRYPT_INVALID_ARG;
74 }
75
76 sha512_done(md, buf);
77 XMEMCPY(out, buf, 48);
78 #ifdef LTC_CLEAN_STACK
79 zeromem(buf, sizeof(buf));
80 #endif
81 return CRYPT_OK;
82 }
83
84 /**
85 Self-test the hash
86 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
87 */
sha384_test(void)88 int sha384_test(void)
89 {
90 #ifndef LTC_TEST
91 return CRYPT_NOP;
92 #else
93 static const struct {
94 const char *msg;
95 unsigned char hash[48];
96 } tests[] = {
97 { "abc",
98 { 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
99 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
100 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
101 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
102 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
103 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7 }
104 },
105 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
106 { 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8,
107 0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47,
108 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,
109 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12,
110 0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9,
111 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39 }
112 },
113 };
114
115 int i;
116 unsigned char tmp[48];
117 hash_state md;
118
119 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
120 sha384_init(&md);
121 sha384_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
122 sha384_done(&md, tmp);
123 if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA384", i)) {
124 return CRYPT_FAIL_TESTVECTOR;
125 }
126 }
127 return CRYPT_OK;
128 #endif
129 }
130
131 #endif /* defined(LTC_SHA384) && defined(LTC_SHA512) */
132
133 /* ref: $Format:%D$ */
134 /* git commit: $Format:%H$ */
135 /* commit time: $Format:%ai$ */
136