| 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 | static_assert(sizeof(BCM_mlkem768_private_key) <= sizeof(MLKEM768_private_key), |
| 21 | ""); |
| 22 | static_assert(alignof(BCM_mlkem768_private_key) <= |
| 23 | alignof(MLKEM768_private_key), |
| 24 | ""); |
| 25 | static_assert(sizeof(BCM_mlkem768_public_key) <= sizeof(MLKEM768_public_key), |
| 26 | ""); |
| 27 | static_assert(alignof(BCM_mlkem768_public_key) <= alignof(MLKEM768_public_key), |
| 28 | ""); |
| 29 | static_assert(MLKEM768_PUBLIC_KEY_BYTES == BCM_MLKEM768_PUBLIC_KEY_BYTES, ""); |
| 30 | static_assert(MLKEM_SEED_BYTES == BCM_MLKEM_SEED_BYTES, ""); |
| 31 | static_assert(MLKEM768_CIPHERTEXT_BYTES == BCM_MLKEM768_CIPHERTEXT_BYTES, ""); |
| 32 | static_assert(MLKEM_SHARED_SECRET_BYTES == BCM_MLKEM_SHARED_SECRET_BYTES, ""); |
| 33 | static_assert(MLKEM1024_PUBLIC_KEY_BYTES == BCM_MLKEM1024_PUBLIC_KEY_BYTES, ""); |
| 34 | static_assert(MLKEM1024_CIPHERTEXT_BYTES == BCM_MLKEM1024_CIPHERTEXT_BYTES, ""); |
| 35 |
| 36 | void MLKEM768_generate_key( |
| 37 | uint8_t out_encoded_public_key[MLKEM768_PUBLIC_KEY_BYTES], |
| 38 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], |
| 39 | struct MLKEM768_private_key *out_private_key) { |
| 40 | BCM_mlkem768_generate_key( |
| 41 | out_encoded_public_key, optional_out_seed, |
| 42 | reinterpret_cast<BCM_mlkem768_private_key *>(out_private_key)); |
| 43 | } |
| 44 |
| 45 | int MLKEM768_private_key_from_seed(struct MLKEM768_private_key *out_private_key, |
| 46 | const uint8_t *seed, size_t seed_len) { |
| 47 | return bcm_success(BCM_mlkem768_private_key_from_seed( |
| 48 | reinterpret_cast<BCM_mlkem768_private_key *>(out_private_key), seed, |
| 49 | seed_len)); |
| 50 | } |
| 51 |
| 52 | void MLKEM768_public_from_private( |
| 53 | struct MLKEM768_public_key *out_public_key, |
| 54 | const struct MLKEM768_private_key *private_key) { |
| 55 | (void)BCM_mlkem768_public_from_private( |
| 56 | reinterpret_cast<BCM_mlkem768_public_key *>(out_public_key), |
| 57 | reinterpret_cast<const BCM_mlkem768_private_key *>(private_key)); |
| 58 | } |
| 59 |
| 60 | void MLKEM768_encap(uint8_t out_ciphertext[MLKEM768_CIPHERTEXT_BYTES], |
| 61 | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], |
| 62 | const struct MLKEM768_public_key *public_key) { |
| 63 | (void)BCM_mlkem768_encap( |
| 64 | out_ciphertext, out_shared_secret, |
| 65 | reinterpret_cast<const BCM_mlkem768_public_key *>(public_key)); |
| 66 | } |
| 67 |
| 68 | int MLKEM768_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], |
| 69 | const uint8_t *ciphertext, size_t ciphertext_len, |
| 70 | const struct MLKEM768_private_key *private_key) { |
| 71 | return bcm_success(BCM_mlkem768_decap( |
| 72 | out_shared_secret, ciphertext, ciphertext_len, |
| 73 | reinterpret_cast<const BCM_mlkem768_private_key *>(private_key))); |
| 74 | } |
| 75 |
| 76 | int MLKEM768_marshal_public_key(CBB *out, |
| 77 | const struct MLKEM768_public_key *public_key) { |
| 78 | return bcm_success(BCM_mlkem768_marshal_public_key( |
| 79 | out, reinterpret_cast<const BCM_mlkem768_public_key *>(public_key))); |
| 80 | } |
| 81 |
| 82 | int MLKEM768_parse_public_key(struct MLKEM768_public_key *out_public_key, |
| 83 | CBS *in) { |
| 84 | return bcm_success(BCM_mlkem768_parse_public_key( |
| 85 | reinterpret_cast<BCM_mlkem768_public_key *>(out_public_key), in)); |
| 86 | } |
| 87 |
| 88 |
| 89 | static_assert(sizeof(BCM_mlkem1024_private_key) <= |
| 90 | sizeof(MLKEM1024_private_key), |
| 91 | ""); |
| 92 | static_assert(alignof(BCM_mlkem1024_private_key) <= |
| 93 | alignof(MLKEM1024_private_key), |
| 94 | ""); |
| 95 | static_assert(sizeof(BCM_mlkem1024_public_key) <= sizeof(MLKEM1024_public_key), |
| 96 | ""); |
| 97 | static_assert(alignof(BCM_mlkem1024_public_key) <= |
| 98 | alignof(MLKEM1024_public_key), |
| 99 | ""); |
| 100 |
| 101 | void MLKEM1024_generate_key( |
| 102 | uint8_t out_encoded_public_key[MLKEM1024_PUBLIC_KEY_BYTES], |
| 103 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], |
| 104 | struct MLKEM1024_private_key *out_private_key) { |
| 105 | (void)BCM_mlkem1024_generate_key( |
| 106 | out_encoded_public_key, optional_out_seed, |
| 107 | reinterpret_cast<BCM_mlkem1024_private_key *>(out_private_key)); |
| 108 | } |
| 109 |
| 110 | int MLKEM1024_private_key_from_seed( |
| 111 | struct MLKEM1024_private_key *out_private_key, const uint8_t *seed, |
| 112 | size_t seed_len) { |
| 113 | return bcm_success(BCM_mlkem1024_private_key_from_seed( |
| 114 | reinterpret_cast<BCM_mlkem1024_private_key *>(out_private_key), seed, |
| 115 | seed_len)); |
| 116 | } |
| 117 |
| 118 | void MLKEM1024_public_from_private( |
| 119 | struct MLKEM1024_public_key *out_public_key, |
| 120 | const struct MLKEM1024_private_key *private_key) { |
| 121 | (void)BCM_mlkem1024_public_from_private( |
| 122 | reinterpret_cast<BCM_mlkem1024_public_key *>(out_public_key), |
| 123 | reinterpret_cast<const BCM_mlkem1024_private_key *>(private_key)); |
| 124 | } |
| 125 |
| 126 | void MLKEM1024_encap(uint8_t out_ciphertext[MLKEM1024_CIPHERTEXT_BYTES], |
| 127 | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], |
| 128 | const struct MLKEM1024_public_key *public_key) { |
| 129 | (void)BCM_mlkem1024_encap( |
| 130 | out_ciphertext, out_shared_secret, |
| 131 | reinterpret_cast<const BCM_mlkem1024_public_key *>(public_key)); |
| 132 | } |
| 133 |
| 134 | int MLKEM1024_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], |
| 135 | const uint8_t *ciphertext, size_t ciphertext_len, |
| 136 | const struct MLKEM1024_private_key *private_key) { |
| 137 | return bcm_success(BCM_mlkem1024_decap( |
| 138 | out_shared_secret, ciphertext, ciphertext_len, |
| 139 | reinterpret_cast<const BCM_mlkem1024_private_key *>(private_key))); |
| 140 | } |
| 141 |
| 142 | int MLKEM1024_marshal_public_key( |
| 143 | CBB *out, const struct MLKEM1024_public_key *public_key) { |
| 144 | return bcm_success(BCM_mlkem1024_marshal_public_key( |
| 145 | out, reinterpret_cast<const BCM_mlkem1024_public_key *>(public_key))); |
| 146 | } |
| 147 |
| 148 | int MLKEM1024_parse_public_key(struct MLKEM1024_public_key *out_public_key, |
| 149 | CBS *in) { |
| 150 | return bcm_success(BCM_mlkem1024_parse_public_key( |
| 151 | reinterpret_cast<BCM_mlkem1024_public_key *>(out_public_key), in)); |
| 152 | } |
| 153 |