monotone

monotone Mtn Source Tree

Root/botan/des.cpp

1/*************************************************
2* DES Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/des.h>
7#include <botan/bit_ops.h>
8
9namespace Botan {
10
11/*************************************************
12* DES Encryption *
13*************************************************/
14void DES::enc(const byte in[], byte out[]) const
15 {
16 u32bit L = make_u32bit(in[0], in[1], in[2], in[3]),
17 R = make_u32bit(in[4], in[5], in[6], in[7]);
18
19 IP(L, R);
20 raw_encrypt(L, R);
21 FP(L, R);
22
23 out[0] = get_byte(0, R); out[1] = get_byte(1, R);
24 out[2] = get_byte(2, R); out[3] = get_byte(3, R);
25 out[4] = get_byte(0, L); out[5] = get_byte(1, L);
26 out[6] = get_byte(2, L); out[7] = get_byte(3, L);
27 }
28
29/*************************************************
30* DES Decryption *
31*************************************************/
32void DES::dec(const byte in[], byte out[]) const
33 {
34 u32bit L = make_u32bit(in[0], in[1], in[2], in[3]),
35 R = make_u32bit(in[4], in[5], in[6], in[7]);
36
37 IP(L, R);
38 raw_decrypt(L, R);
39 FP(L, R);
40
41 out[0] = get_byte(0, R); out[1] = get_byte(1, R);
42 out[2] = get_byte(2, R); out[3] = get_byte(3, R);
43 out[4] = get_byte(0, L); out[5] = get_byte(1, L);
44 out[6] = get_byte(2, L); out[7] = get_byte(3, L);
45 }
46
47/*************************************************
48* DES Initial Permutation *
49*************************************************/
50void DES::IP(u32bit& L, u32bit& R)
51 {
52 u64bit T = (IPTAB1[get_byte(0, L)] ) | (IPTAB1[get_byte(1, L)] << 1) |
53 (IPTAB1[get_byte(2, L)] << 2) | (IPTAB1[get_byte(3, L)] << 3) |
54 (IPTAB1[get_byte(0, R)] << 4) | (IPTAB1[get_byte(1, R)] << 5) |
55 (IPTAB1[get_byte(2, R)] << 6) | (IPTAB2[get_byte(3, R)] );
56 L = (u32bit)((T >> 32) & 0xFFFFFFFF);
57 R = (u32bit)((T ) & 0xFFFFFFFF);
58 }
59
60/*************************************************
61* DES Final Permutation *
62*************************************************/
63void DES::FP(u32bit& L, u32bit& R)
64 {
65 u64bit T = (FPTAB1[get_byte(0, L)] << 5) | (FPTAB1[get_byte(1, L)] << 3) |
66 (FPTAB1[get_byte(2, L)] << 1) | (FPTAB2[get_byte(3, L)] << 1) |
67 (FPTAB1[get_byte(0, R)] << 4) | (FPTAB1[get_byte(1, R)] << 2) |
68 (FPTAB1[get_byte(2, R)] ) | (FPTAB2[get_byte(3, R)] );
69 L = (u32bit)((T >> 32) & 0xFFFFFFFF);
70 R = (u32bit)((T ) & 0xFFFFFFFF);
71 }
72
73/*************************************************
74* DES Raw Encryption *
75*************************************************/
76void DES::raw_encrypt(u32bit& L, u32bit& R) const
77 {
78 for(u32bit j = 0; j != 16; j += 2)
79 {
80 u32bit T0, T1;
81
82 T0 = rotate_right(R, 4) ^ round_key[2*j];
83 T1 = R ^ round_key[2*j + 1];
84
85 L ^= SPBOX1[get_byte(0, T0)] ^ SPBOX2[get_byte(0, T1)] ^
86 SPBOX3[get_byte(1, T0)] ^ SPBOX4[get_byte(1, T1)] ^
87 SPBOX5[get_byte(2, T0)] ^ SPBOX6[get_byte(2, T1)] ^
88 SPBOX7[get_byte(3, T0)] ^ SPBOX8[get_byte(3, T1)];
89
90 T0 = rotate_right(L, 4) ^ round_key[2*j + 2];
91 T1 = L ^ round_key[2*j + 3];
92
93 R ^= SPBOX1[get_byte(0, T0)] ^ SPBOX2[get_byte(0, T1)] ^
94 SPBOX3[get_byte(1, T0)] ^ SPBOX4[get_byte(1, T1)] ^
95 SPBOX5[get_byte(2, T0)] ^ SPBOX6[get_byte(2, T1)] ^
96 SPBOX7[get_byte(3, T0)] ^ SPBOX8[get_byte(3, T1)];
97 }
98 }
99
100/*************************************************
101* DES Raw Decryption *
102*************************************************/
103void DES::raw_decrypt(u32bit& L, u32bit& R) const
104 {
105 for(u32bit j = 16; j != 0; j -= 2)
106 {
107 u32bit T0, T1;
108
109 T0 = rotate_right(R, 4) ^ round_key[2*j - 2];
110 T1 = R ^ round_key[2*j - 1];
111
112 L ^= SPBOX1[get_byte(0, T0)] ^ SPBOX2[get_byte(0, T1)] ^
113 SPBOX3[get_byte(1, T0)] ^ SPBOX4[get_byte(1, T1)] ^
114 SPBOX5[get_byte(2, T0)] ^ SPBOX6[get_byte(2, T1)] ^
115 SPBOX7[get_byte(3, T0)] ^ SPBOX8[get_byte(3, T1)];
116
117 T0 = rotate_right(L, 4) ^ round_key[2*j - 4];
118 T1 = L ^ round_key[2*j - 3];
119
120 R ^= SPBOX1[get_byte(0, T0)] ^ SPBOX2[get_byte(0, T1)] ^
121 SPBOX3[get_byte(1, T0)] ^ SPBOX4[get_byte(1, T1)] ^
122 SPBOX5[get_byte(2, T0)] ^ SPBOX6[get_byte(2, T1)] ^
123 SPBOX7[get_byte(3, T0)] ^ SPBOX8[get_byte(3, T1)];
124 }
125 }
126
127/*************************************************
128* DES Key Schedule *
129*************************************************/
130void DES::key(const byte key[], u32bit)
131 {
132 static const byte ROT[16] = { 1, 1, 2, 2, 2, 2, 2, 2,
133 1, 2, 2, 2, 2, 2, 2, 1 };
134 u32bit C = ((key[7] & 0x80) << 20) | ((key[6] & 0x80) << 19) |
135 ((key[5] & 0x80) << 18) | ((key[4] & 0x80) << 17) |
136 ((key[3] & 0x80) << 16) | ((key[2] & 0x80) << 15) |
137 ((key[1] & 0x80) << 14) | ((key[0] & 0x80) << 13) |
138 ((key[7] & 0x40) << 13) | ((key[6] & 0x40) << 12) |
139 ((key[5] & 0x40) << 11) | ((key[4] & 0x40) << 10) |
140 ((key[3] & 0x40) << 9) | ((key[2] & 0x40) << 8) |
141 ((key[1] & 0x40) << 7) | ((key[0] & 0x40) << 6) |
142 ((key[7] & 0x20) << 6) | ((key[6] & 0x20) << 5) |
143 ((key[5] & 0x20) << 4) | ((key[4] & 0x20) << 3) |
144 ((key[3] & 0x20) << 2) | ((key[2] & 0x20) << 1) |
145 ((key[1] & 0x20) ) | ((key[0] & 0x20) >> 1) |
146 ((key[7] & 0x10) >> 1) | ((key[6] & 0x10) >> 2) |
147 ((key[5] & 0x10) >> 3) | ((key[4] & 0x10) >> 4);
148 u32bit D = ((key[7] & 0x02) << 26) | ((key[6] & 0x02) << 25) |
149 ((key[5] & 0x02) << 24) | ((key[4] & 0x02) << 23) |
150 ((key[3] & 0x02) << 22) | ((key[2] & 0x02) << 21) |
151 ((key[1] & 0x02) << 20) | ((key[0] & 0x02) << 19) |
152 ((key[7] & 0x04) << 17) | ((key[6] & 0x04) << 16) |
153 ((key[5] & 0x04) << 15) | ((key[4] & 0x04) << 14) |
154 ((key[3] & 0x04) << 13) | ((key[2] & 0x04) << 12) |
155 ((key[1] & 0x04) << 11) | ((key[0] & 0x04) << 10) |
156 ((key[7] & 0x08) << 8) | ((key[6] & 0x08) << 7) |
157 ((key[5] & 0x08) << 6) | ((key[4] & 0x08) << 5) |
158 ((key[3] & 0x08) << 4) | ((key[2] & 0x08) << 3) |
159 ((key[1] & 0x08) << 2) | ((key[0] & 0x08) << 1) |
160 ((key[3] & 0x10) >> 1) | ((key[2] & 0x10) >> 2) |
161 ((key[1] & 0x10) >> 3) | ((key[0] & 0x10) >> 4);
162 for(u32bit j = 0; j != 16; j++)
163 {
164 C = ((C << ROT[j]) | (C >> (28-ROT[j]))) & 0x0FFFFFFF;
165 D = ((D << ROT[j]) | (D >> (28-ROT[j]))) & 0x0FFFFFFF;
166 round_key[2*j ] = ((C & 0x00000010) << 22) | ((C & 0x00000800) << 17) |
167 ((C & 0x00000020) << 16) | ((C & 0x00004004) << 15) |
168 ((C & 0x00000200) << 11) | ((C & 0x00020000) << 10) |
169 ((C & 0x01000000) >> 6) | ((C & 0x00100000) >> 4) |
170 ((C & 0x00010000) << 3) | ((C & 0x08000000) >> 2) |
171 ((C & 0x00800000) << 1) | ((D & 0x00000010) << 8) |
172 ((D & 0x00000002) << 7) | ((D & 0x00000001) << 2) |
173 ((D & 0x00000200) ) | ((D & 0x00008000) >> 2) |
174 ((D & 0x00000088) >> 3) | ((D & 0x00001000) >> 7) |
175 ((D & 0x00080000) >> 9) | ((D & 0x02020000) >> 14) |
176 ((D & 0x00400000) >> 21);
177 round_key[2*j+1] = ((C & 0x00000001) << 28) | ((C & 0x00000082) << 18) |
178 ((C & 0x00002000) << 14) | ((C & 0x00000100) << 10) |
179 ((C & 0x00001000) << 9) | ((C & 0x00040000) << 6) |
180 ((C & 0x02400000) << 4) | ((C & 0x00008000) << 2) |
181 ((C & 0x00200000) >> 1) | ((C & 0x04000000) >> 10) |
182 ((D & 0x00000020) << 6) | ((D & 0x00000100) ) |
183 ((D & 0x00000800) >> 1) | ((D & 0x00000040) >> 3) |
184 ((D & 0x00010000) >> 4) | ((D & 0x00000400) >> 5) |
185 ((D & 0x00004000) >> 10) | ((D & 0x04000000) >> 13) |
186 ((D & 0x00800000) >> 14) | ((D & 0x00100000) >> 18) |
187 ((D & 0x01000000) >> 24) | ((D & 0x08000000) >> 26);
188 }
189 }
190
191/*************************************************
192* TripleDES Encryption *
193*************************************************/
194void TripleDES::enc(const byte in[], byte out[]) const
195 {
196 u32bit L = make_u32bit(in[0], in[1], in[2], in[3]),
197 R = make_u32bit(in[4], in[5], in[6], in[7]);
198
199 DES::IP(L, R);
200 des1.raw_encrypt(L, R);
201 des2.raw_decrypt(R, L);
202 des3.raw_encrypt(L, R);
203 DES::FP(L, R);
204
205 out[0] = get_byte(0, R); out[1] = get_byte(1, R);
206 out[2] = get_byte(2, R); out[3] = get_byte(3, R);
207 out[4] = get_byte(0, L); out[5] = get_byte(1, L);
208 out[6] = get_byte(2, L); out[7] = get_byte(3, L);
209 }
210
211/*************************************************
212* TripleDES Decryption *
213*************************************************/
214void TripleDES::dec(const byte in[], byte out[]) const
215 {
216 u32bit L = make_u32bit(in[0], in[1], in[2], in[3]),
217 R = make_u32bit(in[4], in[5], in[6], in[7]);
218
219 DES::IP(L, R);
220 des3.raw_decrypt(L, R);
221 des2.raw_encrypt(R, L);
222 des1.raw_decrypt(L, R);
223 DES::FP(L, R);
224
225 out[0] = get_byte(0, R); out[1] = get_byte(1, R);
226 out[2] = get_byte(2, R); out[3] = get_byte(3, R);
227 out[4] = get_byte(0, L); out[5] = get_byte(1, L);
228 out[6] = get_byte(2, L); out[7] = get_byte(3, L);
229 }
230
231/*************************************************
232* TripleDES Key Schedule *
233*************************************************/
234void TripleDES::key(const byte key[], u32bit length)
235 {
236 des1.set_key(key, 8);
237 des2.set_key(key + 8, 8);
238 if(length == 24)
239 des3.set_key(key + 16, 8);
240 else
241 des3.set_key(key, 8);
242 }
243
244/*************************************************
245* DESX Encryption *
246*************************************************/
247void DESX::enc(const byte in[], byte out[]) const
248 {
249 xor_buf(out, in, K1.begin(), BLOCK_SIZE);
250 des.encrypt(out);
251 xor_buf(out, K2.begin(), BLOCK_SIZE);
252 }
253
254/*************************************************
255* DESX Decryption *
256*************************************************/
257void DESX::dec(const byte in[], byte out[]) const
258 {
259 xor_buf(out, in, K2.begin(), BLOCK_SIZE);
260 des.decrypt(out);
261 xor_buf(out, K1.begin(), BLOCK_SIZE);
262 }
263
264/*************************************************
265* DESX Key Schedule *
266*************************************************/
267void DESX::key(const byte key[], u32bit)
268 {
269 K1.copy(key, 8);
270 des.set_key(key + 8, 8);
271 K2.copy(key + 16, 8);
272 }
273
274}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status