Skip to content

Commit c5ac3ee

Browse files
stevew817adbridge
authored andcommitted
Applied @yanesca and @andresag01 comments (#4825)
1 parent 843d5c1 commit c5ac3ee

File tree

5 files changed

+115
-92
lines changed

5 files changed

+115
-92
lines changed

features/mbedtls/targets/TARGET_Silicon_Labs/aes_aes.c

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
*/
2020

2121
#include "mbedtls/aes.h"
22-
23-
#if defined(MBEDTLS_AES_C)
24-
#if defined(MBEDTLS_AES_ALT)
25-
2622
#include "em_device.h"
2723

2824
#if defined(AES_PRESENT) && (AES_COUNT == 1)
25+
#if defined(MBEDTLS_AES_C)
26+
#if defined(MBEDTLS_AES_ALT)
2927
#include "em_aes.h"
3028
#include "em_cmu.h"
3129
#include "em_bus.h"
@@ -143,51 +141,21 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx,
143141
/*
144142
* AES-ECB block encryption
145143
*/
146-
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
147-
const unsigned char input[16],
148-
unsigned char output[16] )
144+
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
145+
const unsigned char input[16],
146+
unsigned char output[16] )
149147
{
150-
switch ( ctx->keybits )
151-
{
152-
case 128:
153-
aes_lock();
154-
AES_ECB128( output, input, 16, ctx->key, true );
155-
aes_unlock();
156-
break;
157-
case 256:
158-
aes_lock();
159-
AES_ECB256( output, input, 16, ctx->key, true );
160-
aes_unlock();
161-
break;
162-
default:
163-
// Error
164-
break;
165-
}
148+
return mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, input, output);
166149
}
167150

168151
/*
169152
* AES-ECB block decryption
170153
*/
171-
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
172-
const unsigned char input[16],
173-
unsigned char output[16] )
154+
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
155+
const unsigned char input[16],
156+
unsigned char output[16] )
174157
{
175-
switch ( ctx->keybits )
176-
{
177-
case 128:
178-
aes_lock();
179-
AES_ECB128( output, input, 16, ctx->key, false );
180-
aes_unlock();
181-
break;
182-
case 256:
183-
aes_lock();
184-
AES_ECB256( output, input, 16, ctx->key, false );
185-
aes_unlock();
186-
break;
187-
default:
188-
// Error
189-
break;
190-
}
158+
return mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_DECRYPT, input, output);
191159
}
192160

193161
/*
@@ -299,9 +267,9 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
299267
const unsigned char *input,
300268
unsigned char *output )
301269
{
302-
size_t n = iv_off ? *iv_off : 0;
270+
size_t n = ( iv_off != NULL ) ? *iv_off : 0;
303271

304-
if ( n || ( length & 0xf ) )
272+
if ( ( n > 0 ) || ( length & 0xf ) )
305273
{
306274
// IV offset or length not aligned to block size
307275
int c;
@@ -410,7 +378,7 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
410378
/*
411379
* AES-CTR Nonce update function
412380
*/
413-
void aes_ctr_update_nonce( uint8_t *nonce_counter )
381+
static void aes_ctr_update_nonce( uint8_t *nonce_counter )
414382
{
415383
for( size_t i = 16; i > 0; i-- )
416384
if( ++nonce_counter[i - 1] != 0 )
@@ -428,9 +396,9 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
428396
const unsigned char *input,
429397
unsigned char *output )
430398
{
431-
size_t n = nc_off ? *nc_off : 0;
399+
size_t n = ( nc_off != NULL ) ? *nc_off : 0;
432400

433-
if ( n || ( length & 0xf ) || ctx->keybits == 192 )
401+
if ( ( n > 0 ) || ( length & 0xf ) )
434402
{
435403
// IV offset or length not aligned to block size
436404
int c, i;
@@ -493,6 +461,6 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
493461
}
494462
#endif /* MBEDTLS_CIPHER_MODE_CTR */
495463

496-
#endif /* AES_PRESENT && (AES_COUNT == 1) */
497464
#endif /* MBEDTLS_AES_ALT */
498465
#endif /* MBEDTLS_AES_C */
466+
#endif /* AES_PRESENT && (AES_COUNT == 1) */

features/mbedtls/targets/TARGET_Silicon_Labs/aes_alt.h

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,23 +237,74 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
237237
* \param ctx AES context
238238
* \param input Plaintext block
239239
* \param output Output (ciphertext) block
240+
*
241+
* \return 0 if successful
242+
*/
243+
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
244+
const unsigned char input[16],
245+
unsigned char output[16] );
246+
247+
/**
248+
* \brief Internal AES block decryption function
249+
* (Only exposed to allow overriding it,
250+
* see MBEDTLS_AES_DECRYPT_ALT)
251+
*
252+
* \param ctx AES context
253+
* \param input Ciphertext block
254+
* \param output Output (plaintext) block
255+
*
256+
* \return 0 if successful
257+
*/
258+
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
259+
const unsigned char input[16],
260+
unsigned char output[16] );
261+
262+
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
263+
#if defined(MBEDTLS_DEPRECATED_WARNING)
264+
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
265+
#else
266+
#define MBEDTLS_DEPRECATED
267+
#endif
268+
/**
269+
* \brief Internal AES block encryption function
270+
* (Only exposed to allow overriding it,
271+
* see MBEDTLS_AES_ENCRYPT_ALT)
272+
*
273+
* \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0
274+
*
275+
* \param ctx AES context
276+
* \param input Plaintext block
277+
* \param output Output (ciphertext) block
240278
*/
241-
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
242-
const unsigned char input[16],
243-
unsigned char output[16] );
279+
MBEDTLS_DEPRECATED static inline void mbedtls_aes_encrypt(
280+
mbedtls_aes_context *ctx,
281+
const unsigned char input[16],
282+
unsigned char output[16] )
283+
{
284+
mbedtls_internal_aes_encrypt( ctx, input, output );
285+
}
244286

245287
/**
246288
* \brief Internal AES block decryption function
247289
* (Only exposed to allow overriding it,
248290
* see MBEDTLS_AES_DECRYPT_ALT)
249291
*
292+
* \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0
293+
*
250294
* \param ctx AES context
251295
* \param input Ciphertext block
252296
* \param output Output (plaintext) block
253297
*/
254-
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
255-
const unsigned char input[16],
256-
unsigned char output[16] );
298+
MBEDTLS_DEPRECATED static inline void mbedtls_aes_decrypt(
299+
mbedtls_aes_context *ctx,
300+
const unsigned char input[16],
301+
unsigned char output[16] )
302+
{
303+
mbedtls_internal_aes_decrypt( ctx, input, output );
304+
}
305+
306+
#undef MBEDTLS_DEPRECATED
307+
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
257308

258309
#ifdef __cplusplus
259310
}

features/mbedtls/targets/TARGET_Silicon_Labs/crypto_aes.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,36 @@
3131
*/
3232

3333
#include "mbedtls/aes.h"
34-
35-
#if defined(MBEDTLS_AES_C)
36-
#if defined(MBEDTLS_AES_ALT)
37-
3834
#include "em_device.h"
3935

4036
#if defined(CRYPTO_PRESENT)
37+
#if defined(MBEDTLS_AES_C)
38+
#if defined(MBEDTLS_AES_ALT)
4139

4240
#include "crypto_management.h"
4341
#include "em_crypto.h"
4442
#include "em_core.h"
4543
#include <string.h>
4644

4745
__STATIC_INLINE void CRYPTO_DataReadUnaligned(volatile uint32_t * reg,
48-
const uint8_t * val)
46+
uint8_t * const val)
4947
{
5048
/* Check data is 32bit aligned, if not, read into temporary buffer and
5149
then move to user buffer. */
5250
if ((uint32_t)val & 0x3)
5351
{
5452
uint32_t temp[4];
5553
CRYPTO_DataRead(reg, temp);
56-
memcpy((void*)val, temp, 16);
54+
memcpy(val, temp, 16);
5755
}
5856
else
5957
{
60-
CRYPTO_DataRead(reg, (uint32_t*)val);
58+
CRYPTO_DataRead(reg, (uint32_t* const)val);
6159
}
6260
}
6361

6462
__STATIC_INLINE void CRYPTO_DataWriteUnaligned(volatile uint32_t * reg,
65-
const uint8_t * val)
63+
uint8_t * const val)
6664
{
6765
/* Check data is 32bit aligned, if not move to temporary buffer before
6866
writing.*/
@@ -74,7 +72,7 @@ __STATIC_INLINE void CRYPTO_DataWriteUnaligned(volatile uint32_t * reg,
7472
}
7573
else
7674
{
77-
CRYPTO_DataWrite(reg, (uint32_t*)val);
75+
CRYPTO_DataWrite(reg, (uint32_t* const)val);
7876
}
7977
}
8078

@@ -113,8 +111,6 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx,
113111
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
114112
}
115113

116-
memset( ctx, 0, sizeof( mbedtls_aes_context ) );
117-
118114
if ( ( 128UL != keybits ) && ( 256UL != keybits ) ) {
119115
/* Unsupported key size */
120116
return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
@@ -139,8 +135,6 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx,
139135
return ( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
140136
}
141137

142-
memset( ctx, 0, sizeof( mbedtls_aes_context ) );
143-
144138
if ( ( 128UL != keybits ) && ( 256UL != keybits ) ) {
145139
/* Unsupported key size */
146140
return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
@@ -173,6 +167,26 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx,
173167
* functions with in-place implemented functions, to get much shorter
174168
* critical sections */
175169

170+
/*
171+
* AES-ECB block encryption
172+
*/
173+
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
174+
const unsigned char input[16],
175+
unsigned char output[16] )
176+
{
177+
return mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, input, output);
178+
}
179+
180+
/*
181+
* AES-ECB block decryption
182+
*/
183+
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
184+
const unsigned char input[16],
185+
unsigned char output[16] )
186+
{
187+
return mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_DECRYPT, input, output);
188+
}
189+
176190
/*
177191
* AES-ECB block encryption/decryption
178192
*/
@@ -538,8 +552,6 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
538552
}
539553
#endif /* MBEDTLS_CIPHER_MODE_CTR */
540554

541-
#endif /* CRYPTO_PRESENT */
542-
543555
#endif /* MBEDTLS_AES_ALT */
544-
545556
#endif /* MBEDTLS_AES_C */
557+
#endif /* CRYPTO_PRESENT */

0 commit comments

Comments
 (0)