| 1 |
// Copyright (c) 2017, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
/* test_fips exercises various cryptographic primitives for demonstration |
| 5 |
* purposes in the validation process only. */ |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
|
| 9 |
#include <openssl/aead.h> |
| 10 |
#include <openssl/aes.h> |
| 11 |
#include <openssl/bn.h> |
| 12 |
#include <openssl/crypto.h> |
| 13 |
#include <openssl/ctrdrbg.h> |
| 14 |
#include <openssl/curve25519.h> |
| 15 |
#include <openssl/dh.h> |
| 16 |
#include <openssl/ec_key.h> |
| 17 |
#include <openssl/ecdsa.h> |
| 18 |
#include <openssl/evp.h> |
| 19 |
#include <openssl/hmac.h> |
| 20 |
#include <openssl/kdf.h> |
| 21 |
#include <openssl/nid.h> |
| 22 |
#include <openssl/rsa.h> |
| 23 |
#include <openssl/sha.h> |
| 24 |
|
| 25 |
#include "../../crypto/fipsmodule/evp/internal.h" |
| 26 |
#include "../../crypto/fipsmodule/kem/internal.h" |
| 27 |
#include "../../crypto/fipsmodule/pqdsa/internal.h" |
| 28 |
#include "../../crypto/fipsmodule/rand/internal.h" |
| 29 |
#include "../../crypto/internal.h" |
| 30 |
|
| 31 |
|
| 32 |
static void hexdump(const void *a, size_t len) { |
| 33 |
const unsigned char *in = (const unsigned char *)a; |
| 34 |
for (size_t i = 0; i < len; i++) { |
| 35 |
printf("%02x", in[i]); |
| 36 |
} |
| 37 |
|
| 38 |
printf("\n"); |
| 39 |
} |
| 40 |
|
| 41 |
int main(int argc, char **argv) { |
| 42 |
CRYPTO_library_init(); |
| 43 |
|
| 44 |
static const uint8_t kAESKey[16] = {'B', 'o', 'r', 'i', 'n', 'g', 'C', 'r', |
| 45 |
'y', 'p', 't', 'o', ' ', 'K', 'e', 'y'}; |
| 46 |
|
| 47 |
static const uint8_t kPlaintext[64] = { |
| 48 |
'B', 'o', 'r', 'i', 'n', 'g', 'C', 'r', 'y', 'p', 't', 'o', 'M', |
| 49 |
'o', 'd', 'u', 'l', 'e', ' ', 'F', 'I', 'P', 'S', ' ', 'K', 'A', |
| 50 |
'T', ' ', 'E', 'n', 'c', 'r', 'y', 'p', 't', 'i', 'o', 'n', ' ', |
| 51 |
'a', 'n', 'd', ' ', 'D', 'e', 'c', 'r', 'y', 'p', 't', 'i', 'o', |
| 52 |
'n', ' ', 'P', 'l', 'a', 'i', 'n', 't', 'e', 'x', 't', '!'}; |
| 53 |
|
| 54 |
static const uint8_t kPlaintextSHA256[32] = { |
| 55 |
0x37, 0xbd, 0x70, 0x53, 0x72, 0xfc, 0xd4, 0x03, 0x79, 0x70, 0xfb, |
| 56 |
0x06, 0x95, 0xb1, 0x2a, 0x82, 0x48, 0xe1, 0x3e, 0xf2, 0x33, 0xfb, |
| 57 |
0xef, 0x29, 0x81, 0x22, 0x45, 0x40, 0x43, 0x70, 0xce, 0x0f}; |
| 58 |
const uint8_t kDRBGEntropy[48] = { |
| 59 |
'D', 'B', 'R', 'G', ' ', 'I', 'n', 'i', 't', 'i', 'a', 'l', |
| 60 |
' ', 'E', 'n', 't', 'r', 'o', 'p', 'y', ' ', ' ', ' ', ' ', |
| 61 |
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', |
| 62 |
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; |
| 63 |
|
| 64 |
const uint8_t kDRBGPersonalization[18] = {'B', 'C', 'M', 'P', 'e', 'r', |
| 65 |
's', 'o', 'n', 'a', 'l', 'i', |
| 66 |
'z', 'a', 't', 'i', 'o', 'n'}; |
| 67 |
|
| 68 |
const uint8_t kDRBGAD[16] = {'B', 'C', 'M', ' ', 'D', 'R', 'B', 'G', |
| 69 |
' ', 'A', 'D', ' ', ' ', ' ', ' ', ' '}; |
| 70 |
|
| 71 |
const uint8_t kDRBGEntropy2[48] = { |
| 72 |
'D', 'B', 'R', 'G', ' ', 'R', 'e', 's', 'e', 'e', 'd', ' ', |
| 73 |
'E', 'n', 't', 'r', 'o', 'p', 'y', ' ', ' ', ' ', ' ', ' ', |
| 74 |
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', |
| 75 |
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; |
| 76 |
|
| 77 |
AES_KEY aes_key; |
| 78 |
uint8_t aes_iv[16]; |
| 79 |
uint8_t output[256]; |
| 80 |
|
| 81 |
/* AES-CBC Encryption */ |
| 82 |
OPENSSL_memset(aes_iv, 0, sizeof(aes_iv)); |
| 83 |
if (AES_set_encrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) { |
| 84 |
printf("AES_set_encrypt_key failed\n"); |
| 85 |
goto err; |
| 86 |
} |
| 87 |
|
| 88 |
printf("About to AES-CBC encrypt "); |
| 89 |
hexdump(kPlaintext, sizeof(kPlaintext)); |
| 90 |
AES_cbc_encrypt(kPlaintext, output, sizeof(kPlaintext), &aes_key, aes_iv, |
| 91 |
AES_ENCRYPT); |
| 92 |
printf(" got "); |
| 93 |
hexdump(output, sizeof(kPlaintext)); |
| 94 |
|
| 95 |
/* AES-CBC Decryption */ |
| 96 |
OPENSSL_memset(aes_iv, 0, sizeof(aes_iv)); |
| 97 |
if (AES_set_decrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) { |
| 98 |
printf("AES decrypt failed\n"); |
| 99 |
goto err; |
| 100 |
} |
| 101 |
printf("About to AES-CBC decrypt "); |
| 102 |
hexdump(output, sizeof(kPlaintext)); |
| 103 |
AES_cbc_encrypt(output, output, sizeof(kPlaintext), &aes_key, aes_iv, |
| 104 |
AES_DECRYPT); |
| 105 |
printf(" got "); |
| 106 |
hexdump(output, sizeof(kPlaintext)); |
| 107 |
|
| 108 |
OPENSSL_cleanse(&aes_key, sizeof(aes_key)); |
| 109 |
|
| 110 |
/* AES-GCM */ |
| 111 |
size_t out_len; |
| 112 |
uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH]; |
| 113 |
OPENSSL_memset(nonce, 0, sizeof(nonce)); |
| 114 |
EVP_AEAD_CTX aead_ctx; |
| 115 |
if (!EVP_AEAD_CTX_init(&aead_ctx, EVP_aead_aes_128_gcm(), kAESKey, |
| 116 |
sizeof(kAESKey), 0, NULL)) { |
| 117 |
printf("EVP_AEAD_CTX_init failed\n"); |
| 118 |
goto err; |
| 119 |
} |
| 120 |
|
| 121 |
/* AES-GCM Encryption */ |
| 122 |
OPENSSL_memset(aes_iv, 0, sizeof(aes_iv)); |
| 123 |
if (AES_set_encrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) { |
| 124 |
printf("AES_set_encrypt_key failed\n"); |
| 125 |
goto err; |
| 126 |
} |
| 127 |
printf("About to AES-GCM seal "); |
| 128 |
hexdump(output, sizeof(kPlaintext)); |
| 129 |
if (!EVP_AEAD_CTX_seal(&aead_ctx, output, &out_len, sizeof(output), nonce, |
| 130 |
EVP_AEAD_nonce_length(EVP_aead_aes_128_gcm()), |
| 131 |
kPlaintext, sizeof(kPlaintext), NULL, 0)) { |
| 132 |
printf("AES-GCM encrypt failed\n"); |
| 133 |
goto err; |
| 134 |
} |
| 135 |
printf(" got "); |
| 136 |
hexdump(output, out_len); |
| 137 |
|
| 138 |
/* AES-GCM Decryption */ |
| 139 |
OPENSSL_memset(aes_iv, 0, sizeof(aes_iv)); |
| 140 |
printf("About to AES-GCM open "); |
| 141 |
hexdump(output, out_len); |
| 142 |
if (!EVP_AEAD_CTX_open(&aead_ctx, output, &out_len, sizeof(output), nonce, |
| 143 |
EVP_AEAD_nonce_length(EVP_aead_aes_128_gcm()), |
| 144 |
output, out_len, NULL, 0)) { |
| 145 |
printf("AES-GCM decrypt failed\n"); |
| 146 |
goto err; |
| 147 |
} |
| 148 |
printf(" got "); |
| 149 |
hexdump(output, out_len); |
| 150 |
|
| 151 |
EVP_AEAD_CTX_zero(&aead_ctx); |
| 152 |
EVP_AEAD_CTX_cleanup(&aead_ctx); |
| 153 |
|
| 154 |
/* AES-CCM */ |
| 155 |
OPENSSL_memset(nonce, 0, sizeof(nonce)); |
| 156 |
if (!EVP_AEAD_CTX_init(&aead_ctx, EVP_aead_aes_128_ccm_bluetooth(), kAESKey, sizeof(kAESKey), 0, NULL)) { |
| 157 |
fprintf(stderr, "EVP_AED_CTX_init for AES-128-CCM failed.\n"); |
| 158 |
goto err; |
| 159 |
} |
| 160 |
|
| 161 |
/* AES-CCM Encryption */ |
| 162 |
OPENSSL_memset(aes_iv, 0, sizeof(aes_iv)); |
| 163 |
printf("About to AES-CCM seal "); |
| 164 |
hexdump(kPlaintext, sizeof(kPlaintext)); |
| 165 |
if (!EVP_AEAD_CTX_seal(&aead_ctx, output, &out_len, sizeof(output), nonce, |
| 166 |
EVP_AEAD_nonce_length(EVP_aead_aes_128_ccm_bluetooth()), |
| 167 |
kPlaintext, sizeof(kPlaintext), NULL, 0)) { |
| 168 |
fprintf(stderr, "EVP_AEAD_CTX_seal for AES-128-CCM failed.\n"); |
| 169 |
goto err; |
| 170 |
} |
| 171 |
printf(" got "); |
| 172 |
hexdump(output, out_len); |
| 173 |
|
| 174 |
/* AES-CCM Decryption */ |
| 175 |
OPENSSL_memset(aes_iv, 0, sizeof(aes_iv)); |
| 176 |
if (AES_set_decrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) { |
| 177 |
printf("AES decrypt failed\n"); |
| 178 |
goto err; |
| 179 |
} |
| 180 |
printf("About to AES-CCM open "); |
| 181 |
hexdump(output, out_len); |
| 182 |
if (!EVP_AEAD_CTX_open(&aead_ctx, output, &out_len, sizeof(output), nonce, |
| 183 |
EVP_AEAD_nonce_length(EVP_aead_aes_128_ccm_bluetooth()), |
| 184 |
output, out_len, NULL, 0)) { |
| 185 |
fprintf(stderr, "EVP_AEAD_CTX_open for AES-128-CCM failed.\n"); |
| 186 |
goto err; |
| 187 |
} |
| 188 |
printf(" got "); |
| 189 |
hexdump(output, out_len); |
| 190 |
|
| 191 |
OPENSSL_cleanse(&aes_key, sizeof(aes_key)); |
| 192 |
EVP_AEAD_CTX_zero(&aead_ctx); |
| 193 |
|
| 194 |
/* AES-ECB */ |
| 195 |
/* AES-ECB Encryption */ |
| 196 |
OPENSSL_memset(aes_iv, 0, sizeof(aes_iv)); |
| 197 |
if (AES_set_encrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) { |
| 198 |
fprintf(stderr, "AES_set_encrypt_key failed.\n"); |
| 199 |
goto err; |
| 200 |
} |
| 201 |
printf("About to AES-ECB encrypt "); |
| 202 |
hexdump(kPlaintext, sizeof(kPlaintext)); |
| 203 |
for (size_t j = 0; j < sizeof(kPlaintext) / 16; j++) { |
| 204 |
AES_ecb_encrypt(&kPlaintext[j * 16], &output[j * 16], &aes_key, AES_ENCRYPT); |
| 205 |
} |
| 206 |
printf(" got "); |
| 207 |
hexdump(output, sizeof(kPlaintext)); |
| 208 |
|
| 209 |
/* AES-ECB Decryption */ |
| 210 |
OPENSSL_memset(aes_iv, 0, sizeof(aes_iv)); |
| 211 |
if (AES_set_decrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) { |
| 212 |
printf("AES decrypt failed\n"); |
| 213 |
goto err; |
| 214 |
} |
| 215 |
printf("About to AES-ECB decrypt "); |
| 216 |
hexdump(output, sizeof(kPlaintext)); |
| 217 |
for (size_t j = 0; j < sizeof(kPlaintext) / 16; j++) { |
| 218 |
AES_ecb_encrypt(&output[j * 16], &output[j * 16], &aes_key, AES_DECRYPT); |
| 219 |
} |
| 220 |
printf(" got "); |
| 221 |
hexdump(output, sizeof(kPlaintext)); |
| 222 |
|
| 223 |
OPENSSL_cleanse(&aes_key, sizeof(aes_key)); |
| 224 |
|
| 225 |
unsigned int num = 0; |
| 226 |
uint8_t ecount_buf[128]; |
| 227 |
|
| 228 |
/* AES-CTR */ |
| 229 |
/* AES-CTR Encryption */ |
| 230 |
OPENSSL_memset(aes_iv, 0, sizeof(aes_iv)); |
| 231 |
if (AES_set_encrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) { |
| 232 |
fprintf(stderr, "AES_set_encrypt_key failed.\n"); |
| 233 |
goto err; |
| 234 |
} |
| 235 |
printf("About to AES-CTR Encrypt "); |
| 236 |
hexdump(kPlaintext, sizeof(kPlaintext)); |
| 237 |
AES_ctr128_encrypt(kPlaintext, output, sizeof(kPlaintext), &aes_key, aes_iv, ecount_buf, &num); |
| 238 |
printf(" got "); |
| 239 |
hexdump(output, sizeof(kPlaintext)); |
| 240 |
|
| 241 |
|
| 242 |
/* AES-CTR Decryption */ |
| 243 |
OPENSSL_memset(aes_iv, 0, sizeof(aes_iv)); |
| 244 |
printf("About to AES-CTR Decrypt "); |
| 245 |
hexdump(output, sizeof(kPlaintext)); |
| 246 |
AES_ctr128_encrypt(output, output, sizeof(kPlaintext), &aes_key, aes_iv, ecount_buf, &num); |
| 247 |
printf(" got "); |
| 248 |
hexdump(output, sizeof(kPlaintext)); |
| 249 |
|
| 250 |
OPENSSL_cleanse(&aes_key, sizeof(aes_key)); |
| 251 |
|
| 252 |
/* AES-KW Wrap */ |
| 253 |
if (AES_set_encrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) { |
| 254 |
fprintf(stderr, "AES_set_encrypt_key failed.\n"); |
| 255 |
goto err; |
| 256 |
} |
| 257 |
printf("About to AES-KW Wrap "); |
| 258 |
hexdump(kPlaintext, sizeof(kPlaintext)); |
| 259 |
out_len = AES_wrap_key(&aes_key, NULL, output, kPlaintext, sizeof(kPlaintext)); |
| 260 |
printf(" got "); |
| 261 |
hexdump(output, out_len); |
| 262 |
|
| 263 |
/* AES-KW Unwrap */ |
| 264 |
if (AES_set_decrypt_key(kAESKey, 8 * sizeof(kAESKey), &aes_key) != 0) { |
| 265 |
fprintf(stderr, "AES decrypt failed.\n"); |
| 266 |
goto err; |
| 267 |
} |
| 268 |
printf("About to AES-KW Unwrap "); |
| 269 |
hexdump(output, out_len); |
| 270 |
out_len = AES_unwrap_key(&aes_key, NULL, output, output, out_len); |
| 271 |
printf(" got "); |
| 272 |
hexdump(output, out_len); |
| 273 |
|
| 274 |
OPENSSL_cleanse(&aes_key, sizeof(aes_key)); |
| 275 |
|
| 276 |
/* SHA-1 */ |
| 277 |
printf("About to SHA-1 hash "); |
| 278 |
hexdump(kPlaintext, sizeof(kPlaintext)); |
| 279 |
SHA1(kPlaintext, sizeof(kPlaintext), output); |
| 280 |
printf(" got "); |
| 281 |
hexdump(output, SHA_DIGEST_LENGTH); |
| 282 |
|
| 283 |
/* SHA-256 */ |
| 284 |
printf("About to SHA-256 hash "); |
| 285 |
hexdump(kPlaintext, sizeof(kPlaintext)); |
| 286 |
SHA256(kPlaintext, sizeof(kPlaintext), output); |
| 287 |
printf(" got "); |
| 288 |
hexdump(output, SHA256_DIGEST_LENGTH); |
| 289 |
|
| 290 |
/* SHA-512 */ |
| 291 |
printf("About to SHA-512 hash "); |
| 292 |
hexdump(kPlaintext, sizeof(kPlaintext)); |
| 293 |
SHA512(kPlaintext, sizeof(kPlaintext), output); |
| 294 |
printf(" got "); |
| 295 |
hexdump(output, SHA512_DIGEST_LENGTH); |
| 296 |
|
| 297 |
RSA *rsa_key = RSA_new(); |
| 298 |
printf("About to generate RSA key\n"); |
| 299 |
if (!RSA_generate_key_fips(rsa_key, 2048, NULL)) { |
| 300 |
printf("RSA_generate_key_fips failed\n"); |
| 301 |
goto err; |
| 302 |
} |
| 303 |
|
| 304 |
/* RSA Sign */ |
| 305 |
unsigned sig_len; |
| 306 |
printf("About to RSA sign "); |
| 307 |
hexdump(kPlaintextSHA256, sizeof(kPlaintextSHA256)); |
| 308 |
if (!RSA_sign(NID_sha256, kPlaintextSHA256, sizeof(kPlaintextSHA256), output, |
| 309 |
&sig_len, rsa_key)) { |
| 310 |
printf("RSA Sign failed\n"); |
| 311 |
goto err; |
| 312 |
} |
| 313 |
printf(" got "); |
| 314 |
hexdump(output, sig_len); |
| 315 |
|
| 316 |
/* RSA Verify */ |
| 317 |
printf("About to RSA verify "); |
| 318 |
hexdump(output, sig_len); |
| 319 |
if (!RSA_verify(NID_sha256, kPlaintextSHA256, sizeof(kPlaintextSHA256), |
| 320 |
output, sig_len, rsa_key)) { |
| 321 |
printf("RSA Verify failed.\n"); |
| 322 |
goto err; |
| 323 |
} |
| 324 |
|
| 325 |
RSA_free(rsa_key); |
| 326 |
|
| 327 |
EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 328 |
if (ec_key == NULL) { |
| 329 |
printf("invalid ECDSA key\n"); |
| 330 |
goto err; |
| 331 |
} |
| 332 |
|
| 333 |
printf("About to generate P-256 key\n"); |
| 334 |
if (!EC_KEY_generate_key_fips(ec_key)) { |
| 335 |
printf("EC_KEY_generate_key_fips failed\n"); |
| 336 |
goto err; |
| 337 |
} |
| 338 |
|
| 339 |
/* Primitive Z Computation */ |
| 340 |
const EC_GROUP *const ec_group = EC_KEY_get0_group(ec_key); |
| 341 |
EC_POINT *z_point = EC_POINT_new(ec_group); |
| 342 |
uint8_t z_result[65]; |
| 343 |
printf("About to compute key-agreement Z with P-256:\n"); |
| 344 |
if (!EC_POINT_mul(ec_group, z_point, NULL, EC_KEY_get0_public_key(ec_key), |
| 345 |
EC_KEY_get0_private_key(ec_key), NULL) || |
| 346 |
EC_POINT_point2oct(ec_group, z_point, POINT_CONVERSION_UNCOMPRESSED, |
| 347 |
z_result, sizeof(z_result), |
| 348 |
NULL) != sizeof(z_result)) { |
| 349 |
fprintf(stderr, "EC_POINT_mul failed.\n"); |
| 350 |
goto err; |
| 351 |
} |
| 352 |
EC_POINT_free(z_point); |
| 353 |
|
| 354 |
printf(" got "); |
| 355 |
hexdump(z_result, sizeof(z_result)); |
| 356 |
|
| 357 |
/* ECDSA Sign/Verify PWCT */ |
| 358 |
printf("About to ECDSA sign "); |
| 359 |
hexdump(kPlaintextSHA256, sizeof(kPlaintextSHA256)); |
| 360 |
ECDSA_SIG *sig = |
| 361 |
ECDSA_do_sign(kPlaintextSHA256, sizeof(kPlaintextSHA256), ec_key); |
| 362 |
if (sig == NULL || |
| 363 |
!ECDSA_do_verify(kPlaintextSHA256, sizeof(kPlaintextSHA256), sig, |
| 364 |
ec_key)) { |
| 365 |
printf("ECDSA Sign/Verify PWCT failed.\n"); |
| 366 |
goto err; |
| 367 |
} |
| 368 |
|
| 369 |
ECDSA_SIG_free(sig); |
| 370 |
EC_KEY_free(ec_key); |
| 371 |
|
| 372 |
/* Ed25519 */ |
| 373 |
printf("About to Ed25519 sign "); |
| 374 |
hexdump(kPlaintextSHA256, sizeof(kPlaintextSHA256)); |
| 375 |
uint8_t ed_public_key[ED25519_PUBLIC_KEY_LEN]; |
| 376 |
uint8_t ed_private_key[ED25519_PRIVATE_KEY_LEN]; |
| 377 |
ED25519_keypair(ed_public_key, ed_private_key); |
| 378 |
uint8_t ed_signature[ED25519_SIGNATURE_LEN]; |
| 379 |
if (!ED25519_sign(ed_signature,kPlaintextSHA256, sizeof(kPlaintextSHA256), ed_private_key) || |
| 380 |
!ED25519_verify(kPlaintextSHA256, sizeof(kPlaintextSHA256), ed_signature, ed_public_key)) { |
| 381 |
printf("ED25519 Sign/Verify PWCT failed.\n"); |
| 382 |
goto err; |
| 383 |
} |
| 384 |
printf("got signature "); |
| 385 |
hexdump(ed_signature, sizeof(ed_signature)); |
| 386 |
|
| 387 |
/* Ed25519ph */ |
| 388 |
printf("About to Ed25519ph sign "); |
| 389 |
hexdump(kPlaintextSHA256, sizeof(kPlaintextSHA256)); |
| 390 |
uint8_t ed25519_ph_context[32] = { |
| 391 |
0xfe, 0x52, 0xbb, 0xd2, 0x45, 0x54, 0x46, 0xad, 0xa5, 0x24, 0x6b, 0x5a, |
| 392 |
0xf3, 0xba, 0x82, 0x93, 0x9c, 0xed, 0xa6, 0xa1, 0x8f, 0x59, 0xd3, 0x37, |
| 393 |
0x48, 0xde, 0x40, 0x7a, 0xfe, 0x31, 0x48, 0xd1 |
| 394 |
}; |
| 395 |
if (!ED25519ph_sign(ed_signature, kPlaintextSHA256, sizeof(kPlaintextSHA256), ed_private_key, ed25519_ph_context, sizeof(ed25519_ph_context)) || |
| 396 |
!ED25519ph_verify(kPlaintextSHA256, sizeof(kPlaintextSHA256), ed_signature, ed_public_key, ed25519_ph_context, sizeof(ed25519_ph_context))) { |
| 397 |
printf("ED25519ph Sign/Verify PWCT failed.\n"); |
| 398 |
goto err; |
| 399 |
} |
| 400 |
printf("got signature "); |
| 401 |
hexdump(ed_signature, sizeof(ed_signature)); |
| 402 |
|
| 403 |
/* ML-KEM */ |
| 404 |
printf("About to Generate ML-KEM key\n"); |
| 405 |
EVP_PKEY *kem_raw = NULL; |
| 406 |
EVP_PKEY_CTX *kem_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_KEM, NULL); |
| 407 |
if (kem_ctx == NULL || !EVP_PKEY_CTX_kem_set_params(kem_ctx, NID_MLKEM512) || |
| 408 |
!EVP_PKEY_keygen_init(kem_ctx) || |
| 409 |
!EVP_PKEY_keygen(kem_ctx, &kem_raw)) { |
| 410 |
printf("ML-KEM keygen failed.\n"); |
| 411 |
goto err; |
| 412 |
} |
| 413 |
printf("Generated public key: "); |
| 414 |
hexdump(kem_raw->pkey.kem_key->public_key, kem_raw->pkey.kem_key->kem->public_key_len); |
| 415 |
EVP_PKEY_free(kem_raw); |
| 416 |
EVP_PKEY_CTX_free(kem_ctx); |
| 417 |
|
| 418 |
/* ML-DSA */ |
| 419 |
printf("About to Generate ML-DSA key\n"); |
| 420 |
EVP_PKEY *dsa_raw = NULL; |
| 421 |
EVP_PKEY_CTX *dsa_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_PQDSA, NULL); |
| 422 |
if (dsa_ctx == NULL || !EVP_PKEY_CTX_pqdsa_set_params(dsa_ctx, NID_MLDSA44) || |
| 423 |
!EVP_PKEY_keygen_init(dsa_ctx) || |
| 424 |
!EVP_PKEY_keygen(dsa_ctx, &dsa_raw)) { |
| 425 |
printf("ML-DSA keygen failed.\n"); |
| 426 |
goto err; |
| 427 |
} |
| 428 |
printf("Generated public key: "); |
| 429 |
hexdump(dsa_raw->pkey.pqdsa_key->public_key, dsa_raw->pkey.pqdsa_key->pqdsa->public_key_len); |
| 430 |
EVP_PKEY_free(dsa_raw); |
| 431 |
EVP_PKEY_CTX_free(dsa_ctx); |
| 432 |
|
| 433 |
/* DBRG */ |
| 434 |
CTR_DRBG_STATE drbg; |
| 435 |
printf("About to seed CTR-DRBG with "); |
| 436 |
hexdump(kDRBGEntropy, sizeof(kDRBGEntropy)); |
| 437 |
if (!CTR_DRBG_init(&drbg, kDRBGEntropy, kDRBGPersonalization, |
| 438 |
sizeof(kDRBGPersonalization)) || |
| 439 |
!CTR_DRBG_generate(&drbg, output, sizeof(output), kDRBGAD, |
| 440 |
sizeof(kDRBGAD)) || |
| 441 |
!CTR_DRBG_reseed(&drbg, kDRBGEntropy2, kDRBGAD, sizeof(kDRBGAD)) || |
| 442 |
!CTR_DRBG_generate(&drbg, output, sizeof(output), kDRBGAD, |
| 443 |
sizeof(kDRBGAD))) { |
| 444 |
printf("DRBG failed\n"); |
| 445 |
goto err; |
| 446 |
} |
| 447 |
printf(" generated "); |
| 448 |
hexdump(output, sizeof(output)); |
| 449 |
CTR_DRBG_clear(&drbg); |
| 450 |
|
| 451 |
/* TLS KDF */ |
| 452 |
printf("About to run TLS KDF\n"); |
| 453 |
uint8_t tls_output[32]; |
| 454 |
if (!CRYPTO_tls1_prf(EVP_sha256(), tls_output, sizeof(tls_output), kAESKey, |
| 455 |
sizeof(kAESKey), "foo", 3, kPlaintextSHA256, |
| 456 |
sizeof(kPlaintextSHA256), kPlaintextSHA256, |
| 457 |
sizeof(kPlaintextSHA256))) { |
| 458 |
fprintf(stderr, "TLS KDF failed.\n"); |
| 459 |
goto err; |
| 460 |
} |
| 461 |
printf(" got "); |
| 462 |
hexdump(tls_output, sizeof(tls_output)); |
| 463 |
|
| 464 |
/* FFDH */ |
| 465 |
printf("About to compute FFDH key-agreement:\n"); |
| 466 |
DH *dh = DH_get_rfc7919_2048(); |
| 467 |
uint8_t dh_result[2048/8]; |
| 468 |
if (!dh || |
| 469 |
!DH_generate_key(dh) || |
| 470 |
sizeof(dh_result) != DH_size(dh) || |
| 471 |
DH_compute_key_padded(dh_result, DH_get0_pub_key(dh), dh) != |
| 472 |
sizeof(dh_result)) { |
| 473 |
fprintf(stderr, "FFDH failed.\n"); |
| 474 |
goto err; |
| 475 |
} |
| 476 |
DH_free(dh); |
| 477 |
|
| 478 |
printf(" got "); |
| 479 |
hexdump(dh_result, sizeof(dh_result)); |
| 480 |
|
| 481 |
printf("PASS\n"); |
| 482 |
return 0; |
| 483 |
|
| 484 |
err: |
| 485 |
printf("FAIL\n"); |
| 486 |
fflush(stdout); |
| 487 |
abort(); |
| 488 |
} |