mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 1:24750b9ad5ef 1 /*
Christopher Haster 1:24750b9ad5ef 2 * FIPS-180-2 compliant SHA-256 implementation
Christopher Haster 1:24750b9ad5ef 3 *
Christopher Haster 1:24750b9ad5ef 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Christopher Haster 1:24750b9ad5ef 5 * SPDX-License-Identifier: Apache-2.0
Christopher Haster 1:24750b9ad5ef 6 *
Christopher Haster 1:24750b9ad5ef 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Christopher Haster 1:24750b9ad5ef 8 * not use this file except in compliance with the License.
Christopher Haster 1:24750b9ad5ef 9 * You may obtain a copy of the License at
Christopher Haster 1:24750b9ad5ef 10 *
Christopher Haster 1:24750b9ad5ef 11 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 1:24750b9ad5ef 12 *
Christopher Haster 1:24750b9ad5ef 13 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 1:24750b9ad5ef 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Christopher Haster 1:24750b9ad5ef 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 1:24750b9ad5ef 16 * See the License for the specific language governing permissions and
Christopher Haster 1:24750b9ad5ef 17 * limitations under the License.
Christopher Haster 1:24750b9ad5ef 18 *
Christopher Haster 1:24750b9ad5ef 19 * This file is part of mbed TLS (https://tls.mbed.org)
Christopher Haster 1:24750b9ad5ef 20 */
Christopher Haster 1:24750b9ad5ef 21 /*
Christopher Haster 1:24750b9ad5ef 22 * The SHA-256 Secure Hash Standard was published by NIST in 2002.
Christopher Haster 1:24750b9ad5ef 23 *
Christopher Haster 1:24750b9ad5ef 24 * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
Christopher Haster 1:24750b9ad5ef 25 */
Christopher Haster 1:24750b9ad5ef 26
Christopher Haster 1:24750b9ad5ef 27 #if !defined(MBEDTLS_CONFIG_FILE)
Christopher Haster 1:24750b9ad5ef 28 #include "mbedtls/config.h"
Christopher Haster 1:24750b9ad5ef 29 #else
Christopher Haster 1:24750b9ad5ef 30 #include MBEDTLS_CONFIG_FILE
Christopher Haster 1:24750b9ad5ef 31 #endif
Christopher Haster 1:24750b9ad5ef 32
Christopher Haster 1:24750b9ad5ef 33 #if defined(MBEDTLS_SHA256_C)
Christopher Haster 1:24750b9ad5ef 34
Christopher Haster 1:24750b9ad5ef 35 #include "mbedtls/sha256.h"
Christopher Haster 1:24750b9ad5ef 36
Christopher Haster 1:24750b9ad5ef 37 #include <string.h>
Christopher Haster 1:24750b9ad5ef 38
Christopher Haster 1:24750b9ad5ef 39 #if defined(MBEDTLS_SELF_TEST)
Christopher Haster 1:24750b9ad5ef 40 #if defined(MBEDTLS_PLATFORM_C)
Christopher Haster 1:24750b9ad5ef 41 #include "mbedtls/platform.h"
Christopher Haster 1:24750b9ad5ef 42 #else
Christopher Haster 1:24750b9ad5ef 43 #include <stdio.h>
Christopher Haster 1:24750b9ad5ef 44 #define mbedtls_printf printf
Christopher Haster 1:24750b9ad5ef 45 #endif /* MBEDTLS_PLATFORM_C */
Christopher Haster 1:24750b9ad5ef 46 #endif /* MBEDTLS_SELF_TEST */
Christopher Haster 1:24750b9ad5ef 47
Christopher Haster 1:24750b9ad5ef 48 #if !defined(MBEDTLS_SHA256_ALT)
Christopher Haster 1:24750b9ad5ef 49
Christopher Haster 1:24750b9ad5ef 50 /* Implementation that should never be optimized out by the compiler */
Christopher Haster 1:24750b9ad5ef 51 static void mbedtls_zeroize( void *v, size_t n ) {
Christopher Haster 1:24750b9ad5ef 52 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
Christopher Haster 1:24750b9ad5ef 53 }
Christopher Haster 1:24750b9ad5ef 54
Christopher Haster 1:24750b9ad5ef 55 /*
Christopher Haster 1:24750b9ad5ef 56 * 32-bit integer manipulation macros (big endian)
Christopher Haster 1:24750b9ad5ef 57 */
Christopher Haster 1:24750b9ad5ef 58 #ifndef GET_UINT32_BE
Christopher Haster 1:24750b9ad5ef 59 #define GET_UINT32_BE(n,b,i) \
Christopher Haster 1:24750b9ad5ef 60 do { \
Christopher Haster 1:24750b9ad5ef 61 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
Christopher Haster 1:24750b9ad5ef 62 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
Christopher Haster 1:24750b9ad5ef 63 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
Christopher Haster 1:24750b9ad5ef 64 | ( (uint32_t) (b)[(i) + 3] ); \
Christopher Haster 1:24750b9ad5ef 65 } while( 0 )
Christopher Haster 1:24750b9ad5ef 66 #endif
Christopher Haster 1:24750b9ad5ef 67
Christopher Haster 1:24750b9ad5ef 68 #ifndef PUT_UINT32_BE
Christopher Haster 1:24750b9ad5ef 69 #define PUT_UINT32_BE(n,b,i) \
Christopher Haster 1:24750b9ad5ef 70 do { \
Christopher Haster 1:24750b9ad5ef 71 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
Christopher Haster 1:24750b9ad5ef 72 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
Christopher Haster 1:24750b9ad5ef 73 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
Christopher Haster 1:24750b9ad5ef 74 (b)[(i) + 3] = (unsigned char) ( (n) ); \
Christopher Haster 1:24750b9ad5ef 75 } while( 0 )
Christopher Haster 1:24750b9ad5ef 76 #endif
Christopher Haster 1:24750b9ad5ef 77
Christopher Haster 1:24750b9ad5ef 78 void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
Christopher Haster 1:24750b9ad5ef 79 {
Christopher Haster 1:24750b9ad5ef 80 memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
Christopher Haster 1:24750b9ad5ef 81 }
Christopher Haster 1:24750b9ad5ef 82
Christopher Haster 1:24750b9ad5ef 83 void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
Christopher Haster 1:24750b9ad5ef 84 {
Christopher Haster 1:24750b9ad5ef 85 if( ctx == NULL )
Christopher Haster 1:24750b9ad5ef 86 return;
Christopher Haster 1:24750b9ad5ef 87
Christopher Haster 1:24750b9ad5ef 88 mbedtls_zeroize( ctx, sizeof( mbedtls_sha256_context ) );
Christopher Haster 1:24750b9ad5ef 89 }
Christopher Haster 1:24750b9ad5ef 90
Christopher Haster 1:24750b9ad5ef 91 void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
Christopher Haster 1:24750b9ad5ef 92 const mbedtls_sha256_context *src )
Christopher Haster 1:24750b9ad5ef 93 {
Christopher Haster 1:24750b9ad5ef 94 *dst = *src;
Christopher Haster 1:24750b9ad5ef 95 }
Christopher Haster 1:24750b9ad5ef 96
Christopher Haster 1:24750b9ad5ef 97 /*
Christopher Haster 1:24750b9ad5ef 98 * SHA-256 context setup
Christopher Haster 1:24750b9ad5ef 99 */
Christopher Haster 1:24750b9ad5ef 100 void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
Christopher Haster 1:24750b9ad5ef 101 {
Christopher Haster 1:24750b9ad5ef 102 ctx->total[0] = 0;
Christopher Haster 1:24750b9ad5ef 103 ctx->total[1] = 0;
Christopher Haster 1:24750b9ad5ef 104
Christopher Haster 1:24750b9ad5ef 105 if( is224 == 0 )
Christopher Haster 1:24750b9ad5ef 106 {
Christopher Haster 1:24750b9ad5ef 107 /* SHA-256 */
Christopher Haster 1:24750b9ad5ef 108 ctx->state[0] = 0x6A09E667;
Christopher Haster 1:24750b9ad5ef 109 ctx->state[1] = 0xBB67AE85;
Christopher Haster 1:24750b9ad5ef 110 ctx->state[2] = 0x3C6EF372;
Christopher Haster 1:24750b9ad5ef 111 ctx->state[3] = 0xA54FF53A;
Christopher Haster 1:24750b9ad5ef 112 ctx->state[4] = 0x510E527F;
Christopher Haster 1:24750b9ad5ef 113 ctx->state[5] = 0x9B05688C;
Christopher Haster 1:24750b9ad5ef 114 ctx->state[6] = 0x1F83D9AB;
Christopher Haster 1:24750b9ad5ef 115 ctx->state[7] = 0x5BE0CD19;
Christopher Haster 1:24750b9ad5ef 116 }
Christopher Haster 1:24750b9ad5ef 117 else
Christopher Haster 1:24750b9ad5ef 118 {
Christopher Haster 1:24750b9ad5ef 119 /* SHA-224 */
Christopher Haster 1:24750b9ad5ef 120 ctx->state[0] = 0xC1059ED8;
Christopher Haster 1:24750b9ad5ef 121 ctx->state[1] = 0x367CD507;
Christopher Haster 1:24750b9ad5ef 122 ctx->state[2] = 0x3070DD17;
Christopher Haster 1:24750b9ad5ef 123 ctx->state[3] = 0xF70E5939;
Christopher Haster 1:24750b9ad5ef 124 ctx->state[4] = 0xFFC00B31;
Christopher Haster 1:24750b9ad5ef 125 ctx->state[5] = 0x68581511;
Christopher Haster 1:24750b9ad5ef 126 ctx->state[6] = 0x64F98FA7;
Christopher Haster 1:24750b9ad5ef 127 ctx->state[7] = 0xBEFA4FA4;
Christopher Haster 1:24750b9ad5ef 128 }
Christopher Haster 1:24750b9ad5ef 129
Christopher Haster 1:24750b9ad5ef 130 ctx->is224 = is224;
Christopher Haster 1:24750b9ad5ef 131 }
Christopher Haster 1:24750b9ad5ef 132
Christopher Haster 1:24750b9ad5ef 133 #if !defined(MBEDTLS_SHA256_PROCESS_ALT)
Christopher Haster 1:24750b9ad5ef 134 static const uint32_t K[] =
Christopher Haster 1:24750b9ad5ef 135 {
Christopher Haster 1:24750b9ad5ef 136 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
Christopher Haster 1:24750b9ad5ef 137 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
Christopher Haster 1:24750b9ad5ef 138 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
Christopher Haster 1:24750b9ad5ef 139 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
Christopher Haster 1:24750b9ad5ef 140 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
Christopher Haster 1:24750b9ad5ef 141 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
Christopher Haster 1:24750b9ad5ef 142 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
Christopher Haster 1:24750b9ad5ef 143 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
Christopher Haster 1:24750b9ad5ef 144 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
Christopher Haster 1:24750b9ad5ef 145 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
Christopher Haster 1:24750b9ad5ef 146 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
Christopher Haster 1:24750b9ad5ef 147 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
Christopher Haster 1:24750b9ad5ef 148 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
Christopher Haster 1:24750b9ad5ef 149 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
Christopher Haster 1:24750b9ad5ef 150 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
Christopher Haster 1:24750b9ad5ef 151 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
Christopher Haster 1:24750b9ad5ef 152 };
Christopher Haster 1:24750b9ad5ef 153
Christopher Haster 1:24750b9ad5ef 154 #define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
Christopher Haster 1:24750b9ad5ef 155 #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
Christopher Haster 1:24750b9ad5ef 156
Christopher Haster 1:24750b9ad5ef 157 #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
Christopher Haster 1:24750b9ad5ef 158 #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
Christopher Haster 1:24750b9ad5ef 159
Christopher Haster 1:24750b9ad5ef 160 #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
Christopher Haster 1:24750b9ad5ef 161 #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
Christopher Haster 1:24750b9ad5ef 162
Christopher Haster 1:24750b9ad5ef 163 #define F0(x,y,z) ((x & y) | (z & (x | y)))
Christopher Haster 1:24750b9ad5ef 164 #define F1(x,y,z) (z ^ (x & (y ^ z)))
Christopher Haster 1:24750b9ad5ef 165
Christopher Haster 1:24750b9ad5ef 166 #define R(t) \
Christopher Haster 1:24750b9ad5ef 167 ( \
Christopher Haster 1:24750b9ad5ef 168 W[t] = S1(W[t - 2]) + W[t - 7] + \
Christopher Haster 1:24750b9ad5ef 169 S0(W[t - 15]) + W[t - 16] \
Christopher Haster 1:24750b9ad5ef 170 )
Christopher Haster 1:24750b9ad5ef 171
Christopher Haster 1:24750b9ad5ef 172 #define P(a,b,c,d,e,f,g,h,x,K) \
Christopher Haster 1:24750b9ad5ef 173 { \
Christopher Haster 1:24750b9ad5ef 174 temp1 = h + S3(e) + F1(e,f,g) + K + x; \
Christopher Haster 1:24750b9ad5ef 175 temp2 = S2(a) + F0(a,b,c); \
Christopher Haster 1:24750b9ad5ef 176 d += temp1; h = temp1 + temp2; \
Christopher Haster 1:24750b9ad5ef 177 }
Christopher Haster 1:24750b9ad5ef 178
Christopher Haster 1:24750b9ad5ef 179 void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] )
Christopher Haster 1:24750b9ad5ef 180 {
Christopher Haster 1:24750b9ad5ef 181 uint32_t temp1, temp2, W[64];
Christopher Haster 1:24750b9ad5ef 182 uint32_t A[8];
Christopher Haster 1:24750b9ad5ef 183 unsigned int i;
Christopher Haster 1:24750b9ad5ef 184
Christopher Haster 1:24750b9ad5ef 185 for( i = 0; i < 8; i++ )
Christopher Haster 1:24750b9ad5ef 186 A[i] = ctx->state[i];
Christopher Haster 1:24750b9ad5ef 187
Christopher Haster 1:24750b9ad5ef 188 #if defined(MBEDTLS_SHA256_SMALLER)
Christopher Haster 1:24750b9ad5ef 189 for( i = 0; i < 64; i++ )
Christopher Haster 1:24750b9ad5ef 190 {
Christopher Haster 1:24750b9ad5ef 191 if( i < 16 )
Christopher Haster 1:24750b9ad5ef 192 GET_UINT32_BE( W[i], data, 4 * i );
Christopher Haster 1:24750b9ad5ef 193 else
Christopher Haster 1:24750b9ad5ef 194 R( i );
Christopher Haster 1:24750b9ad5ef 195
Christopher Haster 1:24750b9ad5ef 196 P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] );
Christopher Haster 1:24750b9ad5ef 197
Christopher Haster 1:24750b9ad5ef 198 temp1 = A[7]; A[7] = A[6]; A[6] = A[5]; A[5] = A[4]; A[4] = A[3];
Christopher Haster 1:24750b9ad5ef 199 A[3] = A[2]; A[2] = A[1]; A[1] = A[0]; A[0] = temp1;
Christopher Haster 1:24750b9ad5ef 200 }
Christopher Haster 1:24750b9ad5ef 201 #else /* MBEDTLS_SHA256_SMALLER */
Christopher Haster 1:24750b9ad5ef 202 for( i = 0; i < 16; i++ )
Christopher Haster 1:24750b9ad5ef 203 GET_UINT32_BE( W[i], data, 4 * i );
Christopher Haster 1:24750b9ad5ef 204
Christopher Haster 1:24750b9ad5ef 205 for( i = 0; i < 16; i += 8 )
Christopher Haster 1:24750b9ad5ef 206 {
Christopher Haster 1:24750b9ad5ef 207 P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i+0], K[i+0] );
Christopher Haster 1:24750b9ad5ef 208 P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i+1], K[i+1] );
Christopher Haster 1:24750b9ad5ef 209 P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i+2], K[i+2] );
Christopher Haster 1:24750b9ad5ef 210 P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i+3], K[i+3] );
Christopher Haster 1:24750b9ad5ef 211 P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i+4], K[i+4] );
Christopher Haster 1:24750b9ad5ef 212 P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i+5], K[i+5] );
Christopher Haster 1:24750b9ad5ef 213 P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i+6], K[i+6] );
Christopher Haster 1:24750b9ad5ef 214 P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i+7], K[i+7] );
Christopher Haster 1:24750b9ad5ef 215 }
Christopher Haster 1:24750b9ad5ef 216
Christopher Haster 1:24750b9ad5ef 217 for( i = 16; i < 64; i += 8 )
Christopher Haster 1:24750b9ad5ef 218 {
Christopher Haster 1:24750b9ad5ef 219 P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], R(i+0), K[i+0] );
Christopher Haster 1:24750b9ad5ef 220 P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], R(i+1), K[i+1] );
Christopher Haster 1:24750b9ad5ef 221 P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], R(i+2), K[i+2] );
Christopher Haster 1:24750b9ad5ef 222 P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], R(i+3), K[i+3] );
Christopher Haster 1:24750b9ad5ef 223 P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], R(i+4), K[i+4] );
Christopher Haster 1:24750b9ad5ef 224 P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], R(i+5), K[i+5] );
Christopher Haster 1:24750b9ad5ef 225 P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], R(i+6), K[i+6] );
Christopher Haster 1:24750b9ad5ef 226 P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], R(i+7), K[i+7] );
Christopher Haster 1:24750b9ad5ef 227 }
Christopher Haster 1:24750b9ad5ef 228 #endif /* MBEDTLS_SHA256_SMALLER */
Christopher Haster 1:24750b9ad5ef 229
Christopher Haster 1:24750b9ad5ef 230 for( i = 0; i < 8; i++ )
Christopher Haster 1:24750b9ad5ef 231 ctx->state[i] += A[i];
Christopher Haster 1:24750b9ad5ef 232 }
Christopher Haster 1:24750b9ad5ef 233 #endif /* !MBEDTLS_SHA256_PROCESS_ALT */
Christopher Haster 1:24750b9ad5ef 234
Christopher Haster 1:24750b9ad5ef 235 /*
Christopher Haster 1:24750b9ad5ef 236 * SHA-256 process buffer
Christopher Haster 1:24750b9ad5ef 237 */
Christopher Haster 1:24750b9ad5ef 238 void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
Christopher Haster 1:24750b9ad5ef 239 size_t ilen )
Christopher Haster 1:24750b9ad5ef 240 {
Christopher Haster 1:24750b9ad5ef 241 size_t fill;
Christopher Haster 1:24750b9ad5ef 242 uint32_t left;
Christopher Haster 1:24750b9ad5ef 243
Christopher Haster 1:24750b9ad5ef 244 if( ilen == 0 )
Christopher Haster 1:24750b9ad5ef 245 return;
Christopher Haster 1:24750b9ad5ef 246
Christopher Haster 1:24750b9ad5ef 247 left = ctx->total[0] & 0x3F;
Christopher Haster 1:24750b9ad5ef 248 fill = 64 - left;
Christopher Haster 1:24750b9ad5ef 249
Christopher Haster 1:24750b9ad5ef 250 ctx->total[0] += (uint32_t) ilen;
Christopher Haster 1:24750b9ad5ef 251 ctx->total[0] &= 0xFFFFFFFF;
Christopher Haster 1:24750b9ad5ef 252
Christopher Haster 1:24750b9ad5ef 253 if( ctx->total[0] < (uint32_t) ilen )
Christopher Haster 1:24750b9ad5ef 254 ctx->total[1]++;
Christopher Haster 1:24750b9ad5ef 255
Christopher Haster 1:24750b9ad5ef 256 if( left && ilen >= fill )
Christopher Haster 1:24750b9ad5ef 257 {
Christopher Haster 1:24750b9ad5ef 258 memcpy( (void *) (ctx->buffer + left), input, fill );
Christopher Haster 1:24750b9ad5ef 259 mbedtls_sha256_process( ctx, ctx->buffer );
Christopher Haster 1:24750b9ad5ef 260 input += fill;
Christopher Haster 1:24750b9ad5ef 261 ilen -= fill;
Christopher Haster 1:24750b9ad5ef 262 left = 0;
Christopher Haster 1:24750b9ad5ef 263 }
Christopher Haster 1:24750b9ad5ef 264
Christopher Haster 1:24750b9ad5ef 265 while( ilen >= 64 )
Christopher Haster 1:24750b9ad5ef 266 {
Christopher Haster 1:24750b9ad5ef 267 mbedtls_sha256_process( ctx, input );
Christopher Haster 1:24750b9ad5ef 268 input += 64;
Christopher Haster 1:24750b9ad5ef 269 ilen -= 64;
Christopher Haster 1:24750b9ad5ef 270 }
Christopher Haster 1:24750b9ad5ef 271
Christopher Haster 1:24750b9ad5ef 272 if( ilen > 0 )
Christopher Haster 1:24750b9ad5ef 273 memcpy( (void *) (ctx->buffer + left), input, ilen );
Christopher Haster 1:24750b9ad5ef 274 }
Christopher Haster 1:24750b9ad5ef 275
Christopher Haster 1:24750b9ad5ef 276 static const unsigned char sha256_padding[64] =
Christopher Haster 1:24750b9ad5ef 277 {
Christopher Haster 1:24750b9ad5ef 278 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Christopher Haster 1:24750b9ad5ef 279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Christopher Haster 1:24750b9ad5ef 280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Christopher Haster 1:24750b9ad5ef 281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Christopher Haster 1:24750b9ad5ef 282 };
Christopher Haster 1:24750b9ad5ef 283
Christopher Haster 1:24750b9ad5ef 284 /*
Christopher Haster 1:24750b9ad5ef 285 * SHA-256 final digest
Christopher Haster 1:24750b9ad5ef 286 */
Christopher Haster 1:24750b9ad5ef 287 void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] )
Christopher Haster 1:24750b9ad5ef 288 {
Christopher Haster 1:24750b9ad5ef 289 uint32_t last, padn;
Christopher Haster 1:24750b9ad5ef 290 uint32_t high, low;
Christopher Haster 1:24750b9ad5ef 291 unsigned char msglen[8];
Christopher Haster 1:24750b9ad5ef 292
Christopher Haster 1:24750b9ad5ef 293 high = ( ctx->total[0] >> 29 )
Christopher Haster 1:24750b9ad5ef 294 | ( ctx->total[1] << 3 );
Christopher Haster 1:24750b9ad5ef 295 low = ( ctx->total[0] << 3 );
Christopher Haster 1:24750b9ad5ef 296
Christopher Haster 1:24750b9ad5ef 297 PUT_UINT32_BE( high, msglen, 0 );
Christopher Haster 1:24750b9ad5ef 298 PUT_UINT32_BE( low, msglen, 4 );
Christopher Haster 1:24750b9ad5ef 299
Christopher Haster 1:24750b9ad5ef 300 last = ctx->total[0] & 0x3F;
Christopher Haster 1:24750b9ad5ef 301 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
Christopher Haster 1:24750b9ad5ef 302
Christopher Haster 1:24750b9ad5ef 303 mbedtls_sha256_update( ctx, sha256_padding, padn );
Christopher Haster 1:24750b9ad5ef 304 mbedtls_sha256_update( ctx, msglen, 8 );
Christopher Haster 1:24750b9ad5ef 305
Christopher Haster 1:24750b9ad5ef 306 PUT_UINT32_BE( ctx->state[0], output, 0 );
Christopher Haster 1:24750b9ad5ef 307 PUT_UINT32_BE( ctx->state[1], output, 4 );
Christopher Haster 1:24750b9ad5ef 308 PUT_UINT32_BE( ctx->state[2], output, 8 );
Christopher Haster 1:24750b9ad5ef 309 PUT_UINT32_BE( ctx->state[3], output, 12 );
Christopher Haster 1:24750b9ad5ef 310 PUT_UINT32_BE( ctx->state[4], output, 16 );
Christopher Haster 1:24750b9ad5ef 311 PUT_UINT32_BE( ctx->state[5], output, 20 );
Christopher Haster 1:24750b9ad5ef 312 PUT_UINT32_BE( ctx->state[6], output, 24 );
Christopher Haster 1:24750b9ad5ef 313
Christopher Haster 1:24750b9ad5ef 314 if( ctx->is224 == 0 )
Christopher Haster 1:24750b9ad5ef 315 PUT_UINT32_BE( ctx->state[7], output, 28 );
Christopher Haster 1:24750b9ad5ef 316 }
Christopher Haster 1:24750b9ad5ef 317
Christopher Haster 1:24750b9ad5ef 318 #endif /* !MBEDTLS_SHA256_ALT */
Christopher Haster 1:24750b9ad5ef 319
Christopher Haster 1:24750b9ad5ef 320 /*
Christopher Haster 1:24750b9ad5ef 321 * output = SHA-256( input buffer )
Christopher Haster 1:24750b9ad5ef 322 */
Christopher Haster 1:24750b9ad5ef 323 void mbedtls_sha256( const unsigned char *input, size_t ilen,
Christopher Haster 1:24750b9ad5ef 324 unsigned char output[32], int is224 )
Christopher Haster 1:24750b9ad5ef 325 {
Christopher Haster 1:24750b9ad5ef 326 mbedtls_sha256_context ctx;
Christopher Haster 1:24750b9ad5ef 327
Christopher Haster 1:24750b9ad5ef 328 mbedtls_sha256_init( &ctx );
Christopher Haster 1:24750b9ad5ef 329 mbedtls_sha256_starts( &ctx, is224 );
Christopher Haster 1:24750b9ad5ef 330 mbedtls_sha256_update( &ctx, input, ilen );
Christopher Haster 1:24750b9ad5ef 331 mbedtls_sha256_finish( &ctx, output );
Christopher Haster 1:24750b9ad5ef 332 mbedtls_sha256_free( &ctx );
Christopher Haster 1:24750b9ad5ef 333 }
Christopher Haster 1:24750b9ad5ef 334
Christopher Haster 1:24750b9ad5ef 335 #if defined(MBEDTLS_SELF_TEST)
Christopher Haster 1:24750b9ad5ef 336 /*
Christopher Haster 1:24750b9ad5ef 337 * FIPS-180-2 test vectors
Christopher Haster 1:24750b9ad5ef 338 */
Christopher Haster 1:24750b9ad5ef 339 static const unsigned char sha256_test_buf[3][57] =
Christopher Haster 1:24750b9ad5ef 340 {
Christopher Haster 1:24750b9ad5ef 341 { "abc" },
Christopher Haster 1:24750b9ad5ef 342 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
Christopher Haster 1:24750b9ad5ef 343 { "" }
Christopher Haster 1:24750b9ad5ef 344 };
Christopher Haster 1:24750b9ad5ef 345
Christopher Haster 1:24750b9ad5ef 346 static const int sha256_test_buflen[3] =
Christopher Haster 1:24750b9ad5ef 347 {
Christopher Haster 1:24750b9ad5ef 348 3, 56, 1000
Christopher Haster 1:24750b9ad5ef 349 };
Christopher Haster 1:24750b9ad5ef 350
Christopher Haster 1:24750b9ad5ef 351 static const unsigned char sha256_test_sum[6][32] =
Christopher Haster 1:24750b9ad5ef 352 {
Christopher Haster 1:24750b9ad5ef 353 /*
Christopher Haster 1:24750b9ad5ef 354 * SHA-224 test vectors
Christopher Haster 1:24750b9ad5ef 355 */
Christopher Haster 1:24750b9ad5ef 356 { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22,
Christopher Haster 1:24750b9ad5ef 357 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3,
Christopher Haster 1:24750b9ad5ef 358 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7,
Christopher Haster 1:24750b9ad5ef 359 0xE3, 0x6C, 0x9D, 0xA7 },
Christopher Haster 1:24750b9ad5ef 360 { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC,
Christopher Haster 1:24750b9ad5ef 361 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50,
Christopher Haster 1:24750b9ad5ef 362 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19,
Christopher Haster 1:24750b9ad5ef 363 0x52, 0x52, 0x25, 0x25 },
Christopher Haster 1:24750b9ad5ef 364 { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8,
Christopher Haster 1:24750b9ad5ef 365 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B,
Christopher Haster 1:24750b9ad5ef 366 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE,
Christopher Haster 1:24750b9ad5ef 367 0x4E, 0xE7, 0xAD, 0x67 },
Christopher Haster 1:24750b9ad5ef 368
Christopher Haster 1:24750b9ad5ef 369 /*
Christopher Haster 1:24750b9ad5ef 370 * SHA-256 test vectors
Christopher Haster 1:24750b9ad5ef 371 */
Christopher Haster 1:24750b9ad5ef 372 { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
Christopher Haster 1:24750b9ad5ef 373 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
Christopher Haster 1:24750b9ad5ef 374 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
Christopher Haster 1:24750b9ad5ef 375 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
Christopher Haster 1:24750b9ad5ef 376 { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
Christopher Haster 1:24750b9ad5ef 377 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
Christopher Haster 1:24750b9ad5ef 378 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
Christopher Haster 1:24750b9ad5ef 379 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
Christopher Haster 1:24750b9ad5ef 380 { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
Christopher Haster 1:24750b9ad5ef 381 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
Christopher Haster 1:24750b9ad5ef 382 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
Christopher Haster 1:24750b9ad5ef 383 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
Christopher Haster 1:24750b9ad5ef 384 };
Christopher Haster 1:24750b9ad5ef 385
Christopher Haster 1:24750b9ad5ef 386 /*
Christopher Haster 1:24750b9ad5ef 387 * Checkup routine
Christopher Haster 1:24750b9ad5ef 388 */
Christopher Haster 1:24750b9ad5ef 389 int mbedtls_sha256_self_test( int verbose )
Christopher Haster 1:24750b9ad5ef 390 {
Christopher Haster 1:24750b9ad5ef 391 int i, j, k, buflen, ret = 0;
Christopher Haster 1:24750b9ad5ef 392 unsigned char buf[1024];
Christopher Haster 1:24750b9ad5ef 393 unsigned char sha256sum[32];
Christopher Haster 1:24750b9ad5ef 394 mbedtls_sha256_context ctx;
Christopher Haster 1:24750b9ad5ef 395
Christopher Haster 1:24750b9ad5ef 396 mbedtls_sha256_init( &ctx );
Christopher Haster 1:24750b9ad5ef 397
Christopher Haster 1:24750b9ad5ef 398 for( i = 0; i < 6; i++ )
Christopher Haster 1:24750b9ad5ef 399 {
Christopher Haster 1:24750b9ad5ef 400 j = i % 3;
Christopher Haster 1:24750b9ad5ef 401 k = i < 3;
Christopher Haster 1:24750b9ad5ef 402
Christopher Haster 1:24750b9ad5ef 403 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 404 mbedtls_printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 );
Christopher Haster 1:24750b9ad5ef 405
Christopher Haster 1:24750b9ad5ef 406 mbedtls_sha256_starts( &ctx, k );
Christopher Haster 1:24750b9ad5ef 407
Christopher Haster 1:24750b9ad5ef 408 if( j == 2 )
Christopher Haster 1:24750b9ad5ef 409 {
Christopher Haster 1:24750b9ad5ef 410 memset( buf, 'a', buflen = 1000 );
Christopher Haster 1:24750b9ad5ef 411
Christopher Haster 1:24750b9ad5ef 412 for( j = 0; j < 1000; j++ )
Christopher Haster 1:24750b9ad5ef 413 mbedtls_sha256_update( &ctx, buf, buflen );
Christopher Haster 1:24750b9ad5ef 414 }
Christopher Haster 1:24750b9ad5ef 415 else
Christopher Haster 1:24750b9ad5ef 416 mbedtls_sha256_update( &ctx, sha256_test_buf[j],
Christopher Haster 1:24750b9ad5ef 417 sha256_test_buflen[j] );
Christopher Haster 1:24750b9ad5ef 418
Christopher Haster 1:24750b9ad5ef 419 mbedtls_sha256_finish( &ctx, sha256sum );
Christopher Haster 1:24750b9ad5ef 420
Christopher Haster 1:24750b9ad5ef 421 if( memcmp( sha256sum, sha256_test_sum[i], 32 - k * 4 ) != 0 )
Christopher Haster 1:24750b9ad5ef 422 {
Christopher Haster 1:24750b9ad5ef 423 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 424 mbedtls_printf( "failed\n" );
Christopher Haster 1:24750b9ad5ef 425
Christopher Haster 1:24750b9ad5ef 426 ret = 1;
Christopher Haster 1:24750b9ad5ef 427 goto exit;
Christopher Haster 1:24750b9ad5ef 428 }
Christopher Haster 1:24750b9ad5ef 429
Christopher Haster 1:24750b9ad5ef 430 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 431 mbedtls_printf( "passed\n" );
Christopher Haster 1:24750b9ad5ef 432 }
Christopher Haster 1:24750b9ad5ef 433
Christopher Haster 1:24750b9ad5ef 434 if( verbose != 0 )
Christopher Haster 1:24750b9ad5ef 435 mbedtls_printf( "\n" );
Christopher Haster 1:24750b9ad5ef 436
Christopher Haster 1:24750b9ad5ef 437 exit:
Christopher Haster 1:24750b9ad5ef 438 mbedtls_sha256_free( &ctx );
Christopher Haster 1:24750b9ad5ef 439
Christopher Haster 1:24750b9ad5ef 440 return( ret );
Christopher Haster 1:24750b9ad5ef 441 }
Christopher Haster 1:24750b9ad5ef 442
Christopher Haster 1:24750b9ad5ef 443 #endif /* MBEDTLS_SELF_TEST */
Christopher Haster 1:24750b9ad5ef 444
Christopher Haster 1:24750b9ad5ef 445 #endif /* MBEDTLS_SHA256_C */