1 // Copyright 2020 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/prf/hmac_prf_key_manager.h"
17
18 #include <set>
19 #include <string>
20
21 #include "absl/status/status.h"
22 #include "tink/subtle/common_enums.h"
23 #include "tink/util/enums.h"
24 #include "tink/util/input_stream_util.h"
25 #include "tink/util/status.h"
26 #include "tink/util/statusor.h"
27 #include "proto/hmac_prf.pb.h"
28
29 namespace crypto {
30 namespace tink {
31 namespace {
32 constexpr int kMinKeySizeInBytes = 16;
33 }
34
35 using google::crypto::tink::HmacPrfKey;
36 using google::crypto::tink::HmacPrfKeyFormat;
37 using google::crypto::tink::HmacPrfParams;
38 using subtle::HashType;
39 using util::Enums;
40 using util::Status;
41 using util::StatusOr;
42
ValidateKey(const HmacPrfKey & key) const43 util::Status HmacPrfKeyManager::ValidateKey(const HmacPrfKey& key) const {
44 util::Status status = ValidateVersion(key.version(), get_version());
45 if (!status.ok()) return status;
46 if (key.key_value().size() < kMinKeySizeInBytes) {
47 return util::Status(absl::StatusCode::kInvalidArgument,
48 "Invalid HmacPrfKey: key_value wrong length.");
49 }
50 return ValidateParams(key.params());
51 }
52
ValidateKeyFormat(const HmacPrfKeyFormat & key_format) const53 util::Status HmacPrfKeyManager::ValidateKeyFormat(
54 const HmacPrfKeyFormat& key_format) const {
55 util::Status status = ValidateVersion(key_format.version(), get_version());
56 if (!status.ok()) return status;
57 if (key_format.key_size() < kMinKeySizeInBytes) {
58 return util::Status(absl::StatusCode::kInvalidArgument,
59 "Invalid HmacPrfKeyFormat: invalid key_size.");
60 }
61 return ValidateParams(key_format.params());
62 }
63
CreateKey(const HmacPrfKeyFormat & key_format) const64 crypto::tink::util::StatusOr<HmacPrfKey> HmacPrfKeyManager::CreateKey(
65 const HmacPrfKeyFormat& key_format) const {
66 HmacPrfKey key;
67 key.set_version(get_version());
68 key.set_key_value(subtle::Random::GetRandomBytes(key_format.key_size()));
69 *(key.mutable_params()) = key_format.params();
70 return key;
71 }
72
DeriveKey(const HmacPrfKeyFormat & hmac_prf_key_format,InputStream * input_stream) const73 StatusOr<HmacPrfKey> HmacPrfKeyManager::DeriveKey(
74 const HmacPrfKeyFormat& hmac_prf_key_format,
75 InputStream* input_stream) const {
76 crypto::tink::util::Status status = ValidateKeyFormat(hmac_prf_key_format);
77 if (!status.ok()) return status;
78
79 crypto::tink::util::StatusOr<std::string> randomness =
80 ReadBytesFromStream(hmac_prf_key_format.key_size(), input_stream);
81 if (!randomness.status().ok()) {
82 return randomness.status();
83 }
84
85 HmacPrfKey key;
86 key.set_version(get_version());
87 *(key.mutable_params()) = hmac_prf_key_format.params();
88 key.set_key_value(randomness.value());
89 return key;
90 }
91
ValidateParams(const HmacPrfParams & params) const92 Status HmacPrfKeyManager::ValidateParams(const HmacPrfParams& params) const {
93 static const std::set<HashType>* supported_hash_types =
94 new std::set<HashType>({HashType::SHA1, HashType::SHA224,
95 HashType::SHA256, HashType::SHA384,
96 HashType::SHA512});
97 if (supported_hash_types->find(Enums::ProtoToSubtle(params.hash())) ==
98 supported_hash_types->end()) {
99 return ToStatusF(absl::StatusCode::kInvalidArgument,
100 "Invalid HmacParams: HashType '%s' not supported.",
101 Enums::HashName(params.hash()));
102 }
103 return util::OkStatus();
104 }
105
106 } // namespace tink
107 } // namespace crypto
108