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