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