Annotation of OpenXM/src/ox_ntl/crypt/des/des.c, Revision 1.2
1.2 ! iwane 1: /* $OpenXM: OpenXM/src/ox_ntl/crypt/des/des.c,v 1.1 2004/07/11 00:32:17 iwane Exp $ */
1.1 iwane 2: /*
3: * FIPS PUB 46-3
4: * DATA ENCRYPTION STANDARD
5: */
6: #include <stdio.h>
7:
8: #include "des.h"
9: #include "block.h"
10:
1.2 ! iwane 11: #ifdef HAVE_CONFIG_H
! 12: #include "config.h"
! 13: #endif
! 14:
1.1 iwane 15: #define BLOCK 8
16:
17:
18: /*===========================================================*
19: * KEY STRUCT
20: *===========================================================*/
21: static int inline
22: keychk0(uint32_t n)
23: {
24: int i, j, c;
25:
26: for (i = 0; i < 4; i++) {
27: c = 0;
28: for (j = 0; j < 8; j++) {
29: if ((n >> (8 * i + j)) & 1)
30: c++;
31: }
32: if (c % 2 == 0)
33: return (0);
34: }
35: return (1);
36: }
37:
38: int
39: des_keychk(des_key *key)
40: {
41: return (keychk0(key->key.ui[0]) & keychk0(key->key.ui[1]));
42: }
43:
44: void
45: des_keyset(const unsigned char *key, des_key *dkey)
46: {
47: uint32_t k;
48: int i, j;
49:
50: for (j = 0; j < 2; j++) {
51: k = 0;
52: for (i = 0; i < 4; i++) {
53: k |= (key[i + 4 * j] & 0xff) << (8 * (3 - i));
54: }
55: dkey->key.ui[j] = k;
56: }
57:
58: }
59:
60:
61: #ifdef DES_DEBUG
62: /*===========================================================*
63: * DEBUG
64: *===========================================================*/
65: static void
66: pr(uint32_t n)
67: {
68: int i;
69:
70: for (i = 31; i >= 0; i--) {
71: printf("%d", (n >> i) & 1);
72: if (i % 8 == 0)
73: printf(" ");
74: }
75: }
76: #endif
77:
78:
79: /*===========================================================*
80: * SHIFT
81: *===========================================================*/
82: static const int SHIFT_IDX[] = {
83: 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,
84: };
85:
86: static inline uint32_t
87: lshift28(uint32_t x, int c)
88: {
89: int n = SHIFT_IDX[c];
90: return (((x << n) | (x >> (28 - n))) & 0x0fffffff);
91: }
92:
93: static inline uint32_t
94: rshift28(uint32_t x, int c)
95: {
96: int n = SHIFT_IDX[15 - c];
97: return (((x >> n) | (x << (28 - n))) & 0x0fffffff);
98: }
99:
100:
101:
102: /*===========================================================*
103: * TRANS
104: *===========================================================*/
105: static void
106: trans(uint32_t li, uint32_t ri, uint32_t *lo, uint32_t *ro, const int *index, int m)
107: {
108: int idx, n;
109: uint32_t a[2];
110: uint32_t r, l;
111: const static int N = 32;
112:
113: a[0] = li;
114: a[1] = ri;
115:
116: l = 0;
117: r = 0;
118: for (n = 0; n < N; n++) {
119: idx = index[n];
120: l |= ((a[idx / N] >> (N - (idx % N) - 1)) & 1) << (N - n - 1);
121: }
122:
123: for (; n < m; n++) {
124: idx = index[n];
125: r |= ((a[idx / N] >> (N - (idx % N) - 1)) & 1) << (2 * N - n - 1);
126: }
127:
128: *lo = l;
129: *ro = r;
130: }
131:
132:
133:
134:
135: /*
136: * input 64 bit 32+32
137: * output 64 bit 32+32
138: */
139: static void
140: ip(uint32_t li, uint32_t ri, uint32_t *lo, uint32_t *ro)
141: {
142: static const int INDEX[] = {
143: 57, 49, 41, 33, 25, 17, 9, 1,
144: 59, 51, 43, 35, 27, 19, 11, 3,
145: 61, 53, 45, 37, 29, 21, 13, 5,
146: 63, 55, 47, 39, 31, 23, 15, 7,
147: 56, 48, 40, 32, 24, 16, 8, 0,
148: 58, 50, 42, 34, 26, 18, 10, 2,
149: 60, 52, 44, 36, 28, 20, 12, 4,
150: 62, 54, 46, 38, 30, 22, 14, 6,
151: };
152:
153: trans(li, ri, lo, ro, INDEX, 64);
154: }
155:
156:
157: /*
158: * input 64 bit 32+32
159: * output 64 bit 32+32
160: */
161: static void
162: ip_inv(uint32_t li, uint32_t ri, uint32_t *lo, uint32_t *ro)
163: {
164: static const int INDEX[] = {
165: 39, 7, 47, 15, 55, 23, 63, 31,
166: 38, 6, 46, 14, 54, 22, 62, 30,
167: 37, 5, 45, 13, 53, 21, 61, 29,
168: 36, 4, 44, 12, 52, 20, 60, 28,
169: 35, 3, 43, 11, 51, 19, 59, 27,
170: 34, 2, 42, 10, 50, 18, 58, 26,
171: 33, 1, 41, 9, 49, 17, 57, 25,
172: 32, 0, 40, 8, 48, 16, 56, 24,
173: };
174:
175: trans(li, ri, lo, ro, INDEX, 64);
176: }
177:
178:
179:
180: /* PC1
181: * input 64 bit 32+32
182: * output 56 bit 28+28
183: */
184: static void
185: ktrans1(uint32_t li, uint32_t ri, uint32_t *lo, uint32_t *ro)
186: {
187: static const int INDEX[] = {
188: 0, 0, 0, 0,
189: 56, 48, 40, 32, 24, 16, 8,
190: 0, 57, 49, 41, 33, 25, 17,
191: 9, 1, 58, 50, 42, 34, 26,
192: 18, 10, 2, 59, 51, 43, 35,
193: 0, 0, 0, 0,
194: 62, 54, 46, 38, 30, 22, 14,
195: 6, 61, 53, 45, 37, 29, 21,
196: 13, 5, 60, 52, 44, 36, 28,
197: 20, 12, 4, 27, 19, 11, 3,
198: };
199:
200: trans(li, ri, lo, ro, INDEX, 64);
201:
202: *lo &= 0x0fffffff;
203: *ro &= 0x0fffffff;
204: }
205:
206:
207: /* PC2
208: * Input 56 bit = 28+28
209: * Output 48 bit = 32+16
210: */
211: static void
212: ktrans2(uint32_t li, uint32_t ri, uint32_t *lo, uint32_t *ro)
213: {
214: uint32_t l, r;
215: static const int CTRANS2_INDEX[64] = {
216: 13, 16, 10, 23, 0, 4,
217: 2, 27, 14, 5, 20, 9,
218: 22, 18, 11, 3, 25, 7,
219: 15, 6, 26, 19, 12, 1,
220: 40, 51, 30, 36, 46, 54,
221: 29, 39, 50, 44, 32, 47,
222: 43, 48, 38, 55, 33, 52,
223: 45, 41, 49, 35, 28, 31,
224: };
225:
226: l = (li << 4) | ((ri >> 24) & 0xf);
227: r = ri << 8;
228:
229: trans(l, r, lo, ro, CTRANS2_INDEX, 48);
230:
231: *ro &= 0xffff0000;
232: }
233:
234: /*
235: * input 32 bit = 32
236: * output 48 bit = 32 + 16
237: */
238: static void
239: etrans1(uint32_t li, uint32_t *lo, uint32_t *ro)
240: {
241: uint32_t dummy = 0;
242: static const int INDEX[64] = {
243: 31, 0, 1, 2, 3, 4,
244: 3, 4, 5, 6, 7, 8,
245: 7, 8, 9, 10, 11, 12,
246: 11, 12, 13, 14, 15, 16,
247: 15, 16, 17, 18, 19, 20,
248: 19, 20, 21, 22, 23, 24,
249: 23, 24, 25, 26, 27, 28,
250: 27, 28, 29, 30, 31, 0,
251: };
252:
253: trans(li, dummy, lo, ro, INDEX, 48);
254: }
255:
256:
257: /*
258: * input 32 bit = 32
259: * output 32 bit = 32
260: */
261: static uint32_t
262: etrans2(uint32_t n)
263: {
264: static const int INDEX[64] = {
265: 15, 6, 19, 20,
266: 28, 11, 27, 16,
267: 0, 14, 22, 25,
268: 4, 17, 30, 9,
269: 1, 7, 23, 13,
270: 31, 26, 2, 8,
271: 18, 12, 29, 5,
272: 21, 10, 3, 24,
273: };
274:
275: uint32_t p, b;
276:
277: trans(n, 0, &p, &b, INDEX, 48);
278:
279: return (p);
280: }
281:
282: /*
283: * input : 6 bit
284: * oiutput: 6 bit
285: */
286: static uint32_t
287: strans1(uint32_t s, int n)
288: {
289: static const int S[][4 * 16] = {
290: {
291: 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
292: 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
293: 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
294: 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
295: }, {
296: 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
297: 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
298: 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
299: 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
300: }, {
301: 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
302: 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
303: 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
304: 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
305: }, {
306: 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
307: 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
308: 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
309: 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
310: }, {
311: 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
312: 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
313: 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
314: 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
315: }, {
316: 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
317: 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
318: 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
319: 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
320: }, {
321: 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
322: 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
323: 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
324: 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
325: }, {
326: 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
327: 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
328: 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
329: 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11,
330: },
331: };
332:
333: int x, y;
334:
335: x = (((s >> 5) & 1) << 1) | (s & 1);
336: y = (s >> 1) & 0xf;
337:
338: return (S[n][16 * x + y]);
339:
340: }
341:
342: /*
343: * input : 48 bit
344: * output: 32 bit
345: */
346: static void
347: strans(uint32_t li, uint32_t ri, uint32_t *out)
348: {
349: int i;
350: int m;
351:
352: uint32_t s[8];
353: uint32_t ui;
354:
355: /* ... X-( */
356: s[0] = li >> 26;
357: s[1] = (li >> 20) & 0x3f;
358: s[2] = (li >> 14) & 0x3f;
359: s[3] = (li >> 8) & 0x3f;
360: s[4] = (li >> 2) & 0x3f;
361: s[5] = ((li & 0x3) << 4) | (ri >> 28);
362: s[6] = (ri >> 22) & 0x3f;
363: s[7] = (ri >> 16) & 0x3f;
364:
365: ui = 0;
366: for (i = 0; i < 8; i++) {
367: m = strans1(s[i], i);
368: ui |= m << 4 * (7 - i);
369: }
370:
371: *out = ui;
372: }
373:
374:
375: /*
376: * input : r : 32 bit
377: * : k : 48 bit = 32+16
378: * output: 32 bit
379: */
380: static uint32_t
381: f_(uint32_t r, uint32_t kl, uint32_t kr)
382: {
383: uint32_t rl, rr;
384: uint32_t s;
385: uint32_t p;
386:
387: etrans1(r, &rl, &rr);
388:
389: strans(kl ^ rl, kr ^ rr, &s);
390:
391: p = etrans2(s);
392:
393: return (p);
394: }
395:
396: /*===========================================================*
397: * BASE CRIPTO
398: *===========================================================*/
399: void
400: des_dec_i(
401: uint32_t keyl, uint32_t keyr,
402: uint32_t msgl, uint32_t msgr,
403: uint32_t *el, uint32_t *er)
404: {
405: uint32_t c, d;
406: uint32_t kl, kr;
407: uint32_t l, r;
408: uint32_t f;
409: uint32_t xor;
410: int i;
411:
412: ktrans1(keyl, keyr, &c, &d);
413:
414: ip(msgl, msgr, &r, &l);
415:
416: for (i = 0; i < 16; i++) {
417: ktrans2(c, d, &kl, &kr);
418:
419: f = f_(l, kl, kr);
420:
421: xor = r ^ f;
422: r = l;
423: l = xor;
424:
425: c = rshift28(c, i);
426: d = rshift28(d, i);
427: }
428:
429: ip_inv(l, r, el, er);
430: }
431:
432:
433: void
434: des_enc_i(
435: uint32_t keyl, uint32_t keyr,
436: uint32_t msgl, uint32_t msgr,
437: uint32_t *el, uint32_t *er)
438: {
439: uint32_t c, d;
440: uint32_t kl, kr;
441: uint32_t l, r;
442: uint32_t f;
443: uint32_t xor;
444: int i;
445:
446: ktrans1(keyl, keyr, &c, &d);
447:
448: ip(msgl, msgr, &l, &r);
449:
450: for (i = 0; i < 16; i++) {
451: c = lshift28(c, i);
452: d = lshift28(d, i);
453:
454: ktrans2(c, d, &kl, &kr);
455:
456: f = f_(r, kl, kr);
457:
458: xor = l ^ f;
459:
460: l = r;
461: r = xor;
462: }
463:
464: ip_inv(r, l, el, er);
465: }
466:
467: /**
468: * input: data: 8 byte
469: */
470: int
471: des_enc_c(const des_key *key, const unsigned char *data, unsigned char *enc)
472: {
473: uint32_t dl, dr;
474: uint32_t el, er;
475: int i;
476:
477: dl = dr = 0;
478: for (i = 0; i < 4; i++) {
479: dl |= (data[i] & 0xff) << (8 * (3 - i));
480: dr |= (data[i + 4] & 0xff) << (8 * (3 - i));
481: }
482:
483: des_enc_i(key->key.ui[0], key->key.ui[1], dl, dr, &el, &er);
484:
485:
486: for (i = 0; i < 4; i++) {
487: enc[i] = (el >> (8 * (3 - i))) & 0xff;
488: enc[i + 4] = (er >> (8 * (3 - i))) & 0xff;
489: }
490:
491: return (0);
492: }
493:
494:
495: /**
496: * input: data: 8 byte
497: */
498: int
499: des_dec_c(const des_key *key, const unsigned char *enc, unsigned char *data)
500: {
501: uint32_t dl, dr;
502: uint32_t el, er;
503: int i;
504:
505: dl = dr = 0;
506: for (i = 0; i < 4; i++) {
507: dl |= (enc[i] & 0xff) << (8 * (3 - i));
508: dr |= (enc[i + 4] & 0xff) << (8 * (3 - i));
509: }
510:
511: des_dec_i(key->key.ui[0], key->key.ui[1], dl, dr, &el, &er);
512:
513:
514: for (i = 0; i < 4; i++) {
515: data[i] = (el >> (8 * (3 - i))) & 0xff;
516: data[i + 4] = (er >> (8 * (3 - i))) & 0xff;
517: }
518:
519: return (0);
520: }
521:
522: /*===========================================================*
523: * CRIPTO: length of input data is "64 * n" byte
524: *===========================================================*/
525: static int
526: des_prm_chk(int datalen, int buflen)
527: {
528: if (datalen % BLOCK != 0)
529: return (-1);
530: if (buflen < datalen)
531: return (-2);
532: return (0);
533: }
534:
535:
536: int
537: des_enc_ecb(const des_key *key, int len, const unsigned char *data, unsigned char *buf)
538: {
539: int ret;
540:
541: ret = crypt_ecb((void *)key, BLOCK, len, data, buf, (void *)des_enc_c);
542:
543: return (ret);
544: }
545:
546:
547: int
548: des_dec_ecb(const des_key *key, int len, const unsigned char *enc, unsigned char *data)
549: {
550: int ret;
551:
552: ret = crypt_ecb((void *)key, BLOCK, len, enc, data, (void *)des_dec_c);
553:
554: return (ret);
555: }
556:
557:
558:
559: int
560: des_enc_cbc(const des_key *key, const unsigned char *iv, int len,
561: const unsigned char *data, unsigned char *buf)
562: {
563: int ret;
564:
565: ret = crypt_enc_cbc((void *)key, BLOCK, iv, len, data, buf, (void *)des_enc_c);
566:
567: return (ret);
568: }
569:
570:
571: int
572: des_dec_cbc(const des_key *key, const unsigned char *iv, int len,
573: const unsigned char *data, unsigned char *buf)
574: {
575: int ret;
576:
577: ret = crypt_dec_cbc((void *)key, BLOCK, iv, len, data, buf, (void *)des_dec_c);
578:
579: return (ret);
580: }
581:
582:
583: int
584: des_enc_cfb(const des_key *key, int bit,
585: const unsigned char *iv, int len,
586: const unsigned char *data, unsigned char *buf)
587: {
588: int ret;
589:
590: ret = crypt_cfb((void *)key, 0, bit, BLOCK, iv, len, data, buf, (void *)des_enc_c);
591:
592: return (ret);
593: }
594:
595: int
596: des_dec_cfb(const des_key *key, int bit,
597: const unsigned char *iv, int len,
598: const unsigned char *data, unsigned char *buf)
599: {
600: int ret;
601:
602: ret = crypt_cfb((void *)key, 1, bit, BLOCK, iv, len, data, buf, (void *)des_enc_c);
603:
604: return (ret);
605: }
606:
607:
608: int
609: des_ofb(const des_key *key, int bit,
610: const unsigned char *iv, int len,
611: const unsigned char *data,
612: unsigned char *buf)
613: {
614: int ret;
615: ret = crypt_ofb((void *)key, bit, BLOCK, iv, len, data, buf, (void *)des_enc_c);
616:
617: return (ret);
618: }
619:
620:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>