summaryrefslogtreecommitdiffstats
path: root/secure/lib/libcipher/test/cert.c
blob: e9907b9a9232d200eab7c014e45d5a771977de96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * This DES validation program shipped with FreeSec is derived from that
 * shipped with UFC-crypt which is apparently derived from one distributed
 * with Phil Karns PD DES package.
 *
 * $FreeBSD$
 */

#include <stdio.h>

int totfails = 0;

char *crypt();
#ifdef HAVE_CRYPT16
char *crypt16();
#endif /* HAVE_CRYPT16 */


static struct crypt_test {
	char	*key, *setting, *answer;
} crypt_tests[] = {
	"foob",			"ar",		"arlEKn0OzVJn.",
	"holyhooplasbatman!",	"_X.......",	"_X.......N89y2Z.e4WU",
	"holyhooplasbatman!",	"_X...X...",	"_X...X...rSUDQ5Na/QM",
	"holyhooplasbatman!",	"_XX..X...",	"_XX..X...P8vb9xU4JAk",
	"holyhooplasbatman!",	"_XX..XX..",	"_XX..XX..JDs5IlGLqT2",
	"holyhooplasbatman!",	"_XX..XXa.",	"_XX..XXa.bFVsOnCNh8Y",
	"holyhooplasbatman!",	"_XXa.X...",	"_XXa.X...Ghsb3QKNaps",
#ifdef TAKES_TOO_LONG_ON_SOME_CRYPTS
	"holyhooplasbatman!",	"_arararar",	"_ararararNGMzvpNjeCc",
#endif
	NULL, NULL, NULL,
};


static struct crypt_test crypt16_tests[] = {
	"foob",			"ar",		"arxo23jZDD5AYbHbqoy9Dalg",
	"holyhooplasbatman!",	"ar",		"arU5FRLJ3kxIoedlmyrOelEw",
	NULL, NULL, NULL
};


void good_bye()
{
  if(totfails == 0) {
    printf(" Passed validation\n");
    exit(0);
  } else {
    printf(" %d failures during validation!!!\n", totfails);
    exit(1);
  }
}


void put8(cp)
char *cp;
{
	int i,j,t;

	for(i = 0; i < 8; i++){
		t = 0;
		for(j = 0; j < 8; j++)
			t = t << 1 | *cp++;
		printf("%02x", t);
	}
}


void print_bits(bits)
unsigned char *bits;
{
	int	i;

	for (i = 0; i < 8; i++) {
		printf("%02x", bits[i]);
	}
}


int parse_line(buff, salt, key, plain, answer)
char *buff;
long *salt;
char *key, *plain, *answer;
{
	char *ptr1, *ptr2;
	int val;
	int i,j,t;

	/*
	 * Extract salt
	 */
	if (sscanf(buff, "%lu", salt) != 1)
		return(-1);
	for (ptr2 = buff; *ptr2 && !isspace(*ptr2); ptr2++)
		;

	/*
	 * Extract key
	 */
	for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
		;
	for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
		;
	if (ptr2 - ptr1 != 16)
		return(-1);
	for (i = 0; i < 8; i++){
		if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
			return(-2);
		for (j = 0; j < 8; j++)
			*key++ = (t & 1 << (7 - j)) != 0;
	}

	/*
	 * Extract plain
	 */
	for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
		;
	for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
		;
	if (ptr2 - ptr1 != 16)
		return(-1);
	for (i = 0; i < 8; i++){
		if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
			return(-2);
		for (j = 0; j < 8; j++)
			*plain++ = (t & 1 << (7 - j)) != 0;
	}

	/*
	 * Extract answer
	 */
	for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
		;
	for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
		;
	if (ptr2 - ptr1 != 16)
		return(-1);
	for (i = 0; i < 8; i++){
		if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
			return(-2);
		for (j = 0; j < 8; j++)
			*answer++ = (t & 1 << (7 - j)) != 0;
	}
	return(0);
}

/*
 * Test the setkey and encrypt functions
 */
void test_encrypt()
{
	char key[64],plain[64],cipher[64],answer[64];
	char buff[BUFSIZ];
	unsigned long salt;
	int i;
	int test;
	int fail;

	printf("Testing setkey/encrypt\n");

	for(test=0;fgets(buff, BUFSIZ, stdin);test++){

		/*
		 * Allow comments.
		 */
		if (*buff == '#')
			continue;

		if ((fail = parse_line(buff, &salt, key, plain, answer)) < 0){
			printf("test %d garbled (%d)\n", test, fail);
			continue;
		}

		if (salt)
			continue;	/* encrypt has no salt support */

		printf(" K: "); put8(key);
		printf(" P: "); put8(plain);
		printf(" C: "); put8(answer);

		setkey(key);
		for(i = 0; i < 64; i++)
			cipher[i] = plain[i];
		encrypt(cipher, 0);

		for(i=0;i<64;i++)
			if(cipher[i] != answer[i])
				break;
		fail = 0;
		if(i != 64){
			printf(" Enc FAIL ");
			put8(cipher);
			fail++; totfails++;
		}

		encrypt(cipher, 1);

		for(i=0;i<64;i++)
			if(cipher[i] != plain[i])
				break;
		if(i != 64){
			printf(" Dec FAIL");
			fail++; totfails++;
		}

		if(fail == 0)
			printf(" OK");
		printf("\n");
	}
}


void bytes_to_bits(bytes, bits)
char *bytes;
unsigned char *bits;
{
	int	i, j;

	for (i = 0; i < 8; i++) {
		bits[i] = 0;
		for (j = 0; j < 8; j++) {
			bits[i] |= (bytes[i*8+j] & 1) << (7 - j);
		}
	}
}


/*
 * Test the des_setkey and des_cipher functions
 */
void test_des()
{
	char ckey[64], cplain[64], canswer[64];
	unsigned char key[8], plain[8], cipher[8], answer[8];
	char buff[BUFSIZ];
	unsigned long salt;
	int i;
	int test;
	int fail;

	printf("Testing des_setkey/des_cipher\n");

	for(test=0;fgets(buff, BUFSIZ, stdin);test++){

		/*
		 * Allow comments.
		 */
		if (*buff == '#')
			continue;

		if ((fail = parse_line(buff, &salt, ckey, cplain, canswer)) <0){
			printf("test %d garbled (%d)\n", test, fail);
			continue;
		}

		printf(" S: %06x", salt);
		printf(" K: "); put8(ckey);
		printf(" P: "); put8(cplain);
		printf(" C: "); put8(canswer);

		bytes_to_bits(ckey, key);
		bytes_to_bits(cplain, plain);
		bytes_to_bits(canswer, answer);
		des_setkey(key);
		des_cipher(plain, cipher, salt, 1);

		for(i = 0; i < 8; i++)
			if(cipher[i] != answer[i])
				break;
		fail = 0;
		if(i != 8){
			printf(" Enc FAIL ");
			print_bits(cipher);
			fail++; totfails++;
		}

		des_cipher(cipher, cipher, salt, -1);

		for(i = 0; i < 8; i++)
			if(cipher[i] != plain[i])
				break;
		if(i != 8){
			printf(" Dec FAIL");
			fail++; totfails++;
		}

		if(fail == 0)
			printf(" OK");
		printf("\n");
	}
}


/*
 *	Test the old-style crypt(), the new-style crypt(), and crypt16().
 */
void test_crypt()
{
	char	*result;
	struct crypt_test	*p;

	printf("Testing crypt() family\n");

	for (p = crypt_tests; p->key; p++) {
		printf(" crypt(\"%s\", \"%s\"), \"%s\" expected",
			p->key, p->setting, p->answer);
		fflush(stdout);
		result = crypt(p->key, p->setting);
		if(!strcmp(result, p->answer)) {
			printf(", OK\n");
		} else {
			printf("\n  failed (\"%s\")\n", result);
			totfails++;
		}
	}

#ifdef HAVE_CRYPT16
	for (p = crypt16_tests; p->key; p++) {
		printf(" crypt16(\"%s\", \"%s\"), \"%s\" expected",
			p->key, p->setting, p->answer);
		fflush(stdout);
		result = crypt16(p->key, p->setting);
		if(!strcmp(result, p->answer)) {
			printf(", OK\n");
		} else {
			printf("\n  failed (\"%s\")\n", result);
			totfails++;
		}
	}
#endif /* HAVE_CRYPT16 */
}

main(argc, argv)
int argc;
char *argv[];
{
	if(argc < 1 || !strcmp(argv[1], "-e"))
		test_encrypt();
	else if(!strcmp(argv[1], "-d"))
		test_des();
	else if(!strcmp(argv[1], "-c"))
		test_crypt();
	good_bye();
}
OpenPOWER on IntegriCloud