Added wycheproof vectors for pbkdf2 (#10024)

This commit is contained in:
Alex Gaynor 2023-12-21 09:30:36 -05:00 committed by GitHub
parent ac6497f6f0
commit fb4c72c8bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 49 additions and 19 deletions

View file

@ -22,7 +22,7 @@ for various cryptographic algorithms. These are not included in the repository
continuous integration environments.
We have ensured all test vectors are used as of commit
``b063b4aedae951c69df014cd25fa6d69ae9e8cb9``.
``d9f6ec7d8bd8c96da05368999094e4a75ba5cb3d``.
Asymmetric ciphers
~~~~~~~~~~~~~~~~~~

View file

@ -918,8 +918,8 @@ class WycheproofTest:
return cache_val
def load_wycheproof_tests(wycheproof, test_file):
path = os.path.join(wycheproof, "testvectors", test_file)
def load_wycheproof_tests(wycheproof, test_file, subdir):
path = os.path.join(wycheproof, subdir, test_file)
with open(path) as f:
data = json.load(f)
for group in data.pop("testGroups"):

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -0,0 +1,42 @@
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from .utils import wycheproof_tests
_HASH_ALGORITHMS = {
"PBKDF2-HMACSHA1": hashes.SHA1(),
"PBKDF2-HMACSHA224": hashes.SHA224(),
"PBKDF2-HMACSHA256": hashes.SHA256(),
"PBKDF2-HMACSHA384": hashes.SHA384(),
"PBKDF2-HMACSHA512": hashes.SHA512(),
}
@wycheproof_tests(
"pbkdf2_hmacsha1_test.json",
"pbkdf2_hmacsha224_test.json",
"pbkdf2_hmacsha256_test.json",
"pbkdf2_hmacsha384_test.json",
"pbkdf2_hmacsha512_test.json",
subdir="testvectors_v1",
)
def test_pbkdf2(backend, wycheproof):
assert wycheproof.valid
algorithm = _HASH_ALGORITHMS[wycheproof.testfiledata["algorithm"]]
p = PBKDF2HMAC(
algorithm=algorithm,
length=wycheproof.testcase["dkLen"],
salt=binascii.unhexlify(wycheproof.testcase["salt"]),
iterations=wycheproof.testcase["iterationCount"],
)
assert p.derive(
binascii.unhexlify(wycheproof.testcase["password"])
) == binascii.unhexlify(wycheproof.testcase["dk"])

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
from ..utils import WycheproofTest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import binascii
import pytest

View file

@ -1,14 +1,16 @@
from ..utils import load_wycheproof_tests
def wycheproof_tests(*paths):
def wycheproof_tests(*paths, subdir="testvectors"):
def wrapper(func):
def run_wycheproof(backend, subtests, pytestconfig):
wycheproof_root = pytestconfig.getoption(
"--wycheproof-root", skip=True
)
for path in paths:
for test in load_wycheproof_tests(wycheproof_root, path):
for test in load_wycheproof_tests(
wycheproof_root, path, subdir
):
with subtests.test():
func(backend, test)