Blame SOURCES/libgcrypt-1.8.5-kdf-selftest.patch

150a03
diff -up libgcrypt-1.8.5/cipher/kdf.c.kdf-selftest libgcrypt-1.8.5/cipher/kdf.c
150a03
--- libgcrypt-1.8.5/cipher/kdf.c.kdf-selftest	2017-11-23 19:16:58.000000000 +0100
150a03
+++ libgcrypt-1.8.5/cipher/kdf.c	2020-06-15 18:14:26.494995669 +0200
150a03
@@ -305,3 +305,99 @@ _gcry_kdf_derive (const void *passphrase
150a03
  leave:
150a03
   return ec;
150a03
 }
150a03
+
150a03
+
150a03
+/* PBKDF2 selftests.
150a03
+ * Copyright (C) 2008 Free Software Foundation, Inc.
150a03
+ * Copyright (C) 2019, 2020 Red Hat, Inc.
150a03
+ */
150a03
+
150a03
+/* Check one PBKDF2 call with HASH ALGO using the regular KDF
150a03
+ * API. (passphrase,passphraselen) is the password to be derived,
150a03
+ * (salt,saltlen) the salt for the key derivation,
150a03
+ * iterations is the number of the kdf iterations,
150a03
+ * and (expect,expectlen) the expected result. Returns NULL on
150a03
+ * success or a string describing the failure.  */
150a03
+
150a03
+static const char *
150a03
+check_one (int algo,
150a03
+           const void *passphrase, size_t passphraselen,
150a03
+           const void *salt, size_t saltlen,
150a03
+           unsigned long iterations,
150a03
+           const void *expect, size_t expectlen)
150a03
+{
150a03
+  unsigned char key[512]; /* hardcoded to avoid allocation */
150a03
+  size_t keysize = expectlen;
150a03
+
150a03
+  if (keysize > sizeof(key))
150a03
+    return "invalid tests data";
150a03
+
150a03
+  if (_gcry_kdf_derive (passphrase, passphraselen, GCRY_KDF_PBKDF2,
150a03
+                        algo, salt, saltlen, iterations,
150a03
+                         keysize, key))
150a03
+    return "gcry_kdf_derive failed";
150a03
+
150a03
+  if (memcmp (key, expect, expectlen))
150a03
+    return "does not match";
150a03
+
150a03
+  return NULL;
150a03
+}
150a03
+
150a03
+static gpg_err_code_t
150a03
+run_pbkdf2_selftest (int extended, selftest_report_func_t report)
150a03
+{
150a03
+  const char *what;
150a03
+  const char *errtxt;
150a03
+
150a03
+  what = "Basic PBKDF2 SHA256";
150a03
+  errtxt = check_one (GCRY_MD_SHA256,
150a03
+        "password", 8,
150a03
+        "salt", 4,
150a03
+        2,
150a03
+        "\xae\x4d\x0c\x95\xaf\x6b\x46\xd3\x2d\x0a\xdf\xf9\x28\xf0\x6d\xd0"
150a03
+        "\x2a\x30\x3f\x8e\xf3\xc2\x51\xdf\xd6\xe2\xd8\x5a\x95\x47\x4c\x43", 32);
150a03
+  if (errtxt)
150a03
+    goto failed;
150a03
+
150a03
+  if (extended)
150a03
+    {
150a03
+      what = "Extended PBKDF2 SHA256";
150a03
+      errtxt = check_one (GCRY_MD_SHA256,
150a03
+        "passwordPASSWORDpassword", 24,
150a03
+        "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36,
150a03
+        4096,
150a03
+        "\x34\x8c\x89\xdb\xcb\xd3\x2b\x2f\x32\xd8\x14\xb8\x11\x6e\x84\xcf"
150a03
+        "\x2b\x17\x34\x7e\xbc\x18\x00\x18\x1c\x4e\x2a\x1f\xb8\xdd\x53\xe1"
150a03
+        "\xc6\x35\x51\x8c\x7d\xac\x47\xe9", 40);
150a03
+      if (errtxt)
150a03
+        goto failed;
150a03
+    }
150a03
+
150a03
+  return 0; /* Succeeded. */
150a03
+
150a03
+ failed:
150a03
+  if (report)
150a03
+    report ("kdf", GCRY_KDF_PBKDF2, what, errtxt);
150a03
+  return GPG_ERR_SELFTEST_FAILED;
150a03
+}
150a03
+
150a03
+
150a03
+/* Run the selftests for KDF with KDF algorithm ALGO with optional
150a03
+   reporting function REPORT.  */
150a03
+gpg_error_t
150a03
+_gcry_kdf_selftest (int algo, int extended, selftest_report_func_t report)
150a03
+{
150a03
+  gcry_err_code_t ec = 0;
150a03
+
150a03
+  if (algo == GCRY_KDF_PBKDF2)
150a03
+    {
150a03
+      ec = run_pbkdf2_selftest (extended, report);
150a03
+    }
150a03
+  else
150a03
+    {
150a03
+      ec = GPG_ERR_UNSUPPORTED_ALGORITHM;
150a03
+      if (report)
150a03
+        report ("kdf", algo, "module", "algorithm not available");
150a03
+    }
150a03
+  return gpg_error (ec);
150a03
+}
150a03
diff -up libgcrypt-1.8.5/src/cipher-proto.h.kdf-selftest libgcrypt-1.8.5/src/cipher-proto.h
150a03
--- libgcrypt-1.8.5/src/cipher-proto.h.kdf-selftest	2020-06-15 18:03:25.785353036 +0200
150a03
+++ libgcrypt-1.8.5/src/cipher-proto.h	2020-06-15 18:03:25.788353061 +0200
150a03
@@ -259,6 +259,8 @@ gcry_error_t _gcry_hmac_selftest (int al
150a03
                                   selftest_report_func_t report);
150a03
 gcry_error_t _gcry_cmac_selftest (int algo, int extended,
150a03
                                   selftest_report_func_t report);
150a03
+gcry_error_t _gcry_kdf_selftest (int algo, int extended,
150a03
+                                  selftest_report_func_t report);
150a03
 
150a03
 gcry_error_t _gcry_random_selftest (selftest_report_func_t report);
150a03
 
150a03
diff -up libgcrypt-1.8.5/src/fips.c.kdf-selftest libgcrypt-1.8.5/src/fips.c
150a03
--- libgcrypt-1.8.5/src/fips.c.kdf-selftest	2020-06-15 18:03:25.777352968 +0200
150a03
+++ libgcrypt-1.8.5/src/fips.c	2020-06-15 18:08:40.651028096 +0200
150a03
@@ -490,6 +490,29 @@ run_mac_selftests (int extended)
150a03
   return anyerr;
150a03
 }
150a03
 
150a03
+/* Run self-tests for all KDF algorithms.  Return 0 on success. */
150a03
+static int
150a03
+run_kdf_selftests (int extended)
150a03
+{
150a03
+  static int algos[] =
150a03
+    {
150a03
+      GCRY_KDF_PBKDF2,
150a03
+      0
150a03
+    };
150a03
+  int idx;
150a03
+  gpg_error_t err;
150a03
+  int anyerr = 0;
150a03
+
150a03
+  for (idx=0; algos[idx]; idx++)
150a03
+    {
150a03
+      err = _gcry_kdf_selftest (algos[idx], extended, reporter);
150a03
+      reporter ("kdf", algos[idx], NULL, err? gpg_strerror (err):NULL);
150a03
+      if (err)
150a03
+        anyerr = 1;
150a03
+    }
150a03
+  return anyerr;
150a03
+}
150a03
+
150a03
 
150a03
 /* Run self-tests for all required public key algorithms.  Return 0 on
150a03
    success. */
150a03
@@ -673,6 +696,9 @@ _gcry_fips_run_selftests (int extended)
150a03
   if (run_mac_selftests (extended))
150a03
     goto leave;
150a03
 
150a03
+  if (run_kdf_selftests (extended))
150a03
+    goto leave;
150a03
+
150a03
   /* Run random tests before the pubkey tests because the latter
150a03
      require random.  */
150a03
   if (run_random_selftests ())