ssi-crypto 0.2.1

Implementation of various hashes and signatures for the ssi library.
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
use core::fmt;

use serde::{Deserialize, Serialize};

pub trait SignatureAlgorithmType {
    type Instance: SignatureAlgorithmInstance<Algorithm = Self>;
}

pub trait SignatureAlgorithmInstance {
    type Algorithm;

    fn algorithm(&self) -> Self::Algorithm;
}

macro_rules! algorithms {
    ($(
        $(#[doc = $doc:tt])*
        $(#[doc($doc_tag:ident)])?
        $(#[serde $serde:tt])?
        $id:ident $( ($arg:ty) )? : $name:literal
    ),*) => {
        /// Signature algorithm.
        #[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Hash, Eq)]
        pub enum Algorithm {
            $(
                $(#[doc = $doc])*
                $(#[doc($doc_tag)])?
                $(#[serde $serde])?
                #[serde(rename = $name)]
                $id,
            )*
            /// No signature.
            ///
            /// Per the specs it should only be `none` but `None` is kept for backwards
            /// compatibility.
            #[serde(alias = "None")]
            None
        }

        impl Algorithm {
            pub fn as_str(&self) -> &'static str {
                match self {
                    $(
                        Self::$id => $name,
                    )*
                    Self::None => "none"
                }
            }

            pub fn into_str(self) -> &'static str {
                match self {
                    $(
                        Self::$id => $name,
                    )*
                    Self::None => "none"
                }
            }
        }

        impl SignatureAlgorithmType for Algorithm {
            type Instance = AlgorithmInstance;
        }

        #[derive(Debug, Clone)]
        pub enum AlgorithmInstance {
            $(
                $(#[doc = $doc])*
                $(#[doc($doc_tag)])?
                $(#[serde $serde])?
                $id $( ($arg) )?,
            )*
            /// No signature
            None
        }

        impl AlgorithmInstance {
            pub fn algorithm(&self) -> Algorithm {
                match self {
                    $(Self::$id $( (algorithms!(@ignore_arg $arg)) )? => Algorithm::$id,)*
                    Self::None => Algorithm::None
                }
            }
        }

        impl SignatureAlgorithmInstance for AlgorithmInstance {
            type Algorithm = Algorithm;

            fn algorithm(&self) -> Algorithm {
                self.algorithm()
            }
        }

        $(
            $(#[doc = $doc])*
            #[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Eq)]
            pub struct $id;

            algorithms!(@instance $id $($arg)?);

            impl TryFrom<Algorithm> for $id {
                type Error = UnsupportedAlgorithm;

                fn try_from(a: Algorithm) -> Result<Self, Self::Error> {
                    match a {
                        Algorithm::$id => Ok(Self),
                        a => Err(UnsupportedAlgorithm(a))
                    }
                }
            }

            impl From<$id> for Algorithm {
                fn from(_a: $id) -> Self {
                    Self::$id
                }
            }
        )*
    };
    { @instance $id:ident } => {
        impl SignatureAlgorithmType for $id {
            type Instance = Self;
        }

        impl SignatureAlgorithmInstance for $id {
            type Algorithm = $id;

            fn algorithm(&self) -> $id {
                *self
            }
        }

        impl TryFrom<AlgorithmInstance> for $id {
            type Error = UnsupportedAlgorithm;

            fn try_from(a: AlgorithmInstance) -> Result<Self, Self::Error> {
                match a {
                    AlgorithmInstance::$id => Ok(Self),
                    other => Err(UnsupportedAlgorithm(other.algorithm()))
                }
            }
        }

        impl From<$id> for AlgorithmInstance {
            fn from(_: $id) -> Self {
                Self::$id
            }
        }
    };
    { @instance $id:ident $arg:ty } => {
        impl SignatureAlgorithmType for $id {
            type Instance = $arg;
        }

        impl SignatureAlgorithmInstance for $arg {
            type Algorithm = $id;

            fn algorithm(&self) -> $id {
                $id
            }
        }

        impl TryFrom<AlgorithmInstance> for $arg {
            type Error = UnsupportedAlgorithm;

            fn try_from(a: AlgorithmInstance) -> Result<Self, Self::Error> {
                match a {
                    AlgorithmInstance::$id(arg) => Ok(arg),
                    other => Err(UnsupportedAlgorithm(other.algorithm()))
                }
            }
        }

        impl From<$arg> for AlgorithmInstance {
            fn from(value: $arg) -> Self {
                Self::$id(value)
            }
        }
    };
    { @ignore_arg $arg:ty } => { _ };
}

algorithms! {
    /// HMAC using SHA-256.
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc7518.txt>
    HS256: "HS256",

    /// HMAC using SHA-384.
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc7518.txt>
    HS384: "HS384",

    /// HMAC using SHA-512.
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc7518.txt>
    HS512: "HS512",

    /// RSASSA-PKCS1-v1_5 using SHA-256.
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc7518.txt>
    RS256: "RS256",

    /// RSASSA-PKCS1-v1_5 using SHA-384.
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc7518.txt>
    RS384: "RS384",

    /// RSASSA-PKCS1-v1_5 using SHA-512.
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc7518.txt>
    RS512: "RS512",

    /// RSASSA-PSS using SHA-256 and MGF1 with SHA-256.
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc7518.txt>
    PS256: "PS256",

    /// RSASSA-PSS using SHA-384 and MGF1 with SHA-384.
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc7518.txt>
    PS384: "PS384",

    /// RSASSA-PSS using SHA-512 and MGF1 with SHA-512.
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc7518.txt>
    PS512: "PS512",

    /// Edwards-curve Digital Signature Algorithm (EdDSA) using SHA-256.
    ///
    /// The following curves are defined for use with `EdDSA`:
    ///  - `Ed25519`
    ///  - `Ed448`
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc8037>
    EdDSA: "EdDSA",

    /// EdDSA using SHA-256 and Blake2b as pre-hash function.
    EdBlake2b: "EdBlake2b", // TODO Blake2b is supposed to replace SHA-256

    /// ECDSA using P-256 and SHA-256.
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc7518.txt>
    ES256: "ES256",

    /// ECDSA using P-384 and SHA-384.
    ///
    /// See: <https://www.rfc-editor.org/rfc/rfc7518.txt>
    ES384: "ES384",

    /// ECDSA using secp256k1 (K-256) and SHA-256.
    ///
    /// See: <https://datatracker.ietf.org/doc/html/rfc8812>
    ES256K: "ES256K",

    /// ECDSA using secp256k1 (K-256) and SHA-256 with a recovery bit.
    ///
    /// `ES256K-R` is similar to `ES256K` with the recovery bit appended, making
    /// the signature 65 bytes instead of 64. The recovery bit is used to
    /// extract the public key from the signature.
    ///
    /// See: <https://github.com/decentralized-identity/EcdsaSecp256k1RecoverySignature2020#es256k-r>
    ES256KR: "ES256K-R",

    /// ECDSA using secp256k1 (K-256) and Keccak-256.
    ///
    /// Like `ES256K` but using Keccak-256 instead of SHA-256.
    ESKeccakK: "ESKeccakK",

    /// ECDSA using secp256k1 (K-256) and Keccak-256 with a recovery bit.
    ///
    /// Like `ES256K-R` but using Keccak-256 instead of SHA-256.
    ESKeccakKR: "ESKeccakKR",

    /// ECDSA using P-256 and Blake2b.
    ESBlake2b: "ESBlake2b",

    /// ECDSA using secp256k1 (K-256) and Blake2b.
    ESBlake2bK: "ESBlake2bK",

    /// BBS scheme.
    Bbs(BbsInstance): "BBS",
    // Bbs: "BBS",

    #[doc(hidden)]
    AleoTestnet1Signature: "AleoTestnet1Signature"
}

impl Algorithm {
    /// Checks if this algorithm is compatible with the `other` algorithm.
    ///
    /// An algorithm `A` is compatible with `B` if `A` can be used to verify a
    /// signature created from `B`.
    pub fn is_compatible_with(&self, other: Self) -> bool {
        match self {
            Self::ES256K | Self::ES256KR | Self::ESKeccakK | Self::ESKeccakKR => matches!(
                other,
                Self::ES256K | Self::ES256KR | Self::ESKeccakK | Self::ESKeccakKR
            ),
            a => *a == other,
        }
    }
}

impl Default for Algorithm {
    fn default() -> Self {
        Self::None
    }
}

impl AsRef<str> for Algorithm {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Display for Algorithm {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_str().fmt(f)
    }
}

#[derive(Debug, thiserror::Error)]
pub enum AlgorithmError {
    /// Missing algorithm.
    #[error("missing algorithm")]
    Missing,

    /// Unsupported algorithm.
    #[error("unsupported signature algorithm `{0}`")]
    Unsupported(Algorithm),
}

#[derive(Debug, thiserror::Error)]
#[error("unsupported signature algorithm `{0}`")]
pub struct UnsupportedAlgorithm(pub Algorithm);

/// ECDSA using secp256k1 (K-256) and SHA-256, with or without recovery bit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AnyES256K {
    /// ECDSA using secp256k1 (K-256) and SHA-256, without recovery bit.
    ES256K,

    /// ECDSA using secp256k1 (K-256) and SHA-256, with recovery bit.
    ES256KR,
}

impl SignatureAlgorithmType for AnyES256K {
    type Instance = Self;
}

impl SignatureAlgorithmInstance for AnyES256K {
    type Algorithm = AnyES256K;

    fn algorithm(&self) -> AnyES256K {
        *self
    }
}

impl TryFrom<Algorithm> for AnyES256K {
    type Error = UnsupportedAlgorithm;

    fn try_from(value: Algorithm) -> Result<Self, Self::Error> {
        match value {
            Algorithm::ES256K => Ok(Self::ES256K),
            Algorithm::ES256KR => Ok(Self::ES256KR),
            other => Err(UnsupportedAlgorithm(other)),
        }
    }
}

impl From<AnyES256K> for Algorithm {
    fn from(value: AnyES256K) -> Self {
        match value {
            AnyES256K::ES256K => Self::ES256K,
            AnyES256K::ES256KR => Self::ES256KR,
        }
    }
}

impl From<ES256K> for AnyES256K {
    fn from(_value: ES256K) -> Self {
        Self::ES256K
    }
}

impl From<ES256KR> for AnyES256K {
    fn from(_value: ES256KR) -> Self {
        Self::ES256KR
    }
}

/// ECDSA using secp256k1 (K-256) and SHA-256, with or without recovery bit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AnyESKeccakK {
    /// ECDSA using secp256k1 (K-256) and Keccak-256.
    ///
    /// Like `ES256K` but using Keccak-256 instead of SHA-256.
    ESKeccakK,

    /// ECDSA using secp256k1 (K-256) and Keccak-256 with a recovery bit.
    ///
    /// Like `ES256K-R` but using Keccak-256 instead of SHA-256.
    ESKeccakKR,
}

impl SignatureAlgorithmType for AnyESKeccakK {
    type Instance = Self;
}

impl SignatureAlgorithmInstance for AnyESKeccakK {
    type Algorithm = AnyESKeccakK;

    fn algorithm(&self) -> AnyESKeccakK {
        *self
    }
}

impl TryFrom<Algorithm> for AnyESKeccakK {
    type Error = UnsupportedAlgorithm;

    fn try_from(value: Algorithm) -> Result<Self, Self::Error> {
        match value {
            Algorithm::ESKeccakK => Ok(Self::ESKeccakK),
            Algorithm::ESKeccakKR => Ok(Self::ESKeccakKR),
            other => Err(UnsupportedAlgorithm(other)),
        }
    }
}

impl From<AnyESKeccakK> for Algorithm {
    fn from(value: AnyESKeccakK) -> Self {
        match value {
            AnyESKeccakK::ESKeccakK => Self::ESKeccakK,
            AnyESKeccakK::ESKeccakKR => Self::ESKeccakKR,
        }
    }
}

impl From<AnyESKeccakK> for AlgorithmInstance {
    fn from(value: AnyESKeccakK) -> Self {
        match value {
            AnyESKeccakK::ESKeccakK => Self::ESKeccakK,
            AnyESKeccakK::ESKeccakKR => Self::ESKeccakKR,
        }
    }
}

impl From<ESKeccakK> for AnyESKeccakK {
    fn from(_value: ESKeccakK) -> Self {
        Self::ESKeccakK
    }
}

impl From<ESKeccakKR> for AnyESKeccakK {
    fn from(_value: ESKeccakKR) -> Self {
        Self::ESKeccakKR
    }
}

/// ECDSA using secp256k1 (K-256) and SHA-256, with or without recovery bit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AnyES {
    /// ECDSA using secp256k1 (K-256) and SHA-256, without recovery bit.
    ES256K,

    /// ECDSA using secp256k1 (K-256) and SHA-256, with recovery bit.
    ES256KR,

    ESKeccakK,

    /// ECDSA using secp256k1 (K-256) and Keccak-256 with a recovery bit.
    ///
    /// Like `ES256K-R` but using Keccak-256 instead of SHA-256.
    ESKeccakKR,
}

impl SignatureAlgorithmType for AnyES {
    type Instance = Self;
}

impl SignatureAlgorithmInstance for AnyES {
    type Algorithm = AnyES;

    fn algorithm(&self) -> AnyES {
        *self
    }
}

impl TryFrom<Algorithm> for AnyES {
    type Error = UnsupportedAlgorithm;

    fn try_from(value: Algorithm) -> Result<Self, Self::Error> {
        match value {
            Algorithm::ES256K => Ok(Self::ES256K),
            Algorithm::ES256KR => Ok(Self::ES256KR),
            other => Err(UnsupportedAlgorithm(other)),
        }
    }
}

impl From<AnyES> for Algorithm {
    fn from(value: AnyES) -> Self {
        match value {
            AnyES::ES256K => Self::ES256K,
            AnyES::ES256KR => Self::ES256KR,
            AnyES::ESKeccakK => Self::ESKeccakK,
            AnyES::ESKeccakKR => Self::ESKeccakKR,
        }
    }
}

impl From<ES256K> for AnyES {
    fn from(_value: ES256K) -> Self {
        Self::ES256K
    }
}

impl From<ES256KR> for AnyES {
    fn from(_value: ES256KR) -> Self {
        Self::ES256KR
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AnyBlake2b {
    EdBlake2b,
    ESBlake2bK,
    ESBlake2b,
}

impl SignatureAlgorithmType for AnyBlake2b {
    type Instance = Self;
}

impl SignatureAlgorithmInstance for AnyBlake2b {
    type Algorithm = Self;

    fn algorithm(&self) -> AnyBlake2b {
        *self
    }
}

impl From<AnyBlake2b> for Algorithm {
    fn from(value: AnyBlake2b) -> Self {
        match value {
            AnyBlake2b::EdBlake2b => Self::EdBlake2b,
            AnyBlake2b::ESBlake2bK => Self::ESBlake2bK,
            AnyBlake2b::ESBlake2b => Self::ESBlake2b,
        }
    }
}

impl From<AnyBlake2b> for AlgorithmInstance {
    fn from(value: AnyBlake2b) -> Self {
        match value {
            AnyBlake2b::EdBlake2b => Self::EdBlake2b,
            AnyBlake2b::ESBlake2bK => Self::ESBlake2bK,
            AnyBlake2b::ESBlake2b => Self::ESBlake2b,
        }
    }
}

impl TryFrom<Algorithm> for AnyBlake2b {
    type Error = UnsupportedAlgorithm;

    fn try_from(value: Algorithm) -> Result<Self, Self::Error> {
        match value {
            Algorithm::EdBlake2b => Ok(Self::EdBlake2b),
            Algorithm::ESBlake2bK => Ok(Self::ESBlake2bK),
            Algorithm::ESBlake2b => Ok(Self::ESBlake2b),
            a => Err(UnsupportedAlgorithm(a)),
        }
    }
}

impl TryFrom<AlgorithmInstance> for AnyBlake2b {
    type Error = UnsupportedAlgorithm;

    fn try_from(value: AlgorithmInstance) -> Result<Self, Self::Error> {
        match value {
            AlgorithmInstance::EdBlake2b => Ok(Self::EdBlake2b),
            AlgorithmInstance::ESBlake2bK => Ok(Self::ESBlake2bK),
            AlgorithmInstance::ESBlake2b => Ok(Self::ESBlake2b),
            a => Err(UnsupportedAlgorithm(a.algorithm())),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ES256OrES384 {
    ES256,
    ES384,
}

impl ES256OrES384 {
    pub fn name(&self) -> &'static str {
        match self {
            Self::ES256 => "ES256",
            Self::ES384 => "ES384",
        }
    }
}

impl SignatureAlgorithmType for ES256OrES384 {
    type Instance = Self;
}

impl SignatureAlgorithmInstance for ES256OrES384 {
    type Algorithm = Self;

    fn algorithm(&self) -> Self {
        *self
    }
}

impl fmt::Display for ES256OrES384 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.name().fmt(f)
    }
}

impl From<ES256OrES384> for Algorithm {
    fn from(value: ES256OrES384) -> Self {
        match value {
            ES256OrES384::ES256 => Self::ES256,
            ES256OrES384::ES384 => Self::ES384,
        }
    }
}

impl From<ES256OrES384> for AlgorithmInstance {
    fn from(value: ES256OrES384) -> Self {
        match value {
            ES256OrES384::ES256 => Self::ES256,
            ES256OrES384::ES384 => Self::ES384,
        }
    }
}

#[derive(Debug, Clone)]
pub struct BbsInstance(pub Box<BbsParameters>);

#[derive(Debug, Clone)]
pub enum BbsParameters {
    Baseline {
        header: [u8; 64],
    },
    Blind {
        header: [u8; 64],
        commitment_with_proof: Option<Vec<u8>>,
        signer_blind: Option<[u8; 32]>,
    },
}