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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Copyright 2019 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
//! # Rust representation of operations
//!
//! Rust native representation of the language neutral operations described in the
//! [Operations](https://parallaxsecond.github.io/parsec-book/parsec_client/operations/index.html)
//! page in the book.
//! Some of the doc comments have directly been taken from the PSA Crypto API document version
//! 1.0.0. Please check that
//! [document](https://developer.arm.com/architectures/security-architectures/platform-security-architecture/documentation)
//! and the book for more details.
pub mod ping;
pub mod psa_generate_key;
pub mod psa_import_key;
pub mod psa_export_public_key;
pub mod psa_export_key;
pub mod psa_destroy_key;
pub mod psa_sign_hash;
pub mod psa_verify_hash;
pub mod psa_hash_compute;
pub mod psa_hash_compare;
pub mod psa_asymmetric_encrypt;
pub mod psa_asymmetric_decrypt;
pub mod psa_aead_encrypt;
pub mod psa_aead_decrypt;
pub mod list_opcodes;
pub mod list_providers;
pub mod list_authenticators;
pub mod list_keys;
pub mod delete_client;
pub mod list_clients;
pub mod psa_generate_random;
pub mod psa_raw_key_agreement;

pub use psa_crypto::types::algorithm as psa_algorithm;
pub use psa_crypto::types::key as psa_key_attributes;

use crate::requests::{request::RequestBody, response::ResponseBody, BodyType, Opcode, Result};

/// Container type for operation conversion values, holding a native operation object
/// to be passed in/out of a converter.
#[derive(Debug)]
pub enum NativeOperation {
    /// ListProviders operation
    ListProviders(list_providers::Operation),
    /// ListOpcodes operation
    ListOpcodes(list_opcodes::Operation),
    /// ListAuthenticators operation
    ListAuthenticators(list_authenticators::Operation),
    /// ListKeys operation
    ListKeys(list_keys::Operation),
    /// ListClients operation
    ListClients(list_clients::Operation),
    /// DeleteClient operation
    DeleteClient(delete_client::Operation),
    /// Ping operation
    Ping(ping::Operation),
    /// PsaGenerateKey operation
    PsaGenerateKey(psa_generate_key::Operation),
    /// PsaImportKey operation
    PsaImportKey(psa_import_key::Operation),
    /// PsaExportPublicKey operation
    PsaExportPublicKey(psa_export_public_key::Operation),
    /// PsaExportKey operation
    PsaExportKey(psa_export_key::Operation),
    /// PsaDestroyKey operation
    PsaDestroyKey(psa_destroy_key::Operation),
    /// PsaSignHash operation
    PsaSignHash(psa_sign_hash::Operation),
    /// PsaVerifyHash operation
    PsaVerifyHash(psa_verify_hash::Operation),
    /// PsaHashCompute operation
    PsaHashCompute(psa_hash_compute::Operation),
    /// PsaHashCompare operation
    PsaHashCompare(psa_hash_compare::Operation),
    /// PsaAsymmetricEncrypt operation
    PsaAsymmetricEncrypt(psa_asymmetric_encrypt::Operation),
    /// PsaAsymmetricDecrypt operation
    PsaAsymmetricDecrypt(psa_asymmetric_decrypt::Operation),
    /// PsaAeadEncrypt operation
    PsaAeadEncrypt(psa_aead_encrypt::Operation),
    /// PsaAeadDecrypt operation
    PsaAeadDecrypt(psa_aead_decrypt::Operation),
    /// PsaGenerateRandom operation
    PsaGenerateRandom(psa_generate_random::Operation),
    /// PsaRawKeyAgreement operation
    PsaRawKeyAgreement(psa_raw_key_agreement::Operation),
}

impl NativeOperation {
    /// Return the opcode of the operation associated.
    pub fn opcode(&self) -> Opcode {
        match self {
            NativeOperation::Ping(_) => Opcode::Ping,
            NativeOperation::PsaGenerateKey(_) => Opcode::PsaGenerateKey,
            NativeOperation::PsaDestroyKey(_) => Opcode::PsaDestroyKey,
            NativeOperation::PsaSignHash(_) => Opcode::PsaSignHash,
            NativeOperation::PsaVerifyHash(_) => Opcode::PsaVerifyHash,
            NativeOperation::PsaHashCompute(_) => Opcode::PsaHashCompute,
            NativeOperation::PsaHashCompare(_) => Opcode::PsaHashCompare,
            NativeOperation::PsaImportKey(_) => Opcode::PsaImportKey,
            NativeOperation::PsaExportPublicKey(_) => Opcode::PsaExportPublicKey,
            NativeOperation::PsaExportKey(_) => Opcode::PsaExportKey,
            NativeOperation::ListOpcodes(_) => Opcode::ListOpcodes,
            NativeOperation::ListProviders(_) => Opcode::ListProviders,
            NativeOperation::ListAuthenticators(_) => Opcode::ListAuthenticators,
            NativeOperation::ListKeys(_) => Opcode::ListKeys,
            NativeOperation::ListClients(_) => Opcode::ListClients,
            NativeOperation::DeleteClient(_) => Opcode::DeleteClient,
            NativeOperation::PsaAsymmetricEncrypt(_) => Opcode::PsaAsymmetricEncrypt,
            NativeOperation::PsaAsymmetricDecrypt(_) => Opcode::PsaAsymmetricDecrypt,
            NativeOperation::PsaAeadEncrypt(_) => Opcode::PsaAeadEncrypt,
            NativeOperation::PsaAeadDecrypt(_) => Opcode::PsaAeadDecrypt,
            NativeOperation::PsaGenerateRandom(_) => Opcode::PsaGenerateRandom,
            NativeOperation::PsaRawKeyAgreement(_) => Opcode::PsaRawKeyAgreement,
        }
    }
}

/// Container type for result conversion values, holding a native result object to be
/// passed in/out of the converter.
#[derive(Debug)]
pub enum NativeResult {
    /// ListProviders result
    ListProviders(list_providers::Result),
    /// ListOpcodes result
    ListOpcodes(list_opcodes::Result),
    /// ListAuthenticators result
    ListAuthenticators(list_authenticators::Result),
    /// ListKeys result
    ListKeys(list_keys::Result),
    /// ListClients result
    ListClients(list_clients::Result),
    /// DeleteClient result
    DeleteClient(delete_client::Result),
    /// Ping result
    Ping(ping::Result),
    /// PsaGenerateKey result
    PsaGenerateKey(psa_generate_key::Result),
    /// PsaImportKey result
    PsaImportKey(psa_import_key::Result),
    /// PsaExportPublicKey result
    PsaExportPublicKey(psa_export_public_key::Result),
    /// PsaExportKey result
    PsaExportKey(psa_export_key::Result),
    /// PsaDestroyKey result
    PsaDestroyKey(psa_destroy_key::Result),
    /// PsaSignHash result
    PsaSignHash(psa_sign_hash::Result),
    /// PsaHashCompute result
    PsaHashCompute(psa_hash_compute::Result),
    /// PsaHashCompare result
    PsaHashCompare(psa_hash_compare::Result),
    /// PsaVerifyHash result
    PsaVerifyHash(psa_verify_hash::Result),
    /// PsaAsymmetricEncrypt result
    PsaAsymmetricEncrypt(psa_asymmetric_encrypt::Result),
    /// PsaAsymmetricDecrypt result
    PsaAsymmetricDecrypt(psa_asymmetric_decrypt::Result),
    /// PsaAeadEncrypt result
    PsaAeadEncrypt(psa_aead_encrypt::Result),
    /// PsaAeadDecrypt result
    PsaAeadDecrypt(psa_aead_decrypt::Result),
    /// PsaGenerateRandom result
    PsaGenerateRandom(psa_generate_random::Result),
    /// PsaRawKeyAgreement result
    PsaRawKeyAgreement(psa_raw_key_agreement::Result),
}

impl NativeResult {
    /// Return the opcode of the operation associated.
    pub fn opcode(&self) -> Opcode {
        match self {
            NativeResult::Ping(_) => Opcode::Ping,
            NativeResult::PsaGenerateKey(_) => Opcode::PsaGenerateKey,
            NativeResult::PsaDestroyKey(_) => Opcode::PsaDestroyKey,
            NativeResult::PsaSignHash(_) => Opcode::PsaSignHash,
            NativeResult::PsaVerifyHash(_) => Opcode::PsaVerifyHash,
            NativeResult::PsaImportKey(_) => Opcode::PsaImportKey,
            NativeResult::PsaHashCompute(_) => Opcode::PsaHashCompute,
            NativeResult::PsaHashCompare(_) => Opcode::PsaHashCompare,
            NativeResult::PsaExportPublicKey(_) => Opcode::PsaExportPublicKey,
            NativeResult::PsaExportKey(_) => Opcode::PsaExportKey,
            NativeResult::ListOpcodes(_) => Opcode::ListOpcodes,
            NativeResult::ListProviders(_) => Opcode::ListProviders,
            NativeResult::ListAuthenticators(_) => Opcode::ListAuthenticators,
            NativeResult::ListKeys(_) => Opcode::ListKeys,
            NativeResult::ListClients(_) => Opcode::ListClients,
            NativeResult::DeleteClient(_) => Opcode::DeleteClient,
            NativeResult::PsaAsymmetricEncrypt(_) => Opcode::PsaAsymmetricEncrypt,
            NativeResult::PsaAsymmetricDecrypt(_) => Opcode::PsaAsymmetricDecrypt,
            NativeResult::PsaAeadEncrypt(_) => Opcode::PsaAeadEncrypt,
            NativeResult::PsaAeadDecrypt(_) => Opcode::PsaAeadDecrypt,
            NativeResult::PsaGenerateRandom(_) => Opcode::PsaGenerateRandom,
            NativeResult::PsaRawKeyAgreement(_) => Opcode::PsaRawKeyAgreement,
        }
    }
}

/// Definition of the operations converters must implement to allow usage of a specific
/// `BodyType`.
pub trait Convert {
    /// Get the `BodyType` associated with this converter.
    fn body_type(&self) -> BodyType;

    /// Create a native operation object from a request body.
    ///
    /// # Errors
    /// - if deserialization fails, `ResponseStatus::DeserializingBodyFailed` is returned
    fn body_to_operation(&self, body: RequestBody, opcode: Opcode) -> Result<NativeOperation>;

    /// Create a request body from a native operation object.
    ///
    /// # Errors
    /// - if serialization fails, `ResponseStatus::SerializingBodyFailed` is returned
    fn operation_to_body(&self, operation: NativeOperation) -> Result<RequestBody>;

    /// Create a native result object from a response body.
    ///
    /// # Errors
    /// - if deserialization fails, `ResponseStatus::DeserializingBodyFailed` is returned
    fn body_to_result(&self, body: ResponseBody, opcode: Opcode) -> Result<NativeResult>;

    /// Create a response body from a native result object.
    ///
    /// # Errors
    /// - if serialization fails, `ResponseStatus::SerializingBodyFailed` is returned
    fn result_to_body(&self, result: NativeResult) -> Result<ResponseBody>;
}

impl From<list_providers::Operation> for NativeOperation {
    fn from(op: list_providers::Operation) -> Self {
        NativeOperation::ListProviders(op)
    }
}

impl From<list_opcodes::Operation> for NativeOperation {
    fn from(op: list_opcodes::Operation) -> Self {
        NativeOperation::ListOpcodes(op)
    }
}

impl From<list_authenticators::Operation> for NativeOperation {
    fn from(op: list_authenticators::Operation) -> Self {
        NativeOperation::ListAuthenticators(op)
    }
}

impl From<list_keys::Operation> for NativeOperation {
    fn from(op: list_keys::Operation) -> Self {
        NativeOperation::ListKeys(op)
    }
}

impl From<list_clients::Operation> for NativeOperation {
    fn from(op: list_clients::Operation) -> Self {
        NativeOperation::ListClients(op)
    }
}

impl From<delete_client::Operation> for NativeOperation {
    fn from(op: delete_client::Operation) -> Self {
        NativeOperation::DeleteClient(op)
    }
}

impl From<ping::Operation> for NativeOperation {
    fn from(op: ping::Operation) -> Self {
        NativeOperation::Ping(op)
    }
}

impl From<psa_generate_key::Operation> for NativeOperation {
    fn from(op: psa_generate_key::Operation) -> Self {
        NativeOperation::PsaGenerateKey(op)
    }
}

impl From<psa_import_key::Operation> for NativeOperation {
    fn from(op: psa_import_key::Operation) -> Self {
        NativeOperation::PsaImportKey(op)
    }
}

impl From<psa_export_public_key::Operation> for NativeOperation {
    fn from(op: psa_export_public_key::Operation) -> Self {
        NativeOperation::PsaExportPublicKey(op)
    }
}

impl From<psa_export_key::Operation> for NativeOperation {
    fn from(op: psa_export_key::Operation) -> Self {
        NativeOperation::PsaExportKey(op)
    }
}

impl From<psa_destroy_key::Operation> for NativeOperation {
    fn from(op: psa_destroy_key::Operation) -> Self {
        NativeOperation::PsaDestroyKey(op)
    }
}

impl From<psa_sign_hash::Operation> for NativeOperation {
    fn from(op: psa_sign_hash::Operation) -> Self {
        NativeOperation::PsaSignHash(op)
    }
}

impl From<psa_verify_hash::Operation> for NativeOperation {
    fn from(op: psa_verify_hash::Operation) -> Self {
        NativeOperation::PsaVerifyHash(op)
    }
}

impl From<psa_asymmetric_encrypt::Operation> for NativeOperation {
    fn from(op: psa_asymmetric_encrypt::Operation) -> Self {
        NativeOperation::PsaAsymmetricEncrypt(op)
    }
}

impl From<psa_asymmetric_decrypt::Operation> for NativeOperation {
    fn from(op: psa_asymmetric_decrypt::Operation) -> Self {
        NativeOperation::PsaAsymmetricDecrypt(op)
    }
}

impl From<psa_aead_encrypt::Operation> for NativeOperation {
    fn from(op: psa_aead_encrypt::Operation) -> Self {
        NativeOperation::PsaAeadEncrypt(op)
    }
}

impl From<psa_aead_decrypt::Operation> for NativeOperation {
    fn from(op: psa_aead_decrypt::Operation) -> Self {
        NativeOperation::PsaAeadDecrypt(op)
    }
}

impl From<psa_generate_random::Operation> for NativeOperation {
    fn from(op: psa_generate_random::Operation) -> Self {
        NativeOperation::PsaGenerateRandom(op)
    }
}

impl From<psa_hash_compute::Operation> for NativeOperation {
    fn from(op: psa_hash_compute::Operation) -> Self {
        NativeOperation::PsaHashCompute(op)
    }
}

impl From<psa_hash_compare::Operation> for NativeOperation {
    fn from(op: psa_hash_compare::Operation) -> Self {
        NativeOperation::PsaHashCompare(op)
    }
}
impl From<psa_raw_key_agreement::Operation> for NativeOperation {
    fn from(op: psa_raw_key_agreement::Operation) -> Self {
        NativeOperation::PsaRawKeyAgreement(op)
    }
}

impl From<list_providers::Result> for NativeResult {
    fn from(op: list_providers::Result) -> Self {
        NativeResult::ListProviders(op)
    }
}

impl From<list_opcodes::Result> for NativeResult {
    fn from(op: list_opcodes::Result) -> Self {
        NativeResult::ListOpcodes(op)
    }
}

impl From<list_authenticators::Result> for NativeResult {
    fn from(op: list_authenticators::Result) -> Self {
        NativeResult::ListAuthenticators(op)
    }
}

impl From<list_keys::Result> for NativeResult {
    fn from(op: list_keys::Result) -> Self {
        NativeResult::ListKeys(op)
    }
}

impl From<list_clients::Result> for NativeResult {
    fn from(op: list_clients::Result) -> Self {
        NativeResult::ListClients(op)
    }
}

impl From<delete_client::Result> for NativeResult {
    fn from(op: delete_client::Result) -> Self {
        NativeResult::DeleteClient(op)
    }
}

impl From<ping::Result> for NativeResult {
    fn from(op: ping::Result) -> Self {
        NativeResult::Ping(op)
    }
}

impl From<psa_generate_key::Result> for NativeResult {
    fn from(op: psa_generate_key::Result) -> Self {
        NativeResult::PsaGenerateKey(op)
    }
}

impl From<psa_import_key::Result> for NativeResult {
    fn from(op: psa_import_key::Result) -> Self {
        NativeResult::PsaImportKey(op)
    }
}

impl From<psa_export_public_key::Result> for NativeResult {
    fn from(op: psa_export_public_key::Result) -> Self {
        NativeResult::PsaExportPublicKey(op)
    }
}

impl From<psa_export_key::Result> for NativeResult {
    fn from(op: psa_export_key::Result) -> Self {
        NativeResult::PsaExportKey(op)
    }
}

impl From<psa_destroy_key::Result> for NativeResult {
    fn from(op: psa_destroy_key::Result) -> Self {
        NativeResult::PsaDestroyKey(op)
    }
}

impl From<psa_sign_hash::Result> for NativeResult {
    fn from(op: psa_sign_hash::Result) -> Self {
        NativeResult::PsaSignHash(op)
    }
}

impl From<psa_verify_hash::Result> for NativeResult {
    fn from(op: psa_verify_hash::Result) -> Self {
        NativeResult::PsaVerifyHash(op)
    }
}

impl From<psa_hash_compute::Result> for NativeResult {
    fn from(op: psa_hash_compute::Result) -> Self {
        NativeResult::PsaHashCompute(op)
    }
}

impl From<psa_hash_compare::Result> for NativeResult {
    fn from(op: psa_hash_compare::Result) -> Self {
        NativeResult::PsaHashCompare(op)
    }
}

impl From<psa_asymmetric_encrypt::Result> for NativeResult {
    fn from(op: psa_asymmetric_encrypt::Result) -> Self {
        NativeResult::PsaAsymmetricEncrypt(op)
    }
}

impl From<psa_asymmetric_decrypt::Result> for NativeResult {
    fn from(op: psa_asymmetric_decrypt::Result) -> Self {
        NativeResult::PsaAsymmetricDecrypt(op)
    }
}

impl From<psa_aead_encrypt::Result> for NativeResult {
    fn from(op: psa_aead_encrypt::Result) -> Self {
        NativeResult::PsaAeadEncrypt(op)
    }
}

impl From<psa_aead_decrypt::Result> for NativeResult {
    fn from(op: psa_aead_decrypt::Result) -> Self {
        NativeResult::PsaAeadDecrypt(op)
    }
}

impl From<psa_generate_random::Result> for NativeResult {
    fn from(op: psa_generate_random::Result) -> Self {
        NativeResult::PsaGenerateRandom(op)
    }
}

impl From<psa_raw_key_agreement::Result> for NativeResult {
    fn from(op: psa_raw_key_agreement::Result) -> Self {
        NativeResult::PsaRawKeyAgreement(op)
    }
}