PageRenderTime 105ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Crypto/Cipher/Blowfish.py

https://gitlab.com/grayhamster/pycrypto
Python | 132 lines | 94 code | 0 blank | 38 comment | 0 complexity | 16f3cd69a2e9b41d8a4022a4aa0e90fc 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. """Blowfish symmetric cipher
  23. Blowfish_ is a symmetric block cipher designed by Bruce Schneier.
  24. It has a fixed data block size of 8 bytes and its keys can vary in length
  25. from 32 to 448 bits (4 to 56 bytes).
  26. Blowfish is deemed secure and it is fast. However, its keys should be chosen
  27. to be big enough to withstand a brute force attack (e.g. at least 16 bytes).
  28. As an example, encryption can be done as follows:
  29. >>> from Crypto.Cipher import Blowfish
  30. >>> from Crypto import Random
  31. >>> from struct import pack
  32. >>>
  33. >>> bs = Blowfish.block_size
  34. >>> key = b'An arbitrarily long key'
  35. >>> iv = Random.new().read(bs)
  36. >>> cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv)
  37. >>> plaintext = b'docendo discimus '
  38. >>> plen = bs - divmod(len(plaintext),bs)[1]
  39. >>> padding = [plen]*plen
  40. >>> padding = pack('b'*plen, *padding)
  41. >>> msg = iv + cipher.encrypt(plaintext + padding)
  42. .. _Blowfish: http://www.schneier.com/blowfish.html
  43. :undocumented: __revision__, __package__
  44. """
  45. __revision__ = "$Id$"
  46. from Crypto.Cipher import blockalgo
  47. from Crypto.Cipher import _Blowfish
  48. class BlowfishCipher (blockalgo.BlockAlgo):
  49. """Blowfish cipher object"""
  50. def __init__(self, key, *args, **kwargs):
  51. """Initialize a Blowfish cipher object
  52. See also `new()` at the module level."""
  53. blockalgo.BlockAlgo.__init__(self, _Blowfish, key, *args, **kwargs)
  54. def new(key, *args, **kwargs):
  55. """Create a new Blowfish cipher
  56. :Parameters:
  57. key : byte string
  58. The secret key to use in the symmetric cipher.
  59. Its length can vary from 4 to 56 bytes.
  60. :Keywords:
  61. mode : a *MODE_** constant
  62. The chaining mode to use for encryption or decryption.
  63. Default is `MODE_ECB`.
  64. IV : byte string
  65. (*Only* `MODE_CBC`, `MODE_CFB`, `MODE_OFB`, `MODE_OPENPGP`).
  66. The initialization vector to use for encryption or decryption.
  67. It is ignored for `MODE_ECB` and `MODE_CTR`.
  68. For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
  69. and `block_size` +2 bytes for decryption (in the latter case, it is
  70. actually the *encrypted* IV which was prefixed to the ciphertext).
  71. It is mandatory.
  72. For all other modes, it must be 8 bytes long.
  73. nonce : byte string
  74. (*Only* `MODE_EAX`).
  75. A mandatory value that must never be reused for any other encryption.
  76. There are no restrictions on its length, but it is recommended to
  77. use at least 16 bytes.
  78. counter : callable
  79. (*Only* `MODE_CTR`). A stateful function that returns the next
  80. *counter block*, which is a byte string of `block_size` bytes.
  81. For better performance, use `Crypto.Util.Counter`.
  82. mac_len : integer
  83. (*Only* `MODE_EAX`). Length of the MAC, in bytes.
  84. It must be no larger than 8 (which is the default).
  85. segment_size : integer
  86. (*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
  87. are segmented in.
  88. It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
  89. :Return: a `BlowfishCipher` object
  90. """
  91. return BlowfishCipher(key, *args, **kwargs)
  92. #: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
  93. MODE_ECB = 1
  94. #: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
  95. MODE_CBC = 2
  96. #: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
  97. MODE_CFB = 3
  98. #: This mode should not be used.
  99. MODE_PGP = 4
  100. #: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
  101. MODE_OFB = 5
  102. #: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
  103. MODE_CTR = 6
  104. #: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
  105. MODE_OPENPGP = 7
  106. #: EAX Mode. See `blockalgo.MODE_EAX`.
  107. MODE_EAX = 9
  108. #: Size of a data block (in bytes)
  109. block_size = 8
  110. #: Size of a key (in bytes)
  111. key_size = xrange(4,56+1)