Skip to content

Commit 06f5645

Browse files
committed
sys/psa_crypto: add monocypher as ed25519 software backend
switch from c25519 to monocypher as default
1 parent 9e9be91 commit 06f5645

File tree

6 files changed

+94
-1
lines changed

6 files changed

+94
-1
lines changed

pkg/monocypher/Makefile.include

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
INCLUDES += -I$(PKGDIRBASE)/monocypher/src
22
INCLUDES += -I$(PKGDIRBASE)/monocypher/src/optional
3+
4+
ifneq (,$(filter psa_monocypher_%, $(USEMODULE)))
5+
PSEUDOMODULES += psa_monocypher_ed25519
6+
DIRS += $(RIOTPKG)/monocypher/psa_monocypher
7+
INCLUDES += -I$(RIOTBASE)/sys/psa_crypto/include
8+
endif
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BASE_MODULE := psa_monocypher
2+
SUBMODULES := 1
3+
4+
include $(RIOTBASE)/Makefile.base
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
USEMODULE += random
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 TU Dresden
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
6+
/**
7+
* @ingroup sys_psa_crypto pkg_monocypher
8+
* @{
9+
*
10+
* @brief Glue code translating between PSA Crypto and the Monocypher EdDSA APIs
11+
*
12+
* @author Mikolai Gütschow <[email protected]>
13+
*
14+
* @}
15+
*/
16+
17+
#include "string_utils.h"
18+
19+
#include "psa/crypto.h"
20+
#include "monocypher-ed25519.h"
21+
#include "random.h"
22+
23+
psa_status_t psa_generate_ecc_ed25519_key_pair( uint8_t *priv_key_buffer,
24+
uint8_t *pub_key_buffer)
25+
{
26+
uint8_t priv_and_pub_key[64] = { 0 };
27+
28+
// todo: maybe this should usa psa_random instead
29+
random_bytes(priv_key_buffer, 32);
30+
crypto_ed25519_key_pair(priv_and_pub_key, pub_key_buffer, priv_key_buffer);
31+
32+
explicit_bzero(priv_and_pub_key, 64);
33+
34+
return PSA_SUCCESS;
35+
}
36+
37+
psa_status_t psa_derive_ecc_ed25519_public_key( const uint8_t *priv_key_buffer,
38+
uint8_t *pub_key_buffer)
39+
{
40+
uint8_t priv_and_pub_key[64] = { 0 };
41+
42+
memcpy(&priv_and_pub_key[0], priv_key_buffer, 32);
43+
crypto_ed25519_key_pair(priv_and_pub_key, pub_key_buffer, priv_and_pub_key);
44+
45+
explicit_bzero(priv_and_pub_key, 64);
46+
47+
return PSA_SUCCESS;
48+
}
49+
50+
psa_status_t psa_ecc_ed25519_sign_message(const uint8_t *priv_key_buffer,
51+
const uint8_t *pub_key_buffer,
52+
const uint8_t *input, size_t input_length,
53+
uint8_t *signature)
54+
{
55+
uint8_t priv_and_pub_key[64];
56+
memcpy(&priv_and_pub_key[0], priv_key_buffer, 32);
57+
memcpy(&priv_and_pub_key[32], pub_key_buffer, 32);
58+
59+
crypto_ed25519_sign(signature, priv_and_pub_key, input, input_length);
60+
61+
explicit_bzero(priv_and_pub_key, 64);
62+
63+
return PSA_SUCCESS;
64+
}
65+
66+
psa_status_t psa_ecc_ed25519_verify_message(const uint8_t *pub_key_buffer,
67+
const uint8_t *input, size_t input_length,
68+
const uint8_t *signature)
69+
{
70+
if (!crypto_ed25519_check(signature, pub_key_buffer, input, input_length)) {
71+
return PSA_ERROR_INVALID_SIGNATURE;
72+
}
73+
74+
return PSA_SUCCESS;
75+
}

sys/psa_crypto/Makefile.dep

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ ifneq (,$(filter psa_asymmetric_ecc_ed25519,$(USEMODULE)))
7474
ifneq (,$(filter periph_ecc_ed25519,$(FEATURES_USED)))
7575
USEMODULE += psa_asymmetric_ecc_ed25519_backend_periph
7676
else
77-
USEMODULE += psa_asymmetric_ecc_ed25519_backend_c25519
77+
USEMODULE += psa_asymmetric_ecc_ed25519_backend_monocypher
7878
endif
7979
endif
8080
endif
@@ -85,6 +85,12 @@ ifneq (,$(filter psa_asymmetric_ecc_ed25519_backend_c25519,$(USEMODULE)))
8585
USEMODULE += psa_c25519_edsign
8686
endif
8787

88+
ifneq (,$(filter psa_asymmetric_ecc_ed25519_backend_monocypher,$(USEMODULE)))
89+
USEPKG += monocypher
90+
USEMODULE += psa_monocypher
91+
USEMODULE += psa_monocypher_ed25519
92+
endif
93+
8894
ifneq (,$(filter psa_asymmetric_ecc_ed25519_backend_periph,$(USEMODULE)))
8995
FEATURES_REQUIRED += periph_ecc_ed25519
9096
endif

sys/psa_crypto/Makefile.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ endif
3737
PSEUDOMODULES += psa_asymmetric_ecc_ed25519
3838
PSEUDOMODULES += psa_asymmetric_ecc_ed25519_backend_periph
3939
PSEUDOMODULES += psa_asymmetric_ecc_ed25519_backend_c25519
40+
PSEUDOMODULES += psa_asymmetric_ecc_ed25519_backend_monocypher
4041
PSEUDOMODULES += psa_asymmetric_ecc_ed25519_custom_backend
4142

4243
# check that one and only one backend has been selected

0 commit comments

Comments
 (0)