Skip to content

Commit

Permalink
Update example files (#2071)
Browse files Browse the repository at this point in the history
* Update example files to use ML-KEM and ML-DSA

Signed-off-by: Spencer Wilson <[email protected]>

* Call example_sig_stfl in test_cmdline.py

Signed-off-by: Spencer Wilson <[email protected]>

---------

Signed-off-by: Spencer Wilson <[email protected]>
  • Loading branch information
SWilson4 authored Feb 7, 2025
1 parent a554b36 commit b80240c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
54 changes: 27 additions & 27 deletions tests/example_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,55 @@ void cleanup_heap(uint8_t *secret_key, uint8_t *shared_secret_e,
* statically on the stack, calling a specific algorithm's functions
* directly.
*
* The macros OQS_KEM_kyber_768_length_* and the functions
* OQS_KEM_kyber_768_* are only defined if the algorithm
* Kyber-768 was enabled at compile-time which must be
* checked using the OQS_ENABLE_KEM_kyber_768 macro.
* The macros OQS_KEM_ml_kem_768_length_* and the functions
* OQS_KEM_ml_kem_768_* are only defined if the algorithm
* ML-KEM-768 was enabled at compile-time which must be
* checked using the OQS_ENABLE_KEM_ml_kem_768 macro.
*
* <oqs/oqsconfig.h>, which is included in <oqs/oqs.h>, contains macros
* indicating which algorithms were enabled when this instance of liboqs
* was compiled.
*/
static OQS_STATUS example_stack(void) {
#ifndef OQS_ENABLE_KEM_kyber_768 // if Kyber-768 was not enabled at compile-time
printf("[example_stack] OQS_KEM_kyber_768 was not enabled at "
#ifndef OQS_ENABLE_KEM_ml_kem_768 // if ML-KEM-768 was not enabled at compile-time
printf("[example_stack] OQS_KEM_ml_kem_768 was not enabled at "
"compile-time.\n");
return OQS_SUCCESS; // nothing done successfully ;-)
#else
uint8_t public_key[OQS_KEM_kyber_768_length_public_key];
uint8_t secret_key[OQS_KEM_kyber_768_length_secret_key];
uint8_t ciphertext[OQS_KEM_kyber_768_length_ciphertext];
uint8_t shared_secret_e[OQS_KEM_kyber_768_length_shared_secret];
uint8_t shared_secret_d[OQS_KEM_kyber_768_length_shared_secret];
uint8_t public_key[OQS_KEM_ml_kem_768_length_public_key];
uint8_t secret_key[OQS_KEM_ml_kem_768_length_secret_key];
uint8_t ciphertext[OQS_KEM_ml_kem_768_length_ciphertext];
uint8_t shared_secret_e[OQS_KEM_ml_kem_768_length_shared_secret];
uint8_t shared_secret_d[OQS_KEM_ml_kem_768_length_shared_secret];

OQS_STATUS rc = OQS_KEM_kyber_768_keypair(public_key, secret_key);
OQS_STATUS rc = OQS_KEM_ml_kem_768_keypair(public_key, secret_key);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_KEM_kyber_768_keypair failed!\n");
cleanup_stack(secret_key, OQS_KEM_kyber_768_length_secret_key,
fprintf(stderr, "ERROR: OQS_KEM_ml_kem_768_keypair failed!\n");
cleanup_stack(secret_key, OQS_KEM_ml_kem_768_length_secret_key,
shared_secret_e, shared_secret_d,
OQS_KEM_kyber_768_length_shared_secret);
OQS_KEM_ml_kem_768_length_shared_secret);

return OQS_ERROR;
}
rc = OQS_KEM_kyber_768_encaps(ciphertext, shared_secret_e, public_key);
rc = OQS_KEM_ml_kem_768_encaps(ciphertext, shared_secret_e, public_key);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_KEM_kyber_768_encaps failed!\n");
cleanup_stack(secret_key, OQS_KEM_kyber_768_length_secret_key,
fprintf(stderr, "ERROR: OQS_KEM_ml_kem_768_encaps failed!\n");
cleanup_stack(secret_key, OQS_KEM_ml_kem_768_length_secret_key,
shared_secret_e, shared_secret_d,
OQS_KEM_kyber_768_length_shared_secret);
OQS_KEM_ml_kem_768_length_shared_secret);

return OQS_ERROR;
}
rc = OQS_KEM_kyber_768_decaps(shared_secret_d, ciphertext, secret_key);
rc = OQS_KEM_ml_kem_768_decaps(shared_secret_d, ciphertext, secret_key);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_KEM_kyber_768_decaps failed!\n");
cleanup_stack(secret_key, OQS_KEM_kyber_768_length_secret_key,
fprintf(stderr, "ERROR: OQS_KEM_ml_kem_768_decaps failed!\n");
cleanup_stack(secret_key, OQS_KEM_ml_kem_768_length_secret_key,
shared_secret_e, shared_secret_d,
OQS_KEM_kyber_768_length_shared_secret);
OQS_KEM_ml_kem_768_length_shared_secret);

return OQS_ERROR;
}
printf("[example_stack] OQS_KEM_kyber_768 operations completed.\n");
printf("[example_stack] OQS_KEM_ml_kem_768 operations completed.\n");

return OQS_SUCCESS; // success!
#endif
Expand All @@ -100,9 +100,9 @@ static OQS_STATUS example_heap(void) {
uint8_t *shared_secret_e = NULL;
uint8_t *shared_secret_d = NULL;

kem = OQS_KEM_new(OQS_KEM_alg_kyber_768);
kem = OQS_KEM_new(OQS_KEM_alg_ml_kem_768);
if (kem == NULL) {
printf("[example_heap] OQS_KEM_kyber_768 was not enabled at "
printf("[example_heap] OQS_KEM_ml_kem_768 was not enabled at "
"compile-time.\n");
return OQS_SUCCESS;
}
Expand Down Expand Up @@ -146,7 +146,7 @@ static OQS_STATUS example_heap(void) {
return OQS_ERROR;
}

printf("[example_heap] OQS_KEM_kyber_768 operations completed.\n");
printf("[example_heap] OQS_KEM_ml_kem_768 operations completed.\n");
cleanup_heap(secret_key, shared_secret_e, shared_secret_d, public_key,
ciphertext, kem);

Expand Down
48 changes: 24 additions & 24 deletions tests/example_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,56 +27,56 @@ void cleanup_heap(uint8_t *public_key, uint8_t *secret_key,
* statically on the stack, calling a specific algorithm's functions
* directly.
*
* The macros OQS_SIG_dilithium_2_length_* and the functions OQS_SIG_dilithium_2_*
* are only defined if the algorithm dilithium_2 was enabled at compile-time
* which must be checked using the OQS_ENABLE_SIG_dilithium_2 macro.
* The macros OQS_SIG_ml_dsa_65_length_* and the functions OQS_SIG_ml_dsa_65_*
* are only defined if the algorithm ml_dsa_65 was enabled at compile-time
* which must be checked using the OQS_ENABLE_SIG_ml_dsa_65 macro.
*
* <oqs/oqsconfig.h>, which is included in <oqs/oqs.h>, contains macros
* indicating which algorithms were enabled when this instance of liboqs
* was compiled.
*/
static OQS_STATUS example_stack(void) {

#ifdef OQS_ENABLE_SIG_dilithium_2
#ifdef OQS_ENABLE_SIG_ml_dsa_65

OQS_STATUS rc;

uint8_t public_key[OQS_SIG_dilithium_2_length_public_key];
uint8_t secret_key[OQS_SIG_dilithium_2_length_secret_key];
uint8_t public_key[OQS_SIG_ml_dsa_65_length_public_key];
uint8_t secret_key[OQS_SIG_ml_dsa_65_length_secret_key];
uint8_t message[MESSAGE_LEN];
uint8_t signature[OQS_SIG_dilithium_2_length_signature];
uint8_t signature[OQS_SIG_ml_dsa_65_length_signature];
size_t message_len = MESSAGE_LEN;
size_t signature_len;

// let's create a random test message to sign
OQS_randombytes(message, message_len);

rc = OQS_SIG_dilithium_2_keypair(public_key, secret_key);
rc = OQS_SIG_ml_dsa_65_keypair(public_key, secret_key);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_SIG_dilithium_2_keypair failed!\n");
cleanup_stack(secret_key, OQS_SIG_dilithium_2_length_secret_key);
fprintf(stderr, "ERROR: OQS_SIG_ml_dsa_65_keypair failed!\n");
cleanup_stack(secret_key, OQS_SIG_ml_dsa_65_length_secret_key);
return OQS_ERROR;
}
rc = OQS_SIG_dilithium_2_sign(signature, &signature_len, message, message_len, secret_key);
rc = OQS_SIG_ml_dsa_65_sign(signature, &signature_len, message, message_len, secret_key);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_SIG_dilithium_2_sign failed!\n");
cleanup_stack(secret_key, OQS_SIG_dilithium_2_length_secret_key);
fprintf(stderr, "ERROR: OQS_SIG_ml_dsa_65_sign failed!\n");
cleanup_stack(secret_key, OQS_SIG_ml_dsa_65_length_secret_key);
return OQS_ERROR;
}
rc = OQS_SIG_dilithium_2_verify(message, message_len, signature, signature_len, public_key);
rc = OQS_SIG_ml_dsa_65_verify(message, message_len, signature, signature_len, public_key);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_SIG_dilithium_2_verify failed!\n");
cleanup_stack(secret_key, OQS_SIG_dilithium_2_length_secret_key);
fprintf(stderr, "ERROR: OQS_SIG_ml_dsa_65_verify failed!\n");
cleanup_stack(secret_key, OQS_SIG_ml_dsa_65_length_secret_key);
return OQS_ERROR;
}

printf("[example_stack] OQS_SIG_dilithium_2 operations completed.\n");
cleanup_stack(secret_key, OQS_SIG_dilithium_2_length_secret_key);
printf("[example_stack] OQS_SIG_ml_dsa_65 operations completed.\n");
cleanup_stack(secret_key, OQS_SIG_ml_dsa_65_length_secret_key);
return OQS_SUCCESS; // success!

#else

printf("[example_stack] OQS_SIG_dilithium_2 was not enabled at compile-time.\n");
printf("[example_stack] OQS_SIG_ml_dsa_65 was not enabled at compile-time.\n");
return OQS_SUCCESS;

#endif
Expand All @@ -92,7 +92,7 @@ static OQS_STATUS example_stack(void) {
*/
static OQS_STATUS example_heap(void) {

#ifdef OQS_ENABLE_SIG_dilithium_2
#ifdef OQS_ENABLE_SIG_ml_dsa_65

OQS_SIG *sig = NULL;
uint8_t *public_key = NULL;
Expand All @@ -103,9 +103,9 @@ static OQS_STATUS example_heap(void) {
size_t signature_len;
OQS_STATUS rc;

sig = OQS_SIG_new(OQS_SIG_alg_dilithium_2);
sig = OQS_SIG_new(OQS_SIG_alg_ml_dsa_65);
if (sig == NULL) {
printf("[example_heap] OQS_SIG_alg_dilithium_2 was not enabled at compile-time.\n");
printf("[example_heap] OQS_SIG_alg_ml_dsa_65 was not enabled at compile-time.\n");
return OQS_ERROR;
}

Expand Down Expand Up @@ -141,12 +141,12 @@ static OQS_STATUS example_heap(void) {
return OQS_ERROR;
}

printf("[example_heap] OQS_SIG_dilithium_2 operations completed.\n");
printf("[example_heap] OQS_SIG_ml_dsa_65 operations completed.\n");
cleanup_heap(public_key, secret_key, message, signature, sig);
return OQS_SUCCESS; // success
#else

printf("[example_heap] OQS_SIG_dilithium_2 was not enabled at compile-time.\n");
printf("[example_heap] OQS_SIG_ml_dsa_65 was not enabled at compile-time.\n");
return OQS_SUCCESS;

#endif
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys

@helpers.filtered_test
@pytest.mark.parametrize('program', ['example_kem', 'example_sig'])
@pytest.mark.parametrize('program', ['example_kem', 'example_sig', 'example_sig_stfl'])
def test_examples(program):
helpers.run_subprocess(
[helpers.path_to_executable(program)],
Expand Down

0 comments on commit b80240c

Please sign in to comment.