Skip to content

Commit 432c29d

Browse files
committed
PSOC6: add CRC and TRNG APIs
Exclude the labels from inherited FUTURE_SEQUANA targets.
1 parent 6d932c6 commit 432c29d

File tree

3 files changed

+178
-1
lines changed

3 files changed

+178
-1
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2019 Cypress Semiconductor Corporation
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "device.h"
19+
20+
#if DEVICE_CRC
21+
22+
#include "mbed_assert.h"
23+
#include "crc_api.h"
24+
25+
#include "psoc6_utils.h"
26+
27+
#include "cy_crypto_core_crc.h"
28+
29+
static uint32_t crcWidth = 0;
30+
static uint32_t crcShift = 0;
31+
static uint32_t crcXorMask;
32+
33+
/* Cypress CRYPTO HW supports ANY CRC algorithms from CRC-3 to CRC-32 */
34+
bool hal_crc_is_supported(const crc_mbed_config_t *config)
35+
{
36+
return (config != NULL);
37+
}
38+
39+
void hal_crc_compute_partial_start(const crc_mbed_config_t *config)
40+
{
41+
uint32_t myMask = 0;
42+
43+
if (!hal_crc_is_supported(config) || (cy_reserve_crypto(CY_CRYPTO_CRC_HW) != 0))
44+
{
45+
return;
46+
}
47+
48+
crcWidth = config->width;
49+
50+
crcShift = (uint32_t)(!config->reflect_out) * (crcWidth & 7u);
51+
if (crcShift) {
52+
crcShift = 8u - crcShift;
53+
for (uint32_t i = 0; i < crcShift; i++) {
54+
myMask |= 1 << i;
55+
}
56+
crcXorMask = config->final_xor & myMask;
57+
}
58+
59+
Cy_Crypto_Core_Crc_CalcInit(CRYPTO, config->width,
60+
config->polynomial,
61+
config->reflect_in,
62+
0,
63+
config->reflect_out,
64+
config->final_xor >> crcShift,
65+
config->initial_xor);
66+
}
67+
68+
void hal_crc_compute_partial(const uint8_t *data, const size_t size)
69+
{
70+
if ((data == NULL) || (size <= 0) || (crcWidth == 0)) {
71+
return;
72+
}
73+
74+
Cy_Crypto_Core_Crc_CalcPartial(CRYPTO, data, size);
75+
}
76+
77+
uint32_t hal_crc_get_result(void)
78+
{
79+
uint32_t result = 0;
80+
81+
if (crcWidth == 0) {
82+
return 0xffffffffu;
83+
}
84+
85+
Cy_Crypto_Core_Crc_CalcFinish(CRYPTO, crcWidth, &result);
86+
87+
if (crcShift) {
88+
result = result << crcShift;
89+
result = result ^ crcXorMask;
90+
}
91+
92+
crcWidth = 0;
93+
94+
cy_free_crypto(CY_CRYPTO_CRC_HW);
95+
96+
return result;
97+
}
98+
99+
#endif //DEVICE_CRC
100+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2019 Cypress Semiconductor Corporation
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/* Hardware entropy collector for the PSoC6A families */
19+
20+
#if DEVICE_TRNG
21+
22+
#include "trng_api.h"
23+
#include "psoc6_utils.h"
24+
#include "cy_crypto_core_trng.h"
25+
26+
/* Initialization polynomial values fro True Random Generator */
27+
#define GARO31_INITSTATE (0x04c11db7u)
28+
#define FIRO31_INITSTATE (0x04c11db7u)
29+
30+
#define MAX_TRNG_BIT_SIZE (32UL)
31+
32+
void trng_init(trng_t *obj)
33+
{
34+
(void)obj;
35+
36+
cy_reserve_crypto(CY_CRYPTO_TRNG_HW);
37+
}
38+
39+
void trng_free(trng_t *obj)
40+
{
41+
/* Deinitialization is not needed for the driver */
42+
(void)obj;
43+
44+
cy_free_crypto(CY_CRYPTO_TRNG_HW);
45+
}
46+
47+
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
48+
{
49+
int ret = 0;
50+
*output_length = 0;
51+
52+
/* temporary random data buffer */
53+
uint32_t random;
54+
55+
(void)obj;
56+
57+
/* Get Random byte */
58+
while ((*output_length < length) && (ret == 0)) {
59+
if (Cy_Crypto_Core_Trng(CRYPTO, GARO31_INITSTATE, FIRO31_INITSTATE, MAX_TRNG_BIT_SIZE, &random) != CY_CRYPTO_SUCCESS) {
60+
ret = -1;
61+
} else {
62+
for (uint8_t i = 0; (i < 4) && (*output_length < length) ; i++) {
63+
*output++ = ((uint8_t *)&random)[i];
64+
*output_length += 1;
65+
}
66+
}
67+
}
68+
random = 0uL;
69+
70+
return (ret);
71+
}
72+
73+
#endif

targets/targets.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7824,7 +7824,9 @@
78247824
"STDIO_MESSAGES",
78257825
"LPTICKER",
78267826
"SLEEP",
7827-
"FLASH"
7827+
"FLASH",
7828+
"TRNG",
7829+
"CRC"
78287830
],
78297831
"release_versions": ["5"],
78307832
"extra_labels": ["Cypress", "PSOC6"],
@@ -7846,6 +7848,7 @@
78467848
"supported_form_factors": ["ARDUINO"],
78477849
"extra_labels_add": ["PSOC6_FUTURE", "CY8C63XX", "FUTURE_SEQUANA"],
78487850
"extra_labels_remove": ["PSOC6"],
7851+
"device_has_remove": ["TRNG", "CRC"],
78497852
"macros_add": ["CY8C6347BZI_BLD53"],
78507853
"detect_code": ["6000"],
78517854
"post_binary_hook": {
@@ -7874,6 +7877,7 @@
78747877
"supported_form_factors": ["ARDUINO"],
78757878
"extra_labels_add": ["PSOC6_FUTURE", "CY8C63XX", "CORDIO"],
78767879
"extra_labels_remove": ["PSOC6"],
7880+
"device_has_remove": ["TRNG", "CRC"],
78777881
"macros_add": ["CY8C6347BZI_BLD53"],
78787882
"detect_code": ["6000"],
78797883
"hex_filename": "psoc63_m0_default_1.02.hex",

0 commit comments

Comments
 (0)