1 /*
2 * This module provides a thin abstraction over some of the crypto
3 * primitives to make it easier to swap out the used crypto library.
4 *
5 * At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or
6 * MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly
7 * one of these defined.
8 */
9
10 #ifndef __BOOTUTIL_CRYPTO_ECDH_X25519_H_
11 #define __BOOTUTIL_CRYPTO_ECDH_X25519_H_
12
13 #include "mcuboot_config/mcuboot_config.h"
14
15 #if (defined(MCUBOOT_USE_MBED_TLS) + \
16 defined(MCUBOOT_USE_TINYCRYPT)) != 1
17 #error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT"
18 #endif
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #if defined(MCUBOOT_USE_TINYCRYPT) || defined(MCUBOOT_USE_MBED_TLS)
25 extern int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
26 const uint8_t peer_public_value[32]);
27
28 typedef uintptr_t bootutil_ecdh_x25519_context;
bootutil_ecdh_x25519_init(bootutil_ecdh_x25519_context * ctx)29 static inline void bootutil_ecdh_x25519_init(bootutil_ecdh_x25519_context *ctx)
30 {
31 (void)ctx;
32 }
33
bootutil_ecdh_x25519_drop(bootutil_ecdh_x25519_context * ctx)34 static inline void bootutil_ecdh_x25519_drop(bootutil_ecdh_x25519_context *ctx)
35 {
36 (void)ctx;
37 }
38
bootutil_ecdh_x25519_shared_secret(bootutil_ecdh_x25519_context * ctx,const uint8_t * pk,const uint8_t * sk,uint8_t * z)39 static inline int bootutil_ecdh_x25519_shared_secret(bootutil_ecdh_x25519_context *ctx, const uint8_t *pk, const uint8_t *sk, uint8_t *z)
40 {
41 int rc;
42 (void)ctx;
43
44 rc = X25519(z, sk, pk);
45 if (rc != 0) {
46 return -1;
47 }
48
49 return 0;
50 }
51 #endif /* MCUBOOT_USE_TINYCRYPT */
52
53 #ifdef __cplusplus
54 }
55 #endif
56
57 #endif /* __BOOTUTIL_CRYPTO_ECDH_X25519_H_ */
58