Counter.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. little_endian (boolean):
  44. If ``True``, the counter number will be encoded in little endian format.
  45. If ``False`` (default), in big endian format.
  46. allow_wraparound (boolean):
  47. This parameter is ignored.
  48. Returns:
  49. An object that can be passed with the :data:`counter` parameter to a CTR mode
  50. cipher.
  51. It must hold that *len(prefix) + nbits//8 + len(suffix)* matches the
  52. block size of the underlying block cipher.
  53. """
  54. if (nbits % 8) != 0:
  55. raise ValueError("'nbits' must be a multiple of 8")
  56. # Ignore wraparound
  57. return {"counter_len": nbits // 8,
  58. "prefix": prefix,
  59. "suffix": suffix,
  60. "initial_value": initial_value,
  61. "little_endian": little_endian
  62. }