Skip to content

Commit

Permalink
Remove use of mutex status bool. Use recursive mutex” src/sig_stfl/lm…
Browse files Browse the repository at this point in the history
…s/sig_stfl_lms.c src/sig_stfl/xmss/sig_stfl_xmss_secret_key_functions.c tests/test_sig_stfl.c
  • Loading branch information
ashman-p committed Oct 25, 2023
1 parent fc0a7f4 commit 28e8773
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 103 deletions.
60 changes: 0 additions & 60 deletions src/sig_stfl/lms/sig_stfl_lms.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H5_W1_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -172,9 +169,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H5_W2_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -262,9 +256,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H5_W4_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -352,9 +343,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H5_W8_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -442,9 +430,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H10_W1_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -532,9 +517,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H10_W2_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -622,9 +604,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H10_W4_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -712,9 +691,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H10_W8_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -802,9 +778,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H15_W1_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -892,9 +865,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H15_W2_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -982,9 +952,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H15_W4_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -1072,9 +1039,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H15_W8_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -1162,9 +1126,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H20_W1_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -1252,9 +1213,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H20_W2_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -1342,9 +1300,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H20_W4_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -1432,9 +1387,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H20_W8_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -1522,9 +1474,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H25_W1_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -1612,9 +1561,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H25_W2_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -1702,9 +1648,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H25_W4_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down Expand Up @@ -1792,9 +1735,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_LMS_SHA256_H25_W8_new(void) {
*/
sk->lock_key = NULL;

/* Boolean if the secret key is locked */
sk->is_locked = false;

/*
* Set Secret Key Unlocking / Releasing Function
*/
Expand Down
21 changes: 6 additions & 15 deletions src/sig_stfl/xmss/sig_stfl_xmss_secret_key_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ OQS_SIG_STFL_SECRET_KEY *OQS_SECRET_KEY_XMSS_new(size_t length_secret_key) {
// Set Secret Key unlocking / releasing function
sk->unlock_key = NULL;

// Boolean if the secret key is locked
sk->is_locked = false;

// Set Secret Key saving function
sk->secure_store_scrt_key = NULL;

Expand Down Expand Up @@ -127,12 +124,9 @@ void OQS_SECRET_KEY_XMSS_activate_lock(OQS_SIG_STFL_SECRET_KEY *sk) {
return;
}

if (sk->is_locked == false) {
/* Lock the key if possible */
if ((sk->lock_key != NULL) && (sk->mutex != NULL)) {
sk->lock_key(sk->mutex);
sk->is_locked = true;
}
/* Lock the key if possible */
if ((sk->lock_key != NULL) && (sk->mutex != NULL)) {
sk->lock_key(sk->mutex);
}
}

Expand All @@ -141,11 +135,8 @@ void OQS_SECRET_KEY_XMSS_activate_unlock(OQS_SIG_STFL_SECRET_KEY *sk) {
return;
}

if (sk->is_locked == true) {
/* Unlock the key if possible */
if ((sk->unlock_key != NULL) && (sk->mutex != NULL)) {
sk->unlock_key(sk->mutex);
sk->is_locked = false;
}
/* Unlock the key if possible */
if ((sk->unlock_key != NULL) && (sk->mutex != NULL)) {
sk->unlock_key(sk->mutex);
}
}
65 changes: 37 additions & 28 deletions tests/test_sig_stfl.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <pthread.h>

static pthread_mutex_t *test_sk_lock = NULL;
static pthread_mutex_t *sk_lock = NULL;
#endif

#ifdef OQS_ENABLE_TEST_CONSTANT_TIME
Expand Down Expand Up @@ -247,7 +248,7 @@ OQS_STATUS sig_stfl_keypair_from_KATs(OQS_SIG_STFL *sig, uint8_t *public_key, OQ
* XMSSMT-SHAKE_60/3_256
*/
OQS_STATUS sig_stfl_KATs_keygen(OQS_SIG_STFL *sig, uint8_t *public_key, OQS_SIG_STFL_SECRET_KEY *secret_key, const char *katfile) {
if (sig == NULL || public_key == NULL || secret_key == NULL || katfile == NULL) {
if (sig == NULL || public_key == NULL || secret_key == NULL ) {
return OQS_ERROR;
}

Expand Down Expand Up @@ -386,10 +387,6 @@ static OQS_STATUS sig_stfl_test_correctness(const char *method_name, const char

magic_t magic;

#if OQS_USE_PTHREADS_IN_TESTS
pthread_mutex_t *sk_lock = NULL;
#endif

OQS_STATUS rc, ret = OQS_ERROR;

//The magic numbers are random values.
Expand Down Expand Up @@ -423,14 +420,6 @@ static OQS_STATUS sig_stfl_test_correctness(const char *method_name, const char
OQS_SIG_STFL_SECRET_KEY_SET_store_cb(secret_key, save_secret_key, (void *)context);

#if OQS_USE_PTHREADS_IN_TESTS
sk_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
if (sk_lock == NULL) {
goto err;
}

if (0 != pthread_mutex_init(sk_lock, 0)) {
goto err;
}
OQS_SIG_STFL_SECRET_KEY_SET_mutex(secret_key, sk_lock);
#endif
public_key = malloc(sig->length_public_key + 2 * sizeof(magic_t));
Expand Down Expand Up @@ -559,12 +548,6 @@ static OQS_STATUS sig_stfl_test_correctness(const char *method_name, const char
OQS_MEM_insecure_free(context);
OQS_MEM_insecure_free(file_store);

#if OQS_USE_PTHREADS_IN_TESTS
if (sk_lock) {
pthread_mutex_destroy(sk_lock);
OQS_MEM_insecure_free(sk_lock);
}
#endif
return ret;
}

Expand Down Expand Up @@ -902,15 +885,6 @@ static OQS_STATUS sig_stfl_test_secret_key_lock(const char *method_name, const c
OQS_SIG_STFL_SECRET_KEY_SET_unlock(lock_test_sk, unlock_sk_key);

#if OQS_USE_PTHREADS_IN_TESTS

test_sk_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
if (test_sk_lock == NULL) {
goto err;
}

if (0 != pthread_mutex_init(test_sk_lock, 0)) {
goto err;
}
OQS_SIG_STFL_SECRET_KEY_SET_mutex(lock_test_sk, test_sk_lock);
#endif

Expand Down Expand Up @@ -1063,6 +1037,33 @@ int main(int argc, char **argv) {
td_create.katfile = katfile;
td_sign.katfile = katfile;
td_query.katfile = katfile;
pthread_mutexattr_t attr1, attr2;

test_sk_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
if (test_sk_lock == NULL) {
goto err;
}
sk_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
if (sk_lock == NULL) {
goto err;
}

if (0 != pthread_mutexattr_init(&attr1)) {
goto err;
}
if (0 != pthread_mutexattr_init(&attr2)) {
goto err;
}

pthread_mutexattr_settype(&attr1, PTHREAD_MUTEX_RECURSIVE);
pthread_mutexattr_settype(&attr2, PTHREAD_MUTEX_RECURSIVE);

if (0 != pthread_mutex_init(test_sk_lock, &attr1)) {
goto err;
}
if (0 != pthread_mutex_init(test_sk_lock, &attr2)) {
goto err;
}

int trc = pthread_create(&thread, NULL, test_wrapper, &td);
if (trc) {
Expand Down Expand Up @@ -1100,6 +1101,14 @@ int main(int argc, char **argv) {
}
pthread_join(query_key_thread, NULL);
rc_qry = td_query.rc;

err:
if (test_sk_lock) {
pthread_mutex_destroy(test_sk_lock);
}
if (sk_lock) {
pthread_mutex_destroy(sk_lock);
}
#else
rc = sig_stfl_test_correctness(alg_name, katfile);
rc1 = sig_stfl_test_secret_key(alg_name, katfile);
Expand Down

0 comments on commit 28e8773

Please sign in to comment.