This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit be2e72dac22764e7617ecee00f237c6bd3ae50bc Author: makejian <[email protected]> AuthorDate: Thu Aug 7 15:21:12 2025 +0800 crypto/ecc: supports exporting generated keys in uncompressed form Export public keys as separate X and Y coordinates for uncompressed format. Signed-off-by: makejian <[email protected]> --- crypto/ecc.c | 42 ++++++++++++++++++++++++++++++++++++++++++ include/crypto/ecc.h | 4 ++++ 2 files changed, 46 insertions(+) diff --git a/crypto/ecc.c b/crypto/ecc.c index 0a94621aa6f..f1a924b7d71 100644 --- a/crypto/ecc.c +++ b/crypto/ecc.c @@ -1541,6 +1541,48 @@ int ecc_make_key(uint8_t publickey[ECC_BYTES + 1], return 1; } +int ecc_make_key_uncomp(uint8_t publickey_x[ECC_BYTES], + uint8_t publickey_y[ECC_BYTES], + uint8_t privatekey[ECC_BYTES]) +{ + uint64_t l_private[NUM_ECC_DIGITS]; + eccpoint_t l_public; + unsigned l_tries = 0; + + do + { + if (l_tries++ >= MAX_TRIES) + { + return 0; + } + + arc4random_buf(l_private, NUM_ECC_DIGITS); + + if (vli_iszero(l_private)) + { + continue; + } + + /* Make sure the private key is in the range [1, n-1]. + * For the supported curves, n is always large enough that we only + * need to subtract once at most. + */ + + if (vli_cmp(g_curve_n, l_private) != 1) + { + vli_sub(l_private, l_private, g_curve_n); + } + + eccpoint_mult(&l_public, &g_curve_g, l_private, NULL); + } + while (eccpoint_iszero(&l_public)); + + ecc_native2bytes(privatekey, l_private); + ecc_native2bytes(publickey_x, l_public.x); + ecc_native2bytes(publickey_y, l_public.y); + return 1; +} + int ecdh_shared_secret(const uint8_t publickey[ECC_BYTES + 1], const uint8_t privatekey[ECC_BYTES], uint8_t secret[ECC_BYTES]) diff --git a/include/crypto/ecc.h b/include/crypto/ecc.h index eb3898eaabd..3c130e4b9dc 100644 --- a/include/crypto/ecc.h +++ b/include/crypto/ecc.h @@ -76,6 +76,10 @@ extern "C" int ecc_make_key(uint8_t publickey[ECC_BYTES + 1], uint8_t privatekey[ECC_BYTES]); +int ecc_make_key_uncomp(uint8_t publickey_x[ECC_BYTES], + uint8_t publickey_y[ECC_BYTES], + uint8_t privatekey[ECC_BYTES]); + /* ecdh_shared_secret() function. * Compute a shared secret given your secret key and someone else's * public key.
