mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
features/mbedtls/mbed-crypto/src/sha512.c@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
- Parent:
- 0:5b88d5760320
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:5b88d5760320 | 1 | /* |
kenjiArai | 0:5b88d5760320 | 2 | * FIPS-180-2 compliant SHA-384/512 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-512 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_SHA512_C) |
kenjiArai | 0:5b88d5760320 | 34 | |
kenjiArai | 0:5b88d5760320 | 35 | #include "mbedtls/sha512.h" |
kenjiArai | 0:5b88d5760320 | 36 | #include "mbedtls/platform_util.h" |
kenjiArai | 0:5b88d5760320 | 37 | |
kenjiArai | 0:5b88d5760320 | 38 | #if defined(_MSC_VER) || defined(__WATCOMC__) |
kenjiArai | 0:5b88d5760320 | 39 | #define UL64(x) x##ui64 |
kenjiArai | 0:5b88d5760320 | 40 | #else |
kenjiArai | 0:5b88d5760320 | 41 | #define UL64(x) x##ULL |
kenjiArai | 0:5b88d5760320 | 42 | #endif |
kenjiArai | 0:5b88d5760320 | 43 | |
kenjiArai | 0:5b88d5760320 | 44 | #include <string.h> |
kenjiArai | 0:5b88d5760320 | 45 | |
kenjiArai | 0:5b88d5760320 | 46 | #if defined(MBEDTLS_SELF_TEST) |
kenjiArai | 0:5b88d5760320 | 47 | #if defined(MBEDTLS_PLATFORM_C) |
kenjiArai | 0:5b88d5760320 | 48 | #include "mbedtls/platform.h" |
kenjiArai | 0:5b88d5760320 | 49 | #else |
kenjiArai | 0:5b88d5760320 | 50 | #include <stdio.h> |
kenjiArai | 0:5b88d5760320 | 51 | #include <stdlib.h> |
kenjiArai | 0:5b88d5760320 | 52 | #define mbedtls_printf printf |
kenjiArai | 0:5b88d5760320 | 53 | #define mbedtls_calloc calloc |
kenjiArai | 0:5b88d5760320 | 54 | #define mbedtls_free free |
kenjiArai | 0:5b88d5760320 | 55 | #endif /* MBEDTLS_PLATFORM_C */ |
kenjiArai | 0:5b88d5760320 | 56 | #endif /* MBEDTLS_SELF_TEST */ |
kenjiArai | 0:5b88d5760320 | 57 | |
kenjiArai | 0:5b88d5760320 | 58 | #define SHA512_VALIDATE_RET(cond) \ |
kenjiArai | 0:5b88d5760320 | 59 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA512_BAD_INPUT_DATA ) |
kenjiArai | 0:5b88d5760320 | 60 | #define SHA512_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond ) |
kenjiArai | 0:5b88d5760320 | 61 | |
kenjiArai | 0:5b88d5760320 | 62 | #if !defined(MBEDTLS_SHA512_ALT) |
kenjiArai | 0:5b88d5760320 | 63 | |
kenjiArai | 0:5b88d5760320 | 64 | /* |
kenjiArai | 0:5b88d5760320 | 65 | * 64-bit integer manipulation macros (big endian) |
kenjiArai | 0:5b88d5760320 | 66 | */ |
kenjiArai | 0:5b88d5760320 | 67 | #ifndef GET_UINT64_BE |
kenjiArai | 0:5b88d5760320 | 68 | #define GET_UINT64_BE(n,b,i) \ |
kenjiArai | 0:5b88d5760320 | 69 | { \ |
kenjiArai | 0:5b88d5760320 | 70 | (n) = ( (uint64_t) (b)[(i) ] << 56 ) \ |
kenjiArai | 0:5b88d5760320 | 71 | | ( (uint64_t) (b)[(i) + 1] << 48 ) \ |
kenjiArai | 0:5b88d5760320 | 72 | | ( (uint64_t) (b)[(i) + 2] << 40 ) \ |
kenjiArai | 0:5b88d5760320 | 73 | | ( (uint64_t) (b)[(i) + 3] << 32 ) \ |
kenjiArai | 0:5b88d5760320 | 74 | | ( (uint64_t) (b)[(i) + 4] << 24 ) \ |
kenjiArai | 0:5b88d5760320 | 75 | | ( (uint64_t) (b)[(i) + 5] << 16 ) \ |
kenjiArai | 0:5b88d5760320 | 76 | | ( (uint64_t) (b)[(i) + 6] << 8 ) \ |
kenjiArai | 0:5b88d5760320 | 77 | | ( (uint64_t) (b)[(i) + 7] ); \ |
kenjiArai | 0:5b88d5760320 | 78 | } |
kenjiArai | 0:5b88d5760320 | 79 | #endif /* GET_UINT64_BE */ |
kenjiArai | 0:5b88d5760320 | 80 | |
kenjiArai | 0:5b88d5760320 | 81 | #ifndef PUT_UINT64_BE |
kenjiArai | 0:5b88d5760320 | 82 | #define PUT_UINT64_BE(n,b,i) \ |
kenjiArai | 0:5b88d5760320 | 83 | { \ |
kenjiArai | 0:5b88d5760320 | 84 | (b)[(i) ] = (unsigned char) ( (n) >> 56 ); \ |
kenjiArai | 0:5b88d5760320 | 85 | (b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \ |
kenjiArai | 0:5b88d5760320 | 86 | (b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \ |
kenjiArai | 0:5b88d5760320 | 87 | (b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \ |
kenjiArai | 0:5b88d5760320 | 88 | (b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \ |
kenjiArai | 0:5b88d5760320 | 89 | (b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \ |
kenjiArai | 0:5b88d5760320 | 90 | (b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \ |
kenjiArai | 0:5b88d5760320 | 91 | (b)[(i) + 7] = (unsigned char) ( (n) ); \ |
kenjiArai | 0:5b88d5760320 | 92 | } |
kenjiArai | 0:5b88d5760320 | 93 | #endif /* PUT_UINT64_BE */ |
kenjiArai | 0:5b88d5760320 | 94 | |
kenjiArai | 1:9db0e321a9f4 | 95 | #if defined(MBEDTLS_SHA512_SMALLER) |
kenjiArai | 1:9db0e321a9f4 | 96 | static void sha512_put_uint64_be( uint64_t n, unsigned char *b, uint8_t i ) |
kenjiArai | 1:9db0e321a9f4 | 97 | { |
kenjiArai | 1:9db0e321a9f4 | 98 | PUT_UINT64_BE(n, b, i); |
kenjiArai | 1:9db0e321a9f4 | 99 | } |
kenjiArai | 1:9db0e321a9f4 | 100 | #else |
kenjiArai | 1:9db0e321a9f4 | 101 | #define sha512_put_uint64_be PUT_UINT64_BE |
kenjiArai | 1:9db0e321a9f4 | 102 | #endif /* MBEDTLS_SHA512_SMALLER */ |
kenjiArai | 1:9db0e321a9f4 | 103 | |
kenjiArai | 0:5b88d5760320 | 104 | void mbedtls_sha512_init( mbedtls_sha512_context *ctx ) |
kenjiArai | 0:5b88d5760320 | 105 | { |
kenjiArai | 0:5b88d5760320 | 106 | SHA512_VALIDATE( ctx != NULL ); |
kenjiArai | 0:5b88d5760320 | 107 | |
kenjiArai | 0:5b88d5760320 | 108 | memset( ctx, 0, sizeof( mbedtls_sha512_context ) ); |
kenjiArai | 0:5b88d5760320 | 109 | } |
kenjiArai | 0:5b88d5760320 | 110 | |
kenjiArai | 0:5b88d5760320 | 111 | void mbedtls_sha512_free( mbedtls_sha512_context *ctx ) |
kenjiArai | 0:5b88d5760320 | 112 | { |
kenjiArai | 0:5b88d5760320 | 113 | if( ctx == NULL ) |
kenjiArai | 0:5b88d5760320 | 114 | return; |
kenjiArai | 0:5b88d5760320 | 115 | |
kenjiArai | 0:5b88d5760320 | 116 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha512_context ) ); |
kenjiArai | 0:5b88d5760320 | 117 | } |
kenjiArai | 0:5b88d5760320 | 118 | |
kenjiArai | 0:5b88d5760320 | 119 | void mbedtls_sha512_clone( mbedtls_sha512_context *dst, |
kenjiArai | 0:5b88d5760320 | 120 | const mbedtls_sha512_context *src ) |
kenjiArai | 0:5b88d5760320 | 121 | { |
kenjiArai | 0:5b88d5760320 | 122 | SHA512_VALIDATE( dst != NULL ); |
kenjiArai | 0:5b88d5760320 | 123 | SHA512_VALIDATE( src != NULL ); |
kenjiArai | 0:5b88d5760320 | 124 | |
kenjiArai | 0:5b88d5760320 | 125 | *dst = *src; |
kenjiArai | 0:5b88d5760320 | 126 | } |
kenjiArai | 0:5b88d5760320 | 127 | |
kenjiArai | 0:5b88d5760320 | 128 | /* |
kenjiArai | 0:5b88d5760320 | 129 | * SHA-512 context setup |
kenjiArai | 0:5b88d5760320 | 130 | */ |
kenjiArai | 0:5b88d5760320 | 131 | int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 ) |
kenjiArai | 0:5b88d5760320 | 132 | { |
kenjiArai | 0:5b88d5760320 | 133 | SHA512_VALIDATE_RET( ctx != NULL ); |
kenjiArai | 0:5b88d5760320 | 134 | SHA512_VALIDATE_RET( is384 == 0 || is384 == 1 ); |
kenjiArai | 0:5b88d5760320 | 135 | |
kenjiArai | 0:5b88d5760320 | 136 | ctx->total[0] = 0; |
kenjiArai | 0:5b88d5760320 | 137 | ctx->total[1] = 0; |
kenjiArai | 0:5b88d5760320 | 138 | |
kenjiArai | 0:5b88d5760320 | 139 | if( is384 == 0 ) |
kenjiArai | 0:5b88d5760320 | 140 | { |
kenjiArai | 0:5b88d5760320 | 141 | /* SHA-512 */ |
kenjiArai | 0:5b88d5760320 | 142 | ctx->state[0] = UL64(0x6A09E667F3BCC908); |
kenjiArai | 0:5b88d5760320 | 143 | ctx->state[1] = UL64(0xBB67AE8584CAA73B); |
kenjiArai | 0:5b88d5760320 | 144 | ctx->state[2] = UL64(0x3C6EF372FE94F82B); |
kenjiArai | 0:5b88d5760320 | 145 | ctx->state[3] = UL64(0xA54FF53A5F1D36F1); |
kenjiArai | 0:5b88d5760320 | 146 | ctx->state[4] = UL64(0x510E527FADE682D1); |
kenjiArai | 0:5b88d5760320 | 147 | ctx->state[5] = UL64(0x9B05688C2B3E6C1F); |
kenjiArai | 0:5b88d5760320 | 148 | ctx->state[6] = UL64(0x1F83D9ABFB41BD6B); |
kenjiArai | 0:5b88d5760320 | 149 | ctx->state[7] = UL64(0x5BE0CD19137E2179); |
kenjiArai | 0:5b88d5760320 | 150 | } |
kenjiArai | 0:5b88d5760320 | 151 | else |
kenjiArai | 0:5b88d5760320 | 152 | { |
kenjiArai | 0:5b88d5760320 | 153 | /* SHA-384 */ |
kenjiArai | 0:5b88d5760320 | 154 | ctx->state[0] = UL64(0xCBBB9D5DC1059ED8); |
kenjiArai | 0:5b88d5760320 | 155 | ctx->state[1] = UL64(0x629A292A367CD507); |
kenjiArai | 0:5b88d5760320 | 156 | ctx->state[2] = UL64(0x9159015A3070DD17); |
kenjiArai | 0:5b88d5760320 | 157 | ctx->state[3] = UL64(0x152FECD8F70E5939); |
kenjiArai | 0:5b88d5760320 | 158 | ctx->state[4] = UL64(0x67332667FFC00B31); |
kenjiArai | 0:5b88d5760320 | 159 | ctx->state[5] = UL64(0x8EB44A8768581511); |
kenjiArai | 0:5b88d5760320 | 160 | ctx->state[6] = UL64(0xDB0C2E0D64F98FA7); |
kenjiArai | 0:5b88d5760320 | 161 | ctx->state[7] = UL64(0x47B5481DBEFA4FA4); |
kenjiArai | 0:5b88d5760320 | 162 | } |
kenjiArai | 0:5b88d5760320 | 163 | |
kenjiArai | 0:5b88d5760320 | 164 | ctx->is384 = is384; |
kenjiArai | 0:5b88d5760320 | 165 | |
kenjiArai | 0:5b88d5760320 | 166 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 167 | } |
kenjiArai | 0:5b88d5760320 | 168 | |
kenjiArai | 0:5b88d5760320 | 169 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
kenjiArai | 0:5b88d5760320 | 170 | void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, |
kenjiArai | 0:5b88d5760320 | 171 | int is384 ) |
kenjiArai | 0:5b88d5760320 | 172 | { |
kenjiArai | 0:5b88d5760320 | 173 | mbedtls_sha512_starts_ret( ctx, is384 ); |
kenjiArai | 0:5b88d5760320 | 174 | } |
kenjiArai | 0:5b88d5760320 | 175 | #endif |
kenjiArai | 0:5b88d5760320 | 176 | |
kenjiArai | 0:5b88d5760320 | 177 | #if !defined(MBEDTLS_SHA512_PROCESS_ALT) |
kenjiArai | 0:5b88d5760320 | 178 | |
kenjiArai | 0:5b88d5760320 | 179 | /* |
kenjiArai | 0:5b88d5760320 | 180 | * Round constants |
kenjiArai | 0:5b88d5760320 | 181 | */ |
kenjiArai | 0:5b88d5760320 | 182 | static const uint64_t K[80] = |
kenjiArai | 0:5b88d5760320 | 183 | { |
kenjiArai | 0:5b88d5760320 | 184 | UL64(0x428A2F98D728AE22), UL64(0x7137449123EF65CD), |
kenjiArai | 0:5b88d5760320 | 185 | UL64(0xB5C0FBCFEC4D3B2F), UL64(0xE9B5DBA58189DBBC), |
kenjiArai | 0:5b88d5760320 | 186 | UL64(0x3956C25BF348B538), UL64(0x59F111F1B605D019), |
kenjiArai | 0:5b88d5760320 | 187 | UL64(0x923F82A4AF194F9B), UL64(0xAB1C5ED5DA6D8118), |
kenjiArai | 0:5b88d5760320 | 188 | UL64(0xD807AA98A3030242), UL64(0x12835B0145706FBE), |
kenjiArai | 0:5b88d5760320 | 189 | UL64(0x243185BE4EE4B28C), UL64(0x550C7DC3D5FFB4E2), |
kenjiArai | 0:5b88d5760320 | 190 | UL64(0x72BE5D74F27B896F), UL64(0x80DEB1FE3B1696B1), |
kenjiArai | 0:5b88d5760320 | 191 | UL64(0x9BDC06A725C71235), UL64(0xC19BF174CF692694), |
kenjiArai | 0:5b88d5760320 | 192 | UL64(0xE49B69C19EF14AD2), UL64(0xEFBE4786384F25E3), |
kenjiArai | 0:5b88d5760320 | 193 | UL64(0x0FC19DC68B8CD5B5), UL64(0x240CA1CC77AC9C65), |
kenjiArai | 0:5b88d5760320 | 194 | UL64(0x2DE92C6F592B0275), UL64(0x4A7484AA6EA6E483), |
kenjiArai | 0:5b88d5760320 | 195 | UL64(0x5CB0A9DCBD41FBD4), UL64(0x76F988DA831153B5), |
kenjiArai | 0:5b88d5760320 | 196 | UL64(0x983E5152EE66DFAB), UL64(0xA831C66D2DB43210), |
kenjiArai | 0:5b88d5760320 | 197 | UL64(0xB00327C898FB213F), UL64(0xBF597FC7BEEF0EE4), |
kenjiArai | 0:5b88d5760320 | 198 | UL64(0xC6E00BF33DA88FC2), UL64(0xD5A79147930AA725), |
kenjiArai | 0:5b88d5760320 | 199 | UL64(0x06CA6351E003826F), UL64(0x142929670A0E6E70), |
kenjiArai | 0:5b88d5760320 | 200 | UL64(0x27B70A8546D22FFC), UL64(0x2E1B21385C26C926), |
kenjiArai | 0:5b88d5760320 | 201 | UL64(0x4D2C6DFC5AC42AED), UL64(0x53380D139D95B3DF), |
kenjiArai | 0:5b88d5760320 | 202 | UL64(0x650A73548BAF63DE), UL64(0x766A0ABB3C77B2A8), |
kenjiArai | 0:5b88d5760320 | 203 | UL64(0x81C2C92E47EDAEE6), UL64(0x92722C851482353B), |
kenjiArai | 0:5b88d5760320 | 204 | UL64(0xA2BFE8A14CF10364), UL64(0xA81A664BBC423001), |
kenjiArai | 0:5b88d5760320 | 205 | UL64(0xC24B8B70D0F89791), UL64(0xC76C51A30654BE30), |
kenjiArai | 0:5b88d5760320 | 206 | UL64(0xD192E819D6EF5218), UL64(0xD69906245565A910), |
kenjiArai | 0:5b88d5760320 | 207 | UL64(0xF40E35855771202A), UL64(0x106AA07032BBD1B8), |
kenjiArai | 0:5b88d5760320 | 208 | UL64(0x19A4C116B8D2D0C8), UL64(0x1E376C085141AB53), |
kenjiArai | 0:5b88d5760320 | 209 | UL64(0x2748774CDF8EEB99), UL64(0x34B0BCB5E19B48A8), |
kenjiArai | 0:5b88d5760320 | 210 | UL64(0x391C0CB3C5C95A63), UL64(0x4ED8AA4AE3418ACB), |
kenjiArai | 0:5b88d5760320 | 211 | UL64(0x5B9CCA4F7763E373), UL64(0x682E6FF3D6B2B8A3), |
kenjiArai | 0:5b88d5760320 | 212 | UL64(0x748F82EE5DEFB2FC), UL64(0x78A5636F43172F60), |
kenjiArai | 0:5b88d5760320 | 213 | UL64(0x84C87814A1F0AB72), UL64(0x8CC702081A6439EC), |
kenjiArai | 0:5b88d5760320 | 214 | UL64(0x90BEFFFA23631E28), UL64(0xA4506CEBDE82BDE9), |
kenjiArai | 0:5b88d5760320 | 215 | UL64(0xBEF9A3F7B2C67915), UL64(0xC67178F2E372532B), |
kenjiArai | 0:5b88d5760320 | 216 | UL64(0xCA273ECEEA26619C), UL64(0xD186B8C721C0C207), |
kenjiArai | 0:5b88d5760320 | 217 | UL64(0xEADA7DD6CDE0EB1E), UL64(0xF57D4F7FEE6ED178), |
kenjiArai | 0:5b88d5760320 | 218 | UL64(0x06F067AA72176FBA), UL64(0x0A637DC5A2C898A6), |
kenjiArai | 0:5b88d5760320 | 219 | UL64(0x113F9804BEF90DAE), UL64(0x1B710B35131C471B), |
kenjiArai | 0:5b88d5760320 | 220 | UL64(0x28DB77F523047D84), UL64(0x32CAAB7B40C72493), |
kenjiArai | 0:5b88d5760320 | 221 | UL64(0x3C9EBE0A15C9BEBC), UL64(0x431D67C49C100D4C), |
kenjiArai | 0:5b88d5760320 | 222 | UL64(0x4CC5D4BECB3E42B6), UL64(0x597F299CFC657E2A), |
kenjiArai | 0:5b88d5760320 | 223 | UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817) |
kenjiArai | 0:5b88d5760320 | 224 | }; |
kenjiArai | 0:5b88d5760320 | 225 | |
kenjiArai | 0:5b88d5760320 | 226 | int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, |
kenjiArai | 0:5b88d5760320 | 227 | const unsigned char data[128] ) |
kenjiArai | 0:5b88d5760320 | 228 | { |
kenjiArai | 0:5b88d5760320 | 229 | int i; |
kenjiArai | 0:5b88d5760320 | 230 | uint64_t temp1, temp2, W[80]; |
kenjiArai | 1:9db0e321a9f4 | 231 | uint64_t A[8]; |
kenjiArai | 0:5b88d5760320 | 232 | |
kenjiArai | 0:5b88d5760320 | 233 | SHA512_VALIDATE_RET( ctx != NULL ); |
kenjiArai | 0:5b88d5760320 | 234 | SHA512_VALIDATE_RET( (const unsigned char *)data != NULL ); |
kenjiArai | 0:5b88d5760320 | 235 | |
kenjiArai | 0:5b88d5760320 | 236 | #define SHR(x,n) ((x) >> (n)) |
kenjiArai | 0:5b88d5760320 | 237 | #define ROTR(x,n) (SHR((x),(n)) | ((x) << (64 - (n)))) |
kenjiArai | 0:5b88d5760320 | 238 | |
kenjiArai | 0:5b88d5760320 | 239 | #define S0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7)) |
kenjiArai | 0:5b88d5760320 | 240 | #define S1(x) (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6)) |
kenjiArai | 0:5b88d5760320 | 241 | |
kenjiArai | 0:5b88d5760320 | 242 | #define S2(x) (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39)) |
kenjiArai | 0:5b88d5760320 | 243 | #define S3(x) (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41)) |
kenjiArai | 0:5b88d5760320 | 244 | |
kenjiArai | 0:5b88d5760320 | 245 | #define F0(x,y,z) (((x) & (y)) | ((z) & ((x) | (y)))) |
kenjiArai | 0:5b88d5760320 | 246 | #define F1(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) |
kenjiArai | 0:5b88d5760320 | 247 | |
kenjiArai | 0:5b88d5760320 | 248 | #define P(a,b,c,d,e,f,g,h,x,K) \ |
kenjiArai | 0:5b88d5760320 | 249 | do \ |
kenjiArai | 0:5b88d5760320 | 250 | { \ |
kenjiArai | 0:5b88d5760320 | 251 | temp1 = (h) + S3(e) + F1((e),(f),(g)) + (K) + (x); \ |
kenjiArai | 0:5b88d5760320 | 252 | temp2 = S2(a) + F0((a),(b),(c)); \ |
kenjiArai | 0:5b88d5760320 | 253 | (d) += temp1; (h) = temp1 + temp2; \ |
kenjiArai | 0:5b88d5760320 | 254 | } while( 0 ) |
kenjiArai | 0:5b88d5760320 | 255 | |
kenjiArai | 1:9db0e321a9f4 | 256 | for( i = 0; i < 8; i++ ) |
kenjiArai | 1:9db0e321a9f4 | 257 | A[i] = ctx->state[i]; |
kenjiArai | 1:9db0e321a9f4 | 258 | |
kenjiArai | 1:9db0e321a9f4 | 259 | #if defined(MBEDTLS_SHA512_SMALLER) |
kenjiArai | 1:9db0e321a9f4 | 260 | for( i = 0; i < 80; i++ ) |
kenjiArai | 1:9db0e321a9f4 | 261 | { |
kenjiArai | 1:9db0e321a9f4 | 262 | if( i < 16 ) |
kenjiArai | 1:9db0e321a9f4 | 263 | { |
kenjiArai | 1:9db0e321a9f4 | 264 | GET_UINT64_BE( W[i], data, i << 3 ); |
kenjiArai | 1:9db0e321a9f4 | 265 | } |
kenjiArai | 1:9db0e321a9f4 | 266 | else |
kenjiArai | 1:9db0e321a9f4 | 267 | { |
kenjiArai | 1:9db0e321a9f4 | 268 | W[i] = S1(W[i - 2]) + W[i - 7] + |
kenjiArai | 1:9db0e321a9f4 | 269 | S0(W[i - 15]) + W[i - 16]; |
kenjiArai | 1:9db0e321a9f4 | 270 | } |
kenjiArai | 1:9db0e321a9f4 | 271 | |
kenjiArai | 1:9db0e321a9f4 | 272 | P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] ); |
kenjiArai | 1:9db0e321a9f4 | 273 | |
kenjiArai | 1:9db0e321a9f4 | 274 | temp1 = A[7]; A[7] = A[6]; A[6] = A[5]; A[5] = A[4]; A[4] = A[3]; |
kenjiArai | 1:9db0e321a9f4 | 275 | A[3] = A[2]; A[2] = A[1]; A[1] = A[0]; A[0] = temp1; |
kenjiArai | 1:9db0e321a9f4 | 276 | } |
kenjiArai | 1:9db0e321a9f4 | 277 | #else /* MBEDTLS_SHA512_SMALLER */ |
kenjiArai | 0:5b88d5760320 | 278 | for( i = 0; i < 16; i++ ) |
kenjiArai | 0:5b88d5760320 | 279 | { |
kenjiArai | 0:5b88d5760320 | 280 | GET_UINT64_BE( W[i], data, i << 3 ); |
kenjiArai | 0:5b88d5760320 | 281 | } |
kenjiArai | 0:5b88d5760320 | 282 | |
kenjiArai | 0:5b88d5760320 | 283 | for( ; i < 80; i++ ) |
kenjiArai | 0:5b88d5760320 | 284 | { |
kenjiArai | 0:5b88d5760320 | 285 | W[i] = S1(W[i - 2]) + W[i - 7] + |
kenjiArai | 0:5b88d5760320 | 286 | S0(W[i - 15]) + W[i - 16]; |
kenjiArai | 0:5b88d5760320 | 287 | } |
kenjiArai | 0:5b88d5760320 | 288 | |
kenjiArai | 0:5b88d5760320 | 289 | i = 0; |
kenjiArai | 0:5b88d5760320 | 290 | do |
kenjiArai | 0:5b88d5760320 | 291 | { |
kenjiArai | 1:9db0e321a9f4 | 292 | P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] ); i++; |
kenjiArai | 1:9db0e321a9f4 | 293 | P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i], K[i] ); i++; |
kenjiArai | 1:9db0e321a9f4 | 294 | P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i], K[i] ); i++; |
kenjiArai | 1:9db0e321a9f4 | 295 | P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i], K[i] ); i++; |
kenjiArai | 1:9db0e321a9f4 | 296 | P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i], K[i] ); i++; |
kenjiArai | 1:9db0e321a9f4 | 297 | P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i], K[i] ); i++; |
kenjiArai | 1:9db0e321a9f4 | 298 | P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i], K[i] ); i++; |
kenjiArai | 1:9db0e321a9f4 | 299 | P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i], K[i] ); i++; |
kenjiArai | 0:5b88d5760320 | 300 | } |
kenjiArai | 0:5b88d5760320 | 301 | while( i < 80 ); |
kenjiArai | 1:9db0e321a9f4 | 302 | #endif /* MBEDTLS_SHA512_SMALLER */ |
kenjiArai | 0:5b88d5760320 | 303 | |
kenjiArai | 1:9db0e321a9f4 | 304 | for( i = 0; i < 8; i++ ) |
kenjiArai | 1:9db0e321a9f4 | 305 | ctx->state[i] += A[i]; |
kenjiArai | 0:5b88d5760320 | 306 | |
kenjiArai | 0:5b88d5760320 | 307 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 308 | } |
kenjiArai | 0:5b88d5760320 | 309 | |
kenjiArai | 0:5b88d5760320 | 310 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
kenjiArai | 0:5b88d5760320 | 311 | void mbedtls_sha512_process( mbedtls_sha512_context *ctx, |
kenjiArai | 0:5b88d5760320 | 312 | const unsigned char data[128] ) |
kenjiArai | 0:5b88d5760320 | 313 | { |
kenjiArai | 0:5b88d5760320 | 314 | mbedtls_internal_sha512_process( ctx, data ); |
kenjiArai | 0:5b88d5760320 | 315 | } |
kenjiArai | 0:5b88d5760320 | 316 | #endif |
kenjiArai | 0:5b88d5760320 | 317 | #endif /* !MBEDTLS_SHA512_PROCESS_ALT */ |
kenjiArai | 0:5b88d5760320 | 318 | |
kenjiArai | 0:5b88d5760320 | 319 | /* |
kenjiArai | 0:5b88d5760320 | 320 | * SHA-512 process buffer |
kenjiArai | 0:5b88d5760320 | 321 | */ |
kenjiArai | 0:5b88d5760320 | 322 | int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, |
kenjiArai | 0:5b88d5760320 | 323 | const unsigned char *input, |
kenjiArai | 0:5b88d5760320 | 324 | size_t ilen ) |
kenjiArai | 0:5b88d5760320 | 325 | { |
kenjiArai | 0:5b88d5760320 | 326 | int ret; |
kenjiArai | 0:5b88d5760320 | 327 | size_t fill; |
kenjiArai | 0:5b88d5760320 | 328 | unsigned int left; |
kenjiArai | 0:5b88d5760320 | 329 | |
kenjiArai | 0:5b88d5760320 | 330 | SHA512_VALIDATE_RET( ctx != NULL ); |
kenjiArai | 0:5b88d5760320 | 331 | SHA512_VALIDATE_RET( ilen == 0 || input != NULL ); |
kenjiArai | 0:5b88d5760320 | 332 | |
kenjiArai | 0:5b88d5760320 | 333 | if( ilen == 0 ) |
kenjiArai | 0:5b88d5760320 | 334 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 335 | |
kenjiArai | 0:5b88d5760320 | 336 | left = (unsigned int) (ctx->total[0] & 0x7F); |
kenjiArai | 0:5b88d5760320 | 337 | fill = 128 - left; |
kenjiArai | 0:5b88d5760320 | 338 | |
kenjiArai | 0:5b88d5760320 | 339 | ctx->total[0] += (uint64_t) ilen; |
kenjiArai | 0:5b88d5760320 | 340 | |
kenjiArai | 0:5b88d5760320 | 341 | if( ctx->total[0] < (uint64_t) ilen ) |
kenjiArai | 0:5b88d5760320 | 342 | ctx->total[1]++; |
kenjiArai | 0:5b88d5760320 | 343 | |
kenjiArai | 0:5b88d5760320 | 344 | if( left && ilen >= fill ) |
kenjiArai | 0:5b88d5760320 | 345 | { |
kenjiArai | 0:5b88d5760320 | 346 | memcpy( (void *) (ctx->buffer + left), input, fill ); |
kenjiArai | 0:5b88d5760320 | 347 | |
kenjiArai | 0:5b88d5760320 | 348 | if( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 349 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 350 | |
kenjiArai | 0:5b88d5760320 | 351 | input += fill; |
kenjiArai | 0:5b88d5760320 | 352 | ilen -= fill; |
kenjiArai | 0:5b88d5760320 | 353 | left = 0; |
kenjiArai | 0:5b88d5760320 | 354 | } |
kenjiArai | 0:5b88d5760320 | 355 | |
kenjiArai | 0:5b88d5760320 | 356 | while( ilen >= 128 ) |
kenjiArai | 0:5b88d5760320 | 357 | { |
kenjiArai | 0:5b88d5760320 | 358 | if( ( ret = mbedtls_internal_sha512_process( ctx, input ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 359 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 360 | |
kenjiArai | 0:5b88d5760320 | 361 | input += 128; |
kenjiArai | 0:5b88d5760320 | 362 | ilen -= 128; |
kenjiArai | 0:5b88d5760320 | 363 | } |
kenjiArai | 0:5b88d5760320 | 364 | |
kenjiArai | 0:5b88d5760320 | 365 | if( ilen > 0 ) |
kenjiArai | 0:5b88d5760320 | 366 | memcpy( (void *) (ctx->buffer + left), input, ilen ); |
kenjiArai | 0:5b88d5760320 | 367 | |
kenjiArai | 0:5b88d5760320 | 368 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 369 | } |
kenjiArai | 0:5b88d5760320 | 370 | |
kenjiArai | 0:5b88d5760320 | 371 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
kenjiArai | 0:5b88d5760320 | 372 | void mbedtls_sha512_update( mbedtls_sha512_context *ctx, |
kenjiArai | 0:5b88d5760320 | 373 | const unsigned char *input, |
kenjiArai | 0:5b88d5760320 | 374 | size_t ilen ) |
kenjiArai | 0:5b88d5760320 | 375 | { |
kenjiArai | 0:5b88d5760320 | 376 | mbedtls_sha512_update_ret( ctx, input, ilen ); |
kenjiArai | 0:5b88d5760320 | 377 | } |
kenjiArai | 0:5b88d5760320 | 378 | #endif |
kenjiArai | 0:5b88d5760320 | 379 | |
kenjiArai | 0:5b88d5760320 | 380 | /* |
kenjiArai | 0:5b88d5760320 | 381 | * SHA-512 final digest |
kenjiArai | 0:5b88d5760320 | 382 | */ |
kenjiArai | 0:5b88d5760320 | 383 | int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx, |
kenjiArai | 0:5b88d5760320 | 384 | unsigned char output[64] ) |
kenjiArai | 0:5b88d5760320 | 385 | { |
kenjiArai | 0:5b88d5760320 | 386 | int ret; |
kenjiArai | 0:5b88d5760320 | 387 | unsigned used; |
kenjiArai | 0:5b88d5760320 | 388 | uint64_t high, low; |
kenjiArai | 0:5b88d5760320 | 389 | |
kenjiArai | 0:5b88d5760320 | 390 | SHA512_VALIDATE_RET( ctx != NULL ); |
kenjiArai | 0:5b88d5760320 | 391 | SHA512_VALIDATE_RET( (unsigned char *)output != NULL ); |
kenjiArai | 0:5b88d5760320 | 392 | |
kenjiArai | 0:5b88d5760320 | 393 | /* |
kenjiArai | 0:5b88d5760320 | 394 | * Add padding: 0x80 then 0x00 until 16 bytes remain for the length |
kenjiArai | 0:5b88d5760320 | 395 | */ |
kenjiArai | 0:5b88d5760320 | 396 | used = ctx->total[0] & 0x7F; |
kenjiArai | 0:5b88d5760320 | 397 | |
kenjiArai | 0:5b88d5760320 | 398 | ctx->buffer[used++] = 0x80; |
kenjiArai | 0:5b88d5760320 | 399 | |
kenjiArai | 0:5b88d5760320 | 400 | if( used <= 112 ) |
kenjiArai | 0:5b88d5760320 | 401 | { |
kenjiArai | 0:5b88d5760320 | 402 | /* Enough room for padding + length in current block */ |
kenjiArai | 0:5b88d5760320 | 403 | memset( ctx->buffer + used, 0, 112 - used ); |
kenjiArai | 0:5b88d5760320 | 404 | } |
kenjiArai | 0:5b88d5760320 | 405 | else |
kenjiArai | 0:5b88d5760320 | 406 | { |
kenjiArai | 0:5b88d5760320 | 407 | /* We'll need an extra block */ |
kenjiArai | 0:5b88d5760320 | 408 | memset( ctx->buffer + used, 0, 128 - used ); |
kenjiArai | 0:5b88d5760320 | 409 | |
kenjiArai | 0:5b88d5760320 | 410 | if( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 411 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 412 | |
kenjiArai | 0:5b88d5760320 | 413 | memset( ctx->buffer, 0, 112 ); |
kenjiArai | 0:5b88d5760320 | 414 | } |
kenjiArai | 0:5b88d5760320 | 415 | |
kenjiArai | 0:5b88d5760320 | 416 | /* |
kenjiArai | 0:5b88d5760320 | 417 | * Add message length |
kenjiArai | 0:5b88d5760320 | 418 | */ |
kenjiArai | 0:5b88d5760320 | 419 | high = ( ctx->total[0] >> 61 ) |
kenjiArai | 0:5b88d5760320 | 420 | | ( ctx->total[1] << 3 ); |
kenjiArai | 0:5b88d5760320 | 421 | low = ( ctx->total[0] << 3 ); |
kenjiArai | 0:5b88d5760320 | 422 | |
kenjiArai | 1:9db0e321a9f4 | 423 | sha512_put_uint64_be( high, ctx->buffer, 112 ); |
kenjiArai | 1:9db0e321a9f4 | 424 | sha512_put_uint64_be( low, ctx->buffer, 120 ); |
kenjiArai | 0:5b88d5760320 | 425 | |
kenjiArai | 0:5b88d5760320 | 426 | if( ( ret = mbedtls_internal_sha512_process( ctx, ctx->buffer ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 427 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 428 | |
kenjiArai | 0:5b88d5760320 | 429 | /* |
kenjiArai | 0:5b88d5760320 | 430 | * Output final state |
kenjiArai | 0:5b88d5760320 | 431 | */ |
kenjiArai | 1:9db0e321a9f4 | 432 | sha512_put_uint64_be( ctx->state[0], output, 0 ); |
kenjiArai | 1:9db0e321a9f4 | 433 | sha512_put_uint64_be( ctx->state[1], output, 8 ); |
kenjiArai | 1:9db0e321a9f4 | 434 | sha512_put_uint64_be( ctx->state[2], output, 16 ); |
kenjiArai | 1:9db0e321a9f4 | 435 | sha512_put_uint64_be( ctx->state[3], output, 24 ); |
kenjiArai | 1:9db0e321a9f4 | 436 | sha512_put_uint64_be( ctx->state[4], output, 32 ); |
kenjiArai | 1:9db0e321a9f4 | 437 | sha512_put_uint64_be( ctx->state[5], output, 40 ); |
kenjiArai | 0:5b88d5760320 | 438 | |
kenjiArai | 0:5b88d5760320 | 439 | if( ctx->is384 == 0 ) |
kenjiArai | 0:5b88d5760320 | 440 | { |
kenjiArai | 1:9db0e321a9f4 | 441 | sha512_put_uint64_be( ctx->state[6], output, 48 ); |
kenjiArai | 1:9db0e321a9f4 | 442 | sha512_put_uint64_be( ctx->state[7], output, 56 ); |
kenjiArai | 0:5b88d5760320 | 443 | } |
kenjiArai | 0:5b88d5760320 | 444 | |
kenjiArai | 0:5b88d5760320 | 445 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 446 | } |
kenjiArai | 0:5b88d5760320 | 447 | |
kenjiArai | 0:5b88d5760320 | 448 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
kenjiArai | 0:5b88d5760320 | 449 | void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, |
kenjiArai | 0:5b88d5760320 | 450 | unsigned char output[64] ) |
kenjiArai | 0:5b88d5760320 | 451 | { |
kenjiArai | 0:5b88d5760320 | 452 | mbedtls_sha512_finish_ret( ctx, output ); |
kenjiArai | 0:5b88d5760320 | 453 | } |
kenjiArai | 0:5b88d5760320 | 454 | #endif |
kenjiArai | 0:5b88d5760320 | 455 | |
kenjiArai | 0:5b88d5760320 | 456 | #endif /* !MBEDTLS_SHA512_ALT */ |
kenjiArai | 0:5b88d5760320 | 457 | |
kenjiArai | 0:5b88d5760320 | 458 | /* |
kenjiArai | 0:5b88d5760320 | 459 | * output = SHA-512( input buffer ) |
kenjiArai | 0:5b88d5760320 | 460 | */ |
kenjiArai | 0:5b88d5760320 | 461 | int mbedtls_sha512_ret( const unsigned char *input, |
kenjiArai | 0:5b88d5760320 | 462 | size_t ilen, |
kenjiArai | 0:5b88d5760320 | 463 | unsigned char output[64], |
kenjiArai | 0:5b88d5760320 | 464 | int is384 ) |
kenjiArai | 0:5b88d5760320 | 465 | { |
kenjiArai | 0:5b88d5760320 | 466 | int ret; |
kenjiArai | 0:5b88d5760320 | 467 | mbedtls_sha512_context ctx; |
kenjiArai | 0:5b88d5760320 | 468 | |
kenjiArai | 0:5b88d5760320 | 469 | SHA512_VALIDATE_RET( is384 == 0 || is384 == 1 ); |
kenjiArai | 0:5b88d5760320 | 470 | SHA512_VALIDATE_RET( ilen == 0 || input != NULL ); |
kenjiArai | 0:5b88d5760320 | 471 | SHA512_VALIDATE_RET( (unsigned char *)output != NULL ); |
kenjiArai | 0:5b88d5760320 | 472 | |
kenjiArai | 0:5b88d5760320 | 473 | mbedtls_sha512_init( &ctx ); |
kenjiArai | 0:5b88d5760320 | 474 | |
kenjiArai | 0:5b88d5760320 | 475 | if( ( ret = mbedtls_sha512_starts_ret( &ctx, is384 ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 476 | goto exit; |
kenjiArai | 0:5b88d5760320 | 477 | |
kenjiArai | 0:5b88d5760320 | 478 | if( ( ret = mbedtls_sha512_update_ret( &ctx, input, ilen ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 479 | goto exit; |
kenjiArai | 0:5b88d5760320 | 480 | |
kenjiArai | 0:5b88d5760320 | 481 | if( ( ret = mbedtls_sha512_finish_ret( &ctx, output ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 482 | goto exit; |
kenjiArai | 0:5b88d5760320 | 483 | |
kenjiArai | 0:5b88d5760320 | 484 | exit: |
kenjiArai | 0:5b88d5760320 | 485 | mbedtls_sha512_free( &ctx ); |
kenjiArai | 0:5b88d5760320 | 486 | |
kenjiArai | 0:5b88d5760320 | 487 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 488 | } |
kenjiArai | 0:5b88d5760320 | 489 | |
kenjiArai | 0:5b88d5760320 | 490 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
kenjiArai | 0:5b88d5760320 | 491 | void mbedtls_sha512( const unsigned char *input, |
kenjiArai | 0:5b88d5760320 | 492 | size_t ilen, |
kenjiArai | 0:5b88d5760320 | 493 | unsigned char output[64], |
kenjiArai | 0:5b88d5760320 | 494 | int is384 ) |
kenjiArai | 0:5b88d5760320 | 495 | { |
kenjiArai | 0:5b88d5760320 | 496 | mbedtls_sha512_ret( input, ilen, output, is384 ); |
kenjiArai | 0:5b88d5760320 | 497 | } |
kenjiArai | 0:5b88d5760320 | 498 | #endif |
kenjiArai | 0:5b88d5760320 | 499 | |
kenjiArai | 0:5b88d5760320 | 500 | #if defined(MBEDTLS_SELF_TEST) |
kenjiArai | 0:5b88d5760320 | 501 | |
kenjiArai | 0:5b88d5760320 | 502 | /* |
kenjiArai | 0:5b88d5760320 | 503 | * FIPS-180-2 test vectors |
kenjiArai | 0:5b88d5760320 | 504 | */ |
kenjiArai | 0:5b88d5760320 | 505 | static const unsigned char sha512_test_buf[3][113] = |
kenjiArai | 0:5b88d5760320 | 506 | { |
kenjiArai | 0:5b88d5760320 | 507 | { "abc" }, |
kenjiArai | 0:5b88d5760320 | 508 | { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" |
kenjiArai | 0:5b88d5760320 | 509 | "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" }, |
kenjiArai | 0:5b88d5760320 | 510 | { "" } |
kenjiArai | 0:5b88d5760320 | 511 | }; |
kenjiArai | 0:5b88d5760320 | 512 | |
kenjiArai | 0:5b88d5760320 | 513 | static const size_t sha512_test_buflen[3] = |
kenjiArai | 0:5b88d5760320 | 514 | { |
kenjiArai | 0:5b88d5760320 | 515 | 3, 112, 1000 |
kenjiArai | 0:5b88d5760320 | 516 | }; |
kenjiArai | 0:5b88d5760320 | 517 | |
kenjiArai | 0:5b88d5760320 | 518 | static const unsigned char sha512_test_sum[6][64] = |
kenjiArai | 0:5b88d5760320 | 519 | { |
kenjiArai | 0:5b88d5760320 | 520 | /* |
kenjiArai | 0:5b88d5760320 | 521 | * SHA-384 test vectors |
kenjiArai | 0:5b88d5760320 | 522 | */ |
kenjiArai | 0:5b88d5760320 | 523 | { 0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B, |
kenjiArai | 0:5b88d5760320 | 524 | 0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07, |
kenjiArai | 0:5b88d5760320 | 525 | 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63, |
kenjiArai | 0:5b88d5760320 | 526 | 0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED, |
kenjiArai | 0:5b88d5760320 | 527 | 0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23, |
kenjiArai | 0:5b88d5760320 | 528 | 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7 }, |
kenjiArai | 0:5b88d5760320 | 529 | { 0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8, |
kenjiArai | 0:5b88d5760320 | 530 | 0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47, |
kenjiArai | 0:5b88d5760320 | 531 | 0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2, |
kenjiArai | 0:5b88d5760320 | 532 | 0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12, |
kenjiArai | 0:5b88d5760320 | 533 | 0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9, |
kenjiArai | 0:5b88d5760320 | 534 | 0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39 }, |
kenjiArai | 0:5b88d5760320 | 535 | { 0x9D, 0x0E, 0x18, 0x09, 0x71, 0x64, 0x74, 0xCB, |
kenjiArai | 0:5b88d5760320 | 536 | 0x08, 0x6E, 0x83, 0x4E, 0x31, 0x0A, 0x4A, 0x1C, |
kenjiArai | 0:5b88d5760320 | 537 | 0xED, 0x14, 0x9E, 0x9C, 0x00, 0xF2, 0x48, 0x52, |
kenjiArai | 0:5b88d5760320 | 538 | 0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B, |
kenjiArai | 0:5b88d5760320 | 539 | 0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB, |
kenjiArai | 0:5b88d5760320 | 540 | 0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85 }, |
kenjiArai | 0:5b88d5760320 | 541 | |
kenjiArai | 0:5b88d5760320 | 542 | /* |
kenjiArai | 0:5b88d5760320 | 543 | * SHA-512 test vectors |
kenjiArai | 0:5b88d5760320 | 544 | */ |
kenjiArai | 0:5b88d5760320 | 545 | { 0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA, |
kenjiArai | 0:5b88d5760320 | 546 | 0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31, |
kenjiArai | 0:5b88d5760320 | 547 | 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2, |
kenjiArai | 0:5b88d5760320 | 548 | 0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A, |
kenjiArai | 0:5b88d5760320 | 549 | 0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8, |
kenjiArai | 0:5b88d5760320 | 550 | 0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD, |
kenjiArai | 0:5b88d5760320 | 551 | 0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E, |
kenjiArai | 0:5b88d5760320 | 552 | 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F }, |
kenjiArai | 0:5b88d5760320 | 553 | { 0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA, |
kenjiArai | 0:5b88d5760320 | 554 | 0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F, |
kenjiArai | 0:5b88d5760320 | 555 | 0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1, |
kenjiArai | 0:5b88d5760320 | 556 | 0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18, |
kenjiArai | 0:5b88d5760320 | 557 | 0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4, |
kenjiArai | 0:5b88d5760320 | 558 | 0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A, |
kenjiArai | 0:5b88d5760320 | 559 | 0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54, |
kenjiArai | 0:5b88d5760320 | 560 | 0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09 }, |
kenjiArai | 0:5b88d5760320 | 561 | { 0xE7, 0x18, 0x48, 0x3D, 0x0C, 0xE7, 0x69, 0x64, |
kenjiArai | 0:5b88d5760320 | 562 | 0x4E, 0x2E, 0x42, 0xC7, 0xBC, 0x15, 0xB4, 0x63, |
kenjiArai | 0:5b88d5760320 | 563 | 0x8E, 0x1F, 0x98, 0xB1, 0x3B, 0x20, 0x44, 0x28, |
kenjiArai | 0:5b88d5760320 | 564 | 0x56, 0x32, 0xA8, 0x03, 0xAF, 0xA9, 0x73, 0xEB, |
kenjiArai | 0:5b88d5760320 | 565 | 0xDE, 0x0F, 0xF2, 0x44, 0x87, 0x7E, 0xA6, 0x0A, |
kenjiArai | 0:5b88d5760320 | 566 | 0x4C, 0xB0, 0x43, 0x2C, 0xE5, 0x77, 0xC3, 0x1B, |
kenjiArai | 0:5b88d5760320 | 567 | 0xEB, 0x00, 0x9C, 0x5C, 0x2C, 0x49, 0xAA, 0x2E, |
kenjiArai | 0:5b88d5760320 | 568 | 0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B } |
kenjiArai | 0:5b88d5760320 | 569 | }; |
kenjiArai | 0:5b88d5760320 | 570 | |
kenjiArai | 0:5b88d5760320 | 571 | /* |
kenjiArai | 0:5b88d5760320 | 572 | * Checkup routine |
kenjiArai | 0:5b88d5760320 | 573 | */ |
kenjiArai | 0:5b88d5760320 | 574 | int mbedtls_sha512_self_test( int verbose ) |
kenjiArai | 0:5b88d5760320 | 575 | { |
kenjiArai | 0:5b88d5760320 | 576 | int i, j, k, buflen, ret = 0; |
kenjiArai | 0:5b88d5760320 | 577 | unsigned char *buf; |
kenjiArai | 0:5b88d5760320 | 578 | unsigned char sha512sum[64]; |
kenjiArai | 0:5b88d5760320 | 579 | mbedtls_sha512_context ctx; |
kenjiArai | 0:5b88d5760320 | 580 | |
kenjiArai | 0:5b88d5760320 | 581 | buf = mbedtls_calloc( 1024, sizeof(unsigned char) ); |
kenjiArai | 0:5b88d5760320 | 582 | if( NULL == buf ) |
kenjiArai | 0:5b88d5760320 | 583 | { |
kenjiArai | 0:5b88d5760320 | 584 | if( verbose != 0 ) |
kenjiArai | 0:5b88d5760320 | 585 | mbedtls_printf( "Buffer allocation failed\n" ); |
kenjiArai | 0:5b88d5760320 | 586 | |
kenjiArai | 0:5b88d5760320 | 587 | return( 1 ); |
kenjiArai | 0:5b88d5760320 | 588 | } |
kenjiArai | 0:5b88d5760320 | 589 | |
kenjiArai | 0:5b88d5760320 | 590 | mbedtls_sha512_init( &ctx ); |
kenjiArai | 0:5b88d5760320 | 591 | |
kenjiArai | 0:5b88d5760320 | 592 | for( i = 0; i < 6; i++ ) |
kenjiArai | 0:5b88d5760320 | 593 | { |
kenjiArai | 0:5b88d5760320 | 594 | j = i % 3; |
kenjiArai | 0:5b88d5760320 | 595 | k = i < 3; |
kenjiArai | 0:5b88d5760320 | 596 | |
kenjiArai | 0:5b88d5760320 | 597 | if( verbose != 0 ) |
kenjiArai | 0:5b88d5760320 | 598 | mbedtls_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 ); |
kenjiArai | 0:5b88d5760320 | 599 | |
kenjiArai | 0:5b88d5760320 | 600 | if( ( ret = mbedtls_sha512_starts_ret( &ctx, k ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 601 | goto fail; |
kenjiArai | 0:5b88d5760320 | 602 | |
kenjiArai | 0:5b88d5760320 | 603 | if( j == 2 ) |
kenjiArai | 0:5b88d5760320 | 604 | { |
kenjiArai | 0:5b88d5760320 | 605 | memset( buf, 'a', buflen = 1000 ); |
kenjiArai | 0:5b88d5760320 | 606 | |
kenjiArai | 0:5b88d5760320 | 607 | for( j = 0; j < 1000; j++ ) |
kenjiArai | 0:5b88d5760320 | 608 | { |
kenjiArai | 0:5b88d5760320 | 609 | ret = mbedtls_sha512_update_ret( &ctx, buf, buflen ); |
kenjiArai | 0:5b88d5760320 | 610 | if( ret != 0 ) |
kenjiArai | 0:5b88d5760320 | 611 | goto fail; |
kenjiArai | 0:5b88d5760320 | 612 | } |
kenjiArai | 0:5b88d5760320 | 613 | } |
kenjiArai | 0:5b88d5760320 | 614 | else |
kenjiArai | 0:5b88d5760320 | 615 | { |
kenjiArai | 0:5b88d5760320 | 616 | ret = mbedtls_sha512_update_ret( &ctx, sha512_test_buf[j], |
kenjiArai | 0:5b88d5760320 | 617 | sha512_test_buflen[j] ); |
kenjiArai | 0:5b88d5760320 | 618 | if( ret != 0 ) |
kenjiArai | 0:5b88d5760320 | 619 | goto fail; |
kenjiArai | 0:5b88d5760320 | 620 | } |
kenjiArai | 0:5b88d5760320 | 621 | |
kenjiArai | 0:5b88d5760320 | 622 | if( ( ret = mbedtls_sha512_finish_ret( &ctx, sha512sum ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 623 | goto fail; |
kenjiArai | 0:5b88d5760320 | 624 | |
kenjiArai | 0:5b88d5760320 | 625 | if( memcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 626 | { |
kenjiArai | 0:5b88d5760320 | 627 | ret = 1; |
kenjiArai | 0:5b88d5760320 | 628 | goto fail; |
kenjiArai | 0:5b88d5760320 | 629 | } |
kenjiArai | 0:5b88d5760320 | 630 | |
kenjiArai | 0:5b88d5760320 | 631 | if( verbose != 0 ) |
kenjiArai | 0:5b88d5760320 | 632 | mbedtls_printf( "passed\n" ); |
kenjiArai | 0:5b88d5760320 | 633 | } |
kenjiArai | 0:5b88d5760320 | 634 | |
kenjiArai | 0:5b88d5760320 | 635 | if( verbose != 0 ) |
kenjiArai | 0:5b88d5760320 | 636 | mbedtls_printf( "\n" ); |
kenjiArai | 0:5b88d5760320 | 637 | |
kenjiArai | 0:5b88d5760320 | 638 | goto exit; |
kenjiArai | 0:5b88d5760320 | 639 | |
kenjiArai | 0:5b88d5760320 | 640 | fail: |
kenjiArai | 0:5b88d5760320 | 641 | if( verbose != 0 ) |
kenjiArai | 0:5b88d5760320 | 642 | mbedtls_printf( "failed\n" ); |
kenjiArai | 0:5b88d5760320 | 643 | |
kenjiArai | 0:5b88d5760320 | 644 | exit: |
kenjiArai | 0:5b88d5760320 | 645 | mbedtls_sha512_free( &ctx ); |
kenjiArai | 0:5b88d5760320 | 646 | mbedtls_free( buf ); |
kenjiArai | 0:5b88d5760320 | 647 | |
kenjiArai | 0:5b88d5760320 | 648 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 649 | } |
kenjiArai | 0:5b88d5760320 | 650 | |
kenjiArai | 0:5b88d5760320 | 651 | #endif /* MBEDTLS_SELF_TEST */ |
kenjiArai | 0:5b88d5760320 | 652 | |
kenjiArai | 0:5b88d5760320 | 653 | #endif /* MBEDTLS_SHA512_C */ |