aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hazmat/primitives/test_pbkdf2hmac.py
blob: d971ebd038ea7303fde078f8339f4718e07e9d1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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.

from __future__ import absolute_import, division, print_function

import pytest

from cryptography.exceptions import (
    AlreadyFinalized, InvalidKey, _Reasons
)
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

from ...doubles import DummyHashAlgorithm
from ...utils import raises_unsupported_algorithm


class TestPBKDF2HMAC(object):
    def test_already_finalized(self):
        kdf = PBKDF2HMAC(hashes.SHA1(), 20, b"salt", 10, default_backend())
        kdf.derive(b"password")
        with pytest.raises(AlreadyFinalized):
            kdf.derive(b"password2")

        kdf = PBKDF2HMAC(hashes.SHA1(), 20, b"salt", 10, default_backend())
        key = kdf.derive(b"password")
        with pytest.raises(AlreadyFinalized):
            kdf.verify(b"password", key)

        kdf = PBKDF2HMAC(hashes.SHA1(), 20, b"salt", 10, default_backend())
        kdf.verify(b"password", key)
        with pytest.raises(AlreadyFinalized):
            kdf.verify(b"password", key)

    def test_unsupported_algorithm(self):
        with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH):
            PBKDF2HMAC(
                DummyHashAlgorithm(), 20, b"salt", 10, default_backend()
            )

    def test_invalid_key(self):
        kdf = PBKDF2HMAC(hashes.SHA1(), 20, b"salt", 10, default_backend())
        key = kdf.derive(b"password")

        kdf = PBKDF2HMAC(hashes.SHA1(), 20, b"salt", 10, default_backend())
        with pytest.raises(InvalidKey):
            kdf.verify(b"password2", key)

    def test_unicode_error_with_salt(self):
        with pytest.raises(TypeError):
            PBKDF2HMAC(hashes.SHA1(), 20, u"salt", 10, default_backend())

    def test_unicode_error_with_key_material(self):
        kdf = PBKDF2HMAC(hashes.SHA1(), 20, b"salt", 10, default_backend())
        with pytest.raises(TypeError):
            kdf.derive(u"unicode here")


def test_invalid_backend():
    pretend_backend = object()

    with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE):
        PBKDF2HMAC(hashes.SHA1(), 20, b"salt", 10, pretend_backend)
> library ieee; use ieee.std_logic_1164.all; use work.wor_std.all; entity cpu_reg_dummy is generic ( value: std_logic_vector(3 downto 0) := (others => 'Z') ); port ( rddata_o: out std_logic_vector(3 downto 0) ); end entity; architecture foo of cpu_reg_dummy is begin rddata_o <= value after 0.5 ns; end architecture; library ieee; use ieee.std_logic_1164.all; use work.wor_std.all; entity foe is end entity; architecture fum of foe is component cpu_reg_dummy generic ( value: std_logic_vector(rddata_o_range) := (others => 'Z') ); port ( rddata_o: out std_logic_vector(rddata_o_range) ); end component; signal rddata_o: rddata_o_resolv std_logic_vector (rddata_o_range); begin CPU_REG1: cpu_reg_dummy generic map (value => "0000") port map (rddata_o => rddata_o); CPU_REG2: cpu_reg_dummy generic map (value => "1001") port map (rddata_o => rddata_o); CPU_REG3: cpu_reg_dummy generic map (value => "ZZZZ") port map (rddata_o => rddata_o); CPU_REG4: cpu_reg_dummy generic map (value => "ZZZX") port map (rddata_o => rddata_o); WHAT: process begin wait for 0.6 ns; report "rddata_o = " & slv_image(rddata_o); wait; end process; end architecture;