Skip to content

Commit 4876484

Browse files
committed
Add bootutil support for ed25519 validation
Signed-off-by: Fabio Utzig <[email protected]>
1 parent a1e8e43 commit 4876484

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed

boot/bootutil/include/bootutil/image.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct flash_area;
7373
#define IMAGE_TLV_ECDSA224 0x21 /* ECDSA of hash output */
7474
#define IMAGE_TLV_ECDSA256 0x22 /* ECDSA of hash output */
7575
#define IMAGE_TLV_RSA3072_PSS 0x23 /* RSA3072 of hash output */
76+
#define IMAGE_TLV_ED25519 0x24 /* ed25519 of hash output */
7677
#define IMAGE_TLV_ENC_RSA2048 0x30 /* Key encrypted with RSA-OAEP-2048 */
7778
#define IMAGE_TLV_ENC_KW128 0x31 /* Key encrypted with AES-KW-128 */
7879

boot/bootutil/src/image_ed25519.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include <string.h>
21+
22+
#include "mcuboot_config/mcuboot_config.h"
23+
24+
#ifdef MCUBOOT_SIGN_ED25519
25+
#include "bootutil/sign_key.h"
26+
27+
#include "mbedtls/oid.h"
28+
#include "mbedtls/asn1.h"
29+
30+
#include "bootutil_priv.h"
31+
32+
static const uint8_t ed25519_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG "\x65\x70";
33+
#define NUM_ED25519_BYTES 32
34+
35+
extern int ED25519_verify(const uint8_t *message, size_t message_len,
36+
const uint8_t signature[64],
37+
const uint8_t public_key[32]);
38+
39+
/*
40+
* Parse the public key used for signing.
41+
*/
42+
static int
43+
bootutil_import_key(uint8_t **cp, uint8_t *end)
44+
{
45+
size_t len;
46+
mbedtls_asn1_buf alg;
47+
mbedtls_asn1_buf param;
48+
49+
if (mbedtls_asn1_get_tag(cp, end, &len,
50+
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
51+
return -1;
52+
}
53+
end = *cp + len;
54+
55+
if (mbedtls_asn1_get_alg(cp, end, &alg, &param)) {
56+
return -2;
57+
}
58+
59+
if (alg.len != sizeof(ed25519_pubkey_oid) - 1 ||
60+
memcmp(alg.p, ed25519_pubkey_oid, sizeof(ed25519_pubkey_oid) - 1)) {
61+
return -3;
62+
}
63+
64+
if (mbedtls_asn1_get_bitstring_null(cp, end, &len)) {
65+
return -4;
66+
}
67+
if (*cp + len != end) {
68+
return -5;
69+
}
70+
71+
if (len != NUM_ED25519_BYTES) {
72+
return -6;
73+
}
74+
75+
return 0;
76+
}
77+
78+
int
79+
bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
80+
uint8_t key_id)
81+
{
82+
int rc;
83+
uint8_t *pubkey;
84+
uint8_t *end;
85+
86+
if (hlen != 32 || slen != 64) {
87+
return -1;
88+
}
89+
90+
pubkey = (uint8_t *)bootutil_keys[key_id].key;
91+
end = pubkey + *bootutil_keys[key_id].len;
92+
93+
rc = bootutil_import_key(&pubkey, end);
94+
if (rc) {
95+
return -1;
96+
}
97+
98+
rc = ED25519_verify(hash, 32, sig, pubkey);
99+
if (rc == 0) {
100+
return -2;
101+
}
102+
103+
return 0;
104+
}
105+
106+
#endif /* MCUBOOT_SIGN_ED25519 */

boot/bootutil/src/image_validate.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
123123
* call. List the type of TLV we are expecting. If we aren't
124124
* configured for any signature, don't define this macro.
125125
*/
126-
#if (defined(MCUBOOT_SIGN_RSA) + \
127-
defined(MCUBOOT_SIGN_EC) + \
128-
defined(MCUBOOT_SIGN_EC256)) > 1
126+
#if (defined(MCUBOOT_SIGN_RSA) + \
127+
defined(MCUBOOT_SIGN_EC) + \
128+
defined(MCUBOOT_SIGN_EC256) + \
129+
defined(MCUBOOT_SIGN_ED25519)) > 1
129130
#error "Only a single signature type is supported!"
130131
#endif
131132

@@ -147,6 +148,10 @@ bootutil_img_hash(struct image_header *hdr, const struct flash_area *fap,
147148
# define EXPECTED_SIG_TLV IMAGE_TLV_ECDSA256
148149
# define SIG_BUF_SIZE 128
149150
# define EXPECTED_SIG_LEN(x) ((x) >= 72) /* oids + 2 * 32 bytes */
151+
#elif defined(MCUBOOT_SIGN_ED25519)
152+
# define EXPECTED_SIG_TLV IMAGE_TLV_ED25519
153+
# define SIG_BUF_SIZE 64
154+
# define EXPECTED_SIG_LEN(x) ((x) == SIG_BUF_SIZE)
150155
#else
151156
# define SIG_BUF_SIZE 32 /* no signing, sha256 digest only */
152157
#endif

root-ed25519.pem

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MC4CAQAwBQYDK2VwBCIEICjJcMmhqKi8d3hoYtLnbQ1DCitXyHz8N5BLy6rIdMvY
3+
-----END PRIVATE KEY-----

0 commit comments

Comments
 (0)