1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//! DES and Triple DES on the engine the build selects.
use fmt;
use ;
use crate::;
type DesInner = crateDesRustCryptoEngine;
type DesInner = crateDesTableEngine;
type DesEdeInner = crateDesEdeRustCryptoEngine;
type DesEdeInner = crateDesEdeTableEngine;
/// DES on the least leaky engine the build provides: `DesRustCryptoEngine`
/// when the `rustcrypto` feature is on, otherwise
/// [`DesTableEngine`](crate::DesTableEngine). The choice is made at compile
/// time; the type and its API are the same under every configuration.
///
/// For legacy interoperability only; do not use in new designs.
///
/// Variable time under either engine: both look up S-boxes with secret data.
/// The RustCrypto engine's smaller S-boxes and branch-free key schedule narrow
/// the cache-timing channel compared with the table engine, so enable
/// `rustcrypto` to get them; this crate provides no constant-time alternative.
///
/// # Example
///
/// Call `init` again to install a new key or change direction.
///
/// ```
/// use tc_block_cipher::{BlockCipher, BlockCipherInit, CipherDirection, KeyRef};
/// use tc_des::{BLOCK_BYTES, DesEngine};
///
/// let key = [0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1];
/// let plaintext = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
/// let mut engine = DesEngine::new();
/// engine.init(CipherDirection::Encrypt, &KeyRef::new(&key))?;
/// let mut ciphertext = [0; BLOCK_BYTES];
/// engine.process_block(&plaintext, &mut ciphertext)?;
/// assert_eq!(ciphertext, [0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05]);
/// engine.init(CipherDirection::Decrypt, &KeyRef::new(&key))?;
/// let mut recovered = [0; BLOCK_BYTES];
/// engine.process_block(&ciphertext, &mut recovered)?;
/// assert_eq!(recovered, plaintext);
/// assert_eq!(engine.to_string(), "DES");
/// # Ok::<(), Box<dyn core::error::Error>>(())
/// ```
/// EDE Triple DES on the least leaky engine the build provides:
/// `DesEdeRustCryptoEngine` when the `rustcrypto` feature is on, otherwise
/// [`DesEdeTableEngine`](crate::DesEdeTableEngine). The choice is made at
/// compile time; the type and its API are the same under every configuration.
///
/// For legacy interoperability only; do not use in new designs. A 16-byte key
/// is used as `K1, K2, K1`.
///
/// Variable time under either engine: both look up S-boxes with secret data.
/// The RustCrypto engine's smaller S-boxes and branch-free key schedule narrow
/// the cache-timing channel compared with the table engine, so enable
/// `rustcrypto` to get them; this crate provides no constant-time alternative.
///
/// # Example
///
/// ```
/// use tc_block_cipher::{BlockCipher, BlockCipherInit, CipherDirection, KeyRef};
/// use tc_des::{BLOCK_BYTES, DesEdeEngine};
///
/// let key = [
/// 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x23, 0x45, 0x67, 0x89,
/// 0xab, 0xcd, 0xef, 0x01, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23,
/// ];
/// let plaintext = [0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10];
/// let mut engine = DesEdeEngine::new();
/// engine.init(CipherDirection::Encrypt, &KeyRef::new(&key))?;
/// let mut ciphertext = [0; BLOCK_BYTES];
/// engine.process_block(&plaintext, &mut ciphertext)?;
/// assert_eq!(ciphertext, [0x07, 0x37, 0xf6, 0xc5, 0x37, 0x50, 0xd4, 0xa4]);
/// engine.init(CipherDirection::Decrypt, &KeyRef::new(&key))?;
/// let mut recovered = [0; BLOCK_BYTES];
/// engine.process_block(&ciphertext, &mut recovered)?;
/// assert_eq!(recovered, plaintext);
/// assert_eq!(engine.to_string(), "DESede");
/// # Ok::<(), Box<dyn core::error::Error>>(())
/// ```