Sync common signature files on master with nist-branch

This commit is contained in:
Douglas Stebila 2018-10-03 11:56:54 -04:00
parent 049f50b876
commit 79d77d3617
4 changed files with 100 additions and 23 deletions

View File

@ -48,7 +48,7 @@ START_TIMER
// another operation here
STOP_TIMER
FINALIZE_TIMER
PRINT_TIME_HEADER
PRINT_TIMER_HEADER
PRINT_TIMER_AVG("my operation")
PRINT_TIMER_FOOTER
@ -113,26 +113,55 @@ int gettimeofday(struct timeval *tp, struct timezone *tzp) {
}
#endif
static uint64_t rdtsc(void) {
#if defined(_WIN32)
static uint64_t _bench_rdtsc(void) {
#if defined(_WIN32) || defined(_WIN64)
return __rdtsc();
#elif defined(__aarch64__)
uint64_t x;
asm volatile("isb; mrs %0, cntvct_el0"
: "=r"(x));
return x;
#elif defined(__arm__)
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return (int64_t)(time.tv_sec * 1e9 + time.tv_nsec);
#else
#elif defined(__i586__) || defined(__amd64__)
uint64_t x;
__asm__ volatile(".byte 0x0f, 0x31"
: "=A"(x));
return x;
#elif defined(__arm__)
/* Use the ARM performance counters. */
unsigned int value;
/* Read CCNT Register */
asm volatile("mrc p15, 0, %0, c9, c13, 0\t\n"
: "=r"(value));
return value;
#else
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return (int64_t)(time.tv_sec * 1e9 + time.tv_nsec);
#endif
}
#if defined(__arm__)
static void _bench_init_perfcounters(int32_t do_reset, int32_t enable_divider) {
/* In general enable all counters (including cycle counter) */
int32_t value = 1;
/* Peform reset */
if (do_reset) {
value |= 2; /* reset all counters to zero */
value |= 4; /* reset cycle counter to zero */
}
if (enable_divider)
value |= 8; /* enable "by 64" divider for CCNT */
value |= 16;
/* Program the performance-counter control-register */
asm volatile("mcr p15, 0, %0, c9, c12, 0\t\n" ::"r"(value));
/* Enable all counters */
asm volatile("mcr p15, 0, %0, c9, c12, 1\t\n" ::"r"(0x8000000f));
/* Clear overflows */
asm volatile("mcr p15, 0, %0, c9, c12, 3\t\n" ::"r"(0x8000000f));
}
#endif
#define DEFINE_TIMER_VARIABLES \
volatile uint64_t _bench_cycles_start, _bench_cycles_end; \
uint64_t _bench_cycles_cumulative = 0; \
@ -142,6 +171,16 @@ static uint64_t rdtsc(void) {
double _bench_cycles_x, _bench_cycles_mean, _bench_cycles_delta, _bench_cycles_M2, _bench_cycles_stdev; \
double _bench_time_x, _bench_time_mean, _bench_time_delta, _bench_time_M2, _bench_time_stdev;
#if defined(__arm__)
#define INITIALIZE_TIMER \
_bench_init_perfcounters(1, 0); \
_bench_iterations = 0; \
_bench_cycles_mean = 0.0; \
_bench_cycles_M2 = 0.0; \
_bench_time_cumulative = 0; \
_bench_time_mean = 0.0; \
_bench_time_M2 = 0.0;
#else
#define INITIALIZE_TIMER \
_bench_iterations = 0; \
_bench_cycles_mean = 0.0; \
@ -149,15 +188,17 @@ static uint64_t rdtsc(void) {
_bench_time_cumulative = 0; \
_bench_time_mean = 0.0; \
_bench_time_M2 = 0.0;
#endif
#define START_TIMER \
gettimeofday(&_bench_timeval_start, NULL); \
_bench_cycles_start = rdtsc();
_bench_cycles_start = _bench_rdtsc();
// Mean and population standard deviation are calculated in an online way using the algorithm in
// http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
#define STOP_TIMER \
_bench_cycles_end = rdtsc(); \
_bench_cycles_end = _bench_rdtsc(); \
gettimeofday(&_bench_timeval_end, NULL); \
_bench_iterations += 1; \
if (_bench_cycles_end < _bench_cycles_start) { \

View File

@ -42,8 +42,7 @@ OQS_STATUS example_stack() {
printf("[example_stack] OQS_KEM_frodokem_640_aes was not enabled at "
"compile-time.\n");
return OQS_ERROR;
#endif
#ifdef OQS_ENABLE_KEM_frodokem_640_aes
#else
uint8_t public_key[OQS_KEM_frodokem_640_aes_length_public_key];
uint8_t secret_key[OQS_KEM_frodokem_640_aes_length_secret_key];
uint8_t ciphertext[OQS_KEM_frodokem_640_aes_length_ciphertext];

View File

@ -1,9 +1,14 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <oqs/oqs.h>
typedef struct magic_s {
uint8_t val[32];
} magic_t;
static OQS_STATUS kem_test_correctness(const char *method_name) {
OQS_KEM *kem = NULL;
@ -15,6 +20,13 @@ static OQS_STATUS kem_test_correctness(const char *method_name) {
OQS_STATUS rc, ret = OQS_ERROR;
int rv;
//The magic numbers are 32 random values.
//The length of the magic number was chosen arbitrarilly to 32.
magic_t magic = {{0xfa, 0xfa, 0xfa, 0xfa, 0xbc, 0xbc, 0xbc, 0xbc,
0x15, 0x61, 0x15, 0x61, 0x15, 0x61, 0x15, 0x61,
0xad, 0xad, 0x43, 0x43, 0xad, 0xad, 0x34, 0x34,
0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}};
kem = OQS_KEM_new(method_name);
if (kem == NULL) {
return OQS_SUCCESS;
@ -24,11 +36,18 @@ static OQS_STATUS kem_test_correctness(const char *method_name) {
printf("Sample computation for KEM %s\n", kem->method_name);
printf("================================================================================\n");
public_key = malloc(kem->length_public_key);
secret_key = malloc(kem->length_secret_key);
ciphertext = malloc(kem->length_ciphertext);
shared_secret_e = malloc(kem->length_shared_secret);
shared_secret_d = malloc(kem->length_shared_secret);
public_key = malloc(kem->length_public_key + sizeof(magic_t));
secret_key = malloc(kem->length_secret_key + sizeof(magic_t));
ciphertext = malloc(kem->length_ciphertext + sizeof(magic_t));
shared_secret_e = malloc(kem->length_shared_secret + sizeof(magic_t));
shared_secret_d = malloc(kem->length_shared_secret + sizeof(magic_t));
//Set the magic numbers
memcpy(public_key + kem->length_public_key, magic.val, sizeof(magic_t));
memcpy(secret_key + kem->length_secret_key, magic.val, sizeof(magic_t));
memcpy(ciphertext + kem->length_ciphertext, magic.val, sizeof(magic_t));
memcpy(shared_secret_e + kem->length_shared_secret, magic.val, sizeof(magic_t));
memcpy(shared_secret_d + kem->length_shared_secret, magic.val, sizeof(magic_t));
if ((public_key == NULL) || (secret_key == NULL) || (ciphertext == NULL) || (shared_secret_e == NULL) || (shared_secret_d == NULL)) {
fprintf(stderr, "ERROR: malloc failed\n");
@ -63,6 +82,16 @@ static OQS_STATUS kem_test_correctness(const char *method_name) {
printf("shared secrets are equal\n");
}
rv = memcmp(public_key + kem->length_public_key, magic.val, sizeof(magic_t));
rv |= memcmp(secret_key + kem->length_secret_key, magic.val, sizeof(magic_t));
rv |= memcmp(ciphertext + kem->length_ciphertext, magic.val, sizeof(magic_t));
rv |= memcmp(shared_secret_e + kem->length_shared_secret, magic.val, sizeof(magic_t));
rv |= memcmp(shared_secret_d + kem->length_shared_secret, magic.val, sizeof(magic_t));
if (rv != 0) {
fprintf(stderr, "ERROR: Magic numbers do not match\n");
goto err;
}
ret = OQS_SUCCESS;
goto cleanup;
@ -83,6 +112,7 @@ cleanup:
}
int main() {
int ret = EXIT_SUCCESS;
OQS_STATUS rc;

View File

@ -58,8 +58,15 @@ static OQS_STATUS sig_test_correctness(const char *method_name) {
goto err;
}
printf("Signature is valid\n");
/* modify the signature to invalidate it */
signature[0]++;
rc = OQS_SIG_verify(sig, message, message_len, signature, signature_len, public_key);
if (rc != OQS_ERROR) {
fprintf(stderr, "ERROR: OQS_SIG_verify should have failed!\n");
goto err;
}
printf("verification passes as expected\n");
ret = OQS_SUCCESS;
goto cleanup;