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