mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

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