pycryptodome/lib/Crypto/Util/Counter.py

71 lines
2.7 KiB
Python
Raw Normal View History

# -*- 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.
# ===================================================================
2014-06-11 15:46:22 +02:00
def new(nbits, prefix=b"", suffix=b"", initial_value=1, little_endian=False, allow_wraparound=False):
2012-05-09 22:25:44 +02:00
"""Create a stateful counter block function suitable for CTR encryption modes.
Each call to the function returns the next counter block.
2017-08-20 01:09:51 +02:00
Each counter block is made up by three parts:
2017-08-20 01:09:51 +02:00
+------+--------------+-------+
|prefix| counter value|postfix|
+------+--------------+-------+
2012-05-09 22:25:44 +02:00
The counter value is incremented by 1 at each call.
2012-05-09 22:25:44 +02:00
2017-08-20 01:09:51 +02:00
Args:
nbits (integer):
Length of the desired counter value, in bits. It must be a multiple of 8.
2017-08-20 01:09:51 +02:00
prefix (byte string):
2012-05-09 22:25:44 +02:00
The constant prefix of the counter block. By default, no prefix is
used.
2017-08-20 01:09:51 +02:00
suffix (byte string):
2012-05-09 22:25:44 +02:00
The constant postfix of the counter block. By default, no suffix is
used.
2017-08-20 01:09:51 +02:00
initial_value (integer):
2012-05-09 22:25:44 +02:00
The initial value of the counter. Default value is 1.
2017-08-20 01:09:51 +02:00
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):
2014-12-16 07:50:48 +01:00
This parameter is ignored.
2017-08-20 01:09:51 +02:00
Returns:
An object that can be passed with the :data:`counter` parameter to a CTR mode
2014-12-16 07:50:48 +01:00
cipher.
2017-08-20 01:09:51 +02:00
It must hold that *len(prefix) + nbits//8 + len(suffix)* matches the
block size of the underlying block cipher.
2012-05-09 22:25:44 +02:00
"""
2014-12-16 07:50:48 +01:00
if (nbits % 8) != 0:
raise ValueError("'nbits' must be a multiple of 8")
2014-12-16 07:50:48 +01:00
# Ignore wraparound
return {"counter_len": nbits // 8,
"prefix": prefix,
"suffix": suffix,
"initial_value": initial_value,
"little_endian": little_endian
}