This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 0:f7c60d3e7b8a 1 /*
maclobdell 0:f7c60d3e7b8a 2 * NIST SP800-38D compliant GCM 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 /*
maclobdell 0:f7c60d3e7b8a 23 * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
maclobdell 0:f7c60d3e7b8a 24 *
maclobdell 0:f7c60d3e7b8a 25 * See also:
maclobdell 0:f7c60d3e7b8a 26 * [MGV] http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
maclobdell 0:f7c60d3e7b8a 27 *
maclobdell 0:f7c60d3e7b8a 28 * We use the algorithm described as Shoup's method with 4-bit tables in
maclobdell 0:f7c60d3e7b8a 29 * [MGV] 4.1, pp. 12-13, to enhance speed without using too much memory.
maclobdell 0:f7c60d3e7b8a 30 */
maclobdell 0:f7c60d3e7b8a 31
maclobdell 0:f7c60d3e7b8a 32 #if !defined(MBEDTLS_CONFIG_FILE)
maclobdell 0:f7c60d3e7b8a 33 #include "mbedtls/config.h"
maclobdell 0:f7c60d3e7b8a 34 #else
maclobdell 0:f7c60d3e7b8a 35 #include MBEDTLS_CONFIG_FILE
maclobdell 0:f7c60d3e7b8a 36 #endif
maclobdell 0:f7c60d3e7b8a 37
maclobdell 0:f7c60d3e7b8a 38 #if defined(MBEDTLS_GCM_C)
maclobdell 0:f7c60d3e7b8a 39
maclobdell 0:f7c60d3e7b8a 40 #include "mbedtls/gcm.h"
maclobdell 0:f7c60d3e7b8a 41
maclobdell 0:f7c60d3e7b8a 42 #include <string.h>
maclobdell 0:f7c60d3e7b8a 43
maclobdell 0:f7c60d3e7b8a 44 #if defined(MBEDTLS_AESNI_C)
maclobdell 0:f7c60d3e7b8a 45 #include "mbedtls/aesni.h"
maclobdell 0:f7c60d3e7b8a 46 #endif
maclobdell 0:f7c60d3e7b8a 47
maclobdell 0:f7c60d3e7b8a 48 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
maclobdell 0:f7c60d3e7b8a 49 #if defined(MBEDTLS_PLATFORM_C)
maclobdell 0:f7c60d3e7b8a 50 #include "mbedtls/platform.h"
maclobdell 0:f7c60d3e7b8a 51 #else
maclobdell 0:f7c60d3e7b8a 52 #include <stdio.h>
maclobdell 0:f7c60d3e7b8a 53 #define mbedtls_printf printf
maclobdell 0:f7c60d3e7b8a 54 #endif /* MBEDTLS_PLATFORM_C */
maclobdell 0:f7c60d3e7b8a 55 #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
maclobdell 0:f7c60d3e7b8a 56
maclobdell 0:f7c60d3e7b8a 57 /*
maclobdell 0:f7c60d3e7b8a 58 * 32-bit integer manipulation macros (big endian)
maclobdell 0:f7c60d3e7b8a 59 */
maclobdell 0:f7c60d3e7b8a 60 #ifndef GET_UINT32_BE
maclobdell 0:f7c60d3e7b8a 61 #define GET_UINT32_BE(n,b,i) \
maclobdell 0:f7c60d3e7b8a 62 { \
maclobdell 0:f7c60d3e7b8a 63 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
maclobdell 0:f7c60d3e7b8a 64 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
maclobdell 0:f7c60d3e7b8a 65 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
maclobdell 0:f7c60d3e7b8a 66 | ( (uint32_t) (b)[(i) + 3] ); \
maclobdell 0:f7c60d3e7b8a 67 }
maclobdell 0:f7c60d3e7b8a 68 #endif
maclobdell 0:f7c60d3e7b8a 69
maclobdell 0:f7c60d3e7b8a 70 #ifndef PUT_UINT32_BE
maclobdell 0:f7c60d3e7b8a 71 #define PUT_UINT32_BE(n,b,i) \
maclobdell 0:f7c60d3e7b8a 72 { \
maclobdell 0:f7c60d3e7b8a 73 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
maclobdell 0:f7c60d3e7b8a 74 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
maclobdell 0:f7c60d3e7b8a 75 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
maclobdell 0:f7c60d3e7b8a 76 (b)[(i) + 3] = (unsigned char) ( (n) ); \
maclobdell 0:f7c60d3e7b8a 77 }
maclobdell 0:f7c60d3e7b8a 78 #endif
maclobdell 0:f7c60d3e7b8a 79
maclobdell 0:f7c60d3e7b8a 80 /* Implementation that should never be optimized out by the compiler */
maclobdell 0:f7c60d3e7b8a 81 static void mbedtls_zeroize( void *v, size_t n ) {
maclobdell 0:f7c60d3e7b8a 82 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
maclobdell 0:f7c60d3e7b8a 83 }
maclobdell 0:f7c60d3e7b8a 84
maclobdell 0:f7c60d3e7b8a 85 /*
maclobdell 0:f7c60d3e7b8a 86 * Initialize a context
maclobdell 0:f7c60d3e7b8a 87 */
maclobdell 0:f7c60d3e7b8a 88 void mbedtls_gcm_init( mbedtls_gcm_context *ctx )
maclobdell 0:f7c60d3e7b8a 89 {
maclobdell 0:f7c60d3e7b8a 90 memset( ctx, 0, sizeof( mbedtls_gcm_context ) );
maclobdell 0:f7c60d3e7b8a 91 }
maclobdell 0:f7c60d3e7b8a 92
maclobdell 0:f7c60d3e7b8a 93 /*
maclobdell 0:f7c60d3e7b8a 94 * Precompute small multiples of H, that is set
maclobdell 0:f7c60d3e7b8a 95 * HH[i] || HL[i] = H times i,
maclobdell 0:f7c60d3e7b8a 96 * where i is seen as a field element as in [MGV], ie high-order bits
maclobdell 0:f7c60d3e7b8a 97 * correspond to low powers of P. The result is stored in the same way, that
maclobdell 0:f7c60d3e7b8a 98 * is the high-order bit of HH corresponds to P^0 and the low-order bit of HL
maclobdell 0:f7c60d3e7b8a 99 * corresponds to P^127.
maclobdell 0:f7c60d3e7b8a 100 */
maclobdell 0:f7c60d3e7b8a 101 static int gcm_gen_table( mbedtls_gcm_context *ctx )
maclobdell 0:f7c60d3e7b8a 102 {
maclobdell 0:f7c60d3e7b8a 103 int ret, i, j;
maclobdell 0:f7c60d3e7b8a 104 uint64_t hi, lo;
maclobdell 0:f7c60d3e7b8a 105 uint64_t vl, vh;
maclobdell 0:f7c60d3e7b8a 106 unsigned char h[16];
maclobdell 0:f7c60d3e7b8a 107 size_t olen = 0;
maclobdell 0:f7c60d3e7b8a 108
maclobdell 0:f7c60d3e7b8a 109 memset( h, 0, 16 );
maclobdell 0:f7c60d3e7b8a 110 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, h, 16, h, &olen ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 111 return( ret );
maclobdell 0:f7c60d3e7b8a 112
maclobdell 0:f7c60d3e7b8a 113 /* pack h as two 64-bits ints, big-endian */
maclobdell 0:f7c60d3e7b8a 114 GET_UINT32_BE( hi, h, 0 );
maclobdell 0:f7c60d3e7b8a 115 GET_UINT32_BE( lo, h, 4 );
maclobdell 0:f7c60d3e7b8a 116 vh = (uint64_t) hi << 32 | lo;
maclobdell 0:f7c60d3e7b8a 117
maclobdell 0:f7c60d3e7b8a 118 GET_UINT32_BE( hi, h, 8 );
maclobdell 0:f7c60d3e7b8a 119 GET_UINT32_BE( lo, h, 12 );
maclobdell 0:f7c60d3e7b8a 120 vl = (uint64_t) hi << 32 | lo;
maclobdell 0:f7c60d3e7b8a 121
maclobdell 0:f7c60d3e7b8a 122 /* 8 = 1000 corresponds to 1 in GF(2^128) */
maclobdell 0:f7c60d3e7b8a 123 ctx->HL[8] = vl;
maclobdell 0:f7c60d3e7b8a 124 ctx->HH[8] = vh;
maclobdell 0:f7c60d3e7b8a 125
maclobdell 0:f7c60d3e7b8a 126 #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
maclobdell 0:f7c60d3e7b8a 127 /* With CLMUL support, we need only h, not the rest of the table */
maclobdell 0:f7c60d3e7b8a 128 if( mbedtls_aesni_has_support( MBEDTLS_AESNI_CLMUL ) )
maclobdell 0:f7c60d3e7b8a 129 return( 0 );
maclobdell 0:f7c60d3e7b8a 130 #endif
maclobdell 0:f7c60d3e7b8a 131
maclobdell 0:f7c60d3e7b8a 132 /* 0 corresponds to 0 in GF(2^128) */
maclobdell 0:f7c60d3e7b8a 133 ctx->HH[0] = 0;
maclobdell 0:f7c60d3e7b8a 134 ctx->HL[0] = 0;
maclobdell 0:f7c60d3e7b8a 135
maclobdell 0:f7c60d3e7b8a 136 for( i = 4; i > 0; i >>= 1 )
maclobdell 0:f7c60d3e7b8a 137 {
maclobdell 0:f7c60d3e7b8a 138 uint32_t T = ( vl & 1 ) * 0xe1000000U;
maclobdell 0:f7c60d3e7b8a 139 vl = ( vh << 63 ) | ( vl >> 1 );
maclobdell 0:f7c60d3e7b8a 140 vh = ( vh >> 1 ) ^ ( (uint64_t) T << 32);
maclobdell 0:f7c60d3e7b8a 141
maclobdell 0:f7c60d3e7b8a 142 ctx->HL[i] = vl;
maclobdell 0:f7c60d3e7b8a 143 ctx->HH[i] = vh;
maclobdell 0:f7c60d3e7b8a 144 }
maclobdell 0:f7c60d3e7b8a 145
maclobdell 0:f7c60d3e7b8a 146 for( i = 2; i <= 8; i *= 2 )
maclobdell 0:f7c60d3e7b8a 147 {
maclobdell 0:f7c60d3e7b8a 148 uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i;
maclobdell 0:f7c60d3e7b8a 149 vh = *HiH;
maclobdell 0:f7c60d3e7b8a 150 vl = *HiL;
maclobdell 0:f7c60d3e7b8a 151 for( j = 1; j < i; j++ )
maclobdell 0:f7c60d3e7b8a 152 {
maclobdell 0:f7c60d3e7b8a 153 HiH[j] = vh ^ ctx->HH[j];
maclobdell 0:f7c60d3e7b8a 154 HiL[j] = vl ^ ctx->HL[j];
maclobdell 0:f7c60d3e7b8a 155 }
maclobdell 0:f7c60d3e7b8a 156 }
maclobdell 0:f7c60d3e7b8a 157
maclobdell 0:f7c60d3e7b8a 158 return( 0 );
maclobdell 0:f7c60d3e7b8a 159 }
maclobdell 0:f7c60d3e7b8a 160
maclobdell 0:f7c60d3e7b8a 161 int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
maclobdell 0:f7c60d3e7b8a 162 mbedtls_cipher_id_t cipher,
maclobdell 0:f7c60d3e7b8a 163 const unsigned char *key,
maclobdell 0:f7c60d3e7b8a 164 unsigned int keybits )
maclobdell 0:f7c60d3e7b8a 165 {
maclobdell 0:f7c60d3e7b8a 166 int ret;
maclobdell 0:f7c60d3e7b8a 167 const mbedtls_cipher_info_t *cipher_info;
maclobdell 0:f7c60d3e7b8a 168
maclobdell 0:f7c60d3e7b8a 169 cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB );
maclobdell 0:f7c60d3e7b8a 170 if( cipher_info == NULL )
maclobdell 0:f7c60d3e7b8a 171 return( MBEDTLS_ERR_GCM_BAD_INPUT );
maclobdell 0:f7c60d3e7b8a 172
maclobdell 0:f7c60d3e7b8a 173 if( cipher_info->block_size != 16 )
maclobdell 0:f7c60d3e7b8a 174 return( MBEDTLS_ERR_GCM_BAD_INPUT );
maclobdell 0:f7c60d3e7b8a 175
maclobdell 0:f7c60d3e7b8a 176 mbedtls_cipher_free( &ctx->cipher_ctx );
maclobdell 0:f7c60d3e7b8a 177
maclobdell 0:f7c60d3e7b8a 178 if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 179 return( ret );
maclobdell 0:f7c60d3e7b8a 180
maclobdell 0:f7c60d3e7b8a 181 if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
maclobdell 0:f7c60d3e7b8a 182 MBEDTLS_ENCRYPT ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 183 {
maclobdell 0:f7c60d3e7b8a 184 return( ret );
maclobdell 0:f7c60d3e7b8a 185 }
maclobdell 0:f7c60d3e7b8a 186
maclobdell 0:f7c60d3e7b8a 187 if( ( ret = gcm_gen_table( ctx ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 188 return( ret );
maclobdell 0:f7c60d3e7b8a 189
maclobdell 0:f7c60d3e7b8a 190 return( 0 );
maclobdell 0:f7c60d3e7b8a 191 }
maclobdell 0:f7c60d3e7b8a 192
maclobdell 0:f7c60d3e7b8a 193 /*
maclobdell 0:f7c60d3e7b8a 194 * Shoup's method for multiplication use this table with
maclobdell 0:f7c60d3e7b8a 195 * last4[x] = x times P^128
maclobdell 0:f7c60d3e7b8a 196 * where x and last4[x] are seen as elements of GF(2^128) as in [MGV]
maclobdell 0:f7c60d3e7b8a 197 */
maclobdell 0:f7c60d3e7b8a 198 static const uint64_t last4[16] =
maclobdell 0:f7c60d3e7b8a 199 {
maclobdell 0:f7c60d3e7b8a 200 0x0000, 0x1c20, 0x3840, 0x2460,
maclobdell 0:f7c60d3e7b8a 201 0x7080, 0x6ca0, 0x48c0, 0x54e0,
maclobdell 0:f7c60d3e7b8a 202 0xe100, 0xfd20, 0xd940, 0xc560,
maclobdell 0:f7c60d3e7b8a 203 0x9180, 0x8da0, 0xa9c0, 0xb5e0
maclobdell 0:f7c60d3e7b8a 204 };
maclobdell 0:f7c60d3e7b8a 205
maclobdell 0:f7c60d3e7b8a 206 /*
maclobdell 0:f7c60d3e7b8a 207 * Sets output to x times H using the precomputed tables.
maclobdell 0:f7c60d3e7b8a 208 * x and output are seen as elements of GF(2^128) as in [MGV].
maclobdell 0:f7c60d3e7b8a 209 */
maclobdell 0:f7c60d3e7b8a 210 static void gcm_mult( mbedtls_gcm_context *ctx, const unsigned char x[16],
maclobdell 0:f7c60d3e7b8a 211 unsigned char output[16] )
maclobdell 0:f7c60d3e7b8a 212 {
maclobdell 0:f7c60d3e7b8a 213 int i = 0;
maclobdell 0:f7c60d3e7b8a 214 unsigned char lo, hi, rem;
maclobdell 0:f7c60d3e7b8a 215 uint64_t zh, zl;
maclobdell 0:f7c60d3e7b8a 216
maclobdell 0:f7c60d3e7b8a 217 #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
maclobdell 0:f7c60d3e7b8a 218 if( mbedtls_aesni_has_support( MBEDTLS_AESNI_CLMUL ) ) {
maclobdell 0:f7c60d3e7b8a 219 unsigned char h[16];
maclobdell 0:f7c60d3e7b8a 220
maclobdell 0:f7c60d3e7b8a 221 PUT_UINT32_BE( ctx->HH[8] >> 32, h, 0 );
maclobdell 0:f7c60d3e7b8a 222 PUT_UINT32_BE( ctx->HH[8], h, 4 );
maclobdell 0:f7c60d3e7b8a 223 PUT_UINT32_BE( ctx->HL[8] >> 32, h, 8 );
maclobdell 0:f7c60d3e7b8a 224 PUT_UINT32_BE( ctx->HL[8], h, 12 );
maclobdell 0:f7c60d3e7b8a 225
maclobdell 0:f7c60d3e7b8a 226 mbedtls_aesni_gcm_mult( output, x, h );
maclobdell 0:f7c60d3e7b8a 227 return;
maclobdell 0:f7c60d3e7b8a 228 }
maclobdell 0:f7c60d3e7b8a 229 #endif /* MBEDTLS_AESNI_C && MBEDTLS_HAVE_X86_64 */
maclobdell 0:f7c60d3e7b8a 230
maclobdell 0:f7c60d3e7b8a 231 lo = x[15] & 0xf;
maclobdell 0:f7c60d3e7b8a 232
maclobdell 0:f7c60d3e7b8a 233 zh = ctx->HH[lo];
maclobdell 0:f7c60d3e7b8a 234 zl = ctx->HL[lo];
maclobdell 0:f7c60d3e7b8a 235
maclobdell 0:f7c60d3e7b8a 236 for( i = 15; i >= 0; i-- )
maclobdell 0:f7c60d3e7b8a 237 {
maclobdell 0:f7c60d3e7b8a 238 lo = x[i] & 0xf;
maclobdell 0:f7c60d3e7b8a 239 hi = x[i] >> 4;
maclobdell 0:f7c60d3e7b8a 240
maclobdell 0:f7c60d3e7b8a 241 if( i != 15 )
maclobdell 0:f7c60d3e7b8a 242 {
maclobdell 0:f7c60d3e7b8a 243 rem = (unsigned char) zl & 0xf;
maclobdell 0:f7c60d3e7b8a 244 zl = ( zh << 60 ) | ( zl >> 4 );
maclobdell 0:f7c60d3e7b8a 245 zh = ( zh >> 4 );
maclobdell 0:f7c60d3e7b8a 246 zh ^= (uint64_t) last4[rem] << 48;
maclobdell 0:f7c60d3e7b8a 247 zh ^= ctx->HH[lo];
maclobdell 0:f7c60d3e7b8a 248 zl ^= ctx->HL[lo];
maclobdell 0:f7c60d3e7b8a 249
maclobdell 0:f7c60d3e7b8a 250 }
maclobdell 0:f7c60d3e7b8a 251
maclobdell 0:f7c60d3e7b8a 252 rem = (unsigned char) zl & 0xf;
maclobdell 0:f7c60d3e7b8a 253 zl = ( zh << 60 ) | ( zl >> 4 );
maclobdell 0:f7c60d3e7b8a 254 zh = ( zh >> 4 );
maclobdell 0:f7c60d3e7b8a 255 zh ^= (uint64_t) last4[rem] << 48;
maclobdell 0:f7c60d3e7b8a 256 zh ^= ctx->HH[hi];
maclobdell 0:f7c60d3e7b8a 257 zl ^= ctx->HL[hi];
maclobdell 0:f7c60d3e7b8a 258 }
maclobdell 0:f7c60d3e7b8a 259
maclobdell 0:f7c60d3e7b8a 260 PUT_UINT32_BE( zh >> 32, output, 0 );
maclobdell 0:f7c60d3e7b8a 261 PUT_UINT32_BE( zh, output, 4 );
maclobdell 0:f7c60d3e7b8a 262 PUT_UINT32_BE( zl >> 32, output, 8 );
maclobdell 0:f7c60d3e7b8a 263 PUT_UINT32_BE( zl, output, 12 );
maclobdell 0:f7c60d3e7b8a 264 }
maclobdell 0:f7c60d3e7b8a 265
maclobdell 0:f7c60d3e7b8a 266 int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
maclobdell 0:f7c60d3e7b8a 267 int mode,
maclobdell 0:f7c60d3e7b8a 268 const unsigned char *iv,
maclobdell 0:f7c60d3e7b8a 269 size_t iv_len,
maclobdell 0:f7c60d3e7b8a 270 const unsigned char *add,
maclobdell 0:f7c60d3e7b8a 271 size_t add_len )
maclobdell 0:f7c60d3e7b8a 272 {
maclobdell 0:f7c60d3e7b8a 273 int ret;
maclobdell 0:f7c60d3e7b8a 274 unsigned char work_buf[16];
maclobdell 0:f7c60d3e7b8a 275 size_t i;
maclobdell 0:f7c60d3e7b8a 276 const unsigned char *p;
maclobdell 0:f7c60d3e7b8a 277 size_t use_len, olen = 0;
maclobdell 0:f7c60d3e7b8a 278
maclobdell 0:f7c60d3e7b8a 279 /* IV and AD are limited to 2^64 bits, so 2^61 bytes */
maclobdell 0:f7c60d3e7b8a 280 if( ( (uint64_t) iv_len ) >> 61 != 0 ||
maclobdell 0:f7c60d3e7b8a 281 ( (uint64_t) add_len ) >> 61 != 0 )
maclobdell 0:f7c60d3e7b8a 282 {
maclobdell 0:f7c60d3e7b8a 283 return( MBEDTLS_ERR_GCM_BAD_INPUT );
maclobdell 0:f7c60d3e7b8a 284 }
maclobdell 0:f7c60d3e7b8a 285
maclobdell 0:f7c60d3e7b8a 286 memset( ctx->y, 0x00, sizeof(ctx->y) );
maclobdell 0:f7c60d3e7b8a 287 memset( ctx->buf, 0x00, sizeof(ctx->buf) );
maclobdell 0:f7c60d3e7b8a 288
maclobdell 0:f7c60d3e7b8a 289 ctx->mode = mode;
maclobdell 0:f7c60d3e7b8a 290 ctx->len = 0;
maclobdell 0:f7c60d3e7b8a 291 ctx->add_len = 0;
maclobdell 0:f7c60d3e7b8a 292
maclobdell 0:f7c60d3e7b8a 293 if( iv_len == 12 )
maclobdell 0:f7c60d3e7b8a 294 {
maclobdell 0:f7c60d3e7b8a 295 memcpy( ctx->y, iv, iv_len );
maclobdell 0:f7c60d3e7b8a 296 ctx->y[15] = 1;
maclobdell 0:f7c60d3e7b8a 297 }
maclobdell 0:f7c60d3e7b8a 298 else
maclobdell 0:f7c60d3e7b8a 299 {
maclobdell 0:f7c60d3e7b8a 300 memset( work_buf, 0x00, 16 );
maclobdell 0:f7c60d3e7b8a 301 PUT_UINT32_BE( iv_len * 8, work_buf, 12 );
maclobdell 0:f7c60d3e7b8a 302
maclobdell 0:f7c60d3e7b8a 303 p = iv;
maclobdell 0:f7c60d3e7b8a 304 while( iv_len > 0 )
maclobdell 0:f7c60d3e7b8a 305 {
maclobdell 0:f7c60d3e7b8a 306 use_len = ( iv_len < 16 ) ? iv_len : 16;
maclobdell 0:f7c60d3e7b8a 307
maclobdell 0:f7c60d3e7b8a 308 for( i = 0; i < use_len; i++ )
maclobdell 0:f7c60d3e7b8a 309 ctx->y[i] ^= p[i];
maclobdell 0:f7c60d3e7b8a 310
maclobdell 0:f7c60d3e7b8a 311 gcm_mult( ctx, ctx->y, ctx->y );
maclobdell 0:f7c60d3e7b8a 312
maclobdell 0:f7c60d3e7b8a 313 iv_len -= use_len;
maclobdell 0:f7c60d3e7b8a 314 p += use_len;
maclobdell 0:f7c60d3e7b8a 315 }
maclobdell 0:f7c60d3e7b8a 316
maclobdell 0:f7c60d3e7b8a 317 for( i = 0; i < 16; i++ )
maclobdell 0:f7c60d3e7b8a 318 ctx->y[i] ^= work_buf[i];
maclobdell 0:f7c60d3e7b8a 319
maclobdell 0:f7c60d3e7b8a 320 gcm_mult( ctx, ctx->y, ctx->y );
maclobdell 0:f7c60d3e7b8a 321 }
maclobdell 0:f7c60d3e7b8a 322
maclobdell 0:f7c60d3e7b8a 323 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->base_ectr,
maclobdell 0:f7c60d3e7b8a 324 &olen ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 325 {
maclobdell 0:f7c60d3e7b8a 326 return( ret );
maclobdell 0:f7c60d3e7b8a 327 }
maclobdell 0:f7c60d3e7b8a 328
maclobdell 0:f7c60d3e7b8a 329 ctx->add_len = add_len;
maclobdell 0:f7c60d3e7b8a 330 p = add;
maclobdell 0:f7c60d3e7b8a 331 while( add_len > 0 )
maclobdell 0:f7c60d3e7b8a 332 {
maclobdell 0:f7c60d3e7b8a 333 use_len = ( add_len < 16 ) ? add_len : 16;
maclobdell 0:f7c60d3e7b8a 334
maclobdell 0:f7c60d3e7b8a 335 for( i = 0; i < use_len; i++ )
maclobdell 0:f7c60d3e7b8a 336 ctx->buf[i] ^= p[i];
maclobdell 0:f7c60d3e7b8a 337
maclobdell 0:f7c60d3e7b8a 338 gcm_mult( ctx, ctx->buf, ctx->buf );
maclobdell 0:f7c60d3e7b8a 339
maclobdell 0:f7c60d3e7b8a 340 add_len -= use_len;
maclobdell 0:f7c60d3e7b8a 341 p += use_len;
maclobdell 0:f7c60d3e7b8a 342 }
maclobdell 0:f7c60d3e7b8a 343
maclobdell 0:f7c60d3e7b8a 344 return( 0 );
maclobdell 0:f7c60d3e7b8a 345 }
maclobdell 0:f7c60d3e7b8a 346
maclobdell 0:f7c60d3e7b8a 347 int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
maclobdell 0:f7c60d3e7b8a 348 size_t length,
maclobdell 0:f7c60d3e7b8a 349 const unsigned char *input,
maclobdell 0:f7c60d3e7b8a 350 unsigned char *output )
maclobdell 0:f7c60d3e7b8a 351 {
maclobdell 0:f7c60d3e7b8a 352 int ret;
maclobdell 0:f7c60d3e7b8a 353 unsigned char ectr[16];
maclobdell 0:f7c60d3e7b8a 354 size_t i;
maclobdell 0:f7c60d3e7b8a 355 const unsigned char *p;
maclobdell 0:f7c60d3e7b8a 356 unsigned char *out_p = output;
maclobdell 0:f7c60d3e7b8a 357 size_t use_len, olen = 0;
maclobdell 0:f7c60d3e7b8a 358
maclobdell 0:f7c60d3e7b8a 359 if( output > input && (size_t) ( output - input ) < length )
maclobdell 0:f7c60d3e7b8a 360 return( MBEDTLS_ERR_GCM_BAD_INPUT );
maclobdell 0:f7c60d3e7b8a 361
maclobdell 0:f7c60d3e7b8a 362 /* Total length is restricted to 2^39 - 256 bits, ie 2^36 - 2^5 bytes
maclobdell 0:f7c60d3e7b8a 363 * Also check for possible overflow */
maclobdell 0:f7c60d3e7b8a 364 if( ctx->len + length < ctx->len ||
maclobdell 0:f7c60d3e7b8a 365 (uint64_t) ctx->len + length > 0x03FFFFE0ull )
maclobdell 0:f7c60d3e7b8a 366 {
maclobdell 0:f7c60d3e7b8a 367 return( MBEDTLS_ERR_GCM_BAD_INPUT );
maclobdell 0:f7c60d3e7b8a 368 }
maclobdell 0:f7c60d3e7b8a 369
maclobdell 0:f7c60d3e7b8a 370 ctx->len += length;
maclobdell 0:f7c60d3e7b8a 371
maclobdell 0:f7c60d3e7b8a 372 p = input;
maclobdell 0:f7c60d3e7b8a 373 while( length > 0 )
maclobdell 0:f7c60d3e7b8a 374 {
maclobdell 0:f7c60d3e7b8a 375 use_len = ( length < 16 ) ? length : 16;
maclobdell 0:f7c60d3e7b8a 376
maclobdell 0:f7c60d3e7b8a 377 for( i = 16; i > 12; i-- )
maclobdell 0:f7c60d3e7b8a 378 if( ++ctx->y[i - 1] != 0 )
maclobdell 0:f7c60d3e7b8a 379 break;
maclobdell 0:f7c60d3e7b8a 380
maclobdell 0:f7c60d3e7b8a 381 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ectr,
maclobdell 0:f7c60d3e7b8a 382 &olen ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 383 {
maclobdell 0:f7c60d3e7b8a 384 return( ret );
maclobdell 0:f7c60d3e7b8a 385 }
maclobdell 0:f7c60d3e7b8a 386
maclobdell 0:f7c60d3e7b8a 387 for( i = 0; i < use_len; i++ )
maclobdell 0:f7c60d3e7b8a 388 {
maclobdell 0:f7c60d3e7b8a 389 if( ctx->mode == MBEDTLS_GCM_DECRYPT )
maclobdell 0:f7c60d3e7b8a 390 ctx->buf[i] ^= p[i];
maclobdell 0:f7c60d3e7b8a 391 out_p[i] = ectr[i] ^ p[i];
maclobdell 0:f7c60d3e7b8a 392 if( ctx->mode == MBEDTLS_GCM_ENCRYPT )
maclobdell 0:f7c60d3e7b8a 393 ctx->buf[i] ^= out_p[i];
maclobdell 0:f7c60d3e7b8a 394 }
maclobdell 0:f7c60d3e7b8a 395
maclobdell 0:f7c60d3e7b8a 396 gcm_mult( ctx, ctx->buf, ctx->buf );
maclobdell 0:f7c60d3e7b8a 397
maclobdell 0:f7c60d3e7b8a 398 length -= use_len;
maclobdell 0:f7c60d3e7b8a 399 p += use_len;
maclobdell 0:f7c60d3e7b8a 400 out_p += use_len;
maclobdell 0:f7c60d3e7b8a 401 }
maclobdell 0:f7c60d3e7b8a 402
maclobdell 0:f7c60d3e7b8a 403 return( 0 );
maclobdell 0:f7c60d3e7b8a 404 }
maclobdell 0:f7c60d3e7b8a 405
maclobdell 0:f7c60d3e7b8a 406 int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
maclobdell 0:f7c60d3e7b8a 407 unsigned char *tag,
maclobdell 0:f7c60d3e7b8a 408 size_t tag_len )
maclobdell 0:f7c60d3e7b8a 409 {
maclobdell 0:f7c60d3e7b8a 410 unsigned char work_buf[16];
maclobdell 0:f7c60d3e7b8a 411 size_t i;
maclobdell 0:f7c60d3e7b8a 412 uint64_t orig_len = ctx->len * 8;
maclobdell 0:f7c60d3e7b8a 413 uint64_t orig_add_len = ctx->add_len * 8;
maclobdell 0:f7c60d3e7b8a 414
maclobdell 0:f7c60d3e7b8a 415 if( tag_len > 16 || tag_len < 4 )
maclobdell 0:f7c60d3e7b8a 416 return( MBEDTLS_ERR_GCM_BAD_INPUT );
maclobdell 0:f7c60d3e7b8a 417
maclobdell 0:f7c60d3e7b8a 418 if( tag_len != 0 )
maclobdell 0:f7c60d3e7b8a 419 memcpy( tag, ctx->base_ectr, tag_len );
maclobdell 0:f7c60d3e7b8a 420
maclobdell 0:f7c60d3e7b8a 421 if( orig_len || orig_add_len )
maclobdell 0:f7c60d3e7b8a 422 {
maclobdell 0:f7c60d3e7b8a 423 memset( work_buf, 0x00, 16 );
maclobdell 0:f7c60d3e7b8a 424
maclobdell 0:f7c60d3e7b8a 425 PUT_UINT32_BE( ( orig_add_len >> 32 ), work_buf, 0 );
maclobdell 0:f7c60d3e7b8a 426 PUT_UINT32_BE( ( orig_add_len ), work_buf, 4 );
maclobdell 0:f7c60d3e7b8a 427 PUT_UINT32_BE( ( orig_len >> 32 ), work_buf, 8 );
maclobdell 0:f7c60d3e7b8a 428 PUT_UINT32_BE( ( orig_len ), work_buf, 12 );
maclobdell 0:f7c60d3e7b8a 429
maclobdell 0:f7c60d3e7b8a 430 for( i = 0; i < 16; i++ )
maclobdell 0:f7c60d3e7b8a 431 ctx->buf[i] ^= work_buf[i];
maclobdell 0:f7c60d3e7b8a 432
maclobdell 0:f7c60d3e7b8a 433 gcm_mult( ctx, ctx->buf, ctx->buf );
maclobdell 0:f7c60d3e7b8a 434
maclobdell 0:f7c60d3e7b8a 435 for( i = 0; i < tag_len; i++ )
maclobdell 0:f7c60d3e7b8a 436 tag[i] ^= ctx->buf[i];
maclobdell 0:f7c60d3e7b8a 437 }
maclobdell 0:f7c60d3e7b8a 438
maclobdell 0:f7c60d3e7b8a 439 return( 0 );
maclobdell 0:f7c60d3e7b8a 440 }
maclobdell 0:f7c60d3e7b8a 441
maclobdell 0:f7c60d3e7b8a 442 int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
maclobdell 0:f7c60d3e7b8a 443 int mode,
maclobdell 0:f7c60d3e7b8a 444 size_t length,
maclobdell 0:f7c60d3e7b8a 445 const unsigned char *iv,
maclobdell 0:f7c60d3e7b8a 446 size_t iv_len,
maclobdell 0:f7c60d3e7b8a 447 const unsigned char *add,
maclobdell 0:f7c60d3e7b8a 448 size_t add_len,
maclobdell 0:f7c60d3e7b8a 449 const unsigned char *input,
maclobdell 0:f7c60d3e7b8a 450 unsigned char *output,
maclobdell 0:f7c60d3e7b8a 451 size_t tag_len,
maclobdell 0:f7c60d3e7b8a 452 unsigned char *tag )
maclobdell 0:f7c60d3e7b8a 453 {
maclobdell 0:f7c60d3e7b8a 454 int ret;
maclobdell 0:f7c60d3e7b8a 455
maclobdell 0:f7c60d3e7b8a 456 if( ( ret = mbedtls_gcm_starts( ctx, mode, iv, iv_len, add, add_len ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 457 return( ret );
maclobdell 0:f7c60d3e7b8a 458
maclobdell 0:f7c60d3e7b8a 459 if( ( ret = mbedtls_gcm_update( ctx, length, input, output ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 460 return( ret );
maclobdell 0:f7c60d3e7b8a 461
maclobdell 0:f7c60d3e7b8a 462 if( ( ret = mbedtls_gcm_finish( ctx, tag, tag_len ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 463 return( ret );
maclobdell 0:f7c60d3e7b8a 464
maclobdell 0:f7c60d3e7b8a 465 return( 0 );
maclobdell 0:f7c60d3e7b8a 466 }
maclobdell 0:f7c60d3e7b8a 467
maclobdell 0:f7c60d3e7b8a 468 int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
maclobdell 0:f7c60d3e7b8a 469 size_t length,
maclobdell 0:f7c60d3e7b8a 470 const unsigned char *iv,
maclobdell 0:f7c60d3e7b8a 471 size_t iv_len,
maclobdell 0:f7c60d3e7b8a 472 const unsigned char *add,
maclobdell 0:f7c60d3e7b8a 473 size_t add_len,
maclobdell 0:f7c60d3e7b8a 474 const unsigned char *tag,
maclobdell 0:f7c60d3e7b8a 475 size_t tag_len,
maclobdell 0:f7c60d3e7b8a 476 const unsigned char *input,
maclobdell 0:f7c60d3e7b8a 477 unsigned char *output )
maclobdell 0:f7c60d3e7b8a 478 {
maclobdell 0:f7c60d3e7b8a 479 int ret;
maclobdell 0:f7c60d3e7b8a 480 unsigned char check_tag[16];
maclobdell 0:f7c60d3e7b8a 481 size_t i;
maclobdell 0:f7c60d3e7b8a 482 int diff;
maclobdell 0:f7c60d3e7b8a 483
maclobdell 0:f7c60d3e7b8a 484 if( ( ret = mbedtls_gcm_crypt_and_tag( ctx, MBEDTLS_GCM_DECRYPT, length,
maclobdell 0:f7c60d3e7b8a 485 iv, iv_len, add, add_len,
maclobdell 0:f7c60d3e7b8a 486 input, output, tag_len, check_tag ) ) != 0 )
maclobdell 0:f7c60d3e7b8a 487 {
maclobdell 0:f7c60d3e7b8a 488 return( ret );
maclobdell 0:f7c60d3e7b8a 489 }
maclobdell 0:f7c60d3e7b8a 490
maclobdell 0:f7c60d3e7b8a 491 /* Check tag in "constant-time" */
maclobdell 0:f7c60d3e7b8a 492 for( diff = 0, i = 0; i < tag_len; i++ )
maclobdell 0:f7c60d3e7b8a 493 diff |= tag[i] ^ check_tag[i];
maclobdell 0:f7c60d3e7b8a 494
maclobdell 0:f7c60d3e7b8a 495 if( diff != 0 )
maclobdell 0:f7c60d3e7b8a 496 {
maclobdell 0:f7c60d3e7b8a 497 mbedtls_zeroize( output, length );
maclobdell 0:f7c60d3e7b8a 498 return( MBEDTLS_ERR_GCM_AUTH_FAILED );
maclobdell 0:f7c60d3e7b8a 499 }
maclobdell 0:f7c60d3e7b8a 500
maclobdell 0:f7c60d3e7b8a 501 return( 0 );
maclobdell 0:f7c60d3e7b8a 502 }
maclobdell 0:f7c60d3e7b8a 503
maclobdell 0:f7c60d3e7b8a 504 void mbedtls_gcm_free( mbedtls_gcm_context *ctx )
maclobdell 0:f7c60d3e7b8a 505 {
maclobdell 0:f7c60d3e7b8a 506 mbedtls_cipher_free( &ctx->cipher_ctx );
maclobdell 0:f7c60d3e7b8a 507 mbedtls_zeroize( ctx, sizeof( mbedtls_gcm_context ) );
maclobdell 0:f7c60d3e7b8a 508 }
maclobdell 0:f7c60d3e7b8a 509
maclobdell 0:f7c60d3e7b8a 510 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
maclobdell 0:f7c60d3e7b8a 511 /*
maclobdell 0:f7c60d3e7b8a 512 * AES-GCM test vectors from:
maclobdell 0:f7c60d3e7b8a 513 *
maclobdell 0:f7c60d3e7b8a 514 * http://csrc.nist.gov/groups/STM/cavp/documents/mac/gcmtestvectors.zip
maclobdell 0:f7c60d3e7b8a 515 */
maclobdell 0:f7c60d3e7b8a 516 #define MAX_TESTS 6
maclobdell 0:f7c60d3e7b8a 517
maclobdell 0:f7c60d3e7b8a 518 static const int key_index[MAX_TESTS] =
maclobdell 0:f7c60d3e7b8a 519 { 0, 0, 1, 1, 1, 1 };
maclobdell 0:f7c60d3e7b8a 520
maclobdell 0:f7c60d3e7b8a 521 static const unsigned char key[MAX_TESTS][32] =
maclobdell 0:f7c60d3e7b8a 522 {
maclobdell 0:f7c60d3e7b8a 523 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
maclobdell 0:f7c60d3e7b8a 524 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
maclobdell 0:f7c60d3e7b8a 525 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
maclobdell 0:f7c60d3e7b8a 526 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
maclobdell 0:f7c60d3e7b8a 527 { 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
maclobdell 0:f7c60d3e7b8a 528 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
maclobdell 0:f7c60d3e7b8a 529 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
maclobdell 0:f7c60d3e7b8a 530 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08 },
maclobdell 0:f7c60d3e7b8a 531 };
maclobdell 0:f7c60d3e7b8a 532
maclobdell 0:f7c60d3e7b8a 533 static const size_t iv_len[MAX_TESTS] =
maclobdell 0:f7c60d3e7b8a 534 { 12, 12, 12, 12, 8, 60 };
maclobdell 0:f7c60d3e7b8a 535
maclobdell 0:f7c60d3e7b8a 536 static const int iv_index[MAX_TESTS] =
maclobdell 0:f7c60d3e7b8a 537 { 0, 0, 1, 1, 1, 2 };
maclobdell 0:f7c60d3e7b8a 538
maclobdell 0:f7c60d3e7b8a 539 static const unsigned char iv[MAX_TESTS][64] =
maclobdell 0:f7c60d3e7b8a 540 {
maclobdell 0:f7c60d3e7b8a 541 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
maclobdell 0:f7c60d3e7b8a 542 0x00, 0x00, 0x00, 0x00 },
maclobdell 0:f7c60d3e7b8a 543 { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
maclobdell 0:f7c60d3e7b8a 544 0xde, 0xca, 0xf8, 0x88 },
maclobdell 0:f7c60d3e7b8a 545 { 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
maclobdell 0:f7c60d3e7b8a 546 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
maclobdell 0:f7c60d3e7b8a 547 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
maclobdell 0:f7c60d3e7b8a 548 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
maclobdell 0:f7c60d3e7b8a 549 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
maclobdell 0:f7c60d3e7b8a 550 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
maclobdell 0:f7c60d3e7b8a 551 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
maclobdell 0:f7c60d3e7b8a 552 0xa6, 0x37, 0xb3, 0x9b },
maclobdell 0:f7c60d3e7b8a 553 };
maclobdell 0:f7c60d3e7b8a 554
maclobdell 0:f7c60d3e7b8a 555 static const size_t add_len[MAX_TESTS] =
maclobdell 0:f7c60d3e7b8a 556 { 0, 0, 0, 20, 20, 20 };
maclobdell 0:f7c60d3e7b8a 557
maclobdell 0:f7c60d3e7b8a 558 static const int add_index[MAX_TESTS] =
maclobdell 0:f7c60d3e7b8a 559 { 0, 0, 0, 1, 1, 1 };
maclobdell 0:f7c60d3e7b8a 560
maclobdell 0:f7c60d3e7b8a 561 static const unsigned char additional[MAX_TESTS][64] =
maclobdell 0:f7c60d3e7b8a 562 {
maclobdell 0:f7c60d3e7b8a 563 { 0x00 },
maclobdell 0:f7c60d3e7b8a 564 { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
maclobdell 0:f7c60d3e7b8a 565 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
maclobdell 0:f7c60d3e7b8a 566 0xab, 0xad, 0xda, 0xd2 },
maclobdell 0:f7c60d3e7b8a 567 };
maclobdell 0:f7c60d3e7b8a 568
maclobdell 0:f7c60d3e7b8a 569 static const size_t pt_len[MAX_TESTS] =
maclobdell 0:f7c60d3e7b8a 570 { 0, 16, 64, 60, 60, 60 };
maclobdell 0:f7c60d3e7b8a 571
maclobdell 0:f7c60d3e7b8a 572 static const int pt_index[MAX_TESTS] =
maclobdell 0:f7c60d3e7b8a 573 { 0, 0, 1, 1, 1, 1 };
maclobdell 0:f7c60d3e7b8a 574
maclobdell 0:f7c60d3e7b8a 575 static const unsigned char pt[MAX_TESTS][64] =
maclobdell 0:f7c60d3e7b8a 576 {
maclobdell 0:f7c60d3e7b8a 577 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
maclobdell 0:f7c60d3e7b8a 578 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
maclobdell 0:f7c60d3e7b8a 579 { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
maclobdell 0:f7c60d3e7b8a 580 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
maclobdell 0:f7c60d3e7b8a 581 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
maclobdell 0:f7c60d3e7b8a 582 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
maclobdell 0:f7c60d3e7b8a 583 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
maclobdell 0:f7c60d3e7b8a 584 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
maclobdell 0:f7c60d3e7b8a 585 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
maclobdell 0:f7c60d3e7b8a 586 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55 },
maclobdell 0:f7c60d3e7b8a 587 };
maclobdell 0:f7c60d3e7b8a 588
maclobdell 0:f7c60d3e7b8a 589 static const unsigned char ct[MAX_TESTS * 3][64] =
maclobdell 0:f7c60d3e7b8a 590 {
maclobdell 0:f7c60d3e7b8a 591 { 0x00 },
maclobdell 0:f7c60d3e7b8a 592 { 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92,
maclobdell 0:f7c60d3e7b8a 593 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78 },
maclobdell 0:f7c60d3e7b8a 594 { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
maclobdell 0:f7c60d3e7b8a 595 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
maclobdell 0:f7c60d3e7b8a 596 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
maclobdell 0:f7c60d3e7b8a 597 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
maclobdell 0:f7c60d3e7b8a 598 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
maclobdell 0:f7c60d3e7b8a 599 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
maclobdell 0:f7c60d3e7b8a 600 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
maclobdell 0:f7c60d3e7b8a 601 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85 },
maclobdell 0:f7c60d3e7b8a 602 { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
maclobdell 0:f7c60d3e7b8a 603 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
maclobdell 0:f7c60d3e7b8a 604 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
maclobdell 0:f7c60d3e7b8a 605 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
maclobdell 0:f7c60d3e7b8a 606 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
maclobdell 0:f7c60d3e7b8a 607 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
maclobdell 0:f7c60d3e7b8a 608 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
maclobdell 0:f7c60d3e7b8a 609 0x3d, 0x58, 0xe0, 0x91 },
maclobdell 0:f7c60d3e7b8a 610 { 0x61, 0x35, 0x3b, 0x4c, 0x28, 0x06, 0x93, 0x4a,
maclobdell 0:f7c60d3e7b8a 611 0x77, 0x7f, 0xf5, 0x1f, 0xa2, 0x2a, 0x47, 0x55,
maclobdell 0:f7c60d3e7b8a 612 0x69, 0x9b, 0x2a, 0x71, 0x4f, 0xcd, 0xc6, 0xf8,
maclobdell 0:f7c60d3e7b8a 613 0x37, 0x66, 0xe5, 0xf9, 0x7b, 0x6c, 0x74, 0x23,
maclobdell 0:f7c60d3e7b8a 614 0x73, 0x80, 0x69, 0x00, 0xe4, 0x9f, 0x24, 0xb2,
maclobdell 0:f7c60d3e7b8a 615 0x2b, 0x09, 0x75, 0x44, 0xd4, 0x89, 0x6b, 0x42,
maclobdell 0:f7c60d3e7b8a 616 0x49, 0x89, 0xb5, 0xe1, 0xeb, 0xac, 0x0f, 0x07,
maclobdell 0:f7c60d3e7b8a 617 0xc2, 0x3f, 0x45, 0x98 },
maclobdell 0:f7c60d3e7b8a 618 { 0x8c, 0xe2, 0x49, 0x98, 0x62, 0x56, 0x15, 0xb6,
maclobdell 0:f7c60d3e7b8a 619 0x03, 0xa0, 0x33, 0xac, 0xa1, 0x3f, 0xb8, 0x94,
maclobdell 0:f7c60d3e7b8a 620 0xbe, 0x91, 0x12, 0xa5, 0xc3, 0xa2, 0x11, 0xa8,
maclobdell 0:f7c60d3e7b8a 621 0xba, 0x26, 0x2a, 0x3c, 0xca, 0x7e, 0x2c, 0xa7,
maclobdell 0:f7c60d3e7b8a 622 0x01, 0xe4, 0xa9, 0xa4, 0xfb, 0xa4, 0x3c, 0x90,
maclobdell 0:f7c60d3e7b8a 623 0xcc, 0xdc, 0xb2, 0x81, 0xd4, 0x8c, 0x7c, 0x6f,
maclobdell 0:f7c60d3e7b8a 624 0xd6, 0x28, 0x75, 0xd2, 0xac, 0xa4, 0x17, 0x03,
maclobdell 0:f7c60d3e7b8a 625 0x4c, 0x34, 0xae, 0xe5 },
maclobdell 0:f7c60d3e7b8a 626 { 0x00 },
maclobdell 0:f7c60d3e7b8a 627 { 0x98, 0xe7, 0x24, 0x7c, 0x07, 0xf0, 0xfe, 0x41,
maclobdell 0:f7c60d3e7b8a 628 0x1c, 0x26, 0x7e, 0x43, 0x84, 0xb0, 0xf6, 0x00 },
maclobdell 0:f7c60d3e7b8a 629 { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41,
maclobdell 0:f7c60d3e7b8a 630 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57,
maclobdell 0:f7c60d3e7b8a 631 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84,
maclobdell 0:f7c60d3e7b8a 632 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c,
maclobdell 0:f7c60d3e7b8a 633 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25,
maclobdell 0:f7c60d3e7b8a 634 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47,
maclobdell 0:f7c60d3e7b8a 635 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9,
maclobdell 0:f7c60d3e7b8a 636 0xcc, 0xda, 0x27, 0x10, 0xac, 0xad, 0xe2, 0x56 },
maclobdell 0:f7c60d3e7b8a 637 { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41,
maclobdell 0:f7c60d3e7b8a 638 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57,
maclobdell 0:f7c60d3e7b8a 639 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84,
maclobdell 0:f7c60d3e7b8a 640 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c,
maclobdell 0:f7c60d3e7b8a 641 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25,
maclobdell 0:f7c60d3e7b8a 642 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47,
maclobdell 0:f7c60d3e7b8a 643 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9,
maclobdell 0:f7c60d3e7b8a 644 0xcc, 0xda, 0x27, 0x10 },
maclobdell 0:f7c60d3e7b8a 645 { 0x0f, 0x10, 0xf5, 0x99, 0xae, 0x14, 0xa1, 0x54,
maclobdell 0:f7c60d3e7b8a 646 0xed, 0x24, 0xb3, 0x6e, 0x25, 0x32, 0x4d, 0xb8,
maclobdell 0:f7c60d3e7b8a 647 0xc5, 0x66, 0x63, 0x2e, 0xf2, 0xbb, 0xb3, 0x4f,
maclobdell 0:f7c60d3e7b8a 648 0x83, 0x47, 0x28, 0x0f, 0xc4, 0x50, 0x70, 0x57,
maclobdell 0:f7c60d3e7b8a 649 0xfd, 0xdc, 0x29, 0xdf, 0x9a, 0x47, 0x1f, 0x75,
maclobdell 0:f7c60d3e7b8a 650 0xc6, 0x65, 0x41, 0xd4, 0xd4, 0xda, 0xd1, 0xc9,
maclobdell 0:f7c60d3e7b8a 651 0xe9, 0x3a, 0x19, 0xa5, 0x8e, 0x8b, 0x47, 0x3f,
maclobdell 0:f7c60d3e7b8a 652 0xa0, 0xf0, 0x62, 0xf7 },
maclobdell 0:f7c60d3e7b8a 653 { 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
maclobdell 0:f7c60d3e7b8a 654 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
maclobdell 0:f7c60d3e7b8a 655 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
maclobdell 0:f7c60d3e7b8a 656 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
maclobdell 0:f7c60d3e7b8a 657 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
maclobdell 0:f7c60d3e7b8a 658 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
maclobdell 0:f7c60d3e7b8a 659 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
maclobdell 0:f7c60d3e7b8a 660 0xe9, 0xb7, 0x37, 0x3b },
maclobdell 0:f7c60d3e7b8a 661 { 0x00 },
maclobdell 0:f7c60d3e7b8a 662 { 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e,
maclobdell 0:f7c60d3e7b8a 663 0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18 },
maclobdell 0:f7c60d3e7b8a 664 { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
maclobdell 0:f7c60d3e7b8a 665 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
maclobdell 0:f7c60d3e7b8a 666 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
maclobdell 0:f7c60d3e7b8a 667 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
maclobdell 0:f7c60d3e7b8a 668 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
maclobdell 0:f7c60d3e7b8a 669 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
maclobdell 0:f7c60d3e7b8a 670 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
maclobdell 0:f7c60d3e7b8a 671 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad },
maclobdell 0:f7c60d3e7b8a 672 { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
maclobdell 0:f7c60d3e7b8a 673 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
maclobdell 0:f7c60d3e7b8a 674 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
maclobdell 0:f7c60d3e7b8a 675 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
maclobdell 0:f7c60d3e7b8a 676 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
maclobdell 0:f7c60d3e7b8a 677 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
maclobdell 0:f7c60d3e7b8a 678 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
maclobdell 0:f7c60d3e7b8a 679 0xbc, 0xc9, 0xf6, 0x62 },
maclobdell 0:f7c60d3e7b8a 680 { 0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32,
maclobdell 0:f7c60d3e7b8a 681 0xae, 0x47, 0xc1, 0x3b, 0xf1, 0x98, 0x44, 0xcb,
maclobdell 0:f7c60d3e7b8a 682 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa,
maclobdell 0:f7c60d3e7b8a 683 0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0,
maclobdell 0:f7c60d3e7b8a 684 0xfe, 0xb5, 0x82, 0xd3, 0x39, 0x34, 0xa4, 0xf0,
maclobdell 0:f7c60d3e7b8a 685 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78,
maclobdell 0:f7c60d3e7b8a 686 0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99,
maclobdell 0:f7c60d3e7b8a 687 0xf4, 0x7c, 0x9b, 0x1f },
maclobdell 0:f7c60d3e7b8a 688 { 0x5a, 0x8d, 0xef, 0x2f, 0x0c, 0x9e, 0x53, 0xf1,
maclobdell 0:f7c60d3e7b8a 689 0xf7, 0x5d, 0x78, 0x53, 0x65, 0x9e, 0x2a, 0x20,
maclobdell 0:f7c60d3e7b8a 690 0xee, 0xb2, 0xb2, 0x2a, 0xaf, 0xde, 0x64, 0x19,
maclobdell 0:f7c60d3e7b8a 691 0xa0, 0x58, 0xab, 0x4f, 0x6f, 0x74, 0x6b, 0xf4,
maclobdell 0:f7c60d3e7b8a 692 0x0f, 0xc0, 0xc3, 0xb7, 0x80, 0xf2, 0x44, 0x45,
maclobdell 0:f7c60d3e7b8a 693 0x2d, 0xa3, 0xeb, 0xf1, 0xc5, 0xd8, 0x2c, 0xde,
maclobdell 0:f7c60d3e7b8a 694 0xa2, 0x41, 0x89, 0x97, 0x20, 0x0e, 0xf8, 0x2e,
maclobdell 0:f7c60d3e7b8a 695 0x44, 0xae, 0x7e, 0x3f },
maclobdell 0:f7c60d3e7b8a 696 };
maclobdell 0:f7c60d3e7b8a 697
maclobdell 0:f7c60d3e7b8a 698 static const unsigned char tag[MAX_TESTS * 3][16] =
maclobdell 0:f7c60d3e7b8a 699 {
maclobdell 0:f7c60d3e7b8a 700 { 0x58, 0xe2, 0xfc, 0xce, 0xfa, 0x7e, 0x30, 0x61,
maclobdell 0:f7c60d3e7b8a 701 0x36, 0x7f, 0x1d, 0x57, 0xa4, 0xe7, 0x45, 0x5a },
maclobdell 0:f7c60d3e7b8a 702 { 0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec, 0x13, 0xbd,
maclobdell 0:f7c60d3e7b8a 703 0xf5, 0x3a, 0x67, 0xb2, 0x12, 0x57, 0xbd, 0xdf },
maclobdell 0:f7c60d3e7b8a 704 { 0x4d, 0x5c, 0x2a, 0xf3, 0x27, 0xcd, 0x64, 0xa6,
maclobdell 0:f7c60d3e7b8a 705 0x2c, 0xf3, 0x5a, 0xbd, 0x2b, 0xa6, 0xfa, 0xb4 },
maclobdell 0:f7c60d3e7b8a 706 { 0x5b, 0xc9, 0x4f, 0xbc, 0x32, 0x21, 0xa5, 0xdb,
maclobdell 0:f7c60d3e7b8a 707 0x94, 0xfa, 0xe9, 0x5a, 0xe7, 0x12, 0x1a, 0x47 },
maclobdell 0:f7c60d3e7b8a 708 { 0x36, 0x12, 0xd2, 0xe7, 0x9e, 0x3b, 0x07, 0x85,
maclobdell 0:f7c60d3e7b8a 709 0x56, 0x1b, 0xe1, 0x4a, 0xac, 0xa2, 0xfc, 0xcb },
maclobdell 0:f7c60d3e7b8a 710 { 0x61, 0x9c, 0xc5, 0xae, 0xff, 0xfe, 0x0b, 0xfa,
maclobdell 0:f7c60d3e7b8a 711 0x46, 0x2a, 0xf4, 0x3c, 0x16, 0x99, 0xd0, 0x50 },
maclobdell 0:f7c60d3e7b8a 712 { 0xcd, 0x33, 0xb2, 0x8a, 0xc7, 0x73, 0xf7, 0x4b,
maclobdell 0:f7c60d3e7b8a 713 0xa0, 0x0e, 0xd1, 0xf3, 0x12, 0x57, 0x24, 0x35 },
maclobdell 0:f7c60d3e7b8a 714 { 0x2f, 0xf5, 0x8d, 0x80, 0x03, 0x39, 0x27, 0xab,
maclobdell 0:f7c60d3e7b8a 715 0x8e, 0xf4, 0xd4, 0x58, 0x75, 0x14, 0xf0, 0xfb },
maclobdell 0:f7c60d3e7b8a 716 { 0x99, 0x24, 0xa7, 0xc8, 0x58, 0x73, 0x36, 0xbf,
maclobdell 0:f7c60d3e7b8a 717 0xb1, 0x18, 0x02, 0x4d, 0xb8, 0x67, 0x4a, 0x14 },
maclobdell 0:f7c60d3e7b8a 718 { 0x25, 0x19, 0x49, 0x8e, 0x80, 0xf1, 0x47, 0x8f,
maclobdell 0:f7c60d3e7b8a 719 0x37, 0xba, 0x55, 0xbd, 0x6d, 0x27, 0x61, 0x8c },
maclobdell 0:f7c60d3e7b8a 720 { 0x65, 0xdc, 0xc5, 0x7f, 0xcf, 0x62, 0x3a, 0x24,
maclobdell 0:f7c60d3e7b8a 721 0x09, 0x4f, 0xcc, 0xa4, 0x0d, 0x35, 0x33, 0xf8 },
maclobdell 0:f7c60d3e7b8a 722 { 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
maclobdell 0:f7c60d3e7b8a 723 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9 },
maclobdell 0:f7c60d3e7b8a 724 { 0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9,
maclobdell 0:f7c60d3e7b8a 725 0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b },
maclobdell 0:f7c60d3e7b8a 726 { 0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0,
maclobdell 0:f7c60d3e7b8a 727 0x26, 0x5b, 0x98, 0xb5, 0xd4, 0x8a, 0xb9, 0x19 },
maclobdell 0:f7c60d3e7b8a 728 { 0xb0, 0x94, 0xda, 0xc5, 0xd9, 0x34, 0x71, 0xbd,
maclobdell 0:f7c60d3e7b8a 729 0xec, 0x1a, 0x50, 0x22, 0x70, 0xe3, 0xcc, 0x6c },
maclobdell 0:f7c60d3e7b8a 730 { 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
maclobdell 0:f7c60d3e7b8a 731 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b },
maclobdell 0:f7c60d3e7b8a 732 { 0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4,
maclobdell 0:f7c60d3e7b8a 733 0x5e, 0x45, 0x49, 0x13, 0xfe, 0x2e, 0xa8, 0xf2 },
maclobdell 0:f7c60d3e7b8a 734 { 0xa4, 0x4a, 0x82, 0x66, 0xee, 0x1c, 0x8e, 0xb0,
maclobdell 0:f7c60d3e7b8a 735 0xc8, 0xb5, 0xd4, 0xcf, 0x5a, 0xe9, 0xf1, 0x9a },
maclobdell 0:f7c60d3e7b8a 736 };
maclobdell 0:f7c60d3e7b8a 737
maclobdell 0:f7c60d3e7b8a 738 int mbedtls_gcm_self_test( int verbose )
maclobdell 0:f7c60d3e7b8a 739 {
maclobdell 0:f7c60d3e7b8a 740 mbedtls_gcm_context ctx;
maclobdell 0:f7c60d3e7b8a 741 unsigned char buf[64];
maclobdell 0:f7c60d3e7b8a 742 unsigned char tag_buf[16];
maclobdell 0:f7c60d3e7b8a 743 int i, j, ret;
maclobdell 0:f7c60d3e7b8a 744 mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
maclobdell 0:f7c60d3e7b8a 745
maclobdell 0:f7c60d3e7b8a 746 mbedtls_gcm_init( &ctx );
maclobdell 0:f7c60d3e7b8a 747
maclobdell 0:f7c60d3e7b8a 748 for( j = 0; j < 3; j++ )
maclobdell 0:f7c60d3e7b8a 749 {
maclobdell 0:f7c60d3e7b8a 750 int key_len = 128 + 64 * j;
maclobdell 0:f7c60d3e7b8a 751
maclobdell 0:f7c60d3e7b8a 752 for( i = 0; i < MAX_TESTS; i++ )
maclobdell 0:f7c60d3e7b8a 753 {
maclobdell 0:f7c60d3e7b8a 754 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 755 mbedtls_printf( " AES-GCM-%3d #%d (%s): ",
maclobdell 0:f7c60d3e7b8a 756 key_len, i, "enc" );
maclobdell 0:f7c60d3e7b8a 757
maclobdell 0:f7c60d3e7b8a 758 mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]], key_len );
maclobdell 0:f7c60d3e7b8a 759
maclobdell 0:f7c60d3e7b8a 760 ret = mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_ENCRYPT,
maclobdell 0:f7c60d3e7b8a 761 pt_len[i],
maclobdell 0:f7c60d3e7b8a 762 iv[iv_index[i]], iv_len[i],
maclobdell 0:f7c60d3e7b8a 763 additional[add_index[i]], add_len[i],
maclobdell 0:f7c60d3e7b8a 764 pt[pt_index[i]], buf, 16, tag_buf );
maclobdell 0:f7c60d3e7b8a 765
maclobdell 0:f7c60d3e7b8a 766 if( ret != 0 ||
maclobdell 0:f7c60d3e7b8a 767 memcmp( buf, ct[j * 6 + i], pt_len[i] ) != 0 ||
maclobdell 0:f7c60d3e7b8a 768 memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
maclobdell 0:f7c60d3e7b8a 769 {
maclobdell 0:f7c60d3e7b8a 770 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 771 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 772
maclobdell 0:f7c60d3e7b8a 773 return( 1 );
maclobdell 0:f7c60d3e7b8a 774 }
maclobdell 0:f7c60d3e7b8a 775
maclobdell 0:f7c60d3e7b8a 776 mbedtls_gcm_free( &ctx );
maclobdell 0:f7c60d3e7b8a 777
maclobdell 0:f7c60d3e7b8a 778 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 779 mbedtls_printf( "passed\n" );
maclobdell 0:f7c60d3e7b8a 780
maclobdell 0:f7c60d3e7b8a 781 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 782 mbedtls_printf( " AES-GCM-%3d #%d (%s): ",
maclobdell 0:f7c60d3e7b8a 783 key_len, i, "dec" );
maclobdell 0:f7c60d3e7b8a 784
maclobdell 0:f7c60d3e7b8a 785 mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]], key_len );
maclobdell 0:f7c60d3e7b8a 786
maclobdell 0:f7c60d3e7b8a 787 ret = mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_DECRYPT,
maclobdell 0:f7c60d3e7b8a 788 pt_len[i],
maclobdell 0:f7c60d3e7b8a 789 iv[iv_index[i]], iv_len[i],
maclobdell 0:f7c60d3e7b8a 790 additional[add_index[i]], add_len[i],
maclobdell 0:f7c60d3e7b8a 791 ct[j * 6 + i], buf, 16, tag_buf );
maclobdell 0:f7c60d3e7b8a 792
maclobdell 0:f7c60d3e7b8a 793 if( ret != 0 ||
maclobdell 0:f7c60d3e7b8a 794 memcmp( buf, pt[pt_index[i]], pt_len[i] ) != 0 ||
maclobdell 0:f7c60d3e7b8a 795 memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
maclobdell 0:f7c60d3e7b8a 796 {
maclobdell 0:f7c60d3e7b8a 797 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 798 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 799
maclobdell 0:f7c60d3e7b8a 800 return( 1 );
maclobdell 0:f7c60d3e7b8a 801 }
maclobdell 0:f7c60d3e7b8a 802
maclobdell 0:f7c60d3e7b8a 803 mbedtls_gcm_free( &ctx );
maclobdell 0:f7c60d3e7b8a 804
maclobdell 0:f7c60d3e7b8a 805 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 806 mbedtls_printf( "passed\n" );
maclobdell 0:f7c60d3e7b8a 807
maclobdell 0:f7c60d3e7b8a 808 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 809 mbedtls_printf( " AES-GCM-%3d #%d split (%s): ",
maclobdell 0:f7c60d3e7b8a 810 key_len, i, "enc" );
maclobdell 0:f7c60d3e7b8a 811
maclobdell 0:f7c60d3e7b8a 812 mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]], key_len );
maclobdell 0:f7c60d3e7b8a 813
maclobdell 0:f7c60d3e7b8a 814 ret = mbedtls_gcm_starts( &ctx, MBEDTLS_GCM_ENCRYPT,
maclobdell 0:f7c60d3e7b8a 815 iv[iv_index[i]], iv_len[i],
maclobdell 0:f7c60d3e7b8a 816 additional[add_index[i]], add_len[i] );
maclobdell 0:f7c60d3e7b8a 817 if( ret != 0 )
maclobdell 0:f7c60d3e7b8a 818 {
maclobdell 0:f7c60d3e7b8a 819 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 820 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 821
maclobdell 0:f7c60d3e7b8a 822 return( 1 );
maclobdell 0:f7c60d3e7b8a 823 }
maclobdell 0:f7c60d3e7b8a 824
maclobdell 0:f7c60d3e7b8a 825 if( pt_len[i] > 32 )
maclobdell 0:f7c60d3e7b8a 826 {
maclobdell 0:f7c60d3e7b8a 827 size_t rest_len = pt_len[i] - 32;
maclobdell 0:f7c60d3e7b8a 828 ret = mbedtls_gcm_update( &ctx, 32, pt[pt_index[i]], buf );
maclobdell 0:f7c60d3e7b8a 829 if( ret != 0 )
maclobdell 0:f7c60d3e7b8a 830 {
maclobdell 0:f7c60d3e7b8a 831 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 832 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 833
maclobdell 0:f7c60d3e7b8a 834 return( 1 );
maclobdell 0:f7c60d3e7b8a 835 }
maclobdell 0:f7c60d3e7b8a 836
maclobdell 0:f7c60d3e7b8a 837 ret = mbedtls_gcm_update( &ctx, rest_len, pt[pt_index[i]] + 32,
maclobdell 0:f7c60d3e7b8a 838 buf + 32 );
maclobdell 0:f7c60d3e7b8a 839 if( ret != 0 )
maclobdell 0:f7c60d3e7b8a 840 {
maclobdell 0:f7c60d3e7b8a 841 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 842 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 843
maclobdell 0:f7c60d3e7b8a 844 return( 1 );
maclobdell 0:f7c60d3e7b8a 845 }
maclobdell 0:f7c60d3e7b8a 846 }
maclobdell 0:f7c60d3e7b8a 847 else
maclobdell 0:f7c60d3e7b8a 848 {
maclobdell 0:f7c60d3e7b8a 849 ret = mbedtls_gcm_update( &ctx, pt_len[i], pt[pt_index[i]], buf );
maclobdell 0:f7c60d3e7b8a 850 if( ret != 0 )
maclobdell 0:f7c60d3e7b8a 851 {
maclobdell 0:f7c60d3e7b8a 852 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 853 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 854
maclobdell 0:f7c60d3e7b8a 855 return( 1 );
maclobdell 0:f7c60d3e7b8a 856 }
maclobdell 0:f7c60d3e7b8a 857 }
maclobdell 0:f7c60d3e7b8a 858
maclobdell 0:f7c60d3e7b8a 859 ret = mbedtls_gcm_finish( &ctx, tag_buf, 16 );
maclobdell 0:f7c60d3e7b8a 860 if( ret != 0 ||
maclobdell 0:f7c60d3e7b8a 861 memcmp( buf, ct[j * 6 + i], pt_len[i] ) != 0 ||
maclobdell 0:f7c60d3e7b8a 862 memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
maclobdell 0:f7c60d3e7b8a 863 {
maclobdell 0:f7c60d3e7b8a 864 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 865 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 866
maclobdell 0:f7c60d3e7b8a 867 return( 1 );
maclobdell 0:f7c60d3e7b8a 868 }
maclobdell 0:f7c60d3e7b8a 869
maclobdell 0:f7c60d3e7b8a 870 mbedtls_gcm_free( &ctx );
maclobdell 0:f7c60d3e7b8a 871
maclobdell 0:f7c60d3e7b8a 872 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 873 mbedtls_printf( "passed\n" );
maclobdell 0:f7c60d3e7b8a 874
maclobdell 0:f7c60d3e7b8a 875 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 876 mbedtls_printf( " AES-GCM-%3d #%d split (%s): ",
maclobdell 0:f7c60d3e7b8a 877 key_len, i, "dec" );
maclobdell 0:f7c60d3e7b8a 878
maclobdell 0:f7c60d3e7b8a 879 mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]], key_len );
maclobdell 0:f7c60d3e7b8a 880
maclobdell 0:f7c60d3e7b8a 881 ret = mbedtls_gcm_starts( &ctx, MBEDTLS_GCM_DECRYPT,
maclobdell 0:f7c60d3e7b8a 882 iv[iv_index[i]], iv_len[i],
maclobdell 0:f7c60d3e7b8a 883 additional[add_index[i]], add_len[i] );
maclobdell 0:f7c60d3e7b8a 884 if( ret != 0 )
maclobdell 0:f7c60d3e7b8a 885 {
maclobdell 0:f7c60d3e7b8a 886 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 887 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 888
maclobdell 0:f7c60d3e7b8a 889 return( 1 );
maclobdell 0:f7c60d3e7b8a 890 }
maclobdell 0:f7c60d3e7b8a 891
maclobdell 0:f7c60d3e7b8a 892 if( pt_len[i] > 32 )
maclobdell 0:f7c60d3e7b8a 893 {
maclobdell 0:f7c60d3e7b8a 894 size_t rest_len = pt_len[i] - 32;
maclobdell 0:f7c60d3e7b8a 895 ret = mbedtls_gcm_update( &ctx, 32, ct[j * 6 + i], buf );
maclobdell 0:f7c60d3e7b8a 896 if( ret != 0 )
maclobdell 0:f7c60d3e7b8a 897 {
maclobdell 0:f7c60d3e7b8a 898 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 899 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 900
maclobdell 0:f7c60d3e7b8a 901 return( 1 );
maclobdell 0:f7c60d3e7b8a 902 }
maclobdell 0:f7c60d3e7b8a 903
maclobdell 0:f7c60d3e7b8a 904 ret = mbedtls_gcm_update( &ctx, rest_len, ct[j * 6 + i] + 32,
maclobdell 0:f7c60d3e7b8a 905 buf + 32 );
maclobdell 0:f7c60d3e7b8a 906 if( ret != 0 )
maclobdell 0:f7c60d3e7b8a 907 {
maclobdell 0:f7c60d3e7b8a 908 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 909 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 910
maclobdell 0:f7c60d3e7b8a 911 return( 1 );
maclobdell 0:f7c60d3e7b8a 912 }
maclobdell 0:f7c60d3e7b8a 913 }
maclobdell 0:f7c60d3e7b8a 914 else
maclobdell 0:f7c60d3e7b8a 915 {
maclobdell 0:f7c60d3e7b8a 916 ret = mbedtls_gcm_update( &ctx, pt_len[i], ct[j * 6 + i], buf );
maclobdell 0:f7c60d3e7b8a 917 if( ret != 0 )
maclobdell 0:f7c60d3e7b8a 918 {
maclobdell 0:f7c60d3e7b8a 919 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 920 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 921
maclobdell 0:f7c60d3e7b8a 922 return( 1 );
maclobdell 0:f7c60d3e7b8a 923 }
maclobdell 0:f7c60d3e7b8a 924 }
maclobdell 0:f7c60d3e7b8a 925
maclobdell 0:f7c60d3e7b8a 926 ret = mbedtls_gcm_finish( &ctx, tag_buf, 16 );
maclobdell 0:f7c60d3e7b8a 927 if( ret != 0 ||
maclobdell 0:f7c60d3e7b8a 928 memcmp( buf, pt[pt_index[i]], pt_len[i] ) != 0 ||
maclobdell 0:f7c60d3e7b8a 929 memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
maclobdell 0:f7c60d3e7b8a 930 {
maclobdell 0:f7c60d3e7b8a 931 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 932 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 933
maclobdell 0:f7c60d3e7b8a 934 return( 1 );
maclobdell 0:f7c60d3e7b8a 935 }
maclobdell 0:f7c60d3e7b8a 936
maclobdell 0:f7c60d3e7b8a 937 mbedtls_gcm_free( &ctx );
maclobdell 0:f7c60d3e7b8a 938
maclobdell 0:f7c60d3e7b8a 939 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 940 mbedtls_printf( "passed\n" );
maclobdell 0:f7c60d3e7b8a 941
maclobdell 0:f7c60d3e7b8a 942 }
maclobdell 0:f7c60d3e7b8a 943 }
maclobdell 0:f7c60d3e7b8a 944
maclobdell 0:f7c60d3e7b8a 945 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 946 mbedtls_printf( "\n" );
maclobdell 0:f7c60d3e7b8a 947
maclobdell 0:f7c60d3e7b8a 948 return( 0 );
maclobdell 0:f7c60d3e7b8a 949 }
maclobdell 0:f7c60d3e7b8a 950
maclobdell 0:f7c60d3e7b8a 951 #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
maclobdell 0:f7c60d3e7b8a 952
maclobdell 0:f7c60d3e7b8a 953 #endif /* MBEDTLS_GCM_C */