Skip to content

Commit 1e80a3f

Browse files
committed
ECC curves y^2 = x^3 + ax + b
1 parent 1725b87 commit 1e80a3f

Some content is hidden

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

43 files changed

+4278
-426
lines changed

demos/tv_gen.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ void ecc_gen(void)
637637
{
638638
FILE *out;
639639
unsigned char str[512];
640-
void *k, *order, *modulus;
640+
void *k, *order, *modulus, *a;
641641
ecc_point *G, *R;
642642
int x;
643643

@@ -648,26 +648,29 @@ void ecc_gen(void)
648648
mp_init(&k);
649649
mp_init(&order);
650650
mp_init(&modulus);
651+
mp_init(&a);
651652

652653
for (x = 0; ltc_ecc_sets[x].size != 0; x++) {
653-
fprintf(out, "ECC-%d\n", ltc_ecc_sets[x].size*8);
654+
655+
fprintf(out, "%s\n", ltc_ecc_sets[x].name);
654656
mp_set(k, 1);
655657

656658
mp_read_radix(order, (char *)ltc_ecc_sets[x].order, 16);
657659
mp_read_radix(modulus, (char *)ltc_ecc_sets[x].prime, 16);
660+
mp_read_radix(a, (char *)ltc_ecc_sets[x].A, 16);
658661
mp_read_radix(G->x, (char *)ltc_ecc_sets[x].Gx, 16);
659662
mp_read_radix(G->y, (char *)ltc_ecc_sets[x].Gy, 16);
660663
mp_set(G->z, 1);
661664

662665
while (mp_cmp(k, order) == LTC_MP_LT) {
663-
ltc_mp.ecc_ptmul(k, G, R, modulus, 1);
666+
ltc_mp.ecc_ptmul(k, G, R, a, modulus, 1);
664667
mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
665668
mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
666669
mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
667670
mp_mul_d(k, 3, k);
668671
}
669672
}
670-
mp_clear_multi(k, order, modulus, NULL);
673+
mp_clear_multi(k, order, modulus, a, NULL);
671674
ltc_ecc_del_point(G);
672675
ltc_ecc_del_point(R);
673676
fclose(out);

notes/ecc_tv.txt

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

src/headers/tomcrypt_custom.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,34 @@
487487
#ifdef LTC_MECC
488488
/* Supported ECC Key Sizes */
489489
#ifndef LTC_NO_CURVES
490+
#define LTC_ECC_SECP112R1
491+
#define LTC_ECC_SECP112R2
492+
#define LTC_ECC_SECP128R1
493+
#define LTC_ECC_SECP128R2
494+
#define LTC_ECC_SECP160R1
495+
#define LTC_ECC_SECP160R2
496+
#define LTC_ECC_SECP160K1
497+
#define LTC_ECC_BRAINPOOLP160R1
498+
#define LTC_ECC_SECP192R1
499+
#define LTC_ECC_PRIME192V2
500+
#define LTC_ECC_PRIME192V3
501+
#define LTC_ECC_SECP192K1
502+
#define LTC_ECC_BRAINPOOLP192R1
503+
#define LTC_ECC_SECP224R1
504+
#define LTC_ECC_SECP224K1
505+
#define LTC_ECC_BRAINPOOLP224R1
506+
#define LTC_ECC_PRIME239V1
507+
#define LTC_ECC_PRIME239V2
508+
#define LTC_ECC_PRIME239V3
509+
#define LTC_ECC_SECP256R1
510+
#define LTC_ECC_SECP256K1
511+
#define LTC_ECC_BRAINPOOLP256R1
512+
#define LTC_ECC_BRAINPOOLP320R1
513+
#define LTC_ECC_SECP384R1
514+
#define LTC_ECC_BRAINPOOLP384R1
515+
#define LTC_ECC_BRAINPOOLP512R1
516+
#define LTC_ECC_SECP521R1
517+
/* OLD deprecated (but still working) defines */
490518
#define LTC_ECC112
491519
#define LTC_ECC128
492520
#define LTC_ECC160

src/headers/tomcrypt_math.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ typedef struct {
243243
*/
244244
int (*sqr)(void *a, void *b);
245245

246+
/** Square root (mod prime)
247+
@param a The integer to compute square root mod prime from
248+
@param b The prime
249+
@param c The destination
250+
@return CRYPT_OK on success
251+
*/
252+
int (*sqrtmod_prime)(void *a, void *b, void *c);
253+
246254
/** Divide an integer
247255
@param a The dividend
248256
@param b The divisor
@@ -363,6 +371,7 @@ typedef struct {
363371
@param k The integer to multiply the point by
364372
@param G The point to multiply
365373
@param R The destination for kG
374+
@param a ECC curve parameter a (if NULL we assume a == -3)
366375
@param modulus The modulus for the field
367376
@param map Boolean indicated whether to map back to affine or not
368377
(can be ignored if you work in affine only)
@@ -371,32 +380,37 @@ typedef struct {
371380
int (*ecc_ptmul)( void *k,
372381
ecc_point *G,
373382
ecc_point *R,
383+
void *a,
374384
void *modulus,
375385
int map);
376386

377387
/** ECC GF(p) point addition
378388
@param P The first point
379389
@param Q The second point
380390
@param R The destination of P + Q
391+
@param a ECC curve parameter a (if NULL we assume a == -3)
381392
@param modulus The modulus
382393
@param mp The "b" value from montgomery_setup()
383394
@return CRYPT_OK on success
384395
*/
385396
int (*ecc_ptadd)(ecc_point *P,
386397
ecc_point *Q,
387398
ecc_point *R,
399+
void *a,
388400
void *modulus,
389401
void *mp);
390402

391403
/** ECC GF(p) point double
392404
@param P The first point
393405
@param R The destination of 2P
406+
@param a ECC curve parameter a (if NULL we assume a == -3)
394407
@param modulus The modulus
395408
@param mp The "b" value from montgomery_setup()
396409
@return CRYPT_OK on success
397410
*/
398411
int (*ecc_ptdbl)(ecc_point *P,
399412
ecc_point *R,
413+
void *a,
400414
void *modulus,
401415
void *mp);
402416

@@ -424,6 +438,7 @@ typedef struct {
424438
int (*ecc_mul2add)(ecc_point *A, void *kA,
425439
ecc_point *B, void *kB,
426440
ecc_point *C,
441+
void *a,
427442
void *modulus);
428443

429444
/* ---- (optional) rsa optimized math (for internal CRT) ---- */
@@ -542,6 +557,7 @@ extern const ltc_math_descriptor gmp_desc;
542557
#define mp_mul(a, b, c) ltc_mp.mul(a, b, c)
543558
#define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
544559
#define mp_sqr(a, b) ltc_mp.sqr(a, b)
560+
#define mp_sqrtmod_prime(a, b, c) ltc_mp.sqrtmod_prime(a, b, c)
545561
#define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
546562
#define mp_div_2(a, b) ltc_mp.div_2(a, b)
547563
#define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)

src/headers/tomcrypt_pk.h

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
enum {
1313
PK_PUBLIC=0,
14-
PK_PRIVATE=1
14+
PK_PRIVATE=1,
15+
PK_PUBLIC_COMPRESSED=2 /* used only when exporting public ECC key */
1516
};
1617

1718
/* Indicates standard output formats that can be read e.g. by OpenSSL or GnuTLS */
@@ -242,6 +243,9 @@ typedef struct {
242243
/** The prime that defines the field the curve is in (encoded in hex) */
243244
char *prime;
244245

246+
/** The fields A param (hex) */
247+
char *A;
248+
245249
/** The fields B param (hex) */
246250
char *B;
247251

@@ -253,6 +257,12 @@ typedef struct {
253257

254258
/** The y co-ordinate of the base point on the curve (hex) */
255259
char *Gy;
260+
261+
/** The co-factor */
262+
unsigned long cofactor;
263+
264+
/** The OID stucture */
265+
oid_st oid;
256266
} ltc_ecc_set_type;
257267

258268
/** A point on a ECC curve, stored in Jacbobian format such that (x,y,z) => (x/z^2, y/z^3, 1) when interpretted as affine */
@@ -292,13 +302,19 @@ int ecc_test(void);
292302
void ecc_sizes(int *low, int *high);
293303
int ecc_get_size(ecc_key *key);
294304

305+
ltc_ecc_set_type* ecc_dp_find_by_oid(unsigned long *oid, unsigned long oidsize);
306+
ltc_ecc_set_type* ecc_dp_find_by_name(char *curve_name);
307+
ltc_ecc_set_type* ecc_dp_find_by_params(char *hex_prime, char *hex_A, char *hex_B, char *hex_order, char *hex_Gx, char *hex_Gy, unsigned long cofactor);
308+
295309
int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
296310
int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp);
297311
void ecc_free(ecc_key *key);
298312

299313
int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
300314
int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
301315
int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp);
316+
int ecc_export_raw(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
317+
int ecc_import_raw(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp);
302318

303319
int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
304320
int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
@@ -332,23 +348,32 @@ int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
332348
const unsigned char *hash, unsigned long hashlen,
333349
int *stat, ecc_key *key);
334350

351+
int ecc_verify_key(ecc_key *key);
352+
353+
#ifdef LTC_SOURCE
354+
/* INTERNAL ONLY - it should be later moved to src/headers/tomcrypt_internal.h */
355+
335356
/* low level functions */
336357
ecc_point *ltc_ecc_new_point(void);
337358
void ltc_ecc_del_point(ecc_point *p);
338359
int ltc_ecc_is_valid_idx(int n);
360+
int ltc_ecc_is_point(const ltc_ecc_set_type *dp, void *x, void *y);
361+
int ltc_ecc_is_point_at_infinity(ecc_point *p, void *modulus);
362+
int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *prime, void *a, void *b, void *x, void *y);
363+
int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed);
339364

340365
/* point ops (mp == montgomery digit) */
341366
#if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
342367
/* R = 2P */
343-
int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);
368+
int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *a, void *modulus, void *mp);
344369

345370
/* R = P + Q */
346-
int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
371+
int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *a, void *modulus, void *mp);
347372
#endif
348373

349374
#if defined(LTC_MECC_FP)
350375
/* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
351-
int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
376+
int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
352377

353378
/* functions for saving/loading/freeing/adding to fixed point cache */
354379
int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
@@ -361,20 +386,23 @@ void ltc_ecc_fp_tablelock(int lock);
361386
#endif
362387

363388
/* R = kG */
364-
int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
389+
int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
365390

366391
#ifdef LTC_ECC_SHAMIR
367392
/* kA*A + kB*B = C */
368393
int ltc_ecc_mul2add(ecc_point *A, void *kA,
369394
ecc_point *B, void *kB,
370395
ecc_point *C,
396+
void *a,
371397
void *modulus);
372398

373399
#ifdef LTC_MECC_FP
374400
/* Shamir's trick with optimized point multiplication using fixed point cache */
375401
int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
376402
ecc_point *B, void *kB,
377-
ecc_point *C, void *modulus);
403+
ecc_point *C,
404+
void *a,
405+
void *modulus);
378406
#endif
379407

380408
#endif
@@ -383,6 +411,8 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
383411
/* map P to affine from projective */
384412
int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
385413

414+
#endif /* LTC_SOURCE */
415+
386416
#endif
387417

388418
#ifdef LTC_MDSA

0 commit comments

Comments
 (0)