Skip to content

Commit adbfdc1

Browse files
Ron EldorRon Eldor
authored andcommitted
Remove SW fallback for CCM
Remove SW fallback for CCM mode, and return `MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE` when trying to set a key which is not 128 bit size.
1 parent ccaef16 commit adbfdc1

File tree

2 files changed

+8
-275
lines changed

2 files changed

+8
-275
lines changed

features/mbedtls/targets/TARGET_CRYPTOCELL310/ccm_alt.c

Lines changed: 8 additions & 260 deletions
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,14 @@
2222
#if defined(MBEDTLS_CCM_ALT)
2323
#include <string.h>
2424
#include "mbedtls/platform.h"
25+
#include "mbedtls/aes.h"
2526

2627
/* Implementation that should never be optimized out by the compiler */
2728
static void mbedtls_zeroize( void *v, size_t n ) {
2829
volatile unsigned char *p = (unsigned char*)v;
2930
while( n-- ) *p++ = 0;
3031
}
3132

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-
6433
void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
6534
{
6635
memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
@@ -71,30 +40,6 @@ void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
7140
mbedtls_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
7241
}
7342

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-
9843
int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
9944
mbedtls_cipher_id_t cipher,
10045
const unsigned char *key,
@@ -106,14 +51,11 @@ int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
10651
if( cipher != MBEDTLS_CIPHER_ID_AES ||
10752
keybits != 128 )
10853
{
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 );
11155
}
11256

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;
11759

11860
return ( 0 );
11961

@@ -122,159 +64,6 @@ int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
12264
/*
12365
* Authenticated encryption or decryption
12466
*/
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-
}
27867

27968
int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
28069
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,
29685
if( iv_len < 7 || iv_len > 13 )
29786
return ( MBEDTLS_ERR_CCM_BAD_INPUT );
29887

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-
30488
if( length > 0xFFFFFFFF || add_len > 0xFFFFFFFF )
30589
return ( MBEDTLS_ERR_CCM_BAD_INPUT );
30690

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 );
30993
if( CrysRet != CRYS_OK )
31094
return ( MBEDTLS_ERR_CCM_HW_ACCEL_FAILED );
31195

@@ -316,37 +100,6 @@ int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
316100
/*
317101
* Authenticated decryption
318102
*/
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-
350103
int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
351104
const unsigned char *iv, size_t iv_len,
352105
const unsigned char *add, size_t add_len,
@@ -367,16 +120,11 @@ int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
367120
if( iv_len < 7 || iv_len > 13 )
368121
return ( MBEDTLS_ERR_CCM_BAD_INPUT );
369122

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-
375123
if( length > 0xFFFFFFFF || add_len > 0xFFFFFFFF )
376124
return ( MBEDTLS_ERR_CCM_BAD_INPUT );
377125

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 );
380128
if ( CrysRet != CRYS_OK )
381129
return ( MBEDTLS_ERR_CCM_HW_ACCEL_FAILED );
382130

features/mbedtls/targets/TARGET_CRYPTOCELL310/ccm_alt.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,6 @@ typedef struct {
3232
CRYS_AESCCM_Key_t cipher_key; /*!< cipher key used */
3333
CRYS_AESCCM_KeySize_t keySize_ID;
3434
}
35-
mbedtls_ccm_alt_context_t;
36-
37-
typedef struct {
38-
mbedtls_cipher_context_t cipher_ctx; /*!< cipher context used */
39-
}
40-
mbedtls_ccm_sw_context;
41-
42-
typedef struct {
43-
union
44-
{
45-
mbedtls_ccm_alt_context_t ccm_alt_ctx;
46-
mbedtls_ccm_sw_context ccm_sw_ctx;
47-
} u_ctx;
48-
unsigned int is_sw;
49-
}
5035
mbedtls_ccm_context;
5136

5237
/**

0 commit comments

Comments
 (0)