Skip to content

Commit 340f22e

Browse files
Ron EldorRon Eldor
authored andcommitted
Add support for CC trng
add support for HW entropy source
1 parent 7508746 commit 340f22e

File tree

4 files changed

+142
-4
lines changed

4 files changed

+142
-4
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* objects_cryptocell.h
3+
*
4+
* Copyright (C) 2017, 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 MBED_OBJECTS_CRYPTOCELL_H
22+
#define MBED_OBJECTS_CRYPTOCELL_H
23+
#include "sns_silib.h"
24+
25+
typedef struct trng_s
26+
{
27+
/* parameters defining TRNG */
28+
CRYS_RND_mode_t TrngMode;
29+
30+
/* allowed ring oscillator lengths: bits 0,1,2,3 */
31+
uint32_t RoscsAllowed;
32+
33+
/* sampling interval: count of ring oscillator cycles between
34+
consecutive bits sampling */
35+
uint32_t SubSamplingRatio;
36+
37+
uint32_t SubSamplingRatio1;
38+
uint32_t SubSamplingRatio2;
39+
uint32_t SubSamplingRatio3;
40+
uint32_t SubSamplingRatio4;
41+
42+
}CRYS_RND_Params_t;
43+
44+
#endif // MBED_OBJECTS_CRYPTOCELL_H

targets/TARGET_CRYPTOCELL310/trng.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* trng.c
3+
*
4+
* Copyright (C) 2017, 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+
#if defined(DEVICE_TRNG)
22+
23+
#include <string.h>
24+
#include "trng_api.h"
25+
26+
extern CRYS_RND_State_t rndState;
27+
extern CRYS_RND_WorkBuff_t rndWorkBuff;
28+
29+
/* Implementation that should never be optimized out by the compiler */
30+
static void mbedtls_zeroize( void *v, size_t n ) {
31+
volatile unsigned char *p = (unsigned char*)v;
32+
while( n-- ) *p++ = 0;
33+
}
34+
35+
CRYSError_t RNG_PLAT_SetUserRngParameters(
36+
CRYS_RND_State_t *pRndState,
37+
CRYS_RND_Params_t *pTrngParams);
38+
39+
CRYSError_t LLF_RND_GetTrngSource(
40+
CRYS_RND_State_t *rndState_ptr,
41+
CRYS_RND_Params_t *trngParams_ptr,
42+
SaSiBool_t isContinued,
43+
uint32_t *entropySize_ptr,
44+
uint32_t **sourceOut_ptr_ptr,
45+
uint32_t *sourceOutSize_ptr,
46+
uint32_t *rndWorkBuff_ptr);
47+
48+
49+
void trng_init(trng_t *obj)
50+
{
51+
RNG_PLAT_SetUserRngParameters(&rndState, obj);
52+
}
53+
54+
void trng_free(trng_t *obj)
55+
{
56+
(void)obj;
57+
}
58+
59+
60+
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *outputLength)
61+
{
62+
(void)obj;
63+
int ret;
64+
uint32_t entropySizeBits;
65+
uint32_t *entrSource_ptr;
66+
uint32_t actualLength;
67+
68+
ret = LLF_RND_GetTrngSource(
69+
&rndState , /*in/out*/
70+
obj, /*in/out*/
71+
0, /*in*/
72+
&entropySizeBits, /*in/out*/
73+
&entrSource_ptr, /*out*/
74+
&actualLength, /*out*/
75+
(uint32_t*)&rndWorkBuff.crysRndWorkBuff /*in*/);
76+
if ( ret != 0 )
77+
return -1;
78+
79+
if ( length < actualLength )
80+
actualLength = length;
81+
82+
*outputLength = actualLength;
83+
84+
memcpy( output, entrSource_ptr + CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS, *outputLength );
85+
mbedtls_zeroize( entrSource_ptr + CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS, *outputLength );
86+
return 0;
87+
}
88+
89+
#endif //DEVICE_TRNG

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF51/objects.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@
4747
#ifdef __cplusplus
4848
extern "C" {
4949
#endif
50+
#if DEVICE_CRYPTOCELL
51+
#include "objects_cryptocell.h"
52+
#else
53+
struct trng_s {
54+
uint32_t placeholder;
55+
};
56+
#endif
5057

5158
struct serial_s {
5259
uint32_t placeholder; // struct is unused by nRF5x API implementation
@@ -85,10 +92,6 @@ struct flash_s {
8592
uint32_t placeholder;
8693
};
8794

88-
struct trng_s {
89-
uint32_t placeholder;
90-
};
91-
9295
#include "gpio_object.h"
9396

9497
#ifdef __cplusplus

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF51/trng_api.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "hal/trng_api.h"
4242
#include "hal/lp_ticker_api.h"
4343

44+
#if !defined(DEVICE_CRYPTOCELL)
4445
#include "nrf_drv_rng.h"
4546

4647
#define DEFAULT_TIMEOUT_US (1000*1000)
@@ -179,4 +180,5 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
179180
return (*output_length == 0) ? -1 : 0;
180181
}
181182

183+
#endif/* !DEVICE_CRYPTOCELL */
182184
#endif

0 commit comments

Comments
 (0)