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 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c9b80df
Implement signing keys and signing
quexten May 6, 2025
2ad9f59
Remove unused code
quexten May 6, 2025
b0c3dae
Add test vector
quexten May 6, 2025
2e8bd92
Cargo fmt
quexten May 6, 2025
f7f1e2e
Make ed25519 dependency version a range
quexten May 7, 2025
6deb284
Add signed object
quexten May 12, 2025
f42f719
Merge branch 'main' into km/cose-signatures
quexten May 13, 2025
61496cc
Merge branch 'main' into km/cose-signatures
quexten May 19, 2025
8354476
Replace magic value
quexten May 19, 2025
04d5329
Clean up crate::error reference
quexten May 19, 2025
efd23cc
Add comments to signing and verifying key
quexten May 19, 2025
d6737cf
Move and clarify comment, drop optional verifying key from signing key
quexten May 19, 2025
d2b829b
Change comment into docs
quexten May 19, 2025
9484a64
Move signature errors to separate enum
quexten May 20, 2025
7e39e9c
Update test vectors
quexten May 20, 2025
a37f3e1
Format
quexten May 20, 2025
e1642b6
[PM-20361] Expose signing key generation to mobile and wasm clients &โ€ฆ
quexten May 21, 2025
6c725d2
Remove incorrect login response change
quexten May 21, 2025
fc895d8
Fix comment
quexten May 21, 2025
0e33fee
Cleanup
quexten May 21, 2025
6308cc5
Add comments
quexten May 21, 2025
971eb17
Cleanup
quexten May 21, 2025
12387f8
Remove unrelated change
quexten May 21, 2025
f3eb525
Cleanup
quexten May 21, 2025
d6bbae0
Fix init
quexten May 21, 2025
2ab6620
Cleanup
quexten May 21, 2025
17e0a07
Fix sorting of dependencies
quexten May 21, 2025
8c9f00f
Attempt to fix build
quexten May 21, 2025
06b26ba
Add more default nil values for signing key to fix ios build
quexten May 21, 2025
a2e95e7
Fix docs
quexten May 21, 2025
e6ab5ef
Fix build
quexten May 21, 2025
4842309
Fix doc
quexten May 21, 2025
0e3bcb6
Merge branch 'main' into km/cose-signatures
quexten May 21, 2025
d6e6e99
Add tests to purecrypto
quexten May 21, 2025
0578743
Remove code to make test vectors
quexten May 21, 2025
4182ad4
Merge branch 'km/cose-signatures' of github.com:bitwarden/sdk-internaโ€ฆ
quexten May 21, 2025
38ec3eb
Cleanup
quexten May 21, 2025
4467b0d
Fix docs
quexten May 21, 2025
e373638
Merge branch 'main' into km/cose-signatures
quexten May 21, 2025
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
///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Labels
///
// 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
11 changes: 11 additions & 0 deletions crates/bitwarden-crypto/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub enum CryptoError {

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

#[error("Signature error, {0}")]
SignatureError(#[from] SignatureError),
}

#[derive(Debug, Error)]
Expand Down Expand Up @@ -92,5 +95,13 @@ pub enum RsaError {
Rsa(#[from] rsa::Error),
}

#[derive(Debug, Error)]
pub enum SignatureError {
#[error("Invalid signature")]
InvalidSignature,
#[error("Invalid namespace")]
InvalidNamespace,
}

/// Alias for `Result<T, CryptoError>`.
pub(crate) type Result<T, E = CryptoError> = std::result::Result<T, E>;
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
2 changes: 1 addition & 1 deletion 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: The key namespace is getting crowed. I think we need to separate cryptographic keys (asymmetric, symmetric) from Bitwarden concepts like user, master keys. I'm not sure where signing keys falls here? It might be beneficial to tie the keys to the enc string implementations?

This doesn't block this PR but I'd want us to figure this out before adding more files to crypto.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I've been meaning to both look at simplifying some bitwarden-crypto interfaces, but cleaning up some concepts here, but on my priority lists that's after getting cose symmetric crypto + (user) signing done.

MasterKey, PinKey, etc. are just specially named SymmetricKeys. So I feel that the bitwarden labeled concepts, should be separated from the "pure" crypto concepts.

mod user_key;
pub use user_key::UserKey;
mod device_key;
Expand All @@ -26,6 +27,5 @@ pub use kdf::{
default_argon2_iterations, default_argon2_memory, default_argon2_parallelism,
default_pbkdf2_iterations, Kdf,
};
#[cfg(test)]
pub(crate) use key_id::KEY_ID_SIZE;
mod utils;
Loading