updated pycrypto
This commit is contained in:
@@ -31,87 +31,32 @@ encryption.
|
||||
As an example, encryption can be done as follows:
|
||||
|
||||
>>> from Crypto.Cipher import AES
|
||||
>>> from Crypto.Random import get_random_bytes
|
||||
>>> from Crypto import Random
|
||||
>>>
|
||||
>>> key = b'Sixteen byte key'
|
||||
>>> iv = get_random_bytes(16)
|
||||
>>> iv = Random.new().read(AES.block_size)
|
||||
>>> cipher = AES.new(key, AES.MODE_CFB, iv)
|
||||
>>> msg = iv + cipher.encrypt(b'Attack at dawn')
|
||||
|
||||
A more complicated example is based on CCM, (see `MODE_CCM`) an `AEAD`_ mode
|
||||
that provides both confidentiality and authentication for a message.
|
||||
It also allows message for the header to remain in the clear, whilst still
|
||||
being authenticated. The encryption is done as follows:
|
||||
|
||||
>>> from Crypto.Cipher import AES
|
||||
>>> from Crypto.Random import get_random_bytes
|
||||
>>>
|
||||
>>>
|
||||
>>> hdr = b'To your eyes only'
|
||||
>>> plaintext = b'Attack at dawn'
|
||||
>>> key = b'Sixteen byte key'
|
||||
>>> nonce = get_random_bytes(11)
|
||||
>>> cipher = AES.new(key, AES.MODE_CCM, nonce)
|
||||
>>> cipher.update(hdr)
|
||||
>>> msg = nonce, hdr, cipher.encrypt(plaintext), cipher.digest()
|
||||
|
||||
We assume that the tuple ``msg`` is transmitted to the receiver:
|
||||
|
||||
>>> nonce, hdr, ciphertext, mac = msg
|
||||
>>> key = b'Sixteen byte key'
|
||||
>>> cipher = AES.new(key, AES.MODE_CCM, nonce)
|
||||
>>> cipher.update(hdr)
|
||||
>>> plaintext = cipher.decrypt(ciphertext)
|
||||
>>> try:
|
||||
>>> cipher.verify(mac)
|
||||
>>> print "The message is authentic: hdr=%s, pt=%s" % (hdr, plaintext)
|
||||
>>> except ValueError:
|
||||
>>> print "Key incorrect or message corrupted"
|
||||
|
||||
.. __: http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
|
||||
.. _NIST: http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
|
||||
.. _AEAD: http://blog.cryptographyengineering.com/2012/05/how-to-choose-authenticated-encryption.html
|
||||
|
||||
:undocumented: __revision__, __package__
|
||||
"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
import sys
|
||||
if sys.version_info[0] == 2 and sys.version_info[1] == 1:
|
||||
from Crypto.Util.py21compat import *
|
||||
from Crypto.Cipher import blockalgo
|
||||
from Crypto.Cipher import _AES
|
||||
from Crypto.Util import cpuid
|
||||
# Import _AESNI. If AES-NI is not available or _AESNI has not been built, set
|
||||
# _AESNI to None.
|
||||
try:
|
||||
if cpuid.have_aes_ni():
|
||||
from Crypto.Cipher import _AESNI
|
||||
else:
|
||||
_AESNI = None
|
||||
except ImportError:
|
||||
_AESNI = None
|
||||
|
||||
class AESCipher (blockalgo.BlockAlgo):
|
||||
"""AES cipher object"""
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize an AES cipher object
|
||||
|
||||
|
||||
See also `new()` at the module level."""
|
||||
|
||||
# Check if the use_aesni was specified.
|
||||
use_aesni = True
|
||||
if kwargs.has_key('use_aesni'):
|
||||
use_aesni = kwargs['use_aesni']
|
||||
del kwargs['use_aesni']
|
||||
|
||||
# Use _AESNI if the user requested AES-NI and it's available
|
||||
if _AESNI is not None and use_aesni:
|
||||
blockalgo.BlockAlgo.__init__(self, _AESNI, key, *args, **kwargs)
|
||||
else:
|
||||
blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
|
||||
blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
|
||||
|
||||
def new(key, *args, **kwargs):
|
||||
"""Create a new AES cipher
|
||||
@@ -120,15 +65,11 @@ def new(key, *args, **kwargs):
|
||||
key : byte string
|
||||
The secret key to use in the symmetric cipher.
|
||||
It must be 16 (*AES-128*), 24 (*AES-192*), or 32 (*AES-256*) bytes long.
|
||||
|
||||
Only in `MODE_SIV`, it needs to be 32, 48, or 64 bytes long.
|
||||
:Keywords:
|
||||
mode : a *MODE_** constant
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
(*Only* `MODE_CBC`, `MODE_CFB`, `MODE_OFB`, `MODE_OPENPGP`).
|
||||
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
@@ -137,20 +78,9 @@ def new(key, *args, **kwargs):
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be 16 bytes long.
|
||||
nonce : byte string
|
||||
(*Only* `MODE_CCM`, `MODE_EAX`, `MODE_GCM`, `MODE_SIV`).
|
||||
|
||||
A mandatory value that must never be reused for any other encryption.
|
||||
|
||||
For `MODE_CCM`, its length must be in the range ``[7..13]``.
|
||||
11 or 12 bytes are reasonable values in general. Bear in
|
||||
mind that with CCM there is a trade-off between nonce length and
|
||||
maximum message size.
|
||||
|
||||
For the other modes, there are no restrictions on its length,
|
||||
but it is recommended to use at least 16 bytes.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
@@ -159,20 +89,6 @@ def new(key, *args, **kwargs):
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
|
||||
mac_len : integer
|
||||
(*Only* `MODE_CCM`). Length of the MAC, in bytes. It must be even and in
|
||||
the range ``[4..16]``. The default is 16.
|
||||
|
||||
(*Only* `MODE_EAX` and `MODE_GCM`). Length of the MAC, in bytes. It must be no
|
||||
larger than 16 bytes (which is the default).
|
||||
msg_len : integer
|
||||
(*Only* `MODE_CCM`). Length of the message to (de)cipher.
|
||||
If not specified, ``encrypt`` or ``decrypt`` may only be called once.
|
||||
assoc_len : integer
|
||||
(*Only* `MODE_CCM`). Length of the associated data.
|
||||
If not specified, all data is internally buffered.
|
||||
use_aesni : boolean
|
||||
Use AES-NI if available.
|
||||
|
||||
:Return: an `AESCipher` object
|
||||
"""
|
||||
@@ -192,14 +108,6 @@ MODE_OFB = 5
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: Counter with CBC-MAC (CCM) Mode. See `blockalgo.MODE_CCM`.
|
||||
MODE_CCM = 8
|
||||
#: EAX Mode. See `blockalgo.MODE_EAX`.
|
||||
MODE_EAX = 9
|
||||
#: Syntethic Initialization Vector (SIV). See `blockalgo.MODE_SIV`.
|
||||
MODE_SIV = 10
|
||||
#: Galois Counter Mode (GCM). See `blockalgo.MODE_GCM`.
|
||||
MODE_GCM = 11
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 16
|
||||
#: Size of a key (in bytes)
|
||||
|
@@ -32,7 +32,7 @@ The company eventually published its full specification in RFC2268_.
|
||||
|
||||
RC2 has a fixed data block size of 8 bytes. Length of its keys can vary from
|
||||
8 to 128 bits. One particular property of RC2 is that the actual
|
||||
cryptographic strength of the key (*effective key length*) can be reduced
|
||||
cryptographic strength of the key (*effective key length*) can be reduced
|
||||
via a parameter.
|
||||
|
||||
Even though RC2 is not cryptographically broken, it has not been analyzed as
|
||||
@@ -66,7 +66,7 @@ class RC2Cipher (blockalgo.BlockAlgo):
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize an ARC2 cipher object
|
||||
|
||||
|
||||
See also `new()` at the module level."""
|
||||
blockalgo.BlockAlgo.__init__(self, _ARC2, key, *args, **kwargs)
|
||||
|
||||
@@ -82,8 +82,6 @@ def new(key, *args, **kwargs):
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
(*Only* `MODE_CBC`, `MODE_CFB`, `MODE_OFB`, `MODE_OPENPGP`).
|
||||
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
@@ -92,20 +90,13 @@ def new(key, *args, **kwargs):
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be 8 bytes long.
|
||||
nonce : byte string
|
||||
(*Only* `MODE_EAX`).
|
||||
A mandatory value that must never be reused for any other encryption.
|
||||
There are no restrictions on its length, but it is recommended to
|
||||
use at least 16 bytes.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
For better performance, use `Crypto.Util.Counter`.
|
||||
mac_len : integer
|
||||
(*Only* `MODE_EAX`). Length of the MAC, in bytes.
|
||||
It must be no larger than 8 (which is the default).
|
||||
segment_size : integer
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
@@ -132,8 +123,6 @@ MODE_OFB = 5
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: EAX Mode. See `blockalgo.MODE_EAX`.
|
||||
MODE_EAX = 9
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 8
|
||||
#: Size of a key (in bytes)
|
||||
|
@@ -63,7 +63,6 @@ As an example, encryption can be done as follows:
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
from Crypto.Cipher import _ARC4
|
||||
|
||||
class ARC4Cipher:
|
||||
@@ -75,18 +74,7 @@ class ARC4Cipher:
|
||||
|
||||
See also `new()` at the module level."""
|
||||
|
||||
if len(args)>0:
|
||||
ndrop = args[0]
|
||||
args = args[1:]
|
||||
else:
|
||||
ndrop = kwargs.get('drop', 0)
|
||||
if ndrop: del kwargs['drop']
|
||||
self._cipher = _ARC4.new(key, *args, **kwargs)
|
||||
if ndrop:
|
||||
# This is OK even if the cipher is used for decryption, since encrypt
|
||||
# and decrypt are actually the same thing with ARC4.
|
||||
self._cipher.encrypt(b('\x00')*ndrop)
|
||||
|
||||
self.block_size = self._cipher.block_size
|
||||
self.key_size = self._cipher.key_size
|
||||
|
||||
@@ -120,17 +108,8 @@ def new(key, *args, **kwargs):
|
||||
The secret key to use in the symmetric cipher.
|
||||
It can have any length, with a minimum of 40 bytes.
|
||||
Its cryptograpic strength is always capped to 2048 bits (256 bytes).
|
||||
:Keywords:
|
||||
drop : integer
|
||||
The amount of bytes to discard from the initial part of the keystream.
|
||||
In fact, such part has been found to be distinguishable from random
|
||||
data (while it shouldn't) and also correlated to key.
|
||||
|
||||
The recommended value is 3072_ bytes. The default value is 0.
|
||||
|
||||
:Return: an `ARC4Cipher` object
|
||||
|
||||
.. _3072: http://eprint.iacr.org/2002/067.pdf
|
||||
"""
|
||||
return ARC4Cipher(key, *args, **kwargs)
|
||||
|
||||
|
@@ -60,7 +60,7 @@ class BlowfishCipher (blockalgo.BlockAlgo):
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize a Blowfish cipher object
|
||||
|
||||
|
||||
See also `new()` at the module level."""
|
||||
blockalgo.BlockAlgo.__init__(self, _Blowfish, key, *args, **kwargs)
|
||||
|
||||
@@ -76,8 +76,6 @@ def new(key, *args, **kwargs):
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
(*Only* `MODE_CBC`, `MODE_CFB`, `MODE_OFB`, `MODE_OPENPGP`).
|
||||
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
@@ -86,20 +84,13 @@ def new(key, *args, **kwargs):
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be 8 bytes long.
|
||||
nonce : byte string
|
||||
(*Only* `MODE_EAX`).
|
||||
A mandatory value that must never be reused for any other encryption.
|
||||
There are no restrictions on its length, but it is recommended to
|
||||
use at least 16 bytes.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
For better performance, use `Crypto.Util.Counter`.
|
||||
mac_len : integer
|
||||
(*Only* `MODE_EAX`). Length of the MAC, in bytes.
|
||||
It must be no larger than 8 (which is the default).
|
||||
segment_size : integer
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
@@ -123,8 +114,6 @@ MODE_OFB = 5
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: EAX Mode. See `blockalgo.MODE_EAX`.
|
||||
MODE_EAX = 9
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 8
|
||||
#: Size of a key (in bytes)
|
||||
|
@@ -63,7 +63,7 @@ class CAST128Cipher(blockalgo.BlockAlgo):
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize a CAST-128 cipher object
|
||||
|
||||
|
||||
See also `new()` at the module level."""
|
||||
blockalgo.BlockAlgo.__init__(self, _CAST, key, *args, **kwargs)
|
||||
|
||||
@@ -79,8 +79,6 @@ def new(key, *args, **kwargs):
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
(*Only* `MODE_CBC`, `MODE_CFB`, `MODE_OFB`, `MODE_OPENPGP`).
|
||||
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
@@ -89,20 +87,13 @@ def new(key, *args, **kwargs):
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be 8 bytes long.
|
||||
nonce : byte string
|
||||
(*Only* `MODE_EAX`).
|
||||
A mandatory value that must never be reused for any other encryption.
|
||||
There are no restrictions on its length, but it is recommended to
|
||||
use at least 16 bytes.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
For better performance, use `Crypto.Util.Counter`.
|
||||
mac_len : integer
|
||||
(*Only* `MODE_EAX`). Length of the MAC, in bytes.
|
||||
It must be no larger than 8 (which is the default).
|
||||
segment_size : integer
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
@@ -126,8 +117,6 @@ MODE_OFB = 5
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: EAX Mode. See `blockalgo.MODE_EAX`.
|
||||
MODE_EAX = 9
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 8
|
||||
#: Size of a key (in bytes)
|
||||
|
@@ -33,12 +33,12 @@ DES should not be used for new designs. Use `AES`.
|
||||
|
||||
As an example, encryption can be done as follows:
|
||||
|
||||
>>> from Crypto.Cipher import DES
|
||||
>>> from Crypto.Cipher import DES3
|
||||
>>> from Crypto import Random
|
||||
>>>
|
||||
>>> key = b'-8B key-'
|
||||
>>> iv = Random.new().read(DES.block_size)
|
||||
>>> cipher = DES.new(key, DES.MODE_OFB, iv)
|
||||
>>> key = b'Sixteen byte key'
|
||||
>>> iv = Random.new().read(DES3.block_size)
|
||||
>>> cipher = DES3.new(key, DES3.MODE_OFB, iv)
|
||||
>>> plaintext = b'sona si latine loqueris '
|
||||
>>> msg = iv + cipher.encrypt(plaintext)
|
||||
|
||||
@@ -58,7 +58,7 @@ class DESCipher(blockalgo.BlockAlgo):
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize a DES cipher object
|
||||
|
||||
|
||||
See also `new()` at the module level."""
|
||||
blockalgo.BlockAlgo.__init__(self, _DES, key, *args, **kwargs)
|
||||
|
||||
@@ -74,8 +74,6 @@ def new(key, *args, **kwargs):
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
(*Only* `MODE_CBC`, `MODE_CFB`, `MODE_OFB`, `MODE_OPENPGP`).
|
||||
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
@@ -84,20 +82,13 @@ def new(key, *args, **kwargs):
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be 8 bytes long.
|
||||
nonce : byte string
|
||||
(*Only* `MODE_EAX`).
|
||||
A mandatory value that must never be reused for any other encryption.
|
||||
There are no restrictions on its length, but it is recommended to
|
||||
use at least 16 bytes.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
For better performance, use `Crypto.Util.Counter`.
|
||||
mac_len : integer
|
||||
(*Only* `MODE_EAX`). Length of the MAC, in bytes.
|
||||
It must be no larger than 8 (which is the default).
|
||||
segment_size : integer
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
@@ -121,8 +112,6 @@ MODE_OFB = 5
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: EAX Mode. See `blockalgo.MODE_EAX`.
|
||||
MODE_EAX = 9
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 8
|
||||
#: Size of a key (in bytes)
|
||||
|
@@ -44,14 +44,14 @@ as `AES`.
|
||||
|
||||
As an example, encryption can be done as follows:
|
||||
|
||||
>>> from Crypto.Cipher import DES3
|
||||
>>> from Crypto.Cipher import DES
|
||||
>>> from Crypto import Random
|
||||
>>> from Crypto.Util import Counter
|
||||
>>>
|
||||
>>> key = b'Sixteen byte key'
|
||||
>>> nonce = Random.new().read(DES3.block_size/2)
|
||||
>>> ctr = Counter.new(DES3.block_size*8/2, prefix=nonce)
|
||||
>>> cipher = DES3.new(key, DES3.MODE_CTR, counter=ctr)
|
||||
>>> key = b'-8B key-'
|
||||
>>> nonce = Random.new().read(DES.block_size/2)
|
||||
>>> ctr = Counter.new(DES.block_size*8/2, prefix=nonce)
|
||||
>>> cipher = DES.new(key, DES.MODE_CTR, counter=ctr)
|
||||
>>> plaintext = b'We are no longer the knights who say ni!'
|
||||
>>> msg = nonce + cipher.encrypt(plaintext)
|
||||
|
||||
@@ -71,7 +71,7 @@ class DES3Cipher(blockalgo.BlockAlgo):
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize a TDES cipher object
|
||||
|
||||
|
||||
See also `new()` at the module level."""
|
||||
blockalgo.BlockAlgo.__init__(self, _DES3, key, *args, **kwargs)
|
||||
|
||||
@@ -87,8 +87,6 @@ def new(key, *args, **kwargs):
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
(*Only* `MODE_CBC`, `MODE_CFB`, `MODE_OFB`, `MODE_OPENPGP`).
|
||||
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
@@ -97,20 +95,13 @@ def new(key, *args, **kwargs):
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be 8 bytes long.
|
||||
nonce : byte string
|
||||
(*Only* `MODE_EAX`).
|
||||
A mandatory value that must never be reused for any other encryption.
|
||||
There are no restrictions on its length, but it is recommended to
|
||||
use at least 16 bytes.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of 8 bytes.
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
For better performance, use `Crypto.Util.Counter`.
|
||||
mac_len : integer
|
||||
(*Only* `MODE_EAX`). Length of the MAC, in bytes.
|
||||
It must be no larger than 8 (which is the default).
|
||||
segment_size : integer
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
@@ -136,8 +127,6 @@ MODE_OFB = 5
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: EAX Mode. See `blockalgo.MODE_EAX`.
|
||||
MODE_EAX = 9
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 8
|
||||
#: Size of a key (in bytes)
|
||||
|
@@ -31,7 +31,7 @@ As an example, a sender may encrypt a message in this way:
|
||||
>>> from Crypto.Cipher import PKCS1_OAEP
|
||||
>>> from Crypto.PublicKey import RSA
|
||||
>>>
|
||||
>>> message = b'To be encrypted'
|
||||
>>> message = 'To be encrypted'
|
||||
>>> key = RSA.importKey(open('pubkey.der').read())
|
||||
>>> cipher = PKCS1_OAEP.new(key)
|
||||
>>> ciphertext = cipher.encrypt(message)
|
||||
@@ -55,7 +55,7 @@ __revision__ = "$Id$"
|
||||
__all__ = [ 'new', 'PKCS1OAEP_Cipher' ]
|
||||
|
||||
import Crypto.Signature.PKCS1_PSS
|
||||
import Crypto.Hash.SHA1
|
||||
import Crypto.Hash.SHA
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
import Crypto.Util.number
|
||||
@@ -70,17 +70,17 @@ class PKCS1OAEP_Cipher:
|
||||
|
||||
:Parameters:
|
||||
key : an RSA key object
|
||||
If a private half is given, both encryption and decryption are possible.
|
||||
If a public half is given, only encryption is possible.
|
||||
If a private half is given, both encryption and decryption are possible.
|
||||
If a public half is given, only encryption is possible.
|
||||
hashAlgo : hash object
|
||||
The hash function to use. This can be a module under `Crypto.Hash`
|
||||
or an existing hash object created from any of such modules. If not specified,
|
||||
`Crypto.Hash.SHA1` is used.
|
||||
`Crypto.Hash.SHA` (that is, SHA-1) is used.
|
||||
mgfunc : callable
|
||||
A mask generation function that accepts two parameters: a string to
|
||||
use as seed, and the lenth of the mask to generate, in bytes.
|
||||
If not specified, the standard MGF1 is used (a safe choice).
|
||||
label : byte string
|
||||
label : string
|
||||
A label to apply to this particular encryption. If not specified,
|
||||
an empty string is used. Specifying a label does not improve
|
||||
security.
|
||||
@@ -93,7 +93,7 @@ class PKCS1OAEP_Cipher:
|
||||
if hashAlgo:
|
||||
self._hashObj = hashAlgo
|
||||
else:
|
||||
self._hashObj = Crypto.Hash.SHA1
|
||||
self._hashObj = Crypto.Hash.SHA
|
||||
|
||||
if mgfunc:
|
||||
self._mgf = mgfunc
|
||||
@@ -117,12 +117,12 @@ class PKCS1OAEP_Cipher:
|
||||
section 7.1.1 of RFC3447.
|
||||
|
||||
:Parameters:
|
||||
message : byte string
|
||||
message : string
|
||||
The message to encrypt, also known as plaintext. It can be of
|
||||
variable length, but not longer than the RSA modulus (in bytes)
|
||||
minus 2, minus twice the hash output size.
|
||||
|
||||
:Return: A byte string, the ciphertext in which the message is encrypted.
|
||||
:Return: A string, the ciphertext in which the message is encrypted.
|
||||
It is as long as the RSA modulus (in bytes).
|
||||
:Raise ValueError:
|
||||
If the RSA key length is not sufficiently long to deal with the given
|
||||
@@ -173,10 +173,10 @@ class PKCS1OAEP_Cipher:
|
||||
section 7.1.2 of RFC3447.
|
||||
|
||||
:Parameters:
|
||||
ct : byte string
|
||||
ct : string
|
||||
The ciphertext that contains the message to recover.
|
||||
|
||||
:Return: A byte string, the original message.
|
||||
:Return: A string, the original message.
|
||||
:Raise ValueError:
|
||||
If the ciphertext length is incorrect, or if the decryption does not
|
||||
succeed.
|
||||
@@ -238,12 +238,12 @@ def new(key, hashAlgo=None, mgfunc=None, label=b('')):
|
||||
hashAlgo : hash object
|
||||
The hash function to use. This can be a module under `Crypto.Hash`
|
||||
or an existing hash object created from any of such modules. If not specified,
|
||||
`Crypto.Hash.SHA1` is used.
|
||||
`Crypto.Hash.SHA` (that is, SHA-1) is used.
|
||||
mgfunc : callable
|
||||
A mask generation function that accepts two parameters: a string to
|
||||
use as seed, and the lenth of the mask to generate, in bytes.
|
||||
If not specified, the standard MGF1 is used (a safe choice).
|
||||
label : byte string
|
||||
label : string
|
||||
A label to apply to this particular encryption. If not specified,
|
||||
an empty string is used. Specifying a label does not improve
|
||||
security.
|
||||
|
@@ -34,7 +34,7 @@ As an example, a sender may encrypt a message in this way:
|
||||
>>> from Crypto.PublicKey import RSA
|
||||
>>> from Crypto.Hash import SHA
|
||||
>>>
|
||||
>>> message = b'To be encrypted'
|
||||
>>> message = 'To be encrypted'
|
||||
>>> h = SHA.new(message)
|
||||
>>>
|
||||
>>> key = RSA.importKey(open('pubkey.der').read())
|
||||
|
BIN
modules/Crypto/Cipher/_AES.so
Executable file
BIN
modules/Crypto/Cipher/_AES.so
Executable file
Binary file not shown.
BIN
modules/Crypto/Cipher/_ARC2.so
Executable file
BIN
modules/Crypto/Cipher/_ARC2.so
Executable file
Binary file not shown.
BIN
modules/Crypto/Cipher/_ARC4.so
Executable file
BIN
modules/Crypto/Cipher/_ARC4.so
Executable file
Binary file not shown.
BIN
modules/Crypto/Cipher/_Blowfish.so
Executable file
BIN
modules/Crypto/Cipher/_Blowfish.so
Executable file
Binary file not shown.
BIN
modules/Crypto/Cipher/_CAST.so
Executable file
BIN
modules/Crypto/Cipher/_CAST.so
Executable file
Binary file not shown.
BIN
modules/Crypto/Cipher/_DES.so
Executable file
BIN
modules/Crypto/Cipher/_DES.so
Executable file
Binary file not shown.
BIN
modules/Crypto/Cipher/_DES3.so
Executable file
BIN
modules/Crypto/Cipher/_DES3.so
Executable file
Binary file not shown.
BIN
modules/Crypto/Cipher/_XOR.so
Executable file
BIN
modules/Crypto/Cipher/_XOR.so
Executable file
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user