OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # |
| 3 # Cipher/DES3.py : DES3 |
| 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 """Triple DES symmetric cipher |
| 23 |
| 24 `Triple DES`__ (or TDES or TDEA or 3DES) is a symmetric block cipher standardize
d by NIST_. |
| 25 It has a fixed data block size of 8 bytes. Its keys are 128 (*Option 1*) or 192 |
| 26 bits (*Option 2*) long. |
| 27 However, 1 out of 8 bits is used for redundancy and do not contribute to |
| 28 security. The effective key length is respectively 112 or 168 bits. |
| 29 |
| 30 TDES consists of the concatenation of 3 simple `DES` ciphers. |
| 31 |
| 32 The plaintext is first DES encrypted with *K1*, then decrypted with *K2*, |
| 33 and finally encrypted again with *K3*. The ciphertext is decrypted in the rever
se manner. |
| 34 |
| 35 The 192 bit key is a bundle of three 64 bit independent subkeys: *K1*, *K2*, and
*K3*. |
| 36 |
| 37 The 128 bit key is split into *K1* and *K2*, whereas *K1=K3*. |
| 38 |
| 39 It is important that all subkeys are different, otherwise TDES would degrade to |
| 40 single `DES`. |
| 41 |
| 42 TDES is cryptographically secure, even though it is neither as secure nor as fas
t |
| 43 as `AES`. |
| 44 |
| 45 As an example, encryption can be done as follows: |
| 46 |
| 47 >>> from Crypto.Cipher import DES |
| 48 >>> from Crypto import Random |
| 49 >>> from Crypto.Util import Counter |
| 50 >>> |
| 51 >>> key = b'-8B key-' |
| 52 >>> nonce = Random.new().read(DES.block_size/2) |
| 53 >>> ctr = Counter.new(DES.block_size*8/2, prefix=nonce) |
| 54 >>> cipher = DES.new(key, DES.MODE_CTR, counter=ctr) |
| 55 >>> plaintext = b'We are no longer the knights who say ni!' |
| 56 >>> msg = nonce + cipher.encrypt(plaintext) |
| 57 |
| 58 .. __: http://en.wikipedia.org/wiki/Triple_DES |
| 59 .. _NIST: http://csrc.nist.gov/publications/nistpubs/800-67/SP800-67.pdf |
| 60 |
| 61 :undocumented: __revision__, __package__ |
| 62 """ |
| 63 |
| 64 __revision__ = "$Id$" |
| 65 |
| 66 from Crypto.Cipher import blockalgo |
| 67 from Crypto.Cipher import _DES3 |
| 68 |
| 69 class DES3Cipher(blockalgo.BlockAlgo): |
| 70 """TDES cipher object""" |
| 71 |
| 72 def __init__(self, key, *args, **kwargs): |
| 73 """Initialize a TDES cipher object |
| 74 |
| 75 See also `new()` at the module level.""" |
| 76 blockalgo.BlockAlgo.__init__(self, _DES3, key, *args, **kwargs) |
| 77 |
| 78 def new(key, *args, **kwargs): |
| 79 """Create a new TDES cipher |
| 80 |
| 81 :Parameters: |
| 82 key : byte string |
| 83 The secret key to use in the symmetric cipher. |
| 84 It must be 16 or 24 bytes long. The parity bits will be ignored. |
| 85 :Keywords: |
| 86 mode : a *MODE_** constant |
| 87 The chaining mode to use for encryption or decryption. |
| 88 Default is `MODE_ECB`. |
| 89 IV : byte string |
| 90 The initialization vector to use for encryption or decryption. |
| 91 |
| 92 It is ignored for `MODE_ECB` and `MODE_CTR`. |
| 93 |
| 94 For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption |
| 95 and `block_size` +2 bytes for decryption (in the latter case, it is |
| 96 actually the *encrypted* IV which was prefixed to the ciphertext). |
| 97 It is mandatory. |
| 98 |
| 99 For all other modes, it must be `block_size` bytes longs. It is optional
and |
| 100 when not present it will be given a default value of all zeroes. |
| 101 counter : callable |
| 102 (*Only* `MODE_CTR`). A stateful function that returns the next |
| 103 *counter block*, which is a byte string of `block_size` bytes. |
| 104 For better performance, use `Crypto.Util.Counter`. |
| 105 segment_size : integer |
| 106 (*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext |
| 107 are segmented in. |
| 108 It must be a multiple of 8. If 0 or not specified, it will be assumed to
be 8. |
| 109 |
| 110 :Attention: it is important that all 8 byte subkeys are different, |
| 111 otherwise TDES would degrade to single `DES`. |
| 112 :Return: an `DES3Cipher` object |
| 113 """ |
| 114 return DES3Cipher(key, *args, **kwargs) |
| 115 |
| 116 #: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`. |
| 117 MODE_ECB = 1 |
| 118 #: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`. |
| 119 MODE_CBC = 2 |
| 120 #: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`. |
| 121 MODE_CFB = 3 |
| 122 #: This mode should not be used. |
| 123 MODE_PGP = 4 |
| 124 #: Output FeedBack (OFB). See `blockalgo.MODE_OFB`. |
| 125 MODE_OFB = 5 |
| 126 #: CounTer Mode (CTR). See `blockalgo.MODE_CTR`. |
| 127 MODE_CTR = 6 |
| 128 #: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`. |
| 129 MODE_OPENPGP = 7 |
| 130 #: Size of a data block (in bytes) |
| 131 block_size = 8 |
| 132 #: Size of a key (in bytes) |
| 133 key_size = ( 16, 24 ) |
OLD | NEW |