Counter.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: ascii -*-
  2. #
  3. # Util/Counter.py : Fast counter for use with CTR-mode ciphers
  4. #
  5. # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  6. #
  7. # ===================================================================
  8. # The contents of this file are dedicated to the public domain. To
  9. # the extent that dedication to the public domain is not available,
  10. # everyone is granted a worldwide, perpetual, royalty-free,
  11. # non-exclusive license to exercise all rights associated with the
  12. # contents of this file for any purpose whatsoever.
  13. # No rights are reserved.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. # ===================================================================
  24. def new(nbits, prefix=b"", suffix=b"", initial_value=1, little_endian=False, allow_wraparound=False):
  25. """Create a stateful counter block function suitable for CTR encryption modes.
  26. Each call to the function returns the next counter block.
  27. Each counter block is made up by three parts:
  28. +------+--------------+-------+
  29. |prefix| counter value|postfix|
  30. +------+--------------+-------+
  31. The counter value is incremented by 1 at each call.
  32. Args:
  33. nbits (integer):
  34. Length of the desired counter value, in bits. It must be a multiple of 8.
  35. prefix (byte string):
  36. The constant prefix of the counter block. By default, no prefix is
  37. used.
  38. suffix (byte string):
  39. The constant postfix of the counter block. By default, no suffix is
  40. used.
  41. initial_value (integer):
  42. The initial value of the counter. Default value is 1.
  43. Its length in bits must not exceed the argument ``nbits``.
  44. little_endian (boolean):
  45. If ``True``, the counter number will be encoded in little endian format.
  46. If ``False`` (default), in big endian format.
  47. allow_wraparound (boolean):
  48. This parameter is ignored.
  49. Returns:
  50. An object that can be passed with the :data:`counter` parameter to a CTR mode
  51. cipher.
  52. It must hold that *len(prefix) + nbits//8 + len(suffix)* matches the
  53. block size of the underlying block cipher.
  54. """
  55. if (nbits % 8) != 0:
  56. raise ValueError("'nbits' must be a multiple of 8")
  57. iv_bl = initial_value.bit_length()
  58. if iv_bl > nbits:
  59. raise ValueError("Initial value takes %d bits but it is longer than "
  60. "the counter (%d bits)" %
  61. (iv_bl, nbits))
  62. # Ignore wraparound
  63. return {"counter_len": nbits // 8,
  64. "prefix": prefix,
  65. "suffix": suffix,
  66. "initial_value": initial_value,
  67. "little_endian": little_endian
  68. }