Skip to content

Commit 64bf1fb

Browse files
argon2: simplify memory view implementation and internal API
1 parent 0d96d2d commit 64bf1fb

File tree

2 files changed

+220
-279
lines changed

2 files changed

+220
-279
lines changed

argon2/src/lib.rs

Lines changed: 118 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,11 @@ pub use {
175175
use crate::blake2b_long::blake2b_long;
176176
use blake2::{Blake2b512, Digest, digest};
177177
use core::fmt;
178-
use memory::SegmentViews;
178+
use memory::Memory;
179179

180180
#[cfg(all(feature = "alloc", feature = "password-hash"))]
181181
use password_hash::{Decimal, Ident, ParamsString, Salt};
182182

183-
#[cfg(feature = "parallel")]
184-
use rayon::prelude::*;
185-
186183
#[cfg(feature = "zeroize")]
187184
use zeroize::Zeroize;
188185

@@ -388,140 +385,133 @@ impl<'key> Argon2<'key> {
388385

389386
// Run passes on blocks
390387
for pass in 0..iterations {
391-
for slice in 0..SYNC_POINTS {
388+
memory_blocks.for_each_segment(lanes, |mut memory_view, slice, lane| {
392389
let data_independent_addressing = self.algorithm == Algorithm::Argon2i
393390
|| (self.algorithm == Algorithm::Argon2id
394391
&& pass == 0
395392
&& slice < SYNC_POINTS / 2);
396393

397-
memory_blocks
398-
.segment_views(slice, lanes)
399-
.for_each(|mut memory_view| {
400-
let lane = memory_view.lane();
401-
//for (lane, mut memory_view) in memory.as_parallel_views(slice, lanes).enumerate() {
402-
let mut address_block = Block::default();
403-
let mut input_block = Block::default();
404-
let zero_block = Block::default();
405-
406-
if data_independent_addressing {
407-
input_block.as_mut()[..6].copy_from_slice(&[
408-
pass as u64,
409-
lane as u64,
410-
slice as u64,
411-
memory_view.block_count() as u64,
412-
iterations as u64,
413-
self.algorithm as u64,
414-
]);
394+
let mut address_block = Block::default();
395+
let mut input_block = Block::default();
396+
let zero_block = Block::default();
397+
398+
if data_independent_addressing {
399+
input_block.as_mut()[..6].copy_from_slice(&[
400+
pass as u64,
401+
lane as u64,
402+
slice as u64,
403+
block_count as u64,
404+
iterations as u64,
405+
self.algorithm as u64,
406+
]);
407+
}
408+
409+
let first_block = if pass == 0 && slice == 0 {
410+
if data_independent_addressing {
411+
// Generate first set of addresses
412+
self.update_address_block(
413+
&mut address_block,
414+
&mut input_block,
415+
&zero_block,
416+
);
417+
}
418+
419+
// The first two blocks of each lane are already initialized
420+
2
421+
} else {
422+
0
423+
};
424+
425+
let mut cur_index = lane * lane_length + slice * segment_length + first_block;
426+
let mut prev_index = if slice == 0 && first_block == 0 {
427+
// Last block in current lane
428+
cur_index + lane_length - 1
429+
} else {
430+
// Previous block
431+
cur_index - 1
432+
};
433+
434+
// Fill blocks in the segment
435+
for block in first_block..segment_length {
436+
// Extract entropy
437+
let rand = if data_independent_addressing {
438+
let addres_index = block % ADDRESSES_IN_BLOCK;
439+
440+
if addres_index == 0 {
441+
self.update_address_block(
442+
&mut address_block,
443+
&mut input_block,
444+
&zero_block,
445+
);
415446
}
416447

417-
let first_block = if pass == 0 && slice == 0 {
418-
if data_independent_addressing {
419-
// Generate first set of addresses
420-
self.update_address_block(
421-
&mut address_block,
422-
&mut input_block,
423-
&zero_block,
424-
);
425-
}
426-
427-
// The first two blocks of each lane are already initialized
428-
2
448+
address_block.as_ref()[addres_index]
449+
} else {
450+
memory_view.get_block(prev_index).as_ref()[0]
451+
};
452+
453+
// Calculate source block index for compress function
454+
let ref_lane = if pass == 0 && slice == 0 {
455+
// Cannot reference other lanes yet
456+
lane
457+
} else {
458+
(rand >> 32) as usize % lanes
459+
};
460+
461+
let reference_area_size = if pass == 0 {
462+
// First pass
463+
if slice == 0 {
464+
// First slice
465+
block - 1 // all but the previous
466+
} else if ref_lane == lane {
467+
// The same lane => add current segment
468+
slice * segment_length + block - 1
429469
} else {
430-
0
431-
};
432-
433-
let mut cur_index =
434-
lane * lane_length + slice * segment_length + first_block;
435-
let mut prev_index = if slice == 0 && first_block == 0 {
436-
// Last block in current lane
437-
cur_index + lane_length - 1
470+
slice * segment_length - if block == 0 { 1 } else { 0 }
471+
}
472+
} else {
473+
// Second pass
474+
if ref_lane == lane {
475+
lane_length - segment_length + block - 1
438476
} else {
439-
// Previous block
440-
cur_index - 1
441-
};
442-
443-
// Fill blocks in the segment
444-
for block in first_block..segment_length {
445-
// Extract entropy
446-
let rand = if data_independent_addressing {
447-
let addres_index = block % ADDRESSES_IN_BLOCK;
448-
449-
if addres_index == 0 {
450-
self.update_address_block(
451-
&mut address_block,
452-
&mut input_block,
453-
&zero_block,
454-
);
455-
}
456-
457-
address_block.as_ref()[addres_index]
458-
} else {
459-
memory_view.get_block(prev_index).as_ref()[0]
460-
};
461-
462-
// Calculate source block index for compress function
463-
let ref_lane = if pass == 0 && slice == 0 {
464-
// Cannot reference other lanes yet
465-
lane
466-
} else {
467-
(rand >> 32) as usize % lanes
468-
};
469-
470-
let reference_area_size = if pass == 0 {
471-
// First pass
472-
if slice == 0 {
473-
// First slice
474-
block - 1 // all but the previous
475-
} else if ref_lane == lane {
476-
// The same lane => add current segment
477-
slice * segment_length + block - 1
478-
} else {
479-
slice * segment_length - if block == 0 { 1 } else { 0 }
480-
}
481-
} else {
482-
// Second pass
483-
if ref_lane == lane {
484-
lane_length - segment_length + block - 1
485-
} else {
486-
lane_length - segment_length - if block == 0 { 1 } else { 0 }
487-
}
488-
};
489-
490-
// 1.2.4. Mapping rand to 0..<reference_area_size-1> and produce
491-
// relative position
492-
let mut map = rand & 0xFFFFFFFF;
493-
map = (map * map) >> 32;
494-
let relative_position = reference_area_size
495-
- 1
496-
- ((reference_area_size as u64 * map) >> 32) as usize;
497-
498-
// 1.2.5 Computing starting position
499-
let start_position = if pass != 0 && slice != SYNC_POINTS - 1 {
500-
(slice + 1) * segment_length
501-
} else {
502-
0
503-
};
504-
505-
let lane_index = (start_position + relative_position) % lane_length;
506-
let ref_index = ref_lane * lane_length + lane_index;
507-
508-
// Calculate new block
509-
let result = self.compress(
510-
memory_view.get_block(prev_index),
511-
memory_view.get_block(ref_index),
512-
);
513-
514-
if self.version == Version::V0x10 || pass == 0 {
515-
*memory_view.get_block_mut(cur_index) = result;
516-
} else {
517-
*memory_view.get_block_mut(cur_index) ^= &result;
518-
};
519-
520-
prev_index = cur_index;
521-
cur_index += 1;
477+
lane_length - segment_length - if block == 0 { 1 } else { 0 }
522478
}
523-
});
524-
}
479+
};
480+
481+
// 1.2.4. Mapping rand to 0..<reference_area_size-1> and produce
482+
// relative position
483+
let mut map = rand & 0xFFFFFFFF;
484+
map = (map * map) >> 32;
485+
let relative_position = reference_area_size
486+
- 1
487+
- ((reference_area_size as u64 * map) >> 32) as usize;
488+
489+
// 1.2.5 Computing starting position
490+
let start_position = if pass != 0 && slice != SYNC_POINTS - 1 {
491+
(slice + 1) * segment_length
492+
} else {
493+
0
494+
};
495+
496+
let lane_index = (start_position + relative_position) % lane_length;
497+
let ref_index = ref_lane * lane_length + lane_index;
498+
499+
// Calculate new block
500+
let result = self.compress(
501+
memory_view.get_block(prev_index),
502+
memory_view.get_block(ref_index),
503+
);
504+
505+
if self.version == Version::V0x10 || pass == 0 {
506+
*memory_view.get_block_mut(cur_index) = result;
507+
} else {
508+
*memory_view.get_block_mut(cur_index) ^= &result;
509+
};
510+
511+
prev_index = cur_index;
512+
cur_index += 1;
513+
}
514+
});
525515
}
526516

527517
Ok(())

0 commit comments

Comments
 (0)