| 1 |
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <openssl/base.h> |
| 5 |
|
| 6 |
#include "../delocate.h" |
| 7 |
#include "../ml_kem/ml_kem.h" |
| 8 |
#include "internal.h" |
| 9 |
#include <openssl/bytestring.h> |
| 10 |
#include <openssl/err.h> |
| 11 |
|
| 12 |
// https://csrc.nist.gov/projects/computer-security-objects-register/algorithm-registration |
| 13 |
// 2.16.840.1.101.3.4.4.1 |
| 14 |
static const uint8_t kOIDMLKEM512[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x04, 0x01}; |
| 15 |
// 2.16.840.1.101.3.4.4.2 |
| 16 |
static const uint8_t kOIDMLKEM768[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x04, 0x02}; |
| 17 |
// 2.16.840.1.101.3.4.4.3 |
| 18 |
static const uint8_t kOIDMLKEM1024[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x04, 0x03}; |
| 19 |
|
| 20 |
static int ml_kem_1024_keygen_deterministic(uint8_t *public_key, |
| 21 |
size_t *public_len, |
| 22 |
uint8_t *secret_key, |
| 23 |
size_t *secret_len, |
| 24 |
const uint8_t *seed) { |
| 25 |
return ml_kem_1024_keypair_deterministic(public_key, public_len, secret_key, secret_len, seed) == 0; |
| 26 |
} |
| 27 |
|
| 28 |
static int ml_kem_1024_encaps_deterministic(uint8_t *ciphertext, |
| 29 |
size_t *ciphertext_len, |
| 30 |
uint8_t *shared_secret, |
| 31 |
size_t *shared_secret_len, |
| 32 |
const uint8_t *public_key, |
| 33 |
const uint8_t *seed) { |
| 34 |
return ml_kem_1024_encapsulate_deterministic(ciphertext, ciphertext_len, shared_secret, shared_secret_len, public_key, seed) == 0; |
| 35 |
} |
| 36 |
|
| 37 |
static int ml_kem_1024_encaps(uint8_t *ciphertext, |
| 38 |
size_t *ciphertext_len, |
| 39 |
uint8_t *shared_secret, |
| 40 |
size_t *shared_secret_len, |
| 41 |
const uint8_t *public_key) { |
| 42 |
return ml_kem_1024_encapsulate(ciphertext, ciphertext_len, shared_secret, shared_secret_len, public_key) == 0; |
| 43 |
} |
| 44 |
|
| 45 |
static int ml_kem_1024_decaps(uint8_t *shared_secret, |
| 46 |
size_t *shared_secret_len, |
| 47 |
const uint8_t *ciphertext, |
| 48 |
const uint8_t *secret_key) { |
| 49 |
return ml_kem_1024_decapsulate(shared_secret, shared_secret_len, ciphertext, secret_key) == 0; |
| 50 |
} |
| 51 |
|
| 52 |
DEFINE_LOCAL_DATA(KEM_METHOD, kem_ml_kem_1024_method) { |
| 53 |
out->keygen_deterministic = ml_kem_1024_keygen_deterministic; |
| 54 |
out->encaps_deterministic = ml_kem_1024_encaps_deterministic; |
| 55 |
out->encaps = ml_kem_1024_encaps; |
| 56 |
out->decaps = ml_kem_1024_decaps; |
| 57 |
} |
| 58 |
|
| 59 |
static int ml_kem_768_keygen_deterministic(uint8_t *public_key, |
| 60 |
size_t *public_len, |
| 61 |
uint8_t *secret_key, |
| 62 |
size_t *secret_len, |
| 63 |
const uint8_t *seed) { |
| 64 |
return ml_kem_768_keypair_deterministic(public_key, public_len, secret_key, secret_len, seed) == 0; |
| 65 |
} |
| 66 |
|
| 67 |
static int ml_kem_768_encaps_deterministic(uint8_t *ciphertext, |
| 68 |
size_t *ciphertext_len, |
| 69 |
uint8_t *shared_secret, |
| 70 |
size_t *shared_secret_len, |
| 71 |
const uint8_t *public_key, |
| 72 |
const uint8_t *seed) { |
| 73 |
return ml_kem_768_encapsulate_deterministic(ciphertext, ciphertext_len, shared_secret, shared_secret_len, public_key, seed) == 0; |
| 74 |
} |
| 75 |
|
| 76 |
static int ml_kem_768_encaps(uint8_t *ciphertext, |
| 77 |
size_t *ciphertext_len, |
| 78 |
uint8_t *shared_secret, |
| 79 |
size_t *shared_secret_len, |
| 80 |
const uint8_t *public_key) { |
| 81 |
return ml_kem_768_encapsulate(ciphertext, ciphertext_len, shared_secret, shared_secret_len, public_key) == 0; |
| 82 |
} |
| 83 |
|
| 84 |
static int ml_kem_768_decaps(uint8_t *shared_secret, |
| 85 |
size_t *shared_secret_len, |
| 86 |
const uint8_t *ciphertext, |
| 87 |
const uint8_t *secret_key) { |
| 88 |
return ml_kem_768_decapsulate(shared_secret, shared_secret_len, ciphertext, secret_key) == 0; |
| 89 |
} |
| 90 |
|
| 91 |
DEFINE_LOCAL_DATA(KEM_METHOD, kem_ml_kem_768_method) { |
| 92 |
out->keygen_deterministic = ml_kem_768_keygen_deterministic; |
| 93 |
out->encaps_deterministic = ml_kem_768_encaps_deterministic; |
| 94 |
out->encaps = ml_kem_768_encaps; |
| 95 |
out->decaps = ml_kem_768_decaps; |
| 96 |
} |
| 97 |
|
| 98 |
static int ml_kem_512_keygen_deterministic(uint8_t *public_key, |
| 99 |
size_t *public_len, |
| 100 |
uint8_t *secret_key, |
| 101 |
size_t *secret_len, |
| 102 |
const uint8_t *seed) { |
| 103 |
return ml_kem_512_keypair_deterministic(public_key, public_len, secret_key, secret_len, seed) == 0; |
| 104 |
} |
| 105 |
|
| 106 |
static int ml_kem_512_encaps_deterministic(uint8_t *ciphertext, |
| 107 |
size_t *ciphertext_len, |
| 108 |
uint8_t *shared_secret, |
| 109 |
size_t *shared_secret_len, |
| 110 |
const uint8_t *public_key, |
| 111 |
const uint8_t *seed) { |
| 112 |
return ml_kem_512_encapsulate_deterministic(ciphertext, ciphertext_len, shared_secret, shared_secret_len, public_key, seed) == 0; |
| 113 |
} |
| 114 |
|
| 115 |
static int ml_kem_512_encaps(uint8_t *ciphertext, |
| 116 |
size_t *ciphertext_len, |
| 117 |
uint8_t *shared_secret, |
| 118 |
size_t *shared_secret_len, |
| 119 |
const uint8_t *public_key) { |
| 120 |
return ml_kem_512_encapsulate(ciphertext, ciphertext_len, shared_secret, shared_secret_len, public_key) == 0; |
| 121 |
} |
| 122 |
|
| 123 |
static int ml_kem_512_decaps(uint8_t *shared_secret, |
| 124 |
size_t *shared_secret_len, |
| 125 |
const uint8_t *ciphertext, |
| 126 |
const uint8_t *secret_key) { |
| 127 |
return ml_kem_512_decapsulate(shared_secret, shared_secret_len, ciphertext, secret_key) == 0; |
| 128 |
} |
| 129 |
|
| 130 |
DEFINE_LOCAL_DATA(KEM_METHOD, kem_ml_kem_512_method) { |
| 131 |
out->keygen_deterministic = ml_kem_512_keygen_deterministic; |
| 132 |
out->encaps_deterministic = ml_kem_512_encaps_deterministic; |
| 133 |
out->encaps = ml_kem_512_encaps; |
| 134 |
out->decaps = ml_kem_512_decaps; |
| 135 |
} |
| 136 |
|
| 137 |
DEFINE_LOCAL_DATA(KEM, KEM_ml_kem_512) { |
| 138 |
out->nid = NID_MLKEM512; |
| 139 |
out->oid = kOIDMLKEM512; |
| 140 |
out->oid_len = sizeof(kOIDMLKEM512); |
| 141 |
out->comment = "MLKEM512 "; |
| 142 |
out->public_key_len = MLKEM512_PUBLIC_KEY_BYTES; |
| 143 |
out->secret_key_len = MLKEM512_SECRET_KEY_BYTES; |
| 144 |
out->ciphertext_len = MLKEM512_CIPHERTEXT_BYTES; |
| 145 |
out->shared_secret_len = MLKEM512_SHARED_SECRET_LEN; |
| 146 |
out->keygen_seed_len = MLKEM512_KEYGEN_SEED_LEN; |
| 147 |
out->encaps_seed_len = MLKEM512_ENCAPS_SEED_LEN; |
| 148 |
out->method = kem_ml_kem_512_method(); |
| 149 |
} |
| 150 |
|
| 151 |
DEFINE_LOCAL_DATA(KEM, KEM_ml_kem_768) { |
| 152 |
out->nid = NID_MLKEM768; |
| 153 |
out->oid = kOIDMLKEM768; |
| 154 |
out->oid_len = sizeof(kOIDMLKEM768); |
| 155 |
out->comment = "MLKEM768 "; |
| 156 |
out->public_key_len = MLKEM768_PUBLIC_KEY_BYTES; |
| 157 |
out->secret_key_len = MLKEM768_SECRET_KEY_BYTES; |
| 158 |
out->ciphertext_len = MLKEM768_CIPHERTEXT_BYTES; |
| 159 |
out->shared_secret_len = MLKEM768_SHARED_SECRET_LEN; |
| 160 |
out->keygen_seed_len = MLKEM768_KEYGEN_SEED_LEN; |
| 161 |
out->encaps_seed_len = MLKEM768_ENCAPS_SEED_LEN; |
| 162 |
out->method = kem_ml_kem_768_method(); |
| 163 |
} |
| 164 |
|
| 165 |
DEFINE_LOCAL_DATA(KEM, KEM_ml_kem_1024) { |
| 166 |
out->nid = NID_MLKEM1024; |
| 167 |
out->oid = kOIDMLKEM1024; |
| 168 |
out->oid_len = sizeof(kOIDMLKEM1024); |
| 169 |
out->comment = "MLKEM1024 "; |
| 170 |
out->public_key_len = MLKEM1024_PUBLIC_KEY_BYTES; |
| 171 |
out->secret_key_len = MLKEM1024_SECRET_KEY_BYTES; |
| 172 |
out->ciphertext_len = MLKEM1024_CIPHERTEXT_BYTES; |
| 173 |
out->shared_secret_len = MLKEM1024_SHARED_SECRET_LEN; |
| 174 |
out->keygen_seed_len = MLKEM1024_KEYGEN_SEED_LEN; |
| 175 |
out->encaps_seed_len = MLKEM1024_ENCAPS_SEED_LEN; |
| 176 |
out->method = kem_ml_kem_1024_method(); |
| 177 |
} |
| 178 |
|
| 179 |
const KEM *KEM_find_kem_by_nid(int nid) { |
| 180 |
switch (nid) { |
| 181 |
case NID_MLKEM512: |
| 182 |
return KEM_ml_kem_512(); |
| 183 |
case NID_MLKEM768: |
| 184 |
return KEM_ml_kem_768(); |
| 185 |
case NID_MLKEM1024: |
| 186 |
return KEM_ml_kem_1024(); |
| 187 |
default: |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
KEM_KEY *KEM_KEY_new(void) { |
| 193 |
KEM_KEY *ret = OPENSSL_zalloc(sizeof(KEM_KEY)); |
| 194 |
if (ret == NULL) { |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
return ret; |
| 199 |
} |
| 200 |
|
| 201 |
static void KEM_KEY_clear(KEM_KEY *key) { |
| 202 |
key->kem = NULL; |
| 203 |
OPENSSL_free(key->public_key); |
| 204 |
OPENSSL_free(key->secret_key); |
| 205 |
OPENSSL_free(key->seed); |
| 206 |
key->public_key = NULL; |
| 207 |
key->secret_key = NULL; |
| 208 |
key->seed = NULL; |
| 209 |
} |
| 210 |
|
| 211 |
int KEM_KEY_init(KEM_KEY *key, const KEM *kem) { |
| 212 |
if (key == NULL || kem == NULL) { |
| 213 |
return 0; |
| 214 |
} |
| 215 |
// If the key is already initialized clear it. |
| 216 |
KEM_KEY_clear(key); |
| 217 |
|
| 218 |
key->kem = kem; |
| 219 |
key->public_key = OPENSSL_malloc(kem->public_key_len); |
| 220 |
key->secret_key = OPENSSL_malloc(kem->secret_key_len); |
| 221 |
if (key->public_key == NULL || key->secret_key == NULL) { |
| 222 |
KEM_KEY_clear(key); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
return 1; |
| 227 |
} |
| 228 |
|
| 229 |
const EVP_PKEY_ASN1_METHOD *KEM_find_asn1_by_nid(int nid) { |
| 230 |
switch (nid) { |
| 231 |
case NID_MLKEM512: |
| 232 |
case NID_MLKEM768: |
| 233 |
case NID_MLKEM1024: |
| 234 |
return &kem_asn1_meth; |
| 235 |
default: |
| 236 |
return NULL; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void KEM_KEY_free(KEM_KEY *key) { |
| 241 |
if (key == NULL) { |
| 242 |
return; |
| 243 |
} |
| 244 |
KEM_KEY_clear(key); |
| 245 |
OPENSSL_free(key); |
| 246 |
} |
| 247 |
|
| 248 |
const KEM *KEM_KEY_get0_kem(KEM_KEY* key) { |
| 249 |
return key->kem; |
| 250 |
} |
| 251 |
|
| 252 |
int KEM_KEY_set_raw_public_key(KEM_KEY *key, const uint8_t *in) { |
| 253 |
OPENSSL_free(key->public_key); |
| 254 |
key->public_key = OPENSSL_memdup(in, key->kem->public_key_len); |
| 255 |
if (key->public_key == NULL) { |
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
return 1; |
| 260 |
} |
| 261 |
|
| 262 |
int KEM_KEY_set_raw_secret_key(KEM_KEY *key, const uint8_t *in) { |
| 263 |
OPENSSL_free(key->secret_key); |
| 264 |
key->secret_key = OPENSSL_memdup(in, key->kem->secret_key_len); |
| 265 |
if (key->secret_key == NULL) { |
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
return 1; |
| 270 |
} |
| 271 |
|
| 272 |
int KEM_KEY_set_raw_key(KEM_KEY *key, const uint8_t *in_public, |
| 273 |
const uint8_t *in_secret) { |
| 274 |
OPENSSL_free(key->public_key); |
| 275 |
OPENSSL_free(key->secret_key); |
| 276 |
key->public_key = OPENSSL_memdup(in_public, key->kem->public_key_len); |
| 277 |
key->secret_key = OPENSSL_memdup(in_secret, key->kem->secret_key_len); |
| 278 |
if (key->public_key == NULL || key->secret_key == NULL) { |
| 279 |
KEM_KEY_clear(key); |
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
return 1; |
| 284 |
} |
| 285 |
|
| 286 |
int KEM_KEY_set_raw_keypair_from_seed(KEM_KEY *key, const CBS *seed) { |
| 287 |
if (key == NULL || seed == NULL || key->kem == NULL) { |
| 288 |
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
// Ensure key is uninitialized |
| 293 |
if (key->public_key != NULL || key->secret_key != NULL) { |
| 294 |
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 295 |
return 0; |
| 296 |
} |
| 297 |
|
| 298 |
// Validate seed length - all ML-KEM variants use 64-byte seeds |
| 299 |
if (CBS_len(seed) != key->kem->keygen_seed_len) { |
| 300 |
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
// Allocate buffers for key generation |
| 305 |
uint8_t *public_key = OPENSSL_malloc(key->kem->public_key_len); |
| 306 |
uint8_t *secret_key = OPENSSL_malloc(key->kem->secret_key_len); |
| 307 |
|
| 308 |
if (public_key == NULL || secret_key == NULL) { |
| 309 |
OPENSSL_free(public_key); |
| 310 |
OPENSSL_free(secret_key); |
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
size_t public_len = key->kem->public_key_len; |
| 315 |
size_t secret_len = key->kem->secret_key_len; |
| 316 |
|
| 317 |
// Generate keypair from seed using the KEM method |
| 318 |
if (!key->kem->method->keygen_deterministic(public_key, &public_len, |
| 319 |
secret_key, &secret_len, |
| 320 |
CBS_data(seed))) { |
| 321 |
OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); |
| 322 |
OPENSSL_free(public_key); |
| 323 |
OPENSSL_free(secret_key); |
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
// Set public and secret key |
| 328 |
key->public_key = public_key; |
| 329 |
key->secret_key = secret_key; |
| 330 |
|
| 331 |
// Save a copy of the seed for seed-format encoding |
| 332 |
key->seed = OPENSSL_memdup(CBS_data(seed), key->kem->keygen_seed_len); |
| 333 |
if (key->seed == NULL) { |
| 334 |
KEM_KEY_clear(key); |
| 335 |
return 0; |
| 336 |
} |
| 337 |
|
| 338 |
return 1; |
| 339 |
} |