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