Skip to content

Commit d6a7f79

Browse files
committed
Add Tinycrypt based SHA-512 for ED25519
Add option to build ed25519 with Tinycrypt. This depends on merging of an external PR adding SHA-512 support to Tinycrypt and a following update of the local copy. Signed-off-by: Fabio Utzig <[email protected]>
1 parent dd2b680 commit d6a7f79

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

ext/fiat/src/curve25519.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@
3131
#include <string.h>
3232
#include <stdint.h>
3333

34+
#if defined(MCUBOOT_USE_MBED_TLS)
3435
#include <mbedtls/platform_util.h>
3536
#include <mbedtls/sha512.h>
37+
#else
38+
#include <tinycrypt/constants.h>
39+
#include <tinycrypt/utils.h>
40+
#include <tinycrypt/sha512.h>
41+
#endif
3642

3743
#include "curve25519.h"
3844
// Various pre-computed constants.
@@ -126,12 +132,20 @@ static void fe_tobytes(uint8_t s[32], const fe *f) {
126132

127133
// h = 0
128134
static void fe_0(fe *h) {
135+
#if defined(MCUBOOT_USE_MBED_TLS)
129136
mbedtls_platform_zeroize(h, sizeof(fe));
137+
#else
138+
_set(h, 0, sizeof(fe));
139+
#endif
130140
}
131141

132142
// h = 1
133143
static void fe_1(fe *h) {
144+
#if defined(MCUBOOT_USE_MBED_TLS)
134145
mbedtls_platform_zeroize(h, sizeof(fe));
146+
#else
147+
_set(h, 0, sizeof(fe));
148+
#endif
135149
h->v[0] = 1;
136150
}
137151

@@ -1074,9 +1088,13 @@ int ED25519_verify(const uint8_t *message, size_t message_len,
10741088
}
10751089
}
10761090

1091+
#if defined(MCUBOOT_USE_MBED_TLS)
1092+
10771093
mbedtls_sha512_context ctx;
1078-
mbedtls_sha512_init(&ctx);
10791094
int ret;
1095+
1096+
mbedtls_sha512_init(&ctx);
1097+
10801098
ret = mbedtls_sha512_starts_ret(&ctx, 0);
10811099
assert(ret == 0);
10821100

@@ -1092,6 +1110,27 @@ int ED25519_verify(const uint8_t *message, size_t message_len,
10921110
assert(ret == 0);
10931111
mbedtls_sha512_free(&ctx);
10941112

1113+
#else
1114+
1115+
struct tc_sha512_state_struct s;
1116+
int rc;
1117+
1118+
rc = tc_sha512_init(&s);
1119+
assert(rc == TC_CRYPTO_SUCCESS);
1120+
1121+
rc = tc_sha512_update(&s, signature, 32);
1122+
assert(rc == TC_CRYPTO_SUCCESS);
1123+
rc = tc_sha512_update(&s, public_key, 32);
1124+
assert(rc == TC_CRYPTO_SUCCESS);
1125+
rc = tc_sha512_update(&s, message, message_len);
1126+
assert(rc == TC_CRYPTO_SUCCESS);
1127+
1128+
uint8_t h[TC_SHA512_DIGEST_SIZE];
1129+
rc = tc_sha512_final(h, &s);
1130+
assert(rc == TC_CRYPTO_SUCCESS);
1131+
1132+
#endif
1133+
10951134
x25519_sc_reduce(h);
10961135

10971136
ge_p2 R;

sim/mcuboot-sys/build.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,17 @@ fn main() {
8888
conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
8989
} else if sig_ed25519 {
9090
conf.define("MCUBOOT_SIGN_ED25519", None);
91-
conf.define("MCUBOOT_USE_MBED_TLS", None);
91+
conf.define("MCUBOOT_USE_TINYCRYPT", None);
9292

93-
conf.include("../../ext/mbedtls/include");
94-
conf.file("../../ext/mbedtls/library/sha256.c");
95-
conf.file("../../ext/mbedtls/library/sha512.c");
93+
conf.include("../../ext/tinycrypt/lib/include");
94+
conf.include("../../ext/mbedtls-asn1/include");
95+
conf.file("../../ext/tinycrypt/lib/source/sha256.c");
96+
conf.file("../../ext/tinycrypt/lib/source/sha512.c");
97+
conf.file("../../ext/tinycrypt/lib/source/utils.c");
9698
conf.file("csupport/keys.c");
9799
conf.file("../../ext/fiat/src/curve25519.c");
98-
conf.file("../../ext/mbedtls/library/platform.c");
99-
conf.file("../../ext/mbedtls/library/platform_util.c");
100-
conf.file("../../ext/mbedtls/library/asn1parse.c");
100+
conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
101+
conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
101102
} else if !enc_ec256 {
102103
// No signature type, only sha256 validation. The default
103104
// configuration file bundled with mbedTLS is sufficient.
@@ -212,7 +213,7 @@ fn main() {
212213
} else if (sig_ecdsa || enc_ec256) && !enc_kw {
213214
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
214215
} else if sig_ed25519 {
215-
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
216+
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
216217
} else if enc_kw {
217218
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
218219
}

0 commit comments

Comments
 (0)