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
|
/*
* Demo on how to use /dev/crypto device for HMAC.
*
* Placed under public domain.
*
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "../ncr.h"
#include <stdlib.h>
#define DATA_SIZE 4096
static void randomize_data(uint8_t * data, size_t data_size)
{
int i;
srand(time(0)*getpid());
for (i=0;i<data_size;i++) {
data[i] = rand() & 0xff;
}
}
static int
test_ncr_data(int cfd)
{
struct ncr_data_init_st init;
struct ncr_data_st kdata;
uint8_t data[DATA_SIZE];
uint8_t data_bak[DATA_SIZE];
int i;
randomize_data(data, sizeof(data));
memcpy(data_bak, data, sizeof(data));
init.max_object_size = DATA_SIZE;
init.flags = NCR_DATA_FLAG_EXPORTABLE;
init.initial_data = data;
init.initial_data_size = sizeof(data);
if (ioctl(cfd, NCRIO_DATA_INIT, &init)) {
perror("ioctl(NCRIO_DATA_INIT)");
return 1;
}
fprintf(stderr, "Imported data\n");
memset(data, 0, sizeof(data));
kdata.desc = init.desc;
kdata.data = data;
kdata.data_size = sizeof(data);
kdata.append_flag = 0;
if (ioctl(cfd, NCRIO_DATA_GET, &kdata)) {
perror("ioctl(NCRIO_DATA_GET)");
return 1;
}
if (memcmp(data, data_bak, sizeof(data))!=0) {
fprintf(stderr, "data returned but differ!\n");
return 1;
}
fprintf(stderr, "Verified imported data integrity\n");
/* test set */
memset(data, 0xf1, sizeof(data));
kdata.desc = init.desc;
kdata.data = data;
kdata.data_size = sizeof(data);
kdata.append_flag = 0;
if (ioctl(cfd, NCRIO_DATA_SET, &kdata)) {
perror("ioctl(NCRIO_DATA_SET)");
return 1;
}
fprintf(stderr, "Imported new data\n");
/* test get after set */
memset(data, 0, sizeof(data));
kdata.desc = init.desc;
kdata.data = data;
kdata.data_size = sizeof(data);
kdata.append_flag = 0;
if (ioctl(cfd, NCRIO_DATA_GET, &kdata)) {
perror("ioctl(NCRIO_DATA_GET)");
return 1;
}
for(i=0;i<kdata.data_size;i++) {
if (((uint8_t*)kdata.data)[i] != 0xf1) {
fprintf(stderr, "data returned but differ!\n");
return 1;
}
}
fprintf(stderr, "Verified new data\n");
if (ioctl(cfd, NCRIO_DATA_DEINIT, &kdata.desc)) {
perror("ioctl(NCRIO_DATA_DEINIT)");
return 1;
}
fprintf(stderr, "Initializing unexportable data\n");
randomize_data(data, sizeof(data));
init.max_object_size = DATA_SIZE;
init.flags = 0;
init.initial_data = data;
init.initial_data_size = sizeof(data);
if (ioctl(cfd, NCRIO_DATA_INIT, &init)) {
perror("ioctl(NCRIO_DATA_INIT)");
return 1;
}
kdata.desc = init.desc;
kdata.data = data;
kdata.data_size = sizeof(data);
kdata.append_flag = 0;
if (ioctl(cfd, NCRIO_DATA_GET, &kdata)) {
perror("ioctl");
fprintf(stderr, "Verified that unexportable data cannot be exported\n");
} else {
fprintf(stderr, "Unexportable data were exported!?\n");
return 1; /* ok */
}
for (i=0;i<512;i++ ) {
init.max_object_size = DATA_SIZE;
init.flags = 0;
init.initial_data = data;
init.initial_data_size = sizeof(data);
if (ioctl(cfd, NCRIO_DATA_INIT, &init)) {
perror("ioctl(NCRIO_DATA_INIT)");
fprintf(stderr, "Reached maximum limit at: %d data\n", i);
}
}
return 0;
}
int
main()
{
int fd = -1;
/* Open the crypto device */
fd = open("/dev/crypto", O_RDWR, 0);
if (fd < 0) {
perror("open(/dev/crypto)");
return 1;
}
/* Run the test itself */
if (test_ncr_data(fd))
return 1;
/* Close the original descriptor */
if (close(fd)) {
perror("close(fd)");
return 1;
}
return 0;
}
|