Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 3 | * based on: |
| 4 | * FIPS-180-1 compliant SHA-1 implementation |
| 5 | * |
| 6 | * Copyright (C) 2003-2006 Christophe Devine |
| 7 | * |
Tom Rini | e237880 | 2016-01-14 22:05:13 -0500 | [diff] [blame] | 8 | * SPDX-License-Identifier: LGPL-2.1 |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 9 | */ |
| 10 | /* |
| 11 | * The SHA-1 standard was published by NIST in 1993. |
| 12 | * |
| 13 | * http://www.itl.nist.gov/fipspubs/fip180-1.htm |
| 14 | */ |
| 15 | |
| 16 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 17 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 18 | #endif |
| 19 | |
Bartlomiej Sieka | 222e2ca | 2008-04-25 13:54:02 +0200 | [diff] [blame] | 20 | #ifndef USE_HOSTCC |
| 21 | #include <common.h> |
Andy Fleming | 2126482 | 2008-06-11 18:10:20 -0500 | [diff] [blame] | 22 | #include <linux/string.h> |
Wolfgang Denk | a1ef057 | 2008-06-06 14:28:14 +0200 | [diff] [blame] | 23 | #else |
| 24 | #include <string.h> |
Bartlomiej Sieka | 222e2ca | 2008-04-25 13:54:02 +0200 | [diff] [blame] | 25 | #endif /* USE_HOSTCC */ |
| 26 | #include <watchdog.h> |
Jeroen Hofstee | bfe88fe | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 27 | #include <u-boot/sha1.h> |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 28 | |
Andrew Duda | 3db9ff0 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 29 | const uint8_t sha1_der_prefix[SHA1_DER_LEN] = { |
| 30 | 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, |
| 31 | 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 |
| 32 | }; |
| 33 | |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 34 | /* |
| 35 | * 32-bit integer manipulation macros (big endian) |
| 36 | */ |
| 37 | #ifndef GET_UINT32_BE |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 38 | #define GET_UINT32_BE(n,b,i) { \ |
| 39 | (n) = ( (unsigned long) (b)[(i) ] << 24 ) \ |
| 40 | | ( (unsigned long) (b)[(i) + 1] << 16 ) \ |
| 41 | | ( (unsigned long) (b)[(i) + 2] << 8 ) \ |
| 42 | | ( (unsigned long) (b)[(i) + 3] ); \ |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 43 | } |
| 44 | #endif |
| 45 | #ifndef PUT_UINT32_BE |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 46 | #define PUT_UINT32_BE(n,b,i) { \ |
| 47 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
| 48 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
| 49 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
| 50 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 51 | } |
| 52 | #endif |
| 53 | |
| 54 | /* |
| 55 | * SHA-1 context setup |
| 56 | */ |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 57 | void sha1_starts (sha1_context * ctx) |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 58 | { |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 59 | ctx->total[0] = 0; |
| 60 | ctx->total[1] = 0; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 61 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 62 | ctx->state[0] = 0x67452301; |
| 63 | ctx->state[1] = 0xEFCDAB89; |
| 64 | ctx->state[2] = 0x98BADCFE; |
| 65 | ctx->state[3] = 0x10325476; |
| 66 | ctx->state[4] = 0xC3D2E1F0; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Simon Glass | 5f60fa2 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 69 | static void sha1_process(sha1_context *ctx, const unsigned char data[64]) |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 70 | { |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 71 | unsigned long temp, W[16], A, B, C, D, E; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 72 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 73 | GET_UINT32_BE (W[0], data, 0); |
| 74 | GET_UINT32_BE (W[1], data, 4); |
| 75 | GET_UINT32_BE (W[2], data, 8); |
| 76 | GET_UINT32_BE (W[3], data, 12); |
| 77 | GET_UINT32_BE (W[4], data, 16); |
| 78 | GET_UINT32_BE (W[5], data, 20); |
| 79 | GET_UINT32_BE (W[6], data, 24); |
| 80 | GET_UINT32_BE (W[7], data, 28); |
| 81 | GET_UINT32_BE (W[8], data, 32); |
| 82 | GET_UINT32_BE (W[9], data, 36); |
| 83 | GET_UINT32_BE (W[10], data, 40); |
| 84 | GET_UINT32_BE (W[11], data, 44); |
| 85 | GET_UINT32_BE (W[12], data, 48); |
| 86 | GET_UINT32_BE (W[13], data, 52); |
| 87 | GET_UINT32_BE (W[14], data, 56); |
| 88 | GET_UINT32_BE (W[15], data, 60); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 89 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 90 | #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 91 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 92 | #define R(t) ( \ |
| 93 | temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \ |
| 94 | W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \ |
| 95 | ( W[t & 0x0F] = S(temp,1) ) \ |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 96 | ) |
| 97 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 98 | #define P(a,b,c,d,e,x) { \ |
| 99 | e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \ |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 102 | A = ctx->state[0]; |
| 103 | B = ctx->state[1]; |
| 104 | C = ctx->state[2]; |
| 105 | D = ctx->state[3]; |
| 106 | E = ctx->state[4]; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 107 | |
| 108 | #define F(x,y,z) (z ^ (x & (y ^ z))) |
| 109 | #define K 0x5A827999 |
| 110 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 111 | P (A, B, C, D, E, W[0]); |
| 112 | P (E, A, B, C, D, W[1]); |
| 113 | P (D, E, A, B, C, W[2]); |
| 114 | P (C, D, E, A, B, W[3]); |
| 115 | P (B, C, D, E, A, W[4]); |
| 116 | P (A, B, C, D, E, W[5]); |
| 117 | P (E, A, B, C, D, W[6]); |
| 118 | P (D, E, A, B, C, W[7]); |
| 119 | P (C, D, E, A, B, W[8]); |
| 120 | P (B, C, D, E, A, W[9]); |
| 121 | P (A, B, C, D, E, W[10]); |
| 122 | P (E, A, B, C, D, W[11]); |
| 123 | P (D, E, A, B, C, W[12]); |
| 124 | P (C, D, E, A, B, W[13]); |
| 125 | P (B, C, D, E, A, W[14]); |
| 126 | P (A, B, C, D, E, W[15]); |
| 127 | P (E, A, B, C, D, R (16)); |
| 128 | P (D, E, A, B, C, R (17)); |
| 129 | P (C, D, E, A, B, R (18)); |
| 130 | P (B, C, D, E, A, R (19)); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 131 | |
| 132 | #undef K |
| 133 | #undef F |
| 134 | |
| 135 | #define F(x,y,z) (x ^ y ^ z) |
| 136 | #define K 0x6ED9EBA1 |
| 137 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 138 | P (A, B, C, D, E, R (20)); |
| 139 | P (E, A, B, C, D, R (21)); |
| 140 | P (D, E, A, B, C, R (22)); |
| 141 | P (C, D, E, A, B, R (23)); |
| 142 | P (B, C, D, E, A, R (24)); |
| 143 | P (A, B, C, D, E, R (25)); |
| 144 | P (E, A, B, C, D, R (26)); |
| 145 | P (D, E, A, B, C, R (27)); |
| 146 | P (C, D, E, A, B, R (28)); |
| 147 | P (B, C, D, E, A, R (29)); |
| 148 | P (A, B, C, D, E, R (30)); |
| 149 | P (E, A, B, C, D, R (31)); |
| 150 | P (D, E, A, B, C, R (32)); |
| 151 | P (C, D, E, A, B, R (33)); |
| 152 | P (B, C, D, E, A, R (34)); |
| 153 | P (A, B, C, D, E, R (35)); |
| 154 | P (E, A, B, C, D, R (36)); |
| 155 | P (D, E, A, B, C, R (37)); |
| 156 | P (C, D, E, A, B, R (38)); |
| 157 | P (B, C, D, E, A, R (39)); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 158 | |
| 159 | #undef K |
| 160 | #undef F |
| 161 | |
| 162 | #define F(x,y,z) ((x & y) | (z & (x | y))) |
| 163 | #define K 0x8F1BBCDC |
| 164 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 165 | P (A, B, C, D, E, R (40)); |
| 166 | P (E, A, B, C, D, R (41)); |
| 167 | P (D, E, A, B, C, R (42)); |
| 168 | P (C, D, E, A, B, R (43)); |
| 169 | P (B, C, D, E, A, R (44)); |
| 170 | P (A, B, C, D, E, R (45)); |
| 171 | P (E, A, B, C, D, R (46)); |
| 172 | P (D, E, A, B, C, R (47)); |
| 173 | P (C, D, E, A, B, R (48)); |
| 174 | P (B, C, D, E, A, R (49)); |
| 175 | P (A, B, C, D, E, R (50)); |
| 176 | P (E, A, B, C, D, R (51)); |
| 177 | P (D, E, A, B, C, R (52)); |
| 178 | P (C, D, E, A, B, R (53)); |
| 179 | P (B, C, D, E, A, R (54)); |
| 180 | P (A, B, C, D, E, R (55)); |
| 181 | P (E, A, B, C, D, R (56)); |
| 182 | P (D, E, A, B, C, R (57)); |
| 183 | P (C, D, E, A, B, R (58)); |
| 184 | P (B, C, D, E, A, R (59)); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 185 | |
| 186 | #undef K |
| 187 | #undef F |
| 188 | |
| 189 | #define F(x,y,z) (x ^ y ^ z) |
| 190 | #define K 0xCA62C1D6 |
| 191 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 192 | P (A, B, C, D, E, R (60)); |
| 193 | P (E, A, B, C, D, R (61)); |
| 194 | P (D, E, A, B, C, R (62)); |
| 195 | P (C, D, E, A, B, R (63)); |
| 196 | P (B, C, D, E, A, R (64)); |
| 197 | P (A, B, C, D, E, R (65)); |
| 198 | P (E, A, B, C, D, R (66)); |
| 199 | P (D, E, A, B, C, R (67)); |
| 200 | P (C, D, E, A, B, R (68)); |
| 201 | P (B, C, D, E, A, R (69)); |
| 202 | P (A, B, C, D, E, R (70)); |
| 203 | P (E, A, B, C, D, R (71)); |
| 204 | P (D, E, A, B, C, R (72)); |
| 205 | P (C, D, E, A, B, R (73)); |
| 206 | P (B, C, D, E, A, R (74)); |
| 207 | P (A, B, C, D, E, R (75)); |
| 208 | P (E, A, B, C, D, R (76)); |
| 209 | P (D, E, A, B, C, R (77)); |
| 210 | P (C, D, E, A, B, R (78)); |
| 211 | P (B, C, D, E, A, R (79)); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 212 | |
| 213 | #undef K |
| 214 | #undef F |
| 215 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 216 | ctx->state[0] += A; |
| 217 | ctx->state[1] += B; |
| 218 | ctx->state[2] += C; |
| 219 | ctx->state[3] += D; |
| 220 | ctx->state[4] += E; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* |
| 224 | * SHA-1 process buffer |
| 225 | */ |
Simon Glass | 5f60fa2 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 226 | void sha1_update(sha1_context *ctx, const unsigned char *input, |
| 227 | unsigned int ilen) |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 228 | { |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 229 | int fill; |
| 230 | unsigned long left; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 231 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 232 | if (ilen <= 0) |
| 233 | return; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 234 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 235 | left = ctx->total[0] & 0x3F; |
| 236 | fill = 64 - left; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 237 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 238 | ctx->total[0] += ilen; |
| 239 | ctx->total[0] &= 0xFFFFFFFF; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 240 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 241 | if (ctx->total[0] < (unsigned long) ilen) |
| 242 | ctx->total[1]++; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 243 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 244 | if (left && ilen >= fill) { |
| 245 | memcpy ((void *) (ctx->buffer + left), (void *) input, fill); |
| 246 | sha1_process (ctx, ctx->buffer); |
| 247 | input += fill; |
| 248 | ilen -= fill; |
| 249 | left = 0; |
| 250 | } |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 251 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 252 | while (ilen >= 64) { |
| 253 | sha1_process (ctx, input); |
| 254 | input += 64; |
| 255 | ilen -= 64; |
| 256 | } |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 257 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 258 | if (ilen > 0) { |
| 259 | memcpy ((void *) (ctx->buffer + left), (void *) input, ilen); |
| 260 | } |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 263 | static const unsigned char sha1_padding[64] = { |
| 264 | 0x80, 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, |
| 267 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | /* |
| 271 | * SHA-1 final digest |
| 272 | */ |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 273 | void sha1_finish (sha1_context * ctx, unsigned char output[20]) |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 274 | { |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 275 | unsigned long last, padn; |
| 276 | unsigned long high, low; |
| 277 | unsigned char msglen[8]; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 278 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 279 | high = (ctx->total[0] >> 29) |
| 280 | | (ctx->total[1] << 3); |
| 281 | low = (ctx->total[0] << 3); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 282 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 283 | PUT_UINT32_BE (high, msglen, 0); |
| 284 | PUT_UINT32_BE (low, msglen, 4); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 285 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 286 | last = ctx->total[0] & 0x3F; |
| 287 | padn = (last < 56) ? (56 - last) : (120 - last); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 288 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 289 | sha1_update (ctx, (unsigned char *) sha1_padding, padn); |
| 290 | sha1_update (ctx, msglen, 8); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 291 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 292 | PUT_UINT32_BE (ctx->state[0], output, 0); |
| 293 | PUT_UINT32_BE (ctx->state[1], output, 4); |
| 294 | PUT_UINT32_BE (ctx->state[2], output, 8); |
| 295 | PUT_UINT32_BE (ctx->state[3], output, 12); |
| 296 | PUT_UINT32_BE (ctx->state[4], output, 16); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Output = SHA-1( input buffer ) |
| 301 | */ |
Simon Glass | 5f60fa2 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 302 | void sha1_csum(const unsigned char *input, unsigned int ilen, |
| 303 | unsigned char *output) |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 304 | { |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 305 | sha1_context ctx; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 306 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 307 | sha1_starts (&ctx); |
| 308 | sha1_update (&ctx, input, ilen); |
| 309 | sha1_finish (&ctx, output); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* |
Bartlomiej Sieka | da5045d | 2008-04-22 12:27:56 +0200 | [diff] [blame] | 313 | * Output = SHA-1( input buffer ). Trigger the watchdog every 'chunk_sz' |
| 314 | * bytes of input processed. |
| 315 | */ |
Simon Glass | 5f60fa2 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 316 | void sha1_csum_wd(const unsigned char *input, unsigned int ilen, |
| 317 | unsigned char *output, unsigned int chunk_sz) |
Bartlomiej Sieka | da5045d | 2008-04-22 12:27:56 +0200 | [diff] [blame] | 318 | { |
| 319 | sha1_context ctx; |
| 320 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
Simon Glass | 5f60fa2 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 321 | const unsigned char *end, *curr; |
Bartlomiej Sieka | da5045d | 2008-04-22 12:27:56 +0200 | [diff] [blame] | 322 | int chunk; |
| 323 | #endif |
| 324 | |
| 325 | sha1_starts (&ctx); |
| 326 | |
| 327 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
| 328 | curr = input; |
| 329 | end = input + ilen; |
| 330 | while (curr < end) { |
| 331 | chunk = end - curr; |
| 332 | if (chunk > chunk_sz) |
| 333 | chunk = chunk_sz; |
| 334 | sha1_update (&ctx, curr, chunk); |
| 335 | curr += chunk; |
| 336 | WATCHDOG_RESET (); |
| 337 | } |
| 338 | #else |
| 339 | sha1_update (&ctx, input, ilen); |
| 340 | #endif |
| 341 | |
| 342 | sha1_finish (&ctx, output); |
| 343 | } |
| 344 | |
| 345 | /* |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 346 | * Output = HMAC-SHA-1( input buffer, hmac key ) |
| 347 | */ |
Simon Glass | 5f60fa2 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 348 | void sha1_hmac(const unsigned char *key, int keylen, |
| 349 | const unsigned char *input, unsigned int ilen, |
| 350 | unsigned char *output) |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 351 | { |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 352 | int i; |
| 353 | sha1_context ctx; |
| 354 | unsigned char k_ipad[64]; |
| 355 | unsigned char k_opad[64]; |
| 356 | unsigned char tmpbuf[20]; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 357 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 358 | memset (k_ipad, 0x36, 64); |
| 359 | memset (k_opad, 0x5C, 64); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 360 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 361 | for (i = 0; i < keylen; i++) { |
| 362 | if (i >= 64) |
| 363 | break; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 364 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 365 | k_ipad[i] ^= key[i]; |
| 366 | k_opad[i] ^= key[i]; |
| 367 | } |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 368 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 369 | sha1_starts (&ctx); |
| 370 | sha1_update (&ctx, k_ipad, 64); |
| 371 | sha1_update (&ctx, input, ilen); |
| 372 | sha1_finish (&ctx, tmpbuf); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 373 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 374 | sha1_starts (&ctx); |
| 375 | sha1_update (&ctx, k_opad, 64); |
| 376 | sha1_update (&ctx, tmpbuf, 20); |
| 377 | sha1_finish (&ctx, output); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 378 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 379 | memset (k_ipad, 0, 64); |
| 380 | memset (k_opad, 0, 64); |
| 381 | memset (tmpbuf, 0, 20); |
| 382 | memset (&ctx, 0, sizeof (sha1_context)); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 385 | #ifdef SELF_TEST |
| 386 | /* |
| 387 | * FIPS-180-1 test vectors |
| 388 | */ |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 389 | static const char sha1_test_str[3][57] = { |
| 390 | {"abc"}, |
| 391 | {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}, |
| 392 | {""} |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 393 | }; |
| 394 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 395 | static const unsigned char sha1_test_sum[3][20] = { |
| 396 | {0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, |
| 397 | 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D}, |
| 398 | {0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, |
| 399 | 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1}, |
| 400 | {0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E, |
| 401 | 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F} |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 402 | }; |
| 403 | |
| 404 | /* |
| 405 | * Checkup routine |
| 406 | */ |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 407 | int sha1_self_test (void) |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 408 | { |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 409 | int i, j; |
| 410 | unsigned char buf[1000]; |
| 411 | unsigned char sha1sum[20]; |
| 412 | sha1_context ctx; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 413 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 414 | for (i = 0; i < 3; i++) { |
| 415 | printf (" SHA-1 test #%d: ", i + 1); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 416 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 417 | sha1_starts (&ctx); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 418 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 419 | if (i < 2) |
| 420 | sha1_update (&ctx, (unsigned char *) sha1_test_str[i], |
| 421 | strlen (sha1_test_str[i])); |
| 422 | else { |
| 423 | memset (buf, 'a', 1000); |
| 424 | for (j = 0; j < 1000; j++) |
| 425 | sha1_update (&ctx, buf, 1000); |
| 426 | } |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 427 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 428 | sha1_finish (&ctx, sha1sum); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 429 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 430 | if (memcmp (sha1sum, sha1_test_sum[i], 20) != 0) { |
| 431 | printf ("failed\n"); |
| 432 | return (1); |
| 433 | } |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 434 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 435 | printf ("passed\n"); |
| 436 | } |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 437 | |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 438 | printf ("\n"); |
| 439 | return (0); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 440 | } |
| 441 | #else |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 442 | int sha1_self_test (void) |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 443 | { |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 444 | return (0); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 445 | } |
| 446 | #endif |