Skip to content

Commit 24c0eb8

Browse files
committed
ECC curves y^2 = x^3 + ax + b
1 parent ea32b2b commit 24c0eb8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+6806
-1059
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ before_script:
2020
- curl http://ftp.de.debian.org/debian/pool/main/l/lcov/lcov_1.11.orig.tar.gz | tar xz
2121
- export PATH=$PATH:`pwd`/lcov-1.11/bin
2222
- curl -s https://packagecloud.io/install/repositories/libtom/packages/script.deb.sh | sudo bash
23-
- sudo apt-get install libtfm-dev=0.13-5
23+
- sudo apt-get install libtfm-dev=0.13-5 libtommath-dev=1.0-5
2424

2525
matrix:
2626
fast_finish: true

demos/timing.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -947,28 +947,28 @@ static void time_ecc(void)
947947
unsigned long i, w, x, y, z;
948948
int err, stat;
949949
static unsigned long sizes[] = {
950-
#ifdef LTC_ECC112
950+
#ifdef LTC_ECC_SECP112R1
951951
112/8,
952952
#endif
953-
#ifdef LTC_ECC128
953+
#ifdef LTC_ECC_SECP128R1
954954
128/8,
955955
#endif
956-
#ifdef LTC_ECC160
956+
#ifdef LTC_ECC_SECP160R1
957957
160/8,
958958
#endif
959-
#ifdef LTC_ECC192
959+
#ifdef LTC_ECC_SECP192R1
960960
192/8,
961961
#endif
962-
#ifdef LTC_ECC224
962+
#ifdef LTC_ECC_SECP224R1
963963
224/8,
964964
#endif
965-
#ifdef LTC_ECC256
965+
#ifdef LTC_ECC_SECP256R1
966966
256/8,
967967
#endif
968-
#ifdef LTC_ECC384
968+
#ifdef LTC_ECC_SECP384R1
969969
384/8,
970970
#endif
971-
#ifdef LTC_ECC521
971+
#ifdef LTC_ECC_SECP512R1
972972
521/8,
973973
#endif
974974
100000};

demos/tv_gen.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ void ecc_gen(void)
663663
{
664664
FILE *out;
665665
unsigned char str[512];
666-
void *k, *order, *modulus;
666+
void *k, *order, *modulus, *a;
667667
ecc_point *G, *R;
668668
int x;
669669

@@ -674,26 +674,28 @@ void ecc_gen(void)
674674
mp_init(&k);
675675
mp_init(&order);
676676
mp_init(&modulus);
677+
mp_init(&a);
677678

678-
for (x = 0; ltc_ecc_sets[x].size != 0; x++) {
679-
fprintf(out, "ECC-%d\n", ltc_ecc_sets[x].size*8);
679+
for (x = 0; ltc_ecc_curves[x].prime != NULL; x++) {
680+
fprintf(out, "%s\n", ltc_ecc_curves[x].OID);
680681
mp_set(k, 1);
681682

682-
mp_read_radix(order, (char *)ltc_ecc_sets[x].order, 16);
683-
mp_read_radix(modulus, (char *)ltc_ecc_sets[x].prime, 16);
684-
mp_read_radix(G->x, (char *)ltc_ecc_sets[x].Gx, 16);
685-
mp_read_radix(G->y, (char *)ltc_ecc_sets[x].Gy, 16);
683+
mp_read_radix(order, (char *)ltc_ecc_curves[x].order, 16);
684+
mp_read_radix(modulus, (char *)ltc_ecc_curves[x].prime, 16);
685+
mp_read_radix(a, (char *)ltc_ecc_curves[x].A, 16);
686+
mp_read_radix(G->x, (char *)ltc_ecc_curves[x].Gx, 16);
687+
mp_read_radix(G->y, (char *)ltc_ecc_curves[x].Gy, 16);
686688
mp_set(G->z, 1);
687689

688690
while (mp_cmp(k, order) == LTC_MP_LT) {
689-
ltc_mp.ecc_ptmul(k, G, R, modulus, 1);
691+
ltc_mp.ecc_ptmul(k, G, R, a, modulus, 1);
690692
mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
691693
mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
692694
mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
693695
mp_mul_d(k, 3, k);
694696
}
695697
}
696-
mp_clear_multi(k, order, modulus, NULL);
698+
mp_clear_multi(k, order, modulus, a, NULL);
697699
ltc_ecc_del_point(G);
698700
ltc_ecc_del_point(R);
699701
fclose(out);

notes/ecc_tv.txt

Lines changed: 4095 additions & 8 deletions
Large diffs are not rendered by default.

src/headers/tomcrypt_custom.h

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,40 @@
503503
#ifdef LTC_MECC
504504
/* Supported ECC Key Sizes */
505505
#ifndef LTC_NO_CURVES
506-
#define LTC_ECC112
507-
#define LTC_ECC128
508-
#define LTC_ECC160
509-
#define LTC_ECC192
510-
#define LTC_ECC224
511-
#define LTC_ECC256
512-
#define LTC_ECC384
513-
#define LTC_ECC521
506+
#define LTC_ECC_BRAINPOOLP160R1
507+
#define LTC_ECC_BRAINPOOLP160T1
508+
#define LTC_ECC_BRAINPOOLP192R1
509+
#define LTC_ECC_BRAINPOOLP192T1
510+
#define LTC_ECC_BRAINPOOLP224R1
511+
#define LTC_ECC_BRAINPOOLP224T1
512+
#define LTC_ECC_BRAINPOOLP256R1
513+
#define LTC_ECC_BRAINPOOLP256T1
514+
#define LTC_ECC_BRAINPOOLP320R1
515+
#define LTC_ECC_BRAINPOOLP320T1
516+
#define LTC_ECC_BRAINPOOLP384R1
517+
#define LTC_ECC_BRAINPOOLP384T1
518+
#define LTC_ECC_BRAINPOOLP512R1
519+
#define LTC_ECC_BRAINPOOLP512T1
520+
#define LTC_ECC_PRIME192V2
521+
#define LTC_ECC_PRIME192V3
522+
#define LTC_ECC_PRIME239V1
523+
#define LTC_ECC_PRIME239V2
524+
#define LTC_ECC_PRIME239V3
525+
#define LTC_ECC_SECP112R1
526+
#define LTC_ECC_SECP112R2
527+
#define LTC_ECC_SECP128R1
528+
#define LTC_ECC_SECP128R2
529+
#define LTC_ECC_SECP160K1
530+
#define LTC_ECC_SECP160R1
531+
#define LTC_ECC_SECP160R2
532+
#define LTC_ECC_SECP192K1
533+
#define LTC_ECC_SECP192R1
534+
#define LTC_ECC_SECP224K1
535+
#define LTC_ECC_SECP224R1
536+
#define LTC_ECC_SECP256K1
537+
#define LTC_ECC_SECP256R1
538+
#define LTC_ECC_SECP384R1
539+
#define LTC_ECC_SECP521R1
514540
#endif
515541
#endif
516542

@@ -627,6 +653,40 @@
627653
#endif
628654
#endif
629655

656+
/* ECC backwards compatibility */
657+
#if !defined(LTC_ECC_SECP112R1) && defined(LTC_ECC112)
658+
#define LTC_ECC_SECP112R1
659+
#undef LTC_ECC112
660+
#endif
661+
#if !defined(LTC_ECC_SECP128R1) && defined(LTC_ECC128)
662+
#define LTC_ECC_SECP128R1
663+
#undef LTC_ECC128
664+
#endif
665+
#if !defined(LTC_ECC_SECP160R1) && defined(LTC_ECC160)
666+
#define LTC_ECC_SECP160R1
667+
#undef LTC_ECC160
668+
#endif
669+
#if !defined(LTC_ECC_SECP192R1) && defined(LTC_ECC192)
670+
#define LTC_ECC_SECP192R1
671+
#undef LTC_ECC192
672+
#endif
673+
#if !defined(LTC_ECC_SECP224R1) && defined(LTC_ECC224)
674+
#define LTC_ECC_SECP224R1
675+
#undef LTC_ECC224
676+
#endif
677+
#if !defined(LTC_ECC_SECP256R1) && defined(LTC_ECC256)
678+
#define LTC_ECC_SECP256R1
679+
#undef LTC_ECC256
680+
#endif
681+
#if !defined(LTC_ECC_SECP384R1) && defined(LTC_ECC384)
682+
#define LTC_ECC_SECP384R1
683+
#undef LTC_ECC384
684+
#endif
685+
#if !defined(LTC_ECC_SECP512R1) && defined(LTC_ECC521)
686+
#define LTC_ECC_SECP521R1
687+
#undef LTC_ECC521
688+
#endif
689+
630690
/* ref: $Format:%D$ */
631691
/* git commit: $Format:%H$ */
632692
/* commit time: $Format:%ai$ */

src/headers/tomcrypt_math.h

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ typedef struct {
246246
*/
247247
int (*sqr)(void *a, void *b);
248248

249+
/** Square root (mod prime)
250+
@param a The integer to compute square root mod prime from
251+
@param b The prime
252+
@param c The destination
253+
@return CRYPT_OK on success
254+
*/
255+
int (*sqrtmod_prime)(void *a, void *b, void *c);
256+
249257
/** Divide an integer
250258
@param a The dividend
251259
@param b The divisor
@@ -366,42 +374,48 @@ typedef struct {
366374
@param k The integer to multiply the point by
367375
@param G The point to multiply
368376
@param R The destination for kG
377+
@param a ECC curve parameter a
369378
@param modulus The modulus for the field
370379
@param map Boolean indicated whether to map back to affine or not
371380
(can be ignored if you work in affine only)
372381
@return CRYPT_OK on success
373382
*/
374383
int (*ecc_ptmul)( void *k,
375-
ecc_point *G,
376-
ecc_point *R,
377-
void *modulus,
378-
int map);
384+
const ecc_point *G,
385+
ecc_point *R,
386+
void *a,
387+
void *modulus,
388+
int map);
379389

380390
/** ECC GF(p) point addition
381391
@param P The first point
382392
@param Q The second point
383393
@param R The destination of P + Q
394+
@param ma The curve parameter "a" in montgomery form
384395
@param modulus The modulus
385396
@param mp The "b" value from montgomery_setup()
386397
@return CRYPT_OK on success
387398
*/
388-
int (*ecc_ptadd)(ecc_point *P,
389-
ecc_point *Q,
390-
ecc_point *R,
391-
void *modulus,
392-
void *mp);
399+
int (*ecc_ptadd)(const ecc_point *P,
400+
const ecc_point *Q,
401+
ecc_point *R,
402+
void *ma,
403+
void *modulus,
404+
void *mp);
393405

394406
/** ECC GF(p) point double
395407
@param P The first point
396408
@param R The destination of 2P
409+
@param ma The curve parameter "a" in montgomery form
397410
@param modulus The modulus
398411
@param mp The "b" value from montgomery_setup()
399412
@return CRYPT_OK on success
400413
*/
401-
int (*ecc_ptdbl)(ecc_point *P,
402-
ecc_point *R,
403-
void *modulus,
404-
void *mp);
414+
int (*ecc_ptdbl)(const ecc_point *P,
415+
ecc_point *R,
416+
void *ma,
417+
void *modulus,
418+
void *mp);
405419

406420
/** ECC mapping from projective to affine,
407421
currently uses (x,y,z) => (x/z^2, y/z^3, 1)
@@ -421,13 +435,15 @@ typedef struct {
421435
@param B Second point to multiply
422436
@param kB What to multiple B by
423437
@param C [out] Destination point (can overlap with A or B)
438+
@param ma The curve parameter "a" in montgomery form
424439
@param modulus Modulus for curve
425440
@return CRYPT_OK on success
426441
*/
427-
int (*ecc_mul2add)(ecc_point *A, void *kA,
428-
ecc_point *B, void *kB,
429-
ecc_point *C,
430-
void *modulus);
442+
int (*ecc_mul2add)(const ecc_point *A, void *kA,
443+
const ecc_point *B, void *kB,
444+
ecc_point *C,
445+
void *ma,
446+
void *modulus);
431447

432448
/* ---- (optional) rsa optimized math (for internal CRT) ---- */
433449

@@ -547,6 +563,7 @@ extern const ltc_math_descriptor gmp_desc;
547563
#define mp_mul(a, b, c) ltc_mp.mul(a, b, c)
548564
#define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
549565
#define mp_sqr(a, b) ltc_mp.sqr(a, b)
566+
#define mp_sqrtmod_prime(a, b, c) ltc_mp.sqrtmod_prime(a, b, c)
550567
#define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
551568
#define mp_div_2(a, b) ltc_mp.div_2(a, b)
552569
#define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)

0 commit comments

Comments
 (0)