mirror of
https://github.com/kmackay/micro-ecc.git
synced 2026-10-08 08:47:34 +00:00
Updated API and made everything c90 compliant. Removed unnecessary code.
This commit is contained in:
@@ -3,15 +3,24 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* Optimization settings. Define as 1 to enable an optimization, 0 to disable it.
|
||||||
|
ECC_SQUARE_FUNC - If enabled, this will cause a specific function to be used for (scalar) squaring instead of the generic
|
||||||
|
multiplication function. Improves speed by about 1-4% (or more if 32-bit multiplications are slow).
|
||||||
|
ECC_USE_NAF - If enabled, this will convert the private key to a non-adjacent form before point multiplication.
|
||||||
|
Improves speed by about 10%.
|
||||||
|
*/
|
||||||
|
#define ECC_SQUARE_FUNC 1
|
||||||
|
#define ECC_USE_NAF 1
|
||||||
|
|
||||||
|
#define ECC_CURVE secp160r1
|
||||||
|
|
||||||
#define secp128r1 4
|
#define secp128r1 4
|
||||||
#define secp160r1 5
|
#define secp160r1 5
|
||||||
#define secp192r1 6
|
#define secp192r1 6
|
||||||
#define secp224r1 7
|
#define secp224r1 7
|
||||||
#define secp256r1 8
|
#define secp256r1 8
|
||||||
|
|
||||||
#define ECC_CURVE secp160r1
|
#if (ECC_CURVE != secp128r1 && ECC_CURVE != secp160r1 && ECC_CURVE != secp192r1 && ECC_CURVE != secp224r1 && ECC_CURVE != secp256r1)
|
||||||
|
|
||||||
#if !(ECC_CURVE)
|
|
||||||
#error "Must define ECC_CURVE to one of the available curves"
|
#error "Must define ECC_CURVE to one of the available curves"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -23,12 +32,7 @@ typedef struct EccPoint
|
|||||||
uint32_t y[NUM_ECC_DIGITS];
|
uint32_t y[NUM_ECC_DIGITS];
|
||||||
} EccPoint;
|
} EccPoint;
|
||||||
|
|
||||||
typedef struct Curve
|
int ecdh_shared_secret(uint32_t p_secret[NUM_ECC_DIGITS], EccPoint *p_publicKey, uint32_t p_privateKey[NUM_ECC_DIGITS]);
|
||||||
{
|
int ecdh_make_key(EccPoint *p_publicKey, uint32_t p_privateKey[NUM_ECC_DIGITS], uint32_t p_random[NUM_ECC_DIGITS]);
|
||||||
uint32_t p[NUM_ECC_DIGITS];
|
|
||||||
/* the other curve parameters are not necessary to compute the shared secret. */
|
|
||||||
} Curve;
|
|
||||||
|
|
||||||
int ecdh_shared_secret(uint8_t *p_secret, unsigned int p_len, EccPoint *p_publicKey, uint32_t *p_privateKey);
|
|
||||||
|
|
||||||
#endif /* _MICRO_ECDH_H_ */
|
#endif /* _MICRO_ECDH_H_ */
|
||||||
|
|||||||
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
c, link = emk.module("c", "link")
|
c, link = emk.module("c", "link")
|
||||||
|
|
||||||
default_flags = ["-fno-common", "-Wall", "-Wextra"]
|
default_flags = ["-fno-common", "-Wall", "-Wextra", "-Werror", "-Wno-unused", "-ansi", "-pedantic"]
|
||||||
opt_flags = {"prf":["-pg", "-O2"], "dbg":["-g"], "std":["-O1", "-DNDEBUG"], "opt":["-O2", "-DNDEBUG"], "max":["-O3", "-DNDEBUG"]}
|
opt_flags = {"dbg":["-g"], "std":["-O1", "-DNDEBUG"], "opt":["-O2", "-DNDEBUG"], "max":["-O3", "-DNDEBUG"]}
|
||||||
|
|
||||||
opt_level = "dbg"
|
opt_level = "dbg"
|
||||||
if "opt" in emk.options:
|
if "opt" in emk.options:
|
||||||
|
|||||||
-101
@@ -1,101 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
// int params[] = {
|
|
||||||
// 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
// 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
|
||||||
// unsigned size = 128;
|
|
||||||
|
|
||||||
int params[] = {
|
|
||||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
|
||||||
unsigned size = 160;
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
int matrix[size][size];
|
|
||||||
|
|
||||||
unsigned i, j;
|
|
||||||
for(i=0; i<size; ++i)
|
|
||||||
{
|
|
||||||
for(j=0; j<size; ++j)
|
|
||||||
{
|
|
||||||
if(i == 0)
|
|
||||||
{
|
|
||||||
matrix[i][j] = params[j];
|
|
||||||
}
|
|
||||||
else if(j == 0)
|
|
||||||
{
|
|
||||||
matrix[i][j] = params[j] * matrix[i-1][size-1];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
matrix[i][j] = matrix[i-1][j-1] + params[j] * matrix[i-1][size-1];
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%d ", matrix[i][j]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
int max = 0;
|
|
||||||
for(j=0; j<size; ++j)
|
|
||||||
{
|
|
||||||
int sum = 0;
|
|
||||||
for(i=0; i<size; ++i)
|
|
||||||
{
|
|
||||||
sum += matrix[i][j];
|
|
||||||
}
|
|
||||||
if(sum > max)
|
|
||||||
{
|
|
||||||
max = sum;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("w = %d\n", max);
|
|
||||||
|
|
||||||
int af[max][size];
|
|
||||||
|
|
||||||
int k;
|
|
||||||
for(k=0; k<max; ++k)
|
|
||||||
{
|
|
||||||
for(j=0; j<size; ++j)
|
|
||||||
{
|
|
||||||
i = 0;
|
|
||||||
while(i < size && matrix[i][j] == 0)
|
|
||||||
{
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
if(i < size)
|
|
||||||
{
|
|
||||||
af[k][j] = size + i;
|
|
||||||
--matrix[i][j];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
af[k][j] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%d-%d ", af[k][j] / 32, af[k][j] % 32);
|
|
||||||
if(j % 32 == 31)
|
|
||||||
{
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
+15
-25
@@ -14,43 +14,33 @@ void vli_print(uint32_t *p_vli, unsigned int p_size)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void EccPoint_mult(EccPoint *p_result, EccPoint *p_point, uint32_t *p_scalar);
|
/* Test data from http://www.secg.org/collateral/gec2.pdf page 44
|
||||||
extern Curve curve;
|
Requires that ECC_CURVE be set to secp160r1. */
|
||||||
extern EccPoint curve_G;
|
|
||||||
|
|
||||||
// Test data from http://www.secg.org/collateral/gec2.pdf page 44
|
int main()
|
||||||
// Requires that ECC_CURVE be set to secp160r1
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
{
|
||||||
uint32_t l_expectedSecret[NUM_ECC_DIGITS] = {0x347BB40A, 0x8E6AF594, 0x6E1B74AC, 0x3FFA87A9, 0xCA7C0F8C};
|
uint32_t l_expectedSecret[NUM_ECC_DIGITS] = {0x347BB40A, 0x8E6AF594, 0x6E1B74AC, 0x3FFA87A9, 0xCA7C0F8C};
|
||||||
uint32_t l_privateKey1[NUM_ECC_DIGITS] = {0xB2A4E982, 0x72CB6D57, 0xB0733079, 0x3CE144E6, 0xAA374FFC};
|
uint32_t l_privateKey1[NUM_ECC_DIGITS] = {0xB2A4E982, 0x72CB6D57, 0xB0733079, 0x3CE144E6, 0xAA374FFC};
|
||||||
uint32_t l_privateKey2[NUM_ECC_DIGITS] = {0x2B460866, 0xE74F277E, 0x15101C66, 0x2A17AD4B, 0x45FB58A9};
|
uint32_t l_privateKey2[NUM_ECC_DIGITS] = {0x2B460866, 0xE74F277E, 0x15101C66, 0x2A17AD4B, 0x45FB58A9};
|
||||||
EccPoint l_publicKey1, l_publicKey2;
|
EccPoint l_publicKey1 = {
|
||||||
EccPoint_mult(&l_publicKey1, &curve_G, l_privateKey1);
|
{0x51419DC0, 0x3C032062, 0x0E75A24A, 0xECC406ED, 0x51B4496F},
|
||||||
EccPoint_mult(&l_publicKey2, &curve_G, l_privateKey2);
|
{0x1756AA6C, 0x4F381CCC, 0x68D79389, 0x73A514B4, 0xC28DCB4B}
|
||||||
|
};
|
||||||
printf("Public key 1:\n");
|
EccPoint l_publicKey2 = {
|
||||||
vli_print(l_publicKey1.x, NUM_ECC_DIGITS);
|
{0x07C6E5BC, 0x0F63D567, 0x328739D9, 0x9C0369C2, 0x49B41E0E},
|
||||||
printf("\n");
|
{0x0E9C8F83, 0x111C3EDC, 0x6D232A03, 0x67015ED9, 0x26E008B5}
|
||||||
vli_print(l_publicKey1.y, NUM_ECC_DIGITS);
|
};
|
||||||
printf("\n\n");
|
|
||||||
|
|
||||||
printf("Public key 2:\n");
|
|
||||||
vli_print(l_publicKey2.x, NUM_ECC_DIGITS);
|
|
||||||
printf("\n");
|
|
||||||
vli_print(l_publicKey2.y, NUM_ECC_DIGITS);
|
|
||||||
printf("\n\n");
|
|
||||||
|
|
||||||
uint32_t l_shared1[NUM_ECC_DIGITS];
|
uint32_t l_shared1[NUM_ECC_DIGITS];
|
||||||
if(!ecdh_shared_secret((uint8_t *)l_shared1, sizeof(l_shared1), &l_publicKey1, l_privateKey2))
|
uint32_t l_shared2[NUM_ECC_DIGITS];
|
||||||
|
|
||||||
|
if(!ecdh_shared_secret(l_shared1, &l_publicKey1, l_privateKey2))
|
||||||
{
|
{
|
||||||
printf("shared_secret() failed (1)\n");
|
printf("shared_secret() failed (1)\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t l_shared2[NUM_ECC_DIGITS];
|
if(!ecdh_shared_secret(l_shared2, &l_publicKey2, l_privateKey1))
|
||||||
if(!ecdh_shared_secret((uint8_t *)l_shared2, sizeof(l_shared2), &l_publicKey2, l_privateKey1))
|
|
||||||
{
|
{
|
||||||
printf("shared_secret() failed (2)\n");
|
printf("shared_secret() failed (2)\n");
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
+14
-15
@@ -5,8 +5,6 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
extern EccPoint curve_G;
|
|
||||||
|
|
||||||
extern void EccPoint_mult(EccPoint *p_result, EccPoint *p_point, uint32_t *p_scalar);
|
extern void EccPoint_mult(EccPoint *p_result, EccPoint *p_point, uint32_t *p_scalar);
|
||||||
|
|
||||||
void vli_print(uint32_t *p_vli, unsigned int p_size)
|
void vli_print(uint32_t *p_vli, unsigned int p_size)
|
||||||
@@ -30,6 +28,15 @@ void getRandomBytes(void *p_dest, unsigned p_size)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
EccPoint l_Q1, l_Q2; /* public keys */
|
||||||
|
uint32_t l_secret1[NUM_ECC_DIGITS];
|
||||||
|
uint32_t l_secret2[NUM_ECC_DIGITS];
|
||||||
|
uint64_t l_total;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
uint32_t l_shared1[NUM_ECC_DIGITS];
|
||||||
|
uint32_t l_shared2[NUM_ECC_DIGITS];
|
||||||
|
|
||||||
randfd = open("/dev/urandom", O_RDONLY);
|
randfd = open("/dev/urandom", O_RDONLY);
|
||||||
if(randfd == -1)
|
if(randfd == -1)
|
||||||
{
|
{
|
||||||
@@ -37,15 +44,9 @@ int main()
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
EccPoint l_Q1, l_Q2; // public keys
|
|
||||||
uint32_t l_secret1[NUM_ECC_DIGITS];
|
|
||||||
uint32_t l_secret2[NUM_ECC_DIGITS];
|
|
||||||
printf("Testing 256 random private key pairs\n");
|
printf("Testing 256 random private key pairs\n");
|
||||||
|
|
||||||
uint64_t l_total = 0;
|
l_total = 0;
|
||||||
|
|
||||||
int i;
|
|
||||||
for(i=0; i<256; ++i)
|
for(i=0; i<256; ++i)
|
||||||
{
|
{
|
||||||
printf(".");
|
printf(".");
|
||||||
@@ -53,18 +54,16 @@ int main()
|
|||||||
getRandomBytes((char *)l_secret1, NUM_ECC_DIGITS * sizeof(uint32_t));
|
getRandomBytes((char *)l_secret1, NUM_ECC_DIGITS * sizeof(uint32_t));
|
||||||
getRandomBytes((char *)l_secret2, NUM_ECC_DIGITS * sizeof(uint32_t));
|
getRandomBytes((char *)l_secret2, NUM_ECC_DIGITS * sizeof(uint32_t));
|
||||||
|
|
||||||
EccPoint_mult(&l_Q1, &curve_G, l_secret1);
|
ecdh_make_key(&l_Q1, l_secret1, l_secret1);
|
||||||
EccPoint_mult(&l_Q2, &curve_G, l_secret2);
|
ecdh_make_key(&l_Q2, l_secret2, l_secret2);
|
||||||
|
|
||||||
uint32_t l_shared1[NUM_ECC_DIGITS];
|
if(!ecdh_shared_secret(l_shared1, &l_Q1, l_secret2))
|
||||||
if(!ecdh_shared_secret((uint8_t *)l_shared1, sizeof(l_shared1), &l_Q1, l_secret2))
|
|
||||||
{
|
{
|
||||||
printf("shared_secret() failed (1)\n");
|
printf("shared_secret() failed (1)\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t l_shared2[NUM_ECC_DIGITS];
|
if(!ecdh_shared_secret(l_shared2, &l_Q2, l_secret1))
|
||||||
if(!ecdh_shared_secret((uint8_t *)l_shared2, sizeof(l_shared2), &l_Q2, l_secret1))
|
|
||||||
{
|
{
|
||||||
printf("shared_secret() failed (2)\n");
|
printf("shared_secret() failed (2)\n");
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user