8 static void encode32(uchar*, u32int*, ulong);
9 static DigestState* sha2_64(uchar *, ulong, uchar *, SHA2_256state *, int);
11 extern void _sha2block64(uchar*, ulong, u32int*);
14 * for sha2_224 and sha2_256, len must be multiple of 64 for all but
15 * the last call. There must be room in the input buffer to pad.
17 * Note: sha2_224 calls sha2_256block as sha2_224, just uses different
18 * initial seed and produces a 224b hash result. otherwise it's
19 * the same as sha2_256.
23 sha2_224(uchar *p, ulong len, uchar *digest, SHA2_224state *s)
26 s = mallocz(sizeof(*s), 1);
33 * seed the state with the first 32 bits of the fractional
34 * parts of the square roots of the first 8 primes 2..19).
36 s->state[0] = 0xc1059ed8;
37 s->state[1] = 0x367cd507;
38 s->state[2] = 0x3070dd17;
39 s->state[3] = 0xf70e5939;
40 s->state[4] = 0xffc00b31;
41 s->state[5] = 0x68581511;
42 s->state[6] = 0x64f98fa7;
43 s->state[7] = 0xbefa4fa4;
46 return sha2_64(p, len, digest, s, SHA2_224dlen);
50 sha2_256(uchar *p, ulong len, uchar *digest, SHA2_256state *s)
53 s = mallocz(sizeof(*s), 1);
60 * seed the state with the first 32 bits of the fractional
61 * parts of the square roots of the first 8 primes 2..19).
63 s->state[0] = 0x6a09e667;
64 s->state[1] = 0xbb67ae85;
65 s->state[2] = 0x3c6ef372;
66 s->state[3] = 0xa54ff53a;
67 s->state[4] = 0x510e527f;
68 s->state[5] = 0x9b05688c;
69 s->state[6] = 0x1f83d9ab;
70 s->state[7] = 0x5be0cd19;
73 return sha2_64(p, len, digest, s, SHA2_256dlen);
76 /* common 64 byte block padding and count code for SHA2_224 and SHA2_256 */
78 sha2_64(uchar *p, ulong len, uchar *digest, SHA2_256state *s, int dlen)
85 /* fill out the partial 64 byte block from previous calls */
90 memmove(s->buf + s->blen, p, i);
95 _sha2block64(s->buf, s->blen, s->state);
101 /* do 64 byte blocks */
104 _sha2block64(p, i, s->state);
110 /* save the left overs if not last call */
113 memmove(s->buf, p, len);
120 * this is the last time through, pad what's left with 0x80,
121 * 0's, and the input count to create a multiple of 64 bytes.
127 memmove(buf, p, len);
140 /* append the count */
143 encode32(p+len, x, 8);
145 /* digest the last part */
146 _sha2block64(p, len+8, s->state);
149 /* return result and free state */
150 encode32(digest, s->state, dlen);
157 * Encodes input (ulong) into output (uchar).
158 * Assumes len is a multiple of 4.
161 encode32(uchar *output, u32int *input, ulong len)
166 for(e = output + len; output < e;) {
176 hmac_sha2_224(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest,
179 return hmac_x(p, len, key, klen, digest, s, sha2_224, SHA2_224dlen);
183 hmac_sha2_256(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest,
186 return hmac_x(p, len, key, klen, digest, s, sha2_256, SHA2_256dlen);