Skip to content

Commit c32f576

Browse files
style(kyberlib): 🎨 fix Rustfmt warnings
1 parent 6d13a32 commit c32f576

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1345
-414
lines changed

Cargo.lock

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ version = "0.0.5"
3535
[dependencies]
3636
aes = { version = "0.8.4", optional = true }
3737
ctr = { version = "0.9.2", optional = true }
38+
pqc_core = { version = "0.3.0", features = ["zero"]}
3839
rand = { version = "0.8.5", default-features = false, features = ["getrandom"] }
3940
rand_core = { version = "0.6.4", default-features = false }
4041
rlg = "0.0.4"
4142
sha2 = { version = "0.10.8", optional = true }
4243
tokio = { version = "1.37.0", optional = true }
4344
wasm-bindgen = "0.2.92"
45+
zeroize = { version = "1.7.0", features = ["derive"] }
4446

4547
[dev-dependencies]
4648
criterion = "0.5.1"

benches/api.rs

+26-7
Large diffs are not rendered by default.

examples/ake.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ fn main() -> Result<(), KyberLibError> {
3636

3737
// Bob receives the request and authenticates Alice, sends
3838
// encapsulated shared secret back
39-
let server_send =
40-
bob.server_receive(client_send, &alice_keys.public, &bob_keys.secret, &mut rng)?;
39+
let server_send = bob.server_receive(
40+
client_send,
41+
&alice_keys.public,
42+
&bob_keys.secret,
43+
&mut rng,
44+
)?;
4145

4246
// Alice authenticates and decapsulates
4347
alice.client_confirm(server_send, &alice_keys.secret)?;

examples/kem.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ fn main() -> Result<(), KyberLibError> {
3030
let alice_keys = keypair(&mut rng)?;
3131

3232
// Bob encapsulates a shared secret
33-
let (ciphertext, shared_secret_bob) = encapsulate(&alice_keys.public, &mut rng)?;
33+
let (ciphertext, shared_secret_bob) =
34+
encapsulate(&alice_keys.public, &mut rng)?;
3435

3536
// Alice decapsulates the shared secret
36-
let shared_secret_alice = decapsulate(&ciphertext, &alice_keys.secret)?;
37+
let shared_secret_alice =
38+
decapsulate(&ciphertext, &alice_keys.secret)?;
3739

3840
// Both can now communicate symmetrically
3941
assert_eq!(shared_secret_alice, shared_secret_bob);

examples/uake.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ fn main() -> Result<(), KyberLibError> {
3636

3737
// Bob receives the request and authenticates Alice, sends
3838
// encapsulated shared secret back
39-
let server_send = bob.server_receive(client_send, &bob_keys.secret, &mut rng)?;
39+
let server_send =
40+
bob.server_receive(client_send, &bob_keys.secret, &mut rng)?;
4041

4142
// Alice authenticates and decapsulates
4243
alice.client_confirm(server_send)?;

src/api.rs

+34-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
params::*,
99
CryptoRng, RngCore,
1010
};
11+
use pqc_core::zero;
1112
#[cfg(feature = "zeroize")]
1213
use zeroize::{Zeroize, ZeroizeOnDrop};
1314

@@ -38,7 +39,9 @@ where
3839
let mut public = [0u8; KYBER_PUBLIC_KEY_BYTES];
3940
let mut secret = [0u8; KYBER_SECRET_KEY_BYTES];
4041
generate_key_pair(&mut public, &mut secret, rng, None)?;
41-
Ok(Keypair { public, secret })
42+
let keys = Keypair { public, secret };
43+
zero!(secret);
44+
Ok(keys)
4245
}
4346

4447
/// Verify that given secret and public key matches and put them in
@@ -62,7 +65,7 @@ pub fn keypairfrom<R>(
6265
where
6366
R: RngCore + CryptoRng,
6467
{
65-
//Try to encapsulate and decapsule to verify secret key matches public key
68+
//Try to encapsulate and decapsulate to verify secret key matches public key
6669
let (ciphertext, shared_secret) = encapsulate(public, rng)?;
6770
let expected_shared_secret = decapsulate(&ciphertext, secret)?;
6871
//If it does match, return a KeyPair
@@ -142,12 +145,14 @@ where
142145
/// let mut rng = rand::thread_rng();
143146
/// let keys = keypair(&mut rng)?;
144147
/// let (ct, ss1) = encapsulate(&keys.public, &mut rng)?;
145-
/// let ss2 = decapsulate(&ct, &keys.secret)?;
148+
/// let ss2 = decapsulate(&ct, keys.expose_secret())?;
146149
/// assert_eq!(ss1, ss2);
147150
/// # Ok(())}
148151
/// ```
149152
pub fn decapsulate(ct: &[u8], sk: &[u8]) -> Decapsulated {
150-
if ct.len() != KYBER_CIPHERTEXT_BYTES || sk.len() != KYBER_SECRET_KEY_BYTES {
153+
if ct.len() != KYBER_CIPHERTEXT_BYTES
154+
|| sk.len() != KYBER_SECRET_KEY_BYTES
155+
{
151156
return Err(KyberLibError::InvalidInput);
152157
}
153158
let mut ss = [0u8; KYBER_SHARED_SECRET_BYTES];
@@ -188,10 +193,27 @@ impl Keypair {
188193
/// # assert!(empty_keys != keys);
189194
/// # Ok(()) }
190195
/// ```
191-
pub fn generate<R: CryptoRng + RngCore>(rng: &mut R) -> Result<Keypair, KyberLibError> {
196+
pub fn generate<R: CryptoRng + RngCore>(
197+
rng: &mut R,
198+
) -> Result<Keypair, KyberLibError> {
192199
keypair(rng)
193200
}
194201

202+
/// Explicitly exposes the secret key
203+
///```
204+
/// use kyberlib::*;
205+
///
206+
/// let mut rng = rand::thread_rng();
207+
/// let keys = Keypair::generate(&mut rng);
208+
/// let binding = keys.expect("Exposed secret key");
209+
/// let secret = binding.expose_secret();
210+
/// assert!(secret.len() == KYBER_SECRET_KEY_BYTES);
211+
/// assert!(secret.len() != 0);
212+
/// ```
213+
pub fn expose_secret(&self) -> &SecretKey {
214+
&self.secret
215+
}
216+
195217
/// Imports a keypair from existing public and secret key arrays.
196218
///
197219
/// This function imports a keypair from existing public and secret key arrays and returns it as a `Keypair` struct.
@@ -237,7 +259,10 @@ impl RngCore for DummyRng {
237259
panic!()
238260
}
239261

240-
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), rand_core::Error> {
262+
fn try_fill_bytes(
263+
&mut self,
264+
_dest: &mut [u8],
265+
) -> Result<(), rand_core::Error> {
241266
panic!()
242267
}
243268

@@ -287,7 +312,9 @@ pub fn derive(seed: &[u8]) -> Result<Keypair, KyberLibError> {
287312
pub fn public(sk: &[u8]) -> PublicKey {
288313
let mut pk = [0u8; KYBER_INDCPA_PUBLICKEYBYTES];
289314
pk.copy_from_slice(
290-
&sk[KYBER_INDCPA_SECRETKEYBYTES..KYBER_INDCPA_SECRETKEYBYTES + KYBER_INDCPA_PUBLICKEYBYTES],
315+
&sk[KYBER_INDCPA_SECRETKEYBYTES
316+
..KYBER_INDCPA_SECRETKEYBYTES
317+
+ KYBER_INDCPA_PUBLICKEYBYTES],
291318
);
292319
pk
293320
}

src/avx2/aes256ctr.rs

+68-17
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,33 @@ impl Aes256CtrCtx {
2424
}
2525
}
2626

27-
unsafe fn aesni_encrypt4(out: &mut [u8], n: &mut __m128i, rkeys: &[__m128i; 16]) {
28-
let idx: __m128i = _mm_set_epi8(8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 4, 3, 2, 1, 0);
27+
unsafe fn aesni_encrypt4(
28+
out: &mut [u8],
29+
n: &mut __m128i,
30+
rkeys: &[__m128i; 16],
31+
) {
32+
let idx: __m128i = _mm_set_epi8(
33+
8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 4, 3, 2, 1, 0,
34+
);
2935

3036
// Load current counter value
3137
let mut f = _mm_load_si128(n);
3238

3339
// Increase counter in 4 consecutive blocks
34-
let mut f0 = _mm_shuffle_epi8(_mm_add_epi64(f, _mm_set_epi64x(0, 0)), idx);
35-
let mut f1 = _mm_shuffle_epi8(_mm_add_epi64(f, _mm_set_epi64x(1, 0)), idx);
36-
let mut f2 = _mm_shuffle_epi8(_mm_add_epi64(f, _mm_set_epi64x(2, 0)), idx);
37-
let mut f3 = _mm_shuffle_epi8(_mm_add_epi64(f, _mm_set_epi64x(3, 0)), idx);
40+
let mut f0 =
41+
_mm_shuffle_epi8(_mm_add_epi64(f, _mm_set_epi64x(0, 0)), idx);
42+
let mut f1 =
43+
_mm_shuffle_epi8(_mm_add_epi64(f, _mm_set_epi64x(1, 0)), idx);
44+
let mut f2 =
45+
_mm_shuffle_epi8(_mm_add_epi64(f, _mm_set_epi64x(2, 0)), idx);
46+
let mut f3 =
47+
_mm_shuffle_epi8(_mm_add_epi64(f, _mm_set_epi64x(3, 0)), idx);
3848

3949
// Write counter for next iteration, increased by 4
40-
_mm_store_si128(n as *mut __m128i, _mm_add_epi64(f, _mm_set_epi64x(4, 0)));
50+
_mm_store_si128(
51+
n as *mut __m128i,
52+
_mm_add_epi64(f, _mm_set_epi64x(4, 0)),
53+
);
4154

4255
// Actual AES encryption, 4x interleaved4
4356
f = _mm_load_si128(&rkeys[0]);
@@ -76,11 +89,16 @@ unsafe fn cast_128(x: __m128i) -> __m128 {
7689
_mm_castsi128_ps(x)
7790
}
7891

79-
pub fn aes256ctr_init(state: &mut Aes256CtrCtx, key: &[u8], nonce: [u8; 12]) {
92+
pub fn aes256ctr_init(
93+
state: &mut Aes256CtrCtx,
94+
key: &[u8],
95+
nonce: [u8; 12],
96+
) {
8097
unsafe {
8198
let mut idx = 0;
8299
let key0 = _mm_loadu_si128(key.as_ptr() as *const __m128i);
83-
let key1 = _mm_loadu_si128(key[16..].as_ptr() as *const __m128i);
100+
let key1 =
101+
_mm_loadu_si128(key[16..].as_ptr() as *const __m128i);
84102

85103
state.n = _mm_loadl_epi64(nonce[..].as_ptr() as *const __m128i);
86104
state.rkeys[idx] = key0;
@@ -95,11 +113,23 @@ pub fn aes256ctr_init(state: &mut Aes256CtrCtx, key: &[u8], nonce: [u8; 12]) {
95113
temp1 = _mm_aeskeygenassist_si128(temp2, $imm);
96114
state.rkeys[idx] = temp2;
97115
idx += 1;
98-
temp4 = cast_128i(_mm_shuffle_ps(cast_128(temp4), cast_128(temp0), 0x10));
116+
temp4 = cast_128i(_mm_shuffle_ps(
117+
cast_128(temp4),
118+
cast_128(temp0),
119+
0x10,
120+
));
99121
temp0 = _mm_xor_si128(temp0, temp4);
100-
temp4 = cast_128i(_mm_shuffle_ps(cast_128(temp4), cast_128(temp0), 0x8c));
122+
temp4 = cast_128i(_mm_shuffle_ps(
123+
cast_128(temp4),
124+
cast_128(temp0),
125+
0x8c,
126+
));
101127
temp0 = _mm_xor_si128(temp0, temp4);
102-
temp1 = cast_128i(_mm_shuffle_ps(cast_128(temp1), cast_128(temp1), 0xff));
128+
temp1 = cast_128i(_mm_shuffle_ps(
129+
cast_128(temp1),
130+
cast_128(temp1),
131+
0xff,
132+
));
103133
temp0 = _mm_xor_si128(temp0, temp1)
104134
};
105135
}
@@ -109,11 +139,23 @@ pub fn aes256ctr_init(state: &mut Aes256CtrCtx, key: &[u8], nonce: [u8; 12]) {
109139
temp1 = _mm_aeskeygenassist_si128(temp0, $imm);
110140
state.rkeys[idx] = temp0;
111141
idx += 1;
112-
temp4 = cast_128i(_mm_shuffle_ps(cast_128(temp4), cast_128(temp2), 0x10));
142+
temp4 = cast_128i(_mm_shuffle_ps(
143+
cast_128(temp4),
144+
cast_128(temp2),
145+
0x10,
146+
));
113147
temp2 = _mm_xor_si128(temp2, temp4);
114-
temp4 = cast_128i(_mm_shuffle_ps(cast_128(temp4), cast_128(temp2), 0x8c));
148+
temp4 = cast_128i(_mm_shuffle_ps(
149+
cast_128(temp4),
150+
cast_128(temp2),
151+
0x8c,
152+
));
115153
temp2 = _mm_xor_si128(temp2, temp4);
116-
temp1 = cast_128i(_mm_shuffle_ps(cast_128(temp1), cast_128(temp1), 0xaa));
154+
temp1 = cast_128i(_mm_shuffle_ps(
155+
cast_128(temp1),
156+
cast_128(temp1),
157+
0xaa,
158+
));
117159
temp2 = _mm_xor_si128(temp2, temp1)
118160
};
119161
}
@@ -138,7 +180,11 @@ pub fn aes256ctr_init(state: &mut Aes256CtrCtx, key: &[u8], nonce: [u8; 12]) {
138180
}
139181
}
140182

141-
pub fn aes256ctr_squeezeblocks(out: &mut [u8], nblocks: usize, state: &mut Aes256CtrCtx) {
183+
pub fn aes256ctr_squeezeblocks(
184+
out: &mut [u8],
185+
nblocks: usize,
186+
state: &mut Aes256CtrCtx,
187+
) {
142188
let mut idx = 0;
143189
for _ in 0..nblocks {
144190
unsafe {
@@ -149,7 +195,12 @@ pub fn aes256ctr_squeezeblocks(out: &mut [u8], nblocks: usize, state: &mut Aes25
149195
}
150196

151197
#[cfg(feature = "90s")]
152-
pub fn aes256ctr_prf(out: &mut [u8], mut outlen: usize, seed: &[u8], nonce: u8) {
198+
pub fn aes256ctr_prf(
199+
out: &mut [u8],
200+
mut outlen: usize,
201+
seed: &[u8],
202+
nonce: u8,
203+
) {
153204
let mut buf = [0u8; 64];
154205
let mut idx = 0;
155206
let mut pad_nonce = [0u8; 12];

src/avx2/align.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use core::arch::x86_64::*;
1111
#[repr(C, align(32))]
1212
pub union GenMatrixBuf {
1313
pub coeffs: [u8; REJ_UNIFORM_AVX_NBLOCKS * SHAKE128_RATE],
14-
pub vec: [__m256i; (REJ_UNIFORM_AVX_NBLOCKS * SHAKE128_RATE + 31) / 32],
14+
pub vec:
15+
[__m256i; (REJ_UNIFORM_AVX_NBLOCKS * SHAKE128_RATE + 31) / 32],
1516
}
1617

1718
impl GenMatrixBuf {
@@ -26,7 +27,8 @@ impl GenMatrixBuf {
2627
#[repr(C)]
2728
pub union GenMatrixBuf90s {
2829
pub coeffs: [u8; REJ_UNIFORM_AVX_NBLOCKS * XOF_BLOCKBYTES],
29-
pub vec: [__m256i; (REJ_UNIFORM_AVX_NBLOCKS * XOF_BLOCKBYTES + 31) / 32],
30+
pub vec:
31+
[__m256i; (REJ_UNIFORM_AVX_NBLOCKS * XOF_BLOCKBYTES + 31) / 32],
3032
}
3133

3234
#[cfg(feature = "90s")]
@@ -51,15 +53,22 @@ impl GenMatrixBuf90s {
5153

5254
#[repr(C)]
5355
pub union IndcpaBuf {
54-
pub coeffs: [u8; (KYBER_ETA1 * KYBER_N / 4) / XOF_BLOCKBYTES * XOF_BLOCKBYTES + 32],
55-
pub vec:
56-
[__m256i; ((KYBER_ETA1 * KYBER_N / 4) / XOF_BLOCKBYTES * XOF_BLOCKBYTES + 32 + 31) / 32],
56+
pub coeffs: [u8; (KYBER_ETA1 * KYBER_N / 4) / XOF_BLOCKBYTES
57+
* XOF_BLOCKBYTES
58+
+ 32],
59+
pub vec: [__m256i;
60+
((KYBER_ETA1 * KYBER_N / 4) / XOF_BLOCKBYTES * XOF_BLOCKBYTES
61+
+ 32
62+
+ 31)
63+
/ 32],
5764
}
5865

5966
impl IndcpaBuf {
6067
pub fn new() -> Self {
6168
Self {
62-
coeffs: [0u8; (KYBER_ETA1 * KYBER_N / 4) / XOF_BLOCKBYTES * XOF_BLOCKBYTES + 32],
69+
coeffs: [0u8; (KYBER_ETA1 * KYBER_N / 4) / XOF_BLOCKBYTES
70+
* XOF_BLOCKBYTES
71+
+ 32],
6372
}
6473
}
6574
}

0 commit comments

Comments
 (0)