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 #ifndef TINK_AEAD_INTERNAL_SSL_AEAD_H_ 17 #define TINK_AEAD_INTERNAL_SSL_AEAD_H_ 18 19 #include <cstdint> 20 #include <memory> 21 22 #include "absl/strings/string_view.h" 23 #include "absl/types/span.h" 24 #include "tink/util/secret_data.h" 25 #include "tink/util/statusor.h" 26 27 namespace crypto { 28 namespace tink { 29 namespace internal { 30 31 // Tag sizes. 32 ABSL_CONST_INIT extern const int kXchacha20Poly1305TagSizeInBytes; 33 ABSL_CONST_INIT extern const int kAesGcmTagSizeInBytes; 34 ABSL_CONST_INIT extern const int kAesGcmSivTagSizeInBytes; 35 36 // Interface for one-shot AEAD crypters. 37 class SslOneShotAead { 38 public: 39 virtual ~SslOneShotAead() = default; 40 41 // Returns the size of the ciphertext from `plaintext_length`. 42 virtual int64_t CiphertextSize(int64_t plaintext_length) const = 0; 43 44 // Encrypts `plaintext` with `associated_data` and `iv`, and writes the output 45 // to `out`. The implementation places both the raw ciphertext and the 46 // resulting tag in `out`, so the caller must make sure it has sufficient 47 // capacity. There should be no overlap between `plaintext` and `out`. In 48 // particular, in-place encryption is not supported. 49 virtual util::StatusOr<int64_t> Encrypt(absl::string_view plaintext, 50 absl::string_view associated_data, 51 absl::string_view iv, 52 absl::Span<char> out) const = 0; 53 54 // Returns the size of the plaintext given `ciphertext_length`. This is always 55 // >= 0. 56 virtual int64_t PlaintextSize(int64_t ciphertext_length) const = 0; 57 58 // Decrypts `ciphertext` with `associated_data` and `iv`, and writes the 59 // plaintext to `out`. `ciphertext` contains the raw ciphertext and the tag. 60 // There should be no overlap between `ciphertext` and `out`. In particular, 61 // in-place decryption is not supported. 62 virtual util::StatusOr<int64_t> Decrypt(absl::string_view ciphertext, 63 absl::string_view associated_data, 64 absl::string_view iv, 65 absl::Span<char> out) const = 0; 66 }; 67 68 // Create one-shot crypters for the supported algorithms. 69 util::StatusOr<std::unique_ptr<SslOneShotAead>> CreateAesGcmOneShotCrypter( 70 const util::SecretData &key); 71 util::StatusOr<std::unique_ptr<SslOneShotAead>> CreateAesGcmSivOneShotCrypter( 72 const util::SecretData &key); 73 util::StatusOr<std::unique_ptr<SslOneShotAead>> 74 CreateXchacha20Poly1305OneShotCrypter(const util::SecretData &key); 75 76 } // namespace internal 77 } // namespace tink 78 } // namespace crypto 79 80 #endif // TINK_AEAD_INTERNAL_SSL_AEAD_H_ 81