Bläddra i källkod

include compiler defines and other minor refinements

Larry Bugbee 11 år sedan
förälder
incheckning
1b29ce896f

+ 60 - 0
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, [email protected], 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:  $ */

+ 55 - 0
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, [email protected], 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:  $ */

+ 11 - 0
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

+ 121 - 0
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, [email protected], 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<count; i++) {
+        total_len += strlen(_crypt_constants[i].name) + 1;
+        // the above +1 is for the commas
+        sprintf(number,"%lu",_crypt_constants[i].value);
+        total_len += strlen(number) + 1;
+        // this last +1 is for newlines (and ending NULL)
+    }
+    
+    if (names_list == NULL) {
+        *names_list_size = total_len;
+    } else {
+        if (total_len > *names_list_size) {
+            return -1;
+        }
+        /* build the names list */
+        char *ptr = names_list;
+        for (i=0; i<count; i++) {
+            strcpy(ptr, _crypt_constants[i].name);
+            ptr += strlen(_crypt_constants[i].name);
+            strcpy(ptr, ",");
+            ptr += 1;
+            
+            number_len = sprintf(number,"%lu",_crypt_constants[i].value);
+            strcpy(ptr, number);
+            ptr += number_len;
+            strcpy(ptr, "\n");
+            ptr += 1;
+        }
+        ptr -= 1;       // to remove the trailing comma
+        *ptr = 0;
+    }
+    return 0;
+}
+
+
+/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_constants.c,v $ */
+/* $Revision:  $ */
+/* $Date:  $ */

+ 44 - 0
src/misc/crypt/crypt_inits.c

@@ -0,0 +1,44 @@
+/* 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, [email protected], http://libtom.org
+ */
+#include "tomcrypt.h"
+
+/**
+  @file crypt_inits.c
+  
+  Provide math library functions for dynamic languages 
+  like Python - Larry Bugbee, February 2013
+*/
+
+
+#ifdef USE_LTM
+void init_LTM(void) {
+    ltc_mp = ltm_desc;
+}
+#endif
+
+#ifdef USE_TFM
+void init_TFM(void) {
+    ltc_mp = tfm_desc;
+}
+#endif
+
+/*                          *** use of GMP is untested ***
+    #ifdef USE_GMP
+    void init_GMP(void) {
+        ltc_mp = gmp_desc;
+    }
+    #endif
+*/
+
+
+/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_inits.c,v $ */
+/* $Revision:  $ */
+/* $Date:  $ */

+ 305 - 0
src/misc/crypt/crypt_sizes.c

@@ -0,0 +1,305 @@
+/* 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, [email protected], http://libtom.org
+ */
+#include "tomcrypt.h"
+
+/**
+  @file crypt_sizes.c
+  
+  Make various struct sizes 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 size;
+} crypt_size;
+
+crypt_size _crypt_sizes[] = {
+    // hash state sizes
+    {"hash_descriptor_struct_size",   sizeof(struct ltc_hash_descriptor)},
+    {"hash_state_union_size",         sizeof(hash_state)},
+#ifdef LTC_SHA256
+    {"sha256_state_struct_size",      sizeof(struct sha256_state)},
+#endif
+#ifdef LTC_SHA512
+    {"sha512_state_struct_size",      sizeof(struct sha512_state)},
+#endif
+#ifdef LTC_WHIRLPOOL
+    {"whirlpool_state_struct_size",   sizeof(struct whirlpool_state)},
+#endif
+#ifdef LTC_MD2
+    {"md2_state_struct_size",         sizeof(struct md2_state)},
+#endif
+#ifdef LTC_MD4
+    {"md4_state_struct_size",         sizeof(struct md4_state)},
+#endif
+#ifdef LTC_MD5
+    {"md5_state_struct_size",         sizeof(struct md5_state)},
+#endif
+#ifdef LTC_RIPEMD128
+    {"rmd128_state_struct_size",      sizeof(struct rmd128_state)},
+#endif
+#ifdef LTC_RIPEMD160
+    {"rmd160_state_struct_size",      sizeof(struct rmd160_state)},
+#endif
+#ifdef LTC_RIPEMD256
+    {"rmd256_state_struct_size",      sizeof(struct rmd256_state)},
+#endif
+#ifdef LTC_RIPEMD320
+    {"rmd320_state_struct_size",      sizeof(struct rmd320_state)},
+#endif
+#ifdef LTC_SHA1
+    {"sha1_state_struct_size",        sizeof(struct sha1_state)},
+#endif
+#ifdef LTC_TIGER
+    {"tiger_state_struct_size",       sizeof(struct tiger_state)},
+#endif
+#ifdef LTC_CHC_HASH
+    {"chc_state_struct_size",         sizeof(struct chc_state)},
+#endif
+    
+    // block cipher key sizes
+    {"cipher_descriptor_struct_size", sizeof(struct ltc_cipher_descriptor)},
+    {"symmetric_key_union_size",      sizeof(symmetric_key)},
+#ifdef LTC_ANUBIS
+    {"anubis_key_struct_size",        sizeof(struct anubis_key)},
+#endif
+#ifdef LTC_CAMELLIA
+    {"camellia_key_struct_size",      sizeof(struct camellia_key)},
+#endif
+#ifdef LTC_BLOWFISH
+    {"blowfish_key_struct_size",      sizeof(struct blowfish_key)},
+#endif
+#ifdef LTC_CAST5
+    {"cast5_key_struct_size",         sizeof(struct cast5_key)},
+#endif
+#ifdef LTC_DES
+    {"des_key_struct_size",           sizeof(struct des_key)},
+    {"des3_key_struct_size",          sizeof(struct des3_key)},
+#endif
+#ifdef LTC_KASUMI
+    {"kasumi_key_struct_size",        sizeof(struct kasumi_key)},
+#endif
+#ifdef LTC_KHAZAD
+    {"khazad_key_struct_size",        sizeof(struct khazad_key)},
+#endif
+#ifdef LTC_KSEED
+    {"kseed_key_struct_size",         sizeof(struct kseed_key)},
+#endif
+#ifdef LTC_MULTI2
+//    {"multi2_key_struct_size",        sizeof(struct multi2_key)},
+#endif
+#ifdef LTC_NOEKEON
+    {"noekeon_key_struct_size",       sizeof(struct noekeon_key)},
+#endif
+#ifdef LTC_RC2
+    {"rc2_key_struct_size",           sizeof(struct rc2_key)},
+#endif
+#ifdef LTC_RC5
+    {"rc5_key_struct_size",           sizeof(struct rc5_key)},
+#endif
+#ifdef LTC_RC6
+    {"rc6_key_struct_size",           sizeof(struct rc6_key)},
+#endif
+#ifdef LTC_SKIPJACK
+    {"skipjack_key_struct_size",      sizeof(struct skipjack_key)},
+#endif
+#ifdef LTC_XTEA
+    {"xtea_key_struct_size",          sizeof(struct xtea_key)},
+#endif
+#ifdef LTC_RIJNDAEL
+    {"rijndael_key_struct_size",      sizeof(struct rijndael_key)},
+#endif
+#ifdef LTC_SAFER
+    {"safer_key_struct_size",         sizeof(struct safer_key)},
+#endif
+#ifdef LTC_SAFERP
+    {"saferp_key_struct_size",        sizeof(struct saferp_key)},
+#endif
+#ifdef LTC_TWOFISH
+    {"twofish_key_struct_size",       sizeof(struct twofish_key)},
+#endif
+
+    // mode sizes
+#ifdef LTC_CBC_MODE
+    {"symmetric_CBC_struct_size",     sizeof(symmetric_CBC)},
+#endif
+#ifdef LTC_CFB_MODE
+    {"symmetric_CFB_struct_size",     sizeof(symmetric_CFB)},
+#endif
+#ifdef LTC_CTR_MODE
+    {"symmetric_CTR_struct_size",     sizeof(symmetric_CTR)},
+#endif
+#ifdef LTC_ECB_MODE
+    {"symmetric_ECB_struct_size",     sizeof(symmetric_ECB)},
+#endif
+#ifdef LTC_F8_MODE
+    {"symmetric_F8_struct_size",      sizeof(symmetric_F8)},
+#endif
+#ifdef LTC_LRW_MODE
+    {"symmetric_LRW_struct_size",     sizeof(symmetric_LRW)},
+#endif
+#ifdef LTC_OFB_MODE
+    {"symmetric_OFB_struct_size",     sizeof(symmetric_OFB)},
+#endif
+
+    // MAC sizes            -- no states for ccm, lrw
+#ifdef LTC_F9_MODE
+    {"f9_state_struct_size",          sizeof(f9_state)},
+#endif
+#ifdef LTC_HMAC
+    {"hmac_state_struct_size",        sizeof(hmac_state)},
+#endif
+#ifdef LTC_OMAC
+    {"omac_state_struct_size",        sizeof(omac_state)},
+#endif
+#ifdef LTC_PELICAN
+    {"pelican_state_struct_size",     sizeof(pelican_state)},
+#endif
+#ifdef LTC_PMAC
+    {"pmac_state_struct_size",        sizeof(pmac_state)},
+#endif
+#ifdef LTC_XCBC
+    {"xcbc_state_struct_size",        sizeof(xcbc_state)},
+#endif
+#ifdef LTC_OCB_MODE
+    {"ocb_state_struct_size",         sizeof(ocb_state)},
+#endif
+#ifdef LTC_OCB3_MODE
+    {"ocb3_state_struct_size",        sizeof(ocb3_state)},
+#endif
+#ifdef LTC_GCM_MODE
+    {"gcm_state_struct_size",         sizeof(gcm_state)},
+#endif
+#ifdef LTC_EAX_MODE
+    {"eax_state_struct_size",         sizeof(eax_state)},
+#endif
+#ifdef LTC_CCM_MODE
+// not defined
+#endif
+#ifdef LRW_MODE
+// not defined
+#endif
+
+    // asymmetric keys
+#ifdef LTC_MRSA
+    {"rsa_key_struct_size",           sizeof(rsa_key)},
+#endif
+#ifdef LTC_MDSA
+    {"dsa_key_struct_size",           sizeof(dsa_key)},
+#endif
+#ifdef MDH
+    {"dh_key_struct_size",            sizeof(dh_key)},
+#endif
+#ifdef LTC_MECC
+    {"ecc_set_struct_size",           sizeof(ltc_ecc_set_type)},
+    {"ecc_key_struct_size",           sizeof(ecc_key)},
+    {"ecc_point_struct_size",         sizeof(ecc_point)},
+#endif
+#ifdef MKAT
+//    {"katja_key_struct_size",         sizeof(katja_key)},
+#endif
+
+    // prng state sizes
+    {"prng_descriptor_struct_size",   sizeof(struct ltc_prng_descriptor)},
+    {"prng_state_union_size",         sizeof(prng_state)},
+#ifdef LTC_FORTUNA
+    {"fortuna_prng_struct_size",      sizeof(struct fortuna_prng)},
+#endif
+#ifdef LTC_RC4
+    {"rc4_prng_struct_size",          sizeof(struct rc4_prng)},
+#endif
+#ifdef LTC_SOBER128
+    {"sober128_prng_struct_size",     sizeof(struct sober128_prng)},
+#endif
+#ifdef LTC_YARROW
+    {"yarrow_prng_struct_size",       sizeof(struct yarrow_prng)},
+#endif
+    // sprng has no state as it uses other potentially available sources
+    // like /dev/random.  See Developers Guide for more info.
+};
+
+/* crypt_get_size()
+ * sizeout will be the size (bytes) of the named struct or union
+ * return -1 if named item not found
+ */
+int crypt_get_size(const char* namein, int *sizeout) {
+    int i;
+    int count = sizeof(_crypt_sizes) / sizeof(crypt_size);
+    for (i=0; i<count; i++) {
+        if (strcmp(_crypt_sizes[i].name, namein) == 0) {
+            *sizeout = _crypt_sizes[i].size;
+            return 0;
+        }
+    }
+    return -1;
+}
+
+/* crypt_list_all_sizes()
+ * 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_sizes(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_sizes) / sizeof(crypt_size);
+    
+    /* calculate amount of memory required for the list */
+    for (i=0; i<count; i++) {
+        total_len += strlen(_crypt_sizes[i].name) + 1;
+        // the above +1 is for the commas
+        sprintf(number,"%lu",_crypt_sizes[i].size);
+        total_len += strlen(number) + 1;
+        // this last +1 is for newlines (and ending NULL)
+    }
+    
+    if (names_list == NULL) {
+        *names_list_size = total_len;
+    } else {
+        if (total_len > *names_list_size) {
+            return -1;
+        }
+        /* build the names list */
+        char *ptr = names_list;
+        for (i=0; i<count; i++) {
+            strcpy(ptr, _crypt_sizes[i].name);
+            ptr += strlen(_crypt_sizes[i].name);
+            strcpy(ptr, ",");
+            ptr += 1;
+            
+            number_len = sprintf(number,"%lu",_crypt_sizes[i].size);
+            strcpy(ptr, number);
+            ptr += number_len;
+            strcpy(ptr, "\n");
+            ptr += 1;
+        }
+        ptr -= 1;       // to remove the trailing comma
+        *ptr = 0;
+    }
+    return 0;
+}
+
+
+/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_sizes.c,v $ */
+/* $Revision:  $ */
+/* $Date:  $ */