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