| 1 |
Triple DES |
| 2 |
========== |
| 3 |
|
| 4 |
.. warning:: |
| 5 |
Use :doc:`aes` instead. This module is provided only for legacy purposes. |
| 6 |
|
| 7 |
`Triple DES`__ (or TDES or TDEA or 3DES) is a symmetric block cipher |
| 8 |
standardized by NIST in |
| 9 |
`SP 800-67 Rev1 <http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-67r1.pdf>`_, |
| 10 |
though they will `deprecate`_ it soon. |
| 11 |
|
| 12 |
TDES has a fixed data block size of 8 bytes. |
| 13 |
It consists of the cascade of 3 :doc:`des` ciphers |
| 14 |
(EDE: Encryption - Decryption - Encryption), where each stage uses an |
| 15 |
independent DES sub-key. |
| 16 |
|
| 17 |
The standard defines 3 *Keying Options*: |
| 18 |
|
| 19 |
* *Option 1*: all sub-keys take different values (parity bits ignored). |
| 20 |
The TDES key is therefore 24 bytes long (concatenation of *K1*, *K2*, and |
| 21 |
*K3*) , to achieve 112 bits of effective security. |
| 22 |
|
| 23 |
* *Option 2*: *K1* matches *K3* but *K2* is different (parity bits ignored). |
| 24 |
The TDES key is 16 bytes long (concatenation of *K1* and *K2*), |
| 25 |
to achieve `90 bits`_ of effective security. |
| 26 |
In this mode, the cipher is also termed 2TDES. |
| 27 |
|
| 28 |
* *Option 3*: *K1* *K2*, and *K3* all match (parity bits ignored). |
| 29 |
As result, Triple DES degrades to Single DES. |
| 30 |
|
| 31 |
**This implementation does not support and will purposefully fail when |
| 32 |
attempting to configure the cipher in Option 3.** |
| 33 |
|
| 34 |
As an example, encryption can be done as follows:: |
| 35 |
|
| 36 |
>>> from Crypto.Cipher import DES3 |
| 37 |
>>> from Crypto.Random import get_random_bytes |
| 38 |
>>> |
| 39 |
>>> # Avoid Option 3 |
| 40 |
>>> while True: |
| 41 |
>>> try: |
| 42 |
>>> key = DES3.adjust_key_parity(get_random_bytes(24)) |
| 43 |
>>> break |
| 44 |
>>> except ValueError: |
| 45 |
>>> pass |
| 46 |
>>> |
| 47 |
>>> cipher = DES3.new(key, DES3.MODE_CFB) |
| 48 |
>>> plaintext = b'We are no longer the knights who say ni!' |
| 49 |
>>> msg = cipher.iv + cipher.encrypt(plaintext) |
| 50 |
|
| 51 |
.. __: http://en.wikipedia.org/wiki/Triple_DES |
| 52 |
.. _deprecate: https://csrc.nist.gov/News/2017/Update-to-Current-Use-and-Deprecation-of-TDEA |
| 53 |
.. _90 bits: http://people.scs.carleton.ca/~paulv/papers/Euro90.pdf |
| 54 |
|
| 55 |
.. automodule:: Crypto.Cipher.DES3 |
| 56 |
:members: |