Skip to content

[PM-20361] Signature keys #207

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/bitwarden-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cbc = { version = ">=0.1.2, <0.2", features = ["alloc", "zeroize"] }
chacha20poly1305 = { version = "0.10.1" }
ciborium = { version = ">=0.2.2, <0.3" }
coset = { version = ">=0.3.8, <0.4" }
ed25519-dalek = { version = ">=2.1.1, <=2.2.0", features = ["rand_core"] }
generic-array = { version = ">=0.14.7, <1.0", features = ["zeroize"] }
hkdf = ">=0.12.3, <0.13"
hmac = ">=0.12.1, <0.13"
Expand Down
5 changes: 5 additions & 0 deletions crates/bitwarden-crypto/src/cose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ use crate::{
/// the draft was never published as an RFC, we use a private-use value for the algorithm.
pub(crate) const XCHACHA20_POLY1305: i64 = -70000;

// Labels
//
// The label used for the namespace ensuring strong domain separation when using signatures.
pub(crate) const SIGNING_NAMESPACE: i64 = -80000;

/// Encrypts a plaintext message using XChaCha20Poly1305 and returns a COSE Encrypt0 message
pub(crate) fn encrypt_xchacha20_poly1305(
plaintext: &[u8],
Expand Down
6 changes: 6 additions & 0 deletions crates/bitwarden-crypto/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ pub enum CryptoError {

#[error("Invalid nonce length")]
InvalidNonceLength,

#[error("Invalid signature")]
InvalidSignature,

#[error("Invalid namespace")]
InvalidNamespace,
}

#[derive(Debug, Error)]
Expand Down
7 changes: 7 additions & 0 deletions crates/bitwarden-crypto/src/keys/key_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) const KEY_ID_SIZE: usize = 16;
/// A key id is a unique identifier for a single key. There is a 1:1 mapping between key ID and key
/// bytes, so something like a user key rotation is replacing the key with ID A with a new key with
/// ID B.
#[derive(Clone)]
pub(crate) struct KeyId(uuid::Uuid);

/// Fixed length identifiers for keys.
Expand Down Expand Up @@ -38,6 +39,12 @@ impl From<KeyId> for [u8; KEY_ID_SIZE] {
}
}

impl From<&KeyId> for Vec<u8> {
fn from(key_id: &KeyId) -> Self {
key_id.0.as_bytes().to_vec()
}
}

impl From<[u8; KEY_ID_SIZE]> for KeyId {
fn from(bytes: [u8; KEY_ID_SIZE]) -> Self {
KeyId(Uuid::from_bytes(bytes))
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden-crypto/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod asymmetric_crypto_key;
pub use asymmetric_crypto_key::{
AsymmetricCryptoKey, AsymmetricEncryptable, AsymmetricPublicCryptoKey,
};
mod signing_crypto_key;
mod user_key;
pub use user_key::UserKey;
mod device_key;
Expand Down
Loading
Loading