Annotation of OpenXM/src/ox_ntl/crypt/des/destest.c, Revision 1.2
1.2 ! iwane 1: /* $OpenXM: OpenXM/src/ox_ntl/crypt/des/destest.c,v 1.1 2004/07/11 00:32:17 iwane Exp $ */
1.1 iwane 2: /*
3: * FIPS 81 - Des Modes of Operation
4: */
5:
6: #include <stdio.h>
7: #include <string.h>
8:
1.2 ! iwane 9: #include "des3.h"
1.1 iwane 10: #define N 4096
11:
12: int
13: main()
14: {
15: des_key dkey;
1.2 ! iwane 16: des3_key dkey3;
1.1 iwane 17: unsigned char key[] = "\x01\x23\x45\x67\x89\xab\xcd\xef"; /* secret key */
1.2 ! iwane 18: unsigned char key3[3 * 8];
1.1 iwane 19: unsigned char *iv = (unsigned char *)"\x12\x34\x56\x78\x90\xab\xcd\xef"; /* initial vector */
20: unsigned char enc[N], dec[N];
21: int i, k;
22: int ret;
23: int cnt = 0;
24:
25: unsigned char ecb[] =
26: "\x3f\xa4\x0e\x8a\x98\x4d\x48\x15"
27: "\x6a\x27\x17\x87\xab\x88\x83\xf9"
28: "\x89\x3d\x51\xec\x4b\x56\x3b\x53";
29: unsigned char cbc[] =
30: "\xe5\xc7\xcd\xde\x87\x2b\xf2\x7c"
31: "\x43\xe9\x34\x00\x8c\x38\x9c\x0f"
32: "\x68\x37\x88\x49\x9a\x7c\x05\xf6";
33: unsigned char cfb8[] =
34: "\xf3\x1f\xda\x07\x01\x14\x62\xee"
35: "\x18\x7f\x43\xd8\x0a\x7c\xd9\xb5"
36: "\xb0\xd2\x90\xda\x6e\x5b\x9a\x87";
37: unsigned char cfb16[] =
38: "\xf3\x09\x87\x87\x7f\x57\xf7\x3c"
39: "\x36\xb6\xdb\x70\xd8\xd5\x34\x19"
40: "\xd3\x86\xb2\x23\xb7\xb2\xad\x1b";
41: unsigned char cfb32[] =
42: "\xf3\x09\x62\x49\xa4\xdf\xa4\x9f"
43: "\x33\xdc\x7b\xad\x4c\xc8\x9f\x64"
44: "\xe4\x53\xe5\xec\x67\x20\xda\xb6";
45: unsigned char cfb48[] =
46: "\xf3\x09\x62\x49\xc7\xf4\x30\xb5"
47: "\x15\xec\xbb\x85\x97\x5a\x13\x8c"
48: "\x68\x60\xe2\x38\x34\x3c\xdc\x1f";
49: unsigned char cfb64[] =
50: "\xf3\x09\x62\x49\xc7\xf4\x6e\x51"
51: "\xa6\x9e\x83\x9b\x1a\x92\xf7\x84"
52: "\x03\x46\x71\x33\x89\x8e\xa6\x22";
53: /*
54: unsigned char cfb56[] =
55: "\xf3\x09\x62\x49\xc7\xf4\x6e\x2f"
56: "\xfb\xe9\x55\x13\xa4\xc9\x5a\xa7"
57: "\xac\x61\x21\x62\x52";
58: */
59: unsigned char ofb8[] =
60: "\xf3\x4a\x28\x50\xc9\xc6\x49\x85"
61: "\xd6\x84";
62: unsigned char ofb64[] =
63: "\xf3\x09\x62\x49\xc7\xf4\x6e\x51"
64: "\x35\xf2\x4a\x24\x2e\xeb\x3d\x3f"
65: "\x3d\x6d\x5b\xe3\x25\x5a\xf8\xc3";
66: unsigned char cbc_2[] =
67: "\xb9\x91\x6b\x8e\xe4\xc3\xda\x64"
68: "\xb4\xf4\x4e\x3c\xbe\xfb\x99\x48"
69: "\x45\x21\x38\x8f\xa5\x9a\xe6\x7d"
70: "\x58\xd2\xe7\x7e\x86\x06\x27\x33";
71: unsigned char cfb8_2[] =
72: "\x8a\x87\x01\xd9\x21\x30\x8f\x35"
73: "\x74\xbd\x1e\x74\xf4\x32\x14\x77"
74: "\x0c\x7a\x34\x8f\x84\x3d\x0d\x08"
75: "\x34\xb2\xc4\xfa";
76: const unsigned char *plain = (unsigned char *)"Now is the time for all ";
77: const unsigned char *plain2 = (unsigned char *)"7654321 Now is the time for \0\0\0\0\0\0\0\0";
78: const unsigned char *p;
79:
1.2 ! iwane 80: for (i = 0; i < 3; i++) {
! 81: memcpy(key3 + 8 * i, key, 8);
! 82: }
! 83:
1.1 iwane 84: des_keyset(key, &dkey);
1.2 ! iwane 85: des3_keyset(key3, &dkey3);
1.1 iwane 86:
87: k = 24;
88: ret = des_enc_ecb(&dkey, k, plain, enc);
89:
90: if (memcmp(ecb, enc, k)) {
91: printf("ECB enc failed.\n");
92: printf("ret = %d\n", ret);
93: goto _CBC;
94: }
95:
96: ret = des_dec_ecb(&dkey, k, enc, dec);
97: if (memcmp(plain, dec, k)) {
98: printf("ECB dec failed.\n");
99: printf("ret = %d\n", ret);
100: goto _CBC;
101: }
102:
103: printf("ECB ok.\n");
104: cnt++;
105:
106: _CBC:
107: for (i = 0; i < 2; i++) {
108: const unsigned char *ci[] = {cbc, cbc_2};
109: int ks[] = {24, 32};
110: const unsigned char *d[] = {plain, plain2};
111:
112: k = ks[i];
113: p = d[i];
114:
115: ret = des_enc_cbc(&dkey, iv, k, p, enc);
116: if (memcmp(ci[i], enc, k)) {
117: printf("CBC enc failed.\n");
118: printf("ret = %d, i = %d\n", ret, i);
119: goto _CFB;
120: }
121:
122: ret = des_dec_cbc(&dkey, iv, k, enc, dec);
123: if (memcmp(p, dec, k)) {
124: printf("CBC dec failed.\n");
125: printf("ret = %d, i = %d\n", ret, i);
126: goto _CFB;
127: }
1.2 ! iwane 128:
! 129: ret = des3_enc_cbc(&dkey3, iv, k, p, enc);
! 130: if (memcmp(ci[i], enc, k)) {
! 131: printf("CBC enc failed. [des3]\n");
! 132: printf("ret = %d, i = %d\n", ret, i);
! 133: goto _CFB;
! 134: }
! 135:
! 136: ret = des3_dec_cbc(&dkey3, iv, k, enc, dec);
! 137: if (memcmp(p, dec, k)) {
! 138: printf("CBC dec failed. [des3]\n");
! 139: printf("ret = %d, i = %d\n", ret, i);
! 140: goto _CFB;
! 141: }
! 142:
! 143:
1.1 iwane 144: }
145:
146: printf("CBC ok.\n");
147: cnt++;
148:
149: _CFB:
150: for (i = 0; i < 6; i++) {
151: int bit[] = {8, 16, 32, 48, 64, 8};
152: const unsigned char *ci[] = {cfb8, cfb16, cfb32, cfb48, cfb64, cfb8_2};
153: int ks[] = {24, 24, 24, 24, 24, 28};
154: const unsigned char *d[] = {plain, plain, plain, plain, plain, plain2};
155:
156:
157: k = ks[i];
158: p = d[i];
159: ret = des_enc_cfb(&dkey, bit[i], iv, k, p, enc);
160: if (memcmp(ci[i], enc, k)) {
161: printf("CFB 8-bit enc failed.\n");
162: printf("ret = %d, i = %d\n", ret, i);
163: goto _OFB;
164: }
165:
166: ret = des_dec_cfb(&dkey, bit[i], iv, k, enc, dec);
167: if (memcmp(p, dec, k)) {
168: printf("CFB 8-bit dec failed.\n");
169: printf("ret = %d, i = %d\n", ret, i);
170: goto _OFB;
171: }
172: }
173:
174: printf("CFB ok.\n");
175: cnt++;
176:
177: _OFB:
178: for (i = 0; i < 1; i++) {
179: int bit[] = {8, 64};
180: const unsigned char *ci[] = {ofb8, ofb64};
181: int ks[] = {10, 24};
182: const unsigned char *d[] = {plain, plain};
183:
184: k = ks[i];
185: p = d[i];
186: ret = des_ofb(&dkey, bit[i], iv, k, p, enc);
187: if (memcmp(ci[i], enc, k)) {
188: printf("CFB 8-bit enc failed.\n");
189: printf("ret = %d, i = %d\n", ret, i);
190: goto _END;
191: }
192:
193: ret = des_ofb(&dkey, bit[i], iv, k, enc, dec);
194: if (memcmp(p, dec, k)) {
195: printf("CFB 8-bit dec failed.\n");
196: printf("ret = %d, i = %d\n", ret, i);
197: goto _END;
198: }
199: }
200:
201: printf("OFB ok.\n");
202: cnt++;
203:
204: _END:
205: if (cnt == 4) {
206: printf("all test is ok.\n");
207: } else {
208: printf("error has found.\n");
209: }
210:
211: return (0);
212: }
213:
214:
215:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>