Skip to content

Commit 9225cbf

Browse files
committed
nrf_security: drivers: cracen: Remove sicrypto for ecdsa
Add support for ecdsa directly in cracenpsa Remove sicrypto from cracenpsa for escda operations Add support files needed for escda, randinrange, hmac, ecc. Add privat and public ecc key generation without sicrypto Signed-off-by: Dag Erik Gjørvad <dag.erik.gjorvad@nordicsemi.no>
1 parent 876a582 commit 9225cbf

File tree

7 files changed

+989
-99
lines changed

7 files changed

+989
-99
lines changed

subsys/nrf_security/src/drivers/cracen/cracenpsa/cracenpsa.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ if(CONFIG_PSA_NEED_CRACEN_ASYMMETRIC_SIGNATURE_DRIVER)
4545
list(APPEND cracen_driver_sources
4646
${CMAKE_CURRENT_LIST_DIR}/src/sign.c
4747
${CMAKE_CURRENT_LIST_DIR}/src/ed25519.c
48+
${CMAKE_CURRENT_LIST_DIR}/src/ecdsa.c
49+
4850
)
4951
endif()
5052

@@ -63,6 +65,8 @@ endif()
6365
if(CONFIG_PSA_NEED_CRACEN_KEY_MANAGEMENT_DRIVER OR CONFIG_PSA_NEED_CRACEN_KMU_DRIVER OR CONFIG_MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
6466
list(APPEND cracen_driver_sources
6567
${CMAKE_CURRENT_LIST_DIR}/src/ed25519.c
68+
${CMAKE_CURRENT_LIST_DIR}/src/ecdsa.c
69+
${CMAKE_CURRENT_LIST_DIR}/src/ecc.c
6670
${CMAKE_CURRENT_LIST_DIR}/src/key_management.c
6771
)
6872
endif()

subsys/nrf_security/src/drivers/cracen/cracenpsa/include/cracen_psa.h

+31
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,35 @@ int ed25519ph_verify(const uint8_t *pubkey, const char *message,
377377
int ed25519_create_pubkey(const uint8_t *privkey,
378378
uint8_t *pubkey);
379379

380+
struct sx_pk_ecurve;
381+
382+
struct ecdsa_signature {
383+
size_t sz; /**< Total signature size, in bytes. */
384+
char *r; /**< Signature element "r". */
385+
char *s; /**< Signature element "s". */
386+
};
387+
struct eccsk {
388+
const struct sx_pk_ecurve *curve;
389+
char *d;
390+
};
391+
392+
struct eccpk {
393+
const struct sx_pk_ecurve *curve;
394+
char *qx;
395+
char *qy;
396+
};
397+
398+
struct ecc_keypair {
399+
struct eccsk sk;
400+
struct eccpk pk;
401+
};
402+
403+
int ecc_create_genpubkey(const struct si_eccsk *sk, struct si_eccpk *pk, struct sx_pk_ecurve *curve);
404+
405+
void ecc_create_genprivkey(const struct sx_pk_ecurve *curve,
406+
char *priv_key, size_t priv_key_size);
407+
408+
409+
410+
380411
#endif /* CRACEN_PSA_H */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* ECC key pair generation.
2+
* Based on FIPS 186-4, section B.4.2 "Key Pair Generation by Testing
3+
* Candidates".
4+
*
5+
* Copyright (c) 2023 Nordic Semiconductor ASA
6+
*
7+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
8+
*/
9+
10+
#include <string.h>
11+
#include <silexpk/core.h>
12+
#include <silexpk/iomem.h>
13+
#include <silexpk/cmddefs/ecc.h>
14+
#include <cracen/statuscodes.h>
15+
#include "cracen_psa.h"
16+
#include "final.h"
17+
#include "util.h"
18+
19+
#define MAX_ECC_ATTEMPTS 10
20+
21+
int ecc_create_genpubkey(const struct si_eccsk *sk, struct si_eccpk *pk, struct sx_pk_ecurve *curve)
22+
{
23+
int status;
24+
int opsz;
25+
int attempts = MAX_ECC_ATTEMPTS;
26+
struct sx_pk_acq_req pkreq;
27+
struct sx_pk_inops_ecp_mult inputs;
28+
29+
for (int i = 0; i <= MAX_ECC_ATTEMPTS; i++) {
30+
pkreq = sx_pk_acquire_req(SX_PK_CMD_ECC_PTMUL);
31+
if (pkreq.status) {
32+
return pkreq.status;
33+
}
34+
35+
pkreq.status = sx_pk_list_ecc_inslots(pkreq.req, curve, 0,
36+
(struct sx_pk_slot *)&inputs);
37+
if (pkreq.status) {
38+
return pkreq.status;
39+
}
40+
opsz = sx_pk_curve_opsize(curve);
41+
42+
/* Write the private key (random) into ba414ep device memory */
43+
sx_wrpkmem(inputs.k.addr, &pk.d, opsz);
44+
sx_pk_write_curve_gen(pkreq.req, curve, inputs.px, inputs.py);
45+
sx_pk_run(pkreq.req);
46+
47+
status = sx_pk_has_finished(pkreq.req);
48+
49+
if (status == SX_ERR_BUSY) {
50+
return SX_ERR_HW_PROCESSING;
51+
}
52+
53+
status = sx_pk_wait(pkreq.req);
54+
55+
/* static int on_generated_public(struct sitask *t, struct siwq *wq) */
56+
const char **outputs = sx_pk_get_output_ops(pkreq.req);
57+
58+
/* When countermeasures are used, the operation may fail with error code
59+
* SX_ERR_NOT_INVERTIBLE. In this case we can try again.
60+
*/
61+
if (status == SX_ERR_NOT_INVERTIBLE) {
62+
sx_pk_release_req(pkreq.req);
63+
if (i == MAX_ECC_ATTEMPTS) {
64+
return SX_ERR_TOO_MANY_ATTEMPTS;
65+
}
66+
} else {
67+
break;
68+
}
69+
70+
}
71+
sx_rdpkmem(pk->qx, outputs[0], opsz);
72+
sx_rdpkmem(pk->qy, outputs[1], opsz);
73+
sx_pk_release_req(pkreq.req);
74+
75+
return status;
76+
}
77+
78+
int ecc_create_genprivkey(const struct sx_pk_ecurve *curve,
79+
char *priv_key, size_t priv_key_size)
80+
{
81+
int status;
82+
int opsz = sx_pk_curve_opsize(curve);
83+
size_t keysz = (size_t)sx_pk_curve_opsize(curve);
84+
const char *curve_n = sx_pk_curve_order(curve);
85+
86+
if (priv_key_size < keysz) {
87+
return SX_ERR_OUTPUT_BUFFER_TOO_SMALL;
88+
}
89+
90+
/* generate private key, a random in [1, n-1], where n is the curve
91+
* order
92+
*/
93+
status = rndinrange_create((const unsigned char *)curve_n, opsz, priv_key);
94+
95+
return status;
96+
}

0 commit comments

Comments
 (0)