Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nrf_security: drivers: cracen: adding support for ed25519 without sicrypto #19812

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

degjorva
Copy link
Contributor

@degjorva degjorva commented Jan 9, 2025

Added implementation in cracenpsa of ed25519 and ed25519ph without using sicrypto
Removed the possibility of using the sicrypto implementation of ed25519 and ed25519ph through cracenpsa
Updated cracenpsa to support new implementation and
remove references to old

@degjorva degjorva added the DNM label Jan 9, 2025
@degjorva degjorva requested review from a team as code owners January 9, 2025 09:02
@github-actions github-actions bot added the changelog-entry-required Update changelog before merge. Remove label if entry is not needed or already added. label Jan 9, 2025
@degjorva degjorva removed the changelog-entry-required Update changelog before merge. Remove label if entry is not needed or already added. label Jan 9, 2025
@NordicBuilder
Copy link
Contributor

NordicBuilder commented Jan 9, 2025

CI Information

To view the history of this post, clich the 'edited' button above
Build number: 29

Inputs:

Sources:

sdk-nrf: PR head: 872cb18c735280d55d48b93393200e56df0d1a19

more details

sdk-nrf:

PR head: 872cb18c735280d55d48b93393200e56df0d1a19
merge base: d4304c5292dcbc80eaa767451763560a8eb95cec
target head (main): e24b7684210b99007a518e261b4bedae025ebf4a
Diff

Github labels

Enabled Name Description
ci-disabled Disable the ci execution
ci-all-test Run all of ci, no test spec filtering will be done
ci-force-downstream Force execution of downstream even if twister fails
ci-run-twister Force run twister
ci-run-zephyr-twister Force run zephyr twister
List of changed files detected by CI (6)
subsys
│  ├── nrf_security
│  │  ├── src
│  │  │  ├── drivers
│  │  │  │  ├── cracen
│  │  │  │  │  ├── cracenpsa
│  │  │  │  │  │  ├── cracenpsa.cmake
│  │  │  │  │  │  ├── include
│  │  │  │  │  │  │  │ cracen_psa.h
│  │  │  │  │  │  ├── src
│  │  │  │  │  │  │  ├── ed25519.c
│  │  │  │  │  │  │  ├── key_management.c
│  │  │  │  │  │  │  │ sign.c
│  │  │  │  │  │ psa_driver.Kconfig

Outputs:

Toolchain

Version:
Build docker image:

Test Spec & Results: ✅ Success; ❌ Failure; 🟠 Queued; 🟡 Progress; ◻️ Skipped; ⚠️ Quarantine

  • ◻️ Toolchain
  • ◻️ Build twister
  • ◻️ Integration tests
    • ◻️ test-fw-nrfconnect-chip
    • ◻️ test-fw-nrfconnect-nrf_crypto
    • ◻️ test-fw-nrfconnect-tfm
    • ◻️ test-sdk-find-my
    • ◻️ test-sdk-sidewalk
    • ◻️ test-sdk-dfu
    • ⚠️ test-fw-nrfconnect-nrf-iot_cloud
Disabled integration tests
    • desktop52_verification
    • doc-internal
    • test_ble_nrf_config
    • test-fw-nrfconnect-apps
    • test-fw-nrfconnect-ble_mesh
    • test-fw-nrfconnect-ble_samples
    • test-fw-nrfconnect-boot
    • test-fw-nrfconnect-fem
    • test-fw-nrfconnect-nfc
    • test-fw-nrfconnect-nrf-iot_libmodem-nrf
    • test-fw-nrfconnect-nrf-iot_lwm2m
    • test-fw-nrfconnect-nrf-iot_mosh
    • test-fw-nrfconnect-nrf-iot_positioning
    • test-fw-nrfconnect-nrf-iot_samples
    • test-fw-nrfconnect-nrf-iot_serial_lte_modem
    • test-fw-nrfconnect-nrf-iot_thingy91
    • test-fw-nrfconnect-nrf-iot_zephyr_lwm2m
    • test-fw-nrfconnect-ps
    • test-fw-nrfconnect-rpc
    • test-fw-nrfconnect-rs
    • test-fw-nrfconnect-thread
    • test-fw-nrfconnect-zigbee
    • test-low-level
    • test-sdk-audio
    • test-sdk-mcuboot
    • test-sdk-pmic-samples
    • test-sdk-wifi
    • test-secdom-samples-public

Note: This message is automatically posted and updated by the CI

@NordicBuilder
Copy link
Contributor

You can find the documentation preview for this PR at this link. It will be updated about 10 minutes after the documentation build succeeds.

Note: This comment is automatically posted by the Documentation Publish GitHub Action.

@degjorva degjorva force-pushed the no-sicrypto-ED25519 branch from 0c0a5d5 to 933f5a8 Compare January 9, 2025 13:36
@github-actions github-actions bot added the changelog-entry-required Update changelog before merge. Remove label if entry is not needed or already added. label Jan 9, 2025
@degjorva degjorva force-pushed the no-sicrypto-ED25519 branch 3 times, most recently from 21feb07 to e54e7e5 Compare January 13, 2025 14:12
Comment on lines 365 to 371
const uint8_t *message);

int ed25519_verify(const uint8_t *pubkey, const char *message,
size_t message_length, const char *signature);

int create_ed25519_pubkey(const uint8_t *ed25519,
uint8_t *pubkey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the secondary lines of these seem indented to odd lengths, plus the line length is 100 so why are these being chopped into 2 lines even when combined they don't reach 100 characters?

Comment on lines 15 to 17
char workmem[160];
struct sxhash hashopctx;
char pointr_buffer[64];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use defines for lengths

Comment on lines 47 to 63
r = sx_hash_feed(&hashopctx, workmem+32, 32);
if (r != 0) {
return r;
}
r = sx_hash_feed(&hashopctx, message, 100);
if (r != 0) {
return r;
}
r = sx_hash_digest(&hashopctx, workmem+96);
if (r != 0) {
return r;
}

/* Perform point multiplication R = [r]B. This is the encoded point R,
* which is the first part of the signature.
*/
r = sx_ed25519_ptmult((const struct sx_ed25519_dgst *)(workmem + 96),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic numbers everywhere, get rid of them

if (r != 0) {
return r;
}
r = sx_hash_feed(&hashopctx, workmem+32, 32);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x + x with spaces like below

Comment on lines 30 to 31
#if CONFIG_PSA_NEED_NO_SI_CRYPTO_ED25519
#else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first this is a define, secondly, if !defined(x) ?

@@ -605,8 +608,10 @@ static psa_status_t export_ecc_public_key_from_keypair(const psa_key_attributes_
int si_status = 0;
psa_algorithm_t key_alg = psa_get_key_algorithm(attributes);
const struct sx_pk_ecurve *sx_curve;
#if CONFIG_PSA_NEED_NO_SI_CRYPTO_ED25519
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above


#if CONFIG_PSA_NEED_NO_SI_CRYPTO_ED25519
si_status = create_ed25519_pubkey(key_buffer, data);
*data_length = 32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

@degjorva degjorva force-pushed the no-sicrypto-ED25519 branch 13 times, most recently from 718033c to 66dcd8a Compare January 17, 2025 09:37
@degjorva degjorva force-pushed the no-sicrypto-ED25519 branch 2 times, most recently from 758768d to 0ace2dc Compare January 21, 2025 13:57
@degjorva degjorva force-pushed the no-sicrypto-ED25519 branch from 0ace2dc to a434144 Compare January 21, 2025 14:00
@degjorva degjorva requested a review from nordicjm January 21, 2025 14:02
@degjorva degjorva force-pushed the no-sicrypto-ED25519 branch 4 times, most recently from 12ff6c7 to 5b26d57 Compare January 22, 2025 15:00
Add support for Ed25519 and Ed25519ph in cracenpsa
directly using silexpk/sxsymcrypt.
This bypasses sicrypto, which saves on flash usage
Remove sicrypto implementation of Ed25519 from
being accessible from cracenpsa.

Signed-off-by: Dag Erik Gjørvad <[email protected]>
@degjorva degjorva force-pushed the no-sicrypto-ED25519 branch from 5b26d57 to 872cb18 Compare January 23, 2025 11:05
@degjorva degjorva removed the DNM label Jan 23, 2025
Comment on lines +365 to +375
const uint8_t *message, size_t message_length);

int ed25519_verify(const uint8_t *pubkey, const char *message,
size_t message_length, const char *signature);


int ed25519ph_sign(const uint8_t *privkey, char *signature,
const uint8_t *message, size_t message_length, int ismessage);

int ed25519ph_verify(const uint8_t *pubkey, const char *message,
size_t message_length, const char *signature, int ismessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indents are off, use spaces after tabs to align

if (status != 0) {
return status;
}
status = sx_hash_digest(&hashopctx, workmem + 3 * SX_ED25519_SZ);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's special about 3?

int ed25519_sign_internal(const uint8_t *priv_key, char *signature, const uint8_t *message,
size_t message_length, int prehash)
{
char workmem[5 * SX_ED25519_SZ];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's special about 5?

char *pointr = pointr_buffer;
int status;

/*Hash the private key, the digest is stored in the first 64 bytes of workmem*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/*Hash the private key, the digest is stored in the first 64 bytes of workmem*/
/* Hash the private key, the digest is stored in the first 64 bytes of workmem */

/* Perform point multiplication R = [r]B. This is the encoded point R,
* which is the first part of the signature.
*/
status = sx_ed25519_ptmult((const struct sx_ed25519_dgst *)(workmem + 3 * SX_ED25519_SZ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

}

int ed25519ph_sign(const uint8_t *priv_key, char *signature,
const uint8_t *message, size_t message_length, int ismessage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

align off

return status;
}
return ed25519_sign_internal(priv_key, signature,
hashedmessage, SX_ED25519_DGST_SZ, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

align off

}

int ed25519_verify_internal(const uint8_t *pubkey, const char *message,
size_t message_length, const char *signature, int prehash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at this point might as well just throw it through clang-format

}
(void)t;
si_status = ed25519_create_pubkey(key_buffer, data);
*data_length = 32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's special about 32?

Comment on lines +273 to +278
*signature_length = 64;
return silex_statuscodes_to_psa(si_status);

} else if (alg == PSA_ALG_PURE_EDDSA) {
si_status = ed25519_sign(key_buffer, signature, input, input_length);
*signature_length = 64;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

@@ -61,6 +62,7 @@ endif()

if(CONFIG_PSA_NEED_CRACEN_KEY_MANAGEMENT_DRIVER OR CONFIG_PSA_NEED_CRACEN_KMU_DRIVER OR CONFIG_MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
list(APPEND cracen_driver_sources
${CMAKE_CURRENT_LIST_DIR}/src/ed25519.c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this added here also? Is it really needed in all the OR cases?

Comment on lines +364 to +377
int ed25519_sign(const uint8_t *privkey, char *signature,
const uint8_t *message, size_t message_length);

int ed25519_verify(const uint8_t *pubkey, const char *message,
size_t message_length, const char *signature);


int ed25519ph_sign(const uint8_t *privkey, char *signature,
const uint8_t *message, size_t message_length, int ismessage);

int ed25519ph_verify(const uint8_t *pubkey, const char *message,
size_t message_length, const char *signature, int ismessage);

int ed25519_create_pubkey(const uint8_t *privkey, uint8_t *pubkey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefix all those functions with cracen_ for namespacing and consistency.

@@ -0,0 +1,389 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright (c) 2024 Nordic Semiconductor ASA
* Copyright (c) 2025 Nordic Semiconductor ASA

Comment on lines +365 to +375
const uint8_t *message, size_t message_length);

int ed25519_verify(const uint8_t *pubkey, const char *message,
size_t message_length, const char *signature);


int ed25519ph_sign(const uint8_t *privkey, char *signature,
const uint8_t *message, size_t message_length, int ismessage);

int ed25519ph_verify(const uint8_t *pubkey, const char *message,
size_t message_length, const char *signature, int ismessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the indentation of the second lines is off for all the functions.

status = sx_hash_wait(&hashopctx);

return status;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment on lines +239 to +241

}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

int ed25519_sign(const uint8_t *priv_key, char *signature,
const uint8_t *message, size_t message_length)
{
return ed25519_sign_internal(priv_key, signature, message, message_length, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return ed25519_sign_internal(priv_key, signature, message, message_length, 0);
return ed25519_sign_internal(priv_key, signature, message, message_length, false);

Same further down, booleans to say yes/no rather than integers.

}

int ed25519ph_sign(const uint8_t *priv_key, char *signature,
const uint8_t *message, size_t message_length, int ismessage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const uint8_t *message, size_t message_length, int ismessage)
const uint8_t *message, size_t message_length, bool ismessage)

Comment on lines +274 to +303
status = sx_hash_create(&hashopctx, &sxhashalg_sha2_512, sizeof(hashopctx));
if (status != 0) {
return status;
}
if (prehash) {
status = sx_hash_feed(&hashopctx, dom2, sizeof(dom2));
if (status != 0) {
return status;
}
}
status = sx_hash_feed(&hashopctx, signature, SX_ED25519_SZ);
if (status != 0) {
return status;
}
status = sx_hash_feed(&hashopctx, pubkey, SX_ED25519_SZ);
if (status != 0) {
return status;
}
status = sx_hash_feed(&hashopctx, message, message_length);
if (status != 0) {
return status;
}
status = sx_hash_digest(&hashopctx, workmem);
if (status != 0) {
return status;
}
status = sx_hash_wait(&hashopctx);
if (status != 0) {
return status;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this could also use some generic function to do the hashing.

return ed25519_verify_internal(pubkey, message, message_length, signature, 1);
}

int ed25519_create_pubkey(const uint8_t *priv_key, uint8_t *pubkey)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some earlier comments apply to this function as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-entry-required Update changelog before merge. Remove label if entry is not needed or already added.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants