Browse Source

use do_compare_testvector() where possible

Steffen Jaeckel 7 years ago
parent
commit
b026c23b0a
7 changed files with 45 additions and 131 deletions
  1. 3 9
      tests/base32_test.c
  2. 5 13
      tests/base64_test.c
  3. 1 1
      tests/der_test.c
  4. 11 55
      tests/dh_test.c
  5. 14 28
      tests/dsa_test.c
  6. 4 4
      tests/mpi_test.c
  7. 7 21
      tests/rsa_test.c

+ 3 - 9
tests/base32_test.c

@@ -37,23 +37,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]));
-         if (compare_testvector(tmp, l2, in, x, "random base32", idx * 100 + x)) {
-            return CRYPT_FAIL_TESTVECTOR;
-         }
+         DO(do_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]));
-      if (compare_testvector(out, l1, testout[idx], strlen(testout[idx]), "testout base32", idx)) {
-         return CRYPT_FAIL_TESTVECTOR;
-      }
+      DO(do_compare_testvector(out, l1, testout[idx], strlen(testout[idx]), "testout base32", idx));
       l2 = sizeof(tmp);
       DO(base32_decode(out, l1, tmp, &l2, testid[idx]));
-      if (compare_testvector(tmp, l2, testin, sizeof(testin), "testin base32", idx)) {
-         return CRYPT_FAIL_TESTVECTOR;
-      }
+      DO(do_compare_testvector(tmp, l2, testin, sizeof(testin), "testin base32", idx));
    }
 
    return CRYPT_OK;

+ 5 - 13
tests/base64_test.c

@@ -67,18 +67,14 @@ int base64_test(void)
           DO(base64url_strict_decode((unsigned char*)url_cases[x].s, slen1, out, &l1));
        else
           DO(base64url_decode((unsigned char*)url_cases[x].s, slen1, out, &l1));
-       if (compare_testvector(out, l1, special_case, sizeof(special_case) - 1, "base64url decode", x)) {
-           return 1;
-       }
+       DO(do_compare_testvector(out, l1, special_case, sizeof(special_case) - 1, "base64url decode", x));
        if(x < 2) {
           l2 = sizeof(tmp);
           if(x == 0)
              DO(base64url_encode(out, l1, tmp, &l2));
           else
              DO(base64url_strict_encode(out, l1, tmp, &l2));
-          if (compare_testvector(tmp, l2, url_cases[x].s, strlen(url_cases[x].s), "base64url encode", x)) {
-              return 1;
-          }
+          DO(do_compare_testvector(tmp, l2, url_cases[x].s, strlen(url_cases[x].s), "base64url encode", x));
        }
    }
 
@@ -94,10 +90,8 @@ int base64_test(void)
        DO(base64_encode((unsigned char*)cases[x].s, slen1, out, &l1));
        l2 = sizeof(tmp);
        DO(base64_strict_decode(out, l1, tmp, &l2));
-       if (compare_testvector(out, l1, cases[x].b64, strlen(cases[x].b64), "base64 encode", x) ||
-             compare_testvector(tmp, l2, cases[x].s, slen1, "base64 decode", x)) {
-           return 1;
-       }
+       DO(do_compare_testvector(out, l1, cases[x].b64, strlen(cases[x].b64), "base64 encode", x));
+       DO(do_compare_testvector(tmp, l2, cases[x].s, slen1, "base64 decode", x));
    }
 
    for  (x = 0; x < 64; x++) {
@@ -106,9 +100,7 @@ int base64_test(void)
        DO(base64_encode(in, x, out, &l1));
        l2 = sizeof(tmp);
        DO(base64_decode(out, l1, tmp, &l2));
-       if (compare_testvector(tmp, x, in, x, "random base64", x)) {
-           return 1;
-       }
+       DO(do_compare_testvector(tmp, x, in, x, "random base64", x));
    }
 
    x--;

+ 1 - 1
tests/der_test.c

@@ -1238,7 +1238,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(compare_testvector(d2, (l2/x->factor) * x->type_sz, x->in, x->in_sz, x->what, __LINE__) == 0 ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR);
+   DO(do_compare_testvector(d2, (l2/x->factor) * x->type_sz, x->in, x->in_sz, x->what, __LINE__));
    XFREE(d2);
    XFREE(d1);
 }

+ 11 - 55
tests/dh_test.c

@@ -134,11 +134,7 @@ static int _dhparam_test(void)
       return CRYPT_ERROR;
    }
    DO(mp_to_unsigned_bin(k.prime, buf));
-   if (compare_testvector(buf, sizeof(prime), prime, sizeof(prime), "dhparam_test", 1)) {
-      printf("dhparam_test: prime mismatch\n");
-      dh_free(&k);
-      return CRYPT_ERROR;
-   }
+   DO(do_compare_testvector(buf, sizeof(prime), prime, sizeof(prime), "dhparam_test: prime mismatch", 1));
    if (mp_cmp_d(k.base, 2) != LTC_MP_EQ) {
       printf("dhparam_test: base mismatch\n");
       dh_free(&k);
@@ -272,32 +268,16 @@ static int _set_test(void)
 
       len = sizeof(buf);
       DO(dh_export(buf, &len, PK_PRIVATE, &k1));
-      if (compare_testvector(buf, len, export_private, sizeof(export_private), "radix_test", i*10 + 0)) {
-         printf("radix_test: dh_export+PK_PRIVATE mismatch\n");
-         dh_free(&k1);
-         return CRYPT_ERROR;
-      }
+      DO(do_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));
-      if (compare_testvector(buf, len, export_public, sizeof(export_public), "radix_test", i*10 + 1)) {
-         printf("radix_test: dh_export+PK_PUBLIC mismatch\n");
-         dh_free(&k1);
-         return CRYPT_ERROR;
-      }
+      DO(do_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));
-      if (compare_testvector(buf, len, xbin, sizeof(xbin), "radix_test", i*10 + 2)) {
-         printf("radix_test: dh_export+PK_PRIVATE mismatch\n");
-         dh_free(&k1);
-         return CRYPT_ERROR;
-      }
+      DO(do_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));
-      if (compare_testvector(buf, len, ybin, sizeof(ybin), "radix_test", i*10 + 3)) {
-         printf("radix_test: dh_export+PK_PUBLIC mismatch\n");
-         dh_free(&k1);
-         return CRYPT_ERROR;
-      }
+      DO(do_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));
@@ -305,18 +285,10 @@ static int _set_test(void)
 
       len = sizeof(buf);
       DO(dh_export(buf, &len, PK_PRIVATE, &k1));
-      if (compare_testvector(buf, len, export_private, sizeof(export_private), "radix_test", i*10 + 4)) {
-         printf("radix_test: dh_export+PK_PRIVATE mismatch\n");
-         dh_free(&k1);
-         return CRYPT_ERROR;
-      }
+      DO(do_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));
-      if (compare_testvector(buf, len, export_public, sizeof(export_public), "radix_test", i*10 + 5)) {
-         printf("radix_test: dh_export+PK_PUBLIC mismatch\n");
-         dh_free(&k1);
-         return CRYPT_ERROR;
-      }
+      DO(do_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));
@@ -324,18 +296,10 @@ static int _set_test(void)
 
       len = sizeof(buf);
       DO(dh_export(buf, &len, PK_PUBLIC, &k2));
-      if (compare_testvector(buf, len, export_public, sizeof(export_public), "radix_test", i*10 + 6)) {
-         printf("radix_test: dh_export+PK_PUBLIC mismatch\n");
-         dh_free(&k2);
-         return CRYPT_ERROR;
-      }
+      DO(do_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));
-      if (compare_testvector(buf, len, ybin, sizeof(ybin), "radix_test", i*10 + 7)) {
-         printf("radix_test: dh_export+PK_PUBLIC mismatch\n");
-         dh_free(&k2);
-         return CRYPT_ERROR;
-      }
+      DO(do_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));
@@ -343,18 +307,10 @@ static int _set_test(void)
 
       len = mp_unsigned_bin_size(k3.prime);
       DO(mp_to_unsigned_bin(k3.prime, buf));
-      if (compare_testvector(buf, len, pbin, sizeof(pbin), "radix_test", i*10 + 8)) {
-         printf("radix_test: dh_make_key_ex prime mismatch\n");
-         dh_free(&k3);
-         return CRYPT_ERROR;
-      }
+      DO(do_compare_testvector(buf, len, pbin, sizeof(pbin), "radix_test: dh_make_key_ex prime mismatch", i*10 + 8));
       len = mp_unsigned_bin_size(k3.base);
       DO(mp_to_unsigned_bin(k3.base, buf));
-      if (compare_testvector(buf, len, gbin, sizeof(gbin), "radix_test", i*10 + 9)) {
-         printf("radix_test: dh_make_key_ex base mismatch\n");
-         dh_free(&k3);
-         return CRYPT_ERROR;
-      }
+      DO(do_compare_testvector(buf, len, gbin, sizeof(gbin), "radix_test: dh_make_key_ex base mismatch", i*10 + 9));
       dh_free(&k3);
    }
 

+ 14 - 28
tests/dsa_test.c

@@ -142,27 +142,21 @@ static int _dsa_compat_test(void)
 
   x = sizeof(tmp);
   DO(dsa_export(tmp, &x, PK_PRIVATE | PK_STD, &key));
-  if (compare_testvector(tmp, x, openssl_priv_dsa, sizeof(openssl_priv_dsa),
-                         "DSA private export failed from dsa_import(priv_key)\n", __LINE__)) {
-     return CRYPT_FAIL_TESTVECTOR;
-  }
+  DO(do_compare_testvector(tmp, x, openssl_priv_dsa, sizeof(openssl_priv_dsa),
+                         "DSA private export failed from dsa_import(priv_key)\n", __LINE__));
 
   x = sizeof(tmp);
   DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
-  if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
-                         "DSA public export failed from dsa_import(priv_key)\n", __LINE__)) {
-     return CRYPT_FAIL_TESTVECTOR;
-  }
+  DO(do_compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
+                         "DSA public export failed 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));
-  if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
-                         "DSA public export failed from dsa_import(pub_key)\n", __LINE__)) {
-     return CRYPT_FAIL_TESTVECTOR;
-  }
+  DO(do_compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
+                         "DSA public export failed from dsa_import(pub_key)\n", __LINE__));
   dsa_free(&key);
 
   /* try import private key from raw hexadecimal numbers */
@@ -184,10 +178,8 @@ static int _dsa_compat_test(void)
                  &key));
   len = sizeof(buf);
   DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
-  if (compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
-                         "DSA private export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__)) {
-     return CRYPT_FAIL_TESTVECTOR;
-  }
+  DO(do_compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
+                         "DSA private export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__));
   dsa_free(&key);
 
   /* try import public key from raw hexadecimal numbers */
@@ -200,10 +192,8 @@ static int _dsa_compat_test(void)
                  &key));
   len = sizeof(buf);
   DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
-  if (compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
-                         "DSA public export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__)) {
-     return CRYPT_FAIL_TESTVECTOR;
-  }
+  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_free(&key);
 
   /* try import dsaparam */
@@ -224,10 +214,8 @@ static int _dsa_compat_test(void)
                  &key));
   len = sizeof(buf);
   DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
-  if (compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
-                         "DSA public export failed from dsa_set_pqg_dsaparam()\n", __LINE__)) {
-     return CRYPT_FAIL_TESTVECTOR;
-  }
+  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_free(&key);
 
   /* try import dsaparam - our private key */
@@ -237,10 +225,8 @@ static int _dsa_compat_test(void)
                  &key));
   len = sizeof(buf);
   DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
-  if (compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
-                         "DSA private export failed from dsa_set_pqg_dsaparam()\n", __LINE__)) {
-     return CRYPT_FAIL_TESTVECTOR;
-  }
+  DO(do_compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
+                         "DSA private export failed from dsa_set_pqg_dsaparam()\n", __LINE__));
   dsa_free(&key);
 
   return CRYPT_OK;

+ 4 - 4
tests/mpi_test.c

@@ -123,10 +123,10 @@ static int _radix_to_bin_test(void)
       DO(radix_to_bin(test[i].p, test[i].radix, key_parts[2], &key_lens[2]));
       DO(radix_to_bin(test[i].g, test[i].radix, key_parts[3], &key_lens[3]));
 
-      if (compare_testvector(key_parts[0], key_lens[0], test[0].x, test[0].xlen, "radix_to_bin(x)", i)) return CRYPT_FAIL_TESTVECTOR;
-      if (compare_testvector(key_parts[1], key_lens[1], test[0].y, test[0].ylen, "radix_to_bin(y)", i)) return CRYPT_FAIL_TESTVECTOR;
-      if (compare_testvector(key_parts[2], key_lens[2], test[0].p, test[0].plen, "radix_to_bin(p)", i)) return CRYPT_FAIL_TESTVECTOR;
-      if (compare_testvector(key_parts[3], key_lens[3], test[0].g, test[0].glen, "radix_to_bin(g)", i)) return CRYPT_FAIL_TESTVECTOR;
+      DO(do_compare_testvector(key_parts[0], key_lens[0], test[0].x, test[0].xlen, "radix_to_bin(x)", i));
+      DO(do_compare_testvector(key_parts[1], key_lens[1], test[0].y, test[0].ylen, "radix_to_bin(y)", i));
+      DO(do_compare_testvector(key_parts[2], key_lens[2], test[0].p, test[0].plen, "radix_to_bin(p)", i));
+      DO(do_compare_testvector(key_parts[3], key_lens[3], test[0].g, test[0].glen, "radix_to_bin(g)", i));
    }
    return CRYPT_OK;
 }

+ 7 - 21
tests/rsa_test.c

@@ -207,42 +207,32 @@ static int rsa_compat_test(void)
    /* now try to export private/public and compare */
    len = sizeof(buf);
    DO(rsa_export(buf, &len, PK_PRIVATE, &key));
-   if (compare_testvector(buf, len, openssl_private_rsa, sizeof(openssl_private_rsa), "RSA private export (from OpenSSL)", 0)) {
-      return 1;
-   }
+   DO(do_compare_testvector(buf, len, openssl_private_rsa, sizeof(openssl_private_rsa), "RSA private export (from OpenSSL)", 0));
 
    len = sizeof(buf);
    DO(rsa_export(buf, &len, PK_PUBLIC, &key));
-   if (compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from OpenSSL private key)", 0)) {
-      return 1;
-   }
+   DO(do_compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from OpenSSL private key)", 0));
    rsa_free(&key);
 
    /* try reading the public key */
    DO(rsa_import(openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), &key));
    len = sizeof(buf);
    DO(rsa_export(buf, &len, PK_PUBLIC, &key));
-   if (compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from stripped OpenSSL)", 0)) {
-      return 1;
-   }
+   DO(do_compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from stripped OpenSSL)", 0));
    rsa_free(&key);
 
    /* try reading the public key */
    DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
    len = sizeof(buf);
    DO(rsa_export(buf, &len, PK_PUBLIC, &key));
-   if (compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from OpenSSL)", 0)) {
-      return 1;
-   }
+   DO(do_compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from OpenSSL)", 0));
    rsa_free(&key);
 
    /* try import private key in pkcs8 format */
    DO(rsa_import_pkcs8(pkcs8_private_rsa, sizeof(pkcs8_private_rsa), NULL, 0, &key));
    len = sizeof(buf);
    DO(rsa_export(buf, &len, PK_PRIVATE, &key));
-   if (compare_testvector(buf, len, openssl_private_rsa, sizeof(openssl_private_rsa), "RSA private export (from PKCS#8)", 0)) {
-      return 1;
-   }
+   DO(do_compare_testvector(buf, len, openssl_private_rsa, sizeof(openssl_private_rsa), "RSA private export (from PKCS#8)", 0));
    rsa_free(&key);
 
    /* convert raw hexadecimal numbers to binary */
@@ -256,18 +246,14 @@ static int rsa_compat_test(void)
    DO(rsa_set_crt_params(key_parts[pk_dP], key_lens[pk_dP], key_parts[pk_dQ], key_lens[pk_dQ], key_parts[pk_qP], key_lens[pk_qP], &key));
    len = sizeof(buf);
    DO(rsa_export(buf, &len, PK_PRIVATE, &key));
-   if (compare_testvector(buf, len, openssl_private_rsa, sizeof(openssl_private_rsa), "RSA private export (from hex)", 0)) {
-      return 1;
-   }
+   DO(do_compare_testvector(buf, len, openssl_private_rsa, sizeof(openssl_private_rsa), "RSA private export (from hex)", 0));
    rsa_free(&key);
 
    /* try import public key from converted raw hexadecimal numbers */
    DO(rsa_set_key(key_parts[pk_N], key_lens[pk_N], key_parts[pk_e], key_lens[pk_e], NULL, 0, &key));
    len = sizeof(buf);
    DO(rsa_export(buf, &len, PK_PUBLIC, &key));
-   if (compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from hex)", 0)) {
-      return 1;
-   }
+   DO(do_compare_testvector(buf, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export (from hex)", 0));
    rsa_free(&key);
 
    /* try export in SubjectPublicKeyInfo format of the public key */