blob: 4d1835b598afc304ebe4e5ff7f7cef6d87b18622 [file] [log] [blame]
Alexandru Gagniuc48cb9b82021-07-29 11:47:16 -05001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * ECDSA signature verification for u-boot
4 *
5 * This implements the firmware-side wrapper for ECDSA verification. It bridges
6 * the struct crypto_algo API to the ECDSA uclass implementations.
7 *
8 * Copyright (c) 2020, Alexandru Gagniuc <mr.nuke.me@gmail.com>
9 */
10
11#include <crypto/ecdsa-uclass.h>
12#include <dm/uclass.h>
13#include <u-boot/ecdsa.h>
14
15/*
16 * Derive size of an ECDSA key from the curve name
17 *
18 * While it's possible to extract the key size by using string manipulation,
19 * use a list of known curves for the time being.
20 */
21static int ecdsa_key_size(const char *curve_name)
22{
23 if (!strcmp(curve_name, "prime256v1"))
24 return 256;
25 else
26 return 0;
27}
28
29static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node)
30{
31 int x_len, y_len;
32
33 key->curve_name = fdt_getprop(fdt, node, "ecdsa,curve", NULL);
Bob Wolff347316f2024-02-27 15:57:03 -080034 if (!key->curve_name) {
35 debug("Error: ecdsa cannot get 'ecdsa,curve' property from key. Likely not an ecdsa key.\n");
36 return -ENOMSG;
37 }
38
Alexandru Gagniuc48cb9b82021-07-29 11:47:16 -050039 key->size_bits = ecdsa_key_size(key->curve_name);
40 if (key->size_bits == 0) {
41 debug("Unknown ECDSA curve '%s'", key->curve_name);
42 return -EINVAL;
43 }
44
45 key->x = fdt_getprop(fdt, node, "ecdsa,x-point", &x_len);
46 key->y = fdt_getprop(fdt, node, "ecdsa,y-point", &y_len);
47
48 if (!key->x || !key->y)
49 return -EINVAL;
50
51 if (x_len != (key->size_bits / 8) || y_len != (key->size_bits / 8)) {
52 printf("%s: node=%d, curve@%p x@%p+%i y@%p+%i\n", __func__,
53 node, key->curve_name, key->x, x_len, key->y, y_len);
54 return -EINVAL;
55 }
56
57 return 0;
58}
59
60static int ecdsa_verify_hash(struct udevice *dev,
61 const struct image_sign_info *info,
62 const void *hash, const void *sig, uint sig_len)
63{
64 const struct ecdsa_ops *ops = device_get_ops(dev);
65 const struct checksum_algo *algo = info->checksum;
66 struct ecdsa_public_key key;
67 int sig_node, key_node, ret;
68
69 if (!ops || !ops->verify)
70 return -ENODEV;
71
72 if (info->required_keynode > 0) {
73 ret = fdt_get_key(&key, info->fdt_blob, info->required_keynode);
74 if (ret < 0)
75 return ret;
76
77 return ops->verify(dev, &key, hash, algo->checksum_len,
78 sig, sig_len);
79 }
80
81 sig_node = fdt_subnode_offset(info->fdt_blob, 0, FIT_SIG_NODENAME);
82 if (sig_node < 0)
83 return -ENOENT;
84
85 /* Try all possible keys under the "/signature" node */
86 fdt_for_each_subnode(key_node, info->fdt_blob, sig_node) {
87 ret = fdt_get_key(&key, info->fdt_blob, key_node);
88 if (ret < 0)
89 continue;
90
91 ret = ops->verify(dev, &key, hash, algo->checksum_len,
92 sig, sig_len);
93
94 /* On success, don't worry about remaining keys */
95 if (!ret)
96 return 0;
97 }
98
99 return -EPERM;
100}
101
102int ecdsa_verify(struct image_sign_info *info,
103 const struct image_region region[], int region_count,
104 uint8_t *sig, uint sig_len)
105{
106 const struct checksum_algo *algo = info->checksum;
107 uint8_t hash[algo->checksum_len];
108 struct udevice *dev;
109 int ret;
110
111 ret = uclass_first_device_err(UCLASS_ECDSA, &dev);
112 if (ret) {
113 debug("ECDSA: Could not find ECDSA implementation: %d\n", ret);
114 return ret;
115 }
116
117 ret = algo->calculate(algo->name, region, region_count, hash);
118 if (ret < 0)
119 return -EINVAL;
120
121 return ecdsa_verify_hash(dev, info, hash, sig, sig_len);
122}
123
124U_BOOT_CRYPTO_ALGO(ecdsa) = {
125 .name = "ecdsa256",
126 .key_len = ECDSA256_BYTES,
127 .verify = ecdsa_verify,
128};
129
130/*
131 * uclass definition for ECDSA API
132 *
133 * We don't implement any wrappers around ecdsa_ops->verify() because it's
134 * trivial to call ops->verify().
135 */
136UCLASS_DRIVER(ecdsa) = {
137 .id = UCLASS_ECDSA,
138 .name = "ecdsa_verifier",
139};