1 /*
2 * Copyright (c) 2021 Arm Limited
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6 #include "psa_adac_debug.h"
7
8 #include "psa/crypto.h"
9 #include "rnd_dma/local/cc_rnd_local.h"
10 #include "pal/cc_pal_mem.h"
11 #include "cc_rng_plat.h"
12 #include "rnd_dma/llf_rnd_trng.h"
13
hardware_rng_poll(uint8_t * output,size_t len,size_t * olen)14 int hardware_rng_poll(uint8_t *output, size_t len, size_t *olen)
15 {
16 CCRndWorkBuff_t rndWorkBuff;
17 CCRndState_t rndState;
18 CCRndParams_t trngParams;
19 int ret, error = -1;
20 uint32_t *entrSource_ptr;
21
22 if ((NULL == output) || (NULL == olen) || (0 == len)) {
23 return -1;
24 }
25
26 CC_PalMemSetZero(&rndWorkBuff, sizeof(CCRndWorkBuff_t));
27 CC_PalMemSetZero(&rndState, sizeof(CCRndState_t));
28 CC_PalMemSetZero(&trngParams, sizeof(CCRndParams_t));
29
30 ret = RNG_PLAT_SetUserRngParameters(&trngParams);
31 if (ret != 0) {
32 PSA_ADAC_LOG_ERR("cc312", "Error: RNG_PLAT_SetUserRngParameters() failed.\n");
33 error = -1;
34 goto Cleanup;
35 }
36
37 ret = LLF_RND_GetTrngSource(
38 &rndState, /*in/out*/
39 &trngParams, /*in/out*/
40 0, /*in - isContinued - false*/
41 (uint32_t *) &len, /*in/out*/
42 &entrSource_ptr, /*out*/
43 (uint32_t *) olen, /*out*/
44 (uint32_t *) &rndWorkBuff, /*in*/
45 0 /*in - isFipsSupport false*/ );
46 if (ret != 0) {
47 PSA_ADAC_LOG_ERR("cc312", "Error: LLF_RND_GetTrngSource() failed.\n");
48 error = -1;
49 goto Cleanup;
50 }
51
52 CC_PalMemCopy (output, entrSource_ptr + CC_RND_TRNG_SRC_INNER_OFFSET_WORDS,
53 ((*olen) <= len) ? (*olen) : len);
54
55 Cleanup:
56 CC_PalMemSetZero(&rndWorkBuff, sizeof(CCRndWorkBuff_t));
57 CC_PalMemSetZero(&rndState, sizeof(CCRndState_t));
58 CC_PalMemSetZero(&trngParams, sizeof(CCRndParams_t));
59
60 End:
61 return error;
62 }
63
psa_adac_generate_challenge(uint8_t * output,size_t output_size)64 psa_status_t psa_adac_generate_challenge(uint8_t *output, size_t output_size)
65 {
66 size_t size = 0;
67 psa_status_t status = PSA_SUCCESS;
68 size_t part;
69 while ((status == PSA_SUCCESS) && (size < output_size)) {
70 part = 0;
71 status = hardware_rng_poll(output + size, output_size - size, &part) == 0 ?
72 PSA_SUCCESS : PSA_ERROR_HARDWARE_FAILURE;
73 if (status == PSA_SUCCESS) {
74 size += part;
75 }
76 }
77 return status;
78 }
79