mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
polarssl/sha256.c@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /* |
ansond | 0:137634ff4186 | 2 | * FIPS-180-2 compliant SHA-256 implementation |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 9 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 10 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 11 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 12 | * |
ansond | 0:137634ff4186 | 13 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 16 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 17 | * |
ansond | 0:137634ff4186 | 18 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 21 | */ |
ansond | 0:137634ff4186 | 22 | /* |
ansond | 0:137634ff4186 | 23 | * The SHA-256 Secure Hash Standard was published by NIST in 2002. |
ansond | 0:137634ff4186 | 24 | * |
ansond | 0:137634ff4186 | 25 | * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
ansond | 0:137634ff4186 | 26 | */ |
ansond | 0:137634ff4186 | 27 | |
ansond | 0:137634ff4186 | 28 | #if !defined(POLARSSL_CONFIG_FILE) |
ansond | 0:137634ff4186 | 29 | #include "polarssl/config.h" |
ansond | 0:137634ff4186 | 30 | #else |
ansond | 0:137634ff4186 | 31 | #include POLARSSL_CONFIG_FILE |
ansond | 0:137634ff4186 | 32 | #endif |
ansond | 0:137634ff4186 | 33 | |
ansond | 0:137634ff4186 | 34 | #if defined(POLARSSL_SHA256_C) |
ansond | 0:137634ff4186 | 35 | |
ansond | 0:137634ff4186 | 36 | #include "polarssl/sha256.h" |
ansond | 0:137634ff4186 | 37 | |
ansond | 0:137634ff4186 | 38 | #include <string.h> |
ansond | 0:137634ff4186 | 39 | |
ansond | 0:137634ff4186 | 40 | #if defined(POLARSSL_FS_IO) |
ansond | 0:137634ff4186 | 41 | #include <stdio.h> |
ansond | 0:137634ff4186 | 42 | #endif |
ansond | 0:137634ff4186 | 43 | |
ansond | 0:137634ff4186 | 44 | #if defined(POLARSSL_SELF_TEST) |
ansond | 0:137634ff4186 | 45 | #if defined(POLARSSL_PLATFORM_C) |
ansond | 0:137634ff4186 | 46 | #include "polarssl/platform.h" |
ansond | 0:137634ff4186 | 47 | #else |
ansond | 0:137634ff4186 | 48 | #include <stdio.h> |
ansond | 0:137634ff4186 | 49 | #define polarssl_printf printf |
ansond | 0:137634ff4186 | 50 | #endif /* POLARSSL_PLATFORM_C */ |
ansond | 0:137634ff4186 | 51 | #endif /* POLARSSL_SELF_TEST */ |
ansond | 0:137634ff4186 | 52 | |
ansond | 0:137634ff4186 | 53 | /* Implementation that should never be optimized out by the compiler */ |
ansond | 0:137634ff4186 | 54 | static void polarssl_zeroize( void *v, size_t n ) { |
ansond | 0:137634ff4186 | 55 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
ansond | 0:137634ff4186 | 56 | } |
ansond | 0:137634ff4186 | 57 | |
ansond | 0:137634ff4186 | 58 | #if !defined(POLARSSL_SHA256_ALT) |
ansond | 0:137634ff4186 | 59 | |
ansond | 0:137634ff4186 | 60 | /* |
ansond | 0:137634ff4186 | 61 | * 32-bit integer manipulation macros (big endian) |
ansond | 0:137634ff4186 | 62 | */ |
ansond | 0:137634ff4186 | 63 | #ifndef GET_UINT32_BE |
ansond | 0:137634ff4186 | 64 | #define GET_UINT32_BE(n,b,i) \ |
ansond | 0:137634ff4186 | 65 | { \ |
ansond | 0:137634ff4186 | 66 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ |
ansond | 0:137634ff4186 | 67 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ |
ansond | 0:137634ff4186 | 68 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ |
ansond | 0:137634ff4186 | 69 | | ( (uint32_t) (b)[(i) + 3] ); \ |
ansond | 0:137634ff4186 | 70 | } |
ansond | 0:137634ff4186 | 71 | #endif |
ansond | 0:137634ff4186 | 72 | |
ansond | 0:137634ff4186 | 73 | #ifndef PUT_UINT32_BE |
ansond | 0:137634ff4186 | 74 | #define PUT_UINT32_BE(n,b,i) \ |
ansond | 0:137634ff4186 | 75 | { \ |
ansond | 0:137634ff4186 | 76 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
ansond | 0:137634ff4186 | 77 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
ansond | 0:137634ff4186 | 78 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
ansond | 0:137634ff4186 | 79 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
ansond | 0:137634ff4186 | 80 | } |
ansond | 0:137634ff4186 | 81 | #endif |
ansond | 0:137634ff4186 | 82 | |
ansond | 0:137634ff4186 | 83 | void sha256_init( sha256_context *ctx ) |
ansond | 0:137634ff4186 | 84 | { |
ansond | 0:137634ff4186 | 85 | memset( ctx, 0, sizeof( sha256_context ) ); |
ansond | 0:137634ff4186 | 86 | } |
ansond | 0:137634ff4186 | 87 | |
ansond | 0:137634ff4186 | 88 | void sha256_free( sha256_context *ctx ) |
ansond | 0:137634ff4186 | 89 | { |
ansond | 0:137634ff4186 | 90 | if( ctx == NULL ) |
ansond | 0:137634ff4186 | 91 | return; |
ansond | 0:137634ff4186 | 92 | |
ansond | 0:137634ff4186 | 93 | polarssl_zeroize( ctx, sizeof( sha256_context ) ); |
ansond | 0:137634ff4186 | 94 | } |
ansond | 0:137634ff4186 | 95 | |
ansond | 0:137634ff4186 | 96 | /* |
ansond | 0:137634ff4186 | 97 | * SHA-256 context setup |
ansond | 0:137634ff4186 | 98 | */ |
ansond | 0:137634ff4186 | 99 | void sha256_starts( sha256_context *ctx, int is224 ) |
ansond | 0:137634ff4186 | 100 | { |
ansond | 0:137634ff4186 | 101 | ctx->total[0] = 0; |
ansond | 0:137634ff4186 | 102 | ctx->total[1] = 0; |
ansond | 0:137634ff4186 | 103 | |
ansond | 0:137634ff4186 | 104 | if( is224 == 0 ) |
ansond | 0:137634ff4186 | 105 | { |
ansond | 0:137634ff4186 | 106 | /* SHA-256 */ |
ansond | 0:137634ff4186 | 107 | ctx->state[0] = 0x6A09E667; |
ansond | 0:137634ff4186 | 108 | ctx->state[1] = 0xBB67AE85; |
ansond | 0:137634ff4186 | 109 | ctx->state[2] = 0x3C6EF372; |
ansond | 0:137634ff4186 | 110 | ctx->state[3] = 0xA54FF53A; |
ansond | 0:137634ff4186 | 111 | ctx->state[4] = 0x510E527F; |
ansond | 0:137634ff4186 | 112 | ctx->state[5] = 0x9B05688C; |
ansond | 0:137634ff4186 | 113 | ctx->state[6] = 0x1F83D9AB; |
ansond | 0:137634ff4186 | 114 | ctx->state[7] = 0x5BE0CD19; |
ansond | 0:137634ff4186 | 115 | } |
ansond | 0:137634ff4186 | 116 | else |
ansond | 0:137634ff4186 | 117 | { |
ansond | 0:137634ff4186 | 118 | /* SHA-224 */ |
ansond | 0:137634ff4186 | 119 | ctx->state[0] = 0xC1059ED8; |
ansond | 0:137634ff4186 | 120 | ctx->state[1] = 0x367CD507; |
ansond | 0:137634ff4186 | 121 | ctx->state[2] = 0x3070DD17; |
ansond | 0:137634ff4186 | 122 | ctx->state[3] = 0xF70E5939; |
ansond | 0:137634ff4186 | 123 | ctx->state[4] = 0xFFC00B31; |
ansond | 0:137634ff4186 | 124 | ctx->state[5] = 0x68581511; |
ansond | 0:137634ff4186 | 125 | ctx->state[6] = 0x64F98FA7; |
ansond | 0:137634ff4186 | 126 | ctx->state[7] = 0xBEFA4FA4; |
ansond | 0:137634ff4186 | 127 | } |
ansond | 0:137634ff4186 | 128 | |
ansond | 0:137634ff4186 | 129 | ctx->is224 = is224; |
ansond | 0:137634ff4186 | 130 | } |
ansond | 0:137634ff4186 | 131 | |
ansond | 0:137634ff4186 | 132 | void sha256_process( sha256_context *ctx, const unsigned char data[64] ) |
ansond | 0:137634ff4186 | 133 | { |
ansond | 0:137634ff4186 | 134 | uint32_t temp1, temp2, W[64]; |
ansond | 0:137634ff4186 | 135 | uint32_t A, B, C, D, E, F, G, H; |
ansond | 0:137634ff4186 | 136 | |
ansond | 0:137634ff4186 | 137 | GET_UINT32_BE( W[ 0], data, 0 ); |
ansond | 0:137634ff4186 | 138 | GET_UINT32_BE( W[ 1], data, 4 ); |
ansond | 0:137634ff4186 | 139 | GET_UINT32_BE( W[ 2], data, 8 ); |
ansond | 0:137634ff4186 | 140 | GET_UINT32_BE( W[ 3], data, 12 ); |
ansond | 0:137634ff4186 | 141 | GET_UINT32_BE( W[ 4], data, 16 ); |
ansond | 0:137634ff4186 | 142 | GET_UINT32_BE( W[ 5], data, 20 ); |
ansond | 0:137634ff4186 | 143 | GET_UINT32_BE( W[ 6], data, 24 ); |
ansond | 0:137634ff4186 | 144 | GET_UINT32_BE( W[ 7], data, 28 ); |
ansond | 0:137634ff4186 | 145 | GET_UINT32_BE( W[ 8], data, 32 ); |
ansond | 0:137634ff4186 | 146 | GET_UINT32_BE( W[ 9], data, 36 ); |
ansond | 0:137634ff4186 | 147 | GET_UINT32_BE( W[10], data, 40 ); |
ansond | 0:137634ff4186 | 148 | GET_UINT32_BE( W[11], data, 44 ); |
ansond | 0:137634ff4186 | 149 | GET_UINT32_BE( W[12], data, 48 ); |
ansond | 0:137634ff4186 | 150 | GET_UINT32_BE( W[13], data, 52 ); |
ansond | 0:137634ff4186 | 151 | GET_UINT32_BE( W[14], data, 56 ); |
ansond | 0:137634ff4186 | 152 | GET_UINT32_BE( W[15], data, 60 ); |
ansond | 0:137634ff4186 | 153 | |
ansond | 0:137634ff4186 | 154 | #define SHR(x,n) ((x & 0xFFFFFFFF) >> n) |
ansond | 0:137634ff4186 | 155 | #define ROTR(x,n) (SHR(x,n) | (x << (32 - n))) |
ansond | 0:137634ff4186 | 156 | |
ansond | 0:137634ff4186 | 157 | #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3)) |
ansond | 0:137634ff4186 | 158 | #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10)) |
ansond | 0:137634ff4186 | 159 | |
ansond | 0:137634ff4186 | 160 | #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22)) |
ansond | 0:137634ff4186 | 161 | #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25)) |
ansond | 0:137634ff4186 | 162 | |
ansond | 0:137634ff4186 | 163 | #define F0(x,y,z) ((x & y) | (z & (x | y))) |
ansond | 0:137634ff4186 | 164 | #define F1(x,y,z) (z ^ (x & (y ^ z))) |
ansond | 0:137634ff4186 | 165 | |
ansond | 0:137634ff4186 | 166 | #define R(t) \ |
ansond | 0:137634ff4186 | 167 | ( \ |
ansond | 0:137634ff4186 | 168 | W[t] = S1(W[t - 2]) + W[t - 7] + \ |
ansond | 0:137634ff4186 | 169 | S0(W[t - 15]) + W[t - 16] \ |
ansond | 0:137634ff4186 | 170 | ) |
ansond | 0:137634ff4186 | 171 | |
ansond | 0:137634ff4186 | 172 | #define P(a,b,c,d,e,f,g,h,x,K) \ |
ansond | 0:137634ff4186 | 173 | { \ |
ansond | 0:137634ff4186 | 174 | temp1 = h + S3(e) + F1(e,f,g) + K + x; \ |
ansond | 0:137634ff4186 | 175 | temp2 = S2(a) + F0(a,b,c); \ |
ansond | 0:137634ff4186 | 176 | d += temp1; h = temp1 + temp2; \ |
ansond | 0:137634ff4186 | 177 | } |
ansond | 0:137634ff4186 | 178 | |
ansond | 0:137634ff4186 | 179 | A = ctx->state[0]; |
ansond | 0:137634ff4186 | 180 | B = ctx->state[1]; |
ansond | 0:137634ff4186 | 181 | C = ctx->state[2]; |
ansond | 0:137634ff4186 | 182 | D = ctx->state[3]; |
ansond | 0:137634ff4186 | 183 | E = ctx->state[4]; |
ansond | 0:137634ff4186 | 184 | F = ctx->state[5]; |
ansond | 0:137634ff4186 | 185 | G = ctx->state[6]; |
ansond | 0:137634ff4186 | 186 | H = ctx->state[7]; |
ansond | 0:137634ff4186 | 187 | |
ansond | 0:137634ff4186 | 188 | P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 ); |
ansond | 0:137634ff4186 | 189 | P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 ); |
ansond | 0:137634ff4186 | 190 | P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF ); |
ansond | 0:137634ff4186 | 191 | P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 ); |
ansond | 0:137634ff4186 | 192 | P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B ); |
ansond | 0:137634ff4186 | 193 | P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 ); |
ansond | 0:137634ff4186 | 194 | P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 ); |
ansond | 0:137634ff4186 | 195 | P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 ); |
ansond | 0:137634ff4186 | 196 | P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 ); |
ansond | 0:137634ff4186 | 197 | P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 ); |
ansond | 0:137634ff4186 | 198 | P( G, H, A, B, C, D, E, F, W[10], 0x243185BE ); |
ansond | 0:137634ff4186 | 199 | P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 ); |
ansond | 0:137634ff4186 | 200 | P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 ); |
ansond | 0:137634ff4186 | 201 | P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE ); |
ansond | 0:137634ff4186 | 202 | P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 ); |
ansond | 0:137634ff4186 | 203 | P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 ); |
ansond | 0:137634ff4186 | 204 | P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 ); |
ansond | 0:137634ff4186 | 205 | P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 ); |
ansond | 0:137634ff4186 | 206 | P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 ); |
ansond | 0:137634ff4186 | 207 | P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC ); |
ansond | 0:137634ff4186 | 208 | P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F ); |
ansond | 0:137634ff4186 | 209 | P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA ); |
ansond | 0:137634ff4186 | 210 | P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC ); |
ansond | 0:137634ff4186 | 211 | P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA ); |
ansond | 0:137634ff4186 | 212 | P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 ); |
ansond | 0:137634ff4186 | 213 | P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D ); |
ansond | 0:137634ff4186 | 214 | P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 ); |
ansond | 0:137634ff4186 | 215 | P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 ); |
ansond | 0:137634ff4186 | 216 | P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 ); |
ansond | 0:137634ff4186 | 217 | P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 ); |
ansond | 0:137634ff4186 | 218 | P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 ); |
ansond | 0:137634ff4186 | 219 | P( B, C, D, E, F, G, H, A, R(31), 0x14292967 ); |
ansond | 0:137634ff4186 | 220 | P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 ); |
ansond | 0:137634ff4186 | 221 | P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 ); |
ansond | 0:137634ff4186 | 222 | P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC ); |
ansond | 0:137634ff4186 | 223 | P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 ); |
ansond | 0:137634ff4186 | 224 | P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 ); |
ansond | 0:137634ff4186 | 225 | P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB ); |
ansond | 0:137634ff4186 | 226 | P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E ); |
ansond | 0:137634ff4186 | 227 | P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 ); |
ansond | 0:137634ff4186 | 228 | P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 ); |
ansond | 0:137634ff4186 | 229 | P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B ); |
ansond | 0:137634ff4186 | 230 | P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 ); |
ansond | 0:137634ff4186 | 231 | P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 ); |
ansond | 0:137634ff4186 | 232 | P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 ); |
ansond | 0:137634ff4186 | 233 | P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 ); |
ansond | 0:137634ff4186 | 234 | P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 ); |
ansond | 0:137634ff4186 | 235 | P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 ); |
ansond | 0:137634ff4186 | 236 | P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 ); |
ansond | 0:137634ff4186 | 237 | P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 ); |
ansond | 0:137634ff4186 | 238 | P( G, H, A, B, C, D, E, F, R(50), 0x2748774C ); |
ansond | 0:137634ff4186 | 239 | P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 ); |
ansond | 0:137634ff4186 | 240 | P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 ); |
ansond | 0:137634ff4186 | 241 | P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A ); |
ansond | 0:137634ff4186 | 242 | P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F ); |
ansond | 0:137634ff4186 | 243 | P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 ); |
ansond | 0:137634ff4186 | 244 | P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE ); |
ansond | 0:137634ff4186 | 245 | P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F ); |
ansond | 0:137634ff4186 | 246 | P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 ); |
ansond | 0:137634ff4186 | 247 | P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 ); |
ansond | 0:137634ff4186 | 248 | P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA ); |
ansond | 0:137634ff4186 | 249 | P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB ); |
ansond | 0:137634ff4186 | 250 | P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 ); |
ansond | 0:137634ff4186 | 251 | P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 ); |
ansond | 0:137634ff4186 | 252 | |
ansond | 0:137634ff4186 | 253 | ctx->state[0] += A; |
ansond | 0:137634ff4186 | 254 | ctx->state[1] += B; |
ansond | 0:137634ff4186 | 255 | ctx->state[2] += C; |
ansond | 0:137634ff4186 | 256 | ctx->state[3] += D; |
ansond | 0:137634ff4186 | 257 | ctx->state[4] += E; |
ansond | 0:137634ff4186 | 258 | ctx->state[5] += F; |
ansond | 0:137634ff4186 | 259 | ctx->state[6] += G; |
ansond | 0:137634ff4186 | 260 | ctx->state[7] += H; |
ansond | 0:137634ff4186 | 261 | } |
ansond | 0:137634ff4186 | 262 | |
ansond | 0:137634ff4186 | 263 | /* |
ansond | 0:137634ff4186 | 264 | * SHA-256 process buffer |
ansond | 0:137634ff4186 | 265 | */ |
ansond | 0:137634ff4186 | 266 | void sha256_update( sha256_context *ctx, const unsigned char *input, |
ansond | 0:137634ff4186 | 267 | size_t ilen ) |
ansond | 0:137634ff4186 | 268 | { |
ansond | 0:137634ff4186 | 269 | size_t fill; |
ansond | 0:137634ff4186 | 270 | uint32_t left; |
ansond | 0:137634ff4186 | 271 | |
ansond | 0:137634ff4186 | 272 | if( ilen == 0 ) |
ansond | 0:137634ff4186 | 273 | return; |
ansond | 0:137634ff4186 | 274 | |
ansond | 0:137634ff4186 | 275 | left = ctx->total[0] & 0x3F; |
ansond | 0:137634ff4186 | 276 | fill = 64 - left; |
ansond | 0:137634ff4186 | 277 | |
ansond | 0:137634ff4186 | 278 | ctx->total[0] += (uint32_t) ilen; |
ansond | 0:137634ff4186 | 279 | ctx->total[0] &= 0xFFFFFFFF; |
ansond | 0:137634ff4186 | 280 | |
ansond | 0:137634ff4186 | 281 | if( ctx->total[0] < (uint32_t) ilen ) |
ansond | 0:137634ff4186 | 282 | ctx->total[1]++; |
ansond | 0:137634ff4186 | 283 | |
ansond | 0:137634ff4186 | 284 | if( left && ilen >= fill ) |
ansond | 0:137634ff4186 | 285 | { |
ansond | 0:137634ff4186 | 286 | memcpy( (void *) (ctx->buffer + left), input, fill ); |
ansond | 0:137634ff4186 | 287 | sha256_process( ctx, ctx->buffer ); |
ansond | 0:137634ff4186 | 288 | input += fill; |
ansond | 0:137634ff4186 | 289 | ilen -= fill; |
ansond | 0:137634ff4186 | 290 | left = 0; |
ansond | 0:137634ff4186 | 291 | } |
ansond | 0:137634ff4186 | 292 | |
ansond | 0:137634ff4186 | 293 | while( ilen >= 64 ) |
ansond | 0:137634ff4186 | 294 | { |
ansond | 0:137634ff4186 | 295 | sha256_process( ctx, input ); |
ansond | 0:137634ff4186 | 296 | input += 64; |
ansond | 0:137634ff4186 | 297 | ilen -= 64; |
ansond | 0:137634ff4186 | 298 | } |
ansond | 0:137634ff4186 | 299 | |
ansond | 0:137634ff4186 | 300 | if( ilen > 0 ) |
ansond | 0:137634ff4186 | 301 | memcpy( (void *) (ctx->buffer + left), input, ilen ); |
ansond | 0:137634ff4186 | 302 | } |
ansond | 0:137634ff4186 | 303 | |
ansond | 0:137634ff4186 | 304 | static const unsigned char sha256_padding[64] = |
ansond | 0:137634ff4186 | 305 | { |
ansond | 0:137634ff4186 | 306 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
ansond | 0:137634ff4186 | 307 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
ansond | 0:137634ff4186 | 308 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
ansond | 0:137634ff4186 | 309 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
ansond | 0:137634ff4186 | 310 | }; |
ansond | 0:137634ff4186 | 311 | |
ansond | 0:137634ff4186 | 312 | /* |
ansond | 0:137634ff4186 | 313 | * SHA-256 final digest |
ansond | 0:137634ff4186 | 314 | */ |
ansond | 0:137634ff4186 | 315 | void sha256_finish( sha256_context *ctx, unsigned char output[32] ) |
ansond | 0:137634ff4186 | 316 | { |
ansond | 0:137634ff4186 | 317 | uint32_t last, padn; |
ansond | 0:137634ff4186 | 318 | uint32_t high, low; |
ansond | 0:137634ff4186 | 319 | unsigned char msglen[8]; |
ansond | 0:137634ff4186 | 320 | |
ansond | 0:137634ff4186 | 321 | high = ( ctx->total[0] >> 29 ) |
ansond | 0:137634ff4186 | 322 | | ( ctx->total[1] << 3 ); |
ansond | 0:137634ff4186 | 323 | low = ( ctx->total[0] << 3 ); |
ansond | 0:137634ff4186 | 324 | |
ansond | 0:137634ff4186 | 325 | PUT_UINT32_BE( high, msglen, 0 ); |
ansond | 0:137634ff4186 | 326 | PUT_UINT32_BE( low, msglen, 4 ); |
ansond | 0:137634ff4186 | 327 | |
ansond | 0:137634ff4186 | 328 | last = ctx->total[0] & 0x3F; |
ansond | 0:137634ff4186 | 329 | padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); |
ansond | 0:137634ff4186 | 330 | |
ansond | 0:137634ff4186 | 331 | sha256_update( ctx, sha256_padding, padn ); |
ansond | 0:137634ff4186 | 332 | sha256_update( ctx, msglen, 8 ); |
ansond | 0:137634ff4186 | 333 | |
ansond | 0:137634ff4186 | 334 | PUT_UINT32_BE( ctx->state[0], output, 0 ); |
ansond | 0:137634ff4186 | 335 | PUT_UINT32_BE( ctx->state[1], output, 4 ); |
ansond | 0:137634ff4186 | 336 | PUT_UINT32_BE( ctx->state[2], output, 8 ); |
ansond | 0:137634ff4186 | 337 | PUT_UINT32_BE( ctx->state[3], output, 12 ); |
ansond | 0:137634ff4186 | 338 | PUT_UINT32_BE( ctx->state[4], output, 16 ); |
ansond | 0:137634ff4186 | 339 | PUT_UINT32_BE( ctx->state[5], output, 20 ); |
ansond | 0:137634ff4186 | 340 | PUT_UINT32_BE( ctx->state[6], output, 24 ); |
ansond | 0:137634ff4186 | 341 | |
ansond | 0:137634ff4186 | 342 | if( ctx->is224 == 0 ) |
ansond | 0:137634ff4186 | 343 | PUT_UINT32_BE( ctx->state[7], output, 28 ); |
ansond | 0:137634ff4186 | 344 | } |
ansond | 0:137634ff4186 | 345 | |
ansond | 0:137634ff4186 | 346 | #endif /* !POLARSSL_SHA256_ALT */ |
ansond | 0:137634ff4186 | 347 | |
ansond | 0:137634ff4186 | 348 | /* |
ansond | 0:137634ff4186 | 349 | * output = SHA-256( input buffer ) |
ansond | 0:137634ff4186 | 350 | */ |
ansond | 0:137634ff4186 | 351 | void sha256( const unsigned char *input, size_t ilen, |
ansond | 0:137634ff4186 | 352 | unsigned char output[32], int is224 ) |
ansond | 0:137634ff4186 | 353 | { |
ansond | 0:137634ff4186 | 354 | sha256_context ctx; |
ansond | 0:137634ff4186 | 355 | |
ansond | 0:137634ff4186 | 356 | sha256_init( &ctx ); |
ansond | 0:137634ff4186 | 357 | sha256_starts( &ctx, is224 ); |
ansond | 0:137634ff4186 | 358 | sha256_update( &ctx, input, ilen ); |
ansond | 0:137634ff4186 | 359 | sha256_finish( &ctx, output ); |
ansond | 0:137634ff4186 | 360 | sha256_free( &ctx ); |
ansond | 0:137634ff4186 | 361 | } |
ansond | 0:137634ff4186 | 362 | |
ansond | 0:137634ff4186 | 363 | #if defined(POLARSSL_FS_IO) |
ansond | 0:137634ff4186 | 364 | /* |
ansond | 0:137634ff4186 | 365 | * output = SHA-256( file contents ) |
ansond | 0:137634ff4186 | 366 | */ |
ansond | 0:137634ff4186 | 367 | int sha256_file( const char *path, unsigned char output[32], int is224 ) |
ansond | 0:137634ff4186 | 368 | { |
ansond | 0:137634ff4186 | 369 | FILE *f; |
ansond | 0:137634ff4186 | 370 | size_t n; |
ansond | 0:137634ff4186 | 371 | sha256_context ctx; |
ansond | 0:137634ff4186 | 372 | unsigned char buf[1024]; |
ansond | 0:137634ff4186 | 373 | |
ansond | 0:137634ff4186 | 374 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
ansond | 0:137634ff4186 | 375 | return( POLARSSL_ERR_SHA256_FILE_IO_ERROR ); |
ansond | 0:137634ff4186 | 376 | |
ansond | 0:137634ff4186 | 377 | sha256_init( &ctx ); |
ansond | 0:137634ff4186 | 378 | sha256_starts( &ctx, is224 ); |
ansond | 0:137634ff4186 | 379 | |
ansond | 0:137634ff4186 | 380 | while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) |
ansond | 0:137634ff4186 | 381 | sha256_update( &ctx, buf, n ); |
ansond | 0:137634ff4186 | 382 | |
ansond | 0:137634ff4186 | 383 | sha256_finish( &ctx, output ); |
ansond | 0:137634ff4186 | 384 | sha256_free( &ctx ); |
ansond | 0:137634ff4186 | 385 | |
ansond | 0:137634ff4186 | 386 | if( ferror( f ) != 0 ) |
ansond | 0:137634ff4186 | 387 | { |
ansond | 0:137634ff4186 | 388 | fclose( f ); |
ansond | 0:137634ff4186 | 389 | return( POLARSSL_ERR_SHA256_FILE_IO_ERROR ); |
ansond | 0:137634ff4186 | 390 | } |
ansond | 0:137634ff4186 | 391 | |
ansond | 0:137634ff4186 | 392 | fclose( f ); |
ansond | 0:137634ff4186 | 393 | return( 0 ); |
ansond | 0:137634ff4186 | 394 | } |
ansond | 0:137634ff4186 | 395 | #endif /* POLARSSL_FS_IO */ |
ansond | 0:137634ff4186 | 396 | |
ansond | 0:137634ff4186 | 397 | /* |
ansond | 0:137634ff4186 | 398 | * SHA-256 HMAC context setup |
ansond | 0:137634ff4186 | 399 | */ |
ansond | 0:137634ff4186 | 400 | void sha256_hmac_starts( sha256_context *ctx, const unsigned char *key, |
ansond | 0:137634ff4186 | 401 | size_t keylen, int is224 ) |
ansond | 0:137634ff4186 | 402 | { |
ansond | 0:137634ff4186 | 403 | size_t i; |
ansond | 0:137634ff4186 | 404 | unsigned char sum[32]; |
ansond | 0:137634ff4186 | 405 | |
ansond | 0:137634ff4186 | 406 | if( keylen > 64 ) |
ansond | 0:137634ff4186 | 407 | { |
ansond | 0:137634ff4186 | 408 | sha256( key, keylen, sum, is224 ); |
ansond | 0:137634ff4186 | 409 | keylen = ( is224 ) ? 28 : 32; |
ansond | 0:137634ff4186 | 410 | key = sum; |
ansond | 0:137634ff4186 | 411 | } |
ansond | 0:137634ff4186 | 412 | |
ansond | 0:137634ff4186 | 413 | memset( ctx->ipad, 0x36, 64 ); |
ansond | 0:137634ff4186 | 414 | memset( ctx->opad, 0x5C, 64 ); |
ansond | 0:137634ff4186 | 415 | |
ansond | 0:137634ff4186 | 416 | for( i = 0; i < keylen; i++ ) |
ansond | 0:137634ff4186 | 417 | { |
ansond | 0:137634ff4186 | 418 | ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); |
ansond | 0:137634ff4186 | 419 | ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); |
ansond | 0:137634ff4186 | 420 | } |
ansond | 0:137634ff4186 | 421 | |
ansond | 0:137634ff4186 | 422 | sha256_starts( ctx, is224 ); |
ansond | 0:137634ff4186 | 423 | sha256_update( ctx, ctx->ipad, 64 ); |
ansond | 0:137634ff4186 | 424 | |
ansond | 0:137634ff4186 | 425 | polarssl_zeroize( sum, sizeof( sum ) ); |
ansond | 0:137634ff4186 | 426 | } |
ansond | 0:137634ff4186 | 427 | |
ansond | 0:137634ff4186 | 428 | /* |
ansond | 0:137634ff4186 | 429 | * SHA-256 HMAC process buffer |
ansond | 0:137634ff4186 | 430 | */ |
ansond | 0:137634ff4186 | 431 | void sha256_hmac_update( sha256_context *ctx, const unsigned char *input, |
ansond | 0:137634ff4186 | 432 | size_t ilen ) |
ansond | 0:137634ff4186 | 433 | { |
ansond | 0:137634ff4186 | 434 | sha256_update( ctx, input, ilen ); |
ansond | 0:137634ff4186 | 435 | } |
ansond | 0:137634ff4186 | 436 | |
ansond | 0:137634ff4186 | 437 | /* |
ansond | 0:137634ff4186 | 438 | * SHA-256 HMAC final digest |
ansond | 0:137634ff4186 | 439 | */ |
ansond | 0:137634ff4186 | 440 | void sha256_hmac_finish( sha256_context *ctx, unsigned char output[32] ) |
ansond | 0:137634ff4186 | 441 | { |
ansond | 0:137634ff4186 | 442 | int is224, hlen; |
ansond | 0:137634ff4186 | 443 | unsigned char tmpbuf[32]; |
ansond | 0:137634ff4186 | 444 | |
ansond | 0:137634ff4186 | 445 | is224 = ctx->is224; |
ansond | 0:137634ff4186 | 446 | hlen = ( is224 == 0 ) ? 32 : 28; |
ansond | 0:137634ff4186 | 447 | |
ansond | 0:137634ff4186 | 448 | sha256_finish( ctx, tmpbuf ); |
ansond | 0:137634ff4186 | 449 | sha256_starts( ctx, is224 ); |
ansond | 0:137634ff4186 | 450 | sha256_update( ctx, ctx->opad, 64 ); |
ansond | 0:137634ff4186 | 451 | sha256_update( ctx, tmpbuf, hlen ); |
ansond | 0:137634ff4186 | 452 | sha256_finish( ctx, output ); |
ansond | 0:137634ff4186 | 453 | |
ansond | 0:137634ff4186 | 454 | polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) ); |
ansond | 0:137634ff4186 | 455 | } |
ansond | 0:137634ff4186 | 456 | |
ansond | 0:137634ff4186 | 457 | /* |
ansond | 0:137634ff4186 | 458 | * SHA-256 HMAC context reset |
ansond | 0:137634ff4186 | 459 | */ |
ansond | 0:137634ff4186 | 460 | void sha256_hmac_reset( sha256_context *ctx ) |
ansond | 0:137634ff4186 | 461 | { |
ansond | 0:137634ff4186 | 462 | sha256_starts( ctx, ctx->is224 ); |
ansond | 0:137634ff4186 | 463 | sha256_update( ctx, ctx->ipad, 64 ); |
ansond | 0:137634ff4186 | 464 | } |
ansond | 0:137634ff4186 | 465 | |
ansond | 0:137634ff4186 | 466 | /* |
ansond | 0:137634ff4186 | 467 | * output = HMAC-SHA-256( hmac key, input buffer ) |
ansond | 0:137634ff4186 | 468 | */ |
ansond | 0:137634ff4186 | 469 | void sha256_hmac( const unsigned char *key, size_t keylen, |
ansond | 0:137634ff4186 | 470 | const unsigned char *input, size_t ilen, |
ansond | 0:137634ff4186 | 471 | unsigned char output[32], int is224 ) |
ansond | 0:137634ff4186 | 472 | { |
ansond | 0:137634ff4186 | 473 | sha256_context ctx; |
ansond | 0:137634ff4186 | 474 | |
ansond | 0:137634ff4186 | 475 | sha256_init( &ctx ); |
ansond | 0:137634ff4186 | 476 | sha256_hmac_starts( &ctx, key, keylen, is224 ); |
ansond | 0:137634ff4186 | 477 | sha256_hmac_update( &ctx, input, ilen ); |
ansond | 0:137634ff4186 | 478 | sha256_hmac_finish( &ctx, output ); |
ansond | 0:137634ff4186 | 479 | sha256_free( &ctx ); |
ansond | 0:137634ff4186 | 480 | } |
ansond | 0:137634ff4186 | 481 | |
ansond | 0:137634ff4186 | 482 | #if defined(POLARSSL_SELF_TEST) |
ansond | 0:137634ff4186 | 483 | /* |
ansond | 0:137634ff4186 | 484 | * FIPS-180-2 test vectors |
ansond | 0:137634ff4186 | 485 | */ |
ansond | 0:137634ff4186 | 486 | static const unsigned char sha256_test_buf[3][57] = |
ansond | 0:137634ff4186 | 487 | { |
ansond | 0:137634ff4186 | 488 | { "abc" }, |
ansond | 0:137634ff4186 | 489 | { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, |
ansond | 0:137634ff4186 | 490 | { "" } |
ansond | 0:137634ff4186 | 491 | }; |
ansond | 0:137634ff4186 | 492 | |
ansond | 0:137634ff4186 | 493 | static const int sha256_test_buflen[3] = |
ansond | 0:137634ff4186 | 494 | { |
ansond | 0:137634ff4186 | 495 | 3, 56, 1000 |
ansond | 0:137634ff4186 | 496 | }; |
ansond | 0:137634ff4186 | 497 | |
ansond | 0:137634ff4186 | 498 | static const unsigned char sha256_test_sum[6][32] = |
ansond | 0:137634ff4186 | 499 | { |
ansond | 0:137634ff4186 | 500 | /* |
ansond | 0:137634ff4186 | 501 | * SHA-224 test vectors |
ansond | 0:137634ff4186 | 502 | */ |
ansond | 0:137634ff4186 | 503 | { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22, |
ansond | 0:137634ff4186 | 504 | 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3, |
ansond | 0:137634ff4186 | 505 | 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7, |
ansond | 0:137634ff4186 | 506 | 0xE3, 0x6C, 0x9D, 0xA7 }, |
ansond | 0:137634ff4186 | 507 | { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC, |
ansond | 0:137634ff4186 | 508 | 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50, |
ansond | 0:137634ff4186 | 509 | 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19, |
ansond | 0:137634ff4186 | 510 | 0x52, 0x52, 0x25, 0x25 }, |
ansond | 0:137634ff4186 | 511 | { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8, |
ansond | 0:137634ff4186 | 512 | 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B, |
ansond | 0:137634ff4186 | 513 | 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE, |
ansond | 0:137634ff4186 | 514 | 0x4E, 0xE7, 0xAD, 0x67 }, |
ansond | 0:137634ff4186 | 515 | |
ansond | 0:137634ff4186 | 516 | /* |
ansond | 0:137634ff4186 | 517 | * SHA-256 test vectors |
ansond | 0:137634ff4186 | 518 | */ |
ansond | 0:137634ff4186 | 519 | { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA, |
ansond | 0:137634ff4186 | 520 | 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23, |
ansond | 0:137634ff4186 | 521 | 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C, |
ansond | 0:137634ff4186 | 522 | 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD }, |
ansond | 0:137634ff4186 | 523 | { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, |
ansond | 0:137634ff4186 | 524 | 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, |
ansond | 0:137634ff4186 | 525 | 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, |
ansond | 0:137634ff4186 | 526 | 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 }, |
ansond | 0:137634ff4186 | 527 | { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92, |
ansond | 0:137634ff4186 | 528 | 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67, |
ansond | 0:137634ff4186 | 529 | 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E, |
ansond | 0:137634ff4186 | 530 | 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 } |
ansond | 0:137634ff4186 | 531 | }; |
ansond | 0:137634ff4186 | 532 | |
ansond | 0:137634ff4186 | 533 | /* |
ansond | 0:137634ff4186 | 534 | * RFC 4231 test vectors |
ansond | 0:137634ff4186 | 535 | */ |
ansond | 0:137634ff4186 | 536 | static const unsigned char sha256_hmac_test_key[7][26] = |
ansond | 0:137634ff4186 | 537 | { |
ansond | 0:137634ff4186 | 538 | { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" |
ansond | 0:137634ff4186 | 539 | "\x0B\x0B\x0B\x0B" }, |
ansond | 0:137634ff4186 | 540 | { "Jefe" }, |
ansond | 0:137634ff4186 | 541 | { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
ansond | 0:137634ff4186 | 542 | "\xAA\xAA\xAA\xAA" }, |
ansond | 0:137634ff4186 | 543 | { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10" |
ansond | 0:137634ff4186 | 544 | "\x11\x12\x13\x14\x15\x16\x17\x18\x19" }, |
ansond | 0:137634ff4186 | 545 | { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C" |
ansond | 0:137634ff4186 | 546 | "\x0C\x0C\x0C\x0C" }, |
ansond | 0:137634ff4186 | 547 | { "" }, /* 0xAA 131 times */ |
ansond | 0:137634ff4186 | 548 | { "" } |
ansond | 0:137634ff4186 | 549 | }; |
ansond | 0:137634ff4186 | 550 | |
ansond | 0:137634ff4186 | 551 | static const int sha256_hmac_test_keylen[7] = |
ansond | 0:137634ff4186 | 552 | { |
ansond | 0:137634ff4186 | 553 | 20, 4, 20, 25, 20, 131, 131 |
ansond | 0:137634ff4186 | 554 | }; |
ansond | 0:137634ff4186 | 555 | |
ansond | 0:137634ff4186 | 556 | static const unsigned char sha256_hmac_test_buf[7][153] = |
ansond | 0:137634ff4186 | 557 | { |
ansond | 0:137634ff4186 | 558 | { "Hi There" }, |
ansond | 0:137634ff4186 | 559 | { "what do ya want for nothing?" }, |
ansond | 0:137634ff4186 | 560 | { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
ansond | 0:137634ff4186 | 561 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
ansond | 0:137634ff4186 | 562 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
ansond | 0:137634ff4186 | 563 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" |
ansond | 0:137634ff4186 | 564 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" }, |
ansond | 0:137634ff4186 | 565 | { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
ansond | 0:137634ff4186 | 566 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
ansond | 0:137634ff4186 | 567 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
ansond | 0:137634ff4186 | 568 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" |
ansond | 0:137634ff4186 | 569 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" }, |
ansond | 0:137634ff4186 | 570 | { "Test With Truncation" }, |
ansond | 0:137634ff4186 | 571 | { "Test Using Larger Than Block-Size Key - Hash Key First" }, |
ansond | 0:137634ff4186 | 572 | { "This is a test using a larger than block-size key " |
ansond | 0:137634ff4186 | 573 | "and a larger than block-size data. The key needs to " |
ansond | 0:137634ff4186 | 574 | "be hashed before being used by the HMAC algorithm." } |
ansond | 0:137634ff4186 | 575 | }; |
ansond | 0:137634ff4186 | 576 | |
ansond | 0:137634ff4186 | 577 | static const int sha256_hmac_test_buflen[7] = |
ansond | 0:137634ff4186 | 578 | { |
ansond | 0:137634ff4186 | 579 | 8, 28, 50, 50, 20, 54, 152 |
ansond | 0:137634ff4186 | 580 | }; |
ansond | 0:137634ff4186 | 581 | |
ansond | 0:137634ff4186 | 582 | static const unsigned char sha256_hmac_test_sum[14][32] = |
ansond | 0:137634ff4186 | 583 | { |
ansond | 0:137634ff4186 | 584 | /* |
ansond | 0:137634ff4186 | 585 | * HMAC-SHA-224 test vectors |
ansond | 0:137634ff4186 | 586 | */ |
ansond | 0:137634ff4186 | 587 | { 0x89, 0x6F, 0xB1, 0x12, 0x8A, 0xBB, 0xDF, 0x19, |
ansond | 0:137634ff4186 | 588 | 0x68, 0x32, 0x10, 0x7C, 0xD4, 0x9D, 0xF3, 0x3F, |
ansond | 0:137634ff4186 | 589 | 0x47, 0xB4, 0xB1, 0x16, 0x99, 0x12, 0xBA, 0x4F, |
ansond | 0:137634ff4186 | 590 | 0x53, 0x68, 0x4B, 0x22 }, |
ansond | 0:137634ff4186 | 591 | { 0xA3, 0x0E, 0x01, 0x09, 0x8B, 0xC6, 0xDB, 0xBF, |
ansond | 0:137634ff4186 | 592 | 0x45, 0x69, 0x0F, 0x3A, 0x7E, 0x9E, 0x6D, 0x0F, |
ansond | 0:137634ff4186 | 593 | 0x8B, 0xBE, 0xA2, 0xA3, 0x9E, 0x61, 0x48, 0x00, |
ansond | 0:137634ff4186 | 594 | 0x8F, 0xD0, 0x5E, 0x44 }, |
ansond | 0:137634ff4186 | 595 | { 0x7F, 0xB3, 0xCB, 0x35, 0x88, 0xC6, 0xC1, 0xF6, |
ansond | 0:137634ff4186 | 596 | 0xFF, 0xA9, 0x69, 0x4D, 0x7D, 0x6A, 0xD2, 0x64, |
ansond | 0:137634ff4186 | 597 | 0x93, 0x65, 0xB0, 0xC1, 0xF6, 0x5D, 0x69, 0xD1, |
ansond | 0:137634ff4186 | 598 | 0xEC, 0x83, 0x33, 0xEA }, |
ansond | 0:137634ff4186 | 599 | { 0x6C, 0x11, 0x50, 0x68, 0x74, 0x01, 0x3C, 0xAC, |
ansond | 0:137634ff4186 | 600 | 0x6A, 0x2A, 0xBC, 0x1B, 0xB3, 0x82, 0x62, 0x7C, |
ansond | 0:137634ff4186 | 601 | 0xEC, 0x6A, 0x90, 0xD8, 0x6E, 0xFC, 0x01, 0x2D, |
ansond | 0:137634ff4186 | 602 | 0xE7, 0xAF, 0xEC, 0x5A }, |
ansond | 0:137634ff4186 | 603 | { 0x0E, 0x2A, 0xEA, 0x68, 0xA9, 0x0C, 0x8D, 0x37, |
ansond | 0:137634ff4186 | 604 | 0xC9, 0x88, 0xBC, 0xDB, 0x9F, 0xCA, 0x6F, 0xA8 }, |
ansond | 0:137634ff4186 | 605 | { 0x95, 0xE9, 0xA0, 0xDB, 0x96, 0x20, 0x95, 0xAD, |
ansond | 0:137634ff4186 | 606 | 0xAE, 0xBE, 0x9B, 0x2D, 0x6F, 0x0D, 0xBC, 0xE2, |
ansond | 0:137634ff4186 | 607 | 0xD4, 0x99, 0xF1, 0x12, 0xF2, 0xD2, 0xB7, 0x27, |
ansond | 0:137634ff4186 | 608 | 0x3F, 0xA6, 0x87, 0x0E }, |
ansond | 0:137634ff4186 | 609 | { 0x3A, 0x85, 0x41, 0x66, 0xAC, 0x5D, 0x9F, 0x02, |
ansond | 0:137634ff4186 | 610 | 0x3F, 0x54, 0xD5, 0x17, 0xD0, 0xB3, 0x9D, 0xBD, |
ansond | 0:137634ff4186 | 611 | 0x94, 0x67, 0x70, 0xDB, 0x9C, 0x2B, 0x95, 0xC9, |
ansond | 0:137634ff4186 | 612 | 0xF6, 0xF5, 0x65, 0xD1 }, |
ansond | 0:137634ff4186 | 613 | |
ansond | 0:137634ff4186 | 614 | /* |
ansond | 0:137634ff4186 | 615 | * HMAC-SHA-256 test vectors |
ansond | 0:137634ff4186 | 616 | */ |
ansond | 0:137634ff4186 | 617 | { 0xB0, 0x34, 0x4C, 0x61, 0xD8, 0xDB, 0x38, 0x53, |
ansond | 0:137634ff4186 | 618 | 0x5C, 0xA8, 0xAF, 0xCE, 0xAF, 0x0B, 0xF1, 0x2B, |
ansond | 0:137634ff4186 | 619 | 0x88, 0x1D, 0xC2, 0x00, 0xC9, 0x83, 0x3D, 0xA7, |
ansond | 0:137634ff4186 | 620 | 0x26, 0xE9, 0x37, 0x6C, 0x2E, 0x32, 0xCF, 0xF7 }, |
ansond | 0:137634ff4186 | 621 | { 0x5B, 0xDC, 0xC1, 0x46, 0xBF, 0x60, 0x75, 0x4E, |
ansond | 0:137634ff4186 | 622 | 0x6A, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xC7, |
ansond | 0:137634ff4186 | 623 | 0x5A, 0x00, 0x3F, 0x08, 0x9D, 0x27, 0x39, 0x83, |
ansond | 0:137634ff4186 | 624 | 0x9D, 0xEC, 0x58, 0xB9, 0x64, 0xEC, 0x38, 0x43 }, |
ansond | 0:137634ff4186 | 625 | { 0x77, 0x3E, 0xA9, 0x1E, 0x36, 0x80, 0x0E, 0x46, |
ansond | 0:137634ff4186 | 626 | 0x85, 0x4D, 0xB8, 0xEB, 0xD0, 0x91, 0x81, 0xA7, |
ansond | 0:137634ff4186 | 627 | 0x29, 0x59, 0x09, 0x8B, 0x3E, 0xF8, 0xC1, 0x22, |
ansond | 0:137634ff4186 | 628 | 0xD9, 0x63, 0x55, 0x14, 0xCE, 0xD5, 0x65, 0xFE }, |
ansond | 0:137634ff4186 | 629 | { 0x82, 0x55, 0x8A, 0x38, 0x9A, 0x44, 0x3C, 0x0E, |
ansond | 0:137634ff4186 | 630 | 0xA4, 0xCC, 0x81, 0x98, 0x99, 0xF2, 0x08, 0x3A, |
ansond | 0:137634ff4186 | 631 | 0x85, 0xF0, 0xFA, 0xA3, 0xE5, 0x78, 0xF8, 0x07, |
ansond | 0:137634ff4186 | 632 | 0x7A, 0x2E, 0x3F, 0xF4, 0x67, 0x29, 0x66, 0x5B }, |
ansond | 0:137634ff4186 | 633 | { 0xA3, 0xB6, 0x16, 0x74, 0x73, 0x10, 0x0E, 0xE0, |
ansond | 0:137634ff4186 | 634 | 0x6E, 0x0C, 0x79, 0x6C, 0x29, 0x55, 0x55, 0x2B }, |
ansond | 0:137634ff4186 | 635 | { 0x60, 0xE4, 0x31, 0x59, 0x1E, 0xE0, 0xB6, 0x7F, |
ansond | 0:137634ff4186 | 636 | 0x0D, 0x8A, 0x26, 0xAA, 0xCB, 0xF5, 0xB7, 0x7F, |
ansond | 0:137634ff4186 | 637 | 0x8E, 0x0B, 0xC6, 0x21, 0x37, 0x28, 0xC5, 0x14, |
ansond | 0:137634ff4186 | 638 | 0x05, 0x46, 0x04, 0x0F, 0x0E, 0xE3, 0x7F, 0x54 }, |
ansond | 0:137634ff4186 | 639 | { 0x9B, 0x09, 0xFF, 0xA7, 0x1B, 0x94, 0x2F, 0xCB, |
ansond | 0:137634ff4186 | 640 | 0x27, 0x63, 0x5F, 0xBC, 0xD5, 0xB0, 0xE9, 0x44, |
ansond | 0:137634ff4186 | 641 | 0xBF, 0xDC, 0x63, 0x64, 0x4F, 0x07, 0x13, 0x93, |
ansond | 0:137634ff4186 | 642 | 0x8A, 0x7F, 0x51, 0x53, 0x5C, 0x3A, 0x35, 0xE2 } |
ansond | 0:137634ff4186 | 643 | }; |
ansond | 0:137634ff4186 | 644 | |
ansond | 0:137634ff4186 | 645 | /* |
ansond | 0:137634ff4186 | 646 | * Checkup routine |
ansond | 0:137634ff4186 | 647 | */ |
ansond | 0:137634ff4186 | 648 | int sha256_self_test( int verbose ) |
ansond | 0:137634ff4186 | 649 | { |
ansond | 0:137634ff4186 | 650 | int i, j, k, buflen, ret = 0; |
ansond | 0:137634ff4186 | 651 | unsigned char buf[1024]; |
ansond | 0:137634ff4186 | 652 | unsigned char sha256sum[32]; |
ansond | 0:137634ff4186 | 653 | sha256_context ctx; |
ansond | 0:137634ff4186 | 654 | |
ansond | 0:137634ff4186 | 655 | sha256_init( &ctx ); |
ansond | 0:137634ff4186 | 656 | |
ansond | 0:137634ff4186 | 657 | for( i = 0; i < 6; i++ ) |
ansond | 0:137634ff4186 | 658 | { |
ansond | 0:137634ff4186 | 659 | j = i % 3; |
ansond | 0:137634ff4186 | 660 | k = i < 3; |
ansond | 0:137634ff4186 | 661 | |
ansond | 0:137634ff4186 | 662 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 663 | polarssl_printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 ); |
ansond | 0:137634ff4186 | 664 | |
ansond | 0:137634ff4186 | 665 | sha256_starts( &ctx, k ); |
ansond | 0:137634ff4186 | 666 | |
ansond | 0:137634ff4186 | 667 | if( j == 2 ) |
ansond | 0:137634ff4186 | 668 | { |
ansond | 0:137634ff4186 | 669 | memset( buf, 'a', buflen = 1000 ); |
ansond | 0:137634ff4186 | 670 | |
ansond | 0:137634ff4186 | 671 | for( j = 0; j < 1000; j++ ) |
ansond | 0:137634ff4186 | 672 | sha256_update( &ctx, buf, buflen ); |
ansond | 0:137634ff4186 | 673 | } |
ansond | 0:137634ff4186 | 674 | else |
ansond | 0:137634ff4186 | 675 | sha256_update( &ctx, sha256_test_buf[j], |
ansond | 0:137634ff4186 | 676 | sha256_test_buflen[j] ); |
ansond | 0:137634ff4186 | 677 | |
ansond | 0:137634ff4186 | 678 | sha256_finish( &ctx, sha256sum ); |
ansond | 0:137634ff4186 | 679 | |
ansond | 0:137634ff4186 | 680 | if( memcmp( sha256sum, sha256_test_sum[i], 32 - k * 4 ) != 0 ) |
ansond | 0:137634ff4186 | 681 | { |
ansond | 0:137634ff4186 | 682 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 683 | polarssl_printf( "failed\n" ); |
ansond | 0:137634ff4186 | 684 | |
ansond | 0:137634ff4186 | 685 | ret = 1; |
ansond | 0:137634ff4186 | 686 | goto exit; |
ansond | 0:137634ff4186 | 687 | } |
ansond | 0:137634ff4186 | 688 | |
ansond | 0:137634ff4186 | 689 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 690 | polarssl_printf( "passed\n" ); |
ansond | 0:137634ff4186 | 691 | } |
ansond | 0:137634ff4186 | 692 | |
ansond | 0:137634ff4186 | 693 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 694 | polarssl_printf( "\n" ); |
ansond | 0:137634ff4186 | 695 | |
ansond | 0:137634ff4186 | 696 | for( i = 0; i < 14; i++ ) |
ansond | 0:137634ff4186 | 697 | { |
ansond | 0:137634ff4186 | 698 | j = i % 7; |
ansond | 0:137634ff4186 | 699 | k = i < 7; |
ansond | 0:137634ff4186 | 700 | |
ansond | 0:137634ff4186 | 701 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 702 | polarssl_printf( " HMAC-SHA-%d test #%d: ", 256 - k * 32, j + 1 ); |
ansond | 0:137634ff4186 | 703 | |
ansond | 0:137634ff4186 | 704 | if( j == 5 || j == 6 ) |
ansond | 0:137634ff4186 | 705 | { |
ansond | 0:137634ff4186 | 706 | memset( buf, 0xAA, buflen = 131 ); |
ansond | 0:137634ff4186 | 707 | sha256_hmac_starts( &ctx, buf, buflen, k ); |
ansond | 0:137634ff4186 | 708 | } |
ansond | 0:137634ff4186 | 709 | else |
ansond | 0:137634ff4186 | 710 | sha256_hmac_starts( &ctx, sha256_hmac_test_key[j], |
ansond | 0:137634ff4186 | 711 | sha256_hmac_test_keylen[j], k ); |
ansond | 0:137634ff4186 | 712 | |
ansond | 0:137634ff4186 | 713 | sha256_hmac_update( &ctx, sha256_hmac_test_buf[j], |
ansond | 0:137634ff4186 | 714 | sha256_hmac_test_buflen[j] ); |
ansond | 0:137634ff4186 | 715 | |
ansond | 0:137634ff4186 | 716 | sha256_hmac_finish( &ctx, sha256sum ); |
ansond | 0:137634ff4186 | 717 | |
ansond | 0:137634ff4186 | 718 | buflen = ( j == 4 ) ? 16 : 32 - k * 4; |
ansond | 0:137634ff4186 | 719 | |
ansond | 0:137634ff4186 | 720 | if( memcmp( sha256sum, sha256_hmac_test_sum[i], buflen ) != 0 ) |
ansond | 0:137634ff4186 | 721 | { |
ansond | 0:137634ff4186 | 722 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 723 | polarssl_printf( "failed\n" ); |
ansond | 0:137634ff4186 | 724 | |
ansond | 0:137634ff4186 | 725 | ret = 1; |
ansond | 0:137634ff4186 | 726 | goto exit; |
ansond | 0:137634ff4186 | 727 | } |
ansond | 0:137634ff4186 | 728 | |
ansond | 0:137634ff4186 | 729 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 730 | polarssl_printf( "passed\n" ); |
ansond | 0:137634ff4186 | 731 | } |
ansond | 0:137634ff4186 | 732 | |
ansond | 0:137634ff4186 | 733 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 734 | polarssl_printf( "\n" ); |
ansond | 0:137634ff4186 | 735 | |
ansond | 0:137634ff4186 | 736 | exit: |
ansond | 0:137634ff4186 | 737 | sha256_free( &ctx ); |
ansond | 0:137634ff4186 | 738 | |
ansond | 0:137634ff4186 | 739 | return( ret ); |
ansond | 0:137634ff4186 | 740 | } |
ansond | 0:137634ff4186 | 741 | |
ansond | 0:137634ff4186 | 742 | #endif /* POLARSSL_SELF_TEST */ |
ansond | 0:137634ff4186 | 743 | |
ansond | 0:137634ff4186 | 744 | #endif /* POLARSSL_SHA256_C */ |
ansond | 0:137634ff4186 | 745 |