Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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