1.2.5
by Patrick Matthäi
Import upstream version 0.076~beta1 |
1 |
/*
|
2 |
* FIPS 180-2 SHA-224/256/384/512 implementation
|
|
3 |
* Last update: 02/02/2007
|
|
4 |
* Issue date: 04/30/2005
|
|
5 |
*
|
|
6 |
* Copyright (C) 2005, 2007 Olivier Gay <olivier.gay@a3.epfl.ch>
|
|
7 |
* All rights reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
* 1. Redistributions of source code must retain the above copyright
|
|
13 |
* notice, this list of conditions and the following disclaimer.
|
|
14 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
15 |
* notice, this list of conditions and the following disclaimer in the
|
|
16 |
* documentation and/or other materials provided with the distribution.
|
|
17 |
* 3. Neither the name of the project nor the names of its contributors
|
|
18 |
* may be used to endorse or promote products derived from this software
|
|
19 |
* without specific prior written permission.
|
|
20 |
*
|
|
21 |
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
22 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
25 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
27 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
28 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
31 |
* SUCH DAMAGE.
|
|
32 |
*/
|
|
33 |
||
34 |
#include <string.h> |
|
35 |
||
36 |
#include "SHA256.h" |
|
37 |
||
38 |
#define SHFR(x, n) (x >> n)
|
|
39 |
#define ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))
|
|
40 |
#define ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n)))
|
|
41 |
#define CH(x, y, z) ((x & y) ^ (~x & z))
|
|
42 |
#define MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
|
|
43 |
||
44 |
#define SHA256_F1(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
|
|
45 |
#define SHA256_F2(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
|
|
46 |
#define SHA256_F3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHFR(x, 3))
|
|
47 |
#define SHA256_F4(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHFR(x, 10))
|
|
48 |
||
49 |
#define UNPACK32(x, str) \
|
|
50 |
{ \
|
|
51 |
*((str) + 3) = (uint8_t) ((x) ); \
|
|
52 |
*((str) + 2) = (uint8_t) ((x) >> 8); \
|
|
53 |
*((str) + 1) = (uint8_t) ((x) >> 16); \
|
|
54 |
*((str) + 0) = (uint8_t) ((x) >> 24); \
|
|
55 |
}
|
|
56 |
||
57 |
#define PACK32(str, x) \
|
|
58 |
{ \
|
|
59 |
*(x) = ((uint32_t) *((str) + 3) ) \
|
|
60 |
| ((uint32_t) *((str) + 2) << 8) \
|
|
61 |
| ((uint32_t) *((str) + 1) << 16) \
|
|
62 |
| ((uint32_t) *((str) + 0) << 24); \
|
|
63 |
}
|
|
64 |
||
65 |
/* Macros used for loops unrolling */
|
|
66 |
||
67 |
#define SHA256_SCR(i) \
|
|
68 |
{ \
|
|
69 |
w[i] = SHA256_F4(w[i - 2]) + w[i - 7] \
|
|
70 |
+ SHA256_F3(w[i - 15]) + w[i - 16]; \
|
|
71 |
}
|
|
72 |
||
73 |
#define SHA256_EXP(a, b, c, d, e, f, g, h, j) \
|
|
74 |
{ \
|
|
75 |
t1 = wv[h] + SHA256_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \
|
|
76 |
+ sha256_k[j] + w[j]; \
|
|
77 |
t2 = SHA256_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \
|
|
78 |
wv[d] += t1; \
|
|
79 |
wv[h] = t1 + t2; \
|
|
80 |
}
|
|
81 |
||
82 |
uint32_t sha256_h0[8] = |
|
83 |
{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, |
|
84 |
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; |
|
85 |
||
86 |
uint32_t sha256_k[64] = |
|
87 |
{0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
|
88 |
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
|
89 |
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
|
90 |
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
|
91 |
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
|
92 |
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
|
93 |
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
|
94 |
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
|
95 |
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
|
96 |
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
|
97 |
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, |
|
98 |
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
|
99 |
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, |
|
100 |
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
|
101 |
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
|
102 |
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; |
|
103 |
||
104 |
/* SHA-256 functions */
|
|
105 |
||
106 |
static void sha256_transf(sha256_ctx *ctx, const unsigned char *message, |
|
107 |
unsigned int block_nb) |
|
108 |
{
|
|
109 |
uint32_t w[64]; |
|
110 |
uint32_t wv[8]; |
|
111 |
uint32_t t1, t2; |
|
112 |
const unsigned char *sub_block; |
|
113 |
int i; |
|
114 |
||
115 |
int j; |
|
116 |
||
117 |
for (i = 0; i < (int) block_nb; i++) { |
|
118 |
sub_block = message + (i << 6); |
|
119 |
||
120 |
for (j = 0; j < 16; j++) { |
|
121 |
PACK32(&sub_block[j << 2], &w[j]); |
|
122 |
}
|
|
123 |
||
124 |
for (j = 16; j < 64; j++) { |
|
125 |
SHA256_SCR(j); |
|
126 |
}
|
|
127 |
||
128 |
for (j = 0; j < 8; j++) { |
|
129 |
wv[j] = ctx->h[j]; |
|
130 |
}
|
|
131 |
||
132 |
for (j = 0; j < 64; j++) { |
|
133 |
t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) |
|
134 |
+ sha256_k[j] + w[j]; |
|
135 |
t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); |
|
136 |
wv[7] = wv[6]; |
|
137 |
wv[6] = wv[5]; |
|
138 |
wv[5] = wv[4]; |
|
139 |
wv[4] = wv[3] + t1; |
|
140 |
wv[3] = wv[2]; |
|
141 |
wv[2] = wv[1]; |
|
142 |
wv[1] = wv[0]; |
|
143 |
wv[0] = t1 + t2; |
|
144 |
}
|
|
145 |
||
146 |
for (j = 0; j < 8; j++) { |
|
147 |
ctx->h[j] += wv[j]; |
|
148 |
}
|
|
149 |
}
|
|
150 |
}
|
|
151 |
||
152 |
void sha256(const unsigned char *message, unsigned int len, unsigned char *digest) |
|
153 |
{
|
|
154 |
sha256_ctx ctx; |
|
155 |
||
156 |
sha256_init(&ctx); |
|
157 |
sha256_update(&ctx, message, len); |
|
158 |
sha256_final(&ctx, digest); |
|
159 |
}
|
|
160 |
||
161 |
void sha256_init(sha256_ctx *ctx) |
|
162 |
{
|
|
163 |
int i; |
|
164 |
for (i = 0; i < 8; i++) { |
|
165 |
ctx->h[i] = sha256_h0[i]; |
|
166 |
}
|
|
167 |
||
168 |
ctx->len = 0; |
|
169 |
ctx->tot_len = 0; |
|
170 |
}
|
|
171 |
||
172 |
void sha256_update(sha256_ctx *ctx, const unsigned char *message, |
|
173 |
unsigned int len) |
|
174 |
{
|
|
175 |
unsigned int block_nb; |
|
176 |
unsigned int new_len, rem_len, tmp_len; |
|
177 |
const unsigned char *shifted_message; |
|
178 |
||
179 |
tmp_len = SHA256_BLOCK_SIZE - ctx->len; |
|
180 |
rem_len = len < tmp_len ? len : tmp_len; |
|
181 |
||
182 |
memcpy(&ctx->block[ctx->len], message, rem_len); |
|
183 |
||
184 |
if (ctx->len + len < SHA256_BLOCK_SIZE) { |
|
185 |
ctx->len += len; |
|
186 |
return; |
|
187 |
}
|
|
188 |
||
189 |
new_len = len - rem_len; |
|
190 |
block_nb = new_len / SHA256_BLOCK_SIZE; |
|
191 |
||
192 |
shifted_message = message + rem_len; |
|
193 |
||
194 |
sha256_transf(ctx, ctx->block, 1); |
|
195 |
sha256_transf(ctx, shifted_message, block_nb); |
|
196 |
||
197 |
rem_len = new_len % SHA256_BLOCK_SIZE; |
|
198 |
||
199 |
memcpy(ctx->block, &shifted_message[block_nb << 6], |
|
200 |
rem_len); |
|
201 |
||
202 |
ctx->len = rem_len; |
|
203 |
ctx->tot_len += (block_nb + 1) << 6; |
|
204 |
}
|
|
205 |
||
206 |
void sha256_final(sha256_ctx *ctx, unsigned char *digest) |
|
207 |
{
|
|
208 |
unsigned int block_nb; |
|
209 |
unsigned int pm_len; |
|
210 |
unsigned int len_b; |
|
211 |
||
212 |
int i; |
|
213 |
||
214 |
block_nb = (1 + ((SHA256_BLOCK_SIZE - 9) |
|
215 |
< (ctx->len % SHA256_BLOCK_SIZE))); |
|
216 |
||
217 |
len_b = (ctx->tot_len + ctx->len) << 3; |
|
218 |
pm_len = block_nb << 6; |
|
219 |
||
220 |
memset(ctx->block + ctx->len, 0, pm_len - ctx->len); |
|
221 |
ctx->block[ctx->len] = 0x80; |
|
222 |
UNPACK32(len_b, ctx->block + pm_len - 4); |
|
223 |
||
224 |
sha256_transf(ctx, ctx->block, block_nb); |
|
225 |
||
226 |
for (i = 0 ; i < 8; i++) { |
|
227 |
UNPACK32(ctx->h[i], &digest[i << 2]); |
|
228 |
}
|
|
229 |
}
|