Skip to main content

crypto_rsa/
types.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5/// Hash function bound into an RSA signature verification suite.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum RsaHash {
8    /// SHA-1 for historical X.509/eMRTD document verification only.
9    Sha1,
10    /// SHA-256.
11    Sha256,
12    /// SHA-384.
13    Sha384,
14    /// SHA-512.
15    Sha512,
16}
17
18impl RsaHash {
19    /// Numeric suite identifier used by the C FFI.
20    pub const fn ffi_id(self) -> u32 {
21        match self {
22            Self::Sha1 => 1,
23            Self::Sha256 => 2,
24            Self::Sha384 => 3,
25            Self::Sha512 => 4,
26        }
27    }
28
29    /// Parses a C FFI suite identifier.
30    pub const fn from_ffi_id(value: u32) -> Option<Self> {
31        match value {
32            1 => Some(Self::Sha1),
33            2 => Some(Self::Sha256),
34            3 => Some(Self::Sha384),
35            4 => Some(Self::Sha512),
36            _ => None,
37        }
38    }
39}
40
41/// Public-key DER encoding supplied by a caller.
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum RsaPublicKeyDerEncoding {
44    /// PKCS#1 `RSAPublicKey` DER.
45    Pkcs1,
46    /// X.509 SubjectPublicKeyInfo DER.
47    Spki,
48}
49
50impl RsaPublicKeyDerEncoding {
51    /// Numeric encoding identifier used by the C FFI.
52    pub const fn ffi_id(self) -> u32 {
53        match self {
54            Self::Pkcs1 => 1,
55            Self::Spki => 2,
56        }
57    }
58
59    /// Parses a C FFI encoding identifier.
60    pub const fn from_ffi_id(value: u32) -> Option<Self> {
61        match value {
62            1 => Some(Self::Pkcs1),
63            2 => Some(Self::Spki),
64            _ => None,
65        }
66    }
67}
68
69/// Parameters for RSASSA-PSS verification.
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub struct RsaPssParams {
72    /// Hash applied to the message before PSS verification.
73    pub message_hash: RsaHash,
74    /// Hash used by MGF1 inside PSS.
75    pub mgf1_hash: RsaHash,
76    /// Expected salt length in bytes.
77    pub salt_len: usize,
78}