Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of PKA peripheral on stm32mp13 #7151

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

tbourgoi
Copy link
Contributor

Add support of PKA peripheral for stm32mp13.
This PR :
- fix the device tree node
- add the driver stm32-pka.c
- compile the driver by default
- enable pka on stm32mp135-dk

tbourgoi and others added 6 commits November 25, 2024 14:43
use st,stm32mp13-pka instead of st,stm32mp13-pka64 to be aligned with
all other st compatibles.

Signed-off-by: Thomas Bourgoin <[email protected]>
Adds missing RNG clock resource in PKA nodes in stm32mp13 SoC DTSI
files.

Signed-off-by: Etienne Carriere <[email protected]>
Signed-off-by: Thomas Bourgoin <[email protected]>
Add PKA IP drivers, and add hooks in OP-TEE crypto framework to use
PKA IP to do ECC process.

Truncate hash during ECDSA signature according to the NIST
recommendation:
https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf

Signed-off-by: Thomas Bourgoin <[email protected]>
Co-developed-by: Patrick Delaunay <[email protected]>
Signed-off-by: Patrick Delaunay <[email protected]>
Signed-off-by: Nicolas Toromanoff <[email protected]>
Fix regression of xtest 4006 testing TEE_ALG_SM2_PKE.
PKA only support ECDSA key generation ECDH key exchange.

Signed-off-by: Thomas Bourgoin <[email protected]>
Default enable PKA compilation.
Enable the STM32_CRYPTO_DRIVERS if PKA is compiled.

Signed-off-by: Thomas Bourgoin <[email protected]>
Sets PKA peripheral status to okay.

Signed-off-by: Thomas Bourgoin <[email protected]>
@tbourgoi tbourgoi changed the title Pka Add support of PKA on stm32mp13 Nov 25, 2024
@tbourgoi tbourgoi changed the title Add support of PKA on stm32mp13 Add support of PKA peripheral on stm32mp13 Nov 25, 2024
core/drivers/crypto/stm32/stm32_pka.h Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/ecc.c Show resolved Hide resolved
core/drivers/crypto/stm32/ecc.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Outdated Show resolved Hide resolved
ecc.c :
   swap the 2 include lines
   change DMSG to FMSG
   Use common out label
stm32_pka.c :
   reorder _PKA_CRxx defines
   use upper cases for pka_op enum values
   update documentation for struct curve_parameters
   update documentation of read_eo_data()
   use IO_READ32_POLL_TIMEOUT, SHIFT_U32 and SHIF_U64 macros
   use EMSG for error messages.
   Add const for pka_ram offset definition
   Use bit_mask as parameter for pka_wait_bit() function
   fix documentation of read_eo_data()
   Use U() or ULL() insteald of U and ULL
   Use type enum pka_op instead of int for parameter of op in
   stm32_pka_configure_curve()
stm32_pka.h :
   Fix date issue

Signed-off-by: Thomas Bourgoin <[email protected]>
Use TEE_TYPE_xxx_PUBLIC_KEY instead of TEE_TYPE_xxx_KEYPAIR in
stm32_alloc_publickey()

Signed-off-by: Thomas Bourgoin <[email protected]>
Use fdt_reg_info() to read base address and size instead of
fdt_fill_device_info().

Signed-off-by: Thomas Bourgoin <[email protected]>
core/drivers/crypto/stm32/ecc.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/ecc.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/stm32_pka.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/ecc.c Outdated Show resolved Hide resolved
core/drivers/crypto/stm32/ecc.c Outdated Show resolved Hide resolved
ecc.c :
       use PKA_LAST_CID instead of -1;
       use free_wipe() where it is relevant
stm32_pka.c :
       change len to length
       fix coding style for curve parameters
       fix inline documentation
       use for loop instead of do{}while() in function is_smaller()

Signed-off-by: Thomas Bourgoin <[email protected]>
@@ -107,15 +108,15 @@ static TEE_Result stm32_gen_keypair(struct ecc_keypair *key, size_t size_bits)
pk.x.val = calloc(1, bytes);
pk.x.size = bytes;
if (!pk.x.val) {
free(d.val);
free_wipe(d.val);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this stage, d does not hold a secret. free() is enough IMHO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d.val has been filled (with the random value)
If free_wipe does not apply here it is useless for d.val in all below cases IMO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, upon other error cases in this function, d does not hold any secret. You can restore the free() operation at line 119, but IMO you should preserve the free_wipe() operation at function exit at line 140.

free(pk.x.val);
free(d.val);
free_wipe(pk.y.val);
free_wipe(pk.x.val);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pk is a public key. I don't think it deserves a wipe operation.

.size = U(24),
},
.a_sign = U(1),
.a = { .val = (uint8_t[]){0x03},
.a = {
.val = (uint8_t[]){0x03},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicking: { 0x03 } (with space chars inside the braces)
Ditto at lines 353, 425, 509 and 614.

@@ -863,24 +949,20 @@ static bool is_smaller(const struct stm32_pka_bn *a,
{
unsigned int i = MAX(a->size, b->size) + 1;

do {
for (; i != U(1); i--) {
Copy link
Contributor

@etienne-lms etienne-lms Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer with i initial value defined within the for () instruction.
Note that i shall start at MAX() without the increment previously used and stop once reaching 0:
for (i = MAX(a->size, b->size); i; i--)

while ((i < sizeof(tmp)) && (data_index >= 0)) {
tmp |= SHIFT_U64(data[data_index], (INT8_LEN * i));
i++; /* Move byte index in current (u64)tmp */
data_index--; /* Move to next most significant byte */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

			/* Move byte index in current (u64)tmp */
			i++; 
			/* Move to next most significant byte */
			data_index--;

if (PM_HINT_IS_STATE(pm_hint, CONTEXT)) {
if (stm32_pka_reset())
panic();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicnking:

		if (PM_HINT_IS_STATE(pm_hint, CONTEXT) && stm32_pka_reset())
			panic();

struct stm32_pka_bn sig_s = { };
struct stm32_pka_point pk = { };
enum stm32_pka_curve_id cid_from_algo = PKA_LAST_CID;
enum stm32_pka_curve_id cid = -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/-1/PKA_LAST_CID/

* @n: Curve prime order n
* @n_len: Curve prime order bit size
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this empty line

struct mutex *lock;
};

static struct stm32_pka_platdata pka_pdata = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use an explicit all-zero initializer.

if (!n_len || !n || !r2modn)
return TEE_ERROR_BAD_PARAMETERS;

mutex_lock(pka_pdata.lock);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would'nt you prefer to use mutex_lock(&pka_lock) straight here and everywhere the mutex is locked/unlocked?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants