1817466cbSJens Wiklander /* 2817466cbSJens Wiklander * NIST SP800-38D compliant GCM implementation 3817466cbSJens Wiklander * 4*7901324dSJerome Forissier * Copyright The Mbed TLS Contributors 5*7901324dSJerome Forissier * SPDX-License-Identifier: Apache-2.0 6817466cbSJens Wiklander * 7817466cbSJens Wiklander * Licensed under the Apache License, Version 2.0 (the "License"); you may 8817466cbSJens Wiklander * not use this file except in compliance with the License. 9817466cbSJens Wiklander * You may obtain a copy of the License at 10817466cbSJens Wiklander * 11817466cbSJens Wiklander * http://www.apache.org/licenses/LICENSE-2.0 12817466cbSJens Wiklander * 13817466cbSJens Wiklander * Unless required by applicable law or agreed to in writing, software 14817466cbSJens Wiklander * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15817466cbSJens Wiklander * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16817466cbSJens Wiklander * See the License for the specific language governing permissions and 17817466cbSJens Wiklander * limitations under the License. 18817466cbSJens Wiklander */ 19817466cbSJens Wiklander 20817466cbSJens Wiklander /* 21817466cbSJens Wiklander * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf 22817466cbSJens Wiklander * 23817466cbSJens Wiklander * See also: 24817466cbSJens Wiklander * [MGV] http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf 25817466cbSJens Wiklander * 26817466cbSJens Wiklander * We use the algorithm described as Shoup's method with 4-bit tables in 27817466cbSJens Wiklander * [MGV] 4.1, pp. 12-13, to enhance speed without using too much memory. 28817466cbSJens Wiklander */ 29817466cbSJens Wiklander 30*7901324dSJerome Forissier #include "common.h" 31817466cbSJens Wiklander 32817466cbSJens Wiklander #if defined(MBEDTLS_GCM_C) 33817466cbSJens Wiklander 34817466cbSJens Wiklander #include "mbedtls/gcm.h" 353d3b0591SJens Wiklander #include "mbedtls/platform_util.h" 3611fa71b9SJerome Forissier #include "mbedtls/error.h" 37817466cbSJens Wiklander 38817466cbSJens Wiklander #include <string.h> 39817466cbSJens Wiklander 40817466cbSJens Wiklander #if defined(MBEDTLS_AESNI_C) 41817466cbSJens Wiklander #include "mbedtls/aesni.h" 42817466cbSJens Wiklander #endif 43817466cbSJens Wiklander 44817466cbSJens Wiklander #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) 453d3b0591SJens Wiklander #include "mbedtls/aes.h" 46817466cbSJens Wiklander #include "mbedtls/platform.h" 473d3b0591SJens Wiklander #if !defined(MBEDTLS_PLATFORM_C) 48817466cbSJens Wiklander #include <stdio.h> 49817466cbSJens Wiklander #define mbedtls_printf printf 50817466cbSJens Wiklander #endif /* MBEDTLS_PLATFORM_C */ 51817466cbSJens Wiklander #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ 52817466cbSJens Wiklander 533d3b0591SJens Wiklander #if !defined(MBEDTLS_GCM_ALT) 543d3b0591SJens Wiklander 553d3b0591SJens Wiklander /* Parameter validation macros */ 563d3b0591SJens Wiklander #define GCM_VALIDATE_RET( cond ) \ 573d3b0591SJens Wiklander MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_GCM_BAD_INPUT ) 583d3b0591SJens Wiklander #define GCM_VALIDATE( cond ) \ 593d3b0591SJens Wiklander MBEDTLS_INTERNAL_VALIDATE( cond ) 603d3b0591SJens Wiklander 61817466cbSJens Wiklander /* 62817466cbSJens Wiklander * 32-bit integer manipulation macros (big endian) 63817466cbSJens Wiklander */ 64817466cbSJens Wiklander #ifndef GET_UINT32_BE 65817466cbSJens Wiklander #define GET_UINT32_BE(n,b,i) \ 66817466cbSJens Wiklander { \ 67817466cbSJens Wiklander (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ 68817466cbSJens Wiklander | ( (uint32_t) (b)[(i) + 1] << 16 ) \ 69817466cbSJens Wiklander | ( (uint32_t) (b)[(i) + 2] << 8 ) \ 70817466cbSJens Wiklander | ( (uint32_t) (b)[(i) + 3] ); \ 71817466cbSJens Wiklander } 72817466cbSJens Wiklander #endif 73817466cbSJens Wiklander 74817466cbSJens Wiklander #ifndef PUT_UINT32_BE 75817466cbSJens Wiklander #define PUT_UINT32_BE(n,b,i) \ 76817466cbSJens Wiklander { \ 77817466cbSJens Wiklander (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 78817466cbSJens Wiklander (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 79817466cbSJens Wiklander (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 80817466cbSJens Wiklander (b)[(i) + 3] = (unsigned char) ( (n) ); \ 81817466cbSJens Wiklander } 82817466cbSJens Wiklander #endif 83817466cbSJens Wiklander 84817466cbSJens Wiklander /* 85817466cbSJens Wiklander * Initialize a context 86817466cbSJens Wiklander */ 87817466cbSJens Wiklander void mbedtls_gcm_init( mbedtls_gcm_context *ctx ) 88817466cbSJens Wiklander { 893d3b0591SJens Wiklander GCM_VALIDATE( ctx != NULL ); 90817466cbSJens Wiklander memset( ctx, 0, sizeof( mbedtls_gcm_context ) ); 91817466cbSJens Wiklander } 92817466cbSJens Wiklander 93817466cbSJens Wiklander /* 94817466cbSJens Wiklander * Precompute small multiples of H, that is set 95817466cbSJens Wiklander * HH[i] || HL[i] = H times i, 96817466cbSJens Wiklander * where i is seen as a field element as in [MGV], ie high-order bits 97817466cbSJens Wiklander * correspond to low powers of P. The result is stored in the same way, that 98817466cbSJens Wiklander * is the high-order bit of HH corresponds to P^0 and the low-order bit of HL 99817466cbSJens Wiklander * corresponds to P^127. 100817466cbSJens Wiklander */ 101817466cbSJens Wiklander static int gcm_gen_table( mbedtls_gcm_context *ctx ) 102817466cbSJens Wiklander { 103817466cbSJens Wiklander int ret, i, j; 104817466cbSJens Wiklander uint64_t hi, lo; 105817466cbSJens Wiklander uint64_t vl, vh; 106817466cbSJens Wiklander unsigned char h[16]; 107817466cbSJens Wiklander size_t olen = 0; 108817466cbSJens Wiklander 109817466cbSJens Wiklander memset( h, 0, 16 ); 110817466cbSJens Wiklander if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, h, 16, h, &olen ) ) != 0 ) 111817466cbSJens Wiklander return( ret ); 112817466cbSJens Wiklander 113817466cbSJens Wiklander /* pack h as two 64-bits ints, big-endian */ 114817466cbSJens Wiklander GET_UINT32_BE( hi, h, 0 ); 115817466cbSJens Wiklander GET_UINT32_BE( lo, h, 4 ); 116817466cbSJens Wiklander vh = (uint64_t) hi << 32 | lo; 117817466cbSJens Wiklander 118817466cbSJens Wiklander GET_UINT32_BE( hi, h, 8 ); 119817466cbSJens Wiklander GET_UINT32_BE( lo, h, 12 ); 120817466cbSJens Wiklander vl = (uint64_t) hi << 32 | lo; 121817466cbSJens Wiklander 122817466cbSJens Wiklander /* 8 = 1000 corresponds to 1 in GF(2^128) */ 123817466cbSJens Wiklander ctx->HL[8] = vl; 124817466cbSJens Wiklander ctx->HH[8] = vh; 125817466cbSJens Wiklander 126817466cbSJens Wiklander #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64) 127817466cbSJens Wiklander /* With CLMUL support, we need only h, not the rest of the table */ 128817466cbSJens Wiklander if( mbedtls_aesni_has_support( MBEDTLS_AESNI_CLMUL ) ) 129817466cbSJens Wiklander return( 0 ); 130817466cbSJens Wiklander #endif 131817466cbSJens Wiklander 132817466cbSJens Wiklander /* 0 corresponds to 0 in GF(2^128) */ 133817466cbSJens Wiklander ctx->HH[0] = 0; 134817466cbSJens Wiklander ctx->HL[0] = 0; 135817466cbSJens Wiklander 136817466cbSJens Wiklander for( i = 4; i > 0; i >>= 1 ) 137817466cbSJens Wiklander { 138817466cbSJens Wiklander uint32_t T = ( vl & 1 ) * 0xe1000000U; 139817466cbSJens Wiklander vl = ( vh << 63 ) | ( vl >> 1 ); 140817466cbSJens Wiklander vh = ( vh >> 1 ) ^ ( (uint64_t) T << 32); 141817466cbSJens Wiklander 142817466cbSJens Wiklander ctx->HL[i] = vl; 143817466cbSJens Wiklander ctx->HH[i] = vh; 144817466cbSJens Wiklander } 145817466cbSJens Wiklander 146817466cbSJens Wiklander for( i = 2; i <= 8; i *= 2 ) 147817466cbSJens Wiklander { 148817466cbSJens Wiklander uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i; 149817466cbSJens Wiklander vh = *HiH; 150817466cbSJens Wiklander vl = *HiL; 151817466cbSJens Wiklander for( j = 1; j < i; j++ ) 152817466cbSJens Wiklander { 153817466cbSJens Wiklander HiH[j] = vh ^ ctx->HH[j]; 154817466cbSJens Wiklander HiL[j] = vl ^ ctx->HL[j]; 155817466cbSJens Wiklander } 156817466cbSJens Wiklander } 157817466cbSJens Wiklander 158817466cbSJens Wiklander return( 0 ); 159817466cbSJens Wiklander } 160817466cbSJens Wiklander 161817466cbSJens Wiklander int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx, 162817466cbSJens Wiklander mbedtls_cipher_id_t cipher, 163817466cbSJens Wiklander const unsigned char *key, 164817466cbSJens Wiklander unsigned int keybits ) 165817466cbSJens Wiklander { 16611fa71b9SJerome Forissier int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 167817466cbSJens Wiklander const mbedtls_cipher_info_t *cipher_info; 168817466cbSJens Wiklander 1693d3b0591SJens Wiklander GCM_VALIDATE_RET( ctx != NULL ); 1703d3b0591SJens Wiklander GCM_VALIDATE_RET( key != NULL ); 1713d3b0591SJens Wiklander GCM_VALIDATE_RET( keybits == 128 || keybits == 192 || keybits == 256 ); 1723d3b0591SJens Wiklander 17311fa71b9SJerome Forissier cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, 17411fa71b9SJerome Forissier MBEDTLS_MODE_ECB ); 175817466cbSJens Wiklander if( cipher_info == NULL ) 176817466cbSJens Wiklander return( MBEDTLS_ERR_GCM_BAD_INPUT ); 177817466cbSJens Wiklander 178817466cbSJens Wiklander if( cipher_info->block_size != 16 ) 179817466cbSJens Wiklander return( MBEDTLS_ERR_GCM_BAD_INPUT ); 180817466cbSJens Wiklander 181817466cbSJens Wiklander mbedtls_cipher_free( &ctx->cipher_ctx ); 182817466cbSJens Wiklander 183817466cbSJens Wiklander if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 ) 184817466cbSJens Wiklander return( ret ); 185817466cbSJens Wiklander 186817466cbSJens Wiklander if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits, 187817466cbSJens Wiklander MBEDTLS_ENCRYPT ) ) != 0 ) 188817466cbSJens Wiklander { 189817466cbSJens Wiklander return( ret ); 190817466cbSJens Wiklander } 191817466cbSJens Wiklander 192817466cbSJens Wiklander if( ( ret = gcm_gen_table( ctx ) ) != 0 ) 193817466cbSJens Wiklander return( ret ); 194817466cbSJens Wiklander 195817466cbSJens Wiklander return( 0 ); 196817466cbSJens Wiklander } 197817466cbSJens Wiklander 198817466cbSJens Wiklander /* 199817466cbSJens Wiklander * Shoup's method for multiplication use this table with 200817466cbSJens Wiklander * last4[x] = x times P^128 201817466cbSJens Wiklander * where x and last4[x] are seen as elements of GF(2^128) as in [MGV] 202817466cbSJens Wiklander */ 203817466cbSJens Wiklander static const uint64_t last4[16] = 204817466cbSJens Wiklander { 205817466cbSJens Wiklander 0x0000, 0x1c20, 0x3840, 0x2460, 206817466cbSJens Wiklander 0x7080, 0x6ca0, 0x48c0, 0x54e0, 207817466cbSJens Wiklander 0xe100, 0xfd20, 0xd940, 0xc560, 208817466cbSJens Wiklander 0x9180, 0x8da0, 0xa9c0, 0xb5e0 209817466cbSJens Wiklander }; 210817466cbSJens Wiklander 211817466cbSJens Wiklander /* 212817466cbSJens Wiklander * Sets output to x times H using the precomputed tables. 213817466cbSJens Wiklander * x and output are seen as elements of GF(2^128) as in [MGV]. 214817466cbSJens Wiklander */ 215817466cbSJens Wiklander static void gcm_mult( mbedtls_gcm_context *ctx, const unsigned char x[16], 216817466cbSJens Wiklander unsigned char output[16] ) 217817466cbSJens Wiklander { 218817466cbSJens Wiklander int i = 0; 219817466cbSJens Wiklander unsigned char lo, hi, rem; 220817466cbSJens Wiklander uint64_t zh, zl; 221817466cbSJens Wiklander 222817466cbSJens Wiklander #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64) 223817466cbSJens Wiklander if( mbedtls_aesni_has_support( MBEDTLS_AESNI_CLMUL ) ) { 224817466cbSJens Wiklander unsigned char h[16]; 225817466cbSJens Wiklander 226817466cbSJens Wiklander PUT_UINT32_BE( ctx->HH[8] >> 32, h, 0 ); 227817466cbSJens Wiklander PUT_UINT32_BE( ctx->HH[8], h, 4 ); 228817466cbSJens Wiklander PUT_UINT32_BE( ctx->HL[8] >> 32, h, 8 ); 229817466cbSJens Wiklander PUT_UINT32_BE( ctx->HL[8], h, 12 ); 230817466cbSJens Wiklander 231817466cbSJens Wiklander mbedtls_aesni_gcm_mult( output, x, h ); 232817466cbSJens Wiklander return; 233817466cbSJens Wiklander } 234817466cbSJens Wiklander #endif /* MBEDTLS_AESNI_C && MBEDTLS_HAVE_X86_64 */ 235817466cbSJens Wiklander 236817466cbSJens Wiklander lo = x[15] & 0xf; 237817466cbSJens Wiklander 238817466cbSJens Wiklander zh = ctx->HH[lo]; 239817466cbSJens Wiklander zl = ctx->HL[lo]; 240817466cbSJens Wiklander 241817466cbSJens Wiklander for( i = 15; i >= 0; i-- ) 242817466cbSJens Wiklander { 243817466cbSJens Wiklander lo = x[i] & 0xf; 24411fa71b9SJerome Forissier hi = ( x[i] >> 4 ) & 0xf; 245817466cbSJens Wiklander 246817466cbSJens Wiklander if( i != 15 ) 247817466cbSJens Wiklander { 248817466cbSJens Wiklander rem = (unsigned char) zl & 0xf; 249817466cbSJens Wiklander zl = ( zh << 60 ) | ( zl >> 4 ); 250817466cbSJens Wiklander zh = ( zh >> 4 ); 251817466cbSJens Wiklander zh ^= (uint64_t) last4[rem] << 48; 252817466cbSJens Wiklander zh ^= ctx->HH[lo]; 253817466cbSJens Wiklander zl ^= ctx->HL[lo]; 254817466cbSJens Wiklander 255817466cbSJens Wiklander } 256817466cbSJens Wiklander 257817466cbSJens Wiklander rem = (unsigned char) zl & 0xf; 258817466cbSJens Wiklander zl = ( zh << 60 ) | ( zl >> 4 ); 259817466cbSJens Wiklander zh = ( zh >> 4 ); 260817466cbSJens Wiklander zh ^= (uint64_t) last4[rem] << 48; 261817466cbSJens Wiklander zh ^= ctx->HH[hi]; 262817466cbSJens Wiklander zl ^= ctx->HL[hi]; 263817466cbSJens Wiklander } 264817466cbSJens Wiklander 265817466cbSJens Wiklander PUT_UINT32_BE( zh >> 32, output, 0 ); 266817466cbSJens Wiklander PUT_UINT32_BE( zh, output, 4 ); 267817466cbSJens Wiklander PUT_UINT32_BE( zl >> 32, output, 8 ); 268817466cbSJens Wiklander PUT_UINT32_BE( zl, output, 12 ); 269817466cbSJens Wiklander } 270817466cbSJens Wiklander 271817466cbSJens Wiklander int mbedtls_gcm_starts( mbedtls_gcm_context *ctx, 272817466cbSJens Wiklander int mode, 273817466cbSJens Wiklander const unsigned char *iv, 274817466cbSJens Wiklander size_t iv_len, 275817466cbSJens Wiklander const unsigned char *add, 276817466cbSJens Wiklander size_t add_len ) 277817466cbSJens Wiklander { 27811fa71b9SJerome Forissier int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 279817466cbSJens Wiklander unsigned char work_buf[16]; 280817466cbSJens Wiklander size_t i; 281817466cbSJens Wiklander const unsigned char *p; 282817466cbSJens Wiklander size_t use_len, olen = 0; 283817466cbSJens Wiklander 2843d3b0591SJens Wiklander GCM_VALIDATE_RET( ctx != NULL ); 2853d3b0591SJens Wiklander GCM_VALIDATE_RET( iv != NULL ); 2863d3b0591SJens Wiklander GCM_VALIDATE_RET( add_len == 0 || add != NULL ); 2873d3b0591SJens Wiklander 288817466cbSJens Wiklander /* IV and AD are limited to 2^64 bits, so 2^61 bytes */ 289817466cbSJens Wiklander /* IV is not allowed to be zero length */ 290817466cbSJens Wiklander if( iv_len == 0 || 291817466cbSJens Wiklander ( (uint64_t) iv_len ) >> 61 != 0 || 292817466cbSJens Wiklander ( (uint64_t) add_len ) >> 61 != 0 ) 293817466cbSJens Wiklander { 294817466cbSJens Wiklander return( MBEDTLS_ERR_GCM_BAD_INPUT ); 295817466cbSJens Wiklander } 296817466cbSJens Wiklander 297817466cbSJens Wiklander memset( ctx->y, 0x00, sizeof(ctx->y) ); 298817466cbSJens Wiklander memset( ctx->buf, 0x00, sizeof(ctx->buf) ); 299817466cbSJens Wiklander 300817466cbSJens Wiklander ctx->mode = mode; 301817466cbSJens Wiklander ctx->len = 0; 302817466cbSJens Wiklander ctx->add_len = 0; 303817466cbSJens Wiklander 304817466cbSJens Wiklander if( iv_len == 12 ) 305817466cbSJens Wiklander { 306817466cbSJens Wiklander memcpy( ctx->y, iv, iv_len ); 307817466cbSJens Wiklander ctx->y[15] = 1; 308817466cbSJens Wiklander } 309817466cbSJens Wiklander else 310817466cbSJens Wiklander { 311817466cbSJens Wiklander memset( work_buf, 0x00, 16 ); 312817466cbSJens Wiklander PUT_UINT32_BE( iv_len * 8, work_buf, 12 ); 313817466cbSJens Wiklander 314817466cbSJens Wiklander p = iv; 315817466cbSJens Wiklander while( iv_len > 0 ) 316817466cbSJens Wiklander { 317817466cbSJens Wiklander use_len = ( iv_len < 16 ) ? iv_len : 16; 318817466cbSJens Wiklander 319817466cbSJens Wiklander for( i = 0; i < use_len; i++ ) 320817466cbSJens Wiklander ctx->y[i] ^= p[i]; 321817466cbSJens Wiklander 322817466cbSJens Wiklander gcm_mult( ctx, ctx->y, ctx->y ); 323817466cbSJens Wiklander 324817466cbSJens Wiklander iv_len -= use_len; 325817466cbSJens Wiklander p += use_len; 326817466cbSJens Wiklander } 327817466cbSJens Wiklander 328817466cbSJens Wiklander for( i = 0; i < 16; i++ ) 329817466cbSJens Wiklander ctx->y[i] ^= work_buf[i]; 330817466cbSJens Wiklander 331817466cbSJens Wiklander gcm_mult( ctx, ctx->y, ctx->y ); 332817466cbSJens Wiklander } 333817466cbSJens Wiklander 33411fa71b9SJerome Forissier if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, 33511fa71b9SJerome Forissier ctx->base_ectr, &olen ) ) != 0 ) 336817466cbSJens Wiklander { 337817466cbSJens Wiklander return( ret ); 338817466cbSJens Wiklander } 339817466cbSJens Wiklander 340817466cbSJens Wiklander ctx->add_len = add_len; 341817466cbSJens Wiklander p = add; 342817466cbSJens Wiklander while( add_len > 0 ) 343817466cbSJens Wiklander { 344817466cbSJens Wiklander use_len = ( add_len < 16 ) ? add_len : 16; 345817466cbSJens Wiklander 346817466cbSJens Wiklander for( i = 0; i < use_len; i++ ) 347817466cbSJens Wiklander ctx->buf[i] ^= p[i]; 348817466cbSJens Wiklander 349817466cbSJens Wiklander gcm_mult( ctx, ctx->buf, ctx->buf ); 350817466cbSJens Wiklander 351817466cbSJens Wiklander add_len -= use_len; 352817466cbSJens Wiklander p += use_len; 353817466cbSJens Wiklander } 354817466cbSJens Wiklander 355817466cbSJens Wiklander return( 0 ); 356817466cbSJens Wiklander } 357817466cbSJens Wiklander 358817466cbSJens Wiklander int mbedtls_gcm_update( mbedtls_gcm_context *ctx, 359817466cbSJens Wiklander size_t length, 360817466cbSJens Wiklander const unsigned char *input, 361817466cbSJens Wiklander unsigned char *output ) 362817466cbSJens Wiklander { 36311fa71b9SJerome Forissier int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 364817466cbSJens Wiklander unsigned char ectr[16]; 365817466cbSJens Wiklander size_t i; 366817466cbSJens Wiklander const unsigned char *p; 367817466cbSJens Wiklander unsigned char *out_p = output; 368817466cbSJens Wiklander size_t use_len, olen = 0; 369817466cbSJens Wiklander 3703d3b0591SJens Wiklander GCM_VALIDATE_RET( ctx != NULL ); 3713d3b0591SJens Wiklander GCM_VALIDATE_RET( length == 0 || input != NULL ); 3723d3b0591SJens Wiklander GCM_VALIDATE_RET( length == 0 || output != NULL ); 3733d3b0591SJens Wiklander 374817466cbSJens Wiklander if( output > input && (size_t) ( output - input ) < length ) 375817466cbSJens Wiklander return( MBEDTLS_ERR_GCM_BAD_INPUT ); 376817466cbSJens Wiklander 377817466cbSJens Wiklander /* Total length is restricted to 2^39 - 256 bits, ie 2^36 - 2^5 bytes 378817466cbSJens Wiklander * Also check for possible overflow */ 379817466cbSJens Wiklander if( ctx->len + length < ctx->len || 380817466cbSJens Wiklander (uint64_t) ctx->len + length > 0xFFFFFFFE0ull ) 381817466cbSJens Wiklander { 382817466cbSJens Wiklander return( MBEDTLS_ERR_GCM_BAD_INPUT ); 383817466cbSJens Wiklander } 384817466cbSJens Wiklander 385817466cbSJens Wiklander ctx->len += length; 386817466cbSJens Wiklander 387817466cbSJens Wiklander p = input; 388817466cbSJens Wiklander while( length > 0 ) 389817466cbSJens Wiklander { 390817466cbSJens Wiklander use_len = ( length < 16 ) ? length : 16; 391817466cbSJens Wiklander 392817466cbSJens Wiklander for( i = 16; i > 12; i-- ) 393817466cbSJens Wiklander if( ++ctx->y[i - 1] != 0 ) 394817466cbSJens Wiklander break; 395817466cbSJens Wiklander 396817466cbSJens Wiklander if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ectr, 397817466cbSJens Wiklander &olen ) ) != 0 ) 398817466cbSJens Wiklander { 399817466cbSJens Wiklander return( ret ); 400817466cbSJens Wiklander } 401817466cbSJens Wiklander 402817466cbSJens Wiklander for( i = 0; i < use_len; i++ ) 403817466cbSJens Wiklander { 404817466cbSJens Wiklander if( ctx->mode == MBEDTLS_GCM_DECRYPT ) 405817466cbSJens Wiklander ctx->buf[i] ^= p[i]; 406817466cbSJens Wiklander out_p[i] = ectr[i] ^ p[i]; 407817466cbSJens Wiklander if( ctx->mode == MBEDTLS_GCM_ENCRYPT ) 408817466cbSJens Wiklander ctx->buf[i] ^= out_p[i]; 409817466cbSJens Wiklander } 410817466cbSJens Wiklander 411817466cbSJens Wiklander gcm_mult( ctx, ctx->buf, ctx->buf ); 412817466cbSJens Wiklander 413817466cbSJens Wiklander length -= use_len; 414817466cbSJens Wiklander p += use_len; 415817466cbSJens Wiklander out_p += use_len; 416817466cbSJens Wiklander } 417817466cbSJens Wiklander 418817466cbSJens Wiklander return( 0 ); 419817466cbSJens Wiklander } 420817466cbSJens Wiklander 421817466cbSJens Wiklander int mbedtls_gcm_finish( mbedtls_gcm_context *ctx, 422817466cbSJens Wiklander unsigned char *tag, 423817466cbSJens Wiklander size_t tag_len ) 424817466cbSJens Wiklander { 425817466cbSJens Wiklander unsigned char work_buf[16]; 426817466cbSJens Wiklander size_t i; 4273d3b0591SJens Wiklander uint64_t orig_len; 4283d3b0591SJens Wiklander uint64_t orig_add_len; 4293d3b0591SJens Wiklander 4303d3b0591SJens Wiklander GCM_VALIDATE_RET( ctx != NULL ); 4313d3b0591SJens Wiklander GCM_VALIDATE_RET( tag != NULL ); 4323d3b0591SJens Wiklander 4333d3b0591SJens Wiklander orig_len = ctx->len * 8; 4343d3b0591SJens Wiklander orig_add_len = ctx->add_len * 8; 435817466cbSJens Wiklander 436817466cbSJens Wiklander if( tag_len > 16 || tag_len < 4 ) 437817466cbSJens Wiklander return( MBEDTLS_ERR_GCM_BAD_INPUT ); 438817466cbSJens Wiklander 439817466cbSJens Wiklander memcpy( tag, ctx->base_ectr, tag_len ); 440817466cbSJens Wiklander 441817466cbSJens Wiklander if( orig_len || orig_add_len ) 442817466cbSJens Wiklander { 443817466cbSJens Wiklander memset( work_buf, 0x00, 16 ); 444817466cbSJens Wiklander 445817466cbSJens Wiklander PUT_UINT32_BE( ( orig_add_len >> 32 ), work_buf, 0 ); 446817466cbSJens Wiklander PUT_UINT32_BE( ( orig_add_len ), work_buf, 4 ); 447817466cbSJens Wiklander PUT_UINT32_BE( ( orig_len >> 32 ), work_buf, 8 ); 448817466cbSJens Wiklander PUT_UINT32_BE( ( orig_len ), work_buf, 12 ); 449817466cbSJens Wiklander 450817466cbSJens Wiklander for( i = 0; i < 16; i++ ) 451817466cbSJens Wiklander ctx->buf[i] ^= work_buf[i]; 452817466cbSJens Wiklander 453817466cbSJens Wiklander gcm_mult( ctx, ctx->buf, ctx->buf ); 454817466cbSJens Wiklander 455817466cbSJens Wiklander for( i = 0; i < tag_len; i++ ) 456817466cbSJens Wiklander tag[i] ^= ctx->buf[i]; 457817466cbSJens Wiklander } 458817466cbSJens Wiklander 459817466cbSJens Wiklander return( 0 ); 460817466cbSJens Wiklander } 461817466cbSJens Wiklander 462817466cbSJens Wiklander int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx, 463817466cbSJens Wiklander int mode, 464817466cbSJens Wiklander size_t length, 465817466cbSJens Wiklander const unsigned char *iv, 466817466cbSJens Wiklander size_t iv_len, 467817466cbSJens Wiklander const unsigned char *add, 468817466cbSJens Wiklander size_t add_len, 469817466cbSJens Wiklander const unsigned char *input, 470817466cbSJens Wiklander unsigned char *output, 471817466cbSJens Wiklander size_t tag_len, 472817466cbSJens Wiklander unsigned char *tag ) 473817466cbSJens Wiklander { 47411fa71b9SJerome Forissier int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 475817466cbSJens Wiklander 4763d3b0591SJens Wiklander GCM_VALIDATE_RET( ctx != NULL ); 4773d3b0591SJens Wiklander GCM_VALIDATE_RET( iv != NULL ); 4783d3b0591SJens Wiklander GCM_VALIDATE_RET( add_len == 0 || add != NULL ); 4793d3b0591SJens Wiklander GCM_VALIDATE_RET( length == 0 || input != NULL ); 4803d3b0591SJens Wiklander GCM_VALIDATE_RET( length == 0 || output != NULL ); 4813d3b0591SJens Wiklander GCM_VALIDATE_RET( tag != NULL ); 4823d3b0591SJens Wiklander 483817466cbSJens Wiklander if( ( ret = mbedtls_gcm_starts( ctx, mode, iv, iv_len, add, add_len ) ) != 0 ) 484817466cbSJens Wiklander return( ret ); 485817466cbSJens Wiklander 486817466cbSJens Wiklander if( ( ret = mbedtls_gcm_update( ctx, length, input, output ) ) != 0 ) 487817466cbSJens Wiklander return( ret ); 488817466cbSJens Wiklander 489817466cbSJens Wiklander if( ( ret = mbedtls_gcm_finish( ctx, tag, tag_len ) ) != 0 ) 490817466cbSJens Wiklander return( ret ); 491817466cbSJens Wiklander 492817466cbSJens Wiklander return( 0 ); 493817466cbSJens Wiklander } 494817466cbSJens Wiklander 495817466cbSJens Wiklander int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx, 496817466cbSJens Wiklander size_t length, 497817466cbSJens Wiklander const unsigned char *iv, 498817466cbSJens Wiklander size_t iv_len, 499817466cbSJens Wiklander const unsigned char *add, 500817466cbSJens Wiklander size_t add_len, 501817466cbSJens Wiklander const unsigned char *tag, 502817466cbSJens Wiklander size_t tag_len, 503817466cbSJens Wiklander const unsigned char *input, 504817466cbSJens Wiklander unsigned char *output ) 505817466cbSJens Wiklander { 50611fa71b9SJerome Forissier int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 507817466cbSJens Wiklander unsigned char check_tag[16]; 508817466cbSJens Wiklander size_t i; 509817466cbSJens Wiklander int diff; 510817466cbSJens Wiklander 5113d3b0591SJens Wiklander GCM_VALIDATE_RET( ctx != NULL ); 5123d3b0591SJens Wiklander GCM_VALIDATE_RET( iv != NULL ); 5133d3b0591SJens Wiklander GCM_VALIDATE_RET( add_len == 0 || add != NULL ); 5143d3b0591SJens Wiklander GCM_VALIDATE_RET( tag != NULL ); 5153d3b0591SJens Wiklander GCM_VALIDATE_RET( length == 0 || input != NULL ); 5163d3b0591SJens Wiklander GCM_VALIDATE_RET( length == 0 || output != NULL ); 5173d3b0591SJens Wiklander 518817466cbSJens Wiklander if( ( ret = mbedtls_gcm_crypt_and_tag( ctx, MBEDTLS_GCM_DECRYPT, length, 519817466cbSJens Wiklander iv, iv_len, add, add_len, 520817466cbSJens Wiklander input, output, tag_len, check_tag ) ) != 0 ) 521817466cbSJens Wiklander { 522817466cbSJens Wiklander return( ret ); 523817466cbSJens Wiklander } 524817466cbSJens Wiklander 525817466cbSJens Wiklander /* Check tag in "constant-time" */ 526817466cbSJens Wiklander for( diff = 0, i = 0; i < tag_len; i++ ) 527817466cbSJens Wiklander diff |= tag[i] ^ check_tag[i]; 528817466cbSJens Wiklander 529817466cbSJens Wiklander if( diff != 0 ) 530817466cbSJens Wiklander { 5313d3b0591SJens Wiklander mbedtls_platform_zeroize( output, length ); 532817466cbSJens Wiklander return( MBEDTLS_ERR_GCM_AUTH_FAILED ); 533817466cbSJens Wiklander } 534817466cbSJens Wiklander 535817466cbSJens Wiklander return( 0 ); 536817466cbSJens Wiklander } 537817466cbSJens Wiklander 538817466cbSJens Wiklander void mbedtls_gcm_free( mbedtls_gcm_context *ctx ) 539817466cbSJens Wiklander { 5403d3b0591SJens Wiklander if( ctx == NULL ) 5413d3b0591SJens Wiklander return; 542817466cbSJens Wiklander mbedtls_cipher_free( &ctx->cipher_ctx ); 5433d3b0591SJens Wiklander mbedtls_platform_zeroize( ctx, sizeof( mbedtls_gcm_context ) ); 544817466cbSJens Wiklander } 545817466cbSJens Wiklander 5463d3b0591SJens Wiklander #endif /* !MBEDTLS_GCM_ALT */ 5473d3b0591SJens Wiklander 548817466cbSJens Wiklander #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) 549817466cbSJens Wiklander /* 550817466cbSJens Wiklander * AES-GCM test vectors from: 551817466cbSJens Wiklander * 552817466cbSJens Wiklander * http://csrc.nist.gov/groups/STM/cavp/documents/mac/gcmtestvectors.zip 553817466cbSJens Wiklander */ 554817466cbSJens Wiklander #define MAX_TESTS 6 555817466cbSJens Wiklander 55611fa71b9SJerome Forissier static const int key_index_test_data[MAX_TESTS] = 557817466cbSJens Wiklander { 0, 0, 1, 1, 1, 1 }; 558817466cbSJens Wiklander 55911fa71b9SJerome Forissier static const unsigned char key_test_data[MAX_TESTS][32] = 560817466cbSJens Wiklander { 561817466cbSJens Wiklander { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 562817466cbSJens Wiklander 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 563817466cbSJens Wiklander 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 564817466cbSJens Wiklander 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 565817466cbSJens Wiklander { 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 566817466cbSJens Wiklander 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08, 567817466cbSJens Wiklander 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 568817466cbSJens Wiklander 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08 }, 569817466cbSJens Wiklander }; 570817466cbSJens Wiklander 57111fa71b9SJerome Forissier static const size_t iv_len_test_data[MAX_TESTS] = 572817466cbSJens Wiklander { 12, 12, 12, 12, 8, 60 }; 573817466cbSJens Wiklander 57411fa71b9SJerome Forissier static const int iv_index_test_data[MAX_TESTS] = 575817466cbSJens Wiklander { 0, 0, 1, 1, 1, 2 }; 576817466cbSJens Wiklander 57711fa71b9SJerome Forissier static const unsigned char iv_test_data[MAX_TESTS][64] = 578817466cbSJens Wiklander { 579817466cbSJens Wiklander { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 580817466cbSJens Wiklander 0x00, 0x00, 0x00, 0x00 }, 581817466cbSJens Wiklander { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad, 582817466cbSJens Wiklander 0xde, 0xca, 0xf8, 0x88 }, 583817466cbSJens Wiklander { 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5, 584817466cbSJens Wiklander 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa, 585817466cbSJens Wiklander 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1, 586817466cbSJens Wiklander 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28, 587817466cbSJens Wiklander 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39, 588817466cbSJens Wiklander 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54, 589817466cbSJens Wiklander 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57, 590817466cbSJens Wiklander 0xa6, 0x37, 0xb3, 0x9b }, 591817466cbSJens Wiklander }; 592817466cbSJens Wiklander 59311fa71b9SJerome Forissier static const size_t add_len_test_data[MAX_TESTS] = 594817466cbSJens Wiklander { 0, 0, 0, 20, 20, 20 }; 595817466cbSJens Wiklander 59611fa71b9SJerome Forissier static const int add_index_test_data[MAX_TESTS] = 597817466cbSJens Wiklander { 0, 0, 0, 1, 1, 1 }; 598817466cbSJens Wiklander 59911fa71b9SJerome Forissier static const unsigned char additional_test_data[MAX_TESTS][64] = 600817466cbSJens Wiklander { 601817466cbSJens Wiklander { 0x00 }, 602817466cbSJens Wiklander { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 603817466cbSJens Wiklander 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 604817466cbSJens Wiklander 0xab, 0xad, 0xda, 0xd2 }, 605817466cbSJens Wiklander }; 606817466cbSJens Wiklander 60711fa71b9SJerome Forissier static const size_t pt_len_test_data[MAX_TESTS] = 608817466cbSJens Wiklander { 0, 16, 64, 60, 60, 60 }; 609817466cbSJens Wiklander 61011fa71b9SJerome Forissier static const int pt_index_test_data[MAX_TESTS] = 611817466cbSJens Wiklander { 0, 0, 1, 1, 1, 1 }; 612817466cbSJens Wiklander 61311fa71b9SJerome Forissier static const unsigned char pt_test_data[MAX_TESTS][64] = 614817466cbSJens Wiklander { 615817466cbSJens Wiklander { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 616817466cbSJens Wiklander 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 617817466cbSJens Wiklander { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 618817466cbSJens Wiklander 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a, 619817466cbSJens Wiklander 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda, 620817466cbSJens Wiklander 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 621817466cbSJens Wiklander 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53, 622817466cbSJens Wiklander 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25, 623817466cbSJens Wiklander 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 624817466cbSJens Wiklander 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55 }, 625817466cbSJens Wiklander }; 626817466cbSJens Wiklander 62711fa71b9SJerome Forissier static const unsigned char ct_test_data[MAX_TESTS * 3][64] = 628817466cbSJens Wiklander { 629817466cbSJens Wiklander { 0x00 }, 630817466cbSJens Wiklander { 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92, 631817466cbSJens Wiklander 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78 }, 632817466cbSJens Wiklander { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24, 633817466cbSJens Wiklander 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c, 634817466cbSJens Wiklander 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0, 635817466cbSJens Wiklander 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e, 636817466cbSJens Wiklander 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c, 637817466cbSJens Wiklander 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05, 638817466cbSJens Wiklander 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97, 639817466cbSJens Wiklander 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85 }, 640817466cbSJens Wiklander { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24, 641817466cbSJens Wiklander 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c, 642817466cbSJens Wiklander 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0, 643817466cbSJens Wiklander 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e, 644817466cbSJens Wiklander 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c, 645817466cbSJens Wiklander 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05, 646817466cbSJens Wiklander 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97, 647817466cbSJens Wiklander 0x3d, 0x58, 0xe0, 0x91 }, 648817466cbSJens Wiklander { 0x61, 0x35, 0x3b, 0x4c, 0x28, 0x06, 0x93, 0x4a, 649817466cbSJens Wiklander 0x77, 0x7f, 0xf5, 0x1f, 0xa2, 0x2a, 0x47, 0x55, 650817466cbSJens Wiklander 0x69, 0x9b, 0x2a, 0x71, 0x4f, 0xcd, 0xc6, 0xf8, 651817466cbSJens Wiklander 0x37, 0x66, 0xe5, 0xf9, 0x7b, 0x6c, 0x74, 0x23, 652817466cbSJens Wiklander 0x73, 0x80, 0x69, 0x00, 0xe4, 0x9f, 0x24, 0xb2, 653817466cbSJens Wiklander 0x2b, 0x09, 0x75, 0x44, 0xd4, 0x89, 0x6b, 0x42, 654817466cbSJens Wiklander 0x49, 0x89, 0xb5, 0xe1, 0xeb, 0xac, 0x0f, 0x07, 655817466cbSJens Wiklander 0xc2, 0x3f, 0x45, 0x98 }, 656817466cbSJens Wiklander { 0x8c, 0xe2, 0x49, 0x98, 0x62, 0x56, 0x15, 0xb6, 657817466cbSJens Wiklander 0x03, 0xa0, 0x33, 0xac, 0xa1, 0x3f, 0xb8, 0x94, 658817466cbSJens Wiklander 0xbe, 0x91, 0x12, 0xa5, 0xc3, 0xa2, 0x11, 0xa8, 659817466cbSJens Wiklander 0xba, 0x26, 0x2a, 0x3c, 0xca, 0x7e, 0x2c, 0xa7, 660817466cbSJens Wiklander 0x01, 0xe4, 0xa9, 0xa4, 0xfb, 0xa4, 0x3c, 0x90, 661817466cbSJens Wiklander 0xcc, 0xdc, 0xb2, 0x81, 0xd4, 0x8c, 0x7c, 0x6f, 662817466cbSJens Wiklander 0xd6, 0x28, 0x75, 0xd2, 0xac, 0xa4, 0x17, 0x03, 663817466cbSJens Wiklander 0x4c, 0x34, 0xae, 0xe5 }, 664817466cbSJens Wiklander { 0x00 }, 665817466cbSJens Wiklander { 0x98, 0xe7, 0x24, 0x7c, 0x07, 0xf0, 0xfe, 0x41, 666817466cbSJens Wiklander 0x1c, 0x26, 0x7e, 0x43, 0x84, 0xb0, 0xf6, 0x00 }, 667817466cbSJens Wiklander { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41, 668817466cbSJens Wiklander 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57, 669817466cbSJens Wiklander 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84, 670817466cbSJens Wiklander 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c, 671817466cbSJens Wiklander 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25, 672817466cbSJens Wiklander 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47, 673817466cbSJens Wiklander 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9, 674817466cbSJens Wiklander 0xcc, 0xda, 0x27, 0x10, 0xac, 0xad, 0xe2, 0x56 }, 675817466cbSJens Wiklander { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41, 676817466cbSJens Wiklander 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57, 677817466cbSJens Wiklander 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84, 678817466cbSJens Wiklander 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c, 679817466cbSJens Wiklander 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25, 680817466cbSJens Wiklander 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47, 681817466cbSJens Wiklander 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9, 682817466cbSJens Wiklander 0xcc, 0xda, 0x27, 0x10 }, 683817466cbSJens Wiklander { 0x0f, 0x10, 0xf5, 0x99, 0xae, 0x14, 0xa1, 0x54, 684817466cbSJens Wiklander 0xed, 0x24, 0xb3, 0x6e, 0x25, 0x32, 0x4d, 0xb8, 685817466cbSJens Wiklander 0xc5, 0x66, 0x63, 0x2e, 0xf2, 0xbb, 0xb3, 0x4f, 686817466cbSJens Wiklander 0x83, 0x47, 0x28, 0x0f, 0xc4, 0x50, 0x70, 0x57, 687817466cbSJens Wiklander 0xfd, 0xdc, 0x29, 0xdf, 0x9a, 0x47, 0x1f, 0x75, 688817466cbSJens Wiklander 0xc6, 0x65, 0x41, 0xd4, 0xd4, 0xda, 0xd1, 0xc9, 689817466cbSJens Wiklander 0xe9, 0x3a, 0x19, 0xa5, 0x8e, 0x8b, 0x47, 0x3f, 690817466cbSJens Wiklander 0xa0, 0xf0, 0x62, 0xf7 }, 691817466cbSJens Wiklander { 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c, 692817466cbSJens Wiklander 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff, 693817466cbSJens Wiklander 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef, 694817466cbSJens Wiklander 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45, 695817466cbSJens Wiklander 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9, 696817466cbSJens Wiklander 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3, 697817466cbSJens Wiklander 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7, 698817466cbSJens Wiklander 0xe9, 0xb7, 0x37, 0x3b }, 699817466cbSJens Wiklander { 0x00 }, 700817466cbSJens Wiklander { 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e, 701817466cbSJens Wiklander 0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18 }, 702817466cbSJens Wiklander { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 703817466cbSJens Wiklander 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d, 704817466cbSJens Wiklander 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9, 705817466cbSJens Wiklander 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 706817466cbSJens Wiklander 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d, 707817466cbSJens Wiklander 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38, 708817466cbSJens Wiklander 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a, 709817466cbSJens Wiklander 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad }, 710817466cbSJens Wiklander { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 711817466cbSJens Wiklander 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d, 712817466cbSJens Wiklander 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9, 713817466cbSJens Wiklander 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 714817466cbSJens Wiklander 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d, 715817466cbSJens Wiklander 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38, 716817466cbSJens Wiklander 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a, 717817466cbSJens Wiklander 0xbc, 0xc9, 0xf6, 0x62 }, 718817466cbSJens Wiklander { 0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32, 719817466cbSJens Wiklander 0xae, 0x47, 0xc1, 0x3b, 0xf1, 0x98, 0x44, 0xcb, 720817466cbSJens Wiklander 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa, 721817466cbSJens Wiklander 0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0, 722817466cbSJens Wiklander 0xfe, 0xb5, 0x82, 0xd3, 0x39, 0x34, 0xa4, 0xf0, 723817466cbSJens Wiklander 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78, 724817466cbSJens Wiklander 0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99, 725817466cbSJens Wiklander 0xf4, 0x7c, 0x9b, 0x1f }, 726817466cbSJens Wiklander { 0x5a, 0x8d, 0xef, 0x2f, 0x0c, 0x9e, 0x53, 0xf1, 727817466cbSJens Wiklander 0xf7, 0x5d, 0x78, 0x53, 0x65, 0x9e, 0x2a, 0x20, 728817466cbSJens Wiklander 0xee, 0xb2, 0xb2, 0x2a, 0xaf, 0xde, 0x64, 0x19, 729817466cbSJens Wiklander 0xa0, 0x58, 0xab, 0x4f, 0x6f, 0x74, 0x6b, 0xf4, 730817466cbSJens Wiklander 0x0f, 0xc0, 0xc3, 0xb7, 0x80, 0xf2, 0x44, 0x45, 731817466cbSJens Wiklander 0x2d, 0xa3, 0xeb, 0xf1, 0xc5, 0xd8, 0x2c, 0xde, 732817466cbSJens Wiklander 0xa2, 0x41, 0x89, 0x97, 0x20, 0x0e, 0xf8, 0x2e, 733817466cbSJens Wiklander 0x44, 0xae, 0x7e, 0x3f }, 734817466cbSJens Wiklander }; 735817466cbSJens Wiklander 73611fa71b9SJerome Forissier static const unsigned char tag_test_data[MAX_TESTS * 3][16] = 737817466cbSJens Wiklander { 738817466cbSJens Wiklander { 0x58, 0xe2, 0xfc, 0xce, 0xfa, 0x7e, 0x30, 0x61, 739817466cbSJens Wiklander 0x36, 0x7f, 0x1d, 0x57, 0xa4, 0xe7, 0x45, 0x5a }, 740817466cbSJens Wiklander { 0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec, 0x13, 0xbd, 741817466cbSJens Wiklander 0xf5, 0x3a, 0x67, 0xb2, 0x12, 0x57, 0xbd, 0xdf }, 742817466cbSJens Wiklander { 0x4d, 0x5c, 0x2a, 0xf3, 0x27, 0xcd, 0x64, 0xa6, 743817466cbSJens Wiklander 0x2c, 0xf3, 0x5a, 0xbd, 0x2b, 0xa6, 0xfa, 0xb4 }, 744817466cbSJens Wiklander { 0x5b, 0xc9, 0x4f, 0xbc, 0x32, 0x21, 0xa5, 0xdb, 745817466cbSJens Wiklander 0x94, 0xfa, 0xe9, 0x5a, 0xe7, 0x12, 0x1a, 0x47 }, 746817466cbSJens Wiklander { 0x36, 0x12, 0xd2, 0xe7, 0x9e, 0x3b, 0x07, 0x85, 747817466cbSJens Wiklander 0x56, 0x1b, 0xe1, 0x4a, 0xac, 0xa2, 0xfc, 0xcb }, 748817466cbSJens Wiklander { 0x61, 0x9c, 0xc5, 0xae, 0xff, 0xfe, 0x0b, 0xfa, 749817466cbSJens Wiklander 0x46, 0x2a, 0xf4, 0x3c, 0x16, 0x99, 0xd0, 0x50 }, 750817466cbSJens Wiklander { 0xcd, 0x33, 0xb2, 0x8a, 0xc7, 0x73, 0xf7, 0x4b, 751817466cbSJens Wiklander 0xa0, 0x0e, 0xd1, 0xf3, 0x12, 0x57, 0x24, 0x35 }, 752817466cbSJens Wiklander { 0x2f, 0xf5, 0x8d, 0x80, 0x03, 0x39, 0x27, 0xab, 753817466cbSJens Wiklander 0x8e, 0xf4, 0xd4, 0x58, 0x75, 0x14, 0xf0, 0xfb }, 754817466cbSJens Wiklander { 0x99, 0x24, 0xa7, 0xc8, 0x58, 0x73, 0x36, 0xbf, 755817466cbSJens Wiklander 0xb1, 0x18, 0x02, 0x4d, 0xb8, 0x67, 0x4a, 0x14 }, 756817466cbSJens Wiklander { 0x25, 0x19, 0x49, 0x8e, 0x80, 0xf1, 0x47, 0x8f, 757817466cbSJens Wiklander 0x37, 0xba, 0x55, 0xbd, 0x6d, 0x27, 0x61, 0x8c }, 758817466cbSJens Wiklander { 0x65, 0xdc, 0xc5, 0x7f, 0xcf, 0x62, 0x3a, 0x24, 759817466cbSJens Wiklander 0x09, 0x4f, 0xcc, 0xa4, 0x0d, 0x35, 0x33, 0xf8 }, 760817466cbSJens Wiklander { 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb, 761817466cbSJens Wiklander 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9 }, 762817466cbSJens Wiklander { 0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9, 763817466cbSJens Wiklander 0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b }, 764817466cbSJens Wiklander { 0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0, 765817466cbSJens Wiklander 0x26, 0x5b, 0x98, 0xb5, 0xd4, 0x8a, 0xb9, 0x19 }, 766817466cbSJens Wiklander { 0xb0, 0x94, 0xda, 0xc5, 0xd9, 0x34, 0x71, 0xbd, 767817466cbSJens Wiklander 0xec, 0x1a, 0x50, 0x22, 0x70, 0xe3, 0xcc, 0x6c }, 768817466cbSJens Wiklander { 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68, 769817466cbSJens Wiklander 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b }, 770817466cbSJens Wiklander { 0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4, 771817466cbSJens Wiklander 0x5e, 0x45, 0x49, 0x13, 0xfe, 0x2e, 0xa8, 0xf2 }, 772817466cbSJens Wiklander { 0xa4, 0x4a, 0x82, 0x66, 0xee, 0x1c, 0x8e, 0xb0, 773817466cbSJens Wiklander 0xc8, 0xb5, 0xd4, 0xcf, 0x5a, 0xe9, 0xf1, 0x9a }, 774817466cbSJens Wiklander }; 775817466cbSJens Wiklander 776817466cbSJens Wiklander int mbedtls_gcm_self_test( int verbose ) 777817466cbSJens Wiklander { 778817466cbSJens Wiklander mbedtls_gcm_context ctx; 779817466cbSJens Wiklander unsigned char buf[64]; 780817466cbSJens Wiklander unsigned char tag_buf[16]; 781817466cbSJens Wiklander int i, j, ret; 782817466cbSJens Wiklander mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES; 783817466cbSJens Wiklander 784817466cbSJens Wiklander for( j = 0; j < 3; j++ ) 785817466cbSJens Wiklander { 786817466cbSJens Wiklander int key_len = 128 + 64 * j; 787817466cbSJens Wiklander 788817466cbSJens Wiklander for( i = 0; i < MAX_TESTS; i++ ) 789817466cbSJens Wiklander { 7903d3b0591SJens Wiklander mbedtls_gcm_init( &ctx ); 7913d3b0591SJens Wiklander 792817466cbSJens Wiklander if( verbose != 0 ) 793817466cbSJens Wiklander mbedtls_printf( " AES-GCM-%3d #%d (%s): ", 794817466cbSJens Wiklander key_len, i, "enc" ); 795817466cbSJens Wiklander 79611fa71b9SJerome Forissier ret = mbedtls_gcm_setkey( &ctx, cipher, 79711fa71b9SJerome Forissier key_test_data[key_index_test_data[i]], 7983d3b0591SJens Wiklander key_len ); 7993d3b0591SJens Wiklander /* 8003d3b0591SJens Wiklander * AES-192 is an optional feature that may be unavailable when 8013d3b0591SJens Wiklander * there is an alternative underlying implementation i.e. when 8023d3b0591SJens Wiklander * MBEDTLS_AES_ALT is defined. 8033d3b0591SJens Wiklander */ 8043d3b0591SJens Wiklander if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED && key_len == 192 ) 8053d3b0591SJens Wiklander { 8063d3b0591SJens Wiklander mbedtls_printf( "skipped\n" ); 8073d3b0591SJens Wiklander break; 8083d3b0591SJens Wiklander } 8093d3b0591SJens Wiklander else if( ret != 0 ) 8103d3b0591SJens Wiklander { 8113d3b0591SJens Wiklander goto exit; 8123d3b0591SJens Wiklander } 813817466cbSJens Wiklander 814817466cbSJens Wiklander ret = mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_ENCRYPT, 81511fa71b9SJerome Forissier pt_len_test_data[i], 81611fa71b9SJerome Forissier iv_test_data[iv_index_test_data[i]], 81711fa71b9SJerome Forissier iv_len_test_data[i], 81811fa71b9SJerome Forissier additional_test_data[add_index_test_data[i]], 81911fa71b9SJerome Forissier add_len_test_data[i], 82011fa71b9SJerome Forissier pt_test_data[pt_index_test_data[i]], 82111fa71b9SJerome Forissier buf, 16, tag_buf ); 822*7901324dSJerome Forissier #if defined(MBEDTLS_GCM_ALT) 823*7901324dSJerome Forissier /* Allow alternative implementations to only support 12-byte nonces. */ 824*7901324dSJerome Forissier if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED && 825*7901324dSJerome Forissier iv_len_test_data[i] != 12 ) 826*7901324dSJerome Forissier { 827*7901324dSJerome Forissier mbedtls_printf( "skipped\n" ); 828*7901324dSJerome Forissier break; 829*7901324dSJerome Forissier } 830*7901324dSJerome Forissier #endif /* defined(MBEDTLS_GCM_ALT) */ 8313d3b0591SJens Wiklander if( ret != 0 ) 8323d3b0591SJens Wiklander goto exit; 833817466cbSJens Wiklander 83411fa71b9SJerome Forissier if ( memcmp( buf, ct_test_data[j * 6 + i], 83511fa71b9SJerome Forissier pt_len_test_data[i] ) != 0 || 83611fa71b9SJerome Forissier memcmp( tag_buf, tag_test_data[j * 6 + i], 16 ) != 0 ) 837817466cbSJens Wiklander { 8383d3b0591SJens Wiklander ret = 1; 8393d3b0591SJens Wiklander goto exit; 840817466cbSJens Wiklander } 841817466cbSJens Wiklander 842817466cbSJens Wiklander mbedtls_gcm_free( &ctx ); 843817466cbSJens Wiklander 844817466cbSJens Wiklander if( verbose != 0 ) 845817466cbSJens Wiklander mbedtls_printf( "passed\n" ); 846817466cbSJens Wiklander 8473d3b0591SJens Wiklander mbedtls_gcm_init( &ctx ); 8483d3b0591SJens Wiklander 849817466cbSJens Wiklander if( verbose != 0 ) 850817466cbSJens Wiklander mbedtls_printf( " AES-GCM-%3d #%d (%s): ", 851817466cbSJens Wiklander key_len, i, "dec" ); 852817466cbSJens Wiklander 85311fa71b9SJerome Forissier ret = mbedtls_gcm_setkey( &ctx, cipher, 85411fa71b9SJerome Forissier key_test_data[key_index_test_data[i]], 8553d3b0591SJens Wiklander key_len ); 8563d3b0591SJens Wiklander if( ret != 0 ) 8573d3b0591SJens Wiklander goto exit; 858817466cbSJens Wiklander 859817466cbSJens Wiklander ret = mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_DECRYPT, 86011fa71b9SJerome Forissier pt_len_test_data[i], 86111fa71b9SJerome Forissier iv_test_data[iv_index_test_data[i]], 86211fa71b9SJerome Forissier iv_len_test_data[i], 86311fa71b9SJerome Forissier additional_test_data[add_index_test_data[i]], 86411fa71b9SJerome Forissier add_len_test_data[i], 86511fa71b9SJerome Forissier ct_test_data[j * 6 + i], buf, 16, tag_buf ); 866817466cbSJens Wiklander 8673d3b0591SJens Wiklander if( ret != 0 ) 8683d3b0591SJens Wiklander goto exit; 8693d3b0591SJens Wiklander 87011fa71b9SJerome Forissier if( memcmp( buf, pt_test_data[pt_index_test_data[i]], 87111fa71b9SJerome Forissier pt_len_test_data[i] ) != 0 || 87211fa71b9SJerome Forissier memcmp( tag_buf, tag_test_data[j * 6 + i], 16 ) != 0 ) 873817466cbSJens Wiklander { 8743d3b0591SJens Wiklander ret = 1; 8753d3b0591SJens Wiklander goto exit; 876817466cbSJens Wiklander } 877817466cbSJens Wiklander 878817466cbSJens Wiklander mbedtls_gcm_free( &ctx ); 879817466cbSJens Wiklander 880817466cbSJens Wiklander if( verbose != 0 ) 881817466cbSJens Wiklander mbedtls_printf( "passed\n" ); 882817466cbSJens Wiklander 8833d3b0591SJens Wiklander mbedtls_gcm_init( &ctx ); 8843d3b0591SJens Wiklander 885817466cbSJens Wiklander if( verbose != 0 ) 886817466cbSJens Wiklander mbedtls_printf( " AES-GCM-%3d #%d split (%s): ", 887817466cbSJens Wiklander key_len, i, "enc" ); 888817466cbSJens Wiklander 88911fa71b9SJerome Forissier ret = mbedtls_gcm_setkey( &ctx, cipher, 89011fa71b9SJerome Forissier key_test_data[key_index_test_data[i]], 8913d3b0591SJens Wiklander key_len ); 8923d3b0591SJens Wiklander if( ret != 0 ) 8933d3b0591SJens Wiklander goto exit; 894817466cbSJens Wiklander 895817466cbSJens Wiklander ret = mbedtls_gcm_starts( &ctx, MBEDTLS_GCM_ENCRYPT, 89611fa71b9SJerome Forissier iv_test_data[iv_index_test_data[i]], 89711fa71b9SJerome Forissier iv_len_test_data[i], 89811fa71b9SJerome Forissier additional_test_data[add_index_test_data[i]], 89911fa71b9SJerome Forissier add_len_test_data[i] ); 900817466cbSJens Wiklander if( ret != 0 ) 9013d3b0591SJens Wiklander goto exit; 902817466cbSJens Wiklander 90311fa71b9SJerome Forissier if( pt_len_test_data[i] > 32 ) 904817466cbSJens Wiklander { 90511fa71b9SJerome Forissier size_t rest_len = pt_len_test_data[i] - 32; 90611fa71b9SJerome Forissier ret = mbedtls_gcm_update( &ctx, 32, 90711fa71b9SJerome Forissier pt_test_data[pt_index_test_data[i]], 90811fa71b9SJerome Forissier buf ); 909817466cbSJens Wiklander if( ret != 0 ) 9103d3b0591SJens Wiklander goto exit; 911817466cbSJens Wiklander 91211fa71b9SJerome Forissier ret = mbedtls_gcm_update( &ctx, rest_len, 91311fa71b9SJerome Forissier pt_test_data[pt_index_test_data[i]] + 32, 914817466cbSJens Wiklander buf + 32 ); 915817466cbSJens Wiklander if( ret != 0 ) 9163d3b0591SJens Wiklander goto exit; 917817466cbSJens Wiklander } 918817466cbSJens Wiklander else 919817466cbSJens Wiklander { 92011fa71b9SJerome Forissier ret = mbedtls_gcm_update( &ctx, pt_len_test_data[i], 92111fa71b9SJerome Forissier pt_test_data[pt_index_test_data[i]], 92211fa71b9SJerome Forissier buf ); 923817466cbSJens Wiklander if( ret != 0 ) 9243d3b0591SJens Wiklander goto exit; 925817466cbSJens Wiklander } 926817466cbSJens Wiklander 927817466cbSJens Wiklander ret = mbedtls_gcm_finish( &ctx, tag_buf, 16 ); 9283d3b0591SJens Wiklander if( ret != 0 ) 9293d3b0591SJens Wiklander goto exit; 9303d3b0591SJens Wiklander 93111fa71b9SJerome Forissier if( memcmp( buf, ct_test_data[j * 6 + i], 93211fa71b9SJerome Forissier pt_len_test_data[i] ) != 0 || 93311fa71b9SJerome Forissier memcmp( tag_buf, tag_test_data[j * 6 + i], 16 ) != 0 ) 934817466cbSJens Wiklander { 9353d3b0591SJens Wiklander ret = 1; 9363d3b0591SJens Wiklander goto exit; 937817466cbSJens Wiklander } 938817466cbSJens Wiklander 939817466cbSJens Wiklander mbedtls_gcm_free( &ctx ); 940817466cbSJens Wiklander 941817466cbSJens Wiklander if( verbose != 0 ) 942817466cbSJens Wiklander mbedtls_printf( "passed\n" ); 943817466cbSJens Wiklander 9443d3b0591SJens Wiklander mbedtls_gcm_init( &ctx ); 9453d3b0591SJens Wiklander 946817466cbSJens Wiklander if( verbose != 0 ) 947817466cbSJens Wiklander mbedtls_printf( " AES-GCM-%3d #%d split (%s): ", 948817466cbSJens Wiklander key_len, i, "dec" ); 949817466cbSJens Wiklander 95011fa71b9SJerome Forissier ret = mbedtls_gcm_setkey( &ctx, cipher, 95111fa71b9SJerome Forissier key_test_data[key_index_test_data[i]], 9523d3b0591SJens Wiklander key_len ); 9533d3b0591SJens Wiklander if( ret != 0 ) 9543d3b0591SJens Wiklander goto exit; 955817466cbSJens Wiklander 956817466cbSJens Wiklander ret = mbedtls_gcm_starts( &ctx, MBEDTLS_GCM_DECRYPT, 95711fa71b9SJerome Forissier iv_test_data[iv_index_test_data[i]], 95811fa71b9SJerome Forissier iv_len_test_data[i], 95911fa71b9SJerome Forissier additional_test_data[add_index_test_data[i]], 96011fa71b9SJerome Forissier add_len_test_data[i] ); 961817466cbSJens Wiklander if( ret != 0 ) 9623d3b0591SJens Wiklander goto exit; 963817466cbSJens Wiklander 96411fa71b9SJerome Forissier if( pt_len_test_data[i] > 32 ) 965817466cbSJens Wiklander { 96611fa71b9SJerome Forissier size_t rest_len = pt_len_test_data[i] - 32; 96711fa71b9SJerome Forissier ret = mbedtls_gcm_update( &ctx, 32, ct_test_data[j * 6 + i], 96811fa71b9SJerome Forissier buf ); 969817466cbSJens Wiklander if( ret != 0 ) 9703d3b0591SJens Wiklander goto exit; 971817466cbSJens Wiklander 97211fa71b9SJerome Forissier ret = mbedtls_gcm_update( &ctx, rest_len, 97311fa71b9SJerome Forissier ct_test_data[j * 6 + i] + 32, 974817466cbSJens Wiklander buf + 32 ); 975817466cbSJens Wiklander if( ret != 0 ) 9763d3b0591SJens Wiklander goto exit; 977817466cbSJens Wiklander } 978817466cbSJens Wiklander else 979817466cbSJens Wiklander { 98011fa71b9SJerome Forissier ret = mbedtls_gcm_update( &ctx, pt_len_test_data[i], 98111fa71b9SJerome Forissier ct_test_data[j * 6 + i], 9823d3b0591SJens Wiklander buf ); 983817466cbSJens Wiklander if( ret != 0 ) 9843d3b0591SJens Wiklander goto exit; 985817466cbSJens Wiklander } 986817466cbSJens Wiklander 987817466cbSJens Wiklander ret = mbedtls_gcm_finish( &ctx, tag_buf, 16 ); 9883d3b0591SJens Wiklander if( ret != 0 ) 9893d3b0591SJens Wiklander goto exit; 9903d3b0591SJens Wiklander 99111fa71b9SJerome Forissier if( memcmp( buf, pt_test_data[pt_index_test_data[i]], 99211fa71b9SJerome Forissier pt_len_test_data[i] ) != 0 || 99311fa71b9SJerome Forissier memcmp( tag_buf, tag_test_data[j * 6 + i], 16 ) != 0 ) 994817466cbSJens Wiklander { 9953d3b0591SJens Wiklander ret = 1; 9963d3b0591SJens Wiklander goto exit; 997817466cbSJens Wiklander } 998817466cbSJens Wiklander 999817466cbSJens Wiklander mbedtls_gcm_free( &ctx ); 1000817466cbSJens Wiklander 1001817466cbSJens Wiklander if( verbose != 0 ) 1002817466cbSJens Wiklander mbedtls_printf( "passed\n" ); 1003817466cbSJens Wiklander } 1004817466cbSJens Wiklander } 1005817466cbSJens Wiklander 1006817466cbSJens Wiklander if( verbose != 0 ) 1007817466cbSJens Wiklander mbedtls_printf( "\n" ); 1008817466cbSJens Wiklander 10093d3b0591SJens Wiklander ret = 0; 10103d3b0591SJens Wiklander 10113d3b0591SJens Wiklander exit: 10123d3b0591SJens Wiklander if( ret != 0 ) 10133d3b0591SJens Wiklander { 10143d3b0591SJens Wiklander if( verbose != 0 ) 10153d3b0591SJens Wiklander mbedtls_printf( "failed\n" ); 10163d3b0591SJens Wiklander mbedtls_gcm_free( &ctx ); 10173d3b0591SJens Wiklander } 10183d3b0591SJens Wiklander 10193d3b0591SJens Wiklander return( ret ); 1020817466cbSJens Wiklander } 1021817466cbSJens Wiklander 1022817466cbSJens Wiklander #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ 1023817466cbSJens Wiklander 1024817466cbSJens Wiklander #endif /* MBEDTLS_GCM_C */ 1025