xref: /aosp_15_r20/external/federated-compute/fcp/secagg/shared/aes_key_test.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
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 #include "fcp/secagg/shared/aes_key.h"
16 
17 #include <string>
18 
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "absl/strings/str_cat.h"
22 #include "fcp/secagg/shared/math.h"
23 #include "fcp/secagg/shared/shamir_secret_sharing.h"
24 
25 namespace fcp {
26 namespace secagg {
27 namespace {
28 
29 using ::testing::Eq;
30 
31 // For testing purposes, make an AesKey out of a string.
AesKeyFromString(const std::string & key)32 AesKey AesKeyFromString(const std::string& key) {
33   return AesKey(reinterpret_cast<const uint8_t*>(key.c_str()),
34                 static_cast<int>(key.size()));
35 }
36 
37 // Suppose the randomly chosen key can be expressed in bit_length <= 128 bits.
38 // Java did not express the key in 128 bits, but rather will have used
39 // (bit_length + 1) bits. The extra bit is the highest-order bit, and is a sign
40 // bit, guaranteed to be 0. The next highest-order bit is guaranteed to be 1.
JavaStyleKey(int bit_length)41 std::string JavaStyleKey(int bit_length) {
42   EXPECT_TRUE(0 < bit_length && bit_length <= 128);
43   std::string key = "16 byte test key";
44   int byte_with_sign_bit = (127 - bit_length) / 8;
45   int pos_of_sign_bit = (127 - bit_length) % 8;
46   if (bit_length == 128) {
47     pos_of_sign_bit = 7;
48     key = absl::StrCat("\0", key);
49   } else {
50     key.erase(0, byte_with_sign_bit);
51   }
52   // Make sure the high-order bit is the sign bit 0, and the next highest-order
53   // bit is 1.
54   key[0] = static_cast<char>(127 >> pos_of_sign_bit);
55   if (pos_of_sign_bit == 7) {
56     key[1] = static_cast<char>(255);
57   }
58   return key;
59 }
60 
TEST(AesKeyTest,CreateFromSharesHandles32BKeys)61 TEST(AesKeyTest, CreateFromSharesHandles32BKeys) {
62   AesKey original_key = AesKeyFromString("32 byte AES key for testing only");
63   ShamirSecretSharing shamir;
64   auto shares = shamir.Share(5, 7, original_key);
65   auto key_or_error = AesKey::CreateFromShares(shares, 5);
66   EXPECT_THAT(key_or_error.ok(), Eq(true));
67   EXPECT_THAT(key_or_error.value(), Eq(original_key));
68 }
69 
TEST(AesKeyTest,CreateFromSharesHandlesShortKeys)70 TEST(AesKeyTest, CreateFromSharesHandlesShortKeys) {
71   ShamirSecretSharing shamir;
72   for (int i = 1; i <= 128; ++i) {
73     std::string original_key_string = JavaStyleKey(i);
74     AesKey original_key = AesKeyFromString(original_key_string);
75     std::string key_string_for_sharing;
76     if (original_key_string.size() < 16) {
77       key_string_for_sharing =
78           absl::StrCat(std::string(16 - original_key_string.size(), '\0'),
79                        original_key_string);
80     } else if (original_key_string.size() == 17) {
81       key_string_for_sharing = original_key_string.substr(1);
82     } else {
83       key_string_for_sharing = original_key_string;
84     }
85     auto shares = shamir.Share(5, 7, AesKeyFromString(key_string_for_sharing));
86     auto key_or_error = AesKey::CreateFromShares(shares, 5);
87     EXPECT_THAT(key_or_error.ok(), Eq(true));
88     EXPECT_THAT(key_or_error.value(), Eq(original_key))
89         << i << " bit key fails";
90   }
91 }
92 
93 }  // namespace
94 }  // namespace secagg
95 }  // namespace fcp
96