mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 17 23:23:45 2019 +0000
Revision:
0:5b88d5760320
mbed-os5 only for TYBLE16

Who changed what in which revision?

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