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