1 /*
2  * Copyright (C) 2006-2009 Vincent Hanquez <vincent@snarc.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include <string.h>
26 #include "cryptonite_bitfn.h"
27 #include "cryptonite_align.h"
28 #include "cryptonite_sha512.h"
29 
cryptonite_sha384_init(struct sha512_ctx * ctx)30 void cryptonite_sha384_init(struct sha512_ctx *ctx)
31 {
32 	memset(ctx, 0, sizeof(*ctx));
33 
34 	ctx->h[0] = 0xcbbb9d5dc1059ed8ULL;
35 	ctx->h[1] = 0x629a292a367cd507ULL;
36 	ctx->h[2] = 0x9159015a3070dd17ULL;
37 	ctx->h[3] = 0x152fecd8f70e5939ULL;
38 	ctx->h[4] = 0x67332667ffc00b31ULL;
39 	ctx->h[5] = 0x8eb44a8768581511ULL;
40 	ctx->h[6] = 0xdb0c2e0d64f98fa7ULL;
41 	ctx->h[7] = 0x47b5481dbefa4fa4ULL;
42 }
43 
cryptonite_sha512_init(struct sha512_ctx * ctx)44 void cryptonite_sha512_init(struct sha512_ctx *ctx)
45 {
46 	memset(ctx, 0, sizeof(*ctx));
47 
48 	ctx->h[0] = 0x6a09e667f3bcc908ULL;
49 	ctx->h[1] = 0xbb67ae8584caa73bULL;
50 	ctx->h[2] = 0x3c6ef372fe94f82bULL;
51 	ctx->h[3] = 0xa54ff53a5f1d36f1ULL;
52 	ctx->h[4] = 0x510e527fade682d1ULL;
53 	ctx->h[5] = 0x9b05688c2b3e6c1fULL;
54 	ctx->h[6] = 0x1f83d9abfb41bd6bULL;
55 	ctx->h[7] = 0x5be0cd19137e2179ULL;
56 }
57 
58 /* 232 times the cube root of the first 64 primes 2..311 */
59 static const uint64_t k[] = {
60 	0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL,
61 	0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
62 	0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL,
63 	0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
64 	0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL,
65 	0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
66 	0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL,
67 	0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
68 	0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL,
69 	0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
70 	0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL,
71 	0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
72 	0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL,
73 	0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
74 	0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL,
75 	0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
76 	0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL,
77 	0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
78 	0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL,
79 	0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
80 	0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL,
81 	0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
82 	0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL,
83 	0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
84 	0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL,
85 	0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
86 	0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL,
87 };
88 
89 #define e0(x)       (ror64(x, 28) ^ ror64(x, 34) ^ ror64(x, 39))
90 #define e1(x)       (ror64(x, 14) ^ ror64(x, 18) ^ ror64(x, 41))
91 #define s0(x)       (ror64(x, 1) ^ ror64(x, 8) ^ (x >> 7))
92 #define s1(x)       (ror64(x, 19) ^ ror64(x, 61) ^ (x >> 6))
93 
sha512_do_chunk(struct sha512_ctx * ctx,uint64_t * buf)94 static void sha512_do_chunk(struct sha512_ctx *ctx, uint64_t *buf)
95 {
96 	uint64_t a, b, c, d, e, f, g, h, t1, t2;
97 	int i;
98 	uint64_t w[80];
99 
100 	cpu_to_be64_array(w, buf, 16);
101 
102 	for (i = 16; i < 80; i++)
103 		w[i] = s1(w[i - 2]) + w[i - 7] + s0(w[i - 15]) + w[i - 16];
104 
105 	a = ctx->h[0]; b = ctx->h[1]; c = ctx->h[2]; d = ctx->h[3];
106 	e = ctx->h[4]; f = ctx->h[5]; g = ctx->h[6]; h = ctx->h[7];
107 
108 #define R(a, b, c, d, e, f, g, h, k, w)			\
109 	t1 = h + e1(e) + (g ^ (e & (f ^ g))) + k + w;	\
110 	t2 = e0(a) + ((a & b) | (c & (a | b)));		\
111 	d += t1;					\
112 	h = t1 + t2
113 
114 	for (i = 0; i < 80; i += 8) {
115 		R(a, b, c, d, e, f, g, h, k[i + 0], w[i + 0]);
116 		R(h, a, b, c, d, e, f, g, k[i + 1], w[i + 1]);
117 		R(g, h, a, b, c, d, e, f, k[i + 2], w[i + 2]);
118 		R(f, g, h, a, b, c, d, e, k[i + 3], w[i + 3]);
119 		R(e, f, g, h, a, b, c, d, k[i + 4], w[i + 4]);
120 		R(d, e, f, g, h, a, b, c, k[i + 5], w[i + 5]);
121 		R(c, d, e, f, g, h, a, b, k[i + 6], w[i + 6]);
122 		R(b, c, d, e, f, g, h, a, k[i + 7], w[i + 7]);
123 	}
124 
125 #undef R
126 
127 	ctx->h[0] += a; ctx->h[1] += b; ctx->h[2] += c; ctx->h[3] += d;
128 	ctx->h[4] += e; ctx->h[5] += f; ctx->h[6] += g; ctx->h[7] += h;
129 }
130 
cryptonite_sha384_update(struct sha384_ctx * ctx,const uint8_t * data,uint32_t len)131 void cryptonite_sha384_update(struct sha384_ctx *ctx, const uint8_t *data, uint32_t len)
132 {
133 	return cryptonite_sha512_update(ctx, data, len);
134 }
135 
cryptonite_sha512_update(struct sha512_ctx * ctx,const uint8_t * data,uint32_t len)136 void cryptonite_sha512_update(struct sha512_ctx *ctx, const uint8_t *data, uint32_t len)
137 {
138 	unsigned int index, to_fill;
139 
140 	/* check for partial buffer */
141 	index = (unsigned int) (ctx->sz[0] & 0x7f);
142 	to_fill = 128 - index;
143 
144 	ctx->sz[0] += len;
145 	if (ctx->sz[0] < len)
146 		ctx->sz[1]++;
147 
148 	/* process partial buffer if there's enough data to make a block */
149 	if (index && len >= to_fill) {
150 		memcpy(ctx->buf + index, data, to_fill);
151 		sha512_do_chunk(ctx, (uint64_t *) ctx->buf);
152 		len -= to_fill;
153 		data += to_fill;
154 		index = 0;
155 	}
156 
157 	if (need_alignment(data, 8)) {
158 		uint64_t tramp[16];
159 		ASSERT_ALIGNMENT(tramp, 8);
160 		for (; len >= 128; len -= 128, data += 128) {
161 			memcpy(tramp, data, 128);
162 			sha512_do_chunk(ctx, tramp);
163 		}
164 	} else {
165 		/* process as much 128-block as possible */
166 		for (; len >= 128; len -= 128, data += 128)
167 			sha512_do_chunk(ctx, (uint64_t *) data);
168 	}
169 
170 	/* append data into buf */
171 	if (len)
172 		memcpy(ctx->buf + index, data, len);
173 }
174 
cryptonite_sha384_finalize(struct sha384_ctx * ctx,uint8_t * out)175 void cryptonite_sha384_finalize(struct sha384_ctx *ctx, uint8_t *out)
176 {
177 	uint8_t intermediate[SHA512_DIGEST_SIZE];
178 
179 	cryptonite_sha512_finalize(ctx, intermediate);
180 	memcpy(out, intermediate, SHA384_DIGEST_SIZE);
181 }
182 
cryptonite_sha512_finalize(struct sha512_ctx * ctx,uint8_t * out)183 void cryptonite_sha512_finalize(struct sha512_ctx *ctx, uint8_t *out)
184 {
185 	static uint8_t padding[128] = { 0x80, };
186 	uint32_t i, index, padlen;
187 	uint64_t bits[2];
188 
189 	/* cpu -> big endian */
190 	bits[0] = cpu_to_be64((ctx->sz[1] << 3 | ctx->sz[0] >> 61));
191 	bits[1] = cpu_to_be64((ctx->sz[0] << 3));
192 
193 	/* pad out to 56 */
194 	index = (unsigned int) (ctx->sz[0] & 0x7f);
195 	padlen = (index < 112) ? (112 - index) : ((128 + 112) - index);
196 	cryptonite_sha512_update(ctx, padding, padlen);
197 
198 	/* append length */
199 	cryptonite_sha512_update(ctx, (uint8_t *) bits, sizeof(bits));
200 
201 	/* store to digest */
202 	for (i = 0; i < 8; i++)
203 		store_be64(out+8*i, ctx->h[i]);
204 }
205 
206 #include <stdio.h>
207 
cryptonite_sha512t_init(struct sha512_ctx * ctx,uint32_t hashlen)208 void cryptonite_sha512t_init(struct sha512_ctx *ctx, uint32_t hashlen)
209 {
210 	memset(ctx, 0, sizeof(*ctx));
211 	if (hashlen >= 512)
212 		return;
213 	switch (hashlen) {
214 	case 224:
215 		ctx->h[0] = 0x8c3d37c819544da2ULL;
216 		ctx->h[1] = 0x73e1996689dcd4d6ULL;
217 		ctx->h[2] = 0x1dfab7ae32ff9c82ULL;
218 		ctx->h[3] = 0x679dd514582f9fcfULL;
219 		ctx->h[4] = 0x0f6d2b697bd44da8ULL;
220 		ctx->h[5] = 0x77e36f7304c48942ULL;
221 		ctx->h[6] = 0x3f9d85a86a1d36c8ULL;
222 		ctx->h[7] = 0x1112e6ad91d692a1ULL;
223 		break;
224 	case 256:
225 		ctx->h[0] = 0x22312194fc2bf72cULL;
226 		ctx->h[1] = 0x9f555fa3c84c64c2ULL;
227 		ctx->h[2] = 0x2393b86b6f53b151ULL;
228 		ctx->h[3] = 0x963877195940eabdULL;
229 		ctx->h[4] = 0x96283ee2a88effe3ULL;
230 		ctx->h[5] = 0xbe5e1e2553863992ULL;
231 		ctx->h[6] = 0x2b0199fc2c85b8aaULL;
232 		ctx->h[7] = 0x0eb72ddc81c52ca2ULL;
233 		break;
234 	default: {
235 		char buf[8+4];
236 		uint8_t out[64];
237 		int i;
238 
239 		cryptonite_sha512_init(ctx);
240 		for (i = 0; i < 8; i++)
241 			ctx->h[i] ^= 0xa5a5a5a5a5a5a5a5ULL;
242 
243 		i = sprintf(buf, "SHA-512/%d", hashlen);
244 		cryptonite_sha512_update(ctx, (uint8_t *) buf, i);
245 		cryptonite_sha512_finalize(ctx, out);
246 
247 		/* re-init the context, otherwise len is changed */
248 		memset(ctx, 0, sizeof(*ctx));
249 		for (i = 0; i < 8; i++)
250 			ctx->h[i] = cpu_to_be64(((uint64_t *) out)[i]);
251 		}
252 	}
253 }
254 
cryptonite_sha512t_update(struct sha512_ctx * ctx,const uint8_t * data,uint32_t len)255 void cryptonite_sha512t_update(struct sha512_ctx *ctx, const uint8_t *data, uint32_t len)
256 {
257 	return cryptonite_sha512_update(ctx, data, len);
258 }
259 
cryptonite_sha512t_finalize(struct sha512_ctx * ctx,uint32_t hashlen,uint8_t * out)260 void cryptonite_sha512t_finalize(struct sha512_ctx *ctx, uint32_t hashlen, uint8_t *out)
261 {
262 	uint8_t intermediate[SHA512_DIGEST_SIZE];
263 
264 	cryptonite_sha512_finalize(ctx, intermediate);
265 	memcpy(out, intermediate, hashlen / 8);
266 }
267 
268