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