1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use super::generated_ops::psa_verify_hash::{Operation as OperationProto, Result as ResultProto};
use crate::operations::psa_verify_hash::{Operation, Result};
use crate::requests::ResponseStatus;
use log::error;
use std::convert::{TryFrom, TryInto};
use zeroize::Zeroizing;
impl TryFrom<OperationProto> for Operation {
type Error = ResponseStatus;
fn try_from(proto_op: OperationProto) -> std::result::Result<Self, Self::Error> {
let hash = Zeroizing::new(proto_op.hash);
let signature = Zeroizing::new(proto_op.signature);
Ok(Operation {
key_name: proto_op.key_name,
alg: proto_op
.alg
.ok_or_else(|| {
error!("alg field of psa_verify_hash::Operation message is empty.");
ResponseStatus::InvalidEncoding
})?
.try_into()?,
hash,
signature,
})
}
}
impl TryFrom<Operation> for OperationProto {
type Error = ResponseStatus;
fn try_from(op: Operation) -> std::result::Result<Self, Self::Error> {
let alg = Some(op.alg.try_into()?);
Ok(OperationProto {
key_name: op.key_name,
alg,
hash: op.hash.to_vec(),
signature: op.signature.to_vec(),
})
}
}
impl TryFrom<ResultProto> for Result {
type Error = ResponseStatus;
fn try_from(_proto_result: ResultProto) -> std::result::Result<Self, Self::Error> {
Ok(Result {})
}
}
impl TryFrom<Result> for ResultProto {
type Error = ResponseStatus;
fn try_from(_result: Result) -> std::result::Result<Self, Self::Error> {
Ok(ResultProto {})
}
}
#[cfg(test)]
mod test {
use super::super::generated_ops::psa_algorithm as algorithm_proto;
use super::super::generated_ops::psa_verify_hash::{
Operation as OperationProto, Result as ResultProto,
};
use super::super::{Convert, ProtobufConverter};
use crate::operations::psa_algorithm::AsymmetricSignature;
use crate::operations::psa_verify_hash::{Operation, Result};
use crate::operations::{NativeOperation, NativeResult};
use crate::requests::{request::RequestBody, response::ResponseBody, Opcode};
use std::convert::TryInto;
static CONVERTER: ProtobufConverter = ProtobufConverter {};
#[test]
fn asym_proto_to_op() {
let mut proto: OperationProto = Default::default();
let hash = vec![0x11, 0x22, 0x33];
let key_name = "test name".to_string();
let signature = vec![0x11, 0x22, 0x33];
proto.hash = hash.clone();
proto.alg = Some(algorithm_proto::algorithm::AsymmetricSignature {
variant: Some(
algorithm_proto::algorithm::asymmetric_signature::Variant::RsaPkcs1v15Sign(
algorithm_proto::algorithm::asymmetric_signature::RsaPkcs1v15Sign {
hash_alg: Some(algorithm_proto::algorithm::asymmetric_signature::SignHash {
variant: Some(algorithm_proto::algorithm::asymmetric_signature::sign_hash::Variant::Specific(
algorithm_proto::algorithm::Hash::Sha1.into(),
)),
}),
},
),
),
});
proto.key_name = key_name.clone();
proto.signature = signature.clone();
let op: Operation = proto.try_into().expect("Failed to convert");
assert_eq!(op.hash, hash.into());
assert_eq!(op.key_name, key_name);
assert_eq!(op.signature, signature.into());
}
#[test]
fn asym_op_to_proto() {
let hash = vec![0x11, 0x22, 0x33];
let key_name = "test name".to_string();
let signature = vec![0x11, 0x22, 0x33];
let op = Operation {
hash: hash.clone().into(),
alg: AsymmetricSignature::RsaPkcs1v15SignRaw,
key_name: key_name.clone(),
signature: signature.clone().into(),
};
let proto: OperationProto = op.try_into().expect("Failed to convert");
assert_eq!(proto.hash, hash);
assert_eq!(proto.key_name, key_name);
assert_eq!(proto.signature, signature);
}
#[test]
fn asym_proto_to_resp() {
let proto: ResultProto = Default::default();
let _result: Result = proto.try_into().expect("Failed to convert");
}
#[test]
fn asym_resp_to_proto() {
let result = Result {};
let _proto: ResultProto = result.try_into().expect("Failed to convert");
}
#[test]
fn op_asym_sign_e2e() {
let op = Operation {
hash: vec![0x11, 0x22, 0x33].into(),
alg: AsymmetricSignature::RsaPkcs1v15SignRaw,
key_name: "test name".to_string(),
signature: vec![0x11, 0x22, 0x33].into(),
};
let body = CONVERTER
.operation_to_body(NativeOperation::PsaVerifyHash(op))
.expect("Failed to convert request");
assert!(CONVERTER
.body_to_operation(body, Opcode::PsaVerifyHash)
.is_ok());
}
#[test]
fn resp_asym_sign_e2e() {
let result = Result {};
let body = CONVERTER
.result_to_body(NativeResult::PsaVerifyHash(result))
.expect("Failed to convert request");
assert!(CONVERTER
.body_to_result(body, Opcode::PsaVerifyHash)
.is_ok());
}
#[test]
fn result_from_mangled_resp_body() {
let resp_body =
ResponseBody::from_bytes(vec![0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]);
assert!(CONVERTER
.body_to_result(resp_body, Opcode::PsaVerifyHash)
.is_err());
}
#[test]
fn op_from_mangled_req_body() {
let req_body =
RequestBody::from_bytes(vec![0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]);
assert!(CONVERTER
.body_to_operation(req_body, Opcode::PsaVerifyHash)
.is_err());
}
}