1
- /*
2
- * AES hardware acceleration implementation
3
- *
4
- * Copyright (C) 2015-2020, Nuvoton, All Rights Reserved
5
- * SPDX-License-Identifier: Apache-2.0
1
+ /* mbed Microcontroller Library
2
+ * Copyright (c) 2015-2016 Nuvoton
6
3
*
7
- * Licensed under the Apache License, Version 2.0 (the "License"); you may
8
- * not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
10
7
*
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
8
+ * http://www.apache.org/licenses/LICENSE-2.0
18
9
*
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
19
15
*/
16
+
20
17
/*
21
18
* The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
22
19
*
31
28
#endif
32
29
33
30
#if defined(MBEDTLS_AES_C )
31
+ #if defined(MBEDTLS_AES_ALT )
34
32
35
33
#include <string.h>
36
34
37
35
#include "mbedtls/aes.h"
38
- #if defined(MBEDTLS_PADLOCK_C )
39
- #include "mbedtls/padlock.h"
40
- #endif
41
- #if defined(MBEDTLS_AESNI_C )
42
- #include "mbedtls/aesni.h"
43
- #endif
44
-
45
- #if defined(MBEDTLS_SELF_TEST )
46
- #if defined(MBEDTLS_PLATFORM_C )
47
- #include "mbedtls/platform.h"
48
- #else
49
- #include <stdio.h>
50
- #define mbedtls_printf printf
51
- #endif /* MBEDTLS_PLATFORM_C */
52
- #endif /* MBEDTLS_SELF_TEST */
53
36
54
37
#include "NUC472_442.h"
55
38
#include "toolchain.h"
39
+ #include "mbed_assert.h"
56
40
57
41
//static int aes_init_done = 0;
58
42
59
- #if defined(MBEDTLS_AES_ALT )
60
43
61
44
#define mbedtls_trace //printf
62
45
@@ -73,16 +56,34 @@ static uint32_t au32MyAESIV[4] = {
73
56
74
57
extern volatile int g_AES_done ;
75
58
76
- uint8_t au8OutputData [16 ] MBED_ALIGN (4 );
77
- uint8_t au8InputData [16 ] MBED_ALIGN (4 );
59
+ // Must be a multiple of 16 bytes block size
60
+ #define MAX_DMA_CHAIN_SIZE (16*6)
61
+ static uint8_t au8OutputData [MAX_DMA_CHAIN_SIZE ] MBED_ALIGN (4 );
62
+ static uint8_t au8InputData [MAX_DMA_CHAIN_SIZE ] MBED_ALIGN (4 );
78
63
79
- static void dumpHex (uint8_t au8Data [], int len )
64
+ static void dumpHex (const unsigned char au8Data [], int len )
80
65
{
81
66
int j ;
82
67
for (j = 0 ; j < len ; j ++ ) mbedtls_trace ("%02x " , au8Data [j ]);
83
68
mbedtls_trace ("\r\n" );
84
69
}
85
70
71
+ static void swapInitVector (unsigned char iv [16 ])
72
+ {
73
+ unsigned int * piv ;
74
+ int i ;
75
+ // iv SWAP
76
+ piv = (unsigned int * )iv ;
77
+ for ( i = 0 ; i < 4 ; i ++ )
78
+ {
79
+ * piv = (((* piv ) & 0x000000FF ) << 24 ) |
80
+ (((* piv ) & 0x0000FF00 ) << 8 ) |
81
+ (((* piv ) & 0x00FF0000 ) >> 8 ) |
82
+ (((* piv ) & 0xFF000000 ) >> 24 );
83
+ piv ++ ;
84
+ }
85
+ }
86
+
86
87
//volatile void CRYPTO_IRQHandler()
87
88
//{
88
89
// if (AES_GET_INT_FLAG()) {
@@ -104,7 +105,6 @@ static int channel_alloc()
104
105
return i ;
105
106
}
106
107
}
107
- printf ("%s: No available AES channel \r\n" , __FUNCTION__ );
108
108
return (-1 );
109
109
}
110
110
@@ -126,8 +126,11 @@ void mbedtls_aes_init( mbedtls_aes_context *ctx )
126
126
memset ( ctx , 0 , sizeof ( mbedtls_aes_context ) );
127
127
128
128
ctx -> swapType = AES_IN_OUT_SWAP ;
129
- while ( (i = channel_alloc ()) < 0 ) osDelay (300 );
130
-
129
+ while ( (i = channel_alloc ()) < 0 )
130
+ {
131
+ mbed_assert_internal ("No available AES channel" , __FILE__ , __LINE__ );
132
+ //osDelay(300);
133
+ }
131
134
ctx -> channel = i ;
132
135
ctx -> iv = au32MyAESIV ;
133
136
@@ -215,7 +218,7 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
215
218
int ret ;
216
219
217
220
mbedtls_trace ("=== %s keybits[%d]\r\n" , __FUNCTION__ , keybits );
218
- dumpHex (key ,keybits /8 );
221
+ dumpHex (( uint8_t * ) key ,keybits /8 );
219
222
220
223
/* Also checks keybits */
221
224
if ( ( ret = mbedtls_aes_setkey_enc ( ctx , key , keybits ) ) != 0 )
@@ -230,9 +233,8 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
230
233
231
234
static void __nvt_aes_crypt ( mbedtls_aes_context * ctx ,
232
235
const unsigned char input [16 ],
233
- unsigned char output [16 ])
236
+ unsigned char output [16 ], int dataSize )
234
237
{
235
- int i ;
236
238
unsigned char * pIn ;
237
239
unsigned char * pOut ;
238
240
@@ -243,28 +245,27 @@ static void __nvt_aes_crypt( mbedtls_aes_context *ctx,
243
245
AES_SetInitVect (ctx -> channel , ctx -> iv );
244
246
if ( ((uint32_t )input ) & 0x03 )
245
247
{
246
- memcpy (au8InputData , input , sizeof ( au8InputData ) );
248
+ memcpy (au8InputData , input , dataSize );
247
249
pIn = au8InputData ;
248
250
}else {
249
- pIn = ( uint32_t ) input ;
251
+ pIn = input ;
250
252
}
251
253
if ( ((uint32_t )output ) & 0x03 )
252
254
{
253
255
pOut = au8OutputData ;
254
256
} else {
255
- pOut = ( uint32_t ) output ;
257
+ pOut = output ;
256
258
}
257
259
258
- AES_SetDMATransfer (ctx -> channel , (uint32_t )pIn , (uint32_t )pOut , sizeof ( au8InputData ) );
260
+ AES_SetDMATransfer (ctx -> channel , (uint32_t )pIn , (uint32_t )pOut , dataSize );
259
261
260
262
g_AES_done = 0 ;
261
263
AES_Start (ctx -> channel , CRYPTO_DMA_ONE_SHOT );
262
264
while (!g_AES_done );
263
265
264
- if ( pOut != ( uint32_t ) output ) memcpy (output , au8OutputData , sizeof ( au8InputData ) );
266
+ if ( pOut != output ) memcpy (output , au8OutputData , dataSize );
265
267
dumpHex (output ,16 );
266
268
267
-
268
269
}
269
270
270
271
/*
@@ -275,12 +276,11 @@ void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
275
276
const unsigned char input [16 ],
276
277
unsigned char output [16 ] )
277
278
{
278
- int i ;
279
279
280
280
mbedtls_trace ("=== %s \r\n" , __FUNCTION__ );
281
281
282
282
ctx -> encDec = 1 ;
283
- __nvt_aes_crypt (ctx , input , output );
283
+ __nvt_aes_crypt (ctx , input , output , 16 );
284
284
285
285
}
286
286
#endif /* MBEDTLS_AES_ENCRYPT_ALT */
@@ -293,12 +293,11 @@ void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
293
293
const unsigned char input [16 ],
294
294
unsigned char output [16 ] )
295
295
{
296
- int i ;
297
296
298
297
mbedtls_trace ("=== %s \r\n" , __FUNCTION__ );
299
298
300
299
ctx -> encDec = 0 ;
301
- __nvt_aes_crypt (ctx , input , output );
300
+ __nvt_aes_crypt (ctx , input , output , 16 );
302
301
303
302
304
303
}
@@ -312,8 +311,6 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
312
311
const unsigned char input [16 ],
313
312
unsigned char output [16 ] )
314
313
{
315
- unsigned char sw_output [16 ];
316
-
317
314
318
315
mbedtls_trace ("=== %s \r\n" , __FUNCTION__ );
319
316
@@ -333,52 +330,50 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
333
330
*/
334
331
int mbedtls_aes_crypt_cbc ( mbedtls_aes_context * ctx ,
335
332
int mode ,
336
- size_t length ,
333
+ size_t len ,
337
334
unsigned char iv [16 ],
338
335
const unsigned char * input ,
339
336
unsigned char * output )
340
337
{
341
- int i ;
342
338
unsigned char temp [16 ];
343
- unsigned int * piv ;
344
-
339
+ int length = len ;
340
+ int blockChainLen ;
345
341
mbedtls_trace ("=== %s \r\n" , __FUNCTION__ );
346
342
if ( length % 16 )
347
343
return ( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
348
344
349
-
350
- while ( length > 0 )
351
- {
352
- ctx -> opMode = AES_MODE_CBC ;
353
- ctx -> iv = iv ;
354
- if ( mode == MBEDTLS_AES_ENCRYPT )
355
- {
356
- mbedtls_aes_encrypt ( ctx , input , output );
357
- memcpy ( iv , output , 16 );
358
- }else {
359
- memcpy ( temp , input , 16 );
360
- dumpHex (input ,16 );
361
- mbedtls_aes_decrypt ( ctx , input , output );
362
- dumpHex (iv ,16 );
363
- dumpHex (output ,16 );
364
- memcpy ( iv , temp , 16 );
365
- }
366
-
367
- // iv SWAP
368
- piv = (unsigned int * )iv ;
369
- for ( i = 0 ; i < 4 ; i ++ )
370
- {
371
- * piv = (((* piv ) & 0x000000FF ) << 24 ) |
372
- (((* piv ) & 0x0000FF00 ) << 8 ) |
373
- (((* piv ) & 0x00FF0000 ) >> 8 ) |
374
- (((* piv ) & 0xFF000000 ) >> 24 );
375
- piv ++ ;
376
- }
377
- input += 16 ;
378
- output += 16 ;
379
- length -= 16 ;
380
- }
381
-
345
+ if ( (((uint32_t )input ) & 0x03 ) || (((uint32_t )output ) & 0x03 ) )
346
+ {
347
+ blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? MAX_DMA_CHAIN_SIZE : length );
348
+ } else {
349
+ blockChainLen = length ;
350
+ }
351
+
352
+ while ( length > 0 )
353
+ {
354
+ ctx -> opMode = AES_MODE_CBC ;
355
+ swapInitVector (iv ); // iv SWAP
356
+ ctx -> iv = (uint32_t * )iv ;
357
+
358
+ if ( mode == MBEDTLS_AES_ENCRYPT )
359
+ {
360
+ ctx -> encDec = 1 ;
361
+ __nvt_aes_crypt (ctx , input , output , blockChainLen );
362
+ // if( blockChainLen == length ) break; // finish last block chain but still need to prepare next iv for mbedtls_aes_self_test()
363
+ memcpy ( iv , output + blockChainLen - 16 , 16 );
364
+ }else {
365
+ memcpy ( temp , input + blockChainLen - 16 , 16 );
366
+ ctx -> encDec = 0 ;
367
+ __nvt_aes_crypt (ctx , input , output , blockChainLen );
368
+ // if( blockChainLen == length ) break; // finish last block chain but still need to prepare next iv for mbedtls_aes_self_test()
369
+ memcpy ( iv , temp , 16 );
370
+ }
371
+ length -= blockChainLen ;
372
+ input += blockChainLen ;
373
+ output += blockChainLen ;
374
+ if (length < MAX_DMA_CHAIN_SIZE ) blockChainLen = length ; // For last remainder block chain
375
+
376
+ }
382
377
383
378
return ( 0 );
384
379
}
@@ -396,6 +391,52 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
396
391
const unsigned char * input ,
397
392
unsigned char * output )
398
393
{
394
+
395
+ size_t n = * iv_off ;
396
+ unsigned char temp [16 ];
397
+ int ivLen ;
398
+
399
+ mbedtls_trace ("=== %s \r\n" , __FUNCTION__ );
400
+
401
+ // Over reserved Max DMA buffer size
402
+ if ( length > MAX_DMA_CHAIN_SIZE )
403
+ return __aes_crypt_over_dma_size_cfb128 (ctx , mode , length , iv_off , iv , input , output );
404
+
405
+ ctx -> opMode = AES_MODE_CFB ;
406
+ swapInitVector (iv ); // iv SWAP
407
+
408
+ ctx -> iv = (uint32_t * )iv ;
409
+ if ( mode == MBEDTLS_AES_DECRYPT )
410
+ {
411
+ n = length %16 ;
412
+ ivLen = (( n > 0 ) ? n : 16 );
413
+ memcpy (temp , input + length - ivLen , ivLen );
414
+ ctx -> encDec = 0 ;
415
+ __nvt_aes_crypt (ctx , input , output , length );
416
+ memcpy (iv ,temp ,ivLen );
417
+ }
418
+ else
419
+ {
420
+ n = length %16 ;
421
+ ivLen = (( n > 0 ) ? n : 16 );
422
+ ctx -> encDec = 1 ;
423
+ __nvt_aes_crypt (ctx , input , output , length );
424
+ memcpy (iv , output + length - ivLen , ivLen );
425
+ }
426
+
427
+ * iv_off = n ;
428
+
429
+ return ( 0 );
430
+ }
431
+
432
+ static int __aes_crypt_over_dma_size_cfb128 ( mbedtls_aes_context * ctx ,
433
+ int mode ,
434
+ size_t length ,
435
+ size_t * iv_off ,
436
+ unsigned char iv [16 ],
437
+ const unsigned char * input ,
438
+ unsigned char * output )
439
+ {
399
440
int c ;
400
441
size_t n = * iv_off ;
401
442
0 commit comments