__init__.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. def _create_cipher(factory, key, mode, *args, **kwargs):
  25. kwargs["key"] = key
  26. if args:
  27. if mode in (8, 9, 10, 11, 12):
  28. if len(args) > 1:
  29. raise TypeError("Too many arguments for this mode")
  30. kwargs["nonce"] = args[0]
  31. elif mode in (2, 3, 5, 7):
  32. if len(args) > 1:
  33. raise TypeError("Too many arguments for this mode")
  34. kwargs["IV"] = args[0]
  35. elif mode == 6:
  36. if len(args) > 0:
  37. raise TypeError("Too many arguments for this mode")
  38. elif mode == 1:
  39. raise TypeError("IV is not meaningful for the ECB mode")
  40. res = None
  41. extra_modes = kwargs.pop("add_aes_modes", False)
  42. if mode == 1:
  43. from Crypto.Cipher._mode_ecb import _create_ecb_cipher
  44. res = _create_ecb_cipher(factory, **kwargs)
  45. elif mode == 2:
  46. from Crypto.Cipher._mode_cbc import _create_cbc_cipher
  47. res = _create_cbc_cipher(factory, **kwargs)
  48. elif mode == 3:
  49. from Crypto.Cipher._mode_cfb import _create_cfb_cipher
  50. res = _create_cfb_cipher(factory, **kwargs)
  51. elif mode == 5:
  52. from Crypto.Cipher._mode_ofb import _create_ofb_cipher
  53. res = _create_ofb_cipher(factory, **kwargs)
  54. elif mode == 6:
  55. from Crypto.Cipher._mode_ctr import _create_ctr_cipher
  56. res = _create_ctr_cipher(factory, **kwargs)
  57. elif mode == 7:
  58. from Crypto.Cipher._mode_openpgp import _create_openpgp_cipher
  59. res = _create_openpgp_cipher(factory, **kwargs)
  60. elif mode == 9:
  61. from Crypto.Cipher._mode_eax import _create_eax_cipher
  62. res = _create_eax_cipher(factory, **kwargs)
  63. elif extra_modes:
  64. if mode == 8:
  65. from Crypto.Cipher._mode_ccm import _create_ccm_cipher
  66. res = _create_ccm_cipher(factory, **kwargs)
  67. elif mode == 10:
  68. from Crypto.Cipher._mode_siv import _create_siv_cipher
  69. res = _create_siv_cipher(factory, **kwargs)
  70. elif mode == 11:
  71. from Crypto.Cipher._mode_gcm import _create_gcm_cipher
  72. res = _create_gcm_cipher(factory, **kwargs)
  73. elif mode == 12:
  74. from Crypto.Cipher._mode_ocb import _create_ocb_cipher
  75. res = _create_ocb_cipher(factory, **kwargs)
  76. elif mode == 13:
  77. from Crypto.Cipher._mode_kw import _create_kw_cipher
  78. res = _create_kw_cipher(factory, **kwargs)
  79. elif mode == 14:
  80. from Crypto.Cipher._mode_kwp import _create_kwp_cipher
  81. res = _create_kwp_cipher(factory, **kwargs)
  82. if res is None:
  83. raise ValueError("Mode not supported")
  84. return res