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: bytes):
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: bytes):
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: bytes):
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: bytes):
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 this module in 48.0.0.",
86 utils.DeprecatedIn43,
87 name="ARC4",
88)
89
90
91utils.deprecated(
92 TripleDES,
93 __name__,
94 "TripleDES has been moved to "
95 "cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and "
96 "will be removed from this module in 48.0.0.",
97 utils.DeprecatedIn43,
98 name="TripleDES",
99)
100
101utils.deprecated(
102 Blowfish,
103 __name__,
104 "Blowfish has been moved to "
105 "cryptography.hazmat.decrepit.ciphers.algorithms.Blowfish and "
106 "will be removed from this module in 45.0.0.",
107 utils.DeprecatedIn37,
108 name="Blowfish",
109)
110
111
112utils.deprecated(
113 CAST5,
114 __name__,
115 "CAST5 has been moved to "
116 "cryptography.hazmat.decrepit.ciphers.algorithms.CAST5 and "
117 "will be removed from this module in 45.0.0.",
118 utils.DeprecatedIn37,
119 name="CAST5",
120)
121
122
123utils.deprecated(
124 IDEA,
125 __name__,
126 "IDEA has been moved to "
127 "cryptography.hazmat.decrepit.ciphers.algorithms.IDEA and "
128 "will be removed from this module in 45.0.0.",
129 utils.DeprecatedIn37,
130 name="IDEA",
131)
132
133
134utils.deprecated(
135 SEED,
136 __name__,
137 "SEED has been moved to "
138 "cryptography.hazmat.decrepit.ciphers.algorithms.SEED and "
139 "will be removed from this module in 45.0.0.",
140 utils.DeprecatedIn37,
141 name="SEED",
142)
143
144
145class ChaCha20(CipherAlgorithm):
146 name = "ChaCha20"
147 key_sizes = frozenset([256])
148
149 def __init__(self, key: bytes, nonce: bytes):
150 self.key = _verify_key_size(self, key)
151 utils._check_byteslike("nonce", nonce)
152
153 if len(nonce) != 16:
154 raise ValueError("nonce must be 128-bits (16 bytes)")
155
156 self._nonce = nonce
157
158 @property
159 def nonce(self) -> bytes:
160 return self._nonce
161
162 @property
163 def key_size(self) -> int:
164 return len(self.key) * 8
165
166
167class SM4(BlockCipherAlgorithm):
168 name = "SM4"
169 block_size = 128
170 key_sizes = frozenset([128])
171
172 def __init__(self, key: bytes):
173 self.key = _verify_key_size(self, key)
174
175 @property
176 def key_size(self) -> int:
177 return len(self.key) * 8