DES3.py 5.0 KB

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