Skip to content

Commit b9a6251

Browse files
authored
Merge pull request #10907 from RonEld/cc310_aes_port
Port aes cc310 driver
2 parents 2c3ce96 + d09e3ef commit b9a6251

File tree

3 files changed

+366
-0
lines changed

3 files changed

+366
-0
lines changed
Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
/*
2+
* aes_alt.c
3+
*
4+
* Copyright (C) 2019, Arm Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
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
10+
*
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.
18+
*
19+
*/
20+
21+
#include "mbedtls/aes.h"
22+
#if defined(MBEDTLS_AES_ALT)
23+
#include <string.h>
24+
#include "ssi_aes_defs.h"
25+
#include "mbedtls/platform.h"
26+
27+
#if defined(MBEDTLS_CIPHER_MODE_CFB)
28+
/*
29+
* AES-CFB128 buffer encryption/decryption
30+
*/
31+
int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
32+
int mode,
33+
size_t length,
34+
size_t *iv_off,
35+
unsigned char iv[16],
36+
const unsigned char *input,
37+
unsigned char *output )
38+
{
39+
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
40+
}
41+
42+
/*
43+
* AES-CFB8 buffer encryption/decryption
44+
*/
45+
int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
46+
int mode,
47+
size_t length,
48+
unsigned char iv[16],
49+
const unsigned char *input,
50+
unsigned char *output )
51+
{
52+
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
53+
}
54+
#endif /*MBEDTLS_CIPHER_MODE_CFB */
55+
56+
#if defined(MBEDTLS_CIPHER_MODE_XTS)
57+
58+
int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
59+
const unsigned char *key,
60+
unsigned int keybits )
61+
{
62+
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
63+
}
64+
65+
int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
66+
const unsigned char *key,
67+
unsigned int keybits )
68+
{
69+
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
70+
}
71+
72+
int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
73+
int mode,
74+
size_t length,
75+
const unsigned char data_unit[16],
76+
const unsigned char *input,
77+
unsigned char *output )
78+
{
79+
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
80+
}
81+
#endif /* MBEDTLS_CIPHER_MODE_XTS */
82+
83+
#if defined(MBEDTLS_CIPHER_MODE_OFB)
84+
int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
85+
size_t length,
86+
size_t *iv_off,
87+
unsigned char iv[16],
88+
const unsigned char *input,
89+
unsigned char *output );
90+
{
91+
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
92+
}
93+
#endif /* MBEDTLS_CIPHER_MODE_OFB */
94+
95+
void mbedtls_aes_init( mbedtls_aes_context *ctx )
96+
{
97+
memset( ctx, 0, sizeof( mbedtls_aes_context ) );
98+
}
99+
100+
void mbedtls_aes_free( mbedtls_aes_context *ctx )
101+
{
102+
if( ctx == NULL )
103+
return;
104+
105+
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_aes_context ) );
106+
}
107+
#if defined(MBEDTLS_CIPHER_MODE_XTS)
108+
109+
void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx ){}
110+
111+
void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx ){}
112+
#endif /* MBEDTLS_CIPHER_MODE_XTS */
113+
114+
static int CC_aes_setkey( mbedtls_aes_context *ctx, const unsigned char *key,
115+
unsigned int keybits, SaSiAesEncryptMode_t cipher_flag )
116+
{
117+
int ret = 0;
118+
if( ctx == NULL )
119+
return( MBEDTLS_ERR_AES_BAD_INPUT_DATA );
120+
121+
switch( keybits )
122+
{
123+
case 128:
124+
{
125+
ctx->CC_cipherFlag = cipher_flag;
126+
ctx->CC_keySizeInBytes = ( keybits / 8 );
127+
memcpy( ctx->CC_Key, key, ctx->CC_keySizeInBytes );
128+
}
129+
break;
130+
case 192:
131+
case 256:
132+
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
133+
default:
134+
return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
135+
}
136+
137+
return( 0 );
138+
}
139+
int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
140+
unsigned int keybits )
141+
{
142+
return( CC_aes_setkey( ctx, key, keybits, SASI_AES_ENCRYPT ) );
143+
}
144+
145+
int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
146+
unsigned int keybits )
147+
{
148+
return( CC_aes_setkey( ctx, key, keybits, SASI_AES_DECRYPT ) );
149+
}
150+
151+
static int CC_aes_cipher( mbedtls_aes_context *ctx,
152+
int mode,
153+
SaSiAesOperationMode_t aes_mode,
154+
size_t length,
155+
unsigned char* iv,
156+
size_t iv_len,
157+
const unsigned char *input,
158+
unsigned char *output )
159+
{
160+
int ret = 0;
161+
SaSiAesUserKeyData_t CC_KeyData = { ctx->CC_Key,
162+
ctx->CC_keySizeInBytes };
163+
164+
ret = SaSi_AesInit( &ctx->CC_Context,
165+
ctx->CC_cipherFlag,
166+
aes_mode, SASI_AES_PADDING_NONE );
167+
if( ret != 0 )
168+
return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
169+
170+
ret = SaSi_AesSetKey( &ctx->CC_Context, SASI_AES_USER_KEY,
171+
&CC_KeyData, sizeof( CC_KeyData ) );
172+
if( ret != 0 )
173+
return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
174+
175+
if( iv )
176+
{
177+
if( iv_len != SASI_AES_IV_SIZE_IN_BYTES )
178+
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
179+
180+
ret = SaSi_AesSetIv( &ctx->CC_Context, iv );
181+
if( ret != 0 )
182+
return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
183+
}
184+
185+
ret = SaSi_AesFinish( &ctx->CC_Context, length,
186+
( unsigned char* )input, length, output, &length );
187+
if( ret != 0 )
188+
return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
189+
190+
/* update the IV for next block
191+
* For CTR mode, update the nonce only if the current length is a full AES block length
192+
*/
193+
194+
if( ( ( aes_mode == SASI_AES_MODE_CBC ) ||
195+
( (aes_mode == SASI_AES_MODE_CTR) && ( ( length % SASI_AES_BLOCK_SIZE_IN_BYTES) == 0) ) )
196+
&& iv )
197+
{
198+
ret = SaSi_AesGetIv( &ctx->CC_Context, iv );
199+
if( ret != 0 )
200+
return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
201+
}
202+
203+
ret = SaSi_AesFree( &ctx->CC_Context );
204+
if( ret != 0 )
205+
return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
206+
207+
return( 0 );
208+
}
209+
210+
int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
211+
int mode,
212+
const unsigned char input[16],
213+
unsigned char output[16] )
214+
{
215+
if( ctx == NULL )
216+
return( MBEDTLS_ERR_AES_BAD_INPUT_DATA );
217+
218+
if( ( mode == MBEDTLS_AES_ENCRYPT && ctx->CC_cipherFlag != SASI_AES_ENCRYPT ) ||
219+
( mode == MBEDTLS_AES_DECRYPT && ctx->CC_cipherFlag != SASI_AES_DECRYPT ) )
220+
return( MBEDTLS_ERR_AES_BAD_INPUT_DATA );
221+
222+
return( CC_aes_cipher( ctx, mode, SASI_AES_MODE_ECB, 16, NULL, 0, input, output ) );
223+
}
224+
225+
#if defined(MBEDTLS_CIPHER_MODE_CBC)
226+
int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
227+
int mode,
228+
size_t length,
229+
unsigned char iv[16],
230+
const unsigned char *input,
231+
unsigned char *output )
232+
{
233+
if( ctx == NULL )
234+
return( MBEDTLS_ERR_AES_BAD_INPUT_DATA );
235+
236+
if( length % SASI_AES_BLOCK_SIZE_IN_BYTES )
237+
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
238+
239+
if( ( mode != MBEDTLS_AES_ENCRYPT || ctx->CC_cipherFlag != SASI_AES_ENCRYPT ) &&
240+
( mode != MBEDTLS_AES_DECRYPT || ctx->CC_cipherFlag != SASI_AES_DECRYPT ) )
241+
return( MBEDTLS_ERR_AES_BAD_INPUT_DATA );
242+
243+
return( CC_aes_cipher( ctx, mode, SASI_AES_MODE_CBC, length, iv, 16, input, output ) );
244+
}
245+
246+
#endif /* MBEDTLS_CIPHER_MODE_CBC */
247+
248+
#if defined(MBEDTLS_CIPHER_MODE_CTR)
249+
int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
250+
size_t length,
251+
size_t *nc_off,
252+
unsigned char nonce_counter[16],
253+
unsigned char stream_block[16],
254+
const unsigned char *input,
255+
unsigned char *output )
256+
{
257+
int ret = 0;
258+
int n = *nc_off, c, i;
259+
size_t j;
260+
if( ctx == NULL )
261+
return( MBEDTLS_ERR_AES_BAD_INPUT_DATA );
262+
263+
if( *nc_off )
264+
{
265+
/* handle corner case where we are resuming a previous encryption,
266+
* and we are resuming within current cipher stream(stream_block) */
267+
while( n != 0 )
268+
{
269+
c = *input++;
270+
*output++ = (unsigned char)( c ^ stream_block[n] );
271+
n = ( n + 1) & 0x0F;
272+
if( length > 0)
273+
--length;
274+
}
275+
/*
276+
* Increase the nonce_counter by 1 since we now passed one block
277+
*/
278+
for( i = 16; i > 0; i-- )
279+
if( ++nonce_counter[i - 1] != 0 )
280+
break;
281+
}
282+
if( CC_aes_cipher( ctx, MBEDTLS_AES_ENCRYPT, SASI_AES_MODE_CTR,
283+
length, nonce_counter, SASI_AES_IV_SIZE_IN_BYTES, input, output ) != 0 )
284+
{
285+
ret = -1;
286+
}
287+
if( ( ( length % SASI_AES_BLOCK_SIZE_IN_BYTES ) != 0 ) && ret == 0 )
288+
{
289+
/* in case the length is not aligned, generate stream block for resuming
290+
* increase nonce_block to the correct value*/
291+
for( j = 0; j < ( length/SASI_AES_BLOCK_SIZE_IN_BYTES ); j++)
292+
for( i = 16; i > 0; i-- )
293+
if( ++nonce_counter[i - 1] != 0 )
294+
break;
295+
if( ( ret = CC_aes_cipher( ctx, MBEDTLS_AES_ENCRYPT, SASI_AES_MODE_ECB,
296+
SASI_AES_BLOCK_SIZE_IN_BYTES, NULL, 0,
297+
nonce_counter, stream_block ) ) != 0 )
298+
{
299+
goto exit;
300+
}
301+
}
302+
*nc_off = ( length % SASI_AES_BLOCK_SIZE_IN_BYTES );
303+
304+
exit:
305+
return( ret );
306+
}
307+
#endif /* MBEDTLS_CIPHER_MODE_CTR */
308+
#endif/* MBEDTLS_AES_ALT */
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* aes_alt.h
3+
*
4+
* Copyright (C) 2019, Arm Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
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
10+
*
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.
18+
*
19+
*/
20+
21+
#ifndef __AES_ALT__
22+
#define __AES_ALT__
23+
24+
#if defined(MBEDTLS_AES_ALT)
25+
#include "ssi_aes.h"
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
typedef struct
31+
{
32+
SaSiAesUserContext_t CC_Context;
33+
SaSiAesEncryptMode_t CC_cipherFlag;
34+
uint8_t CC_Key[SASI_AES_KEY_MAX_SIZE_IN_BYTES];
35+
size_t CC_keySizeInBytes;
36+
}
37+
mbedtls_aes_context;
38+
39+
#if defined(MBEDTLS_CIPHER_MODE_XTS)
40+
/**
41+
* \brief The AES XTS context-type definition.
42+
*/
43+
typedef struct mbedtls_aes_xts_context
44+
{
45+
int unsupported;
46+
}
47+
mbedtls_aes_xts_context;
48+
#endif /* MBEDTLS_CIPHER_MODE_XTS */
49+
50+
#ifdef __cplusplus
51+
}
52+
#endif
53+
54+
#endif /* MBEDTLS_AES_ALT */
55+
#endif /* __AES_ALT__ */
56+

features/cryptocell/FEATURE_CRYPTOCELL310/mbedtls_device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define __MBEDTLS_DEVICE__
2323

2424
#define MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT
25+
//#define MBEDTLS_AES_ALT
26+
#define MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
2527
#define MBEDTLS_SHA1_ALT
2628
#define MBEDTLS_SHA256_ALT
2729
//#define MBEDTLS_SHA512_ALT

0 commit comments

Comments
 (0)