mirror of
https://github.com/kmackay/micro-ecc.git
synced 2026-07-26 13:29:05 +00:00
Added error checking to uECC_compute_public_key.
This commit is contained in:
+18
-5
@@ -17,12 +17,13 @@ void vli_print(uint8_t *p_vli, unsigned int p_size)
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
|
||||
int success;
|
||||
|
||||
uint8_t l_private[uECC_BYTES];
|
||||
|
||||
|
||||
uint8_t l_public[uECC_BYTES * 2];
|
||||
uint8_t l_public_computed[uECC_BYTES * 2];
|
||||
|
||||
|
||||
printf("Testing 256 random private key pairs\n");
|
||||
|
||||
for(i=0; i<256; ++i)
|
||||
@@ -36,7 +37,10 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
uECC_compute_public_key(l_private, l_public_computed);
|
||||
success = uECC_compute_public_key(l_private, l_public_computed);
|
||||
if (!success) {
|
||||
printf("uECC_compute_public_key() failed\n");
|
||||
}
|
||||
|
||||
if(memcmp(l_public, l_public_computed, sizeof(l_public)) != 0)
|
||||
{
|
||||
@@ -52,7 +56,16 @@ int main()
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
|
||||
printf("Testing private key = 0\n");
|
||||
|
||||
memset(l_private, 0, uECC_BYTES);
|
||||
success = uECC_compute_public_key(l_private, l_public_computed);
|
||||
if (success) {
|
||||
printf("uECC_compute_public_key() should have failed\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2380,16 +2380,36 @@ int uECC_verify(const uint8_t p_publicKey[uECC_BYTES*2], const uint8_t p_hash[uE
|
||||
return vli_equal(rx, r);
|
||||
}
|
||||
|
||||
void uECC_compute_public_key(const uint8_t p_privateKey[uECC_BYTES], uint8_t p_publicKey[uECC_BYTES * 2]) {
|
||||
EccPoint l_public;
|
||||
int uECC_compute_public_key(const uint8_t p_privateKey[uECC_BYTES], uint8_t p_publicKey[uECC_BYTES * 2]) {
|
||||
uECC_word_t l_private[uECC_WORDS];
|
||||
|
||||
vli_bytesToNative(l_private, p_privateKey);
|
||||
|
||||
/* Make sure the private key is in the range [1, n-1]. */
|
||||
if(vli_isZero(l_private))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if uECC_CURVE != uECC_secp160r1
|
||||
if(vli_cmp(curve_n, l_private) != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Compute the public point */
|
||||
EccPoint l_public;
|
||||
EccPoint_mult(&l_public, &curve_G, l_private, 0, vli_numBits(l_private, uECC_WORDS));
|
||||
|
||||
if (EccPoint_isZero(&l_public))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
vli_nativeToBytes(p_publicKey, l_public.x);
|
||||
vli_nativeToBytes(p_publicKey + uECC_BYTES, l_public.y);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int uECC_bytes(void)
|
||||
|
||||
@@ -186,8 +186,10 @@ Inputs:
|
||||
|
||||
Outputs:
|
||||
p_publicKey - Will be filled in with the corresponding public key
|
||||
|
||||
Returns 1 if the key was computed successfully, 0 if an error occurred.
|
||||
*/
|
||||
void uECC_compute_public_key(const uint8_t p_privateKey[uECC_BYTES], uint8_t p_publicKey[uECC_BYTES * 2]);
|
||||
int uECC_compute_public_key(const uint8_t p_privateKey[uECC_BYTES], uint8_t p_publicKey[uECC_BYTES * 2]);
|
||||
|
||||
|
||||
/* uECC_bytes() function.
|
||||
|
||||
Reference in New Issue
Block a user