5151 from wolfcrypt .ciphers import (Ed448Private , Ed448Public )
5252
5353from wolfcrypt .ciphers import (
54- MODE_ECB , MODE_CBC , WolfCryptError
54+ MODE_CTR , MODE_ECB , MODE_CBC , WolfCryptError
5555)
5656
5757
5858@pytest .fixture
5959def vectors ():
60- TestVector = namedtuple ("TestVector" , """key iv plaintext ciphertext raw_key
61- pkcs8_key pem""" )
60+ TestVector = namedtuple ("TestVector" , """key iv plaintext ciphertext
61+ ciphertext_ctr raw_key
62+ pkcs8_key pem""" )
6263 TestVector .__new__ .__defaults__ = (None ,) * len (TestVector ._fields )
6364
6465 # test vector dictionary
@@ -69,7 +70,8 @@ def vectors():
6970 key = "0123456789abcdef" ,
7071 iv = "1234567890abcdef" ,
7172 plaintext = t2b ("now is the time " ),
72- ciphertext = h2b ("959492575f4281532ccc9d4677a233cb" )
73+ ciphertext = h2b ("959492575f4281532ccc9d4677a233cb" ),
74+ ciphertext_ctr = h2b ('287528ddf484b1055debbe751eb52b8a' )
7375 )
7476 if _lib .CHACHA_ENABLED :
7577 vectorArray [ChaCha ]= TestVector (
@@ -235,12 +237,12 @@ def cipher_new(cipher_cls, vectors):
235237 MODE_CBC ,
236238 vectors [cipher_cls ].iv )
237239
238-
239240def test_block_cipher (cipher_cls , vectors ):
240241 key = vectors [cipher_cls ].key
241242 iv = vectors [cipher_cls ].iv
242243 plaintext = vectors [cipher_cls ].plaintext
243244 ciphertext = vectors [cipher_cls ].ciphertext
245+ ciphertext_ctr = vectors [cipher_cls ].ciphertext_ctr
244246
245247 with pytest .raises (ValueError ):
246248 cipher_cls .new (key [:- 1 ], MODE_CBC , iv ) # invalid key length
@@ -257,11 +259,21 @@ def test_block_cipher(cipher_cls, vectors):
257259 with pytest .raises (ValueError ):
258260 cipher_cls .new (key , MODE_CBC , iv [:- 1 ]) # invalid iv length
259261
262+
263+ # Test AES in counter mode
264+ if ciphertext_ctr is not None :
265+ cipher_obj = cipher_cls .new (key , MODE_CTR , iv )
266+ res = cipher_obj .encrypt (plaintext )
267+ assert res == ciphertext_ctr
268+ cipher_obj = cipher_cls .new (key , MODE_CTR , iv )
269+ assert plaintext == cipher_obj .decrypt (res )
270+
260271 # single encryption
261272 cipher_obj = cipher_new (cipher_cls , vectors )
262273
263274 assert cipher_obj .encrypt (plaintext ) == ciphertext
264275
276+
265277 # many encryptions
266278 cipher_obj = cipher_new (cipher_cls , vectors )
267279 result = t2b ("" )
0 commit comments