aead.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. import os
  6. from cryptography import exceptions, utils
  7. from cryptography.hazmat.backends.openssl import aead
  8. from cryptography.hazmat.backends.openssl.backend import backend
  9. class ChaCha20Poly1305(object):
  10. _MAX_SIZE = 2 ** 32
  11. def __init__(self, key):
  12. if not backend.aead_cipher_supported(self):
  13. raise exceptions.UnsupportedAlgorithm(
  14. "ChaCha20Poly1305 is not supported by this version of OpenSSL",
  15. exceptions._Reasons.UNSUPPORTED_CIPHER,
  16. )
  17. utils._check_byteslike("key", key)
  18. if len(key) != 32:
  19. raise ValueError("ChaCha20Poly1305 key must be 32 bytes.")
  20. self._key = key
  21. @classmethod
  22. def generate_key(cls):
  23. return os.urandom(32)
  24. def encrypt(self, nonce, data, associated_data):
  25. if associated_data is None:
  26. associated_data = b""
  27. if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE:
  28. # This is OverflowError to match what cffi would raise
  29. raise OverflowError(
  30. "Data or associated data too long. Max 2**32 bytes"
  31. )
  32. self._check_params(nonce, data, associated_data)
  33. return aead._encrypt(backend, self, nonce, data, associated_data, 16)
  34. def decrypt(self, nonce, data, associated_data):
  35. if associated_data is None:
  36. associated_data = b""
  37. self._check_params(nonce, data, associated_data)
  38. return aead._decrypt(backend, self, nonce, data, associated_data, 16)
  39. def _check_params(self, nonce, data, associated_data):
  40. utils._check_byteslike("nonce", nonce)
  41. utils._check_bytes("data", data)
  42. utils._check_bytes("associated_data", associated_data)
  43. if len(nonce) != 12:
  44. raise ValueError("Nonce must be 12 bytes")
  45. class AESCCM(object):
  46. _MAX_SIZE = 2 ** 32
  47. def __init__(self, key, tag_length=16):
  48. utils._check_byteslike("key", key)
  49. if len(key) not in (16, 24, 32):
  50. raise ValueError("AESCCM key must be 128, 192, or 256 bits.")
  51. self._key = key
  52. if not isinstance(tag_length, int):
  53. raise TypeError("tag_length must be an integer")
  54. if tag_length not in (4, 6, 8, 10, 12, 14, 16):
  55. raise ValueError("Invalid tag_length")
  56. self._tag_length = tag_length
  57. @classmethod
  58. def generate_key(cls, bit_length):
  59. if not isinstance(bit_length, int):
  60. raise TypeError("bit_length must be an integer")
  61. if bit_length not in (128, 192, 256):
  62. raise ValueError("bit_length must be 128, 192, or 256")
  63. return os.urandom(bit_length // 8)
  64. def encrypt(self, nonce, data, associated_data):
  65. if associated_data is None:
  66. associated_data = b""
  67. if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE:
  68. # This is OverflowError to match what cffi would raise
  69. raise OverflowError(
  70. "Data or associated data too long. Max 2**32 bytes"
  71. )
  72. self._check_params(nonce, data, associated_data)
  73. self._validate_lengths(nonce, len(data))
  74. return aead._encrypt(
  75. backend, self, nonce, data, associated_data, self._tag_length
  76. )
  77. def decrypt(self, nonce, data, associated_data):
  78. if associated_data is None:
  79. associated_data = b""
  80. self._check_params(nonce, data, associated_data)
  81. return aead._decrypt(
  82. backend, self, nonce, data, associated_data, self._tag_length
  83. )
  84. def _validate_lengths(self, nonce, data_len):
  85. # For information about computing this, see
  86. # https://tools.ietf.org/html/rfc3610#section-2.1
  87. l_val = 15 - len(nonce)
  88. if 2 ** (8 * l_val) < data_len:
  89. raise ValueError("Data too long for nonce")
  90. def _check_params(self, nonce, data, associated_data):
  91. utils._check_byteslike("nonce", nonce)
  92. utils._check_bytes("data", data)
  93. utils._check_bytes("associated_data", associated_data)
  94. if not 7 <= len(nonce) <= 13:
  95. raise ValueError("Nonce must be between 7 and 13 bytes")
  96. class AESGCM(object):
  97. _MAX_SIZE = 2 ** 32
  98. def __init__(self, key):
  99. utils._check_byteslike("key", key)
  100. if len(key) not in (16, 24, 32):
  101. raise ValueError("AESGCM key must be 128, 192, or 256 bits.")
  102. self._key = key
  103. @classmethod
  104. def generate_key(cls, bit_length):
  105. if not isinstance(bit_length, int):
  106. raise TypeError("bit_length must be an integer")
  107. if bit_length not in (128, 192, 256):
  108. raise ValueError("bit_length must be 128, 192, or 256")
  109. return os.urandom(bit_length // 8)
  110. def encrypt(self, nonce, data, associated_data):
  111. if associated_data is None:
  112. associated_data = b""
  113. if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE:
  114. # This is OverflowError to match what cffi would raise
  115. raise OverflowError(
  116. "Data or associated data too long. Max 2**32 bytes"
  117. )
  118. self._check_params(nonce, data, associated_data)
  119. return aead._encrypt(backend, self, nonce, data, associated_data, 16)
  120. def decrypt(self, nonce, data, associated_data):
  121. if associated_data is None:
  122. associated_data = b""
  123. self._check_params(nonce, data, associated_data)
  124. return aead._decrypt(backend, self, nonce, data, associated_data, 16)
  125. def _check_params(self, nonce, data, associated_data):
  126. utils._check_byteslike("nonce", nonce)
  127. utils._check_bytes("data", data)
  128. utils._check_bytes("associated_data", associated_data)
  129. if len(nonce) == 0:
  130. raise ValueError("Nonce must be at least 1 byte")