• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/hybrid/ecies_aead_hkdf_public_key_manager.h"
18 
19 #include "absl/status/status.h"
20 #include "absl/strings/string_view.h"
21 #include "tink/hybrid/ecies_aead_hkdf_hybrid_encrypt.h"
22 #include "tink/hybrid_encrypt.h"
23 #include "tink/key_manager.h"
24 #include "tink/util/errors.h"
25 #include "tink/util/protobuf_helper.h"
26 #include "tink/util/status.h"
27 #include "tink/util/statusor.h"
28 #include "tink/util/validation.h"
29 #include "proto/common.pb.h"
30 #include "proto/ecies_aead_hkdf.pb.h"
31 #include "proto/tink.pb.h"
32 
33 namespace crypto {
34 namespace tink {
35 
36 using crypto::tink::util::Status;
37 using google::crypto::tink::EciesAeadHkdfParams;
38 using google::crypto::tink::EciesAeadHkdfPublicKey;
39 using google::crypto::tink::EcPointFormat;
40 using google::crypto::tink::EllipticCurveType;
41 using google::crypto::tink::HashType;
42 
ValidateParams(const EciesAeadHkdfParams & params) const43 Status EciesAeadHkdfPublicKeyManager::ValidateParams(
44     const EciesAeadHkdfParams& params) const {
45   // Validate KEM params.
46   if (!params.has_kem_params()) {
47     return Status(absl::StatusCode::kInvalidArgument, "Missing kem_params.");
48   }
49   if (params.kem_params().curve_type() == EllipticCurveType::UNKNOWN_CURVE ||
50       params.kem_params().hkdf_hash_type() == HashType::UNKNOWN_HASH) {
51     return Status(absl::StatusCode::kInvalidArgument, "Invalid kem_params.");
52   }
53 
54   // Validate DEM params.
55   if (!params.has_dem_params()) {
56     return Status(absl::StatusCode::kInvalidArgument, "Missing dem_params.");
57   }
58   if (!params.dem_params().has_aead_dem()) {
59     return Status(absl::StatusCode::kInvalidArgument, "Invalid dem_params.");
60   }
61 
62   // Validate EC point format.
63   if (params.ec_point_format() == EcPointFormat::UNKNOWN_FORMAT) {
64     return Status(absl::StatusCode::kInvalidArgument,
65                   "Unknown EC point format.");
66   }
67   return util::OkStatus();
68 }
69 
ValidateKey(const EciesAeadHkdfPublicKey & key) const70 Status EciesAeadHkdfPublicKeyManager::ValidateKey(
71     const EciesAeadHkdfPublicKey& key) const {
72   Status status = ValidateVersion(key.version(), get_version());
73   if (!status.ok()) return status;
74   if (!key.has_params()) {
75     return Status(absl::StatusCode::kInvalidArgument, "Missing params.");
76   }
77   return ValidateParams(key.params());
78 }
79 
80 
81 }  // namespace tink
82 }  // namespace crypto
83