-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experiment with using a separate crate for crypto
- Loading branch information
Showing
33 changed files
with
216 additions
and
67 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
[package] | ||
name = "bitwarden-crypto" | ||
version = "0.1.0" | ||
authors = ["Bitwarden Inc"] | ||
license-file = "LICENSE" | ||
repository = "https://github.com/bitwarden/sdk" | ||
homepage = "https://bitwarden.com" | ||
description = """ | ||
Bitwarden Cryptographic primitives | ||
""" | ||
keywords = ["bitwarden"] | ||
edition = "2021" | ||
rust-version = "1.57" | ||
|
||
[dependencies] | ||
aes = ">=0.8.2, <0.9" | ||
argon2 = { version = ">=0.5.0, <0.6", features = [ | ||
"alloc", | ||
], default-features = false } | ||
assert_matches = ">=1.5.0, <2.0" | ||
base64 = ">=0.21.2, <0.22" | ||
cbc = { version = ">=0.1.2, <0.2", features = ["alloc"] } | ||
chrono = { version = ">=0.4.26, <0.5", features = [ | ||
"clock", | ||
"serde", | ||
"std", | ||
], default-features = false } | ||
hkdf = ">=0.12.3, <0.13" | ||
hmac = ">=0.12.1, <0.13" | ||
lazy_static = ">=1.4.0, <2.0" | ||
log = ">=0.4.18, <0.5" | ||
num-bigint = ">=0.4, <0.5" | ||
num-traits = ">=0.2.15, <0.3" | ||
pbkdf2 = { version = ">=0.12.1, <0.13", default-features = false } | ||
rand = ">=0.8.5, <0.9" | ||
rsa = ">=0.9.2, <0.10" | ||
schemars = { version = ">=0.8, <0.9", features = ["uuid1", "chrono"] } | ||
serde = { version = ">=1.0, <2.0", features = ["derive"] } | ||
sha1 = ">=0.10.5, <0.11" | ||
sha2 = ">=0.10.6, <0.11" | ||
subtle = ">=2.5.0, <3.0" | ||
thiserror = ">=1.0.40, <2.0" | ||
uniffi = { version = "=0.25.2", optional = true } | ||
|
||
[dev-dependencies] | ||
rand_chacha = "0.3.1" | ||
tokio = { version = "1.34.0", features = ["rt", "macros"] } | ||
wiremock = "0.5.22" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use std::fmt::Debug; | ||
|
||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum CryptoError { | ||
#[error("The provided key is not the expected type")] | ||
InvalidKey, | ||
#[error("The cipher's MAC doesn't match the expected value")] | ||
InvalidMac, | ||
#[error("Error while decrypting EncString")] | ||
KeyDecrypt, | ||
#[error("The cipher key has an invalid length")] | ||
InvalidKeyLen, | ||
#[error("There is no encryption key for the provided organization")] | ||
NoKeyForOrg, | ||
#[error("The value is not a valid UTF8 String")] | ||
InvalidUtf8String, | ||
} | ||
|
||
pub type Result<T, E = CryptoError> = std::result::Result<T, E>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use ::aes::cipher::{generic_array::GenericArray, ArrayLength, Unsigned}; | ||
use base64::{ | ||
alphabet, | ||
engine::{DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig}, | ||
}; | ||
use hmac::digest::OutputSizeUser; | ||
|
||
pub mod aes; | ||
mod error; | ||
pub mod shareable_key; | ||
pub mod symmetric_crypto_key; | ||
|
||
pub use error::CryptoError; | ||
use error::Result; | ||
|
||
// TODO: Move into a util crate | ||
const BASE64_ENGINE_CONFIG: GeneralPurposeConfig = GeneralPurposeConfig::new() | ||
.with_encode_padding(true) | ||
.with_decode_padding_mode(DecodePaddingMode::Indifferent); | ||
|
||
pub const BASE64_ENGINE: GeneralPurpose = | ||
GeneralPurpose::new(&alphabet::STANDARD, BASE64_ENGINE_CONFIG); | ||
|
||
pub(crate) type PbkdfSha256Hmac = hmac::Hmac<sha2::Sha256>; | ||
pub(crate) const PBKDF_SHA256_HMAC_OUT_SIZE: usize = | ||
<<PbkdfSha256Hmac as OutputSizeUser>::OutputSize as Unsigned>::USIZE; | ||
|
||
/// RFC5869 HKDF-Expand operation | ||
fn hkdf_expand<T: ArrayLength<u8>>(prk: &[u8], info: Option<&str>) -> Result<GenericArray<u8, T>> { | ||
let hkdf = hkdf::Hkdf::<sha2::Sha256>::from_prk(prk).map_err(|_| CryptoError::InvalidKeyLen)?; | ||
let mut key = GenericArray::<u8, T>::default(); | ||
|
||
let i = info.map(|i| i.as_bytes()).unwrap_or(&[]); | ||
hkdf.expand(i, &mut key) | ||
.map_err(|_| CryptoError::InvalidKeyLen)?; | ||
|
||
Ok(key) | ||
} |
4 changes: 2 additions & 2 deletions
4
crates/bitwarden/src/crypto/shareable_key.rs → crates/bitwarden-crypto/src/shareable_key.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.