1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
12
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
14
* All curves taken from NIST recommendation paper of July 1999
15
* Available at http://csrc.nist.gov/cryptval/dss.htm
21
ECC Crypto, Tom St Denis
26
static int is_point(ecc_key *key)
28
void *prime, *b, *t1, *t2;
31
if ((err = mp_init_multi(&prime, &b, &t1, &t2, NULL)) != CRYPT_OK) {
35
/* load prime and b */
36
if ((err = mp_read_radix(prime, key->dp->prime, 16)) != CRYPT_OK) { goto error; }
37
if ((err = mp_read_radix(b, key->dp->B, 16)) != CRYPT_OK) { goto error; }
40
if ((err = mp_sqr(key->pubkey.y, t1)) != CRYPT_OK) { goto error; }
43
if ((err = mp_sqr(key->pubkey.x, t2)) != CRYPT_OK) { goto error; }
44
if ((err = mp_mod(t2, prime, t2)) != CRYPT_OK) { goto error; }
45
if ((err = mp_mul(key->pubkey.x, t2, t2)) != CRYPT_OK) { goto error; }
47
/* compute y^2 - x^3 */
48
if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK) { goto error; }
50
/* compute y^2 - x^3 + 3x */
51
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
52
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
53
if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
54
if ((err = mp_mod(t1, prime, t1)) != CRYPT_OK) { goto error; }
55
while (mp_cmp_d(t1, 0) == LTC_MP_LT) {
56
if ((err = mp_add(t1, prime, t1)) != CRYPT_OK) { goto error; }
58
while (mp_cmp(t1, prime) != LTC_MP_LT) {
59
if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK) { goto error; }
63
if (mp_cmp(t1, b) != LTC_MP_EQ) {
64
err = CRYPT_INVALID_PACKET;
70
mp_clear_multi(prime, b, t1, t2, NULL);
75
Import an ECC key from a binary packet
76
@param in The packet to import
77
@param inlen The length of the packet
78
@param key [out] The destination of the import
79
@return CRYPT_OK if successful, upon error all allocated memory will be freed
81
int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key)
83
return ecc_import_ex(in, inlen, key, NULL);
87
Import an ECC key from a binary packet, using user supplied domain params rather than one of the NIST ones
88
@param in The packet to import
89
@param inlen The length of the packet
90
@param key [out] The destination of the import
91
@param dp pointer to user supplied params; must be the same as the params used when exporting
92
@return CRYPT_OK if successful, upon error all allocated memory will be freed
94
int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp)
96
unsigned long key_size;
97
unsigned char flags[1];
100
LTC_ARGCHK(in != NULL);
101
LTC_ARGCHK(key != NULL);
102
LTC_ARGCHK(ltc_mp.name != NULL);
105
if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL) != CRYPT_OK) {
109
/* find out what type of key it is */
110
if ((err = der_decode_sequence_multi(in, inlen,
111
LTC_ASN1_BIT_STRING, 1UL, &flags,
112
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
119
key->type = PK_PRIVATE;
120
if ((err = der_decode_sequence_multi(in, inlen,
121
LTC_ASN1_BIT_STRING, 1UL, flags,
122
LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,
123
LTC_ASN1_INTEGER, 1UL, key->pubkey.x,
124
LTC_ASN1_INTEGER, 1UL, key->pubkey.y,
125
LTC_ASN1_INTEGER, 1UL, key->k,
126
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
131
key->type = PK_PUBLIC;
132
if ((err = der_decode_sequence_multi(in, inlen,
133
LTC_ASN1_BIT_STRING, 1UL, flags,
134
LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,
135
LTC_ASN1_INTEGER, 1UL, key->pubkey.x,
136
LTC_ASN1_INTEGER, 1UL, key->pubkey.y,
137
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
144
for (key->idx = 0; ltc_ecc_sets[key->idx].size && (unsigned long)ltc_ecc_sets[key->idx].size != key_size; ++key->idx);
145
if (ltc_ecc_sets[key->idx].size == 0) {
146
err = CRYPT_INVALID_PACKET;
149
key->dp = <c_ecc_sets[key->idx];
155
if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto done; }
157
/* is it a point on the curve? */
158
if ((err = is_point(key)) != CRYPT_OK) {
165
mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
169
/* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_import.c,v $ */
170
/* $Revision: 1.11 $ */
171
/* $Date: 2006/12/04 02:19:48 $ */