Skip to content

Commit c9b80df

Browse files
committed
Implement signing keys and signing
1 parent bc2f203 commit c9b80df

File tree

9 files changed

+474
-0
lines changed

9 files changed

+474
-0
lines changed

Cargo.lock

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bitwarden-crypto/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ cbc = { version = ">=0.1.2, <0.2", features = ["alloc", "zeroize"] }
3333
chacha20poly1305 = { version = "0.10.1" }
3434
ciborium = { version = ">=0.2.2, <0.3" }
3535
coset = { version = ">=0.3.8, <0.4" }
36+
ed25519-dalek = { version = "2.1.1", features = ["rand_core"] }
3637
generic-array = { version = ">=0.14.7, <1.0", features = ["zeroize"] }
3738
hkdf = ">=0.12.3, <0.13"
3839
hmac = ">=0.12.1, <0.13"

crates/bitwarden-crypto/src/cose.rs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ use crate::{
1616
/// the draft was never published as an RFC, we use a private-use value for the algorithm.
1717
pub(crate) const XCHACHA20_POLY1305: i64 = -70000;
1818

19+
// Labels
20+
//
21+
// The label used for the namespace ensuring strong domain separation when using signatures.
22+
pub(crate) const SIGNING_NAMESPACE: i64 = -80000;
23+
1924
/// Encrypts a plaintext message using XChaCha20Poly1305 and returns a COSE Encrypt0 message
2025
pub(crate) fn encrypt_xchacha20_poly1305(
2126
plaintext: &[u8],

crates/bitwarden-crypto/src/error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ pub enum CryptoError {
5656

5757
#[error("Invalid nonce length")]
5858
InvalidNonceLength,
59+
60+
#[error("Invalid signature")]
61+
InvalidSignature,
62+
63+
#[error("Invalid namespace")]
64+
InvalidNamespace,
5965
}
6066

6167
#[derive(Debug, Error)]

crates/bitwarden-crypto/src/keys/key_id.rs

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub(crate) const KEY_ID_SIZE: usize = 16;
99
/// A key id is a unique identifier for a single key. There is a 1:1 mapping between key ID and key
1010
/// bytes, so something like a user key rotation is replacing the key with ID A with a new key with
1111
/// ID B.
12+
#[derive(Clone)]
1213
pub(crate) struct KeyId(uuid::Uuid);
1314

1415
/// Fixed length identifiers for keys.
@@ -38,6 +39,12 @@ impl From<KeyId> for [u8; KEY_ID_SIZE] {
3839
}
3940
}
4041

42+
impl From<&KeyId> for Vec<u8> {
43+
fn from(key_id: &KeyId) -> Self {
44+
key_id.0.as_bytes().to_vec()
45+
}
46+
}
47+
4148
impl From<[u8; KEY_ID_SIZE]> for KeyId {
4249
fn from(bytes: [u8; KEY_ID_SIZE]) -> Self {
4350
KeyId(Uuid::from_bytes(bytes))

crates/bitwarden-crypto/src/keys/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod asymmetric_crypto_key;
1414
pub use asymmetric_crypto_key::{
1515
AsymmetricCryptoKey, AsymmetricEncryptable, AsymmetricPublicCryptoKey,
1616
};
17+
mod signing_crypto_key;
1718
mod user_key;
1819
pub use user_key::UserKey;
1920
mod device_key;

0 commit comments

Comments
 (0)