liserk_client/
lib.rs

1use std::{
2    fs::File,
3    io::{Read, Write},
4};
5
6use aes_gcm_siv::{
7    aead::{generic_array::GenericArray, Aead, Payload},
8    Aes256GcmSiv, KeyInit,
9};
10use error::{AesError, Error};
11use serde::{Deserialize, Serialize};
12
13pub mod error;
14pub mod stream;
15
16/// Serializes a data structure into a Vec<u8> using CBOR format.
17///
18/// # Arguments
19///
20/// * `data` - A reference to the data to be serialized.
21///
22/// # Returns
23///
24/// * `Result<Vec<u8>, Error>` - The serialized data as a vector of bytes, or an error if serialization fails.
25pub fn serialize<T: Serialize>(data: &T) -> Result<Vec<u8>, Error> {
26    let cbor_data = serde_cbor::to_vec(data)?;
27    Ok(cbor_data)
28}
29
30/// Deserializes a sequence of bytes into a data structure using CBOR format.
31///
32/// # Arguments
33///
34/// * `cbor_data` - A vector of bytes representing the serialized data.
35///
36/// # Returns
37///
38/// * `Result<T, Error>` - The deserialized data structure, or an error if deserialization fails.
39pub fn deserialize<T: for<'a> Deserialize<'a>>(cbor_data: &Vec<u8>) -> Result<T, Error> {
40    let data = serde_cbor::from_slice(&cbor_data)?;
41    Ok(data)
42}
43
44/// Encrypts plaintext using AES-GCM-SIV algorithm.
45///
46/// # Arguments
47///
48/// * `key` - A reference to the 256-bit key for encryption.
49/// * `nonce` - A reference to the 12-byte nonce.
50/// * `plaintext` - A reference to the data to be encrypted.
51/// * `associated_data` - A reference to the associated data.
52///
53/// # Returns
54///
55/// * `Result<Vec<u8>, Error>` - The encrypted data as a vector of bytes, or an error if encryption fails.
56pub fn basic_encrypt(
57    key: &[u8; 32],
58    nonce: &[u8; 12],
59    plaintext: &[u8],
60    associated_data: &[u8],
61) -> Result<Vec<u8>, Error> {
62    let cipher = Aes256GcmSiv::new(GenericArray::from_slice(key));
63    let nonce = GenericArray::from_slice(nonce);
64    let payload = Payload { msg: plaintext, aad: associated_data };
65    let ciphertext = cipher
66        .encrypt(nonce, payload)
67        .map_err(|_| Error::EcryptionError(AesError::Encrypt));
68
69    ciphertext
70}
71
72/// Decrypts ciphertext using AES-GCM-SIV algorithm.
73///
74/// # Arguments
75///
76/// * `key` - A reference to the 256-bit key for decryption.
77/// * `nonce` - A reference to the 12-byte nonce.
78/// * `ciphertext` - A reference to the encrypted data.
79/// * `associated_data` - A reference to the associated data.
80///
81/// # Returns
82///
83/// * `Result<Vec<u8>, Error>` - The decrypted data as a vector of bytes, or an error if decryption fails.
84pub fn basic_decrypt(
85    key: &[u8; 32],
86    nonce: &[u8; 12],
87    ciphertext: &[u8],
88    associated_data: &[u8],
89) -> Result<Vec<u8>, Error> {
90    let cipher = Aes256GcmSiv::new(GenericArray::from_slice(key));
91    let nonce = GenericArray::from_slice(nonce);
92    let payload = Payload { msg: ciphertext, aad: associated_data };
93    let plaintext = cipher
94        .decrypt(nonce, payload)
95        .map_err(|_| Error::EcryptionError(AesError::Decrypt));
96
97    plaintext
98}
99
100/// Generates a random 256-bit key.
101///
102/// # Returns
103///
104/// * `[u8; 32]` - The generated key.
105pub fn generate_key() -> [u8; 32] {
106    let mut key = [0u8; 32];
107    getrandom::getrandom(&mut key).expect("Error generating key");
108    key
109}
110
111/// Saves a 256-bit key to a file.
112///
113/// # Arguments
114///
115/// * `key` - A reference to the 256-bit key to be saved.
116/// * `file_path` - The path to the file where the key should be saved.
117///
118/// # Returns
119///
120/// * `std::io::Result<()>` - Returns `Ok(())` if successful, or an error if there was a problem saving the key.
121pub fn save_key_to_file(key: &[u8; 32], file_path: &str) -> std::io::Result<()> {
122    let mut file = File::create(file_path)?;
123    file.write_all(key)?;
124    Ok(())
125}
126
127/// Loads a 256-bit key from a file.
128///
129/// # Arguments
130///
131/// * `file_path` - The path to the file from which the key should be loaded.
132///
133/// # Returns
134///
135/// * `std::io::Result<[u8; 32]>` - The loaded key or an error if there was a problem loading the key.
136pub fn load_key_from_file(file_path: &str) -> std::io::Result<[u8; 32]> {
137    let mut file = File::open(file_path)?;
138    let mut key = [0u8; 32];
139    file.read_exact(&mut key)?;
140    Ok(key)
141}