|
| 1 | +/* |
| 2 | + * Copyright (c) The mlkem-native project authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 4 | + */ |
| 5 | + |
| 6 | +#include <stdio.h> |
| 7 | +#include <string.h> |
| 8 | + |
| 9 | +/* Import public mlkem-native API |
| 10 | + * |
| 11 | + * This requires specifying the parameter set and namespace prefix |
| 12 | + * used for the build. |
| 13 | + */ |
| 14 | +#define MLK_CONFIG_API_PARAMETER_SET MLK_CONFIG_PARAMETER_SET |
| 15 | +#define MLK_CONFIG_API_NAMESPACE_PREFIX mlkem |
| 16 | +#include "mlkem_native/mlkem/mlkem_native.h" |
| 17 | + |
| 18 | +/* No randombytes needed for deterministic API */ |
| 19 | + |
| 20 | +#define CHECK(x) \ |
| 21 | + do \ |
| 22 | + { \ |
| 23 | + int rc; \ |
| 24 | + rc = (x); \ |
| 25 | + if (!rc) \ |
| 26 | + { \ |
| 27 | + fprintf(stderr, "ERROR (%s,%d)\n", __FILE__, __LINE__); \ |
| 28 | + return 1; \ |
| 29 | + } \ |
| 30 | + } while (0) |
| 31 | + |
| 32 | +int main(void) |
| 33 | +{ |
| 34 | + uint8_t pk[CRYPTO_PUBLICKEYBYTES]; |
| 35 | + uint8_t sk[CRYPTO_SECRETKEYBYTES]; |
| 36 | + uint8_t ct[CRYPTO_CIPHERTEXTBYTES]; |
| 37 | + uint8_t key_a[CRYPTO_BYTES]; |
| 38 | + uint8_t key_b[CRYPTO_BYTES]; |
| 39 | + uint8_t alice_en[2 * MLKEM_SYMBYTES] = {0}; |
| 40 | + uint8_t bob_en[MLKEM_SYMBYTES] = {1}; |
| 41 | + |
| 42 | + |
| 43 | + /* The PCT modifies the PRNG state, so the KAT tests don't work. |
| 44 | + * We run KAT tests only for disabled PCT. |
| 45 | + * Expected keys are generated using deterministic entropy: |
| 46 | + * keypair uses all-zero entropy {0}, enc uses all-one entropy {1} */ |
| 47 | +#if !defined(MLK_CONFIG_KEYGEN_PCT) |
| 48 | +#if MLK_CONFIG_PARAMETER_SET == 512 |
| 49 | + const uint8_t expected_key[] = { |
| 50 | + 0x5f, 0x5f, 0x8c, 0xf5, 0x7c, 0x34, 0xd4, 0x68, 0x06, 0xa2, 0xe9, |
| 51 | + 0xc9, 0x28, 0xba, 0x10, 0x5a, 0x46, 0xf2, 0x67, 0x1a, 0xc7, 0x81, |
| 52 | + 0xdf, 0xf1, 0x4a, 0xbb, 0x27, 0xea, 0x46, 0x06, 0x46, 0x3c}; |
| 53 | +#elif MLK_CONFIG_PARAMETER_SET == 768 |
| 54 | + const uint8_t expected_key[] = { |
| 55 | + 0x85, 0x21, 0xab, 0xc8, 0x14, 0xc7, 0x67, 0x70, 0x4f, 0xa6, 0x25, |
| 56 | + 0xd9, 0x35, 0x95, 0xd0, 0x03, 0x79, 0xa8, 0xb3, 0x70, 0x35, 0x2c, |
| 57 | + 0xa4, 0xba, 0xb3, 0xa6, 0x82, 0x46, 0x63, 0x0d, 0xb0, 0x8b}; |
| 58 | +#elif MLK_CONFIG_PARAMETER_SET == 1024 |
| 59 | + const uint8_t expected_key[] = { |
| 60 | + 0x30, 0x4d, 0xbe, 0x54, 0xd6, 0x6f, 0x80, 0x66, 0xc6, 0xa8, 0x1c, |
| 61 | + 0x6b, 0x36, 0xc4, 0x48, 0x9b, 0xf9, 0xe6, 0x05, 0x79, 0x83, 0x3c, |
| 62 | + 0x4e, 0xdc, 0x8a, 0xc7, 0x92, 0xe5, 0x73, 0x0d, 0xdd, 0x85}; |
| 63 | +#endif /* MLK_CONFIG_PARAMETER_SET == 1024 */ |
| 64 | +#endif /* !MLK_CONFIG_KEYGEN_PCT */ |
| 65 | + |
| 66 | + /* No randombytes_reset() needed for deterministic API */ |
| 67 | + |
| 68 | + printf("Generating keypair ... "); |
| 69 | + |
| 70 | + /* Alice generates a public key using deterministic API with all-zero entropy |
| 71 | + */ |
| 72 | + CHECK(crypto_kem_keypair_derand(pk, sk, alice_en) == 0); |
| 73 | + |
| 74 | + printf("DONE\n"); |
| 75 | + printf("Encaps... "); |
| 76 | + |
| 77 | + /* Bob derives a secret key and creates a response using deterministic API |
| 78 | + * with all-one entropy */ |
| 79 | + CHECK(crypto_kem_enc_derand(ct, key_b, pk, bob_en) == 0); |
| 80 | + |
| 81 | + printf("DONE\n"); |
| 82 | + printf("Decaps... "); |
| 83 | + |
| 84 | + /* Alice uses Bobs response to get her shared key */ |
| 85 | + CHECK(crypto_kem_dec(key_a, ct, sk) == 0); |
| 86 | + |
| 87 | + printf("DONE\n"); |
| 88 | + printf("Compare... "); |
| 89 | + |
| 90 | + CHECK(memcmp(key_a, key_b, CRYPTO_BYTES) == 0); |
| 91 | + |
| 92 | + printf("Shared secret: "); |
| 93 | + { |
| 94 | + size_t i; |
| 95 | + for (i = 0; i < sizeof(key_a); i++) |
| 96 | + { |
| 97 | + printf("%02x", key_a[i]); |
| 98 | + } |
| 99 | + } |
| 100 | + printf("\n"); |
| 101 | + |
| 102 | + /* Check against hardcoded result to make sure that |
| 103 | + * we integrated custom FIPS202 correctly */ |
| 104 | + CHECK(memcmp(key_a, expected_key, CRYPTO_BYTES) == 0); |
| 105 | + |
| 106 | + |
| 107 | + printf("OK\n"); |
| 108 | + return 0; |
| 109 | +} |
0 commit comments