Added code for 32-bit and 64-bit platforms. Added ECDSA implementation (not supported for secp160r1 yet).

This commit is contained in:
Ken MacKay
2014-03-25 21:44:04 -07:00
parent 6038323cdb
commit 938ea44f22
7 changed files with 4748 additions and 3740 deletions
+3598 -1
View File
File diff suppressed because it is too large Load Diff
-3520
View File
File diff suppressed because it is too large Load Diff
+1055 -217
View File
File diff suppressed because it is too large Load Diff
+33 -1
View File
@@ -32,7 +32,7 @@ ecc_asm_fast - Use GCC inline assembly optimized for maximum speed. */
#define secp192r1 2
#define secp256r1 3
#ifndef ECC_CURVE
#define ECC_CURVE secp256r1
#define ECC_CURVE secp160r1
#endif
/* Optimization settings. Define as 1 to enable an optimization, 0 to disable it.
@@ -130,6 +130,38 @@ Outputs:
*/
void ecc_decompress(uint8_t p_compressed[ECC_BYTES+1], uint8_t p_publicKey[ECC_BYTES*2]);
/* ecdsa_sign() function.
Generate an ECDSA signature for a given hash value.
Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and pass it in to
this function along with your private key.
Inputs:
p_privateKey - Your private key.
p_hash - The message hash to sign.
Outputs:
p_signature - Will be filled in with the signature value.
Returns 1 if the signature generated successfully, 0 if an error occurred.
*/
int ecdsa_sign(const uint8_t p_privateKey[ECC_BYTES], const uint8_t p_hash[ECC_BYTES], uint8_t p_signature[ECC_BYTES*2]);
/* ecdsa_verify() function.
Verify an ECDSA signature.
Usage: Compute the hash of the signed data using the same hash as the signer and
pass it to this function along with the signer's public key and the signature values (r and s).
Inputs:
p_publicKey - The signer's public key
p_hash - The hash of the signed data.
p_signature - The signature value.
Returns 1 if the signature is valid, 0 if it is invalid.
*/
int ecdsa_verify(const uint8_t p_publicKey[ECC_BYTES*2], const uint8_t p_hash[ECC_BYTES], const uint8_t p_signature[ECC_BYTES*2]);
#ifdef __cplusplus
} /* end of extern "C" */
#endif
+1 -1
View File
@@ -5,7 +5,7 @@ c, link = emk.module("c", "link")
default_compile_flags = ["-fvisibility=hidden", "-Wall", "-Wextra", "-Wshadow", "-Werror", "-Wno-missing-field-initializers", "-Wno-unused-parameter", \
"-Wno-comment", "-Wno-unused", "-Wno-unknown-pragmas"]
default_link_flags = []
opt_flags = {"dbg":[], "std":["-O2"], "max":["-O3"], "small":["-Os"]}
opt_flags = {"dbg":["-g"], "std":["-O2"], "max":["-O3"], "small":["-Os"]}
opt_link_flags = {"dbg":[], "std":[], "max":[], "small":[]}
c_flags = ["-std=c99"]
cxx_flags = ["-std=c++11", "-Wno-reorder", "-fno-rtti", "-fno-exceptions"]
View File
+61
View File
@@ -0,0 +1,61 @@
#include "ecc.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int randfd;
void getRandomBytes(void *p_dest, unsigned p_size)
{
if(read(randfd, p_dest, p_size) != (int)p_size)
{
printf("Failed to get random bytes.\n");
}
}
int main()
{
uint8_t l_public[ECC_BYTES*2];
uint8_t l_private[ECC_BYTES];
uint8_t l_hash[ECC_BYTES];
uint8_t l_sig[ECC_BYTES*2];
int i;
randfd = open("/dev/urandom", O_RDONLY);
if(randfd == -1)
{
printf("No access to urandom\n");
return -1;
}
printf("Testing 256 signatures\n");
for(i=0; i<256; ++i)
{
printf(".");
fflush(stdout);
ecc_make_key(l_public, l_private);
getRandomBytes((char *)l_hash, ECC_BYTES);
if(!ecdsa_sign(l_private, l_hash, l_sig))
{
printf("ecdsa_sign() failed\n");
continue;
}
if(!ecdsa_verify(l_public, l_hash, l_sig))
{
printf("ecdsa_verify() failed\n");
}
}
printf("\n");
return 0;
}