mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
features/mbedtls/mbed-crypto/src/ccm.c@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
- Parent:
- 0:5b88d5760320
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:5b88d5760320 | 1 | /* |
kenjiArai | 0:5b88d5760320 | 2 | * NIST SP800-38C compliant CCM implementation |
kenjiArai | 0:5b88d5760320 | 3 | * |
kenjiArai | 0:5b88d5760320 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
kenjiArai | 0:5b88d5760320 | 5 | * SPDX-License-Identifier: Apache-2.0 |
kenjiArai | 0:5b88d5760320 | 6 | * |
kenjiArai | 0:5b88d5760320 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
kenjiArai | 0:5b88d5760320 | 8 | * not use this file except in compliance with the License. |
kenjiArai | 0:5b88d5760320 | 9 | * You may obtain a copy of the License at |
kenjiArai | 0:5b88d5760320 | 10 | * |
kenjiArai | 0:5b88d5760320 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 0:5b88d5760320 | 12 | * |
kenjiArai | 0:5b88d5760320 | 13 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 0:5b88d5760320 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
kenjiArai | 0:5b88d5760320 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 0:5b88d5760320 | 16 | * See the License for the specific language governing permissions and |
kenjiArai | 0:5b88d5760320 | 17 | * limitations under the License. |
kenjiArai | 0:5b88d5760320 | 18 | * |
kenjiArai | 0:5b88d5760320 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
kenjiArai | 0:5b88d5760320 | 20 | */ |
kenjiArai | 0:5b88d5760320 | 21 | |
kenjiArai | 0:5b88d5760320 | 22 | /* |
kenjiArai | 0:5b88d5760320 | 23 | * Definition of CCM: |
kenjiArai | 0:5b88d5760320 | 24 | * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf |
kenjiArai | 0:5b88d5760320 | 25 | * RFC 3610 "Counter with CBC-MAC (CCM)" |
kenjiArai | 0:5b88d5760320 | 26 | * |
kenjiArai | 0:5b88d5760320 | 27 | * Related: |
kenjiArai | 0:5b88d5760320 | 28 | * RFC 5116 "An Interface and Algorithms for Authenticated Encryption" |
kenjiArai | 0:5b88d5760320 | 29 | */ |
kenjiArai | 0:5b88d5760320 | 30 | |
kenjiArai | 0:5b88d5760320 | 31 | #if !defined(MBEDTLS_CONFIG_FILE) |
kenjiArai | 0:5b88d5760320 | 32 | #include "mbedtls/config.h" |
kenjiArai | 0:5b88d5760320 | 33 | #else |
kenjiArai | 0:5b88d5760320 | 34 | #include MBEDTLS_CONFIG_FILE |
kenjiArai | 0:5b88d5760320 | 35 | #endif |
kenjiArai | 0:5b88d5760320 | 36 | |
kenjiArai | 0:5b88d5760320 | 37 | #if defined(MBEDTLS_CCM_C) |
kenjiArai | 0:5b88d5760320 | 38 | |
kenjiArai | 0:5b88d5760320 | 39 | #include "mbedtls/ccm.h" |
kenjiArai | 0:5b88d5760320 | 40 | #include "mbedtls/platform_util.h" |
kenjiArai | 0:5b88d5760320 | 41 | |
kenjiArai | 0:5b88d5760320 | 42 | #include <string.h> |
kenjiArai | 0:5b88d5760320 | 43 | |
kenjiArai | 0:5b88d5760320 | 44 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
kenjiArai | 0:5b88d5760320 | 45 | #if defined(MBEDTLS_PLATFORM_C) |
kenjiArai | 0:5b88d5760320 | 46 | #include "mbedtls/platform.h" |
kenjiArai | 0:5b88d5760320 | 47 | #else |
kenjiArai | 0:5b88d5760320 | 48 | #include <stdio.h> |
kenjiArai | 0:5b88d5760320 | 49 | #define mbedtls_printf printf |
kenjiArai | 0:5b88d5760320 | 50 | #endif /* MBEDTLS_PLATFORM_C */ |
kenjiArai | 0:5b88d5760320 | 51 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
kenjiArai | 0:5b88d5760320 | 52 | |
kenjiArai | 0:5b88d5760320 | 53 | #if !defined(MBEDTLS_CCM_ALT) |
kenjiArai | 0:5b88d5760320 | 54 | |
kenjiArai | 0:5b88d5760320 | 55 | #define CCM_VALIDATE_RET( cond ) \ |
kenjiArai | 0:5b88d5760320 | 56 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CCM_BAD_INPUT ) |
kenjiArai | 0:5b88d5760320 | 57 | #define CCM_VALIDATE( cond ) \ |
kenjiArai | 0:5b88d5760320 | 58 | MBEDTLS_INTERNAL_VALIDATE( cond ) |
kenjiArai | 0:5b88d5760320 | 59 | |
kenjiArai | 0:5b88d5760320 | 60 | #define CCM_ENCRYPT 0 |
kenjiArai | 0:5b88d5760320 | 61 | #define CCM_DECRYPT 1 |
kenjiArai | 0:5b88d5760320 | 62 | |
kenjiArai | 0:5b88d5760320 | 63 | /* |
kenjiArai | 0:5b88d5760320 | 64 | * Initialize context |
kenjiArai | 0:5b88d5760320 | 65 | */ |
kenjiArai | 0:5b88d5760320 | 66 | void mbedtls_ccm_init( mbedtls_ccm_context *ctx ) |
kenjiArai | 0:5b88d5760320 | 67 | { |
kenjiArai | 0:5b88d5760320 | 68 | CCM_VALIDATE( ctx != NULL ); |
kenjiArai | 0:5b88d5760320 | 69 | memset( ctx, 0, sizeof( mbedtls_ccm_context ) ); |
kenjiArai | 0:5b88d5760320 | 70 | } |
kenjiArai | 0:5b88d5760320 | 71 | |
kenjiArai | 0:5b88d5760320 | 72 | int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, |
kenjiArai | 0:5b88d5760320 | 73 | mbedtls_cipher_id_t cipher, |
kenjiArai | 0:5b88d5760320 | 74 | const unsigned char *key, |
kenjiArai | 0:5b88d5760320 | 75 | unsigned int keybits ) |
kenjiArai | 0:5b88d5760320 | 76 | { |
kenjiArai | 0:5b88d5760320 | 77 | int ret; |
kenjiArai | 0:5b88d5760320 | 78 | const mbedtls_cipher_info_t *cipher_info; |
kenjiArai | 0:5b88d5760320 | 79 | |
kenjiArai | 0:5b88d5760320 | 80 | CCM_VALIDATE_RET( ctx != NULL ); |
kenjiArai | 0:5b88d5760320 | 81 | CCM_VALIDATE_RET( key != NULL ); |
kenjiArai | 0:5b88d5760320 | 82 | |
kenjiArai | 0:5b88d5760320 | 83 | cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, |
kenjiArai | 0:5b88d5760320 | 84 | MBEDTLS_MODE_ECB ); |
kenjiArai | 0:5b88d5760320 | 85 | if( cipher_info == NULL ) |
kenjiArai | 0:5b88d5760320 | 86 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
kenjiArai | 0:5b88d5760320 | 87 | |
kenjiArai | 0:5b88d5760320 | 88 | if( cipher_info->block_size != 16 ) |
kenjiArai | 0:5b88d5760320 | 89 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
kenjiArai | 0:5b88d5760320 | 90 | |
kenjiArai | 0:5b88d5760320 | 91 | mbedtls_cipher_free( &ctx->cipher_ctx ); |
kenjiArai | 0:5b88d5760320 | 92 | |
kenjiArai | 0:5b88d5760320 | 93 | if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 94 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 95 | |
kenjiArai | 0:5b88d5760320 | 96 | if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits, |
kenjiArai | 0:5b88d5760320 | 97 | MBEDTLS_ENCRYPT ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 98 | { |
kenjiArai | 0:5b88d5760320 | 99 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 100 | } |
kenjiArai | 0:5b88d5760320 | 101 | |
kenjiArai | 0:5b88d5760320 | 102 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 103 | } |
kenjiArai | 0:5b88d5760320 | 104 | |
kenjiArai | 0:5b88d5760320 | 105 | /* |
kenjiArai | 0:5b88d5760320 | 106 | * Free context |
kenjiArai | 0:5b88d5760320 | 107 | */ |
kenjiArai | 0:5b88d5760320 | 108 | void mbedtls_ccm_free( mbedtls_ccm_context *ctx ) |
kenjiArai | 0:5b88d5760320 | 109 | { |
kenjiArai | 0:5b88d5760320 | 110 | if( ctx == NULL ) |
kenjiArai | 0:5b88d5760320 | 111 | return; |
kenjiArai | 0:5b88d5760320 | 112 | mbedtls_cipher_free( &ctx->cipher_ctx ); |
kenjiArai | 0:5b88d5760320 | 113 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ccm_context ) ); |
kenjiArai | 0:5b88d5760320 | 114 | } |
kenjiArai | 0:5b88d5760320 | 115 | |
kenjiArai | 0:5b88d5760320 | 116 | /* |
kenjiArai | 0:5b88d5760320 | 117 | * Macros for common operations. |
kenjiArai | 0:5b88d5760320 | 118 | * Results in smaller compiled code than static inline functions. |
kenjiArai | 0:5b88d5760320 | 119 | */ |
kenjiArai | 0:5b88d5760320 | 120 | |
kenjiArai | 0:5b88d5760320 | 121 | /* |
kenjiArai | 0:5b88d5760320 | 122 | * Update the CBC-MAC state in y using a block in b |
kenjiArai | 0:5b88d5760320 | 123 | * (Always using b as the source helps the compiler optimise a bit better.) |
kenjiArai | 0:5b88d5760320 | 124 | */ |
kenjiArai | 0:5b88d5760320 | 125 | #define UPDATE_CBC_MAC \ |
kenjiArai | 0:5b88d5760320 | 126 | for( i = 0; i < 16; i++ ) \ |
kenjiArai | 0:5b88d5760320 | 127 | y[i] ^= b[i]; \ |
kenjiArai | 0:5b88d5760320 | 128 | \ |
kenjiArai | 0:5b88d5760320 | 129 | if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \ |
kenjiArai | 0:5b88d5760320 | 130 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 131 | |
kenjiArai | 0:5b88d5760320 | 132 | /* |
kenjiArai | 0:5b88d5760320 | 133 | * Encrypt or decrypt a partial block with CTR |
kenjiArai | 0:5b88d5760320 | 134 | * Warning: using b for temporary storage! src and dst must not be b! |
kenjiArai | 0:5b88d5760320 | 135 | * This avoids allocating one more 16 bytes buffer while allowing src == dst. |
kenjiArai | 0:5b88d5760320 | 136 | */ |
kenjiArai | 0:5b88d5760320 | 137 | #define CTR_CRYPT( dst, src, len ) \ |
kenjiArai | 0:5b88d5760320 | 138 | do \ |
kenjiArai | 0:5b88d5760320 | 139 | { \ |
kenjiArai | 0:5b88d5760320 | 140 | if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, \ |
kenjiArai | 0:5b88d5760320 | 141 | 16, b, &olen ) ) != 0 ) \ |
kenjiArai | 0:5b88d5760320 | 142 | { \ |
kenjiArai | 0:5b88d5760320 | 143 | return( ret ); \ |
kenjiArai | 0:5b88d5760320 | 144 | } \ |
kenjiArai | 0:5b88d5760320 | 145 | \ |
kenjiArai | 0:5b88d5760320 | 146 | for( i = 0; i < (len); i++ ) \ |
kenjiArai | 0:5b88d5760320 | 147 | (dst)[i] = (src)[i] ^ b[i]; \ |
kenjiArai | 0:5b88d5760320 | 148 | } while( 0 ) |
kenjiArai | 0:5b88d5760320 | 149 | |
kenjiArai | 0:5b88d5760320 | 150 | /* |
kenjiArai | 0:5b88d5760320 | 151 | * Authenticated encryption or decryption |
kenjiArai | 0:5b88d5760320 | 152 | */ |
kenjiArai | 0:5b88d5760320 | 153 | static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length, |
kenjiArai | 0:5b88d5760320 | 154 | const unsigned char *iv, size_t iv_len, |
kenjiArai | 0:5b88d5760320 | 155 | const unsigned char *add, size_t add_len, |
kenjiArai | 0:5b88d5760320 | 156 | const unsigned char *input, unsigned char *output, |
kenjiArai | 0:5b88d5760320 | 157 | unsigned char *tag, size_t tag_len ) |
kenjiArai | 0:5b88d5760320 | 158 | { |
kenjiArai | 0:5b88d5760320 | 159 | int ret; |
kenjiArai | 0:5b88d5760320 | 160 | unsigned char i; |
kenjiArai | 0:5b88d5760320 | 161 | unsigned char q; |
kenjiArai | 0:5b88d5760320 | 162 | size_t len_left, olen; |
kenjiArai | 0:5b88d5760320 | 163 | unsigned char b[16]; |
kenjiArai | 0:5b88d5760320 | 164 | unsigned char y[16]; |
kenjiArai | 0:5b88d5760320 | 165 | unsigned char ctr[16]; |
kenjiArai | 0:5b88d5760320 | 166 | const unsigned char *src; |
kenjiArai | 0:5b88d5760320 | 167 | unsigned char *dst; |
kenjiArai | 0:5b88d5760320 | 168 | |
kenjiArai | 0:5b88d5760320 | 169 | /* |
kenjiArai | 0:5b88d5760320 | 170 | * Check length requirements: SP800-38C A.1 |
kenjiArai | 0:5b88d5760320 | 171 | * Additional requirement: a < 2^16 - 2^8 to simplify the code. |
kenjiArai | 0:5b88d5760320 | 172 | * 'length' checked later (when writing it to the first block) |
kenjiArai | 0:5b88d5760320 | 173 | * |
kenjiArai | 0:5b88d5760320 | 174 | * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4). |
kenjiArai | 0:5b88d5760320 | 175 | */ |
kenjiArai | 0:5b88d5760320 | 176 | if( tag_len == 2 || tag_len > 16 || tag_len % 2 != 0 ) |
kenjiArai | 0:5b88d5760320 | 177 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
kenjiArai | 0:5b88d5760320 | 178 | |
kenjiArai | 0:5b88d5760320 | 179 | /* Also implies q is within bounds */ |
kenjiArai | 0:5b88d5760320 | 180 | if( iv_len < 7 || iv_len > 13 ) |
kenjiArai | 0:5b88d5760320 | 181 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
kenjiArai | 0:5b88d5760320 | 182 | |
kenjiArai | 0:5b88d5760320 | 183 | if( add_len > 0xFF00 ) |
kenjiArai | 0:5b88d5760320 | 184 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
kenjiArai | 0:5b88d5760320 | 185 | |
kenjiArai | 0:5b88d5760320 | 186 | q = 16 - 1 - (unsigned char) iv_len; |
kenjiArai | 0:5b88d5760320 | 187 | |
kenjiArai | 0:5b88d5760320 | 188 | /* |
kenjiArai | 0:5b88d5760320 | 189 | * First block B_0: |
kenjiArai | 0:5b88d5760320 | 190 | * 0 .. 0 flags |
kenjiArai | 0:5b88d5760320 | 191 | * 1 .. iv_len nonce (aka iv) |
kenjiArai | 0:5b88d5760320 | 192 | * iv_len+1 .. 15 length |
kenjiArai | 0:5b88d5760320 | 193 | * |
kenjiArai | 0:5b88d5760320 | 194 | * With flags as (bits): |
kenjiArai | 0:5b88d5760320 | 195 | * 7 0 |
kenjiArai | 0:5b88d5760320 | 196 | * 6 add present? |
kenjiArai | 0:5b88d5760320 | 197 | * 5 .. 3 (t - 2) / 2 |
kenjiArai | 0:5b88d5760320 | 198 | * 2 .. 0 q - 1 |
kenjiArai | 0:5b88d5760320 | 199 | */ |
kenjiArai | 0:5b88d5760320 | 200 | b[0] = 0; |
kenjiArai | 0:5b88d5760320 | 201 | b[0] |= ( add_len > 0 ) << 6; |
kenjiArai | 0:5b88d5760320 | 202 | b[0] |= ( ( tag_len - 2 ) / 2 ) << 3; |
kenjiArai | 0:5b88d5760320 | 203 | b[0] |= q - 1; |
kenjiArai | 0:5b88d5760320 | 204 | |
kenjiArai | 0:5b88d5760320 | 205 | memcpy( b + 1, iv, iv_len ); |
kenjiArai | 0:5b88d5760320 | 206 | |
kenjiArai | 0:5b88d5760320 | 207 | for( i = 0, len_left = length; i < q; i++, len_left >>= 8 ) |
kenjiArai | 0:5b88d5760320 | 208 | b[15-i] = (unsigned char)( len_left & 0xFF ); |
kenjiArai | 0:5b88d5760320 | 209 | |
kenjiArai | 0:5b88d5760320 | 210 | if( len_left > 0 ) |
kenjiArai | 0:5b88d5760320 | 211 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
kenjiArai | 0:5b88d5760320 | 212 | |
kenjiArai | 0:5b88d5760320 | 213 | |
kenjiArai | 0:5b88d5760320 | 214 | /* Start CBC-MAC with first block */ |
kenjiArai | 0:5b88d5760320 | 215 | memset( y, 0, 16 ); |
kenjiArai | 0:5b88d5760320 | 216 | UPDATE_CBC_MAC; |
kenjiArai | 0:5b88d5760320 | 217 | |
kenjiArai | 0:5b88d5760320 | 218 | /* |
kenjiArai | 0:5b88d5760320 | 219 | * If there is additional data, update CBC-MAC with |
kenjiArai | 0:5b88d5760320 | 220 | * add_len, add, 0 (padding to a block boundary) |
kenjiArai | 0:5b88d5760320 | 221 | */ |
kenjiArai | 0:5b88d5760320 | 222 | if( add_len > 0 ) |
kenjiArai | 0:5b88d5760320 | 223 | { |
kenjiArai | 0:5b88d5760320 | 224 | size_t use_len; |
kenjiArai | 0:5b88d5760320 | 225 | len_left = add_len; |
kenjiArai | 0:5b88d5760320 | 226 | src = add; |
kenjiArai | 0:5b88d5760320 | 227 | |
kenjiArai | 0:5b88d5760320 | 228 | memset( b, 0, 16 ); |
kenjiArai | 0:5b88d5760320 | 229 | b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF ); |
kenjiArai | 0:5b88d5760320 | 230 | b[1] = (unsigned char)( ( add_len ) & 0xFF ); |
kenjiArai | 0:5b88d5760320 | 231 | |
kenjiArai | 0:5b88d5760320 | 232 | use_len = len_left < 16 - 2 ? len_left : 16 - 2; |
kenjiArai | 0:5b88d5760320 | 233 | memcpy( b + 2, src, use_len ); |
kenjiArai | 0:5b88d5760320 | 234 | len_left -= use_len; |
kenjiArai | 0:5b88d5760320 | 235 | src += use_len; |
kenjiArai | 0:5b88d5760320 | 236 | |
kenjiArai | 0:5b88d5760320 | 237 | UPDATE_CBC_MAC; |
kenjiArai | 0:5b88d5760320 | 238 | |
kenjiArai | 0:5b88d5760320 | 239 | while( len_left > 0 ) |
kenjiArai | 0:5b88d5760320 | 240 | { |
kenjiArai | 0:5b88d5760320 | 241 | use_len = len_left > 16 ? 16 : len_left; |
kenjiArai | 0:5b88d5760320 | 242 | |
kenjiArai | 0:5b88d5760320 | 243 | memset( b, 0, 16 ); |
kenjiArai | 0:5b88d5760320 | 244 | memcpy( b, src, use_len ); |
kenjiArai | 0:5b88d5760320 | 245 | UPDATE_CBC_MAC; |
kenjiArai | 0:5b88d5760320 | 246 | |
kenjiArai | 0:5b88d5760320 | 247 | len_left -= use_len; |
kenjiArai | 0:5b88d5760320 | 248 | src += use_len; |
kenjiArai | 0:5b88d5760320 | 249 | } |
kenjiArai | 0:5b88d5760320 | 250 | } |
kenjiArai | 0:5b88d5760320 | 251 | |
kenjiArai | 0:5b88d5760320 | 252 | /* |
kenjiArai | 0:5b88d5760320 | 253 | * Prepare counter block for encryption: |
kenjiArai | 0:5b88d5760320 | 254 | * 0 .. 0 flags |
kenjiArai | 0:5b88d5760320 | 255 | * 1 .. iv_len nonce (aka iv) |
kenjiArai | 0:5b88d5760320 | 256 | * iv_len+1 .. 15 counter (initially 1) |
kenjiArai | 0:5b88d5760320 | 257 | * |
kenjiArai | 0:5b88d5760320 | 258 | * With flags as (bits): |
kenjiArai | 0:5b88d5760320 | 259 | * 7 .. 3 0 |
kenjiArai | 0:5b88d5760320 | 260 | * 2 .. 0 q - 1 |
kenjiArai | 0:5b88d5760320 | 261 | */ |
kenjiArai | 0:5b88d5760320 | 262 | ctr[0] = q - 1; |
kenjiArai | 0:5b88d5760320 | 263 | memcpy( ctr + 1, iv, iv_len ); |
kenjiArai | 0:5b88d5760320 | 264 | memset( ctr + 1 + iv_len, 0, q ); |
kenjiArai | 0:5b88d5760320 | 265 | ctr[15] = 1; |
kenjiArai | 0:5b88d5760320 | 266 | |
kenjiArai | 0:5b88d5760320 | 267 | /* |
kenjiArai | 0:5b88d5760320 | 268 | * Authenticate and {en,de}crypt the message. |
kenjiArai | 0:5b88d5760320 | 269 | * |
kenjiArai | 0:5b88d5760320 | 270 | * The only difference between encryption and decryption is |
kenjiArai | 0:5b88d5760320 | 271 | * the respective order of authentication and {en,de}cryption. |
kenjiArai | 0:5b88d5760320 | 272 | */ |
kenjiArai | 0:5b88d5760320 | 273 | len_left = length; |
kenjiArai | 0:5b88d5760320 | 274 | src = input; |
kenjiArai | 0:5b88d5760320 | 275 | dst = output; |
kenjiArai | 0:5b88d5760320 | 276 | |
kenjiArai | 0:5b88d5760320 | 277 | while( len_left > 0 ) |
kenjiArai | 0:5b88d5760320 | 278 | { |
kenjiArai | 0:5b88d5760320 | 279 | size_t use_len = len_left > 16 ? 16 : len_left; |
kenjiArai | 0:5b88d5760320 | 280 | |
kenjiArai | 0:5b88d5760320 | 281 | if( mode == CCM_ENCRYPT ) |
kenjiArai | 0:5b88d5760320 | 282 | { |
kenjiArai | 0:5b88d5760320 | 283 | memset( b, 0, 16 ); |
kenjiArai | 0:5b88d5760320 | 284 | memcpy( b, src, use_len ); |
kenjiArai | 0:5b88d5760320 | 285 | UPDATE_CBC_MAC; |
kenjiArai | 0:5b88d5760320 | 286 | } |
kenjiArai | 0:5b88d5760320 | 287 | |
kenjiArai | 0:5b88d5760320 | 288 | CTR_CRYPT( dst, src, use_len ); |
kenjiArai | 0:5b88d5760320 | 289 | |
kenjiArai | 0:5b88d5760320 | 290 | if( mode == CCM_DECRYPT ) |
kenjiArai | 0:5b88d5760320 | 291 | { |
kenjiArai | 0:5b88d5760320 | 292 | memset( b, 0, 16 ); |
kenjiArai | 0:5b88d5760320 | 293 | memcpy( b, dst, use_len ); |
kenjiArai | 0:5b88d5760320 | 294 | UPDATE_CBC_MAC; |
kenjiArai | 0:5b88d5760320 | 295 | } |
kenjiArai | 0:5b88d5760320 | 296 | |
kenjiArai | 0:5b88d5760320 | 297 | dst += use_len; |
kenjiArai | 0:5b88d5760320 | 298 | src += use_len; |
kenjiArai | 0:5b88d5760320 | 299 | len_left -= use_len; |
kenjiArai | 0:5b88d5760320 | 300 | |
kenjiArai | 0:5b88d5760320 | 301 | /* |
kenjiArai | 0:5b88d5760320 | 302 | * Increment counter. |
kenjiArai | 0:5b88d5760320 | 303 | * No need to check for overflow thanks to the length check above. |
kenjiArai | 0:5b88d5760320 | 304 | */ |
kenjiArai | 0:5b88d5760320 | 305 | for( i = 0; i < q; i++ ) |
kenjiArai | 0:5b88d5760320 | 306 | if( ++ctr[15-i] != 0 ) |
kenjiArai | 0:5b88d5760320 | 307 | break; |
kenjiArai | 0:5b88d5760320 | 308 | } |
kenjiArai | 0:5b88d5760320 | 309 | |
kenjiArai | 0:5b88d5760320 | 310 | /* |
kenjiArai | 0:5b88d5760320 | 311 | * Authentication: reset counter and crypt/mask internal tag |
kenjiArai | 0:5b88d5760320 | 312 | */ |
kenjiArai | 0:5b88d5760320 | 313 | for( i = 0; i < q; i++ ) |
kenjiArai | 0:5b88d5760320 | 314 | ctr[15-i] = 0; |
kenjiArai | 0:5b88d5760320 | 315 | |
kenjiArai | 0:5b88d5760320 | 316 | CTR_CRYPT( y, y, 16 ); |
kenjiArai | 0:5b88d5760320 | 317 | memcpy( tag, y, tag_len ); |
kenjiArai | 0:5b88d5760320 | 318 | |
kenjiArai | 0:5b88d5760320 | 319 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 320 | } |
kenjiArai | 0:5b88d5760320 | 321 | |
kenjiArai | 0:5b88d5760320 | 322 | /* |
kenjiArai | 0:5b88d5760320 | 323 | * Authenticated encryption |
kenjiArai | 0:5b88d5760320 | 324 | */ |
kenjiArai | 0:5b88d5760320 | 325 | int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, |
kenjiArai | 0:5b88d5760320 | 326 | const unsigned char *iv, size_t iv_len, |
kenjiArai | 0:5b88d5760320 | 327 | const unsigned char *add, size_t add_len, |
kenjiArai | 0:5b88d5760320 | 328 | const unsigned char *input, unsigned char *output, |
kenjiArai | 0:5b88d5760320 | 329 | unsigned char *tag, size_t tag_len ) |
kenjiArai | 0:5b88d5760320 | 330 | { |
kenjiArai | 0:5b88d5760320 | 331 | CCM_VALIDATE_RET( ctx != NULL ); |
kenjiArai | 0:5b88d5760320 | 332 | CCM_VALIDATE_RET( iv != NULL ); |
kenjiArai | 0:5b88d5760320 | 333 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
kenjiArai | 0:5b88d5760320 | 334 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
kenjiArai | 0:5b88d5760320 | 335 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
kenjiArai | 0:5b88d5760320 | 336 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
kenjiArai | 0:5b88d5760320 | 337 | return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len, |
kenjiArai | 0:5b88d5760320 | 338 | add, add_len, input, output, tag, tag_len ) ); |
kenjiArai | 0:5b88d5760320 | 339 | } |
kenjiArai | 0:5b88d5760320 | 340 | |
kenjiArai | 0:5b88d5760320 | 341 | int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, |
kenjiArai | 0:5b88d5760320 | 342 | const unsigned char *iv, size_t iv_len, |
kenjiArai | 0:5b88d5760320 | 343 | const unsigned char *add, size_t add_len, |
kenjiArai | 0:5b88d5760320 | 344 | const unsigned char *input, unsigned char *output, |
kenjiArai | 0:5b88d5760320 | 345 | unsigned char *tag, size_t tag_len ) |
kenjiArai | 0:5b88d5760320 | 346 | { |
kenjiArai | 0:5b88d5760320 | 347 | CCM_VALIDATE_RET( ctx != NULL ); |
kenjiArai | 0:5b88d5760320 | 348 | CCM_VALIDATE_RET( iv != NULL ); |
kenjiArai | 0:5b88d5760320 | 349 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
kenjiArai | 0:5b88d5760320 | 350 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
kenjiArai | 0:5b88d5760320 | 351 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
kenjiArai | 0:5b88d5760320 | 352 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
kenjiArai | 0:5b88d5760320 | 353 | if( tag_len == 0 ) |
kenjiArai | 0:5b88d5760320 | 354 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
kenjiArai | 0:5b88d5760320 | 355 | |
kenjiArai | 0:5b88d5760320 | 356 | return( mbedtls_ccm_star_encrypt_and_tag( ctx, length, iv, iv_len, add, |
kenjiArai | 0:5b88d5760320 | 357 | add_len, input, output, tag, tag_len ) ); |
kenjiArai | 0:5b88d5760320 | 358 | } |
kenjiArai | 0:5b88d5760320 | 359 | |
kenjiArai | 0:5b88d5760320 | 360 | /* |
kenjiArai | 0:5b88d5760320 | 361 | * Authenticated decryption |
kenjiArai | 0:5b88d5760320 | 362 | */ |
kenjiArai | 0:5b88d5760320 | 363 | int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, |
kenjiArai | 0:5b88d5760320 | 364 | const unsigned char *iv, size_t iv_len, |
kenjiArai | 0:5b88d5760320 | 365 | const unsigned char *add, size_t add_len, |
kenjiArai | 0:5b88d5760320 | 366 | const unsigned char *input, unsigned char *output, |
kenjiArai | 0:5b88d5760320 | 367 | const unsigned char *tag, size_t tag_len ) |
kenjiArai | 0:5b88d5760320 | 368 | { |
kenjiArai | 0:5b88d5760320 | 369 | int ret; |
kenjiArai | 0:5b88d5760320 | 370 | unsigned char check_tag[16]; |
kenjiArai | 0:5b88d5760320 | 371 | unsigned char i; |
kenjiArai | 0:5b88d5760320 | 372 | int diff; |
kenjiArai | 0:5b88d5760320 | 373 | |
kenjiArai | 0:5b88d5760320 | 374 | CCM_VALIDATE_RET( ctx != NULL ); |
kenjiArai | 0:5b88d5760320 | 375 | CCM_VALIDATE_RET( iv != NULL ); |
kenjiArai | 0:5b88d5760320 | 376 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
kenjiArai | 0:5b88d5760320 | 377 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
kenjiArai | 0:5b88d5760320 | 378 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
kenjiArai | 0:5b88d5760320 | 379 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
kenjiArai | 0:5b88d5760320 | 380 | |
kenjiArai | 0:5b88d5760320 | 381 | if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length, |
kenjiArai | 0:5b88d5760320 | 382 | iv, iv_len, add, add_len, |
kenjiArai | 0:5b88d5760320 | 383 | input, output, check_tag, tag_len ) ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 384 | { |
kenjiArai | 0:5b88d5760320 | 385 | return( ret ); |
kenjiArai | 0:5b88d5760320 | 386 | } |
kenjiArai | 0:5b88d5760320 | 387 | |
kenjiArai | 0:5b88d5760320 | 388 | /* Check tag in "constant-time" */ |
kenjiArai | 0:5b88d5760320 | 389 | for( diff = 0, i = 0; i < tag_len; i++ ) |
kenjiArai | 0:5b88d5760320 | 390 | diff |= tag[i] ^ check_tag[i]; |
kenjiArai | 0:5b88d5760320 | 391 | |
kenjiArai | 0:5b88d5760320 | 392 | if( diff != 0 ) |
kenjiArai | 0:5b88d5760320 | 393 | { |
kenjiArai | 0:5b88d5760320 | 394 | mbedtls_platform_zeroize( output, length ); |
kenjiArai | 0:5b88d5760320 | 395 | return( MBEDTLS_ERR_CCM_AUTH_FAILED ); |
kenjiArai | 0:5b88d5760320 | 396 | } |
kenjiArai | 0:5b88d5760320 | 397 | |
kenjiArai | 0:5b88d5760320 | 398 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 399 | } |
kenjiArai | 0:5b88d5760320 | 400 | |
kenjiArai | 0:5b88d5760320 | 401 | int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, |
kenjiArai | 0:5b88d5760320 | 402 | const unsigned char *iv, size_t iv_len, |
kenjiArai | 0:5b88d5760320 | 403 | const unsigned char *add, size_t add_len, |
kenjiArai | 0:5b88d5760320 | 404 | const unsigned char *input, unsigned char *output, |
kenjiArai | 0:5b88d5760320 | 405 | const unsigned char *tag, size_t tag_len ) |
kenjiArai | 0:5b88d5760320 | 406 | { |
kenjiArai | 0:5b88d5760320 | 407 | CCM_VALIDATE_RET( ctx != NULL ); |
kenjiArai | 0:5b88d5760320 | 408 | CCM_VALIDATE_RET( iv != NULL ); |
kenjiArai | 0:5b88d5760320 | 409 | CCM_VALIDATE_RET( add_len == 0 || add != NULL ); |
kenjiArai | 0:5b88d5760320 | 410 | CCM_VALIDATE_RET( length == 0 || input != NULL ); |
kenjiArai | 0:5b88d5760320 | 411 | CCM_VALIDATE_RET( length == 0 || output != NULL ); |
kenjiArai | 0:5b88d5760320 | 412 | CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); |
kenjiArai | 0:5b88d5760320 | 413 | |
kenjiArai | 0:5b88d5760320 | 414 | if( tag_len == 0 ) |
kenjiArai | 0:5b88d5760320 | 415 | return( MBEDTLS_ERR_CCM_BAD_INPUT ); |
kenjiArai | 0:5b88d5760320 | 416 | |
kenjiArai | 0:5b88d5760320 | 417 | return( mbedtls_ccm_star_auth_decrypt( ctx, length, iv, iv_len, add, |
kenjiArai | 0:5b88d5760320 | 418 | add_len, input, output, tag, tag_len ) ); |
kenjiArai | 0:5b88d5760320 | 419 | } |
kenjiArai | 0:5b88d5760320 | 420 | #endif /* !MBEDTLS_CCM_ALT */ |
kenjiArai | 0:5b88d5760320 | 421 | |
kenjiArai | 0:5b88d5760320 | 422 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
kenjiArai | 0:5b88d5760320 | 423 | /* |
kenjiArai | 0:5b88d5760320 | 424 | * Examples 1 to 3 from SP800-38C Appendix C |
kenjiArai | 0:5b88d5760320 | 425 | */ |
kenjiArai | 0:5b88d5760320 | 426 | |
kenjiArai | 0:5b88d5760320 | 427 | #define NB_TESTS 3 |
kenjiArai | 0:5b88d5760320 | 428 | #define CCM_SELFTEST_PT_MAX_LEN 24 |
kenjiArai | 0:5b88d5760320 | 429 | #define CCM_SELFTEST_CT_MAX_LEN 32 |
kenjiArai | 0:5b88d5760320 | 430 | /* |
kenjiArai | 0:5b88d5760320 | 431 | * The data is the same for all tests, only the used length changes |
kenjiArai | 0:5b88d5760320 | 432 | */ |
kenjiArai | 0:5b88d5760320 | 433 | static const unsigned char key_test_data[] = { |
kenjiArai | 0:5b88d5760320 | 434 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
kenjiArai | 0:5b88d5760320 | 435 | 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f |
kenjiArai | 0:5b88d5760320 | 436 | }; |
kenjiArai | 0:5b88d5760320 | 437 | |
kenjiArai | 0:5b88d5760320 | 438 | static const unsigned char iv_test_data[] = { |
kenjiArai | 0:5b88d5760320 | 439 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
kenjiArai | 0:5b88d5760320 | 440 | 0x18, 0x19, 0x1a, 0x1b |
kenjiArai | 0:5b88d5760320 | 441 | }; |
kenjiArai | 0:5b88d5760320 | 442 | |
kenjiArai | 0:5b88d5760320 | 443 | static const unsigned char ad_test_data[] = { |
kenjiArai | 0:5b88d5760320 | 444 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
kenjiArai | 0:5b88d5760320 | 445 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
kenjiArai | 0:5b88d5760320 | 446 | 0x10, 0x11, 0x12, 0x13 |
kenjiArai | 0:5b88d5760320 | 447 | }; |
kenjiArai | 0:5b88d5760320 | 448 | |
kenjiArai | 0:5b88d5760320 | 449 | static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = { |
kenjiArai | 0:5b88d5760320 | 450 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
kenjiArai | 0:5b88d5760320 | 451 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
kenjiArai | 0:5b88d5760320 | 452 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
kenjiArai | 0:5b88d5760320 | 453 | }; |
kenjiArai | 0:5b88d5760320 | 454 | |
kenjiArai | 0:5b88d5760320 | 455 | static const size_t iv_len_test_data [NB_TESTS] = { 7, 8, 12 }; |
kenjiArai | 0:5b88d5760320 | 456 | static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 }; |
kenjiArai | 0:5b88d5760320 | 457 | static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 }; |
kenjiArai | 0:5b88d5760320 | 458 | static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 }; |
kenjiArai | 0:5b88d5760320 | 459 | |
kenjiArai | 0:5b88d5760320 | 460 | static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = { |
kenjiArai | 0:5b88d5760320 | 461 | { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d }, |
kenjiArai | 0:5b88d5760320 | 462 | { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, |
kenjiArai | 0:5b88d5760320 | 463 | 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, |
kenjiArai | 0:5b88d5760320 | 464 | 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd }, |
kenjiArai | 0:5b88d5760320 | 465 | { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, |
kenjiArai | 0:5b88d5760320 | 466 | 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, |
kenjiArai | 0:5b88d5760320 | 467 | 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, |
kenjiArai | 0:5b88d5760320 | 468 | 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 } |
kenjiArai | 0:5b88d5760320 | 469 | }; |
kenjiArai | 0:5b88d5760320 | 470 | |
kenjiArai | 0:5b88d5760320 | 471 | int mbedtls_ccm_self_test( int verbose ) |
kenjiArai | 0:5b88d5760320 | 472 | { |
kenjiArai | 0:5b88d5760320 | 473 | mbedtls_ccm_context ctx; |
kenjiArai | 0:5b88d5760320 | 474 | /* |
kenjiArai | 0:5b88d5760320 | 475 | * Some hardware accelerators require the input and output buffers |
kenjiArai | 0:5b88d5760320 | 476 | * would be in RAM, because the flash is not accessible. |
kenjiArai | 0:5b88d5760320 | 477 | * Use buffers on the stack to hold the test vectors data. |
kenjiArai | 0:5b88d5760320 | 478 | */ |
kenjiArai | 0:5b88d5760320 | 479 | unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN]; |
kenjiArai | 0:5b88d5760320 | 480 | unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN]; |
kenjiArai | 0:5b88d5760320 | 481 | size_t i; |
kenjiArai | 0:5b88d5760320 | 482 | int ret; |
kenjiArai | 0:5b88d5760320 | 483 | |
kenjiArai | 0:5b88d5760320 | 484 | mbedtls_ccm_init( &ctx ); |
kenjiArai | 0:5b88d5760320 | 485 | |
kenjiArai | 0:5b88d5760320 | 486 | if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key_test_data, |
kenjiArai | 0:5b88d5760320 | 487 | 8 * sizeof key_test_data ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 488 | { |
kenjiArai | 0:5b88d5760320 | 489 | if( verbose != 0 ) |
kenjiArai | 0:5b88d5760320 | 490 | mbedtls_printf( " CCM: setup failed" ); |
kenjiArai | 0:5b88d5760320 | 491 | |
kenjiArai | 0:5b88d5760320 | 492 | return( 1 ); |
kenjiArai | 0:5b88d5760320 | 493 | } |
kenjiArai | 0:5b88d5760320 | 494 | |
kenjiArai | 0:5b88d5760320 | 495 | for( i = 0; i < NB_TESTS; i++ ) |
kenjiArai | 0:5b88d5760320 | 496 | { |
kenjiArai | 0:5b88d5760320 | 497 | if( verbose != 0 ) |
kenjiArai | 0:5b88d5760320 | 498 | mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 ); |
kenjiArai | 0:5b88d5760320 | 499 | |
kenjiArai | 0:5b88d5760320 | 500 | memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN ); |
kenjiArai | 0:5b88d5760320 | 501 | memset( ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN ); |
kenjiArai | 0:5b88d5760320 | 502 | memcpy( plaintext, msg_test_data, msg_len_test_data[i] ); |
kenjiArai | 0:5b88d5760320 | 503 | |
kenjiArai | 0:5b88d5760320 | 504 | ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len_test_data[i], |
kenjiArai | 0:5b88d5760320 | 505 | iv_test_data, iv_len_test_data[i], |
kenjiArai | 0:5b88d5760320 | 506 | ad_test_data, add_len_test_data[i], |
kenjiArai | 0:5b88d5760320 | 507 | plaintext, ciphertext, |
kenjiArai | 0:5b88d5760320 | 508 | ciphertext + msg_len_test_data[i], |
kenjiArai | 0:5b88d5760320 | 509 | tag_len_test_data[i] ); |
kenjiArai | 0:5b88d5760320 | 510 | |
kenjiArai | 0:5b88d5760320 | 511 | if( ret != 0 || |
kenjiArai | 0:5b88d5760320 | 512 | memcmp( ciphertext, res_test_data[i], |
kenjiArai | 0:5b88d5760320 | 513 | msg_len_test_data[i] + tag_len_test_data[i] ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 514 | { |
kenjiArai | 0:5b88d5760320 | 515 | if( verbose != 0 ) |
kenjiArai | 0:5b88d5760320 | 516 | mbedtls_printf( "failed\n" ); |
kenjiArai | 0:5b88d5760320 | 517 | |
kenjiArai | 0:5b88d5760320 | 518 | return( 1 ); |
kenjiArai | 0:5b88d5760320 | 519 | } |
kenjiArai | 0:5b88d5760320 | 520 | memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN ); |
kenjiArai | 0:5b88d5760320 | 521 | |
kenjiArai | 0:5b88d5760320 | 522 | ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len_test_data[i], |
kenjiArai | 0:5b88d5760320 | 523 | iv_test_data, iv_len_test_data[i], |
kenjiArai | 0:5b88d5760320 | 524 | ad_test_data, add_len_test_data[i], |
kenjiArai | 0:5b88d5760320 | 525 | ciphertext, plaintext, |
kenjiArai | 0:5b88d5760320 | 526 | ciphertext + msg_len_test_data[i], |
kenjiArai | 0:5b88d5760320 | 527 | tag_len_test_data[i] ); |
kenjiArai | 0:5b88d5760320 | 528 | |
kenjiArai | 0:5b88d5760320 | 529 | if( ret != 0 || |
kenjiArai | 0:5b88d5760320 | 530 | memcmp( plaintext, msg_test_data, msg_len_test_data[i] ) != 0 ) |
kenjiArai | 0:5b88d5760320 | 531 | { |
kenjiArai | 0:5b88d5760320 | 532 | if( verbose != 0 ) |
kenjiArai | 0:5b88d5760320 | 533 | mbedtls_printf( "failed\n" ); |
kenjiArai | 0:5b88d5760320 | 534 | |
kenjiArai | 0:5b88d5760320 | 535 | return( 1 ); |
kenjiArai | 0:5b88d5760320 | 536 | } |
kenjiArai | 0:5b88d5760320 | 537 | |
kenjiArai | 0:5b88d5760320 | 538 | if( verbose != 0 ) |
kenjiArai | 0:5b88d5760320 | 539 | mbedtls_printf( "passed\n" ); |
kenjiArai | 0:5b88d5760320 | 540 | } |
kenjiArai | 0:5b88d5760320 | 541 | |
kenjiArai | 0:5b88d5760320 | 542 | mbedtls_ccm_free( &ctx ); |
kenjiArai | 0:5b88d5760320 | 543 | |
kenjiArai | 0:5b88d5760320 | 544 | if( verbose != 0 ) |
kenjiArai | 0:5b88d5760320 | 545 | mbedtls_printf( "\n" ); |
kenjiArai | 0:5b88d5760320 | 546 | |
kenjiArai | 0:5b88d5760320 | 547 | return( 0 ); |
kenjiArai | 0:5b88d5760320 | 548 | } |
kenjiArai | 0:5b88d5760320 | 549 | |
kenjiArai | 0:5b88d5760320 | 550 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
kenjiArai | 0:5b88d5760320 | 551 | |
kenjiArai | 0:5b88d5760320 | 552 | #endif /* MBEDTLS_CCM_C */ |