mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

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