make crypto depending on ring optional

This commit is contained in:
Skgland
2026-04-25 21:17:48 +02:00
committed by Bennet Bleßmann
parent 61d94dceae
commit 938f588769
2 changed files with 362 additions and 245 deletions

View File

@@ -17,16 +17,25 @@ rust-version = "1.93.1"
crate-type = ["cdylib", "rlib"] crate-type = ["cdylib", "rlib"]
[features] [features]
default = ["ffi", "repl", "hostname", "tls", "http", "crypto-full"] default = ["all-simple-cross", "tls", "http"]
# activates all features that depend on no non pure-rust dependencies
#
# currently does not include
# ffi due to libffi
# tls, http due to openssl
# crypto-inpure due to ring
all-pure = ["repl", "hostname"]
# enables all features that are simple to get working for cross-compliation # enables all features that are simple to get working for cross-compliation
# currently all but tls, http as those depend on openssl # currently all but tls, http as those depend on openssl
all-simple-cross = ["ffi", "repl", "hostname", "crypto-full"] all-simple-cross = ["all-pure", "ffi", "crypto-full"]
ffi = ["dep:libffi"] ffi = ["dep:libffi"]
repl = ["dep:crossterm", "dep:ctrlc", "dep:rustyline"] repl = ["dep:crossterm", "dep:ctrlc", "dep:rustyline"]
hostname = ["dep:hostname"] hostname = ["dep:hostname"]
tls = ["dep:native-tls"] tls = ["dep:native-tls"]
http = ["dep:warp", "dep:reqwest"] http = ["dep:warp", "dep:reqwest"]
crypto-full = [] # crypto function that require non pure-rust dependencies
crypto-inpure = ["dep:ring"]
crypto-full = ["crypto-inpure"]
[lints.rust] [lints.rust]
unexpected_cfgs = { level = "deny", check-cfg = [ unexpected_cfgs = { level = "deny", check-cfg = [
@@ -73,7 +82,9 @@ ordered-float = "5.0.0"
phf = { version = "0.11", features = ["macros"] } phf = { version = "0.11", features = ["macros"] }
puruspe = "0.4.1" puruspe = "0.4.1"
rand = "0.8.5" rand = "0.8.5"
ring = { version = "0.17.8", features = ["wasm32_unknown_unknown_js"] } ring = { version = "0.17.8", features = [
"wasm32_unknown_unknown_js",
], optional = true }
ripemd = "0.1.3" ripemd = "0.1.3"
roxmltree = "0.20.0" roxmltree = "0.20.0"
ryu = "1.0.18" ryu = "1.0.18"

View File

@@ -53,7 +53,6 @@ use std::mem;
#[cfg(feature = "http")] #[cfg(feature = "http")]
use std::net::{SocketAddr, ToSocketAddrs}; use std::net::{SocketAddr, ToSocketAddrs};
use std::net::{TcpListener, TcpStream}; use std::net::{TcpListener, TcpStream};
use std::num::NonZeroU32;
use std::process; use std::process;
use std::process::Child; use std::process::Child;
use std::process::Stdio; use std::process::Stdio;
@@ -74,11 +73,15 @@ use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifier
use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use blake2::{Blake2b512, Blake2s256}; use blake2::{Blake2b512, Blake2s256};
use ring::rand::{SecureRandom, SystemRandom};
use ring::{digest, hkdf, hmac, pbkdf2};
#[cfg(feature = "crypto-full")] #[cfg(feature = "crypto-full")]
use ring::aead; use ring::aead;
#[cfg(feature = "crypto-inpure")]
use ring::{
digest, hkdf, hmac, pbkdf2,
rand::{SecureRandom, SystemRandom},
};
use ripemd::{Digest, Ripemd160}; use ripemd::{Digest, Ripemd160};
use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512}; use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512};
@@ -96,8 +99,6 @@ use roxmltree;
use futures::future; use futures::future;
#[cfg(feature = "http")] #[cfg(feature = "http")]
use reqwest::Url; use reqwest::Url;
use tokio::runtime::Handle;
use tokio::task;
#[cfg(feature = "http")] #[cfg(feature = "http")]
use warp::hyper::header::{HeaderName, HeaderValue}; use warp::hyper::header::{HeaderName, HeaderValue};
#[cfg(feature = "http")] #[cfg(feature = "http")]
@@ -4403,6 +4404,8 @@ impl Machine {
#[cfg(feature = "http")] #[cfg(feature = "http")]
#[inline(always)] #[inline(always)]
pub(crate) fn http_open(&mut self) -> CallResult { pub(crate) fn http_open(&mut self) -> CallResult {
use tokio::task;
let address_sink = self.deref_register(1); let address_sink = self.deref_register(1);
let method = read_heap_cell!(self.deref_register(3), let method = read_heap_cell!(self.deref_register(3),
(HeapCellValueTag::Atom, (name, arity)) => { (HeapCellValueTag::Atom, (name, arity)) => {
@@ -4466,7 +4469,7 @@ impl Machine {
// do it! // do it!
task::block_in_place(move || { task::block_in_place(move || {
match Handle::current().block_on(req.send()) { match tokio::runtime::Handle::current().block_on(req.send()) {
Ok(resp) => { Ok(resp) => {
// status code // status code
let status = resp.status().as_u16(); let status = resp.status().as_u16();
@@ -7843,6 +7846,8 @@ impl Machine {
#[inline(always)] #[inline(always)]
pub(crate) fn crypto_random_byte(&mut self) { pub(crate) fn crypto_random_byte(&mut self) {
#[cfg(feature = "crypto-inpure")]
{
let arg = self.machine_st.registers[1]; let arg = self.machine_st.registers[1];
let mut bytes: [u8; 1] = [0]; let mut bytes: [u8; 1] = [0];
@@ -7861,6 +7866,15 @@ impl Machine {
self.machine_st.unify_fixnum(byte, arg); self.machine_st.unify_fixnum(byte, arg);
} }
#[cfg(not(feature = "crypto-inpure"))]
{
let stub_gen = || functor_stub(atom!("crypto_random_byte"), 1);
let err = self.machine_st.missing_feature_error(atom!("crypto"));
let exception = self.machine_st.error_form(err, stub_gen());
self.machine_st.throw_exception(exception);
}
}
#[inline(always)] #[inline(always)]
pub(crate) fn crypto_data_hash(&mut self) { pub(crate) fn crypto_data_hash(&mut self) {
let encoding = cell_as_atom!(self.deref_register(2)); let encoding = cell_as_atom!(self.deref_register(2));
@@ -7990,6 +8004,8 @@ impl Machine {
) )
} }
_ => { _ => {
#[cfg(feature = "crypto-inpure")]
{
let ints = digest::digest( let ints = digest::digest(
match algorithm { match algorithm {
atom!("sha256") => &digest::SHA256, atom!("sha256") => &digest::SHA256,
@@ -8014,6 +8030,15 @@ impl Machine {
) )
) )
} }
#[cfg(not(feature = "crypto-inpure"))]
{
let stub_gen = || functor_stub(atom!("crypto_data_hash"), 1);
let err = self.machine_st.missing_feature_error(atom!("crypto"));
let exception = self.machine_st.error_form(err, stub_gen());
self.machine_st.throw_exception(exception);
return;
}
}
}; };
unify!(self.machine_st, self.machine_st.registers[3], ints_list); unify!(self.machine_st, self.machine_st.registers[3], ints_list);
@@ -8021,11 +8046,13 @@ impl Machine {
#[inline(always)] #[inline(always)]
pub(crate) fn crypto_hmac(&mut self) { pub(crate) fn crypto_hmac(&mut self) {
let stub_gen = || functor_stub(atom!("crypto_data_hash"), 3);
#[cfg(feature = "crypto-inpure")]
{
let encoding = cell_as_atom!(self.deref_register(2)); let encoding = cell_as_atom!(self.deref_register(2));
let data = self.string_encoding_bytes(self.machine_st.registers[1], encoding); let data = self.string_encoding_bytes(self.machine_st.registers[1], encoding);
let stub_gen = || functor_stub(atom!("crypto_data_hash"), 3);
let key = self let key = self
.machine_st .machine_st
.integers_to_bytevec(self.machine_st.registers[3], stub_gen); .integers_to_bytevec(self.machine_st.registers[3], stub_gen);
@@ -8057,12 +8084,23 @@ impl Machine {
unify!(self.machine_st, self.machine_st.registers[4], ints_list); unify!(self.machine_st, self.machine_st.registers[4], ints_list);
} }
#[cfg(not(feature = "crypto-inpure"))]
{
let err = self.machine_st.missing_feature_error(atom!("crypto"));
let exception = self.machine_st.error_form(err, stub_gen());
self.machine_st.throw_exception(exception);
}
}
#[inline(always)] #[inline(always)]
pub(crate) fn crypto_data_hkdf(&mut self) { pub(crate) fn crypto_data_hkdf(&mut self) {
let stub1_gen = || functor_stub(atom!("crypto_data_hkdf"), 4);
#[cfg(feature = "crypto-inpure")]
{
let encoding = cell_as_atom!(self.deref_register(2)); let encoding = cell_as_atom!(self.deref_register(2));
let data = self.string_encoding_bytes(self.machine_st.registers[1], encoding); let data = self.string_encoding_bytes(self.machine_st.registers[1], encoding);
let stub1_gen = || functor_stub(atom!("crypto_data_hkdf"), 4);
let salt = self let salt = self
.machine_st .machine_st
.integers_to_bytevec(self.machine_st.registers[3], stub1_gen); .integers_to_bytevec(self.machine_st.registers[3], stub1_gen);
@@ -8129,9 +8167,21 @@ impl Machine {
unify!(self.machine_st, self.machine_st.registers[7], ints_list); unify!(self.machine_st, self.machine_st.registers[7], ints_list);
} }
#[cfg(not(feature = "crypto-inpure"))]
{
let err = self.machine_st.missing_feature_error(atom!("crypto"));
let exception = self.machine_st.error_form(err, stub1_gen());
self.machine_st.throw_exception(exception);
}
}
#[inline(always)] #[inline(always)]
pub(crate) fn crypto_password_hash(&mut self) { pub(crate) fn crypto_password_hash(&mut self) {
let stub1_gen = || functor_stub(atom!("crypto_password_hash"), 3); let stub1_gen = || functor_stub(atom!("crypto_password_hash"), 3);
#[cfg(feature = "crypto-inpure")]
{
use std::num::NonZeroU32;
let data = self let data = self
.machine_st .machine_st
.integers_to_bytevec(self.machine_st.registers[1], stub1_gen); .integers_to_bytevec(self.machine_st.registers[1], stub1_gen);
@@ -8185,6 +8235,14 @@ impl Machine {
unify!(self.machine_st, self.machine_st.registers[4], ints_list); unify!(self.machine_st, self.machine_st.registers[4], ints_list);
} }
#[cfg(not(feature = "crypto-inpure"))]
{
let err = self.machine_st.missing_feature_error(atom!("crypto"));
let exception = self.machine_st.error_form(err, stub1_gen());
self.machine_st.throw_exception(exception);
}
}
#[cfg(feature = "crypto-full")] #[cfg(feature = "crypto-full")]
#[inline(always)] #[inline(always)]
pub(crate) fn crypto_data_encrypt(&mut self) { pub(crate) fn crypto_data_encrypt(&mut self) {
@@ -8304,6 +8362,8 @@ impl Machine {
#[inline(always)] #[inline(always)]
pub(crate) fn crypto_curve_scalar_mult(&mut self) { pub(crate) fn crypto_curve_scalar_mult(&mut self) {
let stub_gen = || functor_stub(atom!("crypto_curve_scalar_mult"), 4); let stub_gen = || functor_stub(atom!("crypto_curve_scalar_mult"), 4);
{
let scalar_bytes = self let scalar_bytes = self
.machine_st .machine_st
.integers_to_bytevec(self.machine_st.registers[2], stub_gen); .integers_to_bytevec(self.machine_st.registers[2], stub_gen);
@@ -8323,9 +8383,18 @@ impl Machine {
unify!(self.machine_st, self.machine_st.registers[4], uncompressed); unify!(self.machine_st, self.machine_st.registers[4], uncompressed);
} }
{
let err = self.machine_st.missing_feature_error(atom!("crypto"));
let exception = self.machine_st.error_form(err, stub_gen());
self.machine_st.throw_exception(exception);
}
}
#[inline(always)] #[inline(always)]
pub(crate) fn ed25519_seed_to_public_key(&mut self) { pub(crate) fn ed25519_seed_to_public_key(&mut self) {
let stub_gen = || functor_stub(atom!("ed25519_seed_keypair"), 2); let stub_gen = || functor_stub(atom!("ed25519_seed_keypair"), 2);
{
let seed_bytes = self let seed_bytes = self
.machine_st .machine_st
.integers_to_bytevec(self.machine_st.registers[1], stub_gen); .integers_to_bytevec(self.machine_st.registers[1], stub_gen);
@@ -8344,9 +8413,18 @@ impl Machine {
); );
} }
{
let err = self.machine_st.missing_feature_error(atom!("crypto"));
let exception = self.machine_st.error_form(err, stub_gen());
self.machine_st.throw_exception(exception);
}
}
#[inline(always)] #[inline(always)]
pub(crate) fn ed25519_sign_raw(&mut self) { pub(crate) fn ed25519_sign_raw(&mut self) {
let stub_gen = || functor_stub(atom!("ed25519_sign"), 4); let stub_gen = || functor_stub(atom!("ed25519_sign"), 4);
{
let seed_bytes = self let seed_bytes = self
.machine_st .machine_st
.integers_to_bytevec(self.machine_st.registers[1], stub_gen); .integers_to_bytevec(self.machine_st.registers[1], stub_gen);
@@ -8371,16 +8449,25 @@ impl Machine {
unify!(self.machine_st, self.machine_st.registers[4], sig_list); unify!(self.machine_st, self.machine_st.registers[4], sig_list);
} }
{
let err = self.machine_st.missing_feature_error(atom!("crypto"));
let exception = self.machine_st.error_form(err, stub_gen());
self.machine_st.throw_exception(exception);
}
}
#[inline(always)] #[inline(always)]
pub(crate) fn ed25519_verify_raw(&mut self) { pub(crate) fn ed25519_verify_raw(&mut self) {
let key_bytes = self.string_encoding_bytes(self.machine_st.registers[1], atom!("octet")); let stub_gen = || functor_stub(atom!("ed25519_verify"), 4);
{
let key_bytes =
self.string_encoding_bytes(self.machine_st.registers[1], atom!("octet"));
let pkey = ed25519::PublicKey::decode(&key_bytes).unwrap(); let pkey = ed25519::PublicKey::decode(&key_bytes).unwrap();
let encoding = cell_as_atom!(self.deref_register(3)); let encoding = cell_as_atom!(self.deref_register(3));
let data = self.string_encoding_bytes(self.machine_st.registers[2], encoding); let data = self.string_encoding_bytes(self.machine_st.registers[2], encoding);
let stub_gen = || functor_stub(atom!("ed25519_verify"), 4);
let signature = self let signature = self
.machine_st .machine_st
.integers_to_bytevec(self.machine_st.registers[4], stub_gen); .integers_to_bytevec(self.machine_st.registers[4], stub_gen);
@@ -8388,16 +8475,24 @@ impl Machine {
self.machine_st.fail = !pkey.verify_raw(&signature, &data); self.machine_st.fail = !pkey.verify_raw(&signature, &data);
} }
{
let err = self.machine_st.missing_feature_error(atom!("crypto"));
let exception = self.machine_st.error_form(err, stub_gen());
self.machine_st.throw_exception(exception);
}
}
#[inline(always)] #[inline(always)]
pub(crate) fn curve25519_scalar_mult(&mut self) { pub(crate) fn curve25519_scalar_mult(&mut self) {
let stub1_gen = || functor_stub(atom!("curve25519_scalar_mult"), 3); let stub_gen = || functor_stub(atom!("curve25519_scalar_mult"), 3);
{
let scalar_bytes = self let scalar_bytes = self
.machine_st .machine_st
.integers_to_bytevec(self.machine_st.registers[1], stub1_gen); .integers_to_bytevec(self.machine_st.registers[1], stub_gen);
let stub2_gen = || functor_stub(atom!("curve25519_scalar_mult"), 3);
let point_bytes = self let point_bytes = self
.machine_st .machine_st
.integers_to_bytevec(self.machine_st.registers[2], stub2_gen); .integers_to_bytevec(self.machine_st.registers[2], stub_gen);
let result = x25519::x25519( let result = x25519::x25519(
&<[u8; 32]>::try_from(&point_bytes[..]).unwrap(), &<[u8; 32]>::try_from(&point_bytes[..]).unwrap(),
@@ -8409,6 +8504,13 @@ impl Machine {
unify!(self.machine_st, self.machine_st.registers[3], string); unify!(self.machine_st, self.machine_st.registers[3], string);
} }
{
let err = self.machine_st.missing_feature_error(atom!("crypto"));
let exception = self.machine_st.error_form(err, stub_gen());
self.machine_st.throw_exception(exception);
}
}
#[inline(always)] #[inline(always)]
pub(crate) fn first_non_octet(&mut self) { pub(crate) fn first_non_octet(&mut self) {
let addr = self.deref_register(1); let addr = self.deref_register(1);
@@ -9509,7 +9611,9 @@ impl Machine {
} }
} }
#[cfg(feature = "crypto-inpure")]
fn rng() -> &'static dyn SecureRandom { fn rng() -> &'static dyn SecureRandom {
use lazy_static::lazy_static;
use std::ops::Deref; use std::ops::Deref;
static RANDOM: LazyLock<SystemRandom> = LazyLock::new(SystemRandom::new); static RANDOM: LazyLock<SystemRandom> = LazyLock::new(SystemRandom::new);
@@ -9517,8 +9621,10 @@ fn rng() -> &'static dyn SecureRandom {
RANDOM.deref() RANDOM.deref()
} }
#[cfg(feature = "crypto-inpure")]
struct MyKey<T: core::fmt::Debug + PartialEq>(T); struct MyKey<T: core::fmt::Debug + PartialEq>(T);
#[cfg(feature = "crypto-inpure")]
impl hkdf::KeyType for MyKey<usize> { impl hkdf::KeyType for MyKey<usize> {
fn len(&self) -> usize { fn len(&self) -> usize {
self.0 self.0