| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
#![expect(clippy::missing_errors_doc, reason = "OK here.")] |
| 8 |
|
| 9 |
use std::fmt; |
| 10 |
|
| 11 |
use crate::{ |
| 12 |
constants::{Cipher, Version}, |
| 13 |
err::{sec::SEC_ERROR_BAD_DATA, Error, Res}, |
| 14 |
p11::SymKey, |
| 15 |
}; |
| 16 |
|
| 17 |
pub const AEAD_NULL_TAG: &[u8] = &[0x0a; 16]; |
| 18 |
|
| 19 |
pub struct AeadNull {} |
| 20 |
|
| 21 |
impl AeadNull { |
| 22 |
pub const fn new( |
| 23 |
_version: Version, |
| 24 |
_cipher: Cipher, |
| 25 |
_secret: &SymKey, |
| 26 |
_prefix: &str, |
| 27 |
) -> Res<Self> { |
| 28 |
Ok(Self {}) |
| 29 |
} |
| 30 |
|
| 31 |
#[must_use] |
| 32 |
pub const fn expansion(&self) -> usize { |
| 33 |
AEAD_NULL_TAG.len() |
| 34 |
} |
| 35 |
|
| 36 |
pub fn encrypt<'a>( |
| 37 |
&self, |
| 38 |
_count: u64, |
| 39 |
_aad: &[u8], |
| 40 |
input: &[u8], |
| 41 |
output: &'a mut [u8], |
| 42 |
) -> Res<&'a [u8]> { |
| 43 |
let l = input.len(); |
| 44 |
output[..l].copy_from_slice(input); |
| 45 |
output[l..l + self.expansion()].copy_from_slice(AEAD_NULL_TAG); |
| 46 |
Ok(&output[..l + self.expansion()]) |
| 47 |
} |
| 48 |
|
| 49 |
pub fn encrypt_in_place<'a>( |
| 50 |
&self, |
| 51 |
_count: u64, |
| 52 |
_aad: &[u8], |
| 53 |
data: &'a mut [u8], |
| 54 |
) -> Res<&'a mut [u8]> { |
| 55 |
let pos = data.len() - self.expansion(); |
| 56 |
data[pos..].copy_from_slice(AEAD_NULL_TAG); |
| 57 |
Ok(data) |
| 58 |
} |
| 59 |
|
| 60 |
fn decrypt_check(&self, _count: u64, _aad: &[u8], input: &[u8]) -> Res<usize> { |
| 61 |
if input.len() < self.expansion() { |
| 62 |
return Err(Error::from(SEC_ERROR_BAD_DATA)); |
| 63 |
} |
| 64 |
|
| 65 |
let len_encrypted = input |
| 66 |
.len() |
| 67 |
.checked_sub(self.expansion()) |
| 68 |
.ok_or_else(|| Error::from(SEC_ERROR_BAD_DATA))?; |
| 69 |
// Check that: |
| 70 |
// 1) expansion is all zeros and |
| 71 |
// 2) if the encrypted data is also supplied that at least some values are no zero |
| 72 |
// (otherwise padding will be interpreted as a valid packet) |
| 73 |
if &input[len_encrypted..] == AEAD_NULL_TAG |
| 74 |
&& (len_encrypted == 0 || input[..len_encrypted].iter().any(|x| *x != 0x0)) |
| 75 |
{ |
| 76 |
Ok(len_encrypted) |
| 77 |
} else { |
| 78 |
Err(Error::from(SEC_ERROR_BAD_DATA)) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
pub fn decrypt<'a>( |
| 83 |
&self, |
| 84 |
count: u64, |
| 85 |
aad: &[u8], |
| 86 |
input: &[u8], |
| 87 |
output: &'a mut [u8], |
| 88 |
) -> Res<&'a [u8]> { |
| 89 |
self.decrypt_check(count, aad, input).map(|len| { |
| 90 |
output[..len].copy_from_slice(&input[..len]); |
| 91 |
&output[..len] |
| 92 |
}) |
| 93 |
} |
| 94 |
|
| 95 |
pub fn decrypt_in_place<'a>( |
| 96 |
&self, |
| 97 |
count: u64, |
| 98 |
aad: &[u8], |
| 99 |
data: &'a mut [u8], |
| 100 |
) -> Res<&'a mut [u8]> { |
| 101 |
self.decrypt_check(count, aad, data) |
| 102 |
.map(move |len| &mut data[..len]) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl fmt::Debug for AeadNull { |
| 107 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 108 |
write!(f, "[NULL AEAD]") |
| 109 |
} |
| 110 |
} |