Blame SOURCES/libgcrypt-1.8.3-cmac-selftest.patch

5bcc82
diff -up libgcrypt-1.8.3/cipher/cipher-cmac.c.cmac-selftest libgcrypt-1.8.3/cipher/cipher-cmac.c
5bcc82
--- libgcrypt-1.8.3/cipher/cipher-cmac.c.cmac-selftest	2017-11-23 19:16:58.000000000 +0100
5bcc82
+++ libgcrypt-1.8.3/cipher/cipher-cmac.c	2019-05-31 17:33:35.594407152 +0200
5bcc82
@@ -251,3 +251,246 @@ _gcry_cipher_cmac_set_subkeys (gcry_ciph
5bcc82
 
5bcc82
   return GPG_ERR_NO_ERROR;
5bcc82
 }
5bcc82
+
5bcc82
+/* CMAC selftests.
5bcc82
+ * Copyright (C) 2008 Free Software Foundation, Inc.
5bcc82
+ * Copyright (C) 2019 Red Hat, Inc.
5bcc82
+ */
5bcc82
+
5bcc82
+
5bcc82
+
5bcc82
+/* Check one MAC with MAC ALGO using the regular MAC
5bcc82
+ * API. (DATA,DATALEN) is the data to be MACed, (KEY,KEYLEN) the key
5bcc82
+ * and (EXPECT,EXPECTLEN) the expected result.  If TRUNC is set, the
5bcc82
+ * EXPECTLEN may be less than the digest length.  Returns NULL on
5bcc82
+ * success or a string describing the failure.  */
5bcc82
+static const char *
5bcc82
+check_one (int algo,
5bcc82
+           const void *data, size_t datalen,
5bcc82
+           const void *key, size_t keylen,
5bcc82
+           const void *expect, size_t expectlen)
5bcc82
+{
5bcc82
+  gcry_mac_hd_t hd;
5bcc82
+  unsigned char mac[512]; /* hardcoded to avoid allocation */
5bcc82
+  size_t macoutlen = expectlen;
5bcc82
+
5bcc82
+/*   printf ("MAC algo %d\n", algo); */
5bcc82
+  if (_gcry_mac_get_algo_maclen (algo) != expectlen ||
5bcc82
+      expectlen > sizeof (mac))
5bcc82
+    return "invalid tests data";
5bcc82
+  if (_gcry_mac_open (&hd, algo, 0, NULL))
5bcc82
+    return "gcry_mac_open failed";
5bcc82
+  if (_gcry_mac_setkey (hd, key, keylen))
5bcc82
+    {
5bcc82
+      _gcry_mac_close (hd);
5bcc82
+      return "gcry_md_setkey failed";
5bcc82
+    }
5bcc82
+  if (_gcry_mac_write (hd, data, datalen))
5bcc82
+    {
5bcc82
+      _gcry_mac_close (hd);
5bcc82
+      return "gcry_mac_write failed";
5bcc82
+    }
5bcc82
+  if (_gcry_mac_read (hd, mac, &macoutlen))
5bcc82
+    {
5bcc82
+      _gcry_mac_close (hd);
5bcc82
+      return "gcry_mac_read failed";
5bcc82
+    }
5bcc82
+  _gcry_mac_close (hd);
5bcc82
+  if (macoutlen != expectlen || memcmp (mac, expect, expectlen))
5bcc82
+    {
5bcc82
+/*       int i; */
5bcc82
+
5bcc82
+/*       fputs ("        {", stdout); */
5bcc82
+/*       for (i=0; i < expectlen-1; i++) */
5bcc82
+/*         { */
5bcc82
+/*           if (i && !(i % 8)) */
5bcc82
+/*             fputs ("\n         ", stdout); */
5bcc82
+/*           printf (" 0x%02x,", mac[i]); */
5bcc82
+/*         } */
5bcc82
+/*       printf (" 0x%02x } },\n", mac[i]); */
5bcc82
+
5bcc82
+      return "does not match";
5bcc82
+    }
5bcc82
+  return NULL;
5bcc82
+}
5bcc82
+
5bcc82
+
5bcc82
+static gpg_err_code_t
5bcc82
+selftests_cmac_tdes (int extended, selftest_report_func_t report)
5bcc82
+{
5bcc82
+  const char *what;
5bcc82
+  const char *errtxt;
5bcc82
+
5bcc82
+  what = "Basic TDES";
5bcc82
+  errtxt = check_one (GCRY_MAC_CMAC_3DES,
5bcc82
+        "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
5bcc82
+        "\xae\x2d\x8a\x57", 20,
5bcc82
+        "\x8a\xa8\x3b\xf8\xcb\xda\x10\x62\x0b\xc1\xbf\x19\xfb\xb6\xcd\x58"
5bcc82
+        "\xbc\x31\x3d\x4a\x37\x1c\xa8\xb5", 24,
5bcc82
+        "\x74\x3d\xdb\xe0\xce\x2d\xc2\xed", 8);
5bcc82
+  if (errtxt)
5bcc82
+    goto failed;
5bcc82
+
5bcc82
+  if (extended)
5bcc82
+    {
5bcc82
+      what = "Extended TDES #1";
5bcc82
+      errtxt = check_one (GCRY_MAC_CMAC_3DES,
5bcc82
+        "", 0,
5bcc82
+        "\x8a\xa8\x3b\xf8\xcb\xda\x10\x62\x0b\xc1\xbf\x19\xfb\xb6\xcd\x58"
5bcc82
+        "\xbc\x31\x3d\x4a\x37\x1c\xa8\xb5", 24,
5bcc82
+        "\xb7\xa6\x88\xe1\x22\xff\xaf\x95", 8);
5bcc82
+      if (errtxt)
5bcc82
+        goto failed;
5bcc82
+
5bcc82
+      what = "Extended TDES #2";
5bcc82
+      errtxt = check_one (GCRY_MAC_CMAC_3DES,
5bcc82
+        "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96", 8,
5bcc82
+        "\x8a\xa8\x3b\xf8\xcb\xda\x10\x62\x0b\xc1\xbf\x19\xfb\xb6\xcd\x58"
5bcc82
+        "\xbc\x31\x3d\x4a\x37\x1c\xa8\xb5", 24,
5bcc82
+        "\x8e\x8f\x29\x31\x36\x28\x37\x97", 8);
5bcc82
+      if (errtxt)
5bcc82
+        goto failed;
5bcc82
+
5bcc82
+      what = "Extended TDES #3";
5bcc82
+      errtxt = check_one (GCRY_MAC_CMAC_3DES,
5bcc82
+        "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
5bcc82
+        "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51", 32,
5bcc82
+        "\x8a\xa8\x3b\xf8\xcb\xda\x10\x62\x0b\xc1\xbf\x19\xfb\xb6\xcd\x58"
5bcc82
+        "\xbc\x31\x3d\x4a\x37\x1c\xa8\xb5", 24,
5bcc82
+        "\x33\xe6\xb1\x09\x24\x00\xea\xe5", 8);
5bcc82
+      if (errtxt)
5bcc82
+        goto failed;
5bcc82
+    }
5bcc82
+
5bcc82
+  return 0; /* Succeeded. */
5bcc82
+
5bcc82
+ failed:
5bcc82
+  if (report)
5bcc82
+    report ("cmac", GCRY_MAC_CMAC_3DES, what, errtxt);
5bcc82
+  return GPG_ERR_SELFTEST_FAILED;
5bcc82
+}
5bcc82
+
5bcc82
+
5bcc82
+
5bcc82
+static gpg_err_code_t
5bcc82
+selftests_cmac_aes (int extended, selftest_report_func_t report)
5bcc82
+{
5bcc82
+  const char *what;
5bcc82
+  const char *errtxt;
5bcc82
+
5bcc82
+  what = "Basic AES128";
5bcc82
+  errtxt = check_one (GCRY_MAC_CMAC_AES,
5bcc82
+        "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
5bcc82
+        "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
5bcc82
+        "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11", 40,
5bcc82
+        "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
5bcc82
+        "\xdf\xa6\x67\x47\xde\x9a\xe6\x30\x30\xca\x32\x61\x14\x97\xc8\x27", 16);
5bcc82
+  if (errtxt)
5bcc82
+    goto failed;
5bcc82
+
5bcc82
+  what = "Basic AES192";
5bcc82
+  errtxt = check_one (GCRY_MAC_CMAC_AES,
5bcc82
+        "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
5bcc82
+        "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
5bcc82
+        "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11", 40,
5bcc82
+        "\x8e\x73\xb0\xf7\xda\x0e\x64\x52\xc8\x10\xf3\x2b\x80\x90\x79\xe5"
5bcc82
+        "\x62\xf8\xea\xd2\x52\x2c\x6b\x7b", 24,
5bcc82
+        "\x8a\x1d\xe5\xbe\x2e\xb3\x1a\xad\x08\x9a\x82\xe6\xee\x90\x8b\x0e", 16);
5bcc82
+  if (errtxt)
5bcc82
+    goto failed;
5bcc82
+
5bcc82
+  what = "Basic AES256";
5bcc82
+  errtxt = check_one (GCRY_MAC_CMAC_AES,
5bcc82
+        "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
5bcc82
+        "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
5bcc82
+        "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11", 40,
5bcc82
+        "\x60\x3d\xeb\x10\x15\xca\x71\xbe\x2b\x73\xae\xf0\x85\x7d\x77\x81"
5bcc82
+        "\x1f\x35\x2c\x07\x3b\x61\x08\xd7\x2d\x98\x10\xa3\x09\x14\xdf\xf4", 32,
5bcc82
+        "\xaa\xf3\xd8\xf1\xde\x56\x40\xc2\x32\xf5\xb1\x69\xb9\xc9\x11\xe6", 16);
5bcc82
+  if (errtxt)
5bcc82
+    goto failed;
5bcc82
+  if (extended)
5bcc82
+    {
5bcc82
+      what = "Extended AES #1";
5bcc82
+      errtxt = check_one (GCRY_MAC_CMAC_AES,
5bcc82
+        "", 0,
5bcc82
+        "\x2b\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c", 16,
5bcc82
+        "\xbb\x1d\x69\x29\xe9\x59\x37\x28\x7f\xa3\x7d\x12\x9b\x75\x67\x46", 16);
5bcc82
+      if (errtxt)
5bcc82
+        goto failed;
5bcc82
+
5bcc82
+      what = "Extended AES #2";
5bcc82
+      errtxt = check_one (GCRY_MAC_CMAC_AES,
5bcc82
+        "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a", 16,
5bcc82
+        "\x8e\x73\xb0\xf7\xda\x0e\x64\x52\xc8\x10\xf3\x2b\x80\x90\x79\xe5"
5bcc82
+        "\x62\xf8\xea\xd2\x52\x2c\x6b\x7b", 24,
5bcc82
+        "\x9e\x99\xa7\xbf\x31\xe7\x10\x90\x06\x62\xf6\x5e\x61\x7c\x51\x84", 16);
5bcc82
+      if (errtxt)
5bcc82
+        goto failed;
5bcc82
+
5bcc82
+      what = "Extended AES #3";
5bcc82
+      errtxt = check_one (GCRY_MAC_CMAC_AES,
5bcc82
+        "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
5bcc82
+        "\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
5bcc82
+        "\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
5bcc82
+        "\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10", 64,
5bcc82
+        "\x60\x3d\xeb\x10\x15\xca\x71\xbe\x2b\x73\xae\xf0\x85\x7d\x77\x81"
5bcc82
+        "\x1f\x35\x2c\x07\x3b\x61\x08\xd7\x2d\x98\x10\xa3\x09\x14\xdf\xf4", 32,
5bcc82
+        "\xe1\x99\x21\x90\x54\x9f\x6e\xd5\x69\x6a\x2c\x05\x6c\x31\x54\x10", 16 );
5bcc82
+      if (errtxt)
5bcc82
+        goto failed;
5bcc82
+    }
5bcc82
+
5bcc82
+  return 0; /* Succeeded. */
5bcc82
+
5bcc82
+ failed:
5bcc82
+  if (report)
5bcc82
+    report ("cmac", GCRY_MAC_CMAC_AES, what, errtxt);
5bcc82
+  return GPG_ERR_SELFTEST_FAILED;
5bcc82
+}
5bcc82
+
5bcc82
+
5bcc82
+/* Run a full self-test for ALGO and return 0 on success.  */
5bcc82
+static gpg_err_code_t
5bcc82
+run_cmac_selftests (int algo, int extended, selftest_report_func_t report)
5bcc82
+{
5bcc82
+  gpg_err_code_t ec;
5bcc82
+
5bcc82
+  switch (algo)
5bcc82
+    {
5bcc82
+    case GCRY_MAC_CMAC_3DES:
5bcc82
+      ec = selftests_cmac_tdes (extended, report);
5bcc82
+      break;
5bcc82
+    case GCRY_MAC_CMAC_AES:
5bcc82
+      ec = selftests_cmac_aes (extended, report);
5bcc82
+      break;
5bcc82
+
5bcc82
+    default:
5bcc82
+      ec = GPG_ERR_MAC_ALGO;
5bcc82
+      break;
5bcc82
+    }
5bcc82
+  return ec;
5bcc82
+}
5bcc82
+
5bcc82
+
5bcc82
+
5bcc82
+
5bcc82
+/* Run the selftests for CMAC with CMAC algorithm ALGO with optional
5bcc82
+   reporting function REPORT.  */
5bcc82
+gpg_error_t
5bcc82
+_gcry_cmac_selftest (int algo, int extended, selftest_report_func_t report)
5bcc82
+{
5bcc82
+  gcry_err_code_t ec = 0;
5bcc82
+
5bcc82
+  if (!_gcry_mac_algo_info( algo, GCRYCTL_TEST_ALGO, NULL, NULL ))
5bcc82
+    {
5bcc82
+      ec = run_cmac_selftests (algo, extended, report);
5bcc82
+    }
5bcc82
+  else
5bcc82
+    {
5bcc82
+      ec = GPG_ERR_MAC_ALGO;
5bcc82
+      if (report)
5bcc82
+        report ("mac", algo, "module", "algorithm not available");
5bcc82
+    }
5bcc82
+  return gpg_error (ec);
5bcc82
+}
5bcc82
diff -up libgcrypt-1.8.3/src/cipher-proto.h.cmac-selftest libgcrypt-1.8.3/src/cipher-proto.h
5bcc82
--- libgcrypt-1.8.3/src/cipher-proto.h.cmac-selftest	2017-11-23 19:16:58.000000000 +0100
5bcc82
+++ libgcrypt-1.8.3/src/cipher-proto.h	2019-05-31 17:29:34.574588234 +0200
5bcc82
@@ -256,6 +256,8 @@ gcry_error_t _gcry_pk_selftest (int algo
5bcc82
                                 selftest_report_func_t report);
5bcc82
 gcry_error_t _gcry_hmac_selftest (int algo, int extended,
5bcc82
                                   selftest_report_func_t report);
5bcc82
+gcry_error_t _gcry_cmac_selftest (int algo, int extended,
5bcc82
+                                  selftest_report_func_t report);
5bcc82
 
5bcc82
 gcry_error_t _gcry_random_selftest (selftest_report_func_t report);
5bcc82
 
5bcc82
diff -up libgcrypt-1.8.3/src/fips.c.cmac-selftest libgcrypt-1.8.3/src/fips.c
5bcc82
--- libgcrypt-1.8.3/src/fips.c.cmac-selftest	2018-11-01 15:40:36.051865535 +0100
5bcc82
+++ libgcrypt-1.8.3/src/fips.c	2019-05-31 17:31:20.157756640 +0200
5bcc82
@@ -521,29 +521,32 @@ run_digest_selftests (int extended)
5bcc82
 
5bcc82
 /* Run self-tests for all HMAC algorithms.  Return 0 on success. */
5bcc82
 static int
5bcc82
-run_hmac_selftests (int extended)
5bcc82
+run_mac_selftests (int extended)
5bcc82
 {
5bcc82
-  static int algos[] =
5bcc82
+  static int algos[][2] =
5bcc82
     {
5bcc82
-      GCRY_MD_SHA1,
5bcc82
-      GCRY_MD_SHA224,
5bcc82
-      GCRY_MD_SHA256,
5bcc82
-      GCRY_MD_SHA384,
5bcc82
-      GCRY_MD_SHA512,
5bcc82
-      GCRY_MD_SHA3_224,
5bcc82
-      GCRY_MD_SHA3_256,
5bcc82
-      GCRY_MD_SHA3_384,
5bcc82
-      GCRY_MD_SHA3_512,
5bcc82
-      0
5bcc82
+      { GCRY_MD_SHA1, 0 },
5bcc82
+      { GCRY_MD_SHA224, 0 },
5bcc82
+      { GCRY_MD_SHA256, 0 },
5bcc82
+      { GCRY_MD_SHA384, 0 },
5bcc82
+      { GCRY_MD_SHA512, 0 },
5bcc82
+      { GCRY_MD_SHA3_224, 0 },
5bcc82
+      { GCRY_MD_SHA3_256, 0 },
5bcc82
+      { GCRY_MD_SHA3_384, 0 },
5bcc82
+      { GCRY_MD_SHA3_512, 0 },
5bcc82
+      { GCRY_MAC_CMAC_3DES, 1 },
5bcc82
+      { GCRY_MAC_CMAC_AES, 1 },
5bcc82
+      { 0, 0 }
5bcc82
     };
5bcc82
   int idx;
5bcc82
   gpg_error_t err;
5bcc82
   int anyerr = 0;
5bcc82
 
5bcc82
-  for (idx=0; algos[idx]; idx++)
5bcc82
+  for (idx=0; algos[idx][0]; idx++)
5bcc82
     {
5bcc82
-      err = _gcry_hmac_selftest (algos[idx], extended, reporter);
5bcc82
-      reporter ("hmac", algos[idx], NULL,
5bcc82
+      err = algos[idx][1] ? _gcry_cmac_selftest (algos[idx][0], extended, reporter) :
5bcc82
+        _gcry_hmac_selftest (algos[idx][0], extended, reporter);
5bcc82
+      reporter (algos[idx][1] ? "cmac" : "hmac", algos[idx][0], NULL,
5bcc82
                 err? gpg_strerror (err):NULL);
5bcc82
       if (err)
5bcc82
         anyerr = 1;
5bcc82
@@ -747,7 +750,7 @@ _gcry_fips_run_selftests (int extended)
5bcc82
   if (run_digest_selftests (extended))
5bcc82
     goto leave;
5bcc82
 
5bcc82
-  if (run_hmac_selftests (extended))
5bcc82
+  if (run_mac_selftests (extended))
5bcc82
     goto leave;
5bcc82
 
5bcc82
   /* Run random tests before the pubkey tests because the latter