Skip to content

Commit

Permalink
Use separate input_keysize property
Browse files Browse the repository at this point in the history
This allows to propery compute an octect key for algorithms like
A256CBC-HS512 ha sa different input keysize than the putput key size.

Signed-off-by: Simo Sorce <simo@redhat.com>
  • Loading branch information
simo5 committed May 30, 2023
1 parent 2fec703 commit e08cbf1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions jwcrypto/jwa.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def algorithm_usage_location(self):
def algorithm_use(self):
"""One of 'sig', 'kex', 'enc'"""

@property
def input_keysize(self):
"""The input key size"""
try:
return self.wrap_key_size
except AttributeError:
return self.keysize


def _bitsize(x):
return len(x) * 8
Expand Down Expand Up @@ -900,6 +908,9 @@ def encrypt(self, k, a, m):
Returns a dictionary with the computed data.
"""
if len(k) != _inbytes(self.wrap_key_size):
raise ValueError("Invalid input key size")

hkey = k[:_inbytes(self.keysize)]
ekey = k[_inbytes(self.keysize):]

Expand Down Expand Up @@ -928,6 +939,9 @@ def decrypt(self, k, a, iv, e, t):
Returns plaintext or raises an error
"""
if len(k) != _inbytes(self.wrap_key_size):
raise ValueError("Invalid input key size")

hkey = k[:_inbytes(self.keysize)]
dkey = k[_inbytes(self.keysize):]

Expand Down
2 changes: 1 addition & 1 deletion jwcrypto/jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def _get_gen_size(self, params, default_size=None):
alg = JWA.instantiate_alg(params['alg'])
except KeyError as e:
raise ValueError("Invalid 'alg' parameter") from e
size = alg.keysize
size = alg.input_keysize
return size

def _generate_oct(self, params):
Expand Down

0 comments on commit e08cbf1

Please sign in to comment.