Skip to content

Commit f54b702

Browse files
committed
ECC curves y^2 = x^3 + ax + b
1 parent 5dad88f commit f54b702

Some content is hidden

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

45 files changed

+4763
-818
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ addons:
1313

1414
install:
1515
- sudo apt-get update -qq
16-
- sudo apt-get install libtommath-dev
1716

1817
before_script:
1918
- gem install coveralls-lcov
2019
- curl http://ftp.de.debian.org/debian/pool/main/l/lcov/lcov_1.11.orig.tar.gz | tar xz
2120
- export PATH=$PATH:`pwd`/lcov-1.11/bin
2221
- curl -s https://packagecloud.io/install/repositories/libtom/packages/script.deb.sh | sudo bash
23-
- sudo apt-get install libtfm-dev=0.13-5
22+
- sudo apt-get install libtfm-dev=0.13-5 libtommath-dev=1.0-5
2423

2524
matrix:
2625
fast_finish: true

demos/tv_gen.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ void ecc_gen(void)
642642
{
643643
FILE *out;
644644
unsigned char str[512];
645-
void *k, *order, *modulus;
645+
void *k, *order, *modulus, *a;
646646
ecc_point *G, *R;
647647
int x;
648648

@@ -653,26 +653,29 @@ void ecc_gen(void)
653653
mp_init(&k);
654654
mp_init(&order);
655655
mp_init(&modulus);
656+
mp_init(&a);
656657

657658
for (x = 0; ltc_ecc_sets[x].size != 0; x++) {
658-
fprintf(out, "ECC-%d\n", ltc_ecc_sets[x].size*8);
659+
660+
fprintf(out, "%s\n", ltc_ecc_sets[x].name);
659661
mp_set(k, 1);
660662

661663
mp_read_radix(order, (char *)ltc_ecc_sets[x].order, 16);
662664
mp_read_radix(modulus, (char *)ltc_ecc_sets[x].prime, 16);
665+
mp_read_radix(a, (char *)ltc_ecc_sets[x].A, 16);
663666
mp_read_radix(G->x, (char *)ltc_ecc_sets[x].Gx, 16);
664667
mp_read_radix(G->y, (char *)ltc_ecc_sets[x].Gy, 16);
665668
mp_set(G->z, 1);
666669

667670
while (mp_cmp(k, order) == LTC_MP_LT) {
668-
ltc_mp.ecc_ptmul(k, G, R, modulus, 1);
671+
ltc_mp.ecc_ptmul(k, G, R, a, modulus, 1);
669672
mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
670673
mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
671674
mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
672675
mp_mul_d(k, 3, k);
673676
}
674677
}
675-
mp_clear_multi(k, order, modulus, NULL);
678+
mp_clear_multi(k, order, modulus, a, NULL);
676679
ltc_ecc_del_point(G);
677680
ltc_ecc_del_point(R);
678681
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
@@ -461,6 +461,34 @@
461461
#ifdef LTC_MECC
462462
/* Supported ECC Key Sizes */
463463
#ifndef LTC_NO_CURVES
464+
#define LTC_ECC_SECP112R1
465+
#define LTC_ECC_SECP112R2
466+
#define LTC_ECC_SECP128R1
467+
#define LTC_ECC_SECP128R2
468+
#define LTC_ECC_SECP160R1
469+
#define LTC_ECC_SECP160R2
470+
#define LTC_ECC_SECP160K1
471+
#define LTC_ECC_BRAINPOOLP160R1
472+
#define LTC_ECC_SECP192R1
473+
#define LTC_ECC_PRIME192V2
474+
#define LTC_ECC_PRIME192V3
475+
#define LTC_ECC_SECP192K1
476+
#define LTC_ECC_BRAINPOOLP192R1
477+
#define LTC_ECC_SECP224R1
478+
#define LTC_ECC_SECP224K1
479+
#define LTC_ECC_BRAINPOOLP224R1
480+
#define LTC_ECC_PRIME239V1
481+
#define LTC_ECC_PRIME239V2
482+
#define LTC_ECC_PRIME239V3
483+
#define LTC_ECC_SECP256R1
484+
#define LTC_ECC_SECP256K1
485+
#define LTC_ECC_BRAINPOOLP256R1
486+
#define LTC_ECC_BRAINPOOLP320R1
487+
#define LTC_ECC_SECP384R1
488+
#define LTC_ECC_BRAINPOOLP384R1
489+
#define LTC_ECC_BRAINPOOLP512R1
490+
#define LTC_ECC_SECP521R1
491+
/* OLD deprecated (but still working) defines */
464492
#define LTC_ECC112
465493
#define LTC_ECC128
466494
#define LTC_ECC160

src/headers/tomcrypt_math.h

Lines changed: 33 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 (if NULL we assume a == -3)
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 a ECC curve parameter a (if NULL we assume a == -3)
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 *a,
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 a ECC curve parameter a (if NULL we assume a == -3)
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 *a,
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)
@@ -424,10 +438,11 @@ typedef struct {
424438
@param modulus Modulus for curve
425439
@return CRYPT_OK on success
426440
*/
427-
int (*ecc_mul2add)(ecc_point *A, void *kA,
428-
ecc_point *B, void *kB,
429-
ecc_point *C,
430-
void *modulus);
441+
int (*ecc_mul2add)(const ecc_point *A, void *kA,
442+
const ecc_point *B, void *kB,
443+
ecc_point *C,
444+
void *a,
445+
void *modulus);
431446

432447
/* ---- (optional) rsa optimized math (for internal CRT) ---- */
433448

@@ -547,6 +562,7 @@ extern const ltc_math_descriptor gmp_desc;
547562
#define mp_mul(a, b, c) ltc_mp.mul(a, b, c)
548563
#define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
549564
#define mp_sqr(a, b) ltc_mp.sqr(a, b)
565+
#define mp_sqrtmod_prime(a, b, c) ltc_mp.sqrtmod_prime(a, b, c)
550566
#define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
551567
#define mp_div_2(a, b) ltc_mp.div_2(a, b)
552568
#define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)

src/headers/tomcrypt_pk.h

Lines changed: 72 additions & 21 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 */
@@ -248,7 +249,7 @@ int dh_check_pubkey(dh_key *key);
248249
/* max private key size */
249250
#define ECC_MAXSIZE 66
250251

251-
/** Structure defines a NIST GF(p) curve */
252+
/** Structure defines a GF(p) curve */
252253
typedef struct {
253254
/** The size of the curve in octets */
254255
int size;
@@ -259,6 +260,9 @@ typedef struct {
259260
/** The prime that defines the field the curve is in (encoded in hex) */
260261
const char *prime;
261262

263+
/** The fields A param (hex) */
264+
const char *A;
265+
262266
/** The fields B param (hex) */
263267
const char *B;
264268

@@ -270,6 +274,12 @@ typedef struct {
270274

271275
/** The y co-ordinate of the base point on the curve (hex) */
272276
const char *Gy;
277+
278+
/** The co-factor */
279+
unsigned long cofactor;
280+
281+
/** The OID stucture */
282+
oid_st oid;
273283
} ltc_ecc_set_type;
274284

275285
/** 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 */
@@ -284,18 +294,35 @@ typedef struct {
284294
void *z;
285295
} ecc_point;
286296

297+
/** ECC key's domain parameters */
298+
typedef struct {
299+
/** The size of the curve in octets */
300+
int size;
301+
/** The prime that defines the field the curve is in */
302+
void *prime;
303+
/** The fields A param */
304+
void *A;
305+
/** The fields B param */
306+
void *B;
307+
/** The order of the curve */
308+
void *order;
309+
/** The base point G on the curve */
310+
ecc_point base;
311+
/** The co-factor */
312+
unsigned long cofactor;
313+
/** The OID structure */
314+
oid_st oid;
315+
} ltc_ecc_dp;
316+
287317
/** An ECC key */
288318
typedef struct {
289319
/** Type of key, PK_PRIVATE or PK_PUBLIC */
290320
int type;
291321

292-
/** Index into the ltc_ecc_sets[] for the parameters of this curve; if -1, then this key is using user supplied curve in dp */
293-
int idx;
294-
295-
/** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */
296-
const ltc_ecc_set_type *dp;
322+
/** Structure with domain parameters */
323+
ltc_ecc_dp dp;
297324

298-
/** The public key */
325+
/** Structure with the public key */
299326
ecc_point pubkey;
300327

301328
/** The private key */
@@ -309,6 +336,12 @@ int ecc_test(void);
309336
void ecc_sizes(int *low, int *high);
310337
int ecc_get_size(ecc_key *key);
311338

339+
int ecc_get_set_by_name(const char* name, const ltc_ecc_set_type** dp);
340+
int ecc_set_dp(const ltc_ecc_set_type *set, ecc_key *key);
341+
int ecc_generate_key(prng_state *prng, int wprng, ecc_key *key);
342+
int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key *key);
343+
int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
344+
312345
int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
313346
int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp);
314347
void ecc_free(ecc_key *key);
@@ -319,7 +352,7 @@ int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, c
319352

320353
int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
321354
int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
322-
int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp);
355+
int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp);
323356

324357
int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
325358
unsigned char *out, unsigned long *outlen);
@@ -349,23 +382,36 @@ int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
349382
const unsigned char *hash, unsigned long hashlen,
350383
int *stat, ecc_key *key);
351384

385+
386+
#ifdef LTC_SOURCE
387+
/* INTERNAL ONLY - it should be later moved to src/headers/tomcrypt_internal.h */
388+
389+
int ecc_set_dp_bn(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key);
390+
int ecc_set_dp_oid(unsigned long *oid, unsigned long oidsize, ecc_key *key);
391+
int ecc_set_dp_copy(ecc_key *srckey, ecc_key *key);
392+
int ecc_set_dp_size(int size, ecc_key *key);
393+
352394
/* low level functions */
353395
ecc_point *ltc_ecc_new_point(void);
354396
void ltc_ecc_del_point(ecc_point *p);
355-
int ltc_ecc_is_valid_idx(int n);
397+
int ltc_ecc_is_point(const ltc_ecc_dp *dp, void *x, void *y);
398+
int ltc_ecc_is_point_at_infinity(const ecc_point *p, void *modulus);
399+
int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *prime, void *a, void *b, void *x, void *y);
400+
int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed);
401+
int ltc_ecc_verify_key(ecc_key *key);
356402

357403
/* point ops (mp == montgomery digit) */
358404
#if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
359405
/* R = 2P */
360-
int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);
406+
int ltc_ecc_projective_dbl_point(const ecc_point *P, ecc_point *R, void *a, void *modulus, void *mp);
361407

362408
/* R = P + Q */
363-
int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
409+
int ltc_ecc_projective_add_point(const ecc_point *P, const ecc_point *Q, ecc_point *R, void *a, void *modulus, void *mp);
364410
#endif
365411

366412
#if defined(LTC_MECC_FP)
367413
/* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
368-
int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
414+
int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
369415

370416
/* functions for saving/loading/freeing/adding to fixed point cache */
371417
int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
@@ -378,20 +424,23 @@ void ltc_ecc_fp_tablelock(int lock);
378424
#endif
379425

380426
/* R = kG */
381-
int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
427+
int ltc_ecc_mulmod(void *k, const ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
382428

383429
#ifdef LTC_ECC_SHAMIR
384430
/* kA*A + kB*B = C */
385-
int ltc_ecc_mul2add(ecc_point *A, void *kA,
386-
ecc_point *B, void *kB,
387-
ecc_point *C,
388-
void *modulus);
431+
int ltc_ecc_mul2add(const ecc_point *A, void *kA,
432+
const ecc_point *B, void *kB,
433+
ecc_point *C,
434+
void *a,
435+
void *modulus);
389436

390437
#ifdef LTC_MECC_FP
391438
/* Shamir's trick with optimized point multiplication using fixed point cache */
392-
int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
393-
ecc_point *B, void *kB,
394-
ecc_point *C, void *modulus);
439+
int ltc_ecc_fp_mul2add(const ecc_point *A, void *kA,
440+
const ecc_point *B, void *kB,
441+
ecc_point *C,
442+
void *a,
443+
void *modulus);
395444
#endif
396445

397446
#endif
@@ -400,6 +449,8 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
400449
/* map P to affine from projective */
401450
int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
402451

452+
#endif /* LTC_SOURCE */
453+
403454
#endif
404455

405456
#ifdef LTC_MDSA

0 commit comments

Comments
 (0)