1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 /**
4 @param sha512_224.c
5 SHA512/224 hash included in sha512.c
6 */
7
8 #include "tomcrypt_private.h"
9
10 #if defined(LTC_SHA512_224) && defined(LTC_SHA512)
11
12 const struct ltc_hash_descriptor sha512_224_desc =
13 {
14 "sha512-224",
15 15,
16 28,
17 128,
18
19 /* OID */
20 { 2, 16, 840, 1, 101, 3, 4, 2, 5, },
21 9,
22
23 &sha512_224_init,
24 &sha512_process,
25 &sha512_224_done,
26 &sha512_224_test,
27 NULL
28 };
29
30 /**
31 Initialize the hash state
32 @param md The hash state you wish to initialize
33 @return CRYPT_OK if successful
34 */
sha512_224_init(hash_state * md)35 int sha512_224_init(hash_state * md)
36 {
37 LTC_ARGCHK(md != NULL);
38
39 md->sha512.curlen = 0;
40 md->sha512.length = 0;
41 md->sha512.state[0] = CONST64(0x8C3D37C819544DA2);
42 md->sha512.state[1] = CONST64(0x73E1996689DCD4D6);
43 md->sha512.state[2] = CONST64(0x1DFAB7AE32FF9C82);
44 md->sha512.state[3] = CONST64(0x679DD514582F9FCF);
45 md->sha512.state[4] = CONST64(0x0F6D2B697BD44DA8);
46 md->sha512.state[5] = CONST64(0x77E36F7304C48942);
47 md->sha512.state[6] = CONST64(0x3F9D85A86A1D36C8);
48 md->sha512.state[7] = CONST64(0x1112E6AD91D692A1);
49 return CRYPT_OK;
50 }
51
52 /**
53 Terminate the hash to get the digest
54 @param md The hash state
55 @param out [out] The destination of the hash (48 bytes)
56 @return CRYPT_OK if successful
57 */
sha512_224_done(hash_state * md,unsigned char * out)58 int sha512_224_done(hash_state * md, unsigned char *out)
59 {
60 unsigned char buf[64];
61
62 LTC_ARGCHK(md != NULL);
63 LTC_ARGCHK(out != NULL);
64
65 if (md->sha512.curlen >= sizeof(md->sha512.buf)) {
66 return CRYPT_INVALID_ARG;
67 }
68
69 sha512_done(md, buf);
70 XMEMCPY(out, buf, 28);
71 #ifdef LTC_CLEAN_STACK
72 zeromem(buf, sizeof(buf));
73 #endif
74 return CRYPT_OK;
75 }
76
77 /**
78 Self-test the hash
79 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
80 */
sha512_224_test(void)81 int sha512_224_test(void)
82 {
83 #ifndef LTC_TEST
84 return CRYPT_NOP;
85 #else
86 static const struct {
87 const char *msg;
88 unsigned char hash[28];
89 } tests[] = {
90 { "abc",
91 { 0x46, 0x34, 0x27, 0x0F, 0x70, 0x7B, 0x6A, 0x54,
92 0xDA, 0xAE, 0x75, 0x30, 0x46, 0x08, 0x42, 0xE2,
93 0x0E, 0x37, 0xED, 0x26, 0x5C, 0xEE, 0xE9, 0xA4,
94 0x3E, 0x89, 0x24, 0xAA }
95 },
96 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
97 { 0x23, 0xFE, 0xC5, 0xBB, 0x94, 0xD6, 0x0B, 0x23,
98 0x30, 0x81, 0x92, 0x64, 0x0B, 0x0C, 0x45, 0x33,
99 0x35, 0xD6, 0x64, 0x73, 0x4F, 0xE4, 0x0E, 0x72,
100 0x68, 0x67, 0x4A, 0xF9 }
101 },
102 };
103
104 int i;
105 unsigned char tmp[28];
106 hash_state md;
107
108 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
109 sha512_224_init(&md);
110 sha512_224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
111 sha512_224_done(&md, tmp);
112 if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA512-224", i)) {
113 return CRYPT_FAIL_TESTVECTOR;
114 }
115 }
116 return CRYPT_OK;
117 #endif
118 }
119
120 #endif /* defined(LTC_SHA384) && defined(LTC_SHA512) */
121