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 sha512_224.c
12 SHA512/224 hash included in sha512.c
13 */
14
15 #include "tomcrypt_private.h"
16
17 #if defined(LTC_SHA512_224) && defined(LTC_SHA512)
18
19 const struct ltc_hash_descriptor sha512_224_desc =
20 {
21 "sha512-224",
22 15,
23 28,
24 128,
25
26 /* OID */
27 { 2, 16, 840, 1, 101, 3, 4, 2, 5, },
28 9,
29
30 &sha512_224_init,
31 &sha512_process,
32 &sha512_224_done,
33 &sha512_224_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 */
sha512_224_init(hash_state * md)42 int sha512_224_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(0x8C3D37C819544DA2);
49 md->sha512.state[1] = CONST64(0x73E1996689DCD4D6);
50 md->sha512.state[2] = CONST64(0x1DFAB7AE32FF9C82);
51 md->sha512.state[3] = CONST64(0x679DD514582F9FCF);
52 md->sha512.state[4] = CONST64(0x0F6D2B697BD44DA8);
53 md->sha512.state[5] = CONST64(0x77E36F7304C48942);
54 md->sha512.state[6] = CONST64(0x3F9D85A86A1D36C8);
55 md->sha512.state[7] = CONST64(0x1112E6AD91D692A1);
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 */
sha512_224_done(hash_state * md,unsigned char * out)65 int sha512_224_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, 28);
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 */
sha512_224_test(void)88 int sha512_224_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[28];
96 } tests[] = {
97 { "abc",
98 { 0x46, 0x34, 0x27, 0x0F, 0x70, 0x7B, 0x6A, 0x54,
99 0xDA, 0xAE, 0x75, 0x30, 0x46, 0x08, 0x42, 0xE2,
100 0x0E, 0x37, 0xED, 0x26, 0x5C, 0xEE, 0xE9, 0xA4,
101 0x3E, 0x89, 0x24, 0xAA }
102 },
103 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
104 { 0x23, 0xFE, 0xC5, 0xBB, 0x94, 0xD6, 0x0B, 0x23,
105 0x30, 0x81, 0x92, 0x64, 0x0B, 0x0C, 0x45, 0x33,
106 0x35, 0xD6, 0x64, 0x73, 0x4F, 0xE4, 0x0E, 0x72,
107 0x68, 0x67, 0x4A, 0xF9 }
108 },
109 };
110
111 int i;
112 unsigned char tmp[28];
113 hash_state md;
114
115 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
116 sha512_224_init(&md);
117 sha512_224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
118 sha512_224_done(&md, tmp);
119 if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA512-224", i)) {
120 return CRYPT_FAIL_TESTVECTOR;
121 }
122 }
123 return CRYPT_OK;
124 #endif
125 }
126
127 #endif /* defined(LTC_SHA384) && defined(LTC_SHA512) */
128
129 /* ref: $Format:%D$ */
130 /* git commit: $Format:%H$ */
131 /* commit time: $Format:%ai$ */
132