summaryrefslogtreecommitdiffstats
path: root/userspace/ncrypto_session.c
blob: e37aa211f974825d5d2e70b0eaacfb509fa45736 (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

#include <sys/types.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include "../ncr.h"
#include "ncrypto.h"

extern int __ncr_file_descriptor;

int
ncr_session_once_key_data(ncr_key_t key, ncr_key_params_t params, ncr_crypto_op_t op, ncr_algorithm_t algorithm, ncr_key_t input, void *output, size_t output_size)
{
	struct ncr_session_once_op_st io;
	memset(&io, 0, sizeof(io));

	if (input == NCR_KEY_INVALID || !output || !output_size) {
		errno = EINVAL;
		return -1;
	}

	io.init.algorithm = algorithm;
	io.init.key = key;
	if (params)
	    memmove(&io.init.params, params, sizeof(io.init.params));
	io.init.op = op;
	io.op.data.kdata.input = input;
	io.op.data.kdata.output = output;
	io.op.data.kdata.output_size = output_size;
	io.op.type = NCR_KEY_DATA;

	if (__ncr_file_descriptor < 0) {
		errno = EBADF;
		return -1;
	}

	if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_ONCE, &io) < 0)
		 return -1;

	switch (io.op.err) {
	case NCR_VERIFICATION_FAILED:
		errno = EDOM;
		return -1;
        case NCR_SUCCESS:
		errno = 0;
		return io.op.data.kdata.output_size;
        default:
		errno = EFAULT;
		return -1;
	}
}

int
ncr_session_once_direct_data(ncr_key_t key, ncr_key_params_t params, ncr_crypto_op_t op, ncr_algorithm_t algorithm, void *input, size_t input_size, void *output, size_t output_size)
{
	struct ncr_session_once_op_st io;
	memset(&io, 0, sizeof(io));

	if (!input || !input_size || !output || !output_size) {
		errno = EINVAL;
		return -1;
	}

	io.init.key = key;
	if (params)
	    memmove(&io.init.params, params, sizeof(io.init.params));
	io.init.algorithm = algorithm;
	io.init.op = op;
	io.op.data.udata.input = input;
	io.op.data.udata.input_size = input_size;
	io.op.data.udata.output = output;
	io.op.data.udata.output_size = output_size;
	io.op.type = NCR_DIRECT_DATA;

	if (__ncr_file_descriptor < 0) {
		errno = EBADF;
		return -1;
	}

	if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_ONCE, &io) < 0)
		 return -1;

	switch (io.op.err) {
	case NCR_VERIFICATION_FAILED:
		errno = EDOM;
		return -1;
        case NCR_SUCCESS:
		errno = 0;
		return io.op.data.udata.output_size;
        default:
		errno = EFAULT;
		return -1;
	}
}

int
ncr_session_init(ncr_session_t *session, ncr_key_t key, ncr_key_params_t params, ncr_crypto_op_t op, ncr_algorithm_t algorithm)
{
	struct ncr_session_st io;
	memset(&io, 0, sizeof(io));

	if (!session) {
		errno = EINVAL;
		return -1;
	}

	io.algorithm = algorithm;
	io.key = key;
	if (params)
	    memmove(&io.params, params, sizeof(io.params));
	io.op = op;

	if (__ncr_file_descriptor < 0) {
		errno = EBADF;
		return -1;
	}

	if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_INIT, &io) < 0)
		 return -1;

	*session = io.ses;

	return 0;
}

int
ncr_session_update_key_data(ncr_session_t session, ncr_key_t input, void *output, size_t output_size)
{
	struct ncr_session_op_st io;
	memset(&io, 0, sizeof(io));

	if (session == NCR_SESSION_INVALID || input == NCR_KEY_INVALID) {
		errno = EINVAL;
		return -1;
	}

	io.ses = session;
	io.data.kdata.input = input;
	io.data.kdata.output = output;
	io.data.kdata.output_size = output_size;
	io.type = NCR_KEY_DATA;

	if (__ncr_file_descriptor < 0) {
		errno = EBADF;
		return -1;
	}

	if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_UPDATE, &io) < 0)
		 return -1;

	return io.data.kdata.output_size;
}

int
ncr_session_update_direct_data(ncr_session_t session, void *input, size_t input_size, void *output, size_t output_size)
{
	struct ncr_session_op_st io;
	memset(&io, 0, sizeof(io));

	if (session == NCR_SESSION_INVALID || !input || !input_size) {
		errno = EINVAL;
		return -1;
	}

	io.ses = session;
	io.data.udata.input = input;
	io.data.udata.input_size = input_size;
	io.data.udata.output = output;
	io.data.udata.output_size = output_size;
	io.type = NCR_DIRECT_DATA;

	if (__ncr_file_descriptor < 0) {
		errno = EBADF;
		return -1;
	}

	if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_UPDATE, &io) < 0)
		 return -1;

	return io.data.udata.output_size;
}

int
ncr_session_final(ncr_session_t session, void *output, size_t output_size)
{
	struct ncr_session_op_st io;
	memset(&io, 0, sizeof(io));

	if (session == NCR_SESSION_INVALID) {
		errno = EINVAL;
		return -1;
	}

	io.ses = session;
	io.data.udata.output = output;
	io.data.udata.output_size = output_size;
	io.type = NCR_DIRECT_DATA;

	if (__ncr_file_descriptor < 0) {
		errno = EBADF;
		return -1;
	}

	if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_FINAL, &io) < 0)
		 return -1;

	switch (io.err) {
	case NCR_VERIFICATION_FAILED:
		errno = EDOM;
		return -1;
        case NCR_SUCCESS:
		errno = 0;
		return io.data.udata.output_size;
        default:
		errno = EFAULT;
		return -1;
	}
}