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