aboutsummaryrefslogtreecommitdiffstats
path: root/docs/hazmat/primitives/asymmetric/utils.rst
blob: f46acb2ec0813e117f5fdd109d74946226ee7ac8 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
.. hazmat::

Asymmetric Utilities
====================

.. currentmodule:: cryptography.hazmat.primitives.asymmetric.utils


.. function:: decode_dss_signature(signature)

    Takes in signatures generated by the DSA/ECDSA signers and returns a
    tuple ``(r, s)``. These signatures are ASN.1 encoded ``Dss-Sig-Value``
    sequences (as defined in :rfc:`3279`)

    :param bytes signature: The signature to decode.

    :returns: The decoded tuple ``(r, s)``.

    :raises ValueError: Raised if the signature is malformed.

.. function:: encode_dss_signature(r, s)

    Creates an ASN.1 encoded ``Dss-Sig-Value`` (as defined in :rfc:`3279`) from
    raw ``r`` and ``s`` values.

    :param int r: The raw signature value ``r``.

    :param int s: The raw signature value ``s``.

    :return bytes: The encoded signature.

.. class:: Prehashed(algorithm)

    .. versionadded:: 1.6

    ``Prehashed`` can be passed as the ``algorithm`` in the RSA
    :meth:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey.sign`
    and
    :meth:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey.verify`
    as well as DSA
    :meth:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPrivateKey.sign`
    and
    :meth:`~cryptography.hazmat.primitives.asymmetric.dsa.DSAPublicKey.verify`
    methods.

    For elliptic curves it can be passed as the ``algorithm`` in
    :class:`~cryptography.hazmat.primitives.asymmetric.ec.ECDSA` and then used
    with
    :meth:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey.sign`
    and
    :meth:`~cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey.verify`
    .

    :param algorithm: An instance of
        :class:`~cryptography.hazmat.primitives.hashes.HashAlgorithm`.

    .. doctest::

        >>> import hashlib
        >>> from cryptography.hazmat.backends import default_backend
        >>> from cryptography.hazmat.primitives import hashes
        >>> from cryptography.hazmat.primitives.asymmetric import (
        ...    padding, rsa, utils
        ... )
        >>> private_key = rsa.generate_private_key(
        ...     public_exponent=65537,
        ...     key_size=2048,
        ...     backend=default_backend()
        ... )
        >>> prehashed_msg = hashlib.sha256(b"A message I want to sign").digest()
        >>> signature = private_key.sign(
        ...     prehashed_msg,
        ...     padding.PSS(
        ...         mgf=padding.MGF1(hashes.SHA256()),
        ...         salt_length=padding.PSS.MAX_LENGTH
        ...     ),
        ...     utils.Prehashed(hashes.SHA256())
        ... )
        >>> public_key = private_key.public_key()
        >>> public_key.verify(
        ...     signature,
        ...     prehashed_msg,
        ...     padding.PSS(
        ...         mgf=padding.MGF1(hashes.SHA256()),
        ...         salt_length=padding.PSS.MAX_LENGTH
        ...     ),
        ...     utils.Prehashed(hashes.SHA256())
        ... )
n class="n">font_dejavusansbold12, Black); // Always remember to flush the display gdispFlush(); // you could set the backlight color as well, but we won't do it here, since // it's part of the following animation // lcd_backlight_color(hue, saturation, intensity); // We don't need constant updates, just drawing the screen once is enough return false; } // Feel free to modify the animations below, or even add new ones if needed // Don't worry, if the startup animation is long, you can use the keyboard like normal // during that time static keyframe_animation_t startup_animation = { .num_frames = 4, .loop = false, .frame_lengths = {0, MS2ST(1000), MS2ST(5000), 0}, .frame_functions = {display_welcome, keyframe_animate_backlight_color, keyframe_no_operation, enable_visualization}, }; // The color animation animates the LCD color when you change layers static keyframe_animation_t color_animation = { .num_frames = 2, .loop = false, // Note that there's a 200 ms no-operation frame, // this prevents the color from changing when activating the layer // momentarily .frame_lengths = {MS2ST(200), MS2ST(500)}, .frame_functions = {keyframe_no_operation, keyframe_animate_backlight_color}, }; // The LCD animation alternates between the layer name display and a // bitmap that displays all active layers static keyframe_animation_t lcd_animation = { .num_frames = 2, .loop = true, .frame_lengths = {MS2ST(2000), MS2ST(2000)}, .frame_functions = {keyframe_display_layer_text, keyframe_display_layer_bitmap}, }; void initialize_user_visualizer(visualizer_state_t* state) { // The brightness will be dynamically adjustable in the future // But for now, change it here. lcd_backlight_brightness(0x50); state->current_lcd_color = LCD_COLOR(0x00, 0x00, 0xFF); state->target_lcd_color = LCD_COLOR(0x10, 0xFF, 0xFF); start_keyframe_animation(&startup_animation); } void update_user_visualizer_state(visualizer_state_t* state) { // Add more tests, change the colors and layer texts here // Usually you want to check the high bits (higher layers first) // because that's the order layers are processed for keypresses // You can for check for example: // state->status.layer // state->status.default_layer // state->status.leds (see led.h for available statuses) if (state->status.layer & 0x2) { state->target_lcd_color = LCD_COLOR(0xA0, 0xB0, 0xFF); state->layer_text = "Layer 2"; } else { state->target_lcd_color = LCD_COLOR(0x50, 0xB0, 0xFF); state->layer_text = "Layer 1"; } // You can also stop existing animations, and start your custom ones here // remember that you should normally have only one animation for the LCD // and one for the background. But you can also combine them if you want. start_keyframe_animation(&lcd_animation); start_keyframe_animation(&color_animation); }