blob: a5c417dd6b9ed8593e1f71edc83cda9acf4fdb98 [file] [log] [blame]
Raef Colesa657a9c2019-10-24 14:36:43 +01001/*
2 * Copyright (c) 2019, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "crypto_hw.h"
9
10#include "cc_lib.h"
11#include "cc_pal_buff_attr.h"
12#include "cc_rnd_common.h"
13#include "mbedtls/platform.h"
14#include "mbedtls/ctr_drbg.h"
15#include "mbedtls/entropy.h"
Tamas Ban80f28242019-10-25 22:13:53 +010016#include "mbedtls_cc_mng_int.h"
Raef Colesa657a9c2019-10-24 14:36:43 +010017#include "arm_cmse.h"
18
19CCRndContext_t* CC312_pRndCtx = NULL;
20CCRndWorkBuff_t* CC312_pRndWorkBuff = NULL;
21mbedtls_ctr_drbg_context* CC312_pRndState = NULL;
22mbedtls_entropy_context* CC312_pMbedtlsEntropy = NULL;
23
24CCError_t CC_PalDataBufferAttrGet(const unsigned char *pDataBuffer,
25 size_t buffSize, uint8_t buffType,
26 uint8_t *pBuffNs)
27{
28 CC_UNUSED_PARAM(buffType);
29
30 *pBuffNs = DATA_BUFFER_IS_SECURE;
31 if (cmse_check_address_range((void*)pDataBuffer, buffSize, CMSE_NONSECURE)) {
32 *pBuffNs = DATA_BUFFER_IS_NONSECURE;
33 }
34
35 return CC_OK;
36}
37
38/*
39 * \brief Initialize the CC312 crypto accelerator
40 */
41
42int crypto_hw_accelerator_init(void)
43{
44 int ret = 0;
45
46 /* Allocate memory on heap */
47 CC312_pRndCtx = mbedtls_calloc(1, sizeof(CCRndContext_t));
48 CC312_pRndWorkBuff = mbedtls_calloc(1, sizeof(CCRndWorkBuff_t));
49 CC312_pRndState = mbedtls_calloc(1, sizeof(mbedtls_ctr_drbg_context));
50 CC312_pMbedtlsEntropy = mbedtls_calloc(1, sizeof(mbedtls_entropy_context));
51
52 /* Check if memory allocation was successful */
53 if ( !CC312_pRndCtx || !CC312_pRndWorkBuff
54 || !CC312_pRndState || !CC312_pMbedtlsEntropy) {
55 mbedtls_free(CC312_pRndCtx);
56 mbedtls_free(CC312_pRndWorkBuff);
57 mbedtls_free(CC312_pRndState);
58 mbedtls_free(CC312_pMbedtlsEntropy);
59
60 return -1;
61 }
62
63 /* Init Rnd context's inner members */
64 CC312_pRndCtx->rndState = CC312_pRndState;
65 CC312_pRndCtx->entropyCtx = CC312_pMbedtlsEntropy;
66
67 /* Initialise CryptoCell library */
68 ret = CC_LibInit(CC312_pRndCtx, CC312_pRndWorkBuff);
69 if (ret != CC_LIB_RET_OK) {
70 mbedtls_free(CC312_pRndCtx);
71 mbedtls_free(CC312_pRndWorkBuff);
72 mbedtls_free(CC312_pRndState);
73 mbedtls_free(CC312_pMbedtlsEntropy);
74
75 return ret;
76 }
77
78 return 0;
79}
80
81/*
82 * \brief Deallocate the CC312 crypto accelerator
83 */
84int crypto_hw_accelerator_finish(void)
85{
86 int ret = 0;
87
88 ret = CC_LibFini(CC312_pRndCtx);
89 if(ret != CC_LIB_RET_OK) {
90 return ret;
91 }
92
93 mbedtls_free(CC312_pRndCtx);
94 mbedtls_free(CC312_pRndWorkBuff);
95 mbedtls_free(CC312_pRndState);
96 mbedtls_free(CC312_pMbedtlsEntropy);
97
98 return 0;
99}
Tamas Ban80f28242019-10-25 22:13:53 +0100100
101int crypto_hw_accelerator_get_lcs(uint32_t *lcs)
102{
103 return mbedtls_mng_lcsGet(lcs);
104}