aster_forge_crypto/
lib.rs1#![cfg_attr(
8 not(test),
9 deny(
10 clippy::unwrap_used,
11 clippy::unreachable,
12 clippy::expect_used,
13 clippy::panic,
14 clippy::unimplemented,
15 clippy::todo
16 )
17)]
18
19pub mod hash;
20pub mod secret_envelope;
21
22pub use hash::{
23 PasswordHashPolicy, PasswordHashVerification, PasswordHashVerificationLimits,
24 PasswordHashWorkFactor, bytes_to_hex, hash_password, hash_password_with_policy,
25 hmac_sha256_hex, new_sha256, sha256_digest_to_hex, sha256_hex, verify_password,
26 verify_password_with_policy,
27};
28pub use secret_envelope::{decrypt_secret, encrypt_secret};
29
30pub type Result<T> = std::result::Result<T, CryptoError>;
32
33#[derive(Debug, thiserror::Error)]
35pub enum CryptoError {
36 #[error("password hash error: {0}")]
38 PasswordHash(String),
39
40 #[error("password hash policy error: {0}")]
42 PasswordHashPolicy(String),
43
44 #[error("password hash parameter {parameter}={actual} exceeds verification limit {maximum}")]
46 PasswordHashVerificationLimit {
47 parameter: &'static str,
49 actual: u64,
51 maximum: u64,
53 },
54
55 #[error("message authentication error: {0}")]
57 MessageAuthentication(String),
58
59 #[error("invalid secret envelope policy")]
61 InvalidSecretEnvelopePolicy,
62
63 #[error("invalid secret envelope")]
65 InvalidSecretEnvelope,
66
67 #[error("unsupported secret envelope version")]
69 UnsupportedSecretEnvelopeVersion,
70
71 #[error("secret envelope key derivation failed")]
73 SecretEnvelopeKeyDerivation,
74
75 #[error("secret envelope encryption failed")]
77 SecretEnvelopeEncryption,
78
79 #[error("secret envelope authentication failed")]
81 SecretEnvelopeAuthentication,
82}
83
84impl CryptoError {
85 pub fn password_hash(error: impl std::fmt::Display) -> Self {
87 Self::PasswordHash(error.to_string())
88 }
89
90 pub fn password_hash_policy(error: impl std::fmt::Display) -> Self {
92 Self::PasswordHashPolicy(error.to_string())
93 }
94
95 pub fn message_authentication(error: impl std::fmt::Display) -> Self {
97 Self::MessageAuthentication(error.to_string())
98 }
99}