Skip to main content

crypto_core/error/
rng.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5/// Purpose of the random output being generated.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum RngOutputKind {
9    /// Generic random bytes with no fixed length.
10    Generic,
11    /// A 12-byte AEAD nonce.
12    AeadNonce12,
13    /// A 16-byte Argon2 salt.
14    Argon2Salt16,
15    /// A 32-byte Argon2 salt.
16    Argon2Salt32,
17    /// A 32-byte AES-256-GCM key.
18    Aes256GcmKey,
19    /// A 64-byte ML-KEM-1024 seed.
20    MlKem1024Seed,
21    /// A 32-byte ML-DSA-87 seed.
22    MlDsa87Seed,
23    /// A 32-byte Ed25519 private seed.
24    Ed25519Seed,
25    /// One 16-byte SLH-DSA-SHA2-128s key-generation seed component.
26    SlhDsaSha2_128sSeed,
27}
28
29impl core::fmt::Display for RngOutputKind {
30    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
31        let detail = match self {
32            RngOutputKind::Generic => "random bytes",
33            RngOutputKind::AeadNonce12 => "AEAD nonce",
34            RngOutputKind::Argon2Salt16 => "Argon2 16-byte salt",
35            RngOutputKind::Argon2Salt32 => "Argon2 32-byte salt",
36            RngOutputKind::Aes256GcmKey => "AES-256-GCM key",
37            RngOutputKind::MlKem1024Seed => "ML-KEM-1024 seed",
38            RngOutputKind::MlDsa87Seed => "ML-DSA-87 seed",
39            RngOutputKind::Ed25519Seed => "Ed25519 seed",
40            RngOutputKind::SlhDsaSha2_128sSeed => "SLH-DSA-SHA2-128s seed",
41        };
42        write!(f, "{detail}")
43    }
44}
45
46/// Specific reason secure random generation failed.
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48#[non_exhaustive]
49pub enum RngFailureKind {
50    /// The system entropy source was unavailable.
51    EntropyUnavailable,
52    /// The requested output length was unacceptable.
53    InvalidOutputLength,
54}
55
56impl core::fmt::Display for RngFailureKind {
57    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
58        let detail = match self {
59            RngFailureKind::EntropyUnavailable => "entropy unavailable",
60            RngFailureKind::InvalidOutputLength => "invalid output length",
61        };
62        write!(f, "{detail}")
63    }
64}
65
66/// Specific reason a constant-time comparison did not match.
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68#[non_exhaustive]
69pub enum ConstantTimeFailureKind {
70    /// The two inputs had different lengths.
71    LengthMismatch,
72    /// The two inputs had equal length but unequal contents.
73    NotEqual,
74}
75
76impl core::fmt::Display for ConstantTimeFailureKind {
77    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
78        let detail = match self {
79            ConstantTimeFailureKind::LengthMismatch => "length mismatch",
80            ConstantTimeFailureKind::NotEqual => "not equal",
81        };
82        write!(f, "{detail}")
83    }
84}