>From another thread :

On Tue, 2012-11-27 at 22:47 +0100, Niels Möller wrote:
...
> For lack of more authoritative test vectors, adding a couple of
> testvectors generated by python-pbkdf2, to the nettle testsuite would
> be nice.

Attached is a patch that tries to validate the Nettle PBKDF2-HMAC-SHA512
implementation with a few values generated with python-pbkdf2 from
https://www.dlitz.net/software/python-pbkdf2/.

I also conducted 300,000 semi-random tests comparing the Nettle digest
with corresponding OpenSSL digests, using QuickCheck-like testing, with
good results. I did not use python-pbkdf2 checks for these, because
frankly they were too slow.

In case anyone is interested, I'm attaching the QC test too. Quick
instructions for that one would be to install 'qc' from
git://github.com/dbravender/qc.git, my python-ndnkdf from
https://github.com/fredrikt/python-ndnkdf and then do

  $ nosetests -d qc-test.py

/Fredrik

>From 551421d68dbc68c9d7567a9950b1938a6d996f6f Mon Sep 17 00:00:00 2001
From: Fredrik Thulin <[email protected]>
Date: Wed, 28 Nov 2012 17:01:03 +0100
Subject: [PATCH] Add PBKDF2-HMAC-SHA512 test cases.

Test Nettle version using values from another implementation.

Courtesy of NORDUnet A/S.
---
 testsuite/pbkdf2-test.c |   27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/testsuite/pbkdf2-test.c b/testsuite/pbkdf2-test.c
index c0d2eae..ffb5580 100644
--- a/testsuite/pbkdf2-test.c
+++ b/testsuite/pbkdf2-test.c
@@ -20,7 +20,7 @@
     ASSERT(dk[expect->length] == 17);					\
   } while (0)
 
-#define MAX_DKLEN 25
+#define MAX_DKLEN SHA512_DIGEST_SIZE
 
 void
 test_main (void)
@@ -28,6 +28,7 @@ test_main (void)
   uint8_t dk[MAX_DKLEN + 1];
   struct hmac_sha1_ctx sha1ctx;
   struct hmac_sha256_ctx sha256ctx;
+  struct hmac_sha512_ctx sha512ctx;
 
   /* Test vectors for PBKDF2 from RFC 6070. */
 
@@ -78,6 +79,30 @@ test_main (void)
 	       SHA256_DIGEST_SIZE, 80000, LDATA("NaCl"),
 	       SHEX("4ddcd8f60b98be21830cee5ef22701f9"));
 
+  /* PBKDF2-HMAC-SHA-512 test vectors confirmed with another
+     implementation (python-pbkdf2).
+
+     >>> from pbkdf2 import PBKDF2
+     >>> import hmac as HMAC
+     >>> from hashlib import sha512 as SHA512
+     >>> PBKDF2("password", "salt", 50, macmodule=HMAC, digestmodule=SHA512).read(64).encode('hex')
+  */
+
+  hmac_sha512_set_key (&sha512ctx, 8, "password");
+  PBKDF2_TEST (&sha512ctx, hmac_sha512_update, hmac_sha512_digest,
+	       SHA512_DIGEST_SIZE, 1, LDATA("NaCL"),
+	       SHEX("73decfa58aa2e84f94771a75736bb88bd3c7b38270cfb50cb390ed78b305656af8148e52452b2216b2b8098b761fc6336060a09f76415e9f71ea47f9e9064306"));
+
+  hmac_sha512_set_key (&sha512ctx, 9, "pass\0word");
+  PBKDF2_TEST (&sha512ctx, hmac_sha512_update, hmac_sha512_digest,
+	       SHA512_DIGEST_SIZE, 1, LDATA("sa\0lt"),
+	       SHEX("71a0ec842abd5c678bcfd145f09d83522f93361560563c4d0d63b88329871090e76604a49af08fe7c9f57156c8790996b20f06bc535e5ab5440df7e878296fa7"));
+
+  hmac_sha512_set_key (&sha512ctx, 24, "passwordPASSWORDpassword");
+  PBKDF2_TEST (&sha512ctx, hmac_sha512_update, hmac_sha512_digest,
+	       SHA512_DIGEST_SIZE, 50, LDATA("salt\0\0\0"),
+	       SHEX("016871a4c4b75f96857fd2b9f8ca28023b30ee2a39f5adcac8c9375f9bda1ccd1b6f0b2fc3adda505412e79d890056c62e524c7d51154b1a8534575bd02dee39"));
+
   /* Test convenience functions. */
 
   PBKDF2_HMAC_TEST(pbkdf2_hmac_sha1, LDATA("password"), 1, LDATA("salt"),
-- 
1.7.9.5

#!/usr/bin/python
#
# Copyright (c) 2012, NORDUnet A/S
# All rights reserved.
#
#   Redistribution and use in source and binary forms, with or
#   without modification, are permitted provided that the following
#   conditions are met:
#
#     1. Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#     2. Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials provided
#        with the distribution.
#     3. Neither the name of the NORDUnet nor the names of its
#        contributors may be used to endorse or promote products derived
#        from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Author : Fredrik Thulin <[email protected]>
#

import ndnkdf
import ctypes
import ctypes.util
import timeit
import sys

from pbkdf2 import PBKDF2
import hmac as HMAC
from hashlib import sha512 as SHA512

from qc import forall, integers


class OSSL():

    _DIGEST_SIZE=64

    def __init__(self, path=None):
        self.ssl = ctypes.cdll.LoadLibrary('libssl.so')

    def pbkdf2_hmac_sha512(self, key, iterations, salt):
        """
        Invoke OpenSSL PBKDF2 using HMAC-SHA-512 on key, iterations and salt.
        """
        self.ssl.EVP_sha512.restype = ctypes.c_void_p
        evp = ctypes.cast(self.ssl.EVP_sha512(), ctypes.POINTER(ctypes.c_void_p))
        buf = ctypes.create_string_buffer('', size = self._DIGEST_SIZE)
        res = self.ssl.PKCS5_PBKDF2_HMAC(key, len(key), \
                                             salt, len(salt), \
                                             int(iterations), \
                                             evp, \
                                             self._DIGEST_SIZE, \
                                             ctypes.byref(buf) \
                                             )
        return buf.raw

nettle = ndnkdf.NDNKDF()
ossl = OSSL()

keys = ['', chr(0) * 8, chr(0) * 64, chr(0) * 100,
        'passwd', 'This is a secret passphrase.',
        'pass\0word',
        'PASSphrase\0',
        'PASS\0\0phrase\0\0',
        'a', 'a' * 32,
        ]
salts = ['', chr(0) * 8, 'saltSALTsaltSALTsalt', 'NaCL',
        'a', 'a' * 32,
         'sa\0lt',
         'salt\0\0',
         ]

forall.verbose=True

@forall(tries=10000,
        iterations=integers(low=1, high=50000),
        keyidx=integers(low=0, high=len(keys) - 1),
        saltidx=integers(low=0, high=len(salts) - 1),
        keymul=integers(low=1, high=100),
        saltmul=integers(low=1, high=100),
        )
def test_pbkdf2_hmac_sha512(iterations, keyidx, saltidx, keymul, saltmul):
    key = keys[keyidx] * keymul
    salt = salts[saltidx] * saltmul
    nettle_res = nettle.pbkdf2_hmac_sha512(key, iterations, salt)
    openssl_res = ossl.pbkdf2_hmac_sha512(key, iterations, salt)
    #py_res = PBKDF2(keys[key], salts[salt], iterations=iterations, \
    #                    macmodule=HMAC, \
    #                    digestmodule=SHA512).read(64)
    sys.stderr.write("%s %s : i=%i k=%s[%i] s=%s[%i]\n" % (nettle_res[:5].encode('hex'), openssl_res[:5].encode('hex'),
                                                   iterations,
                                                   key[:5], len(key), salt[:5], len(salt),))
    #assert (nettle_res == py_res)
    assert (nettle_res == openssl_res)
_______________________________________________
nettle-bugs mailing list
[email protected]
http://lists.lysator.liu.se/mailman/listinfo/nettle-bugs

Reply via email to