1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 /**
4 @param sha224.c
5 LTC_SHA-224 new NIST standard based off of LTC_SHA-256 truncated to 224 bits (Tom St Denis)
6 */
7
8 #include "tomcrypt_private.h"
9
10 #if defined(LTC_SHA224) && defined(LTC_SHA256)
11
12 const struct ltc_hash_descriptor sha224_desc =
13 {
14 "sha224",
15 10,
16 28,
17 64,
18
19 /* OID */
20 { 2, 16, 840, 1, 101, 3, 4, 2, 4, },
21 9,
22
23 &sha224_init,
24 &sha256_process,
25 &sha224_done,
26 &sha224_test,
27 NULL
28 };
29
30 /* init the sha256 er... sha224 state ;-) */
31 /**
32 Initialize the hash state
33 @param md The hash state you wish to initialize
34 @return CRYPT_OK if successful
35 */
sha224_init(hash_state * md)36 int sha224_init(hash_state * md)
37 {
38 LTC_ARGCHK(md != NULL);
39
40 md->sha256.curlen = 0;
41 md->sha256.length = 0;
42 md->sha256.state[0] = 0xc1059ed8UL;
43 md->sha256.state[1] = 0x367cd507UL;
44 md->sha256.state[2] = 0x3070dd17UL;
45 md->sha256.state[3] = 0xf70e5939UL;
46 md->sha256.state[4] = 0xffc00b31UL;
47 md->sha256.state[5] = 0x68581511UL;
48 md->sha256.state[6] = 0x64f98fa7UL;
49 md->sha256.state[7] = 0xbefa4fa4UL;
50 return CRYPT_OK;
51 }
52
53 /**
54 Terminate the hash to get the digest
55 @param md The hash state
56 @param out [out] The destination of the hash (28 bytes)
57 @return CRYPT_OK if successful
58 */
sha224_done(hash_state * md,unsigned char * out)59 int sha224_done(hash_state * md, unsigned char *out)
60 {
61 unsigned char buf[32];
62 int err;
63
64 LTC_ARGCHK(md != NULL);
65 LTC_ARGCHK(out != NULL);
66
67 err = sha256_done(md, buf);
68 XMEMCPY(out, buf, 28);
69 #ifdef LTC_CLEAN_STACK
70 zeromem(buf, sizeof(buf));
71 #endif
72 return err;
73 }
74
75 /**
76 Self-test the hash
77 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
78 */
sha224_test(void)79 int sha224_test(void)
80 {
81 #ifndef LTC_TEST
82 return CRYPT_NOP;
83 #else
84 static const struct {
85 const char *msg;
86 unsigned char hash[28];
87 } tests[] = {
88 { "abc",
89 { 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8,
90 0x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2,
91 0x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd,
92 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 }
93 },
94 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
95 { 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76,
96 0xcc, 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89,
97 0x01, 0x50, 0xb0, 0xc6, 0x45, 0x5c, 0xb4,
98 0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25, 0x25 }
99 },
100 };
101
102 int i;
103 unsigned char tmp[28];
104 hash_state md;
105
106 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
107 sha224_init(&md);
108 sha224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
109 sha224_done(&md, tmp);
110 if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA224", i)) {
111 return CRYPT_FAIL_TESTVECTOR;
112 }
113 }
114 return CRYPT_OK;
115 #endif
116 }
117
118 #endif /* defined(LTC_SHA224) && defined(LTC_SHA256) */
119
120