libcrux_rsa/
lib.rs

1#![no_std]
2
3extern crate alloc;
4
5#[cfg(not(feature = "expose-hacl"))]
6mod hacl {
7    pub(crate) mod rsapss;
8
9    use libcrux_hacl_rs::streaming_types;
10    use libcrux_sha2::hacl as hash_sha2;
11}
12
13/// The hacl-rs code for RSA signatures
14#[cfg(feature = "expose-hacl")]
15pub mod hacl {
16    /// The RSA-PSS signature code.
17    pub mod rsapss;
18
19    use libcrux_hacl_rs::streaming_types;
20    use libcrux_sha2::hacl as hash_sha2;
21}
22
23/// The hash algorithm used for signing or verifying.
24#[derive(Clone, Copy, Debug)]
25pub enum DigestAlgorithm {
26    /// The SHA256 hash algorithm
27    Sha2_256,
28
29    /// The SHA384 hash algorithm
30    Sha2_384,
31
32    /// The SHA512 hash algorithm
33    Sha2_512,
34}
35
36impl DigestAlgorithm {
37    // using u8 so it can be safely coerced into any uint type
38    const fn hash_len(&self) -> u8 {
39        match self {
40            DigestAlgorithm::Sha2_256 => 32,
41            DigestAlgorithm::Sha2_384 => 48,
42            DigestAlgorithm::Sha2_512 => 64,
43        }
44    }
45}
46
47/// Represents errors that occurred during signing or verifying.
48#[derive(Debug, PartialEq, Eq)]
49pub enum Error {
50    /// Indicates that the salt is too large.
51    SaltTooLarge,
52
53    /// Indicates that the message is too large.
54    MessageTooLarge,
55
56    /// Indicates that the verification of a signature failed.
57    VerificationFailed,
58
59    /// Indicates that signing a message failed.
60    SigningFailed,
61
62    /// The lengths of the public and private parts of the key do not match
63    KeyLengthMismatch,
64
65    /// The length of the provided key is invalid
66    InvalidKeyLength,
67
68    /// The length of the provided signature is invalid
69    InvalidSignatureLength,
70}
71
72impl alloc::fmt::Display for Error {
73    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
74        let text = match self {
75            Error::SaltTooLarge => "Indicates that the salt is too large.",
76            Error::MessageTooLarge => "Indicates that the message is too large.",
77            Error::VerificationFailed => "Indicates that the verification of a signature failed.",
78            Error::SigningFailed => "Indicates that signing a message failed.",
79            Error::KeyLengthMismatch => {
80                "The lengths of the public and private parts of the key do not match"
81            }
82            Error::InvalidKeyLength => "The length of the provided key is invalid",
83            Error::InvalidSignatureLength => "The length of the provided signature is invalid",
84        };
85
86        f.write_str(text)
87    }
88}
89
90impl core::error::Error for Error {}
91
92mod impl_hacl;
93
94pub use impl_hacl::*;