mirror of
https://github.com/kmackay/micro-ecc.git
synced 2026-08-02 00:27:47 +00:00
Added uECC_compute_public_key.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
|
||||
|
||||
#include "uECC.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
void vli_print(uint8_t *p_vli, unsigned int p_size)
|
||||
{
|
||||
while(p_size)
|
||||
{
|
||||
printf("%02X ", (unsigned)p_vli[p_size - 1]);
|
||||
--p_size;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
|
||||
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)
|
||||
{
|
||||
printf(".");
|
||||
fflush(stdout);
|
||||
|
||||
int success = uECC_make_key(l_public, l_private);
|
||||
if (!success) {
|
||||
printf("uECC_make_key() failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
uECC_compute_public_key(l_private, l_public_computed);
|
||||
|
||||
if(memcmp(l_public, l_public_computed, sizeof(l_public)) != 0)
|
||||
{
|
||||
printf("Computed and provided public keys are not identical!\n");
|
||||
printf("Computed public key = ");
|
||||
vli_print(l_public_computed, uECC_BYTES);
|
||||
printf("\n");
|
||||
printf("Provided public key = ");
|
||||
vli_print(l_public, uECC_BYTES);
|
||||
printf("\n");
|
||||
printf("Private key = ");
|
||||
vli_print(l_private, uECC_BYTES);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -2380,6 +2380,18 @@ 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;
|
||||
uECC_word_t l_private[uECC_WORDS];
|
||||
|
||||
vli_bytesToNative(l_private, p_privateKey);
|
||||
|
||||
EccPoint_mult(&l_public, &curve_G, l_private, 0, vli_numBits(l_private, uECC_WORDS));
|
||||
|
||||
vli_nativeToBytes(p_publicKey, l_public.x);
|
||||
vli_nativeToBytes(p_publicKey + uECC_BYTES, l_public.y);
|
||||
}
|
||||
|
||||
int uECC_bytes(void)
|
||||
{
|
||||
return uECC_BYTES;
|
||||
|
||||
@@ -178,6 +178,18 @@ Returns 1 if the public key is valid, 0 if it is invalid.
|
||||
*/
|
||||
int uECC_valid_public_key(const uint8_t p_publicKey[uECC_BYTES*2]);
|
||||
|
||||
/* uECC_compute_public_key() function.
|
||||
Compute the corresponding public key for a private key.
|
||||
|
||||
Inputs:
|
||||
p_privateKey - The private key to compute the public key for
|
||||
|
||||
Outputs:
|
||||
p_publicKey - Will be filled in with the corresponding public key
|
||||
*/
|
||||
void uECC_compute_public_key(const uint8_t p_privateKey[uECC_BYTES], uint8_t p_publicKey[uECC_BYTES * 2]);
|
||||
|
||||
|
||||
/* uECC_bytes() function.
|
||||
Returns the value of uECC_BYTES. Helpful for foreign-interfaces to higher-level languages.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user