Skip to content

Commit

Permalink
code formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Songling Han <[email protected]>
  • Loading branch information
songlingatpan committed Oct 28, 2024
1 parent 533fbdc commit a50428c
Showing 1 changed file with 111 additions and 111 deletions.
222 changes: 111 additions & 111 deletions src/common/rand/rand_nist.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,139 +27,139 @@ You are solely responsible for determining the appropriateness of using and dist
#include <oqs/aes.h>
#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;
}

0 comments on commit a50428c

Please sign in to comment.