raycrypt/
lib.rs

1pub mod aeads;
2pub mod ciphers;
3pub mod ecc;
4pub mod errors;
5pub mod macs;
6pub(crate) mod utils;
7
8pub use ecc::x25519::{PrivateKey, PublicKey};
9pub use getrandom::getrandom;
10
11pub fn encrypt(key: Vec<u8>, msg: &[u8]) -> Vec<u8> {
12    let mut nonce = [0u8; 32];
13    let mut ad = [0u8; 32];
14
15    let _ = getrandom(&mut nonce);
16    let _ = getrandom(&mut ad);
17
18    let mut output = aeads::aegis256::encrypt::<16>(&key, msg, &nonce, &ad);
19    output.append(&mut nonce.to_vec());
20    output.append(&mut ad.to_vec());
21
22    output
23}
24
25pub fn decrypt(key: Vec<u8>, msg: &[u8]) -> Result<Vec<u8>, errors::InvalidMac> {
26    let nonce = &msg[msg.len() - 64..msg.len() - 32];
27    let ad = &msg[msg.len() - 32..];
28    let m = &msg[..msg.len() - 64];
29
30    aeads::aegis256::decrypt::<16>(&key, m, nonce, ad)
31}