Cargo.lock
Cargo.lock
This file has not been changed.
Cargo.toml
Cargo.toml
This file has not been changed.
sachy-crypto/Cargo.toml
sachy-crypto/Cargo.toml
This file has not been changed.
sachy-crypto/README.md
sachy-crypto/README.md
This file has not been changed.
+30
-7
sachy-crypto/src/lib.rs
+30
-7
sachy-crypto/src/lib.rs
···
1
1
#![no_std]
2
2
3
-
use core::ops::AddAssign;
3
+
use core::ops::{AddAssign, BitXor};
4
4
5
5
use chacha20poly1305::{
6
6
AeadInOut, ChaCha20Poly1305, KeyInit,
···
47
47
Server,
48
48
}
49
49
50
+
impl From<Role> for u8 {
51
+
fn from(value: Role) -> Self {
52
+
match value {
53
+
Role::Client => 0,
54
+
Role::Server => 1,
55
+
}
56
+
}
57
+
}
58
+
59
+
impl BitXor for Role {
60
+
type Output = u8;
61
+
62
+
fn bitxor(self, rhs: Self) -> u8 {
63
+
u8::from(self) ^ u8::from(rhs)
64
+
}
65
+
}
66
+
50
67
impl EncapsulatedPublicKey {
51
68
pub fn serialize(&self) -> Sec1Point {
52
69
self.0.to_sec1_point(true)
···
140
157
let counter = self.counter.to_be_bytes();
141
158
142
159
self.transport.encrypt_in_place(
143
-
&self.transport.mix_nonce(&counter, true),
160
+
&self.transport.mix_nonce(&counter, Role::Client),
144
161
&counter,
145
162
msg,
146
163
)?;
···
167
184
let counter = self.counter.to_be_bytes();
168
185
169
186
self.transport.decrypt_in_place(
170
-
&self.transport.mix_nonce(&counter, false),
187
+
&self.transport.mix_nonce(&counter, Role::Server),
171
188
&counter,
172
189
msg,
173
190
)?;
···
263
280
)
264
281
}
265
282
266
-
fn mix_nonce(&self, position: &[u8; 8], send: bool) -> aead::Nonce<ChaCha20Poly1305> {
283
+
fn mix_nonce(&self, position: &[u8; 8], send: Role) -> aead::Nonce<ChaCha20Poly1305> {
267
284
let mut trump = aead::Nonce::<ChaCha20Poly1305>::default();
268
285
269
-
let epstein = match (self.role, send) {
270
-
(Role::Client, true) | (Role::Server, false) => &self.client,
271
-
(Role::Server, true) | (Role::Client, false) => &self.server,
286
+
let switch = self.role ^ send;
287
+
288
+
// Role switch allows toggling which nonce to use for encrypting/decrypting
289
+
// Callee ROLE XOR Transport ROLE selects either same or other nonce context,
290
+
// SAME (0) for sending, OTHER (1) for receiving
291
+
let epstein = if switch.ct_eq(&0).into() {
292
+
&self.client
293
+
} else {
294
+
&self.server
272
295
};
273
296
274
297
let (head, tail) = trump.split_at_mut(position.len());
History
18 rounds
0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
pull request successfully merged
1 commit
expand
collapse
Sachy's crypto scheme lmao
1/2 failed, 1/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
1/2 failed, 1/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 failed
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao
2/2 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
Sachy's crypto scheme lmao