xref: /aosp_15_r20/external/tink/cc/aead/internal/aead_util_test.cc (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 #include "tink/aead/internal/aead_util.h"
17 
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
20 #include "openssl/evp.h"
21 #include "tink/util/test_matchers.h"
22 
23 namespace crypto {
24 namespace tink {
25 namespace internal {
26 namespace {
27 
28 using ::crypto::tink::test::IsOk;
29 using ::crypto::tink::test::IsOkAndHolds;
30 using ::testing::IsFalse;
31 using ::testing::IsTrue;
32 using ::testing::Not;
33 
TEST(AeadUtilTest,GetAesGcmCipherForKeySize)34 TEST(AeadUtilTest, GetAesGcmCipherForKeySize) {
35   for (int i = 0; i < 64; i++) {
36     util::StatusOr<const EVP_CIPHER*> cipher = GetAesGcmCipherForKeySize(i);
37     if (i == 16) {
38       EXPECT_THAT(cipher, IsOkAndHolds(EVP_aes_128_gcm()));
39     } else if (i == 32) {
40       EXPECT_THAT(cipher, IsOkAndHolds(EVP_aes_256_gcm()));
41     } else {
42       EXPECT_THAT(cipher, Not(IsOk()));
43     }
44   }
45 }
46 
TEST(AeadUtilTest,SupportedKmsEnvelopeAeadDekKeyTypes)47 TEST(AeadUtilTest, SupportedKmsEnvelopeAeadDekKeyTypes) {
48   EXPECT_THAT(IsSupportedKmsEnvelopeAeadDekKeyType(
49                   "type.googleapis.com/google.crypto.tink.AesGcmKey"),
50               IsTrue());
51   EXPECT_THAT(IsSupportedKmsEnvelopeAeadDekKeyType(
52                   "type.googleapis.com/google.crypto.tink.KmsEnvelopeAeadKey"),
53               IsFalse());
54 }
55 
56 #ifdef OPENSSL_IS_BORINGSSL
57 
TEST(AeadUtilTest,GetAesAeadForKeySize)58 TEST(AeadUtilTest, GetAesAeadForKeySize) {
59   for (int i = 0; i < 64; i++) {
60     util::StatusOr<const EVP_AEAD*> cipher = GetAesGcmAeadForKeySize(i);
61     if (i == 16) {
62       EXPECT_THAT(cipher, IsOkAndHolds(EVP_aead_aes_128_gcm()));
63     } else if (i == 32) {
64       EXPECT_THAT(cipher, IsOkAndHolds(EVP_aead_aes_256_gcm()));
65     } else {
66       EXPECT_THAT(cipher, Not(IsOk()));
67     }
68   }
69 }
70 
TEST(AeadUtilTest,GetAesGcmSivAeadCipherForKeySize)71 TEST(AeadUtilTest, GetAesGcmSivAeadCipherForKeySize) {
72   for (int i = 0; i < 64; i++) {
73     util::StatusOr<const EVP_AEAD*> cipher =
74         GetAesGcmSivAeadCipherForKeySize(i);
75     if (i == 16) {
76       EXPECT_THAT(cipher, IsOkAndHolds(EVP_aead_aes_128_gcm_siv()));
77     } else if (i == 32) {
78       EXPECT_THAT(cipher, IsOkAndHolds(EVP_aead_aes_256_gcm_siv()));
79     } else {
80       EXPECT_THAT(cipher, Not(IsOk()));
81     }
82   }
83 }
84 
85 #endif
86 
87 }  // namespace
88 }  // namespace internal
89 }  // namespace tink
90 }  // namespace crypto
91