1 /*
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "inner.h"
26
27 #define CH(X, Y, Z) ((((Y) ^ (Z)) & (X)) ^ (Z))
28 #define MAJ(X, Y, Z) (((Y) & (Z)) | (((Y) | (Z)) & (X)))
29
30 #define ROTR(x, n) (((uint64_t)(x) << (64 - (n))) | ((uint64_t)(x) >> (n)))
31
32 #define BSG5_0(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
33 #define BSG5_1(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
34 #define SSG5_0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ (uint64_t)((x) >> 7))
35 #define SSG5_1(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ (uint64_t)((x) >> 6))
36
37 static const uint64_t IV384[8] = {
38 0xCBBB9D5DC1059ED8, 0x629A292A367CD507,
39 0x9159015A3070DD17, 0x152FECD8F70E5939,
40 0x67332667FFC00B31, 0x8EB44A8768581511,
41 0xDB0C2E0D64F98FA7, 0x47B5481DBEFA4FA4
42 };
43
44 static const uint64_t IV512[8] = {
45 0x6A09E667F3BCC908, 0xBB67AE8584CAA73B,
46 0x3C6EF372FE94F82B, 0xA54FF53A5F1D36F1,
47 0x510E527FADE682D1, 0x9B05688C2B3E6C1F,
48 0x1F83D9ABFB41BD6B, 0x5BE0CD19137E2179
49 };
50
51 static const uint64_t K[80] = {
52 0x428A2F98D728AE22, 0x7137449123EF65CD,
53 0xB5C0FBCFEC4D3B2F, 0xE9B5DBA58189DBBC,
54 0x3956C25BF348B538, 0x59F111F1B605D019,
55 0x923F82A4AF194F9B, 0xAB1C5ED5DA6D8118,
56 0xD807AA98A3030242, 0x12835B0145706FBE,
57 0x243185BE4EE4B28C, 0x550C7DC3D5FFB4E2,
58 0x72BE5D74F27B896F, 0x80DEB1FE3B1696B1,
59 0x9BDC06A725C71235, 0xC19BF174CF692694,
60 0xE49B69C19EF14AD2, 0xEFBE4786384F25E3,
61 0x0FC19DC68B8CD5B5, 0x240CA1CC77AC9C65,
62 0x2DE92C6F592B0275, 0x4A7484AA6EA6E483,
63 0x5CB0A9DCBD41FBD4, 0x76F988DA831153B5,
64 0x983E5152EE66DFAB, 0xA831C66D2DB43210,
65 0xB00327C898FB213F, 0xBF597FC7BEEF0EE4,
66 0xC6E00BF33DA88FC2, 0xD5A79147930AA725,
67 0x06CA6351E003826F, 0x142929670A0E6E70,
68 0x27B70A8546D22FFC, 0x2E1B21385C26C926,
69 0x4D2C6DFC5AC42AED, 0x53380D139D95B3DF,
70 0x650A73548BAF63DE, 0x766A0ABB3C77B2A8,
71 0x81C2C92E47EDAEE6, 0x92722C851482353B,
72 0xA2BFE8A14CF10364, 0xA81A664BBC423001,
73 0xC24B8B70D0F89791, 0xC76C51A30654BE30,
74 0xD192E819D6EF5218, 0xD69906245565A910,
75 0xF40E35855771202A, 0x106AA07032BBD1B8,
76 0x19A4C116B8D2D0C8, 0x1E376C085141AB53,
77 0x2748774CDF8EEB99, 0x34B0BCB5E19B48A8,
78 0x391C0CB3C5C95A63, 0x4ED8AA4AE3418ACB,
79 0x5B9CCA4F7763E373, 0x682E6FF3D6B2B8A3,
80 0x748F82EE5DEFB2FC, 0x78A5636F43172F60,
81 0x84C87814A1F0AB72, 0x8CC702081A6439EC,
82 0x90BEFFFA23631E28, 0xA4506CEBDE82BDE9,
83 0xBEF9A3F7B2C67915, 0xC67178F2E372532B,
84 0xCA273ECEEA26619C, 0xD186B8C721C0C207,
85 0xEADA7DD6CDE0EB1E, 0xF57D4F7FEE6ED178,
86 0x06F067AA72176FBA, 0x0A637DC5A2C898A6,
87 0x113F9804BEF90DAE, 0x1B710B35131C471B,
88 0x28DB77F523047D84, 0x32CAAB7B40C72493,
89 0x3C9EBE0A15C9BEBC, 0x431D67C49C100D4C,
90 0x4CC5D4BECB3E42B6, 0x597F299CFC657E2A,
91 0x5FCB6FAB3AD6FAEC, 0x6C44198C4A475817
92 };
93
94 static void
sha2big_round(const unsigned char * buf,uint64_t * val)95 sha2big_round(const unsigned char *buf, uint64_t *val)
96 {
97
98 #define SHA2BIG_STEP(A, B, C, D, E, F, G, H, j) do { \
99 uint64_t T1, T2; \
100 T1 = H + BSG5_1(E) + CH(E, F, G) + K[j] + w[j]; \
101 T2 = BSG5_0(A) + MAJ(A, B, C); \
102 D += T1; \
103 H = T1 + T2; \
104 } while (0)
105
106 int i;
107 uint64_t a, b, c, d, e, f, g, h;
108 uint64_t w[80];
109
110 br_range_dec64be(w, 16, buf);
111 for (i = 16; i < 80; i ++) {
112 w[i] = SSG5_1(w[i - 2]) + w[i - 7]
113 + SSG5_0(w[i - 15]) + w[i - 16];
114 }
115 a = val[0];
116 b = val[1];
117 c = val[2];
118 d = val[3];
119 e = val[4];
120 f = val[5];
121 g = val[6];
122 h = val[7];
123 for (i = 0; i < 80; i += 8) {
124 SHA2BIG_STEP(a, b, c, d, e, f, g, h, i + 0);
125 SHA2BIG_STEP(h, a, b, c, d, e, f, g, i + 1);
126 SHA2BIG_STEP(g, h, a, b, c, d, e, f, i + 2);
127 SHA2BIG_STEP(f, g, h, a, b, c, d, e, i + 3);
128 SHA2BIG_STEP(e, f, g, h, a, b, c, d, i + 4);
129 SHA2BIG_STEP(d, e, f, g, h, a, b, c, i + 5);
130 SHA2BIG_STEP(c, d, e, f, g, h, a, b, i + 6);
131 SHA2BIG_STEP(b, c, d, e, f, g, h, a, i + 7);
132 }
133 val[0] += a;
134 val[1] += b;
135 val[2] += c;
136 val[3] += d;
137 val[4] += e;
138 val[5] += f;
139 val[6] += g;
140 val[7] += h;
141 }
142
143 static void
sha2big_update(br_sha384_context * cc,const void * data,size_t len)144 sha2big_update(br_sha384_context *cc, const void *data, size_t len)
145 {
146 const unsigned char *buf;
147 size_t ptr;
148
149 buf = data;
150 ptr = (size_t)cc->count & 127;
151 cc->count += (uint64_t)len;
152 while (len > 0) {
153 size_t clen;
154
155 clen = 128 - ptr;
156 if (clen > len) {
157 clen = len;
158 }
159 memcpy(cc->buf + ptr, buf, clen);
160 ptr += clen;
161 buf += clen;
162 len -= clen;
163 if (ptr == 128) {
164 sha2big_round(cc->buf, cc->val);
165 ptr = 0;
166 }
167 }
168 }
169
170 static void
sha2big_out(const br_sha384_context * cc,void * dst,int num)171 sha2big_out(const br_sha384_context *cc, void *dst, int num)
172 {
173 unsigned char buf[128];
174 uint64_t val[8];
175 size_t ptr;
176
177 ptr = (size_t)cc->count & 127;
178 memcpy(buf, cc->buf, ptr);
179 memcpy(val, cc->val, sizeof val);
180 buf[ptr ++] = 0x80;
181 if (ptr > 112) {
182 memset(buf + ptr, 0, 128 - ptr);
183 sha2big_round(buf, val);
184 memset(buf, 0, 112);
185 } else {
186 memset(buf + ptr, 0, 112 - ptr);
187 }
188 br_enc64be(buf + 112, cc->count >> 61);
189 br_enc64be(buf + 120, cc->count << 3);
190 sha2big_round(buf, val);
191 br_range_enc64be(dst, val, num);
192 }
193
194 /* see bearssl.h */
195 void
br_sha384_init(br_sha384_context * cc)196 br_sha384_init(br_sha384_context *cc)
197 {
198 cc->vtable = &br_sha384_vtable;
199 memcpy(cc->val, IV384, sizeof IV384);
200 cc->count = 0;
201 }
202
203 /* see bearssl.h */
204 void
br_sha384_update(br_sha384_context * cc,const void * data,size_t len)205 br_sha384_update(br_sha384_context *cc, const void *data, size_t len)
206 {
207 sha2big_update(cc, data, len);
208 }
209
210 /* see bearssl.h */
211 void
br_sha384_out(const br_sha384_context * cc,void * dst)212 br_sha384_out(const br_sha384_context *cc, void *dst)
213 {
214 sha2big_out(cc, dst, 6);
215 }
216
217 /* see bearssl.h */
218 uint64_t
br_sha384_state(const br_sha384_context * cc,void * dst)219 br_sha384_state(const br_sha384_context *cc, void *dst)
220 {
221 br_range_enc64be(dst, cc->val, 8);
222 return cc->count;
223 }
224
225 /* see bearssl.h */
226 void
br_sha384_set_state(br_sha384_context * cc,const void * stb,uint64_t count)227 br_sha384_set_state(br_sha384_context *cc, const void *stb, uint64_t count)
228 {
229 br_range_dec64be(cc->val, 8, stb);
230 cc->count = count;
231 }
232
233 /* see bearssl.h */
234 void
br_sha512_init(br_sha512_context * cc)235 br_sha512_init(br_sha512_context *cc)
236 {
237 cc->vtable = &br_sha512_vtable;
238 memcpy(cc->val, IV512, sizeof IV512);
239 cc->count = 0;
240 }
241
242 /* see bearssl.h */
243 void
br_sha512_out(const br_sha512_context * cc,void * dst)244 br_sha512_out(const br_sha512_context *cc, void *dst)
245 {
246 sha2big_out(cc, dst, 8);
247 }
248
249 /* see bearssl.h */
250 const br_hash_class br_sha384_vtable = {
251 sizeof(br_sha384_context),
252 BR_HASHDESC_ID(br_sha384_ID)
253 | BR_HASHDESC_OUT(48)
254 | BR_HASHDESC_STATE(64)
255 | BR_HASHDESC_LBLEN(7)
256 | BR_HASHDESC_MD_PADDING
257 | BR_HASHDESC_MD_PADDING_BE
258 | BR_HASHDESC_MD_PADDING_128,
259 (void (*)(const br_hash_class **))&br_sha384_init,
260 (void (*)(const br_hash_class **, const void *, size_t))
261 &br_sha384_update,
262 (void (*)(const br_hash_class *const *, void *))&br_sha384_out,
263 (uint64_t (*)(const br_hash_class *const *, void *))&br_sha384_state,
264 (void (*)(const br_hash_class **, const void *, uint64_t))
265 &br_sha384_set_state
266 };
267
268 /* see bearssl.h */
269 const br_hash_class br_sha512_vtable = {
270 sizeof(br_sha512_context),
271 BR_HASHDESC_ID(br_sha512_ID)
272 | BR_HASHDESC_OUT(64)
273 | BR_HASHDESC_STATE(64)
274 | BR_HASHDESC_LBLEN(7)
275 | BR_HASHDESC_MD_PADDING
276 | BR_HASHDESC_MD_PADDING_BE
277 | BR_HASHDESC_MD_PADDING_128,
278 (void (*)(const br_hash_class **))&br_sha512_init,
279 (void (*)(const br_hash_class **, const void *, size_t))
280 &br_sha512_update,
281 (void (*)(const br_hash_class *const *, void *))&br_sha512_out,
282 (uint64_t (*)(const br_hash_class *const *, void *))&br_sha512_state,
283 (void (*)(const br_hash_class **, const void *, uint64_t))
284 &br_sha512_set_state
285 };
286