HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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