1/*	$NetBSD: sha256.c,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2
3/*
4 * Copyright (c) 2006 Kungliga Tekniska H��gskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <config.h>
37#include <krb5/roken.h>
38
39#include "hash.h"
40#include "sha.h"
41
42#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
43#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
44
45#define ROTR(x,n)   (((x)>>(n)) | ((x) << (32 - (n))))
46
47#define Sigma0(x)	(ROTR(x,2)  ^ ROTR(x,13) ^ ROTR(x,22))
48#define Sigma1(x)	(ROTR(x,6)  ^ ROTR(x,11) ^ ROTR(x,25))
49#define sigma0(x)	(ROTR(x,7)  ^ ROTR(x,18) ^ ((x)>>3))
50#define sigma1(x)	(ROTR(x,17) ^ ROTR(x,19) ^ ((x)>>10))
51
52#define A m->counter[0]
53#define B m->counter[1]
54#define C m->counter[2]
55#define D m->counter[3]
56#define E m->counter[4]
57#define F m->counter[5]
58#define G m->counter[6]
59#define H m->counter[7]
60
61static const uint32_t constant_256[64] = {
62    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
63    0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
64    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
65    0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
66    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
67    0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
68    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
69    0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
70    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
71    0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
72    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
73    0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
74    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
75    0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
76    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
77    0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
78};
79
80int
81SHA256_Init (SHA256_CTX *m)
82{
83    m->sz[0] = 0;
84    m->sz[1] = 0;
85    A = 0x6a09e667;
86    B = 0xbb67ae85;
87    C = 0x3c6ef372;
88    D = 0xa54ff53a;
89    E = 0x510e527f;
90    F = 0x9b05688c;
91    G = 0x1f83d9ab;
92    H = 0x5be0cd19;
93    return 1;
94}
95
96static void
97calc (SHA256_CTX *m, uint32_t *in)
98{
99    uint32_t AA, BB, CC, DD, EE, FF, GG, HH;
100    uint32_t data[64];
101    int i;
102
103    AA = A;
104    BB = B;
105    CC = C;
106    DD = D;
107    EE = E;
108    FF = F;
109    GG = G;
110    HH = H;
111
112    for (i = 0; i < 16; ++i)
113	data[i] = in[i];
114    for (i = 16; i < 64; ++i)
115	data[i] = sigma1(data[i-2]) + data[i-7] +
116	    sigma0(data[i-15]) + data[i - 16];
117
118    for (i = 0; i < 64; i++) {
119	uint32_t T1, T2;
120
121	T1 = HH + Sigma1(EE) + Ch(EE, FF, GG) + constant_256[i] + data[i];
122	T2 = Sigma0(AA) + Maj(AA,BB,CC);
123
124	HH = GG;
125	GG = FF;
126	FF = EE;
127	EE = DD + T1;
128	DD = CC;
129	CC = BB;
130	BB = AA;
131	AA = T1 + T2;
132    }
133
134    A += AA;
135    B += BB;
136    C += CC;
137    D += DD;
138    E += EE;
139    F += FF;
140    G += GG;
141    H += HH;
142}
143
144/*
145 * From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
146 */
147
148#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
149static inline uint32_t
150swap_uint32_t (uint32_t t)
151{
152#define ROL(x,n) ((x)<<(n))|((x)>>(32-(n)))
153    uint32_t temp1, temp2;
154
155    temp1   = cshift(t, 16);
156    temp2   = temp1 >> 8;
157    temp1  &= 0x00ff00ff;
158    temp2  &= 0x00ff00ff;
159    temp1 <<= 8;
160    return temp1 | temp2;
161}
162#endif
163
164struct x32{
165    unsigned int a:32;
166    unsigned int b:32;
167};
168
169int
170SHA256_Update (SHA256_CTX *m, const void *v, size_t len)
171{
172    const unsigned char *p = v;
173    size_t old_sz = m->sz[0];
174    size_t offset;
175
176    m->sz[0] += len * 8;
177    if (m->sz[0] < old_sz)
178	++m->sz[1];
179    offset = (old_sz / 8) % 64;
180    while(len > 0){
181	size_t l = min(len, 64 - offset);
182	memcpy(m->save + offset, p, l);
183	offset += l;
184	p += l;
185	len -= l;
186	if(offset == 64){
187#if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
188	    int i;
189	    uint32_t current[16];
190	    struct x32 *us = (struct x32*)m->save;
191	    for(i = 0; i < 8; i++){
192		current[2*i+0] = swap_uint32_t(us[i].a);
193		current[2*i+1] = swap_uint32_t(us[i].b);
194	    }
195	    calc(m, current);
196#else
197	    calc(m, (uint32_t*)m->save);
198#endif
199	    offset = 0;
200	}
201    }
202    return 1;
203}
204
205int
206SHA256_Final (void *res, SHA256_CTX *m)
207{
208    unsigned char zeros[72];
209    unsigned offset = (m->sz[0] / 8) % 64;
210    unsigned int dstart = (120 - offset - 1) % 64 + 1;
211
212    *zeros = 0x80;
213    memset (zeros + 1, 0, sizeof(zeros) - 1);
214    zeros[dstart+7] = (m->sz[0] >> 0) & 0xff;
215    zeros[dstart+6] = (m->sz[0] >> 8) & 0xff;
216    zeros[dstart+5] = (m->sz[0] >> 16) & 0xff;
217    zeros[dstart+4] = (m->sz[0] >> 24) & 0xff;
218    zeros[dstart+3] = (m->sz[1] >> 0) & 0xff;
219    zeros[dstart+2] = (m->sz[1] >> 8) & 0xff;
220    zeros[dstart+1] = (m->sz[1] >> 16) & 0xff;
221    zeros[dstart+0] = (m->sz[1] >> 24) & 0xff;
222    SHA256_Update (m, zeros, dstart + 8);
223    {
224	int i;
225	unsigned char *r = (unsigned char*)res;
226
227	for (i = 0; i < 8; ++i) {
228	    r[4*i+3] = m->counter[i] & 0xFF;
229	    r[4*i+2] = (m->counter[i] >> 8) & 0xFF;
230	    r[4*i+1] = (m->counter[i] >> 16) & 0xFF;
231	    r[4*i]   = (m->counter[i] >> 24) & 0xFF;
232	}
233    }
234    return 1;
235}
236