diff --git a/src/commons/util/ext_serde.rs b/src/commons/util/ext_serde.rs index 623fff77..7891eb8e 100644 --- a/src/commons/util/ext_serde.rs +++ b/src/commons/util/ext_serde.rs @@ -1,5 +1,5 @@ //! Defines helper methods for Serializing and Deserializing external types. -use std::str::FromStr; +use std::{str::FromStr, sync::atomic::{AtomicU64, Ordering}}; use bytes::Bytes; use log::LevelFilter; @@ -162,3 +162,22 @@ where let string = String::deserialize(d)?; Facility::from_str(&string).map_err(|_| de::Error::custom(format!("Unsupported syslog_facility: \"{}\"", string))) } + +//------------- AtomicU64 ----------------------------------------------------- +// Implemented automatically by Serde derive but only for x86_64 architectures, +// for other architectures (such as armv7 for the Raspberry Pi 4b) it has to be +// implemented manually. + +pub fn de_atomicu64<'de, D>(d: D) -> Result +where + D: Deserializer<'de>, +{ + Ok(AtomicU64::new(u64::deserialize(d)?)) +} + +pub fn ser_atomicu64(v: &AtomicU64, s: S) -> Result +where + S: Serializer, +{ + s.serialize_u64(v.load(Ordering::SeqCst)) +} \ No newline at end of file diff --git a/src/daemon/auth/common/crypt.rs b/src/daemon/auth/common/crypt.rs index 4d18f3ee..0bb5be36 100644 --- a/src/daemon/auth/common/crypt.rs +++ b/src/daemon/auth/common/crypt.rs @@ -27,6 +27,7 @@ use std::{ use crate::commons::{ error::{Error, KrillIoError}, KrillResult, + util::ext_serde, }; const CHACHA20_KEY_BIT_LEN: usize = 256; @@ -41,6 +42,11 @@ const UNUSED_AAD: [u8; 0] = [0; 0]; #[derive(Debug, Deserialize, Serialize)] pub struct NonceState { sender_unique: [u8; 4], // 32 bits + + #[serde( + deserialize_with = "ext_serde::de_atomicu64", + serialize_with = "ext_serde::ser_atomicu64" + )] counter: AtomicU64, // + 64 bits = 96 bits = CHACHA20_NONCE_BIT_LEN }