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