Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libluksde/libfcrypto/libfcrypto_serpent_context.c
Line
Count
Source
1
/*
2
 * Serpent (de/en)crypt functions
3
 *
4
 * Copyright (C) 2017-2026, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libfcrypto_definitions.h"
28
#include "libfcrypto_serpent_context.h"
29
30
/* Serpent
31
 *
32
 * http://www.cl.cam.ac.uk/~rja14/Papers/serpent.pdf
33
 */
34
#define libfcrypto_serpent_calculate_forward_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
35
620
  value0  = byte_stream_bit_rotate_left_32bit( value0, 13 ); \
36
620
  value2  = byte_stream_bit_rotate_left_32bit( value2, 3 ); \
37
620
  value1 ^= value0; \
38
620
  value4  = value0 << 3; \
39
620
  value3 ^= value2; \
40
620
  value1 ^= value2; \
41
620
  value1  = byte_stream_bit_rotate_left_32bit( value1, 1 ); \
42
620
  value3 ^= value4; \
43
620
  value3  = byte_stream_bit_rotate_left_32bit( value3, 7 ); \
44
620
  value4  = value1; \
45
620
  value0 ^= value1; \
46
620
  value4 <<= 7; \
47
620
  value2 ^= value3; \
48
620
  value0 ^= value3; \
49
620
  value2 ^= value4; \
50
620
  value3 ^= expanded_key_values[ expanded_key_index + 3 ]; \
51
620
  value1 ^= expanded_key_values[ expanded_key_index + 1 ]; \
52
620
  value0  = byte_stream_bit_rotate_left_32bit( value0, 5 ); \
53
620
  value2  = byte_stream_bit_rotate_left_32bit( value2, 22 ); \
54
620
  value0 ^= expanded_key_values[ expanded_key_index ]; \
55
620
  value2 ^= expanded_key_values[ expanded_key_index + 2 ];
56
57
#define libfcrypto_serpent_calculate_reverse_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
58
1.19M
  value0 ^= expanded_key_values[ expanded_key_index ]; \
59
1.19M
  value1 ^= expanded_key_values[ expanded_key_index + 1 ]; \
60
1.19M
  value2 ^= expanded_key_values[ expanded_key_index + 2 ]; \
61
1.19M
  \
62
1.19M
  value3 ^= expanded_key_values[ expanded_key_index + 3 ];   \
63
1.19M
  value0 = byte_stream_bit_rotate_right_32bit( value0, 5 ); \
64
1.19M
  value2 = byte_stream_bit_rotate_right_32bit( value2, 22 ); \
65
1.19M
  \
66
1.19M
  value4 =  value1; \
67
1.19M
  value2 ^= value3; \
68
1.19M
  value0 ^= value3; \
69
1.19M
  \
70
1.19M
  value4 <<= 7; \
71
1.19M
  value0 ^= value1; \
72
1.19M
  value1 = byte_stream_bit_rotate_right_32bit( value1, 1 );  \
73
1.19M
  value2 ^= value4; \
74
1.19M
  value3 = byte_stream_bit_rotate_right_32bit( value3, 7 ); \
75
1.19M
  value4 = value0 << 3; \
76
1.19M
  \
77
1.19M
  value1 ^= value0; \
78
1.19M
  value3 ^= value4; \
79
1.19M
  value0 = byte_stream_bit_rotate_right_32bit( value0, 13 ); \
80
1.19M
  value1 ^= value2; \
81
1.19M
  value3 ^= value2; \
82
1.19M
  value2 = byte_stream_bit_rotate_right_32bit( value2, 3 );
83
84
#define libfcrypto_serpent_calculate_forward_substitution0( value0, value1, value2, value3, value4 ) \
85
700
  value4  = value3; \
86
700
  value3 |= value0; \
87
700
  value0 ^= value4; \
88
700
  value4 ^= value2; \
89
700
  value4  = ~value4; \
90
700
  value3 ^= value1; \
91
700
  value1 &= value0; \
92
700
  value1 ^= value4; \
93
700
  value2 ^= value0; \
94
700
  value0 ^= value3; \
95
700
  value4 |= value0; \
96
700
  value0 ^= value2; \
97
700
  value2 &= value1; \
98
700
  value3 ^= value2; \
99
700
  value1  = ~value1; \
100
700
  value2 ^= value4; \
101
700
  value1 ^= value2;
102
103
#define libfcrypto_serpent_calculate_forward_substitution1( value0, value1, value2, value3, value4 ) \
104
700
  value4  = value1; \
105
700
  value1 ^= value0; \
106
700
  value0 ^= value3; \
107
700
  value3  = ~value3; \
108
700
  value4 &= value1; \
109
700
  value0 |= value1; \
110
700
  value3 ^= value2; \
111
700
  value0 ^= value3; \
112
700
  value1 ^= value3; \
113
700
  value3 ^= value4; \
114
700
  value1 |= value4; \
115
700
  value4 ^= value2; \
116
700
  value2 &= value0; \
117
700
  value2 ^= value1; \
118
700
  value1 |= value0; \
119
700
  value0  = ~value0; \
120
700
  value0 ^= value2; \
121
700
  value4 ^= value1;
122
123
#define libfcrypto_serpent_calculate_forward_substitution2( value0, value1, value2, value3, value4 ) \
124
700
  value3  = ~value3; \
125
700
  value1 ^= value0; \
126
700
  value4  = value0; \
127
700
  value0 &= value2; \
128
700
  value0 ^= value3; \
129
700
  value3 |= value4; \
130
700
  value2 ^= value1; \
131
700
  value3 ^= value1; \
132
700
  value1 &= value0; \
133
700
  value0 ^= value2; \
134
700
  value2 &= value3; \
135
700
  value3 |= value1; \
136
700
  value0  = ~value0; \
137
700
  value3 ^= value0; \
138
700
  value4 ^= value0; \
139
700
  value0 ^= value2; \
140
700
  value1 |= value2;
141
142
#define libfcrypto_serpent_calculate_forward_substitution3( value0, value1, value2, value3, value4 ) \
143
855
  value4  = value1; \
144
855
  value1 ^= value3; \
145
855
  value3 |= value0; \
146
855
  value4 &= value0; \
147
855
  value0 ^= value2; \
148
855
  value2 ^= value1; \
149
855
  value1 &= value3; \
150
855
  value2 ^= value3; \
151
855
  value0 |= value4; \
152
855
  value4 ^= value3; \
153
855
  value1 ^= value0; \
154
855
  value0 &= value3; \
155
855
  value3 &= value4; \
156
855
  value3 ^= value2; \
157
855
  value4 |= value1; \
158
855
  value2 &= value1; \
159
855
  value4 ^= value3; \
160
855
  value0 ^= value3; \
161
855
  value3 ^= value2;
162
163
#define libfcrypto_serpent_calculate_forward_substitution4( value0, value1, value2, value3, value4 ) \
164
700
  value4  = value3; \
165
700
  value3 &= value0; \
166
700
  value0 ^= value4; \
167
700
  value3 ^= value2; \
168
700
  value2 |= value4; \
169
700
  value0 ^= value1; \
170
700
  value4 ^= value3; \
171
700
  value2 |= value0; \
172
700
  value2 ^= value1; \
173
700
  value1 &= value0; \
174
700
  value1 ^= value4; \
175
700
  value4 &= value2; \
176
700
  value2 ^= value3; \
177
700
  value4 ^= value0; \
178
700
  value3 |= value1; \
179
700
  value1  = ~value1; \
180
700
  value3 ^= value0;
181
182
#define libfcrypto_serpent_calculate_forward_substitution5( value0, value1, value2, value3, value4 ) \
183
700
  value4  = value1; \
184
700
  value1 |= value0; \
185
700
  value2 ^= value1; \
186
700
  value3  = ~value3; \
187
700
  value4 ^= value0; \
188
700
  value0 ^= value2; \
189
700
  value1 &= value4; \
190
700
  value4 |= value3; \
191
700
  value4 ^= value0; \
192
700
  value0 &= value3; \
193
700
  value1 ^= value3; \
194
700
  value3 ^= value2; \
195
700
  value0 ^= value1; \
196
700
  value2 &= value4; \
197
700
  value1 ^= value2; \
198
700
  value2 &= value0; \
199
700
  value3 ^= value2;
200
201
#define libfcrypto_serpent_calculate_forward_substitution6( value0, value1, value2, value3, value4 ) \
202
700
  value4  = value1; \
203
700
  value3 ^= value0; \
204
700
  value1 ^= value2; \
205
700
  value2 ^= value0; \
206
700
  value0 &= value3; \
207
700
  value1 |= value3; \
208
700
  value4  = ~value4; \
209
700
  value0 ^= value1; \
210
700
  value1 ^= value2; \
211
700
  value3 ^= value4; \
212
700
  value4 ^= value0; \
213
700
  value2 &= value0; \
214
700
  value4 ^= value1; \
215
700
  value2 ^= value3; \
216
700
  value3 &= value1; \
217
700
  value3 ^= value0; \
218
700
  value1 ^= value2;
219
220
#define libfcrypto_serpent_calculate_forward_substitution7( value0, value1, value2, value3, value4 ) \
221
700
  value1  = ~value1; \
222
700
  value4  = value1; \
223
700
  value0  = ~value0; \
224
700
  value1 &= value2; \
225
700
  value1 ^= value3; \
226
700
  value3 |= value4; \
227
700
  value4 ^= value2; \
228
700
  value2 ^= value3; \
229
700
  value3 ^= value0; \
230
700
  value0 |= value1; \
231
700
  value2 &= value0; \
232
700
  value0 ^= value4; \
233
700
  value4 ^= value3; \
234
700
  value3 &= value0; \
235
700
  value4 ^= value1; \
236
700
  value2 ^= value4; \
237
700
  value3 ^= value1; \
238
700
  value4 |= value0; \
239
700
  value4 ^= value1;
240
241
#define libfcrypto_serpent_calculate_reverse_substitution0( value0, value1, value2, value3, value4 ) \
242
154k
  value4  = value3; \
243
154k
  value1 ^= value0; \
244
154k
  value3 |= value1; \
245
154k
  value4 ^= value1; \
246
154k
  value0  = ~value0; \
247
154k
  value2 ^= value3; \
248
154k
  value3 ^= value0; \
249
154k
  value0 &= value1; \
250
154k
  value0 ^= value2; \
251
154k
  value2 &= value3; \
252
154k
  value3 ^= value4; \
253
154k
  value2 ^= value3; \
254
154k
  value1 ^= value3; \
255
154k
  value3 &= value0; \
256
154k
  value1 ^= value0; \
257
154k
  value0 ^= value2; \
258
154k
  value4 ^= value3;
259
260
#define libfcrypto_serpent_calculate_reverse_substitution1( value0, value1, value2, value3, value4 ) \
261
154k
  value1 ^= value3; \
262
154k
  value4  = value0; \
263
154k
  value0 ^= value2; \
264
154k
  value2  = ~value2; \
265
154k
  value4 |= value1; \
266
154k
  value4 ^= value3; \
267
154k
  value3 &= value1; \
268
154k
  value1 ^= value2; \
269
154k
  value2 &= value4; \
270
154k
  value4 ^= value1; \
271
154k
  value1 |= value3; \
272
154k
  value3 ^= value0; \
273
154k
  value2 ^= value0; \
274
154k
  value0 |= value4; \
275
154k
  value2 ^= value4; \
276
154k
  value1 ^= value0; \
277
154k
  value4 ^= value1;
278
279
#define libfcrypto_serpent_calculate_reverse_substitution2( value0, value1, value2, value3, value4 ) \
280
154k
  value2 ^= value1; \
281
154k
  value4  = value3; \
282
154k
  value3  = ~value3; \
283
154k
  value3 |= value2; \
284
154k
  value2 ^= value4; \
285
154k
  value4 ^= value0; \
286
154k
  value3 ^= value1; \
287
154k
  value1 |= value2; \
288
154k
  value2 ^= value0; \
289
154k
  value1 ^= value4; \
290
154k
  value4 |= value3; \
291
154k
  value2 ^= value3; \
292
154k
  value4 ^= value2; \
293
154k
  value2 &= value1; \
294
154k
  value2 ^= value3; \
295
154k
  value3 ^= value4; \
296
154k
  value4 ^= value0;
297
298
#define libfcrypto_serpent_calculate_reverse_substitution3( value0, value1, value2, value3, value4 ) \
299
154k
  value2 ^= value1; \
300
154k
  value4  = value1; \
301
154k
  value1 &= value2; \
302
154k
  value1 ^= value0; \
303
154k
  value0 |= value4; \
304
154k
  value4 ^= value3; \
305
154k
  value0 ^= value3; \
306
154k
  value3 |= value1; \
307
154k
  value1 ^= value2; \
308
154k
  value1 ^= value3; \
309
154k
  value0 ^= value2; \
310
154k
  value2 ^= value3; \
311
154k
  value3 &= value1; \
312
154k
  value1 ^= value0; \
313
154k
  value0 &= value2; \
314
154k
  value4 ^= value3; \
315
154k
  value3 ^= value0; \
316
154k
  value0 ^= value1;
317
318
#define libfcrypto_serpent_calculate_reverse_substitution4( value0, value1, value2, value3, value4 ) \
319
154k
  value2 ^= value3; \
320
154k
  value4  = value0; \
321
154k
  value0 &= value1; \
322
154k
  value0 ^= value2; \
323
154k
  value2 |= value3; \
324
154k
  value4  = ~value4; \
325
154k
  value1 ^= value0; \
326
154k
  value0 ^= value2; \
327
154k
  value2 &= value4; \
328
154k
  value2 ^= value0; \
329
154k
  value0 |= value4; \
330
154k
  value0 ^= value3; \
331
154k
  value3 &= value2; \
332
154k
  value4 ^= value3; \
333
154k
  value3 ^= value1; \
334
154k
  value1 &= value0; \
335
154k
  value4 ^= value1; \
336
154k
  value0 ^= value3;
337
338
#define libfcrypto_serpent_calculate_reverse_substitution5( value0, value1, value2, value3, value4 ) \
339
154k
  value4  = value1; \
340
154k
  value1 |= value2; \
341
154k
  value2 ^= value4; \
342
154k
  value1 ^= value3; \
343
154k
  value3 &= value4; \
344
154k
  value2 ^= value3; \
345
154k
  value3 |= value0; \
346
154k
  value0  = ~value0; \
347
154k
  value3 ^= value2; \
348
154k
  value2 |= value0; \
349
154k
  value4 ^= value1; \
350
154k
  value2 ^= value4; \
351
154k
  value4 &= value0; \
352
154k
  value0 ^= value1; \
353
154k
  value1 ^= value3; \
354
154k
  value0 &= value2; \
355
154k
  value2 ^= value3; \
356
154k
  value0 ^= value2; \
357
154k
  value2 ^= value4; \
358
154k
  value4 ^= value3;
359
360
#define libfcrypto_serpent_calculate_reverse_substitution6( value0, value1, value2, value3, value4 ) \
361
154k
  value0 ^= value2; \
362
154k
  value4  = value0; \
363
154k
  value0 &= value3; \
364
154k
  value2 ^= value3; \
365
154k
  value0 ^= value2; \
366
154k
  value3 ^= value1; \
367
154k
  value2 |= value4; \
368
154k
  value2 ^= value3; \
369
154k
  value3 &= value0; \
370
154k
  value0  = ~value0; \
371
154k
  value3 ^= value1; \
372
154k
  value1 &= value2; \
373
154k
  value4 ^= value0; \
374
154k
  value3 ^= value4; \
375
154k
  value4 ^= value2; \
376
154k
  value0 ^= value1; \
377
154k
  value2 ^= value0;
378
379
#define libfcrypto_serpent_calculate_reverse_substitution7( value0, value1, value2, value3, value4 ) \
380
154k
  value4  = value3; \
381
154k
  value3 &= value0; \
382
154k
  value0 ^= value2; \
383
154k
  value2 |= value4; \
384
154k
  value4 ^= value1; \
385
154k
  value0  = ~value0; \
386
154k
  value1 |= value3; \
387
154k
  value4 ^= value0; \
388
154k
  value0 &= value2; \
389
154k
  value0 ^= value1; \
390
154k
  value1 &= value2; \
391
154k
  value3 ^= value2; \
392
154k
  value4 ^= value3; \
393
154k
  value2 &= value3; \
394
154k
  value3 |= value0; \
395
154k
  value1 ^= value4; \
396
154k
  value3 ^= value4; \
397
154k
  value4 &= value0; \
398
154k
  value4 ^= value2;
399
400
20.4k
#define GOLDEN_RATIO_FRACTION 0x9e3779b9UL
401
402
#define libfcrypto_serpent_calculate_expanded_key( expanded_key_values, expanded_key_index, value0, value1, value2, value3 ) \
403
20.4k
  value1 ^= value3; \
404
20.4k
  value1 ^= value2; \
405
20.4k
  value1 ^= value0; \
406
20.4k
  value1 ^= GOLDEN_RATIO_FRACTION ^ expanded_key_index; \
407
20.4k
  value1 = byte_stream_bit_rotate_left_32bit( value1, 11 ); \
408
20.4k
  expanded_key_values[ expanded_key_index ] = value1;
409
410
#define libfcrypto_serpent_calculate_expanded_key_with_forward_substitution0( expanded_key_value0, expanded_key_value1, expanded_key_value2, expanded_key_value3, expanded_key_value4, expanded_key_value5, expanded_key_value6, expanded_key_value7, value0, value1, value2, value3, value4 ) \
411
620
  expanded_key_value0 = value0; \
412
620
  expanded_key_value1 = value1; \
413
620
  expanded_key_value2 = value2; \
414
620
  expanded_key_value3 = value3; \
415
620
  value0 = expanded_key_value4; \
416
620
  value1 = expanded_key_value5; \
417
620
  value2 = expanded_key_value6; \
418
620
  value3 = expanded_key_value7; \
419
620
  libfcrypto_serpent_calculate_forward_substitution0( value0, value1, value2, value3, value4 );
420
421
#define libfcrypto_serpent_calculate_expanded_key_with_forward_substitution1( expanded_key_value0, expanded_key_value1, expanded_key_value2, expanded_key_value3, expanded_key_value4, expanded_key_value5, expanded_key_value6, expanded_key_value7, value0, value1, value2, value3, value4 ) \
422
620
  expanded_key_value0 = value0; \
423
620
  expanded_key_value1 = value1; \
424
620
  expanded_key_value2 = value2; \
425
620
  expanded_key_value3 = value3; \
426
620
  value0 = expanded_key_value4; \
427
620
  value1 = expanded_key_value5; \
428
620
  value2 = expanded_key_value6; \
429
620
  value3 = expanded_key_value7; \
430
620
  libfcrypto_serpent_calculate_forward_substitution1( value0, value1, value2, value3, value4 );
431
432
#define libfcrypto_serpent_calculate_expanded_key_with_forward_substitution2( expanded_key_value0, expanded_key_value1, expanded_key_value2, expanded_key_value3, expanded_key_value4, expanded_key_value5, expanded_key_value6, expanded_key_value7, value0, value1, value2, value3, value4 ) \
433
620
  expanded_key_value0 = value0; \
434
620
  expanded_key_value1 = value1; \
435
620
  expanded_key_value2 = value2; \
436
620
  expanded_key_value3 = value3; \
437
620
  value0 = expanded_key_value4; \
438
620
  value1 = expanded_key_value5; \
439
620
  value2 = expanded_key_value6; \
440
620
  value3 = expanded_key_value7; \
441
620
  libfcrypto_serpent_calculate_forward_substitution2( value0, value1, value2, value3, value4 );
442
443
#define libfcrypto_serpent_calculate_expanded_key_with_forward_substitution3( expanded_key_value0, expanded_key_value1, expanded_key_value2, expanded_key_value3, expanded_key_value4, expanded_key_value5, expanded_key_value6, expanded_key_value7, value0, value1, value2, value3, value4 ) \
444
620
  expanded_key_value0 = value0; \
445
620
  expanded_key_value1 = value1; \
446
620
  expanded_key_value2 = value2; \
447
620
  expanded_key_value3 = value3; \
448
620
  value0 = expanded_key_value4; \
449
620
  value1 = expanded_key_value5; \
450
620
  value2 = expanded_key_value6; \
451
620
  value3 = expanded_key_value7; \
452
620
  libfcrypto_serpent_calculate_forward_substitution3( value0, value1, value2, value3, value4 );
453
454
#define libfcrypto_serpent_calculate_expanded_key_with_forward_substitution4( expanded_key_value0, expanded_key_value1, expanded_key_value2, expanded_key_value3, expanded_key_value4, expanded_key_value5, expanded_key_value6, expanded_key_value7, value0, value1, value2, value3, value4 ) \
455
620
  expanded_key_value0 = value0; \
456
620
  expanded_key_value1 = value1; \
457
620
  expanded_key_value2 = value2; \
458
620
  expanded_key_value3 = value3; \
459
620
  value0 = expanded_key_value4; \
460
620
  value1 = expanded_key_value5; \
461
620
  value2 = expanded_key_value6; \
462
620
  value3 = expanded_key_value7; \
463
620
  libfcrypto_serpent_calculate_forward_substitution4( value0, value1, value2, value3, value4 );
464
465
#define libfcrypto_serpent_calculate_expanded_key_with_forward_substitution5( expanded_key_value0, expanded_key_value1, expanded_key_value2, expanded_key_value3, expanded_key_value4, expanded_key_value5, expanded_key_value6, expanded_key_value7, value0, value1, value2, value3, value4 ) \
466
620
  expanded_key_value0 = value0; \
467
620
  expanded_key_value1 = value1; \
468
620
  expanded_key_value2 = value2; \
469
620
  expanded_key_value3 = value3; \
470
620
  value0 = expanded_key_value4; \
471
620
  value1 = expanded_key_value5; \
472
620
  value2 = expanded_key_value6; \
473
620
  value3 = expanded_key_value7; \
474
620
  libfcrypto_serpent_calculate_forward_substitution5( value0, value1, value2, value3, value4 );
475
476
#define libfcrypto_serpent_calculate_expanded_key_with_forward_substitution6( expanded_key_value0, expanded_key_value1, expanded_key_value2, expanded_key_value3, expanded_key_value4, expanded_key_value5, expanded_key_value6, expanded_key_value7, value0, value1, value2, value3, value4 ) \
477
620
  expanded_key_value0 = value0; \
478
620
  expanded_key_value1 = value1; \
479
620
  expanded_key_value2 = value2; \
480
620
  expanded_key_value3 = value3; \
481
620
  value0 = expanded_key_value4; \
482
620
  value1 = expanded_key_value5; \
483
620
  value2 = expanded_key_value6; \
484
620
  value3 = expanded_key_value7; \
485
620
  libfcrypto_serpent_calculate_forward_substitution6( value0, value1, value2, value3, value4 );
486
487
#define libfcrypto_serpent_calculate_expanded_key_with_forward_substitution7( expanded_key_value0, expanded_key_value1, expanded_key_value2, expanded_key_value3, expanded_key_value4, expanded_key_value5, expanded_key_value6, expanded_key_value7, value0, value1, value2, value3, value4 ) \
488
620
  expanded_key_value0 = value0; \
489
620
  expanded_key_value1 = value1; \
490
620
  expanded_key_value2 = value2; \
491
620
  expanded_key_value3 = value3; \
492
620
  value0 = expanded_key_value4; \
493
620
  value1 = expanded_key_value5; \
494
620
  value2 = expanded_key_value6; \
495
620
  value3 = expanded_key_value7; \
496
620
  libfcrypto_serpent_calculate_forward_substitution7( value0, value1, value2, value3, value4 );
497
498
#define libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution0( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
499
60
  libfcrypto_serpent_calculate_forward_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
500
60
  libfcrypto_serpent_calculate_forward_substitution0( value0, value1, value2, value3, value4 );
501
502
#define libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution1( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
503
80
  libfcrypto_serpent_calculate_forward_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
504
80
  libfcrypto_serpent_calculate_forward_substitution1( value0, value1, value2, value3, value4 );
505
506
#define libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution2( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
507
80
  libfcrypto_serpent_calculate_forward_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
508
80
  libfcrypto_serpent_calculate_forward_substitution2( value0, value1, value2, value3, value4 );
509
510
#define libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution3( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
511
80
  libfcrypto_serpent_calculate_forward_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
512
80
  libfcrypto_serpent_calculate_forward_substitution3( value0, value1, value2, value3, value4 );
513
514
#define libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution4( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
515
80
  libfcrypto_serpent_calculate_forward_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
516
80
  libfcrypto_serpent_calculate_forward_substitution4( value0, value1, value2, value3, value4 );
517
518
#define libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution5( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
519
80
  libfcrypto_serpent_calculate_forward_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
520
80
  libfcrypto_serpent_calculate_forward_substitution5( value0, value1, value2, value3, value4 );
521
522
#define libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution6( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
523
80
  libfcrypto_serpent_calculate_forward_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
524
80
  libfcrypto_serpent_calculate_forward_substitution6( value0, value1, value2, value3, value4 );
525
526
#define libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution7( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
527
80
  libfcrypto_serpent_calculate_forward_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
528
80
  libfcrypto_serpent_calculate_forward_substitution7( value0, value1, value2, value3, value4 );
529
530
#define libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution0( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
531
154k
  libfcrypto_serpent_calculate_reverse_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
532
154k
  libfcrypto_serpent_calculate_reverse_substitution0( value0, value1, value2, value3, value4 );
533
534
#define libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution1( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
535
154k
  libfcrypto_serpent_calculate_reverse_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
536
154k
  libfcrypto_serpent_calculate_reverse_substitution1( value0, value1, value2, value3, value4 );
537
538
#define libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution2( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
539
154k
  libfcrypto_serpent_calculate_reverse_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
540
154k
  libfcrypto_serpent_calculate_reverse_substitution2( value0, value1, value2, value3, value4 );
541
542
#define libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution3( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
543
154k
  libfcrypto_serpent_calculate_reverse_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
544
154k
  libfcrypto_serpent_calculate_reverse_substitution3( value0, value1, value2, value3, value4 );
545
546
#define libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution4( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
547
154k
  libfcrypto_serpent_calculate_reverse_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
548
154k
  libfcrypto_serpent_calculate_reverse_substitution4( value0, value1, value2, value3, value4 );
549
550
#define libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution5( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
551
154k
  libfcrypto_serpent_calculate_reverse_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
552
154k
  libfcrypto_serpent_calculate_reverse_substitution5( value0, value1, value2, value3, value4 );
553
554
#define libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution6( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
555
154k
  libfcrypto_serpent_calculate_reverse_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
556
154k
  libfcrypto_serpent_calculate_reverse_substitution6( value0, value1, value2, value3, value4 );
557
558
#define libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution7( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ) \
559
115k
  libfcrypto_serpent_calculate_reverse_linear_transformation( expanded_key_values, expanded_key_index, value0, value1, value2, value3, value4 ); \
560
115k
  libfcrypto_serpent_calculate_reverse_substitution7( value0, value1, value2, value3, value4 );
561
562
/* Creates a context
563
 * Make sure the value context is referencing, is set to NULL
564
 * Returns 1 if successful or -1 on error
565
 */
566
int libfcrypto_serpent_context_initialize(
567
     libfcrypto_serpent_context_t **context,
568
     libcerror_error_t **error )
569
157
{
570
157
  libfcrypto_internal_serpent_context_t *internal_context = NULL;
571
157
  static char *function                                   = "libfcrypto_serpent_context_initialize";
572
573
157
  if( context == NULL )
574
0
  {
575
0
    libcerror_error_set(
576
0
     error,
577
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
578
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
579
0
     "%s: invalid context.",
580
0
     function );
581
582
0
    return( -1 );
583
0
  }
584
157
  if( *context != NULL )
585
0
  {
586
0
    libcerror_error_set(
587
0
     error,
588
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
589
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
590
0
     "%s: invalid context value already set.",
591
0
     function );
592
593
0
    return( -1 );
594
0
  }
595
157
  internal_context = memory_allocate_structure(
596
157
                      libfcrypto_internal_serpent_context_t );
597
598
157
  if( internal_context == NULL )
599
0
  {
600
0
    libcerror_error_set(
601
0
     error,
602
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
603
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
604
0
     "%s: unable to create context.",
605
0
     function );
606
607
0
    goto on_error;
608
0
  }
609
157
  if( memory_set(
610
157
       internal_context,
611
157
       0,
612
157
       sizeof( libfcrypto_internal_serpent_context_t ) ) == NULL )
613
0
  {
614
0
    libcerror_error_set(
615
0
     error,
616
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
617
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
618
0
     "%s: unable to clear context.",
619
0
     function );
620
621
0
    goto on_error;
622
0
  }
623
157
  *context = (libfcrypto_serpent_context_t *) internal_context;
624
625
157
  return( 1 );
626
627
0
on_error:
628
0
  if( internal_context != NULL )
629
0
  {
630
0
    memory_free(
631
0
     internal_context );
632
0
  }
633
0
  return( -1 );
634
157
}
635
636
/* Frees a context
637
 * Returns 1 if successful or -1 on error
638
 */
639
int libfcrypto_serpent_context_free(
640
     libfcrypto_serpent_context_t **context,
641
     libcerror_error_t **error )
642
157
{
643
157
  libfcrypto_internal_serpent_context_t *internal_context = NULL;
644
157
  static char *function                                   = "libfcrypto_serpent_context_free";
645
157
  int result                                              = 1;
646
647
157
  if( context == NULL )
648
0
  {
649
0
    libcerror_error_set(
650
0
     error,
651
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
652
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
653
0
     "%s: invalid context.",
654
0
     function );
655
656
0
    return( -1 );
657
0
  }
658
157
  if( *context != NULL )
659
157
  {
660
157
    internal_context = (libfcrypto_internal_serpent_context_t *) *context;
661
157
    *context         = NULL;
662
663
157
    if( memory_set(
664
157
         internal_context,
665
157
         0,
666
157
         sizeof( libfcrypto_internal_serpent_context_t ) ) == NULL )
667
0
    {
668
0
      libcerror_error_set(
669
0
       error,
670
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
671
0
       LIBCERROR_MEMORY_ERROR_SET_FAILED,
672
0
       "%s: unable to clear context.",
673
0
       function );
674
675
0
      result = -1;
676
0
    }
677
157
    memory_free(
678
157
     internal_context );
679
157
  }
680
157
  return( result );
681
157
}
682
683
/* Sets the key
684
 * Returns 1 if successful or -1 on error
685
 */
686
int libfcrypto_serpent_context_set_key(
687
     libfcrypto_serpent_context_t *context,
688
     const uint8_t *key,
689
     size_t key_bit_size,
690
     libcerror_error_t **error )
691
156
{
692
156
  uint8_t key_data[ 32 ];
693
694
156
  libfcrypto_internal_serpent_context_t *internal_context = NULL;
695
156
  static char *function                                   = "libfcrypto_serpent_context_set_key";
696
156
  size_t key_byte_offset                                  = 0;
697
156
  size_t key_byte_size                                    = 0;
698
156
  uint32_t value0                                         = 0;
699
156
  uint32_t value1                                         = 0;
700
156
  uint32_t value2                                         = 0;
701
156
  uint32_t value3                                         = 0;
702
156
  uint32_t value4                                         = 0;
703
156
  uint8_t expanded_key_index                              = 0;
704
156
  uint8_t previous_expanded_key_index                     = 0;
705
706
156
  if( context == NULL )
707
0
  {
708
0
    libcerror_error_set(
709
0
     error,
710
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
711
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
712
0
     "%s: invalid context.",
713
0
     function );
714
715
0
    return( -1 );
716
0
  }
717
156
  internal_context = (libfcrypto_internal_serpent_context_t *) context;
718
719
156
  if( key == NULL )
720
0
  {
721
0
    libcerror_error_set(
722
0
     error,
723
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
724
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
725
0
     "%s: invalid key.",
726
0
     function );
727
728
0
    return( -1 );
729
0
  }
730
156
  if( ( key_bit_size != 128 )
731
47
   && ( key_bit_size != 192 )
732
47
   && ( key_bit_size != 256 ) )
733
1
  {
734
1
    libcerror_error_set(
735
1
     error,
736
1
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
737
1
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
738
1
     "%s: unsupported key bit size.",
739
1
     function );
740
741
1
    return( -1 );
742
1
  }
743
155
  if( memory_set(
744
155
       key_data,
745
155
       0,
746
155
       32  ) == NULL )
747
0
  {
748
0
    libcerror_error_set(
749
0
     error,
750
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
751
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
752
0
     "%s: unable to clear key data.",
753
0
     function );
754
755
0
    goto on_error;
756
0
  }
757
155
  if( memory_set(
758
155
       internal_context->expanded_key,
759
155
       0,
760
155
       sizeof( uint32_t ) * LIBFCRYPTO_SERPENT_NUMBER_OF_EXPANDED_KEY_ELEMENTS ) == NULL )
761
0
  {
762
0
    libcerror_error_set(
763
0
     error,
764
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
765
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
766
0
     "%s: unable to clear expanded key.",
767
0
     function );
768
769
0
    goto on_error;
770
0
  }
771
  /* 1. Copy and pad the provided key
772
   */
773
155
  key_byte_size = key_bit_size / 8;
774
775
155
  if( memory_copy(
776
155
       key_data,
777
155
       key,
778
155
       key_byte_size ) == NULL )
779
0
  {
780
0
    libcerror_error_set(
781
0
     error,
782
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
783
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
784
0
     "%s: unable to copy key to key data.",
785
0
     function );
786
787
0
    goto on_error;
788
0
  }
789
155
  if( key_byte_size < 32 )
790
109
  {
791
109
    key_data[ key_byte_size ] = 1;
792
109
  }
793
155
  for( key_byte_offset = 0;
794
1.39k
       key_byte_offset < 32;
795
1.24k
       key_byte_offset += 4 )
796
1.24k
  {
797
1.24k
    byte_stream_copy_to_uint32_little_endian(
798
1.24k
     &( key_data[ key_byte_offset ] ),
799
1.24k
     value0 );
800
801
1.24k
    internal_context->expanded_key[ expanded_key_index++ ] = value0;
802
1.24k
  }
803
  /* 2. Calculate the prekeys
804
   */
805
155
  value0 = internal_context->expanded_key[ 3 ];
806
155
  value1 = internal_context->expanded_key[ 4 ];
807
155
  value2 = internal_context->expanded_key[ 5 ];
808
155
  value3 = internal_context->expanded_key[ 6 ];
809
155
  value4 = internal_context->expanded_key[ 7 ];
810
811
155
  libfcrypto_serpent_calculate_expanded_key(
812
155
   internal_context->expanded_key,
813
155
   0,
814
155
   internal_context->expanded_key[ 0 ],
815
155
   value0,
816
155
   value4,
817
155
   value2 );
818
819
155
  libfcrypto_serpent_calculate_expanded_key(
820
155
   internal_context->expanded_key,
821
155
   1,
822
155
   internal_context->expanded_key[ 1 ],
823
155
   value1,
824
155
   value0,
825
155
   value3 );
826
827
155
  libfcrypto_serpent_calculate_expanded_key(
828
155
   internal_context->expanded_key,
829
155
   2,
830
155
   ( internal_context->expanded_key )[ 2 ],
831
155
   value2,
832
155
   value1,
833
155
   value4 );
834
835
155
  libfcrypto_serpent_calculate_expanded_key(
836
155
   internal_context->expanded_key,
837
155
   3,
838
155
   ( internal_context->expanded_key )[ 3 ],
839
155
   value3,
840
155
   value2,
841
155
   value0 );
842
843
155
  libfcrypto_serpent_calculate_expanded_key(
844
155
   internal_context->expanded_key,
845
155
   4,
846
155
   ( internal_context->expanded_key )[ 4 ],
847
155
   value4,
848
155
   value3,
849
155
   value1 );
850
851
155
  libfcrypto_serpent_calculate_expanded_key(
852
155
   internal_context->expanded_key,
853
155
   5,
854
155
   ( internal_context->expanded_key )[ 5 ],
855
155
   value0,
856
155
   value4,
857
155
   value2 );
858
859
155
  libfcrypto_serpent_calculate_expanded_key(
860
155
   internal_context->expanded_key,
861
155
   6,
862
155
   ( internal_context->expanded_key )[ 6 ],
863
155
   value1,
864
155
   value0,
865
155
   value3 );
866
867
155
  libfcrypto_serpent_calculate_expanded_key(
868
155
   internal_context->expanded_key,
869
155
   7,
870
155
   ( internal_context->expanded_key )[ 7 ],
871
155
   value2,
872
155
   value1,
873
155
   value4 );
874
875
155
  libfcrypto_serpent_calculate_expanded_key(
876
155
   internal_context->expanded_key,
877
155
   8,
878
155
   ( internal_context->expanded_key )[ 0 ],
879
155
   value3,
880
155
   value2,
881
155
   value0 );
882
883
155
  libfcrypto_serpent_calculate_expanded_key(
884
155
   internal_context->expanded_key,
885
155
   9,
886
155
   ( internal_context->expanded_key )[ 1 ],
887
155
   value4,
888
155
   value3,
889
155
   value1 );
890
891
155
  libfcrypto_serpent_calculate_expanded_key(
892
155
   internal_context->expanded_key,
893
155
   10,
894
155
   ( internal_context->expanded_key )[ 2 ],
895
155
   value0,
896
155
   value4,
897
155
   value2 );
898
899
155
  libfcrypto_serpent_calculate_expanded_key(
900
155
   internal_context->expanded_key,
901
155
   11,
902
155
   ( internal_context->expanded_key )[ 3 ],
903
155
   value1,
904
155
   value0,
905
155
   value3 );
906
907
155
  previous_expanded_key_index = 4;
908
155
  expanded_key_index          = 12;
909
910
3.87k
  while( expanded_key_index < 128 )
911
3.72k
  {
912
3.72k
    libfcrypto_serpent_calculate_expanded_key(
913
3.72k
     internal_context->expanded_key,
914
3.72k
     expanded_key_index,
915
3.72k
     ( internal_context->expanded_key )[ previous_expanded_key_index ],
916
3.72k
     value2,
917
3.72k
     value1,
918
3.72k
     value4 );
919
920
3.72k
    expanded_key_index++;
921
3.72k
    previous_expanded_key_index++;
922
923
3.72k
    libfcrypto_serpent_calculate_expanded_key(
924
3.72k
     internal_context->expanded_key,
925
3.72k
     expanded_key_index,
926
3.72k
     ( internal_context->expanded_key )[ previous_expanded_key_index ],
927
3.72k
     value3,
928
3.72k
     value2,
929
3.72k
     value0 );
930
931
3.72k
    expanded_key_index++;
932
3.72k
    previous_expanded_key_index++;
933
934
3.72k
    libfcrypto_serpent_calculate_expanded_key(
935
3.72k
     internal_context->expanded_key,
936
3.72k
     expanded_key_index,
937
3.72k
     ( internal_context->expanded_key )[ previous_expanded_key_index ],
938
3.72k
     value4,
939
3.72k
     value3,
940
3.72k
     value1 );
941
942
3.72k
    expanded_key_index++;
943
3.72k
    previous_expanded_key_index++;
944
945
3.72k
    libfcrypto_serpent_calculate_expanded_key(
946
3.72k
     internal_context->expanded_key,
947
3.72k
     expanded_key_index,
948
3.72k
     ( internal_context->expanded_key )[ previous_expanded_key_index ],
949
3.72k
     value0,
950
3.72k
     value4,
951
3.72k
     value2 );
952
953
3.72k
    expanded_key_index++;
954
3.72k
    previous_expanded_key_index++;
955
956
3.72k
    libfcrypto_serpent_calculate_expanded_key(
957
3.72k
     internal_context->expanded_key,
958
3.72k
     expanded_key_index,
959
3.72k
     ( internal_context->expanded_key )[ previous_expanded_key_index ],
960
3.72k
     value1,
961
3.72k
     value0,
962
3.72k
     value3 );
963
964
3.72k
    expanded_key_index++;
965
3.72k
    previous_expanded_key_index++;
966
3.72k
  }
967
  /* 3. Calculate the round keys
968
   */
969
155
  libfcrypto_serpent_calculate_forward_substitution3(
970
155
   value3,
971
155
   value4,
972
155
   value0,
973
155
   value1,
974
155
   value2 );
975
976
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution4(
977
155
   internal_context->expanded_key[ 128 ],
978
155
   internal_context->expanded_key[ 129 ],
979
155
   internal_context->expanded_key[ 130 ],
980
155
   internal_context->expanded_key[ 131 ],
981
155
   internal_context->expanded_key[ 124 ],
982
155
   internal_context->expanded_key[ 125 ],
983
155
   internal_context->expanded_key[ 126 ],
984
155
   internal_context->expanded_key[ 127 ],
985
155
   value1,
986
155
   value2,
987
155
   value4,
988
155
   value3,
989
155
   value0 );
990
991
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution5(
992
155
   internal_context->expanded_key[ 124 ],
993
155
   internal_context->expanded_key[ 125 ],
994
155
   internal_context->expanded_key[ 126 ],
995
155
   internal_context->expanded_key[ 127 ],
996
155
   internal_context->expanded_key[ 120 ],
997
155
   internal_context->expanded_key[ 121 ],
998
155
   internal_context->expanded_key[ 122 ],
999
155
   internal_context->expanded_key[ 123 ],
1000
155
   value2,
1001
155
   value4,
1002
155
   value3,
1003
155
   value0,
1004
155
   value1 );
1005
1006
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution6(
1007
155
   internal_context->expanded_key[ 120 ],
1008
155
   internal_context->expanded_key[ 121 ],
1009
155
   internal_context->expanded_key[ 122 ],
1010
155
   internal_context->expanded_key[ 123 ],
1011
155
   internal_context->expanded_key[ 116 ],
1012
155
   internal_context->expanded_key[ 117 ],
1013
155
   internal_context->expanded_key[ 118 ],
1014
155
   internal_context->expanded_key[ 119 ],
1015
155
   value1,
1016
155
   value2,
1017
155
   value4,
1018
155
   value0,
1019
155
   value3 );
1020
1021
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution7(
1022
155
   internal_context->expanded_key[ 116 ],
1023
155
   internal_context->expanded_key[ 117 ],
1024
155
   internal_context->expanded_key[ 118 ],
1025
155
   internal_context->expanded_key[ 119 ],
1026
155
   internal_context->expanded_key[ 112 ],
1027
155
   internal_context->expanded_key[ 113 ],
1028
155
   internal_context->expanded_key[ 114 ],
1029
155
   internal_context->expanded_key[ 115 ],
1030
155
   value4,
1031
155
   value3,
1032
155
   value2,
1033
155
   value0,
1034
155
   value1 );
1035
1036
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution0(
1037
155
   internal_context->expanded_key[ 112 ],
1038
155
   internal_context->expanded_key[ 113 ],
1039
155
   internal_context->expanded_key[ 114 ],
1040
155
   internal_context->expanded_key[ 115 ],
1041
155
   internal_context->expanded_key[ 108 ],
1042
155
   internal_context->expanded_key[ 109 ],
1043
155
   internal_context->expanded_key[ 110 ],
1044
155
   internal_context->expanded_key[ 111 ],
1045
155
   value1,
1046
155
   value2,
1047
155
   value0,
1048
155
   value4,
1049
155
   value3 );
1050
1051
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution1(
1052
155
   internal_context->expanded_key[ 108 ],
1053
155
   internal_context->expanded_key[ 109 ],
1054
155
   internal_context->expanded_key[ 110 ],
1055
155
   internal_context->expanded_key[ 111 ],
1056
155
   internal_context->expanded_key[ 104 ],
1057
155
   internal_context->expanded_key[ 105 ],
1058
155
   internal_context->expanded_key[ 106 ],
1059
155
   internal_context->expanded_key[ 107 ],
1060
155
   value0,
1061
155
   value2,
1062
155
   value4,
1063
155
   value1,
1064
155
   value3 );
1065
1066
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution2(
1067
155
   internal_context->expanded_key[ 104 ],
1068
155
   internal_context->expanded_key[ 105 ],
1069
155
   internal_context->expanded_key[ 106 ],
1070
155
   internal_context->expanded_key[ 107 ],
1071
155
   internal_context->expanded_key[ 100 ],
1072
155
   internal_context->expanded_key[ 101 ],
1073
155
   internal_context->expanded_key[ 102 ],
1074
155
   internal_context->expanded_key[ 103 ],
1075
155
   value3,
1076
155
   value4,
1077
155
   value1,
1078
155
   value0,
1079
155
   value2 );
1080
1081
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution3(
1082
155
   internal_context->expanded_key[ 100 ],
1083
155
   internal_context->expanded_key[ 101 ],
1084
155
   internal_context->expanded_key[ 102 ],
1085
155
   internal_context->expanded_key[ 103 ],
1086
155
   internal_context->expanded_key[ 96 ],
1087
155
   internal_context->expanded_key[ 97 ],
1088
155
   internal_context->expanded_key[ 98 ],
1089
155
   internal_context->expanded_key[ 99 ],
1090
155
   value2,
1091
155
   value4,
1092
155
   value3,
1093
155
   value0,
1094
155
   value1 );
1095
1096
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution4(
1097
155
   internal_context->expanded_key[ 96 ],
1098
155
   internal_context->expanded_key[ 97 ],
1099
155
   internal_context->expanded_key[ 98 ],
1100
155
   internal_context->expanded_key[ 99 ],
1101
155
   internal_context->expanded_key[ 92 ],
1102
155
   internal_context->expanded_key[ 93 ],
1103
155
   internal_context->expanded_key[ 94 ],
1104
155
   internal_context->expanded_key[ 95 ],
1105
155
   value0,
1106
155
   value1,
1107
155
   value4,
1108
155
   value2,
1109
155
   value3 );
1110
1111
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution5(
1112
155
   internal_context->expanded_key[ 92 ],
1113
155
   internal_context->expanded_key[ 93 ],
1114
155
   internal_context->expanded_key[ 94 ],
1115
155
   internal_context->expanded_key[ 95 ],
1116
155
   internal_context->expanded_key[ 88 ],
1117
155
   internal_context->expanded_key[ 89 ],
1118
155
   internal_context->expanded_key[ 90 ],
1119
155
   internal_context->expanded_key[ 91 ],
1120
155
   value1,
1121
155
   value4,
1122
155
   value2,
1123
155
   value3,
1124
155
   value0 );
1125
1126
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution6(
1127
155
   internal_context->expanded_key[ 88 ],
1128
155
   internal_context->expanded_key[ 89 ],
1129
155
   internal_context->expanded_key[ 90 ],
1130
155
   internal_context->expanded_key[ 91 ],
1131
155
   internal_context->expanded_key[ 84 ],
1132
155
   internal_context->expanded_key[ 85 ],
1133
155
   internal_context->expanded_key[ 86 ],
1134
155
   internal_context->expanded_key[ 87 ],
1135
155
   value0,
1136
155
   value1,
1137
155
   value4,
1138
155
   value3,
1139
155
   value2 );
1140
1141
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution7(
1142
155
   internal_context->expanded_key[ 84 ],
1143
155
   internal_context->expanded_key[ 85 ],
1144
155
   internal_context->expanded_key[ 86 ],
1145
155
   internal_context->expanded_key[ 87 ],
1146
155
   internal_context->expanded_key[ 80 ],
1147
155
   internal_context->expanded_key[ 81 ],
1148
155
   internal_context->expanded_key[ 82 ],
1149
155
   internal_context->expanded_key[ 83 ],
1150
155
   value4,
1151
155
   value2,
1152
155
   value1,
1153
155
   value3,
1154
155
   value0 );
1155
1156
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution0(
1157
155
   internal_context->expanded_key[ 80 ],
1158
155
   internal_context->expanded_key[ 81 ],
1159
155
   internal_context->expanded_key[ 82 ],
1160
155
   internal_context->expanded_key[ 83 ],
1161
155
   internal_context->expanded_key[ 76 ],
1162
155
   internal_context->expanded_key[ 77 ],
1163
155
   internal_context->expanded_key[ 78 ],
1164
155
   internal_context->expanded_key[ 79 ],
1165
155
   value0,
1166
155
   value1,
1167
155
   value3,
1168
155
   value4,
1169
155
   value2 );
1170
1171
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution1(
1172
155
   internal_context->expanded_key[ 76 ],
1173
155
   internal_context->expanded_key[ 77 ],
1174
155
   internal_context->expanded_key[ 78 ],
1175
155
   internal_context->expanded_key[ 79 ],
1176
155
   internal_context->expanded_key[ 72 ],
1177
155
   internal_context->expanded_key[ 73 ],
1178
155
   internal_context->expanded_key[ 74 ],
1179
155
   internal_context->expanded_key[ 75 ],
1180
155
   value3,
1181
155
   value1,
1182
155
   value4,
1183
155
   value0,
1184
155
   value2 );
1185
1186
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution2(
1187
155
   internal_context->expanded_key[ 72 ],
1188
155
   internal_context->expanded_key[ 73 ],
1189
155
   internal_context->expanded_key[ 74 ],
1190
155
   internal_context->expanded_key[ 75 ],
1191
155
   internal_context->expanded_key[ 68 ],
1192
155
   internal_context->expanded_key[ 69 ],
1193
155
   internal_context->expanded_key[ 70 ],
1194
155
   internal_context->expanded_key[ 71 ],
1195
155
   value2,
1196
155
   value4,
1197
155
   value0,
1198
155
   value3,
1199
155
   value1 );
1200
1201
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution3(
1202
155
   internal_context->expanded_key[ 68 ],
1203
155
   internal_context->expanded_key[ 69 ],
1204
155
   internal_context->expanded_key[ 70 ],
1205
155
   internal_context->expanded_key[ 71 ],
1206
155
   internal_context->expanded_key[ 64 ],
1207
155
   internal_context->expanded_key[ 65 ],
1208
155
   internal_context->expanded_key[ 66 ],
1209
155
   internal_context->expanded_key[ 67 ],
1210
155
   value1,
1211
155
   value4,
1212
155
   value2,
1213
155
   value3,
1214
155
   value0 );
1215
1216
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution4(
1217
155
   internal_context->expanded_key[ 64 ],
1218
155
   internal_context->expanded_key[ 65 ],
1219
155
   internal_context->expanded_key[ 66 ],
1220
155
   internal_context->expanded_key[ 67 ],
1221
155
   internal_context->expanded_key[ 60 ],
1222
155
   internal_context->expanded_key[ 61 ],
1223
155
   internal_context->expanded_key[ 62 ],
1224
155
   internal_context->expanded_key[ 63 ],
1225
155
   value3,
1226
155
   value0,
1227
155
   value4,
1228
155
   value1,
1229
155
   value2 );
1230
1231
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution5(
1232
155
   internal_context->expanded_key[ 60 ],
1233
155
   internal_context->expanded_key[ 61 ],
1234
155
   internal_context->expanded_key[ 62 ],
1235
155
   internal_context->expanded_key[ 63 ],
1236
155
   internal_context->expanded_key[ 56 ],
1237
155
   internal_context->expanded_key[ 57 ],
1238
155
   internal_context->expanded_key[ 58 ],
1239
155
   internal_context->expanded_key[ 59 ],
1240
155
   value0,
1241
155
   value4,
1242
155
   value1,
1243
155
   value2,
1244
155
   value3 );
1245
1246
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution6(
1247
155
   internal_context->expanded_key[ 56 ],
1248
155
   internal_context->expanded_key[ 57 ],
1249
155
   internal_context->expanded_key[ 58 ],
1250
155
   internal_context->expanded_key[ 59 ],
1251
155
   internal_context->expanded_key[ 52 ],
1252
155
   internal_context->expanded_key[ 53 ],
1253
155
   internal_context->expanded_key[ 54 ],
1254
155
   internal_context->expanded_key[ 55 ],
1255
155
   value3,
1256
155
   value0,
1257
155
   value4,
1258
155
   value2,
1259
155
   value1 );
1260
1261
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution7(
1262
155
   internal_context->expanded_key[ 52 ],
1263
155
   internal_context->expanded_key[ 53 ],
1264
155
   internal_context->expanded_key[ 54 ],
1265
155
   internal_context->expanded_key[ 55 ],
1266
155
   internal_context->expanded_key[ 48 ],
1267
155
   internal_context->expanded_key[ 49 ],
1268
155
   internal_context->expanded_key[ 50 ],
1269
155
   internal_context->expanded_key[ 51 ],
1270
155
   value4,
1271
155
   value1,
1272
155
   value0,
1273
155
   value2,
1274
155
   value3 );
1275
1276
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution0(
1277
155
   internal_context->expanded_key[ 48 ],
1278
155
   internal_context->expanded_key[ 49 ],
1279
155
   internal_context->expanded_key[ 50 ],
1280
155
   internal_context->expanded_key[ 51 ],
1281
155
   internal_context->expanded_key[ 44 ],
1282
155
   internal_context->expanded_key[ 45 ],
1283
155
   internal_context->expanded_key[ 46 ],
1284
155
   internal_context->expanded_key[ 47 ],
1285
155
   value3,
1286
155
   value0,
1287
155
   value2,
1288
155
   value4,
1289
155
   value1 );
1290
1291
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution1(
1292
155
   internal_context->expanded_key[ 44 ],
1293
155
   internal_context->expanded_key[ 45 ],
1294
155
   internal_context->expanded_key[ 46 ],
1295
155
   internal_context->expanded_key[ 47 ],
1296
155
   internal_context->expanded_key[ 40 ],
1297
155
   internal_context->expanded_key[ 41 ],
1298
155
   internal_context->expanded_key[ 42 ],
1299
155
   internal_context->expanded_key[ 43 ],
1300
155
   value2,
1301
155
   value0,
1302
155
   value4,
1303
155
   value3,
1304
155
   value1 );
1305
1306
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution2(
1307
155
   internal_context->expanded_key[ 40 ],
1308
155
   internal_context->expanded_key[ 41 ],
1309
155
   internal_context->expanded_key[ 42 ],
1310
155
   internal_context->expanded_key[ 43 ],
1311
155
   internal_context->expanded_key[ 36 ],
1312
155
   internal_context->expanded_key[ 37 ],
1313
155
   internal_context->expanded_key[ 38 ],
1314
155
   internal_context->expanded_key[ 39 ],
1315
155
   value1,
1316
155
   value4,
1317
155
   value3,
1318
155
   value2,
1319
155
   value0 );
1320
1321
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution3(
1322
155
   internal_context->expanded_key[ 36 ],
1323
155
   internal_context->expanded_key[ 37 ],
1324
155
   internal_context->expanded_key[ 38 ],
1325
155
   internal_context->expanded_key[ 39 ],
1326
155
   internal_context->expanded_key[ 32 ],
1327
155
   internal_context->expanded_key[ 33 ],
1328
155
   internal_context->expanded_key[ 34 ],
1329
155
   internal_context->expanded_key[ 35 ],
1330
155
   value0,
1331
155
   value4,
1332
155
   value1,
1333
155
   value2,
1334
155
   value3 );
1335
1336
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution4(
1337
155
   internal_context->expanded_key[ 32 ],
1338
155
   internal_context->expanded_key[ 33 ],
1339
155
   internal_context->expanded_key[ 34 ],
1340
155
   internal_context->expanded_key[ 35 ],
1341
155
   internal_context->expanded_key[ 28 ],
1342
155
   internal_context->expanded_key[ 29 ],
1343
155
   internal_context->expanded_key[ 30 ],
1344
155
   internal_context->expanded_key[ 31 ],
1345
155
   value2,
1346
155
   value3,
1347
155
   value4,
1348
155
   value0,
1349
155
   value1 );
1350
1351
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution5(
1352
155
   internal_context->expanded_key[ 28 ],
1353
155
   internal_context->expanded_key[ 29 ],
1354
155
   internal_context->expanded_key[ 30 ],
1355
155
   internal_context->expanded_key[ 31 ],
1356
155
   internal_context->expanded_key[ 24 ],
1357
155
   internal_context->expanded_key[ 25 ],
1358
155
   internal_context->expanded_key[ 26 ],
1359
155
   internal_context->expanded_key[ 27 ],
1360
155
   value3,
1361
155
   value4,
1362
155
   value0,
1363
155
   value1,
1364
155
   value2 );
1365
1366
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution6(
1367
155
   internal_context->expanded_key[ 24 ],
1368
155
   internal_context->expanded_key[ 25 ],
1369
155
   internal_context->expanded_key[ 26 ],
1370
155
   internal_context->expanded_key[ 27 ],
1371
155
   internal_context->expanded_key[ 20 ],
1372
155
   internal_context->expanded_key[ 21 ],
1373
155
   internal_context->expanded_key[ 22 ],
1374
155
   internal_context->expanded_key[ 23 ],
1375
155
   value2,
1376
155
   value3,
1377
155
   value4,
1378
155
   value1,
1379
155
   value0 );
1380
1381
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution7(
1382
155
   internal_context->expanded_key[ 20 ],
1383
155
   internal_context->expanded_key[ 21 ],
1384
155
   internal_context->expanded_key[ 22 ],
1385
155
   internal_context->expanded_key[ 23 ],
1386
155
   internal_context->expanded_key[ 16 ],
1387
155
   internal_context->expanded_key[ 17 ],
1388
155
   internal_context->expanded_key[ 18 ],
1389
155
   internal_context->expanded_key[ 19 ],
1390
155
   value4,
1391
155
   value0,
1392
155
   value3,
1393
155
   value1,
1394
155
   value2 );
1395
1396
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution0(
1397
155
   internal_context->expanded_key[ 16 ],
1398
155
   internal_context->expanded_key[ 17 ],
1399
155
   internal_context->expanded_key[ 18 ],
1400
155
   internal_context->expanded_key[ 19 ],
1401
155
   internal_context->expanded_key[ 12 ],
1402
155
   internal_context->expanded_key[ 13 ],
1403
155
   internal_context->expanded_key[ 14 ],
1404
155
   internal_context->expanded_key[ 15 ],
1405
155
   value2,
1406
155
   value3,
1407
155
   value1,
1408
155
   value4,
1409
155
   value0 );
1410
1411
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution1(
1412
155
   internal_context->expanded_key[ 12 ],
1413
155
   internal_context->expanded_key[ 13 ],
1414
155
   internal_context->expanded_key[ 14 ],
1415
155
   internal_context->expanded_key[ 15 ],
1416
155
   internal_context->expanded_key[ 8 ],
1417
155
   internal_context->expanded_key[ 9 ],
1418
155
   internal_context->expanded_key[ 10 ],
1419
155
   internal_context->expanded_key[ 11 ],
1420
155
   value1,
1421
155
   value3,
1422
155
   value4,
1423
155
   value2,
1424
155
   value0 );
1425
1426
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution2(
1427
155
   internal_context->expanded_key[ 8 ],
1428
155
   internal_context->expanded_key[ 9 ],
1429
155
   internal_context->expanded_key[ 10 ],
1430
155
   internal_context->expanded_key[ 11 ],
1431
155
   internal_context->expanded_key[ 4 ],
1432
155
   internal_context->expanded_key[ 5 ],
1433
155
   internal_context->expanded_key[ 6 ],
1434
155
   internal_context->expanded_key[ 7 ],
1435
155
   value0,
1436
155
   value4,
1437
155
   value2,
1438
155
   value1,
1439
155
   value3 );
1440
1441
155
  libfcrypto_serpent_calculate_expanded_key_with_forward_substitution3(
1442
155
   internal_context->expanded_key[ 4 ],
1443
155
   internal_context->expanded_key[ 5 ],
1444
155
   internal_context->expanded_key[ 6 ],
1445
155
   internal_context->expanded_key[ 7 ],
1446
155
   internal_context->expanded_key[ 0 ],
1447
155
   internal_context->expanded_key[ 1 ],
1448
155
   internal_context->expanded_key[ 2 ],
1449
155
   internal_context->expanded_key[ 3 ],
1450
155
   value3,
1451
155
   value4,
1452
155
   value0,
1453
155
   value1,
1454
155
   value2 );
1455
1456
155
  internal_context->expanded_key[ 0 ] = value1;
1457
155
  internal_context->expanded_key[ 1 ] = value2;
1458
155
  internal_context->expanded_key[ 2 ] = value4;
1459
155
  internal_context->expanded_key[ 3 ] = value3;
1460
1461
155
  return( 1 );
1462
1463
0
on_error:
1464
0
  memory_set(
1465
0
   key_data,
1466
0
   0,
1467
0
   32 );
1468
1469
0
  memory_set(
1470
0
   internal_context->expanded_key,
1471
0
   0,
1472
0
   sizeof( uint32_t ) * LIBFCRYPTO_SERPENT_NUMBER_OF_EXPANDED_KEY_ELEMENTS );
1473
1474
0
  return( -1 );
1475
155
}
1476
1477
/* Encrypts a block of data using Serpent
1478
 * The size must be a multitude of the Serpent block size (16 byte)
1479
 * Returns 1 if successful or -1 on error
1480
 */
1481
int libfcrypto_internal_serpent_context_encrypt_block(
1482
     libfcrypto_internal_serpent_context_t *internal_context,
1483
     const uint8_t *input_data,
1484
     size_t input_data_size,
1485
     uint8_t *output_data,
1486
     size_t output_data_size,
1487
     libcerror_error_t **error )
1488
20
{
1489
20
  static char *function = "libfcrypto_internal_serpent_context_encrypt_block";
1490
20
  uint32_t value0       = 0;
1491
20
  uint32_t value1       = 0;
1492
20
  uint32_t value2       = 0;
1493
20
  uint32_t value3       = 0;
1494
20
  uint32_t value4       = 0;
1495
1496
20
  if( internal_context == NULL )
1497
0
  {
1498
0
    libcerror_error_set(
1499
0
     error,
1500
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1501
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1502
0
     "%s: invalid context.",
1503
0
     function );
1504
1505
0
    return( -1 );
1506
0
  }
1507
20
  if( input_data == NULL )
1508
0
  {
1509
0
    libcerror_error_set(
1510
0
     error,
1511
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1512
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1513
0
     "%s: invalid input data.",
1514
0
     function );
1515
1516
0
    return( -1 );
1517
0
  }
1518
20
  if( ( input_data_size < 16 )
1519
20
   || ( input_data_size > (size_t) SSIZE_MAX ) )
1520
0
  {
1521
0
    libcerror_error_set(
1522
0
     error,
1523
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1524
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1525
0
     "%s: invalid input data size value out of bounds.",
1526
0
     function );
1527
1528
0
    return( -1 );
1529
0
  }
1530
20
  if( output_data == NULL )
1531
0
  {
1532
0
    libcerror_error_set(
1533
0
     error,
1534
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1535
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1536
0
     "%s: invalid output data.",
1537
0
     function );
1538
1539
0
    return( -1 );
1540
0
  }
1541
20
  if( ( output_data_size < 16 )
1542
20
   || ( output_data_size > (size_t) SSIZE_MAX ) )
1543
0
  {
1544
0
    libcerror_error_set(
1545
0
     error,
1546
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1547
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1548
0
     "%s: invalid output data size value out of bounds.",
1549
0
     function );
1550
1551
0
    return( -1 );
1552
0
  }
1553
20
  byte_stream_copy_to_uint32_little_endian(
1554
20
   &( input_data[ 0 ] ),
1555
20
   value0 );
1556
1557
20
  byte_stream_copy_to_uint32_little_endian(
1558
20
   &( input_data[ 4 ] ),
1559
20
   value1 );
1560
1561
20
  byte_stream_copy_to_uint32_little_endian(
1562
20
   &( input_data[ 8 ] ),
1563
20
   value2 );
1564
1565
20
  byte_stream_copy_to_uint32_little_endian(
1566
20
   &( input_data[ 12 ] ),
1567
20
   value3 );
1568
1569
20
  value0 ^= internal_context->expanded_key[ 0 ];
1570
20
  value1 ^= internal_context->expanded_key[ 1 ];
1571
20
  value2 ^= internal_context->expanded_key[ 2 ];
1572
20
  value3 ^= internal_context->expanded_key[ 3 ];
1573
1574
20
  libfcrypto_serpent_calculate_forward_substitution0(
1575
20
   value0,
1576
20
   value1,
1577
20
   value2,
1578
20
   value3,
1579
20
   value4 );
1580
1581
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution1(
1582
20
   internal_context->expanded_key,
1583
20
   4,
1584
20
   value2,
1585
20
   value1,
1586
20
   value3,
1587
20
   value0,
1588
20
   value4 );
1589
1590
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution2(
1591
20
   internal_context->expanded_key,
1592
20
   8,
1593
20
   value4,
1594
20
   value3,
1595
20
   value0,
1596
20
   value2,
1597
20
   value1 );
1598
1599
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution3(
1600
20
   internal_context->expanded_key,
1601
20
   12,
1602
20
   value1,
1603
20
   value3,
1604
20
   value4,
1605
20
   value2,
1606
20
   value0 );
1607
1608
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution4(
1609
20
   internal_context->expanded_key,
1610
20
   16,
1611
20
   value2,
1612
20
   value0,
1613
20
   value3,
1614
20
   value1,
1615
20
   value4 );
1616
1617
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution5(
1618
20
   internal_context->expanded_key,
1619
20
   20,
1620
20
   value0,
1621
20
   value3,
1622
20
   value1,
1623
20
   value4,
1624
20
   value2 );
1625
1626
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution6(
1627
20
   internal_context->expanded_key,
1628
20
   24,
1629
20
   value2,
1630
20
   value0,
1631
20
   value3,
1632
20
   value4,
1633
20
   value1 );
1634
1635
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution7(
1636
20
   internal_context->expanded_key,
1637
20
   28,
1638
20
   value3,
1639
20
   value1,
1640
20
   value0,
1641
20
   value4,
1642
20
   value2 );
1643
1644
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution0(
1645
20
   internal_context->expanded_key,
1646
20
   32,
1647
20
   value2,
1648
20
   value0,
1649
20
   value4,
1650
20
   value3,
1651
20
   value1 );
1652
1653
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution1(
1654
20
   internal_context->expanded_key,
1655
20
   36,
1656
20
   value4,
1657
20
   value0,
1658
20
   value3,
1659
20
   value2,
1660
20
   value1 );
1661
1662
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution2(
1663
20
   internal_context->expanded_key,
1664
20
   40,
1665
20
   value1,
1666
20
   value3,
1667
20
   value2,
1668
20
   value4,
1669
20
   value0 );
1670
1671
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution3(
1672
20
   internal_context->expanded_key,
1673
20
   44,
1674
20
   value0,
1675
20
   value3,
1676
20
   value1,
1677
20
   value4,
1678
20
   value2 );
1679
1680
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution4(
1681
20
   internal_context->expanded_key,
1682
20
   48,
1683
20
   value4,
1684
20
   value2,
1685
20
   value3,
1686
20
   value0,
1687
20
   value1 );
1688
1689
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution5(
1690
20
   internal_context->expanded_key,
1691
20
   52,
1692
20
   value2,
1693
20
   value3,
1694
20
   value0,
1695
20
   value1,
1696
20
   value4 );
1697
1698
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution6(
1699
20
   internal_context->expanded_key,
1700
20
   56,
1701
20
   value4,
1702
20
   value2,
1703
20
   value3,
1704
20
   value1,
1705
20
   value0 );
1706
1707
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution7(
1708
20
   internal_context->expanded_key,
1709
20
   60,
1710
20
   value3,
1711
20
   value0,
1712
20
   value2,
1713
20
   value1,
1714
20
   value4 );
1715
1716
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution0(
1717
20
   internal_context->expanded_key,
1718
20
   64,
1719
20
   value4,
1720
20
   value2,
1721
20
   value1,
1722
20
   value3,
1723
20
   value0 );
1724
1725
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution1(
1726
20
   internal_context->expanded_key,
1727
20
   68,
1728
20
   value1,
1729
20
   value2,
1730
20
   value3,
1731
20
   value4,
1732
20
   value0 );
1733
1734
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution2(
1735
20
   internal_context->expanded_key,
1736
20
   72,
1737
20
   value0,
1738
20
   value3,
1739
20
   value4,
1740
20
   value1,
1741
20
   value2 );
1742
1743
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution3(
1744
20
   internal_context->expanded_key,
1745
20
   76,
1746
20
   value2,
1747
20
   value3,
1748
20
   value0,
1749
20
   value1,
1750
20
   value4 );
1751
1752
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution4(
1753
20
   internal_context->expanded_key,
1754
20
   80,
1755
20
   value1,
1756
20
   value4,
1757
20
   value3,
1758
20
   value2,
1759
20
   value0 );
1760
1761
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution5(
1762
20
   internal_context->expanded_key,
1763
20
   84,
1764
20
   value4,
1765
20
   value3,
1766
20
   value2,
1767
20
   value0,
1768
20
   value1 );
1769
1770
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution6(
1771
20
   internal_context->expanded_key,
1772
20
   88,
1773
20
   value1,
1774
20
   value4,
1775
20
   value3,
1776
20
   value0,
1777
20
   value2 );
1778
1779
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution7(
1780
20
   internal_context->expanded_key,
1781
20
   92,
1782
20
   value3,
1783
20
   value2,
1784
20
   value4,
1785
20
   value0,
1786
20
   value1 );
1787
1788
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution0(
1789
20
   internal_context->expanded_key,
1790
20
   96,
1791
20
   value1,
1792
20
   value4,
1793
20
   value0,
1794
20
   value3,
1795
20
   value2 );
1796
1797
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution1(
1798
20
   internal_context->expanded_key,
1799
20
   100,
1800
20
   value0,
1801
20
   value4,
1802
20
   value3,
1803
20
   value1,
1804
20
   value2 );
1805
1806
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution2(
1807
20
   internal_context->expanded_key,
1808
20
   104,
1809
20
   value2,
1810
20
   value3,
1811
20
   value1,
1812
20
   value0,
1813
20
   value4 );
1814
1815
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution3(
1816
20
   internal_context->expanded_key,
1817
20
   108,
1818
20
   value4,
1819
20
   value3,
1820
20
   value2,
1821
20
   value0,
1822
20
   value1 );
1823
1824
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution4(
1825
20
   internal_context->expanded_key,
1826
20
   112,
1827
20
   value0,
1828
20
   value1,
1829
20
   value3,
1830
20
   value4,
1831
20
   value2 );
1832
1833
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution5(
1834
20
   internal_context->expanded_key,
1835
20
   116,
1836
20
   value1,
1837
20
   value3,
1838
20
   value4,
1839
20
   value2,
1840
20
   value0 );
1841
1842
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution6(
1843
20
   internal_context->expanded_key,
1844
20
   120,
1845
20
   value0,
1846
20
   value1,
1847
20
   value3,
1848
20
   value2,
1849
20
   value4 );
1850
1851
20
  libfcrypto_serpent_calculate_forward_linear_transformation_and_substitution7(
1852
20
   internal_context->expanded_key,
1853
20
   124,
1854
20
   value3,
1855
20
   value4,
1856
20
   value1,
1857
20
   value2,
1858
20
   value0 );
1859
1860
20
  value0 ^= internal_context->expanded_key[ 128 ];
1861
20
  value1 ^= internal_context->expanded_key[ 129 ];
1862
20
  value2 ^= internal_context->expanded_key[ 130 ];
1863
20
  value3 ^= internal_context->expanded_key[ 131 ];
1864
1865
20
  byte_stream_copy_from_uint32_little_endian(
1866
20
   &( output_data[ 0 ] ),
1867
20
   value0 );
1868
1869
20
  byte_stream_copy_from_uint32_little_endian(
1870
20
   &( output_data[ 4 ] ),
1871
20
   value1 );
1872
1873
20
  byte_stream_copy_from_uint32_little_endian(
1874
20
   &( output_data[ 8 ] ),
1875
20
   value2 );
1876
1877
20
  byte_stream_copy_from_uint32_little_endian(
1878
20
   &( output_data[ 12 ] ),
1879
20
   value3 );
1880
1881
20
  return( 1 );
1882
20
}
1883
1884
/* Decrypts a block of data using Serpent
1885
 * The size must be a multitude of the Serpent block size (16 byte)
1886
 * Returns 1 if successful or -1 on error
1887
 */
1888
int libfcrypto_internal_serpent_context_decrypt_block(
1889
     libfcrypto_internal_serpent_context_t *internal_context,
1890
     const uint8_t *input_data,
1891
     size_t input_data_size,
1892
     uint8_t *output_data,
1893
     size_t output_data_size,
1894
     libcerror_error_t **error  )
1895
38.5k
{
1896
38.5k
  static char *function = "libfcrypto_internal_serpent_context_decrypt_block";
1897
38.5k
  uint32_t value0       = 0;
1898
38.5k
  uint32_t value1       = 0;
1899
38.5k
  uint32_t value2       = 0;
1900
38.5k
  uint32_t value3       = 0;
1901
38.5k
  uint32_t value4       = 0;
1902
1903
38.5k
  if( internal_context == NULL )
1904
0
  {
1905
0
    libcerror_error_set(
1906
0
     error,
1907
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1908
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1909
0
     "%s: invalid context.",
1910
0
     function );
1911
1912
0
    return( -1 );
1913
0
  }
1914
38.5k
  if( input_data == NULL )
1915
0
  {
1916
0
    libcerror_error_set(
1917
0
     error,
1918
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1919
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1920
0
     "%s: invalid input data.",
1921
0
     function );
1922
1923
0
    return( -1 );
1924
0
  }
1925
38.5k
  if( ( input_data_size < 16 )
1926
38.5k
   || ( input_data_size > (size_t) SSIZE_MAX ) )
1927
0
  {
1928
0
    libcerror_error_set(
1929
0
     error,
1930
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1931
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1932
0
     "%s: invalid input data size value out of bounds.",
1933
0
     function );
1934
1935
0
    return( -1 );
1936
0
  }
1937
38.5k
  if( output_data == NULL )
1938
0
  {
1939
0
    libcerror_error_set(
1940
0
     error,
1941
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1942
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1943
0
     "%s: invalid output data.",
1944
0
     function );
1945
1946
0
    return( -1 );
1947
0
  }
1948
38.5k
  if( ( output_data_size < 16 )
1949
38.5k
   || ( output_data_size > (size_t) SSIZE_MAX ) )
1950
0
  {
1951
0
    libcerror_error_set(
1952
0
     error,
1953
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1954
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1955
0
     "%s: invalid output data size value out of bounds.",
1956
0
     function );
1957
1958
0
    return( -1 );
1959
0
  }
1960
38.5k
  byte_stream_copy_to_uint32_little_endian(
1961
38.5k
   &( input_data[ 0 ] ),
1962
38.5k
   value0 );
1963
1964
38.5k
  byte_stream_copy_to_uint32_little_endian(
1965
38.5k
   &( input_data[ 4 ] ),
1966
38.5k
   value1 );
1967
1968
38.5k
  byte_stream_copy_to_uint32_little_endian(
1969
38.5k
   &( input_data[ 8 ] ),
1970
38.5k
   value2 );
1971
1972
38.5k
  byte_stream_copy_to_uint32_little_endian(
1973
38.5k
   &( input_data[ 12 ] ),
1974
38.5k
   value3 );
1975
1976
38.5k
  value0 ^= internal_context->expanded_key[ 128 ];
1977
38.5k
  value1 ^= internal_context->expanded_key[ 129 ];
1978
38.5k
  value2 ^= internal_context->expanded_key[ 130 ];
1979
38.5k
  value3 ^= internal_context->expanded_key[ 131 ];
1980
1981
38.5k
  libfcrypto_serpent_calculate_reverse_substitution7(
1982
38.5k
   value0,
1983
38.5k
   value1,
1984
38.5k
   value2,
1985
38.5k
   value3,
1986
38.5k
   value4 );
1987
1988
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution6(
1989
38.5k
   internal_context->expanded_key,
1990
38.5k
   4 * 31,
1991
38.5k
   value1,
1992
38.5k
   value3,
1993
38.5k
   value0,
1994
38.5k
   value4,
1995
38.5k
   value2 );
1996
1997
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution5(
1998
38.5k
   internal_context->expanded_key,
1999
38.5k
   4 * 30,
2000
38.5k
   value0,
2001
38.5k
   value2,
2002
38.5k
   value4,
2003
38.5k
   value1,
2004
38.5k
   value3 );
2005
2006
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution4(
2007
38.5k
   internal_context->expanded_key,
2008
38.5k
   4 * 29,
2009
38.5k
   value2,
2010
38.5k
   value3,
2011
38.5k
   value0,
2012
38.5k
   value4,
2013
38.5k
   value1 );
2014
2015
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution3(
2016
38.5k
   internal_context->expanded_key,
2017
38.5k
   4 * 28,
2018
38.5k
   value2,
2019
38.5k
   value0,
2020
38.5k
   value1,
2021
38.5k
   value4,
2022
38.5k
   value3 );
2023
2024
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution2(
2025
38.5k
   internal_context->expanded_key,
2026
38.5k
   4 * 27,
2027
38.5k
   value1,
2028
38.5k
   value2,
2029
38.5k
   value3,
2030
38.5k
   value4,
2031
38.5k
   value0 );
2032
2033
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution1(
2034
38.5k
   internal_context->expanded_key,
2035
38.5k
   4 * 26,
2036
38.5k
   value2,
2037
38.5k
   value0,
2038
38.5k
   value4,
2039
38.5k
   value3,
2040
38.5k
   value1 );
2041
2042
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution0(
2043
38.5k
   internal_context->expanded_key,
2044
38.5k
   4 * 25,
2045
38.5k
   value1,
2046
38.5k
   value0,
2047
38.5k
   value4,
2048
38.5k
   value3,
2049
38.5k
   value2 );
2050
2051
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution7(
2052
38.5k
   internal_context->expanded_key,
2053
38.5k
   4 * 24,
2054
38.5k
   value4,
2055
38.5k
   value2,
2056
38.5k
   value0,
2057
38.5k
   value1,
2058
38.5k
   value3 );
2059
2060
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution6(
2061
38.5k
   internal_context->expanded_key,
2062
38.5k
   4 * 23,
2063
38.5k
   value2,
2064
38.5k
   value1,
2065
38.5k
   value4,
2066
38.5k
   value3,
2067
38.5k
   value0 );
2068
2069
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution5(
2070
38.5k
   internal_context->expanded_key,
2071
38.5k
   4 * 22,
2072
38.5k
   value4,
2073
38.5k
   value0,
2074
38.5k
   value3,
2075
38.5k
   value2,
2076
38.5k
   value1 );
2077
2078
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution4(
2079
38.5k
   internal_context->expanded_key,
2080
38.5k
   4 * 21,
2081
38.5k
   value0,
2082
38.5k
   value1,
2083
38.5k
   value4,
2084
38.5k
   value3,
2085
38.5k
   value2 );
2086
2087
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution3(
2088
38.5k
   internal_context->expanded_key,
2089
38.5k
   4 * 20,
2090
38.5k
   value0,
2091
38.5k
   value4,
2092
38.5k
   value2,
2093
38.5k
   value3,
2094
38.5k
   value1 );
2095
2096
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution2(
2097
38.5k
   internal_context->expanded_key,
2098
38.5k
   4 * 19,
2099
38.5k
   value2,
2100
38.5k
   value0,
2101
38.5k
   value1,
2102
38.5k
   value3,
2103
38.5k
   value4 );
2104
2105
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution1(
2106
38.5k
   internal_context->expanded_key,
2107
38.5k
   4 * 18,
2108
38.5k
   value0,
2109
38.5k
   value4,
2110
38.5k
   value3,
2111
38.5k
   value1,
2112
38.5k
   value2 );
2113
2114
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution0(
2115
38.5k
   internal_context->expanded_key,
2116
38.5k
   4 * 17,
2117
38.5k
   value2,
2118
38.5k
   value4,
2119
38.5k
   value3,
2120
38.5k
   value1,
2121
38.5k
   value0 );
2122
2123
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution7(
2124
38.5k
   internal_context->expanded_key,
2125
38.5k
   4 * 16,
2126
38.5k
   value3,
2127
38.5k
   value0,
2128
38.5k
   value4,
2129
38.5k
   value2,
2130
38.5k
   value1 );
2131
2132
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution6(
2133
38.5k
   internal_context->expanded_key,
2134
38.5k
   4 * 15,
2135
38.5k
   value0,
2136
38.5k
   value2,
2137
38.5k
   value3,
2138
38.5k
   value1,
2139
38.5k
   value4 );
2140
2141
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution5(
2142
38.5k
   internal_context->expanded_key,
2143
38.5k
   4 * 14,
2144
38.5k
   value3,
2145
38.5k
   value4,
2146
38.5k
   value1,
2147
38.5k
   value0,
2148
38.5k
   value2 );
2149
2150
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution4(
2151
38.5k
   internal_context->expanded_key,
2152
38.5k
   4 * 13,
2153
38.5k
   value4,
2154
38.5k
   value2,
2155
38.5k
   value3,
2156
38.5k
   value1,
2157
38.5k
   value0 );
2158
2159
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution3(
2160
38.5k
   internal_context->expanded_key,
2161
38.5k
   4 * 12,
2162
38.5k
   value4,
2163
38.5k
   value3,
2164
38.5k
   value0,
2165
38.5k
   value1,
2166
38.5k
   value2 );
2167
2168
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution2(
2169
38.5k
   internal_context->expanded_key,
2170
38.5k
   4 * 11,
2171
38.5k
   value0,
2172
38.5k
   value4,
2173
38.5k
   value2,
2174
38.5k
   value1,
2175
38.5k
   value3 );
2176
2177
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution1(
2178
38.5k
   internal_context->expanded_key,
2179
38.5k
   4 * 10,
2180
38.5k
   value4,
2181
38.5k
   value3,
2182
38.5k
   value1,
2183
38.5k
   value2,
2184
38.5k
   value0 );
2185
2186
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution0(
2187
38.5k
   internal_context->expanded_key,
2188
38.5k
   4 * 9,
2189
38.5k
   value0,
2190
38.5k
   value3,
2191
38.5k
   value1,
2192
38.5k
   value2,
2193
38.5k
   value4 );
2194
2195
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution7(
2196
38.5k
   internal_context->expanded_key,
2197
38.5k
   4 * 8,
2198
38.5k
   value1,
2199
38.5k
   value4,
2200
38.5k
   value3,
2201
38.5k
   value0,
2202
38.5k
   value2 );
2203
2204
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution6(
2205
38.5k
   internal_context->expanded_key,
2206
38.5k
   4 * 7,
2207
38.5k
   value4,
2208
38.5k
   value0,
2209
38.5k
   value1,
2210
38.5k
   value2,
2211
38.5k
   value3 );
2212
2213
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution5(
2214
38.5k
   internal_context->expanded_key,
2215
38.5k
   4 * 6,
2216
38.5k
   value1,
2217
38.5k
   value3,
2218
38.5k
   value2,
2219
38.5k
   value4,
2220
38.5k
   value0 );
2221
2222
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution4(
2223
38.5k
   internal_context->expanded_key,
2224
38.5k
   4 * 5,
2225
38.5k
   value3,
2226
38.5k
   value0,
2227
38.5k
   value1,
2228
38.5k
   value2,
2229
38.5k
   value4 );
2230
2231
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution3(
2232
38.5k
   internal_context->expanded_key,
2233
38.5k
   4 * 4,
2234
38.5k
   value3,
2235
38.5k
   value1,
2236
38.5k
   value4,
2237
38.5k
   value2,
2238
38.5k
   value0 );
2239
2240
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution2(
2241
38.5k
   internal_context->expanded_key,
2242
38.5k
   4 * 3,
2243
38.5k
   value4,
2244
38.5k
   value3,
2245
38.5k
   value0,
2246
38.5k
   value2,
2247
38.5k
   value1 );
2248
2249
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution1(
2250
38.5k
   internal_context->expanded_key,
2251
38.5k
   4 * 2,
2252
38.5k
   value3,
2253
38.5k
   value1,
2254
38.5k
   value2,
2255
38.5k
   value0,
2256
38.5k
   value4 );
2257
2258
38.5k
  libfcrypto_serpent_calculate_reverse_linear_transformation_and_substitution0(
2259
38.5k
   internal_context->expanded_key,
2260
38.5k
   4 * 1,
2261
38.5k
   value4,
2262
38.5k
   value1,
2263
38.5k
   value2,
2264
38.5k
   value0,
2265
38.5k
   value3 );
2266
2267
38.5k
  value2 ^= internal_context->expanded_key[ 0 ];
2268
38.5k
  value3 ^= internal_context->expanded_key[ 1 ];
2269
38.5k
  value1 ^= internal_context->expanded_key[ 2 ];
2270
38.5k
  value4 ^= internal_context->expanded_key[ 3 ];
2271
2272
38.5k
  byte_stream_copy_from_uint32_little_endian(
2273
38.5k
   &( output_data[ 0 ] ),
2274
38.5k
   value2 );
2275
2276
38.5k
  byte_stream_copy_from_uint32_little_endian(
2277
38.5k
   &( output_data[ 4 ] ),
2278
38.5k
   value3 );
2279
2280
38.5k
  byte_stream_copy_from_uint32_little_endian(
2281
38.5k
   &( output_data[ 8 ] ),
2282
38.5k
   value1 );
2283
2284
38.5k
  byte_stream_copy_from_uint32_little_endian(
2285
38.5k
   &( output_data[ 12 ] ),
2286
38.5k
   value4 );
2287
2288
38.5k
  return( 1 );
2289
38.5k
}
2290
2291
/* De- or encrypts a block of data using Serpent-CBC (Cipher Block Chaining)
2292
 * The size must be a multitude of the Serpent block size (16 byte)
2293
 * Returns 1 if successful or -1 on error
2294
 */
2295
int libfcrypto_serpent_crypt_cbc(
2296
     libfcrypto_serpent_context_t *context,
2297
     int mode,
2298
     const uint8_t *initialization_vector,
2299
     size_t initialization_vector_size,
2300
     const uint8_t *input_data,
2301
     size_t input_data_size,
2302
     uint8_t *output_data,
2303
     size_t output_data_size,
2304
     libcerror_error_t **error )
2305
1.25k
{
2306
1.25k
  uint8_t block_data[ 16 ];
2307
1.25k
  uint8_t internal_initialization_vector[ 16 ];
2308
2309
1.25k
  static char *function = "libfcrypto_serpent_context_crypt_cbc";
2310
1.25k
  size_t data_offset    = 0;
2311
2312
1.25k
#if !defined( LIBFCRYPTO_UNFOLLED_LOOPS )
2313
1.25k
  uint8_t block_index   = 0;
2314
1.25k
#endif
2315
2316
1.25k
  if( context == NULL )
2317
0
  {
2318
0
    libcerror_error_set(
2319
0
     error,
2320
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2321
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2322
0
     "%s: invalid context.",
2323
0
     function );
2324
2325
0
    return( -1 );
2326
0
  }
2327
1.25k
  if( initialization_vector == NULL )
2328
0
  {
2329
0
    libcerror_error_set(
2330
0
     error,
2331
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2332
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2333
0
     "%s: invalid initialization vector.",
2334
0
     function );
2335
2336
0
    return( -1 );
2337
0
  }
2338
1.25k
  if( initialization_vector_size != 16 )
2339
0
  {
2340
0
    libcerror_error_set(
2341
0
     error,
2342
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2343
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2344
0
     "%s: invalid initialization vector size value out of bounds.",
2345
0
     function );
2346
2347
0
    return( -1 );
2348
0
  }
2349
1.25k
  if( ( mode != LIBFCRYPTO_SERPENT_CRYPT_MODE_ENCRYPT )
2350
1.20k
   && ( mode != LIBFCRYPTO_SERPENT_CRYPT_MODE_DECRYPT ) )
2351
0
  {
2352
0
    libcerror_error_set(
2353
0
     error,
2354
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2355
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2356
0
     "%s: unsupported mode.",
2357
0
     function );
2358
2359
0
    return( -1 );
2360
0
  }
2361
1.25k
  if( input_data == NULL )
2362
0
  {
2363
0
    libcerror_error_set(
2364
0
     error,
2365
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2366
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2367
0
     "%s: invalid input data.",
2368
0
     function );
2369
2370
0
    return( -1 );
2371
0
  }
2372
  /* Check if the input data size is a multitude of 16-byte
2373
   */
2374
1.25k
  if( ( ( input_data_size & (size_t) 0x0f ) != 0 )
2375
1.25k
   || ( input_data_size < 16 )
2376
1.25k
   || ( input_data_size > (size_t) SSIZE_MAX ) )
2377
4
  {
2378
4
    libcerror_error_set(
2379
4
     error,
2380
4
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2381
4
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2382
4
     "%s: invalid input data size value out of bounds.",
2383
4
     function );
2384
2385
4
    return( -1 );
2386
4
  }
2387
1.25k
  if( output_data == NULL )
2388
0
  {
2389
0
    libcerror_error_set(
2390
0
     error,
2391
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2392
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2393
0
     "%s: invalid output data.",
2394
0
     function );
2395
2396
0
    return( -1 );
2397
0
  }
2398
1.25k
  if( ( output_data_size < input_data_size )
2399
1.20k
   || ( output_data_size > (size_t) SSIZE_MAX ) )
2400
46
  {
2401
46
    libcerror_error_set(
2402
46
     error,
2403
46
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2404
46
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2405
46
     "%s: invalid output data size value out of bounds.",
2406
46
     function );
2407
2408
46
    return( -1 );
2409
46
  }
2410
1.20k
  if( memory_copy(
2411
1.20k
       internal_initialization_vector,
2412
1.20k
       initialization_vector,
2413
1.20k
       16 ) == NULL )
2414
0
  {
2415
0
    libcerror_error_set(
2416
0
     error,
2417
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2418
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
2419
0
     "%s: unable to copy initialization vector.",
2420
0
     function );
2421
2422
0
    goto on_error;
2423
0
  }
2424
39.7k
  while( data_offset < input_data_size )
2425
38.5k
  {
2426
38.5k
    if( mode == LIBFCRYPTO_SERPENT_CRYPT_MODE_ENCRYPT )
2427
10
    {
2428
#if defined( LIBFCRYPTO_UNFOLLED_LOOPS )
2429
      block_data[ 0 ]  = input_data[ data_offset++ ] ^ internal_initialization_vector[ 0 ];
2430
      block_data[ 1 ]  = input_data[ data_offset++ ] ^ internal_initialization_vector[ 1 ];
2431
      block_data[ 2 ]  = input_data[ data_offset++ ] ^ internal_initialization_vector[ 2 ];
2432
      block_data[ 3 ]  = input_data[ data_offset++ ] ^ internal_initialization_vector[ 3 ];
2433
      block_data[ 4 ]  = input_data[ data_offset++ ] ^ internal_initialization_vector[ 4 ];
2434
      block_data[ 5 ]  = input_data[ data_offset++ ] ^ internal_initialization_vector[ 5 ];
2435
      block_data[ 6 ]  = input_data[ data_offset++ ] ^ internal_initialization_vector[ 6 ];
2436
      block_data[ 7 ]  = input_data[ data_offset++ ] ^ internal_initialization_vector[ 7 ];
2437
      block_data[ 8 ]  = input_data[ data_offset++ ] ^ internal_initialization_vector[ 8 ];
2438
      block_data[ 9 ]  = input_data[ data_offset++ ] ^ internal_initialization_vector[ 9 ];
2439
      block_data[ 10 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 10 ];
2440
      block_data[ 11 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 11 ];
2441
      block_data[ 12 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 12 ];
2442
      block_data[ 13 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 13 ];
2443
      block_data[ 14 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 14 ];
2444
      block_data[ 15 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 15 ];
2445
#else
2446
10
      for( block_index = 0;
2447
170
           block_index < 16;
2448
160
           block_index++ )
2449
160
      {
2450
160
        block_data[ block_index ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ block_index ];
2451
160
      }
2452
10
#endif
2453
10
      data_offset -= 16;
2454
2455
10
      if( libfcrypto_internal_serpent_context_encrypt_block(
2456
10
           (libfcrypto_internal_serpent_context_t *) context,
2457
10
           block_data,
2458
10
           16,
2459
10
           &( output_data[ data_offset ] ),
2460
10
           16,
2461
10
           error ) != 1 )
2462
0
      {
2463
0
        libcerror_error_set(
2464
0
         error,
2465
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
2466
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
2467
0
         "%s: unable to encrypt input data.",
2468
0
         function );
2469
2470
0
        goto on_error;
2471
0
      }
2472
10
      if( memory_copy(
2473
10
           internal_initialization_vector,
2474
10
           &( output_data[ data_offset ] ),
2475
10
           16 ) == NULL )
2476
0
      {
2477
0
        libcerror_error_set(
2478
0
         error,
2479
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
2480
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
2481
0
         "%s: unable to copy encrypted output data to initialization vector.",
2482
0
         function );
2483
2484
0
        goto on_error;
2485
0
      }
2486
10
    }
2487
38.5k
    else
2488
38.5k
    {
2489
38.5k
      if( libfcrypto_internal_serpent_context_decrypt_block(
2490
38.5k
           (libfcrypto_internal_serpent_context_t *) context,
2491
38.5k
           &( input_data[ data_offset ] ),
2492
38.5k
           16,
2493
38.5k
           &( output_data[ data_offset ] ),
2494
38.5k
           16,
2495
38.5k
           error ) != 1 )
2496
0
      {
2497
0
        libcerror_error_set(
2498
0
         error,
2499
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
2500
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
2501
0
         "%s: unable to decrypt input data.",
2502
0
         function );
2503
2504
0
        goto on_error;
2505
0
      }
2506
#if defined( LIBFCRYPTO_UNFOLLED_LOOPS )
2507
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 0 ];
2508
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 1 ];
2509
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 2 ];
2510
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 3 ];
2511
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 4 ];
2512
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 5 ];
2513
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 6 ];
2514
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 7 ];
2515
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 8 ];
2516
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 9 ];
2517
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 10 ];
2518
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 11 ];
2519
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 12 ];
2520
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 13 ];
2521
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 14 ];
2522
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 15 ];
2523
#else
2524
38.5k
      for( block_index = 0;
2525
654k
           block_index < 16;
2526
616k
           block_index++ )
2527
616k
      {
2528
616k
        output_data[ data_offset++ ] ^= internal_initialization_vector[ block_index ];
2529
616k
      }
2530
38.5k
#endif
2531
38.5k
      data_offset -= 16;
2532
2533
38.5k
      if( memory_copy(
2534
38.5k
           internal_initialization_vector,
2535
38.5k
           &( input_data[ data_offset ] ),
2536
38.5k
           16 ) == NULL )
2537
0
      {
2538
0
        libcerror_error_set(
2539
0
         error,
2540
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
2541
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
2542
0
         "%s: unable to copy encrypted input data to initialization vector.",
2543
0
         function );
2544
2545
0
        goto on_error;
2546
0
      }
2547
38.5k
    }
2548
38.5k
    data_offset += 16;
2549
38.5k
  }
2550
1.20k
  if( memory_set(
2551
1.20k
       internal_initialization_vector,
2552
1.20k
       0,
2553
1.20k
       16 ) == NULL )
2554
0
  {
2555
0
    libcerror_error_set(
2556
0
     error,
2557
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2558
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
2559
0
     "%s: unable to clear initialization vector.",
2560
0
     function );
2561
2562
0
    goto on_error;
2563
0
  }
2564
1.20k
  if( memory_set(
2565
1.20k
       block_data,
2566
1.20k
       0,
2567
1.20k
       16 ) == NULL )
2568
0
  {
2569
0
    libcerror_error_set(
2570
0
     error,
2571
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2572
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
2573
0
     "%s: unable to clear block data.",
2574
0
     function );
2575
2576
0
    goto on_error;
2577
0
  }
2578
1.20k
  return( 1 );
2579
2580
0
on_error:
2581
0
  memory_set(
2582
0
   internal_initialization_vector,
2583
0
   0,
2584
0
   16 );
2585
2586
0
  memory_set(
2587
0
   block_data,
2588
0
   0,
2589
0
   16 );
2590
2591
0
  return( -1 );
2592
1.20k
}
2593
2594
/* De- or encrypts a block of data using Serpent-ECB (Electronic CodeBook)
2595
 * The size must be a multitude of the Serpent block size (16 byte)
2596
 * Returns 1 if successful or -1 on error
2597
 */
2598
int libfcrypto_serpent_crypt_ecb(
2599
     libfcrypto_serpent_context_t *context,
2600
     int mode,
2601
     const uint8_t *input_data,
2602
     size_t input_data_size,
2603
     uint8_t *output_data,
2604
     size_t output_data_size,
2605
     libcerror_error_t **error )
2606
55
{
2607
55
  static char *function = "libfcrypto_serpent_context_crypt_ecb";
2608
55
  size_t data_offset    = 0;
2609
2610
55
  if( context == NULL )
2611
0
  {
2612
0
    libcerror_error_set(
2613
0
     error,
2614
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2615
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2616
0
     "%s: invalid context.",
2617
0
     function );
2618
2619
0
    return( -1 );
2620
0
  }
2621
55
  if( ( mode != LIBFCRYPTO_SERPENT_CRYPT_MODE_ENCRYPT )
2622
0
   && ( mode != LIBFCRYPTO_SERPENT_CRYPT_MODE_DECRYPT ) )
2623
0
  {
2624
0
    libcerror_error_set(
2625
0
     error,
2626
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2627
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2628
0
     "%s: unsupported mode.",
2629
0
     function );
2630
2631
0
    return( -1 );
2632
0
  }
2633
55
  if( input_data == NULL )
2634
0
  {
2635
0
    libcerror_error_set(
2636
0
     error,
2637
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2638
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2639
0
     "%s: invalid input data.",
2640
0
     function );
2641
2642
0
    return( -1 );
2643
0
  }
2644
  /* Check if the input data size is a multitude of 16-byte
2645
   */
2646
55
  if( ( ( input_data_size & (size_t) 0x0f ) != 0 )
2647
51
   || ( input_data_size < 16 )
2648
51
   || ( input_data_size > (size_t) SSIZE_MAX ) )
2649
4
  {
2650
4
    libcerror_error_set(
2651
4
     error,
2652
4
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2653
4
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2654
4
     "%s: invalid input data size value out of bounds.",
2655
4
     function );
2656
2657
4
    return( -1 );
2658
4
  }
2659
51
  if( output_data == NULL )
2660
0
  {
2661
0
    libcerror_error_set(
2662
0
     error,
2663
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2664
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2665
0
     "%s: invalid output data.",
2666
0
     function );
2667
2668
0
    return( -1 );
2669
0
  }
2670
51
  if( ( output_data_size < input_data_size )
2671
4
   || ( output_data_size > (size_t) SSIZE_MAX ) )
2672
47
  {
2673
47
    libcerror_error_set(
2674
47
     error,
2675
47
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2676
47
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2677
47
     "%s: invalid output data size value out of bounds.",
2678
47
     function );
2679
2680
47
    return( -1 );
2681
47
  }
2682
14
  while( data_offset < input_data_size )
2683
10
  {
2684
10
    if( mode == LIBFCRYPTO_SERPENT_CRYPT_MODE_ENCRYPT )
2685
10
    {
2686
10
      if( libfcrypto_internal_serpent_context_encrypt_block(
2687
10
           (libfcrypto_internal_serpent_context_t *) context,
2688
10
           &( input_data[ data_offset ] ),
2689
10
           16,
2690
10
           &( output_data[ data_offset ] ),
2691
10
           16,
2692
10
           error ) != 1 )
2693
0
      {
2694
0
        libcerror_error_set(
2695
0
         error,
2696
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
2697
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
2698
0
         "%s: unable to encrypt input data.",
2699
0
         function );
2700
2701
0
        return( -1 );
2702
0
      }
2703
10
    }
2704
0
    else
2705
0
    {
2706
0
      if( libfcrypto_internal_serpent_context_decrypt_block(
2707
0
           (libfcrypto_internal_serpent_context_t *) context,
2708
0
           &( input_data[ data_offset ] ),
2709
0
           16,
2710
0
           &( output_data[ data_offset ] ),
2711
0
           16,
2712
0
           error ) != 1 )
2713
0
      {
2714
0
        libcerror_error_set(
2715
0
         error,
2716
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
2717
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
2718
0
         "%s: unable to decrypt input data.",
2719
0
         function );
2720
2721
0
        return( -1 );
2722
0
      }
2723
0
    }
2724
10
    data_offset += 16;
2725
10
  }
2726
4
  return( 1 );
2727
4
}
2728