/src/boringssl/crypto/evp/evp_kem.cc
Line | Count | Source |
1 | | // Copyright 2026 The BoringSSL Authors |
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 | | // https://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 | | #include <openssl/evp.h> |
16 | | |
17 | | #include <stddef.h> |
18 | | #include <stdint.h> |
19 | | |
20 | | #include <openssl/err.h> |
21 | | |
22 | | #include "../internal.h" |
23 | | #include "internal.h" |
24 | | |
25 | | using namespace bssl; |
26 | | |
27 | | // Checks whether KEM function invocation is valid. |
28 | | // `ciphertext_len` may be nullptr if it is not required to match. |
29 | | // `secret_len` must always match. |
30 | | static bool check_kem_invocation(const EVP_KEM *kem, |
31 | | const size_t *ciphertext_len, |
32 | | size_t secret_len, |
33 | 0 | const bssl::EvpPkey *pkey_impl) { |
34 | 0 | if (pkey_impl == nullptr || pkey_impl->pkey == nullptr || |
35 | 0 | pkey_impl->ameth == nullptr) { |
36 | 0 | OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER); |
37 | 0 | return false; |
38 | 0 | } |
39 | 0 | if (kem->pkey_id != EVP_PKEY_id(pkey_impl) || |
40 | 0 | (kem->check_key != nullptr && !kem->check_key(kem, pkey_impl))) { |
41 | 0 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
42 | 0 | return false; |
43 | 0 | } |
44 | 0 | if (ciphertext_len && *ciphertext_len != kem->ciphertext_len) { |
45 | 0 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_CIPHERTEXT_LENGTH); |
46 | 0 | return false; |
47 | 0 | } |
48 | 0 | if (secret_len != kem->secret_len) { |
49 | 0 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH); |
50 | 0 | return false; |
51 | 0 | } |
52 | 0 | return true; |
53 | 0 | } |
54 | | |
55 | 0 | size_t EVP_KEM_ciphertext_len(const EVP_KEM *kem) { |
56 | 0 | return kem->ciphertext_len; |
57 | 0 | } |
58 | | |
59 | 0 | size_t EVP_KEM_secret_len(const EVP_KEM *kem) { return kem->secret_len; } |
60 | | |
61 | 0 | size_t EVP_KEM_entropy_len_for_testing(const EVP_KEM *kem) { |
62 | 0 | return kem->entropy_len; |
63 | 0 | } |
64 | | |
65 | | int EVP_KEM_encap(const EVP_KEM *kem, uint8_t *out_ciphertext, |
66 | | size_t ciphertext_len, uint8_t *out_secret, size_t secret_len, |
67 | 0 | const EVP_PKEY *peer_key) { |
68 | 0 | auto *pkey_impl = FromOpaque(peer_key); |
69 | 0 | if (!check_kem_invocation(kem, &ciphertext_len, secret_len, pkey_impl)) { |
70 | 0 | return 0; |
71 | 0 | } |
72 | 0 | return kem->encap(kem, Span(out_ciphertext, ciphertext_len), |
73 | 0 | Span(out_secret, secret_len), pkey_impl); |
74 | 0 | } |
75 | | |
76 | | int EVP_KEM_encap_external_entropy_for_testing( |
77 | | const EVP_KEM *kem, uint8_t *out_ciphertext, size_t ciphertext_len, |
78 | | uint8_t *out_secret, size_t secret_len, const EVP_PKEY *peer_key, |
79 | 0 | const uint8_t *entropy, size_t entropy_len) { |
80 | 0 | auto *pkey_impl = FromOpaque(peer_key); |
81 | 0 | if (!check_kem_invocation(kem, &ciphertext_len, secret_len, pkey_impl)) { |
82 | 0 | return 0; |
83 | 0 | } |
84 | 0 | if (entropy_len != kem->entropy_len) { |
85 | 0 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_ENTROPY_LENGTH); |
86 | 0 | return 0; |
87 | 0 | } |
88 | 0 | return kem->encap_external_entropy(kem, Span(out_ciphertext, ciphertext_len), |
89 | 0 | Span(out_secret, secret_len), pkey_impl, |
90 | 0 | Span(entropy, entropy_len)); |
91 | 0 | } |
92 | | |
93 | | int EVP_KEM_decap(const EVP_KEM *kem, uint8_t *out_secret, size_t secret_len, |
94 | | const uint8_t *ciphertext, size_t ciphertext_len, |
95 | 0 | const EVP_PKEY *key) { |
96 | 0 | auto *pkey_impl = FromOpaque(key); |
97 | 0 | if (!check_kem_invocation(kem, nullptr, secret_len, pkey_impl)) { |
98 | 0 | return 0; |
99 | 0 | } |
100 | 0 | return kem->decap(kem, Span(out_secret, secret_len), |
101 | 0 | Span(ciphertext, ciphertext_len), pkey_impl); |
102 | 0 | } |