xref: /aosp_15_r20/external/tink/cc/core/restricted_data.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2022 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 
17 #include "tink/restricted_data.h"
18 
19 #include <iostream>
20 
21 #include "absl/log/check.h"
22 #include "openssl/crypto.h"
23 #include "tink/subtle/random.h"
24 #include "tink/util/secret_data.h"
25 
26 namespace crypto {
27 namespace tink {
28 
RestrictedData(int64_t num_random_bytes)29 RestrictedData::RestrictedData(int64_t num_random_bytes) {
30   CHECK_GE(num_random_bytes, 0)
31       << "Cannot generate a negative number of random bytes.\n";
32   secret_ = util::SecretDataFromStringView(
33       subtle::Random::GetRandomBytes(num_random_bytes));
34 }
35 
operator ==(const RestrictedData & other) const36 bool RestrictedData::operator==(const RestrictedData& other) const {
37   if (secret_.size() != other.secret_.size()) {
38     return false;
39   }
40   return CRYPTO_memcmp(secret_.data(), other.secret_.data(), secret_.size()) ==
41          0;
42 }
43 
44 }  // namespace tink
45 }  // namespace crypto
46