From d7f385f2fd28c6414b20a7e3090a0507875b9ba6 Mon Sep 17 00:00:00 2001 From: Vincent Autefage Date: Thu, 29 Aug 2024 12:36:14 +0200 Subject: [PATCH] add a noise_cipherstate_get_nonce test function --- include/noise/protocol/cipherstate.h | 1 + src/protocol/cipherstate.c | 29 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/noise/protocol/cipherstate.h b/include/noise/protocol/cipherstate.h index 0b64973a..c4d75f1e 100644 --- a/include/noise/protocol/cipherstate.h +++ b/include/noise/protocol/cipherstate.h @@ -49,6 +49,7 @@ int noise_cipherstate_decrypt_with_ad int noise_cipherstate_encrypt(NoiseCipherState *state, NoiseBuffer *buffer); int noise_cipherstate_decrypt(NoiseCipherState *state, NoiseBuffer *buffer); int noise_cipherstate_set_nonce(NoiseCipherState *state, uint64_t nonce); +int noise_cipherstate_get_nonce(const NoiseCipherState *state, uint64_t *nonce); int noise_cipherstate_get_max_key_length(void); int noise_cipherstate_get_max_mac_length(void); diff --git a/src/protocol/cipherstate.c b/src/protocol/cipherstate.c index 2f484388..9dc43f7d 100644 --- a/src/protocol/cipherstate.c +++ b/src/protocol/cipherstate.c @@ -534,6 +534,35 @@ int noise_cipherstate_set_nonce(NoiseCipherState *state, uint64_t nonce) return NOISE_ERROR_NONE; } +/** + * \brief Gets the nonce value for this cipherstate object. + * + * \param state The CipherState object. + * \param nonce The nonce value to get. + * + * \return NOISE_ERROR_NONE on success. + * \return NOISE_ERROR_INVALID_PARAM if \a state or \a nonce is NULL. + * \return NOISE_ERROR_INVALID_STATE if the key has not been set yet. + * + * \warning This function is intended for testing purposes only. + * + * \sa noise_cipherstate_init_key() + */ +int noise_cipherstate_get_nonce(const NoiseCipherState *state, uint64_t * nonce) +{ + /* Bail out if the state or the nonce is NULL */ + if (!state || !nonce) + return NOISE_ERROR_INVALID_PARAM; + + /* If the key hasn't been set yet, we cannot do this */ + if (!state->has_key) + return NOISE_ERROR_INVALID_STATE; + + /* Set the nonce param and return */ + *nonce = state->n; + return NOISE_ERROR_NONE; +} + /** * \brief Gets the maximum key length for the supported algorithms. *