Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[246] Replace ring with sha2 #247

Merged
merged 7 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Pearl changelog
- Index regeneration checks record data checksum (#215)

#### Changed
- `ring` crate replaced with `sha2` crate (#246)
- `async-std` replaced with `async-lock` to avoid redundant dependencies (#247)
- Change locks for filters update (#218)
- Serialization moved out of critical section (#188)
- Removed multiple header checksum calculations (#206)
Expand Down
18 changes: 11 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ ahash = "=0.7.6"
anyhow = "1.0"
async-trait = "0.1"
bincode = "1.3"
chrono = "0.4"
clap = { version = "3.2", optional = true }
crc = "=3.0.0"
env_logger = "0.9"
futures = "0.3"
log = "0.4"
rand = "0.8"
ring = "0.16"
sha2 = "0.10"
rio = "0.9.4"
serde = "1.0"
serde_derive = "1.0"
Expand All @@ -44,15 +40,18 @@ nix = "0.25.0"
libc = "0.2"
static_assertions = "1.1.0"
bytes = "1.2"
async-std = "1.12"
async-lock = "2.6"
# Benchmark only dependencies
clap = { version = "3.2", optional = true }
env_logger = { version = "0.9", optional = true }

[dependencies.tokio]
version = "1.21"
features = ["fs", "io-util", "sync", "time", "rt", "macros", "rt-multi-thread"]

[features]
# default = ["benchmark"]
benchmark = ["clap"]
benchmark = ["clap", "env_logger"]

[lib]
name = "pearl"
Expand All @@ -64,3 +63,8 @@ required-features = ["benchmark"]

[build-dependencies]
chrono = "0.4"

[dev-dependencies]
env_logger = "0.9"
chrono = "0.4"
rand = "0.8"
2 changes: 1 addition & 1 deletion src/blob/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::SystemTime;

use async_std::sync::RwLockUpgradableReadGuard;
use async_lock::RwLockUpgradableReadGuard;
use bytes::{BufMut, Bytes, BytesMut};
use tokio::time::Instant;

Expand Down
8 changes: 4 additions & 4 deletions src/blob/index/bptree/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,13 @@ where
}

fn hash_valid(header: &IndexHeader, buf: &mut [u8]) -> Result<bool> {
let hash = header.hash.clone();
let hash = &header.hash;
let mut header = header.clone();
header.hash = vec![0; ring::digest::SHA256.output_len];
header.reset_hash();
header.set_written(false);
serialize_into(&mut buf[..], &header)?;
let new_hash = get_hash(&buf);
Ok(hash == new_hash)
let new_hash = IndexHashCalculator::get_hash(&buf);
Ok(*hash == new_hash)
}

async fn read_index_header(file: &File) -> Result<IndexHeader> {
Expand Down
2 changes: 1 addition & 1 deletion src/blob/index/bptree/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ where
buf.extend_from_slice(&self.meta_buf);
buf.extend_from_slice(&self.tree_buf);
Self::append_headers(self.headers_btree, &mut buf)?;
let hash = get_hash(&buf);
let hash = IndexHashCalculator::get_hash(&buf);
let header = IndexHeader::with_hash(
self.header.record_header_size,
self.header.records_count,
Expand Down
10 changes: 9 additions & 1 deletion src/blob/index/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ impl IndexHeader {
pub(crate) fn blob_size(&self) -> u64 {
self.blob_size
}

pub(crate) fn reset_hash(&mut self) {
if self.hash.len() == IndexHashCalculator::HASH_LENGTH {
self.hash.fill(0);
} else {
self.hash = vec![0; IndexHashCalculator::HASH_LENGTH];
}
}
}

impl Default for IndexHeader {
Expand All @@ -117,7 +125,7 @@ impl Default for IndexHeader {
record_header_size: 0,
meta_size: 0,
blob_size: 0,
hash: vec![0; ring::digest::SHA256.output_len],
hash: vec![0; IndexHashCalculator::HASH_LENGTH],
version: HEADER_VERSION << 1,
key_size: 0,
magic_byte: INDEX_HEADER_MAGIC_BYTE,
Expand Down
10 changes: 5 additions & 5 deletions src/blob/index/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ where
// helpers
impl SimpleFileIndex {
fn hash_valid(header: &IndexHeader, buf: &mut [u8]) -> Result<bool> {
let hash = header.hash.clone();
let hash = &header.hash;
let mut header = header.clone();
header.hash = vec![0; ring::digest::SHA256.output_len];
header.reset_hash();
header.set_written(false);
serialize_into(&mut buf[..], &header)?;
let new_hash = get_hash(&buf);
Ok(hash == new_hash)
let new_hash = IndexHashCalculator::get_hash(&buf);
Ok(*hash == new_hash)
}

async fn read_index_header(file: &File) -> Result<IndexHeader> {
Expand Down Expand Up @@ -348,7 +348,7 @@ impl SimpleFileIndex {
"blob index simple serialize headers buf len after: {}",
buf.len()
);
let hash = get_hash(&buf);
let hash = IndexHashCalculator::get_hash(&buf);
let header = IndexHeader::with_hash(
record_header_size,
headers.len(),
Expand Down
33 changes: 27 additions & 6 deletions src/blob/index/tools.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use super::prelude::*;
use std::mem::size_of;

pub(crate) fn get_hash(buf: &[u8]) -> Vec<u8> {
use ring::digest::{Context, SHA256};
let mut context = Context::new(&SHA256);
context.update(buf);
let digest = context.finish();
digest.as_ref().to_vec()
/// Hash calculation helper for index
pub(crate) struct IndexHashCalculator;

impl IndexHashCalculator {
pub(crate) const HASH_LENGTH: usize = 32;

pub(crate) fn get_hash(buf: &[u8]) -> Vec<u8> {
use sha2::{Sha256, Digest};
let digest = Sha256::digest(buf);
digest.to_vec()
}
}

// if there is no elements, data will be wrong (because we can't get key_size),
Expand Down Expand Up @@ -46,3 +51,19 @@ pub(crate) fn clean_file(path: impl AsRef<Path>, recreate_index_file: bool) -> R
Err(anyhow!(msg))
}
}


#[cfg(test)]
mod tests {
use super::IndexHashCalculator;

#[test]
pub fn test_hash_compatibility() {
let data_vec: Vec<u8> = (0..1024).into_iter().map(|i| (i % 256) as u8).collect();
// SHA256 hash calculated with ring crate
let expected_hash = vec![120, 91, 7, 81, 252, 44, 83, 220, 20, 164, 206, 61, 128, 14, 105, 239, 156, 225, 0, 158, 179, 39, 204, 244, 88, 175, 224, 156, 36, 44, 38, 201];
let actual_hash = IndexHashCalculator::get_hash(&data_vec);

assert_eq!(expected_hash, actual_hash);
}
}
2 changes: 1 addition & 1 deletion src/blob/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) use super::prelude::*;

mod prelude {
pub(crate) use super::*;
pub(crate) use async_std::sync::{
pub(crate) use async_lock::{
RwLock as ASRwLock, RwLockUpgradableReadGuard as ASRwLockUpgradableReadGuard,
};
pub(crate) use index::Index;
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ extern crate serde_derive;
extern crate anyhow;

extern crate bytes;
extern crate ring;

/// Basic info about current build.
pub mod build_info;
Expand Down
2 changes: 1 addition & 1 deletion src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use self::{
};

mod prelude {
pub(crate) use async_std::sync::RwLock as ASRwLock;
pub(crate) use async_lock::RwLock as ASRwLock;
pub(crate) use {
super::{
config::Config, core::Inner, observer::Msg, observer::Observer,
Expand Down