libqabu/lib.rs
1#![cfg_attr(test, feature(test))]
2#![cfg_attr(not(feature = "std"), no_std)]
3#![warn(
4 missing_docs,
5 missing_copy_implementations,
6 missing_debug_implementations
7)]
8
9//! **This library provides various cryptographic primitives and strives to be durable, readable, auditable, and versatile.**
10//!
11//! Just to get your taste buds wet...
12//! # A minimal unpadded AES-CBC example
13//!
14//! ```
15//! use libqabu::prelude::*;
16//! use libqabu::symmetric::{Rijndael, RijndaelKey};
17//! use libqabu::mode::CBC;
18//! # fn error() -> Result<(), Box<dyn std::error::Error>> {
19//!
20//! let mut to_encrypt = [0u8; 32]; // this is just 32 zeros.
21//! let key = [0u8; 16]; // this is just an example of a key
22//! let iv = [0u8; 16]; // this is just an example of a IV
23//!
24//! let mut raw_cipher = Rijndael::new(
25//! RijndaelKey::Small(key)
26//! )?;
27//!
28//! let mut cipher = CBC::new(&mut raw_cipher, iv)?;
29//!
30//! cipher.encrypt_insitu(&mut to_encrypt)?;
31//!
32//! println!("The encrypted data {:?}", to_encrypt);
33//! # Ok(())
34//! # }
35//! # error().unwrap();
36//! ```
37//!
38//! # Important remarks
39//!
40//! - The library exposes a vast amount of very low lever primitives
41//! and **these will be insecure when used wrongly** you use this **only**
42//! if you know exactly what you're doing. For use in applications please
43//! use a higher lever crate depending on this.
44//! - The library contains implementations of obscure and/or obsolete primitives
45//! (eg. DES, MD5 ...) for the sake of completeness. These are disabled by default
46//! in the build configuration but you have been warned to be weary of the build
47//! features you include.
48//! - This is not meant for full disk encryption or very high speed processing of a large volume of
49//! data.
50//! - This is not meant for applications where hardware
51//! side channels are an issue.
52//! - When adding it to your project please pick only the features
53//! which you use. Do not just blindly pick `features=["all"]`
54//! - How fast is this?
55//! That, obviously, depends. Clone the project and run `cargo bench` to
56//! find out for yourself.
57//!
58//! # Usage
59//!
60//! Currently the library exposes the following primitives:
61//!
62//! *it is important to remark that almost everything here requires you
63//! to include `libqabu::prelude::*`*
64//!
65//! ### Block ciphers
66//!
67//! If you just want to use a cipher for operations on single blocks,
68//! simply pick one of the [Block Ciphers](crate::traits::Block), and
69//! construct it while providing it a key.
70//!
71//! eg. for a cipher which simply accepts a array of some length as a key:
72//!
73//! ```
74//! # use libqabu::prelude::*;
75//! # use libqabu::symmetric::Blowfish;
76//! # fn error() -> Result<(), Box<dyn std::error::Error>> {
77//! // key
78//! let cipher = Blowfish::new([0u8; 30])?;
79//! # Ok(())
80//! # }
81//! # error().unwrap();
82//! ```
83//! and then you can use the functions exposed in [Block Ciphers](crate::traits::Block).
84//!
85//! ### Modes
86//! if you need to encrypt more than one block (almost always the case) use one
87//! of the [Modes](crate::traits::Mode)
88//!
89//! eg. for our previous example :
90//!
91//! ```
92//! # use libqabu::prelude::*;
93//! # use libqabu::symmetric::Blowfish;
94//! # use libqabu::mode::CTR;
95//! # fn error() -> Result<(), Box<dyn std::error::Error>> {
96//! let cipher = Blowfish::new([0u8; 30])?;
97//! let mode = CTR::with_counter(&cipher, [0u8; 8])?;
98//! # Ok(())
99//! # }
100//! # error().unwrap();
101//! ```
102//! and then you can use the functions exposed in [Mode](crate::traits::Mode).
103//!
104//! ### Stream ciphers
105//!
106//! Construction is the same as block ciphers, afterwards use functions from
107//! [Stream Ciphers](crate::traits::Stream)
108
109/// Results and errors.
110///
111/// Contains the error types used
112/// in this crate.
113pub mod result;
114
115/// All traits.
116///
117/// Contains all traits implemented by
118/// components of the library.
119pub mod traits;
120
121/// Clears sensitive data from memory.
122///
123/// When the shred feature is enabled all ciphers will
124/// clear out their keys and states on drop or when
125/// the shred() function is called.
126#[cfg(feature = "shred")]
127pub mod shred;
128
129#[cfg(feature = "apn")]
130/// Contains number related structures
131pub mod num;
132
133#[cfg(feature = "shred")]
134#[allow(unused)]
135macro_rules! shred_on_drop {
136 (
137 $ident: tt
138 ) => {
139 impl Drop for $ident {
140 fn drop(&mut self) { self.shred(); }
141 }
142 };
143}
144
145/// The prelude module.
146///
147/// Much alike other preludes, imports traits
148/// which are required to operate most algorithms
149pub mod prelude {
150 pub use crate::traits::*;
151
152 #[cfg(feature = "shred")]
153 pub use crate::shred::*;
154}
155
156/// Symmetric ciphers.
157///
158/// Block and stream symmetric ciphers.
159///
160/// Each algorithm can be included or excluded in compilation via feature choice.
161pub mod symmetric;
162
163/// Message digests.
164///
165/// This module contains both XOFs and static length output hash functions.
166///
167/// Each algorithm can be included or excluded in compilation via feature choice.
168pub mod digest;
169
170#[cfg(test)]
171/// This module contains various test vectors.
172pub mod tests;
173
174/// Block cipher mode of operation.
175///
176/// Use this if you want to encrypt more than
177/// one block worth of data with a block cipher.
178pub mod mode;