_mode_ecb.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/mode_ecb.py : ECB mode
  4. #
  5. # ===================================================================
  6. # The contents of this file are dedicated to the public domain. To
  7. # the extent that dedication to the public domain is not available,
  8. # everyone is granted a worldwide, perpetual, royalty-free,
  9. # non-exclusive license to exercise all rights associated with the
  10. # contents of this file for any purpose whatsoever.
  11. # No rights are reserved.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. # ===================================================================
  22. """
  23. Electronic Code Book (ECB) mode.
  24. """
  25. __all__ = [ 'EcbMode' ]
  26. from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
  27. VoidPointer, create_string_buffer,
  28. get_raw_buffer, SmartPointer,
  29. c_size_t, expect_byte_string)
  30. raw_ecb_lib = load_pycryptodome_raw_lib("Crypto.Cipher._raw_ecb", """
  31. int ECB_start_operation(void *cipher,
  32. void **pResult);
  33. int ECB_encrypt(void *ecbState,
  34. const uint8_t *in,
  35. uint8_t *out,
  36. size_t data_len);
  37. int ECB_decrypt(void *ecbState,
  38. const uint8_t *in,
  39. uint8_t *out,
  40. size_t data_len);
  41. int ECB_stop_operation(void *state);
  42. """
  43. )
  44. class EcbMode(object):
  45. """*Electronic Code Book (ECB)*.
  46. This is the simplest encryption mode. Each of the plaintext blocks
  47. is directly encrypted into a ciphertext block, independently of
  48. any other block.
  49. This mode is dangerous because it exposes frequency of symbols
  50. in your plaintext. Other modes (e.g. *CBC*) should be used instead.
  51. See `NIST SP800-38A`_ , Section 6.1.
  52. .. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  53. :undocumented: __init__
  54. """
  55. def __init__(self, block_cipher):
  56. """Create a new block cipher, configured in ECB mode.
  57. :Parameters:
  58. block_cipher : C pointer
  59. A smart pointer to the low-level block cipher instance.
  60. """
  61. self._state = VoidPointer()
  62. result = raw_ecb_lib.ECB_start_operation(block_cipher.get(),
  63. self._state.address_of())
  64. if result:
  65. raise ValueError("Error %d while instatiating the ECB mode"
  66. % result)
  67. # Ensure that object disposal of this Python object will (eventually)
  68. # free the memory allocated by the raw library for the cipher
  69. # mode
  70. self._state = SmartPointer(self._state.get(),
  71. raw_ecb_lib.ECB_stop_operation)
  72. # Memory allocated for the underlying block cipher is now owned
  73. # by the cipher mode
  74. block_cipher.release()
  75. def encrypt(self, plaintext):
  76. """Encrypt data with the key set at initialization.
  77. The data to encrypt can be broken up in two or
  78. more pieces and `encrypt` can be called multiple times.
  79. That is, the statement:
  80. >>> c.encrypt(a) + c.encrypt(b)
  81. is equivalent to:
  82. >>> c.encrypt(a+b)
  83. This function does not add any padding to the plaintext.
  84. :Parameters:
  85. plaintext : byte string
  86. The piece of data to encrypt.
  87. The length must be multiple of the cipher block length.
  88. :Return:
  89. the encrypted data, as a byte string.
  90. It is as long as *plaintext*.
  91. """
  92. expect_byte_string(plaintext)
  93. ciphertext = create_string_buffer(len(plaintext))
  94. result = raw_ecb_lib.ECB_encrypt(self._state.get(),
  95. plaintext,
  96. ciphertext,
  97. c_size_t(len(plaintext)))
  98. if result:
  99. raise ValueError("Error %d while encrypting in ECB mode" % result)
  100. return get_raw_buffer(ciphertext)
  101. def decrypt(self, ciphertext):
  102. """Decrypt data with the key set at initialization.
  103. The data to decrypt can be broken up in two or
  104. more pieces and `decrypt` can be called multiple times.
  105. That is, the statement:
  106. >>> c.decrypt(a) + c.decrypt(b)
  107. is equivalent to:
  108. >>> c.decrypt(a+b)
  109. This function does not remove any padding from the plaintext.
  110. :Parameters:
  111. ciphertext : byte string
  112. The piece of data to decrypt.
  113. The length must be multiple of the cipher block length.
  114. :Return:
  115. the decrypted data (byte string).
  116. It is as long as *ciphertext*.
  117. """
  118. expect_byte_string(ciphertext)
  119. plaintext = create_string_buffer(len(ciphertext))
  120. result = raw_ecb_lib.ECB_decrypt(self._state.get(),
  121. ciphertext,
  122. plaintext,
  123. c_size_t(len(ciphertext)))
  124. if result:
  125. raise ValueError("Error %d while decrypting in ECB mode" % result)
  126. return get_raw_buffer(plaintext)
  127. def _create_ecb_cipher(factory, **kwargs):
  128. """Instantiate a cipher object that performs ECB encryption/decryption.
  129. :Parameters:
  130. factory : module
  131. The underlying block cipher, a module from ``Crypto.Cipher``.
  132. All keywords are passed to the underlying block cipher.
  133. See the relevant documentation for details (at least ``key`` will need
  134. to be present"""
  135. cipher_state = factory._create_base_cipher(kwargs)
  136. if kwargs:
  137. raise TypeError("Unknown parameters for ECB: %s" % str(kwargs))
  138. return EcbMode(cipher_state)