|
| 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