Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Committer:
HannesTschofenig
Date:
Thu Sep 27 06:34:22 2018 +0000
Revision:
0:796d0f61a05b
Example AES-GCM test program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HannesTschofenig 0:796d0f61a05b 1 /*
HannesTschofenig 0:796d0f61a05b 2 * FIPS-180-2 compliant SHA-384/512 implementation
HannesTschofenig 0:796d0f61a05b 3 *
HannesTschofenig 0:796d0f61a05b 4 * Copyright (C) 2006-2014, Brainspark B.V.
HannesTschofenig 0:796d0f61a05b 5 *
HannesTschofenig 0:796d0f61a05b 6 * This file is part of PolarSSL (http://www.polarssl.org)
HannesTschofenig 0:796d0f61a05b 7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
HannesTschofenig 0:796d0f61a05b 8 *
HannesTschofenig 0:796d0f61a05b 9 * All rights reserved.
HannesTschofenig 0:796d0f61a05b 10 *
HannesTschofenig 0:796d0f61a05b 11 * This program is free software; you can redistribute it and/or modify
HannesTschofenig 0:796d0f61a05b 12 * it under the terms of the GNU General Public License as published by
HannesTschofenig 0:796d0f61a05b 13 * the Free Software Foundation; either version 2 of the License, or
HannesTschofenig 0:796d0f61a05b 14 * (at your option) any later version.
HannesTschofenig 0:796d0f61a05b 15 *
HannesTschofenig 0:796d0f61a05b 16 * This program is distributed in the hope that it will be useful,
HannesTschofenig 0:796d0f61a05b 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
HannesTschofenig 0:796d0f61a05b 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
HannesTschofenig 0:796d0f61a05b 19 * GNU General Public License for more details.
HannesTschofenig 0:796d0f61a05b 20 *
HannesTschofenig 0:796d0f61a05b 21 * You should have received a copy of the GNU General Public License along
HannesTschofenig 0:796d0f61a05b 22 * with this program; if not, write to the Free Software Foundation, Inc.,
HannesTschofenig 0:796d0f61a05b 23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
HannesTschofenig 0:796d0f61a05b 24 */
HannesTschofenig 0:796d0f61a05b 25 /*
HannesTschofenig 0:796d0f61a05b 26 * The SHA-512 Secure Hash Standard was published by NIST in 2002.
HannesTschofenig 0:796d0f61a05b 27 *
HannesTschofenig 0:796d0f61a05b 28 * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
HannesTschofenig 0:796d0f61a05b 29 */
HannesTschofenig 0:796d0f61a05b 30
HannesTschofenig 0:796d0f61a05b 31 #if !defined(POLARSSL_CONFIG_FILE)
HannesTschofenig 0:796d0f61a05b 32 #include "polarssl/config.h"
HannesTschofenig 0:796d0f61a05b 33 #else
HannesTschofenig 0:796d0f61a05b 34 #include POLARSSL_CONFIG_FILE
HannesTschofenig 0:796d0f61a05b 35 #endif
HannesTschofenig 0:796d0f61a05b 36
HannesTschofenig 0:796d0f61a05b 37 #if defined(POLARSSL_SHA512_C)
HannesTschofenig 0:796d0f61a05b 38
HannesTschofenig 0:796d0f61a05b 39 #include "polarssl/sha512.h"
HannesTschofenig 0:796d0f61a05b 40
HannesTschofenig 0:796d0f61a05b 41 #if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST)
HannesTschofenig 0:796d0f61a05b 42 #include <stdio.h>
HannesTschofenig 0:796d0f61a05b 43 #endif
HannesTschofenig 0:796d0f61a05b 44
HannesTschofenig 0:796d0f61a05b 45 #if defined(POLARSSL_PLATFORM_C)
HannesTschofenig 0:796d0f61a05b 46 #include "polarssl/platform.h"
HannesTschofenig 0:796d0f61a05b 47 #else
HannesTschofenig 0:796d0f61a05b 48 #define polarssl_printf printf
HannesTschofenig 0:796d0f61a05b 49 #endif
HannesTschofenig 0:796d0f61a05b 50
HannesTschofenig 0:796d0f61a05b 51 #if !defined(POLARSSL_SHA512_ALT)
HannesTschofenig 0:796d0f61a05b 52
HannesTschofenig 0:796d0f61a05b 53 /*
HannesTschofenig 0:796d0f61a05b 54 * 64-bit integer manipulation macros (big endian)
HannesTschofenig 0:796d0f61a05b 55 */
HannesTschofenig 0:796d0f61a05b 56 #ifndef GET_UINT64_BE
HannesTschofenig 0:796d0f61a05b 57 #define GET_UINT64_BE(n,b,i) \
HannesTschofenig 0:796d0f61a05b 58 { \
HannesTschofenig 0:796d0f61a05b 59 (n) = ( (uint64_t) (b)[(i) ] << 56 ) \
HannesTschofenig 0:796d0f61a05b 60 | ( (uint64_t) (b)[(i) + 1] << 48 ) \
HannesTschofenig 0:796d0f61a05b 61 | ( (uint64_t) (b)[(i) + 2] << 40 ) \
HannesTschofenig 0:796d0f61a05b 62 | ( (uint64_t) (b)[(i) + 3] << 32 ) \
HannesTschofenig 0:796d0f61a05b 63 | ( (uint64_t) (b)[(i) + 4] << 24 ) \
HannesTschofenig 0:796d0f61a05b 64 | ( (uint64_t) (b)[(i) + 5] << 16 ) \
HannesTschofenig 0:796d0f61a05b 65 | ( (uint64_t) (b)[(i) + 6] << 8 ) \
HannesTschofenig 0:796d0f61a05b 66 | ( (uint64_t) (b)[(i) + 7] ); \
HannesTschofenig 0:796d0f61a05b 67 }
HannesTschofenig 0:796d0f61a05b 68 #endif /* GET_UINT64_BE */
HannesTschofenig 0:796d0f61a05b 69
HannesTschofenig 0:796d0f61a05b 70 #ifndef PUT_UINT64_BE
HannesTschofenig 0:796d0f61a05b 71 #define PUT_UINT64_BE(n,b,i) \
HannesTschofenig 0:796d0f61a05b 72 { \
HannesTschofenig 0:796d0f61a05b 73 (b)[(i) ] = (unsigned char) ( (n) >> 56 ); \
HannesTschofenig 0:796d0f61a05b 74 (b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \
HannesTschofenig 0:796d0f61a05b 75 (b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \
HannesTschofenig 0:796d0f61a05b 76 (b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \
HannesTschofenig 0:796d0f61a05b 77 (b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \
HannesTschofenig 0:796d0f61a05b 78 (b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \
HannesTschofenig 0:796d0f61a05b 79 (b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \
HannesTschofenig 0:796d0f61a05b 80 (b)[(i) + 7] = (unsigned char) ( (n) ); \
HannesTschofenig 0:796d0f61a05b 81 }
HannesTschofenig 0:796d0f61a05b 82 #endif /* PUT_UINT64_BE */
HannesTschofenig 0:796d0f61a05b 83
HannesTschofenig 0:796d0f61a05b 84 /*
HannesTschofenig 0:796d0f61a05b 85 * Round constants
HannesTschofenig 0:796d0f61a05b 86 */
HannesTschofenig 0:796d0f61a05b 87 static const uint64_t K[80] =
HannesTschofenig 0:796d0f61a05b 88 {
HannesTschofenig 0:796d0f61a05b 89 UL64(0x428A2F98D728AE22), UL64(0x7137449123EF65CD),
HannesTschofenig 0:796d0f61a05b 90 UL64(0xB5C0FBCFEC4D3B2F), UL64(0xE9B5DBA58189DBBC),
HannesTschofenig 0:796d0f61a05b 91 UL64(0x3956C25BF348B538), UL64(0x59F111F1B605D019),
HannesTschofenig 0:796d0f61a05b 92 UL64(0x923F82A4AF194F9B), UL64(0xAB1C5ED5DA6D8118),
HannesTschofenig 0:796d0f61a05b 93 UL64(0xD807AA98A3030242), UL64(0x12835B0145706FBE),
HannesTschofenig 0:796d0f61a05b 94 UL64(0x243185BE4EE4B28C), UL64(0x550C7DC3D5FFB4E2),
HannesTschofenig 0:796d0f61a05b 95 UL64(0x72BE5D74F27B896F), UL64(0x80DEB1FE3B1696B1),
HannesTschofenig 0:796d0f61a05b 96 UL64(0x9BDC06A725C71235), UL64(0xC19BF174CF692694),
HannesTschofenig 0:796d0f61a05b 97 UL64(0xE49B69C19EF14AD2), UL64(0xEFBE4786384F25E3),
HannesTschofenig 0:796d0f61a05b 98 UL64(0x0FC19DC68B8CD5B5), UL64(0x240CA1CC77AC9C65),
HannesTschofenig 0:796d0f61a05b 99 UL64(0x2DE92C6F592B0275), UL64(0x4A7484AA6EA6E483),
HannesTschofenig 0:796d0f61a05b 100 UL64(0x5CB0A9DCBD41FBD4), UL64(0x76F988DA831153B5),
HannesTschofenig 0:796d0f61a05b 101 UL64(0x983E5152EE66DFAB), UL64(0xA831C66D2DB43210),
HannesTschofenig 0:796d0f61a05b 102 UL64(0xB00327C898FB213F), UL64(0xBF597FC7BEEF0EE4),
HannesTschofenig 0:796d0f61a05b 103 UL64(0xC6E00BF33DA88FC2), UL64(0xD5A79147930AA725),
HannesTschofenig 0:796d0f61a05b 104 UL64(0x06CA6351E003826F), UL64(0x142929670A0E6E70),
HannesTschofenig 0:796d0f61a05b 105 UL64(0x27B70A8546D22FFC), UL64(0x2E1B21385C26C926),
HannesTschofenig 0:796d0f61a05b 106 UL64(0x4D2C6DFC5AC42AED), UL64(0x53380D139D95B3DF),
HannesTschofenig 0:796d0f61a05b 107 UL64(0x650A73548BAF63DE), UL64(0x766A0ABB3C77B2A8),
HannesTschofenig 0:796d0f61a05b 108 UL64(0x81C2C92E47EDAEE6), UL64(0x92722C851482353B),
HannesTschofenig 0:796d0f61a05b 109 UL64(0xA2BFE8A14CF10364), UL64(0xA81A664BBC423001),
HannesTschofenig 0:796d0f61a05b 110 UL64(0xC24B8B70D0F89791), UL64(0xC76C51A30654BE30),
HannesTschofenig 0:796d0f61a05b 111 UL64(0xD192E819D6EF5218), UL64(0xD69906245565A910),
HannesTschofenig 0:796d0f61a05b 112 UL64(0xF40E35855771202A), UL64(0x106AA07032BBD1B8),
HannesTschofenig 0:796d0f61a05b 113 UL64(0x19A4C116B8D2D0C8), UL64(0x1E376C085141AB53),
HannesTschofenig 0:796d0f61a05b 114 UL64(0x2748774CDF8EEB99), UL64(0x34B0BCB5E19B48A8),
HannesTschofenig 0:796d0f61a05b 115 UL64(0x391C0CB3C5C95A63), UL64(0x4ED8AA4AE3418ACB),
HannesTschofenig 0:796d0f61a05b 116 UL64(0x5B9CCA4F7763E373), UL64(0x682E6FF3D6B2B8A3),
HannesTschofenig 0:796d0f61a05b 117 UL64(0x748F82EE5DEFB2FC), UL64(0x78A5636F43172F60),
HannesTschofenig 0:796d0f61a05b 118 UL64(0x84C87814A1F0AB72), UL64(0x8CC702081A6439EC),
HannesTschofenig 0:796d0f61a05b 119 UL64(0x90BEFFFA23631E28), UL64(0xA4506CEBDE82BDE9),
HannesTschofenig 0:796d0f61a05b 120 UL64(0xBEF9A3F7B2C67915), UL64(0xC67178F2E372532B),
HannesTschofenig 0:796d0f61a05b 121 UL64(0xCA273ECEEA26619C), UL64(0xD186B8C721C0C207),
HannesTschofenig 0:796d0f61a05b 122 UL64(0xEADA7DD6CDE0EB1E), UL64(0xF57D4F7FEE6ED178),
HannesTschofenig 0:796d0f61a05b 123 UL64(0x06F067AA72176FBA), UL64(0x0A637DC5A2C898A6),
HannesTschofenig 0:796d0f61a05b 124 UL64(0x113F9804BEF90DAE), UL64(0x1B710B35131C471B),
HannesTschofenig 0:796d0f61a05b 125 UL64(0x28DB77F523047D84), UL64(0x32CAAB7B40C72493),
HannesTschofenig 0:796d0f61a05b 126 UL64(0x3C9EBE0A15C9BEBC), UL64(0x431D67C49C100D4C),
HannesTschofenig 0:796d0f61a05b 127 UL64(0x4CC5D4BECB3E42B6), UL64(0x597F299CFC657E2A),
HannesTschofenig 0:796d0f61a05b 128 UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817)
HannesTschofenig 0:796d0f61a05b 129 };
HannesTschofenig 0:796d0f61a05b 130
HannesTschofenig 0:796d0f61a05b 131 /*
HannesTschofenig 0:796d0f61a05b 132 * SHA-512 context setup
HannesTschofenig 0:796d0f61a05b 133 */
HannesTschofenig 0:796d0f61a05b 134 void sha512_starts( sha512_context *ctx, int is384 )
HannesTschofenig 0:796d0f61a05b 135 {
HannesTschofenig 0:796d0f61a05b 136 ctx->total[0] = 0;
HannesTschofenig 0:796d0f61a05b 137 ctx->total[1] = 0;
HannesTschofenig 0:796d0f61a05b 138
HannesTschofenig 0:796d0f61a05b 139 if( is384 == 0 )
HannesTschofenig 0:796d0f61a05b 140 {
HannesTschofenig 0:796d0f61a05b 141 /* SHA-512 */
HannesTschofenig 0:796d0f61a05b 142 ctx->state[0] = UL64(0x6A09E667F3BCC908);
HannesTschofenig 0:796d0f61a05b 143 ctx->state[1] = UL64(0xBB67AE8584CAA73B);
HannesTschofenig 0:796d0f61a05b 144 ctx->state[2] = UL64(0x3C6EF372FE94F82B);
HannesTschofenig 0:796d0f61a05b 145 ctx->state[3] = UL64(0xA54FF53A5F1D36F1);
HannesTschofenig 0:796d0f61a05b 146 ctx->state[4] = UL64(0x510E527FADE682D1);
HannesTschofenig 0:796d0f61a05b 147 ctx->state[5] = UL64(0x9B05688C2B3E6C1F);
HannesTschofenig 0:796d0f61a05b 148 ctx->state[6] = UL64(0x1F83D9ABFB41BD6B);
HannesTschofenig 0:796d0f61a05b 149 ctx->state[7] = UL64(0x5BE0CD19137E2179);
HannesTschofenig 0:796d0f61a05b 150 }
HannesTschofenig 0:796d0f61a05b 151 else
HannesTschofenig 0:796d0f61a05b 152 {
HannesTschofenig 0:796d0f61a05b 153 /* SHA-384 */
HannesTschofenig 0:796d0f61a05b 154 ctx->state[0] = UL64(0xCBBB9D5DC1059ED8);
HannesTschofenig 0:796d0f61a05b 155 ctx->state[1] = UL64(0x629A292A367CD507);
HannesTschofenig 0:796d0f61a05b 156 ctx->state[2] = UL64(0x9159015A3070DD17);
HannesTschofenig 0:796d0f61a05b 157 ctx->state[3] = UL64(0x152FECD8F70E5939);
HannesTschofenig 0:796d0f61a05b 158 ctx->state[4] = UL64(0x67332667FFC00B31);
HannesTschofenig 0:796d0f61a05b 159 ctx->state[5] = UL64(0x8EB44A8768581511);
HannesTschofenig 0:796d0f61a05b 160 ctx->state[6] = UL64(0xDB0C2E0D64F98FA7);
HannesTschofenig 0:796d0f61a05b 161 ctx->state[7] = UL64(0x47B5481DBEFA4FA4);
HannesTschofenig 0:796d0f61a05b 162 }
HannesTschofenig 0:796d0f61a05b 163
HannesTschofenig 0:796d0f61a05b 164 ctx->is384 = is384;
HannesTschofenig 0:796d0f61a05b 165 }
HannesTschofenig 0:796d0f61a05b 166
HannesTschofenig 0:796d0f61a05b 167 void sha512_process( sha512_context *ctx, const unsigned char data[128] )
HannesTschofenig 0:796d0f61a05b 168 {
HannesTschofenig 0:796d0f61a05b 169 int i;
HannesTschofenig 0:796d0f61a05b 170 uint64_t temp1, temp2, W[80];
HannesTschofenig 0:796d0f61a05b 171 uint64_t A, B, C, D, E, F, G, H;
HannesTschofenig 0:796d0f61a05b 172
HannesTschofenig 0:796d0f61a05b 173 #define SHR(x,n) (x >> n)
HannesTschofenig 0:796d0f61a05b 174 #define ROTR(x,n) (SHR(x,n) | (x << (64 - n)))
HannesTschofenig 0:796d0f61a05b 175
HannesTschofenig 0:796d0f61a05b 176 #define S0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
HannesTschofenig 0:796d0f61a05b 177 #define S1(x) (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6))
HannesTschofenig 0:796d0f61a05b 178
HannesTschofenig 0:796d0f61a05b 179 #define S2(x) (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39))
HannesTschofenig 0:796d0f61a05b 180 #define S3(x) (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41))
HannesTschofenig 0:796d0f61a05b 181
HannesTschofenig 0:796d0f61a05b 182 #define F0(x,y,z) ((x & y) | (z & (x | y)))
HannesTschofenig 0:796d0f61a05b 183 #define F1(x,y,z) (z ^ (x & (y ^ z)))
HannesTschofenig 0:796d0f61a05b 184
HannesTschofenig 0:796d0f61a05b 185 #define P(a,b,c,d,e,f,g,h,x,K) \
HannesTschofenig 0:796d0f61a05b 186 { \
HannesTschofenig 0:796d0f61a05b 187 temp1 = h + S3(e) + F1(e,f,g) + K + x; \
HannesTschofenig 0:796d0f61a05b 188 temp2 = S2(a) + F0(a,b,c); \
HannesTschofenig 0:796d0f61a05b 189 d += temp1; h = temp1 + temp2; \
HannesTschofenig 0:796d0f61a05b 190 }
HannesTschofenig 0:796d0f61a05b 191
HannesTschofenig 0:796d0f61a05b 192 for( i = 0; i < 16; i++ )
HannesTschofenig 0:796d0f61a05b 193 {
HannesTschofenig 0:796d0f61a05b 194 GET_UINT64_BE( W[i], data, i << 3 );
HannesTschofenig 0:796d0f61a05b 195 }
HannesTschofenig 0:796d0f61a05b 196
HannesTschofenig 0:796d0f61a05b 197 for( ; i < 80; i++ )
HannesTschofenig 0:796d0f61a05b 198 {
HannesTschofenig 0:796d0f61a05b 199 W[i] = S1(W[i - 2]) + W[i - 7] +
HannesTschofenig 0:796d0f61a05b 200 S0(W[i - 15]) + W[i - 16];
HannesTschofenig 0:796d0f61a05b 201 }
HannesTschofenig 0:796d0f61a05b 202
HannesTschofenig 0:796d0f61a05b 203 A = ctx->state[0];
HannesTschofenig 0:796d0f61a05b 204 B = ctx->state[1];
HannesTschofenig 0:796d0f61a05b 205 C = ctx->state[2];
HannesTschofenig 0:796d0f61a05b 206 D = ctx->state[3];
HannesTschofenig 0:796d0f61a05b 207 E = ctx->state[4];
HannesTschofenig 0:796d0f61a05b 208 F = ctx->state[5];
HannesTschofenig 0:796d0f61a05b 209 G = ctx->state[6];
HannesTschofenig 0:796d0f61a05b 210 H = ctx->state[7];
HannesTschofenig 0:796d0f61a05b 211 i = 0;
HannesTschofenig 0:796d0f61a05b 212
HannesTschofenig 0:796d0f61a05b 213 do
HannesTschofenig 0:796d0f61a05b 214 {
HannesTschofenig 0:796d0f61a05b 215 P( A, B, C, D, E, F, G, H, W[i], K[i] ); i++;
HannesTschofenig 0:796d0f61a05b 216 P( H, A, B, C, D, E, F, G, W[i], K[i] ); i++;
HannesTschofenig 0:796d0f61a05b 217 P( G, H, A, B, C, D, E, F, W[i], K[i] ); i++;
HannesTschofenig 0:796d0f61a05b 218 P( F, G, H, A, B, C, D, E, W[i], K[i] ); i++;
HannesTschofenig 0:796d0f61a05b 219 P( E, F, G, H, A, B, C, D, W[i], K[i] ); i++;
HannesTschofenig 0:796d0f61a05b 220 P( D, E, F, G, H, A, B, C, W[i], K[i] ); i++;
HannesTschofenig 0:796d0f61a05b 221 P( C, D, E, F, G, H, A, B, W[i], K[i] ); i++;
HannesTschofenig 0:796d0f61a05b 222 P( B, C, D, E, F, G, H, A, W[i], K[i] ); i++;
HannesTschofenig 0:796d0f61a05b 223 }
HannesTschofenig 0:796d0f61a05b 224 while( i < 80 );
HannesTschofenig 0:796d0f61a05b 225
HannesTschofenig 0:796d0f61a05b 226 ctx->state[0] += A;
HannesTschofenig 0:796d0f61a05b 227 ctx->state[1] += B;
HannesTschofenig 0:796d0f61a05b 228 ctx->state[2] += C;
HannesTschofenig 0:796d0f61a05b 229 ctx->state[3] += D;
HannesTschofenig 0:796d0f61a05b 230 ctx->state[4] += E;
HannesTschofenig 0:796d0f61a05b 231 ctx->state[5] += F;
HannesTschofenig 0:796d0f61a05b 232 ctx->state[6] += G;
HannesTschofenig 0:796d0f61a05b 233 ctx->state[7] += H;
HannesTschofenig 0:796d0f61a05b 234 }
HannesTschofenig 0:796d0f61a05b 235
HannesTschofenig 0:796d0f61a05b 236 /*
HannesTschofenig 0:796d0f61a05b 237 * SHA-512 process buffer
HannesTschofenig 0:796d0f61a05b 238 */
HannesTschofenig 0:796d0f61a05b 239 void sha512_update( sha512_context *ctx, const unsigned char *input,
HannesTschofenig 0:796d0f61a05b 240 size_t ilen )
HannesTschofenig 0:796d0f61a05b 241 {
HannesTschofenig 0:796d0f61a05b 242 size_t fill;
HannesTschofenig 0:796d0f61a05b 243 unsigned int left;
HannesTschofenig 0:796d0f61a05b 244
HannesTschofenig 0:796d0f61a05b 245 if( ilen <= 0 )
HannesTschofenig 0:796d0f61a05b 246 return;
HannesTschofenig 0:796d0f61a05b 247
HannesTschofenig 0:796d0f61a05b 248 left = (unsigned int) (ctx->total[0] & 0x7F);
HannesTschofenig 0:796d0f61a05b 249 fill = 128 - left;
HannesTschofenig 0:796d0f61a05b 250
HannesTschofenig 0:796d0f61a05b 251 ctx->total[0] += (uint64_t) ilen;
HannesTschofenig 0:796d0f61a05b 252
HannesTschofenig 0:796d0f61a05b 253 if( ctx->total[0] < (uint64_t) ilen )
HannesTschofenig 0:796d0f61a05b 254 ctx->total[1]++;
HannesTschofenig 0:796d0f61a05b 255
HannesTschofenig 0:796d0f61a05b 256 if( left && ilen >= fill )
HannesTschofenig 0:796d0f61a05b 257 {
HannesTschofenig 0:796d0f61a05b 258 memcpy( (void *) (ctx->buffer + left), input, fill );
HannesTschofenig 0:796d0f61a05b 259 sha512_process( ctx, ctx->buffer );
HannesTschofenig 0:796d0f61a05b 260 input += fill;
HannesTschofenig 0:796d0f61a05b 261 ilen -= fill;
HannesTschofenig 0:796d0f61a05b 262 left = 0;
HannesTschofenig 0:796d0f61a05b 263 }
HannesTschofenig 0:796d0f61a05b 264
HannesTschofenig 0:796d0f61a05b 265 while( ilen >= 128 )
HannesTschofenig 0:796d0f61a05b 266 {
HannesTschofenig 0:796d0f61a05b 267 sha512_process( ctx, input );
HannesTschofenig 0:796d0f61a05b 268 input += 128;
HannesTschofenig 0:796d0f61a05b 269 ilen -= 128;
HannesTschofenig 0:796d0f61a05b 270 }
HannesTschofenig 0:796d0f61a05b 271
HannesTschofenig 0:796d0f61a05b 272 if( ilen > 0 )
HannesTschofenig 0:796d0f61a05b 273 memcpy( (void *) (ctx->buffer + left), input, ilen );
HannesTschofenig 0:796d0f61a05b 274 }
HannesTschofenig 0:796d0f61a05b 275
HannesTschofenig 0:796d0f61a05b 276 static const unsigned char sha512_padding[128] =
HannesTschofenig 0:796d0f61a05b 277 {
HannesTschofenig 0:796d0f61a05b 278 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
HannesTschofenig 0:796d0f61a05b 279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
HannesTschofenig 0:796d0f61a05b 280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
HannesTschofenig 0:796d0f61a05b 281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
HannesTschofenig 0:796d0f61a05b 282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
HannesTschofenig 0:796d0f61a05b 283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
HannesTschofenig 0:796d0f61a05b 284 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
HannesTschofenig 0:796d0f61a05b 285 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
HannesTschofenig 0:796d0f61a05b 286 };
HannesTschofenig 0:796d0f61a05b 287
HannesTschofenig 0:796d0f61a05b 288 /*
HannesTschofenig 0:796d0f61a05b 289 * SHA-512 final digest
HannesTschofenig 0:796d0f61a05b 290 */
HannesTschofenig 0:796d0f61a05b 291 void sha512_finish( sha512_context *ctx, unsigned char output[64] )
HannesTschofenig 0:796d0f61a05b 292 {
HannesTschofenig 0:796d0f61a05b 293 size_t last, padn;
HannesTschofenig 0:796d0f61a05b 294 uint64_t high, low;
HannesTschofenig 0:796d0f61a05b 295 unsigned char msglen[16];
HannesTschofenig 0:796d0f61a05b 296
HannesTschofenig 0:796d0f61a05b 297 high = ( ctx->total[0] >> 61 )
HannesTschofenig 0:796d0f61a05b 298 | ( ctx->total[1] << 3 );
HannesTschofenig 0:796d0f61a05b 299 low = ( ctx->total[0] << 3 );
HannesTschofenig 0:796d0f61a05b 300
HannesTschofenig 0:796d0f61a05b 301 PUT_UINT64_BE( high, msglen, 0 );
HannesTschofenig 0:796d0f61a05b 302 PUT_UINT64_BE( low, msglen, 8 );
HannesTschofenig 0:796d0f61a05b 303
HannesTschofenig 0:796d0f61a05b 304 last = (size_t)( ctx->total[0] & 0x7F );
HannesTschofenig 0:796d0f61a05b 305 padn = ( last < 112 ) ? ( 112 - last ) : ( 240 - last );
HannesTschofenig 0:796d0f61a05b 306
HannesTschofenig 0:796d0f61a05b 307 sha512_update( ctx, sha512_padding, padn );
HannesTschofenig 0:796d0f61a05b 308 sha512_update( ctx, msglen, 16 );
HannesTschofenig 0:796d0f61a05b 309
HannesTschofenig 0:796d0f61a05b 310 PUT_UINT64_BE( ctx->state[0], output, 0 );
HannesTschofenig 0:796d0f61a05b 311 PUT_UINT64_BE( ctx->state[1], output, 8 );
HannesTschofenig 0:796d0f61a05b 312 PUT_UINT64_BE( ctx->state[2], output, 16 );
HannesTschofenig 0:796d0f61a05b 313 PUT_UINT64_BE( ctx->state[3], output, 24 );
HannesTschofenig 0:796d0f61a05b 314 PUT_UINT64_BE( ctx->state[4], output, 32 );
HannesTschofenig 0:796d0f61a05b 315 PUT_UINT64_BE( ctx->state[5], output, 40 );
HannesTschofenig 0:796d0f61a05b 316
HannesTschofenig 0:796d0f61a05b 317 if( ctx->is384 == 0 )
HannesTschofenig 0:796d0f61a05b 318 {
HannesTschofenig 0:796d0f61a05b 319 PUT_UINT64_BE( ctx->state[6], output, 48 );
HannesTschofenig 0:796d0f61a05b 320 PUT_UINT64_BE( ctx->state[7], output, 56 );
HannesTschofenig 0:796d0f61a05b 321 }
HannesTschofenig 0:796d0f61a05b 322 }
HannesTschofenig 0:796d0f61a05b 323
HannesTschofenig 0:796d0f61a05b 324 #endif /* !POLARSSL_SHA512_ALT */
HannesTschofenig 0:796d0f61a05b 325
HannesTschofenig 0:796d0f61a05b 326 /*
HannesTschofenig 0:796d0f61a05b 327 * output = SHA-512( input buffer )
HannesTschofenig 0:796d0f61a05b 328 */
HannesTschofenig 0:796d0f61a05b 329 void sha512( const unsigned char *input, size_t ilen,
HannesTschofenig 0:796d0f61a05b 330 unsigned char output[64], int is384 )
HannesTschofenig 0:796d0f61a05b 331 {
HannesTschofenig 0:796d0f61a05b 332 sha512_context ctx;
HannesTschofenig 0:796d0f61a05b 333
HannesTschofenig 0:796d0f61a05b 334 sha512_starts( &ctx, is384 );
HannesTschofenig 0:796d0f61a05b 335 sha512_update( &ctx, input, ilen );
HannesTschofenig 0:796d0f61a05b 336 sha512_finish( &ctx, output );
HannesTschofenig 0:796d0f61a05b 337
HannesTschofenig 0:796d0f61a05b 338 memset( &ctx, 0, sizeof( sha512_context ) );
HannesTschofenig 0:796d0f61a05b 339 }
HannesTschofenig 0:796d0f61a05b 340
HannesTschofenig 0:796d0f61a05b 341 #if defined(POLARSSL_FS_IO)
HannesTschofenig 0:796d0f61a05b 342 /*
HannesTschofenig 0:796d0f61a05b 343 * output = SHA-512( file contents )
HannesTschofenig 0:796d0f61a05b 344 */
HannesTschofenig 0:796d0f61a05b 345 int sha512_file( const char *path, unsigned char output[64], int is384 )
HannesTschofenig 0:796d0f61a05b 346 {
HannesTschofenig 0:796d0f61a05b 347 FILE *f;
HannesTschofenig 0:796d0f61a05b 348 size_t n;
HannesTschofenig 0:796d0f61a05b 349 sha512_context ctx;
HannesTschofenig 0:796d0f61a05b 350 unsigned char buf[1024];
HannesTschofenig 0:796d0f61a05b 351
HannesTschofenig 0:796d0f61a05b 352 if( ( f = fopen( path, "rb" ) ) == NULL )
HannesTschofenig 0:796d0f61a05b 353 return( POLARSSL_ERR_SHA512_FILE_IO_ERROR );
HannesTschofenig 0:796d0f61a05b 354
HannesTschofenig 0:796d0f61a05b 355 sha512_starts( &ctx, is384 );
HannesTschofenig 0:796d0f61a05b 356
HannesTschofenig 0:796d0f61a05b 357 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
HannesTschofenig 0:796d0f61a05b 358 sha512_update( &ctx, buf, n );
HannesTschofenig 0:796d0f61a05b 359
HannesTschofenig 0:796d0f61a05b 360 sha512_finish( &ctx, output );
HannesTschofenig 0:796d0f61a05b 361
HannesTschofenig 0:796d0f61a05b 362 memset( &ctx, 0, sizeof( sha512_context ) );
HannesTschofenig 0:796d0f61a05b 363
HannesTschofenig 0:796d0f61a05b 364 if( ferror( f ) != 0 )
HannesTschofenig 0:796d0f61a05b 365 {
HannesTschofenig 0:796d0f61a05b 366 fclose( f );
HannesTschofenig 0:796d0f61a05b 367 return( POLARSSL_ERR_SHA512_FILE_IO_ERROR );
HannesTschofenig 0:796d0f61a05b 368 }
HannesTschofenig 0:796d0f61a05b 369
HannesTschofenig 0:796d0f61a05b 370 fclose( f );
HannesTschofenig 0:796d0f61a05b 371 return( 0 );
HannesTschofenig 0:796d0f61a05b 372 }
HannesTschofenig 0:796d0f61a05b 373 #endif /* POLARSSL_FS_IO */
HannesTschofenig 0:796d0f61a05b 374
HannesTschofenig 0:796d0f61a05b 375 /*
HannesTschofenig 0:796d0f61a05b 376 * SHA-512 HMAC context setup
HannesTschofenig 0:796d0f61a05b 377 */
HannesTschofenig 0:796d0f61a05b 378 void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key,
HannesTschofenig 0:796d0f61a05b 379 size_t keylen, int is384 )
HannesTschofenig 0:796d0f61a05b 380 {
HannesTschofenig 0:796d0f61a05b 381 size_t i;
HannesTschofenig 0:796d0f61a05b 382 unsigned char sum[64];
HannesTschofenig 0:796d0f61a05b 383
HannesTschofenig 0:796d0f61a05b 384 if( keylen > 128 )
HannesTschofenig 0:796d0f61a05b 385 {
HannesTschofenig 0:796d0f61a05b 386 sha512( key, keylen, sum, is384 );
HannesTschofenig 0:796d0f61a05b 387 keylen = ( is384 ) ? 48 : 64;
HannesTschofenig 0:796d0f61a05b 388 key = sum;
HannesTschofenig 0:796d0f61a05b 389 }
HannesTschofenig 0:796d0f61a05b 390
HannesTschofenig 0:796d0f61a05b 391 memset( ctx->ipad, 0x36, 128 );
HannesTschofenig 0:796d0f61a05b 392 memset( ctx->opad, 0x5C, 128 );
HannesTschofenig 0:796d0f61a05b 393
HannesTschofenig 0:796d0f61a05b 394 for( i = 0; i < keylen; i++ )
HannesTschofenig 0:796d0f61a05b 395 {
HannesTschofenig 0:796d0f61a05b 396 ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
HannesTschofenig 0:796d0f61a05b 397 ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
HannesTschofenig 0:796d0f61a05b 398 }
HannesTschofenig 0:796d0f61a05b 399
HannesTschofenig 0:796d0f61a05b 400 sha512_starts( ctx, is384 );
HannesTschofenig 0:796d0f61a05b 401 sha512_update( ctx, ctx->ipad, 128 );
HannesTschofenig 0:796d0f61a05b 402
HannesTschofenig 0:796d0f61a05b 403 memset( sum, 0, sizeof( sum ) );
HannesTschofenig 0:796d0f61a05b 404 }
HannesTschofenig 0:796d0f61a05b 405
HannesTschofenig 0:796d0f61a05b 406 /*
HannesTschofenig 0:796d0f61a05b 407 * SHA-512 HMAC process buffer
HannesTschofenig 0:796d0f61a05b 408 */
HannesTschofenig 0:796d0f61a05b 409 void sha512_hmac_update( sha512_context *ctx,
HannesTschofenig 0:796d0f61a05b 410 const unsigned char *input, size_t ilen )
HannesTschofenig 0:796d0f61a05b 411 {
HannesTschofenig 0:796d0f61a05b 412 sha512_update( ctx, input, ilen );
HannesTschofenig 0:796d0f61a05b 413 }
HannesTschofenig 0:796d0f61a05b 414
HannesTschofenig 0:796d0f61a05b 415 /*
HannesTschofenig 0:796d0f61a05b 416 * SHA-512 HMAC final digest
HannesTschofenig 0:796d0f61a05b 417 */
HannesTschofenig 0:796d0f61a05b 418 void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] )
HannesTschofenig 0:796d0f61a05b 419 {
HannesTschofenig 0:796d0f61a05b 420 int is384, hlen;
HannesTschofenig 0:796d0f61a05b 421 unsigned char tmpbuf[64];
HannesTschofenig 0:796d0f61a05b 422
HannesTschofenig 0:796d0f61a05b 423 is384 = ctx->is384;
HannesTschofenig 0:796d0f61a05b 424 hlen = ( is384 == 0 ) ? 64 : 48;
HannesTschofenig 0:796d0f61a05b 425
HannesTschofenig 0:796d0f61a05b 426 sha512_finish( ctx, tmpbuf );
HannesTschofenig 0:796d0f61a05b 427 sha512_starts( ctx, is384 );
HannesTschofenig 0:796d0f61a05b 428 sha512_update( ctx, ctx->opad, 128 );
HannesTschofenig 0:796d0f61a05b 429 sha512_update( ctx, tmpbuf, hlen );
HannesTschofenig 0:796d0f61a05b 430 sha512_finish( ctx, output );
HannesTschofenig 0:796d0f61a05b 431
HannesTschofenig 0:796d0f61a05b 432 memset( tmpbuf, 0, sizeof( tmpbuf ) );
HannesTschofenig 0:796d0f61a05b 433 }
HannesTschofenig 0:796d0f61a05b 434
HannesTschofenig 0:796d0f61a05b 435 /*
HannesTschofenig 0:796d0f61a05b 436 * SHA-512 HMAC context reset
HannesTschofenig 0:796d0f61a05b 437 */
HannesTschofenig 0:796d0f61a05b 438 void sha512_hmac_reset( sha512_context *ctx )
HannesTschofenig 0:796d0f61a05b 439 {
HannesTschofenig 0:796d0f61a05b 440 sha512_starts( ctx, ctx->is384 );
HannesTschofenig 0:796d0f61a05b 441 sha512_update( ctx, ctx->ipad, 128 );
HannesTschofenig 0:796d0f61a05b 442 }
HannesTschofenig 0:796d0f61a05b 443
HannesTschofenig 0:796d0f61a05b 444 /*
HannesTschofenig 0:796d0f61a05b 445 * output = HMAC-SHA-512( hmac key, input buffer )
HannesTschofenig 0:796d0f61a05b 446 */
HannesTschofenig 0:796d0f61a05b 447 void sha512_hmac( const unsigned char *key, size_t keylen,
HannesTschofenig 0:796d0f61a05b 448 const unsigned char *input, size_t ilen,
HannesTschofenig 0:796d0f61a05b 449 unsigned char output[64], int is384 )
HannesTschofenig 0:796d0f61a05b 450 {
HannesTschofenig 0:796d0f61a05b 451 sha512_context ctx;
HannesTschofenig 0:796d0f61a05b 452
HannesTschofenig 0:796d0f61a05b 453 sha512_hmac_starts( &ctx, key, keylen, is384 );
HannesTschofenig 0:796d0f61a05b 454 sha512_hmac_update( &ctx, input, ilen );
HannesTschofenig 0:796d0f61a05b 455 sha512_hmac_finish( &ctx, output );
HannesTschofenig 0:796d0f61a05b 456
HannesTschofenig 0:796d0f61a05b 457 memset( &ctx, 0, sizeof( sha512_context ) );
HannesTschofenig 0:796d0f61a05b 458 }
HannesTschofenig 0:796d0f61a05b 459
HannesTschofenig 0:796d0f61a05b 460 #if defined(POLARSSL_SELF_TEST)
HannesTschofenig 0:796d0f61a05b 461
HannesTschofenig 0:796d0f61a05b 462 /*
HannesTschofenig 0:796d0f61a05b 463 * FIPS-180-2 test vectors
HannesTschofenig 0:796d0f61a05b 464 */
HannesTschofenig 0:796d0f61a05b 465 static unsigned char sha512_test_buf[3][113] =
HannesTschofenig 0:796d0f61a05b 466 {
HannesTschofenig 0:796d0f61a05b 467 { "abc" },
HannesTschofenig 0:796d0f61a05b 468 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
HannesTschofenig 0:796d0f61a05b 469 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" },
HannesTschofenig 0:796d0f61a05b 470 { "" }
HannesTschofenig 0:796d0f61a05b 471 };
HannesTschofenig 0:796d0f61a05b 472
HannesTschofenig 0:796d0f61a05b 473 static const int sha512_test_buflen[3] =
HannesTschofenig 0:796d0f61a05b 474 {
HannesTschofenig 0:796d0f61a05b 475 3, 112, 1000
HannesTschofenig 0:796d0f61a05b 476 };
HannesTschofenig 0:796d0f61a05b 477
HannesTschofenig 0:796d0f61a05b 478 static const unsigned char sha512_test_sum[6][64] =
HannesTschofenig 0:796d0f61a05b 479 {
HannesTschofenig 0:796d0f61a05b 480 /*
HannesTschofenig 0:796d0f61a05b 481 * SHA-384 test vectors
HannesTschofenig 0:796d0f61a05b 482 */
HannesTschofenig 0:796d0f61a05b 483 { 0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B,
HannesTschofenig 0:796d0f61a05b 484 0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07,
HannesTschofenig 0:796d0f61a05b 485 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63,
HannesTschofenig 0:796d0f61a05b 486 0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED,
HannesTschofenig 0:796d0f61a05b 487 0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23,
HannesTschofenig 0:796d0f61a05b 488 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7 },
HannesTschofenig 0:796d0f61a05b 489 { 0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8,
HannesTschofenig 0:796d0f61a05b 490 0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47,
HannesTschofenig 0:796d0f61a05b 491 0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2,
HannesTschofenig 0:796d0f61a05b 492 0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12,
HannesTschofenig 0:796d0f61a05b 493 0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9,
HannesTschofenig 0:796d0f61a05b 494 0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39 },
HannesTschofenig 0:796d0f61a05b 495 { 0x9D, 0x0E, 0x18, 0x09, 0x71, 0x64, 0x74, 0xCB,
HannesTschofenig 0:796d0f61a05b 496 0x08, 0x6E, 0x83, 0x4E, 0x31, 0x0A, 0x4A, 0x1C,
HannesTschofenig 0:796d0f61a05b 497 0xED, 0x14, 0x9E, 0x9C, 0x00, 0xF2, 0x48, 0x52,
HannesTschofenig 0:796d0f61a05b 498 0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B,
HannesTschofenig 0:796d0f61a05b 499 0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB,
HannesTschofenig 0:796d0f61a05b 500 0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85 },
HannesTschofenig 0:796d0f61a05b 501
HannesTschofenig 0:796d0f61a05b 502 /*
HannesTschofenig 0:796d0f61a05b 503 * SHA-512 test vectors
HannesTschofenig 0:796d0f61a05b 504 */
HannesTschofenig 0:796d0f61a05b 505 { 0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA,
HannesTschofenig 0:796d0f61a05b 506 0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31,
HannesTschofenig 0:796d0f61a05b 507 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2,
HannesTschofenig 0:796d0f61a05b 508 0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A,
HannesTschofenig 0:796d0f61a05b 509 0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8,
HannesTschofenig 0:796d0f61a05b 510 0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD,
HannesTschofenig 0:796d0f61a05b 511 0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E,
HannesTschofenig 0:796d0f61a05b 512 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F },
HannesTschofenig 0:796d0f61a05b 513 { 0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA,
HannesTschofenig 0:796d0f61a05b 514 0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F,
HannesTschofenig 0:796d0f61a05b 515 0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1,
HannesTschofenig 0:796d0f61a05b 516 0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18,
HannesTschofenig 0:796d0f61a05b 517 0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4,
HannesTschofenig 0:796d0f61a05b 518 0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A,
HannesTschofenig 0:796d0f61a05b 519 0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54,
HannesTschofenig 0:796d0f61a05b 520 0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09 },
HannesTschofenig 0:796d0f61a05b 521 { 0xE7, 0x18, 0x48, 0x3D, 0x0C, 0xE7, 0x69, 0x64,
HannesTschofenig 0:796d0f61a05b 522 0x4E, 0x2E, 0x42, 0xC7, 0xBC, 0x15, 0xB4, 0x63,
HannesTschofenig 0:796d0f61a05b 523 0x8E, 0x1F, 0x98, 0xB1, 0x3B, 0x20, 0x44, 0x28,
HannesTschofenig 0:796d0f61a05b 524 0x56, 0x32, 0xA8, 0x03, 0xAF, 0xA9, 0x73, 0xEB,
HannesTschofenig 0:796d0f61a05b 525 0xDE, 0x0F, 0xF2, 0x44, 0x87, 0x7E, 0xA6, 0x0A,
HannesTschofenig 0:796d0f61a05b 526 0x4C, 0xB0, 0x43, 0x2C, 0xE5, 0x77, 0xC3, 0x1B,
HannesTschofenig 0:796d0f61a05b 527 0xEB, 0x00, 0x9C, 0x5C, 0x2C, 0x49, 0xAA, 0x2E,
HannesTschofenig 0:796d0f61a05b 528 0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B }
HannesTschofenig 0:796d0f61a05b 529 };
HannesTschofenig 0:796d0f61a05b 530
HannesTschofenig 0:796d0f61a05b 531 /*
HannesTschofenig 0:796d0f61a05b 532 * RFC 4231 test vectors
HannesTschofenig 0:796d0f61a05b 533 */
HannesTschofenig 0:796d0f61a05b 534 static unsigned char sha512_hmac_test_key[7][26] =
HannesTschofenig 0:796d0f61a05b 535 {
HannesTschofenig 0:796d0f61a05b 536 { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B"
HannesTschofenig 0:796d0f61a05b 537 "\x0B\x0B\x0B\x0B" },
HannesTschofenig 0:796d0f61a05b 538 { "Jefe" },
HannesTschofenig 0:796d0f61a05b 539 { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
HannesTschofenig 0:796d0f61a05b 540 "\xAA\xAA\xAA\xAA" },
HannesTschofenig 0:796d0f61a05b 541 { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
HannesTschofenig 0:796d0f61a05b 542 "\x11\x12\x13\x14\x15\x16\x17\x18\x19" },
HannesTschofenig 0:796d0f61a05b 543 { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C"
HannesTschofenig 0:796d0f61a05b 544 "\x0C\x0C\x0C\x0C" },
HannesTschofenig 0:796d0f61a05b 545 { "" }, /* 0xAA 131 times */
HannesTschofenig 0:796d0f61a05b 546 { "" }
HannesTschofenig 0:796d0f61a05b 547 };
HannesTschofenig 0:796d0f61a05b 548
HannesTschofenig 0:796d0f61a05b 549 static const int sha512_hmac_test_keylen[7] =
HannesTschofenig 0:796d0f61a05b 550 {
HannesTschofenig 0:796d0f61a05b 551 20, 4, 20, 25, 20, 131, 131
HannesTschofenig 0:796d0f61a05b 552 };
HannesTschofenig 0:796d0f61a05b 553
HannesTschofenig 0:796d0f61a05b 554 static unsigned char sha512_hmac_test_buf[7][153] =
HannesTschofenig 0:796d0f61a05b 555 {
HannesTschofenig 0:796d0f61a05b 556 { "Hi There" },
HannesTschofenig 0:796d0f61a05b 557 { "what do ya want for nothing?" },
HannesTschofenig 0:796d0f61a05b 558 { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
HannesTschofenig 0:796d0f61a05b 559 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
HannesTschofenig 0:796d0f61a05b 560 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
HannesTschofenig 0:796d0f61a05b 561 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
HannesTschofenig 0:796d0f61a05b 562 "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" },
HannesTschofenig 0:796d0f61a05b 563 { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
HannesTschofenig 0:796d0f61a05b 564 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
HannesTschofenig 0:796d0f61a05b 565 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
HannesTschofenig 0:796d0f61a05b 566 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
HannesTschofenig 0:796d0f61a05b 567 "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" },
HannesTschofenig 0:796d0f61a05b 568 { "Test With Truncation" },
HannesTschofenig 0:796d0f61a05b 569 { "Test Using Larger Than Block-Size Key - Hash Key First" },
HannesTschofenig 0:796d0f61a05b 570 { "This is a test using a larger than block-size key "
HannesTschofenig 0:796d0f61a05b 571 "and a larger than block-size data. The key needs to "
HannesTschofenig 0:796d0f61a05b 572 "be hashed before being used by the HMAC algorithm." }
HannesTschofenig 0:796d0f61a05b 573 };
HannesTschofenig 0:796d0f61a05b 574
HannesTschofenig 0:796d0f61a05b 575 static const int sha512_hmac_test_buflen[7] =
HannesTschofenig 0:796d0f61a05b 576 {
HannesTschofenig 0:796d0f61a05b 577 8, 28, 50, 50, 20, 54, 152
HannesTschofenig 0:796d0f61a05b 578 };
HannesTschofenig 0:796d0f61a05b 579
HannesTschofenig 0:796d0f61a05b 580 static const unsigned char sha512_hmac_test_sum[14][64] =
HannesTschofenig 0:796d0f61a05b 581 {
HannesTschofenig 0:796d0f61a05b 582 /*
HannesTschofenig 0:796d0f61a05b 583 * HMAC-SHA-384 test vectors
HannesTschofenig 0:796d0f61a05b 584 */
HannesTschofenig 0:796d0f61a05b 585 { 0xAF, 0xD0, 0x39, 0x44, 0xD8, 0x48, 0x95, 0x62,
HannesTschofenig 0:796d0f61a05b 586 0x6B, 0x08, 0x25, 0xF4, 0xAB, 0x46, 0x90, 0x7F,
HannesTschofenig 0:796d0f61a05b 587 0x15, 0xF9, 0xDA, 0xDB, 0xE4, 0x10, 0x1E, 0xC6,
HannesTschofenig 0:796d0f61a05b 588 0x82, 0xAA, 0x03, 0x4C, 0x7C, 0xEB, 0xC5, 0x9C,
HannesTschofenig 0:796d0f61a05b 589 0xFA, 0xEA, 0x9E, 0xA9, 0x07, 0x6E, 0xDE, 0x7F,
HannesTschofenig 0:796d0f61a05b 590 0x4A, 0xF1, 0x52, 0xE8, 0xB2, 0xFA, 0x9C, 0xB6 },
HannesTschofenig 0:796d0f61a05b 591 { 0xAF, 0x45, 0xD2, 0xE3, 0x76, 0x48, 0x40, 0x31,
HannesTschofenig 0:796d0f61a05b 592 0x61, 0x7F, 0x78, 0xD2, 0xB5, 0x8A, 0x6B, 0x1B,
HannesTschofenig 0:796d0f61a05b 593 0x9C, 0x7E, 0xF4, 0x64, 0xF5, 0xA0, 0x1B, 0x47,
HannesTschofenig 0:796d0f61a05b 594 0xE4, 0x2E, 0xC3, 0x73, 0x63, 0x22, 0x44, 0x5E,
HannesTschofenig 0:796d0f61a05b 595 0x8E, 0x22, 0x40, 0xCA, 0x5E, 0x69, 0xE2, 0xC7,
HannesTschofenig 0:796d0f61a05b 596 0x8B, 0x32, 0x39, 0xEC, 0xFA, 0xB2, 0x16, 0x49 },
HannesTschofenig 0:796d0f61a05b 597 { 0x88, 0x06, 0x26, 0x08, 0xD3, 0xE6, 0xAD, 0x8A,
HannesTschofenig 0:796d0f61a05b 598 0x0A, 0xA2, 0xAC, 0xE0, 0x14, 0xC8, 0xA8, 0x6F,
HannesTschofenig 0:796d0f61a05b 599 0x0A, 0xA6, 0x35, 0xD9, 0x47, 0xAC, 0x9F, 0xEB,
HannesTschofenig 0:796d0f61a05b 600 0xE8, 0x3E, 0xF4, 0xE5, 0x59, 0x66, 0x14, 0x4B,
HannesTschofenig 0:796d0f61a05b 601 0x2A, 0x5A, 0xB3, 0x9D, 0xC1, 0x38, 0x14, 0xB9,
HannesTschofenig 0:796d0f61a05b 602 0x4E, 0x3A, 0xB6, 0xE1, 0x01, 0xA3, 0x4F, 0x27 },
HannesTschofenig 0:796d0f61a05b 603 { 0x3E, 0x8A, 0x69, 0xB7, 0x78, 0x3C, 0x25, 0x85,
HannesTschofenig 0:796d0f61a05b 604 0x19, 0x33, 0xAB, 0x62, 0x90, 0xAF, 0x6C, 0xA7,
HannesTschofenig 0:796d0f61a05b 605 0x7A, 0x99, 0x81, 0x48, 0x08, 0x50, 0x00, 0x9C,
HannesTschofenig 0:796d0f61a05b 606 0xC5, 0x57, 0x7C, 0x6E, 0x1F, 0x57, 0x3B, 0x4E,
HannesTschofenig 0:796d0f61a05b 607 0x68, 0x01, 0xDD, 0x23, 0xC4, 0xA7, 0xD6, 0x79,
HannesTschofenig 0:796d0f61a05b 608 0xCC, 0xF8, 0xA3, 0x86, 0xC6, 0x74, 0xCF, 0xFB },
HannesTschofenig 0:796d0f61a05b 609 { 0x3A, 0xBF, 0x34, 0xC3, 0x50, 0x3B, 0x2A, 0x23,
HannesTschofenig 0:796d0f61a05b 610 0xA4, 0x6E, 0xFC, 0x61, 0x9B, 0xAE, 0xF8, 0x97 },
HannesTschofenig 0:796d0f61a05b 611 { 0x4E, 0xCE, 0x08, 0x44, 0x85, 0x81, 0x3E, 0x90,
HannesTschofenig 0:796d0f61a05b 612 0x88, 0xD2, 0xC6, 0x3A, 0x04, 0x1B, 0xC5, 0xB4,
HannesTschofenig 0:796d0f61a05b 613 0x4F, 0x9E, 0xF1, 0x01, 0x2A, 0x2B, 0x58, 0x8F,
HannesTschofenig 0:796d0f61a05b 614 0x3C, 0xD1, 0x1F, 0x05, 0x03, 0x3A, 0xC4, 0xC6,
HannesTschofenig 0:796d0f61a05b 615 0x0C, 0x2E, 0xF6, 0xAB, 0x40, 0x30, 0xFE, 0x82,
HannesTschofenig 0:796d0f61a05b 616 0x96, 0x24, 0x8D, 0xF1, 0x63, 0xF4, 0x49, 0x52 },
HannesTschofenig 0:796d0f61a05b 617 { 0x66, 0x17, 0x17, 0x8E, 0x94, 0x1F, 0x02, 0x0D,
HannesTschofenig 0:796d0f61a05b 618 0x35, 0x1E, 0x2F, 0x25, 0x4E, 0x8F, 0xD3, 0x2C,
HannesTschofenig 0:796d0f61a05b 619 0x60, 0x24, 0x20, 0xFE, 0xB0, 0xB8, 0xFB, 0x9A,
HannesTschofenig 0:796d0f61a05b 620 0xDC, 0xCE, 0xBB, 0x82, 0x46, 0x1E, 0x99, 0xC5,
HannesTschofenig 0:796d0f61a05b 621 0xA6, 0x78, 0xCC, 0x31, 0xE7, 0x99, 0x17, 0x6D,
HannesTschofenig 0:796d0f61a05b 622 0x38, 0x60, 0xE6, 0x11, 0x0C, 0x46, 0x52, 0x3E },
HannesTschofenig 0:796d0f61a05b 623
HannesTschofenig 0:796d0f61a05b 624 /*
HannesTschofenig 0:796d0f61a05b 625 * HMAC-SHA-512 test vectors
HannesTschofenig 0:796d0f61a05b 626 */
HannesTschofenig 0:796d0f61a05b 627 { 0x87, 0xAA, 0x7C, 0xDE, 0xA5, 0xEF, 0x61, 0x9D,
HannesTschofenig 0:796d0f61a05b 628 0x4F, 0xF0, 0xB4, 0x24, 0x1A, 0x1D, 0x6C, 0xB0,
HannesTschofenig 0:796d0f61a05b 629 0x23, 0x79, 0xF4, 0xE2, 0xCE, 0x4E, 0xC2, 0x78,
HannesTschofenig 0:796d0f61a05b 630 0x7A, 0xD0, 0xB3, 0x05, 0x45, 0xE1, 0x7C, 0xDE,
HannesTschofenig 0:796d0f61a05b 631 0xDA, 0xA8, 0x33, 0xB7, 0xD6, 0xB8, 0xA7, 0x02,
HannesTschofenig 0:796d0f61a05b 632 0x03, 0x8B, 0x27, 0x4E, 0xAE, 0xA3, 0xF4, 0xE4,
HannesTschofenig 0:796d0f61a05b 633 0xBE, 0x9D, 0x91, 0x4E, 0xEB, 0x61, 0xF1, 0x70,
HannesTschofenig 0:796d0f61a05b 634 0x2E, 0x69, 0x6C, 0x20, 0x3A, 0x12, 0x68, 0x54 },
HannesTschofenig 0:796d0f61a05b 635 { 0x16, 0x4B, 0x7A, 0x7B, 0xFC, 0xF8, 0x19, 0xE2,
HannesTschofenig 0:796d0f61a05b 636 0xE3, 0x95, 0xFB, 0xE7, 0x3B, 0x56, 0xE0, 0xA3,
HannesTschofenig 0:796d0f61a05b 637 0x87, 0xBD, 0x64, 0x22, 0x2E, 0x83, 0x1F, 0xD6,
HannesTschofenig 0:796d0f61a05b 638 0x10, 0x27, 0x0C, 0xD7, 0xEA, 0x25, 0x05, 0x54,
HannesTschofenig 0:796d0f61a05b 639 0x97, 0x58, 0xBF, 0x75, 0xC0, 0x5A, 0x99, 0x4A,
HannesTschofenig 0:796d0f61a05b 640 0x6D, 0x03, 0x4F, 0x65, 0xF8, 0xF0, 0xE6, 0xFD,
HannesTschofenig 0:796d0f61a05b 641 0xCA, 0xEA, 0xB1, 0xA3, 0x4D, 0x4A, 0x6B, 0x4B,
HannesTschofenig 0:796d0f61a05b 642 0x63, 0x6E, 0x07, 0x0A, 0x38, 0xBC, 0xE7, 0x37 },
HannesTschofenig 0:796d0f61a05b 643 { 0xFA, 0x73, 0xB0, 0x08, 0x9D, 0x56, 0xA2, 0x84,
HannesTschofenig 0:796d0f61a05b 644 0xEF, 0xB0, 0xF0, 0x75, 0x6C, 0x89, 0x0B, 0xE9,
HannesTschofenig 0:796d0f61a05b 645 0xB1, 0xB5, 0xDB, 0xDD, 0x8E, 0xE8, 0x1A, 0x36,
HannesTschofenig 0:796d0f61a05b 646 0x55, 0xF8, 0x3E, 0x33, 0xB2, 0x27, 0x9D, 0x39,
HannesTschofenig 0:796d0f61a05b 647 0xBF, 0x3E, 0x84, 0x82, 0x79, 0xA7, 0x22, 0xC8,
HannesTschofenig 0:796d0f61a05b 648 0x06, 0xB4, 0x85, 0xA4, 0x7E, 0x67, 0xC8, 0x07,
HannesTschofenig 0:796d0f61a05b 649 0xB9, 0x46, 0xA3, 0x37, 0xBE, 0xE8, 0x94, 0x26,
HannesTschofenig 0:796d0f61a05b 650 0x74, 0x27, 0x88, 0x59, 0xE1, 0x32, 0x92, 0xFB },
HannesTschofenig 0:796d0f61a05b 651 { 0xB0, 0xBA, 0x46, 0x56, 0x37, 0x45, 0x8C, 0x69,
HannesTschofenig 0:796d0f61a05b 652 0x90, 0xE5, 0xA8, 0xC5, 0xF6, 0x1D, 0x4A, 0xF7,
HannesTschofenig 0:796d0f61a05b 653 0xE5, 0x76, 0xD9, 0x7F, 0xF9, 0x4B, 0x87, 0x2D,
HannesTschofenig 0:796d0f61a05b 654 0xE7, 0x6F, 0x80, 0x50, 0x36, 0x1E, 0xE3, 0xDB,
HannesTschofenig 0:796d0f61a05b 655 0xA9, 0x1C, 0xA5, 0xC1, 0x1A, 0xA2, 0x5E, 0xB4,
HannesTschofenig 0:796d0f61a05b 656 0xD6, 0x79, 0x27, 0x5C, 0xC5, 0x78, 0x80, 0x63,
HannesTschofenig 0:796d0f61a05b 657 0xA5, 0xF1, 0x97, 0x41, 0x12, 0x0C, 0x4F, 0x2D,
HannesTschofenig 0:796d0f61a05b 658 0xE2, 0xAD, 0xEB, 0xEB, 0x10, 0xA2, 0x98, 0xDD },
HannesTschofenig 0:796d0f61a05b 659 { 0x41, 0x5F, 0xAD, 0x62, 0x71, 0x58, 0x0A, 0x53,
HannesTschofenig 0:796d0f61a05b 660 0x1D, 0x41, 0x79, 0xBC, 0x89, 0x1D, 0x87, 0xA6 },
HannesTschofenig 0:796d0f61a05b 661 { 0x80, 0xB2, 0x42, 0x63, 0xC7, 0xC1, 0xA3, 0xEB,
HannesTschofenig 0:796d0f61a05b 662 0xB7, 0x14, 0x93, 0xC1, 0xDD, 0x7B, 0xE8, 0xB4,
HannesTschofenig 0:796d0f61a05b 663 0x9B, 0x46, 0xD1, 0xF4, 0x1B, 0x4A, 0xEE, 0xC1,
HannesTschofenig 0:796d0f61a05b 664 0x12, 0x1B, 0x01, 0x37, 0x83, 0xF8, 0xF3, 0x52,
HannesTschofenig 0:796d0f61a05b 665 0x6B, 0x56, 0xD0, 0x37, 0xE0, 0x5F, 0x25, 0x98,
HannesTschofenig 0:796d0f61a05b 666 0xBD, 0x0F, 0xD2, 0x21, 0x5D, 0x6A, 0x1E, 0x52,
HannesTschofenig 0:796d0f61a05b 667 0x95, 0xE6, 0x4F, 0x73, 0xF6, 0x3F, 0x0A, 0xEC,
HannesTschofenig 0:796d0f61a05b 668 0x8B, 0x91, 0x5A, 0x98, 0x5D, 0x78, 0x65, 0x98 },
HannesTschofenig 0:796d0f61a05b 669 { 0xE3, 0x7B, 0x6A, 0x77, 0x5D, 0xC8, 0x7D, 0xBA,
HannesTschofenig 0:796d0f61a05b 670 0xA4, 0xDF, 0xA9, 0xF9, 0x6E, 0x5E, 0x3F, 0xFD,
HannesTschofenig 0:796d0f61a05b 671 0xDE, 0xBD, 0x71, 0xF8, 0x86, 0x72, 0x89, 0x86,
HannesTschofenig 0:796d0f61a05b 672 0x5D, 0xF5, 0xA3, 0x2D, 0x20, 0xCD, 0xC9, 0x44,
HannesTschofenig 0:796d0f61a05b 673 0xB6, 0x02, 0x2C, 0xAC, 0x3C, 0x49, 0x82, 0xB1,
HannesTschofenig 0:796d0f61a05b 674 0x0D, 0x5E, 0xEB, 0x55, 0xC3, 0xE4, 0xDE, 0x15,
HannesTschofenig 0:796d0f61a05b 675 0x13, 0x46, 0x76, 0xFB, 0x6D, 0xE0, 0x44, 0x60,
HannesTschofenig 0:796d0f61a05b 676 0x65, 0xC9, 0x74, 0x40, 0xFA, 0x8C, 0x6A, 0x58 }
HannesTschofenig 0:796d0f61a05b 677 };
HannesTschofenig 0:796d0f61a05b 678
HannesTschofenig 0:796d0f61a05b 679 /*
HannesTschofenig 0:796d0f61a05b 680 * Checkup routine
HannesTschofenig 0:796d0f61a05b 681 */
HannesTschofenig 0:796d0f61a05b 682 int sha512_self_test( int verbose )
HannesTschofenig 0:796d0f61a05b 683 {
HannesTschofenig 0:796d0f61a05b 684 int i, j, k, buflen;
HannesTschofenig 0:796d0f61a05b 685 unsigned char buf[1024];
HannesTschofenig 0:796d0f61a05b 686 unsigned char sha512sum[64];
HannesTschofenig 0:796d0f61a05b 687 sha512_context ctx;
HannesTschofenig 0:796d0f61a05b 688
HannesTschofenig 0:796d0f61a05b 689 for( i = 0; i < 6; i++ )
HannesTschofenig 0:796d0f61a05b 690 {
HannesTschofenig 0:796d0f61a05b 691 j = i % 3;
HannesTschofenig 0:796d0f61a05b 692 k = i < 3;
HannesTschofenig 0:796d0f61a05b 693
HannesTschofenig 0:796d0f61a05b 694 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 695 polarssl_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 );
HannesTschofenig 0:796d0f61a05b 696
HannesTschofenig 0:796d0f61a05b 697 sha512_starts( &ctx, k );
HannesTschofenig 0:796d0f61a05b 698
HannesTschofenig 0:796d0f61a05b 699 if( j == 2 )
HannesTschofenig 0:796d0f61a05b 700 {
HannesTschofenig 0:796d0f61a05b 701 memset( buf, 'a', buflen = 1000 );
HannesTschofenig 0:796d0f61a05b 702
HannesTschofenig 0:796d0f61a05b 703 for( j = 0; j < 1000; j++ )
HannesTschofenig 0:796d0f61a05b 704 sha512_update( &ctx, buf, buflen );
HannesTschofenig 0:796d0f61a05b 705 }
HannesTschofenig 0:796d0f61a05b 706 else
HannesTschofenig 0:796d0f61a05b 707 sha512_update( &ctx, sha512_test_buf[j],
HannesTschofenig 0:796d0f61a05b 708 sha512_test_buflen[j] );
HannesTschofenig 0:796d0f61a05b 709
HannesTschofenig 0:796d0f61a05b 710 sha512_finish( &ctx, sha512sum );
HannesTschofenig 0:796d0f61a05b 711
HannesTschofenig 0:796d0f61a05b 712 if( memcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 )
HannesTschofenig 0:796d0f61a05b 713 {
HannesTschofenig 0:796d0f61a05b 714 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 715 polarssl_printf( "failed\n" );
HannesTschofenig 0:796d0f61a05b 716
HannesTschofenig 0:796d0f61a05b 717 return( 1 );
HannesTschofenig 0:796d0f61a05b 718 }
HannesTschofenig 0:796d0f61a05b 719
HannesTschofenig 0:796d0f61a05b 720 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 721 polarssl_printf( "passed\n" );
HannesTschofenig 0:796d0f61a05b 722 }
HannesTschofenig 0:796d0f61a05b 723
HannesTschofenig 0:796d0f61a05b 724 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 725 polarssl_printf( "\n" );
HannesTschofenig 0:796d0f61a05b 726
HannesTschofenig 0:796d0f61a05b 727 for( i = 0; i < 14; i++ )
HannesTschofenig 0:796d0f61a05b 728 {
HannesTschofenig 0:796d0f61a05b 729 j = i % 7;
HannesTschofenig 0:796d0f61a05b 730 k = i < 7;
HannesTschofenig 0:796d0f61a05b 731
HannesTschofenig 0:796d0f61a05b 732 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 733 polarssl_printf( " HMAC-SHA-%d test #%d: ", 512 - k * 128, j + 1 );
HannesTschofenig 0:796d0f61a05b 734
HannesTschofenig 0:796d0f61a05b 735 if( j == 5 || j == 6 )
HannesTschofenig 0:796d0f61a05b 736 {
HannesTschofenig 0:796d0f61a05b 737 memset( buf, '\xAA', buflen = 131 );
HannesTschofenig 0:796d0f61a05b 738 sha512_hmac_starts( &ctx, buf, buflen, k );
HannesTschofenig 0:796d0f61a05b 739 }
HannesTschofenig 0:796d0f61a05b 740 else
HannesTschofenig 0:796d0f61a05b 741 sha512_hmac_starts( &ctx, sha512_hmac_test_key[j],
HannesTschofenig 0:796d0f61a05b 742 sha512_hmac_test_keylen[j], k );
HannesTschofenig 0:796d0f61a05b 743
HannesTschofenig 0:796d0f61a05b 744 sha512_hmac_update( &ctx, sha512_hmac_test_buf[j],
HannesTschofenig 0:796d0f61a05b 745 sha512_hmac_test_buflen[j] );
HannesTschofenig 0:796d0f61a05b 746
HannesTschofenig 0:796d0f61a05b 747 sha512_hmac_finish( &ctx, sha512sum );
HannesTschofenig 0:796d0f61a05b 748
HannesTschofenig 0:796d0f61a05b 749 buflen = ( j == 4 ) ? 16 : 64 - k * 16;
HannesTschofenig 0:796d0f61a05b 750
HannesTschofenig 0:796d0f61a05b 751 if( memcmp( sha512sum, sha512_hmac_test_sum[i], buflen ) != 0 )
HannesTschofenig 0:796d0f61a05b 752 {
HannesTschofenig 0:796d0f61a05b 753 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 754 polarssl_printf( "failed\n" );
HannesTschofenig 0:796d0f61a05b 755
HannesTschofenig 0:796d0f61a05b 756 return( 1 );
HannesTschofenig 0:796d0f61a05b 757 }
HannesTschofenig 0:796d0f61a05b 758
HannesTschofenig 0:796d0f61a05b 759 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 760 polarssl_printf( "passed\n" );
HannesTschofenig 0:796d0f61a05b 761 }
HannesTschofenig 0:796d0f61a05b 762
HannesTschofenig 0:796d0f61a05b 763 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 764 polarssl_printf( "\n" );
HannesTschofenig 0:796d0f61a05b 765
HannesTschofenig 0:796d0f61a05b 766 return( 0 );
HannesTschofenig 0:796d0f61a05b 767 }
HannesTschofenig 0:796d0f61a05b 768
HannesTschofenig 0:796d0f61a05b 769 #endif /* POLARSSL_SELF_TEST */
HannesTschofenig 0:796d0f61a05b 770
HannesTschofenig 0:796d0f61a05b 771 #endif /* POLARSSL_SHA512_C */
HannesTschofenig 0:796d0f61a05b 772
HannesTschofenig 0:796d0f61a05b 773