Skip to content

Commit efd5cdc

Browse files
update to prost 0.12
derived Enumerations now provide their own conversion implementation from i32, so map any DecodeErrors originating from there to InvalidEncoding, and drop the manual impls that existed before. Closes: parallaxsecond#153 Signed-off-by: Fabian Grünbichler <[email protected]>
1 parent 9f50ebb commit efd5cdc

File tree

4 files changed

+33
-106
lines changed

4 files changed

+33
-106
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ edition = "2018"
1212
rust-version = "1.66.0"
1313

1414
[build-dependencies]
15-
prost-build = { version = "0.9.0", optional = true }
15+
prost-build = { version = "0.12.0", optional = true }
1616

1717
[dependencies]
1818
serde = { version = "1.0.115", features = ["derive"] }
1919
bincode = "1.3.1"
2020
num-traits = "0.2.12"
2121
num-derive = "0.4.0"
2222
num = "0.4.0"
23-
prost = "0.9.0"
23+
prost = "0.12.0"
2424
arbitrary = { version = "0.4.6", features = ["derive"], optional = true }
2525
uuid = "0.8.1"
2626
log = "0.4.11"

src/operations_protobuf/convert_can_do_crypto.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ impl TryFrom<CheckType> for CheckTypeProto {
8181

8282
// CheckType from protobuf to native
8383
pub fn i32_to_check_type(check_type_val: i32) -> std::result::Result<CheckType, ResponseStatus> {
84-
let check_type_proto: CheckTypeProto = check_type_val.try_into()?;
84+
let check_type_proto: CheckTypeProto = check_type_val
85+
.try_into()
86+
.map_err(|_err: ::prost::DecodeError| ResponseStatus::InvalidEncoding)?;
8587
check_type_proto.try_into()
8688
}
8789

src/operations_protobuf/generated_ops/mod.rs

+18-103
Original file line numberDiff line numberDiff line change
@@ -36,104 +36,15 @@ pub mod prepare_key_attestation;
3636

3737
use zeroize::Zeroize;
3838

39-
use crate::requests::{ResponseStatus, Result};
40-
use log::error;
41-
use psa_algorithm::algorithm::{aead::AeadWithDefaultLengthTag, key_agreement::Raw, Cipher, Hash};
42-
use psa_key_attributes::key_type::{DhFamily, EccFamily};
43-
use can_do_crypto::CheckType;
39+
#[cfg(test)]
40+
use crate::requests::ResponseStatus;
41+
#[cfg(test)]
42+
use psa_algorithm::algorithm::{Cipher, Hash};
43+
#[cfg(test)]
44+
use psa_key_attributes::key_type::EccFamily;
45+
#[cfg(test)]
4446
use std::convert::TryFrom;
4547

46-
impl TryFrom<i32> for Cipher {
47-
type Error = ResponseStatus;
48-
fn try_from(cipher_val: i32) -> Result<Self> {
49-
Cipher::from_i32(cipher_val).ok_or_else(|| {
50-
error!(
51-
"Value {} not supported as a cipher algorithm encoding.",
52-
cipher_val
53-
);
54-
ResponseStatus::InvalidEncoding
55-
})
56-
}
57-
}
58-
59-
impl TryFrom<i32> for Hash {
60-
type Error = ResponseStatus;
61-
fn try_from(hash_val: i32) -> Result<Self> {
62-
Hash::from_i32(hash_val).ok_or_else(|| {
63-
error!(
64-
"Value {} not supported as a hash algorithm encoding.",
65-
hash_val
66-
);
67-
ResponseStatus::InvalidEncoding
68-
})
69-
}
70-
}
71-
72-
impl TryFrom<i32> for AeadWithDefaultLengthTag {
73-
type Error = ResponseStatus;
74-
fn try_from(aead_val: i32) -> Result<Self> {
75-
AeadWithDefaultLengthTag::from_i32(aead_val).ok_or_else(|| {
76-
error!(
77-
"Value {} not supported as an AEAD with default tag length algorithm encoding.",
78-
aead_val
79-
);
80-
ResponseStatus::InvalidEncoding
81-
})
82-
}
83-
}
84-
85-
impl TryFrom<i32> for Raw {
86-
type Error = ResponseStatus;
87-
fn try_from(key_agreement_val: i32) -> Result<Self> {
88-
Raw::from_i32(key_agreement_val).ok_or_else(|| {
89-
error!(
90-
"Value {} not supported as a raw key agreement algorithm encoding.",
91-
key_agreement_val
92-
);
93-
ResponseStatus::InvalidEncoding
94-
})
95-
}
96-
}
97-
98-
impl TryFrom<i32> for EccFamily {
99-
type Error = ResponseStatus;
100-
fn try_from(ecc_family_val: i32) -> Result<Self> {
101-
EccFamily::from_i32(ecc_family_val).ok_or_else(|| {
102-
error!(
103-
"Value {} not supported as an ECC family encoding.",
104-
ecc_family_val
105-
);
106-
ResponseStatus::InvalidEncoding
107-
})
108-
}
109-
}
110-
111-
impl TryFrom<i32> for DhFamily {
112-
type Error = ResponseStatus;
113-
fn try_from(dh_family_val: i32) -> Result<Self> {
114-
DhFamily::from_i32(dh_family_val).ok_or_else(|| {
115-
error!(
116-
"Value {} not supported as a DH family encoding.",
117-
dh_family_val
118-
);
119-
ResponseStatus::InvalidEncoding
120-
})
121-
}
122-
}
123-
124-
impl TryFrom<i32> for CheckType {
125-
type Error = ResponseStatus;
126-
fn try_from(check_type_val: i32) -> Result<Self> {
127-
CheckType::from_i32(check_type_val).ok_or_else(|| {
128-
error!(
129-
"Value {} not supported as a check type.",
130-
check_type_val
131-
);
132-
ResponseStatus::InvalidEncoding
133-
})
134-
}
135-
}
136-
13748
pub(super) trait ClearProtoMessage {
13849
fn clear_message(&mut self) {}
13950
}
@@ -299,11 +210,15 @@ impl ClearProtoMessage for psa_cipher_decrypt::Operation {
299210
}
300211

301212
impl ClearProtoMessage for psa_cipher_encrypt::Result {
302-
fn clear_message(&mut self) { self.ciphertext.zeroize(); }
213+
fn clear_message(&mut self) {
214+
self.ciphertext.zeroize();
215+
}
303216
}
304217

305218
impl ClearProtoMessage for psa_cipher_decrypt::Result {
306-
fn clear_message(&mut self) { self.plaintext.zeroize(); }
219+
fn clear_message(&mut self) {
220+
self.plaintext.zeroize();
221+
}
307222
}
308223

309224
impl ClearProtoMessage for psa_generate_random::Result {
@@ -402,23 +317,23 @@ impl ClearProtoMessage for prepare_key_attestation::Result {
402317
#[test]
403318
fn i32_conversions() {
404319
assert_eq!(
405-
Cipher::try_from(56).unwrap_err(),
320+
<::prost::DecodeError as Into<ResponseStatus>>::into(Cipher::try_from(56).unwrap_err()),
406321
ResponseStatus::InvalidEncoding
407322
);
408323
assert_eq!(
409-
Cipher::try_from(-5).unwrap_err(),
324+
<::prost::DecodeError as Into<ResponseStatus>>::into(Cipher::try_from(-5).unwrap_err()),
410325
ResponseStatus::InvalidEncoding
411326
);
412327
assert_eq!(
413-
Hash::try_from(89).unwrap_err(),
328+
<::prost::DecodeError as Into<ResponseStatus>>::into(Hash::try_from(89).unwrap_err()),
414329
ResponseStatus::InvalidEncoding
415330
);
416331
assert_eq!(
417-
Hash::try_from(-4).unwrap_err(),
332+
<::prost::DecodeError as Into<ResponseStatus>>::into(Hash::try_from(-4).unwrap_err()),
418333
ResponseStatus::InvalidEncoding
419334
);
420335
assert_eq!(
421-
EccFamily::try_from(78).unwrap_err(),
336+
<::prost::DecodeError as Into<ResponseStatus>>::into(EccFamily::try_from(78).unwrap_err()),
422337
ResponseStatus::InvalidEncoding
423338
);
424339
}

src/requests/response_status.rs

+10
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ impl From<std::io::Error> for ResponseStatus {
295295
}
296296
}
297297

298+
impl From<::prost::DecodeError> for ResponseStatus {
299+
fn from(err: ::prost::DecodeError) -> Self {
300+
warn!(
301+
"Conversion from {} to ResponseStatus::InvalidEncoding.",
302+
err
303+
);
304+
ResponseStatus::InvalidEncoding
305+
}
306+
}
307+
298308
impl From<bincode::Error> for ResponseStatus {
299309
fn from(err: bincode::Error) -> Self {
300310
warn!(

0 commit comments

Comments
 (0)