Salsa20.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/Salsa20.py : Salsa20 stream cipher (http://cr.yp.to/snuffle.html)
  4. #
  5. # Contributed by Fabrizio Tarizzo <fabrizio@fabriziotarizzo.org>.
  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. from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
  25. create_string_buffer,
  26. get_raw_buffer, VoidPointer,
  27. SmartPointer, c_size_t,
  28. expect_byte_string)
  29. from Crypto.Random import get_random_bytes
  30. _raw_salsa20_lib = load_pycryptodome_raw_lib("Crypto.Cipher._Salsa20",
  31. """
  32. int Salsa20_stream_init(uint8_t *key, size_t keylen,
  33. uint8_t *nonce, size_t nonce_len,
  34. void **pSalsaState);
  35. int Salsa20_stream_destroy(void *salsaState);
  36. int Salsa20_stream_encrypt(void *salsaState,
  37. const uint8_t in[],
  38. uint8_t out[], size_t len);
  39. """)
  40. class Salsa20Cipher:
  41. """Salsa20 cipher object. Do not create it directly. Use :py:func:`new`
  42. instead.
  43. :var nonce: The nonce with length 8
  44. :vartype nonce: byte string
  45. """
  46. def __init__(self, key, nonce):
  47. """Initialize a Salsa20 cipher object
  48. See also `new()` at the module level."""
  49. if len(key) not in key_size:
  50. raise ValueError("Incorrect key length for Salsa20 (%d bytes)" % len(key))
  51. if len(nonce) != 8:
  52. raise ValueError("Incorrect nonce length for Salsa20 (%d bytes)" %
  53. len(nonce))
  54. self.nonce = nonce
  55. expect_byte_string(key)
  56. expect_byte_string(nonce)
  57. self._state = VoidPointer()
  58. result = _raw_salsa20_lib.Salsa20_stream_init(
  59. key,
  60. c_size_t(len(key)),
  61. nonce,
  62. c_size_t(len(nonce)),
  63. self._state.address_of())
  64. if result:
  65. raise ValueError("Error %d instantiating a Salsa20 cipher")
  66. self._state = SmartPointer(self._state.get(),
  67. _raw_salsa20_lib.Salsa20_stream_destroy)
  68. self.block_size = 1
  69. self.key_size = len(key)
  70. def encrypt(self, plaintext):
  71. """Encrypt a piece of data.
  72. :param plaintext: The data to encrypt, of any size.
  73. :type plaintext: byte string
  74. :returns: the encrypted byte string, of equal length as the
  75. plaintext.
  76. """
  77. expect_byte_string(plaintext)
  78. ciphertext = create_string_buffer(len(plaintext))
  79. result = _raw_salsa20_lib.Salsa20_stream_encrypt(
  80. self._state.get(),
  81. plaintext,
  82. ciphertext,
  83. c_size_t(len(plaintext)))
  84. if result:
  85. raise ValueError("Error %d while encrypting with Salsa20" % result)
  86. return get_raw_buffer(ciphertext)
  87. def decrypt(self, ciphertext):
  88. """Decrypt a piece of data.
  89. :param ciphertext: The data to decrypt, of any size.
  90. :type ciphertext: byte string
  91. :returns: the decrypted byte string, of equal length as the
  92. ciphertext.
  93. """
  94. try:
  95. return self.encrypt(ciphertext)
  96. except ValueError, e:
  97. raise ValueError(str(e).replace("enc", "dec"))
  98. def new(key, nonce=None):
  99. """Create a new Salsa20 cipher
  100. :keyword key: The secret key to use. It must be 16 or 32 bytes long.
  101. :type key: byte string
  102. :keyword nonce:
  103. A value that must never be reused for any other encryption
  104. done with this key. It must be 8 bytes long.
  105. If not provided, a random byte string will be generated (you can read
  106. it back via the ``nonce`` attribute of the returned object).
  107. :type nonce: byte string
  108. :Return: a :class:`Crypto.Cipher.Salsa20.Salsa20Cipher` object
  109. """
  110. if nonce is None:
  111. nonce = get_random_bytes(8)
  112. return Salsa20Cipher(key, nonce)
  113. # Size of a data block (in bytes)
  114. block_size = 1
  115. # Size of a key (in bytes)
  116. key_size = (16, 32)