Skip to content

Commit

Permalink
Merge pull request #737 from cryspen/jonas/no-std
Browse files Browse the repository at this point in the history
`no_std` support (via `alloc`)
  • Loading branch information
jschneider-bensch authored Jan 14, 2025
2 parents c54a7d6 + 01eb0aa commit 91eec18
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ wasm-bindgen-test = "0.3"
getrandom = { version = "0.2", features = ["js"] }

[features]
default = ["std"]
std = []
hacspec = [] # TODO: #7 Use specs instead of efficient implementations
rand = []
wasm = ["wasm-bindgen", "getrandom"]
Expand Down
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ libcrux uses the following configurations for its hardware abstractions
libcrux provides a DRBG implementation that can be used standalone (`drbg::Drbg`)
or through the `Rng` traits.

## `no_std` support
`libcrux` and the individual primitive crates it depends on support
`no_std` environments given a global allocator for the target
platform.

## Verification status

As a quick indicator of overall verification status, subcrates in this workspace include the following badges:
Expand Down
2 changes: 2 additions & 0 deletions src/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::hacl::chacha20_poly1305;

use libcrux_platform::{aes_ni_support, simd128_support, simd256_support};

use crate::std::vec::Vec;

/// The caller has provided an invalid argument.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum InvalidArgumentError {
Expand Down
2 changes: 2 additions & 0 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::hacl::{blake2, sha3};

use libcrux_platform::{simd128_support, simd256_support};

use crate::std::vec::Vec;

#[derive(Debug)]
pub enum Error {
InvalidStateFinished,
Expand Down
8 changes: 5 additions & 3 deletions src/drbg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::hacl::drbg;
// re-export here for convenience
pub use rand::{CryptoRng, RngCore};

use crate::std::{fmt, vec, vec::Vec};

#[derive(Debug)]
pub enum Error {
/// Invalid input.
Expand All @@ -16,13 +18,13 @@ pub enum Error {
UnableToGenerate,
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("{self:?}"))
}
}

impl std::error::Error for Error {}
impl core::error::Error for Error {}

pub struct Drbg {
state: drbg::Drbg,
Expand Down
4 changes: 2 additions & 2 deletions src/hacl/aesgcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ macro_rules! implement {
let mut tag = Tag::default();
hardware_support()?;
let ok = unsafe {
let mut state_ptr: *mut EverCrypt_AEAD_state_s = std::ptr::null_mut();
let mut state_ptr: *mut EverCrypt_AEAD_state_s = core::ptr::null_mut();
let e = EverCrypt_AEAD_create_in($alg as u8, &mut state_ptr, key.as_ptr() as _);
if e != 0 {
return Err(Error::InvalidArgument);
Expand Down Expand Up @@ -99,7 +99,7 @@ macro_rules! implement {
) -> Result<(), Error> {
hardware_support()?;
let ok = unsafe {
let mut state_ptr: *mut EverCrypt_AEAD_state_s = std::ptr::null_mut();
let mut state_ptr: *mut EverCrypt_AEAD_state_s = core::ptr::null_mut();
let e = EverCrypt_AEAD_create_in($alg as u8, &mut state_ptr, key.as_ptr() as _);
if e != 0 {
return Err(Error::UnsupportedHardware);
Expand Down
4 changes: 2 additions & 2 deletions src/hacl/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub mod x4 {
/// bytes in increments.
/// TODO: This module should not be public, see: https://github.com/cryspen/libcrux/issues/157
pub mod incremental {
use std::ptr::null_mut;
use core::ptr::null_mut;

use libcrux_hacl::{
Hacl_Hash_SHA3_Scalar_shake128_absorb_final, Hacl_Hash_SHA3_Scalar_shake128_absorb_nblocks,
Expand Down Expand Up @@ -322,7 +322,7 @@ pub mod incremental {
}

pub mod incremental_x4 {
use std::ptr::null_mut;
use core::ptr::null_mut;

use libcrux_hacl::{
Hacl_Hash_SHA3_Scalar_shake128_absorb_final, Hacl_Hash_SHA3_Scalar_shake128_absorb_nblocks,
Expand Down
2 changes: 2 additions & 0 deletions src/hpke/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ use crate::{
hmac::tag_size,
};

use crate::std::{vec, vec::Vec};

use super::errors::*;

type AeadAlgResult = Result<Algorithm, HpkeError>;
Expand Down
1 change: 1 addition & 0 deletions src/hpke/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
//! - `CryptoError`: An opaque error happened in a crypto operation outside of this code.
use crate::aead::InvalidArgumentError;
use crate::std::vec::Vec;

/// Explicit errors generated throughout this specification.
#[derive(Debug, Copy, Clone, PartialEq)]
Expand Down
2 changes: 2 additions & 0 deletions src/hpke/hpke.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(non_camel_case_types, non_snake_case, unused_imports)]

use crate::std::{vec, vec::Vec};

use libcrux_ecdh::{self, secret_to_public, x25519_derive, X25519PublicKey};
use libcrux_ml_kem::mlkem768;

Expand Down
3 changes: 2 additions & 1 deletion src/hpke/kdf.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![doc = include_str!("KDF_Readme.md")]
#![allow(non_snake_case, non_camel_case_types)]

use super::errors::*;
use crate::hkdf::Algorithm;

use super::errors::*;
use crate::std::{vec, vec::Vec};

/// ## Key Derivation Functions (KDFs)
///
Expand Down
2 changes: 2 additions & 0 deletions src/hpke/kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use libcrux_ecdh::{X25519PrivateKey, X25519PublicKey};

use crate::std::{vec, vec::Vec};

use super::errors::*;
use super::kdf::*;
use libcrux_kem::{
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
//!
//! The unified, formally verified, cryptography library.
#![no_std]

#[cfg(feature = "std")]
extern crate std;

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc as std;

pub use libcrux_platform::aes_ni_support;

// Jasmin
Expand Down
4 changes: 3 additions & 1 deletion src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//! * EdDSA 25519
//! * RSA PSS
use crate::std::{vec, vec::Vec};

use crate::{
ecdh,
hacl::{self, ed25519},
Expand Down Expand Up @@ -84,7 +86,7 @@ pub mod rsa_pss {
Hacl_RSAPSS_rsapss_sign, Hacl_RSAPSS_rsapss_verify,
};

use super::{DigestAlgorithm, Error};
use super::{vec, DigestAlgorithm, Error, Vec};

/// A [`Algorithm::RsaPss`](super::Algorithm::RsaPss) Signature
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down

0 comments on commit 91eec18

Please sign in to comment.