Skip to content

Commit

Permalink
Merge pull request #659 from cryspen/keks/hacl-rs
Browse files Browse the repository at this point in the history
Back implementations of SHA2, HMAC-SHA1, HMAC-SHA2 and HKDF-SHA2 by hacl-rs
  • Loading branch information
keks authored Nov 14, 2024
2 parents 9b4b799 + 7584c13 commit 04fdf14
Show file tree
Hide file tree
Showing 72 changed files with 18,414 additions and 654 deletions.
73 changes: 70 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ members = [
"libcrux-hkdf",
"libcrux-ecdh",
"libcrux-psq",
"macros",
"cavp",
"traits",
"sha2",
"ed25519",
"curve25519",
]

[workspace.package]
Expand Down Expand Up @@ -63,10 +68,14 @@ bench = false # so libtest doesn't eat the argumen
libcrux-platform = { version = "=0.0.2-beta.2", path = "sys/platform" }

[dependencies]
libcrux-traits = { version = "=0.0.2-beta.2", path = "traits" }
libcrux-hacl-rs = { version = "=0.0.2-beta.2", path = "hacl-rs" }
libcrux-hacl = { version = "=0.0.2-beta.2", path = "sys/hacl" }
libcrux-platform = { version = "=0.0.2-beta.2", path = "sys/platform" }
libcrux-hkdf = { version = "=0.0.2-beta.2", path = "libcrux-hkdf" }
libcrux-hmac = { version = "=0.0.2-beta.2", path = "libcrux-hmac" }
libcrux-sha2 = { version = "=0.0.2-beta.2", path = "sha2" }
libcrux-ed25519 = { version = "=0.0.2-beta.2", path = "ed25519" }
libcrux-ecdh = { version = "=0.0.2-beta.2", path = "libcrux-ecdh" }
libcrux-ml-kem = { version = "=0.0.2-beta.2", path = "libcrux-ml-kem" }
libcrux-kem = { version = "=0.0.2-beta.2", path = "libcrux-kem" }
Expand All @@ -93,6 +102,7 @@ serde_json = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
hex = { version = "0.4.3", features = ["serde"] }
clap = { version = "4.5", features = ["derive"] }
wycheproof = "0.6.0"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3"
Expand Down
23 changes: 23 additions & 0 deletions curve25519/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "libcrux-curve25519"
description = "Formally verified curve25519 ECDH library"

version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
edition.workspace = true
repository.workspace = true
readme.workspace = true

[features]
default = ["portable_hacl"]
portable_hacl = ["hacl"]
hacl = ["dep:libcrux-sha2", "dep:libcrux-hacl-rs", "dep:libcrux-macros"]

[dependencies]
libcrux-hacl-rs = { version = "=0.0.2-beta.2", path = "../hacl-rs/", optional = true }
libcrux-sha2 = { version = "=0.0.2-beta.2", path = "../sha2", optional = true, features = [
"hacl",
] }
libcrux-macros = { version = "=0.0.2-beta.2", path = "../macros", optional = true }
21 changes: 21 additions & 0 deletions curve25519/src/impl_hacl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use super::*;

/// Implementation of Curve25519 backed by Hacl.
pub struct HaclCurve25519;

impl Curve25519 for HaclCurve25519 {
// The hacl::ecdh function requires all parameters to be 32 byte long, which we enforce using
// types.
fn secret_to_public(pk: &mut [u8; PK_LEN], sk: &[u8; SK_LEN]) {
crate::hacl::secret_to_public(pk, sk)
}

// The hacl::ecdh function requires all parameters to be 32 byte long, which we enforce using
// types.
fn ecdh(out: &mut [u8; SHK_LEN], pk: &[u8; PK_LEN], sk: &[u8; SK_LEN]) -> Result<(), Error> {
match crate::hacl::ecdh(out, sk, pk) {
true => Ok(()),
false => Err(Error),
}
}
}
41 changes: 41 additions & 0 deletions curve25519/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#[cfg(feature = "hacl")]
pub use libcrux_hacl_rs::curve25519_51 as hacl;

#[cfg(feature = "hacl")]
mod impl_hacl;

#[cfg(feature = "portable_hacl")]
pub use impl_hacl::HaclCurve25519 as Impl;

/// The length of Curve25519 secret keys.
pub const SK_LEN: usize = 32;

/// The length of Curve25519 public keys.
pub const PK_LEN: usize = 32;

/// The length of Curve25519 shared keys.
pub const SHK_LEN: usize = 32;

/// Indicates that an error occurred
pub struct Error;

/// This trait is implemented by the backing implementations.
/// Only used for implementation agility.
trait Curve25519 {
/// Computes a public key from a secret key.
fn secret_to_public(pk: &mut [u8; PK_LEN], sk: &[u8; SK_LEN]);

/// Computes the scalar multiplication between the provided public and secret keys. Returns an
/// error if the result is 0.
fn ecdh(out: &mut [u8; SHK_LEN], pk: &[u8; PK_LEN], sk: &[u8; SK_LEN]) -> Result<(), Error>;
}

/// Computes and writes the public key from the secret key `sk` and writes it into `pk`.
pub fn secret_to_public(pk: &mut [u8; PK_LEN], sk: &[u8; SK_LEN]) {
Impl::secret_to_public(pk, sk)
}

/// Performs the ECDH computation and writes the key shared betweem `pk` and `sk` into `shk`.
pub fn ecdh(out: &mut [u8; SHK_LEN], pk: &[u8; PK_LEN], sk: &[u8; SK_LEN]) -> Result<(), Error> {
Impl::ecdh(out, pk, sk)
}
23 changes: 23 additions & 0 deletions ed25519/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "libcrux-ed25519"
description = "Formally verified ed25519 signature library"

version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
edition.workspace = true
repository.workspace = true
readme.workspace = true

[features]
default = ["portable_hacl"]
portable_hacl = ["hacl"]
hacl = ["dep:libcrux-sha2", "dep:libcrux-hacl-rs", "dep:libcrux-macros"]

[dependencies]
libcrux-hacl-rs = { version = "=0.0.2-beta.2", path = "../hacl-rs/", optional = true }
libcrux-sha2 = { version = "=0.0.2-beta.2", path = "../sha2", optional = true, features = [
"hacl",
] }
libcrux-macros = { version = "=0.0.2-beta.2", path = "../macros", optional = true }
Loading

0 comments on commit 04fdf14

Please sign in to comment.