use core::marker::PhantomData;
#[cfg(feature = "alloc")]
use generic_array::typenum::{B1, IsGreaterOrEqual};
use generic_array::{
ArrayLength, GenericArray,
typenum::{NonZero, U1, U2, U3, U4, U8, U10, U14, U16, U20, U53, U64, Unsigned},
};
use crate::{
Align64, ValidCostFactor,
fixed_r::{Block, BufferSet},
pbkdf2_1::Pbkdf2HmacSha256State,
pipeline::PipelineContext,
};
pub struct CaseN16R1P1;
impl CaseP1 for CaseN16R1P1 {
type OutputLen = U64;
type CF = U4;
type R = U1;
const PASSWORD: &'static [u8] = b"";
const SALT: &'static [u8] = b"";
const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20, 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04,
0x97, 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8, 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2,
0x14, 0x42, 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8, 0x32, 0x6a, 0x75, 0x3a, 0x0f,
0xc8, 0x1f, 0x17, 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28, 0xcf, 0x35, 0xe2, 0x0c,
0x38, 0xd1, 0x89, 0x06,
]);
}
pub struct CaseN16384R8P1;
impl CaseP1 for CaseN16384R8P1 {
type OutputLen = U64;
type CF = U14;
type R = U8;
const PASSWORD: &'static [u8] = b"pleaseletmein";
const SALT: &'static [u8] = b"SodiumChloride";
const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48, 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38,
0xeb, 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e, 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d,
0xa1, 0xf2, 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf, 0x62, 0xd4, 0x97, 0x05, 0x24,
0x2a, 0x9a, 0xf9, 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40, 0xdf, 0xcf, 0x01, 0x7b,
0x45, 0x57, 0x58, 0x87,
]);
}
pub struct CaseN1048576R8P1;
impl CaseP1 for CaseN1048576R8P1 {
type OutputLen = U64;
type CF = U20;
type R = U8;
const PASSWORD: &'static [u8] = b"pleaseletmein";
const SALT: &'static [u8] = b"SodiumChloride";
const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae, 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8,
0x81, 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d, 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad,
0xaa, 0x47, 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f, 0xfa, 0x1c, 0x6d, 0x92, 0x7c,
0x40, 0xf4, 0xc3, 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb, 0xcb, 0xf4, 0x5c, 0x6f,
0xa7, 0x7a, 0x41, 0xa4,
]);
}
pub struct CaseN1024R1P2;
impl Case for CaseN1024R1P2 {
type OutputLen = U64;
type CF = U10;
type R = U1;
type P = U2;
const PASSWORD: &'static [u8] = b"password";
const SALT: &'static [u8] = b"NaCl";
const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
0x09, 0xc4, 0x23, 0x86, 0xb2, 0x46, 0x97, 0x53, 0xeb, 0x76, 0x27, 0x75, 0x15, 0xbe, 0xff,
0x09, 0x80, 0x9d, 0x18, 0xd9, 0x3f, 0xb4, 0xd3, 0x16, 0xea, 0xe1, 0xa8, 0x63, 0x43, 0x9a,
0x48, 0x98, 0x17, 0xcf, 0x56, 0xa5, 0x87, 0x69, 0xcc, 0x13, 0xbd, 0xb3, 0x33, 0x14, 0x11,
0xcc, 0xd7, 0xd5, 0x7f, 0x8e, 0x43, 0x9b, 0xa1, 0xa4, 0x84, 0x58, 0x0f, 0x41, 0x9f, 0x7c,
0x8e, 0x34, 0x99, 0x41,
]);
}
#[cfg(feature = "alloc")]
pub struct CaseN1024R8P16;
#[cfg(feature = "alloc")]
impl Case for CaseN1024R8P16 {
type P = U16;
type OutputLen = U64;
type CF = U10;
type R = U8;
const PASSWORD: &'static [u8] = b"password";
const SALT: &'static [u8] = b"NaCl";
const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00, 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9,
0xfe, 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30, 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37,
0x31, 0x62, 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88, 0x6f, 0xf1, 0x09, 0x27, 0x9d,
0x98, 0x30, 0xda, 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d, 0x83, 0x60, 0xcb, 0xdf,
0xa2, 0xcc, 0x06, 0x40,
]);
}
pub struct CaseN1024R53P1;
impl CaseP1 for CaseN1024R53P1 {
type OutputLen = U64;
type CF = U10;
type R = U53;
const PASSWORD: &'static [u8] = b"password";
const SALT: &'static [u8] = b"NaCl";
const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
0x7c, 0x34, 0x45, 0xf8, 0x73, 0xde, 0x41, 0x9a, 0x96, 0xd7, 0xa3, 0x20, 0x51, 0xd1, 0xb4,
0x8f, 0xb3, 0xde, 0x94, 0x8d, 0xac, 0x06, 0x78, 0x57, 0xf3, 0x7a, 0x58, 0xdf, 0x2d, 0x71,
0xeb, 0x4d, 0x4b, 0xeb, 0x87, 0xd6, 0xe8, 0x7d, 0x70, 0xd4, 0xb7, 0xa5, 0x86, 0x66, 0x99,
0xe4, 0x7a, 0xcd, 0x09, 0x4e, 0x93, 0x4e, 0xc4, 0xa1, 0xd7, 0xb8, 0x7d, 0x53, 0x93, 0x10,
0x7e, 0x75, 0xdc, 0x70,
]);
}
pub struct CaseN1024R53P3;
impl Case for CaseN1024R53P3 {
type P = U3;
type OutputLen = U64;
type CF = U10;
type R = U53;
const PASSWORD: &'static [u8] = b"password";
const SALT: &'static [u8] = b"NaCl";
const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen> = GenericArray::from_array([
0x98, 0x13, 0x74, 0xb7, 0x0a, 0x02, 0x9a, 0x5b, 0x8a, 0xe0, 0x36, 0x28, 0x62, 0x84, 0x9f,
0xed, 0x87, 0xd7, 0x3e, 0x29, 0xf6, 0x08, 0x99, 0xda, 0x4f, 0xc5, 0x37, 0xa3, 0xc0, 0x6b,
0x36, 0x81, 0x04, 0x4a, 0xbf, 0x68, 0x0c, 0x6f, 0x40, 0x68, 0xf0, 0x4f, 0x5f, 0xa2, 0x8c,
0x8a, 0x16, 0x32, 0x26, 0x5b, 0xdc, 0x82, 0xa6, 0xa8, 0x06, 0xce, 0x08, 0x9a, 0x62, 0x18,
0xca, 0x02, 0x93, 0xb2,
]);
}
pub trait CaseP1 {
type OutputLen: ArrayLength + NonZero;
type CF: ValidCostFactor;
type R: ArrayLength + NonZero;
const PASSWORD: &'static [u8];
const SALT: &'static [u8];
const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen>;
fn algorithm_self_test() {
#[cfg(not(feature = "alloc"))]
let mut buffer0: GenericArray<
Align64<Block<Self::R>>,
<Self::CF as ValidCostFactor>::MinimumBlocks,
> = GenericArray::default();
#[cfg(feature = "alloc")]
let mut buffer0: alloc::boxed::Box<
GenericArray<Align64<Block<Self::R>>, <Self::CF as ValidCostFactor>::MinimumBlocks>,
> = unsafe { alloc::boxed::Box::new_uninit().assume_init() };
#[cfg(not(feature = "alloc"))]
let mut buffer1: GenericArray<
Align64<Block<Self::R>>,
<Self::CF as ValidCostFactor>::MinimumBlocks,
> = GenericArray::default();
#[cfg(feature = "alloc")]
let mut buffer1: alloc::boxed::Box<
GenericArray<Align64<Block<Self::R>>, <Self::CF as ValidCostFactor>::MinimumBlocks>,
> = unsafe { alloc::boxed::Box::new_uninit().assume_init() };
let mut buffer_set0 = BufferSet::<_, Self::R>::new(buffer0.as_mut_slice());
let mut buffer_set1 = BufferSet::<_, Self::R>::new(buffer1.as_mut_slice());
assert_eq!(buffer_set0.n(), 1 << Self::CF::U8);
assert_eq!(buffer_set1.n(), 1 << Self::CF::U8);
let mut output0 = GenericArray::default();
let mut output1 = GenericArray::default();
let mut output_dummy = GenericArray::default();
#[cfg(feature = "std")]
{
crate::compat::scrypt(
Self::PASSWORD,
Self::SALT,
Self::CF::U8.try_into().unwrap(),
Self::R::U32.try_into().unwrap(),
1.try_into().unwrap(),
output0.as_mut_slice(),
);
assert_eq!(output0, Self::KNOWN_ANSWER);
output0.fill(0);
}
let hmac_state = Pbkdf2HmacSha256State::new(Self::PASSWORD);
let hmac_state_dummy = Pbkdf2HmacSha256State::new(b"not a password");
buffer_set0.set_input(&hmac_state, Self::SALT);
buffer_set0.scrypt_ro_mix();
buffer_set0.extract_output(&hmac_state, &mut output0);
assert_eq!(output0, Self::KNOWN_ANSWER);
buffer_set0.set_input(&hmac_state_dummy, Self::SALT);
buffer_set0.scrypt_ro_mix();
buffer_set0.extract_output(&hmac_state_dummy, &mut output_dummy);
assert_ne!(output_dummy, Self::KNOWN_ANSWER, "stuck output");
buffer_set0.set_input(&hmac_state, Self::SALT);
buffer_set1.set_input(&hmac_state_dummy, Self::SALT);
buffer_set0.ro_mix_front();
buffer_set0.ro_mix_interleaved(&mut buffer_set1);
buffer_set1.ro_mix_back();
buffer_set0.extract_output(&hmac_state, &mut output0);
buffer_set1.extract_output(&hmac_state_dummy, &mut output1);
assert_eq!(output0, Self::KNOWN_ANSWER);
assert_eq!(output1, output_dummy);
buffer_set0.as_mut().iter_mut().for_each(|b| {
b.fill(0);
});
buffer_set1.as_mut().iter_mut().for_each(|b| {
b.fill(0);
});
}
fn pipeline_api_test() {
#[cfg(not(feature = "alloc"))]
let mut buffer0: GenericArray<
Align64<Block<Self::R>>,
<Self::CF as ValidCostFactor>::MinimumBlocks,
> = GenericArray::default();
#[cfg(feature = "alloc")]
let mut buffer0: alloc::boxed::Box<
GenericArray<Align64<Block<Self::R>>, <Self::CF as ValidCostFactor>::MinimumBlocks>,
> = unsafe { alloc::boxed::Box::new_uninit().assume_init() };
#[cfg(not(feature = "alloc"))]
let mut buffer1: GenericArray<
Align64<Block<Self::R>>,
<Self::CF as ValidCostFactor>::MinimumBlocks,
> = GenericArray::default();
#[cfg(feature = "alloc")]
let mut buffer1: alloc::boxed::Box<
GenericArray<Align64<Block<Self::R>>, <Self::CF as ValidCostFactor>::MinimumBlocks>,
> = unsafe { alloc::boxed::Box::new_uninit().assume_init() };
let hmac_state = Pbkdf2HmacSha256State::new(Self::PASSWORD);
let hmac_state_dummy = Pbkdf2HmacSha256State::new(b"not a password");
let mut buffer_set0 = BufferSet::<_, Self::R>::new(buffer0.as_mut_slice());
let mut buffer_set1 = BufferSet::<_, Self::R>::new(buffer1.as_mut_slice());
struct Context<R: ArrayLength + NonZero, OutputLen: ArrayLength + NonZero> {
i: usize,
salt: &'static [u8],
known_answer: GenericArray<u8, OutputLen>,
hmac_state: Pbkdf2HmacSha256State,
hmac_state_dummy: Pbkdf2HmacSha256State,
_marker: PhantomData<R>,
}
impl<R: ArrayLength + NonZero, OutputLen: ArrayLength + NonZero>
PipelineContext<usize, &mut [Align64<Block<R>>], R, ()> for Context<R, OutputLen>
{
fn begin(
&mut self,
_state: &mut usize,
buffer_set: &mut BufferSet<&mut [Align64<Block<R>>], R>,
) {
match self.i % 3 {
0 | 1 => {
buffer_set.set_input(&self.hmac_state, self.salt);
}
2 => {
buffer_set.set_input(&self.hmac_state_dummy, self.salt);
}
_ => unreachable!(),
}
}
fn drain(
self,
state: &mut usize,
buffer_set: &mut BufferSet<&mut [Align64<Block<R>>], R>,
) -> Option<()> {
match self.i % 3 {
0 | 1 => {
let mut output = GenericArray::<_, OutputLen>::default();
buffer_set.extract_output(&self.hmac_state, &mut output);
assert_eq!(output, self.known_answer);
}
2 => {
let mut output_dummy = GenericArray::<_, OutputLen>::default();
buffer_set.extract_output(&self.hmac_state_dummy, &mut output_dummy);
assert_ne!(output_dummy, self.known_answer, "stuck output");
}
_ => unreachable!(),
}
*state += self.i;
None
}
}
for pipeline_length in 0..11 {
let mut counter = 0;
buffer_set0.pipeline(
&mut buffer_set1,
(0..pipeline_length).map(|i| Context {
i,
hmac_state,
hmac_state_dummy,
salt: Self::SALT,
known_answer: Self::KNOWN_ANSWER,
_marker: PhantomData,
}),
&mut counter,
);
let expected_sum = pipeline_length * (pipeline_length.saturating_sub(1)) / 2;
assert_eq!(counter, expected_sum);
}
}
}
#[cfg(feature = "alloc")]
pub trait Case {
type P: ArrayLength + NonZero + IsGreaterOrEqual<U2, Output = B1>;
type OutputLen: ArrayLength + NonZero;
type CF: ValidCostFactor;
type R: ArrayLength + NonZero;
const PASSWORD: &'static [u8];
const SALT: &'static [u8];
const KNOWN_ANSWER: GenericArray<u8, Self::OutputLen>;
fn algorithm_self_test() {
let hmac_state = Pbkdf2HmacSha256State::new(Self::PASSWORD);
let mut input_buffers: GenericArray<Align64<Block<Self::R>>, Self::P> =
GenericArray::default();
let mut output_buffers: GenericArray<Align64<Block<Self::R>>, Self::P> =
GenericArray::default();
let mut buffer0 = BufferSet::<_, Self::R>::new_boxed(Self::CF::U8.try_into().unwrap());
let mut buffer1 = BufferSet::<_, Self::R>::new_boxed(Self::CF::U8.try_into().unwrap());
hmac_state.emit_scatter(Self::SALT, input_buffers.iter_mut());
buffer0.pipeline(
&mut buffer1,
input_buffers.iter().zip(output_buffers.iter_mut()),
&mut (),
);
let mut output = GenericArray::default();
#[cfg(feature = "std")]
{
crate::compat::scrypt(
Self::PASSWORD,
Self::SALT,
Self::CF::U8.try_into().unwrap(),
Self::R::U32.try_into().unwrap(),
Self::P::U32.try_into().unwrap(),
output.as_mut_slice(),
);
assert_eq!(output, Self::KNOWN_ANSWER);
output.fill(0);
}
hmac_state.emit_gather(output_buffers.iter(), &mut output);
assert_eq!(output, Self::KNOWN_ANSWER);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_case_16_1_1_algorithm_self_test() {
CaseN16R1P1::algorithm_self_test();
}
#[test]
fn test_case_16_1_1_pipeline_api_test() {
CaseN16R1P1::pipeline_api_test();
}
#[cfg(feature = "alloc")]
#[test]
fn test_case_16384_8_1_algorithm_self_test() {
CaseN16384R8P1::algorithm_self_test();
}
#[cfg(feature = "alloc")]
#[test]
fn test_case_1024_8_16_algorithm_self_test() {
CaseN1024R8P16::algorithm_self_test();
}
#[cfg(feature = "alloc")]
#[test]
fn test_case_1024_1_2_algorithm_self_test() {
CaseN1024R1P2::algorithm_self_test();
}
#[cfg(feature = "alloc")]
#[test]
fn test_case_1024_53_1_algorithm_self_test() {
CaseN1024R53P1::algorithm_self_test();
}
#[cfg(feature = "alloc")]
#[test]
fn test_case_1024_53_1_pipeline_api_test() {
CaseN1024R53P1::pipeline_api_test();
}
#[cfg(feature = "alloc")]
#[test]
fn test_case_1024_53_3_algorithm_self_test() {
CaseN1024R53P3::algorithm_self_test();
}
}