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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// Copyright (c) 2020, Arm Limited, All Rights Reserved
// SPDX-License-Identifier: Apache-2.0
// Protobuf imports
use super::generated_ops::psa_algorithm::algorithm;
use super::generated_ops::psa_algorithm::algorithm::aead;
use super::generated_ops::psa_algorithm::algorithm::aead::AeadWithDefaultLengthTag as AeadWithDefaultLengthTagProto;
use super::generated_ops::psa_algorithm::algorithm::asymmetric_encryption;
use super::generated_ops::psa_algorithm::algorithm::asymmetric_signature;
use super::generated_ops::psa_algorithm::algorithm::key_agreement;
use super::generated_ops::psa_algorithm::algorithm::key_agreement::Raw as RawKeyAgreementProto;
use super::generated_ops::psa_algorithm::algorithm::key_derivation;
use super::generated_ops::psa_algorithm::algorithm::Aead as AeadProto;
use super::generated_ops::psa_algorithm::algorithm::AsymmetricEncryption as AsymmetricEncryptionProto;
use super::generated_ops::psa_algorithm::algorithm::AsymmetricSignature as AsymmetricSignatureProto;
use super::generated_ops::psa_algorithm::algorithm::Cipher as CipherProto;
use super::generated_ops::psa_algorithm::algorithm::Hash as HashProto;
use super::generated_ops::psa_algorithm::algorithm::KeyAgreement as KeyAgreementProto;
use super::generated_ops::psa_algorithm::algorithm::KeyDerivation as KeyDerivationProto;
use super::generated_ops::psa_algorithm::algorithm::Mac as MacProto;
use super::generated_ops::psa_algorithm::algorithm::None as NoneProto;
use super::generated_ops::psa_algorithm::algorithm::{mac, mac::FullLength as FullLengthMacProto};
use super::generated_ops::psa_algorithm::Algorithm as AlgorithmProto;

// Native imports
use crate::operations::psa_algorithm::{
    Aead, AeadWithDefaultLengthTag, Algorithm, AsymmetricEncryption, AsymmetricSignature, Cipher,
    FullLengthMac, Hash, KeyAgreement, KeyDerivation, Mac, RawKeyAgreement,
};

use crate::requests::{ResponseStatus, Result};
use log::error;
use std::convert::{TryFrom, TryInto};

// Hash algorithms: from protobuf to native
impl TryFrom<i32> for Hash {
    type Error = ResponseStatus;

    fn try_from(hash_val: i32) -> Result<Self> {
        let hash_val = HashProto::from_i32(hash_val).ok_or_else(|| {
            error!(
                "Value {} not recognised as a valid hash algorithm encoding.",
                hash_val
            );
            ResponseStatus::InvalidEncoding
        })?;
        match hash_val {
            HashProto::None => {
                error!("The None value of Hash enumeration is not allowed.");
                Err(ResponseStatus::InvalidEncoding)
            }
            #[allow(deprecated)]
            HashProto::Md2 => Ok(Hash::Md2),
            #[allow(deprecated)]
            HashProto::Md4 => Ok(Hash::Md4),
            #[allow(deprecated)]
            HashProto::Md5 => Ok(Hash::Md5),
            HashProto::Ripemd160 => Ok(Hash::Ripemd160),
            #[allow(deprecated)]
            HashProto::Sha1 => Ok(Hash::Sha1),
            HashProto::Sha224 => Ok(Hash::Sha224),
            HashProto::Sha256 => Ok(Hash::Sha256),
            HashProto::Sha384 => Ok(Hash::Sha384),
            HashProto::Sha512 => Ok(Hash::Sha512),
            HashProto::Sha512224 => Ok(Hash::Sha512_224),
            HashProto::Sha512256 => Ok(Hash::Sha512_256),
            HashProto::Sha3224 => Ok(Hash::Sha3_224),
            HashProto::Sha3256 => Ok(Hash::Sha3_256),
            HashProto::Sha3384 => Ok(Hash::Sha3_384),
            HashProto::Sha3512 => Ok(Hash::Sha3_512),
            HashProto::AnyHash => Ok(Hash::Any),
        }
    }
}

// Hash algorithms: from native to protobuf
fn hash_to_i32(hash: Hash) -> i32 {
    match hash {
        #[allow(deprecated)]
        Hash::Md2 => HashProto::Md2.into(),
        #[allow(deprecated)]
        Hash::Md4 => HashProto::Md4.into(),
        #[allow(deprecated)]
        Hash::Md5 => HashProto::Md5.into(),
        Hash::Ripemd160 => HashProto::Ripemd160.into(),
        #[allow(deprecated)]
        Hash::Sha1 => HashProto::Sha1.into(),
        Hash::Sha224 => HashProto::Sha224.into(),
        Hash::Sha256 => HashProto::Sha256.into(),
        Hash::Sha384 => HashProto::Sha384.into(),
        Hash::Sha512 => HashProto::Sha512.into(),
        Hash::Sha512_224 => HashProto::Sha512224.into(),
        Hash::Sha512_256 => HashProto::Sha512256.into(),
        Hash::Sha3_224 => HashProto::Sha3224.into(),
        Hash::Sha3_256 => HashProto::Sha3256.into(),
        Hash::Sha3_384 => HashProto::Sha3384.into(),
        Hash::Sha3_512 => HashProto::Sha3512.into(),
        Hash::Any => HashProto::AnyHash.into(),
    }
}

// FullLengthMac algorithms: from protobuf to native
impl TryFrom<FullLengthMacProto> for FullLengthMac {
    type Error = ResponseStatus;

    fn try_from(alg: FullLengthMacProto) -> Result<Self> {
        match alg.variant.ok_or_else(|| {
            error!("variant field of mac::FullLength message is empty.");
            ResponseStatus::InvalidEncoding
        })? {
            mac::full_length::Variant::Hmac(hmac) => Ok(FullLengthMac::Hmac {
                hash_alg: hmac.hash_alg.try_into()?,
            }),
            mac::full_length::Variant::CbcMac(_) => Ok(FullLengthMac::CbcMac),
            mac::full_length::Variant::Cmac(_) => Ok(FullLengthMac::Cmac),
        }
    }
}

// FullLengthMac algorithms: from native to protobuf
impl TryFrom<FullLengthMac> for FullLengthMacProto {
    type Error = ResponseStatus;

    fn try_from(alg: FullLengthMac) -> Result<Self> {
        match alg {
            FullLengthMac::Hmac { hash_alg } => Ok(FullLengthMacProto {
                variant: Some(mac::full_length::Variant::Hmac(mac::full_length::Hmac {
                    hash_alg: hash_to_i32(hash_alg),
                })),
            }),
            FullLengthMac::CbcMac => Ok(FullLengthMacProto {
                variant: Some(mac::full_length::Variant::CbcMac(
                    mac::full_length::CbcMac {},
                )),
            }),
            FullLengthMac::Cmac => Ok(FullLengthMacProto {
                variant: Some(mac::full_length::Variant::Cmac(mac::full_length::Cmac {})),
            }),
        }
    }
}

// Mac algorithms: from protobuf to native
impl TryFrom<MacProto> for Mac {
    type Error = ResponseStatus;

    fn try_from(alg: MacProto) -> Result<Self> {
        match alg.variant.ok_or_else(|| {
            error!("variant field of Mac message is empty.");
            ResponseStatus::InvalidEncoding
        })? {
            mac::Variant::FullLength(full_length) => Ok(Mac::FullLength(full_length.try_into()?)),
            mac::Variant::Truncated(truncated) => Ok(Mac::Truncated {
                mac_alg: truncated.mac_alg.ok_or_else(|| {
                    error!("mac_alg field of mac::Truncated message is empty.");
                    ResponseStatus::InvalidEncoding
                })?.try_into()?,
                mac_length: truncated.mac_length.try_into().or_else(|e| {
                    error!("mac_length field of mac::Truncated message can not be represented by an usize ({}).", e);
                    Err(ResponseStatus::InvalidEncoding)
                })?,
            }),
        }
    }
}

// Mac algorithms: from native to protobuf
impl TryFrom<Mac> for MacProto {
    type Error = ResponseStatus;

    fn try_from(alg: Mac) -> Result<Self> {
        match alg {
            Mac::FullLength(full_length_mac) => Ok(MacProto {
                variant: Some(mac::Variant::FullLength(full_length_mac.try_into()?)),
            }),
            Mac::Truncated {
                mac_alg,
                mac_length,
            } => Ok(MacProto {
                variant: Some(mac::Variant::Truncated(mac::Truncated {
                    mac_alg: Some(mac_alg.try_into()?),
                    mac_length: mac_length.try_into().or_else(|e| {
                        error!(
                            "mac_length field of Mac can not be represented by an u32 ({}).",
                            e
                        );
                        Err(ResponseStatus::InvalidEncoding)
                    })?,
                })),
            }),
        }
    }
}

// Cipher algorithms: from protobuf to native
impl TryFrom<i32> for Cipher {
    type Error = ResponseStatus;

    fn try_from(cipher_val: i32) -> Result<Self> {
        let cipher_val = CipherProto::from_i32(cipher_val).ok_or_else(|| {
            error!(
                "Value {} not recognised as a valid cipher algorithm encoding.",
                cipher_val
            );
            ResponseStatus::InvalidEncoding
        })?;
        match cipher_val {
            CipherProto::None => {
                error!("The None value of Cipher enumeration is not allowed.");
                Err(ResponseStatus::InvalidEncoding)
            }
            CipherProto::StreamCipher => Ok(Cipher::StreamCipher),
            CipherProto::Ctr => Ok(Cipher::Ctr),
            CipherProto::Cfb => Ok(Cipher::Cfb),
            CipherProto::Ofb => Ok(Cipher::Ofb),
            CipherProto::Xts => Ok(Cipher::Xts),
            CipherProto::EcbNoPadding => Ok(Cipher::EcbNoPadding),
            CipherProto::CbcNoPadding => Ok(Cipher::CbcNoPadding),
            CipherProto::CbcPkcs7 => Ok(Cipher::CbcPkcs7),
        }
    }
}

// Cipher algorithms: from native to protobuf
fn cipher_to_i32(cipher: Cipher) -> i32 {
    match cipher {
        Cipher::StreamCipher => CipherProto::StreamCipher.into(),
        Cipher::Ctr => CipherProto::Ctr.into(),
        Cipher::Cfb => CipherProto::Cfb.into(),
        Cipher::Ofb => CipherProto::Ofb.into(),
        Cipher::Xts => CipherProto::Xts.into(),
        Cipher::EcbNoPadding => CipherProto::EcbNoPadding.into(),
        Cipher::CbcNoPadding => CipherProto::CbcNoPadding.into(),
        Cipher::CbcPkcs7 => CipherProto::CbcPkcs7.into(),
    }
}

// AeadWithDefaultLengthTag algorithms: from protobuf to native
impl TryFrom<i32> for AeadWithDefaultLengthTag {
    type Error = ResponseStatus;

    fn try_from(aead_val: i32) -> Result<Self> {
        let aead_val = AeadWithDefaultLengthTagProto::from_i32(aead_val).ok_or_else(|| {
            error!("Value {} not recognised as a valid AEAD with default length tag algorithm encoding.", aead_val);
            ResponseStatus::InvalidEncoding
        })?;
        match aead_val {
            AeadWithDefaultLengthTagProto::None => {
                error!("The None value of AeadWithDefaultLengthTag enumeration is not allowed.");
                Err(ResponseStatus::InvalidEncoding)
            }
            AeadWithDefaultLengthTagProto::Ccm => Ok(AeadWithDefaultLengthTag::Ccm),
            AeadWithDefaultLengthTagProto::Gcm => Ok(AeadWithDefaultLengthTag::Gcm),
            AeadWithDefaultLengthTagProto::Chacha20Poly1305 => {
                Ok(AeadWithDefaultLengthTag::Chacha20Poly1305)
            }
        }
    }
}

// AeadWithDefaultLengthTag algorithms: from native to protobuf
fn aead_with_default_length_tag_to_i32(cipher: AeadWithDefaultLengthTag) -> i32 {
    match cipher {
        AeadWithDefaultLengthTag::Ccm => AeadWithDefaultLengthTagProto::Ccm.into(),
        AeadWithDefaultLengthTag::Gcm => AeadWithDefaultLengthTagProto::Gcm.into(),
        AeadWithDefaultLengthTag::Chacha20Poly1305 => {
            AeadWithDefaultLengthTagProto::Chacha20Poly1305.into()
        }
    }
}

// Aead algorithms: from protobuf to native
impl TryFrom<AeadProto> for Aead {
    type Error = ResponseStatus;

    fn try_from(alg: AeadProto) -> Result<Self> {
        match alg.variant.ok_or_else(|| {
            error!("variant field of Aead message is empty.");
            ResponseStatus::InvalidEncoding
        })? {
            aead::Variant::AeadWithDefaultLengthTag(aead_with_default_length_tag) => Ok(Aead::AeadWithDefaultLengthTag(aead_with_default_length_tag.try_into()?)),
            aead::Variant::AeadWithShortenedTag(aead_with_shortened_tag) => Ok(Aead::AeadWithShortenedTag {
                aead_alg: aead_with_shortened_tag.aead_alg.try_into()?,
                tag_length: aead_with_shortened_tag.tag_length.try_into().or_else(|e| {
                        error!("tag_length field of aead::AeadWithShortenedTag can not be represented by an usize ({}).", e);
                        Err(ResponseStatus::InvalidEncoding)
                })?,
            }),
        }
    }
}

// Aead algorithms: from native to protobuf
impl TryFrom<Aead> for AeadProto {
    type Error = ResponseStatus;

    fn try_from(alg: Aead) -> Result<Self> {
        match alg {
            Aead::AeadWithDefaultLengthTag(aead_with_default_length_tag) => Ok(AeadProto {
                variant: Some(aead::Variant::AeadWithDefaultLengthTag(aead_with_default_length_tag_to_i32(aead_with_default_length_tag))),
            }),
            Aead::AeadWithShortenedTag { aead_alg, tag_length } => Ok(AeadProto {
                variant: Some(aead::Variant::AeadWithShortenedTag(aead::AeadWithShortenedTag {
                    aead_alg: aead_with_default_length_tag_to_i32(aead_alg),
                    tag_length: tag_length.try_into().or_else(|e| {
                        error!("tag_length field of Aead::AeadWithShortenedTag can not be represented by an u32 ({}).", e);
                        Err(ResponseStatus::InvalidEncoding)
                    })?,
                })),
            }),
        }
    }
}

// AsymmetricSignature algorithms: from protobuf to native
impl TryFrom<AsymmetricSignatureProto> for AsymmetricSignature {
    type Error = ResponseStatus;

    fn try_from(alg: AsymmetricSignatureProto) -> Result<Self> {
        match alg.variant.ok_or_else(|| {
            error!("variant field of Asym message is empty.");
            ResponseStatus::InvalidEncoding
        })? {
            asymmetric_signature::Variant::RsaPkcs1v15Sign(rsa_pkcs1v15_sign) => {
                Ok(AsymmetricSignature::RsaPkcs1v15Sign {
                    hash_alg: rsa_pkcs1v15_sign.hash_alg.try_into()?,
                })
            }
            asymmetric_signature::Variant::RsaPkcs1v15SignRaw(_) => {
                Ok(AsymmetricSignature::RsaPkcs1v15SignRaw)
            }
            asymmetric_signature::Variant::RsaPss(rsa_pss) => Ok(AsymmetricSignature::RsaPss {
                hash_alg: rsa_pss.hash_alg.try_into()?,
            }),
            asymmetric_signature::Variant::Ecdsa(ecdsa) => Ok(AsymmetricSignature::Ecdsa {
                hash_alg: ecdsa.hash_alg.try_into()?,
            }),
            asymmetric_signature::Variant::EcdsaAny(_) => Ok(AsymmetricSignature::EcdsaAny),
            asymmetric_signature::Variant::DeterministicEcdsa(deterministic_ecdsa) => {
                Ok(AsymmetricSignature::DeterministicEcdsa {
                    hash_alg: deterministic_ecdsa.hash_alg.try_into()?,
                })
            }
        }
    }
}

// AsymmetricSignature algorithms: from native to protobuf
impl TryFrom<AsymmetricSignature> for AsymmetricSignatureProto {
    type Error = ResponseStatus;

    fn try_from(alg: AsymmetricSignature) -> Result<Self> {
        match alg {
            AsymmetricSignature::RsaPkcs1v15Sign { hash_alg } => Ok(AsymmetricSignatureProto {
                variant: Some(asymmetric_signature::Variant::RsaPkcs1v15Sign(
                    asymmetric_signature::RsaPkcs1v15Sign {
                        hash_alg: hash_to_i32(hash_alg),
                    },
                )),
            }),
            AsymmetricSignature::RsaPkcs1v15SignRaw => Ok(AsymmetricSignatureProto {
                variant: Some(asymmetric_signature::Variant::RsaPkcs1v15SignRaw(
                    asymmetric_signature::RsaPkcs1v15SignRaw {},
                )),
            }),
            AsymmetricSignature::RsaPss { hash_alg } => Ok(AsymmetricSignatureProto {
                variant: Some(asymmetric_signature::Variant::RsaPss(
                    asymmetric_signature::RsaPss {
                        hash_alg: hash_to_i32(hash_alg),
                    },
                )),
            }),
            AsymmetricSignature::Ecdsa { hash_alg } => Ok(AsymmetricSignatureProto {
                variant: Some(asymmetric_signature::Variant::Ecdsa(
                    asymmetric_signature::Ecdsa {
                        hash_alg: hash_to_i32(hash_alg),
                    },
                )),
            }),
            AsymmetricSignature::EcdsaAny => Ok(AsymmetricSignatureProto {
                variant: Some(asymmetric_signature::Variant::EcdsaAny(
                    asymmetric_signature::EcdsaAny {},
                )),
            }),
            AsymmetricSignature::DeterministicEcdsa { hash_alg } => Ok(AsymmetricSignatureProto {
                variant: Some(asymmetric_signature::Variant::DeterministicEcdsa(
                    asymmetric_signature::DeterministicEcdsa {
                        hash_alg: hash_to_i32(hash_alg),
                    },
                )),
            }),
        }
    }
}

// AsymmetricEncryption algorithms: from protobuf to native
impl TryFrom<AsymmetricEncryptionProto> for AsymmetricEncryption {
    type Error = ResponseStatus;

    fn try_from(alg: AsymmetricEncryptionProto) -> Result<Self> {
        match alg.variant.ok_or_else(|| {
            error!("variant field of AsymmetricSignature message is empty.");
            ResponseStatus::InvalidEncoding
        })? {
            asymmetric_encryption::Variant::RsaPkcs1v15Crypt(_) => {
                Ok(AsymmetricEncryption::RsaPkcs1v15Crypt)
            }
            asymmetric_encryption::Variant::RsaOaep(rsa_oaep) => {
                Ok(AsymmetricEncryption::RsaOaep {
                    hash_alg: rsa_oaep.hash_alg.try_into()?,
                })
            }
        }
    }
}

// AsymmetricEncryption algorithms: from native to protobuf
impl TryFrom<AsymmetricEncryption> for AsymmetricEncryptionProto {
    type Error = ResponseStatus;

    fn try_from(alg: AsymmetricEncryption) -> Result<Self> {
        match alg {
            AsymmetricEncryption::RsaPkcs1v15Crypt => Ok(AsymmetricEncryptionProto {
                variant: Some(asymmetric_encryption::Variant::RsaPkcs1v15Crypt(
                    asymmetric_encryption::RsaPkcs1v15Crypt {},
                )),
            }),
            AsymmetricEncryption::RsaOaep { hash_alg } => Ok(AsymmetricEncryptionProto {
                variant: Some(asymmetric_encryption::Variant::RsaOaep(
                    asymmetric_encryption::RsaOaep {
                        hash_alg: hash_to_i32(hash_alg),
                    },
                )),
            }),
        }
    }
}

// RawKeyAgreement algorithms: from protobuf to native
impl TryFrom<i32> for RawKeyAgreement {
    type Error = ResponseStatus;

    fn try_from(raw_key_agreement_val: i32) -> Result<Self> {
        let raw_key_agreement_val = RawKeyAgreementProto::from_i32(raw_key_agreement_val)
            .ok_or_else(|| {
                error!(
                    "Value {} not recognised as a valid raw key agreement algorithm encoding.",
                    raw_key_agreement_val
                );
                ResponseStatus::InvalidEncoding
            })?;
        match raw_key_agreement_val {
            RawKeyAgreementProto::None => {
                error!("The None value of RawKeyAgreement enumeration is not allowed.");
                Err(ResponseStatus::InvalidEncoding)
            }
            RawKeyAgreementProto::Ffdh => Ok(RawKeyAgreement::Ffdh),
            RawKeyAgreementProto::Ecdh => Ok(RawKeyAgreement::Ecdh),
        }
    }
}

// RawKeyAgreement algorithms: from native to protobuf
fn raw_key_agreement_to_i32(raw_key_agreement: RawKeyAgreement) -> i32 {
    match raw_key_agreement {
        RawKeyAgreement::Ffdh => RawKeyAgreementProto::Ffdh.into(),
        RawKeyAgreement::Ecdh => RawKeyAgreementProto::Ecdh.into(),
    }
}

// KeyAgreement algorithms: from protobuf to native
impl TryFrom<KeyAgreementProto> for KeyAgreement {
    type Error = ResponseStatus;

    fn try_from(alg: KeyAgreementProto) -> Result<Self> {
        match alg.variant.ok_or_else(|| {
            error!("variant field of KeyAgreement message is empty.");
            ResponseStatus::InvalidEncoding
        })? {
            key_agreement::Variant::Raw(raw) => Ok(KeyAgreement::Raw(raw.try_into()?)),
            key_agreement::Variant::WithKeyDerivation(with_key_derivation) => Ok(KeyAgreement::WithKeyDerivation {
                ka_alg: with_key_derivation.ka_alg.try_into()?,
                kdf_alg: with_key_derivation.kdf_alg.ok_or_else(|| {
                    error!("kdf_alg field of key_agreement::WithKeyDerivation message is empty.");
                    ResponseStatus::InvalidEncoding
                })?.try_into()?,
            }),
        }
    }
}

// KeyAgreement algorithms: from native to protobuf
impl TryFrom<KeyAgreement> for KeyAgreementProto {
    type Error = ResponseStatus;

    fn try_from(alg: KeyAgreement) -> Result<Self> {
        match alg {
            KeyAgreement::Raw(raw_key_agreement) => Ok(KeyAgreementProto {
                variant: Some(key_agreement::Variant::Raw(raw_key_agreement_to_i32(
                    raw_key_agreement,
                ))),
            }),
            KeyAgreement::WithKeyDerivation { ka_alg, kdf_alg } => Ok(KeyAgreementProto {
                variant: Some(key_agreement::Variant::WithKeyDerivation(
                    key_agreement::WithKeyDerivation {
                        ka_alg: raw_key_agreement_to_i32(ka_alg),
                        kdf_alg: Some(kdf_alg.try_into()?),
                    },
                )),
            }),
        }
    }
}

// KeyDerivation algorithms: from protobuf to native
impl TryFrom<KeyDerivationProto> for KeyDerivation {
    type Error = ResponseStatus;

    fn try_from(alg: KeyDerivationProto) -> Result<Self> {
        match alg.variant.ok_or_else(|| {
            error!("variant field of KeyDerivation message is empty.");
            ResponseStatus::InvalidEncoding
        })? {
            key_derivation::Variant::Hkdf(hkdf) => Ok(KeyDerivation::Hkdf {
                hash_alg: hkdf.hash_alg.try_into()?,
            }),
            key_derivation::Variant::Tls12Prf(tls12_prf) => Ok(KeyDerivation::Tls12Prf {
                hash_alg: tls12_prf.hash_alg.try_into()?,
            }),
            key_derivation::Variant::Tls12PskToMs(tls12_psk_to_ms) => {
                Ok(KeyDerivation::Tls12PskToMs {
                    hash_alg: tls12_psk_to_ms.hash_alg.try_into()?,
                })
            }
        }
    }
}

// KeyDerivation algorithms: from native to protobuf
impl TryFrom<KeyDerivation> for KeyDerivationProto {
    type Error = ResponseStatus;

    fn try_from(alg: KeyDerivation) -> Result<Self> {
        match alg {
            KeyDerivation::Hkdf { hash_alg } => Ok(KeyDerivationProto {
                variant: Some(key_derivation::Variant::Hkdf(key_derivation::Hkdf {
                    hash_alg: hash_to_i32(hash_alg),
                })),
            }),
            KeyDerivation::Tls12Prf { hash_alg } => Ok(KeyDerivationProto {
                variant: Some(key_derivation::Variant::Tls12Prf(
                    key_derivation::Tls12Prf {
                        hash_alg: hash_to_i32(hash_alg),
                    },
                )),
            }),
            KeyDerivation::Tls12PskToMs { hash_alg } => Ok(KeyDerivationProto {
                variant: Some(key_derivation::Variant::Tls12PskToMs(
                    key_derivation::Tls12PskToMs {
                        hash_alg: hash_to_i32(hash_alg),
                    },
                )),
            }),
        }
    }
}

// Algorithm: from protobug to native
impl TryFrom<AlgorithmProto> for Algorithm {
    type Error = ResponseStatus;

    fn try_from(alg: AlgorithmProto) -> Result<Self> {
        match alg.variant.ok_or_else(|| {
            error!("variant field of Algorithm message is empty.");
            ResponseStatus::InvalidEncoding
        })? {
            algorithm::Variant::None(_) => Ok(Algorithm::None),
            algorithm::Variant::Hash(hash) => Ok(Algorithm::Hash(hash.try_into()?)),
            algorithm::Variant::Mac(mac) => Ok(Algorithm::Mac(mac.try_into()?)),
            algorithm::Variant::Cipher(cipher) => Ok(Algorithm::Cipher(cipher.try_into()?)),
            algorithm::Variant::Aead(aead) => Ok(Algorithm::Aead(aead.try_into()?)),
            algorithm::Variant::AsymmetricSignature(asymmetric_signature) => Ok(
                Algorithm::AsymmetricSignature(asymmetric_signature.try_into()?),
            ),
            algorithm::Variant::AsymmetricEncryption(asymmetric_encryption) => Ok(
                Algorithm::AsymmetricEncryption(asymmetric_encryption.try_into()?),
            ),
            algorithm::Variant::KeyAgreement(key_agreement) => {
                Ok(Algorithm::KeyAgreement(key_agreement.try_into()?))
            }
            algorithm::Variant::KeyDerivation(key_derivation) => {
                Ok(Algorithm::KeyDerivation(key_derivation.try_into()?))
            }
        }
    }
}

// Algorithm: from native to protobuf
impl TryFrom<Algorithm> for AlgorithmProto {
    type Error = ResponseStatus;

    fn try_from(alg: Algorithm) -> Result<Self> {
        match alg {
            Algorithm::None => Ok(AlgorithmProto {
                variant: Some(algorithm::Variant::None(NoneProto {})),
            }),
            Algorithm::Hash(hash) => Ok(AlgorithmProto {
                variant: Some(algorithm::Variant::Hash(hash_to_i32(hash))),
            }),
            Algorithm::Mac(mac) => Ok(AlgorithmProto {
                variant: Some(algorithm::Variant::Mac(mac.try_into()?)),
            }),
            Algorithm::Cipher(cipher) => Ok(AlgorithmProto {
                variant: Some(algorithm::Variant::Cipher(cipher_to_i32(cipher))),
            }),
            Algorithm::Aead(aead) => Ok(AlgorithmProto {
                variant: Some(algorithm::Variant::Aead(aead.try_into()?)),
            }),
            Algorithm::AsymmetricSignature(asymmetric_signature) => Ok(AlgorithmProto {
                variant: Some(algorithm::Variant::AsymmetricSignature(
                    asymmetric_signature.try_into()?,
                )),
            }),
            Algorithm::AsymmetricEncryption(asymmetric_encryption) => Ok(AlgorithmProto {
                variant: Some(algorithm::Variant::AsymmetricEncryption(
                    asymmetric_encryption.try_into()?,
                )),
            }),
            Algorithm::KeyAgreement(key_agreement) => Ok(AlgorithmProto {
                variant: Some(algorithm::Variant::KeyAgreement(key_agreement.try_into()?)),
            }),
            Algorithm::KeyDerivation(key_derivation) => Ok(AlgorithmProto {
                variant: Some(algorithm::Variant::KeyDerivation(
                    key_derivation.try_into()?,
                )),
            }),
        }
    }
}

#[cfg(test)]
mod test {
    #![allow(deprecated)]
    use super::super::generated_ops::psa_algorithm::{
        self as algorithm_proto, Algorithm as AlgorithmProto,
    };
    use crate::operations::psa_algorithm::{Algorithm, AsymmetricSignature, Hash};
    use std::convert::TryInto;

    #[test]
    fn sign_algo_from_proto() {
        let proto_sign = algorithm_proto::Algorithm {
            variant: Some(algorithm_proto::algorithm::Variant::AsymmetricSignature(
                algorithm_proto::algorithm::AsymmetricSignature {
                    variant: Some(
                        algorithm_proto::algorithm::asymmetric_signature::Variant::RsaPkcs1v15Sign(
                            algorithm_proto::algorithm::asymmetric_signature::RsaPkcs1v15Sign {
                                hash_alg: algorithm_proto::algorithm::Hash::Sha1.into(),
                            },
                        ),
                    ),
                },
            )),
        };

        let sign: Algorithm = proto_sign.try_into().unwrap();
        let sign_expected = Algorithm::AsymmetricSignature(AsymmetricSignature::RsaPkcs1v15Sign {
            hash_alg: Hash::Sha1,
        });

        assert_eq!(sign, sign_expected);
    }

    #[test]
    fn sign_algo_to_proto() {
        let sign = Algorithm::AsymmetricSignature(AsymmetricSignature::RsaPkcs1v15Sign {
            hash_alg: Hash::Sha1,
        });

        let proto_sign: AlgorithmProto = sign.try_into().unwrap();
        let proto_sign_expected = algorithm_proto::Algorithm {
            variant: Some(algorithm_proto::algorithm::Variant::AsymmetricSignature(
                algorithm_proto::algorithm::AsymmetricSignature {
                    variant: Some(
                        algorithm_proto::algorithm::asymmetric_signature::Variant::RsaPkcs1v15Sign(
                            algorithm_proto::algorithm::asymmetric_signature::RsaPkcs1v15Sign {
                                hash_alg: algorithm_proto::algorithm::Hash::Sha1.into(),
                            },
                        ),
                    ),
                },
            )),
        };

        assert_eq!(proto_sign, proto_sign_expected);
    }
}