Raef Coles | a657a9c | 2019-10-24 14:36:43 +0100 | [diff] [blame^] | 1 | /* |
| 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" |
| 16 | #include "arm_cmse.h" |
| 17 | |
| 18 | CCRndContext_t* CC312_pRndCtx = NULL; |
| 19 | CCRndWorkBuff_t* CC312_pRndWorkBuff = NULL; |
| 20 | mbedtls_ctr_drbg_context* CC312_pRndState = NULL; |
| 21 | mbedtls_entropy_context* CC312_pMbedtlsEntropy = NULL; |
| 22 | |
| 23 | CCError_t CC_PalDataBufferAttrGet(const unsigned char *pDataBuffer, |
| 24 | size_t buffSize, uint8_t buffType, |
| 25 | uint8_t *pBuffNs) |
| 26 | { |
| 27 | CC_UNUSED_PARAM(buffType); |
| 28 | |
| 29 | *pBuffNs = DATA_BUFFER_IS_SECURE; |
| 30 | if (cmse_check_address_range((void*)pDataBuffer, buffSize, CMSE_NONSECURE)) { |
| 31 | *pBuffNs = DATA_BUFFER_IS_NONSECURE; |
| 32 | } |
| 33 | |
| 34 | return CC_OK; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * \brief Initialize the CC312 crypto accelerator |
| 39 | */ |
| 40 | |
| 41 | int crypto_hw_accelerator_init(void) |
| 42 | { |
| 43 | int ret = 0; |
| 44 | |
| 45 | /* Allocate memory on heap */ |
| 46 | CC312_pRndCtx = mbedtls_calloc(1, sizeof(CCRndContext_t)); |
| 47 | CC312_pRndWorkBuff = mbedtls_calloc(1, sizeof(CCRndWorkBuff_t)); |
| 48 | CC312_pRndState = mbedtls_calloc(1, sizeof(mbedtls_ctr_drbg_context)); |
| 49 | CC312_pMbedtlsEntropy = mbedtls_calloc(1, sizeof(mbedtls_entropy_context)); |
| 50 | |
| 51 | /* Check if memory allocation was successful */ |
| 52 | if ( !CC312_pRndCtx || !CC312_pRndWorkBuff |
| 53 | || !CC312_pRndState || !CC312_pMbedtlsEntropy) { |
| 54 | mbedtls_free(CC312_pRndCtx); |
| 55 | mbedtls_free(CC312_pRndWorkBuff); |
| 56 | mbedtls_free(CC312_pRndState); |
| 57 | mbedtls_free(CC312_pMbedtlsEntropy); |
| 58 | |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | /* Init Rnd context's inner members */ |
| 63 | CC312_pRndCtx->rndState = CC312_pRndState; |
| 64 | CC312_pRndCtx->entropyCtx = CC312_pMbedtlsEntropy; |
| 65 | |
| 66 | /* Initialise CryptoCell library */ |
| 67 | ret = CC_LibInit(CC312_pRndCtx, CC312_pRndWorkBuff); |
| 68 | if (ret != CC_LIB_RET_OK) { |
| 69 | mbedtls_free(CC312_pRndCtx); |
| 70 | mbedtls_free(CC312_pRndWorkBuff); |
| 71 | mbedtls_free(CC312_pRndState); |
| 72 | mbedtls_free(CC312_pMbedtlsEntropy); |
| 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * \brief Deallocate the CC312 crypto accelerator |
| 82 | */ |
| 83 | int crypto_hw_accelerator_finish(void) |
| 84 | { |
| 85 | int ret = 0; |
| 86 | |
| 87 | ret = CC_LibFini(CC312_pRndCtx); |
| 88 | if(ret != CC_LIB_RET_OK) { |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | mbedtls_free(CC312_pRndCtx); |
| 93 | mbedtls_free(CC312_pRndWorkBuff); |
| 94 | mbedtls_free(CC312_pRndState); |
| 95 | mbedtls_free(CC312_pMbedtlsEntropy); |
| 96 | |
| 97 | return 0; |
| 98 | } |