Pretty-print.

This commit is contained in:
Douglas Stebila 2016-08-12 00:29:34 -02:30
parent 0b3a0331a3
commit 35ada5af71
6 changed files with 105 additions and 35 deletions

View File

@ -62,3 +62,6 @@ clean:
rm -f test_kex
rm -f liboqs.a
rm -f .DS_Store */.DS_Store */*/.DS_Store */*/*/.DS_Store
prettyprint:
astyle --style=java --indent=tab --pad-header --pad-oper --align-pointer=name --align-reference=name --suffix=none src/*/*.h src/*/*.c

View File

@ -14,24 +14,37 @@ OQS_KEX *OQS_KEX_new(OQS_RAND *rand, const uint8_t *seed, const size_t seed_len)
}
int OQS_KEX_alice_0(OQS_KEX *k, void **alice_priv, uint8_t **alice_msg, size_t *alice_msg_len) {
if (k == NULL) return 0;
else return k->alice_0(k, alice_priv, alice_msg, alice_msg_len);
if (k == NULL) {
return 0;
} else {
return k->alice_0(k, alice_priv, alice_msg, alice_msg_len);
}
}
int OQS_KEX_bob(OQS_KEX *k, const uint8_t *alice_msg, const size_t alice_msg_len, uint8_t **bob_msg, size_t *bob_msg_len, uint8_t **key, size_t *key_len) {
if (k == NULL) return 0;
else return k->bob(k, alice_msg, alice_msg_len, bob_msg, bob_msg_len, key, key_len);
if (k == NULL) {
return 0;
} else {
return k->bob(k, alice_msg, alice_msg_len, bob_msg, bob_msg_len, key, key_len);
}
}
int OQS_KEX_alice_1(OQS_KEX *k, const void *alice_priv, const uint8_t *bob_msg, const size_t bob_msg_len, uint8_t **key, size_t *key_len) {
if (k == NULL) return 0;
else return k->alice_1(k, alice_priv, bob_msg, bob_msg_len, key, key_len);
if (k == NULL) {
return 0;
} else {
return k->alice_1(k, alice_priv, bob_msg, bob_msg_len, key, key_len);
}
}
void OQS_KEX_alice_priv_free(OQS_KEX *k, void *alice_priv) {
if (k) k->alice_priv_free(k, alice_priv);
if (k) {
k->alice_priv_free(k, alice_priv);
}
}
void OQS_KEX_free(OQS_KEX *k) {
if (k) k->free(k);
if (k) {
k->free(k);
}
}

View File

@ -36,15 +36,21 @@ int main(void) {
/* setup RAND */
rand = OQS_RAND_new();
if (rand == NULL) goto err;
if (rand == NULL) {
goto err;
}
/* setup KEX */
kex = OQS_KEX_new(rand, NULL, 0);
if (kex == NULL) goto err;
if (kex == NULL) {
goto err;
}
/* Alice's initial message */
rc = OQS_KEX_alice_0(kex, &alice_priv, &alice_msg, &alice_msg_len);
if (rc != 1) goto err;
if (rc != 1) {
goto err;
}
#ifdef DEBUG
PRINT_HEX_STRING("Alice message", alice_msg, alice_msg_len)
@ -52,7 +58,9 @@ int main(void) {
/* Bob's response */
rc = OQS_KEX_bob(kex, alice_msg, alice_msg_len, &bob_msg, &bob_msg_len, &bob_key, &bob_key_len);
if (rc != 1) goto err;
if (rc != 1) {
goto err;
}
#ifdef DEBUG
PRINT_HEX_STRING("Bob message", bob_msg, bob_msg_len)
@ -61,7 +69,9 @@ int main(void) {
/* Alice processes Bob's response */
rc = OQS_KEX_alice_1(kex, alice_priv, bob_msg, bob_msg_len, &alice_key, &alice_key_len);
if (rc != 1) goto err;
if (rc != 1) {
goto err;
}
#ifdef DEBUG
PRINT_HEX_STRING("Alice session key", alice_key, alice_key_len)

View File

@ -16,12 +16,18 @@
OQS_KEX *OQS_KEX_rlwe_bcns15_new(OQS_RAND *rand, UNUSED const uint8_t *seed, UNUSED const size_t seed_len) {
OQS_KEX *k = malloc(sizeof(OQS_KEX));
if (k == NULL) return NULL;
if (k == NULL) {
return NULL;
}
k->ctx = malloc(sizeof(struct oqs_kex_rlwe_bcns15_fft_ctx));
if (NULL == k->ctx) return NULL;
if (NULL == k->ctx) {
return NULL;
}
int ok = oqs_kex_rlwe_bcns15_fft_ctx_init(k->ctx);
if (ok != 1) return NULL;
if (ok != 1) {
return NULL;
}
k->method_name = strdup("RLWE BCNS15");
k->estimated_classical_security = 163;
@ -47,9 +53,13 @@ int OQS_KEX_rlwe_bcns15_alice_0(OQS_KEX *k, void **alice_priv, uint8_t **alice_m
/* allocate public/private key pair */
*alice_msg = malloc(1024 * sizeof(uint32_t));
if (*alice_msg == NULL) goto err;
if (*alice_msg == NULL) {
goto err;
}
*alice_priv = malloc(1024 * sizeof(uint32_t));
if (*alice_priv == NULL) goto err;
if (*alice_priv == NULL) {
goto err;
}
/* generate public/private key pair */
oqs_kex_rlwe_bcns15_generate_keypair(oqs_kex_rlwe_bcns15_a, (uint32_t *) *alice_priv, (uint32_t *) *alice_msg, k->ctx, k->rand);
@ -76,15 +86,23 @@ int OQS_KEX_rlwe_bcns15_bob(OQS_KEX *k, const uint8_t *alice_msg, const size_t a
*bob_msg = NULL;
*key = NULL;
if (alice_msg_len != 1024 * sizeof(uint32_t)) goto err;
if (alice_msg_len != 1024 * sizeof(uint32_t)) {
goto err;
}
bob_priv = malloc(1024 * sizeof(uint32_t));
if (bob_priv == NULL) goto err;
if (bob_priv == NULL) {
goto err;
}
/* allocate message and session key */
*bob_msg = malloc(1024 * sizeof(uint32_t) + 16 * sizeof(uint64_t));
if (*bob_msg == NULL) goto err;
if (*bob_msg == NULL) {
goto err;
}
*key = malloc(16 * sizeof(uint64_t));
if (*key == NULL) goto err;
if (*key == NULL) {
goto err;
}
/* generate public/private key pair */
oqs_kex_rlwe_bcns15_generate_keypair(oqs_kex_rlwe_bcns15_a, (uint32_t *) bob_priv, (uint32_t *) *bob_msg, k->ctx, k->rand);
@ -116,11 +134,15 @@ int OQS_KEX_rlwe_bcns15_alice_1(OQS_KEX *k, const void *alice_priv, const uint8_
*key = NULL;
if (bob_msg_len != 1024 * sizeof(uint32_t) + 16 * sizeof(uint64_t)) goto err;
if (bob_msg_len != 1024 * sizeof(uint32_t) + 16 * sizeof(uint64_t)) {
goto err;
}
/* allocate session key */
*key = malloc(16 * sizeof(uint64_t));
if (*key == NULL) goto err;
if (*key == NULL) {
goto err;
}
/* generate Alice's session key */
const uint8_t *bob_rec = bob_msg + 1024 * sizeof(uint32_t);
@ -141,11 +163,15 @@ cleanup:
}
void OQS_KEX_rlwe_bcns15_alice_priv_free(UNUSED OQS_KEX *k, void *alice_priv) {
if (alice_priv) free(alice_priv);
if (alice_priv) {
free(alice_priv);
}
}
void OQS_KEX_rlwe_bcns15_free(OQS_KEX *k) {
if (!k) return;
if (!k) {
return;
}
free(k->method_name);
k->method_name = NULL;
free(k->ctx);

View File

@ -31,6 +31,8 @@ void OQS_RAND_n(OQS_RAND *r, uint8_t *out, size_t n) {
}
void OQS_RAND_free(OQS_RAND *r) {
if (r) r->free(r);
if (r) {
r->free(r);
}
}

View File

@ -25,7 +25,9 @@ static void OQS_RAND_urandom_chacha20_ctx_free(void *rand_ctx);
OQS_RAND *OQS_RAND_urandom_chacha20_new() {
OQS_RAND *r = malloc(sizeof(OQS_RAND));
if (r == NULL) return NULL;
if (r == NULL) {
return NULL;
}
r->method_name = strdup("urandom_chacha20");
r->estimated_classical_security = 256;
r->estimated_quantum_security = 128; // Grover search
@ -46,18 +48,28 @@ static OQS_RAND_urandom_chacha20_ctx *OQS_RAND_urandom_chacha20_ctx_new() {
int fd = 0;
OQS_RAND_urandom_chacha20_ctx *rand_ctx = NULL;
rand_ctx = (OQS_RAND_urandom_chacha20_ctx *) malloc(sizeof(OQS_RAND_urandom_chacha20_ctx));
if (rand_ctx == NULL) goto err;
if (rand_ctx == NULL) {
goto err;
}
fd = open("/dev/urandom", O_RDONLY);
if (fd == 0) goto err;
if (fd == 0) {
goto err;
}
int r = read(fd, rand_ctx->key, 32);
if (r != 32) goto err;
if (r != 32) {
goto err;
}
bzero(rand_ctx->nonce, 12);
rand_ctx->counter = 0U;
rand_ctx->cache_next_byte = 64; // cache is empty
goto okay;
err:
if (rand_ctx) free(rand_ctx);
if (fd) close(fd);
if (rand_ctx) {
free(rand_ctx);
}
if (fd) {
close(fd);
}
return NULL;
okay:
close(fd);
@ -122,7 +134,11 @@ static void OQS_RAND_urandom_chacha20_ctx_free(void *rand_ctx) {
}
void OQS_RAND_urandom_chacha20_free(OQS_RAND *r) {
if (r) OQS_RAND_urandom_chacha20_ctx_free(r->ctx);
if (r) free(r->method_name);
if (r) {
OQS_RAND_urandom_chacha20_ctx_free(r->ctx);
}
if (r) {
free(r->method_name);
}
free(r);
}