blob: 47fd5badf676679c4b41d0b75f82d692beff0a4b [file] [log] [blame]
Fabio Utzig48764842019-05-10 19:28:24 -03001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
Fabio Utzig48764842019-05-10 19:28:24 -03003 *
David Brownaac71112020-02-03 16:13:42 -07004 * Copyright (c) 2019 JUUL Labs
Roman Okhrimenko977b3752022-03-31 14:40:48 +03005 * Copyright (c) 2021 Arm Limited
Fabio Utzig48764842019-05-10 19:28:24 -03006 */
7
8#include <string.h>
9
10#include "mcuboot_config/mcuboot_config.h"
11
12#ifdef MCUBOOT_SIGN_ED25519
13#include "bootutil/sign_key.h"
14
15#include "mbedtls/oid.h"
16#include "mbedtls/asn1.h"
17
18#include "bootutil_priv.h"
Roman Okhrimenko977b3752022-03-31 14:40:48 +030019#include "bootutil/crypto/common.h"
Fabio Utzig48764842019-05-10 19:28:24 -030020
21static const uint8_t ed25519_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG "\x65\x70";
22#define NUM_ED25519_BYTES 32
23
24extern int ED25519_verify(const uint8_t *message, size_t message_len,
25 const uint8_t signature[64],
26 const uint8_t public_key[32]);
27
28/*
29 * Parse the public key used for signing.
30 */
31static int
32bootutil_import_key(uint8_t **cp, uint8_t *end)
33{
34 size_t len;
35 mbedtls_asn1_buf alg;
36 mbedtls_asn1_buf param;
37
38 if (mbedtls_asn1_get_tag(cp, end, &len,
39 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
40 return -1;
41 }
42 end = *cp + len;
43
44 if (mbedtls_asn1_get_alg(cp, end, &alg, &param)) {
45 return -2;
46 }
47
Roman Okhrimenko977b3752022-03-31 14:40:48 +030048 if (alg.MBEDTLS_CONTEXT_MEMBER(len) != sizeof(ed25519_pubkey_oid) - 1 ||
49 memcmp(alg.MBEDTLS_CONTEXT_MEMBER(p), ed25519_pubkey_oid, sizeof(ed25519_pubkey_oid) - 1)) {
Fabio Utzig48764842019-05-10 19:28:24 -030050 return -3;
51 }
52
53 if (mbedtls_asn1_get_bitstring_null(cp, end, &len)) {
54 return -4;
55 }
56 if (*cp + len != end) {
57 return -5;
58 }
59
60 if (len != NUM_ED25519_BYTES) {
61 return -6;
62 }
63
64 return 0;
65}
66
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030067fih_int
Fabio Utzig48764842019-05-10 19:28:24 -030068bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
69 uint8_t key_id)
70{
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030071 fih_int fih_rc = FIH_FAILURE;
Fabio Utzig48764842019-05-10 19:28:24 -030072
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030073 if (hlen == 32 && slen == 64) {
74 uint8_t *pubkey = (uint8_t *)bootutil_keys[key_id].key;
75 uint8_t *end = pubkey + *bootutil_keys[key_id].len;
76
77 if (0 == bootutil_import_key(&pubkey, end) &&
78 ED25519_verify(hash, 32, sig, pubkey)) {
79
80 fih_rc = FIH_SUCCESS;
81 }
Fabio Utzig48764842019-05-10 19:28:24 -030082 }
83
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030084 FIH_RET(fih_rc);
Fabio Utzig48764842019-05-10 19:28:24 -030085}
86
87#endif /* MCUBOOT_SIGN_ED25519 */