1 // SPDX-License-Identifier: LGPL-2.1
3 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
5 * FIPS-180-1 compliant SHA-1 implementation
7 * Copyright (C) 2003-2006 Christophe Devine
10 * The SHA-1 standard was published by NIST in 1993.
12 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
15 #ifndef _CRT_SECURE_NO_DEPRECATE
16 #define _CRT_SECURE_NO_DEPRECATE 1
21 #include <linux/string.h>
24 #endif /* USE_HOSTCC */
26 #include <u-boot/sha1.h>
28 const uint8_t sha1_der_prefix[SHA1_DER_LEN] = {
29 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,
30 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14
34 * 32-bit integer manipulation macros (big endian)
37 #define GET_UINT32_BE(n,b,i) { \
38 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
39 | ( (unsigned long) (b)[(i) + 1] << 16 ) \
40 | ( (unsigned long) (b)[(i) + 2] << 8 ) \
41 | ( (unsigned long) (b)[(i) + 3] ); \
45 #define PUT_UINT32_BE(n,b,i) { \
46 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
47 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
48 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
49 (b)[(i) + 3] = (unsigned char) ( (n) ); \
56 void sha1_starts (sha1_context * ctx)
61 ctx->state[0] = 0x67452301;
62 ctx->state[1] = 0xEFCDAB89;
63 ctx->state[2] = 0x98BADCFE;
64 ctx->state[3] = 0x10325476;
65 ctx->state[4] = 0xC3D2E1F0;
68 static void sha1_process(sha1_context *ctx, const unsigned char data[64])
70 unsigned long temp, W[16], A, B, C, D, E;
72 GET_UINT32_BE (W[0], data, 0);
73 GET_UINT32_BE (W[1], data, 4);
74 GET_UINT32_BE (W[2], data, 8);
75 GET_UINT32_BE (W[3], data, 12);
76 GET_UINT32_BE (W[4], data, 16);
77 GET_UINT32_BE (W[5], data, 20);
78 GET_UINT32_BE (W[6], data, 24);
79 GET_UINT32_BE (W[7], data, 28);
80 GET_UINT32_BE (W[8], data, 32);
81 GET_UINT32_BE (W[9], data, 36);
82 GET_UINT32_BE (W[10], data, 40);
83 GET_UINT32_BE (W[11], data, 44);
84 GET_UINT32_BE (W[12], data, 48);
85 GET_UINT32_BE (W[13], data, 52);
86 GET_UINT32_BE (W[14], data, 56);
87 GET_UINT32_BE (W[15], data, 60);
89 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
92 temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \
93 W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \
94 ( W[t & 0x0F] = S(temp,1) ) \
97 #define P(a,b,c,d,e,x) { \
98 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
107 #define F(x,y,z) (z ^ (x & (y ^ z)))
110 P (A, B, C, D, E, W[0]);
111 P (E, A, B, C, D, W[1]);
112 P (D, E, A, B, C, W[2]);
113 P (C, D, E, A, B, W[3]);
114 P (B, C, D, E, A, W[4]);
115 P (A, B, C, D, E, W[5]);
116 P (E, A, B, C, D, W[6]);
117 P (D, E, A, B, C, W[7]);
118 P (C, D, E, A, B, W[8]);
119 P (B, C, D, E, A, W[9]);
120 P (A, B, C, D, E, W[10]);
121 P (E, A, B, C, D, W[11]);
122 P (D, E, A, B, C, W[12]);
123 P (C, D, E, A, B, W[13]);
124 P (B, C, D, E, A, W[14]);
125 P (A, B, C, D, E, W[15]);
126 P (E, A, B, C, D, R (16));
127 P (D, E, A, B, C, R (17));
128 P (C, D, E, A, B, R (18));
129 P (B, C, D, E, A, R (19));
134 #define F(x,y,z) (x ^ y ^ z)
137 P (A, B, C, D, E, R (20));
138 P (E, A, B, C, D, R (21));
139 P (D, E, A, B, C, R (22));
140 P (C, D, E, A, B, R (23));
141 P (B, C, D, E, A, R (24));
142 P (A, B, C, D, E, R (25));
143 P (E, A, B, C, D, R (26));
144 P (D, E, A, B, C, R (27));
145 P (C, D, E, A, B, R (28));
146 P (B, C, D, E, A, R (29));
147 P (A, B, C, D, E, R (30));
148 P (E, A, B, C, D, R (31));
149 P (D, E, A, B, C, R (32));
150 P (C, D, E, A, B, R (33));
151 P (B, C, D, E, A, R (34));
152 P (A, B, C, D, E, R (35));
153 P (E, A, B, C, D, R (36));
154 P (D, E, A, B, C, R (37));
155 P (C, D, E, A, B, R (38));
156 P (B, C, D, E, A, R (39));
161 #define F(x,y,z) ((x & y) | (z & (x | y)))
164 P (A, B, C, D, E, R (40));
165 P (E, A, B, C, D, R (41));
166 P (D, E, A, B, C, R (42));
167 P (C, D, E, A, B, R (43));
168 P (B, C, D, E, A, R (44));
169 P (A, B, C, D, E, R (45));
170 P (E, A, B, C, D, R (46));
171 P (D, E, A, B, C, R (47));
172 P (C, D, E, A, B, R (48));
173 P (B, C, D, E, A, R (49));
174 P (A, B, C, D, E, R (50));
175 P (E, A, B, C, D, R (51));
176 P (D, E, A, B, C, R (52));
177 P (C, D, E, A, B, R (53));
178 P (B, C, D, E, A, R (54));
179 P (A, B, C, D, E, R (55));
180 P (E, A, B, C, D, R (56));
181 P (D, E, A, B, C, R (57));
182 P (C, D, E, A, B, R (58));
183 P (B, C, D, E, A, R (59));
188 #define F(x,y,z) (x ^ y ^ z)
191 P (A, B, C, D, E, R (60));
192 P (E, A, B, C, D, R (61));
193 P (D, E, A, B, C, R (62));
194 P (C, D, E, A, B, R (63));
195 P (B, C, D, E, A, R (64));
196 P (A, B, C, D, E, R (65));
197 P (E, A, B, C, D, R (66));
198 P (D, E, A, B, C, R (67));
199 P (C, D, E, A, B, R (68));
200 P (B, C, D, E, A, R (69));
201 P (A, B, C, D, E, R (70));
202 P (E, A, B, C, D, R (71));
203 P (D, E, A, B, C, R (72));
204 P (C, D, E, A, B, R (73));
205 P (B, C, D, E, A, R (74));
206 P (A, B, C, D, E, R (75));
207 P (E, A, B, C, D, R (76));
208 P (D, E, A, B, C, R (77));
209 P (C, D, E, A, B, R (78));
210 P (B, C, D, E, A, R (79));
223 * SHA-1 process buffer
225 void sha1_update(sha1_context *ctx, const unsigned char *input,
234 left = ctx->total[0] & 0x3F;
237 ctx->total[0] += ilen;
238 ctx->total[0] &= 0xFFFFFFFF;
240 if (ctx->total[0] < (unsigned long) ilen)
243 if (left && ilen >= fill) {
244 memcpy ((void *) (ctx->buffer + left), (void *) input, fill);
245 sha1_process (ctx, ctx->buffer);
252 sha1_process (ctx, input);
258 memcpy ((void *) (ctx->buffer + left), (void *) input, ilen);
262 static const unsigned char sha1_padding[64] = {
263 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
264 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
265 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
272 void sha1_finish (sha1_context * ctx, unsigned char output[20])
274 unsigned long last, padn;
275 unsigned long high, low;
276 unsigned char msglen[8];
278 high = (ctx->total[0] >> 29)
279 | (ctx->total[1] << 3);
280 low = (ctx->total[0] << 3);
282 PUT_UINT32_BE (high, msglen, 0);
283 PUT_UINT32_BE (low, msglen, 4);
285 last = ctx->total[0] & 0x3F;
286 padn = (last < 56) ? (56 - last) : (120 - last);
288 sha1_update (ctx, (unsigned char *) sha1_padding, padn);
289 sha1_update (ctx, msglen, 8);
291 PUT_UINT32_BE (ctx->state[0], output, 0);
292 PUT_UINT32_BE (ctx->state[1], output, 4);
293 PUT_UINT32_BE (ctx->state[2], output, 8);
294 PUT_UINT32_BE (ctx->state[3], output, 12);
295 PUT_UINT32_BE (ctx->state[4], output, 16);
299 * Output = SHA-1( input buffer )
301 void sha1_csum(const unsigned char *input, unsigned int ilen,
302 unsigned char *output)
307 sha1_update (&ctx, input, ilen);
308 sha1_finish (&ctx, output);
312 * Output = SHA-1( input buffer ). Trigger the watchdog every 'chunk_sz'
313 * bytes of input processed.
315 void sha1_csum_wd(const unsigned char *input, unsigned int ilen,
316 unsigned char *output, unsigned int chunk_sz)
319 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
320 const unsigned char *end, *curr;
326 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
331 if (chunk > chunk_sz)
333 sha1_update (&ctx, curr, chunk);
338 sha1_update (&ctx, input, ilen);
341 sha1_finish (&ctx, output);
345 * Output = HMAC-SHA-1( input buffer, hmac key )
347 void sha1_hmac(const unsigned char *key, int keylen,
348 const unsigned char *input, unsigned int ilen,
349 unsigned char *output)
353 unsigned char k_ipad[64];
354 unsigned char k_opad[64];
355 unsigned char tmpbuf[20];
357 memset (k_ipad, 0x36, 64);
358 memset (k_opad, 0x5C, 64);
360 for (i = 0; i < keylen; i++) {
369 sha1_update (&ctx, k_ipad, 64);
370 sha1_update (&ctx, input, ilen);
371 sha1_finish (&ctx, tmpbuf);
374 sha1_update (&ctx, k_opad, 64);
375 sha1_update (&ctx, tmpbuf, 20);
376 sha1_finish (&ctx, output);
378 memset (k_ipad, 0, 64);
379 memset (k_opad, 0, 64);
380 memset (tmpbuf, 0, 20);
381 memset (&ctx, 0, sizeof (sha1_context));
386 * FIPS-180-1 test vectors
388 static const char sha1_test_str[3][57] = {
390 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
394 static const unsigned char sha1_test_sum[3][20] = {
395 {0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
396 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D},
397 {0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
398 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1},
399 {0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
400 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F}
406 int sha1_self_test (void)
409 unsigned char buf[1000];
410 unsigned char sha1sum[20];
413 for (i = 0; i < 3; i++) {
414 printf (" SHA-1 test #%d: ", i + 1);
419 sha1_update (&ctx, (unsigned char *) sha1_test_str[i],
420 strlen (sha1_test_str[i]));
422 memset (buf, 'a', 1000);
423 for (j = 0; j < 1000; j++)
424 sha1_update (&ctx, buf, 1000);
427 sha1_finish (&ctx, sha1sum);
429 if (memcmp (sha1sum, sha1_test_sum[i], 20) != 0) {
441 int sha1_self_test (void)