From a50428ca2991c055cc1c36cc31f7d10737e1530e Mon Sep 17 00:00:00 2001 From: Songling Han Date: Mon, 28 Oct 2024 07:39:02 +0000 Subject: [PATCH] code formatting Signed-off-by: Songling Han --- src/common/rand/rand_nist.c | 222 ++++++++++++++++++------------------ 1 file changed, 111 insertions(+), 111 deletions(-) diff --git a/src/common/rand/rand_nist.c b/src/common/rand/rand_nist.c index 21db19093..57a9cafd1 100644 --- a/src/common/rand/rand_nist.c +++ b/src/common/rand/rand_nist.c @@ -27,139 +27,139 @@ You are solely responsible for determining the appropriateness of using and dist #include #endif - OQS_STATUS OQS_randombytes_nist_kat(unsigned char *x, size_t xlen); +OQS_STATUS OQS_randombytes_nist_kat(unsigned char *x, size_t xlen); - static OQS_NIST_DRBG_struct DRBG_ctx; - static OQS_STATUS AES256_CTR_DRBG_Update(unsigned char *provided_data, unsigned char *Key, unsigned char *V); +static OQS_NIST_DRBG_struct DRBG_ctx; +static OQS_STATUS AES256_CTR_DRBG_Update(unsigned char *provided_data, unsigned char *Key, unsigned char *V); - // Use whatever AES implementation you have. This uses AES from openSSL library - // key - 256-bit AES key - // ctr - a 128-bit plaintext value - // buffer - a 128-bit ciphertext value - static OQS_STATUS AES256_ECB(unsigned char *key, unsigned char *ctr, unsigned char *buffer) { - #ifdef OQS_USE_OPENSSL - EVP_CIPHER_CTX *ctx; +// Use whatever AES implementation you have. This uses AES from openSSL library +// key - 256-bit AES key +// ctr - a 128-bit plaintext value +// buffer - a 128-bit ciphertext value +static OQS_STATUS AES256_ECB(unsigned char *key, unsigned char *ctr, unsigned char *buffer) { +#ifdef OQS_USE_OPENSSL + EVP_CIPHER_CTX *ctx; - int len; + int len; - /* Create and initialise the context */ - ctx = OSSL_FUNC(EVP_CIPHER_CTX_new)(); - if (ctx == NULL) { - return OQS_ERROR; - } - - if (OSSL_FUNC(EVP_EncryptInit_ex)(ctx, oqs_aes_256_ecb(), NULL, key, NULL) != 1 || - OSSL_FUNC(EVP_EncryptUpdate)(ctx, buffer, &len, ctr, 16) != 1) { - OSSL_FUNC(EVP_CIPHER_CTX_free)(ctx); - return OQS_ERROR; - } + /* Create and initialise the context */ + ctx = OSSL_FUNC(EVP_CIPHER_CTX_new)(); + if (ctx == NULL) { + return OQS_ERROR; + } - /* Clean up */ + if (OSSL_FUNC(EVP_EncryptInit_ex)(ctx, oqs_aes_256_ecb(), NULL, key, NULL) != 1 || + OSSL_FUNC(EVP_EncryptUpdate)(ctx, buffer, &len, ctr, 16) != 1) { OSSL_FUNC(EVP_CIPHER_CTX_free)(ctx); - #else - void *schedule = NULL; - OQS_AES256_ECB_load_schedule(key, &schedule); - OQS_AES256_ECB_enc(ctr, 16, key, buffer); - OQS_AES256_free_schedule(schedule); - #endif - return OQS_SUCCESS; + return OQS_ERROR; } - OQS_STATUS OQS_randombytes_nist_kat_init_256bit(const uint8_t *entropy_input, const uint8_t *personalization_string) { - unsigned char seed_material[48]; + /* Clean up */ + OSSL_FUNC(EVP_CIPHER_CTX_free)(ctx); +#else + void *schedule = NULL; + OQS_AES256_ECB_load_schedule(key, &schedule); + OQS_AES256_ECB_enc(ctr, 16, key, buffer); + OQS_AES256_free_schedule(schedule); +#endif + return OQS_SUCCESS; +} - memcpy(seed_material, entropy_input, 48); - if (personalization_string) - for (int i = 0; i < 48; i++) { - seed_material[i] ^= personalization_string[i]; - } - memset(DRBG_ctx.Key, 0x00, 32); - memset(DRBG_ctx.V, 0x00, 16); - if (AES256_CTR_DRBG_Update(seed_material, DRBG_ctx.Key, DRBG_ctx.V) != OQS_SUCCESS) { - return OQS_ERROR; +OQS_STATUS OQS_randombytes_nist_kat_init_256bit(const uint8_t *entropy_input, const uint8_t *personalization_string) { + unsigned char seed_material[48]; + + memcpy(seed_material, entropy_input, 48); + if (personalization_string) + for (int i = 0; i < 48; i++) { + seed_material[i] ^= personalization_string[i]; } - DRBG_ctx.reseed_counter = 1; - return OQS_SUCCESS; + memset(DRBG_ctx.Key, 0x00, 32); + memset(DRBG_ctx.V, 0x00, 16); + if (AES256_CTR_DRBG_Update(seed_material, DRBG_ctx.Key, DRBG_ctx.V) != OQS_SUCCESS) { + return OQS_ERROR; } - - OQS_STATUS OQS_randombytes_nist_kat(unsigned char *x, size_t xlen) { - unsigned char block[16]; - int i = 0; - - while (xlen > 0) { - //increment V - for (int j = 15; j >= 0; j--) { - if (DRBG_ctx.V[j] == 0xff) { - DRBG_ctx.V[j] = 0x00; - } else { - DRBG_ctx.V[j]++; - break; - } - } - if (AES256_ECB(DRBG_ctx.Key, DRBG_ctx.V, block) != OQS_SUCCESS) { - return OQS_ERROR; - } - if (xlen > 15) { - memcpy(x + i, block, 16); - i += 16; - xlen -= 16; + DRBG_ctx.reseed_counter = 1; + return OQS_SUCCESS; +} + +OQS_STATUS OQS_randombytes_nist_kat(unsigned char *x, size_t xlen) { + unsigned char block[16]; + int i = 0; + + while (xlen > 0) { + //increment V + for (int j = 15; j >= 0; j--) { + if (DRBG_ctx.V[j] == 0xff) { + DRBG_ctx.V[j] = 0x00; } else { - memcpy(x + i, block, xlen); - xlen = 0; + DRBG_ctx.V[j]++; + break; } } - if (AES256_CTR_DRBG_Update(NULL, DRBG_ctx.Key, DRBG_ctx.V) != OQS_SUCCESS) { + if (AES256_ECB(DRBG_ctx.Key, DRBG_ctx.V, block) != OQS_SUCCESS) { return OQS_ERROR; } - DRBG_ctx.reseed_counter++; - return OQS_SUCCESS; - } - - OQS_STATUS OQS_randombytes_nist_kat_get_state(void *out) { - OQS_NIST_DRBG_struct *out_state = (OQS_NIST_DRBG_struct *)out; - if (out_state != NULL) { - memcpy(out_state->Key, DRBG_ctx.Key, sizeof(DRBG_ctx.Key)); - memcpy(out_state->V, DRBG_ctx.V, sizeof(DRBG_ctx.V)); - out_state->reseed_counter = DRBG_ctx.reseed_counter; - return OQS_SUCCESS; + if (xlen > 15) { + memcpy(x + i, block, 16); + i += 16; + xlen -= 16; + } else { + memcpy(x + i, block, xlen); + xlen = 0; } - return OQS_ERROR; } - - OQS_STATUS OQS_randombytes_nist_kat_set_state(const void *in) { - const OQS_NIST_DRBG_struct *in_state = (const OQS_NIST_DRBG_struct *)in; - if (in_state != NULL) { - memcpy(DRBG_ctx.Key, in_state->Key, sizeof(DRBG_ctx.Key)); - memcpy(DRBG_ctx.V, in_state->V, sizeof(DRBG_ctx.V)); - DRBG_ctx.reseed_counter = in_state->reseed_counter; - return OQS_SUCCESS; - } + if (AES256_CTR_DRBG_Update(NULL, DRBG_ctx.Key, DRBG_ctx.V) != OQS_SUCCESS) { return OQS_ERROR; } + DRBG_ctx.reseed_counter++; + return OQS_SUCCESS; +} + +OQS_STATUS OQS_randombytes_nist_kat_get_state(void *out) { + OQS_NIST_DRBG_struct *out_state = (OQS_NIST_DRBG_struct *)out; + if (out_state != NULL) { + memcpy(out_state->Key, DRBG_ctx.Key, sizeof(DRBG_ctx.Key)); + memcpy(out_state->V, DRBG_ctx.V, sizeof(DRBG_ctx.V)); + out_state->reseed_counter = DRBG_ctx.reseed_counter; + return OQS_SUCCESS; + } + return OQS_ERROR; +} + +OQS_STATUS OQS_randombytes_nist_kat_set_state(const void *in) { + const OQS_NIST_DRBG_struct *in_state = (const OQS_NIST_DRBG_struct *)in; + if (in_state != NULL) { + memcpy(DRBG_ctx.Key, in_state->Key, sizeof(DRBG_ctx.Key)); + memcpy(DRBG_ctx.V, in_state->V, sizeof(DRBG_ctx.V)); + DRBG_ctx.reseed_counter = in_state->reseed_counter; + return OQS_SUCCESS; + } + return OQS_ERROR; +} - static OQS_STATUS AES256_CTR_DRBG_Update(unsigned char *provided_data, unsigned char *Key, unsigned char *V) { - unsigned char temp[48]; - - for (int i = 0; i < 3; i++) { - //increment V - for (int j = 15; j >= 0; j--) { - if (V[j] == 0xff) { - V[j] = 0x00; - } else { - V[j]++; - break; - } - } +static OQS_STATUS AES256_CTR_DRBG_Update(unsigned char *provided_data, unsigned char *Key, unsigned char *V) { + unsigned char temp[48]; - if (AES256_ECB(Key, V, temp + 16 * i) != OQS_SUCCESS) { - return OQS_ERROR; + for (int i = 0; i < 3; i++) { + //increment V + for (int j = 15; j >= 0; j--) { + if (V[j] == 0xff) { + V[j] = 0x00; + } else { + V[j]++; + break; } } - if (provided_data != NULL) - for (int i = 0; i < 48; i++) { - temp[i] ^= provided_data[i]; - } - memcpy(Key, temp, 32); - memcpy(V, temp + 32, 16); - return OQS_SUCCESS; + + if (AES256_ECB(Key, V, temp + 16 * i) != OQS_SUCCESS) { + return OQS_ERROR; + } } + if (provided_data != NULL) + for (int i = 0; i < 48; i++) { + temp[i] ^= provided_data[i]; + } + memcpy(Key, temp, 32); + memcpy(V, temp + 32, 16); + return OQS_SUCCESS; +}