From fae91217ab893b09a25fc01f0a6f50ed4650747c Mon Sep 17 00:00:00 2001 From: Shun Sakai Date: Sun, 17 Sep 2023 13:07:23 +0900 Subject: [PATCH] Change the structure of `Params` --- CHANGELOG.adoc | 6 ++++++ src/decrypt.rs | 2 +- src/encrypt.rs | 2 +- src/format.rs | 13 ++++++------ src/params.rs | 54 ++++++++++++++++++++++++++++++++++++++++---------- 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index bfa8bd1..945c830 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -14,6 +14,12 @@ All notable changes to this project will be documented in this file. The format is based on https://keepachangelog.com/[Keep a Changelog], and this project adheres to https://semver.org/[Semantic Versioning]. +== {compare-url}/v0.8.1\...HEAD[Unreleased] + +=== Changed + +* Change the structure of `Params` ({pull-request-url}/70[#70]) + == {compare-url}/v0.8.0\...v0.8.1[0.8.1] - 2023-09-07 === Changed diff --git a/src/decrypt.rs b/src/decrypt.rs index bf7f6df..cd3eade 100644 --- a/src/decrypt.rs +++ b/src/decrypt.rs @@ -59,7 +59,7 @@ impl<'c> Decryptor<'c> { // The derived key size is 64 bytes. The first 256 bits are for AES-256-CTR key, // and the last 256 bits are for HMAC-SHA-256 key. let mut dk = [u8::default(); DerivedKey::SIZE]; - scrypt::scrypt(passphrase, &header.salt(), &header.params(), &mut dk) + scrypt::scrypt(passphrase, &header.salt(), &header.params().into(), &mut dk) .expect("derived key size should be 64 bytes"); let dk = DerivedKey::new(dk); diff --git a/src/encrypt.rs b/src/encrypt.rs index 73f7e50..480dd4e 100644 --- a/src/encrypt.rs +++ b/src/encrypt.rs @@ -69,7 +69,7 @@ impl<'m> Encryptor<'m> { // The derived key size is 64 bytes. The first 256 bits are for AES-256-CTR key, // and the last 256 bits are for HMAC-SHA-256 key. let mut dk = [u8::default(); DerivedKey::SIZE]; - scrypt::scrypt(passphrase, &header.salt(), ¶ms, &mut dk) + scrypt::scrypt(passphrase, &header.salt(), &header.params().into(), &mut dk) .expect("derived key size should be 64 bytes"); let dk = DerivedKey::new(dk); diff --git a/src/format.rs b/src/format.rs index ad68c32..56c1994 100644 --- a/src/format.rs +++ b/src/format.rs @@ -15,12 +15,11 @@ use hmac::{ Mac, }; use rand::{rngs::StdRng, Rng, SeedableRng}; -use scrypt::Params; use sha2::{Digest, Sha256}; use crate::{ error::{Error, Result}, - Aes256Ctr128BE, HmacSha256, HmacSha256Key, HmacSha256Output, + Aes256Ctr128BE, HmacSha256, HmacSha256Key, HmacSha256Output, Params, }; /// A type alias for magic number of the scrypt encrypted data format. @@ -78,16 +77,16 @@ impl Header { /// The number of bytes of the header. pub const SIZE: usize = mem::size_of::() + mem::size_of::() - + mem::size_of::() - + (mem::size_of::() * 2) + + (mem::size_of::() - (mem::align_of::() - mem::size_of::())) + mem::size_of::() + mem::size_of::() + ::OutputSize::USIZE; /// Creates a new `Header`. - pub fn new(params: Params) -> Self { + pub fn new(params: scrypt::Params) -> Self { let magic_number = Self::MAGIC_NUMBER; let version = Version::V0; + let params = params.into(); let salt = StdRng::from_entropy().gen(); let checksum = Checksum::default(); let mac = HeaderMacOutput::default(); @@ -128,7 +127,9 @@ impl Header { .try_into() .expect("size of `p` parameter should be 4 bytes"), ); - let params = Params::new(log_n, r, p, Params::RECOMMENDED_LEN).map_err(Error::from)?; + let params = scrypt::Params::new(log_n, r, p, scrypt::Params::RECOMMENDED_LEN) + .map(Params::from) + .map_err(Error::from)?; let salt = data[16..48] .try_into() .expect("size of salt should be 32 bytes"); diff --git a/src/params.rs b/src/params.rs index 4d4298f..cad1321 100644 --- a/src/params.rs +++ b/src/params.rs @@ -8,7 +8,11 @@ use crate::{error::Result, format::Header}; /// The scrypt parameters used for the encrypted data. #[derive(Clone, Copy, Debug)] -pub struct Params(scrypt::Params); +pub struct Params { + log_n: u8, + r: u32, + p: u32, +} impl Params { /// Creates a new instance of the scrypt parameters from `ciphertext`. @@ -33,7 +37,7 @@ impl Params { /// ``` pub fn new(ciphertext: impl AsRef<[u8]>) -> Result { let params = Header::parse(ciphertext.as_ref()).map(|h| h.params())?; - Ok(Self(params)) + Ok(params) } /// Gets log2 of the scrypt parameter `N`. @@ -50,8 +54,8 @@ impl Params { /// ``` #[must_use] #[inline] - pub fn log_n(&self) -> u8 { - self.0.log_n() + pub const fn log_n(&self) -> u8 { + self.log_n } /// Gets `N` parameter. @@ -68,8 +72,8 @@ impl Params { /// ``` #[must_use] #[inline] - pub fn n(&self) -> u64 { - 1 << self.0.log_n() + pub const fn n(&self) -> u64 { + 1 << self.log_n } /// Gets `r` parameter. @@ -86,8 +90,8 @@ impl Params { /// ``` #[must_use] #[inline] - pub fn r(&self) -> u32 { - self.0.r() + pub const fn r(&self) -> u32 { + self.r } /// Gets `p` parameter. @@ -104,7 +108,37 @@ impl Params { /// ``` #[must_use] #[inline] - pub fn p(&self) -> u32 { - self.0.p() + pub const fn p(&self) -> u32 { + self.p + } +} + +impl Default for Params { + fn default() -> Self { + let (log_n, r, p) = ( + scrypt::Params::RECOMMENDED_LOG_N, + scrypt::Params::RECOMMENDED_R, + scrypt::Params::RECOMMENDED_P, + ); + Self { log_n, r, p } + } +} + +impl From for scrypt::Params { + fn from(params: Params) -> Self { + Self::new( + params.log_n(), + params.r(), + params.p(), + Self::RECOMMENDED_LEN, + ) + .expect("`Params` should be valid as `scrypt::Params`") + } +} + +impl From for Params { + fn from(params: scrypt::Params) -> Self { + let (log_n, r, p) = (params.log_n(), params.r(), params.p()); + Self { log_n, r, p } } }