This is a fork due to permission issues
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of 6_songs-from-the-cloud by
mbed-client/mbedtls/source/sha512.c@0:f7c60d3e7b8a, 2016-05-18 (annotated)
- Committer:
- maclobdell
- Date:
- Wed May 18 19:06:32 2016 +0000
- Revision:
- 0:f7c60d3e7b8a
clean version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maclobdell | 0:f7c60d3e7b8a | 1 | /* |
maclobdell | 0:f7c60d3e7b8a | 2 | * FIPS-180-2 compliant SHA-384/512 implementation |
maclobdell | 0:f7c60d3e7b8a | 3 | * |
maclobdell | 0:f7c60d3e7b8a | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
maclobdell | 0:f7c60d3e7b8a | 5 | * SPDX-License-Identifier: Apache-2.0 |
maclobdell | 0:f7c60d3e7b8a | 6 | * |
maclobdell | 0:f7c60d3e7b8a | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
maclobdell | 0:f7c60d3e7b8a | 8 | * not use this file except in compliance with the License. |
maclobdell | 0:f7c60d3e7b8a | 9 | * You may obtain a copy of the License at |
maclobdell | 0:f7c60d3e7b8a | 10 | * |
maclobdell | 0:f7c60d3e7b8a | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
maclobdell | 0:f7c60d3e7b8a | 12 | * |
maclobdell | 0:f7c60d3e7b8a | 13 | * Unless required by applicable law or agreed to in writing, software |
maclobdell | 0:f7c60d3e7b8a | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
maclobdell | 0:f7c60d3e7b8a | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
maclobdell | 0:f7c60d3e7b8a | 16 | * See the License for the specific language governing permissions and |
maclobdell | 0:f7c60d3e7b8a | 17 | * limitations under the License. |
maclobdell | 0:f7c60d3e7b8a | 18 | * |
maclobdell | 0:f7c60d3e7b8a | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
maclobdell | 0:f7c60d3e7b8a | 20 | */ |
maclobdell | 0:f7c60d3e7b8a | 21 | /* |
maclobdell | 0:f7c60d3e7b8a | 22 | * The SHA-512 Secure Hash Standard was published by NIST in 2002. |
maclobdell | 0:f7c60d3e7b8a | 23 | * |
maclobdell | 0:f7c60d3e7b8a | 24 | * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
maclobdell | 0:f7c60d3e7b8a | 25 | */ |
maclobdell | 0:f7c60d3e7b8a | 26 | |
maclobdell | 0:f7c60d3e7b8a | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
maclobdell | 0:f7c60d3e7b8a | 28 | #include "mbedtls/config.h" |
maclobdell | 0:f7c60d3e7b8a | 29 | #else |
maclobdell | 0:f7c60d3e7b8a | 30 | #include MBEDTLS_CONFIG_FILE |
maclobdell | 0:f7c60d3e7b8a | 31 | #endif |
maclobdell | 0:f7c60d3e7b8a | 32 | |
maclobdell | 0:f7c60d3e7b8a | 33 | #if defined(MBEDTLS_SHA512_C) |
maclobdell | 0:f7c60d3e7b8a | 34 | |
maclobdell | 0:f7c60d3e7b8a | 35 | #include "mbedtls/sha512.h" |
maclobdell | 0:f7c60d3e7b8a | 36 | |
maclobdell | 0:f7c60d3e7b8a | 37 | #if defined(_MSC_VER) || defined(__WATCOMC__) |
maclobdell | 0:f7c60d3e7b8a | 38 | #define UL64(x) x##ui64 |
maclobdell | 0:f7c60d3e7b8a | 39 | #else |
maclobdell | 0:f7c60d3e7b8a | 40 | #define UL64(x) x##ULL |
maclobdell | 0:f7c60d3e7b8a | 41 | #endif |
maclobdell | 0:f7c60d3e7b8a | 42 | |
maclobdell | 0:f7c60d3e7b8a | 43 | #include <string.h> |
maclobdell | 0:f7c60d3e7b8a | 44 | |
maclobdell | 0:f7c60d3e7b8a | 45 | #if defined(MBEDTLS_SELF_TEST) |
maclobdell | 0:f7c60d3e7b8a | 46 | #if defined(MBEDTLS_PLATFORM_C) |
maclobdell | 0:f7c60d3e7b8a | 47 | #include "mbedtls/platform.h" |
maclobdell | 0:f7c60d3e7b8a | 48 | #else |
maclobdell | 0:f7c60d3e7b8a | 49 | #include <stdio.h> |
maclobdell | 0:f7c60d3e7b8a | 50 | #define mbedtls_printf printf |
maclobdell | 0:f7c60d3e7b8a | 51 | #endif /* MBEDTLS_PLATFORM_C */ |
maclobdell | 0:f7c60d3e7b8a | 52 | #endif /* MBEDTLS_SELF_TEST */ |
maclobdell | 0:f7c60d3e7b8a | 53 | |
maclobdell | 0:f7c60d3e7b8a | 54 | #if !defined(MBEDTLS_SHA512_ALT) |
maclobdell | 0:f7c60d3e7b8a | 55 | |
maclobdell | 0:f7c60d3e7b8a | 56 | /* Implementation that should never be optimized out by the compiler */ |
maclobdell | 0:f7c60d3e7b8a | 57 | static void mbedtls_zeroize( void *v, size_t n ) { |
maclobdell | 0:f7c60d3e7b8a | 58 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
maclobdell | 0:f7c60d3e7b8a | 59 | } |
maclobdell | 0:f7c60d3e7b8a | 60 | |
maclobdell | 0:f7c60d3e7b8a | 61 | /* |
maclobdell | 0:f7c60d3e7b8a | 62 | * 64-bit integer manipulation macros (big endian) |
maclobdell | 0:f7c60d3e7b8a | 63 | */ |
maclobdell | 0:f7c60d3e7b8a | 64 | #ifndef GET_UINT64_BE |
maclobdell | 0:f7c60d3e7b8a | 65 | #define GET_UINT64_BE(n,b,i) \ |
maclobdell | 0:f7c60d3e7b8a | 66 | { \ |
maclobdell | 0:f7c60d3e7b8a | 67 | (n) = ( (uint64_t) (b)[(i) ] << 56 ) \ |
maclobdell | 0:f7c60d3e7b8a | 68 | | ( (uint64_t) (b)[(i) + 1] << 48 ) \ |
maclobdell | 0:f7c60d3e7b8a | 69 | | ( (uint64_t) (b)[(i) + 2] << 40 ) \ |
maclobdell | 0:f7c60d3e7b8a | 70 | | ( (uint64_t) (b)[(i) + 3] << 32 ) \ |
maclobdell | 0:f7c60d3e7b8a | 71 | | ( (uint64_t) (b)[(i) + 4] << 24 ) \ |
maclobdell | 0:f7c60d3e7b8a | 72 | | ( (uint64_t) (b)[(i) + 5] << 16 ) \ |
maclobdell | 0:f7c60d3e7b8a | 73 | | ( (uint64_t) (b)[(i) + 6] << 8 ) \ |
maclobdell | 0:f7c60d3e7b8a | 74 | | ( (uint64_t) (b)[(i) + 7] ); \ |
maclobdell | 0:f7c60d3e7b8a | 75 | } |
maclobdell | 0:f7c60d3e7b8a | 76 | #endif /* GET_UINT64_BE */ |
maclobdell | 0:f7c60d3e7b8a | 77 | |
maclobdell | 0:f7c60d3e7b8a | 78 | #ifndef PUT_UINT64_BE |
maclobdell | 0:f7c60d3e7b8a | 79 | #define PUT_UINT64_BE(n,b,i) \ |
maclobdell | 0:f7c60d3e7b8a | 80 | { \ |
maclobdell | 0:f7c60d3e7b8a | 81 | (b)[(i) ] = (unsigned char) ( (n) >> 56 ); \ |
maclobdell | 0:f7c60d3e7b8a | 82 | (b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \ |
maclobdell | 0:f7c60d3e7b8a | 83 | (b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \ |
maclobdell | 0:f7c60d3e7b8a | 84 | (b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \ |
maclobdell | 0:f7c60d3e7b8a | 85 | (b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \ |
maclobdell | 0:f7c60d3e7b8a | 86 | (b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \ |
maclobdell | 0:f7c60d3e7b8a | 87 | (b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \ |
maclobdell | 0:f7c60d3e7b8a | 88 | (b)[(i) + 7] = (unsigned char) ( (n) ); \ |
maclobdell | 0:f7c60d3e7b8a | 89 | } |
maclobdell | 0:f7c60d3e7b8a | 90 | #endif /* PUT_UINT64_BE */ |
maclobdell | 0:f7c60d3e7b8a | 91 | |
maclobdell | 0:f7c60d3e7b8a | 92 | /* |
maclobdell | 0:f7c60d3e7b8a | 93 | * Round constants |
maclobdell | 0:f7c60d3e7b8a | 94 | */ |
maclobdell | 0:f7c60d3e7b8a | 95 | static const uint64_t K[80] = |
maclobdell | 0:f7c60d3e7b8a | 96 | { |
maclobdell | 0:f7c60d3e7b8a | 97 | UL64(0x428A2F98D728AE22), UL64(0x7137449123EF65CD), |
maclobdell | 0:f7c60d3e7b8a | 98 | UL64(0xB5C0FBCFEC4D3B2F), UL64(0xE9B5DBA58189DBBC), |
maclobdell | 0:f7c60d3e7b8a | 99 | UL64(0x3956C25BF348B538), UL64(0x59F111F1B605D019), |
maclobdell | 0:f7c60d3e7b8a | 100 | UL64(0x923F82A4AF194F9B), UL64(0xAB1C5ED5DA6D8118), |
maclobdell | 0:f7c60d3e7b8a | 101 | UL64(0xD807AA98A3030242), UL64(0x12835B0145706FBE), |
maclobdell | 0:f7c60d3e7b8a | 102 | UL64(0x243185BE4EE4B28C), UL64(0x550C7DC3D5FFB4E2), |
maclobdell | 0:f7c60d3e7b8a | 103 | UL64(0x72BE5D74F27B896F), UL64(0x80DEB1FE3B1696B1), |
maclobdell | 0:f7c60d3e7b8a | 104 | UL64(0x9BDC06A725C71235), UL64(0xC19BF174CF692694), |
maclobdell | 0:f7c60d3e7b8a | 105 | UL64(0xE49B69C19EF14AD2), UL64(0xEFBE4786384F25E3), |
maclobdell | 0:f7c60d3e7b8a | 106 | UL64(0x0FC19DC68B8CD5B5), UL64(0x240CA1CC77AC9C65), |
maclobdell | 0:f7c60d3e7b8a | 107 | UL64(0x2DE92C6F592B0275), UL64(0x4A7484AA6EA6E483), |
maclobdell | 0:f7c60d3e7b8a | 108 | UL64(0x5CB0A9DCBD41FBD4), UL64(0x76F988DA831153B5), |
maclobdell | 0:f7c60d3e7b8a | 109 | UL64(0x983E5152EE66DFAB), UL64(0xA831C66D2DB43210), |
maclobdell | 0:f7c60d3e7b8a | 110 | UL64(0xB00327C898FB213F), UL64(0xBF597FC7BEEF0EE4), |
maclobdell | 0:f7c60d3e7b8a | 111 | UL64(0xC6E00BF33DA88FC2), UL64(0xD5A79147930AA725), |
maclobdell | 0:f7c60d3e7b8a | 112 | UL64(0x06CA6351E003826F), UL64(0x142929670A0E6E70), |
maclobdell | 0:f7c60d3e7b8a | 113 | UL64(0x27B70A8546D22FFC), UL64(0x2E1B21385C26C926), |
maclobdell | 0:f7c60d3e7b8a | 114 | UL64(0x4D2C6DFC5AC42AED), UL64(0x53380D139D95B3DF), |
maclobdell | 0:f7c60d3e7b8a | 115 | UL64(0x650A73548BAF63DE), UL64(0x766A0ABB3C77B2A8), |
maclobdell | 0:f7c60d3e7b8a | 116 | UL64(0x81C2C92E47EDAEE6), UL64(0x92722C851482353B), |
maclobdell | 0:f7c60d3e7b8a | 117 | UL64(0xA2BFE8A14CF10364), UL64(0xA81A664BBC423001), |
maclobdell | 0:f7c60d3e7b8a | 118 | UL64(0xC24B8B70D0F89791), UL64(0xC76C51A30654BE30), |
maclobdell | 0:f7c60d3e7b8a | 119 | UL64(0xD192E819D6EF5218), UL64(0xD69906245565A910), |
maclobdell | 0:f7c60d3e7b8a | 120 | UL64(0xF40E35855771202A), UL64(0x106AA07032BBD1B8), |
maclobdell | 0:f7c60d3e7b8a | 121 | UL64(0x19A4C116B8D2D0C8), UL64(0x1E376C085141AB53), |
maclobdell | 0:f7c60d3e7b8a | 122 | UL64(0x2748774CDF8EEB99), UL64(0x34B0BCB5E19B48A8), |
maclobdell | 0:f7c60d3e7b8a | 123 | UL64(0x391C0CB3C5C95A63), UL64(0x4ED8AA4AE3418ACB), |
maclobdell | 0:f7c60d3e7b8a | 124 | UL64(0x5B9CCA4F7763E373), UL64(0x682E6FF3D6B2B8A3), |
maclobdell | 0:f7c60d3e7b8a | 125 | UL64(0x748F82EE5DEFB2FC), UL64(0x78A5636F43172F60), |
maclobdell | 0:f7c60d3e7b8a | 126 | UL64(0x84C87814A1F0AB72), UL64(0x8CC702081A6439EC), |
maclobdell | 0:f7c60d3e7b8a | 127 | UL64(0x90BEFFFA23631E28), UL64(0xA4506CEBDE82BDE9), |
maclobdell | 0:f7c60d3e7b8a | 128 | UL64(0xBEF9A3F7B2C67915), UL64(0xC67178F2E372532B), |
maclobdell | 0:f7c60d3e7b8a | 129 | UL64(0xCA273ECEEA26619C), UL64(0xD186B8C721C0C207), |
maclobdell | 0:f7c60d3e7b8a | 130 | UL64(0xEADA7DD6CDE0EB1E), UL64(0xF57D4F7FEE6ED178), |
maclobdell | 0:f7c60d3e7b8a | 131 | UL64(0x06F067AA72176FBA), UL64(0x0A637DC5A2C898A6), |
maclobdell | 0:f7c60d3e7b8a | 132 | UL64(0x113F9804BEF90DAE), UL64(0x1B710B35131C471B), |
maclobdell | 0:f7c60d3e7b8a | 133 | UL64(0x28DB77F523047D84), UL64(0x32CAAB7B40C72493), |
maclobdell | 0:f7c60d3e7b8a | 134 | UL64(0x3C9EBE0A15C9BEBC), UL64(0x431D67C49C100D4C), |
maclobdell | 0:f7c60d3e7b8a | 135 | UL64(0x4CC5D4BECB3E42B6), UL64(0x597F299CFC657E2A), |
maclobdell | 0:f7c60d3e7b8a | 136 | UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817) |
maclobdell | 0:f7c60d3e7b8a | 137 | }; |
maclobdell | 0:f7c60d3e7b8a | 138 | |
maclobdell | 0:f7c60d3e7b8a | 139 | void mbedtls_sha512_init( mbedtls_sha512_context *ctx ) |
maclobdell | 0:f7c60d3e7b8a | 140 | { |
maclobdell | 0:f7c60d3e7b8a | 141 | memset( ctx, 0, sizeof( mbedtls_sha512_context ) ); |
maclobdell | 0:f7c60d3e7b8a | 142 | } |
maclobdell | 0:f7c60d3e7b8a | 143 | |
maclobdell | 0:f7c60d3e7b8a | 144 | void mbedtls_sha512_free( mbedtls_sha512_context *ctx ) |
maclobdell | 0:f7c60d3e7b8a | 145 | { |
maclobdell | 0:f7c60d3e7b8a | 146 | if( ctx == NULL ) |
maclobdell | 0:f7c60d3e7b8a | 147 | return; |
maclobdell | 0:f7c60d3e7b8a | 148 | |
maclobdell | 0:f7c60d3e7b8a | 149 | mbedtls_zeroize( ctx, sizeof( mbedtls_sha512_context ) ); |
maclobdell | 0:f7c60d3e7b8a | 150 | } |
maclobdell | 0:f7c60d3e7b8a | 151 | |
maclobdell | 0:f7c60d3e7b8a | 152 | void mbedtls_sha512_clone( mbedtls_sha512_context *dst, |
maclobdell | 0:f7c60d3e7b8a | 153 | const mbedtls_sha512_context *src ) |
maclobdell | 0:f7c60d3e7b8a | 154 | { |
maclobdell | 0:f7c60d3e7b8a | 155 | *dst = *src; |
maclobdell | 0:f7c60d3e7b8a | 156 | } |
maclobdell | 0:f7c60d3e7b8a | 157 | |
maclobdell | 0:f7c60d3e7b8a | 158 | /* |
maclobdell | 0:f7c60d3e7b8a | 159 | * SHA-512 context setup |
maclobdell | 0:f7c60d3e7b8a | 160 | */ |
maclobdell | 0:f7c60d3e7b8a | 161 | void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 ) |
maclobdell | 0:f7c60d3e7b8a | 162 | { |
maclobdell | 0:f7c60d3e7b8a | 163 | ctx->total[0] = 0; |
maclobdell | 0:f7c60d3e7b8a | 164 | ctx->total[1] = 0; |
maclobdell | 0:f7c60d3e7b8a | 165 | |
maclobdell | 0:f7c60d3e7b8a | 166 | if( is384 == 0 ) |
maclobdell | 0:f7c60d3e7b8a | 167 | { |
maclobdell | 0:f7c60d3e7b8a | 168 | /* SHA-512 */ |
maclobdell | 0:f7c60d3e7b8a | 169 | ctx->state[0] = UL64(0x6A09E667F3BCC908); |
maclobdell | 0:f7c60d3e7b8a | 170 | ctx->state[1] = UL64(0xBB67AE8584CAA73B); |
maclobdell | 0:f7c60d3e7b8a | 171 | ctx->state[2] = UL64(0x3C6EF372FE94F82B); |
maclobdell | 0:f7c60d3e7b8a | 172 | ctx->state[3] = UL64(0xA54FF53A5F1D36F1); |
maclobdell | 0:f7c60d3e7b8a | 173 | ctx->state[4] = UL64(0x510E527FADE682D1); |
maclobdell | 0:f7c60d3e7b8a | 174 | ctx->state[5] = UL64(0x9B05688C2B3E6C1F); |
maclobdell | 0:f7c60d3e7b8a | 175 | ctx->state[6] = UL64(0x1F83D9ABFB41BD6B); |
maclobdell | 0:f7c60d3e7b8a | 176 | ctx->state[7] = UL64(0x5BE0CD19137E2179); |
maclobdell | 0:f7c60d3e7b8a | 177 | } |
maclobdell | 0:f7c60d3e7b8a | 178 | else |
maclobdell | 0:f7c60d3e7b8a | 179 | { |
maclobdell | 0:f7c60d3e7b8a | 180 | /* SHA-384 */ |
maclobdell | 0:f7c60d3e7b8a | 181 | ctx->state[0] = UL64(0xCBBB9D5DC1059ED8); |
maclobdell | 0:f7c60d3e7b8a | 182 | ctx->state[1] = UL64(0x629A292A367CD507); |
maclobdell | 0:f7c60d3e7b8a | 183 | ctx->state[2] = UL64(0x9159015A3070DD17); |
maclobdell | 0:f7c60d3e7b8a | 184 | ctx->state[3] = UL64(0x152FECD8F70E5939); |
maclobdell | 0:f7c60d3e7b8a | 185 | ctx->state[4] = UL64(0x67332667FFC00B31); |
maclobdell | 0:f7c60d3e7b8a | 186 | ctx->state[5] = UL64(0x8EB44A8768581511); |
maclobdell | 0:f7c60d3e7b8a | 187 | ctx->state[6] = UL64(0xDB0C2E0D64F98FA7); |
maclobdell | 0:f7c60d3e7b8a | 188 | ctx->state[7] = UL64(0x47B5481DBEFA4FA4); |
maclobdell | 0:f7c60d3e7b8a | 189 | } |
maclobdell | 0:f7c60d3e7b8a | 190 | |
maclobdell | 0:f7c60d3e7b8a | 191 | ctx->is384 = is384; |
maclobdell | 0:f7c60d3e7b8a | 192 | } |
maclobdell | 0:f7c60d3e7b8a | 193 | |
maclobdell | 0:f7c60d3e7b8a | 194 | #if !defined(MBEDTLS_SHA512_PROCESS_ALT) |
maclobdell | 0:f7c60d3e7b8a | 195 | void mbedtls_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] ) |
maclobdell | 0:f7c60d3e7b8a | 196 | { |
maclobdell | 0:f7c60d3e7b8a | 197 | int i; |
maclobdell | 0:f7c60d3e7b8a | 198 | uint64_t temp1, temp2, W[80]; |
maclobdell | 0:f7c60d3e7b8a | 199 | uint64_t A, B, C, D, E, F, G, H; |
maclobdell | 0:f7c60d3e7b8a | 200 | |
maclobdell | 0:f7c60d3e7b8a | 201 | #define SHR(x,n) (x >> n) |
maclobdell | 0:f7c60d3e7b8a | 202 | #define ROTR(x,n) (SHR(x,n) | (x << (64 - n))) |
maclobdell | 0:f7c60d3e7b8a | 203 | |
maclobdell | 0:f7c60d3e7b8a | 204 | #define S0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7)) |
maclobdell | 0:f7c60d3e7b8a | 205 | #define S1(x) (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6)) |
maclobdell | 0:f7c60d3e7b8a | 206 | |
maclobdell | 0:f7c60d3e7b8a | 207 | #define S2(x) (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39)) |
maclobdell | 0:f7c60d3e7b8a | 208 | #define S3(x) (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41)) |
maclobdell | 0:f7c60d3e7b8a | 209 | |
maclobdell | 0:f7c60d3e7b8a | 210 | #define F0(x,y,z) ((x & y) | (z & (x | y))) |
maclobdell | 0:f7c60d3e7b8a | 211 | #define F1(x,y,z) (z ^ (x & (y ^ z))) |
maclobdell | 0:f7c60d3e7b8a | 212 | |
maclobdell | 0:f7c60d3e7b8a | 213 | #define P(a,b,c,d,e,f,g,h,x,K) \ |
maclobdell | 0:f7c60d3e7b8a | 214 | { \ |
maclobdell | 0:f7c60d3e7b8a | 215 | temp1 = h + S3(e) + F1(e,f,g) + K + x; \ |
maclobdell | 0:f7c60d3e7b8a | 216 | temp2 = S2(a) + F0(a,b,c); \ |
maclobdell | 0:f7c60d3e7b8a | 217 | d += temp1; h = temp1 + temp2; \ |
maclobdell | 0:f7c60d3e7b8a | 218 | } |
maclobdell | 0:f7c60d3e7b8a | 219 | |
maclobdell | 0:f7c60d3e7b8a | 220 | for( i = 0; i < 16; i++ ) |
maclobdell | 0:f7c60d3e7b8a | 221 | { |
maclobdell | 0:f7c60d3e7b8a | 222 | GET_UINT64_BE( W[i], data, i << 3 ); |
maclobdell | 0:f7c60d3e7b8a | 223 | } |
maclobdell | 0:f7c60d3e7b8a | 224 | |
maclobdell | 0:f7c60d3e7b8a | 225 | for( ; i < 80; i++ ) |
maclobdell | 0:f7c60d3e7b8a | 226 | { |
maclobdell | 0:f7c60d3e7b8a | 227 | W[i] = S1(W[i - 2]) + W[i - 7] + |
maclobdell | 0:f7c60d3e7b8a | 228 | S0(W[i - 15]) + W[i - 16]; |
maclobdell | 0:f7c60d3e7b8a | 229 | } |
maclobdell | 0:f7c60d3e7b8a | 230 | |
maclobdell | 0:f7c60d3e7b8a | 231 | A = ctx->state[0]; |
maclobdell | 0:f7c60d3e7b8a | 232 | B = ctx->state[1]; |
maclobdell | 0:f7c60d3e7b8a | 233 | C = ctx->state[2]; |
maclobdell | 0:f7c60d3e7b8a | 234 | D = ctx->state[3]; |
maclobdell | 0:f7c60d3e7b8a | 235 | E = ctx->state[4]; |
maclobdell | 0:f7c60d3e7b8a | 236 | F = ctx->state[5]; |
maclobdell | 0:f7c60d3e7b8a | 237 | G = ctx->state[6]; |
maclobdell | 0:f7c60d3e7b8a | 238 | H = ctx->state[7]; |
maclobdell | 0:f7c60d3e7b8a | 239 | i = 0; |
maclobdell | 0:f7c60d3e7b8a | 240 | |
maclobdell | 0:f7c60d3e7b8a | 241 | do |
maclobdell | 0:f7c60d3e7b8a | 242 | { |
maclobdell | 0:f7c60d3e7b8a | 243 | P( A, B, C, D, E, F, G, H, W[i], K[i] ); i++; |
maclobdell | 0:f7c60d3e7b8a | 244 | P( H, A, B, C, D, E, F, G, W[i], K[i] ); i++; |
maclobdell | 0:f7c60d3e7b8a | 245 | P( G, H, A, B, C, D, E, F, W[i], K[i] ); i++; |
maclobdell | 0:f7c60d3e7b8a | 246 | P( F, G, H, A, B, C, D, E, W[i], K[i] ); i++; |
maclobdell | 0:f7c60d3e7b8a | 247 | P( E, F, G, H, A, B, C, D, W[i], K[i] ); i++; |
maclobdell | 0:f7c60d3e7b8a | 248 | P( D, E, F, G, H, A, B, C, W[i], K[i] ); i++; |
maclobdell | 0:f7c60d3e7b8a | 249 | P( C, D, E, F, G, H, A, B, W[i], K[i] ); i++; |
maclobdell | 0:f7c60d3e7b8a | 250 | P( B, C, D, E, F, G, H, A, W[i], K[i] ); i++; |
maclobdell | 0:f7c60d3e7b8a | 251 | } |
maclobdell | 0:f7c60d3e7b8a | 252 | while( i < 80 ); |
maclobdell | 0:f7c60d3e7b8a | 253 | |
maclobdell | 0:f7c60d3e7b8a | 254 | ctx->state[0] += A; |
maclobdell | 0:f7c60d3e7b8a | 255 | ctx->state[1] += B; |
maclobdell | 0:f7c60d3e7b8a | 256 | ctx->state[2] += C; |
maclobdell | 0:f7c60d3e7b8a | 257 | ctx->state[3] += D; |
maclobdell | 0:f7c60d3e7b8a | 258 | ctx->state[4] += E; |
maclobdell | 0:f7c60d3e7b8a | 259 | ctx->state[5] += F; |
maclobdell | 0:f7c60d3e7b8a | 260 | ctx->state[6] += G; |
maclobdell | 0:f7c60d3e7b8a | 261 | ctx->state[7] += H; |
maclobdell | 0:f7c60d3e7b8a | 262 | } |
maclobdell | 0:f7c60d3e7b8a | 263 | #endif /* !MBEDTLS_SHA512_PROCESS_ALT */ |
maclobdell | 0:f7c60d3e7b8a | 264 | |
maclobdell | 0:f7c60d3e7b8a | 265 | /* |
maclobdell | 0:f7c60d3e7b8a | 266 | * SHA-512 process buffer |
maclobdell | 0:f7c60d3e7b8a | 267 | */ |
maclobdell | 0:f7c60d3e7b8a | 268 | void mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input, |
maclobdell | 0:f7c60d3e7b8a | 269 | size_t ilen ) |
maclobdell | 0:f7c60d3e7b8a | 270 | { |
maclobdell | 0:f7c60d3e7b8a | 271 | size_t fill; |
maclobdell | 0:f7c60d3e7b8a | 272 | unsigned int left; |
maclobdell | 0:f7c60d3e7b8a | 273 | |
maclobdell | 0:f7c60d3e7b8a | 274 | if( ilen == 0 ) |
maclobdell | 0:f7c60d3e7b8a | 275 | return; |
maclobdell | 0:f7c60d3e7b8a | 276 | |
maclobdell | 0:f7c60d3e7b8a | 277 | left = (unsigned int) (ctx->total[0] & 0x7F); |
maclobdell | 0:f7c60d3e7b8a | 278 | fill = 128 - left; |
maclobdell | 0:f7c60d3e7b8a | 279 | |
maclobdell | 0:f7c60d3e7b8a | 280 | ctx->total[0] += (uint64_t) ilen; |
maclobdell | 0:f7c60d3e7b8a | 281 | |
maclobdell | 0:f7c60d3e7b8a | 282 | if( ctx->total[0] < (uint64_t) ilen ) |
maclobdell | 0:f7c60d3e7b8a | 283 | ctx->total[1]++; |
maclobdell | 0:f7c60d3e7b8a | 284 | |
maclobdell | 0:f7c60d3e7b8a | 285 | if( left && ilen >= fill ) |
maclobdell | 0:f7c60d3e7b8a | 286 | { |
maclobdell | 0:f7c60d3e7b8a | 287 | memcpy( (void *) (ctx->buffer + left), input, fill ); |
maclobdell | 0:f7c60d3e7b8a | 288 | mbedtls_sha512_process( ctx, ctx->buffer ); |
maclobdell | 0:f7c60d3e7b8a | 289 | input += fill; |
maclobdell | 0:f7c60d3e7b8a | 290 | ilen -= fill; |
maclobdell | 0:f7c60d3e7b8a | 291 | left = 0; |
maclobdell | 0:f7c60d3e7b8a | 292 | } |
maclobdell | 0:f7c60d3e7b8a | 293 | |
maclobdell | 0:f7c60d3e7b8a | 294 | while( ilen >= 128 ) |
maclobdell | 0:f7c60d3e7b8a | 295 | { |
maclobdell | 0:f7c60d3e7b8a | 296 | mbedtls_sha512_process( ctx, input ); |
maclobdell | 0:f7c60d3e7b8a | 297 | input += 128; |
maclobdell | 0:f7c60d3e7b8a | 298 | ilen -= 128; |
maclobdell | 0:f7c60d3e7b8a | 299 | } |
maclobdell | 0:f7c60d3e7b8a | 300 | |
maclobdell | 0:f7c60d3e7b8a | 301 | if( ilen > 0 ) |
maclobdell | 0:f7c60d3e7b8a | 302 | memcpy( (void *) (ctx->buffer + left), input, ilen ); |
maclobdell | 0:f7c60d3e7b8a | 303 | } |
maclobdell | 0:f7c60d3e7b8a | 304 | |
maclobdell | 0:f7c60d3e7b8a | 305 | static const unsigned char sha512_padding[128] = |
maclobdell | 0:f7c60d3e7b8a | 306 | { |
maclobdell | 0:f7c60d3e7b8a | 307 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
maclobdell | 0:f7c60d3e7b8a | 308 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
maclobdell | 0:f7c60d3e7b8a | 309 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
maclobdell | 0:f7c60d3e7b8a | 310 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
maclobdell | 0:f7c60d3e7b8a | 311 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
maclobdell | 0:f7c60d3e7b8a | 312 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
maclobdell | 0:f7c60d3e7b8a | 313 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
maclobdell | 0:f7c60d3e7b8a | 314 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
maclobdell | 0:f7c60d3e7b8a | 315 | }; |
maclobdell | 0:f7c60d3e7b8a | 316 | |
maclobdell | 0:f7c60d3e7b8a | 317 | /* |
maclobdell | 0:f7c60d3e7b8a | 318 | * SHA-512 final digest |
maclobdell | 0:f7c60d3e7b8a | 319 | */ |
maclobdell | 0:f7c60d3e7b8a | 320 | void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64] ) |
maclobdell | 0:f7c60d3e7b8a | 321 | { |
maclobdell | 0:f7c60d3e7b8a | 322 | size_t last, padn; |
maclobdell | 0:f7c60d3e7b8a | 323 | uint64_t high, low; |
maclobdell | 0:f7c60d3e7b8a | 324 | unsigned char msglen[16]; |
maclobdell | 0:f7c60d3e7b8a | 325 | |
maclobdell | 0:f7c60d3e7b8a | 326 | high = ( ctx->total[0] >> 61 ) |
maclobdell | 0:f7c60d3e7b8a | 327 | | ( ctx->total[1] << 3 ); |
maclobdell | 0:f7c60d3e7b8a | 328 | low = ( ctx->total[0] << 3 ); |
maclobdell | 0:f7c60d3e7b8a | 329 | |
maclobdell | 0:f7c60d3e7b8a | 330 | PUT_UINT64_BE( high, msglen, 0 ); |
maclobdell | 0:f7c60d3e7b8a | 331 | PUT_UINT64_BE( low, msglen, 8 ); |
maclobdell | 0:f7c60d3e7b8a | 332 | |
maclobdell | 0:f7c60d3e7b8a | 333 | last = (size_t)( ctx->total[0] & 0x7F ); |
maclobdell | 0:f7c60d3e7b8a | 334 | padn = ( last < 112 ) ? ( 112 - last ) : ( 240 - last ); |
maclobdell | 0:f7c60d3e7b8a | 335 | |
maclobdell | 0:f7c60d3e7b8a | 336 | mbedtls_sha512_update( ctx, sha512_padding, padn ); |
maclobdell | 0:f7c60d3e7b8a | 337 | mbedtls_sha512_update( ctx, msglen, 16 ); |
maclobdell | 0:f7c60d3e7b8a | 338 | |
maclobdell | 0:f7c60d3e7b8a | 339 | PUT_UINT64_BE( ctx->state[0], output, 0 ); |
maclobdell | 0:f7c60d3e7b8a | 340 | PUT_UINT64_BE( ctx->state[1], output, 8 ); |
maclobdell | 0:f7c60d3e7b8a | 341 | PUT_UINT64_BE( ctx->state[2], output, 16 ); |
maclobdell | 0:f7c60d3e7b8a | 342 | PUT_UINT64_BE( ctx->state[3], output, 24 ); |
maclobdell | 0:f7c60d3e7b8a | 343 | PUT_UINT64_BE( ctx->state[4], output, 32 ); |
maclobdell | 0:f7c60d3e7b8a | 344 | PUT_UINT64_BE( ctx->state[5], output, 40 ); |
maclobdell | 0:f7c60d3e7b8a | 345 | |
maclobdell | 0:f7c60d3e7b8a | 346 | if( ctx->is384 == 0 ) |
maclobdell | 0:f7c60d3e7b8a | 347 | { |
maclobdell | 0:f7c60d3e7b8a | 348 | PUT_UINT64_BE( ctx->state[6], output, 48 ); |
maclobdell | 0:f7c60d3e7b8a | 349 | PUT_UINT64_BE( ctx->state[7], output, 56 ); |
maclobdell | 0:f7c60d3e7b8a | 350 | } |
maclobdell | 0:f7c60d3e7b8a | 351 | } |
maclobdell | 0:f7c60d3e7b8a | 352 | |
maclobdell | 0:f7c60d3e7b8a | 353 | #endif /* !MBEDTLS_SHA512_ALT */ |
maclobdell | 0:f7c60d3e7b8a | 354 | |
maclobdell | 0:f7c60d3e7b8a | 355 | /* |
maclobdell | 0:f7c60d3e7b8a | 356 | * output = SHA-512( input buffer ) |
maclobdell | 0:f7c60d3e7b8a | 357 | */ |
maclobdell | 0:f7c60d3e7b8a | 358 | void mbedtls_sha512( const unsigned char *input, size_t ilen, |
maclobdell | 0:f7c60d3e7b8a | 359 | unsigned char output[64], int is384 ) |
maclobdell | 0:f7c60d3e7b8a | 360 | { |
maclobdell | 0:f7c60d3e7b8a | 361 | mbedtls_sha512_context ctx; |
maclobdell | 0:f7c60d3e7b8a | 362 | |
maclobdell | 0:f7c60d3e7b8a | 363 | mbedtls_sha512_init( &ctx ); |
maclobdell | 0:f7c60d3e7b8a | 364 | mbedtls_sha512_starts( &ctx, is384 ); |
maclobdell | 0:f7c60d3e7b8a | 365 | mbedtls_sha512_update( &ctx, input, ilen ); |
maclobdell | 0:f7c60d3e7b8a | 366 | mbedtls_sha512_finish( &ctx, output ); |
maclobdell | 0:f7c60d3e7b8a | 367 | mbedtls_sha512_free( &ctx ); |
maclobdell | 0:f7c60d3e7b8a | 368 | } |
maclobdell | 0:f7c60d3e7b8a | 369 | |
maclobdell | 0:f7c60d3e7b8a | 370 | #if defined(MBEDTLS_SELF_TEST) |
maclobdell | 0:f7c60d3e7b8a | 371 | |
maclobdell | 0:f7c60d3e7b8a | 372 | /* |
maclobdell | 0:f7c60d3e7b8a | 373 | * FIPS-180-2 test vectors |
maclobdell | 0:f7c60d3e7b8a | 374 | */ |
maclobdell | 0:f7c60d3e7b8a | 375 | static const unsigned char sha512_test_buf[3][113] = |
maclobdell | 0:f7c60d3e7b8a | 376 | { |
maclobdell | 0:f7c60d3e7b8a | 377 | { "abc" }, |
maclobdell | 0:f7c60d3e7b8a | 378 | { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" |
maclobdell | 0:f7c60d3e7b8a | 379 | "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" }, |
maclobdell | 0:f7c60d3e7b8a | 380 | { "" } |
maclobdell | 0:f7c60d3e7b8a | 381 | }; |
maclobdell | 0:f7c60d3e7b8a | 382 | |
maclobdell | 0:f7c60d3e7b8a | 383 | static const int sha512_test_buflen[3] = |
maclobdell | 0:f7c60d3e7b8a | 384 | { |
maclobdell | 0:f7c60d3e7b8a | 385 | 3, 112, 1000 |
maclobdell | 0:f7c60d3e7b8a | 386 | }; |
maclobdell | 0:f7c60d3e7b8a | 387 | |
maclobdell | 0:f7c60d3e7b8a | 388 | static const unsigned char sha512_test_sum[6][64] = |
maclobdell | 0:f7c60d3e7b8a | 389 | { |
maclobdell | 0:f7c60d3e7b8a | 390 | /* |
maclobdell | 0:f7c60d3e7b8a | 391 | * SHA-384 test vectors |
maclobdell | 0:f7c60d3e7b8a | 392 | */ |
maclobdell | 0:f7c60d3e7b8a | 393 | { 0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B, |
maclobdell | 0:f7c60d3e7b8a | 394 | 0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07, |
maclobdell | 0:f7c60d3e7b8a | 395 | 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63, |
maclobdell | 0:f7c60d3e7b8a | 396 | 0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED, |
maclobdell | 0:f7c60d3e7b8a | 397 | 0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23, |
maclobdell | 0:f7c60d3e7b8a | 398 | 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7 }, |
maclobdell | 0:f7c60d3e7b8a | 399 | { 0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8, |
maclobdell | 0:f7c60d3e7b8a | 400 | 0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47, |
maclobdell | 0:f7c60d3e7b8a | 401 | 0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2, |
maclobdell | 0:f7c60d3e7b8a | 402 | 0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12, |
maclobdell | 0:f7c60d3e7b8a | 403 | 0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9, |
maclobdell | 0:f7c60d3e7b8a | 404 | 0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39 }, |
maclobdell | 0:f7c60d3e7b8a | 405 | { 0x9D, 0x0E, 0x18, 0x09, 0x71, 0x64, 0x74, 0xCB, |
maclobdell | 0:f7c60d3e7b8a | 406 | 0x08, 0x6E, 0x83, 0x4E, 0x31, 0x0A, 0x4A, 0x1C, |
maclobdell | 0:f7c60d3e7b8a | 407 | 0xED, 0x14, 0x9E, 0x9C, 0x00, 0xF2, 0x48, 0x52, |
maclobdell | 0:f7c60d3e7b8a | 408 | 0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B, |
maclobdell | 0:f7c60d3e7b8a | 409 | 0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB, |
maclobdell | 0:f7c60d3e7b8a | 410 | 0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85 }, |
maclobdell | 0:f7c60d3e7b8a | 411 | |
maclobdell | 0:f7c60d3e7b8a | 412 | /* |
maclobdell | 0:f7c60d3e7b8a | 413 | * SHA-512 test vectors |
maclobdell | 0:f7c60d3e7b8a | 414 | */ |
maclobdell | 0:f7c60d3e7b8a | 415 | { 0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA, |
maclobdell | 0:f7c60d3e7b8a | 416 | 0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31, |
maclobdell | 0:f7c60d3e7b8a | 417 | 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2, |
maclobdell | 0:f7c60d3e7b8a | 418 | 0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A, |
maclobdell | 0:f7c60d3e7b8a | 419 | 0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8, |
maclobdell | 0:f7c60d3e7b8a | 420 | 0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD, |
maclobdell | 0:f7c60d3e7b8a | 421 | 0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E, |
maclobdell | 0:f7c60d3e7b8a | 422 | 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F }, |
maclobdell | 0:f7c60d3e7b8a | 423 | { 0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA, |
maclobdell | 0:f7c60d3e7b8a | 424 | 0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F, |
maclobdell | 0:f7c60d3e7b8a | 425 | 0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1, |
maclobdell | 0:f7c60d3e7b8a | 426 | 0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18, |
maclobdell | 0:f7c60d3e7b8a | 427 | 0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4, |
maclobdell | 0:f7c60d3e7b8a | 428 | 0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A, |
maclobdell | 0:f7c60d3e7b8a | 429 | 0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54, |
maclobdell | 0:f7c60d3e7b8a | 430 | 0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09 }, |
maclobdell | 0:f7c60d3e7b8a | 431 | { 0xE7, 0x18, 0x48, 0x3D, 0x0C, 0xE7, 0x69, 0x64, |
maclobdell | 0:f7c60d3e7b8a | 432 | 0x4E, 0x2E, 0x42, 0xC7, 0xBC, 0x15, 0xB4, 0x63, |
maclobdell | 0:f7c60d3e7b8a | 433 | 0x8E, 0x1F, 0x98, 0xB1, 0x3B, 0x20, 0x44, 0x28, |
maclobdell | 0:f7c60d3e7b8a | 434 | 0x56, 0x32, 0xA8, 0x03, 0xAF, 0xA9, 0x73, 0xEB, |
maclobdell | 0:f7c60d3e7b8a | 435 | 0xDE, 0x0F, 0xF2, 0x44, 0x87, 0x7E, 0xA6, 0x0A, |
maclobdell | 0:f7c60d3e7b8a | 436 | 0x4C, 0xB0, 0x43, 0x2C, 0xE5, 0x77, 0xC3, 0x1B, |
maclobdell | 0:f7c60d3e7b8a | 437 | 0xEB, 0x00, 0x9C, 0x5C, 0x2C, 0x49, 0xAA, 0x2E, |
maclobdell | 0:f7c60d3e7b8a | 438 | 0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B } |
maclobdell | 0:f7c60d3e7b8a | 439 | }; |
maclobdell | 0:f7c60d3e7b8a | 440 | |
maclobdell | 0:f7c60d3e7b8a | 441 | /* |
maclobdell | 0:f7c60d3e7b8a | 442 | * Checkup routine |
maclobdell | 0:f7c60d3e7b8a | 443 | */ |
maclobdell | 0:f7c60d3e7b8a | 444 | int mbedtls_sha512_self_test( int verbose ) |
maclobdell | 0:f7c60d3e7b8a | 445 | { |
maclobdell | 0:f7c60d3e7b8a | 446 | int i, j, k, buflen, ret = 0; |
maclobdell | 0:f7c60d3e7b8a | 447 | unsigned char buf[1024]; |
maclobdell | 0:f7c60d3e7b8a | 448 | unsigned char sha512sum[64]; |
maclobdell | 0:f7c60d3e7b8a | 449 | mbedtls_sha512_context ctx; |
maclobdell | 0:f7c60d3e7b8a | 450 | |
maclobdell | 0:f7c60d3e7b8a | 451 | mbedtls_sha512_init( &ctx ); |
maclobdell | 0:f7c60d3e7b8a | 452 | |
maclobdell | 0:f7c60d3e7b8a | 453 | for( i = 0; i < 6; i++ ) |
maclobdell | 0:f7c60d3e7b8a | 454 | { |
maclobdell | 0:f7c60d3e7b8a | 455 | j = i % 3; |
maclobdell | 0:f7c60d3e7b8a | 456 | k = i < 3; |
maclobdell | 0:f7c60d3e7b8a | 457 | |
maclobdell | 0:f7c60d3e7b8a | 458 | if( verbose != 0 ) |
maclobdell | 0:f7c60d3e7b8a | 459 | mbedtls_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 ); |
maclobdell | 0:f7c60d3e7b8a | 460 | |
maclobdell | 0:f7c60d3e7b8a | 461 | mbedtls_sha512_starts( &ctx, k ); |
maclobdell | 0:f7c60d3e7b8a | 462 | |
maclobdell | 0:f7c60d3e7b8a | 463 | if( j == 2 ) |
maclobdell | 0:f7c60d3e7b8a | 464 | { |
maclobdell | 0:f7c60d3e7b8a | 465 | memset( buf, 'a', buflen = 1000 ); |
maclobdell | 0:f7c60d3e7b8a | 466 | |
maclobdell | 0:f7c60d3e7b8a | 467 | for( j = 0; j < 1000; j++ ) |
maclobdell | 0:f7c60d3e7b8a | 468 | mbedtls_sha512_update( &ctx, buf, buflen ); |
maclobdell | 0:f7c60d3e7b8a | 469 | } |
maclobdell | 0:f7c60d3e7b8a | 470 | else |
maclobdell | 0:f7c60d3e7b8a | 471 | mbedtls_sha512_update( &ctx, sha512_test_buf[j], |
maclobdell | 0:f7c60d3e7b8a | 472 | sha512_test_buflen[j] ); |
maclobdell | 0:f7c60d3e7b8a | 473 | |
maclobdell | 0:f7c60d3e7b8a | 474 | mbedtls_sha512_finish( &ctx, sha512sum ); |
maclobdell | 0:f7c60d3e7b8a | 475 | |
maclobdell | 0:f7c60d3e7b8a | 476 | if( memcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 ) |
maclobdell | 0:f7c60d3e7b8a | 477 | { |
maclobdell | 0:f7c60d3e7b8a | 478 | if( verbose != 0 ) |
maclobdell | 0:f7c60d3e7b8a | 479 | mbedtls_printf( "failed\n" ); |
maclobdell | 0:f7c60d3e7b8a | 480 | |
maclobdell | 0:f7c60d3e7b8a | 481 | ret = 1; |
maclobdell | 0:f7c60d3e7b8a | 482 | goto exit; |
maclobdell | 0:f7c60d3e7b8a | 483 | } |
maclobdell | 0:f7c60d3e7b8a | 484 | |
maclobdell | 0:f7c60d3e7b8a | 485 | if( verbose != 0 ) |
maclobdell | 0:f7c60d3e7b8a | 486 | mbedtls_printf( "passed\n" ); |
maclobdell | 0:f7c60d3e7b8a | 487 | } |
maclobdell | 0:f7c60d3e7b8a | 488 | |
maclobdell | 0:f7c60d3e7b8a | 489 | if( verbose != 0 ) |
maclobdell | 0:f7c60d3e7b8a | 490 | mbedtls_printf( "\n" ); |
maclobdell | 0:f7c60d3e7b8a | 491 | |
maclobdell | 0:f7c60d3e7b8a | 492 | exit: |
maclobdell | 0:f7c60d3e7b8a | 493 | mbedtls_sha512_free( &ctx ); |
maclobdell | 0:f7c60d3e7b8a | 494 | |
maclobdell | 0:f7c60d3e7b8a | 495 | return( ret ); |
maclobdell | 0:f7c60d3e7b8a | 496 | } |
maclobdell | 0:f7c60d3e7b8a | 497 | |
maclobdell | 0:f7c60d3e7b8a | 498 | #endif /* MBEDTLS_SELF_TEST */ |
maclobdell | 0:f7c60d3e7b8a | 499 | |
maclobdell | 0:f7c60d3e7b8a | 500 | #endif /* MBEDTLS_SHA512_C */ |