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