[PATCH 4/4] tests: Fix tests to account for differentiated flags

Lucas Mulling via Gcrypt-devel <[email protected]>
Newsgroups gmane.comp.encryption.gpg.libgcrypt.devel
Message-ID <[email protected]>
* tests/basic.c (check_one_cipher, check_pubkey_sign,
check_pubkey_sign_ecdsa): Fix formatting, include test index in fail
print, mark tests that use random-override with FLAG_NOFIPS, as
random-override is not allowed in fips mode.
* tests/pkcs1v2.c (check_oaep, check_pss, check_v15crypt): Skip tests
that use random-override.
* tests/t-dsa.c (one_test_sexp): Skip tests that set the RSA salt in
fips mode. Setting the rsa salt is not allowed in fips mode.
* tests/t-ecdsa.c (one_test_sexp): Skip tests that set the
salt in fips mode. Setting the salt is not allowed in fips mode.
* tests/t-ed25519.c (one_test): Skip tests that set the
salt in fips mode. Setting the salt is not allowed in fips mode.
* tests/t-ed448.c (one_test): Skip tests that set the
salt in fips mode. Setting the salt is not allowed in fips mode.
* tests/t-fips-service-ind.c (check_pk_g_t_n_c,
check_pk_hash_sign_verify): Check for correct differentiation of
igninvflag, the flag itself does nothing but its not allowed in fips
mode.
* tests/t-rsa-pss.c (one_test_sexp): Skip tests that set the RSA salt in
fips mode. Setting the RSA salt is not allowed in fips mode.

Signed-off-by: Lucas Mulling <[email protected]>
---
 tests/basic.c              | 47 +++++++++++++++++++--------------
 tests/pkcs1v2.c            | 23 ++++++++++++++---
 tests/t-dsa.c              |  4 +++
 tests/t-ecdsa.c            |  4 +++
 tests/t-ed25519.c          |  4 +++
 tests/t-ed448.c            |  7 +++++
 tests/t-fips-service-ind.c | 53 +++++++++++++++++++++++++++++---------
 tests/t-rsa-pss.c          |  4 +++
 8 files changed, 112 insertions(+), 34 deletions(-)

diff --git a/tests/basic.c b/tests/basic.c
index f32e41db..5cd464d9 100644
--- a/tests/basic.c
+++ b/tests/basic.c
@@ -12533,7 +12533,7 @@ check_one_cipher (int algo, int mode, int flags)
 					  0, 61))
 	goto out;
     }
-   else if (mode == GCRY_CIPHER_MODE_CTR)
+  else if (mode == GCRY_CIPHER_MODE_CTR)
     {
       /* 32bit overflow test (big-endian counter) */
       if (check_one_cipher_ctr_overflow (algo, mode, flags, key, 64, plain,
@@ -17215,7 +17215,7 @@ check_pubkey_sign (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo,
 	" (hash sha1 #11223344556677889900AABBCCDDEEFF10203040#)\n"
         " (random-override #4253647587980912233445566778899019283747#))\n",
 	GCRY_PK_RSA,
-	0 },
+	0, FLAG_NOFIPS },
       { "(data\n (flags pss)\n"
 	" (hash-algo sha1)\n"
 	" (value #11223344556677889900AA#))\n",
@@ -17226,7 +17226,7 @@ check_pubkey_sign (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo,
 	" (value #11223344556677889900AA#)\n"
         " (random-override #4253647587980912233445566778899019283747#))\n",
 	GCRY_PK_RSA,
-	0 },
+	0, FLAG_NOFIPS},
       { "(data\n (flags pss)\n"
 	" (hash-algo sha256)\n"
 	" (value #11223344556677889900AABBCCDDEEFF#)\n"
@@ -17234,7 +17234,7 @@ check_pubkey_sign (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo,
         " (random-override #42536475879809122334455667788990192837465564738291"
                            "00122334455667#))\n",
 	GCRY_PK_RSA,
-	0 },
+	0, FLAG_NOFIPS },
       { "(data\n (flags pss)\n"
 	" (hash-algo sha256)\n"
 	" (value #11223344556677889900AABBCCDDEEFF#)\n"
@@ -17273,7 +17273,11 @@ check_pubkey_sign (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo,
           goto next;
         }
       if (gcry_err_code (rc) != datas[dataidx].expected_rc)
-	fail ("gcry_pk_sign %d failed: %s\n", dataidx, gpg_strerror (rc));
+        {
+          show_sexp("hash: ", hash);
+          show_sexp("skey: ", skey);
+	      fail ("gcry_pk_sign %d failed: %s\n", dataidx, gpg_strerror (rc));
+        }
 
       if (!rc)
 	verify_one_signature (pkey, hash, badhash, sig);
@@ -17402,7 +17406,7 @@ check_pubkey_sign_ecdsa (int n, gcry_sexp_t skey, gcry_sexp_t pkey,
           goto next;
         }
       if (gcry_err_code (rc) != datas[dataidx].expected_rc)
-	fail ("gcry_pk_sign failed: %s\n", gpg_strerror (rc));
+	fail ("gcry_pk_sign (%d) failed: %s\n", dataidx, gpg_strerror (rc));
 
       if (!rc && verbose > 1)
         show_sexp ("ECDSA signature:\n", sig);
@@ -17483,7 +17487,7 @@ check_pubkey_crypt (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo,
 	"(flags oaep)(hash-algo sha1)(label \"test\")",
 	1,
 	0,
-	0 },
+	0, FLAG_NOFIPS },
       {	0,
         "(data\n (flags )\n" " (value #11223344556677889900AA#))\n",
 	NULL,
@@ -17556,14 +17560,18 @@ check_pubkey_crypt (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo,
 	die ("converting data failed: %s\n", gpg_strerror (rc));
 
       rc = gcry_pk_encrypt (&ciph, data, pkey);
-      if (in_fips_mode && (flags & FLAG_NOFIPS))
+      if (in_fips_mode && (flags & FLAG_NOFIPS || datas[dataidx].flags & FLAG_NOFIPS))
         {
           if (!rc)
             fail ("gcry_pk_encrypt did not fail as expected in FIPS mode\n");
           goto next;
         }
       if (gcry_err_code (rc) != datas[dataidx].encrypt_expected_rc)
-	fail ("gcry_pk_encrypt failed: %s\n", gpg_strerror (rc));
+        {
+          show_sexp("data: ", data);
+          show_sexp("pkey: ", pkey);
+          fail ("gcry_pk_encrypt (%d) failed: %s\n", flags, gpg_strerror (rc));
+        }
 
       if (!rc)
 	{
@@ -17605,16 +17613,17 @@ check_pubkey_crypt (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo,
 	      ciph = list;
 	    }
 	  rc = gcry_pk_decrypt (&plain, ciph, skey);
-          if (!rc && (datas[dataidx].flags & FLAG_SPECIAL))
-            {
-              /* It may happen that OAEP formatted data which is
-                 decrypted as pkcs#1 data returns a valid pkcs#1
-                 frame.  However, the returned value will not be
-                 identical - thus we expect a mismatch and test further on
-                 whether this mismatch actually happened.  */
-              expect_mismatch = 1;
-            }
-	  else if (gcry_err_code (rc) != datas[dataidx].decrypt_expected_rc)
+
+      if (!rc && (datas[dataidx].flags & FLAG_SPECIAL))
+        {
+          /* It may happen that OAEP formatted data which is
+             decrypted as pkcs#1 data returns a valid pkcs#1
+             frame.  However, the returned value will not be
+             identical - thus we expect a mismatch and test further on
+             whether this mismatch actually happened.  */
+          expect_mismatch = 1;
+        }
+      else if (gcry_err_code (rc) != datas[dataidx].decrypt_expected_rc)
             {
               if (verbose)
                 {
diff --git a/tests/pkcs1v2.c b/tests/pkcs1v2.c
index 9874b81c..25156d65 100644
--- a/tests/pkcs1v2.c
+++ b/tests/pkcs1v2.c
@@ -186,8 +186,15 @@ check_oaep (void)
           err = gcry_pk_encrypt (&ciph, plain, pub_key);
           if (err)
             {
-              show_sexp ("plain:\n", ciph);
-              fail ("gcry_pk_encrypt failed: %s\n", gpg_strerror (err));
+              /* SKIP the test in fips mode, random-override is not allowed */
+              if (in_fips_mode && gcry_err_code (err) == GPG_ERR_INV_FLAG)
+                {
+                  gcry_sexp_release (plain);
+                  continue;
+                }
+              show_sexp ("plain:\n", plain);
+              show_sexp ("pub_key:\n", pub_key);
+              fail ("gcry_pk_encrypt failed: (%d) %s\n", mno, gpg_strerror (err));
             }
           else
             {
@@ -320,6 +327,11 @@ check_pss (void)
           err = gcry_pk_sign (&sig, sigtmpl, sec_key);
           if (err)
             {
+              if (in_fips_mode && gcry_err_code (err) == GPG_ERR_INV_FLAG)
+                {
+                  gcry_sexp_release (sigtmpl);
+                  continue;
+                }
               show_sexp ("sigtmpl:\n", sigtmpl);
               fail ("gcry_pk_sign failed: %s\n", gpg_strerror (err));
             }
@@ -448,7 +460,12 @@ check_v15crypt (void)
           err = gcry_pk_encrypt (&ciph, plain, pub_key);
           if (err)
             {
-              show_sexp ("plain:\n", ciph);
+              if (in_fips_mode && gcry_err_code (err) == GPG_ERR_INV_FLAG)
+                {
+                  gcry_sexp_release (plain);
+                  continue;
+                }
+              show_sexp ("plain:\n", plain);
               fail ("gcry_pk_encrypt failed: %s\n", gpg_strerror (err));
             }
           else
diff --git a/tests/t-dsa.c b/tests/t-dsa.c
index 8ed3b65b..3f8ad97e 100644
--- a/tests/t-dsa.c
+++ b/tests/t-dsa.c
@@ -322,6 +322,10 @@ one_test_sexp (const char *sha_alg,
   err = gcry_pk_random_override_new (&ctx, buffer2, buflen2);
   if (err)
     {
+      /* Setting rsa salt is not allowed in FIPS mode */
+      if (in_fips_mode && gpg_err_code (err) == GPG_ERR_INV_OP)
+          goto leave;
+
       fail ("error setting salt for test: %s",
 	    gpg_strerror (err));
       goto leave;
diff --git a/tests/t-ecdsa.c b/tests/t-ecdsa.c
index d36d217b..73f283ac 100644
--- a/tests/t-ecdsa.c
+++ b/tests/t-ecdsa.c
@@ -357,6 +357,10 @@ one_test_sexp (const char *curvename, const char *sha_alg,
   err = gcry_pk_random_override_new (&ctx, buffer2, buflen2);
   if (err)
     {
+      /* Setting salt is not allowed in FIPS mode */
+      if (in_fips_mode && gpg_err_code (err) == GPG_ERR_INV_OP)
+          goto leave;
+
       fail ("error setting salt for test: %s",
 	    gpg_strerror (err));
       goto leave;
diff --git a/tests/t-ed25519.c b/tests/t-ed25519.c
index ce828b27..3b78c365 100644
--- a/tests/t-ed25519.c
+++ b/tests/t-ed25519.c
@@ -265,6 +265,10 @@ one_test (int testno, const char *sk, const char *pk,
   err = gcry_pk_input_data_push (&ctx, buffer, buflen);
   if (err)
     {
+      /* Setting salt is not allowed in FIPS mode */
+      if (!no_fips && gpg_err_code (err) == GPG_ERR_INV_OP)
+          goto leave;
+
       fail ("error setting input data for test: %s",
 	    gpg_strerror (err));
       goto leave;
diff --git a/tests/t-ed448.c b/tests/t-ed448.c
index b68fd855..0761c9a0 100644
--- a/tests/t-ed448.c
+++ b/tests/t-ed448.c
@@ -37,6 +37,7 @@ static int sign_with_pk;
 static int no_verify;
 static int custom_data_file;
 
+static int in_fips_mode;
 
 static void
 show_note (const char *format, ...)
@@ -261,6 +262,9 @@ one_test (int testno, int ph, const char *sk, const char *pk,
   err = gcry_pk_input_data_push (&ctx, buffer, buflen);
   if (err)
     {
+      /* Setting salt is not allowed in FIPS mode */
+      if (in_fips_mode && gpg_err_code (err) == GPG_ERR_INV_OP)
+          goto leave;
       fail ("error setting input data for test: %s",
             gpg_strerror (err));
       goto leave;
@@ -517,6 +521,9 @@ main (int argc, char **argv)
   xgcry_control ((GCRYCTL_ENABLE_QUICK_RANDOM, 0));
   xgcry_control ((GCRYCTL_INITIALIZATION_FINISHED, 0));
 
+  if (gcry_fips_mode_active ())
+    in_fips_mode = 1;
+
   start_timer ();
   check_ed448 (fname);
   stop_timer ();
diff --git a/tests/t-fips-service-ind.c b/tests/t-fips-service-ind.c
index f8485176..95807783 100644
--- a/tests/t-fips-service-ind.c
+++ b/tests/t-fips-service-ind.c
@@ -47,6 +47,9 @@ check_pk_g_t_n_c (int reject)
   static struct {
     const char *keyparms;
     int expect_failure;
+    int expect_failure_testkey;
+    int expect_failure_nbits;
+    int expect_failure_curve;
   } tv[] = {
     {
       "(genkey (ecc (curve nistp256)))",
@@ -54,8 +57,12 @@ check_pk_g_t_n_c (int reject)
     },
     {                           /* non-compliant curve */
       "(genkey (ecc (curve secp256k1)))",
-      1
-    }
+      1, 1, 1, 1
+    },
+    {                           /* non-compliant igninvflag */
+      "(genkey (ecc (curve nistp256) (flags igninvflag)))",
+      1, 0, 0, 0
+    },
   };
   int tvidx;
   gpg_error_t err;
@@ -138,14 +145,14 @@ check_pk_g_t_n_c (int reject)
           goto next;
         }
 
-      if (in_fips_mode && !tv[tvidx].expect_failure && ec)
+      if (in_fips_mode && !tv[tvidx].expect_failure_testkey && ec)
         {
           /* Success with the FIPS service indicator == 0 expected, but != 0.  */
           fail ("gcry_pk_testkey test %d unexpectedly set the indicator in FIPS mode.\n",
                 tvidx);
           goto next;
         }
-      else if (in_fips_mode && tv[tvidx].expect_failure && !ec)
+      else if (in_fips_mode && tv[tvidx].expect_failure_testkey && !ec)
         {
           /* Success with the FIPS service indicator != 0 expected, but == 0.  */
           fail ("gcry_pk_testkey test %d unexpectedly cleared the indicator in FIPS mode.\n",
@@ -169,14 +176,14 @@ check_pk_g_t_n_c (int reject)
           goto next;
         }
 
-      if (in_fips_mode && !tv[tvidx].expect_failure && ec)
+      if (in_fips_mode && !tv[tvidx].expect_failure_nbits && ec)
         {
           /* Success with the FIPS service indicator == 0 expected, but != 0.  */
           fail ("gcry_pk_get_nbits test %d unexpectedly set the indicator in FIPS mode.\n",
                 tvidx);
           goto next;
         }
-      else if (in_fips_mode && tv[tvidx].expect_failure && !ec)
+      else if (in_fips_mode && tv[tvidx].expect_failure_nbits && !ec)
         {
           /* Success with the FIPS service indicator != 0 expected, but == 0.  */
           fail ("gcry_pk_get_nbits test %d unexpectedly cleared the indicator in FIPS mode.\n",
@@ -200,14 +207,14 @@ check_pk_g_t_n_c (int reject)
           goto next;
         }
 
-      if (in_fips_mode && !tv[tvidx].expect_failure && ec)
+      if (in_fips_mode && !tv[tvidx].expect_failure_curve && ec)
         {
           /* Success with the FIPS service indicator == 0 expected, but != 0.  */
           fail ("gcry_pk_get_curve test %d unexpectedly set the indicator in FIPS mode.\n",
                 tvidx);
           goto next;
         }
-      else if (in_fips_mode && tv[tvidx].expect_failure && !ec)
+      else if (in_fips_mode && tv[tvidx].expect_failure_curve && !ec)
         {
           /* Success with the FIPS service indicator != 0 expected, but == 0.  */
           fail ("gcry_pk_get_curve test %d unexpectedly cleared the indicator in FIPS mode.\n",
@@ -404,6 +411,7 @@ check_pk_hash_sign_verify (void)
     const char *k;
     int expect_failure;
     int expect_failure_hash;
+    int expect_failure_verify;
   } tv[] = {
     {                           /* non-compliant hash */
       GCRY_MD_BLAKE2B_512,
@@ -414,18 +422,28 @@ check_pk_hash_sign_verify (void)
       "ce4014c68811f9a21a1fdb2c0e6113e06db7ca93b7404e78dc7ccd5ca89a4ca9#)))",
       "(data(flags raw)(hash %s %b)(label %b))",
       "94a1bbb14b906a61a280f245f9e93c7f3b4a6247824f5d33b9670787642a68de",
-      1, 1
+      1, 1, 1
     },
     {                           /* non-compliant curve */
       GCRY_MD_SHA256,
       "(private-key (ecc (curve secp256k1)"
       " (d #c2cdf0a8b0a83b35ace53f097b5e6e6a0a1f2d40535eff1cf434f52a43d59d8f#)))",
-
       "(public-key (ecc (curve secp256k1)"
       " (q #046fcc37ea5e9e09fec6c83e5fbd7a745e3eee81d16ebd861c9e66f55518c19798"
       "4e9f113c07f875691df8afc1029496fc4cb9509b39dcd38f251a83359cc8b4f7#)))",
       "(data(flags raw)(hash %s %b)(label %b))",
       "94a1bbb14b906a61a280f245f9e93c7f3b4a6247824f5d33b9670787642a68de",
+      1, 0, 1
+    },
+    {                           /* non-compliant igninvflag */
+      GCRY_MD_SHA256,
+      "(private-key (ecc (curve nistp256) (flags igninvflag)"
+      " (d #519b423d715f8b581f4fa8ee59f4771a5b44c8130b4e3eacca54a56dda72b464#)))",
+      "(public-key (ecc (curve nistp256)"
+      " (q #041ccbe91c075fc7f4f033bfa248db8fccd3565de94bbfb12f3c59ff46c271bf83"
+      "ce4014c68811f9a21a1fdb2c0e6113e06db7ca93b7404e78dc7ccd5ca89a4ca9#)))",
+      "(data(flags raw)(hash %s %b)(label %b))",
+      "94a1bbb14b906a61a280f245f9e93c7f3b4a6247824f5d33b9670787642a68de",
       1, 0
     },
     {
@@ -438,6 +456,17 @@ check_pk_hash_sign_verify (void)
       "(data(flags raw)(hash %s %b)(label %b))",
       "94a1bbb14b906a61a280f245f9e93c7f3b4a6247824f5d33b9670787642a68de",
       0, 0
+    },
+    {
+      GCRY_MD_GOSTR3411_94,     /* non-compliant hash */
+      "(private-key (ecc (curve nistp256)"
+      " (d #519b423d715f8b581f4fa8ee59f4771a5b44c8130b4e3eacca54a56dda72b464#)))",
+      "(public-key (ecc (curve nistp256)"
+      " (q #041ccbe91c075fc7f4f033bfa248db8fccd3565de94bbfb12f3c59ff46c271bf83"
+      "ce4014c68811f9a21a1fdb2c0e6113e06db7ca93b7404e78dc7ccd5ca89a4ca9#)))",
+      "(data(flags raw)(hash %s %b)(label %b))",
+      "94a1bbb14b906a61a280f245f9e93c7f3b4a6247824f5d33b9670787642a68de",
+      1, 1, 1
     }
   };
   int tvidx;
@@ -573,14 +602,14 @@ check_pk_hash_sign_verify (void)
           goto next;
         }
 
-      if (in_fips_mode && !tv[tvidx].expect_failure && ec)
+      if (in_fips_mode && !tv[tvidx].expect_failure_verify && ec)
         {
           /* Success with the FIPS service indicator == 0 expected, but != 0.  */
           fail ("gcry_pk_hash test %d unexpectedly set the indicator in FIPS mode.\n",
                 tvidx);
           goto next;
         }
-      else if (in_fips_mode && tv[tvidx].expect_failure && !ec)
+      else if (in_fips_mode && tv[tvidx].expect_failure_verify && !ec)
         {
           /* Success with the FIPS service indicator != 0 expected, but == 0.  */
           fail ("gcry_pk_hash_verify test %d unexpectedly cleared the indicator in FIPS mode.\n",
diff --git a/tests/t-rsa-pss.c b/tests/t-rsa-pss.c
index 82dd54b3..2206c291 100644
--- a/tests/t-rsa-pss.c
+++ b/tests/t-rsa-pss.c
@@ -283,6 +283,10 @@ one_test_sexp (const char *n, const char *e, const char *d,
       err = gcry_pk_random_override_new (&ctx, buffer2, buflen2);
       if (err)
         {
+          /* Setting rsa salt is not allowed in FIPS mode */
+          if (in_fips_mode && gpg_err_code (err) == GPG_ERR_INV_OP)
+              goto leave;
+
           fail ("error setting salt for test: %s",
                 gpg_strerror (err));
           goto leave;
-- 
2.48.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.