1 // Copyright 2019 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 #include "tink/subtle/aead_test_util.h"
17
18 #include <string>
19
20 #include "tink/subtle/test_util.h"
21 #include "tink/util/status.h"
22
23 namespace crypto {
24 namespace tink {
25
26 using ::crypto::tink::util::StatusOr;
27
EncryptThenDecrypt(const Aead & encrypter,const Aead & decrypter,absl::string_view message,absl::string_view aad)28 crypto::tink::util::Status EncryptThenDecrypt(const Aead& encrypter,
29 const Aead& decrypter,
30 absl::string_view message,
31 absl::string_view aad) {
32 StatusOr<std::string> encryption_or = encrypter.Encrypt(message, aad);
33 if (!encryption_or.status().ok()) return encryption_or.status();
34 StatusOr<std::string> decryption_or =
35 decrypter.Decrypt(encryption_or.value(), aad);
36 if (!decryption_or.status().ok()) return decryption_or.status();
37 if (decryption_or.value() != message) {
38 return crypto::tink::util::Status(absl::StatusCode::kInternal,
39 "Message/Decryption mismatch");
40 }
41 return util::OkStatus();
42 }
43
EncryptThenDecrypt(const CordAead & encrypter,const CordAead & decrypter,absl::string_view message,absl::string_view aad)44 crypto::tink::util::Status EncryptThenDecrypt(const CordAead& encrypter,
45 const CordAead& decrypter,
46 absl::string_view message,
47 absl::string_view aad) {
48 absl::Cord message_cord = absl::Cord(message);
49 absl::Cord aad_cord = absl::Cord(aad);
50 StatusOr<absl::Cord> encryption_or =
51 encrypter.Encrypt(message_cord, aad_cord);
52 if (!encryption_or.status().ok()) return encryption_or.status();
53 StatusOr<absl::Cord> decryption_or =
54 decrypter.Decrypt(encryption_or.value(), aad_cord);
55 if (!decryption_or.status().ok()) return decryption_or.status();
56 if (decryption_or.value() != message) {
57 return crypto::tink::util::Status(absl::StatusCode::kInternal,
58 "Message/Decryption mismatch");
59 }
60 return util::OkStatus();
61 }
62
63 } // namespace tink
64 } // namespace crypto
65