folder reorganization
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
# -*- coding: ascii -*-
|
||||
#
|
||||
# Util/Counter.py : Fast counter for use with CTR-mode ciphers
|
||||
#
|
||||
# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
"""Fast counter functions for CTR cipher modes.
|
||||
|
||||
CTR is a chaining mode for symmetric block encryption or decryption.
|
||||
Messages are divideded into blocks, and the cipher operation takes
|
||||
place on each block using the secret key and a unique *counter block*.
|
||||
|
||||
The most straightforward way to fulfil the uniqueness property is
|
||||
to start with an initial, random *counter block* value, and increment it as
|
||||
the next block is processed.
|
||||
|
||||
The block ciphers from `Crypto.Cipher` (when configured in *MODE_CTR* mode)
|
||||
invoke a callable object (the *counter* parameter) to get the next *counter block*.
|
||||
Unfortunately, the Python calling protocol leads to major performance degradations.
|
||||
|
||||
The counter functions instantiated by this module will be invoked directly
|
||||
by the ciphers in `Crypto.Cipher`. The fact that the Python layer is bypassed
|
||||
lead to more efficient (and faster) execution of CTR cipher modes.
|
||||
|
||||
An example of usage is the following:
|
||||
|
||||
>>> from Crypto.Cipher import AES
|
||||
>>> from Crypto.Util import Counter
|
||||
>>>
|
||||
>>> pt = b'\x00'*1000000
|
||||
>>> ctr = Counter.new(128)
|
||||
>>> cipher = AES.new(b'\x00'*16, AES.MODE_CTR, counter=ctr)
|
||||
>>> ct = cipher.encrypt(pt)
|
||||
|
||||
:undocumented: __package__
|
||||
"""
|
||||
import sys
|
||||
if sys.version_info[0] == 2 and sys.version_info[1] == 1:
|
||||
from Crypto.Util.py21compat import *
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
from Crypto.Util import _counter
|
||||
import struct
|
||||
|
||||
# Factory function
|
||||
def new(nbits, prefix=b(""), suffix=b(""), initial_value=1, overflow=0, little_endian=False, allow_wraparound=False, disable_shortcut=False):
|
||||
"""Create a stateful counter block function suitable for CTR encryption modes.
|
||||
|
||||
Each call to the function returns the next counter block.
|
||||
Each counter block is made up by three parts::
|
||||
|
||||
prefix || counter value || postfix
|
||||
|
||||
The counter value is incremented by one at each call.
|
||||
|
||||
:Parameters:
|
||||
nbits : integer
|
||||
Length of the desired counter, in bits. It must be a multiple of 8.
|
||||
prefix : byte string
|
||||
The constant prefix of the counter block. By default, no prefix is
|
||||
used.
|
||||
suffix : byte string
|
||||
The constant postfix of the counter block. By default, no suffix is
|
||||
used.
|
||||
initial_value : integer
|
||||
The initial value of the counter. Default value is 1.
|
||||
little_endian : boolean
|
||||
If True, the counter number will be encoded in little endian format.
|
||||
If False (default), in big endian format.
|
||||
allow_wraparound : boolean
|
||||
If True, the function will raise an *OverflowError* exception as soon
|
||||
as the counter wraps around. If False (default), the counter will
|
||||
simply restart from zero.
|
||||
disable_shortcut : boolean
|
||||
If True, do not make ciphers from `Crypto.Cipher` bypass the Python
|
||||
layer when invoking the counter block function.
|
||||
If False (default), bypass the Python layer.
|
||||
:Returns:
|
||||
The counter block function.
|
||||
"""
|
||||
|
||||
# Sanity-check the message size
|
||||
(nbytes, remainder) = divmod(nbits, 8)
|
||||
if remainder != 0:
|
||||
# In the future, we might support arbitrary bit lengths, but for now we don't.
|
||||
raise ValueError("nbits must be a multiple of 8; got %d" % (nbits,))
|
||||
if nbytes < 1:
|
||||
raise ValueError("nbits too small")
|
||||
elif nbytes > 0xffff:
|
||||
raise ValueError("nbits too large")
|
||||
|
||||
initval = _encode(initial_value, nbytes, little_endian)
|
||||
|
||||
if little_endian:
|
||||
return _counter._newLE(bstr(prefix), bstr(suffix), initval, allow_wraparound=allow_wraparound, disable_shortcut=disable_shortcut)
|
||||
else:
|
||||
return _counter._newBE(bstr(prefix), bstr(suffix), initval, allow_wraparound=allow_wraparound, disable_shortcut=disable_shortcut)
|
||||
|
||||
def _encode(n, nbytes, little_endian=False):
|
||||
retval = []
|
||||
n = long(n)
|
||||
for i in range(nbytes):
|
||||
if little_endian:
|
||||
retval.append(bchr(n & 0xff))
|
||||
else:
|
||||
retval.insert(0, bchr(n & 0xff))
|
||||
n >>= 8
|
||||
return b("").join(retval)
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
||||
@@ -0,0 +1,37 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Miscellaneous modules
|
||||
|
||||
Contains useful modules that don't belong into any of the
|
||||
other Crypto.* subpackages.
|
||||
|
||||
Crypto.Util.number Number-theoretic functions (primality testing, etc.)
|
||||
Crypto.Util.randpool Random number generation
|
||||
Crypto.Util.RFC1751 Converts between 128-bit keys and human-readable
|
||||
strings of words.
|
||||
Crypto.Util.asn1 Minimal support for ASN.1 DER encoding
|
||||
|
||||
"""
|
||||
|
||||
__all__ = ['randpool', 'RFC1751', 'number', 'strxor', 'asn1' ]
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,107 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Util/py3compat.py : Compatibility code for handling Py3k / Python 2.x
|
||||
#
|
||||
# Written in 2010 by Thorsten Behrens
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Compatibility code for handling string/bytes changes from Python 2.x to Py3k
|
||||
|
||||
In Python 2.x, strings (of type ''str'') contain binary data, including encoded
|
||||
Unicode text (e.g. UTF-8). The separate type ''unicode'' holds Unicode text.
|
||||
Unicode literals are specified via the u'...' prefix. Indexing or slicing
|
||||
either type always produces a string of the same type as the original.
|
||||
Data read from a file is always of '''str'' type.
|
||||
|
||||
In Python 3.x, strings (type ''str'') may only contain Unicode text. The u'...'
|
||||
prefix and the ''unicode'' type are now redundant. A new type (called
|
||||
''bytes'') has to be used for binary data (including any particular
|
||||
''encoding'' of a string). The b'...' prefix allows one to specify a binary
|
||||
literal. Indexing or slicing a string produces another string. Slicing a byte
|
||||
string produces another byte string, but the indexing operation produces an
|
||||
integer. Data read from a file is of '''str'' type if the file was opened in
|
||||
text mode, or of ''bytes'' type otherwise.
|
||||
|
||||
Since PyCrypto aims at supporting both Python 2.x and 3.x, the following helper
|
||||
functions are used to keep the rest of the library as independent as possible
|
||||
from the actual Python version.
|
||||
|
||||
In general, the code should always deal with binary strings, and use integers
|
||||
instead of 1-byte character strings.
|
||||
|
||||
b(s)
|
||||
Take a text string literal (with no prefix or with u'...' prefix) and
|
||||
make a byte string.
|
||||
bchr(c)
|
||||
Take an integer and make a 1-character byte string.
|
||||
bord(c)
|
||||
Take the result of indexing on a byte string and make an integer.
|
||||
tobytes(s)
|
||||
Take a text string, a byte string, or a sequence of character taken from
|
||||
a byte string and make a byte string.
|
||||
"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
import sys
|
||||
|
||||
if sys.version_info[0] == 2:
|
||||
def b(s):
|
||||
return s
|
||||
def bchr(s):
|
||||
return chr(s)
|
||||
def bstr(s):
|
||||
return str(s)
|
||||
def bord(s):
|
||||
return ord(s)
|
||||
if sys.version_info[1] == 1:
|
||||
def tobytes(s):
|
||||
try:
|
||||
return s.encode('latin-1')
|
||||
except:
|
||||
return ''.join(s)
|
||||
else:
|
||||
def tobytes(s):
|
||||
if isinstance(s, unicode):
|
||||
return s.encode("latin-1")
|
||||
else:
|
||||
return ''.join(s)
|
||||
else:
|
||||
def b(s):
|
||||
return s.encode("latin-1") # utf-8 would cause some side-effects we don't want
|
||||
def bchr(s):
|
||||
return bytes([s])
|
||||
def bstr(s):
|
||||
if isinstance(s,str):
|
||||
return bytes(s,"latin-1")
|
||||
else:
|
||||
return bytes(s)
|
||||
def bord(s):
|
||||
return s
|
||||
def tobytes(s):
|
||||
if isinstance(s,bytes):
|
||||
return s
|
||||
else:
|
||||
if isinstance(s,str):
|
||||
return s.encode("latin-1")
|
||||
else:
|
||||
return bytes(s)
|
||||
|
||||
# vim:set ts=4 sw=4 sts=4 expandtab:
|
||||
Reference in New Issue
Block a user