From 432c29d806f5669521a1a628f61295c53e63c62c Mon Sep 17 00:00:00 2001 From: Volodymyr Medvid Date: Fri, 25 Jan 2019 18:58:48 -0800 Subject: [PATCH] PSOC6: add CRC and TRNG APIs Exclude the labels from inherited FUTURE_SEQUANA targets. --- targets/TARGET_Cypress/TARGET_PSOC6/crc_api.c | 100 ++++++++++++++++++ .../TARGET_Cypress/TARGET_PSOC6/trng_api.c | 73 +++++++++++++ targets/targets.json | 6 +- 3 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 targets/TARGET_Cypress/TARGET_PSOC6/crc_api.c create mode 100644 targets/TARGET_Cypress/TARGET_PSOC6/trng_api.c diff --git a/targets/TARGET_Cypress/TARGET_PSOC6/crc_api.c b/targets/TARGET_Cypress/TARGET_PSOC6/crc_api.c new file mode 100644 index 0000000000..9b8fba9a26 --- /dev/null +++ b/targets/TARGET_Cypress/TARGET_PSOC6/crc_api.c @@ -0,0 +1,100 @@ +/* mbed Microcontroller Library + * Copyright (c) 2019 Cypress Semiconductor Corporation + * 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. + */ + +#include "device.h" + +#if DEVICE_CRC + +#include "mbed_assert.h" +#include "crc_api.h" + +#include "psoc6_utils.h" + +#include "cy_crypto_core_crc.h" + +static uint32_t crcWidth = 0; +static uint32_t crcShift = 0; +static uint32_t crcXorMask; + +/* Cypress CRYPTO HW supports ANY CRC algorithms from CRC-3 to CRC-32 */ +bool hal_crc_is_supported(const crc_mbed_config_t *config) +{ + return (config != NULL); +} + +void hal_crc_compute_partial_start(const crc_mbed_config_t *config) +{ + uint32_t myMask = 0; + + if (!hal_crc_is_supported(config) || (cy_reserve_crypto(CY_CRYPTO_CRC_HW) != 0)) + { + return; + } + + crcWidth = config->width; + + crcShift = (uint32_t)(!config->reflect_out) * (crcWidth & 7u); + if (crcShift) { + crcShift = 8u - crcShift; + for (uint32_t i = 0; i < crcShift; i++) { + myMask |= 1 << i; + } + crcXorMask = config->final_xor & myMask; + } + + Cy_Crypto_Core_Crc_CalcInit(CRYPTO, config->width, + config->polynomial, + config->reflect_in, + 0, + config->reflect_out, + config->final_xor >> crcShift, + config->initial_xor); +} + +void hal_crc_compute_partial(const uint8_t *data, const size_t size) +{ + if ((data == NULL) || (size <= 0) || (crcWidth == 0)) { + return; + } + + Cy_Crypto_Core_Crc_CalcPartial(CRYPTO, data, size); +} + +uint32_t hal_crc_get_result(void) +{ + uint32_t result = 0; + + if (crcWidth == 0) { + return 0xffffffffu; + } + + Cy_Crypto_Core_Crc_CalcFinish(CRYPTO, crcWidth, &result); + + if (crcShift) { + result = result << crcShift; + result = result ^ crcXorMask; + } + + crcWidth = 0; + + cy_free_crypto(CY_CRYPTO_CRC_HW); + + return result; +} + +#endif //DEVICE_CRC + diff --git a/targets/TARGET_Cypress/TARGET_PSOC6/trng_api.c b/targets/TARGET_Cypress/TARGET_PSOC6/trng_api.c new file mode 100644 index 0000000000..49bb1ae26a --- /dev/null +++ b/targets/TARGET_Cypress/TARGET_PSOC6/trng_api.c @@ -0,0 +1,73 @@ +/* mbed Microcontroller Library + * Copyright (c) 2019 Cypress Semiconductor Corporation + * 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. + */ + +/* Hardware entropy collector for the PSoC6A families */ + +#if DEVICE_TRNG + +#include "trng_api.h" +#include "psoc6_utils.h" +#include "cy_crypto_core_trng.h" + +/* Initialization polynomial values fro True Random Generator */ +#define GARO31_INITSTATE (0x04c11db7u) +#define FIRO31_INITSTATE (0x04c11db7u) + +#define MAX_TRNG_BIT_SIZE (32UL) + +void trng_init(trng_t *obj) +{ + (void)obj; + + cy_reserve_crypto(CY_CRYPTO_TRNG_HW); +} + +void trng_free(trng_t *obj) +{ + /* Deinitialization is not needed for the driver */ + (void)obj; + + cy_free_crypto(CY_CRYPTO_TRNG_HW); +} + +int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) +{ + int ret = 0; + *output_length = 0; + + /* temporary random data buffer */ + uint32_t random; + + (void)obj; + + /* Get Random byte */ + while ((*output_length < length) && (ret == 0)) { + if (Cy_Crypto_Core_Trng(CRYPTO, GARO31_INITSTATE, FIRO31_INITSTATE, MAX_TRNG_BIT_SIZE, &random) != CY_CRYPTO_SUCCESS) { + ret = -1; + } else { + for (uint8_t i = 0; (i < 4) && (*output_length < length) ; i++) { + *output++ = ((uint8_t *)&random)[i]; + *output_length += 1; + } + } + } + random = 0uL; + + return (ret); +} + +#endif diff --git a/targets/targets.json b/targets/targets.json index 458e4c4515..f6c39ef94d 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -7824,7 +7824,9 @@ "STDIO_MESSAGES", "LPTICKER", "SLEEP", - "FLASH" + "FLASH", + "TRNG", + "CRC" ], "release_versions": ["5"], "extra_labels": ["Cypress", "PSOC6"], @@ -7846,6 +7848,7 @@ "supported_form_factors": ["ARDUINO"], "extra_labels_add": ["PSOC6_FUTURE", "CY8C63XX", "FUTURE_SEQUANA"], "extra_labels_remove": ["PSOC6"], + "device_has_remove": ["TRNG", "CRC"], "macros_add": ["CY8C6347BZI_BLD53"], "detect_code": ["6000"], "post_binary_hook": { @@ -7874,6 +7877,7 @@ "supported_form_factors": ["ARDUINO"], "extra_labels_add": ["PSOC6_FUTURE", "CY8C63XX", "CORDIO"], "extra_labels_remove": ["PSOC6"], + "device_has_remove": ["TRNG", "CRC"], "macros_add": ["CY8C6347BZI_BLD53"], "detect_code": ["6000"], "hex_filename": "psoc63_m0_default_1.02.hex",