1*81f5b20cSNicolas Toromanoff /* SPDX-License-Identifier: BSD-3-Clause */ 2*81f5b20cSNicolas Toromanoff /* 3*81f5b20cSNicolas Toromanoff * Copyright (c) 2021-2024, STMicroelectronics - All Rights Reserved 4*81f5b20cSNicolas Toromanoff */ 5*81f5b20cSNicolas Toromanoff 6*81f5b20cSNicolas Toromanoff #ifndef STM32_PKA_H 7*81f5b20cSNicolas Toromanoff #define STM32_PKA_H 8*81f5b20cSNicolas Toromanoff 9*81f5b20cSNicolas Toromanoff #include <drivers/clk.h> 10*81f5b20cSNicolas Toromanoff #include <drivers/rstctrl.h> 11*81f5b20cSNicolas Toromanoff #include <kernel/mutex.h> 12*81f5b20cSNicolas Toromanoff #include <mm/core_memprot.h> 13*81f5b20cSNicolas Toromanoff #include <stdint.h> 14*81f5b20cSNicolas Toromanoff 15*81f5b20cSNicolas Toromanoff #define PKA_MAX_ECC_LEN 640 16*81f5b20cSNicolas Toromanoff #define PKA_MAX_ECC_SIZE (PKA_MAX_ECC_LEN / 8) 17*81f5b20cSNicolas Toromanoff 18*81f5b20cSNicolas Toromanoff enum stm32_pka_curve_id { 19*81f5b20cSNicolas Toromanoff PKA_NIST_P192, 20*81f5b20cSNicolas Toromanoff PKA_NIST_P224, 21*81f5b20cSNicolas Toromanoff PKA_NIST_P256, 22*81f5b20cSNicolas Toromanoff PKA_NIST_P384, 23*81f5b20cSNicolas Toromanoff PKA_NIST_P521, 24*81f5b20cSNicolas Toromanoff 25*81f5b20cSNicolas Toromanoff PKA_LAST_CID 26*81f5b20cSNicolas Toromanoff }; 27*81f5b20cSNicolas Toromanoff 28*81f5b20cSNicolas Toromanoff /* 29*81f5b20cSNicolas Toromanoff * struct stm32_pka_bn - Internal representation of binary number 30*81f5b20cSNicolas Toromanoff * 31*81f5b20cSNicolas Toromanoff * @val: a byte array with most significant bytes first 32*81f5b20cSNicolas Toromanoff * @size: number of bytes in @val 33*81f5b20cSNicolas Toromanoff */ 34*81f5b20cSNicolas Toromanoff struct stm32_pka_bn { 35*81f5b20cSNicolas Toromanoff uint8_t *val; 36*81f5b20cSNicolas Toromanoff size_t size; 37*81f5b20cSNicolas Toromanoff }; 38*81f5b20cSNicolas Toromanoff 39*81f5b20cSNicolas Toromanoff struct stm32_pka_point { 40*81f5b20cSNicolas Toromanoff struct stm32_pka_bn x; 41*81f5b20cSNicolas Toromanoff struct stm32_pka_bn y; 42*81f5b20cSNicolas Toromanoff }; 43*81f5b20cSNicolas Toromanoff 44*81f5b20cSNicolas Toromanoff TEE_Result stm32_pka_get_max_size(size_t *bytes, size_t *bits, 45*81f5b20cSNicolas Toromanoff const enum stm32_pka_curve_id cid); 46*81f5b20cSNicolas Toromanoff TEE_Result stm32_pka_compute_montgomery(const struct stm32_pka_bn *n, 47*81f5b20cSNicolas Toromanoff const size_t n_len, 48*81f5b20cSNicolas Toromanoff struct stm32_pka_bn *r2modn); 49*81f5b20cSNicolas Toromanoff TEE_Result stm32_pka_ecc_compute_montgomery(struct stm32_pka_bn *r2modn, 50*81f5b20cSNicolas Toromanoff const enum stm32_pka_curve_id cid); 51*81f5b20cSNicolas Toromanoff TEE_Result stm32_pka_is_point_on_curve(const struct stm32_pka_point *p, 52*81f5b20cSNicolas Toromanoff const struct stm32_pka_bn *r2modn, 53*81f5b20cSNicolas Toromanoff const enum stm32_pka_curve_id cid); 54*81f5b20cSNicolas Toromanoff TEE_Result stm32_pka_ecc_scalar_mul(const struct stm32_pka_bn *k, 55*81f5b20cSNicolas Toromanoff const struct stm32_pka_point *p, 56*81f5b20cSNicolas Toromanoff struct stm32_pka_point *kp, 57*81f5b20cSNicolas Toromanoff const enum stm32_pka_curve_id cid); 58*81f5b20cSNicolas Toromanoff TEE_Result stm32_pka_edac_gen_pubkey(const struct stm32_pka_bn *k, 59*81f5b20cSNicolas Toromanoff struct stm32_pka_point *pk, 60*81f5b20cSNicolas Toromanoff const enum stm32_pka_curve_id cid); 61*81f5b20cSNicolas Toromanoff TEE_Result stm32_pka_ecdsa_sign(const void *hash, unsigned int hash_size, 62*81f5b20cSNicolas Toromanoff struct stm32_pka_bn *sig_r, 63*81f5b20cSNicolas Toromanoff struct stm32_pka_bn *sig_s, 64*81f5b20cSNicolas Toromanoff const struct stm32_pka_bn *d, 65*81f5b20cSNicolas Toromanoff const struct stm32_pka_bn *k, 66*81f5b20cSNicolas Toromanoff const enum stm32_pka_curve_id cid); 67*81f5b20cSNicolas Toromanoff TEE_Result stm32_pka_ecdsa_verif(const void *hash, unsigned int hash_size, 68*81f5b20cSNicolas Toromanoff const struct stm32_pka_bn *sig_r, 69*81f5b20cSNicolas Toromanoff const struct stm32_pka_bn *sig_s, 70*81f5b20cSNicolas Toromanoff const struct stm32_pka_point *pk, 71*81f5b20cSNicolas Toromanoff const enum stm32_pka_curve_id cid); 72*81f5b20cSNicolas Toromanoff #endif /* STM32_PKA_H */ 73