summaryrefslogtreecommitdiff
path: root/lib/ecdsa/ecdsa-libcrypto.c
blob: 7415d685ee1b5ca0b84317d0842d93324117c597 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// SPDX-License-Identifier: GPL-2.0+
/*
 * ECDSA image signing implementation using libcrypto backend
 *
 * The signature is a binary representation of the (R, S) points, padded to the
 * key size. The signature will be (2 * key_size_bits) / 8 bytes.
 *
 * Deviations from behavior of RSA equivalent:
 *  - Verification uses private key. This is not technically required, but a
 *    limitation on how clumsy the openssl API is to use.
 *  - Handling of keys and key paths:
 *    - The '-K' key directory option must contain path to the key file,
 *      instead of the key directory.
 *    - No assumptions are made about the file extension of the key
 *    - The 'key-name-hint' property is only used for naming devicetree nodes,
 *      but is not used for looking up keys on the filesystem.
 *
 * Copyright (c) 2020,2021, Alexandru Gagniuc <mr.nuke.me@gmail.com>
 */

#define OPENSSL_API_COMPAT 0x10101000L

#include <u-boot/ecdsa.h>
#include <u-boot/fdt-libcrypto.h>
#include <openssl/ssl.h>
#include <openssl/ec.h>
#include <openssl/bn.h>

/* Image signing context for openssl-libcrypto */
struct signer {
	EVP_PKEY *evp_key;	/* Pointer to EVP_PKEY object */
	EC_KEY *ecdsa_key;	/* Pointer to EC_KEY object */
	void *hash;		/* Pointer to hash used for verification */
	void *signature;	/* Pointer to output signature. Do not free()!*/
};

struct ecdsa_public_key {
	const char *curve_name;
	const uint8_t *x;
	const uint8_t *y;
	int size_bits;
};

static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node)
{
	int x_len;
	int y_len;

	key->curve_name = fdt_getprop(fdt, node, "ecdsa,curve", NULL);
	if (!key->curve_name)
		return -ENOMSG;

	if (!strcmp(key->curve_name, "prime256v1"))
		key->size_bits = 256;
	else if (!strcmp(key->curve_name, "secp384r1"))
		key->size_bits = 384;
	else
		return -EINVAL;

	key->x = fdt_getprop(fdt, node, "ecdsa,x-point", &x_len);
	key->y = fdt_getprop(fdt, node, "ecdsa,y-point", &y_len);

	if (!key->x || !key->y)
		return -EINVAL;

	if (x_len != key->size_bits / 8 || y_len != key->size_bits / 8)
		return -EINVAL;

	return 0;
}

static int read_key_from_fdt(struct signer *ctx, const void *fdt, int node)
{
	struct ecdsa_public_key pubkey;
	const EC_GROUP *group;
	EC_POINT *point;
	EC_KEY *ec_key;
	int ret;
	int nid;
	int len;

	ret = fdt_get_key(&pubkey, fdt, node);
	if (ret) {
		fprintf(stderr, "Failed to parse ECDSA key from FDT node %d (ret=%d)\n", node, ret);
		return ret;
	}

	if (!strcmp(pubkey.curve_name, "prime256v1")) {
		nid = NID_X9_62_prime256v1;
	} else if (!strcmp(pubkey.curve_name, "secp384r1")) {
		nid = NID_secp384r1;
	} else {
		fprintf(stderr, "Unsupported curve name: '%s'\n", pubkey.curve_name);
		return -EINVAL;
	}

	fprintf(stderr, "Loading ECDSA key: curve=%s, bits=%d\n", pubkey.curve_name,
		pubkey.size_bits);

	ec_key = EC_KEY_new_by_curve_name(nid);
	if (!ec_key) {
		fprintf(stderr, "Failed to allocate EC_KEY for curve %s\n", pubkey.curve_name);
		return -ENOMEM;
	}

	group = EC_KEY_get0_group(ec_key);
	point = EC_POINT_new(group);
	if (!point) {
		fprintf(stderr, "Failed to allocate EC_POINT\n");
		EC_KEY_free(ec_key);
		return -ENOMEM;
	}

	len = pubkey.size_bits / 8;

	uint8_t buf[1 + len * 2];

	/* uncompressed */
	buf[0] = 0x04;
	memcpy(&buf[1], pubkey.x, len);
	memcpy(&buf[1 + len], pubkey.y, len);
	if (!EC_POINT_oct2point(group, point, buf, sizeof(buf), NULL)) {
		fprintf(stderr, "Failed to convert (x,y) point to EC_POINT\n");
		EC_POINT_free(point);
		EC_KEY_free(ec_key);
		return -EINVAL;
	}

	if (!EC_KEY_set_public_key(ec_key, point)) {
		fprintf(stderr, "Failed to set EC_POINT as public key\n");
		EC_POINT_free(point);
		EC_KEY_free(ec_key);
		return -EINVAL;
	}

	fprintf(stderr, "Successfully loaded ECDSA key from FDT node %d\n", node);
	EC_POINT_free(point);
	ctx->ecdsa_key = ec_key;

	return 0;
}

static int alloc_ctx(struct signer *ctx, const struct image_sign_info *info)
{
	memset(ctx, 0, sizeof(*ctx));

	if (!OPENSSL_init_ssl(0, NULL)) {
		fprintf(stderr, "Failure to init SSL library\n");
		return -1;
	}

	ctx->hash = malloc(info->checksum->checksum_len);
	ctx->signature = malloc(info->crypto->key_len * 2);

	if (!ctx->hash || !ctx->signature)
		return -ENOMEM;

	return 0;
}

static void free_ctx(struct signer *ctx)
{
	if (ctx->ecdsa_key)
		EC_KEY_free(ctx->ecdsa_key);

	if (ctx->evp_key)
		EVP_PKEY_free(ctx->evp_key);

	if (ctx->hash)
		free(ctx->hash);
}

/*
 * Convert an ECDSA signature to raw format
 *
 * openssl DER-encodes 'binary' signatures. We want the signature in a raw
 * (R, S) point pair. So we have to dance a bit.
 */
static void ecdsa_sig_encode_raw(void *buf, const ECDSA_SIG *sig, size_t order)
{
	int point_bytes = order;
	const BIGNUM *r, *s;
	uintptr_t s_buf;

	ECDSA_SIG_get0(sig, &r, &s);
	s_buf = (uintptr_t)buf + point_bytes;
	BN_bn2binpad(r, buf, point_bytes);
	BN_bn2binpad(s, (void *)s_buf, point_bytes);
}

/* Get a signature from a raw encoding */
static ECDSA_SIG *ecdsa_sig_from_raw(void *buf, size_t order)
{
	int point_bytes = order;
	uintptr_t s_buf;
	ECDSA_SIG *sig;
	BIGNUM *r, *s;

	sig = ECDSA_SIG_new();
	if (!sig)
		return NULL;

	s_buf = (uintptr_t)buf + point_bytes;
	r = BN_bin2bn(buf, point_bytes, NULL);
	s = BN_bin2bn((void *)s_buf, point_bytes, NULL);
	ECDSA_SIG_set0(sig, r, s);

	return sig;
}

/* ECDSA key size in bytes */
static size_t ecdsa_key_size_bytes(const EC_KEY *key)
{
	const EC_GROUP *group;

	group = EC_KEY_get0_group(key);
	return (EC_GROUP_order_bits(group) + 7) / 8;
}

static int default_password(char *buf, int size, int rwflag, void *u)
{
	strncpy(buf, (char *)u, size);
	buf[size - 1] = '\0';
	return strlen(buf);
}

static int read_key(struct signer *ctx, const char *key_name)
{
	FILE *f = fopen(key_name, "r");
	const char *key_pass;

	if (!f) {
		fprintf(stderr, "Can not get key file '%s'\n", key_name);
		return -ENOENT;
	}

	key_pass = getenv("MKIMAGE_SIGN_PASSWORD");
	if (key_pass) {
		ctx->evp_key = PEM_read_PrivateKey(f, NULL, default_password, (void *)key_pass);

	} else {
		ctx->evp_key = PEM_read_PrivateKey(f, NULL, NULL, NULL);
	}
	fclose(f);
	if (!ctx->evp_key) {
		fprintf(stderr, "Can not read key from '%s'\n", key_name);
		return -EIO;
	}

	if (EVP_PKEY_id(ctx->evp_key) != EVP_PKEY_EC) {
		fprintf(stderr, "'%s' is not an ECDSA key\n", key_name);
		return -EINVAL;
	}

	ctx->ecdsa_key = EVP_PKEY_get1_EC_KEY(ctx->evp_key);
	if (!ctx->ecdsa_key)
		fprintf(stderr, "Can not extract ECDSA key\n");

	return (ctx->ecdsa_key) ? 0 : -EINVAL;
}

static int load_key_from_fdt(struct signer *ctx, const struct image_sign_info *info)
{
	const void *fdt = info->fdt_blob;
	char name[128];
	int sig_node;
	int key_node;
	int key_len;
	int ret;

	if (!fdt)
		return -EINVAL;

	ret = alloc_ctx(ctx, info);
	if (ret)
		return ret;

	sig_node = fdt_subnode_offset(fdt, 0, FIT_SIG_NODENAME);
	if (sig_node < 0) {
		fprintf(stderr, "No /signature node found\n");
		return -ENOENT;
	}

	/* Case 1: explicitly specified key node */
	if (info->required_keynode >= 0) {
		ret = read_key_from_fdt(ctx, fdt, info->required_keynode);
		if (ret == 0)
			goto check_key_len;

		fprintf(stderr, "Failed to load required keynode %d\n", info->required_keynode);
		return ret;
	}

	/* Case 2: use keyname hint */
	if (info->keyname) {
		snprintf(name, sizeof(name), "%s", info->keyname);
		key_node = fdt_subnode_offset(fdt, sig_node, name);
		if (key_node >= 0) {
			ret = read_key_from_fdt(ctx, fdt, key_node);
			if (ret == 0)
				goto check_key_len;

			fprintf(stderr, "Key hint '%s' found but failed to load\n", info->keyname);
		}
	}

	/* Case 3: try all subnodes */
	fdt_for_each_subnode(key_node, fdt, sig_node) {
		ret = read_key_from_fdt(ctx, fdt, key_node);
		if (ret == 0)
			goto check_key_len;
	}

	fprintf(stderr, "Failed to load any usable ECDSA key from FDT\n");
	return -EINVAL;

check_key_len:
	key_len = ecdsa_key_size_bytes(ctx->ecdsa_key);
	if (key_len != info->crypto->key_len) {
		fprintf(stderr, "Expected %u-bit key, got %u-bit key\n",
			info->crypto->key_len * 8, key_len * 8);
		return -EINVAL;
	}

	return 0;
}

/* Prepare a 'signer' context that's ready to sign and verify. */
static int prepare_ctx(struct signer *ctx, const struct image_sign_info *info)
{
	int key_len_bytes, ret;
	char kname[1024];

	memset(ctx, 0, sizeof(*ctx));

	if (info->fdt_blob) {
		return load_key_from_fdt(ctx, info);
	} else if (info->keyfile) {
		snprintf(kname,  sizeof(kname), "%s", info->keyfile);
	} else if (info->keydir && info->keyname) {
		snprintf(kname, sizeof(kname), "%s/%s.pem", info->keydir,
			 info->keyname);
	} else {
		fprintf(stderr, "keyfile, keyname, or key-name-hint missing\n");
		return -EINVAL;
	}

	ret = alloc_ctx(ctx, info);
	if (ret)
		return ret;

	ret = read_key(ctx, kname);
	if (ret)
		return ret;

	key_len_bytes = ecdsa_key_size_bytes(ctx->ecdsa_key);
	if (key_len_bytes != info->crypto->key_len) {
		fprintf(stderr, "Expected a %u-bit key, got %u-bit key\n",
			info->crypto->key_len * 8, key_len_bytes * 8);
		return -EINVAL;
	}

	return 0;
}

static int do_sign(struct signer *ctx, struct image_sign_info *info,
		   const struct image_region region[], int region_count)
{
	const struct checksum_algo *algo = info->checksum;
	ECDSA_SIG *sig;

	algo->calculate(algo->name, region, region_count, ctx->hash);
	sig = ECDSA_do_sign(ctx->hash, algo->checksum_len, ctx->ecdsa_key);

	ecdsa_sig_encode_raw(ctx->signature, sig, info->crypto->key_len);

	return 0;
}

static int ecdsa_check_signature(struct signer *ctx, struct image_sign_info *info)
{
	ECDSA_SIG *sig;
	int okay;

	sig = ecdsa_sig_from_raw(ctx->signature, info->crypto->key_len);
	if (!sig)
		return -ENOMEM;

	okay = ECDSA_do_verify(ctx->hash, info->checksum->checksum_len,
			       sig, ctx->ecdsa_key);
	if (!okay)
		fprintf(stderr, "WARNING: Signature is fake news!\n");

	ECDSA_SIG_free(sig);
	return !okay;
}

static int do_verify(struct signer *ctx, struct image_sign_info *info,
		     const struct image_region region[], int region_count,
		     uint8_t *raw_sig, uint sig_len)
{
	const struct checksum_algo *algo = info->checksum;

	if (sig_len != info->crypto->key_len * 2) {
		fprintf(stderr, "Signature has wrong length\n");
		return -EINVAL;
	}

	memcpy(ctx->signature, raw_sig, sig_len);
	algo->calculate(algo->name, region, region_count, ctx->hash);

	return ecdsa_check_signature(ctx, info);
}

int ecdsa_sign(struct image_sign_info *info, const struct image_region region[],
	       int region_count, uint8_t **sigp, uint *sig_len)
{
	struct signer ctx;
	int ret;

	ret = prepare_ctx(&ctx, info);
	if (ret >= 0) {
		do_sign(&ctx, info, region, region_count);
		*sigp = ctx.signature;
		*sig_len = info->crypto->key_len * 2;

		ret = ecdsa_check_signature(&ctx, info);
	}

	free_ctx(&ctx);
	return ret;
}

int ecdsa_verify(struct image_sign_info *info,
		 const struct image_region region[], int region_count,
		 uint8_t *sig, uint sig_len)
{
	struct signer ctx;
	int ret;

	ret = prepare_ctx(&ctx, info);
	if (ret >= 0)
		ret = do_verify(&ctx, info, region, region_count, sig, sig_len);

	free_ctx(&ctx);
	return ret;
}

static int do_add(struct signer *ctx, void *fdt, const char *key_node_name,
		  struct image_sign_info *info)
{
	int signature_node, key_node, ret, key_bits;
	const char *curve_name;
	const EC_GROUP *group;
	const EC_POINT *point;
	BIGNUM *x, *y;

	signature_node = fdt_subnode_offset(fdt, 0, FIT_SIG_NODENAME);
	if (signature_node == -FDT_ERR_NOTFOUND) {
		signature_node = fdt_add_subnode(fdt, 0, FIT_SIG_NODENAME);
		if (signature_node < 0) {
			if (signature_node != -FDT_ERR_NOSPACE) {
				fprintf(stderr, "Couldn't create signature node: %s\n",
					fdt_strerror(signature_node));
			}
			return signature_node;
		}
	} else if (signature_node < 0) {
		fprintf(stderr, "Cannot select keys signature_node: %s\n",
			fdt_strerror(signature_node));
		return signature_node;
	}

	/* Either create or overwrite the named key node */
	key_node = fdt_subnode_offset(fdt, signature_node, key_node_name);
	if (key_node == -FDT_ERR_NOTFOUND) {
		key_node = fdt_add_subnode(fdt, signature_node, key_node_name);
		if (key_node < 0) {
			if (key_node != -FDT_ERR_NOSPACE) {
				fprintf(stderr, "Could not create key subnode: %s\n",
					fdt_strerror(key_node));
			}
			return key_node;
		}
	} else if (key_node < 0) {
		fprintf(stderr, "Cannot select keys key_node: %s\n",
			fdt_strerror(key_node));
		return key_node;
	}

	group = EC_KEY_get0_group(ctx->ecdsa_key);
	key_bits = EC_GROUP_order_bits(group);
	curve_name = OBJ_nid2sn(EC_GROUP_get_curve_name(group));
	/* Let 'x' and 'y' memory leak by not BN_free()'ing them. */
	x = BN_new();
	y = BN_new();
	point = EC_KEY_get0_public_key(ctx->ecdsa_key);
	EC_POINT_get_affine_coordinates(group, point, x, y, NULL);

	ret = fdt_setprop_string(fdt, key_node, FIT_KEY_HINT,
				 info->keyname);
	if (ret < 0)
		return ret;

	ret = fdt_setprop_string(fdt, key_node, "ecdsa,curve", curve_name);
	if (ret < 0)
		return ret;

	ret = fdt_add_bignum(fdt, key_node, "ecdsa,x-point", x, key_bits);
	if (ret < 0)
		return ret;

	ret = fdt_add_bignum(fdt, key_node, "ecdsa,y-point", y, key_bits);
	if (ret < 0)
		return ret;

	ret = fdt_setprop_string(fdt, key_node, FIT_ALGO_PROP,
				 info->name);
	if (ret < 0)
		return ret;

	ret = fdt_setprop_string(fdt, key_node, FIT_KEY_REQUIRED,
				 info->require_keys);
	if (ret < 0)
		return ret;

	return key_node;
}

int ecdsa_add_verify_data(struct image_sign_info *info, void *fdt)
{
	const char *fdt_key_name;
	struct signer ctx;
	int ret;

	fdt_key_name = info->keyname ? info->keyname : "default-key";
	ret = prepare_ctx(&ctx, info);
	if (ret >= 0) {
		ret = do_add(&ctx, fdt, fdt_key_name, info);
		if (ret < 0) {
			free_ctx(&ctx);
			return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
		}
	}

	free_ctx(&ctx);
	return ret;
}