aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hazmat/primitives/test_pbkdf2hmac.py
blob: df49d23c68646ebe1ee1b466d536069dcac8a153 (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
66
67
68
69
70
71
72
# 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

import six

from cryptography import utils
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 ...utils import raises_unsupported_algorithm


@utils.register_interface(hashes.HashAlgorithm)
class DummyHash(object):
    name = "dummy-hash"
    block_size = None
    digest_size = None


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(DummyHash(), 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, six.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(six.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)
lass="nf">rgb_matrix_step_reverse(void); void rgb_matrix_increase_hue(void); void rgb_matrix_decrease_hue(void); void rgb_matrix_increase_sat(void); void rgb_matrix_decrease_sat(void); void rgb_matrix_increase_val(void); void rgb_matrix_decrease_val(void); void rgb_matrix_increase_speed(void); void rgb_matrix_decrease_speed(void); led_flags_t rgb_matrix_get_flags(void); void rgb_matrix_set_flags(led_flags_t flags); void rgb_matrix_mode(uint8_t mode); void rgb_matrix_mode_noeeprom(uint8_t mode); uint8_t rgb_matrix_get_mode(void); void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val); void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val); #ifndef RGBLIGHT_ENABLE # define rgblight_toggle rgb_matrix_toggle # define rgblight_enable rgb_matrix_enable # define rgblight_enable_noeeprom rgb_matrix_enable_noeeprom # define rgblight_disable rgb_matrix_disable # define rgblight_disable_noeeprom rgb_matrix_disable_noeeprom # define rgblight_step rgb_matrix_step # define rgblight_sethsv rgb_matrix_sethsv # define rgblight_sethsv_noeeprom rgb_matrix_sethsv_noeeprom # define rgblight_step_reverse rgb_matrix_step_reverse # define rgblight_increase_hue rgb_matrix_increase_hue # define rgblight_decrease_hue rgb_matrix_decrease_hue # define rgblight_increase_sat rgb_matrix_increase_sat # define rgblight_decrease_sat rgb_matrix_decrease_sat # define rgblight_increase_val rgb_matrix_increase_val # define rgblight_decrease_val rgb_matrix_decrease_val # define rgblight_increase_speed rgb_matrix_increase_speed # define rgblight_decrease_speed rgb_matrix_decrease_speed # define rgblight_mode rgb_matrix_mode # define rgblight_mode_noeeprom rgb_matrix_mode_noeeprom # define rgblight_get_mode rgb_matrix_get_mode #endif typedef struct { /* Perform any initialisation required for the other driver functions to work. */ void (*init)(void); /* Set the colour of a single LED in the buffer. */ void (*set_color)(int index, uint8_t r, uint8_t g, uint8_t b); /* Set the colour of all LEDS on the keyboard in the buffer. */ void (*set_color_all)(uint8_t r, uint8_t g, uint8_t b); /* Flush any buffered changes to the hardware. */ void (*flush)(void); } rgb_matrix_driver_t; extern const rgb_matrix_driver_t rgb_matrix_driver; extern rgb_config_t rgb_matrix_config; extern bool g_suspend_state; extern rgb_counters_t g_rgb_counters; extern led_config_t g_led_config; #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED extern last_hit_t g_last_hit_tracker; #endif #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS extern uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS]; #endif #endif