diff --git a/demos/demo_crypt_constants.c b/demos/demo_crypt_constants.c new file mode 100644 index 0000000..5ca6a87 --- /dev/null +++ b/demos/demo_crypt_constants.c @@ -0,0 +1,60 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file demo_crypt_constants.c + + Demo how to get various constants to dynamic languages + like Python + + Larry Bugbee, February 2013 +*/ + + +// in lieu of a header file +int crypt_get_constant(const char* namein, int *valueout); +int crypt_list_all_constants(char *names_list, + unsigned long *names_list_size); + + +int main(void) { + int rc; + + printf("\n"); + + // given a specific constant name, get and print its value + char name[] = "CTR_COUNTER_BIG_ENDIAN"; + int value; + + rc = crypt_get_constant(name, &value); + printf(" %s is %d \n", name, value); + printf("\n"); + + // get and print the length of the names (and values) list + char *names_list; + unsigned long names_list_len; + + rc = crypt_list_all_constants(NULL, &names_list_len); + printf(" need to allocate %lu bytes \n", names_list_len); + printf("\n"); + + // get and print the names (and values) list + names_list = malloc(names_list_len); + rc = crypt_list_all_constants(names_list, &names_list_len); + printf(" supported constants: \n%s \n", names_list); + printf("\n"); +} + + +/* $Source: $ */ +/* $Revision: $ */ +/* $Date: $ */ diff --git a/demos/demo_crypt_sizes.c b/demos/demo_crypt_sizes.c new file mode 100644 index 0000000..f2154e2 --- /dev/null +++ b/demos/demo_crypt_sizes.c @@ -0,0 +1,55 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file demo_crypt_sizes.c + + Demo how to get various sizes to dynamic languages + like Python - Larry Bugbee, February 2013 +*/ + + +// in lieu of a header file +int crypt_get_size(const char* namein, int *sizeout); +int crypt_list_all_sizes(char *names_list, + unsigned long *names_list_size); + + +int main(void) { + int rc; + printf("\n"); + + // given a specific size name, get and print its size + char name[] = "ecc_key_struct_size"; + int size; + rc = crypt_get_size(name, &size); + printf(" %s is %d \n", name, size); + printf("\n"); + + // get and print the length of the names (and sizes) list + char *sizes_list; + unsigned long sizes_list_len; + rc = crypt_list_all_sizes(NULL, &sizes_list_len); + printf(" need to allocate %lu bytes \n", sizes_list_len); + printf("\n"); + + // get and print the names (and sizes) list + sizes_list = malloc(sizes_list_len); + rc = crypt_list_all_sizes(sizes_list, &sizes_list_len); + printf(" supported sizes: %s \n", sizes_list); + printf("\n"); +} + + +/* $Source: $ */ +/* $Revision: $ */ +/* $Date: $ */ diff --git a/makefile b/makefile index 7a055f2..2ff2d82 100644 --- a/makefile +++ b/makefile @@ -81,6 +81,8 @@ TV=tv_gen MULTI=multi TIMING=timing TEST=test +SIZES=sizes +CONSTANTS=constants #LIBPATH-The directory for libtomcrypt to be installed to. #INCPATH-The directory to install the header files for libtomcrypt. @@ -255,6 +257,8 @@ TVS=demos/tv_gen.o MULTIS=demos/multi.o TIMINGS=demos/timing.o TESTS=demos/test.o +CRYPTSIZES=demos/demo_crypt_sizes.o +CRYPTCONSTANTS=demos/demo_crypt_constants.o #Files left over from making the crypt.pdf. LEFTOVERS=*.dvi *.log *.aux *.toc *.idx *.ilg *.ind *.out *.lof @@ -312,6 +316,12 @@ timing: library testprof/$(LIBTEST) $(TIMINGS) test: library testprof/$(LIBTEST) $(TESTS) $(CC) $(LDFLAGS) $(TESTS) testprof/$(LIBTEST) $(LIBNAME) $(EXTRALIBS) -o $(TEST) +sizes: library $(CRYPTSIZES) + $(CC) $(LDFLAGS) $(CRYPTSIZES) $(LIBNAME) $(EXTRALIBS) -o $(SIZES) + +constants: library $(CRYPTCONSTANTS) + $(CC) $(LDFLAGS) $(CRYPTCONSTANTS) $(LIBNAME) $(EXTRALIBS) -o $(CONSTANTS) + #This rule installs the library and the header files. This must be run #as root in order to have a high enough permission to write to the correct #directories and to set the owner and group to root. @@ -360,6 +370,7 @@ clean: rm -rf `find . -type d -name "*.libs" | xargs` rm -f crypt.aux crypt.dvi crypt.idx crypt.ilg crypt.ind crypt.log crypt.toc rm -f $(TV) $(SMALL) $(CRYPT) $(HASH) $(MULTI) $(TIMING) $(TEST) + rm -f $(SIZES) $(CONSTANTS) rm -rf doc/doxygen rm -f `find . -type f -name "*.pdf" | grep -FL crypt.pdf | xargs` rm -f *.txt diff --git a/src/misc/crypt/crypt_constants.c b/src/misc/crypt/crypt_constants.c new file mode 100755 index 0000000..79b1942 --- /dev/null +++ b/src/misc/crypt/crypt_constants.c @@ -0,0 +1,121 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +/** + @file crypt_constants.c + + Make various constants available to dynamic languages + like Python - Larry Bugbee, February 2013 + + LB - Dec 2013 - revised to include compiler define options +*/ + +typedef struct { + const char *name; + const long value; +} crypt_constant; + +crypt_constant _crypt_constants[] = { +#ifdef LTC_CTR_MODE + {"CTR_COUNTER_LITTLE_ENDIAN", CTR_COUNTER_LITTLE_ENDIAN}, + {"CTR_COUNTER_BIG_ENDIAN", CTR_COUNTER_BIG_ENDIAN}, + {"LTC_CTR_RFC3686", LTC_CTR_RFC3686}, +#endif + + {"PK_PUBLIC", PK_PUBLIC}, + {"PK_PRIVATE", PK_PRIVATE}, +#ifdef LTC_MRSA + {"MIN_RSA_SIZE", MIN_RSA_SIZE}, + {"MAX_RSA_SIZE", MAX_RSA_SIZE}, +#endif + +#ifdef LTC_PKCS_1 + {"LTC_PKCS_1_OAEP", LTC_PKCS_1_OAEP}, + {"LTC_PKCS_1_PSS", LTC_PKCS_1_PSS}, + {"LTC_PKCS_1_V1_5", LTC_PKCS_1_V1_5}, +#endif +}; + + +/* crypt_get_constant() + * sizeout will be the size (bytes) of the named struct or union + * return -1 if named item not found + */ +int crypt_get_constant(const char* namein, int *valueout) { + int i; + int _crypt_constants_len = sizeof(_crypt_constants) / sizeof(crypt_constant); + for (i=0; i<_crypt_constants_len; i++) { + if (strcmp(_crypt_constants[i].name, namein) == 0) { + *valueout = _crypt_constants[i].value; + return 0; + } + } + return 1; +} + +/* crypt_list_all_constants() + * if names_list is NULL, names_list_size will be the minimum + * size needed to receive the complete names_list + * if names_list is NOT NULL, names_list must be the addr with + * sufficient memory allocated into which the names_list + * is to be written. Also, the value in names_list_size + * sets the upper bound of the number of characters to be + * written. + * a -1 return value signifies insufficient space made available + */ +int crypt_list_all_constants(char *names_list, + unsigned long *names_list_size) { + int i; + unsigned long total_len = 0; + char number[10]; + int number_len; + int count = sizeof(_crypt_constants) / sizeof(crypt_constant); + + /* calculate amount of memory required for the list */ + for (i=0; i *names_list_size) { + return -1; + } + /* build the names list */ + char *ptr = names_list; + for (i=0; i *names_list_size) { + return -1; + } + /* build the names list */ + char *ptr = names_list; + for (i=0; i