22
22
#if defined(MBEDTLS_CCM_ALT )
23
23
#include <string.h>
24
24
#include "mbedtls/platform.h"
25
+ #include "mbedtls/aes.h"
25
26
26
27
/* Implementation that should never be optimized out by the compiler */
27
28
static void mbedtls_zeroize ( void * v , size_t n ) {
28
29
volatile unsigned char * p = (unsigned char * )v ;
29
30
while ( n -- ) * p ++ = 0 ;
30
31
}
31
32
32
- #define CCM_ENCRYPT 0
33
- #define CCM_DECRYPT 1
34
-
35
-
36
- /*
37
- * Macros for common operations.
38
- * Results in smaller compiled code than static inline functions.
39
- */
40
-
41
- /*
42
- * Update the CBC-MAC state in y using a block in b
43
- * (Always using b as the source helps the compiler optimise a bit better.)
44
- */
45
- #define UPDATE_CBC_MAC \
46
- for( i = 0; i < 16; i++ ) \
47
- y[i] ^= b[i]; \
48
- \
49
- if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
50
- return ( ret );
51
-
52
- /*
53
- * Encrypt or decrypt a partial block with CTR
54
- * Warning: using b for temporary storage! src and dst must not be b!
55
- * This avoids allocating one more 16 bytes buffer while allowing src == dst.
56
- */
57
- #define CTR_CRYPT ( dst , src , len ) \
58
- if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 ) \
59
- return ( ret ); \
60
- \
61
- for( i = 0; i < len; i++ ) \
62
- dst[i] = src[i] ^ b[i];
63
-
64
33
void mbedtls_ccm_init ( mbedtls_ccm_context * ctx )
65
34
{
66
35
memset ( ctx , 0 , sizeof ( mbedtls_ccm_context ) );
@@ -71,30 +40,6 @@ void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
71
40
mbedtls_zeroize ( ctx , sizeof ( mbedtls_ccm_context ) );
72
41
}
73
42
74
- static int mbedtls_ccm_setkey_sw ( mbedtls_ccm_sw_context * ctx ,
75
- mbedtls_cipher_id_t cipher ,
76
- const unsigned char * key ,
77
- unsigned int keybits )
78
- {
79
-
80
- int ret = 0 ;
81
- const mbedtls_cipher_info_t * cipher_info = NULL ;
82
-
83
- cipher_info = mbedtls_cipher_info_from_values ( cipher , keybits , MBEDTLS_MODE_ECB );
84
- if ( cipher_info == NULL )
85
- return ( MBEDTLS_ERR_CCM_BAD_INPUT );
86
-
87
- if ( cipher_info -> block_size != 16 )
88
- return ( MBEDTLS_ERR_CCM_BAD_INPUT );
89
-
90
- mbedtls_cipher_free ( & ctx -> cipher_ctx );
91
-
92
- if ( ( ret = mbedtls_cipher_setup ( & ctx -> cipher_ctx , cipher_info ) ) != 0 )
93
- return ( ret );
94
-
95
- return ( mbedtls_cipher_setkey ( & ctx -> cipher_ctx , key , keybits , MBEDTLS_ENCRYPT ) );
96
- }
97
-
98
43
int mbedtls_ccm_setkey ( mbedtls_ccm_context * ctx ,
99
44
mbedtls_cipher_id_t cipher ,
100
45
const unsigned char * key ,
@@ -106,14 +51,11 @@ int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
106
51
if ( cipher != MBEDTLS_CIPHER_ID_AES ||
107
52
keybits != 128 )
108
53
{
109
- ctx -> is_sw = 1 ;
110
- return ( mbedtls_ccm_setkey_sw ( & ctx -> u_ctx .ccm_sw_ctx , cipher , key , keybits ) );
54
+ return ( MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE );
111
55
}
112
56
113
- ctx -> is_sw = 0 ;
114
-
115
- memcpy ( ctx -> u_ctx .ccm_alt_ctx .cipher_key , key , keybits /8 );
116
- ctx -> u_ctx .ccm_alt_ctx .keySize_ID = CRYS_AES_Key128BitSize ;
57
+ memcpy ( ctx -> cipher_key , key , keybits /8 );
58
+ ctx -> keySize_ID = CRYS_AES_Key128BitSize ;
117
59
118
60
return ( 0 );
119
61
@@ -122,159 +64,6 @@ int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
122
64
/*
123
65
* Authenticated encryption or decryption
124
66
*/
125
- static int ccm_auth_crypt_sw ( mbedtls_ccm_sw_context * ctx , int mode , size_t length ,
126
- const unsigned char * iv , size_t iv_len ,
127
- const unsigned char * add , size_t add_len ,
128
- const unsigned char * input , unsigned char * output ,
129
- unsigned char * tag , size_t tag_len )
130
- {
131
- int ret ;
132
- unsigned char i ;
133
- unsigned char q ;
134
- size_t len_left , olen ;
135
- unsigned char b [16 ];
136
- unsigned char y [16 ];
137
- unsigned char ctr [16 ];
138
- const unsigned char * src ;
139
- unsigned char * dst ;
140
-
141
- if ( add_len > 0xFF00 )
142
- return ( MBEDTLS_ERR_CCM_BAD_INPUT );
143
-
144
- q = 16 - 1 - (unsigned char ) iv_len ;
145
-
146
- /*
147
- * First block B_0:
148
- * 0 .. 0 flags
149
- * 1 .. iv_len nonce (aka iv)
150
- * iv_len+1 .. 15 length
151
- *
152
- * With flags as (bits):
153
- * 7 0
154
- * 6 add present?
155
- * 5 .. 3 (t - 2) / 2
156
- * 2 .. 0 q - 1
157
- */
158
- b [0 ] = 0 ;
159
- b [0 ] |= ( add_len > 0 ) << 6 ;
160
- b [0 ] |= ( ( tag_len - 2 ) / 2 ) << 3 ;
161
- b [0 ] |= q - 1 ;
162
-
163
- memcpy ( b + 1 , iv , iv_len );
164
-
165
- for ( i = 0 , len_left = length ; i < q ; i ++ , len_left >>= 8 )
166
- b [15 - i ] = (unsigned char )( len_left & 0xFF );
167
-
168
- if ( len_left > 0 )
169
- return ( MBEDTLS_ERR_CCM_BAD_INPUT );
170
-
171
-
172
- /* Start CBC-MAC with first block */
173
- memset ( y , 0 , 16 );
174
- UPDATE_CBC_MAC ;
175
-
176
- /*
177
- * If there is additional data, update CBC-MAC with
178
- * add_len, add, 0 (padding to a block boundary)
179
- */
180
- if ( add_len > 0 )
181
- {
182
- size_t use_len ;
183
- len_left = add_len ;
184
- src = add ;
185
-
186
- memset ( b , 0 , 16 );
187
- b [0 ] = (unsigned char )( ( add_len >> 8 ) & 0xFF );
188
- b [1 ] = (unsigned char )( ( add_len ) & 0xFF );
189
-
190
- use_len = len_left < 16 - 2 ? len_left : 16 - 2 ;
191
- memcpy ( b + 2 , src , use_len );
192
- len_left -= use_len ;
193
- src += use_len ;
194
-
195
- UPDATE_CBC_MAC ;
196
- while ( len_left > 0 )
197
- {
198
- use_len = len_left > 16 ? 16 : len_left ;
199
-
200
- memset ( b , 0 , 16 );
201
- memcpy ( b , src , use_len );
202
- UPDATE_CBC_MAC ;
203
-
204
- len_left -= use_len ;
205
- src += use_len ;
206
- }
207
- }
208
-
209
- /*
210
- * Prepare counter block for encryption:
211
- * 0 .. 0 flags
212
- * 1 .. iv_len nonce (aka iv)
213
- * iv_len+1 .. 15 counter (initially 1)
214
- *
215
- * With flags as (bits):
216
- * 7 .. 3 0
217
- * 2 .. 0 q - 1
218
- */
219
- ctr [0 ] = q - 1 ;
220
- memcpy ( ctr + 1 , iv , iv_len );
221
- memset ( ctr + 1 + iv_len , 0 , q );
222
- ctr [15 ] = 1 ;
223
-
224
- /*
225
- * Authenticate and {en,de}crypt the message.
226
- *
227
- * The only difference between encryption and decryption is
228
- * the respective order of authentication and {en,de}cryption.
229
- */
230
- len_left = length ;
231
- src = input ;
232
- dst = output ;
233
-
234
- while ( len_left > 0 )
235
- {
236
- size_t use_len = len_left > 16 ? 16 : len_left ;
237
-
238
- if ( mode == CCM_ENCRYPT )
239
- {
240
- memset ( b , 0 , 16 );
241
- memcpy ( b , src , use_len );
242
- UPDATE_CBC_MAC ;
243
- }
244
-
245
- CTR_CRYPT ( dst , src , use_len );
246
-
247
- if ( mode == CCM_DECRYPT )
248
- {
249
- memset ( b , 0 , 16 );
250
- memcpy ( b , dst , use_len );
251
- UPDATE_CBC_MAC ;
252
- }
253
-
254
- dst += use_len ;
255
- src += use_len ;
256
- len_left -= use_len ;
257
-
258
- /*
259
- * Increment counter.
260
- * No need to check for overflow thanks to the length check above.
261
- */
262
- for ( i = 0 ; i < q ; i ++ )
263
- if ( ++ ctr [15 - i ] != 0 )
264
- break ;
265
- }
266
-
267
- /*
268
- * Authentication: reset counter and crypt/mask internal tag
269
- */
270
- for ( i = 0 ; i < q ; i ++ )
271
- ctr [15 - i ] = 0 ;
272
-
273
- CTR_CRYPT ( y , y , 16 );
274
- memcpy ( tag , y , tag_len );
275
-
276
- return ( 0 );
277
- }
278
67
279
68
int mbedtls_ccm_encrypt_and_tag ( mbedtls_ccm_context * ctx , size_t length ,
280
69
const unsigned char * iv , size_t iv_len ,
@@ -296,16 +85,11 @@ int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
296
85
if ( iv_len < 7 || iv_len > 13 )
297
86
return ( MBEDTLS_ERR_CCM_BAD_INPUT );
298
87
299
- if ( ctx -> is_sw )
300
- return ( ccm_auth_crypt_sw ( & ctx -> u_ctx .ccm_sw_ctx , CCM_ENCRYPT , length , iv , iv_len ,
301
- add , add_len , input , output ,
302
- tag , tag_len ) );
303
-
304
88
if ( length > 0xFFFFFFFF || add_len > 0xFFFFFFFF )
305
89
return ( MBEDTLS_ERR_CCM_BAD_INPUT );
306
90
307
- CrysRet = CRYS_AESCCM ( SASI_AES_ENCRYPT , ctx -> u_ctx . ccm_alt_ctx . cipher_key , ctx -> u_ctx . ccm_alt_ctx . keySize_ID ,
308
- ( uint8_t * ) iv , iv_len , (uint8_t * )add , add_len , (uint8_t * )input , length , output , tag_len , tag );
91
+ CrysRet = CRYS_AESCCM ( SASI_AES_ENCRYPT , ctx -> cipher_key , ctx -> keySize_ID ,( uint8_t * ) iv , iv_len ,
92
+ (uint8_t * )add , add_len , (uint8_t * )input , length , output , tag_len , tag );
309
93
if ( CrysRet != CRYS_OK )
310
94
return ( MBEDTLS_ERR_CCM_HW_ACCEL_FAILED );
311
95
@@ -316,37 +100,6 @@ int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
316
100
/*
317
101
* Authenticated decryption
318
102
*/
319
- static int mbedtls_ccm_auth_decrypt_sw ( mbedtls_ccm_sw_context * ctx , size_t length ,
320
- const unsigned char * iv , size_t iv_len ,
321
- const unsigned char * add , size_t add_len ,
322
- const unsigned char * input , unsigned char * output ,
323
- const unsigned char * tag , size_t tag_len )
324
- {
325
- int ret ;
326
- unsigned char check_tag [16 ];
327
- unsigned char i ;
328
- int diff ;
329
-
330
- if ( ( ret = ccm_auth_crypt_sw ( ctx , CCM_DECRYPT , length ,
331
- iv , iv_len , add , add_len ,
332
- input , output , check_tag , tag_len ) ) != 0 )
333
- {
334
- return ( ret );
335
- }
336
-
337
- /* Check tag in "constant-time" */
338
- for ( diff = 0 , i = 0 ; i < tag_len ; i ++ )
339
- diff |= tag [i ] ^ check_tag [i ];
340
-
341
- if ( diff != 0 )
342
- {
343
- mbedtls_zeroize ( output , length );
344
- return ( MBEDTLS_ERR_CCM_AUTH_FAILED );
345
- }
346
-
347
- return ( 0 );
348
- }
349
-
350
103
int mbedtls_ccm_auth_decrypt ( mbedtls_ccm_context * ctx , size_t length ,
351
104
const unsigned char * iv , size_t iv_len ,
352
105
const unsigned char * add , size_t add_len ,
@@ -367,16 +120,11 @@ int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
367
120
if ( iv_len < 7 || iv_len > 13 )
368
121
return ( MBEDTLS_ERR_CCM_BAD_INPUT );
369
122
370
- if ( ctx -> is_sw )
371
- return ( mbedtls_ccm_auth_decrypt_sw ( & ctx -> u_ctx .ccm_sw_ctx , length , iv , iv_len ,
372
- add , add_len , input , output ,
373
- tag , tag_len ) );
374
-
375
123
if ( length > 0xFFFFFFFF || add_len > 0xFFFFFFFF )
376
124
return ( MBEDTLS_ERR_CCM_BAD_INPUT );
377
125
378
- CrysRet = CRYS_AESCCM ( SASI_AES_DECRYPT , ctx -> u_ctx . ccm_alt_ctx . cipher_key , ctx -> u_ctx . ccm_alt_ctx . keySize_ID ,
379
- ( uint8_t * ) iv , iv_len , (uint8_t * )add , add_len , (uint8_t * )input , length , output , tag_len , (uint8_t * )tag );
126
+ CrysRet = CRYS_AESCCM ( SASI_AES_DECRYPT , ctx -> cipher_key , ctx -> keySize_ID ,( uint8_t * ) iv , iv_len ,
127
+ (uint8_t * )add , add_len , (uint8_t * )input , length , output , tag_len , (uint8_t * )tag );
380
128
if ( CrysRet != CRYS_OK )
381
129
return ( MBEDTLS_ERR_CCM_HW_ACCEL_FAILED );
382
130
0 commit comments