xref: /aosp_15_r20/external/tink/cc/aead/internal/zero_copy_aead.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2021 Google LLC
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 #ifndef TINK_AEAD_INTERNAL_ZERO_COPY_AEAD_H_
18 #define TINK_AEAD_INTERNAL_ZERO_COPY_AEAD_H_
19 
20 #include <cstdint>
21 
22 #include "absl/strings/string_view.h"
23 #include "tink/util/statusor.h"
24 
25 namespace crypto {
26 namespace tink {
27 namespace internal {
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // The interface for authenticated encryption with associated data.
31 // Implementations of this interface are secure against adaptive
32 // chosen ciphertext attacks. Encryption with associated data ensures
33 // authenticity and integrity of that data, but not its secrecy.
34 // (see RFC 5116, https://tools.ietf.org/html/rfc5116)
35 //
36 // This implementation expects the user to provide a contiguous block
37 // of memory and writes the Encrypt and Decrypt results in the block.
38 // This requires the user to avoid mutating this block during calls to
39 // Encrypt and Decrypt and aims to reduce the latency associated with
40 // copying strings from one location to another.
41 class ZeroCopyAead {
42  public:
43   virtual ~ZeroCopyAead() = default;
44 
45   // Returns the maximum buffer size needed for encryption. The actual
46   // size of the written cypertext may be smaller.
47   virtual int64_t MaxEncryptionSize(int64_t plaintext_size) const = 0;
48 
49   // Encrypts `plaintext` with `associated_data` as associated data,
50   // and returns the size of the ciphertext that is written in `buffer`.
51   // `buffer` size must be at least MaxEncryptionSize to guarantee
52   // enough space for encryption.
53   // The ciphertext allows for checking authenticity and integrity
54   // of the associated data, but does not guarantee its secrecy.
55   virtual crypto::tink::util::StatusOr<int64_t> Encrypt(
56       absl::string_view plaintext, absl::string_view associated_data,
57       absl::Span<char> buffer) const = 0;
58 
59   // Returns an upper bound on the size of the plaintext based on
60   // `ciphertext_size`. The actual size of the written plaintext may be smaller.
61   // The returned value is always >= 0.
62   virtual int64_t MaxDecryptionSize(int64_t ciphertext_size) const = 0;
63 
64   // Decrypts `ciphertext` with `associated_data` as associated data,
65   // and returns the size of the plaintext that is written in `buffer`.
66   // `buffer` size must be at least MaxDecryptionSize to guarantee
67   // enough space for decryption.
68   // If the authentication tag does not validate, `buffer` is zeroed.
69   // The decryption verifies the authenticity and integrity of the
70   // associated data, but there are no guarantees wrt. secrecy of
71   // that data.
72   virtual crypto::tink::util::StatusOr<int64_t> Decrypt(
73       absl::string_view ciphertext, absl::string_view associated_data,
74       absl::Span<char> buffer) const = 0;
75 };
76 
77 }  // namespace internal
78 }  // namespace tink
79 }  // namespace crypto
80 
81 #endif  // TINK_AEAD_INTERNAL_ZERO_COPY_AEAD_H_
82