AES.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/AES.py : AES
  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. """AES symmetric cipher
  23. AES `(Advanced Encryption Standard)`__ is a symmetric block cipher standardized
  24. by NIST_ . It has a fixed data block size of 16 bytes.
  25. Its keys can be 128, 192, or 256 bits long.
  26. AES is very fast and secure, and it is the de facto standard for symmetric
  27. encryption.
  28. As an example, encryption can be done as follows:
  29. >>> from Crypto.Cipher import AES
  30. >>> from Crypto import Random
  31. >>>
  32. >>> key = b'Sixteen byte key'
  33. >>> iv = Random.new().read(AES.block_size)
  34. >>> cipher = AES.new(key, AES.MODE_CFB, iv)
  35. >>> msg = iv + cipher.encrypt(b'Attack at dawn')
  36. .. __: http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
  37. .. _NIST: http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
  38. :undocumented: __revision__, __package__
  39. """
  40. __revision__ = "$Id$"
  41. from Crypto.Cipher import blockalgo
  42. from Crypto.Cipher import _AES
  43. class AESCipher (blockalgo.BlockAlgo):
  44. """AES cipher object"""
  45. def __init__(self, key, *args, **kwargs):
  46. """Initialize an AES cipher object
  47. See also `new()` at the module level."""
  48. blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
  49. def new(key, *args, **kwargs):
  50. """Create a new AES cipher
  51. :Parameters:
  52. key : byte string
  53. The secret key to use in the symmetric cipher.
  54. It must be 16 (*AES-128*), 24 (*AES-192*), or 32 (*AES-256*) bytes long.
  55. :Keywords:
  56. mode : a *MODE_** constant
  57. The chaining mode to use for encryption or decryption.
  58. Default is `MODE_ECB`.
  59. IV : byte string
  60. The initialization vector to use for encryption or decryption.
  61. It is ignored for `MODE_ECB` and `MODE_CTR`.
  62. For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
  63. and `block_size` +2 bytes for decryption (in the latter case, it is
  64. actually the *encrypted* IV which was prefixed to the ciphertext).
  65. It is mandatory.
  66. For all other modes, it must be `block_size` bytes longs. It is optional and
  67. when not present it will be given a default value of all zeroes.
  68. counter : callable
  69. (*Only* `MODE_CTR`). A stateful function that returns the next
  70. *counter block*, which is a byte string of `block_size` bytes.
  71. For better performance, use `Crypto.Util.Counter`.
  72. segment_size : integer
  73. (*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
  74. are segmented in.
  75. It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
  76. :Return: an `AESCipher` object
  77. """
  78. return AESCipher(key, *args, **kwargs)
  79. #: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
  80. MODE_ECB = 1
  81. #: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
  82. MODE_CBC = 2
  83. #: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
  84. MODE_CFB = 3
  85. #: This mode should not be used.
  86. MODE_PGP = 4
  87. #: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
  88. MODE_OFB = 5
  89. #: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
  90. MODE_CTR = 6
  91. #: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
  92. MODE_OPENPGP = 7
  93. #: Size of a data block (in bytes)
  94. block_size = 16
  95. #: Size of a key (in bytes)
  96. key_size = ( 16, 24, 32 )