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