Преглед изворни кода

Merge pull request #696 from libtom/some-improvements

Some improvements
Steffen Jaeckel пре 3 недеља
родитељ
комит
5edb54e522
100 измењених фајлова са 512 додато и 389 уклоњено
  1. 6 2
      .github/workflows/main.yml
  2. 6 2
      demos/gcm-file/gcm_filehandle.c
  3. 6 2
      demos/hashsum.c
  4. 2 2
      demos/pem-info.c
  5. 3 3
      demos/timing.c
  6. 22 18
      demos/tv_gen.c
  7. 4 3
      makefile.mingw
  8. 4 3
      makefile.msvc
  9. 4 3
      makefile.unix
  10. 7 5
      makefile_include.mk
  11. 1 1
      src/ciphers/aes/aes.c
  12. 1 1
      src/ciphers/aes/aes_desc.c
  13. 1 1
      src/ciphers/aes/aesni.c
  14. 1 1
      src/ciphers/anubis.c
  15. 1 1
      src/ciphers/camellia.c
  16. 3 3
      src/ciphers/cast5.c
  17. 2 2
      src/ciphers/des.c
  18. 1 1
      src/ciphers/idea.c
  19. 1 1
      src/ciphers/kasumi.c
  20. 1 1
      src/ciphers/khazad.c
  21. 1 1
      src/ciphers/kseed.c
  22. 1 1
      src/ciphers/multi2.c
  23. 1 1
      src/ciphers/noekeon.c
  24. 1 1
      src/ciphers/serpent.c
  25. 12 12
      src/ciphers/sm4.c
  26. 1 1
      src/ciphers/tea.c
  27. 1 1
      src/ciphers/twofish/twofish.c
  28. 1 1
      src/ciphers/xtea.c
  29. 1 1
      src/encauth/ccm/ccm_test.c
  30. 1 1
      src/encauth/eax/eax_test.c
  31. 1 1
      src/encauth/gcm/gcm_test.c
  32. 1 1
      src/encauth/ocb/ocb_init.c
  33. 6 0
      src/encauth/ocb/ocb_ntz.c
  34. 1 1
      src/encauth/ocb/ocb_test.c
  35. 1 1
      src/encauth/ocb3/ocb3_init.c
  36. 6 0
      src/encauth/ocb3/ocb3_int_ntz.c
  37. 1 1
      src/encauth/ocb3/ocb3_test.c
  38. 1 1
      src/encauth/siv/siv.c
  39. 1 1
      src/hashes/chc/chc.c
  40. 1 1
      src/hashes/rmd128.c
  41. 1 1
      src/hashes/rmd160.c
  42. 1 1
      src/hashes/rmd256.c
  43. 1 1
      src/hashes/rmd320.c
  44. 9 9
      src/hashes/tiger.c
  45. 1 1
      src/hashes/whirl/whirl.c
  46. 8 0
      src/headers/tomcrypt_cfg.h
  47. 0 8
      src/headers/tomcrypt_pk.h
  48. 15 1
      src/headers/tomcrypt_private.h
  49. 1 1
      src/mac/f9/f9_test.c
  50. 1 1
      src/mac/omac/omac_test.c
  51. 1 1
      src/mac/pelican/pelican_test.c
  52. 2 2
      src/mac/pmac/pmac_init.c
  53. 6 0
      src/mac/pmac/pmac_ntz.c
  54. 1 1
      src/mac/pmac/pmac_test.c
  55. 1 1
      src/mac/xcbc/xcbc_test.c
  56. 1 1
      src/math/ltm_desc.c
  57. 1 1
      src/math/tfm_desc.c
  58. 4 3
      src/misc/base16/base16_encode.c
  59. 9 0
      src/misc/crypt/crypt.c
  60. 7 2
      src/misc/crypt/crypt_register_all_hashes.c
  61. 1 1
      src/misc/error_to_string.c
  62. 2 2
      src/misc/pbes/pbes2.c
  63. 2 2
      src/misc/pem/pem.c
  64. 22 22
      src/misc/pem/pem_pkcs.c
  65. 50 27
      src/misc/pem/pem_read.c
  66. 6 6
      src/misc/pem/pem_ssh.c
  67. 1 1
      src/modes/ctr/ctr_test.c
  68. 1 1
      src/modes/lrw/lrw_test.c
  69. 5 5
      src/pk/asn1/der/general/der_asn1_maps.c
  70. 1 1
      src/pk/asn1/der/general/der_decode_asn1_identifier.c
  71. 2 2
      src/pk/asn1/der/ia5/der_length_ia5_string.c
  72. 2 13
      src/pk/asn1/der/object_identifier/der_encode_object_identifier.c
  73. 24 12
      src/pk/asn1/der/object_identifier/der_length_object_identifier.c
  74. 2 2
      src/pk/asn1/der/printable_string/der_length_printable_string.c
  75. 22 16
      src/pk/asn1/der/sequence/der_decode_sequence_flexi.c
  76. 2 2
      src/pk/asn1/der/teletex_string/der_length_teletex_string.c
  77. 2 2
      src/pk/asn1/oid/pk_get.c
  78. 49 26
      src/pk/asn1/oid/pk_oid_str.c
  79. 1 1
      src/pk/asn1/x509/x509_decode_subject_public_key_info.c
  80. 2 2
      src/pk/asn1/x509/x509_encode_subject_public_key_info.c
  81. 1 1
      src/pk/dsa/dsa_decrypt_key.c
  82. 1 1
      src/pk/dsa/dsa_generate_pqg.c
  83. 1 1
      src/pk/ec25519/ec25519_export.c
  84. 1 1
      src/pk/ecc/ecc_decrypt_key.c
  85. 1 1
      src/pk/ecc/ecc_export_openssl.c
  86. 1 1
      src/pk/ecc/ecc_ssh_ecdsa_encode_name.c
  87. 3 3
      src/pk/rsa/rsa_key.c
  88. 1 1
      src/pk/rsa/rsa_verify_hash.c
  89. 3 3
      tests/base16_test.c
  90. 3 3
      tests/base32_test.c
  91. 15 15
      tests/base64_test.c
  92. 1 1
      tests/bcrypt_test.c
  93. 5 2
      tests/common.c
  94. 41 43
      tests/der_test.c
  95. 11 11
      tests/dh_test.c
  96. 10 10
      tests/dsa_test.c
  97. 5 5
      tests/ecc_test.c
  98. 9 9
      tests/ed25519_test.c
  99. 10 10
      tests/file_test.c
  100. 1 0
      tests/misc_test.c

+ 6 - 2
.github/workflows/main.yml

@@ -5,12 +5,16 @@ on:
     branches:
       - master
       - develop
-      - /^release\/.*$/
+      - 'release/**'
+      - 'support/**'
+      - 'ci/**'
   pull_request:
     branches:
       - master
       - develop
-      - /^release\/.*$/
+      - 'release/**'
+      - 'support/**'
+      - 'ci/**'
 
 concurrency:
   group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}

+ 6 - 2
demos/gcm-file/gcm_filehandle.c

@@ -116,7 +116,7 @@ int gcm_filehandle(      int           cipher,
     if (direction == GCM_DECRYPT) {
        tot_data -= taglen;
     }
-    rewind(in);
+    fseek(in, 0, SEEK_SET);
     do {
        x = MIN(tot_data, LTC_FILE_READ_BUFSIZE);
        x = fread(buf, 1, x, in);
@@ -140,6 +140,10 @@ int gcm_filehandle(      int           cipher,
     }
 
     if (direction == GCM_DECRYPT) {
+       if (feof(in) || ferror(in)) {
+          err = CRYPT_ERROR;
+          goto LBL_CLEANBUF;
+       }
        x = fread(buf, 1, taglen, in);
        if (x != taglen) {
           err = CRYPT_ERROR;
@@ -170,7 +174,7 @@ LBL_ERR:
 #endif
     if(*res == 0) {
        x = ftell(out);
-       rewind(out);
+       fseek(in, 0, SEEK_SET);
        while((size_t)ftell(out) < x) {
           fwrite(buf, 1, LTC_FILE_READ_BUFSIZE, out);
        }

+ 6 - 2
demos/hashsum.c

@@ -30,6 +30,10 @@
             '\255')
 #define HEXOF(x) (x - s_base(x))
 
+#ifndef LTC_ARRAY_SIZE
+#define LTC_ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
+#endif
+
 static char* hashsum;
 
 static void cleanup(void)
@@ -190,7 +194,7 @@ int main(int argc, char **argv)
       die(EXIT_FAILURE);
    }
 
-   for (x = 0; x < sizeof(idxs)/sizeof(idxs[0]); ++x) {
+   for (x = 0; x < LTC_ARRAY_SIZE(idxs); ++x) {
       idxs[x] = -2;
    }
    argn = 1;
@@ -243,7 +247,7 @@ int main(int argc, char **argv)
                die(EXIT_FAILURE);
             }
             idx++;
-            if ((size_t)idx >= sizeof(idxs)/sizeof(idxs[0])) {
+            if ((size_t)idx >= LTC_ARRAY_SIZE(idxs)) {
                fprintf(stderr, "%s: Too many '-a' options chosen\n", hashsum);
                die(EXIT_FAILURE);
             }

+ 2 - 2
demos/pem-info.c

@@ -34,7 +34,7 @@ static const struct {
 static const char *s_map_cipher(const char *name)
 {
    unsigned long n;
-   for (n = 0; n < sizeof(cipher_name_map)/sizeof(cipher_name_map[0]); ++n) {
+   for (n = 0; n < LTC_ARRAY_SIZE(cipher_name_map); ++n) {
       if (strcmp(name, cipher_name_map[n].is) == 0)
          return cipher_name_map[n].should;
    }
@@ -61,7 +61,7 @@ static const char *s_map_mode(enum cipher_mode mode)
 {
    size_t n;
    mode &= cm_modes | cm_1bit | cm_8bit;
-   for (n = 0; n < sizeof(cipher_mode_map)/sizeof(cipher_mode_map[0]); ++n) {
+   for (n = 0; n < LTC_ARRAY_SIZE(cipher_mode_map); ++n) {
       if (cipher_mode_map[n].mode == mode)
          return cipher_mode_map[n].name;
    }

+ 3 - 3
demos/timing.c

@@ -658,7 +658,7 @@ static const struct {
 
    if (ltc_mp.name == NULL) return;
 
-   for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
+   for (x = 0; x < LTC_ARRAY_SIZE(groups); x++) {
        t2 = 0;
        for (y = 0; y < 4; y++) {
            t_start();
@@ -1408,7 +1408,7 @@ if (argc > 1) {
    if (strstr(argv[1], "-h")) {
       die(EXIT_SUCCESS);
    } else if (strstr(argv[1], "-l")) {
-      for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
+      for (i = 0; i < LTC_ARRAY_SIZE(test_functions); ++i) {
          printf("%s\n", test_functions[i].name);
       }
       exit(0);
@@ -1446,7 +1446,7 @@ if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT
 /* single test name from commandline */
 if (argc > 1) single_test = argv[1];
 
-for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
+for (i = 0; i < LTC_ARRAY_SIZE(test_functions); ++i) {
    if (single_test && strstr(test_functions[i].name, single_test) == NULL) {
      continue;
    }

+ 22 - 18
demos/tv_gen.c

@@ -2,6 +2,14 @@
 /* SPDX-License-Identifier: Unlicense */
 #include "tomcrypt_private.h"
 
+#define OPEN_FILE(f, o) do { \
+   o = fopen(f, "w");                     \
+   if (o == NULL) {                       \
+      perror("can't open " f);   \
+      return;                    \
+   }                             \
+} while(0)
+
 static void hash_gen(void)
 {
    unsigned char md[MAXBLOCKSIZE], *buf;
@@ -9,11 +17,7 @@ static void hash_gen(void)
    FILE *out;
    int   err;
 
-   out = fopen("hash_tv.txt", "w");
-   if (out == NULL) {
-      perror("can't open hash_tv.txt");
-      return;
-   }
+   OPEN_FILE("hash_tv.txt", out);
 
    fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n");
    for (x = 0; hash_descriptor[x].name != NULL; x++) {
@@ -52,7 +56,7 @@ static void cipher_gen(void)
    FILE *out;
    symmetric_key skey;
 
-   out = fopen("cipher_tv.txt", "w");
+   OPEN_FILE("cipher_tv.txt", out);
 
    fprintf(out,
 "Cipher Test Vectors\n\nThese are test encryptions with key of nn bytes '00 01 02 03 .. (nn-1)' and original PT of the same style.\n"
@@ -126,7 +130,7 @@ static void hmac_gen(void)
    FILE *out;
    unsigned long len;
 
-   out = fopen("hmac_tv.txt", "w");
+   OPEN_FILE("hmac_tv.txt", out);
 
    fprintf(out,
 "HMAC Tests.  In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed.  The initial key is\n"
@@ -179,7 +183,7 @@ static void omac_gen(void)
    FILE *out;
    unsigned long len;
 
-   out = fopen("omac_tv.txt", "w");
+   OPEN_FILE("omac_tv.txt", out);
 
    fprintf(out,
 "OMAC Tests.  In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed.  The initial key is\n"
@@ -240,7 +244,7 @@ static void pmac_gen(void)
    FILE *out;
    unsigned long len;
 
-   out = fopen("pmac_tv.txt", "w");
+   OPEN_FILE("pmac_tv.txt", out);
 
    fprintf(out,
 "PMAC Tests.  In these tests messages of N bytes long (00,01,02,...,NN-1) are PMAC'ed.  The initial key is\n"
@@ -302,7 +306,7 @@ static void eax_gen(void)
                  plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
    unsigned long len;
 
-   out = fopen("eax_tv.txt", "w");
+   OPEN_FILE("eax_tv.txt", out);
    fprintf(out, "EAX Test Vectors.  Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key.  The outputs\n"
                 "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"
                 "step repeated sufficiently.\n\n");
@@ -368,7 +372,7 @@ static void ocb_gen(void)
                  plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
    unsigned long len;
 
-   out = fopen("ocb_tv.txt", "w");
+   OPEN_FILE("ocb_tv.txt", out);
    fprintf(out, "OCB Test Vectors.  Uses the 00010203...NN-1 pattern for nonce/plaintext/key.  The outputs\n"
                 "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"
                 "step repeated sufficiently.  The nonce is fixed throughout.\n\n");
@@ -437,7 +441,7 @@ static void ocb3_gen(void)
                  plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
    unsigned long len;
 
-   out = fopen("ocb3_tv.txt", "w");
+   OPEN_FILE("ocb3_tv.txt", out);
    fprintf(out, "OCB3 Test Vectors.  Uses the 00010203...NN-1 pattern for nonce/plaintext/key.  The outputs\n"
                 "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"
                 "step repeated sufficiently.  The nonce is fixed throughout. AAD is fixed to 3 bytes (ASCII) 'AAD'.\n\n");
@@ -509,7 +513,7 @@ static void ccm_gen(void)
    unsigned long len;
    const unsigned int taglen[] = {4, 6, 8, 10, 12, 14, 16};
 
-   out = fopen("ccm_tv.txt", "w");
+   OPEN_FILE("ccm_tv.txt", out);
    fprintf(out, "CCM Test Vectors.  Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key.  The outputs\n"
                 "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"
                 "step repeated sufficiently.  The nonce is fixed throughout at 13 bytes 000102...\n\n");
@@ -535,7 +539,7 @@ static void ccm_gen(void)
           nonce[z] = z;
       }
 
-      for (t = 0; t < sizeof(taglen)/sizeof(taglen[0]); ++t) {
+      for (t = 0; t < LTC_ARRAY_SIZE(taglen); ++t) {
          for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
             for (z = 0; z < y1; z++) {
                plaintext[z] = (unsigned char)(z & 255);
@@ -579,7 +583,7 @@ static void gcm_gen(void)
    unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
    unsigned long len;
 
-   out = fopen("gcm_tv.txt", "w");
+   OPEN_FILE("gcm_tv.txt", out);
    fprintf(out, "GCM Test Vectors.  Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key.  The outputs\n"
                 "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"
                 "step repeated sufficiently.  The nonce is fixed throughout at 13 bytes 000102...\n\n");
@@ -641,7 +645,7 @@ static void base64_gen(void)
    char dst[256];
    unsigned long x, len;
 
-   out = fopen("base64_tv.txt", "w");
+   OPEN_FILE("base64_tv.txt", out);
    fprintf(out, "Base64 vectors.  These are the base64 encodings of the strings 00,01,02...NN-1\n\n");
    for (x = 0; x <= 32; x++) {
        for (ch = 0; ch < x; ch++) {
@@ -666,7 +670,7 @@ static void ecc_gen(void)
    ecc_point    *G, *R;
    int           x;
 
-   out = fopen("ecc_tv.txt", "w");
+   OPEN_FILE("ecc_tv.txt", out);
    fprintf(out, "ecc vectors.  These are for kG for k=1,3,9,27,...,3**n until k > order of the curve outputs are <k,x,y> triplets\n\n");
    G = ltc_ecc_new_point();
    R = ltc_ecc_new_point();
@@ -713,7 +717,7 @@ static void lrw_gen(void)
       tweak[x] = key[x] = iv[x] = x;
    }
 
-   out = fopen("lrw_tv.txt", "w");
+   OPEN_FILE("lrw_tv.txt", out);
    for (x = 16; x < (int)(sizeof(buf)); x += 16) {
        if ((err = lrw_start(find_cipher("aes"), iv, key, 16, tweak, 0, &lrw)) != CRYPT_OK) {
           fprintf(stderr, "Error starting LRW-AES: %s\n", error_to_string(err));

+ 4 - 3
makefile.mingw

@@ -236,9 +236,10 @@ TOBJECTS=tests/base16_test.o tests/base32_test.o tests/base64_test.o tests/bcryp
 tests/cipher_hash_test.o tests/common.o tests/der_test.o tests/dh_test.o tests/dsa_test.o \
 tests/ecc_test.o tests/ed25519_test.o tests/file_test.o tests/mac_test.o tests/misc_test.o \
 tests/modes_test.o tests/mpi_test.o tests/multi_test.o tests/no_null_termination_check_test.o \
-tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pkcs_1_eme_test.o tests/pkcs_1_emsa_test.o \
-tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o tests/prng_test.o \
-tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o tests/x25519_test.o
+tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o \
+tests/pkcs_1_emsa_test.o tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o \
+tests/prng_test.o tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o \
+tests/x25519_test.o
 
 #The following headers will be installed by "make install"
 HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \

+ 4 - 3
makefile.msvc

@@ -229,9 +229,10 @@ TOBJECTS=tests/base16_test.obj tests/base32_test.obj tests/base64_test.obj tests
 tests/cipher_hash_test.obj tests/common.obj tests/der_test.obj tests/dh_test.obj tests/dsa_test.obj \
 tests/ecc_test.obj tests/ed25519_test.obj tests/file_test.obj tests/mac_test.obj tests/misc_test.obj \
 tests/modes_test.obj tests/mpi_test.obj tests/multi_test.obj tests/no_null_termination_check_test.obj \
-tests/no_prng.obj tests/padding_test.obj tests/pem_test.obj tests/pkcs_1_eme_test.obj tests/pkcs_1_emsa_test.obj \
-tests/pkcs_1_oaep_test.obj tests/pkcs_1_pss_test.obj tests/pkcs_1_test.obj tests/prng_test.obj \
-tests/rotate_test.obj tests/rsa_test.obj tests/ssh_test.obj tests/store_test.obj tests/test.obj tests/x25519_test.obj
+tests/no_prng.obj tests/padding_test.obj tests/pem_test.obj tests/pk_oid_test.obj tests/pkcs_1_eme_test.obj \
+tests/pkcs_1_emsa_test.obj tests/pkcs_1_oaep_test.obj tests/pkcs_1_pss_test.obj tests/pkcs_1_test.obj \
+tests/prng_test.obj tests/rotate_test.obj tests/rsa_test.obj tests/ssh_test.obj tests/store_test.obj tests/test.obj \
+tests/x25519_test.obj
 
 #The following headers will be installed by "make install"
 HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \

+ 4 - 3
makefile.unix

@@ -250,9 +250,10 @@ TOBJECTS=tests/base16_test.o tests/base32_test.o tests/base64_test.o tests/bcryp
 tests/cipher_hash_test.o tests/common.o tests/der_test.o tests/dh_test.o tests/dsa_test.o \
 tests/ecc_test.o tests/ed25519_test.o tests/file_test.o tests/mac_test.o tests/misc_test.o \
 tests/modes_test.o tests/mpi_test.o tests/multi_test.o tests/no_null_termination_check_test.o \
-tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pkcs_1_eme_test.o tests/pkcs_1_emsa_test.o \
-tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o tests/prng_test.o \
-tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o tests/x25519_test.o
+tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o \
+tests/pkcs_1_emsa_test.o tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o \
+tests/prng_test.o tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o \
+tests/x25519_test.o
 
 #The following headers will be installed by "make install"
 HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \

+ 7 - 5
makefile_include.mk

@@ -420,9 +420,10 @@ TOBJECTS=tests/base16_test.o tests/base32_test.o tests/base64_test.o tests/bcryp
 tests/cipher_hash_test.o tests/common.o tests/der_test.o tests/dh_test.o tests/dsa_test.o \
 tests/ecc_test.o tests/ed25519_test.o tests/file_test.o tests/mac_test.o tests/misc_test.o \
 tests/modes_test.o tests/mpi_test.o tests/multi_test.o tests/no_null_termination_check_test.o \
-tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pkcs_1_eme_test.o tests/pkcs_1_emsa_test.o \
-tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o tests/prng_test.o \
-tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o tests/x25519_test.o
+tests/no_prng.o tests/padding_test.o tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o \
+tests/pkcs_1_emsa_test.o tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o \
+tests/prng_test.o tests/rotate_test.o tests/rsa_test.o tests/ssh_test.o tests/store_test.o tests/test.o \
+tests/x25519_test.o
 
 # The following headers will be installed by "make install"
 HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \
@@ -494,7 +495,7 @@ $(DESTDIR)$(BINPATH):
 	install -p -d $(DESTDIR)$(BINPATH)
 
 .common_install_bins: $(USEFUL_DEMOS) $(DESTDIR)$(BINPATH)
-	for d in $(USEFUL_DEMOS); do $(INSTALL_CMD) -p -m 775 $$d $(DESTDIR)$(BINPATH)/ltc-$$d
+	for d in $(USEFUL_DEMOS); do $(INSTALL_CMD) -p -m 775 $$d $(DESTDIR)$(BINPATH)/ltc-$$d; done
 	$(INSTALL_CMD) -p -m 775 demos/ltc $(DESTDIR)$(BINPATH)
 
 install_docs: $(call print-help,install_docs,Installs the Developer Manual) doc/crypt.pdf
@@ -502,7 +503,7 @@ install_docs: $(call print-help,install_docs,Installs the Developer Manual) doc/
 	install -p -m 644 doc/crypt.pdf $(DESTDIR)$(DATAPATH)
 
 install_test: $(call print-help,install_test,Installs the self-test binary) test $(DESTDIR)$(BINPATH)
-	$(INSTALL_CMD) -p -m 775 $< $(DESTDIR)$(BINPATH)
+	$(INSTALL_CMD) -p -m 775 $< $(DESTDIR)$(BINPATH)/ltc-$<
 
 install_hooks: $(call print-help,install_hooks,Installs the git hooks)
 	for s in `ls hooks/`; do ln -s ../../hooks/$$s .git/hooks/$$s; done
@@ -510,6 +511,7 @@ install_hooks: $(call print-help,install_hooks,Installs the git hooks)
 HEADER_FILES=$(notdir $(HEADERS_PUB))
 .common_uninstall:
 	$(UNINSTALL_CMD) $(DESTDIR)$(LIBPATH)/$(LIBNAME)
+	for d in $(USEFUL_DEMOS) test; do rm -f $(DESTDIR)$(BINPATH)/ltc-$$d; done
 	$(UNINSTALL_CMD) $(HEADER_FILES:%=$(DESTDIR)$(INCPATH)/%)
 
 #This rule cleans the source tree of all compiled code, not including the pdf

+ 1 - 1
src/ciphers/aes/aes.c

@@ -669,7 +669,7 @@ int ECB_TEST(void)
   unsigned char tmp[2][16];
   int i, y;
 
-  for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+  for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
     zeromem(&key, sizeof(key));
     if ((err = rijndael_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
        return err;

+ 1 - 1
src/ciphers/aes/aes_desc.c

@@ -189,7 +189,7 @@ int AES_TEST(void)
   int y;
 #endif
 
-  for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+  for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
     zeromem(&key, sizeof(key));
     if ((err = AES_SETUP(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
        return err;

+ 1 - 1
src/ciphers/aes/aesni.c

@@ -313,7 +313,7 @@ int aesni_test(void)
   unsigned char tmp[2][16];
   int i, y;
 
-  for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+  for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
     zeromem(&key, sizeof(key));
     if ((err = aesni_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
        return err;

+ 1 - 1
src/ciphers/anubis.c

@@ -1498,7 +1498,7 @@ int anubis_test(void)
    unsigned char buf[2][16];
    symmetric_key skey;
 
-   for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
        anubis_setup(tests[x].key, tests[x].keylen, 0, &skey);
        anubis_ecb_encrypt(tests[x].pt, buf[0], &skey);
        anubis_ecb_decrypt(buf[0], buf[1], &skey);

+ 1 - 1
src/ciphers/camellia.c

@@ -680,7 +680,7 @@ int camellia_test(void)
    int err;
    unsigned int x;
 
-   for (x = 0; x < sizeof(tests)/sizeof(tests[0]); x++) {
+   for (x = 0; x < LTC_ARRAY_SIZE(tests); x++) {
       zeromem(&skey, sizeof(skey));
       if ((err = camellia_setup(tests[x].key, tests[x].keylen, 0, &skey)) != CRYPT_OK) {
          return err;

+ 3 - 3
src/ciphers/cast5.c

@@ -492,7 +492,7 @@ int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
 #endif
 
 #define FI cast5_FI
-LTC_INLINE static ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr)
+static LTC_INLINE ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr)
 {
    ulong32 I;
    I = (Km + R);
@@ -500,7 +500,7 @@ LTC_INLINE static ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr)
    return ((S1[LTC_BYTE(I, 3)] ^ S2[LTC_BYTE(I,2)]) - S3[LTC_BYTE(I,1)]) + S4[LTC_BYTE(I,0)];
 }
 
-LTC_INLINE static ulong32 FII(ulong32 R, ulong32 Km, ulong32 Kr)
+static LTC_INLINE ulong32 FII(ulong32 R, ulong32 Km, ulong32 Kr)
 {
    ulong32 I;
    I = (Km ^ R);
@@ -508,7 +508,7 @@ LTC_INLINE static ulong32 FII(ulong32 R, ulong32 Km, ulong32 Kr)
    return ((S1[LTC_BYTE(I, 3)] - S2[LTC_BYTE(I,2)]) + S3[LTC_BYTE(I,1)]) ^ S4[LTC_BYTE(I,0)];
 }
 
-LTC_INLINE static ulong32 FIII(ulong32 R, ulong32 Km, ulong32 Kr)
+static LTC_INLINE ulong32 FIII(ulong32 R, ulong32 Km, ulong32 Kr)
 {
    ulong32 I;
    I = (Km - R);

+ 2 - 2
src/ciphers/des.c

@@ -2018,7 +2018,7 @@ int des_test(void)
     symmetric_key skey;
     int i, err;
 
-    for (i = 0; i < (int)(sizeof(cases)/sizeof(cases[0])); i++)
+    for (i = 0; i < (int)LTC_ARRAY_SIZE(cases); i++)
     {
         if ((err = des_setup(cases[i].key, 8, 0, &skey)) != CRYPT_OK) {
             return err;
@@ -2125,7 +2125,7 @@ int des3_test(void)
         return err;
     }
 
-    for (i = 0; i < (int)(sizeof(cases)/sizeof(cases[0])); i++)
+    for (i = 0; i < (int)LTC_ARRAY_SIZE(cases); i++)
     {
         if ((err = des3_setup(cases[i].key, 16, 0, &skey)) != CRYPT_OK) {
             return err;

+ 1 - 1
src/ciphers/idea.c

@@ -226,7 +226,7 @@ int idea_test(void)
       return CRYPT_FAIL_TESTVECTOR;
    }
 
-   for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
       if ((err = idea_setup(tests[x].key, 16, 8, &key)) != CRYPT_OK) {
          return err;
       }

+ 1 - 1
src/ciphers/kasumi.c

@@ -286,7 +286,7 @@ int kasumi_test(void)
    symmetric_key key;
    int err, x;
 
-   for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
        if ((err = kasumi_setup(tests[x].key, 16, 0, &key)) != CRYPT_OK) {
           return err;
        }

+ 1 - 1
src/ciphers/khazad.c

@@ -794,7 +794,7 @@ int khazad_test(void)
    unsigned char buf[2][8];
    symmetric_key skey;
 
-   for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
        khazad_setup(tests[x].key, 16, 0, &skey);
        khazad_ecb_encrypt(tests[x].pt, buf[0], &skey);
        khazad_ecb_decrypt(buf[0], buf[1], &skey);

+ 1 - 1
src/ciphers/kseed.c

@@ -334,7 +334,7 @@ int kseed_test(void)
    unsigned char buf[2][16];
    symmetric_key skey;
 
-   for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
        kseed_setup(tests[x].key, 16, 0, &skey);
        kseed_ecb_encrypt(tests[x].pt, buf[0], &skey);
        kseed_ecb_decrypt(buf[0], buf[1], &skey);

+ 1 - 1
src/ciphers/multi2.c

@@ -242,7 +242,7 @@ int multi2_test(void)
    symmetric_key skey;
    int err, x;
 
-   for (x = 1; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+   for (x = 1; x < (int)LTC_ARRAY_SIZE(tests); x++) {
       if ((err = multi2_setup(tests[x].key, 40, tests[x].rounds, &skey)) != CRYPT_OK) {
          return err;
       }

+ 1 - 1
src/ciphers/noekeon.c

@@ -267,7 +267,7 @@ int noekeon_test(void)
  unsigned char tmp[2][16];
  int err, i, y;
 
- for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+ for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
     zeromem(&key, sizeof(key));
     if ((err = noekeon_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
        return err;

+ 1 - 1
src/ciphers/serpent.c

@@ -693,7 +693,7 @@ int serpent_test(void)
    symmetric_key key;
    int err, x;
 
-   for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
       if ((err = serpent_setup(tests[x].key, tests[x].keylen, 0, &key)) != CRYPT_OK) {
         return err;
       }

+ 12 - 12
src/ciphers/sm4.c

@@ -67,7 +67,7 @@ static const sm4_u8_t sm4_sbox_table[16][16] = {
  * S-box
  * defined in section 2.6 S-box
  */
-LTC_INLINE static sm4_u8_t s_sm4_sbox(sm4_u8_t a)
+static LTC_INLINE sm4_u8_t s_sm4_sbox(sm4_u8_t a)
 {
     return sm4_sbox_table[(a >> 4) & 0x0f][a & 0x0f];
 }
@@ -80,7 +80,7 @@ LTC_INLINE static sm4_u8_t s_sm4_sbox(sm4_u8_t a)
  * But we just convert a 32bit word byte by byte.
  * So it's OK if we don't convert the endian order
  */
-LTC_INLINE static sm4_u32_t s_sm4_t(sm4_u32_t A)
+static LTC_INLINE sm4_u32_t s_sm4_t(sm4_u32_t A)
 {
     sm4_u8_t  a[4];
     sm4_u8_t  b[4];
@@ -98,7 +98,7 @@ LTC_INLINE static sm4_u32_t s_sm4_t(sm4_u32_t A)
 /*
  * defined in section 6.2 (2) Linear transformation L
  */
-LTC_INLINE static sm4_u32_t s_sm4_L62(sm4_u32_t B)
+static LTC_INLINE sm4_u32_t s_sm4_L62(sm4_u32_t B)
 {
     return B ^ ROLc(B, 2) ^ ROLc(B, 10) ^ ROLc(B, 18) ^ ROLc(B, 24);
 }
@@ -106,7 +106,7 @@ LTC_INLINE static sm4_u32_t s_sm4_L62(sm4_u32_t B)
 /*
  * defined in section 6.2 Permutation T
  */
-LTC_INLINE static sm4_u32_t s_sm4_T62(sm4_u32_t Z)
+static LTC_INLINE sm4_u32_t s_sm4_T62(sm4_u32_t Z)
 {
     return s_sm4_L62(s_sm4_t(Z));
 }
@@ -137,7 +137,7 @@ static const sm4_u32_t sm4_CK[32] =
 /*
  * defined in section 7.3 (1) L'
  */
-LTC_INLINE static sm4_u32_t s_sm4_L73(sm4_u32_t B)
+static LTC_INLINE sm4_u32_t s_sm4_L73(sm4_u32_t B)
 {
     return B ^ ROLc(B, 13) ^ ROLc(B, 23);
 }
@@ -145,7 +145,7 @@ LTC_INLINE static sm4_u32_t s_sm4_L73(sm4_u32_t B)
 /*
  * defined in section 7.3 (1) T'
  */
-LTC_INLINE static sm4_u32_t s_sm4_T73(sm4_u32_t Z)
+static LTC_INLINE sm4_u32_t s_sm4_T73(sm4_u32_t Z)
 {
     return s_sm4_L73(s_sm4_t(Z));
 }
@@ -153,7 +153,7 @@ LTC_INLINE static sm4_u32_t s_sm4_T73(sm4_u32_t Z)
 /*
  * defined in section 7.3 Key Expansion
  */
-LTC_INLINE static void s_sm4_mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
+static LTC_INLINE void s_sm4_mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
 {
     sm4_u32_t MK[4] = { 0 };
     sm4_u32_t K[4+32] = { 0 };
@@ -175,7 +175,7 @@ LTC_INLINE static void s_sm4_mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
 /*
  * defined in section 6 Round Function F
  */
-LTC_INLINE static sm4_u32_t s_sm4_F(sm4_u32_t X[4], sm4_u32_t rk)
+static LTC_INLINE sm4_u32_t s_sm4_F(sm4_u32_t X[4], sm4_u32_t rk)
 {
     return X[0] ^ s_sm4_T62(X[1] ^ X[2] ^ X[3] ^ rk);
 }
@@ -183,7 +183,7 @@ LTC_INLINE static sm4_u32_t s_sm4_F(sm4_u32_t X[4], sm4_u32_t rk)
 /*
  * defined in section 7.1 (2) The reverse transformation
  */
-LTC_INLINE static void s_sm4_R(sm4_u32_t Y[4], sm4_u32_t X[32+4])
+static LTC_INLINE void s_sm4_R(sm4_u32_t Y[4], sm4_u32_t X[32+4])
 {
     Y[0] = X[35];
     Y[1] = X[34];
@@ -194,7 +194,7 @@ LTC_INLINE static void s_sm4_R(sm4_u32_t Y[4], sm4_u32_t X[32+4])
 /*
  * defined in section 7.1 (En)cryption
  */
-LTC_INLINE static void s_sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_u32_t rk[32])
+static LTC_INLINE void s_sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_u32_t rk[32])
 {
     int i;
 
@@ -203,7 +203,7 @@ LTC_INLINE static void s_sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_
     s_sm4_R(Y, X);
 }
 
-LTC_INLINE static void s_sm4_setkey(struct sm4_key *sm4, const unsigned char *key)
+static LTC_INLINE void s_sm4_setkey(struct sm4_key *sm4, const unsigned char *key)
 {
     int i;
 
@@ -229,7 +229,7 @@ int sm4_setup(const unsigned char *key, int keylen,
 /*
  * SM4 encryption.
  */
-LTC_INLINE static void s_sm4_do(void *output, const void *input, const sm4_u32_t rk[32])
+static LTC_INLINE void s_sm4_do(void *output, const void *input, const sm4_u32_t rk[32])
 {
     sm4_u32_t Y[4];
     sm4_u32_t X[32+4];

+ 1 - 1
src/ciphers/tea.c

@@ -150,7 +150,7 @@ int tea_test(void)
    symmetric_key skey;
    size_t i;
    int err, y;
-   for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
+   for (i = 0; i < LTC_ARRAY_SIZE(tests); i++) {
        zeromem(&skey, sizeof(skey));
 
        l = sizeof(key);

+ 1 - 1
src/ciphers/twofish/twofish.c

@@ -651,7 +651,7 @@ int twofish_test(void)
   unsigned char tmp[2][16];
   int err, i, y;
 
-  for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+  for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
     if ((err = twofish_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
        return err;
     }

+ 1 - 1
src/ciphers/xtea.c

@@ -197,7 +197,7 @@ int xtea_test(void)
    unsigned char tmp[2][8];
    symmetric_key skey;
    int i, err, y;
-   for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+   for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
        zeromem(&skey, sizeof(skey));
        if ((err = xtea_setup(tests[i].key, 16, 0, &skey)) != CRYPT_OK)  {
           return err;

+ 1 - 1
src/encauth/ccm/ccm_test.c

@@ -121,7 +121,7 @@ int ccm_test(void)
       }
    }
 
-   for (x = 0; x < (sizeof(tests)/sizeof(tests[0])); x++) {
+   for (x = 0; x < LTC_ARRAY_SIZE(tests); x++) {
       for (y = 0; y < 2; y++) {
          taglen = tests[x].taglen;
          if (y == 0) {

+ 1 - 1
src/encauth/eax/eax_test.c

@@ -216,7 +216,7 @@ int eax_test(void)
        }
     }
 
-    for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+    for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
         len = sizeof(outtag);
         if ((err = eax_encrypt_authenticate_memory(idx, tests[x].key, tests[x].keylen,
             tests[x].nonce, tests[x].noncelen, tests[x].header, tests[x].headerlen,

+ 1 - 1
src/encauth/gcm/gcm_test.c

@@ -338,7 +338,7 @@ int gcm_test(void)
    if ((err = gcm_done(&gcm, T[0], &y)) != CRYPT_OK)                         return err;
    if (compare_testvector(T[0], y, tests[0].T, 16, "GCM Encrypt Tag-special", 0))      return CRYPT_FAIL_TESTVECTOR;
 
-   for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
        y = sizeof(T[0]);
        if ((err = gcm_memory(idx, tests[x].K, tests[x].keylen,
                              tests[x].IV, tests[x].IVlen,

+ 1 - 1
src/encauth/ocb/ocb_init.c

@@ -53,7 +53,7 @@ int ocb_init(ocb_state *ocb, int cipher,
 
    /* determine which polys to use */
    ocb->block_len = cipher_descriptor[cipher].block_length;
-   x = (int)(sizeof(polys)/sizeof(polys[0]));
+   x = (int)LTC_ARRAY_SIZE(polys);
    for (poly = 0; poly < x; poly++) {
        if (polys[poly].len == ocb->block_len) {
           break;

+ 6 - 0
src/encauth/ocb/ocb_ntz.c

@@ -17,6 +17,11 @@
 */
 int ocb_ntz(unsigned long x)
 {
+#if defined(LTC_HAVE_CTZL_BUILTIN)
+   if (x == 0)
+      return sizeof(unsigned long) * CHAR_BIT;
+   return __builtin_ctzl(x);
+#else
    int c;
    x &= 0xFFFFFFFFUL;
    c = 0;
@@ -25,6 +30,7 @@ int ocb_ntz(unsigned long x)
       x >>= 1;
    }
    return c;
+#endif
 }
 
 #endif

+ 1 - 1
src/encauth/ocb/ocb_test.c

@@ -167,7 +167,7 @@ int ocb_test(void)
        }
     }
 
-    for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+    for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
         len = sizeof(outtag);
         if ((err = ocb_encrypt_authenticate_memory(idx, tests[x].key, 16,
              tests[x].nonce, tests[x].pt, tests[x].ptlen, outct, outtag, &len)) != CRYPT_OK) {

+ 1 - 1
src/encauth/ocb3/ocb3_init.c

@@ -116,7 +116,7 @@ int ocb3_init(ocb3_state *ocb, int cipher,
 
    /* determine which polys to use */
    ocb->block_len = cipher_descriptor[cipher].block_length;
-   x = (int)(sizeof(polys)/sizeof(polys[0]));
+   x = (int)LTC_ARRAY_SIZE(polys);
    for (poly = 0; poly < x; poly++) {
        if (polys[poly].len == ocb->block_len) {
           break;

+ 6 - 0
src/encauth/ocb3/ocb3_int_ntz.c

@@ -16,6 +16,11 @@
 */
 int ocb3_int_ntz(unsigned long x)
 {
+#if defined(LTC_HAVE_CTZL_BUILTIN)
+   if (x == 0)
+      return sizeof(unsigned long) * CHAR_BIT;
+   return __builtin_ctzl(x);
+#else
    int c;
    x &= 0xFFFFFFFFUL;
    c = 0;
@@ -24,6 +29,7 @@ int ocb3_int_ntz(unsigned long x)
       x >>= 1;
    }
    return c;
+#endif
 }
 
 #endif

+ 1 - 1
src/encauth/ocb3/ocb3_test.c

@@ -209,7 +209,7 @@ int ocb3_test(void)
        }
     }
 
-    for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+    for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
         len = 16; /* must be the same as the required taglen */
         if ((err = ocb3_encrypt_authenticate_memory(idx,
                                                    key, sizeof(key),

+ 1 - 1
src/encauth/siv/siv.c

@@ -608,7 +608,7 @@ int siv_test(void)
 
    cipher = find_cipher("aes");
 
-   for (n = 0; n < sizeof(siv_tests)/sizeof(siv_tests[0]); ++n) {
+   for (n = 0; n < LTC_ARRAY_SIZE(siv_tests); ++n) {
       buflen = sizeof(buf);
       if ((err = siv_encrypt_memory(cipher,
                              siv_tests[n].Key, siv_tests[n].Keylen,

+ 1 - 1
src/hashes/chc/chc.c

@@ -277,7 +277,7 @@ int chc_test(void)
    oldhashidx = cipher_idx;
    chc_register(idx);
 
-   for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+   for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
        if ((err = chc_init(&md)) != CRYPT_OK) {
           return err;
        }

+ 1 - 1
src/hashes/rmd128.c

@@ -380,7 +380,7 @@ int rmd128_test(void)
    unsigned char tmp[16];
    hash_state md;
 
-   for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+   for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
        rmd128_init(&md);
        rmd128_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg));
        rmd128_done(&md, tmp);

+ 1 - 1
src/hashes/rmd160.c

@@ -439,7 +439,7 @@ int rmd160_test(void)
    unsigned char tmp[20];
    hash_state md;
 
-   for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+   for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
        rmd160_init(&md);
        rmd160_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg));
        rmd160_done(&md, tmp);

+ 1 - 1
src/hashes/rmd256.c

@@ -405,7 +405,7 @@ int rmd256_test(void)
    unsigned char tmp[32];
    hash_state md;
 
-   for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+   for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
        rmd256_init(&md);
        rmd256_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg));
        rmd256_done(&md, tmp);

+ 1 - 1
src/hashes/rmd320.c

@@ -470,7 +470,7 @@ int rmd320_test(void)
    unsigned char tmp[40];
    hash_state md;
 
-   for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+   for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
        rmd320_init(&md);
        rmd320_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg));
        rmd320_done(&md, tmp);

+ 9 - 9
src/hashes/tiger.c

@@ -566,7 +566,7 @@ static const ulong64 table[4*256] = {
     CONST64(0xC83223F1720AEF96) /* 1022 */, CONST64(0xC3A0396F7363A51F) /* 1023 */};
 
 /* one round of the hash function */
-LTC_INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul)
+static LTC_INLINE void s_tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul)
 {
     ulong64 tmp;
     tmp = (*c ^= x);
@@ -582,14 +582,14 @@ LTC_INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x
 /* one complete pass */
 static void s_pass(ulong64 *a, ulong64 *b, ulong64 *c, const ulong64 *x, int mul)
 {
-   tiger_round(a,b,c,x[0],mul);
-   tiger_round(b,c,a,x[1],mul);
-   tiger_round(c,a,b,x[2],mul);
-   tiger_round(a,b,c,x[3],mul);
-   tiger_round(b,c,a,x[4],mul);
-   tiger_round(c,a,b,x[5],mul);
-   tiger_round(a,b,c,x[6],mul);
-   tiger_round(b,c,a,x[7],mul);
+   s_tiger_round(a,b,c,x[0],mul);
+   s_tiger_round(b,c,a,x[1],mul);
+   s_tiger_round(c,a,b,x[2],mul);
+   s_tiger_round(a,b,c,x[3],mul);
+   s_tiger_round(b,c,a,x[4],mul);
+   s_tiger_round(c,a,b,x[5],mul);
+   s_tiger_round(a,b,c,x[6],mul);
+   s_tiger_round(b,c,a,x[7],mul);
 }
 
 /* The key mixing schedule */

+ 1 - 1
src/hashes/whirl/whirl.c

@@ -281,7 +281,7 @@ int  whirlpool_test(void)
   unsigned char tmp[64];
   hash_state md;
 
-  for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+  for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
       whirlpool_init(&md);
       whirlpool_process(&md, (unsigned char *)tests[i].msg, tests[i].len);
       whirlpool_done(&md, tmp);

+ 8 - 0
src/headers/tomcrypt_cfg.h

@@ -295,6 +295,14 @@ typedef unsigned long ltc_mp_digit;
    #define LTC_HAVE_ROTATE_BUILTIN
 #endif
 
+#if __has_builtin(__builtin_clzl)
+   #define LTC_HAVE_CLZL_BUILTIN
+#endif
+
+#if __has_builtin(__builtin_ctzl)
+   #define LTC_HAVE_CTZL_BUILTIN
+#endif
+
 #if defined(__GNUC__)
    #define LTC_ALIGN(n) __attribute__((aligned(n)))
 #else

+ 0 - 8
src/headers/tomcrypt_pk.h

@@ -844,7 +844,6 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long  nwor
 int der_decode_object_identifier(const unsigned char *in,    unsigned long  inlen,
                                        unsigned long *words, unsigned long *outlen);
 int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen);
-unsigned long der_object_identifier_bits(unsigned long x);
 
 /* IA5 STRING */
 int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
@@ -853,9 +852,6 @@ int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
                                 unsigned char *out, unsigned long *outlen);
 int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
 
-int der_ia5_char_encode(int c);
-int der_ia5_value_decode(int v);
-
 /* TELETEX STRING */
 int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
                                 unsigned char *out, unsigned long *outlen);
@@ -868,9 +864,6 @@ int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
                                 unsigned char *out, unsigned long *outlen);
 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
 
-int der_printable_char_encode(int c);
-int der_printable_value_decode(int v);
-
 /* UTF-8 */
 #if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(__WCHAR_MAX__) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
    #if defined(__WCHAR_MAX__)
@@ -890,7 +883,6 @@ int der_encode_utf8_string(const wchar_t *in,  unsigned long inlen,
 
 int der_decode_utf8_string(const unsigned char *in,  unsigned long inlen,
                                        wchar_t *out, unsigned long *outlen);
-unsigned long der_utf8_charsize(const wchar_t c);
 int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
 
 

+ 15 - 1
src/headers/tomcrypt_private.h

@@ -45,6 +45,8 @@ LTC_STATIC_ASSERT(correct_ltc_uintptr_size, sizeof(ltc_uintptr) == sizeof(void*)
    #define LTC_NULL ((void *)0)
 #endif
 
+#define LTC_ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
+
 /*
  * Internal Enums
  */
@@ -361,6 +363,7 @@ struct get_char {
    } data;
    struct str unget_buf;
    char unget_buf_[LTC_PEM_DECODE_BUFSZ];
+   int prev_get;
 };
 #endif
 
@@ -387,7 +390,7 @@ int pem_decrypt(unsigned char *data, unsigned long *datalen,
 int pem_get_char_from_file(struct get_char *g);
 #endif /* LTC_NO_FILE */
 int pem_get_char_from_buf(struct get_char *g);
-int pem_read(void *pem, unsigned long *w, struct pem_headers *hdr, struct get_char *g);
+int pem_read(void *asn1_cert, unsigned long *asn1_len, struct pem_headers *hdr, struct get_char *g);
 #endif
 
 /* tomcrypt_pk.h */
@@ -585,6 +588,17 @@ int der_length_asn1_length(unsigned long len, unsigned long *outlen);
 int der_length_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
                            unsigned long *outlen, unsigned long *payloadlen);
 
+int der_length_object_identifier_full(const unsigned long *words,  unsigned long  nwords,
+                                            unsigned long *outlen, unsigned long *datalen);
+
+int der_ia5_char_encode(int c);
+int der_ia5_value_decode(int v);
+
+int der_printable_char_encode(int c);
+int der_printable_value_decode(int v);
+
+unsigned long der_utf8_charsize(const wchar_t c);
+
 typedef struct {
    ltc_asn1_type t;
    ltc_asn1_list **pp;

+ 1 - 1
src/mac/f9/f9_test.c

@@ -48,7 +48,7 @@ int f9_test(void)
      return CRYPT_NOP;
   }
 
-  for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+  for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
      taglen = 4;
      if ((err = f9_memory(idx, tests[x].K, 16, tests[x].M, tests[x].msglen, T, &taglen)) != CRYPT_OK) {
         return err;

+ 1 - 1
src/mac/omac/omac_test.c

@@ -76,7 +76,7 @@ int omac_test(void)
        }
     }
 
-    for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+    for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
        len = sizeof(out);
        if ((err = omac_memory(idx, tests[x].key, tests[x].keylen, tests[x].msg, tests[x].msglen, out, &len)) != CRYPT_OK) {
           return err;

+ 1 - 1
src/mac/pelican/pelican_test.c

@@ -80,7 +80,7 @@ int pelican_test(void)
    unsigned char out[16];
    pelican_state pel;
 
-   for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
        if ((err = pelican_init(&pel, tests[x].K, tests[x].keylen)) != CRYPT_OK) {
           return err;
        }

+ 2 - 2
src/mac/pmac/pmac_init.c

@@ -51,12 +51,12 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l
 
    /* determine which polys to use */
    pmac->block_len = cipher_descriptor[cipher].block_length;
-   for (poly = 0; poly < (int)(sizeof(polys)/sizeof(polys[0])); poly++) {
+   for (poly = 0; poly < (int)LTC_ARRAY_SIZE(polys); poly++) {
        if (polys[poly].len == pmac->block_len) {
           break;
        }
    }
-   if (poly >= (int)(sizeof(polys)/sizeof(polys[0]))) {
+   if (poly >= (int)LTC_ARRAY_SIZE(polys)) {
       return CRYPT_INVALID_ARG;
     }
    if (polys[poly].len != pmac->block_len) {

+ 6 - 0
src/mac/pmac/pmac_ntz.c

@@ -14,6 +14,11 @@
 */
 int pmac_ntz(unsigned long x)
 {
+#if defined(LTC_HAVE_CTZL_BUILTIN)
+   if (x == 0)
+      return sizeof(unsigned long) * CHAR_BIT;
+   return __builtin_ctzl(x);
+#else
    int c;
    x &= 0xFFFFFFFFUL;
    c = 0;
@@ -22,6 +27,7 @@ int pmac_ntz(unsigned long x)
       x >>= 1;
    }
    return c;
+#endif
 }
 
 #endif

+ 1 - 1
src/mac/pmac/pmac_test.c

@@ -124,7 +124,7 @@ int pmac_test(void)
        }
     }
 
-    for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+    for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
         len = sizeof(outtag);
         if ((err = pmac_memory(idx, tests[x].key, 16, tests[x].msg, tests[x].msglen, outtag, &len)) != CRYPT_OK) {
            return err;

+ 1 - 1
src/mac/xcbc/xcbc_test.c

@@ -98,7 +98,7 @@ int xcbc_test(void)
      }
   }
 
-  for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+  for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
      taglen = 16;
      if ((err = xcbc_memory(idx, tests[x].K, 16, tests[x].M, tests[x].msglen, T, &taglen)) != CRYPT_OK) {
         return err;

+ 1 - 1
src/math/ltm_desc.c

@@ -33,7 +33,7 @@ static int mpi_to_ltc_error(mp_err err)
 {
    size_t x;
 
-   for (x = 0; x < sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0]); x++) {
+   for (x = 0; x < LTC_ARRAY_SIZE(mpi_to_ltc_codes); x++) {
        if (err == mpi_to_ltc_codes[x].mpi_code) {
           return mpi_to_ltc_codes[x].ltc_code;
        }

+ 1 - 1
src/math/tfm_desc.c

@@ -39,7 +39,7 @@ static int tfm_to_ltc_error(int err)
 {
    int x;
 
-   for (x = 0; x < (int)(sizeof(tfm_to_ltc_codes)/sizeof(tfm_to_ltc_codes[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(tfm_to_ltc_codes); x++) {
        if (err == tfm_to_ltc_codes[x].tfm_code) {
           return tfm_to_ltc_codes[x].ltc_code;
        }

+ 4 - 3
src/misc/base16/base16_encode.c

@@ -52,10 +52,11 @@ int base16_encode(const unsigned char *in,  unsigned long  inlen,
       alphabet = alphabets[1];
    }
 
-   for (i = 0; i < x; i += 2) {
-      out[i]   = alphabet[(in[i/2] >> 4) & 0x0f];
-      out[i+1] = alphabet[in[i/2] & 0x0f];
+   for (i = x; i > 0; i -= 2) {
+      out[i-2] = alphabet[(in[(i-1)/2] >> 4) & 0x0f];
+      out[i-1] = alphabet[in[(i-1)/2] & 0x0f];
    }
+
    out[x] = '\0';
 
    return CRYPT_OK;

+ 9 - 0
src/misc/crypt/crypt.c

@@ -548,6 +548,15 @@ const char *crypt_build_settings =
     " LTC_NO_ROLC "
 #endif
 #endif
+#if defined(LTC_HAVE_ROTATE_BUILTIN)
+    " LTC_HAVE_ROTATE_BUILTIN "
+#endif
+#if defined(LTC_HAVE_CLZL_BUILTIN)
+    " LTC_HAVE_CLZL_BUILTIN "
+#endif
+#if defined(LTC_HAVE_CTZL_BUILTIN)
+    " LTC_HAVE_CTZL_BUILTIN "
+#endif
 #if defined(LTC_NO_TEST)
     " LTC_NO_TEST "
 #endif

+ 7 - 2
src/misc/crypt/crypt_register_all_hashes.c

@@ -89,8 +89,13 @@ int register_all_hashes(void)
    REGISTER_HASH(&blake2b_512_desc);
 #endif
 #ifdef LTC_CHC_HASH
-   REGISTER_HASH(&chc_desc);
-   LTC_ARGCHK(chc_register(find_cipher_any("aes", 8, 16)) == CRYPT_OK);
+   {
+      int aes_index = find_cipher_any("aes", 8, 16);
+      if (aes_index != -1) {
+         REGISTER_HASH(&chc_desc);
+         LTC_ARGCHK(chc_register(aes_index) == CRYPT_OK);
+      }
+   }
 #endif
    return CRYPT_OK;
 }

+ 1 - 1
src/misc/error_to_string.c

@@ -54,7 +54,7 @@ static const char * const err_2_str[CRYPT_ERR_NUM] =
    "The PEM header was not recognized",
 };
 
-LTC_STATIC_ASSERT(correct_err_2_str_size, (sizeof(err_2_str)/sizeof(err_2_str[0])) == CRYPT_ERR_NUM)
+LTC_STATIC_ASSERT(correct_err_2_str_size, LTC_ARRAY_SIZE(err_2_str) == CRYPT_ERR_NUM)
 
 /**
    Convert an LTC error code to ASCII

+ 2 - 2
src/misc/pbes/pbes2.c

@@ -51,7 +51,7 @@ static const oid_to_pbes s_pbes2_list[] = {
 static int s_pbes2_from_oid(const ltc_asn1_list *cipher_oid, const ltc_asn1_list *hmac_oid, pbes_properties *res)
 {
    unsigned int i;
-   for (i = 0; i < sizeof(s_pbes2_list)/sizeof(s_pbes2_list[0]); ++i) {
+   for (i = 0; i < LTC_ARRAY_SIZE(s_pbes2_list); ++i) {
       if (pk_oid_cmp_with_asn1(s_pbes2_list[i].oid, cipher_oid) == CRYPT_OK) {
          *res = *s_pbes2_list[i].data;
          break;
@@ -59,7 +59,7 @@ static int s_pbes2_from_oid(const ltc_asn1_list *cipher_oid, const ltc_asn1_list
    }
    if (res->c == NULL) return CRYPT_INVALID_CIPHER;
    if (hmac_oid != NULL) {
-      for (i = 0; i < sizeof(s_hmac_oid_names)/sizeof(s_hmac_oid_names[0]); ++i) {
+      for (i = 0; i < LTC_ARRAY_SIZE(s_hmac_oid_names); ++i) {
          if (pk_oid_cmp_with_asn1(s_hmac_oid_names[i].oid, hmac_oid) == CRYPT_OK) {
             res->h = s_hmac_oid_names[i].id;
             return CRYPT_OK;

+ 2 - 2
src/misc/pem/pem.c

@@ -65,7 +65,7 @@ const struct pem_header_id pem_std_headers[] = {
      .pka = LTC_PKA_DSA,
    },
 };
-const unsigned long pem_std_headers_num = sizeof(pem_std_headers)/sizeof(pem_std_headers[0]);
+const unsigned long pem_std_headers_num = LTC_ARRAY_SIZE(pem_std_headers);
 
 /* Encrypted PEM files */
 const struct str pem_proc_type_encrypted = { SET_CSTR(, "Proc-Type: 4,ENCRYPTED") };
@@ -147,7 +147,7 @@ const struct blockcipher_info pem_dek_infos[] =
       { .name = "SEED-CFB,",          .algo = "seed",     .keylen = 128 / 8, .mode = cm_cfb,    },
       { .name = "SEED-OFB,",          .algo = "seed",     .keylen = 128 / 8, .mode = cm_ofb,    },
    };
-const unsigned long pem_dek_infos_num = sizeof(pem_dek_infos)/sizeof(pem_dek_infos[0]);
+const unsigned long pem_dek_infos_num = LTC_ARRAY_SIZE(pem_dek_infos);
 
 int pem_decrypt(unsigned char *data, unsigned long *datalen,
                 unsigned char *key,  unsigned long keylen,

+ 22 - 22
src/misc/pem/pem_pkcs.c

@@ -12,7 +12,7 @@
 extern const struct pem_header_id pem_std_headers[];
 extern const unsigned long pem_std_headers_num;
 
-static int s_decrypt_pem(unsigned char *pem, unsigned long *l, const struct pem_headers *hdr)
+static int s_decrypt_pem(unsigned char *asn1_cert, unsigned long *asn1_len, const struct pem_headers *hdr)
 {
    unsigned char iv[MAXBLOCKSIZE], key[MAXBLOCKSIZE];
    unsigned long ivlen, klen;
@@ -34,7 +34,7 @@ static int s_decrypt_pem(unsigned char *pem, unsigned long *l, const struct pem_
       return err;
    }
 
-   err = pem_decrypt(pem, l, key, klen, iv, ivlen, NULL, 0, &hdr->info, LTC_PAD_PKCS7);
+   err = pem_decrypt(asn1_cert, asn1_len, key, klen, iv, ivlen, NULL, 0, &hdr->info, LTC_PAD_PKCS7);
 
    zeromem(key, sizeof(key));
    zeromem(iv, sizeof(iv));
@@ -82,12 +82,12 @@ static const import_fn s_import_x509_fns[LTC_PKA_NUM] = {
 #endif
 };
 
-static int s_import_x509(unsigned char *pem, unsigned long l, ltc_pka_key *k)
+static int s_import_x509(unsigned char *asn1_cert, unsigned long asn1_len, ltc_pka_key *k)
 {
    enum ltc_pka_id pka = LTC_PKA_UNDEF;
    ltc_asn1_list *d, *spki;
    int err;
-   if ((err = x509_decode_spki(pem, l, &d, &spki)) != CRYPT_OK) {
+   if ((err = x509_decode_spki(asn1_cert, asn1_len, &d, &spki)) != CRYPT_OK) {
       return err;
    }
    err = s_get_pka(spki, &pka);
@@ -96,23 +96,23 @@ static int s_import_x509(unsigned char *pem, unsigned long l, ltc_pka_key *k)
       return err;
    }
    if (pka < 0
-         || pka > sizeof(s_import_x509_fns)/sizeof(s_import_x509_fns[0])
+         || pka > LTC_ARRAY_SIZE(s_import_x509_fns)
          || s_import_x509_fns[pka] == NULL) {
       return CRYPT_PK_INVALID_TYPE;
    }
-   if ((err = s_import_x509_fns[pka](pem, l, &k->u)) == CRYPT_OK) {
+   if ((err = s_import_x509_fns[pka](asn1_cert, asn1_len, &k->u)) == CRYPT_OK) {
       k->id = pka;
    }
    return err;
 }
 
-static int s_import_pkcs8(unsigned char *pem, unsigned long l, ltc_pka_key *k, const password_ctx *pw_ctx)
+static int s_import_pkcs8(unsigned char *asn1_cert, unsigned long asn1_len, ltc_pka_key *k, const password_ctx *pw_ctx)
 {
    int err;
    enum ltc_oid_id pka;
    ltc_asn1_list *alg_id, *priv_key;
    ltc_asn1_list *p8_asn1 = NULL;
-   if ((err = pkcs8_decode_flexi(pem, l, pw_ctx, &p8_asn1)) != CRYPT_OK) {
+   if ((err = pkcs8_decode_flexi(asn1_cert, asn1_len, pw_ctx, &p8_asn1)) != CRYPT_OK) {
       goto cleanup;
    }
    if ((err = pkcs8_get_children(p8_asn1, &pka, &alg_id, &priv_key)) != CRYPT_OK) {
@@ -164,11 +164,11 @@ cleanup:
    return err;
 }
 
-static int s_extract_pka(unsigned char *pem, unsigned long w, enum ltc_pka_id *pka)
+static int s_extract_pka(unsigned char *asn1_cert, unsigned long asn1_len, enum ltc_pka_id *pka)
 {
    ltc_asn1_list *pub;
    int err = CRYPT_ERROR;
-   if ((err = der_decode_sequence_flexi(pem, &w, &pub)) != CRYPT_OK) {
+   if ((err = der_decode_sequence_flexi(asn1_cert, &asn1_len, &pub)) != CRYPT_OK) {
       return err;
    }
    err = s_get_pka(pub, pka);
@@ -194,8 +194,8 @@ static const import_fn s_import_openssl_fns[LTC_PKA_NUM] = {
 
 static int s_decode(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_ctx)
 {
-   unsigned char *pem = NULL;
-   unsigned long w, l, n;
+   unsigned char *asn1_cert = NULL;
+   unsigned long w, asn1_len, n;
    int err = CRYPT_ERROR;
    struct pem_headers hdr = { 0 };
    struct password pw = { 0 };
@@ -203,10 +203,10 @@ static int s_decode(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_c
    XMEMSET(k, 0, sizeof(*k));
    w = LTC_PEM_READ_BUFSIZE * 2;
 retry:
-   pem = XREALLOC(pem, w);
+   asn1_cert = XREALLOC(asn1_cert, w);
    for (n = 0; n < pem_std_headers_num; ++n) {
       hdr.id = &pem_std_headers[n];
-      err = pem_read(pem, &w, &hdr, g);
+      err = pem_read(asn1_cert, &w, &hdr, g);
       if (err == CRYPT_BUFFER_OVERFLOW) {
          goto retry;
       } else if (err == CRYPT_OK) {
@@ -219,15 +219,15 @@ retry:
    /* id not found */
    if (hdr.id == NULL)
       goto cleanup;
-   l = w;
+   asn1_len = w;
    if (hdr.id->flags & pf_pkcs8) {
-      err = s_import_pkcs8(pem, l, k, pw_ctx);
+      err = s_import_pkcs8(asn1_cert, asn1_len, k, pw_ctx);
       goto cleanup;
    } else if (hdr.id->flags == pf_x509) {
-      err = s_import_x509(pem, l, k);
+      err = s_import_x509(asn1_cert, asn1_len, k);
       goto cleanup;
    } else if ((hdr.id->flags & pf_public) && hdr.id->pka == LTC_PKA_UNDEF) {
-      if ((err = s_extract_pka(pem, w, &pka)) != CRYPT_OK) {
+      if ((err = s_extract_pka(asn1_cert, asn1_len, &pka)) != CRYPT_OK) {
          goto cleanup;
       }
    } else if (hdr.encrypted) {
@@ -242,7 +242,7 @@ retry:
          goto cleanup;
       }
 
-      if ((err = s_decrypt_pem(pem, &l, &hdr)) != CRYPT_OK) {
+      if ((err = s_decrypt_pem(asn1_cert, &asn1_len, &hdr)) != CRYPT_OK) {
          goto cleanup;
       }
       pka = hdr.id->pka;
@@ -251,18 +251,18 @@ retry:
    }
 
    if (pka < 0
-         || pka > sizeof(s_import_openssl_fns)/sizeof(s_import_openssl_fns[0])
+         || pka > LTC_ARRAY_SIZE(s_import_openssl_fns)
          || s_import_openssl_fns[pka] == NULL) {
       err = CRYPT_PK_INVALID_TYPE;
       goto cleanup;
    }
-   if ((err = s_import_openssl_fns[pka](pem, l, &k->u)) == CRYPT_OK) {
+   if ((err = s_import_openssl_fns[pka](asn1_cert, asn1_len, &k->u)) == CRYPT_OK) {
       k->id = pka;
    }
 
 cleanup:
    password_free(hdr.pw, pw_ctx);
-   XFREE(pem);
+   XFREE(asn1_cert);
    return err;
 }
 

+ 50 - 27
src/misc/pem/pem_read.c

@@ -62,10 +62,10 @@ static void s_tts(char *buf, unsigned long *buflen)
    }
 }
 
-static char* s_get_line(char *buf, unsigned long *buflen, struct get_char *g)
+static char* s_get_line_i(char *buf, unsigned long *buflen, struct get_char *g, int search_for_start)
 {
-   unsigned long blen = 0;
-   int c = -1, c_;
+   unsigned long blen = 0, wr = 0;
+   int c_;
    if (g->unget_buf.p) {
       if (*buflen < g->unget_buf.len) {
          return NULL;
@@ -75,30 +75,44 @@ static char* s_get_line(char *buf, unsigned long *buflen, struct get_char *g)
       RESET_STR(g->unget_buf);
       return buf;
    }
-   while(blen < *buflen) {
-      c_ = c;
-      c = g->get(g);
-      if (c == '\n') {
-         buf[blen] = '\0';
+   if (g->prev_get == -1) {
+      return NULL;
+   }
+   while(blen < *buflen || search_for_start) {
+      wr = blen < *buflen ? blen : *buflen - 1;
+      c_ = g->prev_get;
+      g->prev_get = g->get(g);
+      if (g->prev_get == '\n') {
+         buf[wr] = '\0';
          if (c_ == '\r') {
-            buf[--blen] = '\0';
+            buf[--wr] = '\0';
          }
-         s_tts(buf, &blen);
-         *buflen = blen;
+         s_tts(buf, &wr);
+         *buflen = wr;
          return buf;
       }
-      if (c == -1 || c == '\0') {
-         buf[blen] = '\0';
-         s_tts(buf, &blen);
-         *buflen = blen;
+      if (g->prev_get == -1 || g->prev_get == '\0') {
+         buf[wr] = '\0';
+         s_tts(buf, &wr);
+         *buflen = wr;
          return buf;
       }
-      buf[blen] = c;
+      buf[wr] = g->prev_get;
       blen++;
    }
    return NULL;
 }
 
+LTC_INLINE static char* s_get_first_line(char *buf, unsigned long *buflen, struct get_char *g)
+{
+   return s_get_line_i(buf, buflen, g, 1);
+}
+
+LTC_INLINE static char* s_get_line(char *buf, unsigned long *buflen, struct get_char *g)
+{
+   return s_get_line_i(buf, buflen, g, 0);
+}
+
 static LTC_INLINE int s_fits_buf(void *dest, unsigned long to_write, void *end)
 {
    unsigned char *d = dest;
@@ -176,20 +190,29 @@ static int s_pem_decode_headers(struct pem_headers *hdr, struct get_char *g)
    return CRYPT_OK;
 }
 
-int pem_read(void *pem, unsigned long *w, struct pem_headers *hdr, struct get_char *g)
+int pem_read(void *asn1_cert, unsigned long *asn1_len, struct pem_headers *hdr, struct get_char *g)
 {
    char buf[LTC_PEM_DECODE_BUFSZ];
-   char *wpem = pem;
-   char *end = wpem + *w;
+   char *wpem = asn1_cert;
+   char *end = wpem + *asn1_len;
+   const char pem_start[] = "----";
    unsigned long slen, linelen;
    int err, hdr_ok = 0;
    int would_overflow = 0;
    unsigned char empty_lines = 0;
 
-   linelen = sizeof(buf);
-   if (s_get_line(buf, &linelen, g) == NULL) {
-      return CRYPT_INVALID_PACKET;
-   }
+   g->prev_get = 0;
+   do {
+      linelen = sizeof(buf);
+      if (s_get_first_line(buf, &linelen, g) == NULL) {
+         if (g->prev_get == -1)
+            return CRYPT_NOP;
+         else
+            return CRYPT_INVALID_PACKET;
+      }
+      if (linelen < sizeof(pem_start) - 1)
+         continue;
+   } while(XMEMCMP(buf, pem_start, sizeof(pem_start) - 1) != 0);
    if (hdr->id->start.len != linelen || XMEMCMP(buf, hdr->id->start.p, hdr->id->start.len)) {
       s_unget_line(buf, linelen, g);
       return CRYPT_UNKNOWN_PEM;
@@ -226,16 +249,16 @@ int pem_read(void *pem, unsigned long *w, struct pem_headers *hdr, struct get_ch
       /* NUL termination */
       wpem++;
       /* prevent a wrap-around */
-      if (wpem < (char*)pem)
+      if (wpem < (char*)asn1_cert)
          return CRYPT_OVERFLOW;
-      *w = wpem - (char*)pem;
+      *asn1_len = wpem - (char*)asn1_cert;
       return CRYPT_BUFFER_OVERFLOW;
    }
 
-   *w = wpem - (char*)pem;
+   *asn1_len = wpem - (char*)asn1_cert;
    *wpem++ = '\0';
 
-   if ((err = base64_strict_decode(pem, *w, pem, w)) != CRYPT_OK) {
+   if ((err = base64_strict_decode(asn1_cert, *asn1_len, asn1_cert, asn1_len)) != CRYPT_OK) {
       return err;
    }
    return CRYPT_OK;

+ 6 - 6
src/misc/pem/pem_ssh.c

@@ -47,7 +47,7 @@ const struct blockcipher_info ssh_ciphers[] =
    { .name = "twofish256-cbc",                .algo = "twofish",  .keylen = 256 / 8, .mode = cm_cbc                 },
    { .name = "twofish256-ctr",                .algo = "twofish",  .keylen = 256 / 8, .mode = cm_ctr                 },
 };
-const unsigned long ssh_ciphers_num = sizeof(ssh_ciphers)/sizeof(ssh_ciphers[0]);
+const unsigned long ssh_ciphers_num = LTC_ARRAY_SIZE(ssh_ciphers);
 
 struct kdf_options {
    const char *name;
@@ -398,7 +398,7 @@ static int s_decode_key(const unsigned char *in, unsigned long *inlen, ltc_pka_k
    remaining -= cur_len;
    cur_len = remaining;
 
-   for (n = 0; n < sizeof(ssh_pkas)/sizeof(ssh_pkas[0]); ++n) {
+   for (n = 0; n < LTC_ARRAY_SIZE(ssh_pkas); ++n) {
       if (ssh_pkas[n].name.p != NULL) {
          if (pkalen != ssh_pkas[n].name.len
                || XMEMCMP(pka, ssh_pkas[n].name.p, ssh_pkas[n].name.len) != 0) continue;
@@ -411,7 +411,7 @@ static int s_decode_key(const unsigned char *in, unsigned long *inlen, ltc_pka_k
       }
       break;
    }
-   if (n == sizeof(ssh_pkas)/sizeof(ssh_pkas[0])) {
+   if (n == LTC_ARRAY_SIZE(ssh_pkas)) {
       return CRYPT_PK_INVALID_TYPE;
    }
 
@@ -486,7 +486,7 @@ static int s_parse_line(char *line, unsigned long *len, ltc_pka_key *key, char *
 
    rlen = *len;
    /* Chop up string into the three authorized_keys_elements */
-   for (n = 0; n < sizeof(elements)/sizeof(elements[0]) && rlen; ++n) {
+   for (n = 0; n < LTC_ARRAY_SIZE(elements) && rlen; ++n) {
       skip_spaces(&r, &rlen);
       elements[n].p = r;
       if (n != 2)
@@ -498,7 +498,7 @@ static int s_parse_line(char *line, unsigned long *len, ltc_pka_key *key, char *
       r++;
    }
 
-   for (n = 0; n < sizeof(ssh_pkas)/sizeof(ssh_pkas[0]); ++n) {
+   for (n = 0; n < LTC_ARRAY_SIZE(ssh_pkas); ++n) {
       if (ssh_pkas[n].name.p != NULL) {
          if (elements[ake_algo_name].len != ssh_pkas[n].name.len
                || XMEMCMP(elements[ake_algo_name].p, ssh_pkas[n].name.p, ssh_pkas[n].name.len) != 0) continue;
@@ -707,7 +707,7 @@ static const struct pem_header_id pem_openssh[] = {
      .flags = pf_public
    },
 };
-static const unsigned long pem_openssh_num = sizeof(pem_openssh)/sizeof(pem_openssh[0]);
+static const unsigned long pem_openssh_num = LTC_ARRAY_SIZE(pem_openssh);
 
 static int s_decode_openssh(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_ctx)
 {

+ 1 - 1
src/modes/ctr/ctr_test.c

@@ -51,7 +51,7 @@ int ctr_test(void)
      }
   }
 
-  for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+  for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
      if ((err = ctr_start(idx, tests[x].IV, tests[x].key, tests[x].keylen, 0, CTR_COUNTER_BIG_ENDIAN|LTC_CTR_RFC3686, &ctr)) != CRYPT_OK) {
         return err;
      }

+ 1 - 1
src/modes/lrw/lrw_test.c

@@ -73,7 +73,7 @@ int lrw_test(void)
      }
   }
 
-  for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
+  for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
      /* schedule it */
      if ((err = lrw_start(idx, tests[x].IV, tests[x].key, 16, tests[x].tweak, 0, &lrw)) != CRYPT_OK) {
         return err;

+ 5 - 5
src/pk/asn1/der/general/der_asn1_maps.c

@@ -39,7 +39,7 @@ const int der_asn1_type_to_identifier_map[] =
  24, /* LTC_ASN1_GENERALIZEDTIME, */
  -1, /* LTC_ASN1_CUSTOM_TYPE, */
 };
-const unsigned long der_asn1_type_to_identifier_map_sz = sizeof(der_asn1_type_to_identifier_map)/sizeof(der_asn1_type_to_identifier_map[0]);
+const unsigned long der_asn1_type_to_identifier_map_sz = LTC_ARRAY_SIZE(der_asn1_type_to_identifier_map);
 
 /**
   A Map from the ASN.1 Class to its string
@@ -51,7 +51,7 @@ const char* der_asn1_class_to_string_map[] =
   "CONTEXT-SPECIFIC",
   "PRIVATE",
 };
-const unsigned long der_asn1_class_to_string_map_sz = sizeof(der_asn1_class_to_string_map)/sizeof(der_asn1_class_to_string_map[0]);
+const unsigned long der_asn1_class_to_string_map_sz = LTC_ARRAY_SIZE(der_asn1_class_to_string_map);
 
 /**
   A Map from the ASN.1 P/C-bit to its string
@@ -61,7 +61,7 @@ const char* der_asn1_pc_to_string_map[] =
   "PRIMITIVE",
   "CONSTRUCTED",
 };
-const unsigned long der_asn1_pc_to_string_map_sz = sizeof(der_asn1_pc_to_string_map)/sizeof(der_asn1_pc_to_string_map[0]);
+const unsigned long der_asn1_pc_to_string_map_sz = LTC_ARRAY_SIZE(der_asn1_pc_to_string_map);
 
 /**
   A Map from the ASN.1 tag to its string
@@ -106,7 +106,7 @@ const char* der_asn1_tag_to_string_map[] =
   "OID internationalized resource identifier type",
   "Relative OID internationalized resource identifier type",
 };
-const unsigned long der_asn1_tag_to_string_map_sz = sizeof(der_asn1_tag_to_string_map)/sizeof(der_asn1_tag_to_string_map[0]);
+const unsigned long der_asn1_tag_to_string_map_sz = LTC_ARRAY_SIZE(der_asn1_tag_to_string_map);
 
 /**
   A Map from ASN.1 Tags to ltc_asn1_type
@@ -152,6 +152,6 @@ const ltc_asn1_type der_asn1_tag_to_type_map[] =
   /* 30 */
   LTC_ASN1_CUSTOM_TYPE,      /* BMPString types */
 };
-const unsigned long der_asn1_tag_to_type_map_sz = sizeof(der_asn1_tag_to_type_map)/sizeof(der_asn1_tag_to_type_map[0]);
+const unsigned long der_asn1_tag_to_type_map_sz = LTC_ARRAY_SIZE(der_asn1_tag_to_type_map);
 
 #endif

+ 1 - 1
src/pk/asn1/der/general/der_decode_asn1_identifier.c

@@ -47,7 +47,7 @@ static const unsigned char tag_constructed_map[] =
  LTC_ASN1_PC_PRIMITIVE,
  LTC_ASN1_PC_PRIMITIVE,
 };
- static const unsigned long tag_constructed_map_sz = sizeof(tag_constructed_map)/sizeof(tag_constructed_map[0]);
+ static const unsigned long tag_constructed_map_sz = LTC_ARRAY_SIZE(tag_constructed_map);
 
 /**
   Decode the ASN.1 Identifier

+ 2 - 2
src/pk/asn1/der/ia5/der_length_ia5_string.c

@@ -119,7 +119,7 @@ static const struct {
 int der_ia5_char_encode(int c)
 {
    int x;
-   for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(ia5_table); x++) {
        if (ia5_table[x].code == c) {
           return ia5_table[x].value;
        }
@@ -130,7 +130,7 @@ int der_ia5_char_encode(int c)
 int der_ia5_value_decode(int v)
 {
    int x;
-   for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(ia5_table); x++) {
        if (ia5_table[x].value == v) {
           return ia5_table[x].code;
        }

+ 2 - 13
src/pk/asn1/der/object_identifier/der_encode_object_identifier.c

@@ -27,7 +27,7 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long  nwor
    LTC_ARGCHK(outlen != NULL);
 
    /* check length */
-   if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) {
+   if ((err = der_length_object_identifier_full(words, nwords, &x, &z)) != CRYPT_OK) {
       return err;
    }
    if (x > *outlen) {
@@ -35,17 +35,6 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long  nwor
       return CRYPT_BUFFER_OVERFLOW;
    }
 
-   /* compute length to store OID data */
-   z = 0;
-   wordbuf = words[0] * 40 + words[1];
-   for (y = 1; y < nwords; y++) {
-       t = der_object_identifier_bits(wordbuf);
-       z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
-       if (y < nwords - 1) {
-          wordbuf = words[y + 1];
-       }
-   }
-
    /* store header + length */
    x = 0;
    out[x++] = 0x06;
@@ -59,7 +48,7 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long  nwor
    wordbuf = words[0] * 40 + words[1];
    for (i = 1; i < nwords; i++) {
       /* store 7 bit words in little endian */
-      t    = wordbuf & 0xFFFFFFFF;
+      t    = wordbuf;
       if (t) {
          y    = x;
          mask = 0;

+ 24 - 12
src/pk/asn1/der/object_identifier/der_length_object_identifier.c

@@ -9,27 +9,24 @@
 
 #ifdef LTC_DER
 
-unsigned long der_object_identifier_bits(unsigned long x)
+static LTC_INLINE unsigned long s_der_object_identifier_bits(unsigned long x)
 {
+#if defined(LTC_HAVE_CLZL_BUILTIN)
+   if (x == 0)
+      return 0;
+   return sizeof(unsigned long) * CHAR_BIT - __builtin_clzl(x);
+#else
    unsigned long c;
-   x &= 0xFFFFFFFF;
    c  = 0;
    while (x) {
      ++c;
      x >>= 1;
    }
    return c;
+#endif
 }
 
-
-/**
-  Gets length of DER encoding of Object Identifier
-  @param nwords   The number of OID words
-  @param words    The actual OID words to get the size of
-  @param outlen   [out] The length of the DER encoding for the given string
-  @return CRYPT_OK if successful
-*/
-int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen)
+int der_length_object_identifier_full(const unsigned long *words, unsigned long nwords, unsigned long *outlen, unsigned long *datalen)
 {
    unsigned long y, z, t, wordbuf;
 
@@ -51,7 +48,7 @@ int der_length_object_identifier(const unsigned long *words, unsigned long nword
    z = 0;
    wordbuf = words[0] * 40 + words[1];
    for (y = 1; y < nwords; y++) {
-       t = der_object_identifier_bits(wordbuf);
+       t = s_der_object_identifier_bits(wordbuf);
        z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
        if (y < nwords - 1) {
           /* grab next word */
@@ -59,6 +56,9 @@ int der_length_object_identifier(const unsigned long *words, unsigned long nword
        }
    }
 
+   if (datalen) {
+      *datalen = z;
+   }
    /* now depending on the length our length encoding changes */
    if (z < 128) {
       z += 2;
@@ -74,4 +74,16 @@ int der_length_object_identifier(const unsigned long *words, unsigned long nword
    return CRYPT_OK;
 }
 
+/**
+  Gets length of DER encoding of Object Identifier
+  @param nwords   The number of OID words
+  @param words    The actual OID words to get the size of
+  @param outlen   [out] The length of the DER encoding for the given string
+  @return CRYPT_OK if successful
+*/
+int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen)
+{
+   return der_length_object_identifier_full(words, nwords, outlen, NULL);
+}
+
 #endif

+ 2 - 2
src/pk/asn1/der/printable_string/der_length_printable_string.c

@@ -91,7 +91,7 @@ static const struct {
 int der_printable_char_encode(int c)
 {
    int x;
-   for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(printable_table); x++) {
        if (printable_table[x].code == c) {
           return printable_table[x].value;
        }
@@ -102,7 +102,7 @@ int der_printable_char_encode(int c)
 int der_printable_value_decode(int v)
 {
    int x;
-   for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(printable_table); x++) {
        if (printable_table[x].value == v) {
           return printable_table[x].code;
        }

+ 22 - 16
src/pk/asn1/der/sequence/der_decode_sequence_flexi.c

@@ -27,6 +27,22 @@ static int s_new_element(ltc_asn1_list **l)
    }
    return CRYPT_OK;
 }
+#if defined(LTC_TEST_DBG)
+void s_print_err(const char *errstr, ltc_asn1_list *l, int err, unsigned long identifier, unsigned long data_offset, unsigned long len)
+{
+#if LTC_TEST_DBG <= 1
+   if (err == CRYPT_OK)
+      return;
+#endif
+   if (l->type == LTC_ASN1_CUSTOM_TYPE) {
+      fprintf(stderr, "%s %02lx: hl=%4lu l=%4lu - %s[%s %llu] (%s)\n", errstr, identifier, data_offset, len, der_asn1_class_to_string_map[l->klass], der_asn1_pc_to_string_map[l->pc], l->tag, error_to_string(err));
+   } else {
+      fprintf(stderr, "%s %02lx: hl=%4lu l=%4lu - %s (%s)\n", errstr, identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag], error_to_string(err));
+   }
+}
+#else
+#define s_print_err(errstr, l, err, identifier, data_offset, len) LTC_UNUSED_PARAM(data_offset)
+#endif
 
 /**
    ASN.1 DER Flexi(ble) decoder will decode arbitrary DER packets and create a linked list of the decoded elements.
@@ -39,7 +55,8 @@ static int s_new_element(ltc_asn1_list **l)
 static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out, unsigned long depth)
 {
    ltc_asn1_list *l;
-   unsigned long err, identifier, len, totlen, data_offset, id_len, len_len;
+   int err;
+   unsigned long identifier, len, totlen, data_offset, id_len, len_len;
    void          *realloc_tmp;
 
    LTC_ARGCHK(in    != NULL);
@@ -73,30 +90,19 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i
       if (l->type != LTC_ASN1_EOL) {
          /* fetch length */
          len_len = *inlen - id_len;
-#if defined(LTC_TEST_DBG)
+         /* init with dummy values for error cases */
          data_offset = 666;
          len = 0;
-#endif
          if ((err = der_decode_asn1_length(&in[id_len], &len_len, &len)) != CRYPT_OK) {
-#if defined(LTC_TEST_DBG)
-            fprintf(stderr, "E1 %02lx: hl=%4lu l=%4lu - %s (%s)\n", identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag], error_to_string(err));
-#endif
+            s_print_err("E1", l, err, identifier, data_offset, len);
             goto error;
          } else if (len > (*inlen - id_len - len_len)) {
             err = CRYPT_INVALID_PACKET;
-#if defined(LTC_TEST_DBG)
-            fprintf(stderr, "E2 %02lx: hl=%4lu l=%4lu - %s (%s)\n", identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag], error_to_string(err));
-#endif
+            s_print_err("E2", l, err, identifier, data_offset, len);
             goto error;
          }
          data_offset = id_len + len_len;
-#if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
-         if (l->type == LTC_ASN1_CUSTOM_TYPE && l->klass == LTC_ASN1_CL_CONTEXT_SPECIFIC) {
-            fprintf(stderr, "OK %02lx: hl=%4lu l=%4lu - Context Specific[%s %llu]\n", identifier, data_offset, len, der_asn1_pc_to_string_map[l->pc], l->tag);
-         } else {
-            fprintf(stderr, "OK %02lx: hl=%4lu l=%4lu - %s\n", identifier, data_offset, len, der_asn1_tag_to_string_map[l->tag]);
-         }
-#endif
+         s_print_err("OK", l, err, identifier, data_offset, len);
          len += data_offset;
 
          if (l->type == LTC_ASN1_CUSTOM_TYPE) {

+ 2 - 2
src/pk/asn1/der/teletex_string/der_length_teletex_string.c

@@ -135,7 +135,7 @@ static const struct {
 int der_teletex_char_encode(int c)
 {
    int x;
-   for (x = 0; x < (int)(sizeof(teletex_table)/sizeof(teletex_table[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(teletex_table); x++) {
        if (teletex_table[x].code == c) {
           return teletex_table[x].value;
        }
@@ -146,7 +146,7 @@ int der_teletex_char_encode(int c)
 int der_teletex_value_decode(int v)
 {
    int x;
-   for (x = 0; x < (int)(sizeof(teletex_table)/sizeof(teletex_table[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(teletex_table); x++) {
        if (teletex_table[x].value == v) {
           return teletex_table[x].code;
        }

+ 2 - 2
src/pk/asn1/oid/pk_get.c

@@ -66,7 +66,7 @@ int pk_get_oid_id(enum ltc_pka_id pka, enum ltc_oid_id *oid)
 {
    unsigned int i;
    LTC_ARGCHK(oid != NULL);
-   for (i = 1; i < sizeof(pka_oids)/sizeof(pka_oids[0]); ++i) {
+   for (i = 1; i < LTC_ARRAY_SIZE(pka_oids); ++i) {
       if (pka_oids[i].pka == pka) {
          *oid = pka_oids[i].id;
          return CRYPT_OK;
@@ -95,7 +95,7 @@ int pk_get_oid_from_asn1(const ltc_asn1_list *oid, enum ltc_oid_id *id)
       return err;
    }
 
-   for (i = 1; i < sizeof(pka_oids)/sizeof(pka_oids[0]); ++i) {
+   for (i = 1; i < LTC_ARRAY_SIZE(pka_oids); ++i) {
       if (XSTRCMP(pka_oids[i].oid, tmp) == 0) {
          *id = pka_oids[i].id;
          return CRYPT_OK;

+ 49 - 26
src/pk/asn1/oid/pk_oid_str.c

@@ -6,20 +6,18 @@
 int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen)
 {
    unsigned long i, j, limit, oid_j;
-   size_t OID_len;
 
    LTC_ARGCHK(oidlen != NULL);
 
    limit = *oidlen;
    *oidlen = 0; /* make sure that we return zero oidlen on error */
-   for (i = 0; i < limit; i++) oid[i] = 0;
-
+   if (oid != NULL) {
+      XMEMSET(oid, 0, sizeof(*oid) * limit);
+   }
    if (OID == NULL) return CRYPT_OK;
+   if (OID[0] == '\0') return CRYPT_OK;
 
-   OID_len = XSTRLEN(OID);
-   if (OID_len == 0) return CRYPT_OK;
-
-   for (i = 0, j = 0; i < OID_len; i++) {
+   for (i = 0, j = 0; OID[i] != '\0'; i++) {
       if (OID[i] == '.') {
          if (++j >= limit) continue;
       }
@@ -34,49 +32,74 @@ int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen
       }
    }
    if (j == 0) return CRYPT_ERROR;
-   if (j >= limit) {
-      *oidlen = j;
+   *oidlen = j + 1;
+   if (j >= limit || oid == NULL) {
       return CRYPT_BUFFER_OVERFLOW;
    }
-   *oidlen = j + 1;
    return CRYPT_OK;
 }
 
+typedef struct num_to_str {
+   int err;
+   char *wr;
+   unsigned long max_len, res_len;
+} num_to_str;
+
+static LTC_INLINE void s_wr(char c, num_to_str *w)
+{
+   if (w->res_len == ULONG_MAX) {
+      w->err = CRYPT_OVERFLOW;
+      return;
+   }
+   w->res_len++;
+   if (w->res_len > w->max_len) w->wr = NULL;
+   if (w->wr) w->wr[w->max_len - w->res_len] = c;
+}
+
 int pk_oid_num_to_str(const unsigned long *oid, unsigned long oidlen, char *OID, unsigned long *outlen)
 {
    int i;
-   unsigned long j, k;
-   char tmp[LTC_OID_MAX_STRLEN] = { 0 };
+   num_to_str w;
+   unsigned long j;
 
    LTC_ARGCHK(oid != NULL);
    LTC_ARGCHK(oidlen < INT_MAX);
    LTC_ARGCHK(outlen != NULL);
 
-   for (i = oidlen - 1, k = 0; i >= 0; i--) {
+   if (OID == NULL || *outlen == 0) {
+      w.max_len = ULONG_MAX;
+      w.wr = NULL;
+   } else {
+      w.max_len = *outlen;
+      w.wr = OID;
+   }
+   w.res_len = 0;
+   w.err = CRYPT_OK;
+
+   s_wr('\0', &w);
+   for (i = oidlen; i --> 0;) {
       j = oid[i];
       if (j == 0) {
-         tmp[k] = '0';
-         if (++k >= sizeof(tmp)) return CRYPT_ERROR;
-      }
-      else {
+         s_wr('0', &w);
+      } else {
          while (j > 0) {
-            tmp[k] = '0' + (j % 10);
-            if (++k >= sizeof(tmp)) return CRYPT_ERROR;
+            s_wr('0' + (j % 10), &w);
             j /= 10;
          }
       }
       if (i > 0) {
-        tmp[k] = '.';
-        if (++k >= sizeof(tmp)) return CRYPT_ERROR;
+         s_wr('.', &w);
       }
    }
-   if (*outlen < k + 1) {
-      *outlen = k + 1;
+   if (w.err != CRYPT_OK) {
+      return w.err;
+   }
+   if (*outlen < w.res_len || OID == NULL) {
+      *outlen = w.res_len;
       return CRYPT_BUFFER_OVERFLOW;
    }
    LTC_ARGCHK(OID != NULL);
-   for (j = 0; j < k; j++) OID[j] = tmp[k - j - 1];
-   OID[k] = '\0';
-   *outlen = k; /* the length without terminating NUL byte */
+   XMEMMOVE(OID, OID + (w.max_len - w.res_len), w.res_len);
+   *outlen = w.res_len;
    return CRYPT_OK;
 }

+ 1 - 1
src/pk/asn1/x509/x509_decode_subject_public_key_info.c

@@ -70,7 +70,7 @@ int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long i
    }
 
    /* this includes the internal hash ID and optional params (NULL in this case) */
-   LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, sizeof(tmpoid)/sizeof(tmpoid[0]));
+   LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, tmpoid, LTC_ARRAY_SIZE(tmpoid));
    if (parameters_type == LTC_ASN1_EOL) {
       alg_id_num = 1;
    } else {

+ 2 - 2
src/pk/asn1/x509/x509_encode_subject_public_key_info.c

@@ -47,7 +47,7 @@ int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outle
         return err;
    }
 
-   oidlen = sizeof(oid)/sizeof(oid[0]);
+   oidlen = LTC_ARRAY_SIZE(oid);
    if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) {
       return err;
    }
@@ -56,7 +56,7 @@ int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outle
    LTC_SET_ASN1(alg_id, 1, parameters_type,            parameters, parameters_len);
 
    return der_encode_sequence_multi(out, outlen,
-        LTC_ASN1_SEQUENCE, (unsigned long)sizeof(alg_id)/sizeof(alg_id[0]), alg_id,
+        LTC_ASN1_SEQUENCE, (unsigned long)LTC_ARRAY_SIZE(alg_id), alg_id,
         LTC_ASN1_RAW_BIT_STRING, public_key_len*8U, public_key,
         LTC_ASN1_EOL,     0UL, NULL);
 

+ 1 - 1
src/pk/dsa/dsa_decrypt_key.c

@@ -40,7 +40,7 @@ int dsa_decrypt_key(const unsigned char *in,  unsigned long  inlen,
    }
 
    /* decode to find out hash */
-   LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, sizeof(hashOID)/sizeof(hashOID[0]));
+   LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, LTC_ARRAY_SIZE(hashOID));
    err = der_decode_sequence(in, inlen, decode, 1);
    if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
       return err;

+ 1 - 1
src/pk/dsa/dsa_generate_pqg.c

@@ -89,7 +89,7 @@ static int s_dsa_make_params(prng_state *prng, int wprng, int group_size, int mo
 #endif
 
   hash = -1;
-  for (i = 0; i < sizeof(accepted_hashes)/sizeof(accepted_hashes[0]); ++i) {
+  for (i = 0; i < LTC_ARRAY_SIZE(accepted_hashes); ++i) {
     hash = find_hash(accepted_hashes[i]);
     if (hash != -1) break;
   }

+ 1 - 1
src/pk/ec25519/ec25519_export.c

@@ -46,7 +46,7 @@ int ec25519_export(       unsigned char *out, unsigned long *outlen,
          if ((err = pk_get_oid(oid_id, &OID)) != CRYPT_OK) {
             return err;
          }
-         oidlen = sizeof(oid)/sizeof(oid[0]);
+         oidlen = LTC_ARRAY_SIZE(oid);
          if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) {
             return err;
          }

+ 1 - 1
src/pk/ecc/ecc_decrypt_key.c

@@ -41,7 +41,7 @@ int ecc_decrypt_key(const unsigned char *in,  unsigned long  inlen,
    }
 
    /* decode to find out hash */
-   LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, sizeof(hashOID)/sizeof(hashOID[0]));
+   LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, LTC_ARRAY_SIZE(hashOID));
    err = der_decode_sequence(in, inlen, decode, 1);
    if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
       return err;

+ 1 - 1
src/pk/ecc/ecc_export_openssl.c

@@ -90,7 +90,7 @@ int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, cons
          }
       */
 
-      oidlen = sizeof(oid)/sizeof(oid[0]);
+      oidlen = LTC_ARRAY_SIZE(oid);
       if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) {
          goto error;
       }

+ 1 - 1
src/pk/ecc/ecc_ssh_ecdsa_encode_name.c

@@ -19,7 +19,7 @@
 */
 int ecc_ssh_ecdsa_encode_name(char *buffer, unsigned long *buflen, const ecc_key *key)
 {
-   char oidstr[64] = {0};
+   char oidstr[LTC_OID_MAX_STRLEN] = {0};
    unsigned long oidlen = sizeof(oidstr);
    int err, size = 0;
 

+ 3 - 3
src/pk/rsa/rsa_key.c

@@ -24,7 +24,7 @@ static void s_mpi_shrink_multi(void **a, ...)
    cur = a;
    va_start(args, a);
    while (cur != NULL) {
-      if (n >= sizeof(tmp)/sizeof(tmp[0])) {
+      if (n >= LTC_ARRAY_SIZE(tmp)) {
          goto out;
       }
       if (*cur != NULL) {
@@ -49,8 +49,8 @@ out:
     * or after this was called with too many args
     */
    if ((err != CRYPT_OK) ||
-         (n >= sizeof(tmp)/sizeof(tmp[0]))) {
-      for (n = 0; n < sizeof(tmp)/sizeof(tmp[0]); ++n) {
+         (n >= LTC_ARRAY_SIZE(tmp))) {
+      for (n = 0; n < LTC_ARRAY_SIZE(tmp); ++n) {
          if (tmp[n] != NULL) {
             ltc_mp_clear(tmp[n]);
          }

+ 1 - 1
src/pk/rsa/rsa_verify_hash.c

@@ -131,7 +131,7 @@ int rsa_verify_hash_ex(const unsigned char *sig,            unsigned long  sigle
            hash    OCTET STRING
         }
      */
-      LTC_SET_ASN1(digestinfo, 0, LTC_ASN1_OBJECT_IDENTIFIER, loid, sizeof(loid)/sizeof(loid[0]));
+      LTC_SET_ASN1(digestinfo, 0, LTC_ASN1_OBJECT_IDENTIFIER, loid, LTC_ARRAY_SIZE(loid));
       LTC_SET_ASN1(digestinfo, 1, LTC_ASN1_NULL,              NULL,                          0);
       LTC_SET_ASN1(siginfo,    0, LTC_ASN1_SEQUENCE,          digestinfo,                    2);
       LTC_SET_ASN1(siginfo,    1, LTC_ASN1_OCTET_STRING,      tmpbuf,                        siglen);

+ 3 - 3
tests/base16_test.c

@@ -25,17 +25,17 @@ int base16_test(void)
          DO(base16_encode(in, x, out, &l1, idx));
          l2 = sizeof(tmp);
          DO(base16_decode(out, l1, tmp, &l2));
-         DO(do_compare_testvector(tmp, l2, in, x, "random base16", idx * 100 + x));
+         COMPARE_TESTVECTOR(tmp, l2, in, x, "random base16", idx * 100 + x);
       }
    }
 
    for (idx = 0; idx < 2; idx++) {
       l1 = sizeof(out);
       DO(base16_encode(testin, sizeof(testin), out, &l1, idx));
-      DO(do_compare_testvector(out, XSTRLEN(out), testout[idx], XSTRLEN(testout[idx]), "testout base16", idx));
+      COMPARE_TESTVECTOR(out, XSTRLEN(out), testout[idx], XSTRLEN(testout[idx]), "testout base16", idx);
       l2 = sizeof(tmp);
       DO(base16_decode(out, l1, tmp, &l2));
-      DO(do_compare_testvector(tmp, l2, testin, sizeof(testin), "testin base16", idx));
+      COMPARE_TESTVECTOR(tmp, l2, testin, sizeof(testin), "testin base16", idx);
    }
 
    l1 = 4;

+ 3 - 3
tests/base32_test.c

@@ -32,17 +32,17 @@ int base32_test(void)
          DO(base32_encode(in, x, out, &l1, testid[idx]));
          l2 = sizeof(tmp);
          DO(base32_decode(out, l1, tmp, &l2, testid[idx]));
-         DO(do_compare_testvector(tmp, l2, in, x, "random base32", idx * 100 + x));
+         COMPARE_TESTVECTOR(tmp, l2, in, x, "random base32", idx * 100 + x);
       }
    }
 
    for (idx = 0; idx < 4; idx++) {
       l1 = sizeof(out);
       DO(base32_encode(testin, sizeof(testin), out, &l1, testid[idx]));
-      DO(do_compare_testvector(out, l1, testout[idx], XSTRLEN(testout[idx]), "testout base32", idx));
+      COMPARE_TESTVECTOR(out, l1, testout[idx], XSTRLEN(testout[idx]), "testout base32", idx);
       l2 = sizeof(tmp);
       DO(base32_decode(out, l1, tmp, &l2, testid[idx]));
-      DO(do_compare_testvector(tmp, l2, testin, sizeof(testin), "testin base32", idx));
+      COMPARE_TESTVECTOR(tmp, l2, testin, sizeof(testin), "testin base32", idx);
    }
 
    return CRYPT_OK;

+ 15 - 15
tests/base64_test.c

@@ -71,29 +71,29 @@ int base64_test(void)
          {"vuiSPKIl8PiR5O-rC4z9_xTQK", invalid},
    };
 
-   for (x = 0; x < sizeof(url_cases)/sizeof(url_cases[0]); ++x) {
+   for (x = 0; x < LTC_ARRAY_SIZE(url_cases); ++x) {
        slen1 = XSTRLEN(url_cases[x].s);
        l1 = sizeof(tmp);
        if(url_cases[x].flag == strict) {
           DO(base64url_strict_decode(url_cases[x].s, slen1, tmp, &l1));
-          DO(do_compare_testvector(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_strict_decode", x));
+          COMPARE_TESTVECTOR(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_strict_decode", x);
           DO(base64url_sane_decode(url_cases[x].s, slen1, tmp, &l1));
-          DO(do_compare_testvector(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_sane_decode/strict", x));
+          COMPARE_TESTVECTOR(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_sane_decode/strict", x);
           DO(base64url_decode(url_cases[x].s, slen1, tmp, &l1));
-          DO(do_compare_testvector(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_decode/strict", x));
+          COMPARE_TESTVECTOR(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_decode/strict", x);
        }
        else if(url_cases[x].flag == relaxed) {
           DO(base64url_strict_decode(url_cases[x].s, slen1, tmp, &l1) == CRYPT_INVALID_PACKET ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR);
           DO(base64url_sane_decode(url_cases[x].s, slen1, tmp, &l1));
-          DO(do_compare_testvector(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_sane_decode/relaxed", x));
+          COMPARE_TESTVECTOR(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_sane_decode/relaxed", x);
           DO(base64url_decode(url_cases[x].s, slen1, tmp, &l1));
-          DO(do_compare_testvector(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_decode/relaxed", x));
+          COMPARE_TESTVECTOR(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_decode/relaxed", x);
        }
        else if(url_cases[x].flag == insane) {
           DO(base64url_strict_decode(url_cases[x].s, slen1, tmp, &l1) == CRYPT_INVALID_PACKET ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR);
           DO(base64url_sane_decode(url_cases[x].s, slen1, tmp, &l1) == CRYPT_INVALID_PACKET ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR);
           DO(base64url_decode(url_cases[x].s, slen1, tmp, &l1));
-          DO(do_compare_testvector(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_decode/insane", x));
+          COMPARE_TESTVECTOR(tmp, l1, special_case, sizeof(special_case) - 1, "base64url_decode/insane", x);
        }
        else { /* invalid */
           DO(base64url_strict_decode(url_cases[x].s, slen1, tmp, &l1) == CRYPT_INVALID_PACKET ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR);
@@ -103,30 +103,30 @@ int base64_test(void)
        l2 = sizeof(out);
        if(x == 0) {
           DO(base64url_encode(tmp, l1, out, &l2));
-          DO(do_compare_testvector(out, l2, url_cases[x].s, XSTRLEN(url_cases[x].s), "base64url_encode", x));
+          COMPARE_TESTVECTOR(out, l2, url_cases[x].s, XSTRLEN(url_cases[x].s), "base64url_encode", x);
        }
        if(x == 1) {
           DO(base64url_strict_encode(tmp, l1, out, &l2));
-          DO(do_compare_testvector(out, l2, url_cases[x].s, XSTRLEN(url_cases[x].s), "base64url_strict_encode", x));
+          COMPARE_TESTVECTOR(out, l2, url_cases[x].s, XSTRLEN(url_cases[x].s), "base64url_strict_encode", x);
        }
    }
 #endif
 
 #if defined(LTC_BASE64)
-   for (x = 0; x < sizeof(cases)/sizeof(cases[0]); ++x) {
+   for (x = 0; x < LTC_ARRAY_SIZE(cases); ++x) {
        memset(out, 0, sizeof(out));
        memset(tmp, 0, sizeof(tmp));
        slen1 = XSTRLEN(cases[x].s);
        l1 = sizeof(out);
        DO(base64_encode((unsigned char*)cases[x].s, slen1, out, &l1));
-       DO(do_compare_testvector(out, l1, cases[x].b64, XSTRLEN(cases[x].b64), "base64_encode", x));
+       COMPARE_TESTVECTOR(out, l1, cases[x].b64, XSTRLEN(cases[x].b64), "base64_encode", x);
        l2 = sizeof(tmp);
        DO(base64_strict_decode(out, l1, tmp, &l2));
-       DO(do_compare_testvector(tmp, l2, cases[x].s, slen1, "base64_strict_decode", x));
+       COMPARE_TESTVECTOR(tmp, l2, cases[x].s, slen1, "base64_strict_decode", x);
        DO(base64_sane_decode(out, l1, tmp, &l2));
-       DO(do_compare_testvector(tmp, l2, cases[x].s, slen1, "base64_sane_decode", x));
+       COMPARE_TESTVECTOR(tmp, l2, cases[x].s, slen1, "base64_sane_decode", x);
        DO(base64_decode(out, l1, tmp, &l2));
-       DO(do_compare_testvector(tmp, l2, cases[x].s, slen1, "base64_decode", x));
+       COMPARE_TESTVECTOR(tmp, l2, cases[x].s, slen1, "base64_decode", x);
    }
 
    for  (x = 0; x < 64; x++) {
@@ -135,7 +135,7 @@ int base64_test(void)
        DO(base64_encode(in, x, out, &l1));
        l2 = sizeof(tmp);
        DO(base64_decode(out, l1, tmp, &l2));
-       DO(do_compare_testvector(tmp, x, in, x, "random base64", x));
+       COMPARE_TESTVECTOR(tmp, x, in, x, "random base64", x);
    }
 
    x--;

+ 1 - 1
tests/bcrypt_test.c

@@ -125,7 +125,7 @@ int bcrypt_test(void)
       l = t->keylen;
       XMEMSET(key, 0, sizeof(key));
       DO(bcrypt_pbkdf_openbsd(t->password, t->passlen, (unsigned char*)t->salt, t->saltlen, t->rounds, idx, key, &l));
-      DO(do_compare_testvector(key, l, t->key, t->keylen, "OpenBSD testvectors", i));
+      COMPARE_TESTVECTOR(key, l, t->key, t->keylen, "OpenBSD testvectors", i);
 
 #if defined(LTC_TEST_DBG) && LTC_TEST_DBG > 1
       printf("BCRYPT test #%d OK\n", i);

+ 5 - 2
tests/common.c

@@ -103,7 +103,10 @@ static DIR *s_opendir(const char *path, char *mypath, unsigned long l)
 static int s_read_and_process(FILE *f, unsigned long sz, void *ctx, dir_iter_cb process)
 {
    int err = CRYPT_OK;
-   void* buf = XMALLOC(sz + 1);
+   void* buf;
+   if (f == NULL)
+      return CRYPT_FILE_NOTFOUND;
+   buf = XMALLOC(sz + 1);
    if (buf == NULL)
       return CRYPT_MEM;
    if (fread(buf, 1, sz, f) != sz) {
@@ -177,7 +180,7 @@ int test_process_dir(const char *path, void *ctx, dir_iter_cb iter, dir_fiter_cb
       }
 
 continue_loop:
-      fclose(f);
+      if (f != NULL) fclose(f);
       f = NULL;
    }
    if (f != NULL) fclose(f);

+ 41 - 43
tests/der_test.c

@@ -691,32 +691,6 @@ static void der_set_test(void)
 
 }
 
-
-/* we are encoding
-
-  SEQUENCE {
-     PRINTABLE "printable"
-     IA5       "ia5"
-     SEQUENCE {
-        INTEGER 12345678
-        UTCTIME { 91, 5, 6, 16, 45, 40, 1, 7, 0 }
-        GENERALIZEDTIME { 2017, 03, 21, 10, 21, 12, 4, 1, 2, 0 }
-        SEQUENCE {
-           OCTET STRING { 1, 2, 3, 4 }
-           BIT STRING   { 1, 0, 0, 1 }
-           SEQUENCE {
-              OID       { 1, 2, 840, 113549 }
-              NULL
-              SET OF {
-                 PRINTABLE "333"  -- WILL GET SORTED
-                 PRINTABLE "222"
-           }
-        }
-     }
-  }
-
-*/
-
 static void s_der_oid_test(void)
 {
    static const unsigned char oid_x690_8_19_5_example[] = { 0x06, 0x03, 0x88, 0x37, 0x03 };
@@ -733,7 +707,7 @@ static void s_der_oid_test(void)
    DO(der_encode_object_identifier(decoded_list->data, decoded_list->size, buf, &len));
    der_sequence_free(decoded_list);
 
-   DO(do_compare_testvector(buf, len, oid_x690_8_19_5_example, sizeof(oid_x690_8_19_5_example), "OID X6.90 Ch. 8.19.5 Example", 0));
+   COMPARE_TESTVECTOR(buf, len, oid_x690_8_19_5_example, sizeof(oid_x690_8_19_5_example), "OID X6.90 Ch. 8.19.5 Example", 0);
 
    oid[0] = 3;
    oid[1] = 4;
@@ -745,6 +719,30 @@ static void s_der_oid_test(void)
    SHOULD_FAIL(der_length_object_identifier(oid, 3, &len));
 }
 
+/* we are encoding
+
+  SEQUENCE {
+     PRINTABLE "printable"
+     IA5       "ia5"
+     SEQUENCE {
+        INTEGER 12345678
+        UTCTIME { 91, 5, 6, 16, 45, 40, 1, 7, 0 }
+        GENERALIZEDTIME { 2017, 03, 21, 10, 21, 12, 4, 1, 2, 0 }
+        SEQUENCE {
+           OCTET STRING { 1, 2, 3, 4 }
+           BIT STRING   { 1, 0, 0, 1 }
+           SEQUENCE {
+              OID       { 1, 2, 840, 113549 }
+              NULL
+              SET OF {
+                 PRINTABLE "333"  -- WILL GET SORTED
+                 PRINTABLE "222"
+           }
+        }
+     }
+  }
+
+*/
 static void der_flexi_test(void)
 {
    static const char printable_str[]    = "printable";
@@ -1014,7 +1012,7 @@ static void der_flexi_test(void)
          exit(EXIT_FAILURE);
       }
 
-      if (l->size != sizeof(oid_str)/sizeof(oid_str[0]) || memcmp(oid_str, l->data, l->size*sizeof(oid_str[0]))) {
+      if (l->size != LTC_ARRAY_SIZE(oid_str) || memcmp(oid_str, l->data, l->size*sizeof(oid_str[0]))) {
          fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child);
          exit(EXIT_FAILURE);
       }
@@ -1097,13 +1095,13 @@ static int der_choice_n_custom_test(void)
    for (x = 0; x < sizeof(octetbuf); x++) { octetbuf[x] = x;     }
    for (x = 0; x < sizeof(ia5buf); x++)   { ia5buf[x]   = 'a';   }
    for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a';   }
-   for (x = 0; x < sizeof(utf8buf)/sizeof(utf8buf[0]); x++) { utf8buf[x] = L'a';   }
+   for (x = 0; x < LTC_ARRAY_SIZE(utf8buf); x++) { utf8buf[x] = L'a';   }
    integer = 1;
    boolean[0] = 1;
-   for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++)   { oidbuf[x] = x + 1;   }
+   for (x = 0; x < LTC_ARRAY_SIZE(oidbuf); x++)   { oidbuf[x] = x + 1;   }
    DO(ltc_mp_init(&mpinteger));
 
-   n = sizeof(types)/sizeof(types[0]);
+   n = LTC_ARRAY_SIZE(types);
    for (x = 0; x < n * 2; x++) {
        /* setup list */
        y = 0;
@@ -1115,13 +1113,13 @@ static int der_choice_n_custom_test(void)
        }
        LTC_SET_ASN1(types, y++, LTC_ASN1_OCTET_STRING, octetbuf, sizeof(octetbuf));
        LTC_SET_ASN1(types, y++, LTC_ASN1_IA5_STRING, ia5buf, sizeof(ia5buf));
-       LTC_SET_ASN1(types, y++, LTC_ASN1_BOOLEAN, boolean, sizeof(boolean)/sizeof(boolean[0]));
+       LTC_SET_ASN1(types, y++, LTC_ASN1_BOOLEAN, boolean, LTC_ARRAY_SIZE(boolean));
        if (x > n) {
           LTC_SET_ASN1(types, y++, LTC_ASN1_SHORT_INTEGER, &integer, 1);
        } else {
           LTC_SET_ASN1(types, y++, LTC_ASN1_INTEGER, mpinteger, 1);
        }
-       LTC_SET_ASN1(types, y++, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, sizeof(oidbuf)/sizeof(oidbuf[0]));
+       LTC_SET_ASN1(types, y++, LTC_ASN1_OBJECT_IDENTIFIER, oidbuf, LTC_ARRAY_SIZE(oidbuf));
        if (x > n) {
           LTC_SET_ASN1(types, y++, LTC_ASN1_UTCTIME, &utctime, 1);
        } else {
@@ -1131,7 +1129,7 @@ static int der_choice_n_custom_test(void)
        LTC_SET_ASN1(custom, 0, LTC_ASN1_NULL, NULL, 0);
        LTC_SET_ASN1_CUSTOM_CONSTRUCTED(types, y++, LTC_ASN1_CL_CONTEXT_SPECIFIC, 0, custom);
 
-       LTC_SET_ASN1(types, y++, LTC_ASN1_UTF8_STRING, utf8buf, sizeof(utf8buf)/sizeof(utf8buf[0]));
+       LTC_SET_ASN1(types, y++, LTC_ASN1_UTF8_STRING, utf8buf, LTC_ARRAY_SIZE(utf8buf));
 
        LTC_SET_ASN1(host, 0, LTC_ASN1_CHOICE, types, n);
 
@@ -1295,7 +1293,7 @@ static void der_Xcode_run(const der_Xcode_t* x)
       d2 = XREALLOC(d2, l2 * x->type_sz);
    }
    DO(x->decode(d1, l1, d2, &l2));
-   DO(do_compare_testvector(d2, (l2/x->factor) * x->type_sz, x->in, x->in_sz, x->what, __LINE__));
+   COMPARE_TESTVECTOR(d2, (l2/x->factor) * x->type_sz, x->in, x->in_sz, x->what, __LINE__);
    XFREE(d2);
    XFREE(d1);
 }
@@ -1344,7 +1342,7 @@ static void der_Xcode_test(void)
     DER_XCODE(utf8_string, wchar_string),
    };
 
-   for (i = 0; i < sizeof(xcode_tests)/sizeof(xcode_tests[0]); ++i) {
+   for (i = 0; i < LTC_ARRAY_SIZE(xcode_tests); ++i) {
       der_Xcode_run(&xcode_tests[i]);
    }
 
@@ -1425,7 +1423,7 @@ static void s_der_regression_test(void)
    SHOULD_FAIL(der_decode_sequence_flexi(issue_507, &len, &l));
 
    len = sizeof(utf8_length);
-   outlen = sizeof(wtmp)/sizeof(wtmp[0]);
+   outlen = LTC_ARRAY_SIZE(wtmp);
    DO(der_decode_utf8_string(utf8_length, len, wtmp, &outlen));
    ENSURE(outlen == 2);
 }
@@ -1544,7 +1542,7 @@ static void der_toolong_test(void)
 
    ltc_mp_deinit_multi(int1, int2, LTC_NULL);
 
-   LTC_SET_ASN1(seqoid,  0, LTC_ASN1_OBJECT_IDENTIFIER, oid, sizeof(oid)/sizeof(oid[0]));
+   LTC_SET_ASN1(seqoid,  0, LTC_ASN1_OBJECT_IDENTIFIER, oid, LTC_ARRAY_SIZE(oid));
    LTC_SET_ASN1(seqoid,  1, LTC_ASN1_NULL,              NULL,   0);
    LTC_SET_ASN1(seqmain, 0, LTC_ASN1_SEQUENCE,          seqoid, 2);
    LTC_SET_ASN1(seqmain, 1, LTC_ASN1_OCTET_STRING,      buf32,  32);
@@ -1805,7 +1803,7 @@ int der_test(void)
 
 /* test OID */
    x = sizeof(buf[0]);
-   DO(der_encode_object_identifier((unsigned long*)rsa_oid, sizeof(rsa_oid)/sizeof(rsa_oid[0]), buf[0], &x));
+   DO(der_encode_object_identifier((unsigned long*)rsa_oid, LTC_ARRAY_SIZE(rsa_oid), buf[0], &x));
    if (x != sizeof(rsa_oid_der) || memcmp(rsa_oid_der, buf[0], x)) {
       fprintf(stderr, "rsa_oid_der encode failed to match, %lu, ", x);
       for (y = 0; y < x; y++) fprintf(stderr, "%02x ", buf[0][y]);
@@ -1813,9 +1811,9 @@ int der_test(void)
       return 1;
    }
 
-   y = sizeof(oid[0])/sizeof(oid[0][0]);
+   y = LTC_ARRAY_SIZE(oid[0]);
    DO(der_decode_object_identifier(buf[0], x, oid[0], &y));
-   if (y != sizeof(rsa_oid)/sizeof(rsa_oid[0]) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
+   if (y != LTC_ARRAY_SIZE(rsa_oid) || memcmp(rsa_oid, oid[0], sizeof(rsa_oid))) {
       fprintf(stderr, "rsa_oid_der decode failed to match, %lu, ", y);
       for (z = 0; z < y; z++) fprintf(stderr, "%lu ", oid[0][z]);
       fprintf(stderr, "\n");
@@ -1827,7 +1825,7 @@ int der_test(void)
        /* pick a random number of words */
        ENSURE(yarrow_read(buf[0], 4, &yarrow_prng) == 4);
        LOAD32L(z, buf[0]);
-       z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
+       z = 2 + (z % (LTC_ARRAY_SIZE(oid[0]) - 2));
 
        /* fill them in */
        oid[0][0] = buf[0][0] % 3;
@@ -1849,7 +1847,7 @@ int der_test(void)
        }
 
        /* decode it */
-       y = sizeof(oid[0])/sizeof(oid[0][0]);
+       y = LTC_ARRAY_SIZE(oid[0]);
        DO(der_decode_object_identifier(buf[0], x, oid[1], &y));
        if (y != z) {
           fprintf(stderr, "Random OID %lu test failed, decode length mismatch: %lu, %lu\n", z, x, y);

+ 11 - 11
tests/dh_test.c

@@ -125,7 +125,7 @@ static int s_dhparam_test(void)
       return CRYPT_ERROR;
    }
    DO(ltc_mp_to_unsigned_bin(k.prime, buf));
-   DO(do_compare_testvector(buf, sizeof(prime), prime, sizeof(prime), "dhparam_test: prime mismatch", 1));
+   COMPARE_TESTVECTOR(buf, sizeof(prime), prime, sizeof(prime), "dhparam_test: prime mismatch", 1);
    if (ltc_mp_cmp_d(k.base, 2) != LTC_MP_EQ) {
       printf("dhparam_test: base mismatch\n");
       dh_free(&k);
@@ -259,16 +259,16 @@ static int s_set_test(void)
 
       len = sizeof(buf);
       DO(dh_export(buf, &len, PK_PRIVATE, &k1));
-      DO(do_compare_testvector(buf, len, export_private, sizeof(export_private), "radix_test: dh_export+PK_PRIVATE mismatch", i*10 + 0));
+      COMPARE_TESTVECTOR(buf, len, export_private, sizeof(export_private), "radix_test: dh_export+PK_PRIVATE mismatch", i*10 + 0);
       len = sizeof(buf);
       DO(dh_export(buf, &len, PK_PUBLIC, &k1));
-      DO(do_compare_testvector(buf, len, export_public, sizeof(export_public), "radix_test: dh_export+PK_PUBLIC mismatch", i*10 + 1));
+      COMPARE_TESTVECTOR(buf, len, export_public, sizeof(export_public), "radix_test: dh_export+PK_PUBLIC mismatch", i*10 + 1);
       len = sizeof(buf);
       DO(dh_export_key(buf, &len, PK_PRIVATE, &k1));
-      DO(do_compare_testvector(buf, len, xbin, sizeof(xbin), "radix_test: dh_export+PK_PRIVATE mismatch", i*10 + 2));
+      COMPARE_TESTVECTOR(buf, len, xbin, sizeof(xbin), "radix_test: dh_export+PK_PRIVATE mismatch", i*10 + 2);
       len = sizeof(buf);
       DO(dh_export_key(buf, &len, PK_PUBLIC, &k1));
-      DO(do_compare_testvector(buf, len, ybin, sizeof(ybin), "radix_test: dh_export+PK_PUBLIC mismatch", i*10 + 3));
+      COMPARE_TESTVECTOR(buf, len, ybin, sizeof(ybin), "radix_test: dh_export+PK_PUBLIC mismatch", i*10 + 3);
       dh_free(&k1);
 
       DO(dh_set_pg(test[i].p, test[i].plen, test[i].g, test[i].glen, &k1));
@@ -276,10 +276,10 @@ static int s_set_test(void)
 
       len = sizeof(buf);
       DO(dh_export(buf, &len, PK_PRIVATE, &k1));
-      DO(do_compare_testvector(buf, len, export_private, sizeof(export_private), "radix_test: dh_export+PK_PRIVATE mismatc", i*10 + 4));
+      COMPARE_TESTVECTOR(buf, len, export_private, sizeof(export_private), "radix_test: dh_export+PK_PRIVATE mismatc", i*10 + 4);
       len = sizeof(buf);
       DO(dh_export(buf, &len, PK_PUBLIC, &k1));
-      DO(do_compare_testvector(buf, len, export_public, sizeof(export_public), "radix_test: dh_export+PK_PUBLIC mismatch", i*10 + 5));
+      COMPARE_TESTVECTOR(buf, len, export_public, sizeof(export_public), "radix_test: dh_export+PK_PUBLIC mismatch", i*10 + 5);
       dh_free(&k1);
 
       DO(dh_set_pg(test[i].p, test[i].plen, test[i].g, test[i].glen, &k2));
@@ -287,10 +287,10 @@ static int s_set_test(void)
 
       len = sizeof(buf);
       DO(dh_export(buf, &len, PK_PUBLIC, &k2));
-      DO(do_compare_testvector(buf, len, export_public, sizeof(export_public), "radix_test: dh_export+PK_PUBLIC mismatch", i*10 + 6));
+      COMPARE_TESTVECTOR(buf, len, export_public, sizeof(export_public), "radix_test: dh_export+PK_PUBLIC mismatch", i*10 + 6);
       len = sizeof(buf);
       DO(dh_export_key(buf, &len, PK_PUBLIC, &k2));
-      DO(do_compare_testvector(buf, len, ybin, sizeof(ybin), "radix_test: dh_export+PK_PUBLIC mismatch", i*10 + 7));
+      COMPARE_TESTVECTOR(buf, len, ybin, sizeof(ybin), "radix_test: dh_export+PK_PUBLIC mismatch", i*10 + 7);
       dh_free(&k2);
 
       DO(dh_set_pg(test[i].p, test[i].plen, test[i].g, test[i].glen, &k3));
@@ -298,10 +298,10 @@ static int s_set_test(void)
 
       len = ltc_mp_unsigned_bin_size(k3.prime);
       DO(ltc_mp_to_unsigned_bin(k3.prime, buf));
-      DO(do_compare_testvector(buf, len, pbin, sizeof(pbin), "radix_test: dh_make_key_ex prime mismatch", i*10 + 8));
+      COMPARE_TESTVECTOR(buf, len, pbin, sizeof(pbin), "radix_test: dh_make_key_ex prime mismatch", i*10 + 8);
       len = ltc_mp_unsigned_bin_size(k3.base);
       DO(ltc_mp_to_unsigned_bin(k3.base, buf));
-      DO(do_compare_testvector(buf, len, gbin, sizeof(gbin), "radix_test: dh_make_key_ex base mismatch", i*10 + 9));
+      COMPARE_TESTVECTOR(buf, len, gbin, sizeof(gbin), "radix_test: dh_make_key_ex base mismatch", i*10 + 9);
       dh_free(&k3);
    }
 

+ 10 - 10
tests/dsa_test.c

@@ -151,21 +151,21 @@ static int s_dsa_compat_test(void)
 
   x = sizeof(tmp);
   DO(dsa_export(tmp, &x, PK_PRIVATE | PK_STD, &key));
-  DO(do_compare_testvector(tmp, x, ltc_dsa_private_test_key, sizeof(ltc_dsa_private_test_key),
-                         "DSA private export failed from dsa_import(priv_key)\n", __LINE__));
+  COMPARE_TESTVECTOR(tmp, x, ltc_dsa_private_test_key, sizeof(ltc_dsa_private_test_key),
+                         "DSA private export from dsa_import(priv_key)\n", __LINE__);
 
   x = sizeof(tmp);
   DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
-  DO(do_compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
-                         "DSA public export failed from dsa_import(priv_key)\n", __LINE__));
+  COMPARE_TESTVECTOR(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
+                         "DSA public export from dsa_import(priv_key)\n", __LINE__);
   dsa_free(&key);
 
   DO(dsa_import(openssl_pub_dsa, sizeof(openssl_pub_dsa), &key));
 
   x = sizeof(tmp);
   DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
-  DO(do_compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
-                         "DSA public export failed from dsa_import(pub_key)\n", __LINE__));
+  COMPARE_TESTVECTOR(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
+                         "DSA public export from dsa_import(pub_key)\n", __LINE__);
   dsa_free(&key);
 
   /* try import private key from raw hexadecimal numbers */
@@ -188,7 +188,7 @@ static int s_dsa_compat_test(void)
   len = sizeof(buf);
   DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
   DO(do_compare_testvector(buf, len, ltc_dsa_private_test_key, sizeof(ltc_dsa_private_test_key),
-                         "DSA private export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__));
+                         "DSA private export from dsa_set_pqg() & dsa_set_key()\n", __LINE__));
   dsa_free(&key);
 
   /* try import public key from raw hexadecimal numbers */
@@ -202,7 +202,7 @@ static int s_dsa_compat_test(void)
   len = sizeof(buf);
   DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
   DO(do_compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
-                         "DSA public export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__));
+                         "DSA public export from dsa_set_pqg() & dsa_set_key()\n", __LINE__));
   dsa_free(&key);
 
   /* try import dsaparam */
@@ -224,7 +224,7 @@ static int s_dsa_compat_test(void)
   len = sizeof(buf);
   DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
   DO(do_compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
-                         "DSA public export failed from dsa_set_pqg_dsaparam()\n", __LINE__));
+                         "DSA public export from dsa_set_pqg_dsaparam()\n", __LINE__));
   dsa_free(&key);
 
   /* try import dsaparam - our private key */
@@ -235,7 +235,7 @@ static int s_dsa_compat_test(void)
   len = sizeof(buf);
   DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
   DO(do_compare_testvector(buf, len, ltc_dsa_private_test_key, sizeof(ltc_dsa_private_test_key),
-                         "DSA private export failed from dsa_set_pqg_dsaparam()\n", __LINE__));
+                         "DSA private export from dsa_set_pqg_dsaparam()\n", __LINE__));
   dsa_free(&key);
 
   return CRYPT_OK;

+ 5 - 5
tests/ecc_test.c

@@ -156,7 +156,7 @@ static int s_ecc_test_shamir(void)
    LTC_ARGCHK((C1 = ltc_ecc_new_point()) != NULL);
    LTC_ARGCHK((C2 = ltc_ecc_new_point()) != NULL);
 
-   for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
+   for (x = 0; x < (int)LTC_ARRAY_SIZE(sizes); x++) {
        /* get the base point */
        for (z = 0; ltc_ecc_curves[z].prime != NULL; z++) {
            DO(ltc_mp_read_radix(modulus, ltc_ecc_curves[z].prime, 16));
@@ -406,7 +406,7 @@ static int s_ecc_old_api(void)
    ecc_sizes(&low, &high);
    if (low < 14 || high < 14 || low > 100 || high > 100 || high < low) return CRYPT_FAIL_TESTVECTOR;
 
-   for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) {
+   for (s = 0; s < LTC_ARRAY_SIZE(sizes); s++) {
       /* make up two keys */
       DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera));
       DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &userb));
@@ -420,7 +420,7 @@ static int s_ecc_old_api(void)
       y = sizeof(buf[1]);
       DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
 
-      DO(do_compare_testvector(buf[0], x, buf[1], y, "ecc Shared keys", s));
+      COMPARE_TESTVECTOR(buf[0], x, buf[1], y, "ecc Shared keys", s);
 
       /* now export userb */
       y = sizeof(buf[0]);
@@ -557,7 +557,7 @@ static int s_ecc_new_api(void)
    unsigned char data16[16] = { 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1, 0xd1 };
    unsigned long len16;
 
-   for (i = 0; i < (int)(sizeof(curvenames)/sizeof(curvenames[0])); i++) {
+   for (i = 0; i < (int)LTC_ARRAY_SIZE(curvenames); i++) {
       DO(ecc_find_curve(curvenames[i], &dp));
       /* make new key */
       DO(ecc_make_key_ex(&yarrow_prng, find_prng ("yarrow"), &key, dp));
@@ -1985,7 +1985,7 @@ static int s_ecc_test_recovery(void)
    ecc_free(&pubkey);
 #endif
 
-   for (i = 0; i < (int)(sizeof(curvenames)/sizeof(curvenames[0])); i++) {
+   for (i = 0; i < (int)LTC_ARRAY_SIZE(curvenames); i++) {
       DO(ecc_find_curve(curvenames[i], &dp));
 
       /* generate new key */

+ 9 - 9
tests/ed25519_test.c

@@ -66,7 +66,7 @@ static int s_rfc_8410_10_test(void)
    char tmp[512];
    unsigned long buflen, tmplen;
    password_ctx pw_ctx = { .callback = password_get };
-   for (n = 0; n < sizeof(rfc_8410_10)/sizeof(rfc_8410_10[0]); ++n) {
+   for (n = 0; n < LTC_ARRAY_SIZE(rfc_8410_10); ++n) {
       buflen = sizeof(buf);
       DO(base64_decode(rfc_8410_10[n].b64, XSTRLEN(rfc_8410_10[n].b64), buf, &buflen));
       switch (n) {
@@ -92,7 +92,7 @@ static int s_rfc_8410_10_test(void)
          DO(ed25519_export(buf, &buflen, rfc_8410_10[n].type, &key));
          tmplen = sizeof(tmp);
          DO(base64_encode(buf, buflen, tmp, &tmplen));
-         DO(do_compare_testvector(tmp, tmplen, rfc_8410_10[n].b64, XSTRLEN(rfc_8410_10[n].b64), "Ed25519 export-import", n));
+         COMPARE_TESTVECTOR(tmp, tmplen, rfc_8410_10[n].b64, XSTRLEN(rfc_8410_10[n].b64), "Ed25519 export-import", n);
       }
    }
    return CRYPT_OK;
@@ -213,7 +213,7 @@ static int s_rfc_8032_7_1_test(void)
    curve25519_key key, key2;
    int ret;
    const int should = 1;
-   for (n = 0; n < sizeof(rfc_8032_7_1)/sizeof(rfc_8032_7_1[0]); ++n) {
+   for (n = 0; n < LTC_ARRAY_SIZE(rfc_8032_7_1); ++n) {
       slen = sizeof(sec);
       DO(base16_decode(rfc_8032_7_1[n].secret_key, XSTRLEN(rfc_8032_7_1[n].secret_key), sec, &slen));
       plen = sizeof(pub);
@@ -225,9 +225,9 @@ static int s_rfc_8032_7_1_test(void)
       DO(ed25519_import_raw(sec, slen, PK_PRIVATE, &key));
       buflen = sizeof(buf);
       DO(ed25519_sign(msg, mlen, buf, &buflen, &key));
-      DO(do_compare_testvector(buf, buflen, sig, siglen, "Ed25519 RFC8032 7.1 - sign", n));
+      COMPARE_TESTVECTOR(buf, buflen, sig, siglen, "Ed25519 RFC8032 7.1 - sign", n);
       DO(ed25519_verify(msg, mlen, sig, siglen, &ret, &key));
-      DO(do_compare_testvector(&ret, sizeof(ret), &should, sizeof(should), "Ed25519 RFC8032 7.1 - verify w/ privkey", n));
+      COMPARE_TESTVECTOR(&ret, sizeof(ret), &should, sizeof(should), "Ed25519 RFC8032 7.1 - verify w/ privkey", n);
 
       xor_shuffle(sig, siglen, 0x8u);
       DO( ed25519_verify(msg, mlen, sig, siglen, &ret, &key));
@@ -244,7 +244,7 @@ static int s_rfc_8032_7_1_test(void)
       DO(base16_decode(rfc_8032_7_1[n].signature, XSTRLEN(rfc_8032_7_1[n].signature), sig, &siglen));
       DO(ed25519_import_raw(pub, plen, PK_PUBLIC, &key2));
       DO(ed25519_verify(msg, mlen, sig, siglen, &ret, &key2));
-      DO(do_compare_testvector(&ret, sizeof(ret), &should, sizeof(should), "Ed25519 RFC8032 7.1 - verify w/ pubkey", n));
+      COMPARE_TESTVECTOR(&ret, sizeof(ret), &should, sizeof(should), "Ed25519 RFC8032 7.1 - verify w/ pubkey", n);
 
       zeromem(&key, sizeof(key));
       zeromem(&key2, sizeof(key2));
@@ -316,7 +316,7 @@ static int s_rfc_8032_7_2_test(void)
    int ret;
    const int should = 1;
 
-   for (n = 0; n < sizeof(rfc_8032_7_2)/sizeof(rfc_8032_7_2[0]); ++n) {
+   for (n = 0; n < LTC_ARRAY_SIZE(rfc_8032_7_2); ++n) {
       slen = sizeof(sec);
       DO(base16_decode(rfc_8032_7_2[n].secret_key, XSTRLEN(rfc_8032_7_2[n].secret_key), sec, &slen));
       plen = sizeof(pub);
@@ -331,7 +331,7 @@ static int s_rfc_8032_7_2_test(void)
 
       DO(ed25519_import_raw(sec, slen, PK_PRIVATE, &key));
       DO(ed25519ctx_sign(msg, mlen, buf, &buflen, ctx, ctxlen, &key));
-      DO(do_compare_testvector(buf, buflen, sig, siglen, "Ed25519 RFC8032 7.2 - sign", n));
+      COMPARE_TESTVECTOR(buf, buflen, sig, siglen, "Ed25519 RFC8032 7.2 - sign", n);
       DO(ed25519ctx_verify(msg, mlen, buf, buflen, ctx, ctxlen, &ret, &key));
       ENSUREX(ret == should, "Ed25519 RFC8032 7.2 - verify w/ privkey");
 
@@ -389,7 +389,7 @@ static int s_rfc_8032_7_3_test(void)
 
    DO(ed25519_import_raw(sec, slen, PK_PRIVATE, &key));
    DO(ed25519ph_sign(msg, mlen, buf, &buflen, NULL, 0, &key));
-   DO(do_compare_testvector(buf, buflen, sig, siglen, "Ed25519 RFC8032 7.3 - sign", 0));
+   COMPARE_TESTVECTOR(buf, buflen, sig, siglen, "Ed25519 RFC8032 7.3 - sign", 0);
    DO(ed25519ph_verify(msg, mlen, buf, buflen, NULL, 0, &ret, &key));
    ENSUREX(ret == should, "Ed25519 RFC8032 7.3 - verify w/ privkey");
 

+ 10 - 10
tests/file_test.c

@@ -33,11 +33,11 @@ int file_test(void)
    err = hash_filehandle(isha256, in, buf, &len);
    fclose(in);
    DO(err);
-   DO(do_compare_testvector(buf, len, exp_sha256, 32, "hash_filehandle", 1));
+   COMPARE_TESTVECTOR(buf, len, exp_sha256, 32, "hash_filehandle", 1);
 
    len = sizeof(buf);
    DO(hash_file(isha256, fname, buf, &len));
-   DO(do_compare_testvector(buf, len, exp_sha256, 32, "hash_file", 1));
+   COMPARE_TESTVECTOR(buf, len, exp_sha256, 32, "hash_file", 1);
 
 #ifdef LTC_HMAC
    {
@@ -45,7 +45,7 @@ int file_test(void)
                                            0x8F, 0x68, 0xCF, 0xBA, 0x2E, 0xAD, 0xC4, 0x63, 0x9D, 0x7F, 0x02, 0x99, 0x8C, 0x08, 0xAC, 0xC0 };
       len = sizeof(buf);
       DO(hmac_file(isha256, fname, key, 32, buf, &len));
-      DO(do_compare_testvector(buf, len, exp_hmacsha256, 32, "hmac_file", 1));
+      COMPARE_TESTVECTOR(buf, len, exp_hmacsha256, 32, "hmac_file", 1);
    }
 #endif
 #ifdef LTC_OMAC
@@ -53,7 +53,7 @@ int file_test(void)
       unsigned char exp_omacaes[16]    = { 0x50, 0xB4, 0x6C, 0x62, 0xE9, 0xCA, 0x48, 0xFC, 0x38, 0x8D, 0xF4, 0xA2, 0x7D, 0x6A, 0x1E, 0xD8 };
       len = sizeof(buf);
       DO(omac_file(iaes, key, 32, fname, buf, &len));
-      DO(do_compare_testvector(buf, len, exp_omacaes, 16, "omac_file", 1));
+      COMPARE_TESTVECTOR(buf, len, exp_omacaes, 16, "omac_file", 1);
    }
 #endif
 #ifdef LTC_PMAC
@@ -61,7 +61,7 @@ int file_test(void)
       unsigned char exp_pmacaes[16]    = { 0x7D, 0x65, 0xF0, 0x75, 0x4F, 0x8D, 0xE2, 0xB0, 0xE4, 0xFA, 0x54, 0x4E, 0x45, 0x01, 0x36, 0x1B };
       len = sizeof(buf);
       DO(pmac_file(iaes, key, 32, fname, buf, &len));
-      DO(do_compare_testvector(buf, len, exp_pmacaes, 16, "pmac_file", 1));
+      COMPARE_TESTVECTOR(buf, len, exp_pmacaes, 16, "pmac_file", 1);
    }
 #endif
 #ifdef LTC_XCBC
@@ -69,7 +69,7 @@ int file_test(void)
       unsigned char exp_xcbcaes[16]    = { 0x9C, 0x73, 0xA2, 0xD7, 0x90, 0xA5, 0x86, 0x25, 0x4D, 0x3C, 0x8A, 0x6A, 0x24, 0x6D, 0xD1, 0xAB };
       len = sizeof(buf);
       DO(xcbc_file(iaes, key, 32, fname, buf, &len));
-      DO(do_compare_testvector(buf, len, exp_xcbcaes, 16, "xcbc_file", 1));
+      COMPARE_TESTVECTOR(buf, len, exp_xcbcaes, 16, "xcbc_file", 1);
    }
 #endif
 #ifdef LTC_F9_MODE
@@ -77,7 +77,7 @@ int file_test(void)
       unsigned char exp_f9aes[16]      = { 0x6B, 0x6A, 0x18, 0x34, 0x13, 0x8E, 0x01, 0xEF, 0x33, 0x8E, 0x7A, 0x3F, 0x5B, 0x9A, 0xA6, 0x7A };
       len = sizeof(buf);
       DO(f9_file(iaes, key, 32, fname, buf, &len));
-      DO(do_compare_testvector(buf, len, exp_f9aes, 16, "f9_file", 1));
+      COMPARE_TESTVECTOR(buf, len, exp_f9aes, 16, "f9_file", 1);
    }
 #endif
 #ifdef LTC_POLY1305
@@ -85,7 +85,7 @@ int file_test(void)
       unsigned char exp_poly1305[16]   = { 0xD0, 0xC7, 0xFB, 0x13, 0xA8, 0x87, 0x84, 0x23, 0x21, 0xCC, 0xA9, 0x43, 0x81, 0x18, 0x75, 0xBE };
       len = sizeof(buf);
       DO(poly1305_file(fname, key, 32, buf, &len));
-      DO(do_compare_testvector(buf, len, exp_poly1305, 16, "poly1305_file", 1));
+      COMPARE_TESTVECTOR(buf, len, exp_poly1305, 16, "poly1305_file", 1);
    }
 #endif
 #ifdef LTC_BLAKE2SMAC
@@ -93,7 +93,7 @@ int file_test(void)
       unsigned char exp_blake2smac[16]   = { 0x4f, 0x94, 0x45, 0x15, 0xcd, 0xd1, 0xca, 0x02, 0x1a, 0x0c, 0x7a, 0xe4, 0x6d, 0x2f, 0xe8, 0xb3 };
       len = 16;
       DO(blake2smac_file(fname, key, 32, buf, &len));
-      DO(do_compare_testvector(buf, len, exp_blake2smac, 16, "exp_blake2smac_file", 1));
+      COMPARE_TESTVECTOR(buf, len, exp_blake2smac, 16, "exp_blake2smac_file", 1);
    }
 #endif
 #ifdef LTC_BLAKE2BMAC
@@ -101,7 +101,7 @@ int file_test(void)
       unsigned char exp_blake2bmac[16]   = { 0xdf, 0x0e, 0x7a, 0xab, 0x96, 0x6b, 0x75, 0x4e, 0x52, 0x6a, 0x43, 0x96, 0xbd, 0xef, 0xab, 0x44 };
       len = 16;
       DO(blake2bmac_file(fname, key, 32, buf, &len));
-      DO(do_compare_testvector(buf, len, exp_blake2bmac, 16, "exp_blake2bmac_file", 1));
+      COMPARE_TESTVECTOR(buf, len, exp_blake2bmac, 16, "exp_blake2bmac_file", 1);
    }
 #endif
 

+ 1 - 0
tests/misc_test.c

@@ -34,6 +34,7 @@ int misc_test(void)
 #ifdef LTC_SSH
    ssh_test();
 #endif
+   pk_oid_test();
    no_null_termination_check_test();
    return 0;
 }

Неке датотеке нису приказане због велике количине промена