summaryrefslogtreecommitdiff
path: root/test/crypt/crypt.c
blob: 786464710f4b4392a91bb27a7d5f859b9665568b (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

/*
 * This crypt(3) validation program shipped with UFC-crypt
 * is derived from one distributed with Phil Karns PD DES package.
 *
 * @(#)cert.c	1.8 11 Aug 1996
 */

#include <stdio.h>
#include <stdlib.h>
#include "crypt.h"

static int totfails = 0;

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

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

	for(i=0;i<8;i++){
		scanf("%2x",&t);
		if(feof(stdin))
		  good_bye();
		for(j=0; j<8 ; j++) {
		  *cp++ = (t & (0x01 << (7-j))) != 0;
		}
	}
}

static void put8(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);
	}
}

int main(void)
{
	char key[64],plain[64],cipher[64],answer[64];
	int i;
	int test;
	int fail;

	for(test=0;!feof(stdin);test++){

		get8(key);
		printf(" K: "); put8(key);
		setkey(key);

		get8(plain);
		printf(" P: "); put8(plain);

		get8(answer);
		printf(" C: "); put8(answer);

		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(" Encrypt FAIL");
			fail++; totfails++;
		}

		encrypt(cipher, 1);

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

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