-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt_decrypt.rs
171 lines (148 loc) · 5.25 KB
/
encrypt_decrypt.rs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::{
fmt::Write,
io::{self, BufRead, Write as _},
};
use clap::{Parser, ValueEnum};
use sframe::{
header::SframeHeader,
ratchet::{RatchetingBaseKey, RatchetingKeyId},
receiver::{Receiver, ReceiverOptions},
sender::{Sender, SenderOptions},
CipherSuiteVariant,
};
fn main() {
let Args {
cipher_suite,
key_id,
log_level,
max_frame_count,
secret,
n_ratchet_bits,
} = Args::parse();
println!(
"- Using cipher suite {:?}, key id {}, secret {}",
cipher_suite, key_id, secret
);
if let Some(log_level) = log_level {
println!("- Using log level {}", log_level);
simple_logger::init_with_level(log_level).unwrap();
}
let cipher_suite_variant = cipher_suite.into();
let (mut base_key, key_id) = if let Some(n_ratchet_bits) = n_ratchet_bits {
// just to demonstrate the functionality, ratcheting should only take place if a new receiver joins
println!("- Using {} bits for the ratcheting step", n_ratchet_bits);
let r = RatchetingKeyId::new(key_id, n_ratchet_bits);
let base_key =
RatchetingBaseKey::ratchet_forward(r, secret.as_bytes(), cipher_suite_variant).unwrap();
(Some(base_key), r.into())
} else {
(None, key_id)
};
let sender_options = SenderOptions {
key_id,
cipher_suite_variant,
max_frame_count,
};
let mut sender = Sender::from(sender_options);
sender.set_encryption_key(&secret).unwrap();
let receiver_options = ReceiverOptions {
cipher_suite_variant,
frame_validation: None,
n_ratchet_bits,
};
let mut receiver = Receiver::from(receiver_options);
receiver.set_encryption_key(key_id, &secret).unwrap();
let print_before_input = || {
println!("--------------------------------------------------------------------------");
println!("- Enter a phrase to be encrypted, confirm with [ENTER], abort with [CTRL+C]");
print!("- To be encrypted: ");
std::io::stdout().flush().unwrap();
};
print_before_input();
let stdin = io::stdin();
let lines = stdin
.lock()
.lines()
.take_while(Result::is_ok)
.map(Result::unwrap);
lines.enumerate().for_each(|(_i, line)| {
if n_ratchet_bits.is_some() {
let base_key = base_key.as_mut().unwrap();
let (new_key_id, key_material) = base_key.next_base_key().unwrap();
println!(
"- Ratcheting sender key, ratcheting step: {}",
new_key_id.ratchet_step()
);
sender
.ratchet_encryption_key(new_key_id, &key_material)
.unwrap();
}
println!("- Encrypting {}", bin2string(line.as_bytes()));
let encrypted = sender.encrypt(line, 0).unwrap();
display_encrypted(encrypted);
let decrypted = receiver.decrypt(encrypted, 0).unwrap();
println!("- Decrypted {}", bin2string(decrypted));
print_before_input();
});
}
fn display_encrypted(encrypted: &[u8]) {
let header = SframeHeader::deserialize(encrypted).unwrap();
println!("- Sframe Header: {}", header);
let header_len = header.len();
let payload = bin2string(&encrypted[header_len..]);
println!("- Encrypted Payload: {}", payload)
}
fn bin2string(bin: &[u8]) -> String {
bin.iter().fold(String::new(), |mut output, x| {
let _ = write!(output, "{x:08b} ");
output
})
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(value_enum, short, long, default_value_t = ArgCipherSuiteVariant::AesGcm128Sha256)]
cipher_suite: ArgCipherSuiteVariant,
#[arg(short, long, default_value_t = 3)]
key_id: u64,
#[arg(short, long)]
log_level: Option<log::Level>,
#[arg(short, long, default_value_t = u64::MAX)]
max_frame_count: u64,
#[arg(short, long, default_value = "SUPER_SECRET")]
secret: String,
#[arg(short, long)]
n_ratchet_bits: Option<u8>,
}
// We need to redeclare here, as we need to derive ValueEnum to use it with clap...
#[derive(ValueEnum, Clone, Copy, Debug)]
pub enum ArgCipherSuiteVariant {
#[cfg(feature = "openssl")]
AesCtr128HmacSha256_80,
#[cfg(feature = "openssl")]
AesCtr128HmacSha256_64,
#[cfg(feature = "openssl")]
AesCtr128HmacSha256_32,
AesGcm128Sha256,
AesGcm256Sha512,
}
impl From<ArgCipherSuiteVariant> for CipherSuiteVariant {
fn from(val: ArgCipherSuiteVariant) -> Self {
match val {
#[cfg(feature = "openssl")]
ArgCipherSuiteVariant::AesCtr128HmacSha256_80 => {
CipherSuiteVariant::AesCtr128HmacSha256_80
}
#[cfg(feature = "openssl")]
ArgCipherSuiteVariant::AesCtr128HmacSha256_64 => {
CipherSuiteVariant::AesCtr128HmacSha256_64
}
#[cfg(feature = "openssl")]
ArgCipherSuiteVariant::AesCtr128HmacSha256_32 => {
CipherSuiteVariant::AesCtr128HmacSha256_32
}
ArgCipherSuiteVariant::AesGcm128Sha256 => CipherSuiteVariant::AesGcm128Sha256,
ArgCipherSuiteVariant::AesGcm256Sha512 => CipherSuiteVariant::AesGcm256Sha512,
}
}
}