Skip to content

Commit 2e87bc9

Browse files
committed
PSOC6: add Cypress target to mbedtls
Provide alternate mbedtls implementations for AES, ECDSA, ECP targeting PSoC 6 crypto hardware accelerator. Also add alternate implementations for SHA1, SHA256 and SHA512: disabled in mbedtld_device.h due to limitations with concurrent operation of AES and SHA accelerators. Update targets.json to enable MBEDTLS_CONFIG_HW_SUPPORT for Cypress PSoC6 boards. Disable the macro for FUTURE_SEQUANA port that doesn't yet support the TRNG and CRC HAL implementation.
1 parent 5c47eb4 commit 2e87bc9

File tree

16 files changed

+5760
-1
lines changed

16 files changed

+5760
-1
lines changed

features/mbedtls/targets/TARGET_Cypress/TARGET_PSOC6/aes_alt.c

Lines changed: 721 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
3+
* Copyright (C) 2019 Cypress Semiconductor Corporation
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* \file aes_alt.h
21+
*
22+
* \brief This file contains AES definitions and functions.
23+
*
24+
* The Advanced Encryption Standard (AES) specifies a FIPS-approved
25+
* cryptographic algorithm that can be used to protect electronic
26+
* data.
27+
*
28+
* The AES algorithm is a symmetric block cipher that can
29+
* encrypt and decrypt information. For more information, see
30+
* <em>FIPS Publication 197: Advanced Encryption Standard</em> and
31+
* <em>ISO/IEC 18033-2:2006: Information technology -- Security
32+
* techniques -- Encryption algorithms -- Part 2: Asymmetric
33+
* ciphers</em>.
34+
*
35+
* The AES-XTS block mode is standardized by NIST SP 800-38E
36+
* <https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38e.pdf>
37+
* and described in detail by IEEE P1619
38+
* <https://ieeexplore.ieee.org/servlet/opac?punumber=4375278>.
39+
*/
40+
41+
#ifndef AES_ALT_H
42+
#define AES_ALT_H
43+
44+
#if defined(MBEDTLS_AES_ALT)
45+
// Regular implementation
46+
47+
#define CY_CIPHER_MODE_CBC_ALT
48+
49+
#include <cy_crypto_common.h>
50+
#include <cy_crypto_core_aes.h>
51+
#include "cy_syslib.h"
52+
53+
/**
54+
* \brief The AES context-type definition.
55+
*/
56+
typedef struct mbedtls_aes_context
57+
{
58+
cy_stc_crypto_aes_state_t aes_state;
59+
cy_stc_crypto_aes_buffers_t aes_buffers;
60+
}
61+
mbedtls_aes_context;
62+
63+
#if defined(MBEDTLS_CIPHER_MODE_XTS)
64+
65+
/**
66+
* \brief The AES XTS context-type definition.
67+
*/
68+
typedef struct mbedtls_aes_xts_context
69+
{
70+
mbedtls_aes_context crypt; /*!< The AES context to use for AES block
71+
encryption or decryption. */
72+
mbedtls_aes_context tweak; /*!< The AES context used for tweak
73+
computation. */
74+
} mbedtls_aes_xts_context;
75+
76+
#endif /* MBEDTLS_CIPHER_MODE_XTS */
77+
78+
#endif /* MBEDTLS_AES_ALT */
79+
80+
#endif /* aes_alt.h */
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2019 Cypress Semiconductor Corporation
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+
/**
19+
* \file crypto_common.c
20+
*
21+
* \brief Source file for common mbedtls acceleration functions
22+
*
23+
*/
24+
25+
#include <string.h>
26+
27+
#include "psoc6_utils.h"
28+
29+
#include "crypto_common.h"
30+
#include "cy_crypto_common.h"
31+
#include "cy_crypto_core_hw.h"
32+
33+
int cy_hw_sha_start(uint32_t *currBlockLen, cy_stc_crypto_sha_state_t *hashState, cy_en_crypto_sha_mode_t shaMode, void *shaBuffers)
34+
{
35+
if ((hashState == NULL) || (shaBuffers == NULL) || (currBlockLen == 0))
36+
return (-1);
37+
38+
*currBlockLen = 0;
39+
40+
if (cy_reserve_crypto(CY_CRYPTO_COMMON_HW) != 0)
41+
{
42+
return (-1);
43+
}
44+
45+
Cy_Crypto_Core_Sha_Init (CRYPTO, hashState, shaMode, shaBuffers);
46+
Cy_Crypto_Core_Sha_Start(CRYPTO, hashState);
47+
48+
return (0);
49+
}
50+
51+
int cy_hw_sha_update(uint32_t *currBlockLen,
52+
cy_stc_crypto_sha_state_t *hashState,
53+
const uint8_t *in, uint32_t inlen,
54+
uint8_t *shaTempBlock)
55+
{
56+
uint32_t tmpBlockSize;
57+
uint32_t shaUpdateLen;
58+
uint32_t n;
59+
60+
if ((hashState == NULL) || (in == NULL) || (shaTempBlock == NULL) ||
61+
(currBlockLen == 0))
62+
return (-1);
63+
64+
if (hashState->blockSize == 0)
65+
return (-1);
66+
67+
tmpBlockSize = hashState->blockSize;
68+
69+
while (inlen > 0)
70+
{
71+
if (*currBlockLen == 0 && inlen >= tmpBlockSize)
72+
{
73+
shaUpdateLen = (inlen / tmpBlockSize) * tmpBlockSize;
74+
75+
Cy_Crypto_Core_Sha_Update(CRYPTO, hashState, in, shaUpdateLen);
76+
77+
inlen -= shaUpdateLen;
78+
in += shaUpdateLen;
79+
} else {
80+
n = inlen;
81+
82+
if (n > (tmpBlockSize - *currBlockLen))
83+
{
84+
n = (tmpBlockSize - *currBlockLen);
85+
}
86+
87+
memcpy(shaTempBlock + *currBlockLen, in, n);
88+
89+
*currBlockLen += n;
90+
in += n;
91+
inlen -= n;
92+
93+
if (*currBlockLen == tmpBlockSize)
94+
{
95+
Cy_Crypto_Core_Sha_Update(CRYPTO, hashState, shaTempBlock, tmpBlockSize);
96+
97+
*currBlockLen = 0;
98+
}
99+
}
100+
}
101+
102+
return (0);
103+
}
104+
105+
int cy_hw_sha_finish(cy_stc_crypto_sha_state_t *hashState,
106+
uint8_t *output,
107+
uint8_t *shaTempBlock, uint32_t currBlockLen)
108+
{
109+
if ((hashState == NULL) || (output == NULL) || (shaTempBlock == NULL))
110+
return (-1);
111+
112+
if (hashState->blockSize == 0)
113+
return (-1);
114+
115+
Cy_Crypto_Core_Sha_Update(CRYPTO, hashState, shaTempBlock, currBlockLen);
116+
Cy_Crypto_Core_Sha_Finish(CRYPTO, hashState, output);
117+
Cy_Crypto_Core_Sha_Free (CRYPTO, hashState);
118+
119+
cy_free_crypto(CY_CRYPTO_COMMON_HW);
120+
121+
return (0);
122+
}
123+
124+
int cy_hw_sha_process(cy_stc_crypto_sha_state_t *hashState, const uint8_t *in)
125+
{
126+
Cy_Crypto_Core_Sha_Update(CRYPTO, hashState, in, hashState->blockSize);
127+
128+
return (0);
129+
}
130+
131+
cy_en_crypto_ecc_curve_id_t get_dp_idx(mbedtls_ecp_group_id gid)
132+
{
133+
cy_en_crypto_ecc_curve_id_t dp_idx;
134+
135+
switch( gid )
136+
{
137+
case MBEDTLS_ECP_DP_SECP192R1:
138+
dp_idx = CY_CRYPTO_ECC_ECP_SECP192R1;
139+
break;
140+
case MBEDTLS_ECP_DP_SECP224R1:
141+
dp_idx = CY_CRYPTO_ECC_ECP_SECP224R1;
142+
break;
143+
case MBEDTLS_ECP_DP_SECP256R1:
144+
dp_idx = CY_CRYPTO_ECC_ECP_SECP256R1;
145+
break;
146+
case MBEDTLS_ECP_DP_SECP384R1:
147+
dp_idx = CY_CRYPTO_ECC_ECP_SECP384R1;
148+
break;
149+
case MBEDTLS_ECP_DP_SECP521R1:
150+
dp_idx = CY_CRYPTO_ECC_ECP_SECP521R1;
151+
break;
152+
153+
default:
154+
dp_idx = CY_CRYPTO_ECC_ECP_NONE;
155+
break;
156+
}
157+
158+
return dp_idx;
159+
}
160+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2019 Cypress Semiconductor Corporation
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+
/**
19+
* \file crypto_common.h
20+
*
21+
* \brief Header file for common mbedtls acceleration functions
22+
*
23+
*/
24+
25+
#if !defined(CRYPTO_COMMON_H)
26+
#define CRYPTO_COMMON_H
27+
28+
#if !defined(MBEDTLS_CONFIG_FILE)
29+
#include "mbedtls/config.h"
30+
#else
31+
#include MBEDTLS_CONFIG_FILE
32+
#endif
33+
34+
#include "mbedtls/ecp.h"
35+
36+
#include "cy_crypto_core_sha.h"
37+
#include "cy_crypto_core_ecc.h"
38+
39+
int cy_hw_sha_start (uint32_t *currBlockLen, cy_stc_crypto_sha_state_t *hashState,
40+
cy_en_crypto_sha_mode_t shaMode, void *shaBuffers);
41+
42+
int cy_hw_sha_update(uint32_t *currBlockLen, cy_stc_crypto_sha_state_t *hashState,
43+
const uint8_t *in, uint32_t inlen, uint8_t *shaTempBlock);
44+
45+
int cy_hw_sha_finish(cy_stc_crypto_sha_state_t *hashState, uint8_t *output,
46+
uint8_t *shaTempBlock, uint32_t currBlockLen);
47+
48+
int cy_hw_sha_process(cy_stc_crypto_sha_state_t *hashState, const uint8_t *in);
49+
50+
cy_en_crypto_ecc_curve_id_t get_dp_idx(mbedtls_ecp_group_id gid);
51+
52+
#endif /* (CRYPTO_COMMON_H) */

0 commit comments

Comments
 (0)