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
103
104class ChaCha20(CipherAlgorithm):
105 name = "ChaCha20"
106 key_sizes = frozenset([256])
107
108 def __init__(self, key: utils.Buffer, nonce: utils.Buffer):
109 self.key = _verify_key_size(self, key)
110 utils._check_byteslike("nonce", nonce)
111
112 if len(nonce) != 16:
113 raise ValueError("nonce must be 128-bits (16 bytes)")
114
115 self._nonce = nonce
116
117 @property
118 def nonce(self) -> utils.Buffer:
119 return self._nonce
120
121 @property
122 def key_size(self) -> int:
123 return len(self.key) * 8
124
125
126class SM4(BlockCipherAlgorithm):
127 name = "SM4"
128 block_size = 128
129 key_sizes = frozenset([128])
130
131 def __init__(self, key: bytes):
132 self.key = _verify_key_size(self, key)
133
134 @property
135 def key_size(self) -> int:
136 return len(self.key) * 8