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