Skip to content

Commit

Permalink
Simplify Makefile and allow for better incremental builds. (#3)
Browse files Browse the repository at this point in the history
* Simplify Makefile and allow for better incremental builds.

Also add -std=gnu11 option since some older compilers default
to c99 which causes compilation to fail.

* Enable more warnings and fix some minor issues.

* Fixes a memory leak

Caused by method_name not being freed if
OQS_RAND_urandom_chacha20_ctx_new() fails. Also accounts for the fact
that strdup can fail and return NULL.
  • Loading branch information
aparent authored and dstebila committed Aug 25, 2016
1 parent 27131da commit c999815
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
73 changes: 38 additions & 35 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ CURL=curl
RANLIB=ranlib
LN=ln -s

DEFAULTS=-DOQS_RAND_DEFAULT_URANDOM_CHACHA20 -DOQS_KEX_DEFAULT_BCNS15
DEFAULTS= -std=gnu11 -Wpedantic -Wall -Wextra -DOQS_RAND_DEFAULT_URANDOM_CHACHA20 -DOQS_KEX_DEFAULT_BCNS15
CFLAGS=$(DEFAULTS) -DCONSTANT_TIME
LDFLAGS=-lm
INCLUDES=-Iinclude

all: links objdir rand_urandom_chacha20 rand kex_rlwe_bcns15 kex lib tests
all: links lib tests

objdir:
mkdir -p objs
rm -f liboqs.a
objs/%.o: src/%.c
@mkdir -p $(@D)
$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@

links:
rm -rf include/oqs
Expand All @@ -23,29 +23,36 @@ links:
$(LN) ../../src/rand/rand.h include/oqs
$(LN) ../../src/rand_urandom_chacha20/rand_urandom_chacha20.h include/oqs

rand_urandom_chacha20: src/rand_urandom_chacha20/rand_urandom_chacha20.h src/rand_urandom_chacha20/rand_urandom_chacha20.c src/rand_urandom_chacha20/chacha20.c
$(CC) -c $(CFLAGS) $(INCLUDES) src/rand_urandom_chacha20/rand_urandom_chacha20.c -o objs/rand_urandom_chacha20.o
$(AR) liboqs.a objs/rand_urandom_chacha20.o

rand: src/rand/rand.h src/rand/rand.c
$(CC) -c $(CFLAGS) $(INCLUDES) src/rand/rand.c -o objs/rand.o
$(AR) liboqs.a objs/rand.o

kex_rlwe_bcns15: src/kex_rlwe_bcns15/fft.c src/kex_rlwe_bcns15/kex_rlwe_bcns15.c src/kex_rlwe_bcns15/kex_rlwe_bcns15.h src/kex_rlwe_bcns15/local.h src/kex_rlwe_bcns15/rlwe.c src/kex_rlwe_bcns15/rlwe_a.h src/kex_rlwe_bcns15/rlwe_kex.c src/kex_rlwe_bcns15/rlwe_table.h
$(CC) -c $(CFLAGS) $(INCLUDES) src/kex_rlwe_bcns15/fft.c -o objs/kex_rlwe_bcns15_fft.o
$(CC) -c $(CFLAGS) $(INCLUDES) src/kex_rlwe_bcns15/rlwe.c -o objs/kex_rlwe_bcns15_rlwe.o
$(CC) -c $(CFLAGS) $(INCLUDES) src/kex_rlwe_bcns15/rlwe_kex.c -o objs/kex_rlwe_bcns15_rlwe_kex.o
$(CC) -c $(CFLAGS) $(INCLUDES) src/kex_rlwe_bcns15/kex_rlwe_bcns15.c -o objs/kex_rlwe_bcns15.o
$(AR) liboqs.a objs/kex_rlwe_bcns15_fft.o
$(AR) liboqs.a objs/kex_rlwe_bcns15_rlwe.o
$(AR) liboqs.a objs/kex_rlwe_bcns15_rlwe_kex.o
$(AR) liboqs.a objs/kex_rlwe_bcns15.o

kex: src/kex/kex.h src/kex/kex.c
$(CC) -c $(CFLAGS) $(INCLUDES) src/kex/kex.c -o objs/kex.o
$(AR) liboqs.a objs/kex.o

lib:
# CHACHA

CHACHA_OBJS := $(addprefix objs/rand_urandom_chacha20/, \
rand_urandom_chacha20.o)

$(CHACHA_OBJS): src/rand_urandom_chacha20/rand_urandom_chacha20.h src/rand_urandom_chacha20/chacha20.c

# RAND

objs/rand/rand.o: src/rand/rand.h

# BCNS

BCNS_OBJS := $(addprefix objs/kex_rlwe_bcns15/, \
fft.o kex_rlwe_bcns15.o rlwe.o rlwe_kex.o)

BCNS_HEADERS := $(addprefix src/kex_rlwe_bcns15/, \
kex_rlwe_bcns15.h local.h rlwe_a.h rlwe_table.h)

$(BCNS_OBJS): $(BCNS_HEADERS)

# KEX

objs/kex/kex.o: src/kex/kex.h

#LIB

lib: $(CHACHA_OBJS) $(BCNS_OBJS) objs/rand/rand.o objs/kex/kex.o
rm -f liboqs.a
$(AR) liboqs.a $^
$(RANLIB) liboqs.a

tests: lib src/rand/test_rand.c src/kex/test_kex.c
Expand All @@ -56,13 +63,9 @@ docs: links
doxygen

clean:
rm -rf docs
rm -rf objs
rm -rf include
rm -f test_rand
rm -f test_kex
rm -f liboqs.a
rm -f .DS_Store */.DS_Store */*/.DS_Store */*/*/.DS_Store
rm -rf docs objs include
rm -f test_rand test_kex liboqs.a
find . -name .DS_Store -type f -delete

prettyprint:
astyle --style=java --indent=tab --pad-header --pad-oper --align-pointer=name --align-reference=name --suffix=none src/*/*.h src/*/*.c
2 changes: 1 addition & 1 deletion src/kex/test_kex.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static int kex_test_correctness(OQS_RAND *rand, OQS_KEX *(*new_method)(OQS_RAND
}

/* record generated bytes for statistical analysis */
for (int i = 0; i < alice_key_len; i++) {
for (size_t i = 0; i < alice_key_len; i++) {
OQS_RAND_test_record_occurrence(alice_key[i], occurrences);
}

Expand Down
4 changes: 2 additions & 2 deletions src/rand/test_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static void rand_test_distribution_32(OQS_RAND *rand, unsigned long occurrences[
for (int i = 0; i < iterations; i++) {
x = OQS_RAND_32(rand);
uint8_t b;
for (int j = 0; j < sizeof(uint32_t); j++) {
for (size_t j = 0; j < sizeof(uint32_t); j++) {
b = (x >> j) & 0xFF;
OQS_RAND_test_record_occurrence(b, occurrences);
}
Expand All @@ -32,7 +32,7 @@ static void rand_test_distribution_64(OQS_RAND *rand, unsigned long occurrences[
for (int i = 0; i < iterations; i++) {
x = OQS_RAND_64(rand);
uint8_t b;
for (int j = 0; j < sizeof(uint64_t); j++) {
for (size_t j = 0; j < sizeof(uint64_t); j++) {
b = (x >> j) & 0xFF;
OQS_RAND_test_record_occurrence(b, occurrences);
}
Expand Down
8 changes: 4 additions & 4 deletions src/rand_urandom_chacha20/rand_urandom_chacha20.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ OQS_RAND *OQS_RAND_urandom_chacha20_new() {
return NULL;
}
r->method_name = strdup("urandom_chacha20");
r->estimated_classical_security = 256;
r->estimated_quantum_security = 128; // Grover search
r->ctx = OQS_RAND_urandom_chacha20_ctx_new();
if (r->ctx == NULL) {
free(r);
if (r->ctx == NULL || r->method_name == NULL) {
OQS_RAND_urandom_chacha20_free(r);
return NULL;
}
r->estimated_classical_security = 256;
r->estimated_quantum_security = 128; // Grover search
r->rand_8 = &OQS_RAND_urandom_chacha20_8;
r->rand_32 = &OQS_RAND_urandom_chacha20_32;
r->rand_64 = &OQS_RAND_urandom_chacha20_64;
Expand Down

0 comments on commit c999815

Please sign in to comment.