|
| 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 | +} |
0 commit comments