libcrux_traits/aead/
arrayref.rs1use libcrux_secrets::U8;
6
7pub trait Aead<const KEY_LEN: usize, const TAG_LEN: usize, const NONCE_LEN: usize> {
14 fn keygen(key: &mut [U8; KEY_LEN], rand: &[U8; KEY_LEN]) -> Result<(), KeyGenError>;
16
17 fn encrypt(
20 ciphertext: &mut [u8],
21 tag: &mut [U8; TAG_LEN],
22 key: &[U8; KEY_LEN],
23 nonce: &[U8; NONCE_LEN],
24 aad: &[u8],
25 plaintext: &[U8],
26 ) -> Result<(), EncryptError>;
27
28 fn decrypt(
31 plaintext: &mut [U8],
32 key: &[U8; KEY_LEN],
33 nonce: &[U8; NONCE_LEN],
34 aad: &[u8],
35 ciphertext: &[u8],
36 tag: &[U8; TAG_LEN],
37 ) -> Result<(), DecryptError>;
38}
39
40pub struct KeyGenError;
42
43#[derive(Debug, PartialEq, Eq)]
45pub enum EncryptError {
46 WrongCiphertextLength,
48
49 PlaintextTooLong,
51
52 AadTooLong,
54
55 Unknown,
57}
58
59#[derive(Debug, PartialEq, Eq)]
61pub enum DecryptError {
62 InvalidTag,
65
66 WrongPlaintextLength,
68
69 PlaintextTooLong,
71
72 AadTooLong,
74
75 Unknown,
77}
78
79impl core::fmt::Display for EncryptError {
80 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
81 let text = match self {
82 EncryptError::WrongCiphertextLength => "ciphertext buffer has wrong length",
83 EncryptError::PlaintextTooLong => {
84 "plaintext is too long for algorithm or implementation"
85 }
86 EncryptError::AadTooLong => "aad is too long for algorithm or implementation",
87 EncryptError::Unknown => "an unknown error occurred",
88 };
89
90 f.write_str(text)
91 }
92}
93
94impl core::fmt::Display for DecryptError {
95 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
96 let text = match self {
97 DecryptError::InvalidTag => "invalid authentication tag",
98 DecryptError::WrongPlaintextLength => "plaintext buffer has wrong length",
99 DecryptError::PlaintextTooLong => {
100 "plaintext is too long for algorithm or implementation"
101 }
102 DecryptError::AadTooLong => "aad is too long for algorithm or implementation",
103 DecryptError::Unknown => "an unknown error occurred",
104 };
105
106 f.write_str(text)
107 }
108}
109
110#[cfg(feature = "error-in-core")]
111mod error_in_core {
112 impl core::error::Error for super::EncryptError {}
113 impl core::error::Error for super::DecryptError {}
114}