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