/src/boringssl/crypto/mlkem/mlkem.cc
Line | Count | Source |
1 | | // Copyright 2024 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/mlkem.h> |
16 | | |
17 | | #include "../fipsmodule/bcm_interface.h" |
18 | | |
19 | | |
20 | | using namespace bssl; |
21 | | |
22 | | void MLKEM768_generate_key( |
23 | | uint8_t out_encoded_public_key[MLKEM768_PUBLIC_KEY_BYTES], |
24 | | uint8_t optional_out_seed[MLKEM_SEED_BYTES], |
25 | 52.8k | struct MLKEM768_private_key *out_private_key) { |
26 | 52.8k | BCM_mlkem768_generate_key(out_encoded_public_key, optional_out_seed, |
27 | 52.8k | out_private_key); |
28 | 52.8k | } |
29 | | |
30 | | int MLKEM768_private_key_from_seed(struct MLKEM768_private_key *out_private_key, |
31 | 68 | const uint8_t *seed, size_t seed_len) { |
32 | 68 | return bcm_success( |
33 | 68 | BCM_mlkem768_private_key_from_seed(out_private_key, seed, seed_len)); |
34 | 68 | } |
35 | | |
36 | | void MLKEM768_public_from_private( |
37 | | struct MLKEM768_public_key *out_public_key, |
38 | 0 | const struct MLKEM768_private_key *private_key) { |
39 | 0 | (void)BCM_mlkem768_public_from_private(out_public_key, private_key); |
40 | 0 | } |
41 | | |
42 | | void MLKEM768_encap(uint8_t out_ciphertext[MLKEM768_CIPHERTEXT_BYTES], |
43 | | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], |
44 | 227 | const struct MLKEM768_public_key *public_key) { |
45 | 227 | (void)BCM_mlkem768_encap(out_ciphertext, out_shared_secret, public_key); |
46 | 227 | } |
47 | | |
48 | | int MLKEM768_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], |
49 | | const uint8_t *ciphertext, size_t ciphertext_len, |
50 | 20 | const struct MLKEM768_private_key *private_key) { |
51 | 20 | return bcm_success(BCM_mlkem768_decap(out_shared_secret, ciphertext, |
52 | 20 | ciphertext_len, private_key)); |
53 | 20 | } |
54 | | |
55 | | int MLKEM768_marshal_public_key(CBB *out, |
56 | 111 | const struct MLKEM768_public_key *public_key) { |
57 | 111 | return bcm_success(BCM_mlkem768_marshal_public_key(out, public_key)); |
58 | 111 | } |
59 | | |
60 | | int MLKEM768_parse_public_key(struct MLKEM768_public_key *out_public_key, |
61 | 677 | CBS *in) { |
62 | 677 | return bcm_success(BCM_mlkem768_parse_public_key(out_public_key, in)); |
63 | 677 | } |
64 | | |
65 | | |
66 | | void MLKEM1024_generate_key( |
67 | | uint8_t out_encoded_public_key[MLKEM1024_PUBLIC_KEY_BYTES], |
68 | | uint8_t optional_out_seed[MLKEM_SEED_BYTES], |
69 | 32 | struct MLKEM1024_private_key *out_private_key) { |
70 | 32 | (void)BCM_mlkem1024_generate_key(out_encoded_public_key, optional_out_seed, |
71 | 32 | out_private_key); |
72 | 32 | } |
73 | | |
74 | | int MLKEM1024_private_key_from_seed( |
75 | | struct MLKEM1024_private_key *out_private_key, const uint8_t *seed, |
76 | 72 | size_t seed_len) { |
77 | 72 | return bcm_success( |
78 | 72 | BCM_mlkem1024_private_key_from_seed(out_private_key, seed, seed_len)); |
79 | 72 | } |
80 | | |
81 | | void MLKEM1024_public_from_private( |
82 | | struct MLKEM1024_public_key *out_public_key, |
83 | 0 | const struct MLKEM1024_private_key *private_key) { |
84 | 0 | (void)BCM_mlkem1024_public_from_private(out_public_key, private_key); |
85 | 0 | } |
86 | | |
87 | | void MLKEM1024_encap(uint8_t out_ciphertext[MLKEM1024_CIPHERTEXT_BYTES], |
88 | | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], |
89 | 0 | const struct MLKEM1024_public_key *public_key) { |
90 | 0 | (void)BCM_mlkem1024_encap(out_ciphertext, out_shared_secret, public_key); |
91 | 0 | } |
92 | | |
93 | | int MLKEM1024_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], |
94 | | const uint8_t *ciphertext, size_t ciphertext_len, |
95 | 1 | const struct MLKEM1024_private_key *private_key) { |
96 | 1 | return bcm_success(BCM_mlkem1024_decap(out_shared_secret, ciphertext, |
97 | 1 | ciphertext_len, private_key)); |
98 | 1 | } |
99 | | |
100 | | int MLKEM1024_marshal_public_key( |
101 | 26 | CBB *out, const struct MLKEM1024_public_key *public_key) { |
102 | 26 | return bcm_success(BCM_mlkem1024_marshal_public_key(out, public_key)); |
103 | 26 | } |
104 | | |
105 | | int MLKEM1024_parse_public_key(struct MLKEM1024_public_key *out_public_key, |
106 | 126 | CBS *in) { |
107 | 126 | return bcm_success(BCM_mlkem1024_parse_public_key(out_public_key, in)); |
108 | 126 | } |