Skip to content

Commit 294ea20

Browse files
authored
Fixup to work with mbedtls 3.x (#219)
Allow access to the private variables required, and some changed function signatures Maintain compatibility with mbedtls 2.x using #ifs
1 parent d9691ed commit 294ea20

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

bintool/bintool.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ int read_keys(const std::string &filename, public_t *public_key, private_t *priv
5050
int rc;
5151

5252
mbedtls_pk_init(&pk_ctx);
53+
#if MBEDTLS_VERSION_MAJOR >= 3
54+
// This rng is only used for blinding when reading the key file
55+
// As this should only be done on a secure computer, blinding is not required, so it's fine to not actually seed it with any entropy
56+
mbedtls_ctr_drbg_context ctr_drbg;
57+
mbedtls_ctr_drbg_init(&ctr_drbg);
58+
rc = mbedtls_pk_parse_keyfile(&pk_ctx, filename.c_str(), NULL, mbedtls_ctr_drbg_random, &ctr_drbg);
59+
#else
5360
rc = mbedtls_pk_parse_keyfile(&pk_ctx, filename.c_str(), NULL);
61+
#endif
5462
if (rc != 0) {
5563
char error_string[128];
5664
mbedtls_strerror(rc, error_string, sizeof(error_string));

bintool/mbedtls_wrapper.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ void mb_sign_sha256(const uint8_t *entropy, size_t entropy_size, const message_d
175175

176176
DEBUG_LOG(" ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits);
177177

178+
#if MBEDTLS_VERSION_MAJOR >= 3
179+
ret = mbedtls_ecp_check_pub_priv(&ctx_sign, &ctx_sign, mbedtls_ctr_drbg_random, &ctr_drbg);
180+
#else
178181
ret = mbedtls_ecp_check_pub_priv(&ctx_sign, &ctx_sign);
182+
#endif
179183
DEBUG_LOG("Pub Priv Returned %d\n", ret);
180184

181185
dump_pubkey(" + Public key: ", &ctx_sign);
@@ -187,7 +191,11 @@ void mb_sign_sha256(const uint8_t *entropy, size_t entropy_size, const message_d
187191

188192
if ((ret = mbedtls_ecdsa_write_signature(&ctx_sign, MBEDTLS_MD_SHA256,
189193
m->bytes, sizeof(m->bytes),
190-
out->der, &out->der_len,
194+
out->der,
195+
#if MBEDTLS_VERSION_MAJOR >= 3
196+
sizeof(out->der),
197+
#endif
198+
&out->der_len,
191199
mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
192200
DEBUG_LOG(" failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret);
193201
return;

bintool/mbedtls_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extern "C" {
44
#endif
55

66
#undef MBEDTLS_ECDSA_DETERMINISTIC
7+
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
78

89
#include <stdint.h>
910
#include <stdlib.h>
@@ -16,6 +17,7 @@ extern "C" {
1617
#include <mbedtls/pk.h>
1718
#include <mbedtls/ecp.h>
1819
#include <mbedtls/aes.h>
20+
#include <mbedtls/version.h>
1921

2022
#ifdef __cplusplus
2123
#define _Static_assert static_assert

0 commit comments

Comments
 (0)