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
use bc_ur::prelude::*;
#[cfg(feature = "ssh")]
use ssh_key::public::PublicKey as SSHPublicKey;

#[cfg(feature = "ed25519")]
use crate::Ed25519PublicKey;
#[cfg(feature = "pqcrypto")]
use crate::MLDSAPublicKey;
use crate::{Digest, Reference, ReferenceProvider, Signature, Verifier, tags};
#[cfg(feature = "secp256k1")]
use crate::{ECKeyBase, ECPublicKey, SchnorrPublicKey};

/// A public key used for verifying digital signatures.
///
/// `SigningPublicKey` is an enum representing different types of signing public
/// keys, including elliptic curve schemes (ECDSA, Schnorr), Edwards curve
/// schemes (Ed25519), post-quantum schemes (ML-DSA), and SSH keys.
///
/// This type implements the `Verifier` trait, allowing it to verify signatures
/// of the appropriate type.
///
/// # Examples
///
/// Creating and using a signing public key pair:
///
/// ```ignore
/// # // Requires secp256k1 feature (enabled by default)
/// use bc_components::{SignatureScheme, Signer, Verifier};
///
/// // Create a key pair
/// let (private_key, public_key) = SignatureScheme::Schnorr.keypair();
///
/// // Sign a message
/// let message = b"Hello, world!";
/// let signature = private_key.sign(&message).unwrap();
///
/// // Verify the signature
/// assert!(public_key.verify(&signature, &message));
/// ```
///
/// # CBOR Serialization
///
/// `SigningPublicKey` can be serialized to and from CBOR with appropriate tags:
///
/// ```ignore
/// # // Requires secp256k1 feature (enabled by default)
/// use bc_components::{SignatureScheme, SigningPublicKey};
/// use dcbor::prelude::*;
///
/// // Create a key pair and get the public key
/// let (_, public_key) = SignatureScheme::Schnorr.keypair();
///
/// // Convert to CBOR
/// let cbor: CBOR = public_key.clone().into();
/// let data = cbor.to_cbor_data();
///
/// // Convert back from CBOR
/// let recovered = SigningPublicKey::from_tagged_cbor_data(&data).unwrap();
///
/// // The keys should be equal
/// assert_eq!(public_key, recovered);
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum SigningPublicKey {
    /// A Schnorr public key (BIP-340, x-only)
    #[cfg(feature = "secp256k1")]
    Schnorr(SchnorrPublicKey),

    /// An ECDSA public key (compressed, 33 bytes)
    #[cfg(feature = "secp256k1")]
    ECDSA(ECPublicKey),

    /// An Ed25519 public key
    #[cfg(feature = "ed25519")]
    Ed25519(Ed25519PublicKey),

    /// An SSH public key
    #[cfg(feature = "ssh")]
    SSH(SSHPublicKey),

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

impl SigningPublicKey {
    /// Creates a new signing public key from a Schnorr public key.
    ///
    /// # Arguments
    ///
    /// * `key` - A BIP-340 Schnorr public key
    ///
    /// # Returns
    ///
    /// A new signing public key containing the Schnorr key
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "secp256k1")]
    /// # {
    /// use bc_components::{SchnorrPublicKey, SigningPublicKey};
    ///
    /// // Create a Schnorr public key
    /// let schnorr_key = SchnorrPublicKey::from_data([0u8; 32]);
    ///
    /// // Create a signing public key from it
    /// let signing_key = SigningPublicKey::from_schnorr(schnorr_key);
    /// # }
    /// ```
    #[cfg(feature = "secp256k1")]
    pub fn from_schnorr(key: SchnorrPublicKey) -> Self { Self::Schnorr(key) }

    /// Creates a new signing public key from an ECDSA public key.
    ///
    /// # Arguments
    ///
    /// * `key` - A compressed ECDSA public key
    ///
    /// # Returns
    ///
    /// A new signing public key containing the ECDSA key
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "secp256k1")]
    /// # {
    /// use bc_components::{ECKey, ECPrivateKey, SigningPublicKey};
    ///
    /// // Create an EC private key and derive its public key
    /// let private_key = ECPrivateKey::new();
    /// let public_key = private_key.public_key();
    ///
    /// // Create a signing public key from it
    /// let signing_key = SigningPublicKey::from_ecdsa(public_key);
    /// # }
    /// ```
    #[cfg(feature = "secp256k1")]
    pub fn from_ecdsa(key: ECPublicKey) -> Self { Self::ECDSA(key) }

    /// Creates a new signing public key from an Ed25519 public key.
    ///
    /// # Arguments
    ///
    /// * `key` - An Ed25519 public key
    ///
    /// # Returns
    ///
    /// A new signing public key containing the Ed25519 key
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "ed25519")]
    /// # {
    /// use bc_components::{Ed25519PrivateKey, SigningPublicKey};
    ///
    /// // Create an Ed25519 private key and get its public key
    /// let private_key = Ed25519PrivateKey::new();
    /// let public_key = private_key.public_key();
    ///
    /// // Create a signing public key from it
    /// let signing_key = SigningPublicKey::from_ed25519(public_key);
    /// # }
    /// ```
    #[cfg(feature = "ed25519")]
    pub fn from_ed25519(key: Ed25519PublicKey) -> Self { Self::Ed25519(key) }

    /// Creates a new signing public key from an SSH public key.
    ///
    /// # Arguments
    ///
    /// * `key` - An SSH public key
    ///
    /// # Returns
    ///
    /// A new signing public key containing the SSH key
    #[cfg(feature = "ssh")]
    pub fn from_ssh(key: SSHPublicKey) -> Self { Self::SSH(key) }

    /// Returns the underlying Schnorr public key if this is a Schnorr key.
    ///
    /// # Returns
    ///
    /// Some reference to the Schnorr public key if this is a Schnorr key,
    /// or None if it's a different key type.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "secp256k1")]
    /// # {
    /// use bc_components::{SignatureScheme, Signer};
    ///
    /// // Create a Schnorr key pair
    /// let (private_key, public_key) = SignatureScheme::Schnorr.keypair();
    ///
    /// // We can access the Schnorr public key
    /// assert!(public_key.to_schnorr().is_some());
    ///
    /// // Create an ECDSA key pair
    /// let (_, ecdsa_public) = SignatureScheme::Ecdsa.keypair();
    ///
    /// // This will return None since it's not a Schnorr key
    /// assert!(ecdsa_public.to_schnorr().is_none());
    /// # }
    /// ```
    #[cfg(feature = "secp256k1")]
    pub fn to_schnorr(&self) -> Option<&SchnorrPublicKey> {
        match self {
            Self::Schnorr(key) => Some(key),
            _ => None,
        }
    }

    /// Returns the underlying ECDSA public key if this is an ECDSA key.
    ///
    /// # Returns
    ///
    /// Some reference to the ECDSA public key if this is an ECDSA key,
    /// or None if it's a different key type.
    #[cfg(feature = "secp256k1")]
    pub fn to_ecdsa(&self) -> Option<&ECPublicKey> {
        match self {
            Self::ECDSA(key) => Some(key),
            _ => None,
        }
    }

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

/// Implementation of the Verifier trait for SigningPublicKey
impl Verifier for SigningPublicKey {
    /// Verifies a signature against a message.
    ///
    /// The type of signature must match the type of this key, and the
    /// signature must be valid for the message, or the verification
    /// will fail.
    ///
    /// # Arguments
    ///
    /// * `signature` - The signature to verify
    /// * `message` - The message that was allegedly signed
    ///
    /// # Returns
    ///
    /// `true` if the signature is valid for the message, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```ignore
    /// # // Requires secp256k1 feature (enabled by default)
    /// use bc_components::{SignatureScheme, Signer, Verifier};
    ///
    /// // Create a key pair
    /// let (private_key, public_key) = SignatureScheme::Schnorr.keypair();
    ///
    /// // Sign a message
    /// let message = b"Hello, world!";
    /// let signature = private_key.sign(&message).unwrap();
    ///
    /// // Verify the signature with the correct message (should succeed)
    /// assert!(public_key.verify(&signature, &message));
    ///
    /// // Verify the signature with an incorrect message (should fail)
    /// assert!(!public_key.verify(&signature, &b"Tampered message"));
    /// ```
    #[allow(unreachable_patterns)]
    fn verify(
        &self,
        _signature: &Signature,
        _message: &dyn AsRef<[u8]>,
    ) -> bool {
        match self {
            #[cfg(feature = "secp256k1")]
            SigningPublicKey::Schnorr(key) => match _signature {
                Signature::Schnorr(sig) => key.schnorr_verify(sig, _message),
                _ => false,
            },
            #[cfg(feature = "secp256k1")]
            SigningPublicKey::ECDSA(key) => match _signature {
                Signature::ECDSA(sig) => key.verify(sig, _message),
                _ => false,
            },
            #[cfg(feature = "ed25519")]
            SigningPublicKey::Ed25519(key) => match _signature {
                Signature::Ed25519(sig) => key.verify(sig, _message),
                #[cfg(any(
                    feature = "secp256k1",
                    feature = "ssh",
                    feature = "pqcrypto"
                ))]
                _ => false,
            },
            #[cfg(feature = "ssh")]
            SigningPublicKey::SSH(key) => match _signature {
                Signature::SSH(sig) => {
                    key.verify(sig.namespace(), _message.as_ref(), sig).is_ok()
                }
                _ => false,
            },
            #[cfg(feature = "pqcrypto")]
            SigningPublicKey::MLDSA(key) => match _signature {
                Signature::MLDSA(sig) => key
                    .verify(sig, _message)
                    .map_err(|_| false)
                    .unwrap_or(false),
                _ => false,
            },
            #[cfg(not(any(
                feature = "secp256k1",
                feature = "ed25519",
                feature = "ssh",
                feature = "pqcrypto"
            )))]
            _ => unreachable!(),
        }
    }
}

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

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

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

/// Implementation of the CBORTaggedEncodable trait for SigningPublicKey
impl CBORTaggedEncodable for SigningPublicKey {
    /// Converts the SigningPublicKey to an untagged CBOR value.
    ///
    /// The CBOR encoding depends on the key type:
    ///
    /// - Schnorr: A byte string containing the 32-byte x-only public key
    /// - ECDSA: An array containing the discriminator 1 and the 33-byte
    ///   compressed public key
    /// - Ed25519: An array containing the discriminator 2 and the 32-byte
    ///   public key
    /// - SSH: A tagged text string containing the OpenSSH-encoded public key
    /// - ML-DSA: Delegates to the MLDSAPublicKey implementation
    #[allow(unreachable_patterns)]
    fn untagged_cbor(&self) -> CBOR {
        match self {
            #[cfg(feature = "secp256k1")]
            SigningPublicKey::Schnorr(key) => CBOR::to_byte_string(key.data()),
            #[cfg(feature = "secp256k1")]
            SigningPublicKey::ECDSA(key) => {
                vec![(1).into(), CBOR::to_byte_string(key.data())].into()
            }
            #[cfg(feature = "ed25519")]
            SigningPublicKey::Ed25519(key) => {
                vec![(2).into(), CBOR::to_byte_string(key.data())].into()
            }
            #[cfg(feature = "ssh")]
            SigningPublicKey::SSH(key) => {
                let string = key.to_openssh().unwrap();
                CBOR::to_tagged_value(tags::TAG_SSH_TEXT_PUBLIC_KEY, string)
            }
            #[cfg(feature = "pqcrypto")]
            SigningPublicKey::MLDSA(key) => key.clone().into(),
            #[cfg(not(any(
                feature = "secp256k1",
                feature = "ed25519",
                feature = "ssh",
                feature = "pqcrypto"
            )))]
            _ => unreachable!(),
        }
    }
}

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

    /// Tries to convert a CBOR value to a SigningPublicKey.
    ///
    /// 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 SigningPublicKey
impl CBORTaggedDecodable for SigningPublicKey {
    /// Creates a SigningPublicKey from an untagged CBOR value.
    ///
    /// # Arguments
    ///
    /// * `untagged_cbor` - The CBOR value to decode
    ///
    /// # Returns
    ///
    /// A Result containing the decoded SigningPublicKey or an error if decoding
    /// fails.
    ///
    /// # Format
    ///
    /// The CBOR value must be one of:
    /// - A byte string (interpreted as a Schnorr public key)
    /// - An array of length 2, where the first element is a discriminator (1
    ///   for ECDSA, 2 for Ed25519) and the second element is a byte string
    ///   containing the key data
    /// - A tagged value with a tag for ML-DSA or SSH keys
    fn from_untagged_cbor(untagged_cbor: CBOR) -> dcbor::Result<Self> {
        match untagged_cbor.clone().into_case() {
            CBORCase::ByteString(data) => {
                #[cfg(feature = "secp256k1")]
                {
                    Ok(Self::Schnorr(SchnorrPublicKey::from_data_ref(data)?))
                }
                #[cfg(not(feature = "secp256k1"))]
                {
                    let _ = data;
                    Err("Schnorr public key 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();
                    #[cfg(feature = "secp256k1")]
                    if let CBORCase::Unsigned(1) = ele_0
                        && let CBORCase::ByteString(data) = ele_1
                    {
                        return Ok(Self::ECDSA(ECPublicKey::from_data_ref(
                            data,
                        )?));
                    }
                    #[cfg(not(feature = "secp256k1"))]
                    let _ = ele_0;
                    #[cfg(feature = "ed25519")]
                    if let CBORCase::Unsigned(2) = ele_0
                        && let CBORCase::ByteString(data) = ele_1
                    {
                        return Ok(Self::Ed25519(
                            Ed25519PublicKey::from_data_ref(data)?,
                        ));
                    }
                }
                Err("invalid signing public key".into())
            }
            #[cfg_attr(not(feature = "ssh"), allow(unused_variables))]
            CBORCase::Tagged(tag, item) => match tag.value() {
                #[cfg(feature = "ssh")]
                tags::TAG_SSH_TEXT_PUBLIC_KEY => {
                    let string = item.try_into_text()?;
                    let key = SSHPublicKey::from_openssh(&string)
                        .map_err(|_| "invalid SSH public key")?;
                    Ok(Self::SSH(key))
                }
                #[cfg(feature = "pqcrypto")]
                tags::TAG_MLDSA_PUBLIC_KEY => {
                    let key = MLDSAPublicKey::from_tagged_cbor(untagged_cbor)?;
                    Ok(Self::MLDSA(key))
                }
                _ => Err("invalid signing public key".into()),
            },
            _ => Err("invalid signing public key".into()),
        }
    }
}

#[cfg(feature = "ssh")]
impl ReferenceProvider for SSHPublicKey {
    fn reference(&self) -> Reference {
        let string = self.to_openssh().unwrap();
        let bytes = string.as_bytes();
        let digest = Digest::from_image(bytes);
        Reference::from_digest(digest)
    }
}

impl ReferenceProvider for SigningPublicKey {
    fn reference(&self) -> Reference {
        Reference::from_digest(Digest::from_image(
            self.tagged_cbor().to_cbor_data(),
        ))
    }
}

impl std::fmt::Display for SigningPublicKey {
    #[allow(unreachable_patterns)]
    fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        #[cfg(any(
            feature = "secp256k1",
            feature = "ed25519",
            feature = "ssh",
            feature = "pqcrypto"
        ))]
        {
            let display_key = match self {
                #[cfg(feature = "secp256k1")]
                SigningPublicKey::Schnorr(key) => key.to_string(),
                #[cfg(feature = "secp256k1")]
                SigningPublicKey::ECDSA(key) => key.to_string(),
                #[cfg(feature = "ed25519")]
                SigningPublicKey::Ed25519(key) => key.to_string(),
                #[cfg(feature = "ssh")]
                SigningPublicKey::SSH(key) => {
                    format!("SSHPublicKey({})", key.ref_hex_short())
                }
                #[cfg(feature = "pqcrypto")]
                SigningPublicKey::MLDSA(key) => key.to_string(),
                _ => unreachable!(),
            };
            write!(
                _f,
                "SigningPublicKey({}, {})",
                self.ref_hex_short(),
                display_key
            )
        }
        #[cfg(not(any(
            feature = "secp256k1",
            feature = "ed25519",
            feature = "ssh",
            feature = "pqcrypto"
        )))]
        {
            match *self {}
        }
    }
}