mirror of
https://github.com/apprenticeharper/DeDRM_tools
synced 2025-02-08 08:46:21 +01:00
61 lines
2.4 KiB
Python
61 lines
2.4 KiB
Python
# -*- 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.
|
|
# ===================================================================
|
|
|
|
from Crypto.Util.python_compat import *
|
|
|
|
from Crypto.Util import _counter
|
|
import struct
|
|
|
|
# Factory function
|
|
def new(nbits, prefix="", suffix="", initial_value=1, overflow=0, little_endian=False, allow_wraparound=False, disable_shortcut=False):
|
|
# TODO: Document this
|
|
|
|
# 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(str(prefix), str(suffix), initval, allow_wraparound=allow_wraparound, disable_shortcut=disable_shortcut)
|
|
else:
|
|
return _counter._newBE(str(prefix), str(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(chr(n & 0xff))
|
|
else:
|
|
retval.insert(0, chr(n & 0xff))
|
|
n >>= 8
|
|
return "".join(retval)
|
|
|
|
# vim:set ts=4 sw=4 sts=4 expandtab:
|