Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC/POC] Static Math Library Binding #542

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions demos/timing.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ static void time_mult(void)
unsigned long x, y;
void *a, *b, *c;

if (ltc_mp.name == NULL) return;
if (mp_name() == NULL) return;

fprintf(stderr, "Timing Multiplying:\n");
mp_init_multi(&a,&b,&c,NULL);
Expand Down Expand Up @@ -554,7 +554,7 @@ static void time_sqr(void)
unsigned long x, y;
void *a, *b;

if (ltc_mp.name == NULL) return;
if (mp_name() == NULL) return;

fprintf(stderr, "Timing Squaring:\n");
mp_init_multi(&a,&b,NULL);
Expand Down Expand Up @@ -652,7 +652,7 @@ static const struct {
#endif
};

if (ltc_mp.name == NULL) return;
if (mp_name() == NULL) return;

for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
t2 = 0;
Expand Down Expand Up @@ -699,7 +699,7 @@ static void time_rsa(void)
unsigned long x, y, z, zzz;
int err, zz, stat;

if (ltc_mp.name == NULL) return;
if (mp_name() == NULL) return;

for (x = 2048; x <= 8192; x <<= 1) {
t2 = 0;
Expand Down Expand Up @@ -830,7 +830,7 @@ static void time_dh(void)
100000
};

if (ltc_mp.name == NULL) return;
if (mp_name() == NULL) return;

for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
t2 = 0;
Expand Down Expand Up @@ -895,7 +895,7 @@ static void time_ecc(void)
#endif
100000};

if (ltc_mp.name == NULL) return;
if (mp_name() == NULL) return;

for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
t2 = 0;
Expand Down
4 changes: 2 additions & 2 deletions demos/tv_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ static void ecc_gen(void)
mp_set(G->z, 1);

while (mp_cmp(k, order) == LTC_MP_LT) {
ltc_mp.ecc_ptmul(k, G, R, a, modulus, 1);
mp_ecc_ptmul(k, G, R, a, modulus, 1);
mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str);
mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
Expand Down Expand Up @@ -807,7 +807,7 @@ int main(void)
printf("Generating GCM vectors..."); fflush(stdout); gcm_gen(); printf("done\n");
#endif
printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");
if (ltc_mp.name != NULL) {
if (mp_name() != NULL) {
printf("Generating MATH vectors..."); fflush(stdout); math_gen(); printf("done\n");
printf("Generating ECC vectors..."); fflush(stdout); ecc_gen(); printf("done\n");
}
Expand Down
50 changes: 48 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ ifndef LIBNAME
LIBNAME=libtomcrypt.a
endif

ifndef LIBNAME_LTM
LIBNAME_LTM=libtomcrypt_ltm.a
endif

ifndef LIBNAME_TFM
LIBNAME_TFM=libtomcrypt_tfm.a
endif

ifndef LIBNAME_GMP
LIBNAME_GMP=libtomcrypt_gmp.a
endif


include makefile_include.mk

Expand All @@ -57,21 +69,55 @@ ifneq ($V,1)
endif
${silent} ${CC} ${LTC_CFLAGS} -c $< -o $@

$(LIBNAME): $(OBJECTS)
$(LIBNAME): $(OBJECTS) $(MOBJECTS)
ifneq ($V,1)
@echo " * ${AR} $@"
endif
${silent} $(AR) $(ARFLAGS) $@ $^
ifneq ($V,1)
@echo " * ${RANLIB} $@"
endif
${silent} $(RANLIB) $@

$(LIBNAME_LTM): $(OBJECTS) src/math/ltm_static.o
ifneq ($V,1)
@echo " * ${AR} $@" ${silent_echo}
endif
${silent} $(AR) $(ARFLAGS) $@ $(OBJECTS)
${silent} $(AR) $(ARFLAGS) $@ $^
ifneq ($V,1)
@echo " * ${RANLIB} $@" ${silent_echo}
endif
${silent} $(RANLIB) $@

$(LIBNAME_TFM): $(OBJECTS) src/math/tfm_static.o
ifneq ($V,1)
@echo " * ${AR} $@"
endif
${silent} $(AR) $(ARFLAGS) $@ $^
ifneq ($V,1)
@echo " * ${RANLIB} $@"
endif
${silent} $(RANLIB) $@

$(LIBNAME_GMP): $(OBJECTS) src/math/gmp_static.o
ifneq ($V,1)
@echo " * ${AR} $@"
endif
${silent} $(AR) $(ARFLAGS) $@ $^
ifneq ($V,1)
@echo " * ${RANLIB} $@"
endif
${silent} $(RANLIB) $@


test: $(call print-help,test,Builds the library and the 'test' application to run all self-tests) $(LIBNAME) $(TOBJECTS)
ifneq ($V,1)
@echo " * ${CC} $@" ${silent_echo}
endif
${silent} $(CC) $(LTC_LDFLAGS) $(TOBJECTS) $(LIB_PRE) $(LIBNAME) $(LIB_POST) $(LTC_EXTRALIBS) -o $(TEST)
${silent} $(CC) $(LTC_LDFLAGS) $(TOBJECTS) $(LIB_PRE) $(LIBNAME_LTM) $(LIB_POST) $(LTC_EXTRALIBS) -ltommath -o $(TEST)_ltm
${silent} $(CC) $(LTC_LDFLAGS) $(TOBJECTS) $(LIB_PRE) $(LIBNAME_TFM) $(LIB_POST) $(LTC_EXTRALIBS) -o $(TEST)_tfm
${silent} $(CC) $(LTC_LDFLAGS) $(TOBJECTS) $(LIB_PRE) $(LIBNAME_GMP) $(LIB_POST) $(LTC_EXTRALIBS) -o $(TEST)_gmp

# build the demos from a template
define DEMO_template
Expand Down
14 changes: 10 additions & 4 deletions makefile_include.mk
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ endif


#The first rule is also the default rule and builds the libtomcrypt library.
library: $(call print-help,library,Builds the library) $(LIBNAME)
library: $(call print-help,library,Builds the library) $(LIBNAME) $(LIBNAME_LTM) $(LIBNAME_TFM) $(LIBNAME_GMP)


# List of objects to compile (all goes to libtomcrypt.a)
Expand Down Expand Up @@ -260,8 +260,8 @@ src/mac/poly1305/poly1305_file.o src/mac/poly1305/poly1305_memory.o \
src/mac/poly1305/poly1305_memory_multi.o src/mac/poly1305/poly1305_test.o src/mac/xcbc/xcbc_done.o \
src/mac/xcbc/xcbc_file.o src/mac/xcbc/xcbc_init.o src/mac/xcbc/xcbc_memory.o \
src/mac/xcbc/xcbc_memory_multi.o src/mac/xcbc/xcbc_process.o src/mac/xcbc/xcbc_test.o \
src/math/fp/ltc_ecc_fp_mulmod.o src/math/gmp_desc.o src/math/ltm_desc.o src/math/multi.o \
src/math/radix_to_bin.o src/math/rand_bn.o src/math/rand_prime.o src/math/tfm_desc.o src/misc/adler32.o \
src/math/fp/ltc_ecc_fp_mulmod.o src/math/multi.o \
src/math/radix_to_bin.o src/math/rand_bn.o src/math/rand_prime.o src/misc/adler32.o \
src/misc/base16/base16_decode.o src/misc/base16/base16_encode.o src/misc/base32/base32_decode.o \
src/misc/base32/base32_encode.o src/misc/base64/base64_decode.o src/misc/base64/base64_encode.o \
src/misc/bcrypt/bcrypt.o src/misc/burn_stack.o src/misc/compare_testvector.o src/misc/copy_or_zeromem.o \
Expand Down Expand Up @@ -393,6 +393,12 @@ src/stream/sober128/sober128_stream.o src/stream/sober128/sober128_stream_memory
src/stream/sober128/sober128_test.o src/stream/sosemanuk/sosemanuk.o \
src/stream/sosemanuk/sosemanuk_memory.o src/stream/sosemanuk/sosemanuk_test.o

# List of dynamic math objects to compile (all goes to libtomcrypt.a)
MOBJECTS=src/math/gmp_desc.o src/math/ltm_desc.o src/math/tfm_desc.o src/math/dynamic.o

# List of static math objects
SOBJECTS=src/math/gmp_static.o src/math/ltm_static.o src/math/tfm_static.o

# List of test objects to compile (all goes to libtomcrypt_prof.a)
TOBJECTS=tests/base16_test.o tests/base32_test.o tests/base64_test.o tests/bcrypt_test.o \
tests/cipher_hash_test.o tests/common.o tests/der_test.o tests/dh_test.o tests/dsa_test.o \
Expand Down Expand Up @@ -424,7 +430,7 @@ $(DOBJECTS): LTC_CFLAGS := -Itests $(LTC_CFLAGS)
$(TOBJECTS): LTC_CFLAGS := -Itests $(LTC_CFLAGS)

#Dependencies on *.h
$(OBJECTS): $(HEADERS)
$(OBJECTS) $(MOBJECTS) $(SOBJECTS): $(HEADERS)
$(DOBJECTS): $(HEADERS) $(THEADERS)
$(TOBJECTS): $(HEADERS) $(THEADERS)

Expand Down
71 changes: 71 additions & 0 deletions src/headers/tomcrypt_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,77 @@ int ltc_init_multi(void **a, ...);
void ltc_deinit_multi(void *a, ...);
void ltc_cleanup_multi(void **a, ...);

const char *ltc_mp_name(void);
int ltc_mp_bits_per_digit(void);
int ltc_mp_init(void *a);
void ltc_mp_deinit(void *a);
int ltc_mp_init_copy(void **dst, void *src);

int ltc_mp_neg(void *src, void *dst);
int ltc_mp_copy(void *src, void *dst);

int ltc_mp_set_int(void *a, ltc_mp_digit n);
unsigned long ltc_mp_get_int(void *a);
ltc_mp_digit ltc_mp_get_digit(void *a, int n);
int ltc_mp_get_digit_count(void *a);
int ltc_mp_compare(void *a, void *b);
int ltc_mp_compare_d(void *a, ltc_mp_digit n);
int ltc_mp_count_bits(void *a);
int ltc_mp_count_lsb_bits(void *a);
int ltc_mp_twoexpt(void *a , int n);

int ltc_mp_read_radix(void *a, const char *str, int radix);
int ltc_mp_write_radix(void *a, char *str, int radix);
unsigned long ltc_mp_unsigned_size(void *a);
int ltc_mp_unsigned_write(void *src, unsigned char *dst);
int ltc_mp_unsigned_read(void *dst, unsigned char *src, unsigned long len);

int ltc_mp_add(void *a, void *b, void *c);
int ltc_mp_addi(void *a, ltc_mp_digit b, void *c);
int ltc_mp_sub(void *a, void *b, void *c);
int ltc_mp_subi(void *a, ltc_mp_digit b, void *c);
int ltc_mp_mul(void *a, void *b, void *c);
int ltc_mp_muli(void *a, ltc_mp_digit b, void *c);
int ltc_mp_sqr(void *a, void *b);
int ltc_mp_sqrtmod_prime_support(void);
int ltc_mp_sqrtmod_prime(void *a, void *b, void *c);
int ltc_mp_mpdiv(void *a, void *b, void *c, void *d);
int ltc_mp_div_2(void *a, void *b);
int ltc_mp_modi(void *a, ltc_mp_digit b, ltc_mp_digit *c);
int ltc_mp_gcd(void *a, void *b, void *c);
int ltc_mp_lcm(void *a, void *b, void *c);

int ltc_mp_rsa_me(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, int which,
const rsa_key *key);

int ltc_mp_addmod(void *a, void *b, void *c, void *d);
int ltc_mp_submod(void *a, void *b, void *c, void *d);
int ltc_mp_mulmod(void *a, void *b, void *c, void *d);
int ltc_mp_sqrmod(void *a, void *b, void *c);
int ltc_mp_invmod(void *a, void *b, void *c);

int ltc_mp_montgomery_setup(void *a, void **b);
int ltc_mp_montgomery_normalization(void *a, void *b);
int ltc_mp_montgomery_reduce(void *a, void *b, void *c);
void ltc_mp_montgomery_deinit(void *a);

int ltc_mp_exptmod(void *a, void *b, void *c, void *d);
int ltc_mp_isprime(void *a, int b, int *c);

int ltc_mp_ecc_ptmul(void *k, const ecc_point *G, ecc_point *R, void *a,
void *modulus, int map);
int ltc_mp_ecc_ptadd(const ecc_point *P, const ecc_point *Q, ecc_point *R,
void *ma, void *modulus, void *mp);
int ltc_mp_ecc_ptdbl(const ecc_point *P, ecc_point *R, void *ma, void *modulus,
void *mp);
int ltc_mp_ecc_map(ecc_point *P, void *modulus, void *mp);
int ltc_mp_ecc_mul2add_support(void);
int ltc_mp_ecc_mul2add(const ecc_point *A, void *kA, const ecc_point *B,
void *kB, ecc_point *C, void *ma, void *modulus);

int ltc_mp_rand(void *a, int size);

#ifdef LTM_DESC
extern const ltc_math_descriptor ltm_desc;
#endif
Expand Down
Loading