Skip to content

Commit 23ba8bc

Browse files
Ron EldorRon Eldor
authored andcommitted
Change Cryptocell target to a feature
Change the Cryptocell310 target to `FEATURE_CRYPTOCELL310`
1 parent 800f71c commit 23ba8bc

File tree

93 files changed

+338
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+338
-11
lines changed

features/mbedtls/targets/TARGET_CRYPTOCELL310/Readme.md renamed to features/cryptocell/FEATURE_CRYPTOCELL310/Readme.md

Lines changed: 3 additions & 4 deletions

features/mbedtls/targets/TARGET_CRYPTOCELL310/cc_internal.h renamed to features/cryptocell/FEATURE_CRYPTOCELL310/cc_internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ typedef struct
106106
* different standard tests to pass.
107107
*
108108
*
109-
* \param grp_id The mbedtls mbedtls_ecp_group_id to convert
109+
* \param mbedtls_rand The mbedtls rnd context pointer
110+
* \param outSizeBytes The size of the output buffer
111+
* \param out_ptr Pointer to the output buffer
110112
*
111113
* \return \c The corresponding CRYS_ECPKI_DomainID_t.
112114
* CRYS_ECPKI_DomainID_OffMode if not recognized.
Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
/*
2+
* ecdsa_alt.c
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+
#include "mbedtls/ecdsa.h"
22+
#include <string.h>
23+
#include "crys_ecpki_ecdsa.h"
24+
#include "crys_ecpki_build.h"
25+
#include "crys_common.h"
26+
#include "crys_ecpki_kg.h"
27+
#include "crys_ecpki_domain.h"
28+
#include "crys_ec_edw_api.h"
29+
#include "mbedtls/platform.h"
30+
#include "cc_internal.h"
31+
32+
/* Implementation that should never be optimized out by the compiler */
33+
static void mbedtls_zeroize( void *v, size_t n ) {
34+
volatile unsigned char *p = (unsigned char*)v;
35+
while( n-- ) *p++ = 0;
36+
}
37+
38+
39+
static CRYS_ECPKI_HASH_OpMode_t message_size_to_hash_mode( size_t blen )
40+
{
41+
CRYS_ECPKI_HASH_OpMode_t hash_mode;
42+
switch( blen )
43+
{
44+
case CRYS_HASH_SHA1_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t):
45+
hash_mode = CRYS_ECPKI_AFTER_HASH_SHA1_mode;
46+
break;
47+
case CRYS_HASH_SHA224_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t):
48+
hash_mode = CRYS_ECPKI_AFTER_HASH_SHA224_mode;
49+
break;
50+
case CRYS_HASH_SHA256_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t):
51+
hash_mode = CRYS_ECPKI_AFTER_HASH_SHA256_mode;
52+
break;
53+
case CRYS_HASH_SHA384_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t):
54+
hash_mode = CRYS_ECPKI_AFTER_HASH_SHA384_mode;
55+
break;
56+
case CRYS_HASH_SHA512_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t):
57+
hash_mode = CRYS_ECPKI_AFTER_HASH_SHA512_mode;
58+
break;
59+
default:
60+
hash_mode = CRYS_ECPKI_HASH_OpModeLast;
61+
}
62+
63+
return hash_mode;
64+
}
65+
66+
#if defined(MBEDTLS_ECDSA_SIGN_ALT)
67+
int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
68+
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
69+
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
70+
{
71+
int ret = 0;
72+
CRYSError_t CrysRet = CRYS_OK;
73+
void* pHeap = NULL;
74+
size_t heapSize = 0;
75+
uint8_t* pSignature = NULL;
76+
CRYS_ECPKI_HASH_OpMode_t hash_mode = message_size_to_hash_mode( blen );
77+
uint32_t signature_size = ( ( grp->nbits + 7 ) / 8 ) *2;
78+
const uint32_t signature_size_for_heap = signature_size;
79+
mbedtls_rand_func_container cc_rand = { f_rng, p_rng };
80+
const CRYS_ECPKI_Domain_t* pDomain = CRYS_ECPKI_GetEcDomain ( convert_mbedtls_grp_id_to_crys_domain_id( grp->id ) );
81+
82+
if( blen > 0xFFFFFFFF )
83+
{
84+
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
85+
goto cleanup;
86+
}
87+
88+
if ( pDomain != NULL )
89+
{
90+
uint8_t temp_buf[ MAX_KEY_SIZE_IN_BYTES ] = {0};
91+
cc_ecc_ws_sign_params_t* signParams = mbedtls_calloc( 1, sizeof(cc_ecc_ws_sign_params_t) );
92+
if ( signParams == NULL)
93+
return ( MBEDTLS_ERR_ECP_ALLOC_FAILED );
94+
pHeap = signParams;
95+
heapSize = sizeof(cc_ecc_ws_sign_params_t);
96+
97+
pSignature = mbedtls_calloc( 1, signature_size_for_heap );
98+
if ( pSignature == NULL)
99+
{
100+
ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
101+
goto cleanup;
102+
}
103+
104+
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, temp_buf, mbedtls_mpi_size( d ) ) );
105+
106+
CrysRet = CRYS_ECPKI_BuildPrivKey( pDomain,
107+
temp_buf,
108+
mbedtls_mpi_size( d ),
109+
&signParams->privKey);
110+
if( CrysRet != CRYS_OK )
111+
{
112+
ret = convert_CrysError_to_mbedtls_err( CrysRet );
113+
mbedtls_zeroize( temp_buf, sizeof(temp_buf) );
114+
goto cleanup;
115+
}
116+
117+
CrysRet = CRYS_ECDSA_Sign( &cc_rand,
118+
convert_mbedtls_to_cc_rand,
119+
&signParams->signContext,
120+
&signParams->privKey,
121+
hash_mode,
122+
(uint8_t*)buf,
123+
blen,
124+
pSignature,
125+
&signature_size );
126+
mbedtls_zeroize( temp_buf, sizeof(temp_buf) );
127+
if( CrysRet != CRYS_OK )
128+
{
129+
ret = convert_CrysError_to_mbedtls_err( CrysRet );
130+
goto cleanup;
131+
}
132+
133+
}
134+
else
135+
{
136+
ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
137+
goto cleanup;
138+
}
139+
140+
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( r, pSignature, ( ( grp->nbits + 7 ) / 8 ) ) );
141+
142+
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( s, pSignature + ( ( grp->nbits + 7 ) / 8 ), ( ( grp->nbits + 7 ) / 8 ) ) );
143+
144+
145+
cleanup:
146+
147+
if ( pHeap )
148+
{
149+
mbedtls_zeroize( pHeap, heapSize );
150+
mbedtls_free( pHeap );
151+
}
152+
153+
if( pSignature )
154+
{
155+
mbedtls_zeroize( pSignature, signature_size_for_heap );
156+
mbedtls_free( pSignature );
157+
158+
}
159+
160+
return ( ret ) ;
161+
}
162+
#endif /* MBEDTLS_ECDSA_SIGN_ALT*/
163+
164+
#if defined(MBEDTLS_ECDSA_VERIFY_ALT)
165+
//need to normalize the coordinates
166+
int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
167+
const unsigned char *buf, size_t blen,
168+
const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
169+
{
170+
int ret = 0;
171+
CRYSError_t CrysRet = CRYS_OK;
172+
void* pHeap = NULL;
173+
size_t heapSize = 0;
174+
uint8_t * pSignature = NULL;
175+
CRYS_ECPKI_HASH_OpMode_t hash_mode = message_size_to_hash_mode( blen );
176+
size_t temp_size = 0;
177+
uint32_t signature_size = ( ( grp->nbits + 7 ) / 8 ) * 2;
178+
const CRYS_ECPKI_Domain_t* pDomain = CRYS_ECPKI_GetEcDomain ( convert_mbedtls_grp_id_to_crys_domain_id( grp->id ) );
179+
180+
if( blen > 0xFFFFFFFF )
181+
{
182+
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
183+
goto cleanup;
184+
}
185+
186+
if ( pDomain )
187+
{
188+
uint8_t temp_buf[ 2*MAX_KEY_SIZE_IN_BYTES + 1 ] = {0};
189+
190+
cc_ecc_ws_verify_params_t* verifyParams = mbedtls_calloc( 1, sizeof(cc_ecc_ws_verify_params_t) );
191+
if ( verifyParams == NULL)
192+
return ( MBEDTLS_ERR_ECP_ALLOC_FAILED );
193+
pHeap = verifyParams;
194+
heapSize = sizeof(cc_ecc_ws_verify_params_t);
195+
196+
pSignature = mbedtls_calloc( 1, signature_size );
197+
if ( pSignature == NULL)
198+
{
199+
ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
200+
goto cleanup;
201+
}
202+
203+
MBEDTLS_MPI_CHK( mbedtls_ecp_point_write_binary( grp, Q, MBEDTLS_ECP_PF_UNCOMPRESSED,
204+
&temp_size, temp_buf, sizeof(temp_buf) ) );
205+
206+
CrysRet = CRYS_ECPKI_BuildPublKey(pDomain, temp_buf, temp_size, &verifyParams->pubKey);
207+
if( CrysRet != CRYS_OK )
208+
{
209+
ret = convert_CrysError_to_mbedtls_err( CrysRet );
210+
goto cleanup;
211+
}
212+
213+
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( r, pSignature, ( ( grp->nbits + 7 ) / 8 ) ) );
214+
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( s, pSignature + ( ( grp->nbits + 7 ) / 8 ), ( ( grp->nbits + 7 ) / 8 ) ) );
215+
CrysRet = CRYS_ECDSA_Verify ( &verifyParams->verifyContext,
216+
&verifyParams->pubKey,
217+
hash_mode,
218+
pSignature,
219+
signature_size,
220+
(uint8_t*)buf,
221+
blen );
222+
if( CrysRet != CRYS_OK )
223+
{
224+
ret = convert_CrysError_to_mbedtls_err( CrysRet );
225+
goto cleanup;
226+
}
227+
}
228+
else
229+
ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
230+
231+
cleanup:
232+
233+
if( pHeap )
234+
{
235+
mbedtls_zeroize( pHeap, heapSize );
236+
mbedtls_free( pHeap );
237+
}
238+
if( pSignature )
239+
{
240+
mbedtls_zeroize( pSignature, signature_size );
241+
mbedtls_free( pSignature );
242+
243+
}
244+
245+
return ret;
246+
}
247+
#endif /* MBEDTLS_ECDSA_VERIFY_ALT */
248+
249+
#if defined(MBEDTLS_ECDSA_GENKEY_ALT)
250+
int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
251+
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
252+
{
253+
int ret = 0;
254+
CRYSError_t CrysRet = CRYS_OK;
255+
void* pHeap = NULL;
256+
size_t heapSize = 0;
257+
uint32_t key_size = 2*MAX_KEY_SIZE_IN_BYTES + 1;
258+
const CRYS_ECPKI_Domain_t* pDomain = CRYS_ECPKI_GetEcDomain ( convert_mbedtls_grp_id_to_crys_domain_id( gid ) );
259+
mbedtls_rand_func_container cc_rand = { f_rng, p_rng };
260+
261+
262+
if ( pDomain )
263+
{
264+
uint8_t temp_buf[ 2 * MAX_KEY_SIZE_IN_BYTES + 1 ] = {0};
265+
266+
cc_ecc_ws_keygen_params_t* kgParams = mbedtls_calloc( 1, sizeof(cc_ecc_ws_keygen_params_t) );
267+
if ( kgParams == NULL )
268+
return ( MBEDTLS_ERR_ECP_ALLOC_FAILED );
269+
270+
pHeap = kgParams;
271+
heapSize = sizeof(cc_ecc_ws_keygen_params_t);
272+
273+
CrysRet = CRYS_ECPKI_GenKeyPair( &cc_rand, convert_mbedtls_to_cc_rand, pDomain,
274+
&kgParams->privKey, &kgParams->pubKey,
275+
&kgParams->kgTempData, NULL );
276+
if ( CrysRet != CRYS_OK )
277+
{
278+
ret = convert_CrysError_to_mbedtls_err( CrysRet );
279+
goto cleanup;
280+
}
281+
282+
MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ctx->grp, gid ) );
283+
284+
CrysRet = CRYS_ECPKI_ExportPublKey( &kgParams->pubKey, CRYS_EC_PointUncompressed, temp_buf, &key_size );
285+
if ( CrysRet != CRYS_OK )
286+
{
287+
ret = convert_CrysError_to_mbedtls_err( CrysRet );
288+
goto cleanup;
289+
}
290+
291+
ret = mbedtls_ecp_point_read_binary( &ctx->grp, &ctx->Q, temp_buf, key_size );
292+
if ( ret != 0 )
293+
goto cleanup;
294+
295+
memset ( temp_buf, 0 , sizeof(temp_buf) );
296+
297+
CrysRet = CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes( temp_buf, (ctx->grp.nbits+7)/8,
298+
kgParams->privKey.PrivKeyDbBuff,
299+
4*((((ctx->grp.nbits+7)/8)+3)/4) );
300+
if ( CrysRet != CRYS_OK )
301+
{
302+
ret = convert_CrysError_to_mbedtls_err( CrysRet );
303+
mbedtls_zeroize( temp_buf, sizeof(temp_buf) );
304+
goto cleanup;
305+
}
306+
307+
ret = mbedtls_mpi_read_binary( &ctx->d, temp_buf, (ctx->grp.nbits+7)/8 );
308+
mbedtls_zeroize( temp_buf, sizeof(temp_buf) );
309+
if ( ret != 0 )
310+
{
311+
goto cleanup;
312+
}
313+
314+
}
315+
else
316+
ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
317+
318+
319+
cleanup:
320+
if ( pHeap )
321+
{
322+
mbedtls_zeroize( pHeap, heapSize );
323+
mbedtls_free ( pHeap );
324+
}
325+
return ( ret );
326+
}
327+
#endif /* MBEDTLS_ECDSA_GENKEY_ALT */

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/objects.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extern "C" {
5454

5555
#include "nrf_uart.h"
5656

57-
#if defined(DEVICE_CRYPTOCELL)
57+
#if defined(FEATURE_CRYPTOCELL310)
5858
#include "objects_cryptocell.h"
5959
#else
6060
struct trng_s {

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/trng_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838

3939
#if defined(DEVICE_TRNG)
40-
#if !defined(DEVICE_CRYPTOCELL)
40+
#if !defined(FEATURE_CRYPTOCELL310)
4141
#include "hal/trng_api.h"
4242
#include "hal/critical_section_api.h"
4343

@@ -119,5 +119,5 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
119119

120120
return result;
121121
}
122-
#endif/* !DEVICE_CRYPTOCELL */
122+
#endif/* !FEATURE_CRYPTOCELL310 */
123123
#endif

targets/targets.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3683,8 +3683,7 @@
36833683
"supported_form_factors": ["ARDUINO"],
36843684
"inherits": ["MCU_NRF52840"],
36853685
"macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT"],
3686-
"device_has_add": ["CRYPTOCELL"],
3687-
"extra_labels_add": ["CRYPTOCELL310"],
3686+
"features": ["CRYPTOCELL310"],
36883687
"release_versions": ["5"],
36893688
"device_name": "nRF52840_xxAA"
36903689
},

tools/config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ class Config(object):
369369

370370
# Allowed features in configurations
371371
__allowed_features = [
372-
"UVISOR", "BLE", "CLIENT", "IPV4", "LWIP", "COMMON_PAL", "STORAGE", "NANOSTACK",
372+
"UVISOR", "BLE", "CLIENT", "IPV4", "LWIP", "COMMON_PAL", "STORAGE", "NANOSTACK","CRYPTOCELL310",
373373
# Nanostack configurations
374374
"LOWPAN_BORDER_ROUTER", "LOWPAN_HOST", "LOWPAN_ROUTER", "NANOSTACK_FULL", "THREAD_BORDER_ROUTER", "THREAD_END_DEVICE", "THREAD_ROUTER", "ETHERNET_HOST"
375375
]

0 commit comments

Comments
 (0)