1#
2# A block cipher is instantiated as a combination of:
3# 1. A base cipher (such as AES)
4# 2. A mode of operation (such as CBC)
5#
6# Both items are implemented as C modules.
7#
8# The API of #1 is (replace "AES" with the name of the actual cipher):
9# - AES_start_operaion(key) --> base_cipher_state
10# - AES_encrypt(base_cipher_state, in, out, length)
11# - AES_decrypt(base_cipher_state, in, out, length)
12# - AES_stop_operation(base_cipher_state)
13#
14# Where base_cipher_state is AES_State, a struct with BlockBase (set of
15# pointers to encrypt/decrypt/stop) followed by cipher-specific data.
16#
17# The API of #2 is (replace "CBC" with the name of the actual mode):
18# - CBC_start_operation(base_cipher_state) --> mode_state
19# - CBC_encrypt(mode_state, in, out, length)
20# - CBC_decrypt(mode_state, in, out, length)
21# - CBC_stop_operation(mode_state)
22#
23# where mode_state is a a pointer to base_cipher_state plus mode-specific data.
24
25from __future__ import annotations
26
27from typing import TYPE_CHECKING, TypedDict, Union
28
29from Crypto.Util._typing import Buffer
30
31if TYPE_CHECKING:
32 from Crypto.Util.Counter import CounterParams
33
34
35class BlockCipherParams(TypedDict, total=False):
36 """Optional keyword arguments of ``new()`` for the classic block ciphers.
37 Which ones are accepted depends on the mode of operation."""
38
39 iv: Buffer
40 IV: Buffer
41 nonce: Buffer
42 segment_size: int
43 mac_len: int
44 msg_len: int
45 assoc_len: int
46 initial_value: Union[int, Buffer]
47 counter: CounterParams
48 tweak: Buffer
49 threads: int
50
51
52def _create_cipher(factory, key, mode, *args, **kwargs):
53 kwargs["key"] = key
54
55 if args:
56 if mode in (8, 9, 10, 11, 12, 15):
57 if len(args) > 1:
58 raise TypeError("Too many arguments for this mode")
59 kwargs["nonce"] = args[0]
60 elif mode == 16:
61 raise TypeError("The tweak must be passed as a keyword argument")
62 elif mode in (2, 3, 5, 7):
63 if len(args) > 1:
64 raise TypeError("Too many arguments for this mode")
65 kwargs["IV"] = args[0]
66 elif mode == 6:
67 if len(args) > 0:
68 raise TypeError("Too many arguments for this mode")
69 elif mode == 1:
70 raise TypeError("IV is not meaningful for the ECB mode")
71
72 res = None
73 extra_modes = kwargs.pop("add_aes_modes", False)
74
75 if mode == 1:
76 from Crypto.Cipher._mode_ecb import _create_ecb_cipher
77
78 res = _create_ecb_cipher(factory, **kwargs)
79 elif mode == 2:
80 from Crypto.Cipher._mode_cbc import _create_cbc_cipher
81
82 res = _create_cbc_cipher(factory, **kwargs)
83 elif mode == 3:
84 from Crypto.Cipher._mode_cfb import _create_cfb_cipher
85
86 res = _create_cfb_cipher(factory, **kwargs)
87 elif mode == 5:
88 from Crypto.Cipher._mode_ofb import _create_ofb_cipher
89
90 res = _create_ofb_cipher(factory, **kwargs)
91 elif mode == 6:
92 from Crypto.Cipher._mode_ctr import _create_ctr_cipher
93
94 res = _create_ctr_cipher(factory, **kwargs)
95 elif mode == 7:
96 from Crypto.Cipher._mode_openpgp import _create_openpgp_cipher
97
98 res = _create_openpgp_cipher(factory, **kwargs)
99 elif mode == 9:
100 from Crypto.Cipher._mode_eax import _create_eax_cipher
101
102 res = _create_eax_cipher(factory, **kwargs)
103 elif extra_modes:
104 if mode == 8:
105 from Crypto.Cipher._mode_ccm import _create_ccm_cipher
106
107 res = _create_ccm_cipher(factory, **kwargs)
108 elif mode == 10:
109 from Crypto.Cipher._mode_siv import _create_siv_cipher
110
111 res = _create_siv_cipher(factory, **kwargs)
112 elif mode == 11:
113 from Crypto.Cipher._mode_gcm import _create_gcm_cipher
114
115 res = _create_gcm_cipher(factory, **kwargs)
116 elif mode == 12:
117 from Crypto.Cipher._mode_ocb import _create_ocb_cipher
118
119 res = _create_ocb_cipher(factory, **kwargs)
120 elif mode == 13:
121 from Crypto.Cipher._mode_kw import _create_kw_cipher
122
123 res = _create_kw_cipher(factory, **kwargs)
124 elif mode == 14:
125 from Crypto.Cipher._mode_kwp import _create_kwp_cipher
126
127 res = _create_kwp_cipher(factory, **kwargs)
128 elif mode == 15:
129 from Crypto.Cipher._mode_gcm_siv import _create_gcm_siv_cipher
130
131 res = _create_gcm_siv_cipher(factory, **kwargs)
132 elif mode == 16:
133 from Crypto.Cipher._mode_xts import _create_xts_cipher
134
135 res = _create_xts_cipher(factory, **kwargs)
136
137 if res is None:
138 raise ValueError("Mode not supported")
139
140 return res