Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions boring-sys/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,20 +755,26 @@ fn generate_bindings(config: &Config) {
bindings
.write(Box::new(&mut source_code))
.expect("Couldn't serialize bindings!");
let mut source_code = String::from_utf8_lossy(&source_code).into_owned();

if !source_code.contains("MLKEM768_encap") {
// Fail at runtime to allow boring v5 to compile with mlkem disabled
source_code.push_str("\n#[cfg(not(feature = \"mlkem\"))] #[allow(deprecated)] pub use crate::mlkem_dummy::*;\n");
}

ensure_err_lib_enum_is_named(&mut source_code);
fs::write(config.out_dir.join("bindings.rs"), source_code).expect("Couldn't write bindings!");
fs::write(config.out_dir.join("bindings.rs"), source_code.as_bytes())
.expect("Couldn't write bindings!");
}

/// err.h has anonymous `enum { ERR_LIB_NONE = 1 }`, which makes a dodgy `_bindgen_ty_1` name
fn ensure_err_lib_enum_is_named(source_code: &mut Vec<u8>) {
let src = String::from_utf8_lossy(source_code);
let enum_type = src
fn ensure_err_lib_enum_is_named(source_code: &mut String) {
let enum_type = source_code
.split_once("ERR_LIB_SSL:")
.and_then(|(_, def)| Some(def.split_once('=')?.0))
.unwrap_or("_bindgen_ty_1");

source_code.extend_from_slice(
format!("\n/// Newtype for [`ERR_LIB_SSL`] constants\npub use {enum_type} as ErrLib;\n")
.as_bytes(),
);
let ty =
format!("\n/// Newtype for [`ERR_LIB_SSL`] constants\npub use {enum_type} as ErrLib;\n");
source_code.push_str(&ty);
}
3 changes: 3 additions & 0 deletions boring-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use std::convert::TryInto;
use std::ffi::c_void;
use std::os::raw::{c_char, c_int, c_uint, c_ulong};

#[cfg(not(feature = "mlkem"))]
mod mlkem_dummy;

#[allow(
clippy::useless_transmute,
clippy::derive_partial_eq_without_eq,
Expand Down
184 changes: 184 additions & 0 deletions boring-sys/src/mlkem_dummy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
#![allow(unused, deprecated)]

use crate::ERR_put_error;
use crate::CBB;
use crate::CBS;
use crate::ERR_R_FATAL;

#[derive(Copy, Clone, Default)]
pub struct UnimplementedPlaceholder {
pub bytes: [u8; 0],
}

#[derive(Copy, Clone, Default)]
#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub struct MLKEM768_private_key {
pub opaque: UnimplementedPlaceholder,
}

#[derive(Copy, Clone, Default)]
#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub struct MLKEM768_public_key;
#[derive(Copy, Clone, Default)]
#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub struct MLKEM1024_private_key {
pub opaque: UnimplementedPlaceholder,
}
#[derive(Copy, Clone, Default)]
#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub struct MLKEM1024_public_key;

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub const MLKEM_SEED_BYTES: u32 = 0;
#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub const MLKEM_SHARED_SECRET_BYTES: u32 = 0;
#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub const MLKEM768_PUBLIC_KEY_BYTES: u32 = 0;
#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub const MLKEM1024_PUBLIC_KEY_BYTES: u32 = 0;
#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub const MLKEM768_CIPHERTEXT_BYTES: u32 = 0;
#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub const MLKEM1024_CIPHERTEXT_BYTES: u32 = 0;

fn put_error() {
unsafe {
ERR_put_error(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only records an error, right? Doesn't halt the execution.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I've renamed it to just put_error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we panic and halt execution to prevent the thing you worried about?

34,
0,
ERR_R_FATAL,
c"boring-sys missing mlkem.h".as_ptr(),
1,
)
}
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C-unwind" fn MLKEM1024_generate_key(
_out_encoded_public_key: *mut u8,
_optional_out_seed: *mut u8,
_out_private_key: *mut MLKEM1024_private_key,
) {
unimplemented!();
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C" fn MLKEM1024_private_key_from_seed(
_out_private_key: *mut MLKEM1024_private_key,
_seed: *const u8,
_seed_len: usize,
) -> ::std::os::raw::c_int {
put_error();
0
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C-unwind" fn MLKEM1024_public_from_private(
_out_public_key: *mut MLKEM1024_public_key,
_private_key: *const MLKEM1024_private_key,
) {
unimplemented!();
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C-unwind" fn MLKEM1024_encap(
_out_ciphertext: *mut u8,
_out_shared_secret: *mut u8,
_public_key: *const MLKEM1024_public_key,
) {
unimplemented!();
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C" fn MLKEM1024_decap(
_out_shared_secret: *mut u8,
_ciphertext: *const u8,
_ciphertext_len: usize,
_private_key: *const MLKEM1024_private_key,
) -> ::std::os::raw::c_int {
put_error();
0
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C-unwind" fn MLKEM768_generate_key(
_out_encoded_public_key: *mut u8,
_optional_out_seed: *mut u8,
_out_private_key: *mut MLKEM768_private_key,
) {
unimplemented!();
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C" fn MLKEM768_private_key_from_seed(
_out_private_key: *mut MLKEM768_private_key,
_seed: *const u8,
_seed_len: usize,
) -> ::std::os::raw::c_int {
put_error();
0
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C-unwind" fn MLKEM768_public_from_private(
_out_public_key: *mut MLKEM768_public_key,
_private_key: *const MLKEM768_private_key,
) {
unimplemented!();
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C-unwind" fn MLKEM768_encap(
_out_ciphertext: *mut u8,
_out_shared_secret: *mut u8,
_public_key: *const MLKEM768_public_key,
) {
unimplemented!();
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C" fn MLKEM768_decap(
_out_shared_secret: *mut u8,
_ciphertext: *const u8,
_ciphertext_len: usize,
_private_key: *const MLKEM768_private_key,
) -> ::std::os::raw::c_int {
put_error();
0
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C" fn MLKEM768_marshal_public_key(
out: *mut CBB,
public_key: *const MLKEM768_public_key,
) -> ::std::os::raw::c_int {
put_error();
0
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C" fn MLKEM768_parse_public_key(
out_public_key: *mut MLKEM768_public_key,
in_: *mut CBS,
) -> ::std::os::raw::c_int {
put_error();
0
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C" fn MLKEM1024_marshal_public_key(
out: *mut CBB,
public_key: *const MLKEM1024_public_key,
) -> ::std::os::raw::c_int {
put_error();
0
}

#[deprecated(note = "mlkem.h was missing; this API won't work")]
pub unsafe extern "C" fn MLKEM1024_parse_public_key(
out_public_key: *mut MLKEM1024_public_key,
in_: *mut CBS,
) -> ::std::os::raw::c_int {
put_error();
0
}
Loading
Loading