Skip to content

Commit 641c518

Browse files
Ron EldorRon Eldor
authored andcommitted
Port the cryptocell 310 cmac driver
Add support for CC310 CMAC driver returning `MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED` for key size other than 128 bits, and for crypto algorithms other than AES( e.g. DES).
1 parent 15b5b5d commit 641c518

File tree

3 files changed

+384
-0
lines changed

3 files changed

+384
-0
lines changed
Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
/*
2+
* cmac_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/cmac.h"
22+
#if defined(MBEDTLS_CMAC_ALT)
23+
#include "mbedtls/platform.h"
24+
#include "mbedtls/platform_util.h"
25+
#if defined(MBEDTLS_AES_C)
26+
#include "mbedtls/aes.h"
27+
#endif
28+
#include "ssi_aes_defs.h"
29+
#include <string.h>
30+
31+
int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
32+
const unsigned char *key, size_t keybits )
33+
{
34+
mbedtls_cmac_context_t *cmac_ctx;
35+
mbedtls_cipher_type_t type;
36+
37+
if( ctx == NULL || ctx->cipher_info == NULL || key == NULL )
38+
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
39+
40+
type = ctx->cipher_info->type;
41+
42+
switch( type )
43+
{
44+
case MBEDTLS_CIPHER_AES_128_ECB:
45+
break;
46+
case MBEDTLS_CIPHER_AES_192_ECB:
47+
case MBEDTLS_CIPHER_AES_256_ECB:
48+
case MBEDTLS_CIPHER_DES_EDE3_ECB:
49+
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
50+
default:
51+
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
52+
}
53+
54+
/* Allocated and initialise in the cipher context memory for the CMAC
55+
* context
56+
*/
57+
cmac_ctx = mbedtls_calloc( 1, sizeof( mbedtls_cmac_context_t ) );
58+
if( cmac_ctx == NULL )
59+
return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
60+
61+
62+
switch( keybits )
63+
{
64+
case 128:
65+
{
66+
cmac_ctx->CC_keySizeInBytes = ( keybits / 8 );
67+
memcpy( cmac_ctx->CC_Key, key, cmac_ctx->CC_keySizeInBytes );
68+
}
69+
break;
70+
case 192:
71+
case 256:
72+
{
73+
mbedtls_free( cmac_ctx );
74+
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
75+
}
76+
default:
77+
{
78+
mbedtls_free( cmac_ctx );
79+
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
80+
}
81+
}
82+
83+
ctx->cmac_ctx = cmac_ctx;
84+
return 0;
85+
}
86+
87+
static int init_cc( mbedtls_cmac_context_t *cmac_ctx )
88+
{
89+
int ret = 0;
90+
SaSiAesUserKeyData_t CC_KeyData;
91+
if( SaSi_AesInit( &cmac_ctx->CC_Context, SASI_AES_ENCRYPT,
92+
SASI_AES_MODE_CMAC, SASI_AES_PADDING_NONE) != 0 )
93+
{
94+
return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
95+
}
96+
97+
CC_KeyData.pKey = cmac_ctx->CC_Key;
98+
CC_KeyData.keySize = cmac_ctx->CC_keySizeInBytes;
99+
100+
if( SaSi_AesSetKey( &cmac_ctx->CC_Context, SASI_AES_USER_KEY,
101+
&CC_KeyData, sizeof( CC_KeyData ) ) != 0 )
102+
{
103+
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
104+
goto exit;
105+
}
106+
107+
cmac_ctx->is_cc_initiated = 1;
108+
109+
exit:
110+
return( ret );
111+
112+
}
113+
114+
int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
115+
const unsigned char *input, size_t ilen )
116+
{
117+
mbedtls_cmac_context_t* cmac_ctx;
118+
int ret = 0;
119+
size_t block_size;
120+
121+
if( ctx == NULL || ctx->cipher_info == NULL || input == NULL ||
122+
ctx->cmac_ctx == NULL )
123+
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
124+
125+
if( ctx == NULL || ctx->cipher_info == NULL )
126+
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
127+
128+
129+
block_size = ctx->cipher_info->block_size;
130+
if( block_size != SASI_AES_BLOCK_SIZE_IN_BYTES )
131+
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
132+
133+
cmac_ctx = ctx->cmac_ctx;
134+
135+
if( cmac_ctx->is_cc_initiated == 0 )
136+
{
137+
ret = init_cc( cmac_ctx );
138+
if( ret != 0 )
139+
goto exit;
140+
}
141+
142+
/* Is there data still to process from the last call?
143+
*/
144+
if( cmac_ctx->unprocessed_len > 0 )
145+
{
146+
const size_t size_to_copy = ilen > ( block_size - cmac_ctx->unprocessed_len ) ?
147+
block_size - cmac_ctx->unprocessed_len : ilen;
148+
memcpy( &cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
149+
input, size_to_copy );
150+
cmac_ctx->unprocessed_len += size_to_copy;
151+
input += size_to_copy;
152+
ilen -= size_to_copy;
153+
154+
/*
155+
* Process the unproccessed data, in case it reached a full AES block,
156+
* and there is there is still input data.
157+
*/
158+
if( cmac_ctx->unprocessed_len == SASI_AES_BLOCK_SIZE_IN_BYTES && ilen > 0 )
159+
{
160+
if( SaSi_AesBlock( &cmac_ctx->CC_Context, cmac_ctx->unprocessed_block,
161+
SASI_AES_BLOCK_SIZE_IN_BYTES, NULL ) != 0 )
162+
{
163+
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
164+
goto exit;
165+
}
166+
cmac_ctx->unprocessed_len = 0;
167+
}
168+
}
169+
170+
if( ilen > 0 )
171+
{
172+
const size_t size_to_store = ( ilen % SASI_AES_BLOCK_SIZE_IN_BYTES == 0) ?
173+
SASI_AES_BLOCK_SIZE_IN_BYTES : ilen % SASI_AES_BLOCK_SIZE_IN_BYTES;
174+
memcpy( &cmac_ctx->unprocessed_block[0],
175+
input + ilen - size_to_store,
176+
size_to_store );
177+
cmac_ctx->unprocessed_len = size_to_store;
178+
ilen -= size_to_store;
179+
if( ilen > 0 )
180+
{
181+
if( SaSi_AesBlock( &cmac_ctx->CC_Context, ( uint8_t * )input,
182+
ilen, NULL ) != 0 )
183+
{
184+
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
185+
goto exit;
186+
}
187+
}
188+
}
189+
190+
exit:
191+
if( ret != 0 )
192+
{
193+
mbedtls_platform_zeroize( cmac_ctx, sizeof( *cmac_ctx ) );
194+
mbedtls_free( cmac_ctx );
195+
SaSi_AesFree( &cmac_ctx->CC_Context );
196+
}
197+
return( ret );
198+
}
199+
200+
int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
201+
unsigned char *output )
202+
{
203+
mbedtls_cmac_context_t* cmac_ctx;
204+
int ret = 0;
205+
size_t olen = SASI_AES_BLOCK_SIZE_IN_BYTES;
206+
207+
if( ctx == NULL || ctx->cipher_info == NULL ||
208+
ctx->cmac_ctx == NULL || output == NULL )
209+
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
210+
211+
cmac_ctx = ctx->cmac_ctx;
212+
213+
if( cmac_ctx->is_cc_initiated == 0 )
214+
{
215+
ret = init_cc( cmac_ctx );
216+
if( ret != 0 )
217+
goto exit;
218+
}
219+
220+
if( ( ret = SaSi_AesFinish( &cmac_ctx->CC_Context, cmac_ctx->unprocessed_len,
221+
cmac_ctx->unprocessed_block,
222+
cmac_ctx->unprocessed_len, output, &olen ) ) != 0 )
223+
{
224+
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
225+
goto exit;
226+
}
227+
228+
exit:
229+
if( SaSi_AesFree( &cmac_ctx->CC_Context ) != 0 && ret == 0 )
230+
{
231+
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
232+
}
233+
234+
return( ret );
235+
}
236+
237+
int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx )
238+
{
239+
mbedtls_cmac_context_t* cmac_ctx;
240+
241+
if( ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL )
242+
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
243+
244+
cmac_ctx = ctx->cmac_ctx;
245+
246+
/* Reset the internal state */
247+
cmac_ctx->unprocessed_len = 0;
248+
mbedtls_platform_zeroize( cmac_ctx->unprocessed_block,
249+
sizeof( cmac_ctx->unprocessed_block ) );
250+
251+
return( 0 );
252+
}
253+
254+
/**
255+
* \brief This function calculates the full generic CMAC
256+
* on the input buffer with the provided key.
257+
*
258+
* The function allocates the context, performs the
259+
* calculation, and frees the context.
260+
*
261+
* The CMAC result is calculated as
262+
* output = generic CMAC(cmac key, input buffer).
263+
*
264+
*
265+
* \param cipher_info The cipher information.
266+
* \param key The CMAC key.
267+
* \param keylen The length of the CMAC key in bits.
268+
* \param input The buffer holding the input data.
269+
* \param ilen The length of the input data.
270+
* \param output The buffer for the generic CMAC result.
271+
*
272+
* \return \c 0 on success.
273+
* \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
274+
* if parameter verification fails.
275+
*/
276+
int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
277+
const unsigned char *key, size_t keylen,
278+
const unsigned char *input, size_t ilen,
279+
unsigned char *output )
280+
{
281+
int ret = 0;
282+
mbedtls_cipher_context_t ctx;
283+
size_t olen = SASI_AES_BLOCK_SIZE_IN_BYTES;
284+
285+
if( cipher_info == NULL || key == NULL ||
286+
input == NULL || output == NULL )
287+
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
288+
289+
mbedtls_cipher_init( &ctx );
290+
291+
if( ( ret = mbedtls_cipher_setup( &ctx, cipher_info ) ) != 0 )
292+
goto exit;
293+
294+
ret = mbedtls_cipher_cmac_starts( &ctx, key, keylen );
295+
if( ret != 0 )
296+
goto exit;
297+
298+
if( ( ret = init_cc( ctx.cmac_ctx ) ) != 0 )
299+
{
300+
goto exit;
301+
}
302+
303+
if( SaSi_AesFinish( &ctx.cmac_ctx->CC_Context, ilen, ( uint8_t * ) input,
304+
ilen, output, &olen ) != 0 )
305+
{
306+
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
307+
goto exit;
308+
}
309+
310+
311+
exit:
312+
if( SaSi_AesFree( &ctx.cmac_ctx->CC_Context ) != 0 && ret == 0 )
313+
{
314+
ret = MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
315+
}
316+
mbedtls_cipher_free( &ctx );
317+
318+
return( ret );
319+
320+
}
321+
322+
#if defined(MBEDTLS_AES_C)
323+
324+
int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
325+
const unsigned char *input, size_t in_len,
326+
unsigned char output[16] )
327+
{
328+
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
329+
}
330+
#endif /* MBEDTLS_AES_C */
331+
332+
333+
#endif /* MBEDTLS_CMAC_ALT */
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* aes_alt.h
3+
*
4+
* Copyright (C) 2018, 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 __CMAC_ALT__
22+
#define __CMAC_ALT__
23+
24+
#if defined(MBEDTLS_CMAC_ALT)
25+
#include "ssi_aes.h"
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
struct mbedtls_cmac_context_t
31+
{
32+
SaSiAesUserContext_t CC_Context;
33+
uint8_t CC_Key[SASI_AES_KEY_MAX_SIZE_IN_BYTES];
34+
size_t CC_keySizeInBytes;
35+
/** Unprocessed data - either data that was not block aligned and is still
36+
* pending processing, or the final block */
37+
unsigned char unprocessed_block[SASI_AES_BLOCK_SIZE_IN_BYTES];
38+
39+
/** The length of data pending processing. */
40+
size_t unprocessed_len;
41+
42+
int is_cc_initiated;
43+
};
44+
45+
#ifdef __cplusplus
46+
}
47+
#endif
48+
49+
#endif /* MBEDTLS_CMAC_ALT */
50+
#endif /* __CMAC_ALT__ */

features/cryptocell/FEATURE_CRYPTOCELL310/mbedtls_device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define MBEDTLS_SHA1_ALT
2626
#define MBEDTLS_SHA256_ALT
2727
#define MBEDTLS_CCM_ALT
28+
#define MBEDTLS_CMAC_ALT
2829
#define MBEDTLS_ECDSA_VERIFY_ALT
2930
#define MBEDTLS_ECDSA_SIGN_ALT
3031
#define MBEDTLS_ECDSA_GENKEY_ALT

0 commit comments

Comments
 (0)