xref: /aosp_15_r20/external/tink/cc/hybrid/internal/hpke_key_manager_util.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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 ///////////////////////////////////////////////////////////////////////////////
16 
17 #include "tink/hybrid/internal/hpke_key_manager_util.h"
18 
19 #include "absl/status/status.h"
20 #include "tink/util/status.h"
21 #include "tink/util/validation.h"
22 #include "proto/hpke.pb.h"
23 
24 namespace crypto {
25 namespace tink {
26 namespace internal {
27 
28 using ::google::crypto::tink::HpkeAead;
29 using ::google::crypto::tink::HpkeKdf;
30 using ::google::crypto::tink::HpkeKem;
31 using ::google::crypto::tink::HpkeParams;
32 using ::google::crypto::tink::HpkePublicKey;
33 
ValidateParams(const HpkeParams & params)34 util::Status ValidateParams(const HpkeParams& params) {
35   if (params.kem() == HpkeKem::KEM_UNKNOWN) {
36     return util::Status(absl::StatusCode::kInvalidArgument,
37                         "Invalid KEM param.");
38   }
39   if (params.kdf() == HpkeKdf::KDF_UNKNOWN) {
40     return util::Status(absl::StatusCode::kInvalidArgument,
41                         "Invalid KDF param.");
42   }
43   if (params.aead() == HpkeAead::AEAD_UNKNOWN) {
44     return util::Status(absl::StatusCode::kInvalidArgument,
45                         "Invalid AEAD param.");
46   }
47   return util::OkStatus();
48 }
49 
ValidateKeyAndVersion(const HpkePublicKey & key,uint32_t max_key_version)50 util::Status ValidateKeyAndVersion(const HpkePublicKey& key,
51                                    uint32_t max_key_version) {
52   util::Status status = ValidateVersion(key.version(), max_key_version);
53   if (!status.ok()) return status;
54   if (!key.has_params()) {
55     return util::Status(absl::StatusCode::kInvalidArgument,
56                         "Missing HPKE key params.");
57   }
58   return ValidateParams(key.params());
59 }
60 
61 }  // namespace internal
62 }  // namespace tink
63 }  // namespace crypto
64