1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use aes_gcm::aead::{Aead, NewAead};
use aes_gcm::{Aes256Gcm, Key, Nonce};
use rsa::{PaddingScheme, PublicKey, RsaPrivateKey, RsaPublicKey};
pub fn rsa_keys(bits: usize) -> Result<(RsaPublicKey, RsaPrivateKey), rsa::errors::Error> {
let mut rng = rand::thread_rng();
let private_key = RsaPrivateKey::new(&mut rng, bits)?;
let public_key = RsaPublicKey::from(&private_key);
Ok((public_key, private_key))
}
pub fn rsa_encrypt(
public_key: RsaPublicKey,
plaintext: &[u8],
) -> Result<Vec<u8>, rsa::errors::Error> {
let mut rng = rand::thread_rng();
let padding = PaddingScheme::new_oaep::<sha2::Sha256>();
let ciphertext = public_key.encrypt(&mut rng, padding, &plaintext[..])?;
Ok(ciphertext)
}
pub fn rsa_decrypt(
private_key: RsaPrivateKey,
ciphertext: &[u8],
) -> Result<Vec<u8>, rsa::errors::Error> {
let padding = PaddingScheme::new_oaep::<sha2::Sha256>();
let plaintext = private_key.decrypt(padding, ciphertext)?;
Ok(plaintext)
}
pub fn aes_key() -> [u8; 32] {
rand::random()
}
pub fn aes_encrypt(key: &[u8; 32], plaintext: &[u8]) -> Result<Vec<u8>, aes_gcm::Error> {
let aes_key = Key::from(*key);
let cipher = Aes256Gcm::new(&aes_key);
let nonce_slice: [u8; 12] = rand::random();
let nonce = Nonce::from(nonce_slice);
let ciphertext = cipher.encrypt(&nonce, plaintext.as_ref())?;
let mut ciphertext_with_nonce = nonce_slice.to_vec();
ciphertext_with_nonce.extend(ciphertext);
Ok(ciphertext_with_nonce)
}
pub fn aes_decrypt(
key: &[u8; 32],
ciphertext_with_nonce: &[u8],
) -> Result<Vec<u8>, aes_gcm::Error> {
let aes_key = Key::from(*key);
let cipher = Aes256Gcm::new(&aes_key);
let (nonce_slice, ciphertext) = ciphertext_with_nonce.split_at(12);
let nonce_slice_sized: [u8; 12] = nonce_slice.try_into().expect("incorrect nonce length");
let nonce = Nonce::from(nonce_slice_sized);
let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
Ok(plaintext)
}