8 static void encode64(uchar*, u64int*, ulong);
9 static DigestState* sha2_128(uchar *, ulong, uchar *, SHA2_256state *, int);
11 extern void _sha2block128(uchar*, ulong, u64int*);
14 * for sha2_384 and sha2_512, len must be multiple of 128 for all but
15 * the last call. There must be room in the input buffer to pad.
17 * Note: sha2_384 calls sha2_512block as sha2_384; it just uses a different
18 * initial seed to produce a truncated 384b hash result. otherwise
19 * it's the same as sha2_512.
22 sha2_384(uchar *p, ulong len, uchar *digest, SHA2_384state *s)
25 s = mallocz(sizeof(*s), 1);
32 * seed the state with the first 64 bits of the fractional
33 * parts of the square roots of the 9th thru 16th primes.
35 s->bstate[0] = 0xcbbb9d5dc1059ed8LL;
36 s->bstate[1] = 0x629a292a367cd507LL;
37 s->bstate[2] = 0x9159015a3070dd17LL;
38 s->bstate[3] = 0x152fecd8f70e5939LL;
39 s->bstate[4] = 0x67332667ffc00b31LL;
40 s->bstate[5] = 0x8eb44a8768581511LL;
41 s->bstate[6] = 0xdb0c2e0d64f98fa7LL;
42 s->bstate[7] = 0x47b5481dbefa4fa4LL;
45 return sha2_128(p, len, digest, s, SHA2_384dlen);
49 sha2_512(uchar *p, ulong len, uchar *digest, SHA2_512state *s)
53 s = mallocz(sizeof(*s), 1);
60 * seed the state with the first 64 bits of the fractional
61 * parts of the square roots of the first 8 primes 2..19).
63 s->bstate[0] = 0x6a09e667f3bcc908LL;
64 s->bstate[1] = 0xbb67ae8584caa73bLL;
65 s->bstate[2] = 0x3c6ef372fe94f82bLL;
66 s->bstate[3] = 0xa54ff53a5f1d36f1LL;
67 s->bstate[4] = 0x510e527fade682d1LL;
68 s->bstate[5] = 0x9b05688c2b3e6c1fLL;
69 s->bstate[6] = 0x1f83d9abfb41bd6bLL;
70 s->bstate[7] = 0x5be0cd19137e2179LL;
73 return sha2_128(p, len, digest, s, SHA2_512dlen);
76 /* common 128 byte block padding and count code for SHA2_384 and SHA2_512 */
78 sha2_128(uchar *p, ulong len, uchar *digest, SHA2_512state *s, int dlen)
85 /* fill out the partial 128 byte block from previous calls */
90 memmove(s->buf + s->blen, p, i);
95 _sha2block128(s->buf, s->blen, s->bstate);
101 /* do 128 byte blocks */
104 _sha2block128(p, i, s->bstate);
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 128 bytes.
127 memmove(buf, p, len);
140 /* append the count */
141 x[0] = 0; /* assume 32b length, i.e. < 4GB */
143 encode64(p+len, x, 16);
145 /* digest the last part */
146 _sha2block128(p, len+16, s->bstate);
149 /* return result and free state */
150 encode64(digest, s->bstate, dlen);
157 * Encodes input (ulong long) into output (uchar).
158 * Assumes len is a multiple of 8.
161 encode64(uchar *output, u64int *input, ulong len)
166 for(e = output + len; output < e;) {
180 hmac_sha2_384(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest,
183 return hmac_x(p, len, key, klen, digest, s, sha2_384, SHA2_384dlen);
187 hmac_sha2_512(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest,
190 return hmac_x(p, len, key, klen, digest, s, sha2_512, SHA2_512dlen);