Skip to content

Commit cf7d7a5

Browse files
committed
rely on CryptoCore to provide the shuffling primitives
1 parent 8ebcd9f commit cf7d7a5

2 files changed

Lines changed: 7 additions & 17 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ curve25519 = ["cosmian_crypto_core/curve25519"]
4545
test-utils = []
4646

4747
[dependencies]
48-
cosmian_crypto_core = { git = "https://github.com/Cosmian/crypto_core.git", branch = "develop", default-features = false, features = ["ser", "sha3", "aes"] }
48+
cosmian_crypto_core = { git = "https://github.com/Cosmian/crypto_core.git", branch = "feat/add-crypto-shuffle", default-features = false, features = ["ser", "sha3", "aes"] }
4949
elliptic-curve = { version = "0.13.8", optional = true }
5050
ml-kem = { version = "0.2.1", features = ["zeroize"] }
5151
p256 = { version = "0.13.2", optional = true }

src/core/primitives.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use std::{
44
};
55

66
use cosmian_crypto_core::{
7-
bytes_ser_de::Serializable,
8-
reexport::rand_core::{CryptoRngCore, RngCore},
7+
bytes_ser_de::Serializable, reexport::rand_core::CryptoRngCore, shuffle, shuffle_in_place,
98
RandomFixedSizeCBytes, Secret, SymmetricKey,
109
};
1110

@@ -45,13 +44,6 @@ fn xor_in_place<const LENGTH: usize>(
4544
lhs
4645
}
4746

48-
fn shuffle<T>(xs: &mut [T], rng: &mut impl RngCore) {
49-
for i in 0..xs.len() {
50-
let j = rng.next_u32() as usize % xs.len();
51-
xs.swap(i, j);
52-
}
53-
}
54-
5547
/// Computes the signature of the given USK using the MSK.
5648
fn sign(
5749
msk: &MasterSecretKey,
@@ -325,7 +317,7 @@ pub fn encaps(
325317
// rights are hashed in-order. If shuffling is performed after generating
326318
// the encapsulations, there would be no way to know in which order to
327319
// perform hashing upon decapsulation.
328-
shuffle(&mut coordinate_keys, rng);
320+
shuffle_in_place(&mut coordinate_keys, rng);
329321

330322
let S = Secret::random(rng);
331323
let r = G_hash(&S)?;
@@ -377,14 +369,13 @@ fn h_decaps(
377369

378370
// Shuffle encapsulation to counter timing attacks attempting to determine
379371
// which right was used to open an encapsulation.
380-
let mut encs = encs.iter().collect::<Vec<_>>();
381-
shuffle(&mut encs, rng);
372+
let encs = shuffle(encs, rng);
382373

383374
// Loop order matters: this ordering is faster.
384375
for mut revision in usk.secrets.revisions() {
385376
// Shuffle secrets to counter timing attacks attempting to determine
386377
// whether successive encapsulations target the same user right.
387-
shuffle(&mut revision, rng);
378+
shuffle_in_place(&mut revision, rng);
388379
for (E, F) in &encs {
389380
for (_, secret) in &revision {
390381
if let RightSecretKey::Hybridized { sk, dk } = secret {
@@ -440,14 +431,13 @@ fn c_decaps(
440431

441432
// Shuffle encapsulation to counter timing attacks attempting to determine
442433
// which right was used to open an encapsulation.
443-
let mut encs = encs.iter().collect::<Vec<_>>();
444-
shuffle(&mut encs, rng);
434+
let encs = shuffle(encs, rng);
445435

446436
// Loop order matters: this ordering is faster.
447437
for mut revision in usk.secrets.revisions() {
448438
// Shuffle secrets to counter timing attacks attempting to determine
449439
// whether successive encapsulations target the same user right.
450-
shuffle(&mut revision, rng);
440+
shuffle_in_place(&mut revision, rng);
451441
for F in &encs {
452442
for (_, secret) in &revision {
453443
let sk = match secret {

0 commit comments

Comments
 (0)