Skip to content

Commit

Permalink
Updating CryptographyAESKey::encrypt to generate 96 bit IVs for GCM…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
twwildey committed May 29, 2024
1 parent 4b0701b commit 7f9243c
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion jose/backends/cryptography_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@ class CryptographyAESKey(Key):
ALGORITHMS.A256KW: None,
}

IV_BYTE_LENGTH_MODE_MAP = {
'CBC': algorithms.AES.block_size // 8,
'GCM': 96 // 8
}

def __init__(self, key, algorithm):
if algorithm not in ALGORITHMS.AES:
raise JWKError("%s is not a valid AES algorithm" % algorithm)
Expand Down Expand Up @@ -468,7 +473,8 @@ def to_dict(self):
def encrypt(self, plain_text, aad=None):
plain_text = ensure_binary(plain_text)
try:
iv = get_random_bytes(algorithms.AES.block_size // 8)
iv_byte_length = self.IV_BYTE_LENGTH_MODE_MAP[self._mode.name] if self._mode.name in self.IV_BYTE_LENGTH_MODE_MAP else algorithms.AES.block_size
iv = get_random_bytes(iv_byte_length)
mode = self._mode(iv)
if mode.name == "GCM":
cipher = aead.AESGCM(self._key)
Expand Down

0 comments on commit 7f9243c

Please sign in to comment.