Skip to content

Commit 15a5164

Browse files
fspreissrandombit
andauthored
feat(crypto): CRP-2599 implement VetKdProtocol trait for CryptoComponent (dfinity#3565)
Implements the `VetKdProtocol` trait for the `CryptoComponent` Smoke tests are added in dfinity#3649. They are split from this PR to make the PR review easier. --------- Co-authored-by: Jack Lloyd <[email protected]>
1 parent ef59572 commit 15a5164

File tree

19 files changed

+684
-59
lines changed

19 files changed

+684
-59
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rs/crypto/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ DEPENDENCIES = [
1717
"//rs/crypto/ed25519",
1818
"//rs/crypto/interfaces/sig_verification",
1919
"//rs/crypto/internal/crypto_lib/basic_sig/ed25519",
20+
"//rs/crypto/internal/crypto_lib/bls12_381/vetkd",
2021
"//rs/crypto/internal/crypto_lib/seed",
2122
"//rs/crypto/internal/crypto_lib/threshold_sig/bls12_381",
2223
"//rs/crypto/internal/crypto_lib/threshold_sig/canister_threshold_sig",

rs/crypto/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ic-crypto-internal-logmon = { path = "internal/logmon" }
2525
ic-crypto-internal-seed = { path = "internal/crypto_lib/seed" }
2626
ic-crypto-internal-threshold-sig-bls12381 = { path = "internal/crypto_lib/threshold_sig/bls12_381" }
2727
ic-crypto-internal-threshold-sig-canister-threshold-sig = { path = "internal/crypto_lib/threshold_sig/canister_threshold_sig" }
28+
ic-crypto-internal-bls12-381-vetkd = { path = "internal/crypto_lib/bls12_381/vetkd" }
2829
ic-crypto-internal-types = { path = "internal/crypto_lib/types" }
2930
ic-crypto-standalone-sig-verifier = { path = "standalone-sig-verifier" }
3031
ic-crypto-tls-cert-validation = { path = "node_key_validation/tls_cert_validation" }

rs/crypto/internal/crypto_lib/bls12_381/vetkd/benches/vetkd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn vetkd_bench(c: &mut Criterion) {
5353
group.bench_function("EncryptedKeyShare::deserialize", |b| {
5454
b.iter_batched(
5555
|| eks.serialize(),
56-
EncryptedKeyShare::deserialize,
56+
|val| EncryptedKeyShare::deserialize(&val),
5757
BatchSize::SmallInput,
5858
)
5959
});

rs/crypto/internal/crypto_lib/bls12_381/vetkd/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,10 @@ impl EncryptedKey {
297297
}
298298

299299
/// Deserialize an encrypted key
300-
pub fn deserialize(val: [u8; Self::BYTES]) -> Result<Self, EncryptedKeyDeserializationError> {
300+
pub fn deserialize(val: &[u8]) -> Result<Self, EncryptedKeyDeserializationError> {
301+
if val.len() != Self::BYTES {
302+
return Err(EncryptedKeyDeserializationError::InvalidEncryptedKey);
303+
}
301304
let c2_start = G1Affine::BYTES;
302305
let c3_start = G1Affine::BYTES + G2Affine::BYTES;
303306

@@ -407,9 +410,10 @@ impl EncryptedKeyShare {
407410
}
408411

409412
/// Deserialize an encrypted key share
410-
pub fn deserialize(
411-
val: [u8; Self::BYTES],
412-
) -> Result<Self, EncryptedKeyShareDeserializationError> {
413+
pub fn deserialize(val: &[u8]) -> Result<Self, EncryptedKeyShareDeserializationError> {
414+
if val.len() != Self::BYTES {
415+
return Err(EncryptedKeyShareDeserializationError::InvalidEncryptedKeyShare);
416+
}
413417
let c2_start = G1Affine::BYTES;
414418
let c3_start = G1Affine::BYTES + G2Affine::BYTES;
415419

rs/crypto/internal/crypto_lib/bls12_381/vetkd/tests/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<'a> VetkdTestProtocolExecution<'a> {
253253

254254
// check that EKS serialization round trips:
255255
let eks_bytes = eks.serialize();
256-
let eks2 = EncryptedKeyShare::deserialize(eks_bytes).unwrap();
256+
let eks2 = EncryptedKeyShare::deserialize(&eks_bytes).unwrap();
257257
assert_eq!(eks, eks2);
258258

259259
node_info.push((node_idx as u32, node_pk.clone(), eks.clone()));

rs/crypto/internal/crypto_service_provider/src/vault/api.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,10 +974,14 @@ pub trait VetKdCspVault {
974974
/// Vault-level error for vetKD key share creation.
975975
#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)]
976976
pub enum VetKdEncryptedKeyShareCreationVaultError {
977-
/// If some arguments are invalid
978-
InvalidArgument(String),
977+
/// If the secret key is missing in the key store of if it has the wrong type
978+
SecretKeyMissingOrWrongType(String),
979979
/// If a transient internal error occurs, e.g., an RPC error communicating with the remote vault
980980
TransientInternalError(String),
981+
/// If the given master public key is invalid
982+
InvalidArgumentMasterPublicKey,
983+
/// If the given encryption public key is invalid
984+
InvalidArgumentEncryptionPublicKey,
981985
}
982986

983987
/// An error returned by failing to generate a public seed from [`CspVault`].

rs/crypto/internal/crypto_service_provider/src/vault/local_csp_vault/vetkd/mod.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,40 +60,35 @@ impl<R: Rng + CryptoRng, S: SecretKeyStore, C: SecretKeyStore, P: PublicKeyStore
6060
) -> Result<VetKdEncryptedKeyShareContent, VetKdEncryptedKeyShareCreationVaultError> {
6161
let master_public_key =
6262
G2Affine::deserialize(&master_public_key).map_err(|_: PairingInvalidPoint| {
63-
VetKdEncryptedKeyShareCreationVaultError::InvalidArgument(format!(
64-
"invalid master public key: 0x{}",
65-
hex::encode(&master_public_key)
66-
))
63+
VetKdEncryptedKeyShareCreationVaultError::InvalidArgumentMasterPublicKey
6764
})?;
6865

6966
let transport_public_key = TransportPublicKey::deserialize(&encryption_public_key)
7067
.map_err(|e| match e {
7168
TransportPublicKeyDeserializationError::InvalidPublicKey => {
72-
VetKdEncryptedKeyShareCreationVaultError::InvalidArgument(format!(
73-
"invalid encryption public key: 0x{}",
74-
hex::encode(&encryption_public_key)
75-
))
69+
VetKdEncryptedKeyShareCreationVaultError::InvalidArgumentEncryptionPublicKey
7670
}
7771
})?;
7872

7973
let secret_key_from_store = self.sks_read_lock().get(&key_id).ok_or(
80-
VetKdEncryptedKeyShareCreationVaultError::InvalidArgument(format!(
81-
"missing key with ID {key_id:?}",
74+
VetKdEncryptedKeyShareCreationVaultError::SecretKeyMissingOrWrongType(format!(
75+
"missing key with ID {key_id}"
8276
)),
8377
)?;
84-
let secret_bls_scalar = if let CspSecretKey::ThresBls12_381(secret_key_bytes) =
85-
&secret_key_from_store
86-
{
87-
// We use the unchecked deserialization here because it is slighly cheaper, but mainly because
88-
// it cannot fail, and the data is anyway trusted as it comes from the secret key store.
89-
Ok(Scalar::deserialize_unchecked(
90-
secret_key_bytes.inner_secret().expose_secret(),
91-
))
92-
} else {
93-
Err(VetKdEncryptedKeyShareCreationVaultError::InvalidArgument(
94-
format!("wrong secret key type for key with ID {key_id}: expected ThresBls12_381"),
95-
))
96-
}?;
78+
let secret_bls_scalar =
79+
if let CspSecretKey::ThresBls12_381(secret_key_bytes) = &secret_key_from_store {
80+
// We use the unchecked deserialization here because it is slighly cheaper, but mainly because
81+
// it cannot fail, and the data is anyway trusted as it comes from the secret key store.
82+
Ok(Scalar::deserialize_unchecked(
83+
secret_key_bytes.inner_secret().expose_secret(),
84+
))
85+
} else {
86+
Err(
87+
VetKdEncryptedKeyShareCreationVaultError::SecretKeyMissingOrWrongType(format!(
88+
"wrong secret key type for key with ID {key_id}: expected ThresBls12_381"
89+
)),
90+
)
91+
}?;
9792

9893
// Create encrypted key share using our library
9994
let encrypted_key_share = EncryptedKeyShare::create(

rs/crypto/internal/crypto_service_provider/src/vault/local_csp_vault/vetkd/tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ fn should_fail_to_create_key_share_with_invalid_master_public_key() {
6363
let result = test_env.create_encrypted_vetkd_key_share();
6464

6565
assert_matches!(
66-
result, Err(VetKdEncryptedKeyShareCreationVaultError::InvalidArgument(error))
67-
if error.contains("invalid master public key")
66+
result,
67+
Err(VetKdEncryptedKeyShareCreationVaultError::InvalidArgumentMasterPublicKey)
6868
);
6969
}
7070

@@ -79,8 +79,8 @@ fn should_fail_to_create_key_share_with_invalid_encryption_public_key() {
7979
let result = test_env.create_encrypted_vetkd_key_share();
8080

8181
assert_matches!(
82-
result, Err(VetKdEncryptedKeyShareCreationVaultError::InvalidArgument(error))
83-
if error.contains("invalid encryption public key")
82+
result,
83+
Err(VetKdEncryptedKeyShareCreationVaultError::InvalidArgumentEncryptionPublicKey)
8484
);
8585
}
8686

@@ -94,7 +94,7 @@ fn should_fail_to_create_key_share_if_key_is_missing_in_secret_key_store() {
9494
let result = test_env.create_encrypted_vetkd_key_share();
9595

9696
assert_matches!(
97-
result, Err(VetKdEncryptedKeyShareCreationVaultError::InvalidArgument(error))
97+
result, Err(VetKdEncryptedKeyShareCreationVaultError::SecretKeyMissingOrWrongType(error))
9898
if error.contains("missing key with ID")
9999
);
100100
}
@@ -111,7 +111,7 @@ fn should_fail_to_create_key_share_if_key_in_secret_key_store_has_wrong_type() {
111111
let result = test_env.create_encrypted_vetkd_key_share();
112112

113113
assert_matches!(
114-
result, Err(VetKdEncryptedKeyShareCreationVaultError::InvalidArgument(error))
114+
result, Err(VetKdEncryptedKeyShareCreationVaultError::SecretKeyMissingOrWrongType(error))
115115
if error.contains("wrong secret key type")
116116
);
117117
}

rs/crypto/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod common;
1616
mod keygen;
1717
mod sign;
1818
mod tls;
19+
mod vetkd;
1920

2021
use ic_crypto_internal_csp::vault::api::CspVault;
2122
pub use sign::{

0 commit comments

Comments
 (0)