xref: /aosp_15_r20/external/tink/cc/hybrid/internal/hpke_util_boringssl.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_util_boringssl.h"
18 
19 #include "absl/status/status.h"
20 #include "absl/strings/str_cat.h"
21 #include "openssl/base.h"
22 #include "openssl/hpke.h"
23 #include "tink/hybrid/internal/hpke_util.h"
24 #include "tink/util/status.h"
25 #include "tink/util/statusor.h"
26 #include "proto/hpke.pb.h"
27 
28 namespace crypto {
29 namespace tink {
30 namespace internal {
31 
KemParam(const HpkeParams & params)32 util::StatusOr<const EVP_HPKE_KEM*> KemParam(const HpkeParams& params) {
33   switch (params.kem) {
34     case HpkeKem::kX25519HkdfSha256:
35       return EVP_hpke_x25519_hkdf_sha256();
36     default:
37       return util::Status(
38           absl::StatusCode::kInvalidArgument,
39           absl::StrCat("Unsupported HPKE KEM algorithm: ", params.kem));
40   }
41 }
42 
KemParam(const google::crypto::tink::HpkeKem & kem)43 util::StatusOr<const EVP_HPKE_KEM*> KemParam(
44     const google::crypto::tink::HpkeKem& kem) {
45   switch (kem) {
46     case google::crypto::tink::HpkeKem::DHKEM_X25519_HKDF_SHA256:
47       return EVP_hpke_x25519_hkdf_sha256();
48     default:
49       return util::Status(
50           absl::StatusCode::kInvalidArgument,
51           absl::StrCat("Unsupported HPKE KEM algorithm: ", kem));
52   }
53 }
54 
KemParam(const google::crypto::tink::HpkeParams & params)55 util::StatusOr<const EVP_HPKE_KEM*> KemParam(
56     const google::crypto::tink::HpkeParams& params) {
57   return KemParam(params.kem());
58 }
59 
KdfParam(const HpkeParams & params)60 util::StatusOr<const EVP_HPKE_KDF*> KdfParam(const HpkeParams& params) {
61   switch (params.kdf) {
62     case HpkeKdf::kHkdfSha256:
63       return EVP_hpke_hkdf_sha256();
64     default:
65       return util::Status(
66           absl::StatusCode::kInvalidArgument,
67           absl::StrCat("Unsupported HPKE KDF algorithm: ", params.kdf));
68   }
69 }
70 
KdfParam(const google::crypto::tink::HpkeParams & params)71 util::StatusOr<const EVP_HPKE_KDF*> KdfParam(
72     const google::crypto::tink::HpkeParams& params) {
73   switch (params.kdf()) {
74     case google::crypto::tink::HpkeKdf::HKDF_SHA256:
75       return EVP_hpke_hkdf_sha256();
76     default:
77       return util::Status(
78           absl::StatusCode::kInvalidArgument,
79           absl::StrCat("Unsupported HPKE KDF algorithm: ", params.kdf()));
80   }
81 }
82 
AeadParam(const HpkeParams & params)83 util::StatusOr<const EVP_HPKE_AEAD*> AeadParam(const HpkeParams& params) {
84   switch (params.aead) {
85     case HpkeAead::kAes128Gcm:
86       return EVP_hpke_aes_128_gcm();
87     case HpkeAead::kAes256Gcm:
88       return EVP_hpke_aes_256_gcm();
89     case HpkeAead::kChaCha20Poly1305:
90       return EVP_hpke_chacha20_poly1305();
91     default:
92       return util::Status(
93           absl::StatusCode::kInvalidArgument,
94           absl::StrCat("Unsupported HPKE AEAD algorithm: ", params.aead));
95   }
96 }
97 
AeadParam(const google::crypto::tink::HpkeParams & params)98 util::StatusOr<const EVP_HPKE_AEAD*> AeadParam(
99     const google::crypto::tink::HpkeParams& params) {
100   switch (params.aead()) {
101     case google::crypto::tink::HpkeAead::AES_128_GCM:
102       return EVP_hpke_aes_128_gcm();
103     case google::crypto::tink::HpkeAead::AES_256_GCM:
104       return EVP_hpke_aes_256_gcm();
105     case google::crypto::tink::HpkeAead::CHACHA20_POLY1305:
106       return EVP_hpke_chacha20_poly1305();
107     default:
108       return util::Status(
109           absl::StatusCode::kInvalidArgument,
110           absl::StrCat("Unsupported HPKE AEAD algorithm: ", params.aead()));
111   }
112 }
113 
114 }  // namespace internal
115 }  // namespace tink
116 }  // namespace crypto
117