annotate des.c @ 992:a13125b5be3a libavutil

bswap: change ME to NE in macro names Other parts of FFmpeg use NE (native endian) rather than ME (machine). This makes it consistent.
author mru
date Sat, 10 Jul 2010 22:09:01 +0000
parents e89d42c1effa
children f8db9a2bae05
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
1 /*
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
2 * DES encryption/decryption
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
3 * Copyright (c) 2007 Reimar Doeffinger
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
4 *
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
5 * This file is part of FFmpeg.
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
6 *
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
11 *
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
15 * Lesser General Public License for more details.
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
16 *
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
20 */
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
21 #include <inttypes.h>
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
22 #include "avutil.h"
481
f4187c1c15a6 Reapply r12489: Add pure, const and malloc attributes to proper functions
zuxy
parents: 478
diff changeset
23 #include "common.h"
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
24 #include "intreadwrite.h"
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
25 #include "des.h"
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
26
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
27 typedef struct AVDES AVDES;
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
28
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
29 #define T(a, b, c, d, e, f, g, h) 64-a,64-b,64-c,64-d,64-e,64-f,64-g,64-h
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
30 static const uint8_t IP_shuffle[] = {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
31 T(58, 50, 42, 34, 26, 18, 10, 2),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
32 T(60, 52, 44, 36, 28, 20, 12, 4),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
33 T(62, 54, 46, 38, 30, 22, 14, 6),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
34 T(64, 56, 48, 40, 32, 24, 16, 8),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
35 T(57, 49, 41, 33, 25, 17, 9, 1),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
36 T(59, 51, 43, 35, 27, 19, 11, 3),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
37 T(61, 53, 45, 37, 29, 21, 13, 5),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
38 T(63, 55, 47, 39, 31, 23, 15, 7)
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
39 };
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
40 #undef T
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
41
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
42 #define T(a, b, c, d) 32-a,32-b,32-c,32-d
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
43 static const uint8_t P_shuffle[] = {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
44 T(16, 7, 20, 21),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
45 T(29, 12, 28, 17),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
46 T( 1, 15, 23, 26),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
47 T( 5, 18, 31, 10),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
48 T( 2, 8, 24, 14),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
49 T(32, 27, 3, 9),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
50 T(19, 13, 30, 6),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
51 T(22, 11, 4, 25)
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
52 };
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
53 #undef T
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
54
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
55 #define T(a, b, c, d, e, f, g) 64-a,64-b,64-c,64-d,64-e,64-f,64-g
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
56 static const uint8_t PC1_shuffle[] = {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
57 T(57, 49, 41, 33, 25, 17, 9),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
58 T( 1, 58, 50, 42, 34, 26, 18),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
59 T(10, 2, 59, 51, 43, 35, 27),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
60 T(19, 11, 3, 60, 52, 44, 36),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
61 T(63, 55, 47, 39, 31, 23, 15),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
62 T( 7, 62, 54, 46, 38, 30, 22),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
63 T(14, 6, 61, 53, 45, 37, 29),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
64 T(21, 13, 5, 28, 20, 12, 4)
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
65 };
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
66 #undef T
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
67
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
68 #define T(a, b, c, d, e, f) 56-a,56-b,56-c,56-d,56-e,56-f
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
69 static const uint8_t PC2_shuffle[] = {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
70 T(14, 17, 11, 24, 1, 5),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
71 T( 3, 28, 15, 6, 21, 10),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
72 T(23, 19, 12, 4, 26, 8),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
73 T(16, 7, 27, 20, 13, 2),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
74 T(41, 52, 31, 37, 47, 55),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
75 T(30, 40, 51, 45, 33, 48),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
76 T(44, 49, 39, 56, 34, 53),
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
77 T(46, 42, 50, 36, 29, 32)
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
78 };
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
79 #undef T
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
80
603
880c6441f56a Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurel
parents: 497
diff changeset
81 #if CONFIG_SMALL
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
82 static const uint8_t S_boxes[8][32] = {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
83 {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
84 0x0e, 0xf4, 0x7d, 0x41, 0xe2, 0x2f, 0xdb, 0x18, 0xa3, 0x6a, 0xc6, 0xbc, 0x95, 0x59, 0x30, 0x87,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
85 0xf4, 0xc1, 0x8e, 0x28, 0x4d, 0x96, 0x12, 0x7b, 0x5f, 0xbc, 0x39, 0xe7, 0xa3, 0x0a, 0x65, 0xd0,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
86 }, {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
87 0x3f, 0xd1, 0x48, 0x7e, 0xf6, 0x2b, 0x83, 0xe4, 0xc9, 0x07, 0x12, 0xad, 0x6c, 0x90, 0xb5, 0x5a,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
88 0xd0, 0x8e, 0xa7, 0x1b, 0x3a, 0xf4, 0x4d, 0x21, 0xb5, 0x68, 0x7c, 0xc6, 0x09, 0x53, 0xe2, 0x9f,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
89 }, {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
90 0xda, 0x70, 0x09, 0x9e, 0x36, 0x43, 0x6f, 0xa5, 0x21, 0x8d, 0x5c, 0xe7, 0xcb, 0xb4, 0xf2, 0x18,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
91 0x1d, 0xa6, 0xd4, 0x09, 0x68, 0x9f, 0x83, 0x70, 0x4b, 0xf1, 0xe2, 0x3c, 0xb5, 0x5a, 0x2e, 0xc7,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
92 }, {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
93 0xd7, 0x8d, 0xbe, 0x53, 0x60, 0xf6, 0x09, 0x3a, 0x41, 0x72, 0x28, 0xc5, 0x1b, 0xac, 0xe4, 0x9f,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
94 0x3a, 0xf6, 0x09, 0x60, 0xac, 0x1b, 0xd7, 0x8d, 0x9f, 0x41, 0x53, 0xbe, 0xc5, 0x72, 0x28, 0xe4,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
95 }, {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
96 0xe2, 0xbc, 0x24, 0xc1, 0x47, 0x7a, 0xdb, 0x16, 0x58, 0x05, 0xf3, 0xaf, 0x3d, 0x90, 0x8e, 0x69,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
97 0xb4, 0x82, 0xc1, 0x7b, 0x1a, 0xed, 0x27, 0xd8, 0x6f, 0xf9, 0x0c, 0x95, 0xa6, 0x43, 0x50, 0x3e,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
98 }, {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
99 0xac, 0xf1, 0x4a, 0x2f, 0x79, 0xc2, 0x96, 0x58, 0x60, 0x1d, 0xd3, 0xe4, 0x0e, 0xb7, 0x35, 0x8b,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
100 0x49, 0x3e, 0x2f, 0xc5, 0x92, 0x58, 0xfc, 0xa3, 0xb7, 0xe0, 0x14, 0x7a, 0x61, 0x0d, 0x8b, 0xd6,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
101 }, {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
102 0xd4, 0x0b, 0xb2, 0x7e, 0x4f, 0x90, 0x18, 0xad, 0xe3, 0x3c, 0x59, 0xc7, 0x25, 0xfa, 0x86, 0x61,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
103 0x61, 0xb4, 0xdb, 0x8d, 0x1c, 0x43, 0xa7, 0x7e, 0x9a, 0x5f, 0x06, 0xf8, 0xe0, 0x25, 0x39, 0xc2,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
104 }, {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
105 0x1d, 0xf2, 0xd8, 0x84, 0xa6, 0x3f, 0x7b, 0x41, 0xca, 0x59, 0x63, 0xbe, 0x05, 0xe0, 0x9c, 0x27,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
106 0x27, 0x1b, 0xe4, 0x71, 0x49, 0xac, 0x8e, 0xd2, 0xf0, 0xc6, 0x9a, 0x0d, 0x3f, 0x53, 0x65, 0xb8,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
107 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
108 };
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
109 #else
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
110 /**
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
111 * This table contains the results of applying both the S-box and P-shuffle.
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
112 * It can be regenerated by compiling this file with -DCONFIG_SMALL -DTEST -DGENTABLES
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
113 */
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
114 static const uint32_t S_boxes_P_shuffle[8][64] = {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
115 {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
116 0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808002, 0x00008202, 0x00000002, 0x00008000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
117 0x00000200, 0x00808200, 0x00808202, 0x00000200, 0x00800202, 0x00808002, 0x00800000, 0x00000002,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
118 0x00000202, 0x00800200, 0x00800200, 0x00008200, 0x00008200, 0x00808000, 0x00808000, 0x00800202,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
119 0x00008002, 0x00800002, 0x00800002, 0x00008002, 0x00000000, 0x00000202, 0x00008202, 0x00800000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
120 0x00008000, 0x00808202, 0x00000002, 0x00808000, 0x00808200, 0x00800000, 0x00800000, 0x00000200,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
121 0x00808002, 0x00008000, 0x00008200, 0x00800002, 0x00000200, 0x00000002, 0x00800202, 0x00008202,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
122 0x00808202, 0x00008002, 0x00808000, 0x00800202, 0x00800002, 0x00000202, 0x00008202, 0x00808200,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
123 0x00000202, 0x00800200, 0x00800200, 0x00000000, 0x00008002, 0x00008200, 0x00000000, 0x00808002,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
124 },
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
125 {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
126 0x40084010, 0x40004000, 0x00004000, 0x00084010, 0x00080000, 0x00000010, 0x40080010, 0x40004010,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
127 0x40000010, 0x40084010, 0x40084000, 0x40000000, 0x40004000, 0x00080000, 0x00000010, 0x40080010,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
128 0x00084000, 0x00080010, 0x40004010, 0x00000000, 0x40000000, 0x00004000, 0x00084010, 0x40080000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
129 0x00080010, 0x40000010, 0x00000000, 0x00084000, 0x00004010, 0x40084000, 0x40080000, 0x00004010,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
130 0x00000000, 0x00084010, 0x40080010, 0x00080000, 0x40004010, 0x40080000, 0x40084000, 0x00004000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
131 0x40080000, 0x40004000, 0x00000010, 0x40084010, 0x00084010, 0x00000010, 0x00004000, 0x40000000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
132 0x00004010, 0x40084000, 0x00080000, 0x40000010, 0x00080010, 0x40004010, 0x40000010, 0x00080010,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
133 0x00084000, 0x00000000, 0x40004000, 0x00004010, 0x40000000, 0x40080010, 0x40084010, 0x00084000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
134 },
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
135 {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
136 0x00000104, 0x04010100, 0x00000000, 0x04010004, 0x04000100, 0x00000000, 0x00010104, 0x04000100,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
137 0x00010004, 0x04000004, 0x04000004, 0x00010000, 0x04010104, 0x00010004, 0x04010000, 0x00000104,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
138 0x04000000, 0x00000004, 0x04010100, 0x00000100, 0x00010100, 0x04010000, 0x04010004, 0x00010104,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
139 0x04000104, 0x00010100, 0x00010000, 0x04000104, 0x00000004, 0x04010104, 0x00000100, 0x04000000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
140 0x04010100, 0x04000000, 0x00010004, 0x00000104, 0x00010000, 0x04010100, 0x04000100, 0x00000000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
141 0x00000100, 0x00010004, 0x04010104, 0x04000100, 0x04000004, 0x00000100, 0x00000000, 0x04010004,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
142 0x04000104, 0x00010000, 0x04000000, 0x04010104, 0x00000004, 0x00010104, 0x00010100, 0x04000004,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
143 0x04010000, 0x04000104, 0x00000104, 0x04010000, 0x00010104, 0x00000004, 0x04010004, 0x00010100,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
144 },
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
145 {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
146 0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x00401040, 0x80400040, 0x80400000, 0x80001000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
147 0x00000000, 0x00401000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00400040, 0x80400000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
148 0x80000000, 0x00001000, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x80001000, 0x00001040,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
149 0x80400040, 0x80000000, 0x00001040, 0x00400040, 0x00001000, 0x00401040, 0x80401040, 0x80000040,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
150 0x00400040, 0x80400000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00000000, 0x00401000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
151 0x00001040, 0x00400040, 0x80400040, 0x80000000, 0x80401000, 0x80001040, 0x80001040, 0x00000040,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
152 0x80401040, 0x80000040, 0x80000000, 0x00001000, 0x80400000, 0x80001000, 0x00401040, 0x80400040,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
153 0x80001000, 0x00001040, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x00001000, 0x00401040,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
154 },
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
155 {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
156 0x00000080, 0x01040080, 0x01040000, 0x21000080, 0x00040000, 0x00000080, 0x20000000, 0x01040000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
157 0x20040080, 0x00040000, 0x01000080, 0x20040080, 0x21000080, 0x21040000, 0x00040080, 0x20000000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
158 0x01000000, 0x20040000, 0x20040000, 0x00000000, 0x20000080, 0x21040080, 0x21040080, 0x01000080,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
159 0x21040000, 0x20000080, 0x00000000, 0x21000000, 0x01040080, 0x01000000, 0x21000000, 0x00040080,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
160 0x00040000, 0x21000080, 0x00000080, 0x01000000, 0x20000000, 0x01040000, 0x21000080, 0x20040080,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
161 0x01000080, 0x20000000, 0x21040000, 0x01040080, 0x20040080, 0x00000080, 0x01000000, 0x21040000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
162 0x21040080, 0x00040080, 0x21000000, 0x21040080, 0x01040000, 0x00000000, 0x20040000, 0x21000000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
163 0x00040080, 0x01000080, 0x20000080, 0x00040000, 0x00000000, 0x20040000, 0x01040080, 0x20000080,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
164 },
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
165 {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
166 0x10000008, 0x10200000, 0x00002000, 0x10202008, 0x10200000, 0x00000008, 0x10202008, 0x00200000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
167 0x10002000, 0x00202008, 0x00200000, 0x10000008, 0x00200008, 0x10002000, 0x10000000, 0x00002008,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
168 0x00000000, 0x00200008, 0x10002008, 0x00002000, 0x00202000, 0x10002008, 0x00000008, 0x10200008,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
169 0x10200008, 0x00000000, 0x00202008, 0x10202000, 0x00002008, 0x00202000, 0x10202000, 0x10000000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
170 0x10002000, 0x00000008, 0x10200008, 0x00202000, 0x10202008, 0x00200000, 0x00002008, 0x10000008,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
171 0x00200000, 0x10002000, 0x10000000, 0x00002008, 0x10000008, 0x10202008, 0x00202000, 0x10200000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
172 0x00202008, 0x10202000, 0x00000000, 0x10200008, 0x00000008, 0x00002000, 0x10200000, 0x00202008,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
173 0x00002000, 0x00200008, 0x10002008, 0x00000000, 0x10202000, 0x10000000, 0x00200008, 0x10002008,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
174 },
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
175 {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
176 0x00100000, 0x02100001, 0x02000401, 0x00000000, 0x00000400, 0x02000401, 0x00100401, 0x02100400,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
177 0x02100401, 0x00100000, 0x00000000, 0x02000001, 0x00000001, 0x02000000, 0x02100001, 0x00000401,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
178 0x02000400, 0x00100401, 0x00100001, 0x02000400, 0x02000001, 0x02100000, 0x02100400, 0x00100001,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
179 0x02100000, 0x00000400, 0x00000401, 0x02100401, 0x00100400, 0x00000001, 0x02000000, 0x00100400,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
180 0x02000000, 0x00100400, 0x00100000, 0x02000401, 0x02000401, 0x02100001, 0x02100001, 0x00000001,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
181 0x00100001, 0x02000000, 0x02000400, 0x00100000, 0x02100400, 0x00000401, 0x00100401, 0x02100400,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
182 0x00000401, 0x02000001, 0x02100401, 0x02100000, 0x00100400, 0x00000000, 0x00000001, 0x02100401,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
183 0x00000000, 0x00100401, 0x02100000, 0x00000400, 0x02000001, 0x02000400, 0x00000400, 0x00100001,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
184 },
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
185 {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
186 0x08000820, 0x00000800, 0x00020000, 0x08020820, 0x08000000, 0x08000820, 0x00000020, 0x08000000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
187 0x00020020, 0x08020000, 0x08020820, 0x00020800, 0x08020800, 0x00020820, 0x00000800, 0x00000020,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
188 0x08020000, 0x08000020, 0x08000800, 0x00000820, 0x00020800, 0x00020020, 0x08020020, 0x08020800,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
189 0x00000820, 0x00000000, 0x00000000, 0x08020020, 0x08000020, 0x08000800, 0x00020820, 0x00020000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
190 0x00020820, 0x00020000, 0x08020800, 0x00000800, 0x00000020, 0x08020020, 0x00000800, 0x00020820,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
191 0x08000800, 0x00000020, 0x08000020, 0x08020000, 0x08020020, 0x08000000, 0x00020000, 0x08000820,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
192 0x00000000, 0x08020820, 0x00020020, 0x08000020, 0x08020000, 0x08000800, 0x08000820, 0x00000000,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
193 0x08020820, 0x00020800, 0x00020800, 0x00000820, 0x00000820, 0x00020020, 0x08000000, 0x08020800,
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
194 },
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
195 };
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
196 #endif
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
197
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
198 static uint64_t shuffle(uint64_t in, const uint8_t *shuffle, int shuffle_len) {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
199 int i;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
200 uint64_t res = 0;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
201 for (i = 0; i < shuffle_len; i++)
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
202 res += res + ((in >> *shuffle++) & 1);
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
203 return res;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
204 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
205
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
206 static uint64_t shuffle_inv(uint64_t in, const uint8_t *shuffle, int shuffle_len) {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
207 int i;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
208 uint64_t res = 0;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
209 shuffle += shuffle_len - 1;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
210 for (i = 0; i < shuffle_len; i++) {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
211 res |= (in & 1) << *shuffle--;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
212 in >>= 1;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
213 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
214 return res;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
215 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
216
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
217 static uint32_t f_func(uint32_t r, uint64_t k) {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
218 int i;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
219 uint32_t out = 0;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
220 // rotate to get first part of E-shuffle in the lowest 6 bits
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
221 r = (r << 1) | (r >> 31);
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
222 // apply S-boxes, those compress the data again from 8 * 6 to 8 * 4 bits
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
223 for (i = 7; i >= 0; i--) {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
224 uint8_t tmp = (r ^ k) & 0x3f;
603
880c6441f56a Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurel
parents: 497
diff changeset
225 #if CONFIG_SMALL
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
226 uint8_t v = S_boxes[i][tmp >> 1];
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
227 if (tmp & 1) v >>= 4;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
228 out = (out >> 4) | (v << 28);
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
229 #else
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
230 out |= S_boxes_P_shuffle[i][tmp];
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
231 #endif
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
232 // get next 6 bits of E-shuffle and round key k into the lowest bits
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
233 r = (r >> 4) | (r << 28);
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
234 k >>= 6;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
235 }
603
880c6441f56a Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurel
parents: 497
diff changeset
236 #if CONFIG_SMALL
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
237 out = shuffle(out, P_shuffle, sizeof(P_shuffle));
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
238 #endif
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
239 return out;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
240 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
241
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
242 /**
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
243 * \brief rotate the two halves of the expanded 56 bit key each 1 bit left
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
244 *
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
245 * Note: the specification calls this "shift", so I kept it although
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
246 * it is confusing.
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
247 */
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
248 static uint64_t key_shift_left(uint64_t CDn) {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
249 uint64_t carries = (CDn >> 27) & 0x10000001;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
250 CDn <<= 1;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
251 CDn &= ~0x10000001;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
252 CDn |= carries;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
253 return CDn;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
254 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
255
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
256 static void gen_roundkeys(uint64_t K[16], uint64_t key) {
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
257 int i;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
258 // discard parity bits from key and shuffle it into C and D parts
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
259 uint64_t CDn = shuffle(key, PC1_shuffle, sizeof(PC1_shuffle));
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
260 // generate round keys
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
261 for (i = 0; i < 16; i++) {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
262 CDn = key_shift_left(CDn);
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
263 if (i > 1 && i != 8 && i != 15)
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
264 CDn = key_shift_left(CDn);
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
265 K[i] = shuffle(CDn, PC2_shuffle, sizeof(PC2_shuffle));
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
266 }
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
267 }
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
268
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
269 static uint64_t des_encdec(uint64_t in, uint64_t K[16], int decrypt) {
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
270 int i;
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
271 // used to apply round keys in reverse order for decryption
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
272 decrypt = decrypt ? 15 : 0;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
273 // shuffle irrelevant to security but to ease hardware implementations
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
274 in = shuffle(in, IP_shuffle, sizeof(IP_shuffle));
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
275 for (i = 0; i < 16; i++) {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
276 uint32_t f_res;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
277 f_res = f_func(in, K[decrypt ^ i]);
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
278 in = (in << 32) | (in >> 32);
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
279 in ^= f_res;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
280 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
281 in = (in << 32) | (in >> 32);
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
282 // reverse shuffle used to ease hardware implementations
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
283 in = shuffle_inv(in, IP_shuffle, sizeof(IP_shuffle));
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
284 return in;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
285 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
286
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
287 int av_des_init(AVDES *d, const uint8_t *key, int key_bits, int decrypt) {
648
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
288 if (key_bits != 64 && key_bits != 192)
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
289 return -1;
648
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
290 d->triple_des = key_bits > 64;
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
291 gen_roundkeys(d->round_keys[0], AV_RB64(key));
648
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
292 if (d->triple_des) {
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
293 gen_roundkeys(d->round_keys[1], AV_RB64(key + 8));
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
294 gen_roundkeys(d->round_keys[2], AV_RB64(key + 16));
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
295 }
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
296 return 0;
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
297 }
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
298
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
299 void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
992
a13125b5be3a bswap: change ME to NE in macro names
mru
parents: 820
diff changeset
300 uint64_t iv_val = iv ? be2ne_64(*(uint64_t *)iv) : 0;
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
301 while (count-- > 0) {
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
302 uint64_t dst_val;
992
a13125b5be3a bswap: change ME to NE in macro names
mru
parents: 820
diff changeset
303 uint64_t src_val = src ? be2ne_64(*(const uint64_t *)src) : 0;
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
304 if (decrypt) {
650
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
305 uint64_t tmp = src_val;
648
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
306 if (d->triple_des) {
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
307 src_val = des_encdec(src_val, d->round_keys[2], 1);
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
308 src_val = des_encdec(src_val, d->round_keys[1], 0);
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
309 }
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
310 dst_val = des_encdec(src_val, d->round_keys[0], 1) ^ iv_val;
650
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
311 iv_val = iv ? tmp : 0;
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
312 } else {
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
313 dst_val = des_encdec(src_val ^ iv_val, d->round_keys[0], 0);
648
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
314 if (d->triple_des) {
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
315 dst_val = des_encdec(dst_val, d->round_keys[1], 1);
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
316 dst_val = des_encdec(dst_val, d->round_keys[2], 0);
ed614f42a5d6 Add support for 3DES to DES module
reimar
parents: 647
diff changeset
317 }
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
318 iv_val = iv ? dst_val : 0;
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
319 }
992
a13125b5be3a bswap: change ME to NE in macro names
mru
parents: 820
diff changeset
320 *(uint64_t *)dst = be2ne_64(dst_val);
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
321 src += 8;
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
322 dst += 8;
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
323 }
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
324 if (iv)
992
a13125b5be3a bswap: change ME to NE in macro names
mru
parents: 820
diff changeset
325 *(uint64_t *)iv = be2ne_64(iv_val);
647
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
326 }
67fb0b442dd2 Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents: 617
diff changeset
327
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
328 #ifdef TEST
497
af2d7b4ffa64 Fix des-test compilation.
diego
parents: 481
diff changeset
329 #undef printf
af2d7b4ffa64 Fix des-test compilation.
diego
parents: 481
diff changeset
330 #undef rand
af2d7b4ffa64 Fix des-test compilation.
diego
parents: 481
diff changeset
331 #undef srand
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
332 #include <stdlib.h>
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
333 #include <stdio.h>
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
334 #include <sys/time.h>
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
335 static uint64_t rand64(void) {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
336 uint64_t r = rand();
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
337 r = (r << 32) | rand();
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
338 return r;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
339 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
340
649
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
341 static const uint8_t test_key[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
820
e89d42c1effa Move array specifiers outside DECLARE_ALIGNED() invocations
mru
parents: 716
diff changeset
342 static const DECLARE_ALIGNED(8, uint8_t, plain)[] = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
e89d42c1effa Move array specifiers outside DECLARE_ALIGNED() invocations
mru
parents: 716
diff changeset
343 static const DECLARE_ALIGNED(8, uint8_t, crypt)[] = {0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18};
e89d42c1effa Move array specifiers outside DECLARE_ALIGNED() invocations
mru
parents: 716
diff changeset
344 static DECLARE_ALIGNED(8, uint8_t, tmp)[8];
e89d42c1effa Move array specifiers outside DECLARE_ALIGNED() invocations
mru
parents: 716
diff changeset
345 static DECLARE_ALIGNED(8, uint8_t, large_buffer)[10002][8];
650
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
346 static const uint8_t cbc_key[] = {
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
347 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
348 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
349 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
350 };
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
351
716
3f3e69baaaf8 Mark non-exported functions in test and example programs as static.
diego
parents: 685
diff changeset
352 static int run_test(int cbc, int decrypt) {
650
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
353 AVDES d;
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
354 int delay = cbc && !decrypt ? 2 : 1;
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
355 uint64_t res;
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
356 AV_WB64(large_buffer[0], 0x4e6f772069732074ULL);
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
357 AV_WB64(large_buffer[1], 0x1234567890abcdefULL);
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
358 AV_WB64(tmp, 0x1234567890abcdefULL);
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
359 av_des_init(&d, cbc_key, 192, decrypt);
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
360 av_des_crypt(&d, large_buffer[delay], large_buffer[0], 10000, cbc ? tmp : NULL, decrypt);
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
361 res = AV_RB64(large_buffer[9999 + delay]);
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
362 if (cbc) {
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
363 if (decrypt)
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
364 return res == 0xc5cecf63ecec514cULL;
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
365 else
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
366 return res == 0xcb191f85d1ed8439ULL;
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
367 } else {
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
368 if (decrypt)
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
369 return res == 0x8325397644091a0aULL;
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
370 else
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
371 return res == 0xdd17e8b8b437d232ULL;
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
372 }
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
373 }
649
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
374
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
375 int main(void) {
649
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
376 AVDES d;
617
96a457934740 Avoid unused variable warning when compiling DES test program.
diego
parents: 603
diff changeset
377 int i;
96a457934740 Avoid unused variable warning when compiling DES test program.
diego
parents: 603
diff changeset
378 #ifdef GENTABLES
96a457934740 Avoid unused variable warning when compiling DES test program.
diego
parents: 603
diff changeset
379 int j;
96a457934740 Avoid unused variable warning when compiling DES test program.
diego
parents: 603
diff changeset
380 #endif
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
381 struct timeval tv;
649
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
382 uint64_t key[3];
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
383 uint64_t data;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
384 uint64_t ct;
685
634bcf10f5df Remove/replace code disabled by major version bump.
reimar
parents: 651
diff changeset
385 uint64_t roundkeys[16];
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
386 gettimeofday(&tv, NULL);
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
387 srand(tv.tv_sec * 1000 * 1000 + tv.tv_usec);
649
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
388 key[0] = AV_RB64(test_key);
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
389 data = AV_RB64(plain);
685
634bcf10f5df Remove/replace code disabled by major version bump.
reimar
parents: 651
diff changeset
390 gen_roundkeys(roundkeys, key[0]);
634bcf10f5df Remove/replace code disabled by major version bump.
reimar
parents: 651
diff changeset
391 if (des_encdec(data, roundkeys, 0) != AV_RB64(crypt)) {
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
392 printf("Test 1 failed\n");
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
393 return 1;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
394 }
649
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
395 av_des_init(&d, test_key, 64, 0);
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
396 av_des_crypt(&d, tmp, plain, 1, NULL, 0);
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
397 if (memcmp(tmp, crypt, sizeof(crypt))) {
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
398 printf("Public API decryption failed\n");
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
399 return 1;
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
400 }
650
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
401 if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) {
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
402 printf("Partial Monte-Carlo test failed\n");
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
403 return 1;
8e35c6f747dc Fix a bug in 3DEC CBC decryption and add more extensive tests based on
reimar
parents: 649
diff changeset
404 }
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
405 for (i = 0; i < 1000000; i++) {
649
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
406 key[0] = rand64(); key[1] = rand64(); key[2] = rand64();
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
407 data = rand64();
649
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
408 av_des_init(&d, key, 192, 0);
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
409 av_des_crypt(&d, &ct, &data, 1, NULL, 0);
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
410 av_des_init(&d, key, 192, 1);
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
411 av_des_crypt(&d, &ct, &ct, 1, NULL, 1);
9c0d8e9fe37b Update DES test code to use the new public API.
reimar
parents: 648
diff changeset
412 if (ct != data) {
393
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
413 printf("Test 2 failed\n");
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
414 return 1;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
415 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
416 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
417 #ifdef GENTABLES
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
418 printf("static const uint32_t S_boxes_P_shuffle[8][64] = {\n");
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
419 for (i = 0; i < 8; i++) {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
420 printf(" {");
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
421 for (j = 0; j < 64; j++) {
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
422 uint32_t v = S_boxes[i][j >> 1];
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
423 v = j & 1 ? v >> 4 : v & 0xf;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
424 v <<= 28 - 4 * i;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
425 v = shuffle(v, P_shuffle, sizeof(P_shuffle));
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
426 printf((j & 7) == 0 ? "\n " : " ");
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
427 printf("0x%08X,", v);
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
428 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
429 printf("\n },\n");
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
430 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
431 printf("};\n");
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
432 #endif
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
433 return 0;
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
434 }
df047574d017 Add support for DES en- and decryption.
reimar
parents:
diff changeset
435 #endif