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
|
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use super::convert_psa_algorithm;
use super::generated_ops::psa_cipher_decrypt::{
Operation as OperationProto, Result as ResultProto,
};
use crate::operations::psa_cipher_decrypt::{Operation, Result};
use crate::requests::ResponseStatus;
use std::convert::TryFrom;
impl TryFrom<OperationProto> for Operation {
type Error = ResponseStatus;
fn try_from(proto_op: OperationProto) -> std::result::Result<Self, Self::Error> {
Ok(Operation {
key_name: proto_op.key_name,
alg: convert_psa_algorithm::i32_to_cipher(proto_op.alg)?,
ciphertext: proto_op.ciphertext.into(),
})
}
}
impl TryFrom<Operation> for OperationProto {
type Error = ResponseStatus;
fn try_from(op: Operation) -> std::result::Result<Self, Self::Error> {
Ok(OperationProto {
key_name: op.key_name,
alg: convert_psa_algorithm::cipher_to_i32(op.alg),
ciphertext: op.ciphertext.to_vec(),
})
}
}
impl TryFrom<ResultProto> for Result {
type Error = ResponseStatus;
fn try_from(proto_result: ResultProto) -> std::result::Result<Self, Self::Error> {
Ok(Result {
plaintext: proto_result.plaintext.into(),
})
}
}
impl TryFrom<Result> for ResultProto {
type Error = ResponseStatus;
fn try_from(result: Result) -> std::result::Result<Self, Self::Error> {
Ok(ResultProto {
plaintext: result.plaintext.to_vec(),
})
}
}
#[cfg(test)]
mod test {
use super::super::generated_ops::psa_cipher_decrypt::{
Operation as OperationProto, Result as ResultProto,
};
use super::super::{Convert, ProtobufConverter};
use super::convert_psa_algorithm;
use crate::operations::psa_algorithm::Cipher;
use crate::operations::psa_cipher_decrypt::{Operation, Result};
use std::convert::TryInto;
static CONVERTER: ProtobufConverter = ProtobufConverter {};
use crate::operations::{NativeOperation, NativeResult};
use crate::requests::{request::RequestBody, response::ResponseBody, Opcode};
use zeroize::Zeroizing;
#[test]
fn cipher_proto_to_op() {
let mut proto: OperationProto = Default::default();
let message = vec![0x11, 0x22, 0x33];
let key_name = "test name".to_string();
let proto_alg = Cipher::StreamCipher;
proto.ciphertext = message.clone();
proto.alg = convert_psa_algorithm::cipher_to_i32(proto_alg);
proto.key_name = key_name.clone();
let op: Operation = proto.try_into().expect("Failed to convert");
assert_eq!(op.ciphertext.to_vec(), message);
assert_eq!(op.key_name, key_name);
}
#[test]
fn cipher_op_to_proto() {
let message = vec![0x11, 0x22, 0x33];
let key_name = "test name".to_string();
let op = Operation {
ciphertext: Zeroizing::new(message.clone()),
alg: Cipher::StreamCipher,
key_name: key_name.clone(),
};
let proto: OperationProto = op.try_into().expect("Failed to convert");
assert_eq!(proto.ciphertext, message);
assert_eq!(proto.key_name, key_name);
}
#[test]
fn cipher_proto_to_resp() {
let mut proto: ResultProto = Default::default();
let plaintext: Vec<u8> = vec![0x11, 0x22, 0x33];
proto.plaintext = plaintext.clone();
let result: Result = proto.try_into().expect("Failed to convert");
assert_eq!(*result.plaintext, plaintext);
}
#[test]
fn cipher_resp_to_proto() {
let plaintext = vec![0x11, 0x22, 0x33];
let result = Result {
plaintext: plaintext.clone().into(),
};
let proto: ResultProto = result.try_into().expect("Failed to convert");
assert_eq!(proto.plaintext, plaintext);
}
#[test]
fn psa_decrypt_message_op_e2e() {
let name = "test name".to_string();
let op = Operation {
key_name: name,
alg: Cipher::StreamCipher,
ciphertext: Zeroizing::new(vec![0x11, 0x22, 0x33]),
};
let body = CONVERTER
.operation_to_body(NativeOperation::PsaCipherDecrypt(op))
.expect("Failed to convert to body");
let _ = CONVERTER
.body_to_operation(body, Opcode::PsaCipherDecrypt)
.expect("Failed to convert to operation");
}
#[test]
fn resp_cipher_decrypt_e2e() {
let result = Result {
plaintext: vec![0x11, 0x22, 0x33].into(),
};
let body = CONVERTER
.result_to_body(NativeResult::PsaCipherDecrypt(result))
.expect("Failed to convert request");
assert!(CONVERTER
.body_to_result(body, Opcode::PsaCipherDecrypt)
.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::PsaCipherDecrypt)
.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::PsaCipherDecrypt)
.is_err());
}
}
|