|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 WIKA Alexander Wiegand SE & Co. KG |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include "mcuboot_config/mcuboot_config.h" |
| 7 | + |
| 8 | +#if defined(MCUBOOT_GEN_ENC_KEY) |
| 9 | +#include "mbedtls/entropy.h" |
| 10 | +#include "mbedtls/ctr_drbg.h" |
| 11 | +#include "mbedtls/pk.h" |
| 12 | +#include "mbedtls/ecp.h" |
| 13 | +#include "bootutil/generate_key_pair.h" |
| 14 | +#include "bootutil/boot_store_enc_keys.h" |
| 15 | +#include "pkcs8secp256write.h" |
| 16 | +#include "bootutil/bootutil_log.h" |
| 17 | +BOOT_LOG_MODULE_DECLARE(mcuboot); |
| 18 | + |
| 19 | +#define MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES (30 + 2 * MBEDTLS_ECP_MAX_BYTES) |
| 20 | +#define PK_ECP_SIZE_BASE64 ((MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES*4+3-1)/3) |
| 21 | +#define PEM_BEGIN_PUBLIC_KEY_SIZE 25 |
| 22 | +#define PEM_END_PUBLIC_KEY_SIZE 27 |
| 23 | +#define NEW_LINE_CHARS 3 |
| 24 | +#define PEM_PUBLIC_KEY_SIZE (PEM_BEGIN_PUBLIC_KEY_SIZE + PK_ECP_SIZE_BASE64 + PEM_END_PUBLIC_KEY_SIZE + NEW_LINE_CHARS) |
| 25 | + |
| 26 | +/** |
| 27 | + * @brief Generate public and private key and contain in mbedtls_pk_context. |
| 28 | + * |
| 29 | + * @param pk Initialize mbedtls_pk_context and contains the generate key pair. |
| 30 | + * |
| 31 | + * @return 0 for Success. |
| 32 | + * @return Not equal to zero, therefore failure. |
| 33 | + */ |
| 34 | +static int |
| 35 | +gen_p256_keypair(mbedtls_pk_context *pk) |
| 36 | +{ |
| 37 | + int ret; |
| 38 | + mbedtls_entropy_context entropy; |
| 39 | + mbedtls_ctr_drbg_context ctr_drbg; |
| 40 | + const unsigned char pers[] = "keyge-p256-keygenkeyge-p256-keygen"; |
| 41 | + |
| 42 | + mbedtls_pk_init(pk); |
| 43 | + mbedtls_entropy_init(&entropy); |
| 44 | + mbedtls_ctr_drbg_init(&ctr_drbg); |
| 45 | + |
| 46 | + ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, |
| 47 | + pers, sizeof(pers) - 1); |
| 48 | + if (ret != 0) { |
| 49 | + BOOT_LOG_ERR("SEED FAIL ret=%d", ret); |
| 50 | + goto cleanup; |
| 51 | + } |
| 52 | + |
| 53 | + ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)); |
| 54 | + if (ret != 0) { |
| 55 | + BOOT_LOG_ERR("PK_SETUP FAIL ret=%d", ret); |
| 56 | + goto cleanup; |
| 57 | + } |
| 58 | + |
| 59 | + ret = mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(*pk), |
| 60 | + mbedtls_ctr_drbg_random, &ctr_drbg); |
| 61 | + if (ret != 0) { |
| 62 | + BOOT_LOG_ERR("GEN_KEY FAIL ret=%d", ret); |
| 63 | + goto cleanup; |
| 64 | + } |
| 65 | + |
| 66 | +cleanup: |
| 67 | + mbedtls_ctr_drbg_free(&ctr_drbg); |
| 68 | + mbedtls_entropy_free(&entropy); |
| 69 | + return ret; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * @brief Export private key in PKCS8 format. |
| 74 | + * |
| 75 | + * @param pk Initialize mbedtls_pk_context and contains the generate key pair. |
| 76 | + * |
| 77 | + * @return 0 for Success. |
| 78 | + * @return Not equal to zero, therefore failure. |
| 79 | + */ |
| 80 | +static int |
| 81 | +save_privkey_der(const mbedtls_pk_context *pk) |
| 82 | +{ |
| 83 | + unsigned char buf[MCUBOOT_PRIV_ENC_KEY_LEN]; |
| 84 | + |
| 85 | + int len = mbedtls_pk_write_keypkcs8_der(pk, buf, sizeof(buf)); |
| 86 | + if (len != MCUBOOT_PRIV_ENC_KEY_LEN) { |
| 87 | + BOOT_LOG_ERR("fails write pkcs8 der: len=%d", len); |
| 88 | + return len; |
| 89 | + } |
| 90 | + |
| 91 | + int ret = store_priv_enc_key(buf, len); |
| 92 | + if (ret != 0) |
| 93 | + { |
| 94 | + BOOT_LOG_ERR("key store fail store ret=%d",ret); |
| 95 | + return ret; |
| 96 | + } |
| 97 | + |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * @brief Export private and public key in PEM format. |
| 103 | + * |
| 104 | + * @param pk Initialize mbedtls_pk_context and contains the generate key pair. |
| 105 | + * |
| 106 | + * @return 0 for Success. |
| 107 | + * @return Not equal to zero, therefore failure. |
| 108 | + */ |
| 109 | +static int |
| 110 | +export_pub_pem(const mbedtls_pk_context *pk) |
| 111 | +{ |
| 112 | + unsigned char buf_pub[PEM_PUBLIC_KEY_SIZE]; |
| 113 | + |
| 114 | + int ret = mbedtls_pk_write_pubkey_pem(pk, buf_pub, sizeof(buf_pub)); |
| 115 | + if (ret != 0) |
| 116 | + { |
| 117 | + BOOT_LOG_ERR("PUBKEY_PEM FAIL ret=%d", ret); |
| 118 | + return ret; |
| 119 | + } |
| 120 | + |
| 121 | + BOOT_LOG_INF("\n%s", buf_pub); |
| 122 | + |
| 123 | + return 0; |
| 124 | +} |
| 125 | + |
| 126 | +void |
| 127 | +generate_enc_key_pair(void) |
| 128 | +{ |
| 129 | + mbedtls_pk_context pk; |
| 130 | + BOOT_LOG_INF("Generate enc key pair starting..."); |
| 131 | + |
| 132 | + int ret = gen_p256_keypair(&pk); |
| 133 | + if(ret != 0){ |
| 134 | + BOOT_LOG_ERR("Error during the generation enc key pair"); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + ret = save_privkey_der(&pk); |
| 139 | + if(ret != 0){ |
| 140 | + BOOT_LOG_ERR("Error during safe of private key der"); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + BOOT_LOG_INF("Saved private enc key"); |
| 145 | + |
| 146 | + ret = export_pub_pem(&pk); |
| 147 | + if(ret != 0){ |
| 148 | + BOOT_LOG_ERR("Error during the exportation of public key pem"); |
| 149 | + return; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +#endif /* MCUBOOT_GEN_ENC_KEY */ |
0 commit comments