@@ -198,15 +198,18 @@ cdef class _CipherBase:
198198 _cipher.mbedtls_cipher_set_padding_mode(
199199 & self ._dec_ctx, _cipher.MBEDTLS_PADDING_NONE)
200200
201-
202201 if key is not None :
203202 _exc.check_error(_cipher.mbedtls_cipher_setkey(
204203 & self ._enc_ctx, & key[0 ], 8 * key.size,
205204 _cipher.MBEDTLS_ENCRYPT))
206205 _exc.check_error(_cipher.mbedtls_cipher_setkey(
207206 & self ._dec_ctx, & key[0 ], 8 * key.size,
208207 _cipher.MBEDTLS_DECRYPT))
209- self ._iv = iv
208+
209+ _exc.check_error(_cipher.mbedtls_cipher_set_iv(
210+ & self ._enc_ctx, & iv[0 ] if iv.size else NULL , iv.size))
211+ _exc.check_error(_cipher.mbedtls_cipher_set_iv(
212+ & self ._dec_ctx, & iv[0 ] if iv.size else NULL , iv.size))
210213
211214 def __cinit__ (self ):
212215 """ Initialize a `cipher_context` (as NONE)."""
@@ -264,39 +267,35 @@ cdef class _CipherBase:
264267
265268
266269cdef class Cipher(_CipherBase):
267- cdef _crypt(self ,
268- const unsigned char [:] iv,
269- const unsigned char [:] input ,
270- const _cipher.mbedtls_operation_t operation):
271- """ Generic all-in-one encryption/decryption."""
270+ cdef _crypt(self ,
271+ _cipher.mbedtls_cipher_context_t * ctx,
272+ const unsigned char [:] input ):
272273 if input .size == 0 :
273274 _exc.check_error(- 0x6280 ) # Raise full block expected error.
274275 cdef size_t olen
276+ cdef size_t finish_olen
275277 cdef size_t sz = input .size + self .block_size
276278 cdef unsigned char * output = < unsigned char * > malloc(
277279 sz * sizeof(unsigned char ))
278280 if not output:
279281 raise MemoryError ()
280282 try :
281- # We can call `check_error` directly here because we return a
282- # python object.
283- err = _cipher.mbedtls_cipher_crypt(
284- & self ._enc_ctx if operation is _cipher.MBEDTLS_ENCRYPT else
285- & self ._dec_ctx,
286- & iv[0 ] if iv.size else NULL , iv.size,
287- & input [0 ], input .size, output, & olen)
283+ _exc.check_error(_cipher.mbedtls_cipher_reset(ctx))
284+ _exc.check_error(_cipher.mbedtls_cipher_update(
285+ ctx, & input [0 ], input .size, output, & olen))
286+ err = _cipher.mbedtls_cipher_finish(ctx, output + olen, & finish_olen)
288287 if err == - 0x6280 :
289288 raise ValueError (" expected a full block" )
290289 _exc.check_error(err)
291- return output[:olen]
290+ return output[:olen + finish_olen ]
292291 finally :
293292 free(output)
294293
295294 def encrypt (self , const unsigned char[:] message not None ):
296- return self ._crypt(self ._iv , message, _cipher.MBEDTLS_ENCRYPT )
295+ return self ._crypt(& self ._enc_ctx , message)
297296
298297 def decrypt (self , const unsigned char[:] message not None ):
299- return self ._crypt(self ._iv , message, _cipher.MBEDTLS_DECRYPT )
298+ return self ._crypt(& self ._dec_ctx , message)
300299
301300
302301cdef class AEADCipher(_CipherBase):
@@ -307,6 +306,7 @@ cdef class AEADCipher(_CipherBase):
307306 const unsigned char[:] iv not None ,
308307 const unsigned char[:] ad not None ):
309308 super ().__init__(cipher_name, key, mode, iv)
309+ self ._iv = iv
310310 self ._ad = ad
311311
312312 cdef _aead_encrypt(self ,
0 commit comments