sksatest.c
4.69 KB
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
#include "testlib.c"
char pname[256];
char *myname;
char *dname = DEVICE_NAME;
#include "fillcard.c"
static int
write_sksa(BBCHandle h, const char *fname, int option)
{
unsigned char *buf;
struct stat sb;
int len;
int fd;
int rv1, rv2;
#ifndef WIN32
if ((fd = open(fname, O_RDONLY )) < 0) {
#else
if ((fd = open(fname, O_RDONLY | O_BINARY)) < 0) {
#endif
printf("%s: open %s fails (errno %d)\n", pname, fname, errno);
return fd;
}
if (fstat(fd, &sb) < 0) {
printf("%s: stat %s fails (errno %d)\n", pname, fname, errno);
return -1;
}
len = sb.st_size;
if ((buf = malloc(len)) == NULL) {
perror("malloc");
exit(1);
}
if ((rv1 = read(fd, buf, len)) < len) {
printf("%s: read %s fails (ret %d, len %d)\n", pname, fname, rv1, len);
rv1 = -1;
goto out;
}
rv1 = BBCVerifySKSA2(h, buf, len, option);
printf("%s: BBCVerifySKSA %d %s returns %d\n", pname, option, fname, rv1);
if (option == BBC_SKSA_FORCE_SA2) {
int tmplen = len - BLK_SIZE;
/* Only SA2 is to be written - should get BBC_SA1DIFFERS */
if (rv1 != BBC_SA1DIFFERS)
goto out;
/* Now force part of SKSA to be written and try again */
rv1 = BBCVerifySKSA2(h, buf, tmplen, BBC_SKSA_FORCE_ALL);
if (rv1 != 0 && rv1 != tmplen)
goto out;
/* Try again */
rv1 = BBCVerifySKSA2(h, buf, len, option);
printf("%s: BBCVerifySKSA %d %s returns %d\n", pname, option, fname, rv1);
}
if (rv1 != 0 && rv1 != len)
goto out;
/* Write SKSA2 again */
rv2 = BBCVerifySKSA2(h, buf, len, option);
printf("%s: BBCVerifySKSA-2 %d %s returns %d\n", pname, option, fname, rv2);
if (option == BBC_SKSA_FORCE_ALL) {
/* Forcing the SKSA to be replaced - rv2 should be same as len */
if (rv2 != len) {
printf("%s: Failure: BBCVerifySKSA-2 %d %s return not length\n",
pname, option, fname);
rv1 = -1;
}
}
else {
/* All other cases - rv2 should be 0 (SKSA not replaced) */
if (rv2 != 0) {
printf("%s: Failure: BBCVerifySKSA-2 %d %s return not zero\n",
pname, option, fname);
rv1 = -1;
}
}
out:
close(fd);
free(buf);
return rv1;
}
static void
do_sksa_test(BBCHandle h)
{
int rv;
int blks;
char fname[32];
int len;
int sa1len = 1;
for (blks = 1; blks < 66 ; blks++) {
len = blks * BLK_SIZE;
sprintf(fname, "sksafile.%d", blks);
if (blks-2 < 4) {
printf("%s: create sksa file %s, len %d (sk=%d, sa1=%d, sa2=%d)\n",
pname, fname, len, blks, 0, 0);
mksksa(fname, blks-2, 0, 0);
}
else {
printf("%s: create sksa file %s, len %d (sk=%d, sa1=%d, sa2=%d)\n",
pname, fname, len, 4, sa1len, blks - 4 - sa1len);
mksksa(fname, 4, sa1len, blks - 2 - 4 - sa1len);
if (blks % 4 == 0) {
sa1len++;
}
}
}
/*
* Write the biggest file that will fit first
*/
for (blks = 65; blks > 0; blks--) {
sprintf(fname, "sksafile.%d", blks);
if ((rv = write_sksa(h, fname, blks % 4)) >= 0)
break;
}
for (blks = 1; blks < 66 ; blks++) {
len = blks * BLK_SIZE;
sprintf(fname, "sksafile.%d", blks);
if ((rv = write_sksa(h, fname, blks % 4)) != len) {
printf("%s: write_sksa file %s, len %d ret %d ERROR\n",
pname, fname, len, rv);
}
}
}
int
main(int argc, char **argv)
{
BBCHandle h;
int loops = 0;
int cardsize, freeblks, reservedblks, badblks;
int rv;
myname = argv[0];
if (argc > 1)
dname = argv[argc - 1];
setbuf(stdout, NULL);
setbuf(stderr, NULL);
while (1) {
sprintf(pname, "%s(%d)", myname, ++loops);
printf("%s: Doing BBCInit\n", pname);
#ifndef WIN32
h = BBCInit(dname, BBC_SYNC);
#else
h = BBCInit(BBC_SYNC);
#endif
printf("%s: BBCInit(%s) returns %d\n", pname, dname, h);
if (h < 0) continue;
while (!BBCCardPresent(h)) {
printf("%s: BBCCardPresent returns false\n", pname);
}
if ((rv = BBCStats(h, &cardsize, &reservedblks, &freeblks, &badblks)) < 0 && rv != BBC_NOFORMAT) {
printf("%s: BBCStats returns %d\n", pname, rv);
BBCClose(h);
continue;
}
printf("%s: BBCStats cardsize %d, freeblks %d\n", pname, cardsize, freeblks);
printf("%s: format card ...\n", pname);
rv = BBCSetLed(h, BBC_LED_ORANGE);
rv = BBCFormatCard(h);
printf("%s: BBCFormatCard returns %d\n", pname, rv);
if (h < 0) continue;
do_fill_card(h);
//do_check_files(h);
//do_sksa_test(h);
//write_sksa(h, "sksa.aes", 3);
rv = BBCSetLed(h, BBC_LED_OFF);
rv = BBCClose(h);
printf("%s: BBCClose returns %d\n", pname, rv);
#if 0
printf("%s: exit to check status!\n", pname);
exit(1);
#endif
}
exit(0);
}