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