PageRenderTime 31ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/abhi1tb/build
Python | 131 lines | 38 code | 15 blank | 78 comment | 4 complexity | 5a8184b824facf55f9ed87c917357d84 MD5 | raw file
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2019, Legrandin <helderijs@gmail.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. # ===================================================================
  30. import sys
  31. from Crypto.Cipher import _create_cipher
  32. from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
  33. VoidPointer, SmartPointer, c_size_t,
  34. c_uint8_ptr, c_uint)
  35. _raw_blowfish_lib = load_pycryptodome_raw_lib(
  36. "Crypto.Cipher._raw_eksblowfish",
  37. """
  38. int EKSBlowfish_start_operation(const uint8_t key[],
  39. size_t key_len,
  40. const uint8_t salt[16],
  41. size_t salt_len,
  42. unsigned cost,
  43. unsigned invert,
  44. void **pResult);
  45. int EKSBlowfish_encrypt(const void *state,
  46. const uint8_t *in,
  47. uint8_t *out,
  48. size_t data_len);
  49. int EKSBlowfish_decrypt(const void *state,
  50. const uint8_t *in,
  51. uint8_t *out,
  52. size_t data_len);
  53. int EKSBlowfish_stop_operation(void *state);
  54. """
  55. )
  56. def _create_base_cipher(dict_parameters):
  57. """This method instantiates and returns a smart pointer to
  58. a low-level base cipher. It will absorb named parameters in
  59. the process."""
  60. try:
  61. key = dict_parameters.pop("key")
  62. salt = dict_parameters.pop("salt")
  63. cost = dict_parameters.pop("cost")
  64. except KeyError as e:
  65. raise TypeError("Missing EKSBlowfish parameter: " + str(e))
  66. invert = dict_parameters.pop("invert", True)
  67. if len(key) not in key_size:
  68. raise ValueError("Incorrect EKSBlowfish key length (%d bytes)" % len(key))
  69. start_operation = _raw_blowfish_lib.EKSBlowfish_start_operation
  70. stop_operation = _raw_blowfish_lib.EKSBlowfish_stop_operation
  71. void_p = VoidPointer()
  72. result = start_operation(c_uint8_ptr(key),
  73. c_size_t(len(key)),
  74. c_uint8_ptr(salt),
  75. c_size_t(len(salt)),
  76. c_uint(cost),
  77. c_uint(int(invert)),
  78. void_p.address_of())
  79. if result:
  80. raise ValueError("Error %X while instantiating the EKSBlowfish cipher"
  81. % result)
  82. return SmartPointer(void_p.get(), stop_operation)
  83. def new(key, mode, salt, cost, invert):
  84. """Create a new EKSBlowfish cipher
  85. Args:
  86. key (bytes, bytearray, memoryview):
  87. The secret key to use in the symmetric cipher.
  88. Its length can vary from 0 to 72 bytes.
  89. mode (one of the supported ``MODE_*`` constants):
  90. The chaining mode to use for encryption or decryption.
  91. salt (bytes, bytearray, memoryview):
  92. The salt that bcrypt uses to thwart rainbow table attacks
  93. cost (integer):
  94. The complexity factor in bcrypt
  95. invert (bool):
  96. If ``False``, in the inner loop use ``ExpandKey`` first over the salt
  97. and then over the key, as defined in
  98. the `original bcrypt specification <https://www.usenix.org/legacy/events/usenix99/provos/provos_html/node4.html>`_.
  99. If ``True``, reverse the order, as in the first implementation of
  100. `bcrypt` in OpenBSD.
  101. :Return: an EKSBlowfish object
  102. """
  103. kwargs = { 'salt':salt, 'cost':cost, 'invert':invert }
  104. return _create_cipher(sys.modules[__name__], key, mode, **kwargs)
  105. MODE_ECB = 1
  106. # Size of a data block (in bytes)
  107. block_size = 8
  108. # Size of a key (in bytes)
  109. key_size = range(0, 72 + 1)