1 /*
2 ** Copyright 2005 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5 
6 #define	SHA1_INTERNAL
7 #include	"sha1.h"
8 
9 #include	<string.h>
10 #include	<stdlib.h>
11 
12 
13 #define ROTR(x,n) ((SHA256_WORD)(((SHA256_WORD)(x) >> (n))|((x) << (32-(n)))))
14 
15 #define ROTL(x,n) ((SHA256_WORD)(((SHA256_WORD)(x) << (n))|((x) >> (32-(n)))))
16 
17 
18 #define CH(x,y,z) ((SHA256_WORD)(((x) & (y)) ^ ((~(x))&(z))))
19 #define MAJ(x,y,z) ((SHA256_WORD)(((x)&(y))^((x)&(z))^((y)&(z))))
20 
21 #define SUM0(x) ((SHA256_WORD)(ROTR((x),2)^ROTR((x),13)^ROTR((x),22)))
22 #define SUM1(x) ((SHA256_WORD)(ROTR((x),6)^ROTR((x),11)^ROTR((x),25)))
23 
24 #define TH0(x) ((SHA256_WORD)(ROTR((x),7)^ROTR((x),18)^((SHA256_WORD)(x)>>3)))
25 #define TH1(x) ((SHA256_WORD)(ROTR((x),17)^ROTR((x),19)^((SHA256_WORD)(x)>>10)))
26 
27 static const SHA256_WORD K[64]=
28 	{0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
29 	 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
30 	 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
31 	 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
32 	 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
33 	 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
34 	 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
35 	 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2};
36 
sha256_context_init(struct SHA256_CONTEXT * c)37 void sha256_context_init(struct SHA256_CONTEXT *c)
38 {
39 	if (sizeof(SHA256_WORD) != 4)
40 		abort();
41 
42 	c->H[0] = 0x6A09E667;
43 	c->H[1] = 0xBB67AE85;
44 	c->H[2] = 0x3C6EF372;
45 	c->H[3] = 0xA54FF53A;
46 	c->H[4] = 0x510E527F;
47 	c->H[5] = 0x9B05688C;
48 	c->H[6] = 0x1F83D9AB;
49 	c->H[7] = 0x5BE0CD19;
50 	c->blk_ptr=0;
51 }
52 
sha256_context_hash(struct SHA256_CONTEXT * cc,const unsigned char blk[SHA256_BLOCK_SIZE])53 void sha256_context_hash(struct SHA256_CONTEXT *cc,
54 			 const unsigned char blk[SHA256_BLOCK_SIZE])
55 {
56 	SHA256_WORD W[64];
57 	unsigned i, t;
58 	SHA256_WORD a,b,c,d,e,f,g,h;
59 
60 	for (i=t=0; t<16; t++)
61 	{
62 		SHA256_WORD x=blk[i]; i++;
63 
64 		x=(x << 8) | blk[i]; i++;
65 		x=(x << 8) | blk[i]; i++;
66 		W[t]=(x << 8) | blk[i]; i++;
67 	}
68 
69 	for (t=16; t<64; t++)
70 		W[t]= TH1(W[t-2]) + W[t-7] + TH0(W[t-15]) + W[t-16];
71 
72 	a=cc->H[0];
73 	b=cc->H[1];
74 	c=cc->H[2];
75 	d=cc->H[3];
76 	e=cc->H[4];
77 	f=cc->H[5];
78 	g=cc->H[6];
79 	h=cc->H[7];
80 
81 	for (t=0; t<64; t++)
82 	{
83 		SHA256_WORD T1=h + SUM1(e) + CH(e,f,g) + K[t] + W[t];
84 		SHA256_WORD T2=SUM0(a)+MAJ(a,b,c);
85 		h=g;
86 		g=f;
87 		f=e;
88 		e=d+T1;
89 		d=c;
90 		c=b;
91 		b=a;
92 		a=T1+T2;
93 	}
94 
95 	cc->H[0] += a;
96 	cc->H[1] += b;
97 	cc->H[2] += c;
98 	cc->H[3] += d;
99 	cc->H[4] += e;
100 	cc->H[5] += f;
101 	cc->H[6] += g;
102 	cc->H[7] += h;
103 }
104 
sha256_context_hashstream(struct SHA256_CONTEXT * c,const void * p,unsigned l)105 void sha256_context_hashstream(struct SHA256_CONTEXT *c, const void *p, unsigned l)
106 {
107 const unsigned char *cp=(const unsigned char *)p;
108 unsigned ll;
109 
110 	while (l)
111 	{
112 		if (c->blk_ptr == 0 && l >= SHA256_BLOCK_SIZE)
113 		{
114 			sha256_context_hash(c, cp);
115 			cp += SHA256_BLOCK_SIZE;
116 			l -= SHA256_BLOCK_SIZE;
117 			continue;
118 		}
119 
120 		ll=l;
121 		if (ll > SHA256_BLOCK_SIZE - c->blk_ptr)
122 			ll=SHA256_BLOCK_SIZE - c->blk_ptr;
123 		memcpy(c->blk + c->blk_ptr, cp, ll);
124 		c->blk_ptr += ll;
125 		cp += ll;
126 		l -= ll;
127 		if (c->blk_ptr >= SHA256_BLOCK_SIZE)
128 		{
129 			sha256_context_hash(c, c->blk);
130 			c->blk_ptr=0;
131 		}
132 	}
133 }
134 
sha256_context_endstream(struct SHA256_CONTEXT * c,unsigned long l)135 void sha256_context_endstream(struct SHA256_CONTEXT *c, unsigned long l)
136 {
137 	unsigned char buf[8];
138 	static const unsigned char zero[SHA256_BLOCK_SIZE-8];
139 
140 	buf[0]=0x80;
141 	sha256_context_hashstream(c, &buf, 1);
142 	while (c->blk_ptr != SHA256_BLOCK_SIZE-8)
143 	{
144 		if (c->blk_ptr > SHA256_BLOCK_SIZE-8)
145 		{
146 			sha256_context_hashstream(c, zero,
147 				SHA256_BLOCK_SIZE - c->blk_ptr);
148 			continue;
149 		}
150 		sha256_context_hashstream(c, zero,
151 			SHA256_BLOCK_SIZE-8-c->blk_ptr);
152 	}
153 
154 	l *= 8;
155 	buf[7] = l;
156 	buf[6] = (l >>= 8);
157 	buf[5] = (l >>= 8);
158 	buf[4] = (l >> 8);
159 	buf[3]=buf[2]=buf[1]=buf[0]=0;
160 
161 	sha256_context_hashstream(c, buf, 8);
162 }
163 
sha256_context_digest(struct SHA256_CONTEXT * c,SHA256_DIGEST d)164 void sha256_context_digest(struct SHA256_CONTEXT *c, SHA256_DIGEST d)
165 {
166 	unsigned char *dp=d + SHA256_DIGEST_SIZE;
167 	unsigned i;
168 
169 	for ( i=8; i; )
170 	{
171 		SHA256_WORD	w=c->H[--i];
172 
173 		*--dp=w; w >>= 8;
174 		*--dp=w; w >>= 8;
175 		*--dp=w; w >>= 8;
176 		*--dp=w;
177 	}
178 }
179 
sha256_context_restore(struct SHA256_CONTEXT * c,const SHA256_DIGEST d)180 void sha256_context_restore(struct SHA256_CONTEXT *c, const SHA256_DIGEST d)
181 {
182 	const unsigned char *dp=d;
183 	unsigned i;
184 
185 	for (i=0; i<8; i++)
186 	{
187 		SHA256_WORD	w= *dp++;
188 
189 		w=(w << 8) | *dp++;
190 		w=(w << 8) | *dp++;
191 		w=(w << 8) | *dp++;
192 		c->H[i]=w;
193 	}
194 	c->blk_ptr=0;
195 }
196 
sha256_digest(const void * msg,unsigned len,SHA256_DIGEST d)197 void sha256_digest(const void *msg, unsigned len, SHA256_DIGEST d)
198 {
199 	struct SHA256_CONTEXT c;
200 
201 	sha256_context_init( &c );
202 	sha256_context_hashstream(&c, msg, len);
203 	sha256_context_endstream(&c, len);
204 	sha256_context_digest( &c, d );
205 }
206