Skip to content

Commit a6311f6

Browse files
committed
nrf_security: CRACEN: Let ecdsa sign/verify digest without hash
ECDSA_SIGN/VERIFY_HASH should not require a hash algorithm to be present. Updated so it is not required. Signed-off-by: Dag Erik Gjørvad <[email protected]>
1 parent 2071f46 commit a6311f6

File tree

2 files changed

+28
-17
lines changed
  • subsys/nrf_security/src/drivers/cracen/cracenpsa/src

2 files changed

+28
-17
lines changed

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/ecdsa.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,27 +215,31 @@ int cracen_ecdsa_sign_digest(const struct cracen_ecc_priv_key *privkey,
215215
const uint8_t *digest, size_t digest_length, uint8_t *signature)
216216
{
217217
int status;
218-
const size_t digestsz = sx_hash_get_alg_digestsz(hashalg);
219218
size_t opsz = sx_pk_curve_opsize(curve);
220219
struct sx_pk_acq_req pkreq;
221220
struct sx_pk_inops_ecdsa_generate inputs;
222221
const uint8_t *curve_n;
223-
const size_t workmem_requirement = digestsz + opsz;
222+
const size_t workmem_requirement = digest_length + opsz;
224223
struct cracen_signature internal_signature = {0};
225224
uint8_t workmem[workmem_requirement];
226225

226+
/* Checking against the hash algoritm with the largest digest we support */
227+
if (digest_length > SX_HASH_DIGESTSZ_SHA2_512) {
228+
return SX_ERR_TOO_BIG;
229+
}
230+
227231
memcpy(workmem, digest, digest_length);
228232
curve_n = sx_pk_curve_order(curve);
229233

230234
internal_signature.r = signature;
231235
internal_signature.s = signature + opsz;
232236

233237
for (int i = 0; i <= MAX_ECDSA_ATTEMPTS; i++) {
234-
status = cracen_get_rnd_in_range(curve_n, opsz, workmem + digestsz);
238+
status = cracen_get_rnd_in_range(curve_n, opsz, workmem + digest_length);
235239
if (status != SX_OK) {
236240
return status;
237241
}
238-
status = ecdsa_run_generate_sign(&pkreq, privkey, curve, workmem, digestsz, opsz,
242+
status = ecdsa_run_generate_sign(&pkreq, privkey, curve, workmem, digest_length, opsz,
239243
&inputs);
240244

241245
if (status != SX_OK) {

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/sign.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,16 @@ static psa_status_t handle_ecdsa_sign(bool is_message, const uint8_t *key_buffer
246246
const struct sxhashalg *hashalgpointer = &hashalg;
247247

248248
privkey.d = key_buffer;
249-
status = hash_get_algo(alg, &hashalgpointer);
250-
if (status != PSA_SUCCESS) {
251-
return status;
252-
}
253249

254250
*signature_length = 2 * ecurve->sz;
255251
status = SX_ERR_INCOMPATIBLE_HW;
256252

257253
if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) &&
258254
IS_ENABLED(PSA_NEED_CRACEN_DETERMINISTIC_ECDSA)) {
255+
status = hash_get_algo(alg, &hashalgpointer);
256+
if (status != PSA_SUCCESS) {
257+
return status;
258+
}
259259
if (is_message) {
260260
status = cracen_ecdsa_sign_message_deterministic(
261261
&privkey, hashalgpointer, ecurve, input, input_length, signature);
@@ -266,6 +266,10 @@ static psa_status_t handle_ecdsa_sign(bool is_message, const uint8_t *key_buffer
266266
} else if ((PSA_ALG_IS_ECDSA(alg) && IS_ENABLED(PSA_NEED_CRACEN_ECDSA)) &&
267267
!PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)) {
268268
if (is_message) {
269+
status = hash_get_algo(alg, &hashalgpointer);
270+
if (status != PSA_SUCCESS) {
271+
return status;
272+
}
269273
status = cracen_ecdsa_sign_message(&privkey, hashalgpointer, ecurve, input,
270274
input_length, signature);
271275
} else {
@@ -402,19 +406,22 @@ static psa_status_t cracen_signature_ecc_verify(bool is_message,
402406
const struct sxhashalg *hash_algorithm_ptr = &hashalg;
403407

404408
psa_status = cracen_ecc_get_ecurve_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(key_type),
405-
psa_get_key_bits(attributes), &curve);
409+
psa_get_key_bits(attributes), &curve);
406410
if (psa_status != PSA_SUCCESS) {
407411
return psa_status;
408412
}
409-
psa_status = hash_get_algo(alg, &hash_algorithm_ptr);
410-
if (psa_status != PSA_SUCCESS) {
411-
return psa_status;
413+
if (is_message) {
414+
psa_status = hash_get_algo(alg, &hash_algorithm_ptr);
415+
if (psa_status != PSA_SUCCESS) {
416+
return psa_status;
417+
}
418+
sx_status = cracen_ecdsa_verify_message(pubkey_buffer, hash_algorithm_ptr,
419+
input, input_length, curve,
420+
signature);
421+
} else {
422+
sx_status = cracen_ecdsa_verify_digest(pubkey_buffer, input, input_length,
423+
curve, signature);
412424
}
413-
sx_status = is_message ? cracen_ecdsa_verify_message(pubkey_buffer,
414-
hash_algorithm_ptr, input,
415-
input_length, curve, signature)
416-
: cracen_ecdsa_verify_digest(pubkey_buffer, input,
417-
input_length, curve, signature);
418425
} else {
419426
return PSA_ERROR_NOT_SUPPORTED;
420427
}

0 commit comments

Comments
 (0)