monotone

monotone Mtn Source Tree

Root/botan/des.cpp

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

Archive Download this file

Branches

Tags

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