Skip to main content

liboscore_cryptobackend/
lib.rs

1//! Backend for liboscore's crypto API that fans out to AEAD algorithms base on the aead Rust
2//! crate.
3//!
4//! This takes a mixture of trait- and enum-based approaches; algorithms are handled as trait
5//! objects (or constructors derived from them), but the oscore_crypto_aead_encryptstate_t would
6//! eventually be an enum in order to be Sized and thus stack-allocatable.
7#![no_std]
8
9macro_rules! log_secrets {
10    ( $($arg:tt)* ) => {
11        #[cfg(feature = "log_cryptographic_operations_including_secret_keys")]
12        log::info!($($arg)*);
13    }
14}
15
16// pub only because for bindgen we need types again. This is probably cleaner (because done in Rust
17// rather than in cbindgen) in the message backend.
18pub mod aead;
19mod hkdf;
20
21/// Void stand-in recognized by the cbindgen library by its name
22#[allow(non_camel_case_types)]
23pub enum c_void {}
24
25// Those types that are passed in and out as arguments need to be repr(C). The rest can be any repr
26// as it is only stack-allocated and passed through pointers, but CryptoErr and Algorithm are
27// passed around explicitly.
28
29#[repr(C)]
30pub enum CryptoErr {
31    Ok,
32    NoSuchAlgorithm,
33    /// Data was put into the AAD, plaintext or buffer whose length was not as originally announced
34    UnexpectedDataLength,
35    /// The only possible encryption error
36    BufferShorterThanTag,
37    /// Decryption failed (ie. message corruption / tampering / disagreement on nonce or AAD)
38    DecryptError,
39    /// Returned when the AAD is longer than pre-allocated, and neither streaming AAD nor dynamic
40    /// allocation are not implemented (which is unconditional so far)
41    AadPreallocationExceeded,
42    /// A kind of identifier was requested of an algorithm that is not specified
43    NoIdentifier,
44}
45
46#[no_mangle]
47pub extern "C" fn oscore_cryptoerr_is_error(err: CryptoErr) -> bool {
48    match err {
49        CryptoErr::Ok => false,
50        _ => true,
51    }
52}