-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmechanisms.rs
More file actions
375 lines (314 loc) · 13.1 KB
/
mechanisms.rs
File metadata and controls
375 lines (314 loc) · 13.1 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use super::*;
#[cfg(feature = "aes256-cbc")]
impl<S: Syscall> Aes256Cbc for ClientImplementation<S> {}
pub trait Aes256Cbc: CryptoClient {
fn decrypt_aes256cbc<'c>(&'c mut self, key: KeyId, message: &[u8])
-> ClientResult<'c, reply::Decrypt, Self>
{
self.decrypt(
Mechanism::Aes256Cbc, key, message, &[], &[], &[],
)
}
fn wrap_key_aes256cbc(&mut self, wrapping_key: KeyId, key: KeyId)
-> ClientResult<'_, reply::WrapKey, Self>
{
self.wrap_key(Mechanism::Aes256Cbc, wrapping_key, key, &[])
}
}
#[cfg(feature = "chacha8-poly1305")]
impl<S: Syscall> Chacha8Poly1305 for ClientImplementation<S> {}
pub trait Chacha8Poly1305: CryptoClient {
fn decrypt_chacha8poly1305<'c>(&'c mut self, key: KeyId, message: &[u8], associated_data: &[u8],
nonce: &[u8], tag: &[u8])
-> ClientResult<'c, reply::Decrypt, Self>
{
self.decrypt(Mechanism::Chacha8Poly1305, key, message, associated_data, nonce, tag)
}
fn encrypt_chacha8poly1305<'c>(&'c mut self, key: KeyId, message: &[u8], associated_data: &[u8],
nonce: Option<&[u8; 12]>)
-> ClientResult<'c, reply::Encrypt, Self>
{
self.encrypt(Mechanism::Chacha8Poly1305, key, message, associated_data,
nonce.and_then(|nonce| ShortData::from_slice(nonce).ok()))
}
fn generate_chacha8poly1305_key(&mut self, persistence: Location)
-> ClientResult<'_, reply::GenerateKey, Self>
{
self.generate_key(Mechanism::Chacha8Poly1305, StorageAttributes::new().set_persistence(persistence))
}
fn unwrap_key_chacha8poly1305<'c>(&'c mut self, wrapping_key: KeyId, wrapped_key: &[u8],
associated_data: &[u8], location: Location)
-> ClientResult<'c, reply::UnwrapKey, Self>
{
self.unwrap_key(Mechanism::Chacha8Poly1305, wrapping_key,
Message::from_slice(wrapped_key).map_err(|_| ClientError::DataTooLarge)?,
associated_data,
StorageAttributes::new().set_persistence(location))
}
fn wrap_key_chacha8poly1305<'c>(&'c mut self, wrapping_key: KeyId, key: KeyId,
associated_data: &[u8])
-> ClientResult<'c, reply::WrapKey, Self>
{
self.wrap_key(Mechanism::Chacha8Poly1305, wrapping_key, key, associated_data)
}
}
#[cfg(feature = "hmac-blake2s")]
impl<S: Syscall> HmacBlake2s for ClientImplementation<S> {}
pub trait HmacBlake2s: CryptoClient {
fn hmacblake2s_derive_key(&mut self, base_key: KeyId, message: &[u8], persistence: Location)
-> ClientResult<'_, reply::DeriveKey, Self>
{
self.derive_key(
Mechanism::HmacBlake2s, base_key,
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence))
}
fn sign_hmacblake2s<'c>(&'c mut self, key: KeyId, message: &[u8])
-> ClientResult<'c, reply::Sign, Self>
{
self.sign(Mechanism::HmacBlake2s, key, message, SignatureSerialization::Raw)
}
}
#[cfg(feature = "hmac-sha1")]
impl<S: Syscall> HmacSha1 for ClientImplementation<S> {}
pub trait HmacSha1: CryptoClient {
fn hmacsha1_derive_key(&mut self, base_key: KeyId, message: &[u8], persistence: Location)
-> ClientResult<'_, reply::DeriveKey, Self>
{
self.derive_key(
Mechanism::HmacSha1, base_key,
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence))
}
fn sign_hmacsha1<'c>(&'c mut self, key: KeyId, message: &[u8])
-> ClientResult<'c, reply::Sign, Self>
{
self.sign(Mechanism::HmacSha1, key, message, SignatureSerialization::Raw)
}
}
#[cfg(feature = "hmac-sha256")]
impl<S: Syscall> HmacSha256 for ClientImplementation<S> {}
pub trait HmacSha256: CryptoClient {
fn hmacsha256_derive_key(&mut self, base_key: KeyId, message: &[u8], persistence: Location)
-> ClientResult<'_, reply::DeriveKey, Self>
{
self.derive_key(
Mechanism::HmacSha256, base_key,
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence))
}
fn sign_hmacsha256<'c>(&'c mut self, key: KeyId, message: &[u8])
-> ClientResult<'c, reply::Sign, Self>
{
self.sign(Mechanism::HmacSha256, key, message, SignatureSerialization::Raw)
}
}
#[cfg(feature = "hmac-sha512")]
impl<S: Syscall> HmacSha512 for ClientImplementation<S> {}
pub trait HmacSha512: CryptoClient {
fn hmacsha512_derive_key(&mut self, base_key: KeyId, message: &[u8], persistence: Location)
-> ClientResult<'_, reply::DeriveKey, Self>
{
self.derive_key(
Mechanism::HmacSha512, base_key,
Some(MediumData::from_slice(message).map_err(|_| ClientError::DataTooLarge)?),
StorageAttributes::new().set_persistence(persistence))
}
fn sign_hmacsha512<'c>(&'c mut self, key: KeyId, message: &[u8])
-> ClientResult<'c, reply::Sign, Self>
{
self.sign(Mechanism::HmacSha512, key, message, SignatureSerialization::Raw)
}
}
#[cfg(feature = "ed255")]
impl<S: Syscall> Ed255 for ClientImplementation<S> {}
pub trait Ed255: CryptoClient {
fn generate_ed255_private_key(&mut self, persistence: Location)
-> ClientResult<'_, reply::GenerateKey, Self>
{
self.generate_key(Mechanism::Ed255, StorageAttributes::new().set_persistence(persistence))
}
fn derive_ed255_public_key(&mut self, private_key: KeyId, persistence: Location)
-> ClientResult<'_, reply::DeriveKey, Self>
{
self.derive_key(Mechanism::Ed255, private_key, None, StorageAttributes::new().set_persistence(persistence))
}
fn deserialize_ed255_key<'c>(&'c mut self, serialized_key: &[u8], format: KeySerialization, attributes: StorageAttributes)
-> ClientResult<'c, reply::DeserializeKey, Self>
{
self.deserialize_key(Mechanism::Ed255, serialized_key, format, attributes)
}
fn serialize_ed255_key(&mut self, key: KeyId, format: KeySerialization)
-> ClientResult<'_, reply::SerializeKey, Self>
{
self.serialize_key(Mechanism::Ed255, key, format)
}
fn sign_ed255<'c>(&'c mut self, key: KeyId, message: &[u8])
-> ClientResult<'c, reply::Sign, Self>
{
self.sign(Mechanism::Ed255, key, message, SignatureSerialization::Raw)
}
fn verify_ed255<'c>(&'c mut self, key: KeyId, message: &[u8], signature: &[u8])
-> ClientResult<'c, reply::Verify, Self>
{
self.verify(Mechanism::Ed255, key, message, signature, SignatureSerialization::Raw)
}
}
#[cfg(feature = "p256")]
impl<S: Syscall> P256 for ClientImplementation<S> {}
pub trait P256: CryptoClient {
fn generate_p256_private_key(&mut self, persistence: Location)
-> ClientResult<'_, reply::GenerateKey, Self>
{
self.generate_key(Mechanism::P256, StorageAttributes::new().set_persistence(persistence))
}
fn derive_p256_public_key(&mut self, private_key: KeyId, persistence: Location)
-> ClientResult<'_, reply::DeriveKey, Self>
{
self.derive_key(Mechanism::P256, private_key, None, StorageAttributes::new().set_persistence(persistence))
}
fn deserialize_p256_key<'c>(&'c mut self, serialized_key: &[u8], format: KeySerialization, attributes: StorageAttributes)
-> ClientResult<'c, reply::DeserializeKey, Self>
{
self.deserialize_key(Mechanism::P256, serialized_key, format, attributes)
}
fn serialize_p256_key(&mut self, key: KeyId, format: KeySerialization)
-> ClientResult<'_, reply::SerializeKey, Self>
{
self.serialize_key(Mechanism::P256, key, format)
}
// generally, don't offer multiple versions of a mechanism, if possible.
// try using the simplest when given the choice.
// hashing is something users can do themselves hopefully :)
//
// on the other hand: if users need sha256, then if the service runs in secure trustzone
// domain, we'll maybe need two copies of the sha2 code
fn sign_p256<'c>(&'c mut self, key: KeyId, message: &[u8], format: SignatureSerialization)
-> ClientResult<'c, reply::Sign, Self>
{
self.sign(Mechanism::P256, key, message, format)
}
fn verify_p256<'c>(&'c mut self, key: KeyId, message: &[u8], signature: &[u8])
-> ClientResult<'c, reply::Verify, Self>
{
self.verify(Mechanism::P256, key, message, signature, SignatureSerialization::Raw)
}
fn agree_p256(&mut self, private_key: KeyId, public_key: KeyId, persistence: Location)
-> ClientResult<'_, reply::Agree, Self>
{
self.agree(
Mechanism::P256,
private_key,
public_key,
StorageAttributes::new().set_persistence(persistence),
)
}
}
#[cfg(feature = "p384")]
impl<S: Syscall> P384 for ClientImplementation<S> {}
pub trait P384: CryptoClient {
fn generate_p384_private_key(&mut self, persistence: Location)
-> ClientResult<'_, reply::GenerateKey, Self>
{
self.generate_key(Mechanism::P384, StorageAttributes::new().set_persistence(persistence))
}
fn derive_p384_public_key(&mut self, private_key: KeyId, persistence: Location)
-> ClientResult<'_, reply::DeriveKey, Self>
{
self.derive_key(Mechanism::P384, private_key, None, StorageAttributes::new().set_persistence(persistence))
}
fn deserialize_p384_key<'c>(&'c mut self, serialized_key: &[u8], format: KeySerialization, attributes: StorageAttributes)
-> ClientResult<'c, reply::DeserializeKey, Self>
{
self.deserialize_key(Mechanism::P384, serialized_key, format, attributes)
}
fn serialize_p384_key(&mut self, key: KeyId, format: KeySerialization)
-> ClientResult<'_, reply::SerializeKey, Self>
{
self.serialize_key(Mechanism::P384, key, format)
}
fn sign_p384<'c>(&'c mut self, key: KeyId, message: &[u8], format: SignatureSerialization)
-> ClientResult<'c, reply::Sign, Self>
{
self.sign(Mechanism::P384, key, message, format)
}
fn verify_p384<'c>(&'c mut self, key: KeyId, message: &[u8], signature: &[u8])
-> ClientResult<'c, reply::Verify, Self>
{
self.verify(Mechanism::P384, key, message, signature, SignatureSerialization::Raw)
}
fn agree_p384(&mut self, private_key: KeyId, public_key: KeyId, persistence: Location)
-> ClientResult<'_, reply::Agree, Self>
{
self.agree(
Mechanism::P384,
private_key,
public_key,
StorageAttributes::new().set_persistence(persistence),
)
}
}
#[cfg(feature = "sha256")]
impl<S: Syscall> Sha256 for ClientImplementation<S> {}
pub trait Sha256: CryptoClient {
fn sha256_derive_key(&mut self, shared_key: KeyId, persistence: Location)
-> ClientResult<'_, reply::DeriveKey, Self>
{
self.derive_key(Mechanism::Sha256, shared_key, None, StorageAttributes::new().set_persistence(persistence))
}
fn hash_sha256<'c>(&'c mut self, message: &[u8])
-> ClientResult<'c, reply::Hash, Self>
{
self.hash(Mechanism::Sha256, Message::from_slice(message).map_err(|_| ClientError::DataTooLarge)?)
}
}
#[cfg(feature = "tdes")]
impl<S: Syscall> Tdes for ClientImplementation<S> {}
pub trait Tdes: CryptoClient {
fn decrypt_tdes<'c>(&'c mut self, key: KeyId, message: &[u8])
-> ClientResult<'c, reply::Decrypt, Self>
{
self.decrypt(Mechanism::Tdes, key, message, &[], &[], &[])
}
fn encrypt_tdes<'c>(&'c mut self, key: KeyId, message: &[u8])
-> ClientResult<'c, reply::Encrypt, Self>
{
self.encrypt(Mechanism::Tdes, key, message, &[], None)
}
}
#[cfg(feature = "totp")]
impl<S: Syscall> Totp for ClientImplementation<S> {}
pub trait Totp: CryptoClient {
fn sign_totp(&mut self, key: KeyId, timestamp: u64)
-> ClientResult<'_, reply::Sign, Self>
{
self.sign(Mechanism::Totp, key,
×tamp.to_le_bytes().as_ref(),
SignatureSerialization::Raw,
)
}
}
#[cfg(feature = "x255")]
impl<S: Syscall> X255 for ClientImplementation<S> {}
pub trait X255: CryptoClient {
fn generate_x255_secret_key(&mut self, persistence: Location)
-> ClientResult<'_, reply::GenerateKey, Self>
{
self.generate_key(Mechanism::X255, StorageAttributes::new().set_persistence(persistence))
}
fn derive_x255_public_key(&mut self, secret_key: KeyId, persistence: Location)
-> ClientResult<'_, reply::DeriveKey, Self>
{
self.derive_key(Mechanism::X255, secret_key, None, StorageAttributes::new().set_persistence(persistence))
}
fn agree_x255(&mut self, private_key: KeyId, public_key: KeyId, persistence: Location)
-> ClientResult<'_, reply::Agree, Self>
{
self.agree(
Mechanism::X255,
private_key,
public_key,
StorageAttributes::new().set_persistence(persistence),
)
}
}