A wrapper to present a cryptography api but use the pycryptodome module.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

62 lines
1.5 KiB

  1. from cryptography.hazmat.backends import default_backend
  2. from cryptography.hazmat.primitives.ciphers import (
  3. Cipher, algorithms, modes
  4. )
  5. from cryptography.exceptions import InvalidTag
  6. import random
  7. import unittest
  8. TAG_LENGTH = 16
  9. class TestAES(unittest.TestCase):
  10. def test_aesgcm_cav(self):
  11. pass
  12. def test_aesgcm(self):
  13. buf = bytes.fromhex('00000000000000000000000000000000')
  14. key = bytes.fromhex('00000000000000000000000000000000')
  15. iv = bytes.fromhex('000000000000000000000000')
  16. origdata = buf
  17. # encryption
  18. encryptor = Cipher(algorithms.AES(key),
  19. modes.GCM(iv), backend=default_backend()
  20. ).encryptor()
  21. last = True
  22. #if version == 'aes128gcm':
  23. data = encryptor.update(buf)
  24. self.assertEqual(data, bytes.fromhex('0388dace60b6a392f328c2b971b2fe78'))
  25. data += encryptor.finalize()
  26. self.assertEqual(encryptor.tag, bytes.fromhex('ab6e47d42cec13bdf53a67b21257bddf'))
  27. data += encryptor.tag
  28. encdata = data
  29. # decryption
  30. content = encdata
  31. decryptor = Cipher(algorithms.AES(key),
  32. modes.GCM(iv, tag=content[-TAG_LENGTH:]),
  33. backend=default_backend()
  34. ).decryptor()
  35. decdata = decryptor.update(content[:-TAG_LENGTH]) + decryptor.finalize()
  36. self.assertEqual(origdata, decdata)
  37. # decryption
  38. content = encdata
  39. decryptor = Cipher(algorithms.AES(key),
  40. modes.GCM(iv, tag=b'\x00' * TAG_LENGTH),
  41. backend=default_backend()
  42. ).decryptor()
  43. decdata = decryptor.update(content[:-TAG_LENGTH])
  44. self.assertRaises(InvalidTag, decryptor.finalize)