Annotation of OpenXM_contrib2/asir2000/io/des.c, Revision 1.1.1.1
1.1 noro 1: /* $OpenXM: OpenXM/src/asir99/io/des.c,v 1.1.1.1 1999/11/10 08:12:30 noro Exp $ */
2: /* Copyright (C) 1996 S.Amada */
3:
4: #include <stdio.h>
5: #include <stdlib.h>
6:
7: #define ROUND 16 /* 段数 */
8:
9: typedef unsigned long ULONG;
10: typedef unsigned short USHORT;
11: typedef unsigned char UCHAR;
12:
13:
14: /* 関数のproto type */
15: void des_enc(ULONG *, UCHAR *, ULONG *);
16: ULONG round_func(ULONG , UCHAR *);
17: ULONG s_box_func(UCHAR *);
18: void des_dec(ULONG *, UCHAR *, ULONG *);
19:
20: /* 鍵の縮約型転置 PC-1 */
21: static USHORT pc_1[56] = {
22: 56, 48, 40, 32, 24, 16, 8,
23: 0, 57, 49, 41, 33, 25, 17,
24: 9, 1, 58, 50, 42, 34, 26,
25: 18, 10, 2, 59, 51, 43, 35,
26: 62, 54, 46, 38, 30, 22, 14,
27: 6, 61, 53, 45, 37, 29, 21,
28: 13, 5, 60, 52, 44, 36, 28,
29: 20, 12, 4, 27, 19, 11, 3
30: };
31:
32: /* 鍵のシフト回数 */
33: static USHORT k_rot[16] = {
34: 1, 2, 4, 6, 8, 10, 12, 14,
35: 15, 17, 19, 21, 23, 25, 27, 28
36: };
37:
38: /* 鍵の縮約型転置 PC-2 */
39: static USHORT pc_2[48] = {
40: 13, 16, 10, 23, 0, 4,
41: 2, 27, 14, 5, 20, 9,
42: 22, 18, 11, 3, 25, 7,
43: 15, 6, 26, 19, 12, 1,
44: 40, 51, 30, 36, 46, 54,
45: 29, 39, 50, 44, 32, 47,
46: 43, 48, 38, 55, 33, 52,
47: 45, 41, 49, 35, 28, 31
48: };
49:
50: /* 初期転置(IP) */
51: static USHORT ip_1[64] = {
52: 57, 49, 41, 33, 25, 17, 9, 1,
53: 59, 51, 43, 35, 27, 19, 11, 3,
54: 61, 53, 45, 37, 29, 21, 13, 5,
55: 63, 55, 47, 39, 31, 23, 15, 7,
56: 56, 48, 40, 32, 24, 16, 8, 0,
57: 58, 50, 42, 34, 26, 18, 10, 2,
58: 60, 52, 44, 36, 28, 20, 12, 4,
59: 62, 54, 46, 38, 30, 22, 14, 6
60: };
61:
62: /* 初期転置(IP^-1) */
63: static USHORT ip_2[64] = {
64: 39, 7, 47, 15, 55, 23, 63, 31,
65: 38, 6, 46, 14, 54, 22, 62, 30,
66: 37, 5, 45, 13, 53, 21, 61, 29,
67: 36, 4, 44, 12, 52, 20, 60, 28,
68: 35, 3, 43, 11, 51, 19, 59, 27,
69: 34, 2, 42, 10, 50, 18, 58, 26,
70: 33, 1, 41, 9, 49, 17, 57, 25,
71: 32, 0, 40, 8, 48, 16, 56, 24
72: };
73:
74: /* 拡大転置(E) */
75: static USHORT f_expand[48] = {
76: 31, 0, 1, 2, 3, 4,
77: 3, 4, 5, 6, 7, 8,
78: 7, 8, 9, 10, 11, 12,
79: 11, 12, 13, 14, 15, 16,
80: 15, 16, 17, 18, 19, 20,
81: 19, 20, 21, 22, 23, 24,
82: 23, 24, 25, 26, 27, 28,
83: 27, 28, 29, 30, 31, 0
84: };
85:
86: /* S-box */
87: static USHORT s_1[64] = {
88: 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
89: 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
90: 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
91: 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
92: };
93:
94: static USHORT s_2[64] = {
95: 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
96: 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
97: 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
98: 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
99: };
100:
101: static USHORT s_3[64] = {
102: 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
103: 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
104: 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
105: 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
106: };
107:
108: static USHORT s_4[64] = {
109: 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
110: 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
111: 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
112: 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
113: };
114:
115: static USHORT s_5[64] = {
116: 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
117: 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
118: 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
119: 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
120: };
121:
122: static USHORT s_6[64] = {
123: 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
124: 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
125: 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
126: 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
127: };
128:
129: static USHORT s_7[64] = {
130: 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
131: 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
132: 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
133: 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
134: };
135:
136: static USHORT s_8[64] = {
137: 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
138: 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
139: 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
140: 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
141: };
142:
143: /* 転置(P) */
144: static USHORT f_perm[32] = {
145: 15, 6, 19, 20,
146: 28, 11, 27, 16,
147: 0, 14, 22, 25,
148: 4, 17, 30, 9,
149: 1, 7, 23, 13,
150: 31, 26, 2, 8,
151: 18, 12, 29, 5,
152: 21, 10, 3, 24
153: };
154:
155:
156: /****************************************************************************
157: * *
158: * 暗号化関数 *
159: * void des_enc(plane,r_key,cipher) *
160: * ULONG *plane : 平文(64 bit) (input) *
161: * UCHAR *r_key : 拡大鍵(48 * 16 bit) (input) *
162: * ULONG *cipher : 暗号文(64 bit) (output) *
163: * *
164: * 機能 : 平文及び拡大鍵を入力とし、DESのアルゴリズムに基づき暗号文 *
165: * を生成する。 *
166: * 戻り値 : なし *
167: * *
168: ****************************************************************************/
169: void des_enc(plane,r_key,cipher)
170: ULONG *plane; /* 平文(64 bit) */
171: UCHAR *r_key; /* 拡大鍵(48 * 16 bit) */
172: ULONG *cipher; /* 暗号文(64 bit) */
173: {
174: ULONG l_text,r_text,
175: l_work,r_work,tmp_text,c_text[2];
176: int loop;
177:
178:
179: l_text = *plane;
180: r_text = *(plane+1);
181:
182: /* 初期転置(IP) */
183: l_work = 0x00000000L;
184: r_work = 0x00000000L;
185: for(loop = 0;loop < 64;loop++)
186: {
187: if(loop < 32)
188: {
189: if(ip_1[loop] < 32)
190: {
191: if(l_text & (0x80000000L >> ip_1[loop]))
192: l_work |= 0x80000000L >> loop;
193: }
194: else
195: {
196: if(r_text & (0x80000000L >> (ip_1[loop] - 32)))
197: l_work |= 0x80000000L >> loop;
198: }
199: }
200: else
201: {
202: if(ip_1[loop] < 32)
203: {
204: if(l_text & (0x80000000L >> ip_1[loop]))
205: r_work |= 0x80000000L >> (loop - 32);
206: }
207: else
208: {
209: if(r_text & (0x80000000L >> (ip_1[loop] - 32)))
210: r_work |= 0x80000000L >> (loop - 32);
211: }
212: }
213: }
214: l_text = l_work;
215: r_text = r_work;
216:
217: #ifdef DEBUG
218: printf("l_text: %08x,r_text: %08x.\n",l_text,r_text);
219: #endif
220:
221: /* 段関数 f(R,K) */
222: for(loop = 0;loop < ROUND;loop++,r_key+=6)
223: {
224: tmp_text = l_text;
225: l_text = r_text;
226: r_text = (ULONG)tmp_text ^ (ULONG)round_func(r_text,r_key);
227: #ifdef DEBUG
228: printf("round[%d] l_text: %08x, r_text: %08x.\n",loop,l_text,r_text);
229: #endif
230: }
231: tmp_text = l_text;
232: l_text = r_text;
233: r_text = tmp_text;
234:
235: /* 最終転置(IP^-1) */
236: l_work = 0x00000000L;
237: r_work = 0x00000000L;
238: for(loop = 0;loop < 64;loop++)
239: {
240: if(loop < 32)
241: {
242: if(ip_2[loop] < 32)
243: {
244: if(l_text & (0x80000000L >> ip_2[loop]))
245: l_work |= 0x80000000L >> loop;
246: }
247: else
248: {
249: if(r_text & (0x80000000L >> (ip_2[loop] - 32)))
250: l_work |= 0x80000000L >> loop;
251: }
252: }
253: else
254: {
255: if(ip_2[loop] < 32)
256: {
257: if(l_text & (0x80000000L >> ip_2[loop]))
258: r_work |= 0x80000000L >> (loop - 32);
259: }
260: else
261: {
262: if(r_text & (0x80000000L >> (ip_2[loop] - 32)))
263: r_work |= 0x80000000L >> (loop - 32);
264: }
265: }
266: }
267: /*l_text = l_work;
268: r_text = r_work;*/
269:
270: #ifdef DEBUG
271: printf("l_text: %08x,r_text: %08x.\n",l_text,r_text);
272: #endif
273:
274: *cipher = l_work;
275: *(cipher+1) = r_work;
276:
277: return;
278: }
279:
280:
281: /****************************************************************************
282: * *
283: * 段関数(f(R,K) *
284: * ULONG round_func(text,key) *
285: * ULONG text : 入力文(32 bit) (input) *
286: * UCHAR *key : 拡大鍵(48 bit) (input) *
287: * *
288: * 機能 : 入力文及び拡大鍵を入力とし、DESの段関数の出力を返す。 *
289: * 戻り値 : 段関数(f(R,K))の出力値 *
290: * *
291: ****************************************************************************/
292: ULONG round_func(text,key)
293: ULONG text;
294: UCHAR *key;
295: {
296: UCHAR e_val[6],k_work[6];
297: ULONG t_work,t_val;
298: int loop;
299:
300:
301: t_work = text;
302:
303: /* 拡大転置(E) 32bit(4byte) -> 48bit(6byte) */
304: memset(e_val,0x00L,6);
305: for(loop = 0;loop < 48;loop++)
306: if(t_work & (0x80000000L >> f_expand[loop]))
307: e_val[loop / 8] |= 0x80L >> (loop % 8);
308:
309: #ifdef DEBUG
310: printf("Expand: %02x%02x%02x%02x%02x%02x.\n",
311: e_val[0],e_val[1],e_val[2],e_val[3],e_val[4],e_val[5]);
312: #endif
313:
314: /* 鍵との排他的論理和演算 48bit(6byte) -> 48bit(6byte) */
315: memset(k_work,0x00L,6);
316: for(loop = 0;loop < 6;loop++,key++)
317: k_work[loop] = e_val[loop] ^ *key;
318:
319: #ifdef DEBUG
320: printf("Key EXORed: %02x%02x%02x%02x%02x%02x.\n",
321: k_work[0],k_work[1],k_work[2],k_work[3],k_work[4],k_work[5]);
322: #endif
323:
324: /* S-box 48bit(6byte) -> 32bit(4byte) */
325: t_work = s_box_func(k_work);
326:
327: #ifdef DEBUG
328: printf("s-box value: %08x.\n",t_work);
329: #endif
330:
331: /* 転置(P) 32bit(4byte) -> 32bit(4byte) */
332: t_val = 0x00000000L;
333: for(loop = 0;loop < 32;loop++)
334: if(t_work & (0x80000000L >> f_perm[loop]))
335: t_val |= 0x80000000L >> loop;
336:
337: #ifdef DEBUG
338: printf("Round func value: %08x.\n",t_val);
339: #endif
340:
341: return(t_val);
342: }
343:
344:
345: /****************************************************************************
346: * *
347: * 段関数(f(R,K) *
348: * ULONG s_box_func(i_data) *
349: * UCHAR *i_data : 入力文(48 bit) (input) *
350: * *
351: * 機能 : 入力文を入力とし、DESのS-Boxの出力値(32 bit)を返す。 *
352: * 戻り値 : S-Boxの出力値 *
353: * *
354: ****************************************************************************/
355: ULONG s_box_func(i_data)
356: UCHAR *i_data;
357: {
358: int loop;
359: UCHAR work[6],s_work[8],val;
360: ULONG fval;
361:
362:
363: for(loop = 0;loop < 6;loop++,i_data++) work[loop] = *i_data;
364:
365: /* S1 */
366: s_work[0] = (work[0] >> 2) & 0x3fL;
367: val = (s_work[0] >> 1) & 0x0fL;
368: if(s_work[0] & 0x20L)
369: {
370: if(s_work[0] & 0x01L) s_work[0] = s_1[val+48];
371: else s_work[0] = s_1[val+32];
372: }
373: else
374: {
375: if(s_work[0] & 0x01L) s_work[0] = s_1[val+16];
376: else s_work[0] = s_1[val];
377: }
378:
379: /* S2 */
380: s_work[1] = ((work[0] << 4) | (work[1] >> 4)) & 0x3fL;
381: val = (s_work[1] >> 1) & 0x0fL;
382: if(s_work[1] & 0x20L)
383: {
384: if(s_work[1] & 0x01L) s_work[1] = s_2[val+48];
385: else s_work[1] = s_2[val+32];
386: }
387: else
388: {
389: if(s_work[1] & 0x01L) s_work[1] = s_2[val+16];
390: else s_work[1] = s_2[val];
391: }
392:
393: /* S3 */
394: s_work[2] = ((work[1] << 2) | (work[2] >> 6)) & 0x3fL;
395: val = (s_work[2] >> 1) & 0x0fL;
396: if(s_work[2] & 0x20L)
397: {
398: if(s_work[2] & 0x01L) s_work[2] = s_3[val+48];
399: else s_work[2] = s_3[val+32];
400: }
401: else
402: {
403: if(s_work[2] & 0x01L) s_work[2] = s_3[val+16];
404: else s_work[2] = s_3[val];
405: }
406:
407: /* S4 */
408: s_work[3] = work[2] & 0x3fL;
409: val = (s_work[3] >> 1) & 0x0fL;
410: if(s_work[3] & 0x20L)
411: {
412: if(s_work[3] & 0x01L) s_work[3] = s_4[val+48];
413: else s_work[3] = s_4[val+32];
414: }
415: else
416: {
417: if(s_work[3] & 0x01L) s_work[3] = s_4[val+16];
418: else s_work[3] = s_4[val];
419: }
420:
421: /* S5 */
422: s_work[4] = (work[3] >> 2) & 0x3fL;
423: val = (s_work[4] >> 1) & 0x0fL;
424: if(s_work[4] & 0x20L)
425: {
426: if(s_work[4] & 0x01L) s_work[4] = s_5[val+48];
427: else s_work[4] = s_5[val+32];
428: }
429: else
430: {
431: if(s_work[4] & 0x01L) s_work[4] = s_5[val+16];
432: else s_work[4] = s_5[val];
433: }
434:
435: /* S6 */
436: s_work[5] = ((work[3] << 4) | (work[4] >> 4)) & 0x3fL;
437: val = (s_work[5] >> 1) & 0x0fL;
438: if(s_work[5] & 0x20L)
439: {
440: if(s_work[5] & 0x01L) s_work[5] = s_6[val+48];
441: else s_work[5] = s_6[val+32];
442: }
443: else
444: {
445: if(s_work[5] & 0x01L) s_work[5] = s_6[val+16];
446: else s_work[5] = s_6[val];
447: }
448:
449: /* S7 */
450: s_work[6] = ((work[4] << 2) | (work[5] >> 6)) & 0x3fL;
451: val = (s_work[6] >> 1) & 0x0fL;
452: if(s_work[6] & 0x20L)
453: {
454: if(s_work[6] & 0x01L) s_work[6] = s_7[val+48];
455: else s_work[6] = s_7[val+32];
456: }
457: else
458: {
459: if(s_work[6] & 0x01L) s_work[6] = s_7[val+16];
460: else s_work[6] = s_7[val];
461: }
462:
463: /* S8 */
464: s_work[7] = work[5] & 0x3fL;
465: val = (s_work[7] >> 1) & 0x0fL;
466: if(s_work[7] & 0x20L)
467: {
468: if(s_work[7] & 0x01L) s_work[7] = s_8[val+48];
469: else s_work[7] = s_8[val+32];
470: }
471: else
472: {
473: if(s_work[7] & 0x01L) s_work[7] = s_8[val+16];
474: else s_work[7] = s_8[val];
475: }
476:
477: fval = (s_work[0] << 28) | (s_work[1] << 24)
478: | (s_work[2] << 20) | (s_work[3] << 16)
479: | (s_work[4] << 12) | (s_work[5] << 8)
480: | (s_work[6] << 4) | s_work[7];
481:
482: return(fval);
483: }
484:
485:
486: /****************************************************************************
487: * *
488: * 復号関数 *
489: * void des_dec(cipher,r_key,plane) *
490: * ULONG *cipher : 暗号文(64 bit) (output) *
491: * UCHAR *r_key : 拡大鍵(48 * 16 bit) (input) *
492: * ULONG *plane : 平文(64 bit) (input) *
493: * *
494: * 機能 : 暗号文及び拡大鍵を入力とし、DESのアルゴリズムに基づき平文 *
495: * を生成する。 *
496: * 戻り値 : なし *
497: * *
498: ****************************************************************************/
499: void des_dec(cipher,r_key,plane)
500: ULONG *cipher;
501: UCHAR *r_key;
502: ULONG *plane;
503: {
504: ULONG l_text,r_text,
505: l_work,r_work,tmp_text;
506: UCHAR *tmp_k;
507: int loop;
508:
509:
510: l_text = *cipher;
511: r_text = *(cipher+1);
512:
513:
514: /* 初期転置(IP) */
515: l_work = 0x00000000L;
516: r_work = 0x00000000l;
517: for(loop = 0;loop < 64;loop++)
518: {
519: if(loop < 32)
520: {
521: if(ip_1[loop] < 32)
522: {
523: if(l_text & (0x80000000L >> ip_1[loop]))
524: l_work |= 0x80000000L >> loop;
525: }
526: else
527: {
528: if(r_text & (0x80000000L >> (ip_1[loop] - 32)))
529: l_work |= 0x80000000L >> loop;
530: }
531: }
532: else
533: {
534: if(ip_1[loop] < 32)
535: {
536: if(l_text & (0x80000000L >> ip_1[loop]))
537: r_work |= 0x80000000l >> (loop - 32);
538: }
539: else
540: {
541: if(r_text & (0x80000000L >> (ip_1[loop] - 32)))
542: r_work |= 0x80000000L >> (loop - 32);
543: }
544: }
545: }
546: l_text = l_work;
547: r_text = r_work;
548:
549: /* 段関数 f(R,K) */
550: tmp_k = r_key+90;
551: for(loop = 0;loop < ROUND;loop++,tmp_k-=6)
552: {
553: tmp_text = l_text;
554: l_text = r_text;
555: r_text = tmp_text ^ round_func(r_text,tmp_k);
556:
557: #ifdef DEBUG
558: printf("round[%d] l_text: %08x, r_text: %08x.\n",loop,l_text,r_text);
559: #endif
560:
561: }
562: tmp_text = l_text;
563: l_text = r_text;
564: r_text = tmp_text;
565:
566: /* 最終転置(IP^-1) */
567: l_work = 0x00000000L;
568: r_work = 0x00000000L;
569: for(loop = 0;loop < 64;loop++)
570: {
571: if(loop < 32)
572: {
573: if(ip_2[loop] < 32)
574: {
575: if(l_text & (0x80000000L >> ip_2[loop]))
576: l_work |= 0x80000000L >> loop;
577: }
578: else
579: {
580: if(r_text & (0x80000000L >> (ip_2[loop] - 32)))
581: l_work |= 0x80000000L >> loop;
582: }
583: }
584: else
585: {
586: if(ip_2[loop] < 32)
587: {
588: if(l_text & (0x80000000L >> ip_2[loop]))
589: r_work |= 0x80000000L >> (loop - 32);
590: }
591: else
592: {
593: if(r_text & (0x80000000L >> (ip_2[loop] - 32)))
594: r_work |= 0x80000000L >> (loop - 32);
595: }
596: }
597: }
598: l_text = l_work;
599: r_text = r_work;
600:
601: *plane = l_text;
602: *(plane+1) = r_text;
603:
604: return;
605: }
606:
607:
608: /****************************************************************************
609: * *
610: * 鍵スケジュール関数 *
611: * void key_schedule(key) *
612: * UCHAR *key : 暗号化鍵(64 bit) (input) *
613: * *
614: * 機能 : 暗号化鍵を入力とし、DESの鍵スケジュールアルゴリズムに基づ *
615: * き段毎の拡大鍵を生成し、大域変数(EX_KEY[96])に格納する。 *
616: * 戻り値 : なし *
617: * *
618: ****************************************************************************/
619: void key_schedule(key,ex_key)
620: UCHAR *key,*ex_key;
621: {
622: UCHAR r_key[6],*k_p;
623: ULONG k_work1,k_work2,
624: c_key,d_key,
625: c_tmp,d_tmp;
626: int loop,loop2,len,strcnt;
627:
628:
629: k_work1 = 0x00000000L;
630: k_work2 = 0x00000000L;
631: k_work1 |= ((ULONG)*key << 24) | ((ULONG)*(key+1) << 16)
632: | ((ULONG)*(key+2) << 8) | (ULONG)*(key+3);
633: k_work2 |= ((ULONG)*(key+4) << 24) | ((ULONG)*(key+5) << 16)
634: | ((ULONG)*(key+6) << 8) | (ULONG)*(key+7);
635:
636: /* 鍵の縮約転置(PC-1) */
637: c_key = 0x00000000L;
638: d_key = 0x00000000L;
639: for(loop = 0;loop < 56;loop++)
640: {
641: if(loop < 28)
642: {
643: if(pc_1[loop] < 32)
644: {
645: if(k_work1 & (0x80000000L >> pc_1[loop]))
646: c_key |= 0x80000000L >> loop;
647: }
648: else
649: {
650: if(k_work2 & (0x80000000L >> pc_1[loop] - 32))
651: c_key |= 0x80000000L >> loop;
652: }
653: }
654: else
655: {
656: if(pc_1[loop] < 32)
657: {
658: if(k_work1 & (0x80000000L >> pc_1[loop]))
659: d_key |= 0x80000000L >> (loop - 28);
660: }
661: else
662: {
663: if(k_work2 & (0x80000000L >> pc_1[loop] - 32))
664: d_key |= 0x80000000L >> (loop - 28);
665: }
666: }
667: }
668:
669: #ifdef DEBUG
670: printf("c: %08x,d: %08x.\n",c_key,d_key);
671: #endif
672:
673: k_p = ex_key;
674: for(loop = 0;loop < 16;loop++,k_p+=6)
675: {
676: /* 鍵のシフト */
677: c_tmp = 0x00000000L;
678: d_tmp = 0x00000000L;
679: c_tmp = (c_key << k_rot[loop]) | (c_key >> (28-k_rot[loop]));
680: d_tmp = (d_key << k_rot[loop]) | (d_key >> (28-k_rot[loop]));
681:
682: /* 鍵の縮約型転置(PC-2) */
683: memset(r_key,0x00L,6);
684: for(loop2 = 0;loop2 < 48;loop2++)
685: {
686: if(pc_2[loop2] < 28)
687: {
688: if(c_tmp & (0x80000000L >> pc_2[loop2]))
689: r_key[loop2 / 8] |= 0x80 >> (loop2 % 8);
690: }
691: else
692: {
693: if(d_tmp & (0x80000000L >> pc_2[loop2]-28))
694: r_key[loop2 / 8] |= (0x80 >> (loop2 % 8));
695: }
696: }
697:
698: memcpy(k_p,r_key,6);
699:
700: #ifdef DEBUG
701: printf("key[%d]: %02x %02x %02x %02x %02x %02x\n",
702: loop,r_key[0],r_key[1],r_key[2],r_key[3],r_key[4],r_key[5]) ;
703: #endif
704: }
705: }
706:
707: #if 0
708: /* 動作確認用main */
709: void main(void)
710: {
711: ULONG p_text[2],c_text[2];
712: UCHAR key[8] =
713: /*{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};*/
714: /*{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};*/
715: { 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
716: /*{ 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};*/
717: /*{ 0x03,0x96,0x49,0xc5,0x39,0x31,0x39,0x65};*/
718: int loop;
719:
720:
721: /*p_text[0] = 0x00000000;
722: p_text[1] = 0x00000000;*/
723: p_text[0] = 0x01234567L;
724: p_text[1] = 0x89abcde7L;
725: /*p_text[0] = 0xffffffff;
726: p_text[1] = 0xffffffff;*/
727:
728: /*printf("plane: %08x%08x.\n",p_text[0],p_text[1]);*/
729:
730:
731: for(loop =0;loop < 1024*10;loop++)
732: {
733: key_schedule(key);
734: des_enc(p_text,EX_KEY,c_text);
735: /*printf("cipher: %08x%08x.\n\n",c_text[0],c_text[1]);*/
736:
737: /*p_text[0] = c_text[0];
738: p_text[1] = c_text[1];*/
739: }
740:
741: /*des_dec(c_text,EX_KEY,p_text);
742: printf("plane: %08x%08x.\n",p_text[0],p_text[1]);*/
743:
744: }
745: #endif
746:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>