bc-components 0.31.1

Secure Components for Rust.
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
#[cfg(feature = "ed25519")]
use bc_crypto::ED25519_SIGNATURE_SIZE;
#[cfg(feature = "secp256k1")]
use bc_crypto::{ECDSA_SIGNATURE_SIZE, SCHNORR_SIGNATURE_SIZE};
use bc_ur::prelude::*;
#[cfg(feature = "ssh")]
use ssh_key::{LineEnding, SshSig};

use super::SignatureScheme;
#[cfg(any(feature = "secp256k1", feature = "ed25519", feature = "ssh"))]
use crate::Error;
#[cfg(feature = "pqcrypto")]
use crate::MLDSASignature;
use crate::{Result, tags};

/// A digital signature created with various signature algorithms.
///
/// `Signature` is an enum representing different types of digital signatures:
///
/// - `Schnorr`: A BIP-340 Schnorr signature (64 bytes)
/// - `ECDSA`: An ECDSA signature using the secp256k1 curve (64 bytes)
/// - `Ed25519`: An Ed25519 signature (64 bytes)
/// - `SSH`: An SSH signature in various formats
/// - `MLDSA`: A post-quantum ML-DSA signature
///
/// Signatures can be serialized to and from CBOR with appropriate tags.
///
/// # Examples
///
/// ```ignore
/// # // Requires secp256k1 feature (enabled by default)
/// use bc_components::{SignatureScheme, Signer, Verifier};
///
/// // Create a key pair using Schnorr
/// let (private_key, public_key) = SignatureScheme::Schnorr.keypair();
///
/// // Sign a message
/// let message = b"Hello, world!";
/// let signature = private_key.sign(&message).unwrap();
///
/// // The signature can be verified with the corresponding public key
/// assert!(public_key.verify(&signature, &message));
/// ```
///
/// Converting to and from CBOR:
///
/// ```ignore
/// # // Requires secp256k1 feature (enabled by default)
/// use bc_components::{SignatureScheme, Signer};
/// use dcbor::prelude::*;
///
/// // Create a signature
/// let (private_key, _) = SignatureScheme::Schnorr.keypair();
/// let message = b"Hello, world!";
/// let signature = private_key.sign(&message).unwrap();
///
/// // Convert to CBOR
/// let cbor: CBOR = signature.clone().into();
/// let data = cbor.to_cbor_data();
///
/// // Convert back from CBOR
/// let recovered =
///     bc_components::Signature::from_tagged_cbor_data(&data).unwrap();
///
/// // The signatures should be identical
/// assert_eq!(signature, recovered);
/// ```
#[derive(Clone)]
pub enum Signature {
    /// A BIP-340 Schnorr signature (64 bytes)
    #[cfg(feature = "secp256k1")]
    Schnorr([u8; SCHNORR_SIGNATURE_SIZE]),

    /// An ECDSA signature using the secp256k1 curve (64 bytes)
    #[cfg(feature = "secp256k1")]
    ECDSA([u8; ECDSA_SIGNATURE_SIZE]),

    /// An Ed25519 signature (64 bytes)
    #[cfg(feature = "ed25519")]
    Ed25519([u8; ED25519_SIGNATURE_SIZE]),

    /// An SSH signature
    #[cfg(feature = "ssh")]
    SSH(SshSig),

    /// A post-quantum ML-DSA signature
    #[cfg(feature = "pqcrypto")]
    MLDSA(MLDSASignature),
}

/// Implementation of equality comparison for Signature
impl PartialEq for Signature {
    /// Compares two signatures for equality.
    ///
    /// Signatures are equal if they have the same type and the same signature
    /// data. Signatures of different types (e.g., Schnorr vs ECDSA) are
    /// never equal.
    #[allow(unreachable_patterns)]
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            #[cfg(feature = "secp256k1")]
            (Self::Schnorr(a), Self::Schnorr(b)) => a == b,
            #[cfg(feature = "secp256k1")]
            (Self::ECDSA(a), Self::ECDSA(b)) => a == b,
            #[cfg(feature = "ed25519")]
            (Self::Ed25519(a), Self::Ed25519(b)) => a == b,
            #[cfg(feature = "ssh")]
            (Self::SSH(a), Self::SSH(b)) => a == b,
            #[cfg(feature = "pqcrypto")]
            (Self::MLDSA(a), Self::MLDSA(b)) => a.as_bytes() == b.as_bytes(),
            #[cfg(any(
                feature = "secp256k1",
                feature = "ed25519",
                feature = "ssh",
                feature = "pqcrypto"
            ))]
            _ => false,
            #[cfg(not(any(
                feature = "secp256k1",
                feature = "ed25519",
                feature = "ssh",
                feature = "pqcrypto"
            )))]
            _ => unreachable!(),
        }
    }
}

impl Signature {
    /// Creates a Schnorr signature from a 64-byte array.
    ///
    /// # Arguments
    ///
    /// * `data` - The 64-byte signature data
    ///
    /// # Returns
    ///
    /// A new Schnorr signature
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "secp256k1")]
    /// # {
    /// use bc_components::Signature;
    ///
    /// let data = [0u8; 64]; // In practice, this would be a real signature
    /// let signature = Signature::schnorr_from_data(data);
    /// # }
    /// ```
    #[cfg(feature = "secp256k1")]
    pub fn schnorr_from_data(data: [u8; SCHNORR_SIGNATURE_SIZE]) -> Self {
        Self::Schnorr(data)
    }

    /// Creates a Schnorr signature from a byte slice.
    ///
    /// # Arguments
    ///
    /// * `data` - A byte slice containing the signature data
    ///
    /// # Returns
    ///
    /// A `Result` containing the signature or an error if the data is not
    /// exactly 64 bytes in length.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "secp256k1")]
    /// # {
    /// use bc_components::Signature;
    ///
    /// let data = vec![0u8; 64]; // In practice, this would be a real signature
    /// let signature = Signature::schnorr_from_data_ref(&data).unwrap();
    /// # }
    /// ```
    #[cfg(feature = "secp256k1")]
    pub fn schnorr_from_data_ref(data: impl AsRef<[u8]>) -> Result<Self> {
        let data = data.as_ref();
        if data.len() != SCHNORR_SIGNATURE_SIZE {
            return Err(Error::invalid_size(
                "Schnorr signature",
                SCHNORR_SIGNATURE_SIZE,
                data.len(),
            ));
        }
        let mut arr = [0u8; SCHNORR_SIGNATURE_SIZE];
        arr.copy_from_slice(data);
        Ok(Self::schnorr_from_data(arr))
    }

    /// Creates an ECDSA signature from a 64-byte array.
    ///
    /// # Arguments
    ///
    /// * `data` - The 64-byte signature data
    ///
    /// # Returns
    ///
    /// A new ECDSA signature
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "secp256k1")]
    /// # {
    /// use bc_components::Signature;
    ///
    /// let data = [0u8; 64]; // In practice, this would be a real signature
    /// let signature = Signature::ecdsa_from_data(data);
    /// # }
    /// ```
    #[cfg(feature = "secp256k1")]
    pub fn ecdsa_from_data(data: [u8; ECDSA_SIGNATURE_SIZE]) -> Self {
        Self::ECDSA(data)
    }

    /// Creates an ECDSA signature from a byte slice.
    ///
    /// # Arguments
    ///
    /// * `data` - A byte slice containing the signature data
    ///
    /// # Returns
    ///
    /// A `Result` containing the signature or an error if the data is not
    /// exactly 64 bytes in length.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "secp256k1")]
    /// # {
    /// use bc_components::Signature;
    ///
    /// let data = vec![0u8; 64]; // In practice, this would be a real signature
    /// let signature = Signature::ecdsa_from_data_ref(&data).unwrap();
    /// # }
    /// ```
    #[cfg(feature = "secp256k1")]
    pub fn ecdsa_from_data_ref(data: impl AsRef<[u8]>) -> Result<Self> {
        let data = data.as_ref();
        if data.len() != ECDSA_SIGNATURE_SIZE {
            return Err(Error::invalid_size(
                "ECDSA signature",
                ECDSA_SIGNATURE_SIZE,
                data.len(),
            ));
        }
        let mut arr = [0u8; ECDSA_SIGNATURE_SIZE];
        arr.copy_from_slice(data);
        Ok(Self::ecdsa_from_data(arr))
    }

    /// Creates an Ed25519 signature from a 64-byte array.
    ///
    /// # Arguments
    ///
    /// * `data` - The 64-byte signature data
    ///
    /// # Returns
    ///
    /// A new Ed25519 signature
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "ed25519")]
    /// # {
    /// use bc_components::Signature;
    ///
    /// let data = [0u8; 64]; // In practice, this would be a real signature
    /// let signature = Signature::ed25519_from_data(data);
    /// # }
    /// ```
    #[cfg(feature = "ed25519")]
    pub fn ed25519_from_data(data: [u8; ED25519_SIGNATURE_SIZE]) -> Self {
        Self::Ed25519(data)
    }

    /// Creates an Ed25519 signature from a byte slice.
    ///
    /// # Arguments
    ///
    /// * `data` - A byte slice containing the signature data
    ///
    /// # Returns
    ///
    /// A `Result` containing the signature or an error if the data is not
    /// exactly 64 bytes in length.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "ed25519")]
    /// # {
    /// use bc_components::Signature;
    ///
    /// let data = vec![0u8; 64]; // In practice, this would be a real signature
    /// let signature = Signature::ed25519_from_data_ref(&data).unwrap();
    /// # }
    /// ```
    #[cfg(feature = "ed25519")]
    pub fn ed25519_from_data_ref(data: impl AsRef<[u8]>) -> Result<Self> {
        let data = data.as_ref();
        if data.len() != ED25519_SIGNATURE_SIZE {
            return Err(Error::invalid_size(
                "Ed25519 signature",
                ED25519_SIGNATURE_SIZE,
                data.len(),
            ));
        }
        let mut arr = [0u8; ED25519_SIGNATURE_SIZE];
        arr.copy_from_slice(data);
        Ok(Self::Ed25519(arr))
    }

    /// Creates an SSH signature from an `SshSig` object.
    ///
    /// # Arguments
    ///
    /// * `sig` - The SSH signature object
    ///
    /// # Returns
    ///
    /// A new SSH signature
    #[cfg(feature = "ssh")]
    pub fn from_ssh(sig: SshSig) -> Self { Self::SSH(sig) }

    /// Returns the Schnorr signature data if this is a Schnorr signature.
    ///
    /// # Returns
    ///
    /// Some reference to the 64-byte signature data if this is a Schnorr
    /// signature, or None if it's a different signature type.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "secp256k1")]
    /// # {
    /// use bc_components::{SignatureScheme, Signer};
    ///
    /// // Create a Schnorr signature
    /// let (private_key, _) = SignatureScheme::Schnorr.keypair();
    /// let message = b"Hello, world!";
    /// let signature = private_key.sign(&message).unwrap();
    ///
    /// // We can access the Schnorr signature data
    /// assert!(signature.to_schnorr().is_some());
    ///
    /// // Create an ECDSA signature
    /// let (ecdsa_key, _) = SignatureScheme::Ecdsa.keypair();
    /// let ecdsa_sig = ecdsa_key.sign(&message).unwrap();
    ///
    /// // This will return None since it's not a Schnorr signature
    /// assert!(ecdsa_sig.to_schnorr().is_none());
    /// # }
    /// ```
    #[cfg(feature = "secp256k1")]
    pub fn to_schnorr(&self) -> Option<&[u8; SCHNORR_SIGNATURE_SIZE]> {
        match self {
            Self::Schnorr(sig) => Some(sig),
            _ => None,
        }
    }

    /// Returns the ECDSA signature data if this is an ECDSA signature.
    ///
    /// # Returns
    ///
    /// Some reference to the 64-byte signature data if this is an ECDSA
    /// signature, or None if it's a different signature type.
    #[cfg(feature = "secp256k1")]
    pub fn to_ecdsa(&self) -> Option<&[u8; ECDSA_SIGNATURE_SIZE]> {
        match self {
            Self::ECDSA(sig) => Some(sig),
            _ => None,
        }
    }

    /// Returns the SSH signature if this is an SSH signature.
    ///
    /// # Returns
    ///
    /// Some reference to the SSH signature if this is an SSH signature,
    /// or None if it's a different signature type.
    #[cfg(feature = "ssh")]
    pub fn to_ssh(&self) -> Option<&SshSig> {
        match self {
            Self::SSH(sig) => Some(sig),
            #[cfg(any(
                feature = "secp256k1",
                feature = "ed25519",
                feature = "pqcrypto"
            ))]
            _ => None,
        }
    }

    /// Determines the signature scheme used to create this signature.
    ///
    /// # Returns
    ///
    /// A `Result` containing the signature scheme, or an error if the
    /// signature scheme cannot be determined (e.g., for unsupported SSH
    /// algorithms).
    ///
    /// # Examples
    ///
    /// ```ignore
    /// # // Requires secp256k1 feature (enabled by default)
    /// use bc_components::{SignatureScheme, Signer};
    ///
    /// // Create a signature with ECDSA
    /// let (private_key, _) = SignatureScheme::Ecdsa.keypair();
    /// let message = b"Hello, world!";
    /// let signature = private_key.sign(&message).unwrap();
    ///
    /// // Get the signature scheme
    /// let scheme = signature.scheme().unwrap();
    /// assert_eq!(scheme, SignatureScheme::Ecdsa);
    /// ```
    #[allow(unreachable_patterns)]
    pub fn scheme(&self) -> Result<SignatureScheme> {
        match self {
            #[cfg(feature = "secp256k1")]
            Self::Schnorr(_) => Ok(SignatureScheme::Schnorr),
            #[cfg(feature = "secp256k1")]
            Self::ECDSA(_) => Ok(SignatureScheme::Ecdsa),
            #[cfg(feature = "ed25519")]
            Self::Ed25519(_) => Ok(SignatureScheme::Ed25519),
            #[cfg(feature = "ssh")]
            Self::SSH(sig) => match sig.algorithm() {
                ssh_key::Algorithm::Dsa => Ok(SignatureScheme::SshDsa),
                ssh_key::Algorithm::Ecdsa { curve } => match curve {
                    ssh_key::EcdsaCurve::NistP256 => {
                        Ok(SignatureScheme::SshEcdsaP256)
                    }
                    ssh_key::EcdsaCurve::NistP384 => {
                        Ok(SignatureScheme::SshEcdsaP384)
                    }
                    _ => Err(Error::ssh("Unsupported SSH ECDSA curve")),
                },
                ssh_key::Algorithm::Ed25519 => Ok(SignatureScheme::SshEd25519),
                _ => Err(Error::ssh("Unsupported SSH signature algorithm")),
            },
            #[cfg(feature = "ssh")]
            Self::SSH(_) => unreachable!(),
            #[cfg(feature = "pqcrypto")]
            Self::MLDSA(sig) => match sig.level() {
                crate::MLDSA::MLDSA44 => Ok(SignatureScheme::MLDSA44),
                crate::MLDSA::MLDSA65 => Ok(SignatureScheme::MLDSA65),
                crate::MLDSA::MLDSA87 => Ok(SignatureScheme::MLDSA87),
            },
            #[cfg(not(any(
                feature = "secp256k1",
                feature = "ed25519",
                feature = "ssh",
                feature = "pqcrypto"
            )))]
            _ => unreachable!(),
        }
    }
}

/// Debug implementation for Signature
impl std::fmt::Debug for Signature {
    /// Formats the signature for display.
    ///
    /// For binary signatures (Schnorr, ECDSA, Ed25519), displays the
    /// hex-encoded signature data. For SSH and ML-DSA signatures, displays
    /// the signature object.
    #[allow(unreachable_patterns)]
    fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            #[cfg(feature = "secp256k1")]
            Signature::Schnorr(data) => _f
                .debug_struct("Schnorr")
                .field("data", &hex::encode(data))
                .finish(),
            #[cfg(feature = "secp256k1")]
            Signature::ECDSA(data) => _f
                .debug_struct("ECDSA")
                .field("data", &hex::encode(data))
                .finish(),
            #[cfg(feature = "ed25519")]
            Signature::Ed25519(data) => _f
                .debug_struct("Ed25519")
                .field("data", &hex::encode(data))
                .finish(),
            #[cfg(feature = "ssh")]
            Signature::SSH(sig) => {
                _f.debug_struct("SSH").field("sig", sig).finish()
            }
            #[cfg(feature = "pqcrypto")]
            Signature::MLDSA(sig) => {
                _f.debug_struct("MLDSA").field("sig", sig).finish()
            }
            #[cfg(not(any(
                feature = "secp256k1",
                feature = "ed25519",
                feature = "ssh",
                feature = "pqcrypto"
            )))]
            _ => unreachable!(),
        }
    }
}

/// Implementation of AsRef for Signature
impl AsRef<Signature> for Signature {
    /// Returns a reference to self.
    fn as_ref(&self) -> &Signature { self }
}

/// Implementation of the CBORTagged trait for Signature
impl CBORTagged for Signature {
    /// Returns the CBOR tags used for this type.
    ///
    /// For Signature, the tag is 40020.
    fn cbor_tags() -> Vec<dcbor::Tag> {
        tags_for_values(&[tags::TAG_SIGNATURE])
    }
}

/// Conversion from Signature to CBOR
impl From<Signature> for CBOR {
    /// Converts a Signature to a tagged CBOR value.
    fn from(value: Signature) -> Self { value.tagged_cbor() }
}

/// Implementation of the CBORTaggedEncodable trait for Signature
impl CBORTaggedEncodable for Signature {
    /// Converts the Signature to an untagged CBOR value.
    ///
    /// The CBOR encoding depends on the signature type:
    ///
    /// - Schnorr: A byte string containing the 64-byte signature
    /// - ECDSA: An array containing the discriminator 1 and the 64-byte
    ///   signature
    /// - Ed25519: An array containing the discriminator 2 and the 64-byte
    ///   signature
    /// - SSH: A tagged text string containing the PEM-encoded signature
    /// - ML-DSA: Delegates to the MLDSASignature implementation
    #[allow(unreachable_patterns)]
    fn untagged_cbor(&self) -> CBOR {
        match self {
            #[cfg(feature = "secp256k1")]
            Signature::Schnorr(data) => CBOR::to_byte_string(data),
            #[cfg(feature = "secp256k1")]
            Signature::ECDSA(data) => {
                vec![(1).into(), CBOR::to_byte_string(data)].into()
            }
            #[cfg(feature = "ed25519")]
            Signature::Ed25519(data) => {
                vec![(2).into(), CBOR::to_byte_string(data)].into()
            }
            #[cfg(feature = "ssh")]
            Signature::SSH(sig) => {
                let pem = sig.to_pem(LineEnding::LF).unwrap();
                CBOR::to_tagged_value(tags::TAG_SSH_TEXT_SIGNATURE, pem)
            }
            #[cfg(feature = "pqcrypto")]
            Signature::MLDSA(sig) => sig.clone().into(),
            #[cfg(not(any(
                feature = "secp256k1",
                feature = "ed25519",
                feature = "ssh",
                feature = "pqcrypto"
            )))]
            _ => unreachable!(),
        }
    }
}

/// TryFrom implementation for converting CBOR to Signature
impl TryFrom<CBOR> for Signature {
    type Error = dcbor::Error;

    /// Tries to convert a CBOR value to a Signature.
    ///
    /// This is a convenience method that calls from_tagged_cbor.
    fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
        Self::from_tagged_cbor(cbor)
    }
}

/// Implementation of the CBORTaggedDecodable trait for Signature
impl CBORTaggedDecodable for Signature {
    /// Creates a Signature from an untagged CBOR value.
    ///
    /// # Arguments
    ///
    /// * `cbor` - The CBOR value to decode
    ///
    /// # Returns
    ///
    /// A Result containing the decoded Signature or an error if decoding fails.
    ///
    /// # Format
    ///
    /// The CBOR value must be one of:
    /// - A byte string (interpreted as a Schnorr signature)
    /// - An array of length 2, where the first element is 1 (ECDSA) or 2
    ///   (Ed25519) and the second element is a byte string containing the
    ///   signature data
    /// - A tagged value with a tag for MLDSA or SSH signatures
    fn from_untagged_cbor(cbor: CBOR) -> dcbor::Result<Self> {
        match cbor.clone().into_case() {
            CBORCase::ByteString(bytes) => {
                #[cfg(feature = "secp256k1")]
                {
                    Ok(Self::schnorr_from_data_ref(bytes)?)
                }
                #[cfg(not(feature = "secp256k1"))]
                {
                    let _ = bytes;
                    Err("Schnorr signature not available without secp256k1 feature".into())
                }
            }
            CBORCase::Array(mut elements) => {
                if elements.len() == 2 {
                    let mut drain = elements.drain(0..);
                    let ele_0 = drain.next().unwrap().into_case();
                    #[cfg_attr(
                        not(any(feature = "secp256k1", feature = "ed25519")),
                        allow(unused_variables)
                    )]
                    let ele_1 = drain.next().unwrap().into_case();
                    match ele_0 {
                        #[cfg(feature = "secp256k1")]
                        CBORCase::ByteString(data) => {
                            return Ok(Self::schnorr_from_data_ref(data)?);
                        }
                        #[cfg(feature = "secp256k1")]
                        CBORCase::Unsigned(1) => {
                            if let CBORCase::ByteString(data) = ele_1 {
                                return Ok(Self::ecdsa_from_data_ref(data)?);
                            }
                        }
                        #[cfg(feature = "ed25519")]
                        CBORCase::Unsigned(2) => {
                            if let CBORCase::ByteString(data) = ele_1 {
                                return Ok(Self::ed25519_from_data_ref(data)?);
                            }
                        }
                        _ => (),
                    }
                }
                Err("Invalid signature format".into())
            }
            #[cfg_attr(not(feature = "ssh"), allow(unused_variables))]
            CBORCase::Tagged(tag, item) => match tag.value() {
                #[cfg(feature = "pqcrypto")]
                tags::TAG_MLDSA_SIGNATURE => {
                    let sig = MLDSASignature::try_from(cbor)?;
                    Ok(Self::MLDSA(sig))
                }
                #[cfg(feature = "ssh")]
                tags::TAG_SSH_TEXT_SIGNATURE => {
                    let string = item.try_into_text()?;
                    let pem = SshSig::from_pem(string)
                        .map_err(|_| "Invalid PEM format")?;
                    Ok(Self::SSH(pem))
                }
                _ => Err("Invalid signature format".into()),
            },
            _ => Err("Invalid signature format".into()),
        }
    }
}