Skip to content

Commit

Permalink
Change the structure of Params
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Sep 17, 2023
1 parent 97cf90e commit fae9121
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(), &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);

Expand Down
13 changes: 7 additions & 6 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -78,16 +77,16 @@ impl Header {
/// The number of bytes of the header.
pub const SIZE: usize = mem::size_of::<MagicNumber>()
+ mem::size_of::<Version>()
+ mem::size_of::<u8>()
+ (mem::size_of::<u32>() * 2)
+ (mem::size_of::<Params>() - (mem::align_of::<Params>() - mem::size_of::<u8>()))
+ mem::size_of::<Salt>()
+ mem::size_of::<Checksum>()
+ <HeaderMac as OutputSizeUser>::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();
Expand Down Expand Up @@ -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");
Expand Down
54 changes: 44 additions & 10 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -33,7 +37,7 @@ impl Params {
/// ```
pub fn new(ciphertext: impl AsRef<[u8]>) -> Result<Self> {
let params = Header::parse(ciphertext.as_ref()).map(|h| h.params())?;
Ok(Self(params))
Ok(params)
}

/// Gets log2 of the scrypt parameter `N`.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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<Params> 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<scrypt::Params> 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 }
}
}

0 comments on commit fae9121

Please sign in to comment.