Skip to content

Commit

Permalink
⬆️ use crypto_base 2.0 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
heavenboy8 authored Jul 1, 2022
1 parent fbe42e3 commit 0643f97
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 48 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file.

---
## [3.1.0] - 2022-07-01
### Added
### Changed
- Bump the cosmian_crypto_base version
### Fixed
### Removed
---

---
## [3.0.1] - 2022-06-21
### Added
Expand Down
23 changes: 3 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cover_crypt"
version = "3.0.1"
version = "3.1.0"
edition = "2021"
authors = [
"Théophile Brezot <[email protected]>",
Expand Down Expand Up @@ -31,7 +31,7 @@ wasm_bindgen = ["js-sys", "wasm-bindgen"]
python = ["pyo3"]

[dependencies]
cosmian_crypto_base = "1.3"
cosmian_crypto_base = "2.0"
hex = "0.4"
leb128 = "0.2"
rand_core = { version = "0.5", features = ["getrandom"] }
Expand Down
6 changes: 2 additions & 4 deletions src/cover_crypt_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ where
}
let partition = Partition::from(partition_bytes);
let key_bytes = de.read_array()?;
let key =
<<KEM as Kem>::KeyPair as KeyPair>::PrivateKey::try_from_bytes(key_bytes.to_vec())?;
let key = <<KEM as Kem>::KeyPair as KeyPair>::PrivateKey::try_from_bytes(&key_bytes)?;
map.insert(partition, key);
}
Ok(Self(map))
Expand Down Expand Up @@ -242,8 +241,7 @@ where
}
let partition = Partition::from(partition_bytes);
let key_bytes = de.read_array()?;
let key =
<<KEM as Kem>::KeyPair as KeyPair>::PublicKey::try_from_bytes(key_bytes.to_vec())?;
let key = <<KEM as Kem>::KeyPair as KeyPair>::PublicKey::try_from_bytes(&key_bytes)?;
map.insert(partition, key);
}
Ok(Self(map))
Expand Down
12 changes: 6 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use std::{
num::{ParseIntError, TryFromIntError},
};

use cosmian_crypto_base::Error as CryptoError;
use cosmian_crypto_base::CryptoBaseError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("Unknown partition {0}")]
UnknownPartition(String),
#[error("{0}")]
CryptoError(CryptoError),
CryptoError(CryptoBaseError),
#[error("attribute not found: {0}")]
AttributeNotFound(String),
#[error("{} is missing{}",
Expand Down Expand Up @@ -94,13 +94,13 @@ impl From<ParseIntError> for Error {
}
}

impl From<cosmian_crypto_base::Error> for Error {
fn from(e: cosmian_crypto_base::Error) -> Self {
impl From<CryptoBaseError> for Error {
fn from(e: CryptoBaseError) -> Self {
match e {
CryptoError::SizeError { given, expected } => {
CryptoBaseError::SizeError { given, expected } => {
Error::InvalidSize(format!("expected: {}, given: {}", expected, given))
}
CryptoError::InvalidSize(e) => Error::InvalidSize(e),
CryptoBaseError::InvalidSize(e) => Error::InvalidSize(e),
e => Error::CryptoError(e),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/ffi/hybrid_cc_aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ pub unsafe extern "C" fn h_aes_encrypt_block(
let data = std::slice::from_raw_parts(data_ptr as *const u8, data_len as usize).to_vec();

let symmetric_key = ffi_unwrap!(<Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(
symmetric_key
&symmetric_key.to_vec()
));
let encrypted_block = ffi_unwrap!(encrypt_hybrid_block::<
X25519Crypto,
Expand Down Expand Up @@ -823,7 +823,7 @@ pub unsafe extern "C" fn h_aes_decrypt_block(
.to_vec();

let symmetric_key = ffi_unwrap!(<Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(
symmetric_key
&symmetric_key.to_vec()
));
let encrypted_block = ffi_unwrap!(decrypt_hybrid_block::<
X25519Crypto,
Expand Down
12 changes: 4 additions & 8 deletions src/interfaces/ffi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ unsafe fn encrypt_header(
))?;

let symmetric_key_ = <Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(
std::slice::from_raw_parts(symmetric_key_ptr as *const u8, symmetric_key_len as usize)
.to_vec(),
std::slice::from_raw_parts(symmetric_key_ptr as *const u8, symmetric_key_len as usize),
)
.map_err(|e| Error::Other(e.to_string()))?;

Expand Down Expand Up @@ -121,8 +120,7 @@ unsafe fn decrypt_header(
))?;

let symmetric_key_ = <Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(
std::slice::from_raw_parts(symmetric_key_ptr as *const u8, symmetric_key_len as usize)
.to_vec(),
std::slice::from_raw_parts(symmetric_key_ptr as *const u8, symmetric_key_len as usize),
)
.map_err(|e| Error::Other(e.to_string()))?;

Expand Down Expand Up @@ -275,8 +273,7 @@ unsafe fn encrypt_header_using_cache(
))?;

let symmetric_key_ = <Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(
std::slice::from_raw_parts(symmetric_key_ptr as *const u8, symmetric_key_len as usize)
.to_vec(),
std::slice::from_raw_parts(symmetric_key_ptr as *const u8, symmetric_key_len as usize),
)
.map_err(|e| Error::Other(e.to_string()))?;

Expand Down Expand Up @@ -335,8 +332,7 @@ unsafe fn decrypt_header_using_cache(
))?;

let symmetric_key_ = <Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(
std::slice::from_raw_parts(symmetric_key_ptr as *const u8, symmetric_key_len as usize)
.to_vec(),
std::slice::from_raw_parts(symmetric_key_ptr as *const u8, symmetric_key_len as usize),
)
.map_err(|e| Error::Other(e.to_string()))?;

Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/pyo3/hybrid_cc_aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn encrypt_hybrid_block(
//
// Parse symmetric key
let symmetric_key =
<Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(symmetric_key_bytes)
<Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(&symmetric_key_bytes)
.map_err(|e| PyTypeError::new_err(format!("Deserialize symmetric key failed: {e}")))?;

//
Expand Down Expand Up @@ -168,7 +168,7 @@ pub fn decrypt_hybrid_block(
//
// Parse symmetric key
let symmetric_key =
<Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(symmetric_key_bytes)
<Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(&symmetric_key_bytes)
.map_err(|e| PyTypeError::new_err(format!("Deserialize symmetric key failed: {e}")))?;

//
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn encrypt_hybrid_header<KEM: Kem, DEM: Dem>(
}

Ok(EncryptedHeader {
symmetric_key: DEM::Key::try_from_bytes(secret_key.into()).map_err(Error::CryptoError)?,
symmetric_key: DEM::Key::try_from_bytes(&secret_key).map_err(Error::CryptoError)?,
header_bytes,
})
}
Expand Down Expand Up @@ -128,7 +128,7 @@ pub fn decrypt_hybrid_header<KEM: Kem, DEM: Dem>(
};

Ok(ClearTextHeader {
symmetric_key: DEM::Key::try_from_bytes(secret_key.into()).map_err(Error::CryptoError)?,
symmetric_key: DEM::Key::try_from_bytes(&secret_key).map_err(Error::CryptoError)?,
meta_data,
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/wasm_bindgen/hybrid_cc_aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub fn webassembly_encrypt_hybrid_block(
//
// Parse symmetric key
let symmetric_key =
<Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(symmetric_key_bytes.to_vec())
<Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(&symmetric_key_bytes.to_vec())
.map_err(|e| {
JsValue::from_str(&format!(
"Error parsing
Expand Down Expand Up @@ -186,7 +186,7 @@ pub fn webassembly_decrypt_hybrid_block(
//
// Parse symmetric key
let symmetric_key =
<Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(symmetric_key_bytes.to_vec())
<Aes256GcmCrypto as SymmetricCrypto>::Key::try_from_bytes(&symmetric_key_bytes.to_vec())
.map_err(|e| {
JsValue::from_str(&format!(
"Error parsing
Expand Down

0 comments on commit 0643f97

Please sign in to comment.