Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/pk/ecc/ecc_import.c
4396 views
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
*
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
5
*
6
* The library is free for all purposes without any express
7
* guarantee it works.
8
*/
9
10
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
11
*
12
* All curves taken from NIST recommendation paper of July 1999
13
* Available at http://csrc.nist.gov/cryptval/dss.htm
14
*/
15
#include "tomcrypt.h"
16
17
/**
18
@file ecc_import.c
19
ECC Crypto, Tom St Denis
20
*/
21
22
#ifdef LTC_MECC
23
24
static int _is_point(ecc_key *key)
25
{
26
void *prime, *b, *t1, *t2;
27
int err;
28
29
if ((err = mp_init_multi(&prime, &b, &t1, &t2, NULL)) != CRYPT_OK) {
30
return err;
31
}
32
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; }
36
37
/* compute y^2 */
38
if ((err = mp_sqr(key->pubkey.y, t1)) != CRYPT_OK) { goto error; }
39
40
/* compute x^3 */
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; }
44
45
/* compute y^2 - x^3 */
46
if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK) { goto error; }
47
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; }
55
}
56
while (mp_cmp(t1, prime) != LTC_MP_LT) {
57
if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK) { goto error; }
58
}
59
60
/* compare to b */
61
if (mp_cmp(t1, b) != LTC_MP_EQ) {
62
err = CRYPT_INVALID_PACKET;
63
} else {
64
err = CRYPT_OK;
65
}
66
67
error:
68
mp_clear_multi(prime, b, t1, t2, NULL);
69
return err;
70
}
71
72
/**
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
78
*/
79
int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key)
80
{
81
return ecc_import_ex(in, inlen, key, NULL);
82
}
83
84
/**
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
91
*/
92
int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp)
93
{
94
unsigned long key_size;
95
unsigned char flags[1];
96
int err;
97
98
LTC_ARGCHK(in != NULL);
99
LTC_ARGCHK(key != NULL);
100
LTC_ARGCHK(ltc_mp.name != NULL);
101
102
/* init key */
103
if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL) != CRYPT_OK) {
104
return CRYPT_MEM;
105
}
106
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) {
111
goto done;
112
}
113
114
115
if (flags[0] == 1) {
116
/* private key */
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) {
125
goto done;
126
}
127
} else if (flags[0] == 0) {
128
/* public key */
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) {
136
goto done;
137
}
138
}
139
else {
140
err = CRYPT_INVALID_PACKET;
141
goto done;
142
}
143
144
if (dp == NULL) {
145
/* find the idx */
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;
149
goto done;
150
}
151
key->dp = &ltc_ecc_sets[key->idx];
152
} else {
153
key->idx = -1;
154
key->dp = dp;
155
}
156
/* set z */
157
if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto done; }
158
159
/* is it a point on the curve? */
160
if ((err = _is_point(key)) != CRYPT_OK) {
161
goto done;
162
}
163
164
/* we're good */
165
return CRYPT_OK;
166
done:
167
mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
168
return err;
169
}
170
#endif
171
172