xref: /aosp_15_r20/external/tink/cc/aead/internal/ssl_aead.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1*e7b1675dSTing-Kang Chang // Copyright 2021 Google LLC.
2*e7b1675dSTing-Kang Chang //
3*e7b1675dSTing-Kang Chang // Licensed under the Apache License, Version 2.0 (the "License");
4*e7b1675dSTing-Kang Chang // you may not use this file except in compliance with the License.
5*e7b1675dSTing-Kang Chang // You may obtain a copy of the License at
6*e7b1675dSTing-Kang Chang //
7*e7b1675dSTing-Kang Chang //     http://www.apache.org/licenses/LICENSE-2.0
8*e7b1675dSTing-Kang Chang //
9*e7b1675dSTing-Kang Chang // Unless required by applicable law or agreed to in writing, software
10*e7b1675dSTing-Kang Chang // distributed under the License is distributed on an "AS IS" BASIS,
11*e7b1675dSTing-Kang Chang // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e7b1675dSTing-Kang Chang // See the License for the specific language governing permissions and
13*e7b1675dSTing-Kang Chang // limitations under the License.
14*e7b1675dSTing-Kang Chang //
15*e7b1675dSTing-Kang Chang ///////////////////////////////////////////////////////////////////////////////
16*e7b1675dSTing-Kang Chang #ifndef TINK_AEAD_INTERNAL_SSL_AEAD_H_
17*e7b1675dSTing-Kang Chang #define TINK_AEAD_INTERNAL_SSL_AEAD_H_
18*e7b1675dSTing-Kang Chang 
19*e7b1675dSTing-Kang Chang #include <cstdint>
20*e7b1675dSTing-Kang Chang #include <memory>
21*e7b1675dSTing-Kang Chang 
22*e7b1675dSTing-Kang Chang #include "absl/strings/string_view.h"
23*e7b1675dSTing-Kang Chang #include "absl/types/span.h"
24*e7b1675dSTing-Kang Chang #include "tink/util/secret_data.h"
25*e7b1675dSTing-Kang Chang #include "tink/util/statusor.h"
26*e7b1675dSTing-Kang Chang 
27*e7b1675dSTing-Kang Chang namespace crypto {
28*e7b1675dSTing-Kang Chang namespace tink {
29*e7b1675dSTing-Kang Chang namespace internal {
30*e7b1675dSTing-Kang Chang 
31*e7b1675dSTing-Kang Chang // Tag sizes.
32*e7b1675dSTing-Kang Chang ABSL_CONST_INIT extern const int kXchacha20Poly1305TagSizeInBytes;
33*e7b1675dSTing-Kang Chang ABSL_CONST_INIT extern const int kAesGcmTagSizeInBytes;
34*e7b1675dSTing-Kang Chang ABSL_CONST_INIT extern const int kAesGcmSivTagSizeInBytes;
35*e7b1675dSTing-Kang Chang 
36*e7b1675dSTing-Kang Chang // Interface for one-shot AEAD crypters.
37*e7b1675dSTing-Kang Chang class SslOneShotAead {
38*e7b1675dSTing-Kang Chang  public:
39*e7b1675dSTing-Kang Chang   virtual ~SslOneShotAead() = default;
40*e7b1675dSTing-Kang Chang 
41*e7b1675dSTing-Kang Chang   // Returns the size of the ciphertext from `plaintext_length`.
42*e7b1675dSTing-Kang Chang   virtual int64_t CiphertextSize(int64_t plaintext_length) const = 0;
43*e7b1675dSTing-Kang Chang 
44*e7b1675dSTing-Kang Chang   // Encrypts `plaintext` with `associated_data` and `iv`, and writes the output
45*e7b1675dSTing-Kang Chang   // to `out`. The implementation places both the raw ciphertext and the
46*e7b1675dSTing-Kang Chang   // resulting tag in `out`, so the caller must make sure it has sufficient
47*e7b1675dSTing-Kang Chang   // capacity. There should be no overlap between `plaintext` and `out`. In
48*e7b1675dSTing-Kang Chang   // particular, in-place encryption is not supported.
49*e7b1675dSTing-Kang Chang   virtual util::StatusOr<int64_t> Encrypt(absl::string_view plaintext,
50*e7b1675dSTing-Kang Chang                                           absl::string_view associated_data,
51*e7b1675dSTing-Kang Chang                                           absl::string_view iv,
52*e7b1675dSTing-Kang Chang                                           absl::Span<char> out) const = 0;
53*e7b1675dSTing-Kang Chang 
54*e7b1675dSTing-Kang Chang   // Returns the size of the plaintext given `ciphertext_length`. This is always
55*e7b1675dSTing-Kang Chang   // >= 0.
56*e7b1675dSTing-Kang Chang   virtual int64_t PlaintextSize(int64_t ciphertext_length) const = 0;
57*e7b1675dSTing-Kang Chang 
58*e7b1675dSTing-Kang Chang   // Decrypts `ciphertext` with `associated_data` and `iv`, and writes the
59*e7b1675dSTing-Kang Chang   // plaintext to `out`. `ciphertext` contains the raw ciphertext and the tag.
60*e7b1675dSTing-Kang Chang   // There should be no overlap between `ciphertext` and `out`. In particular,
61*e7b1675dSTing-Kang Chang   // in-place decryption is not supported.
62*e7b1675dSTing-Kang Chang   virtual util::StatusOr<int64_t> Decrypt(absl::string_view ciphertext,
63*e7b1675dSTing-Kang Chang                                           absl::string_view associated_data,
64*e7b1675dSTing-Kang Chang                                           absl::string_view iv,
65*e7b1675dSTing-Kang Chang                                           absl::Span<char> out) const = 0;
66*e7b1675dSTing-Kang Chang };
67*e7b1675dSTing-Kang Chang 
68*e7b1675dSTing-Kang Chang // Create one-shot crypters for the supported algorithms.
69*e7b1675dSTing-Kang Chang util::StatusOr<std::unique_ptr<SslOneShotAead>> CreateAesGcmOneShotCrypter(
70*e7b1675dSTing-Kang Chang     const util::SecretData &key);
71*e7b1675dSTing-Kang Chang util::StatusOr<std::unique_ptr<SslOneShotAead>> CreateAesGcmSivOneShotCrypter(
72*e7b1675dSTing-Kang Chang     const util::SecretData &key);
73*e7b1675dSTing-Kang Chang util::StatusOr<std::unique_ptr<SslOneShotAead>>
74*e7b1675dSTing-Kang Chang CreateXchacha20Poly1305OneShotCrypter(const util::SecretData &key);
75*e7b1675dSTing-Kang Chang 
76*e7b1675dSTing-Kang Chang }  // namespace internal
77*e7b1675dSTing-Kang Chang }  // namespace tink
78*e7b1675dSTing-Kang Chang }  // namespace crypto
79*e7b1675dSTing-Kang Chang 
80*e7b1675dSTing-Kang Chang #endif  // TINK_AEAD_INTERNAL_SSL_AEAD_H_
81