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
use crate::api::*;
use crate::error::Error;
use crate::service::*;
use crate::types::*;
#[cfg(feature = "aes256-cbc")]
impl Encrypt for super::Aes256Cbc
{
fn encrypt(keystore: &mut impl Keystore, request: &request::Encrypt)
-> Result<reply::Encrypt, Error>
{
use block_modes::{BlockMode, Cbc};
use block_modes::block_padding::ZeroPadding;
use aes::Aes256;
type Aes256Cbc = Cbc<Aes256, ZeroPadding>;
let key_id = request.key;
let symmetric_key: [u8; 32] = keystore
.load_key(key::Secrecy::Secret, None, &key_id)?
.material.as_slice().try_into()
.map_err(|_| Error::InternalError)?;
let zero_iv = [0u8; 16];
let cipher = Aes256Cbc::new_from_slices(&symmetric_key, &zero_iv).unwrap();
let mut buffer = request.message.clone();
let l = buffer.len();
let ciphertext = cipher.encrypt(&mut buffer, l).unwrap();
let ciphertext = Message::from_slice(&ciphertext).unwrap();
Ok(reply::Encrypt { ciphertext, nonce: ShortData::new(), tag: ShortData::new() })
}
}
#[cfg(feature = "aes256-cbc")]
impl WrapKey for super::Aes256Cbc
{
fn wrap_key(keystore: &mut impl Keystore, request: &request::WrapKey)
-> Result<reply::WrapKey, Error>
{
let message = Message::from_slice(keystore
.load_key(key::Secrecy::Secret, None, &request.key)?
.material.as_slice()).map_err(|_| Error::InternalError)?;
let encryption_request = request::Encrypt {
mechanism: Mechanism::Aes256Cbc,
key: request.wrapping_key,
message,
associated_data: ShortData::new(),
nonce: None,
};
let encryption_reply = <super::Aes256Cbc>::encrypt(keystore, &encryption_request)?;
let wrapped_key = encryption_reply.ciphertext;
Ok(reply::WrapKey { wrapped_key })
}
}
#[cfg(feature = "aes256-cbc")]
impl Decrypt for super::Aes256Cbc
{
fn decrypt(keystore: &mut impl Keystore, request: &request::Decrypt)
-> Result<reply::Decrypt, Error>
{
use block_modes::{BlockMode, Cbc};
use block_modes::block_padding::ZeroPadding;
use aes::Aes256;
type Aes256Cbc = Cbc<Aes256, ZeroPadding>;
let key_id = request.key;
let symmetric_key: [u8; 32] = keystore
.load_key(key::Secrecy::Secret, None, &key_id)?
.material.as_slice()
.try_into()
.map_err(|_| Error::InternalError)?;
let zero_iv = [0u8; 16];
let cipher = Aes256Cbc::new_from_slices(&symmetric_key, &zero_iv).unwrap();
let mut buffer = request.message.clone();
let plaintext = cipher.decrypt(&mut buffer).unwrap();
let plaintext = Message::from_slice(&plaintext).unwrap();
Ok(reply::Decrypt { plaintext: Some(plaintext) })
}
}
#[cfg(not(feature = "aes256-cbc"))]
impl Decrypt for super::Aes256Cbc {}
#[cfg(not(feature = "aes256-cbc"))]
impl Encrypt for super::Aes256Cbc {}