__init__.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #
  2. # A block cipher is instantiated as a combination of:
  3. # 1. A base cipher (such as AES)
  4. # 2. A mode of operation (such as CBC)
  5. #
  6. # Both items are implemented as C modules.
  7. #
  8. # The API of #1 is (replace "AES" with the name of the actual cipher):
  9. # - AES_start_operaion(key) --> base_cipher_state
  10. # - AES_encrypt(base_cipher_state, in, out, length)
  11. # - AES_decrypt(base_cipher_state, in, out, length)
  12. # - AES_stop_operation(base_cipher_state)
  13. #
  14. # Where base_cipher_state is AES_State, a struct with BlockBase (set of
  15. # pointers to encrypt/decrypt/stop) followed by cipher-specific data.
  16. #
  17. # The API of #2 is (replace "CBC" with the name of the actual mode):
  18. # - CBC_start_operation(base_cipher_state) --> mode_state
  19. # - CBC_encrypt(mode_state, in, out, length)
  20. # - CBC_decrypt(mode_state, in, out, length)
  21. # - CBC_stop_operation(mode_state)
  22. #
  23. # where mode_state is a a pointer to base_cipher_state plus mode-specific data.
  24. import os
  25. from tls.Crypto.Cipher._mode_ecb import _create_ecb_cipher
  26. from tls.Crypto.Cipher._mode_cbc import _create_cbc_cipher
  27. from tls.Crypto.Cipher._mode_cfb import _create_cfb_cipher
  28. from tls.Crypto.Cipher._mode_ofb import _create_ofb_cipher
  29. from tls.Crypto.Cipher._mode_ctr import _create_ctr_cipher
  30. from tls.Crypto.Cipher._mode_openpgp import _create_openpgp_cipher
  31. from tls.Crypto.Cipher._mode_ccm import _create_ccm_cipher
  32. from tls.Crypto.Cipher._mode_eax import _create_eax_cipher
  33. from tls.Crypto.Cipher._mode_siv import _create_siv_cipher
  34. from tls.Crypto.Cipher._mode_gcm import _create_gcm_cipher
  35. from tls.Crypto.Cipher._mode_ocb import _create_ocb_cipher
  36. _modes = { 1:_create_ecb_cipher,
  37. 2:_create_cbc_cipher,
  38. 3:_create_cfb_cipher,
  39. 5:_create_ofb_cipher,
  40. 6:_create_ctr_cipher,
  41. 7:_create_openpgp_cipher,
  42. 9:_create_eax_cipher
  43. }
  44. _extra_modes = { 8:_create_ccm_cipher,
  45. 10:_create_siv_cipher,
  46. 11:_create_gcm_cipher,
  47. 12:_create_ocb_cipher
  48. }
  49. def _create_cipher(factory, key, mode, *args, **kwargs):
  50. kwargs["key"] = key
  51. modes = dict(_modes)
  52. if kwargs.pop("add_aes_modes", False):
  53. modes.update(_extra_modes)
  54. if not mode in modes:
  55. raise ValueError("Mode not supported")
  56. if args:
  57. if mode in (8, 9, 10, 11, 12):
  58. if len(args) > 1:
  59. raise TypeError("Too many arguments for this mode")
  60. kwargs["nonce"] = args[0]
  61. elif mode in (2, 3, 5, 7):
  62. if len(args) > 1:
  63. raise TypeError("Too many arguments for this mode")
  64. kwargs["IV"] = args[0]
  65. elif mode == 6:
  66. if len(args) > 0:
  67. raise TypeError("Too many arguments for this mode")
  68. elif mode == 1:
  69. raise TypeError("IV is not meaningful for the ECB mode")
  70. return modes[mode](factory, **kwargs)