tink_core/
aead.rs

1// Copyright 2020 The Tink-Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15////////////////////////////////////////////////////////////////////////////////
16
17//! Authenticated encryption with additional authenticated data.
18
19/// `Aead` is the interface for authenticated encryption with additional authenticated data.
20///
21/// Implementations of this trait are secure against adaptive chosen ciphertext attacks.
22/// Encryption with additional data ensures authenticity and integrity of that data, but not
23/// its secrecy (see [RFC 5116](https://tools.ietf.org/html/rfc5116)).
24pub trait Aead: AeadBoxClone {
25    /// Encrypt plaintext with `additional_data` as additional
26    /// authenticated data. The resulting ciphertext allows for checking
27    /// authenticity and integrity of additional data `additional_data`,
28    /// but there are no guarantees wrt. secrecy of that data.
29    fn encrypt(
30        &self,
31        plaintext: &[u8],
32        additional_data: &[u8],
33    ) -> Result<Vec<u8>, crate::TinkError>;
34
35    /// Decrypt ciphertext with `additional_data` as additional
36    /// authenticated data. The decryption verifies the authenticity and integrity
37    /// of the additional data, but there are no guarantees wrt. secrecy of that data.
38    fn decrypt(
39        &self,
40        ciphertext: &[u8],
41        additional_data: &[u8],
42    ) -> Result<Vec<u8>, crate::TinkError>;
43}
44
45/// Trait bound to indicate that primitive trait objects should support cloning
46/// themselves as trait objects.
47pub trait AeadBoxClone {
48    fn box_clone(&self) -> Box<dyn Aead>;
49}
50
51/// Default implementation of the box-clone trait bound for any underlying
52/// concrete type that implements [`Clone`].
53impl<T> AeadBoxClone for T
54where
55    T: 'static + Aead + Clone,
56{
57    fn box_clone(&self) -> Box<dyn Aead> {
58        Box::new(self.clone())
59    }
60}