comparison libtomcrypt/src/pk/ecc/ecc_make_key.c @ 1435:f849a5ca2efc

update to libtomcrypt 1.17 (with Dropbear changes)
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 17:50:50 +0800
parents 0cbe8f6dbf9e
children 6dba84798cd5
comparison
equal deleted inserted replaced
1434:27b9ddb06b09 1435:f849a5ca2efc
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 * 8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.com 9 * Tom St Denis, [email protected], http://libtom.org
10 */ 10 */
11 11
12 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b 12 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
13 * 13 *
14 * All curves taken from NIST recommendation paper of July 1999 14 * All curves taken from NIST recommendation paper of July 1999
19 /** 19 /**
20 @file ecc_make_key.c 20 @file ecc_make_key.c
21 ECC Crypto, Tom St Denis 21 ECC Crypto, Tom St Denis
22 */ 22 */
23 23
24 #ifdef MECC 24 #ifdef LTC_MECC
25 25
26 /** 26 /**
27 Make a new ECC key 27 Make a new ECC key
28 @param prng An active PRNG state 28 @param prng An active PRNG state
29 @param wprng The index of the PRNG you wish to use 29 @param wprng The index of the PRNG you wish to use
49 49
50 int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp) 50 int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp)
51 { 51 {
52 int err; 52 int err;
53 ecc_point *base; 53 ecc_point *base;
54 void *prime; 54 void *prime, *order;
55 unsigned char *buf; 55 unsigned char *buf;
56 int keysize; 56 int keysize;
57 57
58 LTC_ARGCHK(key != NULL); 58 LTC_ARGCHK(key != NULL);
59 LTC_ARGCHK(ltc_mp.name != NULL); 59 LTC_ARGCHK(ltc_mp.name != NULL);
80 err = CRYPT_ERROR_READPRNG; 80 err = CRYPT_ERROR_READPRNG;
81 goto ERR_BUF; 81 goto ERR_BUF;
82 } 82 }
83 83
84 /* setup the key variables */ 84 /* setup the key variables */
85 if ((err = mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, &prime, NULL)) != CRYPT_OK) { 85 if ((err = mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, &prime, &order, NULL)) != CRYPT_OK) {
86 goto ERR_BUF; 86 goto ERR_BUF;
87 } 87 }
88 base = ltc_ecc_new_point(); 88 base = ltc_ecc_new_point();
89 if (base == NULL) { 89 if (base == NULL) {
90 err = CRYPT_MEM; 90 err = CRYPT_MEM;
91 goto errkey; 91 goto errkey;
92 } 92 }
93 93
94 /* read in the specs for this key */ 94 /* read in the specs for this key */
95 if ((err = mp_read_radix(prime, (char *)key->dp->prime, 16)) != CRYPT_OK) { goto errkey; } 95 if ((err = mp_read_radix(prime, (char *)key->dp->prime, 16)) != CRYPT_OK) { goto errkey; }
96 if ((err = mp_read_radix(order, (char *)key->dp->order, 16)) != CRYPT_OK) { goto errkey; }
96 if ((err = mp_read_radix(base->x, (char *)key->dp->Gx, 16)) != CRYPT_OK) { goto errkey; } 97 if ((err = mp_read_radix(base->x, (char *)key->dp->Gx, 16)) != CRYPT_OK) { goto errkey; }
97 if ((err = mp_read_radix(base->y, (char *)key->dp->Gy, 16)) != CRYPT_OK) { goto errkey; } 98 if ((err = mp_read_radix(base->y, (char *)key->dp->Gy, 16)) != CRYPT_OK) { goto errkey; }
98 if ((err = mp_set(base->z, 1)) != CRYPT_OK) { goto errkey; } 99 if ((err = mp_set(base->z, 1)) != CRYPT_OK) { goto errkey; }
99 if ((err = mp_read_unsigned_bin(key->k, (unsigned char *)buf, keysize)) != CRYPT_OK) { goto errkey; } 100 if ((err = mp_read_unsigned_bin(key->k, (unsigned char *)buf, keysize)) != CRYPT_OK) { goto errkey; }
100 101
102 /* the key should be smaller than the order of base point */
103 if (mp_cmp(key->k, order) != LTC_MP_LT) {
104 if((err = mp_mod(key->k, order, key->k)) != CRYPT_OK) { goto errkey; }
105 }
101 /* make the public key */ 106 /* make the public key */
102 if ((err = ltc_mp.ecc_ptmul(key->k, base, &key->pubkey, prime, 1)) != CRYPT_OK) { goto errkey; } 107 if ((err = ltc_mp.ecc_ptmul(key->k, base, &key->pubkey, prime, 1)) != CRYPT_OK) { goto errkey; }
103 key->type = PK_PRIVATE; 108 key->type = PK_PRIVATE;
104 109
105 /* free up ram */ 110 /* free up ram */
107 goto cleanup; 112 goto cleanup;
108 errkey: 113 errkey:
109 mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL); 114 mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
110 cleanup: 115 cleanup:
111 ltc_ecc_del_point(base); 116 ltc_ecc_del_point(base);
112 mp_clear(prime); 117 mp_clear_multi(prime, order, NULL);
113 ERR_BUF: 118 ERR_BUF:
114 #ifdef LTC_CLEAN_STACK 119 #ifdef LTC_CLEAN_STACK
115 zeromem(buf, ECC_MAXSIZE); 120 zeromem(buf, ECC_MAXSIZE);
116 #endif 121 #endif
117 XFREE(buf); 122 XFREE(buf);
118 return err; 123 return err;
119 } 124 }
120 125
121 #endif 126 #endif
122 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_make_key.c,v $ */ 127 /* $Source$ */
123 /* $Revision: 1.9 $ */ 128 /* $Revision$ */
124 /* $Date: 2006/12/04 02:50:11 $ */ 129 /* $Date$ */
125 130