Skip to content

Commit a1c142d

Browse files
committed
Add Tinycrypt based SHA-512 for ED25519
Add option to build ed25519 with tinycrypt; enable tinycrypt based sha-512 for ed25519 sim tests. Signed-off-by: Fabio Utzig <[email protected]>
1 parent e4fe463 commit a1c142d

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

ext/fiat/src/curve25519.c

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

34+
#include <mcuboot_config/mcuboot_config.h>
35+
36+
#if defined(MCUBOOT_USE_MBED_TLS)
3437
#include <mbedtls/platform_util.h>
3538
#include <mbedtls/sha512.h>
39+
#else
40+
#include <tinycrypt/constants.h>
41+
#include <tinycrypt/utils.h>
42+
#include <tinycrypt/sha512.h>
43+
#endif
3644

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

127135
// h = 0
128136
static void fe_0(fe *h) {
137+
#if defined(MCUBOOT_USE_MBED_TLS)
129138
mbedtls_platform_zeroize(h, sizeof(fe));
139+
#else
140+
_set(h, 0, sizeof(fe));
141+
#endif
130142
}
131143

132144
// h = 1
133145
static void fe_1(fe *h) {
146+
#if defined(MCUBOOT_USE_MBED_TLS)
134147
mbedtls_platform_zeroize(h, sizeof(fe));
148+
#else
149+
_set(h, 0, sizeof(fe));
150+
#endif
135151
h->v[0] = 1;
136152
}
137153

@@ -1074,9 +1090,13 @@ int ED25519_verify(const uint8_t *message, size_t message_len,
10741090
}
10751091
}
10761092

1093+
#if defined(MCUBOOT_USE_MBED_TLS)
1094+
10771095
mbedtls_sha512_context ctx;
1078-
mbedtls_sha512_init(&ctx);
10791096
int ret;
1097+
1098+
mbedtls_sha512_init(&ctx);
1099+
10801100
ret = mbedtls_sha512_starts_ret(&ctx, 0);
10811101
assert(ret == 0);
10821102

@@ -1092,6 +1112,27 @@ int ED25519_verify(const uint8_t *message, size_t message_len,
10921112
assert(ret == 0);
10931113
mbedtls_sha512_free(&ctx);
10941114

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

10971138
ge_p2 R;

sim/mcuboot-sys/build.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,18 @@ fn main() {
9797
conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
9898
} else if sig_ed25519 {
9999
conf.define("MCUBOOT_SIGN_ED25519", None);
100-
conf.define("MCUBOOT_USE_MBED_TLS", None);
100+
conf.define("MCUBOOT_USE_TINYCRYPT", None);
101101

102-
conf.include("../../ext/mbedtls/include");
103-
conf.file("../../ext/mbedtls/library/sha256.c");
104-
conf.file("../../ext/mbedtls/library/sha512.c");
102+
conf.include("../../ext/tinycrypt/lib/include");
103+
conf.include("../../ext/tinycrypt-sha512/lib/include");
104+
conf.include("../../ext/mbedtls-asn1/include");
105+
conf.file("../../ext/tinycrypt/lib/source/sha256.c");
106+
conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c");
107+
conf.file("../../ext/tinycrypt/lib/source/utils.c");
105108
conf.file("csupport/keys.c");
106109
conf.file("../../ext/fiat/src/curve25519.c");
107-
conf.file("../../ext/mbedtls/library/platform.c");
108-
conf.file("../../ext/mbedtls/library/platform_util.c");
109-
conf.file("../../ext/mbedtls/library/asn1parse.c");
110+
conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
111+
conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
110112
} else if !enc_ec256 {
111113
// No signature type, only sha256 validation. The default
112114
// configuration file bundled with mbedTLS is sufficient.
@@ -221,7 +223,7 @@ fn main() {
221223
} else if (sig_ecdsa || enc_ec256) && !enc_kw {
222224
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
223225
} else if sig_ed25519 {
224-
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
226+
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
225227
} else if enc_kw {
226228
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
227229
}

0 commit comments

Comments
 (0)