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