MCPcopy Create free account
hub / github.com/authlib/authlib / AESAlgorithm

Class AESAlgorithm

authlib/jose/rfc7518/jwe_algs.py:86–122  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

84
85
86class AESAlgorithm(JWEAlgorithm):
87 def __init__(self, key_size):
88 self.name = f"A{key_size}KW"
89 self.description = f"AES Key Wrap using {key_size}-bit key"
90 self.key_size = key_size
91
92 def prepare_key(self, raw_data):
93 return OctKey.import_key(raw_data)
94
95 def generate_preset(self, enc_alg, key):
96 cek = enc_alg.generate_cek()
97 return {"cek": cek}
98
99 def _check_key(self, key):
100 if len(key) * 8 != self.key_size:
101 raise ValueError(f"A key of size {self.key_size} bits is required.")
102
103 def wrap_cek(self, cek, key):
104 op_key = key.get_op_key("wrapKey")
105 self._check_key(op_key)
106 ek = aes_key_wrap(op_key, cek, default_backend())
107 return {"ek": ek, "cek": cek}
108
109 def wrap(self, enc_alg, headers, key, preset=None):
110 if preset and "cek" in preset:
111 cek = preset["cek"]
112 else:
113 cek = enc_alg.generate_cek()
114 return self.wrap_cek(cek, key)
115
116 def unwrap(self, enc_alg, ek, headers, key):
117 op_key = key.get_op_key("unwrapKey")
118 self._check_key(op_key)
119 cek = aes_key_unwrap(op_key, ek, default_backend())
120 if len(cek) * 8 != enc_alg.CEK_SIZE:
121 cek = secrets.token_bytes(enc_alg.CEK_SIZE // 8)
122 return cek
123
124
125class AESGCMAlgorithm(JWEAlgorithm):

Callers 3

__init__Method · 0.90
__init__Method · 0.85
jwe_algs.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected