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