-
Notifications
You must be signed in to change notification settings - Fork 2
/
edriver.c
161 lines (134 loc) · 3.27 KB
/
edriver.c
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
/*
* Test driver for equivalence class stuff.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "window.h"
#include "specs.h"
#include "cipher.h"
ecinfo myecinfo;
char plainbuf[BLOCKSIZE+1];
float accept_level;
extern int ec_best();
extern void ec_dplain();
extern void ec_dscipher();
extern void ec_dnext();
extern void ec_dsizetab();
extern void ec_dperm();
extern void ec_dpmap();
extern void ec_init();
extern void ec_cscore();
/* Forward declarations */
void do_block(ecinfo *eci, int blknum, char *cipherfile, char *plainfile);
extern char mcbuf[];
extern char *fname; /* Used by fillcbuf. */
/* Test routine for equiv class info. */
int main(argc, argv)
int argc;
char *argv[];
{
ecinfo *eci;
FILE *inp;
int blknum;
int maxblock;
long filelength;
char infile[100];
char inplain[100];
char *plain = ".txt";
char *code = ".cipher";
char *p, *q;
if (argc != 3) {
printf("Usage: %s input_file_root acceptance_level\n", argv[0]);
exit(0);
}
p = inplain;
q = argv[1];
while ((*p++ = *q++));
--p;
q = plain;
while ((*p++ = *q++));
p = infile;
q = argv[1];
while ((*p++ = *q++));
--p;
q = code;
while ((*p++ = *q++));
if (sscanf(argv[2], "%f", &accept_level) != 1) {
printf("Could not parse the acceptance level from %s.\n", argv[2]);
exit(0);
}
load_1stats_from("mss.stats");
eci = &myecinfo;
if ((inp = fopen(infile, "r")) == NULL) {
printf("\nCannot open %s for reading.\n", infile);
exit(0);
}
fseek(inp, 0L, 2);
filelength = ftell(inp);
fclose(inp);
maxblock = filelength / BLOCKSIZE;
if (maxblock > 19) maxblock = 19;
printf("\t\tEquivalence Class Guessing\n\n");
printf("Filename = %s. Acceptance level = %4.2f\n",infile,accept_level);
for (blknum = 0 ; blknum <= maxblock ; blknum++) {
do_block(eci, blknum, infile, inplain);
}
return 0;
}
void do_block(ecinfo *eci, int blknum, char *cipherfile, char *plainfile)
{
int i,c,x,y;
int naccepted, nwrong;
int classpos;
int charcount;
fname = cipherfile;
fillcbuf(blknum, mcbuf);
fname = plainfile;
fillcbuf(blknum, plainbuf);
ec_init(mcbuf, refperm(blknum), eci);
naccepted = 0;
nwrong = 0;
for (i = 0 ; i < eci->sizelast ; i++) {
classpos = eci->sizelist[i].firstpos;
c = ec_best(eci, classpos, accept_level);
if (c != NONE) {
x = eci->scipher[classpos];
y = MODMASK & (c + classpos);
if (eci->perm[x] == NONE && eci->perm[y] == NONE) {
naccepted++;
eci->perm[x] = y;
eci->perm[y] = x;
/* printf("ACCEPTING best guess of %d wired to %d.\n",
x, y);
*/ if ((MODMASK & plainbuf[classpos]) != c) {
nwrong++;
/* printf("*** WRONG *** First char should be %c.\n",
plainbuf[classpos]);
*/ }
}
else if (eci->perm[x] == y) {
/* printf("CONFIRMING guess of %d wired to %d.\n",
x, y);
*/ }
else {
/* printf("CONFLICTING guess of %d wired to %d.\n",
x, y);
*/ }
}
}
decode(eci->ciphertext, eci->plaintext, eci->perm);
charcount = 0;
for (i = 0 ; i < BLOCKSIZE ; i++) {
if (eci->plaintext[i] != NONE) charcount++;
}
printf("\n\nPlaintext for block %d using %d wires", blknum, naccepted);
printf(" (%d wrong)", nwrong);
printf(" yields %d characters.\n\n", charcount);
ec_dplain(stdout, eci);
}
key u_getkey(void)
{
return 0;
}
keyer topktab[] ={{0, NULL}};