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