Debian Codegraph @

docs/algorithms.rst

raw
1 Digital Signature Algorithms
2 ============================
3
4 The JWT specification supports several algorithms for cryptographic signing.
5 This library currently supports:
6
7 * HS256 - HMAC using SHA-256 hash algorithm (default)
8 * HS384 - HMAC using SHA-384 hash algorithm
9 * HS512 - HMAC using SHA-512 hash algorithm
10 * ES256 - ECDSA signature algorithm using SHA-256 hash algorithm
11 * ES256K - ECDSA signature algorithm with secp256k1 curve using SHA-256 hash algorithm
12 * ES384 - ECDSA signature algorithm using SHA-384 hash algorithm
13 * ES512 - ECDSA signature algorithm using SHA-512 hash algorithm
14 * RS256 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-256 hash algorithm
15 * RS384 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-384 hash algorithm
16 * RS512 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-512 hash algorithm
17 * PS256 - RSASSA-PSS signature using SHA-256 and MGF1 padding with SHA-256
18 * PS384 - RSASSA-PSS signature using SHA-384 and MGF1 padding with SHA-384
19 * PS512 - RSASSA-PSS signature using SHA-512 and MGF1 padding with SHA-512
20 * EdDSA - Both Ed25519 signature using SHA-512 and Ed448 signature using SHA-3 are supported. Ed25519 and Ed448 provide 128-bit and 224-bit security respectively.
21
22 Minimum Key Length Requirements
23 -------------------------------
24
25 PyJWT enforces minimum key lengths per industry standards. Keys below these
26 minimums will trigger an ``InsecureKeyLengthWarning`` by default, or raise
27 ``InvalidKeyError`` if ``enforce_minimum_key_length`` is enabled.
28
29 .. list-table::
30 :header-rows: 1
31 :widths: auto
32
33 * - Algorithm
34 - Minimum Key Length
35 - Standard
36 * - HS256
37 - 32 bytes (256 bits)
38 - RFC 7518 Section 3.2
39 * - HS384
40 - 48 bytes (384 bits)
41 - RFC 7518 Section 3.2
42 * - HS512
43 - 64 bytes (512 bits)
44 - RFC 7518 Section 3.2
45 * - RS256/384/512
46 - 2048 bits
47 - NIST SP 800-131A
48 * - PS256/384/512
49 - 2048 bits
50 - NIST SP 800-131A
51
52 See :ref:`key-length-validation` for configuration details.
53
54 Asymmetric (Public-key) Algorithms
55 ----------------------------------
56 Usage of RSA (RS\*) and EC (EC\*) algorithms require a basic understanding
57 of how public-key cryptography is used with regards to digital signatures.
58 If you are unfamiliar, you may want to read
59 `this article <https://en.wikipedia.org/wiki/Public-key_cryptography>`_.
60
61 When using the RSASSA-PKCS1-v1_5 algorithms, the `key` argument in both
62 ``jwt.encode()`` and ``jwt.decode()`` (``"secret"`` in the examples) is expected to
63 be either an RSA public or private key in PEM or SSH format. The type of key
64 (private or public) depends on whether you are signing or verifying a token.
65
66 When using the ECDSA algorithms, the ``key`` argument is expected to
67 be an Elliptic Curve public or private key in PEM format. The type of key
68 (private or public) depends on whether you are signing or verifying.
69
70 Specifying an Algorithm
71 -----------------------
72 You can specify which algorithm you would like to use to sign the JWT
73 by using the `algorithm` parameter:
74
75 .. code-block:: pycon
76
77 >>> encoded = jwt.encode({"some": "payload"}, "secret", algorithm="HS512")
78 >>> print(encoded)
79 eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.WTzLzFO079PduJiFIyzrOah54YaM8qoxH9fLMQoQhKtw3_fMGjImIOokijDkXVbyfBqhMo2GCNu4w9v7UXvnpA
80
81 When decoding, you can also specify which algorithms you would like to permit
82 when validating the JWT by using the `algorithms` parameter which takes a list
83 of allowed algorithms:
84
85 .. code-block:: pycon
86
87 >>> jwt.decode(encoded, "secret", algorithms=["HS512", "HS256"])
88 {'some': 'payload'}
89
90 In the above case, if the JWT has any value for its alg header other than
91 HS512 or HS256, the claim will be rejected with an ``InvalidAlgorithmError``.
92
93 .. warning::
94
95 Do **not** compute the ``algorithms`` parameter based on the
96 ``alg`` from the token itself, or on any other data that an
97 attacker may be able to influence, as that might expose you to
98 various vulnerabilities (see `RFC 8725 §2.1
99 <https://www.rfc-editor.org/rfc/rfc8725.html#section-2.1>`_). Instead,
100 either hard-code a fixed value for ``algorithms``, or configure it
101 in the same place you configure the ``key``. Make sure not to mix
102 symmetric and asymmetric algorithms that interpret the ``key`` in
103 different ways (e.g. HS\* and RS\*).