xref: /aosp_15_r20/external/tink/cc/core/crypto_format_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2017 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/crypto_format.h"
18 
19 #include "gtest/gtest.h"
20 #include "proto/tink.pb.h"
21 
22 using google::crypto::tink::KeysetInfo;
23 using google::crypto::tink::OutputPrefixType;
24 
25 namespace crypto {
26 namespace tink {
27 namespace {
28 
29 // static
30 
TestNonRawPrefix(const KeysetInfo::KeyInfo & key_info,int prefix_size,uint8_t prefix_first_byte)31 void TestNonRawPrefix(const KeysetInfo::KeyInfo& key_info, int prefix_size,
32                       uint8_t prefix_first_byte) {
33   auto prefix_result =
34       CryptoFormat::GetOutputPrefix(key_info);
35   EXPECT_TRUE(prefix_result.ok()) << prefix_result.status();
36   auto prefix = prefix_result.value();
37   EXPECT_EQ(prefix_size, prefix.length());
38   EXPECT_EQ(prefix_first_byte, prefix[0]);
39   // key_id should follow in BigEndian order
40   for (int i = 1; i <= 4; i++) {
41     EXPECT_EQ(0xff & (key_info.key_id() >> ((4 - i) * 8)), 0xff & prefix[i])
42         << "Failed at byte " << i << ".";
43   }
44 }
45 
46 class CryptoFormatTest : public ::testing::Test {
47 };
48 
TEST_F(CryptoFormatTest,testConstants)49 TEST_F(CryptoFormatTest, testConstants) {
50   EXPECT_EQ(5, CryptoFormat::kNonRawPrefixSize);
51   EXPECT_EQ(0, CryptoFormat::kRawPrefixSize);
52   EXPECT_EQ(0x01, CryptoFormat::kTinkStartByte);
53   EXPECT_EQ(0x00, CryptoFormat::kLegacyStartByte);
54   EXPECT_EQ("", CryptoFormat::kRawPrefix);
55 }
56 
TEST_F(CryptoFormatTest,testTinkPrefix)57 TEST_F(CryptoFormatTest, testTinkPrefix) {
58   uint32_t key_id = 263829;
59   KeysetInfo::KeyInfo key_info;
60   key_info.set_output_prefix_type(OutputPrefixType::TINK);
61   key_info.set_key_id(key_id);
62 
63   TestNonRawPrefix(key_info, CryptoFormat::kNonRawPrefixSize,
64                    CryptoFormat::kTinkStartByte);
65 }
66 
TEST_F(CryptoFormatTest,testLegacyPrefix)67 TEST_F(CryptoFormatTest, testLegacyPrefix) {
68   uint32_t key_id = 8327256;
69   KeysetInfo::KeyInfo key_info;
70   key_info.set_output_prefix_type(OutputPrefixType::LEGACY);
71   key_info.set_key_id(key_id);
72 
73   TestNonRawPrefix(key_info, CryptoFormat::kNonRawPrefixSize,
74                    CryptoFormat::kLegacyStartByte);
75 }
76 
TEST_F(CryptoFormatTest,testCrunchyPrefix)77 TEST_F(CryptoFormatTest, testCrunchyPrefix) {
78   uint32_t key_id = 1223345;
79   KeysetInfo::KeyInfo key_info;
80   key_info.set_output_prefix_type(OutputPrefixType::CRUNCHY);
81   key_info.set_key_id(key_id);
82 
83   TestNonRawPrefix(key_info, CryptoFormat::kNonRawPrefixSize,
84                    CryptoFormat::kLegacyStartByte);
85 }
86 
TEST_F(CryptoFormatTest,testRawPrefix)87 TEST_F(CryptoFormatTest, testRawPrefix) {
88   uint32_t key_id = 7662387;
89   KeysetInfo::KeyInfo key_info;
90   key_info.set_output_prefix_type(OutputPrefixType::RAW);
91   key_info.set_key_id(key_id);
92   auto prefix_result =
93       CryptoFormat::GetOutputPrefix(key_info);
94   EXPECT_TRUE(prefix_result.ok()) << prefix_result.status();
95   auto prefix = prefix_result.value();
96   EXPECT_EQ(CryptoFormat::kRawPrefixSize, prefix.length());
97 }
98 
99 }  // namespace
100 }  // namespace tink
101 }  // namespace crypto
102