modes.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 annotations
  5. from cryptography import utils
  6. from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
  7. from cryptography.hazmat.decrepit.ciphers.modes import CFB as CFB
  8. from cryptography.hazmat.decrepit.ciphers.modes import CFB8 as CFB8
  9. from cryptography.hazmat.decrepit.ciphers.modes import OFB as OFB
  10. from cryptography.hazmat.primitives._cipheralgorithm import (
  11. BlockCipherAlgorithm,
  12. CipherAlgorithm,
  13. )
  14. from cryptography.hazmat.primitives._modes import (
  15. Mode as Mode,
  16. )
  17. from cryptography.hazmat.primitives._modes import (
  18. ModeWithAuthenticationTag as ModeWithAuthenticationTag,
  19. )
  20. from cryptography.hazmat.primitives._modes import (
  21. ModeWithInitializationVector as ModeWithInitializationVector,
  22. )
  23. from cryptography.hazmat.primitives._modes import (
  24. ModeWithNonce as ModeWithNonce,
  25. )
  26. from cryptography.hazmat.primitives._modes import (
  27. ModeWithTweak as ModeWithTweak,
  28. )
  29. from cryptography.hazmat.primitives._modes import (
  30. _check_aes_key_length,
  31. _check_iv_and_key_length,
  32. _check_nonce_length,
  33. )
  34. from cryptography.hazmat.primitives.ciphers import algorithms
  35. class CBC(ModeWithInitializationVector):
  36. name = "CBC"
  37. def __init__(self, initialization_vector: utils.Buffer):
  38. utils._check_byteslike("initialization_vector", initialization_vector)
  39. self._initialization_vector = initialization_vector
  40. @property
  41. def initialization_vector(self) -> utils.Buffer:
  42. return self._initialization_vector
  43. validate_for_algorithm = _check_iv_and_key_length
  44. class XTS(ModeWithTweak):
  45. name = "XTS"
  46. def __init__(self, tweak: utils.Buffer):
  47. utils._check_byteslike("tweak", tweak)
  48. if len(tweak) != 16:
  49. raise ValueError("tweak must be 128-bits (16 bytes)")
  50. self._tweak = tweak
  51. @property
  52. def tweak(self) -> utils.Buffer:
  53. return self._tweak
  54. def validate_for_algorithm(self, algorithm: CipherAlgorithm) -> None:
  55. if isinstance(algorithm, (algorithms.AES128, algorithms.AES256)):
  56. raise TypeError(
  57. "The AES128 and AES256 classes do not support XTS, please use "
  58. "the standard AES class instead."
  59. )
  60. if algorithm.key_size not in (256, 512):
  61. raise ValueError(
  62. "The XTS specification requires a 256-bit key for AES-128-XTS"
  63. " and 512-bit key for AES-256-XTS"
  64. )
  65. class ECB(Mode):
  66. name = "ECB"
  67. validate_for_algorithm = _check_aes_key_length
  68. class CTR(ModeWithNonce):
  69. name = "CTR"
  70. def __init__(self, nonce: utils.Buffer):
  71. utils._check_byteslike("nonce", nonce)
  72. self._nonce = nonce
  73. @property
  74. def nonce(self) -> utils.Buffer:
  75. return self._nonce
  76. def validate_for_algorithm(self, algorithm: CipherAlgorithm) -> None:
  77. _check_aes_key_length(self, algorithm)
  78. _check_nonce_length(self.nonce, self.name, algorithm)
  79. class GCM(ModeWithInitializationVector, ModeWithAuthenticationTag):
  80. name = "GCM"
  81. _MAX_ENCRYPTED_BYTES = (2**39 - 256) // 8
  82. _MAX_AAD_BYTES = (2**64) // 8
  83. def __init__(
  84. self,
  85. initialization_vector: utils.Buffer,
  86. tag: bytes | None = None,
  87. min_tag_length: int = 16,
  88. ):
  89. # OpenSSL 3.0.0 constrains GCM IVs to [64, 1024] bits inclusive
  90. # This is a sane limit anyway so we'll enforce it here.
  91. utils._check_byteslike("initialization_vector", initialization_vector)
  92. if len(initialization_vector) < 8 or len(initialization_vector) > 128:
  93. raise ValueError(
  94. "initialization_vector must be between 8 and 128 bytes (64 "
  95. "and 1024 bits)."
  96. )
  97. self._initialization_vector = initialization_vector
  98. if tag is not None:
  99. utils._check_bytes("tag", tag)
  100. if min_tag_length < 4:
  101. raise ValueError("min_tag_length must be >= 4")
  102. if len(tag) < min_tag_length:
  103. raise ValueError(
  104. f"Authentication tag must be {min_tag_length} bytes or "
  105. "longer."
  106. )
  107. self._tag = tag
  108. self._min_tag_length = min_tag_length
  109. @property
  110. def tag(self) -> bytes | None:
  111. return self._tag
  112. @property
  113. def initialization_vector(self) -> utils.Buffer:
  114. return self._initialization_vector
  115. def validate_for_algorithm(self, algorithm: CipherAlgorithm) -> None:
  116. _check_aes_key_length(self, algorithm)
  117. if not isinstance(algorithm, BlockCipherAlgorithm):
  118. raise UnsupportedAlgorithm(
  119. "GCM requires a block cipher algorithm",
  120. _Reasons.UNSUPPORTED_CIPHER,
  121. )
  122. block_size_bytes = algorithm.block_size // 8
  123. if self._tag is not None and len(self._tag) > block_size_bytes:
  124. raise ValueError(
  125. f"Authentication tag cannot be more than {block_size_bytes} "
  126. "bytes."
  127. )
  128. utils.deprecated(
  129. OFB,
  130. __name__,
  131. "OFB has been moved to "
  132. "cryptography.hazmat.decrepit.ciphers.modes.OFB and "
  133. "will be removed from "
  134. "cryptography.hazmat.primitives.ciphers.modes in 49.0.0.",
  135. utils.DeprecatedIn47,
  136. name="OFB",
  137. )
  138. utils.deprecated(
  139. CFB,
  140. __name__,
  141. "CFB has been moved to "
  142. "cryptography.hazmat.decrepit.ciphers.modes.CFB and "
  143. "will be removed from "
  144. "cryptography.hazmat.primitives.ciphers.modes in 49.0.0.",
  145. utils.DeprecatedIn47,
  146. name="CFB",
  147. )
  148. utils.deprecated(
  149. CFB8,
  150. __name__,
  151. "CFB8 has been moved to "
  152. "cryptography.hazmat.decrepit.ciphers.modes.CFB8 and "
  153. "will be removed from "
  154. "cryptography.hazmat.primitives.ciphers.modes in 49.0.0.",
  155. utils.DeprecatedIn47,
  156. name="CFB8",
  157. )