algorithms.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.hazmat.decrepit.ciphers.algorithms import (
  7. ARC4 as ARC4,
  8. )
  9. from cryptography.hazmat.decrepit.ciphers.algorithms import (
  10. CAST5 as CAST5,
  11. )
  12. from cryptography.hazmat.decrepit.ciphers.algorithms import (
  13. IDEA as IDEA,
  14. )
  15. from cryptography.hazmat.decrepit.ciphers.algorithms import (
  16. SEED as SEED,
  17. )
  18. from cryptography.hazmat.decrepit.ciphers.algorithms import (
  19. Blowfish as Blowfish,
  20. )
  21. from cryptography.hazmat.decrepit.ciphers.algorithms import (
  22. TripleDES as TripleDES,
  23. )
  24. from cryptography.hazmat.primitives._cipheralgorithm import _verify_key_size
  25. from cryptography.hazmat.primitives.ciphers import (
  26. BlockCipherAlgorithm,
  27. CipherAlgorithm,
  28. )
  29. class AES(BlockCipherAlgorithm):
  30. name = "AES"
  31. block_size = 128
  32. # 512 added to support AES-256-XTS, which uses 512-bit keys
  33. key_sizes = frozenset([128, 192, 256, 512])
  34. def __init__(self, key: bytes):
  35. self.key = _verify_key_size(self, key)
  36. @property
  37. def key_size(self) -> int:
  38. return len(self.key) * 8
  39. class AES128(BlockCipherAlgorithm):
  40. name = "AES"
  41. block_size = 128
  42. key_sizes = frozenset([128])
  43. key_size = 128
  44. def __init__(self, key: bytes):
  45. self.key = _verify_key_size(self, key)
  46. class AES256(BlockCipherAlgorithm):
  47. name = "AES"
  48. block_size = 128
  49. key_sizes = frozenset([256])
  50. key_size = 256
  51. def __init__(self, key: bytes):
  52. self.key = _verify_key_size(self, key)
  53. class Camellia(BlockCipherAlgorithm):
  54. name = "camellia"
  55. block_size = 128
  56. key_sizes = frozenset([128, 192, 256])
  57. def __init__(self, key: bytes):
  58. self.key = _verify_key_size(self, key)
  59. @property
  60. def key_size(self) -> int:
  61. return len(self.key) * 8
  62. utils.deprecated(
  63. ARC4,
  64. __name__,
  65. "ARC4 has been moved to "
  66. "cryptography.hazmat.decrepit.ciphers.algorithms.ARC4 and "
  67. "will be removed from this module in 48.0.0.",
  68. utils.DeprecatedIn43,
  69. name="ARC4",
  70. )
  71. utils.deprecated(
  72. TripleDES,
  73. __name__,
  74. "TripleDES has been moved to "
  75. "cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and "
  76. "will be removed from this module in 48.0.0.",
  77. utils.DeprecatedIn43,
  78. name="TripleDES",
  79. )
  80. utils.deprecated(
  81. Blowfish,
  82. __name__,
  83. "Blowfish has been moved to "
  84. "cryptography.hazmat.decrepit.ciphers.algorithms.Blowfish and "
  85. "will be removed from this module in 45.0.0.",
  86. utils.DeprecatedIn37,
  87. name="Blowfish",
  88. )
  89. utils.deprecated(
  90. CAST5,
  91. __name__,
  92. "CAST5 has been moved to "
  93. "cryptography.hazmat.decrepit.ciphers.algorithms.CAST5 and "
  94. "will be removed from this module in 45.0.0.",
  95. utils.DeprecatedIn37,
  96. name="CAST5",
  97. )
  98. utils.deprecated(
  99. IDEA,
  100. __name__,
  101. "IDEA has been moved to "
  102. "cryptography.hazmat.decrepit.ciphers.algorithms.IDEA and "
  103. "will be removed from this module in 45.0.0.",
  104. utils.DeprecatedIn37,
  105. name="IDEA",
  106. )
  107. utils.deprecated(
  108. SEED,
  109. __name__,
  110. "SEED has been moved to "
  111. "cryptography.hazmat.decrepit.ciphers.algorithms.SEED and "
  112. "will be removed from this module in 45.0.0.",
  113. utils.DeprecatedIn37,
  114. name="SEED",
  115. )
  116. class ChaCha20(CipherAlgorithm):
  117. name = "ChaCha20"
  118. key_sizes = frozenset([256])
  119. def __init__(self, key: bytes, nonce: bytes):
  120. self.key = _verify_key_size(self, key)
  121. utils._check_byteslike("nonce", nonce)
  122. if len(nonce) != 16:
  123. raise ValueError("nonce must be 128-bits (16 bytes)")
  124. self._nonce = nonce
  125. @property
  126. def nonce(self) -> bytes:
  127. return self._nonce
  128. @property
  129. def key_size(self) -> int:
  130. return len(self.key) * 8
  131. class SM4(BlockCipherAlgorithm):
  132. name = "SM4"
  133. block_size = 128
  134. key_sizes = frozenset([128])
  135. def __init__(self, key: bytes):
  136. self.key = _verify_key_size(self, key)
  137. @property
  138. def key_size(self) -> int:
  139. return len(self.key) * 8