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