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
10 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
12 * All curves taken from NIST recommendation paper of July 1999
13 * Available at http://csrc.nist.gov/cryptval/dss.htm
19 ECC Crypto, Tom St Denis
24 static int _is_point(ecc_key *key)
26 void *prime, *b, *t1, *t2;
29 if ((err = mp_init_multi(&prime, &b, &t1, &t2, NULL)) != CRYPT_OK) {
33 /* load prime and b */
34 if ((err = mp_read_radix(prime, key->dp->prime, 16)) != CRYPT_OK) { goto error; }
35 if ((err = mp_read_radix(b, key->dp->B, 16)) != CRYPT_OK) { goto error; }
38 if ((err = mp_sqr(key->pubkey.y, t1)) != CRYPT_OK) { goto error; }
41 if ((err = mp_sqr(key->pubkey.x, t2)) != CRYPT_OK) { goto error; }
42 if ((err = mp_mod(t2, prime, t2)) != CRYPT_OK) { goto error; }
43 if ((err = mp_mul(key->pubkey.x, t2, t2)) != CRYPT_OK) { goto error; }
45 /* compute y^2 - x^3 */
46 if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK) { goto error; }
48 /* compute y^2 - x^3 + 3x */
49 if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
50 if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
51 if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }
52 if ((err = mp_mod(t1, prime, t1)) != CRYPT_OK) { goto error; }
53 while (mp_cmp_d(t1, 0) == LTC_MP_LT) {
54 if ((err = mp_add(t1, prime, t1)) != CRYPT_OK) { goto error; }
56 while (mp_cmp(t1, prime) != LTC_MP_LT) {
57 if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK) { goto error; }
61 if (mp_cmp(t1, b) != LTC_MP_EQ) {
62 err = CRYPT_INVALID_PACKET;
68 mp_clear_multi(prime, b, t1, t2, NULL);
73 Import an ECC key from a binary packet
74 @param in The packet to import
75 @param inlen The length of the packet
76 @param key [out] The destination of the import
77 @return CRYPT_OK if successful, upon error all allocated memory will be freed
79 int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key)
81 return ecc_import_ex(in, inlen, key, NULL);
85 Import an ECC key from a binary packet, using user supplied domain params rather than one of the NIST ones
86 @param in The packet to import
87 @param inlen The length of the packet
88 @param key [out] The destination of the import
89 @param dp pointer to user supplied params; must be the same as the params used when exporting
90 @return CRYPT_OK if successful, upon error all allocated memory will be freed
92 int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp)
94 unsigned long key_size;
95 unsigned char flags[1];
98 LTC_ARGCHK(in != NULL);
99 LTC_ARGCHK(key != NULL);
100 LTC_ARGCHK(ltc_mp.name != NULL);
103 if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL) != CRYPT_OK) {
107 /* find out what type of key it is */
108 err = der_decode_sequence_multi(in, inlen, LTC_ASN1_BIT_STRING, 1UL, flags,
109 LTC_ASN1_EOL, 0UL, NULL);
110 if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
117 key->type = PK_PRIVATE;
118 if ((err = der_decode_sequence_multi(in, inlen,
119 LTC_ASN1_BIT_STRING, 1UL, flags,
120 LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,
121 LTC_ASN1_INTEGER, 1UL, key->pubkey.x,
122 LTC_ASN1_INTEGER, 1UL, key->pubkey.y,
123 LTC_ASN1_INTEGER, 1UL, key->k,
124 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
127 } else if (flags[0] == 0) {
129 key->type = PK_PUBLIC;
130 if ((err = der_decode_sequence_multi(in, inlen,
131 LTC_ASN1_BIT_STRING, 1UL, flags,
132 LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,
133 LTC_ASN1_INTEGER, 1UL, key->pubkey.x,
134 LTC_ASN1_INTEGER, 1UL, key->pubkey.y,
135 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
140 err = CRYPT_INVALID_PACKET;
146 for (key->idx = 0; ltc_ecc_sets[key->idx].size && (unsigned long)ltc_ecc_sets[key->idx].size != key_size; ++key->idx);
147 if (ltc_ecc_sets[key->idx].size == 0) {
148 err = CRYPT_INVALID_PACKET;
151 key->dp = <c_ecc_sets[key->idx];
157 if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto done; }
159 /* is it a point on the curve? */
160 if ((err = _is_point(key)) != CRYPT_OK) {
167 mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
171 /* ref: $Format:%D$ */
172 /* git commit: $Format:%H$ */
173 /* commit time: $Format:%ai$ */