ARC2.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/ARC2.py : ARC2.py
  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. """RC2 symmetric cipher
  23. RC2_ (Rivest's Cipher version 2) is a symmetric block cipher designed
  24. by Ron Rivest in 1987. The cipher started as a proprietary design,
  25. that was reverse engineered and anonymously posted on Usenet in 1996.
  26. For this reason, the algorithm was first called *Alleged* RC2 (ARC2),
  27. since the company that owned RC2 (RSA Data Inc.) did not confirm whether
  28. the details leaked into public domain were really correct.
  29. The company eventually published its full specification in RFC2268_.
  30. RC2 has a fixed data block size of 8 bytes. Length of its keys can vary from
  31. 8 to 128 bits. One particular property of RC2 is that the actual
  32. cryptographic strength of the key (*effective key length*) can be reduced
  33. via a parameter.
  34. Even though RC2 is not cryptographically broken, it has not been analyzed as
  35. thoroughly as AES, which is also faster than RC2.
  36. New designs should not use RC2.
  37. As an example, encryption can be done as follows:
  38. >>> from Crypto.Cipher import ARC2
  39. >>> from Crypto import Random
  40. >>>
  41. >>> key = b'Sixteen byte key'
  42. >>> iv = Random.new().read(ARC2.block_size)
  43. >>> cipher = ARC2.new(key, ARC2.MODE_CFB, iv)
  44. >>> msg = iv + cipher.encrypt(b'Attack at dawn')
  45. .. _RC2: http://en.wikipedia.org/wiki/RC2
  46. .. _RFC2268: http://tools.ietf.org/html/rfc2268
  47. :undocumented: __revision__, __package__
  48. """
  49. __revision__ = "$Id$"
  50. from Crypto.Cipher import blockalgo
  51. from Crypto.Cipher import _ARC2
  52. class RC2Cipher (blockalgo.BlockAlgo):
  53. """RC2 cipher object"""
  54. def __init__(self, key, *args, **kwargs):
  55. """Initialize an ARC2 cipher object
  56. See also `new()` at the module level."""
  57. blockalgo.BlockAlgo.__init__(self, _ARC2, key, *args, **kwargs)
  58. def new(key, *args, **kwargs):
  59. """Create a new RC2 cipher
  60. :Parameters:
  61. key : byte string
  62. The secret key to use in the symmetric cipher.
  63. Its length can vary from 1 to 128 bytes.
  64. :Keywords:
  65. mode : a *MODE_** constant
  66. The chaining mode to use for encryption or decryption.
  67. Default is `MODE_ECB`.
  68. IV : byte string
  69. The initialization vector to use for encryption or decryption.
  70. It is ignored for `MODE_ECB` and `MODE_CTR`.
  71. For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
  72. and `block_size` +2 bytes for decryption (in the latter case, it is
  73. actually the *encrypted* IV which was prefixed to the ciphertext).
  74. It is mandatory.
  75. For all other modes, it must be `block_size` bytes longs. It is optional and
  76. when not present it will be given a default value of all zeroes.
  77. counter : callable
  78. (*Only* `MODE_CTR`). A stateful function that returns the next
  79. *counter block*, which is a byte string of `block_size` bytes.
  80. For better performance, use `Crypto.Util.Counter`.
  81. segment_size : integer
  82. (*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
  83. are segmented in.
  84. It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
  85. effective_keylen : integer
  86. Maximum cryptographic strength of the key, in bits.
  87. It can vary from 0 to 1024. The default value is 1024.
  88. :Return: an `RC2Cipher` object
  89. """
  90. return RC2Cipher(key, *args, **kwargs)
  91. #: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
  92. MODE_ECB = 1
  93. #: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
  94. MODE_CBC = 2
  95. #: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
  96. MODE_CFB = 3
  97. #: This mode should not be used.
  98. MODE_PGP = 4
  99. #: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
  100. MODE_OFB = 5
  101. #: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
  102. MODE_CTR = 6
  103. #: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
  104. MODE_OPENPGP = 7
  105. #: Size of a data block (in bytes)
  106. block_size = 8
  107. #: Size of a key (in bytes)
  108. key_size = xrange(1,16+1)