| 1 |
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#ifndef AWSLC_HEADER_KEM_INTERNAL_H |
| 5 |
#define AWSLC_HEADER_KEM_INTERNAL_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
|
| 10 |
#if defined(__cplusplus) |
| 11 |
extern "C" { |
| 12 |
#endif |
| 13 |
|
| 14 |
// KEM_METHOD structure and helper functions. |
| 15 |
typedef struct { |
| 16 |
int (*keygen_deterministic)(uint8_t *ctx, |
| 17 |
size_t *ctx_len, |
| 18 |
uint8_t *pkey, |
| 19 |
size_t *pkey_len, |
| 20 |
const uint8_t *seed); |
| 21 |
|
| 22 |
int (*encaps_deterministic)(uint8_t *ciphertext, |
| 23 |
size_t *ciphertext_len, |
| 24 |
uint8_t *shared_secret, |
| 25 |
size_t *shared_secret_len, |
| 26 |
const uint8_t *public_key, |
| 27 |
const uint8_t *seed); |
| 28 |
|
| 29 |
int (*encaps)(uint8_t *ciphertext, |
| 30 |
size_t *ciphertext_len, |
| 31 |
uint8_t *shared_secret, |
| 32 |
size_t *shared_secret_len, |
| 33 |
const uint8_t *public_key); |
| 34 |
|
| 35 |
int (*decaps)(uint8_t *shared_secret, |
| 36 |
size_t *shared_secret_len, |
| 37 |
const uint8_t *ciphertext, |
| 38 |
const uint8_t *secret_key); |
| 39 |
} KEM_METHOD; |
| 40 |
|
| 41 |
// KEM structure and helper functions. |
| 42 |
typedef struct { |
| 43 |
int nid; |
| 44 |
const uint8_t *oid; |
| 45 |
uint8_t oid_len; |
| 46 |
const char *comment; |
| 47 |
size_t public_key_len; |
| 48 |
size_t secret_key_len; |
| 49 |
size_t ciphertext_len; |
| 50 |
size_t shared_secret_len; |
| 51 |
size_t keygen_seed_len; |
| 52 |
size_t encaps_seed_len; |
| 53 |
const KEM_METHOD *method; |
| 54 |
} KEM; |
| 55 |
|
| 56 |
// KEM_KEY structure and helper functions. |
| 57 |
struct kem_key_st { |
| 58 |
const KEM *kem; |
| 59 |
uint8_t *public_key; |
| 60 |
uint8_t *secret_key; |
| 61 |
uint8_t *seed; |
| 62 |
}; |
| 63 |
|
| 64 |
const KEM *KEM_find_kem_by_nid(int nid); |
| 65 |
const EVP_PKEY_ASN1_METHOD *KEM_find_asn1_by_nid(int nid); |
| 66 |
int EVP_PKEY_kem_set_params(EVP_PKEY *pkey, int nid); |
| 67 |
|
| 68 |
KEM_KEY *KEM_KEY_new(void); |
| 69 |
int KEM_KEY_init(KEM_KEY *key, const KEM *kem); |
| 70 |
void KEM_KEY_free(KEM_KEY *key); |
| 71 |
const KEM *KEM_KEY_get0_kem(KEM_KEY* key); |
| 72 |
|
| 73 |
// KEM_KEY_set_raw_public_key function allocates the public key buffer |
| 74 |
// within the given |key| and copies the contents of |in| to it. |
| 75 |
// |
| 76 |
// NOTE: No checks are done in this function, the caller has to ensure |
| 77 |
// that the pointers are valid and |in| has the correct size. |
| 78 |
int KEM_KEY_set_raw_public_key(KEM_KEY *key, const uint8_t *in); |
| 79 |
|
| 80 |
// KEM_KEY_set_raw_secret_key function allocates the secret key buffer |
| 81 |
// within the given |key| and copies the contents of |in| to it. |
| 82 |
// |
| 83 |
// NOTE: No checks are done in this function, the caller has to ensure |
| 84 |
// that the pointers are valid and |in| has the correct size. |
| 85 |
int KEM_KEY_set_raw_secret_key(KEM_KEY *key, const uint8_t *in); |
| 86 |
|
| 87 |
// KEM_KEY_set_raw_key function allocates the public and secret key buffers |
| 88 |
// within the given |key| and copies the contents of |in_public| and |
| 89 |
// |in_secret| to them. |
| 90 |
// |
| 91 |
// NOTE: No checks are done in this function, the caller has to ensure |
| 92 |
// that the pointers are valid and |in_public| and |in_secret| |
| 93 |
// have the correct size. |
| 94 |
int KEM_KEY_set_raw_key(KEM_KEY *key, const uint8_t *in_public, |
| 95 |
const uint8_t *in_secret); |
| 96 |
|
| 97 |
// KEM_KEY_set_raw_keypair_from_seed function generates a keypair from the |
| 98 |
// given seed using the appropriate key generation function based on the |
| 99 |
// KEM variant, then allocates and sets both public and secret key buffers |
| 100 |
// within the given |key|. |
| 101 |
// |
| 102 |
// NOTE: The seed must be exactly 64 bytes for all ML-KEM variants. |
| 103 |
// The caller must ensure the seed CBS contains valid data. |
| 104 |
// |key->kem| must be initialized and |key->public_key| and |
| 105 |
// |key->secret_key| must both be NULL. |
| 106 |
int KEM_KEY_set_raw_keypair_from_seed(KEM_KEY *key, const CBS *seed); |
| 107 |
|
| 108 |
#if defined(__cplusplus) |
| 109 |
} // extern C |
| 110 |
#endif |
| 111 |
|
| 112 |
#endif // AWSLC_HEADER_KEM_TEST_INTERNAL_H |