chore: Move sodium init integration into rosenpass-sodium crate

This commit is contained in:
Karolin Varner
2023-11-17 16:47:25 +01:00
committed by Karolin Varner
parent 46156fcb29
commit 99634d9702
9 changed files with 64 additions and 17 deletions

11
Cargo.lock generated
View File

@@ -1025,6 +1025,7 @@ dependencies = [
"oqs-sys", "oqs-sys",
"paste", "paste",
"rosenpass-constant-time", "rosenpass-constant-time",
"rosenpass-sodium",
"rosenpass-util", "rosenpass-util",
"serde", "serde",
"stacker", "stacker",
@@ -1038,6 +1039,16 @@ dependencies = [
name = "rosenpass-constant-time" name = "rosenpass-constant-time"
version = "0.1.0" version = "0.1.0"
[[package]]
name = "rosenpass-sodium"
version = "0.1.0"
dependencies = [
"anyhow",
"libsodium-sys-stable",
"log",
"rosenpass-util",
]
[[package]] [[package]]
name = "rosenpass-util" name = "rosenpass-util"
version = "0.1.0" version = "0.1.0"

View File

@@ -5,6 +5,7 @@ members = [
"rosenpass", "rosenpass",
"rosenpass-util", "rosenpass-util",
"rosenpass-constant-time", "rosenpass-constant-time",
"rosenpass-sodium",
] ]
[workspace.metadata.release] [workspace.metadata.release]

View File

@@ -0,0 +1,16 @@
[package]
name = "rosenpass-sodium"
authors = ["Karolin Varner <karo@cupdev.net>", "wucke13 <wucke13@gmail.com>"]
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Rosenpass internal bindings to libsodium"
homepage = "https://rosenpass.eu/"
repository = "https://github.com/rosenpass/rosenpass"
readme = "readme.md"
[dependencies]
rosenpass-util = { path = "../rosenpass-util" }
anyhow = { version = "1.0.71", features = ["backtrace"] }
libsodium-sys-stable = { version = "1.19.28", features = ["use-pkg-config"] }
log = { version = "0.4.17" }

View File

@@ -0,0 +1,5 @@
# Rosenpass internal libsodium bindings
Rosenpass internal library providing bindings to libsodium.
This is an internal library; not guarantee is made about its API at this point in time.

View File

@@ -0,0 +1,16 @@
use libsodium_sys as libsodium;
macro_rules! sodium_call {
($name:ident, $($args:expr),*) => { ::rosenpass_util::attempt!({
anyhow::ensure!(unsafe{libsodium::$name($($args),*)} > -1,
"Error in libsodium's {}.", stringify!($name));
Ok(())
})};
($name:ident) => { sodium_call!($name, ) };
}
#[inline]
pub fn init() -> anyhow::Result<()> {
log::trace!("initializing libsodium");
sodium_call!(sodium_init)
}

View File

@@ -16,6 +16,7 @@ harness = false
[dependencies] [dependencies]
rosenpass-util = { path = "../rosenpass-util" } rosenpass-util = { path = "../rosenpass-util" }
rosenpass-constant-time = { path = "../rosenpass-constant-time" } rosenpass-constant-time = { path = "../rosenpass-constant-time" }
rosenpass-sodium = { path = "../rosenpass-sodium" }
anyhow = { version = "1.0.71", features = ["backtrace"] } anyhow = { version = "1.0.71", features = ["backtrace"] }
static_assertions = "1.1.0" static_assertions = "1.1.0"
memoffset = "0.9.0" memoffset = "0.9.0"
@@ -24,8 +25,8 @@ oqs-sys = { version = "0.8", default-features = false, features = ['classic_mcel
lazy_static = "1.4.0" lazy_static = "1.4.0"
thiserror = "1.0.40" thiserror = "1.0.40"
paste = "1.0.12" paste = "1.0.12"
log = { version = "0.4.17", optional = true } log = { version = "0.4.17" }
env_logger = { version = "0.10.0", optional = true } env_logger = { version = "0.10.0" }
serde = { version = "1.0.163", features = ["derive"] } serde = { version = "1.0.163", features = ["derive"] }
toml = "0.7.4" toml = "0.7.4"
clap = { version = "4.3.0", features = ["derive"] } clap = { version = "4.3.0", features = ["derive"] }
@@ -38,6 +39,3 @@ anyhow = "1.0.71"
criterion = "0.4.0" criterion = "0.4.0"
test_bin = "0.4.0" test_bin = "0.4.0"
stacker = "0.1.15" stacker = "0.1.15"
[features]
default = ["log", "env_logger"]

View File

@@ -1,11 +1,18 @@
use log::error; use log::error;
use rosenpass::{cli::Cli, sodium::sodium_init}; use rosenpass::cli::Cli;
use rosenpass_util::attempt;
use std::process::exit; use std::process::exit;
/// Catches errors, prints them through the logger, then exits /// Catches errors, prints them through the logger, then exits
pub fn main() { pub fn main() {
env_logger::init(); env_logger::init();
match sodium_init().and_then(|()| Cli::run()) {
let res = attempt!({
rosenpass_sodium::init()?;
Cli::run()
});
match res {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
error!("{e}"); error!("{e}");

View File

@@ -25,8 +25,8 @@
//! }; //! };
//! # fn main() -> anyhow::Result<()> { //! # fn main() -> anyhow::Result<()> {
//! //!
//! // always init libsodium before anything //! // always initialize libsodium before anything
//! rosenpass::sodium::sodium_init()?; //! rosenpass_sodium::init()?;
//! //!
//! // initialize secret and public key for peer a ... //! // initialize secret and public key for peer a ...
//! let (mut peer_a_sk, mut peer_a_pk) = (SSk::zero(), SPk::zero()); //! let (mut peer_a_sk, mut peer_a_pk) = (SSk::zero(), SPk::zero());
@@ -1750,7 +1750,7 @@ mod test {
/// Through all this, the handshake should still successfully terminate; /// Through all this, the handshake should still successfully terminate;
/// i.e. an exchanged key must be produced in both servers. /// i.e. an exchanged key must be produced in both servers.
fn handles_incorrect_size_messages() { fn handles_incorrect_size_messages() {
crate::sodium::sodium_init().unwrap(); rosenpass_sodium::init().unwrap();
stacker::grow(8 * 1024 * 1024, || { stacker::grow(8 * 1024 * 1024, || {
const OVERSIZED_MESSAGE: usize = ((MAX_MESSAGE_LEN as f32) * 1.2) as usize; const OVERSIZED_MESSAGE: usize = ((MAX_MESSAGE_LEN as f32) * 1.2) as usize;

View File

@@ -2,7 +2,6 @@
use anyhow::{ensure, Result}; use anyhow::{ensure, Result};
use libsodium_sys as libsodium; use libsodium_sys as libsodium;
use log::trace;
use rosenpass_constant_time::xor_into; use rosenpass_constant_time::xor_into;
use rosenpass_util::attempt; use rosenpass_util::attempt;
use static_assertions::const_assert_eq; use static_assertions::const_assert_eq;
@@ -34,12 +33,6 @@ macro_rules! sodium_call {
($name:ident) => { sodium_call!($name, ) }; ($name:ident) => { sodium_call!($name, ) };
} }
#[inline]
pub fn sodium_init() -> Result<()> {
trace!("initializing libsodium");
sodium_call!(sodium_init)
}
#[inline] #[inline]
pub fn sodium_memcmp(a: &[u8], b: &[u8]) -> bool { pub fn sodium_memcmp(a: &[u8], b: &[u8]) -> bool {
a.len() == b.len() a.len() == b.len()