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