bitwarden-crypto 3.0.0

Internal crate for the bitwarden crate. Do not use.
Documentation
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
use std::{borrow::Cow, str::FromStr};

use bitwarden_encoding::{B64, FromStrVisitor};
use coset::{CborSerializable, iana::KeyOperation};
use serde::Deserialize;
use tracing::instrument;
#[cfg(feature = "wasm")]
use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi};

use super::{check_length, from_b64, from_b64_vec, split_enc_string};
use crate::{
    Aes256CbcHmacKey, ContentFormat, CoseEncrypt0Bytes, KeyDecryptable, KeyEncryptable,
    KeyEncryptableWithContentType, SymmetricCryptoKey, Utf8Bytes, XChaCha20Poly1305Key,
    cose::XCHACHA20_POLY1305,
    error::{CryptoError, EncStringParseError, Result, UnsupportedOperationError},
    keys::KeyId,
};

#[cfg(feature = "wasm")]
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
const TS_CUSTOM_TYPES: &'static str = r#"
export type EncString = Tagged<string, "EncString">;
"#;

/// # Encrypted string primitive
///
/// [EncString] is a Bitwarden specific primitive that represents a symmetrically encrypted piece of
/// data, encoded as a string. They are are used together with the [KeyDecryptable] and
/// [KeyEncryptable] traits to encrypt and decrypt data using [SymmetricCryptoKey]s.
///
/// The flexibility of the [EncString] type allows for different encryption algorithms to be used
/// which is represented by the different variants of the enum.
///
/// ## Note
///
/// For backwards compatibility we will rarely if ever be able to remove support for decrypting old
/// variants, but we should be opinionated in which variants are used for encrypting.
///
/// ## Variants
/// - [Aes256Cbc_B64](EncString::Aes256Cbc_B64) - Deprecated and MUST NOT be used for encrypting as
///   it is not authenticated
/// - [Aes256Cbc_HmacSha256_B64](EncString::Aes256Cbc_HmacSha256_B64)
/// - [Cose_Encrypt0_B64](EncString::Cose_Encrypt0_B64) - The preferred variant for encrypting data.
///
/// ## Serialization
///
/// [EncString] implements [ToString] and [FromStr] to allow for easy serialization and uses a
/// custom scheme to represent the different variants.
///
/// The scheme is one of the following schemes:
/// - `[type].[iv]|[data]`
/// - `[type].[iv]|[data]|[mac]`
/// - `[type].[cose_encrypt0_bytes]`
///
/// Where:
/// - `[type]`: is a digit number representing the variant.
/// - `[iv]`: (optional) is the initialization vector used for encryption.
/// - `[data]`: is the encrypted data.
/// - `[mac]`: (optional) is the MAC used to validate the integrity of the data.
/// - `[cose_encrypt0_bytes]`: is the COSE Encrypt0 message, serialized to bytes
#[allow(missing_docs)]
#[derive(Clone, zeroize::ZeroizeOnDrop, PartialEq)]
#[allow(unused, non_camel_case_types)]
pub enum EncString {
    /// 0
    Aes256Cbc_B64 {
        iv: [u8; 16],
        data: Vec<u8>,
    },
    /// 1 was the now removed `AesCbc128_HmacSha256_B64`.
    /// 2
    Aes256Cbc_HmacSha256_B64 {
        iv: [u8; 16],
        mac: [u8; 32],
        data: Vec<u8>,
    },
    // 7 The actual enc type is contained in the cose struct
    Cose_Encrypt0_B64 {
        data: Vec<u8>,
    },
}

#[cfg(feature = "wasm")]
impl wasm_bindgen::describe::WasmDescribe for EncString {
    fn describe() {
        <String as wasm_bindgen::describe::WasmDescribe>::describe();
    }
}

#[cfg(feature = "wasm")]
impl FromWasmAbi for EncString {
    type Abi = <String as FromWasmAbi>::Abi;

    unsafe fn from_abi(abi: Self::Abi) -> Self {
        use wasm_bindgen::UnwrapThrowExt;

        let s = unsafe { String::from_abi(abi) };
        Self::from_str(&s).unwrap_throw()
    }
}

#[cfg(feature = "wasm")]
impl OptionFromWasmAbi for EncString {
    fn is_none(abi: &Self::Abi) -> bool {
        <String as OptionFromWasmAbi>::is_none(abi)
    }
}

#[cfg(feature = "wasm")]
impl IntoWasmAbi for EncString {
    type Abi = <String as IntoWasmAbi>::Abi;

    fn into_abi(self) -> Self::Abi {
        self.to_string().into_abi()
    }
}

/// Deserializes an [EncString] from a string.
impl FromStr for EncString {
    type Err = CryptoError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (enc_type, parts) = split_enc_string(s);
        match (enc_type, parts.len()) {
            ("0", 2) => {
                let iv = from_b64(parts[0])?;
                let data = from_b64_vec(parts[1])?;

                Ok(EncString::Aes256Cbc_B64 { iv, data })
            }
            ("2", 3) => {
                let iv = from_b64(parts[0])?;
                let data = from_b64_vec(parts[1])?;
                let mac = from_b64(parts[2])?;

                Ok(EncString::Aes256Cbc_HmacSha256_B64 { iv, mac, data })
            }
            ("7", 1) => {
                let buffer = from_b64_vec(parts[0])?;

                Ok(EncString::Cose_Encrypt0_B64 { data: buffer })
            }
            (enc_type, parts) => Err(EncStringParseError::InvalidTypeSymm {
                enc_type: enc_type.to_string(),
                parts,
            }
            .into()),
        }
    }
}

impl EncString {
    /// Synthetic sugar for mapping `Option<String>` to `Result<Option<EncString>>`
    pub fn try_from_optional(s: Option<String>) -> Result<Option<EncString>, CryptoError> {
        s.map(|s| s.parse()).transpose()
    }

    #[allow(missing_docs)]
    pub fn from_buffer(buf: &[u8]) -> Result<Self> {
        if buf.is_empty() {
            return Err(EncStringParseError::NoType.into());
        }
        let enc_type = buf[0];

        match enc_type {
            0 => {
                check_length(buf, 18)?;
                let iv = buf[1..17].try_into().expect("Valid length");
                let data = buf[17..].to_vec();

                Ok(EncString::Aes256Cbc_B64 { iv, data })
            }
            2 => {
                check_length(buf, 50)?;
                let iv = buf[1..17].try_into().expect("Valid length");
                let mac = buf[17..49].try_into().expect("Valid length");
                let data = buf[49..].to_vec();

                Ok(EncString::Aes256Cbc_HmacSha256_B64 { iv, mac, data })
            }
            7 => Ok(EncString::Cose_Encrypt0_B64 {
                data: buf[1..].to_vec(),
            }),
            _ => Err(EncStringParseError::InvalidTypeSymm {
                enc_type: enc_type.to_string(),
                parts: 1,
            }
            .into()),
        }
    }

    #[allow(missing_docs)]
    pub fn to_buffer(&self) -> Result<Vec<u8>> {
        let mut buf;

        match self {
            EncString::Aes256Cbc_B64 { iv, data } => {
                buf = Vec::with_capacity(1 + 16 + data.len());
                buf.push(self.enc_type());
                buf.extend_from_slice(iv);
                buf.extend_from_slice(data);
            }
            EncString::Aes256Cbc_HmacSha256_B64 { iv, mac, data } => {
                buf = Vec::with_capacity(1 + 16 + 32 + data.len());
                buf.push(self.enc_type());
                buf.extend_from_slice(iv);
                buf.extend_from_slice(mac);
                buf.extend_from_slice(data);
            }
            EncString::Cose_Encrypt0_B64 { data } => {
                buf = Vec::with_capacity(1 + data.len());
                buf.push(self.enc_type());
                buf.extend_from_slice(data);
            }
        }

        Ok(buf)
    }
}

// `Display` is not implemented here because printing for debug purposes should be different
// from serializing to a string. For Aes256_Cbc, or Aes256_Cbc_Hmac, `ToString` and `Debug`
// are the same. For `Cose_Encrypt0`, `Debug` will print the decoded COSE message, while
// `ToString` will print the Cose_Encrypt0 bytes, encoded in base64.
#[allow(clippy::to_string_trait_impl)]
impl ToString for EncString {
    fn to_string(&self) -> String {
        fn fmt_parts(enc_type: u8, parts: &[&[u8]]) -> String {
            let encoded_parts: Vec<String> = parts
                .iter()
                .map(|part| B64::from(*part).to_string())
                .collect();
            format!("{}.{}", enc_type, encoded_parts.join("|"))
        }

        let enc_type = self.enc_type();
        match &self {
            EncString::Aes256Cbc_B64 { iv, data } => fmt_parts(enc_type, &[iv, data]),
            EncString::Aes256Cbc_HmacSha256_B64 { iv, mac, data } => {
                fmt_parts(enc_type, &[iv, data, mac])
            }
            EncString::Cose_Encrypt0_B64 { data } => fmt_parts(enc_type, &[data]),
        }
    }
}

impl std::fmt::Debug for EncString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            EncString::Aes256Cbc_B64 { iv, data } => {
                let mut debug_struct = f.debug_struct("EncString::Aes256Cbc");
                #[cfg(feature = "dangerous-crypto-debug")]
                {
                    debug_struct.field("iv", &hex::encode(iv));
                    debug_struct.field("data", &hex::encode(data));
                }
                #[cfg(not(feature = "dangerous-crypto-debug"))]
                {
                    _ = iv;
                    _ = data;
                }
                debug_struct.finish()
            }
            EncString::Aes256Cbc_HmacSha256_B64 { iv, mac, data } => {
                let mut debug_struct = f.debug_struct("EncString::Aes256CbcHmacSha256");
                #[cfg(feature = "dangerous-crypto-debug")]
                {
                    debug_struct.field("iv", &hex::encode(iv));
                    debug_struct.field("data", &hex::encode(data));
                    debug_struct.field("mac", &hex::encode(mac));
                }
                #[cfg(not(feature = "dangerous-crypto-debug"))]
                {
                    _ = iv;
                    _ = data;
                    _ = mac;
                }
                debug_struct.finish()
            }
            EncString::Cose_Encrypt0_B64 { data } => {
                let mut debug_struct = f.debug_struct("EncString::CoseEncrypt0");

                match coset::CoseEncrypt0::from_slice(data.as_slice()) {
                    Ok(msg) => {
                        if let Some(ref alg) = msg.protected.header.alg {
                            let alg_name = match alg {
                                coset::Algorithm::PrivateUse(XCHACHA20_POLY1305) => {
                                    "XChaCha20-Poly1305"
                                }
                                other => return debug_struct.field("algorithm", other).finish(),
                            };
                            debug_struct.field("algorithm", &alg_name);
                        }

                        let key_id = &msg.protected.header.key_id;
                        if let Ok(key_id) = KeyId::try_from(key_id.as_slice()) {
                            debug_struct.field("key_id", &key_id);
                        }
                        debug_struct.field("nonce", &hex::encode(msg.unprotected.iv.as_slice()));
                        if let Some(ref content_type) = msg.protected.header.content_type {
                            debug_struct.field("content_type", content_type);
                        }

                        #[cfg(feature = "dangerous-crypto-debug")]
                        if let Some(ref ciphertext) = msg.ciphertext {
                            debug_struct.field("ciphertext", &hex::encode(ciphertext));
                        }
                    }
                    Err(_) => {
                        debug_struct.field("error", &"INVALID_COSE");
                    }
                }

                debug_struct.finish()
            }
        }
    }
}

impl<'de> Deserialize<'de> for EncString {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_str(FromStrVisitor::new())
    }
}

impl serde::Serialize for EncString {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

impl EncString {
    pub(crate) fn encrypt_aes256_hmac(
        data_dec: &[u8],
        key: &Aes256CbcHmacKey,
    ) -> Result<EncString> {
        let (iv, mac, data) =
            crate::aes::encrypt_aes256_hmac(data_dec, &key.mac_key, &key.enc_key)?;
        Ok(EncString::Aes256Cbc_HmacSha256_B64 { iv, mac, data })
    }

    pub(crate) fn encrypt_xchacha20_poly1305(
        data_dec: &[u8],
        key: &XChaCha20Poly1305Key,
        content_format: ContentFormat,
    ) -> Result<EncString> {
        let data = crate::cose::encrypt_xchacha20_poly1305(data_dec, key, content_format)?;
        Ok(EncString::Cose_Encrypt0_B64 {
            data: data.to_vec(),
        })
    }

    /// The numerical representation of the encryption type of the [EncString].
    const fn enc_type(&self) -> u8 {
        match self {
            EncString::Aes256Cbc_B64 { .. } => 0,
            EncString::Aes256Cbc_HmacSha256_B64 { .. } => 2,
            EncString::Cose_Encrypt0_B64 { .. } => 7,
        }
    }
}

impl KeyEncryptableWithContentType<SymmetricCryptoKey, EncString> for &[u8] {
    fn encrypt_with_key(
        self,
        key: &SymmetricCryptoKey,
        content_format: ContentFormat,
    ) -> Result<EncString> {
        match key {
            SymmetricCryptoKey::Aes256CbcHmacKey(key) => EncString::encrypt_aes256_hmac(self, key),
            SymmetricCryptoKey::XChaCha20Poly1305Key(inner_key) => {
                if !inner_key
                    .supported_operations
                    .contains(&KeyOperation::Encrypt)
                {
                    return Err(CryptoError::KeyOperationNotSupported(KeyOperation::Encrypt));
                }
                EncString::encrypt_xchacha20_poly1305(self, inner_key, content_format)
            }
            SymmetricCryptoKey::Aes256CbcKey(_) => Err(CryptoError::OperationNotSupported(
                UnsupportedOperationError::EncryptionNotImplementedForKey,
            )),
        }
    }
}

impl KeyDecryptable<SymmetricCryptoKey, Vec<u8>> for EncString {
    fn decrypt_with_key(&self, key: &SymmetricCryptoKey) -> Result<Vec<u8>> {
        match (self, key) {
            (EncString::Aes256Cbc_B64 { iv, data }, SymmetricCryptoKey::Aes256CbcKey(key)) => {
                crate::aes::decrypt_aes256(iv, data.clone(), &key.enc_key)
                    .map_err(|_| CryptoError::Decrypt)
            }
            (
                EncString::Aes256Cbc_HmacSha256_B64 { iv, mac, data },
                SymmetricCryptoKey::Aes256CbcHmacKey(key),
            ) => crate::aes::decrypt_aes256_hmac(iv, mac, data.clone(), &key.mac_key, &key.enc_key)
                .map_err(|_| CryptoError::Decrypt),
            (
                EncString::Cose_Encrypt0_B64 { data },
                SymmetricCryptoKey::XChaCha20Poly1305Key(key),
            ) => {
                let (decrypted_message, _) = crate::cose::decrypt_xchacha20_poly1305(
                    &CoseEncrypt0Bytes::from(data.as_slice()),
                    key,
                )?;
                Ok(decrypted_message)
            }
            _ => Err(CryptoError::WrongKeyType),
        }
    }
}

impl KeyEncryptable<SymmetricCryptoKey, EncString> for String {
    fn encrypt_with_key(self, key: &SymmetricCryptoKey) -> Result<EncString> {
        Utf8Bytes::from(self).encrypt_with_key(key)
    }
}

impl KeyEncryptable<SymmetricCryptoKey, EncString> for &str {
    fn encrypt_with_key(self, key: &SymmetricCryptoKey) -> Result<EncString> {
        Utf8Bytes::from(self).encrypt_with_key(key)
    }
}

impl KeyDecryptable<SymmetricCryptoKey, String> for EncString {
    #[instrument(err, skip_all)]
    fn decrypt_with_key(&self, key: &SymmetricCryptoKey) -> Result<String> {
        let dec: Vec<u8> = self.decrypt_with_key(key)?;
        String::from_utf8(dec).map_err(|_| CryptoError::InvalidUtf8String)
    }
}

/// Usually we wouldn't want to expose EncStrings in the API or the schemas.
/// But during the transition phase we will expose endpoints using the EncString type.
impl schemars::JsonSchema for EncString {
    fn schema_name() -> Cow<'static, str> {
        "EncString".into()
    }

    fn json_schema(generator: &mut schemars::generate::SchemaGenerator) -> schemars::Schema {
        generator.subschema_for::<String>()
    }
}

#[cfg(test)]
mod tests {
    use coset::iana::KeyOperation;
    use schemars::schema_for;

    use super::EncString;
    use crate::{
        CryptoError, KEY_ID_SIZE, KeyDecryptable, KeyEncryptable, SymmetricCryptoKey,
        derive_symmetric_key,
    };

    fn encrypt_with_xchacha20(plaintext: &str) -> EncString {
        let key_id = [0u8; KEY_ID_SIZE];
        let enc_key = [0u8; 32];
        let key = SymmetricCryptoKey::XChaCha20Poly1305Key(crate::XChaCha20Poly1305Key {
            key_id: key_id.into(),
            enc_key: Box::pin(enc_key.into()),
            supported_operations: vec![
                coset::iana::KeyOperation::Decrypt,
                coset::iana::KeyOperation::Encrypt,
                coset::iana::KeyOperation::WrapKey,
                coset::iana::KeyOperation::UnwrapKey,
            ],
        });

        plaintext.encrypt_with_key(&key).expect("encryption works")
    }

    #[test]
    #[ignore = "Manual test to verify debug format"]
    fn test_debug() {
        let enc_string = encrypt_with_xchacha20("Test debug string");
        println!("{:?}", enc_string);
        let enc_string_aes =
            EncString::encrypt_aes256_hmac(b"Test debug string", &derive_symmetric_key("test"))
                .unwrap();
        println!("{:?}", enc_string_aes);
    }

    /// XChaCha20Poly1305 encstrings should be padded in blocks of 32 bytes. This ensures that the
    /// encstring length does not reveal more than the 32-byte range of lengths that the contained
    /// string falls into.
    #[test]
    fn test_xchacha20_encstring_string_padding_block_sizes() {
        let cases = [
            ("", 32),              // empty string, padded to 32
            (&"a".repeat(31), 32), // largest in first block
            (&"a".repeat(32), 64), // smallest in second block
            (&"a".repeat(63), 64), // largest in second block
            (&"a".repeat(64), 96), // smallest in third block
        ];

        let ciphertext_lengths: Vec<_> = cases
            .iter()
            .map(|(plaintext, _)| encrypt_with_xchacha20(plaintext).to_string().len())
            .collect();

        // Block 1: 0-31 (same length)
        assert_eq!(ciphertext_lengths[0], ciphertext_lengths[1]);
        // Block 2: 32-63 (same length, different from block 1)
        assert_ne!(ciphertext_lengths[1], ciphertext_lengths[2]);
        assert_eq!(ciphertext_lengths[2], ciphertext_lengths[3]);
        // Block 3: 64+ (different from block 2)
        assert_ne!(ciphertext_lengths[3], ciphertext_lengths[4]);
    }

    #[test]
    fn test_enc_roundtrip_xchacha20() {
        let key_id = [0u8; KEY_ID_SIZE];
        let enc_key = [0u8; 32];
        let key = SymmetricCryptoKey::XChaCha20Poly1305Key(crate::XChaCha20Poly1305Key {
            key_id: key_id.into(),
            enc_key: Box::pin(enc_key.into()),
            supported_operations: vec![
                coset::iana::KeyOperation::Decrypt,
                coset::iana::KeyOperation::Encrypt,
                coset::iana::KeyOperation::WrapKey,
                coset::iana::KeyOperation::UnwrapKey,
            ],
        });

        let test_string = "encrypted_test_string";
        let cipher = test_string.to_owned().encrypt_with_key(&key).unwrap();
        let decrypted_str: String = cipher.decrypt_with_key(&key).unwrap();
        assert_eq!(decrypted_str, test_string);
    }

    #[test]
    fn test_enc_string_roundtrip() {
        let key = SymmetricCryptoKey::Aes256CbcHmacKey(derive_symmetric_key("test"));

        let test_string = "encrypted_test_string";
        let cipher = test_string.to_string().encrypt_with_key(&key).unwrap();

        let decrypted_str: String = cipher.decrypt_with_key(&key).unwrap();
        assert_eq!(decrypted_str, test_string);
    }

    #[test]
    fn test_enc_roundtrip_xchacha20_empty() {
        let key_id = [0u8; KEY_ID_SIZE];
        let enc_key = [0u8; 32];
        let key = SymmetricCryptoKey::XChaCha20Poly1305Key(crate::XChaCha20Poly1305Key {
            key_id: key_id.into(),
            enc_key: Box::pin(enc_key.into()),
            supported_operations: vec![
                coset::iana::KeyOperation::Decrypt,
                coset::iana::KeyOperation::Encrypt,
                coset::iana::KeyOperation::WrapKey,
                coset::iana::KeyOperation::UnwrapKey,
            ],
        });

        let test_string = "";
        let cipher = test_string.to_owned().encrypt_with_key(&key).unwrap();
        let decrypted_str: String = cipher.decrypt_with_key(&key).unwrap();
        assert_eq!(decrypted_str, test_string);
    }

    #[test]
    fn test_enc_string_roundtrip_empty() {
        let key = SymmetricCryptoKey::Aes256CbcHmacKey(derive_symmetric_key("test"));

        let test_string = "";
        let cipher = test_string.to_string().encrypt_with_key(&key).unwrap();

        let decrypted_str: String = cipher.decrypt_with_key(&key).unwrap();
        assert_eq!(decrypted_str, test_string);
    }

    #[test]
    fn test_enc_string_ref_roundtrip() {
        let key = SymmetricCryptoKey::Aes256CbcHmacKey(derive_symmetric_key("test"));

        let test_string: &'static str = "encrypted_test_string";
        let cipher = test_string.to_string().encrypt_with_key(&key).unwrap();

        let decrypted_str: String = cipher.decrypt_with_key(&key).unwrap();
        assert_eq!(decrypted_str, test_string);
    }

    #[test]
    fn test_enc_string_serialization() {
        #[derive(serde::Serialize, serde::Deserialize)]
        struct Test {
            key: EncString,
        }

        let cipher = "2.pMS6/icTQABtulw52pq2lg==|XXbxKxDTh+mWiN1HjH2N1w==|Q6PkuT+KX/axrgN9ubD5Ajk2YNwxQkgs3WJM0S0wtG8=";
        let serialized = format!("{{\"key\":\"{cipher}\"}}");

        let t = serde_json::from_str::<Test>(&serialized).unwrap();
        assert_eq!(t.key.enc_type(), 2);
        assert_eq!(t.key.to_string(), cipher);
        assert_eq!(serde_json::to_string(&t).unwrap(), serialized);
    }

    #[test]
    fn test_enc_from_to_buffer() {
        let enc_str: &str = "2.pMS6/icTQABtulw52pq2lg==|XXbxKxDTh+mWiN1HjH2N1w==|Q6PkuT+KX/axrgN9ubD5Ajk2YNwxQkgs3WJM0S0wtG8=";
        let enc_string: EncString = enc_str.parse().unwrap();

        let enc_buf = enc_string.to_buffer().unwrap();

        assert_eq!(
            enc_buf,
            vec![
                2, 164, 196, 186, 254, 39, 19, 64, 0, 109, 186, 92, 57, 218, 154, 182, 150, 67,
                163, 228, 185, 63, 138, 95, 246, 177, 174, 3, 125, 185, 176, 249, 2, 57, 54, 96,
                220, 49, 66, 72, 44, 221, 98, 76, 209, 45, 48, 180, 111, 93, 118, 241, 43, 16, 211,
                135, 233, 150, 136, 221, 71, 140, 125, 141, 215
            ]
        );

        let enc_string_new = EncString::from_buffer(&enc_buf).unwrap();

        assert_eq!(enc_string_new.to_string(), enc_str)
    }

    #[test]
    fn test_from_str_cbc256() {
        let enc_str = "0.pMS6/icTQABtulw52pq2lg==|XXbxKxDTh+mWiN1HjH2N1w==";
        let enc_string: EncString = enc_str.parse().unwrap();

        assert_eq!(enc_string.enc_type(), 0);
        if let EncString::Aes256Cbc_B64 { iv, data } = &enc_string {
            assert_eq!(
                iv,
                &[
                    164, 196, 186, 254, 39, 19, 64, 0, 109, 186, 92, 57, 218, 154, 182, 150
                ]
            );
            assert_eq!(
                data,
                &[
                    93, 118, 241, 43, 16, 211, 135, 233, 150, 136, 221, 71, 140, 125, 141, 215
                ]
            );
        } else {
            panic!("Invalid variant")
        };
    }

    #[test]
    fn test_decrypt_cbc256() {
        let key = "hvBMMb1t79YssFZkpetYsM3deyVuQv4r88Uj9gvYe08=".to_string();
        let key = SymmetricCryptoKey::try_from(key).unwrap();

        let enc_str = "0.NQfjHLr6za7VQVAbrpL81w==|wfrjmyJ0bfwkQlySrhw8dA==";
        let enc_string: EncString = enc_str.parse().unwrap();
        assert_eq!(enc_string.enc_type(), 0);

        let dec_str: String = enc_string.decrypt_with_key(&key).unwrap();
        assert_eq!(dec_str, "EncryptMe!");
    }

    #[test]
    fn test_decrypt_downgrade_encstring_prevention() {
        // Simulate a potential downgrade attack by removing the mac portion of the `EncString` and
        // attempt to decrypt it using a `SymmetricCryptoKey` with a mac key.
        let key = "hvBMMb1t79YssFZkpetYsM3deyVuQv4r88Uj9gvYe0+G8EwxvW3v1iywVmSl61iwzd17JW5C/ivzxSP2C9h7Tw==".to_string();
        let key = SymmetricCryptoKey::try_from(key).unwrap();

        // A "downgraded" `EncString` from `EncString::Aes256Cbc_HmacSha256_B64` (2) to
        // `EncString::Aes256Cbc_B64` (0), with the mac portion removed.
        // <enc_string>
        let enc_str = "0.NQfjHLr6za7VQVAbrpL81w==|wfrjmyJ0bfwkQlySrhw8dA==";
        let enc_string: EncString = enc_str.parse().unwrap();
        assert_eq!(enc_string.enc_type(), 0);

        let result: Result<String, CryptoError> = enc_string.decrypt_with_key(&key);
        assert!(matches!(result, Err(CryptoError::WrongKeyType)));
    }

    #[test]
    fn test_encrypt_fails_when_operation_not_allowed() {
        // Key with only Decrypt allowed
        let key_id = [0u8; KEY_ID_SIZE];
        let enc_key = [0u8; 32];
        let key = SymmetricCryptoKey::XChaCha20Poly1305Key(crate::XChaCha20Poly1305Key {
            key_id: key_id.into(),
            enc_key: Box::pin(enc_key.into()),
            supported_operations: vec![KeyOperation::Decrypt],
        });

        let plaintext = "should fail";
        let result = plaintext.encrypt_with_key(&key);
        assert!(
            matches!(
                result,
                Err(CryptoError::KeyOperationNotSupported(KeyOperation::Encrypt))
            ),
            "Expected encrypt to fail with KeyOperationNotSupported, got: {result:?}"
        );
    }

    #[test]
    fn test_from_str_invalid() {
        let enc_str = "8.ABC";
        let enc_string: Result<EncString, _> = enc_str.parse();

        let err = enc_string.unwrap_err();
        assert_eq!(
            err.to_string(),
            "EncString error, Invalid symmetric type, got type 8 with 1 parts"
        );
    }

    #[test]
    #[cfg(not(feature = "dangerous-crypto-debug"))]
    fn test_debug_format() {
        let enc_str = "2.pMS6/icTQABtulw52pq2lg==|XXbxKxDTh+mWiN1HjH2N1w==|Q6PkuT+KX/axrgN9ubD5Ajk2YNwxQkgs3WJM0S0wtG8=";
        let enc_string: EncString = enc_str.parse().unwrap();
        assert_eq!(
            "EncString::Aes256CbcHmacSha256".to_string(),
            format!("{:?}", enc_string)
        );
    }

    #[test]
    fn test_json_schema() {
        let schema = schema_for!(EncString);

        assert_eq!(
            serde_json::to_string(&schema).unwrap(),
            r#"{"$schema":"https://json-schema.org/draft/2020-12/schema","title":"EncString","type":"string"}"#
        );
    }
}