forked from ARMmbed/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trng.c
90 lines (74 loc) · 2.74 KB
/
trng.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* trng.c
*
* Copyright (C) 2017, Arm Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#if DEVICE_TRNG
#include <string.h>
#include "trng_api.h"
#include "mbedtls/platform.h"
extern mbedtls_platform_context plat_ctx;
static CRYS_RND_WorkBuff_t rndWorkBuff = { { 0 } };
/* Implementation that should never be optimized out by the compiler */
static void mbedtls_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v;
while( n-- ) *p++ = 0;
}
CRYSError_t RNG_PLAT_SetUserRngParameters(
CRYS_RND_State_t *pRndState,
CRYS_RND_Params_t *pTrngParams);
CRYSError_t LLF_RND_GetTrngSource(
CRYS_RND_State_t *rndState_ptr,
CRYS_RND_Params_t *trngParams_ptr,
SaSiBool_t isContinued,
uint32_t *entropySize_ptr,
uint32_t **sourceOut_ptr_ptr,
uint32_t *sourceOutSize_ptr,
uint32_t *rndWorkBuff_ptr);
void trng_init(trng_t *obj)
{
RNG_PLAT_SetUserRngParameters(&plat_ctx.platform_impl_ctx.rndState, obj);
}
void trng_free(trng_t *obj)
{
(void)obj;
}
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *outputLength)
{
(void)obj;
int ret;
uint32_t entropySizeBits;
uint32_t *entrSource_ptr;
uint32_t actualLength;
ret = LLF_RND_GetTrngSource(
&plat_ctx.platform_impl_ctx.rndState , /*in/out*/
obj, /*in/out*/
0, /*in*/
&entropySizeBits, /*in/out*/
&entrSource_ptr, /*out*/
&actualLength, /*out*/
(uint32_t*)&rndWorkBuff.crysRndWorkBuff /*in*/);
if ( ret != 0 )
return -1;
if ( length < actualLength )
actualLength = length;
*outputLength = actualLength;
memcpy( output, entrSource_ptr + CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS, *outputLength );
mbedtls_zeroize( entrSource_ptr + CRYS_RND_TRNG_SRC_INNER_OFFSET_WORDS, *outputLength );
return 0;
}
#endif //DEVICE_TRNG