Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:54:31 2010 +0000
Revision:
5:8e53abda9900
Parent:
0:355018f44c9f

        

Who changed what in which revision?

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