Skip to content

Commit c533ed6

Browse files
committed
Updating CryptographyAESKey::encrypt to generate 96 bit IVs for GCM block cipher mode to adhere to the RFC for JWA in jose/backends/cryptography_backend.py
See https://www.rfc-editor.org/rfc/rfc7518.html#section-5.3 for the official RFC requirements for JWA See panva/jose#678 for related discussion on this issue
1 parent 4b0701b commit c533ed6

File tree

4 files changed

+7
-1
lines changed

4 files changed

+7
-1
lines changed

jose/backends/_asn1.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
Required by rsa_backend but not cryptography_backend.
44
"""
5+
56
from pyasn1.codec.der import decoder, encoder
67
from pyasn1.type import namedtype, univ
78

jose/backends/cryptography_backend.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@ class CryptographyAESKey(Key):
439439
ALGORITHMS.A256KW: None,
440440
}
441441

442+
IV_BYTE_LENGTH_MODE_MAP = {"CBC": algorithms.AES.block_size // 8, "GCM": 96 // 8}
443+
442444
def __init__(self, key, algorithm):
443445
if algorithm not in ALGORITHMS.AES:
444446
raise JWKError("%s is not a valid AES algorithm" % algorithm)
@@ -468,7 +470,8 @@ def to_dict(self):
468470
def encrypt(self, plain_text, aad=None):
469471
plain_text = ensure_binary(plain_text)
470472
try:
471-
iv = get_random_bytes(algorithms.AES.block_size // 8)
473+
iv_byte_length = self.IV_BYTE_LENGTH_MODE_MAP.get(self._mode.name, algorithms.AES.block_size)
474+
iv = get_random_bytes(iv_byte_length)
472475
mode = self._mode(iv)
473476
if mode.name == "GCM":
474477
cipher = aead.AESGCM(self._key)

tests/test_asn1.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Tests for ``jose.backends._asn1``."""
2+
23
import base64
34

45
import pytest

tests/test_backends.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Test the default import handling."""
2+
23
try:
34
from jose.backends.rsa_backend import RSAKey as PurePythonRSAKey
45
except ImportError:

0 commit comments

Comments
 (0)