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