Skip to content

Commit 09eb676

Browse files
committed
Merge branch 'mathias-CRP-1934-remove-string-conversions-for-csp-types' into 'master'
cleanup(crypto): CRP-1934: Remove string conversions for SecretKeyBytes Remove conversions to and from base64-encoded strings for `ic_crypto_internal_multi_sig_bls12381::types::SecretKeyBytes`, `ic_crypto_internal_threshold_sig_bls12381::types::SecretKeyBytes`, and `ic_crypto_internal_basic_sig_ed25519::types::SecretKeyBytes`, based on [the discussion in MR 10314](https://gitlab.com/dfinity-lab/public/ic/-/merge_requests/10314#note_1262953398). See merge request dfinity-lab/public/ic!12370
2 parents bc9f69d + b1f1a68 commit 09eb676

File tree

7 files changed

+2
-146
lines changed

7 files changed

+2
-146
lines changed

rs/crypto/internal/crypto_lib/basic_sig/ed25519/src/types/conversions.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::*;
2-
use ic_crypto_secrets_containers::SecretArray;
32
use ic_types::crypto::{AlgorithmId, CryptoError};
43
use std::convert::TryFrom;
54

@@ -8,39 +7,6 @@ pub mod protobuf;
87
#[cfg(test)]
98
mod tests;
109

11-
impl From<SecretKeyBytes> for String {
12-
fn from(val: SecretKeyBytes) -> Self {
13-
base64::encode(val.0.expose_secret())
14-
}
15-
}
16-
impl TryFrom<&str> for SecretKeyBytes {
17-
type Error = CryptoError;
18-
19-
fn try_from(key: &str) -> Result<Self, CryptoError> {
20-
let mut key = base64::decode(key).map_err(|e| CryptoError::MalformedSecretKey {
21-
algorithm: AlgorithmId::Ed25519,
22-
internal_error: format!("Key is not a valid base64 encoded string: {}", e),
23-
})?;
24-
if key.len() != SecretKeyBytes::SIZE {
25-
return Err(CryptoError::MalformedSecretKey {
26-
algorithm: AlgorithmId::Ed25519,
27-
internal_error: "Key length is incorrect".to_string(),
28-
});
29-
}
30-
let mut buffer = [0u8; SecretKeyBytes::SIZE];
31-
buffer.copy_from_slice(&key);
32-
key.zeroize();
33-
let ret = SecretKeyBytes(SecretArray::new_and_zeroize_argument(&mut buffer));
34-
Ok(ret)
35-
}
36-
}
37-
impl TryFrom<&String> for SecretKeyBytes {
38-
type Error = CryptoError;
39-
fn try_from(signature: &String) -> Result<Self, CryptoError> {
40-
Self::try_from(signature as &str)
41-
}
42-
}
43-
4410
impl From<PublicKeyBytes> for String {
4511
fn from(val: PublicKeyBytes) -> Self {
4612
base64::encode(&val.0[..])

rs/crypto/internal/crypto_lib/multi_sig/bls12_381/src/types/conversions.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -90,35 +90,3 @@ impl From<&CombinedSignature> for CombinedSignatureBytes {
9090
CombinedSignatureBytes(signature.serialize())
9191
}
9292
}
93-
impl From<SecretKeyBytes> for String {
94-
fn from(val: SecretKeyBytes) -> Self {
95-
base64::encode(&val.0.expose_secret())
96-
}
97-
}
98-
impl TryFrom<&str> for SecretKeyBytes {
99-
type Error = CryptoError;
100-
101-
fn try_from(key: &str) -> Result<Self, CryptoError> {
102-
let key = base64::decode(key).map_err(|e| CryptoError::MalformedSecretKey {
103-
algorithm: AlgorithmId::MultiBls12_381,
104-
internal_error: format!("Key is not a valid base64 encoded string: {}", e),
105-
})?;
106-
if key.len() != SecretKeyBytes::SIZE {
107-
return Err(CryptoError::MalformedSecretKey {
108-
algorithm: AlgorithmId::MultiBls12_381,
109-
internal_error: "Key length is incorrect".to_string(),
110-
});
111-
}
112-
let mut buffer = [0u8; SecretKeyBytes::SIZE];
113-
buffer.copy_from_slice(&key);
114-
Ok(SecretKeyBytes(
115-
ic_crypto_secrets_containers::SecretArray::new_and_zeroize_argument(&mut buffer),
116-
))
117-
}
118-
}
119-
impl TryFrom<&String> for SecretKeyBytes {
120-
type Error = CryptoError;
121-
fn try_from(signature: &String) -> Result<Self, CryptoError> {
122-
Self::try_from(signature as &str)
123-
}
124-
}

rs/crypto/internal/crypto_lib/multi_sig/bls12_381/src/types/conversions/tests.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ proptest! {
5353
let reconstructed_pops: Result<Vec<Pop>, CryptoError> = bytes.map(|bytes| Pop::try_from(&bytes)).collect();
5454
assert_eq!(Ok(pops), reconstructed_pops);
5555
}
56-
#[test]
57-
fn secret_key_base64_serde(key in arbitrary::secret_key()) {
58-
let bytes = SecretKeyBytes::from(&key);
59-
let base64: String = bytes.clone().into();
60-
assert_eq!(Ok(bytes), SecretKeyBytes::try_from(&base64));
61-
}
6256
}
6357

6458
#[test]
@@ -85,15 +79,3 @@ fn conversion_to_combined_signature_fails_gracefully() {
8579
let converted: Result<CombinedSignature, _> = CombinedSignature::try_from(&bad_point);
8680
assert!(converted.is_err());
8781
}
88-
#[test]
89-
fn base64_decoding_secret_key_fails_gracefully() {
90-
let bad_base64 = "leNiiKeMW5l8UQUf=SrVRevJbAFoQafWg4X09pBCcI2cP5kKTmW/j97hGyCX5PtXrB+0HKj/rmJ8r4uiapMNZt3ecy4aQTELqJl/2f2UKV+uyFNrjt4WuP7NOlt89SXua".to_string();
91-
let converted = SecretKeyBytes::try_from(&bad_base64);
92-
assert!(converted.is_err());
93-
}
94-
#[test]
95-
fn decoding_wrong_length_secret_key_fails_gracefully() {
96-
let bad_value = "lUn1Nh1inf3zBhf3okqo/q7vyNhKcEnHwJ0dLqMatUk1poY5rf80rBJOunirUU3zAJF96i/cLCzoD3wEvGusbr2OqO+zFI143BCwCo1OGhgEULSqvRm6kxGigqPhp+Ma".to_string();
97-
let converted = SecretKeyBytes::try_from(&bad_value);
98-
assert!(converted.is_err());
99-
}

rs/crypto/internal/crypto_lib/multi_sig/bls12_381/src/types/generic_traits/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ proptest! {
2626
#[test]
2727
fn debug_should_redact_secretkey_bytes(secret_key_bytes: SecretKeyBytes) {
2828
let debug_str = format!("{:?}", secret_key_bytes);
29-
let raw_str: String = secret_key_bytes.into();
29+
let raw_str = base64::encode(&secret_key_bytes.0.expose_secret());
3030
assert!(!debug_str.contains(&raw_str));
3131
assert_eq!(debug_str, "REDACTED");
3232
}

rs/crypto/internal/crypto_lib/threshold_sig/bls12_381/src/types/conversions.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -123,39 +123,6 @@ impl TryFrom<&CombinedSignatureBytes> for CombinedSignature {
123123
}
124124
}
125125

126-
impl From<SecretKeyBytes> for String {
127-
fn from(bytes: SecretKeyBytes) -> String {
128-
base64::encode(&bytes.0.expose_secret())
129-
}
130-
}
131-
impl TryFrom<&str> for SecretKeyBytes {
132-
type Error = CryptoError;
133-
134-
fn try_from(string: &str) -> Result<Self, CryptoError> {
135-
let bytes = base64::decode(string).map_err(|e| CryptoError::MalformedSecretKey {
136-
algorithm: AlgorithmId::ThresBls12_381,
137-
internal_error: format!("Secret key is not a valid base64 encoded string: {}", e),
138-
})?;
139-
if bytes.len() != SecretKeyBytes::SIZE {
140-
return Err(CryptoError::MalformedSecretKey {
141-
algorithm: AlgorithmId::ThresBls12_381,
142-
internal_error: "Secret key length is incorrect".to_string(),
143-
});
144-
}
145-
let mut buffer = [0u8; SecretKeyBytes::SIZE];
146-
buffer.copy_from_slice(&bytes);
147-
Ok(SecretKeyBytes(
148-
ic_crypto_secrets_containers::SecretArray::new_and_zeroize_argument(&mut buffer),
149-
))
150-
}
151-
}
152-
impl TryFrom<&String> for SecretKeyBytes {
153-
type Error = CryptoError;
154-
fn try_from(string: &String) -> Result<Self, CryptoError> {
155-
Self::try_from(string as &str)
156-
}
157-
}
158-
159126
impl From<IndividualSignatureBytes> for String {
160127
fn from(bytes: IndividualSignatureBytes) -> String {
161128
base64::encode(&bytes.0[..])

rs/crypto/internal/crypto_lib/threshold_sig/bls12_381/src/types/conversions/tests.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,6 @@ proptest! {
5353
assert_eq!(signature_bytes, serialised, "Parsing followed by serailizing produced a value different from the starting value.");
5454
}
5555

56-
/// Verifies that stringifying SecretKeyBytes and paring them again yields the original.
57-
#[test]
58-
#[allow(clippy::unnecessary_operation)] // Clippy believes that these tests are unnecessary.
59-
fn proptest_secret_key_stringifying_and_parsing_should_be_inverse(secret_key: SecretKeyBytes) {
60-
let string = String::from(secret_key.clone());
61-
let parsed = SecretKeyBytes::try_from(&string).expect("Failed to parse stringified secret key");
62-
assert_eq!(secret_key, parsed, "Stringifying followed by parsing produced a value different from the starting value.");
63-
}
64-
6556
/// Verifies that stringifying PublicKeyBytes and paring them again yields the original.
6657
#[test]
6758
#[allow(clippy::unnecessary_operation)] // Clippy believes that these tests are unnecessary.
@@ -158,24 +149,6 @@ fn test_invalid_combined_signature_fails_to_parse() {
158149
}
159150
}
160151

161-
/// Verifies that parsing invalid base64 SecretKeyBytes fails
162-
#[test]
163-
fn test_snowman_is_not_valid_secret_key() {
164-
match SecretKeyBytes::try_from(SNOWMAN) {
165-
Err(CryptoError::MalformedSecretKey { .. }) => (),
166-
other => panic!("Expected a MalformedSecretKey error. Got: {:?}", other),
167-
}
168-
}
169-
170-
/// Verifies that parsing invalid base64 SecretKeyBytes fails
171-
#[test]
172-
fn test_base64_snowman_is_not_valid_secret_key() {
173-
match SecretKeyBytes::try_from(SNOWCODE) {
174-
Err(CryptoError::MalformedSecretKey { .. }) => (),
175-
other => panic!("Expected a MalformedSecretKey error. Got: {:?}", other),
176-
}
177-
}
178-
179152
/// Verifies that parsing invalid base64 PublicKeyBytes fails
180153
#[test]
181154
fn test_snowman_is_not_valid_public_key() {

rs/crypto/internal/crypto_lib/threshold_sig/bls12_381/src/types/generic_traits/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ proptest! {
2424
#[test]
2525
fn debug_should_redact_secretkey_bytes(secret_key_bytes: SecretKeyBytes) {
2626
let debug_str = format!("{:?}", secret_key_bytes);
27-
let raw_str = String::from(secret_key_bytes);
27+
let raw_str = base64::encode(&secret_key_bytes.0.expose_secret());
2828
assert!(!debug_str.contains(&raw_str));
2929
assert_eq!(debug_str, "REDACTED");
3030
}

0 commit comments

Comments
 (0)