|
| 1 | +/* Isolated key operations (signature generation, ...) |
| 2 | + * |
| 3 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 6 | + * |
| 7 | + * Workmem layout for the sign operation: |
| 8 | + * 1. Hash digest of the message to be signed (size: digestsz). |
| 9 | + * |
| 10 | + * Workmem layout for the sign digest operation: |
| 11 | + * 1. Hash digest of the message to be signed (size: digestsz). |
| 12 | + * |
| 13 | + * Other IKG tasks don't need workmem memory. |
| 14 | + */ |
| 15 | + |
| 16 | +#include <stdint.h> |
| 17 | +#include <string.h> |
| 18 | +#include <silexpk/core.h> |
| 19 | +#include <silexpk/iomem.h> |
| 20 | +#include <sxsymcrypt/hash.h> |
| 21 | +#include <sxsymcrypt/hashdefs.h> |
| 22 | +#include <silexpk/ik.h> |
| 23 | +#include <cracen/statuscodes.h> |
| 24 | +#include <cracen_psa_ikg.h> |
| 25 | +#include <cracen_psa_primitives.h> |
| 26 | +#include "common.h" |
| 27 | + |
| 28 | +#define MAX_ECDSA_ATTEMPTS 255 |
| 29 | + |
| 30 | +/** Convert a digest into an operand for ECDSA |
| 31 | + * |
| 32 | + * The raw digest may need to be padded or truncated to fit the curve |
| 33 | + * operand used for ECDSA. |
| 34 | + * |
| 35 | + * Conversion could also imply byte order inversion, but that is not |
| 36 | + * implemented here. It's expected that SilexPK takes big endian |
| 37 | + * operands here. |
| 38 | + * |
| 39 | + * As the digest size is expressed in bytes, this procedure does not |
| 40 | + * work for curves which have sizes not multiples of 8 bits. |
| 41 | + */ |
| 42 | +static void digest2op(const uint8_t *digest, size_t sz, uint8_t *dst, size_t opsz) |
| 43 | +{ |
| 44 | + if (opsz > sz) { |
| 45 | + sx_clrpkmem(dst, opsz - sz); |
| 46 | + dst += opsz - sz; |
| 47 | + } |
| 48 | + sx_wrpkmem(dst, digest, opsz); |
| 49 | +} |
| 50 | + |
| 51 | +static void ecdsa_read_sig(struct ecdsa_signature *sig, const uint8_t *r, const uint8_t *s, |
| 52 | + size_t opsz) |
| 53 | +{ |
| 54 | + sx_rdpkmem(sig->r, r, opsz); |
| 55 | + if (!sig->s) { |
| 56 | + sig->s = sig->r + opsz; |
| 57 | + } |
| 58 | + sx_rdpkmem(sig->s, s, opsz); |
| 59 | +} |
| 60 | + |
| 61 | +static int exit_ikg(struct sx_pk_acq_req *pkreq) |
| 62 | +{ |
| 63 | + |
| 64 | + int status; |
| 65 | + |
| 66 | + sx_pk_release_req(pkreq->req); |
| 67 | + *pkreq = sx_pk_acquire_req(SX_PK_CMD_IK_EXIT); |
| 68 | + pkreq->status = sx_pk_list_ik_inslots(pkreq->req, 0, NULL); |
| 69 | + if (pkreq->status) { |
| 70 | + return pkreq->status; |
| 71 | + } |
| 72 | + sx_pk_run(pkreq->req); |
| 73 | + status = sx_pk_wait(pkreq->req); |
| 74 | + if (status != SX_OK) { |
| 75 | + return status; |
| 76 | + } |
| 77 | + sx_pk_release_req(pkreq->req); |
| 78 | + return SX_OK; |
| 79 | +} |
| 80 | + |
| 81 | +int cracen_ikg_sign_message(int identity_key_index, const struct sxhashalg *hashalg, |
| 82 | + const struct sx_pk_ecurve *curve, const uint8_t *message, |
| 83 | + size_t message_length, uint8_t *signature) |
| 84 | +{ |
| 85 | + int status; |
| 86 | + size_t digestsz = sx_hash_get_alg_digestsz(hashalg); |
| 87 | + uint8_t digest[digestsz]; |
| 88 | + |
| 89 | + status = hash_input(message, message_length, hashalg, digest); |
| 90 | + if (status != SX_OK) { |
| 91 | + return status; |
| 92 | + } |
| 93 | + |
| 94 | + return cracen_ikg_sign_digest(identity_key_index, hashalg, curve, digest, digestsz, |
| 95 | + signature); |
| 96 | +} |
| 97 | + |
| 98 | +int cracen_ikg_sign_digest(int identity_key_index, const struct sxhashalg *hashalg, |
| 99 | + const struct sx_pk_ecurve *curve, const uint8_t *digest, |
| 100 | + size_t digest_length, uint8_t *signature) |
| 101 | +{ |
| 102 | + int status; |
| 103 | + size_t opsz = sx_pk_curve_opsize(curve); |
| 104 | + struct sx_pk_acq_req pkreq; |
| 105 | + struct sx_pk_inops_ik_ecdsa_sign inputs; |
| 106 | + size_t digestsz = sx_hash_get_alg_digestsz(hashalg); |
| 107 | + const uint8_t *curve_n; |
| 108 | + uint8_t workmem[digestsz]; |
| 109 | + struct ecdsa_signature internal_signature = {0}; |
| 110 | + |
| 111 | + memcpy(workmem, digest, digest_length); |
| 112 | + curve_n = sx_pk_curve_order(curve); |
| 113 | + |
| 114 | + internal_signature.r = signature; |
| 115 | + internal_signature.s = signature + opsz; |
| 116 | + |
| 117 | + for (int i = 0; i <= MAX_ECDSA_ATTEMPTS; i++) { |
| 118 | + |
| 119 | + pkreq = sx_pk_acquire_req(SX_PK_CMD_IK_ECDSA_SIGN); |
| 120 | + if (pkreq.status) { |
| 121 | + return pkreq.status; |
| 122 | + } |
| 123 | + pkreq.status = sx_pk_list_ik_inslots(pkreq.req, identity_key_index, |
| 124 | + (struct sx_pk_slot *)&inputs); |
| 125 | + if (pkreq.status) { |
| 126 | + return pkreq.status; |
| 127 | + } |
| 128 | + |
| 129 | + digest2op(workmem, digestsz, inputs.h.addr, opsz); |
| 130 | + sx_pk_run(pkreq.req); |
| 131 | + status = sx_pk_wait(pkreq.req); |
| 132 | + if (status != SX_OK) { |
| 133 | + return status; |
| 134 | + } |
| 135 | + |
| 136 | + /* SX_ERR_NOT_INVERTIBLE may be due to silexpk countermeasures. */ |
| 137 | + if ((status == SX_ERR_INVALID_SIGNATURE) || (status == SX_ERR_NOT_INVERTIBLE)) { |
| 138 | + sx_pk_release_req(pkreq.req); |
| 139 | + if (i == MAX_ECDSA_ATTEMPTS) { |
| 140 | + return SX_ERR_TOO_MANY_ATTEMPTS; |
| 141 | + } |
| 142 | + } else { |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + if (status != SX_OK) { |
| 147 | + sx_pk_release_req(pkreq.req); |
| 148 | + return status; |
| 149 | + } |
| 150 | + |
| 151 | + const uint8_t **outputs = (const uint8_t **)sx_pk_get_output_ops(pkreq.req); |
| 152 | + |
| 153 | + ecdsa_read_sig(&internal_signature, outputs[0], outputs[1], opsz); |
| 154 | + safe_memzero(workmem, digestsz); |
| 155 | + |
| 156 | + return exit_ikg(&pkreq); |
| 157 | +} |
| 158 | + |
| 159 | +int cracen_ikg_create_pub_key(int identity_key_index, uint8_t *pub_key) |
| 160 | +{ |
| 161 | + int status; |
| 162 | + struct sx_pk_acq_req pkreq; |
| 163 | + |
| 164 | + pkreq = sx_pk_acquire_req(SX_PK_CMD_IK_PUBKEY_GEN); |
| 165 | + if (pkreq.status) { |
| 166 | + return pkreq.status; |
| 167 | + } |
| 168 | + pkreq.status = sx_pk_list_ik_inslots(pkreq.req, identity_key_index, NULL); |
| 169 | + if (pkreq.status) { |
| 170 | + return pkreq.status; |
| 171 | + } |
| 172 | + |
| 173 | + sx_pk_run(pkreq.req); |
| 174 | + status = sx_pk_wait(pkreq.req); |
| 175 | + if (status != SX_OK) { |
| 176 | + return status; |
| 177 | + } |
| 178 | + const uint8_t **outputs = (const uint8_t **)sx_pk_get_output_ops(pkreq.req); |
| 179 | + const int opsz = sx_pk_get_opsize(pkreq.req); |
| 180 | + |
| 181 | + sx_rdpkmem(pub_key, outputs[0], opsz); |
| 182 | + sx_rdpkmem(pub_key + opsz, outputs[1], opsz); |
| 183 | + |
| 184 | + return exit_ikg(&pkreq); |
| 185 | +} |
0 commit comments