FreeCalypso > hg > freecalypso-citrine
comparison comlib/cl_des.c @ 0:75a11d740a02
initial import of gsm-fw from freecalypso-sw rev 1033:5ab737ac3ad7
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 09 Jun 2016 00:02:41 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:75a11d740a02 |
---|---|
1 /* | |
2 +----------------------------------------------------------------------------- | |
3 | Project : COMLIB | |
4 | Modul : cl_des.c | |
5 +----------------------------------------------------------------------------- | |
6 | Copyright 2002 Texas Instruments Berlin, AG | |
7 | All rights reserved. | |
8 | | |
9 | This file is confidential and a trade secret of Texas | |
10 | Instruments Berlin, AG | |
11 | The receipt of or possession of this file does not convey | |
12 | any rights to reproduce or disclose its contents or to | |
13 | manufacture, use, or sell anything it may describe, in | |
14 | whole, or in part, without the specific written consent of | |
15 | Texas Instruments Berlin, AG. | |
16 +----------------------------------------------------------------------------- | |
17 | Purpose : Definitions of common library functions: Implementation of | |
18 DES algorithm | |
19 +----------------------------------------------------------------------------- | |
20 */ | |
21 /* | |
22 * Version 1.0 | |
23 */ | |
24 | |
25 /**********************************************************************************/ | |
26 | |
27 /* | |
28 NOTE: | |
29 */ | |
30 | |
31 /**********************************************************************************/ | |
32 | |
33 #ifndef CL_DES_C | |
34 #define CL_DES_C | |
35 | |
36 #include <string.h> | |
37 #include "typedefs.h" | |
38 #include "cl_des.h" | |
39 | |
40 | |
41 /* 64+64+17*56+16*48+64+17*32+17*32 = 3000 bytes */ | |
42 static UBYTE binmsg[64] , binkey[64], cd[17][56] , deskey[16][48] , ip[64]; | |
43 static UBYTE l[17][32] , r[17][32]; | |
44 /* 64+64+32+32+64+64+17*3+2 = 373 bytes */ | |
45 static UBYTE rnew[64] , xorres[64] , scale[32] , perm[32] , rl[64] , encpt[64]; | |
46 | |
47 /* 64+16+48+64+48+32+64+8*66 = 864 bytes */ | |
48 static | |
49 const UBYTE shtamt[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1}; | |
50 static | |
51 const UBYTE iporder[64] = {58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54, | |
52 46,38,30,22,14,6,64,56,48,40,32,24,16,8,57,49,41,33, | |
53 25,17,9,1,59,51,43,35,27,19,11,3,61,53,45,37,29,21, | |
54 13,5,63,55,47,39,31,23,15,7}; | |
55 static | |
56 const UBYTE pc1[64] = {57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43, | |
57 35,27,19,11,3,60,52,44,36,63,55,47,39,31,23,15,7,62,54, | |
58 46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4}; | |
59 static | |
60 const UBYTE pc2[48] = {14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8, | |
61 16,7,27,20,13,2,41,52,31,37,47,55,30,40,51,45,33, | |
62 48,44,49,39,56,34,53,46,42,50,36,29,32}; | |
63 static | |
64 const UBYTE e[48] = {32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15, | |
65 16,17,16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27, | |
66 28,29,28, 29,30,31,32,1}; | |
67 static | |
68 const UBYTE sp[32] = {16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10, | |
69 2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25}; | |
70 static | |
71 const UBYTE ipinv[64] = {40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46, | |
72 14,54,22,62,30,37,5,45,13,53,21,61,29,36,4,44,12,52, | |
73 20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,50,18,58, | |
74 26,33,1,41,9,49,17,57,25}; | |
75 static | |
76 const UBYTE s[8][66] = {{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,0,15,7,4,14,2,13, | |
77 1,10,6,12,11,9,5,3,8,4,1,14,8,13,6,2,11,15,12,9,7,3,10, | |
78 5,0,15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}, | |
79 {15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,3,13,4,7,15,2,8, | |
80 14,12,0,1,10,6,9,11,5,0,14,7,11,10,4,13,1,5,8,12,6,9,3, | |
81 2,15,13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}, | |
82 {10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,13,7,0,9,3,4,6, | |
83 10,2,8,5,14,12,11,15,1,13,6,4,9,8,15,3,0,11,1,2,12,5, | |
84 10,14,7,1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12 }, | |
85 {7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,13,8,11,5,6,15, | |
86 0,3,4,7,2,12,1,10,14,9,10,6,9,0,12,11,7,13,15,1,3,14, | |
87 5,2,8,4,3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14 }, | |
88 {2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,14,11,2,12,4,7, | |
89 13,1,5,0,15,10,3,9,8,6,4,2,1,11,10,13,7,8,15,9,12,5,6, | |
90 3,0,14,11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3 }, | |
91 {12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,10,15,4,2,7,12, | |
92 9,5,6,1,13,14,0,11,3,8,9,14,15,5,2,8,12,3,7,0,4,10,1, | |
93 13,11,6,4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13 }, | |
94 {4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,13,0,11,7,4,9,1, | |
95 10,14,3,5,12,2,15,8,6,1,4,11,13,12,3,7,14,10,15,6,8,0, | |
96 5,9,2,6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12 }, | |
97 {13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,1,15,13,8,10,3,7, | |
98 4,12,5,6,11,0,14,9,2,7,11,4,1,9,12,14,2,0,6,10,13,15,3, | |
99 5,8,2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11 }}; | |
100 | |
101 | |
102 /*==== FUNCTIONS ==================================================*/ | |
103 | |
104 /* | |
105 +------------------------------------------------------------------------------ | |
106 | Function : des_hex2bin4 | |
107 +------------------------------------------------------------------------------ | |
108 | Description : The function converts a 4 bit hex value to 4 binary values | |
109 | | |
110 | Parameters : hex : value in hex | |
111 | m : pointer to buffer of 4 elements to store binary values | |
112 +------------------------------------------------------------------------------ | |
113 */ | |
114 LOCAL void des_hex2bin4(UBYTE hex, UBYTE *m) | |
115 { | |
116 m[0] = (hex & 0x08) >> 3; | |
117 m[1] = (hex & 0x04) >> 2; | |
118 m[2] = (hex & 0x02) >> 1; | |
119 m[3] = hex & 0x01; | |
120 } | |
121 | |
122 /* | |
123 +------------------------------------------------------------------------------ | |
124 | Function : des_hex2bin8 | |
125 +------------------------------------------------------------------------------ | |
126 | Description : The function converts a 8 bit hex value to 8 binary values | |
127 | | |
128 | Parameters : hex : value in hex | |
129 | m : pointer to buffer of 8 elements to store binary values | |
130 +------------------------------------------------------------------------------ | |
131 */ | |
132 LOCAL void des_hex2bin8(UBYTE hex, UBYTE *m) | |
133 { | |
134 m[0] = (hex & 0x80) >> 7; | |
135 m[1] = (hex & 0x40) >> 6; | |
136 m[2] = (hex & 0x20) >> 5; | |
137 m[3] = (hex & 0x10) >> 4; | |
138 m[4] = (hex & 0x08) >> 3; | |
139 m[5] = (hex & 0x04) >> 2; | |
140 m[6] = (hex & 0x02) >> 1; | |
141 m[7] = hex & 0x01; | |
142 } | |
143 | |
144 /* | |
145 +------------------------------------------------------------------------------ | |
146 | Function : des_bin2hex | |
147 +------------------------------------------------------------------------------ | |
148 | Description : The function converts 8 bin values to an 8 bit hex value | |
149 | | |
150 | Parameters : m[8] : input bin values | |
151 | Return : converted hex value | |
152 +------------------------------------------------------------------------------ | |
153 */ | |
154 | |
155 LOCAL UBYTE des_bin2hex(UBYTE *m) | |
156 { | |
157 UBYTE hex; | |
158 return hex = (m[0]<<7) | (m[1]<<6) | (m[2]<<5) | (m[3]<<4) | | |
159 (m[4]<<3) | (m[5]<<2) | (m[6]<<1) | m[7]; | |
160 } | |
161 | |
162 | |
163 | |
164 /* | |
165 +------------------------------------------------------------------------------ | |
166 | Function : des_shift | |
167 +------------------------------------------------------------------------------ | |
168 | Description : The function performs shifting | |
169 | | |
170 | Parameters : dst : pointer to destination buffer | |
171 | src : pointer to source buffer | |
172 | sht : shift value | |
173 +------------------------------------------------------------------------------ | |
174 */ | |
175 | |
176 LOCAL void des_shift(UBYTE *dst, UBYTE *src, UBYTE sht) | |
177 { | |
178 UBYTE c1 , c2 , d1 , d2; | |
179 int i; | |
180 | |
181 c1 = src[0]; | |
182 c2 = src[1]; | |
183 d1 = src[28]; | |
184 d2 = src[29]; | |
185 | |
186 for ( i = 0 ; i < 28 - sht ; i++) { | |
187 dst[i] = src[i + sht]; /* copying c[i] */ | |
188 dst[28 + i] = src[28 + i + sht]; /* copying d[i] */ | |
189 } | |
190 | |
191 if (sht == 1){ | |
192 dst[27] = c1; | |
193 dst[55] = d1; | |
194 } else { | |
195 dst[26] = c1; | |
196 dst[27] = c2; | |
197 dst[54] = d1; | |
198 dst[55] = d2; | |
199 } | |
200 } | |
201 | |
202 /* | |
203 +------------------------------------------------------------------------------ | |
204 | Function : des_indx | |
205 +------------------------------------------------------------------------------ | |
206 | Description : The function generates index for S table | |
207 | | |
208 | Parameters : m[6] : | |
209 | Return : index value | |
210 +------------------------------------------------------------------------------ | |
211 */ | |
212 LOCAL UBYTE des_indx(UBYTE *m) | |
213 { | |
214 return( (((m[0]<<1) + m[5])<<4) + ((m[1]<<3) + (m[2]<<2) + (m[3]<<1) + m[4])); | |
215 } | |
216 | |
217 /* | |
218 +------------------------------------------------------------------------------ | |
219 | Function : cl_des | |
220 +------------------------------------------------------------------------------ | |
221 | Description : The function performs DES encrypting or decrypting | |
222 | | |
223 | Parameters : inMsgPtr : pointer to input message M. The length of message | |
224 | has to be min. 8 bytes e.g. M = 0123456789abcdef | |
225 | desKeyPtr : pointer to DES key. Length has to be 8 bytes | |
226 | outMsgPtr : output encrypted/decrypted message. The length is 8 b. | |
227 | code : CL_DES_ENCRYPTION, CL_DES_DECRYPTION | |
228 +------------------------------------------------------------------------------ | |
229 */ | |
230 EXTERN void cl_des(UBYTE *inMsgPtr, UBYTE *desKeyPtr, UBYTE *outMsgPtr, UBYTE code) | |
231 { | |
232 | |
233 int y , z , g; | |
234 UBYTE temp, more; | |
235 | |
236 /* | |
237 * convert message from hex to bin format | |
238 */ | |
239 for(y = 0; y < 8; y++){ | |
240 des_hex2bin8(inMsgPtr[y], &binmsg[8 * y]); | |
241 } | |
242 | |
243 /* | |
244 * Convert DES key value from hex to bin format | |
245 */ | |
246 for( y = 0; y < 8; y++){ | |
247 des_hex2bin8(desKeyPtr[y], &binkey[8 * y]); | |
248 } | |
249 | |
250 /* | |
251 * Step 1: Create 16 subkeys, each of which is 48-bits long. | |
252 * | |
253 * The 64-bit key is permuted according to the table pc1, | |
254 * to get the 56 bit subkey K+. The subkey K+ consists of left | |
255 * and right halves C0 and D0, where each half has 28 bits. | |
256 */ | |
257 for(y = 0 ; y < 56 ; y++) | |
258 cd[0][y] = binkey[pc1[y] - 1]; | |
259 /* | |
260 * Create futher 15 subkeys C1-C16 and D1-D16 by left shifts of | |
261 * each previous key, i.e. C2 and D2 are obtained from C1 and D1 and so on. | |
262 */ | |
263 for(y = 0 ; y < 16 ; y++) | |
264 des_shift(cd[y + 1] , cd[y] , shtamt[y]); | |
265 | |
266 /* | |
267 * Form the keys K1-K16 by applying the pc2 permutation | |
268 * table to each of the concatenated pairs CnDn. | |
269 */ | |
270 for(y = 0; y < 16; y++){ | |
271 for(z = 0 ; z < 48 ; z++){ | |
272 deskey[y][z] = cd[y + 1][pc2[z] - 1]; | |
273 } | |
274 } | |
275 | |
276 /* | |
277 * Step 2: Encode each 64-bit block of data | |
278 * | |
279 * Perform initial permutation IP of th e64 bits the message data M. | |
280 * This rearranges the bits according to the iporder table. | |
281 */ | |
282 for(y = 0; y < 64; y++) | |
283 ip[y] = binmsg[iporder[y] - 1]; | |
284 | |
285 /* | |
286 * Divide the permuted block IP into left half L0 | |
287 * and a right half R0 each of 32 bits. | |
288 */ | |
289 for(y = 0; y < 32; y++){ | |
290 l[0][y] = ip[y]; | |
291 r[0][y] = ip[y + 32]; | |
292 } | |
293 | |
294 /* | |
295 * Proceed through 16 iterations, operation on two blocks: | |
296 * a data block of 32 bits and a key Kn of 48 bits to produce a block of 32 | |
297 * bits. This results in a final block L16R16. In each iteration, we take | |
298 * the right 32 bits of the previous result and make them the left 32 bits | |
299 * of the current step. For the right 32 bits in the current step, we XOR | |
300 * the left 32 bits of the previous step. | |
301 */ | |
302 for (y = 0; y < 16; y++){ | |
303 if (code == CL_DES_ENCRYPTION)/* encryption */ | |
304 g = y; | |
305 else /* decryption */ | |
306 g = 15 - y; | |
307 | |
308 /* | |
309 * Copie the right bits Rn of the current step | |
310 * to the left bits Ln+1 of the next step | |
311 */ | |
312 for(z = 0; z < 32; z++) | |
313 l[y + 1][z] = r[y][z]; | |
314 | |
315 /* | |
316 * Expand the block Rn from 32 to 48 bits by using the selection table E. | |
317 * Then XOR the result with the key Kn+1. | |
318 */ | |
319 for(z = 0; z < 48; z++){ | |
320 rnew[z] = r[y][e[z] - 1]; | |
321 xorres[z] = (rnew[z] ^ deskey[g][z]); | |
322 } | |
323 | |
324 /* | |
325 * We now have 48 bits, or eight groups of six bits. We use them as | |
326 * addresses in tables calle "S boxes". Each group of six bits will | |
327 * give us an address in a different S box. | |
328 */ | |
329 for(z = 0; z < 8; z++){ | |
330 temp = s[z][des_indx(&xorres[z * 6])]; | |
331 des_hex2bin4(temp, &scale[z * 4]); | |
332 } | |
333 | |
334 /* | |
335 * Perform a permutation P of the S box output. | |
336 */ | |
337 for(z = 0; z < 32; z++) | |
338 perm[z] = scale[sp[z] - 1]; | |
339 | |
340 /* | |
341 * XOR the result with the left half of current step | |
342 * and copie it to the right half of the next step | |
343 */ | |
344 for(z = 0; z < 32; z++) | |
345 r[y+1][z] = (l[y][z] ^ perm[z]); | |
346 } | |
347 | |
348 /* | |
349 * Reserve the order of the final block L16R16 to R16L16 | |
350 */ | |
351 for( z = 0; z < 32; z++){ | |
352 rl[z] = r[16][z]; | |
353 rl[z + 32] = l[16][z]; | |
354 } | |
355 | |
356 /* | |
357 * Apply the final inverse permutation IP | |
358 */ | |
359 for( z = 0; z < 64; z++){ | |
360 encpt[z] = rl[ipinv[z] - 1]; | |
361 } | |
362 | |
363 /* | |
364 * Convert from bin to hex format | |
365 */ | |
366 for(z = 0; z < 8; z++){ | |
367 outMsgPtr[z] = des_bin2hex(&encpt[8 * z]); | |
368 } | |
369 } | |
370 #endif /* CL_DES_C */ |