Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1 |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 4 | * based on: |
| 5 | * FIPS-180-1 compliant SHA-1 implementation |
| 6 | * |
| 7 | * Copyright (C) 2003-2006 Christophe Devine |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 8 | */ |
| 9 | /* |
| 10 | * The SHA-1 standard was published by NIST in 1993. |
| 11 | * |
| 12 | * http://www.itl.nist.gov/fipspubs/fip180-1.htm |
| 13 | */ |
| 14 | |
| 15 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 16 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 17 | #endif |
| 18 | |
Bartlomiej Sieka | 7590378 | 2008-04-25 13:54:02 +0200 | [diff] [blame] | 19 | #ifndef USE_HOSTCC |
| 20 | #include <common.h> |
Andy Fleming | a94f22f | 2008-06-11 18:10:20 -0500 | [diff] [blame] | 21 | #include <linux/string.h> |
Wolfgang Denk | 338cc03 | 2008-06-06 14:28:14 +0200 | [diff] [blame] | 22 | #else |
| 23 | #include <string.h> |
Bartlomiej Sieka | 7590378 | 2008-04-25 13:54:02 +0200 | [diff] [blame] | 24 | #endif /* USE_HOSTCC */ |
| 25 | #include <watchdog.h> |
Jeroen Hofstee | 2b9912e | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 26 | #include <u-boot/sha1.h> |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 27 | |
Andrew Duda | da29f29 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 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 |
| 31 | }; |
| 32 | |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 33 | /* |
| 34 | * 32-bit integer manipulation macros (big endian) |
| 35 | */ |
| 36 | #ifndef GET_UINT32_BE |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 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] ); \ |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 42 | } |
| 43 | #endif |
| 44 | #ifndef PUT_UINT32_BE |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 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) ); \ |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 50 | } |
| 51 | #endif |
| 52 | |
| 53 | /* |
| 54 | * SHA-1 context setup |
| 55 | */ |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 56 | void sha1_starts (sha1_context * ctx) |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 57 | { |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 58 | ctx->total[0] = 0; |
| 59 | ctx->total[1] = 0; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 60 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 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; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Simon Glass | a7d1d76 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 68 | static void sha1_process(sha1_context *ctx, const unsigned char data[64]) |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 69 | { |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 70 | unsigned long temp, W[16], A, B, C, D, E; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 71 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 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); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 88 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 89 | #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 90 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 91 | #define R(t) ( \ |
| 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) ) \ |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 95 | ) |
| 96 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 97 | #define P(a,b,c,d,e,x) { \ |
| 98 | e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \ |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 101 | A = ctx->state[0]; |
| 102 | B = ctx->state[1]; |
| 103 | C = ctx->state[2]; |
| 104 | D = ctx->state[3]; |
| 105 | E = ctx->state[4]; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 106 | |
| 107 | #define F(x,y,z) (z ^ (x & (y ^ z))) |
| 108 | #define K 0x5A827999 |
| 109 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 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)); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 130 | |
| 131 | #undef K |
| 132 | #undef F |
| 133 | |
| 134 | #define F(x,y,z) (x ^ y ^ z) |
| 135 | #define K 0x6ED9EBA1 |
| 136 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 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)); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 157 | |
| 158 | #undef K |
| 159 | #undef F |
| 160 | |
| 161 | #define F(x,y,z) ((x & y) | (z & (x | y))) |
| 162 | #define K 0x8F1BBCDC |
| 163 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 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)); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 184 | |
| 185 | #undef K |
| 186 | #undef F |
| 187 | |
| 188 | #define F(x,y,z) (x ^ y ^ z) |
| 189 | #define K 0xCA62C1D6 |
| 190 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 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)); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 211 | |
| 212 | #undef K |
| 213 | #undef F |
| 214 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 215 | ctx->state[0] += A; |
| 216 | ctx->state[1] += B; |
| 217 | ctx->state[2] += C; |
| 218 | ctx->state[3] += D; |
| 219 | ctx->state[4] += E; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* |
| 223 | * SHA-1 process buffer |
| 224 | */ |
Simon Glass | a7d1d76 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 225 | void sha1_update(sha1_context *ctx, const unsigned char *input, |
| 226 | unsigned int ilen) |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 227 | { |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 228 | int fill; |
| 229 | unsigned long left; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 230 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 231 | if (ilen <= 0) |
| 232 | return; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 233 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 234 | left = ctx->total[0] & 0x3F; |
| 235 | fill = 64 - left; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 236 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 237 | ctx->total[0] += ilen; |
| 238 | ctx->total[0] &= 0xFFFFFFFF; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 239 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 240 | if (ctx->total[0] < (unsigned long) ilen) |
| 241 | ctx->total[1]++; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 242 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 243 | if (left && ilen >= fill) { |
| 244 | memcpy ((void *) (ctx->buffer + left), (void *) input, fill); |
| 245 | sha1_process (ctx, ctx->buffer); |
| 246 | input += fill; |
| 247 | ilen -= fill; |
| 248 | left = 0; |
| 249 | } |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 250 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 251 | while (ilen >= 64) { |
| 252 | sha1_process (ctx, input); |
| 253 | input += 64; |
| 254 | ilen -= 64; |
| 255 | } |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 256 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 257 | if (ilen > 0) { |
| 258 | memcpy ((void *) (ctx->buffer + left), (void *) input, ilen); |
| 259 | } |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 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 |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | /* |
| 270 | * SHA-1 final digest |
| 271 | */ |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 272 | void sha1_finish (sha1_context * ctx, unsigned char output[20]) |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 273 | { |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 274 | unsigned long last, padn; |
| 275 | unsigned long high, low; |
| 276 | unsigned char msglen[8]; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 277 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 278 | high = (ctx->total[0] >> 29) |
| 279 | | (ctx->total[1] << 3); |
| 280 | low = (ctx->total[0] << 3); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 281 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 282 | PUT_UINT32_BE (high, msglen, 0); |
| 283 | PUT_UINT32_BE (low, msglen, 4); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 284 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 285 | last = ctx->total[0] & 0x3F; |
| 286 | padn = (last < 56) ? (56 - last) : (120 - last); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 287 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 288 | sha1_update (ctx, (unsigned char *) sha1_padding, padn); |
| 289 | sha1_update (ctx, msglen, 8); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 290 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 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); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Output = SHA-1( input buffer ) |
| 300 | */ |
Simon Glass | a7d1d76 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 301 | void sha1_csum(const unsigned char *input, unsigned int ilen, |
| 302 | unsigned char *output) |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 303 | { |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 304 | sha1_context ctx; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 305 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 306 | sha1_starts (&ctx); |
| 307 | sha1_update (&ctx, input, ilen); |
| 308 | sha1_finish (&ctx, output); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* |
Bartlomiej Sieka | 215b01b | 2008-04-22 12:27:56 +0200 | [diff] [blame] | 312 | * Output = SHA-1( input buffer ). Trigger the watchdog every 'chunk_sz' |
| 313 | * bytes of input processed. |
| 314 | */ |
Simon Glass | a7d1d76 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 315 | void sha1_csum_wd(const unsigned char *input, unsigned int ilen, |
| 316 | unsigned char *output, unsigned int chunk_sz) |
Bartlomiej Sieka | 215b01b | 2008-04-22 12:27:56 +0200 | [diff] [blame] | 317 | { |
| 318 | sha1_context ctx; |
| 319 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
Simon Glass | a7d1d76 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 320 | const unsigned char *end, *curr; |
Bartlomiej Sieka | 215b01b | 2008-04-22 12:27:56 +0200 | [diff] [blame] | 321 | int chunk; |
| 322 | #endif |
| 323 | |
| 324 | sha1_starts (&ctx); |
| 325 | |
| 326 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
| 327 | curr = input; |
| 328 | end = input + ilen; |
| 329 | while (curr < end) { |
| 330 | chunk = end - curr; |
| 331 | if (chunk > chunk_sz) |
| 332 | chunk = chunk_sz; |
| 333 | sha1_update (&ctx, curr, chunk); |
| 334 | curr += chunk; |
| 335 | WATCHDOG_RESET (); |
| 336 | } |
| 337 | #else |
| 338 | sha1_update (&ctx, input, ilen); |
| 339 | #endif |
| 340 | |
| 341 | sha1_finish (&ctx, output); |
| 342 | } |
| 343 | |
| 344 | /* |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 345 | * Output = HMAC-SHA-1( input buffer, hmac key ) |
| 346 | */ |
Simon Glass | a7d1d76 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 347 | void sha1_hmac(const unsigned char *key, int keylen, |
| 348 | const unsigned char *input, unsigned int ilen, |
| 349 | unsigned char *output) |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 350 | { |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 351 | int i; |
| 352 | sha1_context ctx; |
| 353 | unsigned char k_ipad[64]; |
| 354 | unsigned char k_opad[64]; |
| 355 | unsigned char tmpbuf[20]; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 356 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 357 | memset (k_ipad, 0x36, 64); |
| 358 | memset (k_opad, 0x5C, 64); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 359 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 360 | for (i = 0; i < keylen; i++) { |
| 361 | if (i >= 64) |
| 362 | break; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 363 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 364 | k_ipad[i] ^= key[i]; |
| 365 | k_opad[i] ^= key[i]; |
| 366 | } |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 367 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 368 | sha1_starts (&ctx); |
| 369 | sha1_update (&ctx, k_ipad, 64); |
| 370 | sha1_update (&ctx, input, ilen); |
| 371 | sha1_finish (&ctx, tmpbuf); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 372 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 373 | sha1_starts (&ctx); |
| 374 | sha1_update (&ctx, k_opad, 64); |
| 375 | sha1_update (&ctx, tmpbuf, 20); |
| 376 | sha1_finish (&ctx, output); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 377 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 378 | memset (k_ipad, 0, 64); |
| 379 | memset (k_opad, 0, 64); |
| 380 | memset (tmpbuf, 0, 20); |
| 381 | memset (&ctx, 0, sizeof (sha1_context)); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 384 | #ifdef SELF_TEST |
| 385 | /* |
| 386 | * FIPS-180-1 test vectors |
| 387 | */ |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 388 | static const char sha1_test_str[3][57] = { |
| 389 | {"abc"}, |
| 390 | {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}, |
| 391 | {""} |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 392 | }; |
| 393 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 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} |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | /* |
| 404 | * Checkup routine |
| 405 | */ |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 406 | int sha1_self_test (void) |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 407 | { |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 408 | int i, j; |
| 409 | unsigned char buf[1000]; |
| 410 | unsigned char sha1sum[20]; |
| 411 | sha1_context ctx; |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 412 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 413 | for (i = 0; i < 3; i++) { |
| 414 | printf (" SHA-1 test #%d: ", i + 1); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 415 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 416 | sha1_starts (&ctx); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 417 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 418 | if (i < 2) |
| 419 | sha1_update (&ctx, (unsigned char *) sha1_test_str[i], |
| 420 | strlen (sha1_test_str[i])); |
| 421 | else { |
| 422 | memset (buf, 'a', 1000); |
| 423 | for (j = 0; j < 1000; j++) |
| 424 | sha1_update (&ctx, buf, 1000); |
| 425 | } |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 426 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 427 | sha1_finish (&ctx, sha1sum); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 428 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 429 | if (memcmp (sha1sum, sha1_test_sum[i], 20) != 0) { |
| 430 | printf ("failed\n"); |
| 431 | return (1); |
| 432 | } |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 433 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 434 | printf ("passed\n"); |
| 435 | } |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 436 | |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 437 | printf ("\n"); |
| 438 | return (0); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 439 | } |
| 440 | #else |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 441 | int sha1_self_test (void) |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 442 | { |
Wolfgang Denk | 4ef218f | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 443 | return (0); |
Heiko Schocher | 566a494 | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 444 | } |
| 445 | #endif |