Skip to content

Commit

Permalink
Reorganize Makefile and add checks that would have detected bug on pr…
Browse files Browse the repository at this point in the history
…/14 branch.
  • Loading branch information
dstebila committed Apr 25, 2016
1 parent df79b10 commit e158574
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
34 changes: 23 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
CC=gcc -O3 -Wall -Wextra -std=c99 -Wno-deprecated-declarations
CC=gcc

CPPFLAGS=
LDFLAGS=-lcrypto

# Choose your PRNG
# RLWE_RANDOMNESS_USE_OPENSSL_AES
# RLWE_RANDOMNESS_USE_OPENSSL_RC4
# RLWE_RANDOMNESS_USE_OPENSSL_RAND
# RLWE_RANDOMNESS_USE_DEV_URANDOM
# RLWE_RANDOMNESS_USE_INSECURE_LIBC
PRNG=RLWE_RANDOMNESS_USE_DEV_URANDOM
PRNG_CCFLAGS=
# LDFLAGS needs to have -lcrypto if you are using an OpenSSL-based PRNG
PRNG_LDFLAGS=-lcrypto

# On Mac OS X, the system OpenSSL is too old.
# Install your own more recent version and point to it.
# If you have installed OpenSSL via brew, you can use the following two lines.
CPPFLAGS=-I/usr/local/opt/openssl/include
LDFLAGS=-L/usr/local/opt/openssl/lib -lcrypto
PRNG_CCFLAGS=-I/usr/local/opt/openssl/include
PRNG_LDFLAGS=-L/usr/local/opt/openssl/lib -lcrypto

CCFLAGS=-O3 -Wall -Wextra -std=c99 -Wno-deprecated-declarations -D$(PRNG) $(PRNG_CCFLAGS)
LDFLAGS=$(PRNG_LDFLAGS)

all:
$(CC) $(CPPFLAGS) -Wno-unused-function -c fft.c
$(CC) $(CPPFLAGS) -Wno-unused-function -c rlwe.c
$(CC) $(CPPFLAGS) -Wno-unused-function -c rlwe_kex.c
$(CC) $(CPPFLAGS) -Wno-unused-function -Wno-unused-parameter -c rlwe_rand.c
$(CC) $(CPPFLAGS) -o rlwe_main -lcrypto rlwe_main.c fft.o rlwe.o rlwe_kex.o rlwe_rand.o $(LDFLAGS)
$(CC) $(CPPFLAGS) -o rlwe_benchmark -lcrypto rlwe_benchmark.c fft.o rlwe.o rlwe_kex.o rlwe_rand.o $(LDFLAGS)
$(CC) $(CCFLAGS) -Wno-unused-function -c fft.c
$(CC) $(CCFLAGS) -Wno-unused-function -c rlwe.c
$(CC) $(CCFLAGS) -Wno-unused-function -c rlwe_kex.c
$(CC) $(CCFLAGS) -Wno-unused-function -Wno-unused-parameter -c rlwe_rand.c
$(CC) $(CCFLAGS) -o rlwe_main -lcrypto rlwe_main.c fft.o rlwe.o rlwe_kex.o rlwe_rand.o $(LDFLAGS)
$(CC) $(CCFLAGS) -o rlwe_benchmark -lcrypto rlwe_benchmark.c fft.o rlwe.o rlwe_kex.o rlwe_rand.o $(LDFLAGS)

clean:
rm fft.o rlwe.o rlwe_kex.o rlwe_rand.o rlwe_main rlwe_benchmark
Expand Down
3 changes: 3 additions & 0 deletions rlwe_kex.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@
/* Generate keypair for RLWE KEX
* - input: parameters: a
* - output: private key s, public key b
* - return: 1 on success, 0 on failure
*/
int rlwe_kex_generate_keypair(const uint32_t *a, uint32_t s[1024], uint32_t b[1024], FFT_CTX *ctx);

/* Alice's shared key computation for RLWE KEX
* - input: Bob's public key b, Alice's private key s, reconciliation data c
* - output: shared secret k
* - return: 1 on success, 0 on failure
*/
int rlwe_kex_compute_key_alice(const uint32_t b[1024], const uint32_t s[1024], const uint64_t c[16], uint64_t k[16], FFT_CTX *ctx);

/* Bob's shared key computation for RLWE KEX
* - input: Alice's public key b, Bob's private key s
* - output: reconciliation data c, shared secret k
* - return: 1 on success, 0 on failure
*/
int rlwe_kex_compute_key_bob(const uint32_t b[1024], const uint32_t s[1024], uint64_t c[16], uint64_t k[16], FFT_CTX *ctx);

Expand Down
18 changes: 14 additions & 4 deletions rlwe_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
#include "rlwe_a.h"
#include "rlwe_rand.h"

#define CHECK_OK(op, val) \
{ \
int tmp_ret; \
tmp_ret = (op); \
if (tmp_ret != (val)) { \
fprintf(stderr, "Error (return code %d) at %s:%d\n", tmp_ret, __FILE__, __LINE__); \
return -1; \
} \
}

int main() {

uint32_t *a = rlwe_a;
Expand All @@ -33,11 +43,11 @@ int main() {
return -1;
}

rlwe_kex_generate_keypair(a, s_alice, b_alice, &ctx);
rlwe_kex_generate_keypair(a, s_bob, b_bob, &ctx);
CHECK_OK(rlwe_kex_generate_keypair(a, s_alice, b_alice, &ctx), 1)
CHECK_OK(rlwe_kex_generate_keypair(a, s_bob, b_bob, &ctx), 1)

rlwe_kex_compute_key_bob(b_alice, s_bob, c, k_bob, &ctx);
rlwe_kex_compute_key_alice(b_bob, s_alice, c, k_alice, &ctx);
CHECK_OK(rlwe_kex_compute_key_bob(b_alice, s_bob, c, k_bob, &ctx), 1)
CHECK_OK(rlwe_kex_compute_key_alice(b_bob, s_alice, c, k_alice, &ctx), 1)

int keys_match = 1;
for (int i = 0; i < 16; i++) {
Expand Down
6 changes: 0 additions & 6 deletions rlwe_rand.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
#include <stdint.h>
#include <stddef.h>

// #define RLWE_RANDOMNESS_USE_OPENSSL_AES
// #define RLWE_RANDOMNESS_USE_OPENSSL_RC4
// #define RLWE_RANDOMNESS_USE_OPENSSL_RAND
// #define RLWE_RANDOMNESS_USE_INSECURE_LIBC
#define RLWE_RANDOMNESS_USE_DEV_URANDOM

#if defined(RLWE_RANDOMNESS_USE_OPENSSL_AES)
#include <openssl/evp.h>
#define RAND_CTX EVP_CIPHER_CTX
Expand Down

0 comments on commit e158574

Please sign in to comment.