Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368
Dependents: UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more
services/mysql/sha1.c@0:350011bf8be7, 2010-11-23 (annotated)
- Committer:
- simon
- Date:
- Tue Nov 23 14:15:36 2010 +0000
- Revision:
- 0:350011bf8be7
Experimental version for testing UDP
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:350011bf8be7 | 1 | /* |
simon | 0:350011bf8be7 | 2 | * FIPS-180-1 compliant SHA-1 implementation |
simon | 0:350011bf8be7 | 3 | * |
simon | 0:350011bf8be7 | 4 | * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org> |
simon | 0:350011bf8be7 | 5 | * All rights reserved. |
simon | 0:350011bf8be7 | 6 | * |
simon | 0:350011bf8be7 | 7 | * This program is free software; you can redistribute it and/or modify |
simon | 0:350011bf8be7 | 8 | * it under the terms of the GNU General Public License as published by |
simon | 0:350011bf8be7 | 9 | * the Free Software Foundation; either version 2 of the License, or |
simon | 0:350011bf8be7 | 10 | * (at your option) any later version. |
simon | 0:350011bf8be7 | 11 | * |
simon | 0:350011bf8be7 | 12 | * This program is distributed in the hope that it will be useful, |
simon | 0:350011bf8be7 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
simon | 0:350011bf8be7 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
simon | 0:350011bf8be7 | 15 | * GNU General Public License for more details. |
simon | 0:350011bf8be7 | 16 | * |
simon | 0:350011bf8be7 | 17 | * You should have received a copy of the GNU General Public License along |
simon | 0:350011bf8be7 | 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
simon | 0:350011bf8be7 | 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
simon | 0:350011bf8be7 | 20 | */ |
simon | 0:350011bf8be7 | 21 | /* |
simon | 0:350011bf8be7 | 22 | * The SHA-1 standard was published by NIST in 1993. |
simon | 0:350011bf8be7 | 23 | * |
simon | 0:350011bf8be7 | 24 | * http://www.itl.nist.gov/fipspubs/fip180-1.htm |
simon | 0:350011bf8be7 | 25 | */ |
simon | 0:350011bf8be7 | 26 | |
simon | 0:350011bf8be7 | 27 | #include "sha1config.h" |
simon | 0:350011bf8be7 | 28 | |
simon | 0:350011bf8be7 | 29 | #if defined(POLARSSL_SHA1_C) |
simon | 0:350011bf8be7 | 30 | |
simon | 0:350011bf8be7 | 31 | #include "sha1.h" |
simon | 0:350011bf8be7 | 32 | |
simon | 0:350011bf8be7 | 33 | #include <string.h> |
simon | 0:350011bf8be7 | 34 | #include <stdio.h> |
simon | 0:350011bf8be7 | 35 | |
simon | 0:350011bf8be7 | 36 | /* |
simon | 0:350011bf8be7 | 37 | * 32-bit integer manipulation macros (big endian) |
simon | 0:350011bf8be7 | 38 | */ |
simon | 0:350011bf8be7 | 39 | #ifndef GET_ULONG_BE |
simon | 0:350011bf8be7 | 40 | #define GET_ULONG_BE(n,b,i) \ |
simon | 0:350011bf8be7 | 41 | { \ |
simon | 0:350011bf8be7 | 42 | (n) = ( (unsigned long) (b)[(i) ] << 24 ) \ |
simon | 0:350011bf8be7 | 43 | | ( (unsigned long) (b)[(i) + 1] << 16 ) \ |
simon | 0:350011bf8be7 | 44 | | ( (unsigned long) (b)[(i) + 2] << 8 ) \ |
simon | 0:350011bf8be7 | 45 | | ( (unsigned long) (b)[(i) + 3] ); \ |
simon | 0:350011bf8be7 | 46 | } |
simon | 0:350011bf8be7 | 47 | #endif |
simon | 0:350011bf8be7 | 48 | |
simon | 0:350011bf8be7 | 49 | #ifndef PUT_ULONG_BE |
simon | 0:350011bf8be7 | 50 | #define PUT_ULONG_BE(n,b,i) \ |
simon | 0:350011bf8be7 | 51 | { \ |
simon | 0:350011bf8be7 | 52 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
simon | 0:350011bf8be7 | 53 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
simon | 0:350011bf8be7 | 54 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
simon | 0:350011bf8be7 | 55 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
simon | 0:350011bf8be7 | 56 | } |
simon | 0:350011bf8be7 | 57 | #endif |
simon | 0:350011bf8be7 | 58 | |
simon | 0:350011bf8be7 | 59 | /* |
simon | 0:350011bf8be7 | 60 | * SHA-1 context setup |
simon | 0:350011bf8be7 | 61 | */ |
simon | 0:350011bf8be7 | 62 | void sha1_starts( sha1_context *ctx ) |
simon | 0:350011bf8be7 | 63 | { |
simon | 0:350011bf8be7 | 64 | ctx->total[0] = 0; |
simon | 0:350011bf8be7 | 65 | ctx->total[1] = 0; |
simon | 0:350011bf8be7 | 66 | |
simon | 0:350011bf8be7 | 67 | ctx->state[0] = 0x67452301; |
simon | 0:350011bf8be7 | 68 | ctx->state[1] = 0xEFCDAB89; |
simon | 0:350011bf8be7 | 69 | ctx->state[2] = 0x98BADCFE; |
simon | 0:350011bf8be7 | 70 | ctx->state[3] = 0x10325476; |
simon | 0:350011bf8be7 | 71 | ctx->state[4] = 0xC3D2E1F0; |
simon | 0:350011bf8be7 | 72 | } |
simon | 0:350011bf8be7 | 73 | |
simon | 0:350011bf8be7 | 74 | static void sha1_process( sha1_context *ctx, const unsigned char data[64] ) |
simon | 0:350011bf8be7 | 75 | { |
simon | 0:350011bf8be7 | 76 | unsigned long temp, W[16], A, B, C, D, E; |
simon | 0:350011bf8be7 | 77 | |
simon | 0:350011bf8be7 | 78 | GET_ULONG_BE( W[ 0], data, 0 ); |
simon | 0:350011bf8be7 | 79 | GET_ULONG_BE( W[ 1], data, 4 ); |
simon | 0:350011bf8be7 | 80 | GET_ULONG_BE( W[ 2], data, 8 ); |
simon | 0:350011bf8be7 | 81 | GET_ULONG_BE( W[ 3], data, 12 ); |
simon | 0:350011bf8be7 | 82 | GET_ULONG_BE( W[ 4], data, 16 ); |
simon | 0:350011bf8be7 | 83 | GET_ULONG_BE( W[ 5], data, 20 ); |
simon | 0:350011bf8be7 | 84 | GET_ULONG_BE( W[ 6], data, 24 ); |
simon | 0:350011bf8be7 | 85 | GET_ULONG_BE( W[ 7], data, 28 ); |
simon | 0:350011bf8be7 | 86 | GET_ULONG_BE( W[ 8], data, 32 ); |
simon | 0:350011bf8be7 | 87 | GET_ULONG_BE( W[ 9], data, 36 ); |
simon | 0:350011bf8be7 | 88 | GET_ULONG_BE( W[10], data, 40 ); |
simon | 0:350011bf8be7 | 89 | GET_ULONG_BE( W[11], data, 44 ); |
simon | 0:350011bf8be7 | 90 | GET_ULONG_BE( W[12], data, 48 ); |
simon | 0:350011bf8be7 | 91 | GET_ULONG_BE( W[13], data, 52 ); |
simon | 0:350011bf8be7 | 92 | GET_ULONG_BE( W[14], data, 56 ); |
simon | 0:350011bf8be7 | 93 | GET_ULONG_BE( W[15], data, 60 ); |
simon | 0:350011bf8be7 | 94 | |
simon | 0:350011bf8be7 | 95 | #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) |
simon | 0:350011bf8be7 | 96 | |
simon | 0:350011bf8be7 | 97 | #define R(t) \ |
simon | 0:350011bf8be7 | 98 | ( \ |
simon | 0:350011bf8be7 | 99 | temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \ |
simon | 0:350011bf8be7 | 100 | W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \ |
simon | 0:350011bf8be7 | 101 | ( W[t & 0x0F] = S(temp,1) ) \ |
simon | 0:350011bf8be7 | 102 | ) |
simon | 0:350011bf8be7 | 103 | |
simon | 0:350011bf8be7 | 104 | #define P(a,b,c,d,e,x) \ |
simon | 0:350011bf8be7 | 105 | { \ |
simon | 0:350011bf8be7 | 106 | e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \ |
simon | 0:350011bf8be7 | 107 | } |
simon | 0:350011bf8be7 | 108 | |
simon | 0:350011bf8be7 | 109 | A = ctx->state[0]; |
simon | 0:350011bf8be7 | 110 | B = ctx->state[1]; |
simon | 0:350011bf8be7 | 111 | C = ctx->state[2]; |
simon | 0:350011bf8be7 | 112 | D = ctx->state[3]; |
simon | 0:350011bf8be7 | 113 | E = ctx->state[4]; |
simon | 0:350011bf8be7 | 114 | |
simon | 0:350011bf8be7 | 115 | #define F(x,y,z) (z ^ (x & (y ^ z))) |
simon | 0:350011bf8be7 | 116 | #define K 0x5A827999 |
simon | 0:350011bf8be7 | 117 | |
simon | 0:350011bf8be7 | 118 | P( A, B, C, D, E, W[0] ); |
simon | 0:350011bf8be7 | 119 | P( E, A, B, C, D, W[1] ); |
simon | 0:350011bf8be7 | 120 | P( D, E, A, B, C, W[2] ); |
simon | 0:350011bf8be7 | 121 | P( C, D, E, A, B, W[3] ); |
simon | 0:350011bf8be7 | 122 | P( B, C, D, E, A, W[4] ); |
simon | 0:350011bf8be7 | 123 | P( A, B, C, D, E, W[5] ); |
simon | 0:350011bf8be7 | 124 | P( E, A, B, C, D, W[6] ); |
simon | 0:350011bf8be7 | 125 | P( D, E, A, B, C, W[7] ); |
simon | 0:350011bf8be7 | 126 | P( C, D, E, A, B, W[8] ); |
simon | 0:350011bf8be7 | 127 | P( B, C, D, E, A, W[9] ); |
simon | 0:350011bf8be7 | 128 | P( A, B, C, D, E, W[10] ); |
simon | 0:350011bf8be7 | 129 | P( E, A, B, C, D, W[11] ); |
simon | 0:350011bf8be7 | 130 | P( D, E, A, B, C, W[12] ); |
simon | 0:350011bf8be7 | 131 | P( C, D, E, A, B, W[13] ); |
simon | 0:350011bf8be7 | 132 | P( B, C, D, E, A, W[14] ); |
simon | 0:350011bf8be7 | 133 | P( A, B, C, D, E, W[15] ); |
simon | 0:350011bf8be7 | 134 | P( E, A, B, C, D, R(16) ); |
simon | 0:350011bf8be7 | 135 | P( D, E, A, B, C, R(17) ); |
simon | 0:350011bf8be7 | 136 | P( C, D, E, A, B, R(18) ); |
simon | 0:350011bf8be7 | 137 | P( B, C, D, E, A, R(19) ); |
simon | 0:350011bf8be7 | 138 | |
simon | 0:350011bf8be7 | 139 | #undef K |
simon | 0:350011bf8be7 | 140 | #undef F |
simon | 0:350011bf8be7 | 141 | |
simon | 0:350011bf8be7 | 142 | #define F(x,y,z) (x ^ y ^ z) |
simon | 0:350011bf8be7 | 143 | #define K 0x6ED9EBA1 |
simon | 0:350011bf8be7 | 144 | |
simon | 0:350011bf8be7 | 145 | P( A, B, C, D, E, R(20) ); |
simon | 0:350011bf8be7 | 146 | P( E, A, B, C, D, R(21) ); |
simon | 0:350011bf8be7 | 147 | P( D, E, A, B, C, R(22) ); |
simon | 0:350011bf8be7 | 148 | P( C, D, E, A, B, R(23) ); |
simon | 0:350011bf8be7 | 149 | P( B, C, D, E, A, R(24) ); |
simon | 0:350011bf8be7 | 150 | P( A, B, C, D, E, R(25) ); |
simon | 0:350011bf8be7 | 151 | P( E, A, B, C, D, R(26) ); |
simon | 0:350011bf8be7 | 152 | P( D, E, A, B, C, R(27) ); |
simon | 0:350011bf8be7 | 153 | P( C, D, E, A, B, R(28) ); |
simon | 0:350011bf8be7 | 154 | P( B, C, D, E, A, R(29) ); |
simon | 0:350011bf8be7 | 155 | P( A, B, C, D, E, R(30) ); |
simon | 0:350011bf8be7 | 156 | P( E, A, B, C, D, R(31) ); |
simon | 0:350011bf8be7 | 157 | P( D, E, A, B, C, R(32) ); |
simon | 0:350011bf8be7 | 158 | P( C, D, E, A, B, R(33) ); |
simon | 0:350011bf8be7 | 159 | P( B, C, D, E, A, R(34) ); |
simon | 0:350011bf8be7 | 160 | P( A, B, C, D, E, R(35) ); |
simon | 0:350011bf8be7 | 161 | P( E, A, B, C, D, R(36) ); |
simon | 0:350011bf8be7 | 162 | P( D, E, A, B, C, R(37) ); |
simon | 0:350011bf8be7 | 163 | P( C, D, E, A, B, R(38) ); |
simon | 0:350011bf8be7 | 164 | P( B, C, D, E, A, R(39) ); |
simon | 0:350011bf8be7 | 165 | |
simon | 0:350011bf8be7 | 166 | #undef K |
simon | 0:350011bf8be7 | 167 | #undef F |
simon | 0:350011bf8be7 | 168 | |
simon | 0:350011bf8be7 | 169 | #define F(x,y,z) ((x & y) | (z & (x | y))) |
simon | 0:350011bf8be7 | 170 | #define K 0x8F1BBCDC |
simon | 0:350011bf8be7 | 171 | |
simon | 0:350011bf8be7 | 172 | P( A, B, C, D, E, R(40) ); |
simon | 0:350011bf8be7 | 173 | P( E, A, B, C, D, R(41) ); |
simon | 0:350011bf8be7 | 174 | P( D, E, A, B, C, R(42) ); |
simon | 0:350011bf8be7 | 175 | P( C, D, E, A, B, R(43) ); |
simon | 0:350011bf8be7 | 176 | P( B, C, D, E, A, R(44) ); |
simon | 0:350011bf8be7 | 177 | P( A, B, C, D, E, R(45) ); |
simon | 0:350011bf8be7 | 178 | P( E, A, B, C, D, R(46) ); |
simon | 0:350011bf8be7 | 179 | P( D, E, A, B, C, R(47) ); |
simon | 0:350011bf8be7 | 180 | P( C, D, E, A, B, R(48) ); |
simon | 0:350011bf8be7 | 181 | P( B, C, D, E, A, R(49) ); |
simon | 0:350011bf8be7 | 182 | P( A, B, C, D, E, R(50) ); |
simon | 0:350011bf8be7 | 183 | P( E, A, B, C, D, R(51) ); |
simon | 0:350011bf8be7 | 184 | P( D, E, A, B, C, R(52) ); |
simon | 0:350011bf8be7 | 185 | P( C, D, E, A, B, R(53) ); |
simon | 0:350011bf8be7 | 186 | P( B, C, D, E, A, R(54) ); |
simon | 0:350011bf8be7 | 187 | P( A, B, C, D, E, R(55) ); |
simon | 0:350011bf8be7 | 188 | P( E, A, B, C, D, R(56) ); |
simon | 0:350011bf8be7 | 189 | P( D, E, A, B, C, R(57) ); |
simon | 0:350011bf8be7 | 190 | P( C, D, E, A, B, R(58) ); |
simon | 0:350011bf8be7 | 191 | P( B, C, D, E, A, R(59) ); |
simon | 0:350011bf8be7 | 192 | |
simon | 0:350011bf8be7 | 193 | #undef K |
simon | 0:350011bf8be7 | 194 | #undef F |
simon | 0:350011bf8be7 | 195 | |
simon | 0:350011bf8be7 | 196 | #define F(x,y,z) (x ^ y ^ z) |
simon | 0:350011bf8be7 | 197 | #define K 0xCA62C1D6 |
simon | 0:350011bf8be7 | 198 | |
simon | 0:350011bf8be7 | 199 | P( A, B, C, D, E, R(60) ); |
simon | 0:350011bf8be7 | 200 | P( E, A, B, C, D, R(61) ); |
simon | 0:350011bf8be7 | 201 | P( D, E, A, B, C, R(62) ); |
simon | 0:350011bf8be7 | 202 | P( C, D, E, A, B, R(63) ); |
simon | 0:350011bf8be7 | 203 | P( B, C, D, E, A, R(64) ); |
simon | 0:350011bf8be7 | 204 | P( A, B, C, D, E, R(65) ); |
simon | 0:350011bf8be7 | 205 | P( E, A, B, C, D, R(66) ); |
simon | 0:350011bf8be7 | 206 | P( D, E, A, B, C, R(67) ); |
simon | 0:350011bf8be7 | 207 | P( C, D, E, A, B, R(68) ); |
simon | 0:350011bf8be7 | 208 | P( B, C, D, E, A, R(69) ); |
simon | 0:350011bf8be7 | 209 | P( A, B, C, D, E, R(70) ); |
simon | 0:350011bf8be7 | 210 | P( E, A, B, C, D, R(71) ); |
simon | 0:350011bf8be7 | 211 | P( D, E, A, B, C, R(72) ); |
simon | 0:350011bf8be7 | 212 | P( C, D, E, A, B, R(73) ); |
simon | 0:350011bf8be7 | 213 | P( B, C, D, E, A, R(74) ); |
simon | 0:350011bf8be7 | 214 | P( A, B, C, D, E, R(75) ); |
simon | 0:350011bf8be7 | 215 | P( E, A, B, C, D, R(76) ); |
simon | 0:350011bf8be7 | 216 | P( D, E, A, B, C, R(77) ); |
simon | 0:350011bf8be7 | 217 | P( C, D, E, A, B, R(78) ); |
simon | 0:350011bf8be7 | 218 | P( B, C, D, E, A, R(79) ); |
simon | 0:350011bf8be7 | 219 | |
simon | 0:350011bf8be7 | 220 | #undef K |
simon | 0:350011bf8be7 | 221 | #undef F |
simon | 0:350011bf8be7 | 222 | |
simon | 0:350011bf8be7 | 223 | ctx->state[0] += A; |
simon | 0:350011bf8be7 | 224 | ctx->state[1] += B; |
simon | 0:350011bf8be7 | 225 | ctx->state[2] += C; |
simon | 0:350011bf8be7 | 226 | ctx->state[3] += D; |
simon | 0:350011bf8be7 | 227 | ctx->state[4] += E; |
simon | 0:350011bf8be7 | 228 | } |
simon | 0:350011bf8be7 | 229 | |
simon | 0:350011bf8be7 | 230 | /* |
simon | 0:350011bf8be7 | 231 | * SHA-1 process buffer |
simon | 0:350011bf8be7 | 232 | */ |
simon | 0:350011bf8be7 | 233 | void sha1_update( sha1_context *ctx, const unsigned char *input, int ilen ) |
simon | 0:350011bf8be7 | 234 | { |
simon | 0:350011bf8be7 | 235 | int fill; |
simon | 0:350011bf8be7 | 236 | unsigned long left; |
simon | 0:350011bf8be7 | 237 | |
simon | 0:350011bf8be7 | 238 | if( ilen <= 0 ) |
simon | 0:350011bf8be7 | 239 | return; |
simon | 0:350011bf8be7 | 240 | |
simon | 0:350011bf8be7 | 241 | left = ctx->total[0] & 0x3F; |
simon | 0:350011bf8be7 | 242 | fill = 64 - left; |
simon | 0:350011bf8be7 | 243 | |
simon | 0:350011bf8be7 | 244 | ctx->total[0] += ilen; |
simon | 0:350011bf8be7 | 245 | ctx->total[0] &= 0xFFFFFFFF; |
simon | 0:350011bf8be7 | 246 | |
simon | 0:350011bf8be7 | 247 | if( ctx->total[0] < (unsigned long) ilen ) |
simon | 0:350011bf8be7 | 248 | ctx->total[1]++; |
simon | 0:350011bf8be7 | 249 | |
simon | 0:350011bf8be7 | 250 | if( left && ilen >= fill ) |
simon | 0:350011bf8be7 | 251 | { |
simon | 0:350011bf8be7 | 252 | memcpy( (void *) (ctx->buffer + left), |
simon | 0:350011bf8be7 | 253 | (void *) input, fill ); |
simon | 0:350011bf8be7 | 254 | sha1_process( ctx, ctx->buffer ); |
simon | 0:350011bf8be7 | 255 | input += fill; |
simon | 0:350011bf8be7 | 256 | ilen -= fill; |
simon | 0:350011bf8be7 | 257 | left = 0; |
simon | 0:350011bf8be7 | 258 | } |
simon | 0:350011bf8be7 | 259 | |
simon | 0:350011bf8be7 | 260 | while( ilen >= 64 ) |
simon | 0:350011bf8be7 | 261 | { |
simon | 0:350011bf8be7 | 262 | sha1_process( ctx, input ); |
simon | 0:350011bf8be7 | 263 | input += 64; |
simon | 0:350011bf8be7 | 264 | ilen -= 64; |
simon | 0:350011bf8be7 | 265 | } |
simon | 0:350011bf8be7 | 266 | |
simon | 0:350011bf8be7 | 267 | if( ilen > 0 ) |
simon | 0:350011bf8be7 | 268 | { |
simon | 0:350011bf8be7 | 269 | memcpy( (void *) (ctx->buffer + left), |
simon | 0:350011bf8be7 | 270 | (void *) input, ilen ); |
simon | 0:350011bf8be7 | 271 | } |
simon | 0:350011bf8be7 | 272 | } |
simon | 0:350011bf8be7 | 273 | |
simon | 0:350011bf8be7 | 274 | static const unsigned char sha1_padding[64] = |
simon | 0:350011bf8be7 | 275 | { |
simon | 0:350011bf8be7 | 276 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
simon | 0:350011bf8be7 | 277 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
simon | 0:350011bf8be7 | 278 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
simon | 0:350011bf8be7 | 279 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
simon | 0:350011bf8be7 | 280 | }; |
simon | 0:350011bf8be7 | 281 | |
simon | 0:350011bf8be7 | 282 | /* |
simon | 0:350011bf8be7 | 283 | * SHA-1 final digest |
simon | 0:350011bf8be7 | 284 | */ |
simon | 0:350011bf8be7 | 285 | void sha1_finish( sha1_context *ctx, unsigned char output[20] ) |
simon | 0:350011bf8be7 | 286 | { |
simon | 0:350011bf8be7 | 287 | unsigned long last, padn; |
simon | 0:350011bf8be7 | 288 | unsigned long high, low; |
simon | 0:350011bf8be7 | 289 | unsigned char msglen[8]; |
simon | 0:350011bf8be7 | 290 | |
simon | 0:350011bf8be7 | 291 | high = ( ctx->total[0] >> 29 ) |
simon | 0:350011bf8be7 | 292 | | ( ctx->total[1] << 3 ); |
simon | 0:350011bf8be7 | 293 | low = ( ctx->total[0] << 3 ); |
simon | 0:350011bf8be7 | 294 | |
simon | 0:350011bf8be7 | 295 | PUT_ULONG_BE( high, msglen, 0 ); |
simon | 0:350011bf8be7 | 296 | PUT_ULONG_BE( low, msglen, 4 ); |
simon | 0:350011bf8be7 | 297 | |
simon | 0:350011bf8be7 | 298 | last = ctx->total[0] & 0x3F; |
simon | 0:350011bf8be7 | 299 | padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); |
simon | 0:350011bf8be7 | 300 | |
simon | 0:350011bf8be7 | 301 | sha1_update( ctx, (unsigned char *) sha1_padding, padn ); |
simon | 0:350011bf8be7 | 302 | sha1_update( ctx, msglen, 8 ); |
simon | 0:350011bf8be7 | 303 | |
simon | 0:350011bf8be7 | 304 | PUT_ULONG_BE( ctx->state[0], output, 0 ); |
simon | 0:350011bf8be7 | 305 | PUT_ULONG_BE( ctx->state[1], output, 4 ); |
simon | 0:350011bf8be7 | 306 | PUT_ULONG_BE( ctx->state[2], output, 8 ); |
simon | 0:350011bf8be7 | 307 | PUT_ULONG_BE( ctx->state[3], output, 12 ); |
simon | 0:350011bf8be7 | 308 | PUT_ULONG_BE( ctx->state[4], output, 16 ); |
simon | 0:350011bf8be7 | 309 | } |
simon | 0:350011bf8be7 | 310 | |
simon | 0:350011bf8be7 | 311 | /* |
simon | 0:350011bf8be7 | 312 | * output = SHA-1( input buffer ) |
simon | 0:350011bf8be7 | 313 | */ |
simon | 0:350011bf8be7 | 314 | void sha1( const unsigned char *input, int ilen, unsigned char output[20] ) |
simon | 0:350011bf8be7 | 315 | { |
simon | 0:350011bf8be7 | 316 | sha1_context ctx; |
simon | 0:350011bf8be7 | 317 | |
simon | 0:350011bf8be7 | 318 | sha1_starts( &ctx ); |
simon | 0:350011bf8be7 | 319 | sha1_update( &ctx, input, ilen ); |
simon | 0:350011bf8be7 | 320 | sha1_finish( &ctx, output ); |
simon | 0:350011bf8be7 | 321 | |
simon | 0:350011bf8be7 | 322 | memset( &ctx, 0, sizeof( sha1_context ) ); |
simon | 0:350011bf8be7 | 323 | } |
simon | 0:350011bf8be7 | 324 | |
simon | 0:350011bf8be7 | 325 | /* |
simon | 0:350011bf8be7 | 326 | * output = SHA-1( file contents ) |
simon | 0:350011bf8be7 | 327 | */ |
simon | 0:350011bf8be7 | 328 | #if 0 //No need for that |
simon | 0:350011bf8be7 | 329 | int sha1_file( const char *path, unsigned char output[20] ) |
simon | 0:350011bf8be7 | 330 | { |
simon | 0:350011bf8be7 | 331 | FILE *f; |
simon | 0:350011bf8be7 | 332 | size_t n; |
simon | 0:350011bf8be7 | 333 | sha1_context ctx; |
simon | 0:350011bf8be7 | 334 | unsigned char buf[1024]; |
simon | 0:350011bf8be7 | 335 | |
simon | 0:350011bf8be7 | 336 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
simon | 0:350011bf8be7 | 337 | return( 1 ); |
simon | 0:350011bf8be7 | 338 | |
simon | 0:350011bf8be7 | 339 | sha1_starts( &ctx ); |
simon | 0:350011bf8be7 | 340 | |
simon | 0:350011bf8be7 | 341 | while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) |
simon | 0:350011bf8be7 | 342 | sha1_update( &ctx, buf, (int) n ); |
simon | 0:350011bf8be7 | 343 | |
simon | 0:350011bf8be7 | 344 | sha1_finish( &ctx, output ); |
simon | 0:350011bf8be7 | 345 | |
simon | 0:350011bf8be7 | 346 | memset( &ctx, 0, sizeof( sha1_context ) ); |
simon | 0:350011bf8be7 | 347 | |
simon | 0:350011bf8be7 | 348 | if( ferror( f ) != 0 ) |
simon | 0:350011bf8be7 | 349 | { |
simon | 0:350011bf8be7 | 350 | fclose( f ); |
simon | 0:350011bf8be7 | 351 | return( 2 ); |
simon | 0:350011bf8be7 | 352 | } |
simon | 0:350011bf8be7 | 353 | |
simon | 0:350011bf8be7 | 354 | fclose( f ); |
simon | 0:350011bf8be7 | 355 | return( 0 ); |
simon | 0:350011bf8be7 | 356 | } |
simon | 0:350011bf8be7 | 357 | #endif |
simon | 0:350011bf8be7 | 358 | |
simon | 0:350011bf8be7 | 359 | /* |
simon | 0:350011bf8be7 | 360 | * SHA-1 HMAC context setup |
simon | 0:350011bf8be7 | 361 | */ |
simon | 0:350011bf8be7 | 362 | void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, int keylen ) |
simon | 0:350011bf8be7 | 363 | { |
simon | 0:350011bf8be7 | 364 | int i; |
simon | 0:350011bf8be7 | 365 | unsigned char sum[20]; |
simon | 0:350011bf8be7 | 366 | |
simon | 0:350011bf8be7 | 367 | if( keylen > 64 ) |
simon | 0:350011bf8be7 | 368 | { |
simon | 0:350011bf8be7 | 369 | sha1( key, keylen, sum ); |
simon | 0:350011bf8be7 | 370 | keylen = 20; |
simon | 0:350011bf8be7 | 371 | key = sum; |
simon | 0:350011bf8be7 | 372 | } |
simon | 0:350011bf8be7 | 373 | |
simon | 0:350011bf8be7 | 374 | memset( ctx->ipad, 0x36, 64 ); |
simon | 0:350011bf8be7 | 375 | memset( ctx->opad, 0x5C, 64 ); |
simon | 0:350011bf8be7 | 376 | |
simon | 0:350011bf8be7 | 377 | for( i = 0; i < keylen; i++ ) |
simon | 0:350011bf8be7 | 378 | { |
simon | 0:350011bf8be7 | 379 | ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); |
simon | 0:350011bf8be7 | 380 | ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); |
simon | 0:350011bf8be7 | 381 | } |
simon | 0:350011bf8be7 | 382 | |
simon | 0:350011bf8be7 | 383 | sha1_starts( ctx ); |
simon | 0:350011bf8be7 | 384 | sha1_update( ctx, ctx->ipad, 64 ); |
simon | 0:350011bf8be7 | 385 | |
simon | 0:350011bf8be7 | 386 | memset( sum, 0, sizeof( sum ) ); |
simon | 0:350011bf8be7 | 387 | } |
simon | 0:350011bf8be7 | 388 | |
simon | 0:350011bf8be7 | 389 | /* |
simon | 0:350011bf8be7 | 390 | * SHA-1 HMAC process buffer |
simon | 0:350011bf8be7 | 391 | */ |
simon | 0:350011bf8be7 | 392 | void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, int ilen ) |
simon | 0:350011bf8be7 | 393 | { |
simon | 0:350011bf8be7 | 394 | sha1_update( ctx, input, ilen ); |
simon | 0:350011bf8be7 | 395 | } |
simon | 0:350011bf8be7 | 396 | |
simon | 0:350011bf8be7 | 397 | /* |
simon | 0:350011bf8be7 | 398 | * SHA-1 HMAC final digest |
simon | 0:350011bf8be7 | 399 | */ |
simon | 0:350011bf8be7 | 400 | void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] ) |
simon | 0:350011bf8be7 | 401 | { |
simon | 0:350011bf8be7 | 402 | unsigned char tmpbuf[20]; |
simon | 0:350011bf8be7 | 403 | |
simon | 0:350011bf8be7 | 404 | sha1_finish( ctx, tmpbuf ); |
simon | 0:350011bf8be7 | 405 | sha1_starts( ctx ); |
simon | 0:350011bf8be7 | 406 | sha1_update( ctx, ctx->opad, 64 ); |
simon | 0:350011bf8be7 | 407 | sha1_update( ctx, tmpbuf, 20 ); |
simon | 0:350011bf8be7 | 408 | sha1_finish( ctx, output ); |
simon | 0:350011bf8be7 | 409 | |
simon | 0:350011bf8be7 | 410 | memset( tmpbuf, 0, sizeof( tmpbuf ) ); |
simon | 0:350011bf8be7 | 411 | } |
simon | 0:350011bf8be7 | 412 | |
simon | 0:350011bf8be7 | 413 | /* |
simon | 0:350011bf8be7 | 414 | * SHA1 HMAC context reset |
simon | 0:350011bf8be7 | 415 | */ |
simon | 0:350011bf8be7 | 416 | void sha1_hmac_reset( sha1_context *ctx ) |
simon | 0:350011bf8be7 | 417 | { |
simon | 0:350011bf8be7 | 418 | sha1_starts( ctx ); |
simon | 0:350011bf8be7 | 419 | sha1_update( ctx, ctx->ipad, 64 ); |
simon | 0:350011bf8be7 | 420 | } |
simon | 0:350011bf8be7 | 421 | |
simon | 0:350011bf8be7 | 422 | /* |
simon | 0:350011bf8be7 | 423 | * output = HMAC-SHA-1( hmac key, input buffer ) |
simon | 0:350011bf8be7 | 424 | */ |
simon | 0:350011bf8be7 | 425 | void sha1_hmac( const unsigned char *key, int keylen, |
simon | 0:350011bf8be7 | 426 | const unsigned char *input, int ilen, |
simon | 0:350011bf8be7 | 427 | unsigned char output[20] ) |
simon | 0:350011bf8be7 | 428 | { |
simon | 0:350011bf8be7 | 429 | sha1_context ctx; |
simon | 0:350011bf8be7 | 430 | |
simon | 0:350011bf8be7 | 431 | sha1_hmac_starts( &ctx, key, keylen ); |
simon | 0:350011bf8be7 | 432 | sha1_hmac_update( &ctx, input, ilen ); |
simon | 0:350011bf8be7 | 433 | sha1_hmac_finish( &ctx, output ); |
simon | 0:350011bf8be7 | 434 | |
simon | 0:350011bf8be7 | 435 | memset( &ctx, 0, sizeof( sha1_context ) ); |
simon | 0:350011bf8be7 | 436 | } |
simon | 0:350011bf8be7 | 437 | |
simon | 0:350011bf8be7 | 438 | #if defined(POLARSSL_SELF_TEST) |
simon | 0:350011bf8be7 | 439 | /* |
simon | 0:350011bf8be7 | 440 | * FIPS-180-1 test vectors |
simon | 0:350011bf8be7 | 441 | */ |
simon | 0:350011bf8be7 | 442 | static unsigned char sha1_test_buf[3][57] = |
simon | 0:350011bf8be7 | 443 | { |
simon | 0:350011bf8be7 | 444 | { "abc" }, |
simon | 0:350011bf8be7 | 445 | { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, |
simon | 0:350011bf8be7 | 446 | { "" } |
simon | 0:350011bf8be7 | 447 | }; |
simon | 0:350011bf8be7 | 448 | |
simon | 0:350011bf8be7 | 449 | static const int sha1_test_buflen[3] = |
simon | 0:350011bf8be7 | 450 | { |
simon | 0:350011bf8be7 | 451 | 3, 56, 1000 |
simon | 0:350011bf8be7 | 452 | }; |
simon | 0:350011bf8be7 | 453 | |
simon | 0:350011bf8be7 | 454 | static const unsigned char sha1_test_sum[3][20] = |
simon | 0:350011bf8be7 | 455 | { |
simon | 0:350011bf8be7 | 456 | { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, |
simon | 0:350011bf8be7 | 457 | 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D }, |
simon | 0:350011bf8be7 | 458 | { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, |
simon | 0:350011bf8be7 | 459 | 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 }, |
simon | 0:350011bf8be7 | 460 | { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E, |
simon | 0:350011bf8be7 | 461 | 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F } |
simon | 0:350011bf8be7 | 462 | }; |
simon | 0:350011bf8be7 | 463 | |
simon | 0:350011bf8be7 | 464 | /* |
simon | 0:350011bf8be7 | 465 | * RFC 2202 test vectors |
simon | 0:350011bf8be7 | 466 | */ |
simon | 0:350011bf8be7 | 467 | static unsigned char sha1_hmac_test_key[7][26] = |
simon | 0:350011bf8be7 | 468 | { |
simon | 0:350011bf8be7 | 469 | { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" |
simon | 0:350011bf8be7 | 470 | "\x0B\x0B\x0B\x0B" }, |
simon | 0:350011bf8be7 | 471 | { "Jefe" }, |
simon | 0:350011bf8be7 | 472 | { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
simon | 0:350011bf8be7 | 473 | "\xAA\xAA\xAA\xAA" }, |
simon | 0:350011bf8be7 | 474 | { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10" |
simon | 0:350011bf8be7 | 475 | "\x11\x12\x13\x14\x15\x16\x17\x18\x19" }, |
simon | 0:350011bf8be7 | 476 | { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C" |
simon | 0:350011bf8be7 | 477 | "\x0C\x0C\x0C\x0C" }, |
simon | 0:350011bf8be7 | 478 | { "" }, /* 0xAA 80 times */ |
simon | 0:350011bf8be7 | 479 | { "" } |
simon | 0:350011bf8be7 | 480 | }; |
simon | 0:350011bf8be7 | 481 | |
simon | 0:350011bf8be7 | 482 | static const int sha1_hmac_test_keylen[7] = |
simon | 0:350011bf8be7 | 483 | { |
simon | 0:350011bf8be7 | 484 | 20, 4, 20, 25, 20, 80, 80 |
simon | 0:350011bf8be7 | 485 | }; |
simon | 0:350011bf8be7 | 486 | |
simon | 0:350011bf8be7 | 487 | static unsigned char sha1_hmac_test_buf[7][74] = |
simon | 0:350011bf8be7 | 488 | { |
simon | 0:350011bf8be7 | 489 | { "Hi There" }, |
simon | 0:350011bf8be7 | 490 | { "what do ya want for nothing?" }, |
simon | 0:350011bf8be7 | 491 | { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
simon | 0:350011bf8be7 | 492 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
simon | 0:350011bf8be7 | 493 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
simon | 0:350011bf8be7 | 494 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
simon | 0:350011bf8be7 | 495 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" }, |
simon | 0:350011bf8be7 | 496 | { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
simon | 0:350011bf8be7 | 497 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
simon | 0:350011bf8be7 | 498 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
simon | 0:350011bf8be7 | 499 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
simon | 0:350011bf8be7 | 500 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" }, |
simon | 0:350011bf8be7 | 501 | { "Test With Truncation" }, |
simon | 0:350011bf8be7 | 502 | { "Test Using Larger Than Block-Size Key - Hash Key First" }, |
simon | 0:350011bf8be7 | 503 | { "Test Using Larger Than Block-Size Key and Larger" |
simon | 0:350011bf8be7 | 504 | " Than One Block-Size Data" } |
simon | 0:350011bf8be7 | 505 | }; |
simon | 0:350011bf8be7 | 506 | |
simon | 0:350011bf8be7 | 507 | static const int sha1_hmac_test_buflen[7] = |
simon | 0:350011bf8be7 | 508 | { |
simon | 0:350011bf8be7 | 509 | 8, 28, 50, 50, 20, 54, 73 |
simon | 0:350011bf8be7 | 510 | }; |
simon | 0:350011bf8be7 | 511 | |
simon | 0:350011bf8be7 | 512 | static const unsigned char sha1_hmac_test_sum[7][20] = |
simon | 0:350011bf8be7 | 513 | { |
simon | 0:350011bf8be7 | 514 | { 0xB6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xE2, 0x8B, |
simon | 0:350011bf8be7 | 515 | 0xC0, 0xB6, 0xFB, 0x37, 0x8C, 0x8E, 0xF1, 0x46, 0xBE, 0x00 }, |
simon | 0:350011bf8be7 | 516 | { 0xEF, 0xFC, 0xDF, 0x6A, 0xE5, 0xEB, 0x2F, 0xA2, 0xD2, 0x74, |
simon | 0:350011bf8be7 | 517 | 0x16, 0xD5, 0xF1, 0x84, 0xDF, 0x9C, 0x25, 0x9A, 0x7C, 0x79 }, |
simon | 0:350011bf8be7 | 518 | { 0x12, 0x5D, 0x73, 0x42, 0xB9, 0xAC, 0x11, 0xCD, 0x91, 0xA3, |
simon | 0:350011bf8be7 | 519 | 0x9A, 0xF4, 0x8A, 0xA1, 0x7B, 0x4F, 0x63, 0xF1, 0x75, 0xD3 }, |
simon | 0:350011bf8be7 | 520 | { 0x4C, 0x90, 0x07, 0xF4, 0x02, 0x62, 0x50, 0xC6, 0xBC, 0x84, |
simon | 0:350011bf8be7 | 521 | 0x14, 0xF9, 0xBF, 0x50, 0xC8, 0x6C, 0x2D, 0x72, 0x35, 0xDA }, |
simon | 0:350011bf8be7 | 522 | { 0x4C, 0x1A, 0x03, 0x42, 0x4B, 0x55, 0xE0, 0x7F, 0xE7, 0xF2, |
simon | 0:350011bf8be7 | 523 | 0x7B, 0xE1 }, |
simon | 0:350011bf8be7 | 524 | { 0xAA, 0x4A, 0xE5, 0xE1, 0x52, 0x72, 0xD0, 0x0E, 0x95, 0x70, |
simon | 0:350011bf8be7 | 525 | 0x56, 0x37, 0xCE, 0x8A, 0x3B, 0x55, 0xED, 0x40, 0x21, 0x12 }, |
simon | 0:350011bf8be7 | 526 | { 0xE8, 0xE9, 0x9D, 0x0F, 0x45, 0x23, 0x7D, 0x78, 0x6D, 0x6B, |
simon | 0:350011bf8be7 | 527 | 0xBA, 0xA7, 0x96, 0x5C, 0x78, 0x08, 0xBB, 0xFF, 0x1A, 0x91 } |
simon | 0:350011bf8be7 | 528 | }; |
simon | 0:350011bf8be7 | 529 | |
simon | 0:350011bf8be7 | 530 | /* |
simon | 0:350011bf8be7 | 531 | * Checkup routine |
simon | 0:350011bf8be7 | 532 | */ |
simon | 0:350011bf8be7 | 533 | int sha1_self_test( int verbose ) |
simon | 0:350011bf8be7 | 534 | { |
simon | 0:350011bf8be7 | 535 | int i, j, buflen; |
simon | 0:350011bf8be7 | 536 | unsigned char buf[1024]; |
simon | 0:350011bf8be7 | 537 | unsigned char sha1sum[20]; |
simon | 0:350011bf8be7 | 538 | sha1_context ctx; |
simon | 0:350011bf8be7 | 539 | |
simon | 0:350011bf8be7 | 540 | /* |
simon | 0:350011bf8be7 | 541 | * SHA-1 |
simon | 0:350011bf8be7 | 542 | */ |
simon | 0:350011bf8be7 | 543 | for( i = 0; i < 3; i++ ) |
simon | 0:350011bf8be7 | 544 | { |
simon | 0:350011bf8be7 | 545 | if( verbose != 0 ) |
simon | 0:350011bf8be7 | 546 | printf( " SHA-1 test #%d: ", i + 1 ); |
simon | 0:350011bf8be7 | 547 | |
simon | 0:350011bf8be7 | 548 | sha1_starts( &ctx ); |
simon | 0:350011bf8be7 | 549 | |
simon | 0:350011bf8be7 | 550 | if( i == 2 ) |
simon | 0:350011bf8be7 | 551 | { |
simon | 0:350011bf8be7 | 552 | memset( buf, 'a', buflen = 1000 ); |
simon | 0:350011bf8be7 | 553 | |
simon | 0:350011bf8be7 | 554 | for( j = 0; j < 1000; j++ ) |
simon | 0:350011bf8be7 | 555 | sha1_update( &ctx, buf, buflen ); |
simon | 0:350011bf8be7 | 556 | } |
simon | 0:350011bf8be7 | 557 | else |
simon | 0:350011bf8be7 | 558 | sha1_update( &ctx, sha1_test_buf[i], |
simon | 0:350011bf8be7 | 559 | sha1_test_buflen[i] ); |
simon | 0:350011bf8be7 | 560 | |
simon | 0:350011bf8be7 | 561 | sha1_finish( &ctx, sha1sum ); |
simon | 0:350011bf8be7 | 562 | |
simon | 0:350011bf8be7 | 563 | if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 ) |
simon | 0:350011bf8be7 | 564 | { |
simon | 0:350011bf8be7 | 565 | if( verbose != 0 ) |
simon | 0:350011bf8be7 | 566 | printf( "failed\n" ); |
simon | 0:350011bf8be7 | 567 | |
simon | 0:350011bf8be7 | 568 | return( 1 ); |
simon | 0:350011bf8be7 | 569 | } |
simon | 0:350011bf8be7 | 570 | |
simon | 0:350011bf8be7 | 571 | if( verbose != 0 ) |
simon | 0:350011bf8be7 | 572 | printf( "passed\n" ); |
simon | 0:350011bf8be7 | 573 | } |
simon | 0:350011bf8be7 | 574 | |
simon | 0:350011bf8be7 | 575 | if( verbose != 0 ) |
simon | 0:350011bf8be7 | 576 | printf( "\n" ); |
simon | 0:350011bf8be7 | 577 | |
simon | 0:350011bf8be7 | 578 | for( i = 0; i < 7; i++ ) |
simon | 0:350011bf8be7 | 579 | { |
simon | 0:350011bf8be7 | 580 | if( verbose != 0 ) |
simon | 0:350011bf8be7 | 581 | printf( " HMAC-SHA-1 test #%d: ", i + 1 ); |
simon | 0:350011bf8be7 | 582 | |
simon | 0:350011bf8be7 | 583 | if( i == 5 || i == 6 ) |
simon | 0:350011bf8be7 | 584 | { |
simon | 0:350011bf8be7 | 585 | memset( buf, '\xAA', buflen = 80 ); |
simon | 0:350011bf8be7 | 586 | sha1_hmac_starts( &ctx, buf, buflen ); |
simon | 0:350011bf8be7 | 587 | } |
simon | 0:350011bf8be7 | 588 | else |
simon | 0:350011bf8be7 | 589 | sha1_hmac_starts( &ctx, sha1_hmac_test_key[i], |
simon | 0:350011bf8be7 | 590 | sha1_hmac_test_keylen[i] ); |
simon | 0:350011bf8be7 | 591 | |
simon | 0:350011bf8be7 | 592 | sha1_hmac_update( &ctx, sha1_hmac_test_buf[i], |
simon | 0:350011bf8be7 | 593 | sha1_hmac_test_buflen[i] ); |
simon | 0:350011bf8be7 | 594 | |
simon | 0:350011bf8be7 | 595 | sha1_hmac_finish( &ctx, sha1sum ); |
simon | 0:350011bf8be7 | 596 | |
simon | 0:350011bf8be7 | 597 | buflen = ( i == 4 ) ? 12 : 20; |
simon | 0:350011bf8be7 | 598 | |
simon | 0:350011bf8be7 | 599 | if( memcmp( sha1sum, sha1_hmac_test_sum[i], buflen ) != 0 ) |
simon | 0:350011bf8be7 | 600 | { |
simon | 0:350011bf8be7 | 601 | if( verbose != 0 ) |
simon | 0:350011bf8be7 | 602 | printf( "failed\n" ); |
simon | 0:350011bf8be7 | 603 | |
simon | 0:350011bf8be7 | 604 | return( 1 ); |
simon | 0:350011bf8be7 | 605 | } |
simon | 0:350011bf8be7 | 606 | |
simon | 0:350011bf8be7 | 607 | if( verbose != 0 ) |
simon | 0:350011bf8be7 | 608 | printf( "passed\n" ); |
simon | 0:350011bf8be7 | 609 | } |
simon | 0:350011bf8be7 | 610 | |
simon | 0:350011bf8be7 | 611 | if( verbose != 0 ) |
simon | 0:350011bf8be7 | 612 | printf( "\n" ); |
simon | 0:350011bf8be7 | 613 | |
simon | 0:350011bf8be7 | 614 | return( 0 ); |
simon | 0:350011bf8be7 | 615 | } |
simon | 0:350011bf8be7 | 616 | |
simon | 0:350011bf8be7 | 617 | #endif |
simon | 0:350011bf8be7 | 618 | |
simon | 0:350011bf8be7 | 619 | #endif |