summaryrefslogtreecommitdiffhomepage
path: root/ecc.c
blob: f949dd191e1f8e1dd87e34624fc9089562b940a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "includes.h"
#include "options.h"
#include "ecc.h"
#include "dbutil.h"
#include "bignum.h"

#if DROPBEAR_ECC

/* .dp members are filled out by dropbear_ecc_fill_dp() at startup */
#if DROPBEAR_ECC_256
struct dropbear_ecc_curve ecc_curve_nistp256 = {
	32,		/* .ltc_size	*/
	NULL,		/* .dp		*/
	&sha256_desc,	/* .hash_desc	*/
	"nistp256"	/* .name	*/
};
#endif
#if DROPBEAR_ECC_384
struct dropbear_ecc_curve ecc_curve_nistp384 = {
	48,		/* .ltc_size	*/
	NULL,		/* .dp		*/
	&sha384_desc,	/* .hash_desc	*/
	"nistp384"	/* .name	*/
};
#endif
#if DROPBEAR_ECC_521
struct dropbear_ecc_curve ecc_curve_nistp521 = {
	66,		/* .ltc_size	*/
	NULL,		/* .dp		*/
	&sha512_desc,	/* .hash_desc	*/
	"nistp521"	/* .name	*/
};
#endif

struct dropbear_ecc_curve *dropbear_ecc_curves[] = {
#if DROPBEAR_ECC_256
	&ecc_curve_nistp256,
#endif
#if DROPBEAR_ECC_384
	&ecc_curve_nistp384,
#endif
#if DROPBEAR_ECC_521
	&ecc_curve_nistp521,
#endif
	NULL
};

void dropbear_ecc_fill_dp() {
	struct dropbear_ecc_curve **curve;
	/* libtomcrypt guarantees they're ordered by size */
	const ltc_ecc_set_type *dp = ltc_ecc_sets;
	for (curve = dropbear_ecc_curves; *curve; curve++) {
		for (;dp->size > 0; dp++) {
			if (dp->size == (*curve)->ltc_size) {
				(*curve)->dp = dp;
				break;
			}
		}
		if (!(*curve)->dp) {
			dropbear_exit("Missing ECC params %s", (*curve)->name);
		}
	}
}

struct dropbear_ecc_curve* curve_for_dp(const ltc_ecc_set_type *dp) {
	struct dropbear_ecc_curve **curve = NULL;
	for (curve = dropbear_ecc_curves; *curve; curve++) {
		if ((*curve)->dp == dp) {
			break;
		}
	}
	assert(*curve);
	return *curve;
}

ecc_key * new_ecc_key(void) {
	ecc_key *key = m_malloc(sizeof(*key));
	m_mp_alloc_init_multi((mp_int**)&key->pubkey.x, (mp_int**)&key->pubkey.y, 
		(mp_int**)&key->pubkey.z, (mp_int**)&key->k, NULL);
	return key;
}

/* Copied from libtomcrypt ecc_import.c (version there is static), modified
   for different mp_int pointer without LTC_SOURCE */
static int ecc_is_point(const ecc_key *key)
{
	mp_int *prime, *b, *t1, *t2;
	int err;
	
	m_mp_alloc_init_multi(&prime, &b, &t1, &t2, NULL);
	
   /* load prime and b */
	if ((err = mp_read_radix(prime, key->dp->prime, 16)) != CRYPT_OK)                          { goto error; }
	if ((err = mp_read_radix(b, key->dp->B, 16)) != CRYPT_OK)                                  { goto error; }
	
   /* compute y^2 */
	if ((err = mp_sqr(key->pubkey.y, t1)) != CRYPT_OK)                                         { goto error; }
	
   /* compute x^3 */
	if ((err = mp_sqr(key->pubkey.x, t2)) != CRYPT_OK)                                         { goto error; }
	if ((err = mp_mod(t2, prime, t2)) != CRYPT_OK)                                             { goto error; }
	if ((err = mp_mul(key->pubkey.x, t2, t2)) != CRYPT_OK)                                     { goto error; }
	
   /* compute y^2 - x^3 */
	if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK)                                                { goto error; }
	
   /* compute y^2 - x^3 + 3x */
	if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK)                                     { goto error; }
	if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK)                                     { goto error; }
	if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK)                                     { goto error; }
	if ((err = mp_mod(t1, prime, t1)) != CRYPT_OK)                                             { goto error; }
	while (mp_cmp_d(t1, 0) == LTC_MP_LT) {
		if ((err = mp_add(t1, prime, t1)) != CRYPT_OK)                                          { goto error; }
	}
	while (mp_cmp(t1, prime) != LTC_MP_LT) {
		if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK)                                          { goto error; }
	}
	
   /* compare to b */
	if (mp_cmp(t1, b) != LTC_MP_EQ) {
		err = CRYPT_INVALID_PACKET;
	} else {
		err = CRYPT_OK;
	}
	
	error:
	mp_clear_multi(prime, b, t1, t2, NULL);
	m_free(prime);
	m_free(b);
	m_free(t1);
	m_free(t2);
	return err;
}

/* For the "ephemeral public key octet string" in ECDH (rfc5656 section 4) */
void buf_put_ecc_raw_pubkey_string(buffer *buf, ecc_key *key) {
	unsigned long len = key->dp->size*2 + 1;
	int err;
	buf_putint(buf, len);
	err = ecc_ansi_x963_export(key, buf_getwriteptr(buf, len), &len);
	if (err != CRYPT_OK) {
		dropbear_exit("ECC error");
	}
	buf_incrwritepos(buf, len);
}

/* For the "ephemeral public key octet string" in ECDH (rfc5656 section 4) */
ecc_key * buf_get_ecc_raw_pubkey(buffer *buf, const struct dropbear_ecc_curve *curve) {
	ecc_key *key = NULL;
	int ret = DROPBEAR_FAILURE;
	const unsigned int size = curve->dp->size;
	unsigned char first;

	TRACE(("enter buf_get_ecc_raw_pubkey"))

	buf_setpos(buf, 0);
	first = buf_getbyte(buf);
	if (first == 2 || first == 3) {
		dropbear_log(LOG_WARNING, "Dropbear doesn't support ECC point compression");
		return NULL;
	}
	if (first != 4 || buf->len != 1+2*size) {
		TRACE(("leave, wrong size"))
		return NULL;
	}

	key = new_ecc_key();
	key->dp = curve->dp;

	if (mp_read_unsigned_bin(key->pubkey.x, buf_getptr(buf, size), size) != MP_OKAY) {
		TRACE(("failed to read x"))
		goto out;
	}
	buf_incrpos(buf, size);

	if (mp_read_unsigned_bin(key->pubkey.y, buf_getptr(buf, size), size) != MP_OKAY) {
		TRACE(("failed to read y"))
		goto out;
	}
	buf_incrpos(buf, size);

	mp_set(key->pubkey.z, 1);

	if (ecc_is_point(key) != CRYPT_OK) {
		TRACE(("failed, not a point"))
		goto out;
	}

   /* SEC1 3.2.3.1 Check that Q != 0 */
	if (mp_cmp_d(key->pubkey.x, 0) == LTC_MP_EQ) {
		TRACE(("failed, x == 0"))
		goto out;
	}
	if (mp_cmp_d(key->pubkey.y, 0) == LTC_MP_EQ) {
		TRACE(("failed, y == 0"))
		goto out;
	}

	ret = DROPBEAR_SUCCESS;

	out:
	if (ret == DROPBEAR_FAILURE) {
		if (key) {
			ecc_free(key);
			m_free(key);
			key = NULL;
		}
	}

	return key;

}

/* a modified version of libtomcrypt's "ecc_shared_secret" to output
   a mp_int instead. */
mp_int * dropbear_ecc_shared_secret(ecc_key *public_key, const ecc_key *private_key)
{
	ecc_point *result = NULL;
	mp_int *prime = NULL, *shared_secret = NULL;
	int err = DROPBEAR_FAILURE;

   /* type valid? */
	if (private_key->type != PK_PRIVATE) {
		goto out;
	}

	if (private_key->dp != public_key->dp) {
		goto out;
	}

   /* make new point */
	result = ltc_ecc_new_point();
	if (result == NULL) {
		goto out;
	}

	prime = m_malloc(sizeof(*prime));
	m_mp_init(prime);

	if (mp_read_radix(prime, (char *)private_key->dp->prime, 16) != CRYPT_OK) { 
		goto out;
	}
	if (ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1) != CRYPT_OK) { 
		goto out;
	}

	shared_secret = m_malloc(sizeof(*shared_secret));
	m_mp_init(shared_secret);
	if (mp_copy(result->x, shared_secret) != CRYPT_OK) {
		goto out;
	}

	mp_clear(prime);
	m_free(prime);
	ltc_ecc_del_point(result);

	err = DROPBEAR_SUCCESS;
	out:
	if (err == DROPBEAR_FAILURE) {
		dropbear_exit("ECC error");
	}
	return shared_secret;
}

#endif