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