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 <makejian@xiaomi.com>
This commit is contained in:
makejian
2025-08-07 15:21:12 +08:00
committed by Xiang Xiao
parent 3f0cc5f09c
commit be2e72dac2
2 changed files with 46 additions and 0 deletions
+42
View File
@@ -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])
+4
View File
@@ -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.