diff --git a/src/dap_enc_ringct20.c b/src/dap_enc_ringct20.c index 9fbb1eec7796c05bee45343f5e35f524690128cb..ba98dbf91cc5800693294b5f3fe3d7b1f4fbeb21 100644 --- a/src/dap_enc_ringct20.c +++ b/src/dap_enc_ringct20.c @@ -148,7 +148,7 @@ int ringct20_crypto_sign( ringct20_signature_t *sig, const unsigned char *m, uns //Pi %= p->wLen;// //CRUTCH Pi = 1 + Pi%(p->wLen - 1); - printf("Pi = %d\n", Pi); + //printf("Pi = %d\n", Pi); poly_ringct20 *aList = malloc(p->POLY_RINGCT20_SIZE*p->wLen); poly_ringct20 *S = malloc(p->POLY_RINGCT20_SIZE*p->mLen); ringct20_unpack_prk(private_key->data,S,p); @@ -188,7 +188,7 @@ int ringct20_crypto_sign( ringct20_signature_t *sig, const unsigned char *m, uns for (int k = 0; k < p->M; k++) { - OQS_randombytes(bt, NEWHOPE_POLYBYTES); + randombytes(bt, NEWHOPE_POLYBYTES); poly_frombytes(u + k, bt); poly_serial(u + k); //poly_print(u+k); @@ -259,7 +259,6 @@ int ringct20_crypto_sign_open( const unsigned char * msg, const unsigned long lo result = 1 ^ LRCT_SigVer(&c1, t, p->A, p->H, p->mLen, &h, aList, p->wLen, msg, msg_size); - printf("result = %d\n", result); free(aList); for(int i = 0; i < p->wLen; ++i) diff --git a/src/ringct20/rand.c b/src/ringct20/rand.c deleted file mode 100644 index 3b6578676e9c4da2cbcddbf3de8cf05abf4f81db..0000000000000000000000000000000000000000 --- a/src/ringct20/rand.c +++ /dev/null @@ -1,121 +0,0 @@ -#include <stdio.h> -#if defined(_WIN32) -#include <windows.h> -#include <Wincrypt.h> -#include <assert.h> -#define strcasecmp _stricmp -#else -#include <stdlib.h> -#include <unistd.h> -#include <strings.h> -#if defined(__APPLE__) -#include <sys/random.h> -#else -#include <unistd.h> -#endif -#endif -#include <fcntl.h> - -#include "oqs.h" - -void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read); -void OQS_randombytes_nist_kat(uint8_t *random_array, size_t bytes_to_read); -#ifdef USE_OPENSSL -void OQS_randombytes_openssl(uint8_t *random_array, size_t bytes_to_read); -#endif - -#ifdef USE_OPENSSL -#include <openssl/rand.h> -// Use OpenSSL's RAND_bytes as the default PRNG -static void (*oqs_randombytes_algorithm)(uint8_t *, size_t) = &OQS_randombytes_openssl; -#else -static void (*oqs_randombytes_algorithm)(uint8_t *, size_t) = &OQS_randombytes_system; -#endif - -OQS_API OQS_STATUS OQS_randombytes_switch_algorithm(const char *algorithm) { - if (0 == strcasecmp(OQS_RAND_alg_system, algorithm)) { - oqs_randombytes_algorithm = &OQS_randombytes_system; - return OQS_SUCCESS; -// } else if (0 == strcasecmp(OQS_RAND_alg_nist_kat, algorithm)) { -// oqs_randombytes_algorithm = &OQS_randombytes_nist_kat; -// return OQS_SUCCESS; - } else if (0 == strcasecmp(OQS_RAND_alg_openssl, algorithm)) { -#ifdef USE_OPENSSL - oqs_randombytes_algorithm = &OQS_randombytes_openssl; - return OQS_SUCCESS; -#else - return OQS_ERROR; -#endif - } else { - return OQS_ERROR; - } -} - -OQS_API void OQS_randombytes_custom_algorithm(void (*algorithm_ptr)(uint8_t *, size_t)) { - oqs_randombytes_algorithm = algorithm_ptr; -} - -OQS_API void OQS_randombytes(uint8_t *random_array, size_t bytes_to_read) { - oqs_randombytes_algorithm(random_array, bytes_to_read); -} - -#if !defined(_WIN32) -#if defined(HAVE_GETENTROPY) -void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { - - int rc; - do { - rc = getentropy(random_array, bytes_to_read); - } while (rc != 0); -} -#else -static __inline void delay(unsigned int count) { - while (count--) { - } -} - -void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { - - FILE *handle; - do { - handle = fopen("/dev/urandom", "rb"); - if (handle == NULL) { - delay(0xFFFFF); - } - } while (handle == NULL); - - int bytes_last_read, bytes_total_read, bytes_left_to_read; - bytes_total_read = 0; - bytes_left_to_read = bytes_to_read; - while (bytes_left_to_read > 0) { - do { - bytes_last_read = fread(random_array + bytes_total_read, 1, bytes_left_to_read, handle); - if (bytes_last_read <= 0) { - delay(0xFFFF); - } - } while (bytes_last_read <= 0); - bytes_total_read += bytes_last_read; - bytes_left_to_read -= bytes_last_read; - } - fclose(handle); -} -#endif -#else -void OQS_randombytes_system(uint8_t *random_array, size_t bytes_to_read) { - HCRYPTPROV hCryptProv; - if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) || - !CryptGenRandom(hCryptProv, (DWORD) bytes_to_read, random_array)) { - assert(0); // no other way to return an error; better fail than return bad random data - } - CryptReleaseContext(hCryptProv, 0); -} -#endif - -#ifdef USE_OPENSSL -void OQS_randombytes_openssl(uint8_t *random_array, size_t bytes_to_read) { - int rc; - do { - rc = RAND_bytes(random_array, bytes_to_read); - } while (rc != 1); -} -#endif diff --git a/src/ringct20/rand.h b/src/ringct20/rand.h deleted file mode 100644 index 662f7f2cb0e534ba2353b4aba60737ef30337d96..0000000000000000000000000000000000000000 --- a/src/ringct20/rand.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * \file rand.h - * \brief Random number generator. - */ - -#ifndef __OQS_RANDOM_H -#define __OQS_RANDOM_H - -#include <stdbool.h> -#include <stddef.h> -#include <stdint.h> - -#include "common.h" - -/** Algorithm identifier for system PRNG. */ -#define OQS_RAND_alg_system "system" -/** Algorithm identifier for NIST deterministic RNG for KATs. */ -#define OQS_RAND_alg_nist_kat "NIST-KAT" -/** Algorithm identifier for using OpenSSL's PRNG. */ -#define OQS_RAND_alg_openssl "OpenSSL" - -/** - * Switches OQS_randombytes to use the specified algorithm. - * - * @param[in] algorithm The name of the algorithm to use. - * @return OQS_SUCCESS if `algorithm` is a supported algorithm name, OQS_ERROR otherwise. - */ -OQS_API OQS_STATUS OQS_randombytes_switch_algorithm(const char *algorithm); - -/** - * Switches OQS_randombytes to use the given function. - * - * This allows additional custom RNGs besides the provided ones. The provided RNG - * function must have the same signature as `OQS_randombytes`. - * - * @param[in] algorithm_ptr Pointer to the RNG function to use. - */ -OQS_API void OQS_randombytes_custom_algorithm(void (*algorithm_ptr)(uint8_t *, size_t)); - -/** - * Fills the given memory with the requested number of (pseudo)random bytes. - * - * This implementation uses whichever algorithm has been selected by - * OQS_randombytes_switch_algorithm. The default is OQS_randombytes_system, which - * reads bytes directly from `/dev/urandom`. - * - * The caller is responsible for providing a buffer allocated with sufficient room. - * - * @param[out] random_array Pointer to the memory to fill with (pseudo)random bytes - * @param[in] bytes_to_read The number of random bytes to read into memory - */ -OQS_API void OQS_randombytes(uint8_t *random_array, size_t bytes_to_read); - -/** - * Initializes the NIST DRBG with a given seed. - * - * @param[in] entropy_input The seed; must be exactly 48 bytes - * @param[in] personalization_string An optional personalization string; may be NULL - * @param[in] security_strength The required security strength; must be 256 - */ -OQS_API void OQS_randombytes_nist_kat_init(const uint8_t *entropy_input, const uint8_t *personalization_string, int security_strength); - -#endif // __OQS_RANDOM_H diff --git a/src/ringct20/rand_nist.c b/src/ringct20/rand_nist.c deleted file mode 100644 index be4f91c42ab0babb590921aa24d45f99a6aa6789..0000000000000000000000000000000000000000 --- a/src/ringct20/rand_nist.c +++ /dev/null @@ -1,131 +0,0 @@ -// -// rng.c -// -// Created by Bassham, Lawrence E (Fed) on 8/29/17. -// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved. -// Modified for liboqs by Douglas Stebila -// - -#include <assert.h> -#include <string.h> - -#include "common.h" - -#if USE_OPENSSL -#include <openssl/conf.h> -#include <openssl/evp.h> -#include <openssl/err.h> -#else -#include "aes.h" -#endif - -typedef struct { - unsigned char Key[32]; - unsigned char V[16]; - int reseed_counter; -} AES256_CTR_DRBG_struct; - -static AES256_CTR_DRBG_struct DRBG_ctx; -static void AES256_CTR_DRBG_Update(unsigned char *provided_data, unsigned char *Key, unsigned char *V); - -#ifdef USE_OPENSSL -static void handleErrors(void) { - ERR_print_errors_fp(stderr); - abort(); -} -#endif - -// 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 void AES256_ECB(unsigned char *key, unsigned char *ctr, unsigned char *buffer) { -#ifdef USE_OPENSSL - EVP_CIPHER_CTX *ctx; - - int len; - - /* Create and initialise the context */ - if (!(ctx = EVP_CIPHER_CTX_new())) - handleErrors(); - - if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key, NULL)) - handleErrors(); - - if (1 != EVP_EncryptUpdate(ctx, buffer, &len, ctr, 16)) - handleErrors(); - - /* Clean up */ - EVP_CIPHER_CTX_free(ctx); -#else - void *schedule = NULL; - OQS_AES256_load_schedule(key, &schedule, 1); - OQS_AES256_ECB_enc(ctr, 16, key, buffer); - OQS_AES256_free_schedule(schedule); -#endif -} - -OQS_API void OQS_randombytes_nist_kat_init(unsigned char *entropy_input, unsigned char *personalization_string, int security_strength) { - unsigned char seed_material[48]; - - assert(security_strength == 256); - 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); - AES256_CTR_DRBG_Update(seed_material, DRBG_ctx.Key, DRBG_ctx.V); - DRBG_ctx.reseed_counter = 1; -} - -void OQS_randombytes_nist_kat(unsigned char *x, unsigned long long 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; - } - } - AES256_ECB(DRBG_ctx.Key, DRBG_ctx.V, block); - if (xlen > 15) { - memcpy(x + i, block, 16); - i += 16; - xlen -= 16; - } else { - memcpy(x + i, block, xlen); - xlen = 0; - } - } - AES256_CTR_DRBG_Update(NULL, DRBG_ctx.Key, DRBG_ctx.V); - DRBG_ctx.reseed_counter++; -} - -static void 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; - } - } - - AES256_ECB(Key, V, temp + 16 * i); - } - 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); -} diff --git a/src/ringct20/ring.c b/src/ringct20/ring.c index fc81178c9550f01f505280253c94e5853382310e..06f9c65738f37b45ff6c7557bd662defd4b14516 100644 --- a/src/ringct20/ring.c +++ b/src/ringct20/ring.c @@ -1,6 +1,6 @@ #include <stdio.h> #include "ring.h" -#include "rand.h" +//#include "rand.h" #include "common.h" #include "sha256.h" @@ -11,7 +11,7 @@ void LRCT_SampleKey(poly_ringct20 *r, size_t mLen) size_t i; for ( i = 0; i < mLen; i++) { - OQS_randombytes(seed, NEWHOPE_SYMBYTES); + randombytes(seed, NEWHOPE_SYMBYTES); for (size_t j = 0; j < NEWHOPE_SYMBYTES; j++) { @@ -24,7 +24,7 @@ void LRCT_SampleKey(poly_ringct20 *r, size_t mLen) r[i].coeffs[j * 8 + 6] = (seed[j] & 0x40)>>6; r[i].coeffs[j * 8 + 7] = (seed[j] & 0x80)>>7; } - OQS_randombytes(seed, NEWHOPE_SYMBYTES); + randombytes(seed, NEWHOPE_SYMBYTES); for (size_t j = 0; j < NEWHOPE_SYMBYTES; j++) { @@ -49,10 +49,10 @@ void LRCT_Setup(poly_ringct20 *A, poly_ringct20 *H, size_t mLen) for ( i = 0; i < mLen; i++) { - OQS_randombytes(seed, NEWHOPE_SYMBYTES); + randombytes(seed, NEWHOPE_SYMBYTES); poly_uniform_ringct20(A + i, seed); poly_serial(A + i); - OQS_randombytes(seed, NEWHOPE_SYMBYTES); + randombytes(seed, NEWHOPE_SYMBYTES); poly_uniform_ringct20(H + i, seed); poly_serial(H + i); } @@ -142,7 +142,7 @@ void LRCT_SigGen(poly_ringct20 *c1, poly_ringct20 **t, poly_ringct20 *h, poly_ri for ( k = 0; k < mLen+1; k++) { - OQS_randombytes(bt, NEWHOPE_POLYBYTES); + randombytes(bt, NEWHOPE_POLYBYTES); poly_frombytes(t[j] + k, bt); poly_serial(t[j] + k); } @@ -157,8 +157,8 @@ void LRCT_SigGen(poly_ringct20 *c1, poly_ringct20 **t, poly_ringct20 *h, poly_ri poly_tobytes(bpoly, &tmp); SHA256_Update(&ctx, bpoly, NEWHOPE_POLYBYTES);//H2q*U SHA256_Final(bHash, &ctx);//C_(pai+1) - printf("sign bHash======================%d:\n", j); - BytePrint(bHash, 32); +// printf("sign bHash======================%d:\n", j); +// BytePrint(bHash, 32); SHA256_KDF(bHash, 32, NEWHOPE_POLYBYTES, bt); poly_frombytes(&c, bt); @@ -170,7 +170,7 @@ void LRCT_SigGen(poly_ringct20 *c1, poly_ringct20 **t, poly_ringct20 *h, poly_ri } } - OQS_randombytes(&coin, 1); + randombytes(&coin, 1); LRCT_PolyMultMatrix(tmp2q, &cpai, S2q, mLen + 1);//S2qpai *c_pai if (coin&0x01)//b =1 { @@ -232,8 +232,8 @@ int LRCT_SigVer(const poly_ringct20 *c1, poly_ringct20 **t, poly_ringct20 *A, po poly_tobytes(bpoly, &tmp); SHA256_Update(&ctx, bpoly, NEWHOPE_POLYBYTES);//H2q*U SHA256_Final(bHash, &ctx);//C_(pai+1) - printf("bHash======================%d:\n", i); - BytePrint(bHash, 32); +// printf("bHash======================%d:\n", i); +// BytePrint(bHash, 32); SHA256_KDF(bHash, 32, NEWHOPE_POLYBYTES, bpoly); poly_frombytes(&c, bpoly); poly_serial(&c); @@ -291,10 +291,10 @@ void MIMO_LRCT_Setup(poly_ringct20 *A, poly_ringct20 *H, size_t mLen) for (i = 0; i < mLen; i++) { - OQS_randombytes(seed, NEWHOPE_SYMBYTES); + randombytes(seed, NEWHOPE_SYMBYTES); poly_uniform_ringct20(A + i, seed); poly_serial(A + i); - OQS_randombytes(seed, NEWHOPE_SYMBYTES); + randombytes(seed, NEWHOPE_SYMBYTES); poly_uniform_ringct20(H + i, seed); poly_serial(H + i); } @@ -401,7 +401,7 @@ void MIMO_LRCT_SigGen(poly_ringct20 *c1, poly_ringct20 *tList, poly_ringct20 *hL { for (k = 0; k < mLen + 1; k++) { - OQS_randombytes(bt, NEWHOPE_POLYBYTES); + randombytes(bt, NEWHOPE_POLYBYTES); poly_frombytes(u + i * (mLen + 1) + k, bt); poly_serial(u + i * (mLen + 1) + k); } @@ -455,7 +455,7 @@ void MIMO_LRCT_SigGen(poly_ringct20 *c1, poly_ringct20 *tList, poly_ringct20 *hL LRCT_Lift(tmp2q, A, LList + j * wLen + index, mLen); for (k = 0; k < mLen + 1; k++) { - OQS_randombytes(bt, NEWHOPE_POLYBYTES); + randombytes(bt, NEWHOPE_POLYBYTES); poly_frombytes(tList + j * wLen*(mLen + 1) + index * (mLen + 1) + k, bt); poly_serial(tList + j * wLen*(mLen + 1) + index * (mLen+1)+ k); } @@ -485,7 +485,7 @@ void MIMO_LRCT_SigGen(poly_ringct20 *c1, poly_ringct20 *tList, poly_ringct20 *hL poly_copy(S2q, SList+i*mLen, mLen); poly_setValue(S2q + mLen, 1);//S_{2q} ////// - OQS_randombytes(&coin, 1); + randombytes(&coin, 1); LRCT_PolyMultMatrix(tmp2q, &cpai, S2q, mLen + 1);//S2qpai *c_pai if (coin & 0x01)//b =1 { diff --git a/src/ringct20/ring.h b/src/ringct20/ring.h index e2c480b70d198c4d190dfa439d96b48f32b08262..ea3a0cdc44d08465342b9e3b8f9817e3c3f33903 100644 --- a/src/ringct20/ring.h +++ b/src/ringct20/ring.h @@ -4,7 +4,7 @@ #include "params.h" #include "poly.h" - +#include"dap_crypto_common.h" /** *function: setup diff --git a/src/ringct20/ring_test.c b/src/ringct20/ring_test.c index 30cedf1abedcce2876c77cb07ee76a33fb59b7b6..192347fc4759f0ab8b3a16afa6ee6c3ee58494b0 100644 --- a/src/ringct20/ring_test.c +++ b/src/ringct20/ring_test.c @@ -1,5 +1,5 @@ #include "ring.h" -#include "rand.h" +//#include "rand.h" #include "common.h" #define MSIZE 2