Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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