PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Windows/Python3.8/WPy64-3830/WPy64-3830/python-3.8.3.amd64/Lib/site-packages/Crypto/Cipher/Blowfish.py

https://gitlab.com/abhi1tb/build
Python | 159 lines | 126 code | 1 blank | 32 comment | 3 complexity | ae13eb8a113ebeb765269755567c9255 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/Blowfish.py : Blowfish
  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. Module's constants for the modes of operation supported with Blowfish:
  24. :var MODE_ECB: :ref:`Electronic Code Book (ECB) <ecb_mode>`
  25. :var MODE_CBC: :ref:`Cipher-Block Chaining (CBC) <cbc_mode>`
  26. :var MODE_CFB: :ref:`Cipher FeedBack (CFB) <cfb_mode>`
  27. :var MODE_OFB: :ref:`Output FeedBack (OFB) <ofb_mode>`
  28. :var MODE_CTR: :ref:`CounTer Mode (CTR) <ctr_mode>`
  29. :var MODE_OPENPGP: :ref:`OpenPGP Mode <openpgp_mode>`
  30. :var MODE_EAX: :ref:`EAX Mode <eax_mode>`
  31. """
  32. import sys
  33. from Crypto.Cipher import _create_cipher
  34. from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
  35. VoidPointer, SmartPointer, c_size_t,
  36. c_uint8_ptr)
  37. _raw_blowfish_lib = load_pycryptodome_raw_lib(
  38. "Crypto.Cipher._raw_blowfish",
  39. """
  40. int Blowfish_start_operation(const uint8_t key[],
  41. size_t key_len,
  42. void **pResult);
  43. int Blowfish_encrypt(const void *state,
  44. const uint8_t *in,
  45. uint8_t *out,
  46. size_t data_len);
  47. int Blowfish_decrypt(const void *state,
  48. const uint8_t *in,
  49. uint8_t *out,
  50. size_t data_len);
  51. int Blowfish_stop_operation(void *state);
  52. """
  53. )
  54. def _create_base_cipher(dict_parameters):
  55. """This method instantiates and returns a smart pointer to
  56. a low-level base cipher. It will absorb named parameters in
  57. the process."""
  58. try:
  59. key = dict_parameters.pop("key")
  60. except KeyError:
  61. raise TypeError("Missing 'key' parameter")
  62. if len(key) not in key_size:
  63. raise ValueError("Incorrect Blowfish key length (%d bytes)" % len(key))
  64. start_operation = _raw_blowfish_lib.Blowfish_start_operation
  65. stop_operation = _raw_blowfish_lib.Blowfish_stop_operation
  66. void_p = VoidPointer()
  67. result = start_operation(c_uint8_ptr(key),
  68. c_size_t(len(key)),
  69. void_p.address_of())
  70. if result:
  71. raise ValueError("Error %X while instantiating the Blowfish cipher"
  72. % result)
  73. return SmartPointer(void_p.get(), stop_operation)
  74. def new(key, mode, *args, **kwargs):
  75. """Create a new Blowfish cipher
  76. :param key:
  77. The secret key to use in the symmetric cipher.
  78. Its length can vary from 5 to 56 bytes.
  79. :type key: bytes, bytearray, memoryview
  80. :param mode:
  81. The chaining mode to use for encryption or decryption.
  82. :type mode: One of the supported ``MODE_*`` constants
  83. :Keyword Arguments:
  84. * **iv** (*bytes*, *bytearray*, *memoryview*) --
  85. (Only applicable for ``MODE_CBC``, ``MODE_CFB``, ``MODE_OFB``,
  86. and ``MODE_OPENPGP`` modes).
  87. The initialization vector to use for encryption or decryption.
  88. For ``MODE_CBC``, ``MODE_CFB``, and ``MODE_OFB`` it must be 8 bytes long.
  89. For ``MODE_OPENPGP`` mode only,
  90. it must be 8 bytes long for encryption
  91. and 10 bytes for decryption (in the latter case, it is
  92. actually the *encrypted* IV which was prefixed to the ciphertext).
  93. If not provided, a random byte string is generated (you must then
  94. read its value with the :attr:`iv` attribute).
  95. * **nonce** (*bytes*, *bytearray*, *memoryview*) --
  96. (Only applicable for ``MODE_EAX`` and ``MODE_CTR``).
  97. A value that must never be reused for any other encryption done
  98. with this key.
  99. For ``MODE_EAX`` there are no
  100. restrictions on its length (recommended: **16** bytes).
  101. For ``MODE_CTR``, its length must be in the range **[0..7]**.
  102. If not provided for ``MODE_EAX``, a random byte string is generated (you
  103. can read it back via the ``nonce`` attribute).
  104. * **segment_size** (*integer*) --
  105. (Only ``MODE_CFB``).The number of **bits** the plaintext and ciphertext
  106. are segmented in. It must be a multiple of 8.
  107. If not specified, it will be assumed to be 8.
  108. * **mac_len** : (*integer*) --
  109. (Only ``MODE_EAX``)
  110. Length of the authentication tag, in bytes.
  111. It must be no longer than 8 (default).
  112. * **initial_value** : (*integer*) --
  113. (Only ``MODE_CTR``). The initial value for the counter within
  114. the counter block. By default it is **0**.
  115. :Return: a Blowfish object, of the applicable mode.
  116. """
  117. return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
  118. MODE_ECB = 1
  119. MODE_CBC = 2
  120. MODE_CFB = 3
  121. MODE_OFB = 5
  122. MODE_CTR = 6
  123. MODE_OPENPGP = 7
  124. MODE_EAX = 9
  125. # Size of a data block (in bytes)
  126. block_size = 8
  127. # Size of a key (in bytes)
  128. key_size = range(4, 56 + 1)