xref: /aosp_15_r20/external/tink/cc/streamingaead/streaming_aead_config_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2019 Google Inc.
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/streamingaead/streaming_aead_config.h"
18 
19 #include <list>
20 #include <sstream>
21 #include <utility>
22 
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 #include "absl/memory/memory.h"
26 #include "absl/status/status.h"
27 #include "tink/config/tink_fips.h"
28 #include "tink/keyset_handle.h"
29 #include "tink/registry.h"
30 #include "tink/streaming_aead.h"
31 #include "tink/streamingaead/aes_ctr_hmac_streaming_key_manager.h"
32 #include "tink/streamingaead/aes_gcm_hkdf_streaming_key_manager.h"
33 #include "tink/streamingaead/streaming_aead_key_templates.h"
34 #include "tink/util/status.h"
35 #include "tink/util/test_matchers.h"
36 #include "tink/util/test_util.h"
37 
38 namespace crypto {
39 namespace tink {
40 namespace {
41 
42 using ::crypto::tink::test::DummyStreamingAead;
43 using ::crypto::tink::test::IsOk;
44 using ::crypto::tink::test::StatusIs;
45 
46 class StreamingAeadConfigTest : public ::testing::Test {
47  protected:
SetUp()48   void SetUp() override { Registry::Reset(); }
49 };
50 
TEST_F(StreamingAeadConfigTest,Basic)51 TEST_F(StreamingAeadConfigTest, Basic) {
52   if (IsFipsModeEnabled()) {
53     GTEST_SKIP() << "Not supported in FIPS-only mode";
54   }
55 
56   EXPECT_THAT(Registry::get_key_manager<StreamingAead>(
57                   AesGcmHkdfStreamingKeyManager().get_key_type())
58                   .status(),
59               StatusIs(absl::StatusCode::kNotFound));
60   EXPECT_THAT(Registry::get_key_manager<StreamingAead>(
61                   AesCtrHmacStreamingKeyManager().get_key_type())
62                   .status(),
63               StatusIs(absl::StatusCode::kNotFound));
64   EXPECT_THAT(StreamingAeadConfig::Register(), IsOk());
65   EXPECT_THAT(Registry::get_key_manager<StreamingAead>(
66                   AesGcmHkdfStreamingKeyManager().get_key_type())
67                   .status(),
68               IsOk());
69   EXPECT_THAT(Registry::get_key_manager<StreamingAead>(
70                   AesCtrHmacStreamingKeyManager().get_key_type())
71                   .status(),
72               IsOk());
73 }
74 
75 // Tests that the StreamingAeadWrapper has been properly registered
76 // and we can wrap primitives.
TEST_F(StreamingAeadConfigTest,WrappersRegistered)77 TEST_F(StreamingAeadConfigTest, WrappersRegistered) {
78   if (IsFipsModeEnabled()) {
79     GTEST_SKIP() << "Not supported in FIPS-only mode";
80   }
81 
82   ASSERT_TRUE(StreamingAeadConfig::Register().ok());
83 
84   google::crypto::tink::KeysetInfo::KeyInfo key_info;
85   key_info.set_status(google::crypto::tink::KeyStatusType::ENABLED);
86   key_info.set_key_id(1234);
87   key_info.set_output_prefix_type(google::crypto::tink::OutputPrefixType::RAW);
88   auto primitive_set = absl::make_unique<PrimitiveSet<StreamingAead>>();
89   ASSERT_THAT(
90       primitive_set->set_primary(
91           primitive_set
92               ->AddPrimitive(absl::make_unique<DummyStreamingAead>("dummy"),
93                              key_info)
94               .value()),
95       IsOk());
96 
97   auto primitive_result = Registry::Wrap(std::move(primitive_set));
98   ASSERT_TRUE(primitive_result.ok()) << primitive_result.status();
99 }
100 
101 // FIPS-only mode tests
TEST_F(StreamingAeadConfigTest,RegisterNonFipsTemplates)102 TEST_F(StreamingAeadConfigTest, RegisterNonFipsTemplates) {
103   if (!IsFipsModeEnabled()) {
104     GTEST_SKIP() << "Only supported in FIPS-only mode";
105   }
106 
107   EXPECT_THAT(StreamingAeadConfig::Register(), IsOk());
108 
109   // Check that we can not retrieve non-FIPS keyset handle
110   std::list<google::crypto::tink::KeyTemplate> non_fips_key_templates;
111   non_fips_key_templates.push_back(
112       StreamingAeadKeyTemplates::Aes128CtrHmacSha256Segment4KB());
113   non_fips_key_templates.push_back(
114       StreamingAeadKeyTemplates::Aes128GcmHkdf4KB());
115   non_fips_key_templates.push_back(
116       StreamingAeadKeyTemplates::Aes256CtrHmacSha256Segment4KB());
117   non_fips_key_templates.push_back(
118       StreamingAeadKeyTemplates::Aes256GcmHkdf1MB());
119   non_fips_key_templates.push_back(
120       StreamingAeadKeyTemplates::Aes256GcmHkdf4KB());
121 
122   for (auto key_template : non_fips_key_templates) {
123     EXPECT_THAT(KeysetHandle::GenerateNew(key_template).status(),
124                 StatusIs(absl::StatusCode::kNotFound));
125   }
126 }
127 
128 }  // namespace
129 }  // namespace tink
130 }  // namespace crypto
131