xref: /aosp_15_r20/external/tink/cc/daead/subtle/aead_or_daead.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2020 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 #include "tink/daead/subtle/aead_or_daead.h"
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "absl/functional/bind_front.h"
24 
25 namespace crypto {
26 namespace tink {
27 namespace subtle {
28 namespace {
29 
30 // Functor implementing Encryption of a given plaintext.
31 struct EncryptFunctor {
operator ()crypto::tink::subtle::__anon0e120dea0111::EncryptFunctor32   crypto::tink::util::StatusOr<std::string> operator()(
33       absl::string_view plaintext, absl::string_view associated_data,
34       const std::unique_ptr<const Aead>& aead_primitive) {
35     return aead_primitive->Encrypt(plaintext, associated_data);
36   }
operator ()crypto::tink::subtle::__anon0e120dea0111::EncryptFunctor37   crypto::tink::util::StatusOr<std::string> operator()(
38       absl::string_view plaintext, absl::string_view associated_data,
39       const std::unique_ptr<const DeterministicAead>& aead_primitive) {
40     return aead_primitive->EncryptDeterministically(plaintext, associated_data);
41   }
42 };
43 
44 // Functor implementing Decryption of a given ciphertext.
45 struct DecryptFunctor {
operator ()crypto::tink::subtle::__anon0e120dea0111::DecryptFunctor46   crypto::tink::util::StatusOr<std::string> operator()(
47       absl::string_view ciphertext, absl::string_view associated_data,
48       const std::unique_ptr<const Aead>& aead_primitive) {
49     return aead_primitive->Decrypt(ciphertext, associated_data);
50   }
operator ()crypto::tink::subtle::__anon0e120dea0111::DecryptFunctor51   crypto::tink::util::StatusOr<std::string> operator()(
52       absl::string_view ciphertext, absl::string_view associated_data,
53       const std::unique_ptr<const DeterministicAead>& aead_primitive) {
54     return aead_primitive->DecryptDeterministically(ciphertext,
55                                                     associated_data);
56   }
57 };
58 }  // namespace
59 
Encrypt(absl::string_view plaintext,absl::string_view associated_data) const60 crypto::tink::util::StatusOr<std::string> AeadOrDaead::Encrypt(
61     absl::string_view plaintext, absl::string_view associated_data) const {
62   return absl::visit(
63       absl::bind_front(EncryptFunctor(), plaintext, associated_data),
64       primitive_variant_);
65 }
66 
Decrypt(absl::string_view ciphertext,absl::string_view associated_data) const67 crypto::tink::util::StatusOr<std::string> AeadOrDaead::Decrypt(
68     absl::string_view ciphertext, absl::string_view associated_data) const {
69   return absl::visit(
70       absl::bind_front(DecryptFunctor(), ciphertext, associated_data),
71       primitive_variant_);
72 }
73 
74 }  // namespace subtle
75 }  // namespace tink
76 }  // namespace crypto
77