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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/// DES Permuted Choice 1 (PC-1) table.
///
/// Reduces the 64-bit key to 56 bits by removing parity bits
/// and permuting the remaining bits.
const PC1: = ;
/// DES Permuted Choice 2 (PC-2) table.
///
/// Reduces the 56-bit key halves into a 48-bit round key.
const PC2: = ;
/// Rounds where only one left shift is performed.
///
/// DES shift schedule:
///
/// 1, 2, 9, 16 → 1 shift
/// others → 2 shifts
const ONE_SHIFT_ROUNDS: = ;
/// Applies DES Permuted Choice 1 (PC-1).
///
/// Converts a 64-bit key into a 56-bit key by removing parity bits
/// and permuting the remaining bits.
///
/// # Arguments
///
/// * `key` - 64-bit DES key
///
/// # Returns
///
/// 56-bit permuted key stored in `u64`
///
/// # Examples
///
/// ```rust
/// use cryptograph::cryptography::des::key::permutated_choice_1;
///
/// let key = 0x133457799BBCDFF1;
/// let result = permutated_choice_1(key);
///
/// assert!(result <= 0x00FFFFFFFFFFFFFF);
/// ```
/// Applies DES Permuted Choice 2 (PC-2).
///
/// Combines left and right 28-bit halves and produces
/// a 48-bit round key.
///
/// # Arguments
///
/// * `left` - Left 28-bit key half
/// * `right` - Right 28-bit key half
///
/// # Returns
///
/// 48-bit round key stored in `u64`
///
/// # Examples
///
/// ```rust
/// use cryptograph::cryptography::des::key::permutated_choice_2;
///
/// let left = 0x0FFFFFFF;
/// let right = 0x0FFFFFFF;
///
/// let key = permutated_choice_2(left, right);
/// ```
/// Performs DES key schedule shifting.
///
/// Rotates both 28-bit key halves left according to
/// DES shift schedule.
///
/// # Arguments
///
/// * `left_key` - Left 28-bit key half
/// * `right_key` - Right 28-bit key half
/// * `n` - Current round number
///
/// # Returns
///
/// Tuple `(left, right)` shifted key halves
///
/// # Examples
///
/// ```rust
/// use cryptograph::cryptography::des::key::key_shift;
///
/// let left = 0x0FFFFFFF;
/// let right = 0x0FFFFFFF;
///
/// let (l, r) = key_shift(left, right, 1);
/// ```
/// Performs the inverse key rotation used during DES decryption.
///
/// # Description
///
/// This function applies the **inverse key schedule rotation** for the
/// Data Encryption Standard (DES). During DES encryption, the key halves
/// are rotated **left** according to a predefined shift schedule.
/// This function performs the **inverse operation** by rotating the key
/// halves **right**, allowing generation of subkeys in reverse order
/// for decryption.
///
/// DES splits the key into two 28-bit halves:
///
/// ```text
/// C_i | D_i
/// ```
///
/// During encryption:
///
/// ```text
/// C_i = left_rotate(C_{i-1})
/// D_i = left_rotate(D_{i-1})
/// ```
///
/// During decryption (this function):
///
/// ```text
/// C_{i-1} = right_rotate(C_i)
/// D_{i-1} = right_rotate(D_i)
/// ```
///
/// # Shift Schedule
///
/// DES uses the following rotation schedule:
///
/// | Round | Shift |
/// |-------|-------|
/// | 1 | 1 |
/// | 2 | 1 |
/// | 3–8 | 2 |
/// | 9 | 1 |
/// | 10–15 | 2 |
/// | 16 | 1 |
///
/// This function applies the **inverse** of that schedule.
///
/// # Arguments
///
/// * `left_key` - Left 28-bit key half (C_i)
/// * `right_key` - Right 28-bit key half (D_i)
/// * `n` - Current round number (1..=16)
///
/// # Returns
///
/// Returns a tuple containing:
///
/// ```text
/// (C_{i-1}, D_{i-1})
/// ```
///
/// # Implementation Details
///
/// - Uses 28-bit circular rotations
/// - Applies mask `0x0FFFFFFF` to maintain 28-bit width
/// - Uses bitwise operations for maximum performance
///
/// # Example
///
/// ```rust
/// use cryptograph::cryptography::des::key::inverse_key_shift;
/// let left = 0b1010101010101010101010101010;
/// let right = 0b0101010101010101010101010101;
///
/// let (l, r) = inverse_key_shift(left, right, 1);
/// ```
///
/// # Performance
///
/// This implementation:
///
/// - Uses only register operations
/// - Avoids allocations
/// - Runs in constant time
///
/// # Security Notes
///
/// This function is part of DES, which is considered insecure for
/// modern cryptographic use. Intended for educational or legacy purposes.
///
/// # References
///
/// - FIPS 46-3 (Data Encryption Standard)
/// - NIST DES Specification
/// - Feistel Network Key Scheduling