1
+ import contextlib
1
2
import struct
2
3
3
4
from cryptography .hazmat .primitives .asymmetric import ec
@@ -110,8 +111,8 @@ def enc_setup(self, msg, key=None, auth_data=b"", **kwargs):
110
111
if self .alg == "ECDH-ES" :
111
112
try :
112
113
dk_len = KEY_LEN [self .enc ]
113
- except KeyError :
114
- raise ValueError ("Unknown key length for algorithm %s" % self .enc )
114
+ except KeyError as exc :
115
+ raise ValueError (f "Unknown key length for algorithm { self .enc } " ) from exc
115
116
116
117
cek = ecdh_derive_key (_epk , key .pub_key , apu , apv , str (self .enc ).encode (), dk_len )
117
118
elif self .alg in ["ECDH-ES+A128KW" , "ECDH-ES+A192KW" , "ECDH-ES+A256KW" ]:
@@ -121,7 +122,7 @@ def enc_setup(self, msg, key=None, auth_data=b"", **kwargs):
121
122
cek = self ._generate_key (self .enc , cek = cek )
122
123
encrypted_key = aes_key_wrap (kek , cek )
123
124
else :
124
- raise Exception ("Unsupported algorithm %s" % self .alg )
125
+ raise Exception (f "Unsupported algorithm { self .alg } " )
125
126
126
127
return cek , encrypted_key , iv , params , epk
127
128
@@ -152,8 +153,8 @@ def dec_setup(self, token, key=None, **kwargs):
152
153
if self .headers ["alg" ] == "ECDH-ES" :
153
154
try :
154
155
dk_len = KEY_LEN [self .headers ["enc" ]]
155
- except KeyError :
156
- raise Exception ("Unknown key length for algorithm" )
156
+ except KeyError as exc :
157
+ raise Exception ("Unknown key length for algorithm" ) from exc
157
158
158
159
self .cek = ecdh_derive_key (
159
160
key ,
@@ -173,7 +174,7 @@ def dec_setup(self, token, key=None, **kwargs):
173
174
kek = ecdh_derive_key (key , epubkey .pub_key , apu , apv , str (_post ).encode (), klen )
174
175
self .cek = aes_key_unwrap (kek , token .encrypted_key ())
175
176
else :
176
- raise Exception ("Unsupported algorithm %s" % self .headers ["alg" ])
177
+ raise Exception ("Unsupported algorithm {}" . format ( self .headers ["alg" ]) )
177
178
178
179
return self .cek
179
180
@@ -190,10 +191,8 @@ def encrypt(self, key=None, iv="", cek="", **kwargs):
190
191
_msg = as_bytes (self .msg )
191
192
192
193
_args = self ._dict
193
- try :
194
+ with contextlib . suppress ( KeyError ) :
194
195
_args ["kid" ] = kwargs ["kid" ]
195
- except KeyError :
196
- pass
197
196
198
197
if "params" in kwargs :
199
198
if "apu" in kwargs ["params" ]:
@@ -204,23 +203,20 @@ def encrypt(self, key=None, iv="", cek="", **kwargs):
204
203
_args ["epk" ] = kwargs ["params" ]["epk" ]
205
204
206
205
jwe = JWEnc (** _args )
207
- ctxt , tag , cek = super (JWE_EC , self ).enc_setup (
206
+ ctxt , tag , cek = super ().enc_setup (
208
207
self ["enc" ], _msg , auth_data = jwe .b64_encode_header (), key = cek , iv = iv
209
208
)
210
209
if "encrypted_key" in kwargs :
211
210
return jwe .pack (parts = [kwargs ["encrypted_key" ], iv , ctxt , tag ])
212
211
return jwe .pack (parts = [iv , ctxt , tag ])
213
212
214
213
def decrypt (self , token = None , ** kwargs ):
215
- if isinstance (token , JWEnc ):
216
- jwe = token
217
- else :
218
- jwe = JWEnc ().unpack (token )
214
+ jwe = token if isinstance (token , JWEnc ) else JWEnc ().unpack (token )
219
215
220
216
if not self .cek :
221
217
raise Exception ("Content Encryption Key is Not Yet Set" )
222
218
223
- msg = super (JWE_EC , self )._decrypt (
219
+ msg = super ()._decrypt (
224
220
self .headers ["enc" ],
225
221
self .cek ,
226
222
self .ctxt ,
0 commit comments