1
# -*- coding: utf-8 -*-
3
# Cipher/Blowfish.py : Blowfish
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.
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
21
# ===================================================================
22
"""Blowfish symmetric cipher
24
Blowfish_ is a symmetric block cipher designed by Bruce Schneier.
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).
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).
32
As an example, encryption can be done as follows:
34
>>> from Crypto.Cipher import Blowfish
35
>>> from Crypto import Random
36
>>> from struct import pack
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)
48
.. _Blowfish: http://www.schneier.com/blowfish.html
50
:undocumented: __revision__, __package__
55
from Crypto.Cipher import blockalgo
56
from Crypto.Cipher import _Blowfish
58
class BlowfishCipher (blockalgo.BlockAlgo):
59
"""Blowfish cipher object"""
61
def __init__(self, key, *args, **kwargs):
62
"""Initialize a Blowfish cipher object
64
See also `new()` at the module level."""
65
blockalgo.BlockAlgo.__init__(self, _Blowfish, key, *args, **kwargs)
67
def new(key, *args, **kwargs):
68
"""Create a new Blowfish cipher
72
The secret key to use in the symmetric cipher.
73
Its length can vary from 4 to 56 bytes.
75
mode : a *MODE_** constant
76
The chaining mode to use for encryption or decryption.
77
Default is `MODE_ECB`.
79
The initialization vector to use for encryption or decryption.
81
It is ignored for `MODE_ECB` and `MODE_CTR`.
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).
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.
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
97
It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
99
:Return: a `BlowfishCipher` object
101
return BlowfishCipher(key, *args, **kwargs)
103
#: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
105
#: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
107
#: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
109
#: This mode should not be used.
111
#: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
113
#: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
115
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
117
#: Size of a data block (in bytes)
119
#: Size of a key (in bytes)
120
key_size = xrange(4,56+1)