CAST.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/CAST.py : CAST
  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. """CAST-128 symmetric cipher
  23. CAST-128_ (or CAST5) is a symmetric block cipher specified in RFC2144_.
  24. It has a fixed data block size of 8 bytes. Its key can vary in length
  25. from 40 to 128 bits.
  26. CAST is deemed to be cryptographically secure, but its usage is not widespread.
  27. Keys of sufficient length should be used to prevent brute force attacks
  28. (128 bits are recommended).
  29. As an example, encryption can be done as follows:
  30. >>> from Crypto.Cipher import CAST
  31. >>> from Crypto import Random
  32. >>>
  33. >>> key = b'Sixteen byte key'
  34. >>> iv = Random.new().read(CAST.block_size)
  35. >>> cipher = CAST.new(key, CAST.MODE_OPENPGP, iv)
  36. >>> plaintext = b'sona si latine loqueris '
  37. >>> msg = cipher.encrypt(plaintext)
  38. >>>
  39. ...
  40. >>> eiv = msg[:CAST.block_size+2]
  41. >>> ciphertext = msg[CAST.block_size+2:]
  42. >>> cipher = CAST.new(key, CAST.MODE_OPENPGP, eiv)
  43. >>> print cipher.decrypt(ciphertext)
  44. .. _CAST-128: http://en.wikipedia.org/wiki/CAST-128
  45. .. _RFC2144: http://tools.ietf.org/html/rfc2144
  46. :undocumented: __revision__, __package__
  47. """
  48. __revision__ = "$Id$"
  49. from Crypto.Cipher import blockalgo
  50. from Crypto.Cipher import _CAST
  51. class CAST128Cipher(blockalgo.BlockAlgo):
  52. """CAST-128 cipher object"""
  53. def __init__(self, key, *args, **kwargs):
  54. """Initialize a CAST-128 cipher object
  55. See also `new()` at the module level."""
  56. blockalgo.BlockAlgo.__init__(self, _CAST, key, *args, **kwargs)
  57. def new(key, *args, **kwargs):
  58. """Create a new CAST-128 cipher
  59. :Parameters:
  60. key : byte string
  61. The secret key to use in the symmetric cipher.
  62. Its length may vary from 5 to 16 bytes.
  63. :Keywords:
  64. mode : a *MODE_** constant
  65. The chaining mode to use for encryption or decryption.
  66. Default is `MODE_ECB`.
  67. IV : byte string
  68. The initialization vector to use for encryption or decryption.
  69. It is ignored for `MODE_ECB` and `MODE_CTR`.
  70. For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
  71. and `block_size` +2 bytes for decryption (in the latter case, it is
  72. actually the *encrypted* IV which was prefixed to the ciphertext).
  73. It is mandatory.
  74. For all other modes, it must be `block_size` bytes longs. It is optional and
  75. when not present it will be given a default value of all zeroes.
  76. counter : callable
  77. (*Only* `MODE_CTR`). A stateful function that returns the next
  78. *counter block*, which is a byte string of `block_size` bytes.
  79. For better performance, use `Crypto.Util.Counter`.
  80. segment_size : integer
  81. (*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
  82. are segmented in.
  83. It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
  84. :Return: an `CAST128Cipher` object
  85. """
  86. return CAST128Cipher(key, *args, **kwargs)
  87. #: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
  88. MODE_ECB = 1
  89. #: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
  90. MODE_CBC = 2
  91. #: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
  92. MODE_CFB = 3
  93. #: This mode should not be used.
  94. MODE_PGP = 4
  95. #: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
  96. MODE_OFB = 5
  97. #: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
  98. MODE_CTR = 6
  99. #: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
  100. MODE_OPENPGP = 7
  101. #: Size of a data block (in bytes)
  102. block_size = 8
  103. #: Size of a key (in bytes)
  104. key_size = xrange(5,16+1)