diff --git a/demos/demo_crypt_constants.c b/demos/demo_crypt_constants.c index 410c132..9c41cf7 100644 --- a/demos/demo_crypt_constants.c +++ b/demos/demo_crypt_constants.c @@ -12,43 +12,43 @@ /** @file demo_crypt_constants.c - - Demo how to get various constants to dynamic languages + + Demo how to get various constants to dynamic languages like Python - + Larry Bugbee, February 2013 */ 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"); - + + if (crypt_get_constant(name, &value) != 0) + exit(EXIT_FAILURE); + printf("\n %s is %d \n\n", name, value); + // 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"); - + + if (crypt_list_all_constants(NULL, &names_list_len) != 0) + exit(EXIT_FAILURE); + printf(" need to allocate %lu bytes \n\n", names_list_len); + // 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"); + if ((names_list = malloc(names_list_len)) == NULL) + exit(EXIT_FAILURE); + if (crypt_list_all_constants(names_list, &names_list_len) != 0) + exit(EXIT_FAILURE); + printf(" supported constants:\n\n%s\n\n", names_list); + free(names_list); + + return 0; } -/* $Source: $ */ -/* $Revision: $ */ -/* $Date: $ */ +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ diff --git a/demos/demo_crypt_sizes.c b/demos/demo_crypt_sizes.c index ea566d9..dcd8bb0 100644 --- a/demos/demo_crypt_sizes.c +++ b/demos/demo_crypt_sizes.c @@ -12,35 +12,31 @@ /** @file demo_crypt_sizes.c - - Demo how to get various sizes to dynamic languages + + Demo how to get various sizes to dynamic languages like Python - Larry Bugbee, February 2013 */ 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"); - + printf("\n size of '%s' is %d \n\n", name, size); + // 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"); - + printf(" need to allocate %lu bytes \n\n", sizes_list_len); + // 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"); + printf(" supported sizes:\n\n%s\n\n", sizes_list); } diff --git a/src/headers/tomcrypt_misc.h b/src/headers/tomcrypt_misc.h index 73b0c5f..18d8b6d 100644 --- a/src/headers/tomcrypt_misc.h +++ b/src/headers/tomcrypt_misc.h @@ -51,12 +51,10 @@ int crypt_fsa(void *mp, ...); /* ---- Dynamic language support ---- */ int crypt_get_constant(const char* namein, int *valueout); -int crypt_list_all_constants(char *names_list, - unsigned long *names_list_size); +int crypt_list_all_constants(char *names_list, unsigned long *names_list_size); int crypt_get_size(const char* namein, int *sizeout); -int crypt_list_all_sizes(char *names_list, - unsigned long *names_list_size); +int crypt_list_all_sizes(char *names_list, unsigned long *names_list_size); void init_LTM(void); void init_TFM(void); diff --git a/src/misc/crypt/crypt_constants.c b/src/misc/crypt/crypt_constants.c index 486a312..eda3b75 100755 --- a/src/misc/crypt/crypt_constants.c +++ b/src/misc/crypt/crypt_constants.c @@ -12,10 +12,10 @@ /** @file crypt_constants.c - - Make various constants available to dynamic languages + + Make various constants available to dynamic languages like Python - Larry Bugbee, February 2013 - + LB - Dec 2013 - revised to include compiler define options LB - Mar 2014 - added endianness and word size */ @@ -39,7 +39,7 @@ crypt_constant _crypt_constants[] = { {"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}, @@ -47,27 +47,27 @@ crypt_constant _crypt_constants[] = { #endif #ifdef ENDIAN_LITTLE - {"ENDIAN_LITTLE", 1}, // true + {"ENDIAN_LITTLE", 1}, #else - {"ENDIAN_LITTLE", 0}, // false + {"ENDIAN_LITTLE", 0}, #endif #ifdef ENDIAN_BIG - {"ENDIAN_BIG", 1}, // true + {"ENDIAN_BIG", 1}, #else - {"ENDIAN_BIG", 0}, // false + {"ENDIAN_BIG", 0}, #endif #ifdef ENDIAN_32BITWORD - {"ENDIAN_32BITWORD", 1}, // true + {"ENDIAN_32BITWORD", 1}, #else - {"ENDIAN_32BITWORD", 0}, // false + {"ENDIAN_32BITWORD", 0}, #endif #ifdef ENDIAN_64BITWORD - {"ENDIAN_64BITWORD", 1}, // true + {"ENDIAN_64BITWORD", 1}, #else - {"ENDIAN_64BITWORD", 0}, // false + {"ENDIAN_64BITWORD", 0}, #endif }; @@ -89,32 +89,31 @@ int crypt_get_constant(const char* namein, int *valueout) { } /* crypt_list_all_constants() - * if names_list is NULL, names_list_size will be the minimum + * if names_list is NULL, names_list_size will be the minimum * number of bytes needed to receive the complete names_list - * if names_list is NOT NULL, names_list must be the addr of - * 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 + * if names_list is NOT NULL, names_list must be the addr of + * 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 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); - + int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]); + /* calculate amount of memory required for the list */ for (i=0; i