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
172
173
174
175
176
177
178
179
use crate::constants::{CIPHERKEYLEN, MAXBLOCKLEN, MAXHASHLEN, TAGLEN};
use rand_core::{CryptoRng, RngCore};
pub trait Random: CryptoRng + RngCore + Send + Sync {}
pub trait Dh: Send + Sync {
fn name(&self) -> &'static str;
fn pub_len(&self) -> usize;
fn priv_len(&self) -> usize;
fn set(&mut self, privkey: &[u8]);
fn generate(&mut self, rng: &mut dyn Random);
fn pubkey(&self) -> &[u8];
fn privkey(&self) -> &[u8];
fn dh(&self, pubkey: &[u8], out: &mut [u8]) -> Result<(), ()>;
}
pub trait Cipher: Send + Sync {
fn name(&self) -> &'static str;
fn set(&mut self, key: &[u8]);
fn encrypt(&self, nonce: u64, authtext: &[u8], plaintext: &[u8], out: &mut [u8]) -> usize;
fn decrypt(
&self,
nonce: u64,
authtext: &[u8],
ciphertext: &[u8],
out: &mut [u8],
) -> Result<usize, ()>;
fn rekey(&mut self) {
let mut ciphertext = [0; CIPHERKEYLEN + TAGLEN];
let ciphertext_len =
self.encrypt(u64::max_value(), &[], &[0; CIPHERKEYLEN], &mut ciphertext);
assert_eq!(ciphertext_len, ciphertext.len());
self.set(&ciphertext[..CIPHERKEYLEN]);
}
}
pub trait Hash: Send + Sync {
fn name(&self) -> &'static str;
fn block_len(&self) -> usize;
fn hash_len(&self) -> usize;
fn reset(&mut self);
fn input(&mut self, data: &[u8]);
fn result(&mut self, out: &mut [u8]);
fn hmac(&mut self, key: &[u8], data: &[u8], out: &mut [u8]) {
assert!(key.len() <= self.block_len());
let block_len = self.block_len();
let hash_len = self.hash_len();
let mut ipad = [0x36u8; MAXBLOCKLEN];
let mut opad = [0x5cu8; MAXBLOCKLEN];
for count in 0..key.len() {
ipad[count] ^= key[count];
opad[count] ^= key[count];
}
self.reset();
self.input(&ipad[..block_len]);
self.input(data);
let mut inner_output = [0u8; MAXHASHLEN];
self.result(&mut inner_output);
self.reset();
self.input(&opad[..block_len]);
self.input(&inner_output[..hash_len]);
self.result(out);
}
fn hkdf(
&mut self,
chaining_key: &[u8],
input_key_material: &[u8],
outputs: usize,
out1: &mut [u8],
out2: &mut [u8],
out3: &mut [u8],
) {
let hash_len = self.hash_len();
let mut temp_key = [0u8; MAXHASHLEN];
self.hmac(chaining_key, input_key_material, &mut temp_key);
self.hmac(&temp_key, &[1u8], out1);
if outputs == 1 {
return;
}
let mut in2 = [0u8; MAXHASHLEN + 1];
copy_slices!(&out1[0..hash_len], &mut in2);
in2[hash_len] = 2;
self.hmac(&temp_key, &in2[..=hash_len], out2);
if outputs == 2 {
return;
}
let mut in3 = [0u8; MAXHASHLEN + 1];
copy_slices!(&out2[0..hash_len], &mut in3);
in3[hash_len] = 3;
self.hmac(&temp_key, &in3[..=hash_len], out3);
}
}
#[cfg(feature = "hfs")]
pub trait Kem: Send + Sync {
fn name(&self) -> &'static str;
fn pub_len(&self) -> usize;
fn ciphertext_len(&self) -> usize;
fn shared_secret_len(&self) -> usize;
fn generate(&mut self, rng: &mut dyn Random);
fn pubkey(&self) -> &[u8];
#[must_use]
fn encapsulate(
&self,
pubkey: &[u8],
shared_secret_out: &mut [u8],
ciphertext_out: &mut [u8],
) -> Result<(usize, usize), ()>;
#[must_use]
fn decapsulate(&self, ciphertext: &[u8], shared_secret_out: &mut [u8]) -> Result<usize, ()>;
}