summaryrefslogtreecommitdiffstats
path: root/userspace/ncrypto_key.c
blob: db46ee43e009f7e8f6e36ae1d7903e2a85399223 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

#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_key_init(ncr_key_t *key)
{
	if ((__ncr_file_descriptor < 0) && (ncr_global_init(0) < 0))
		return -1;

	if (ioctl(__ncr_file_descriptor, NCRIO_KEY_INIT, key) < 0)
		return -1;

	return 0;
}

int
ncr_key_generate(ncr_key_t key, ncr_key_generate_params_t params)
{
	struct ncr_key_generate_st io;
	memset(&io, 0, sizeof(io));

	if (key == NCR_KEY_INVALID) {
		errno = EINVAL;
		return -1;
	}

	io.desc = key;
	if (params)
	    memmove(&io.params, params, sizeof(io.params));
	
	if (__ncr_file_descriptor < 0) {
		errno = EBADF;
		return -1;
	}

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

	return 0;
}

int
ncr_key_generate_pair(ncr_key_t key1, ncr_key_t key2, ncr_key_generate_params_t params)
{
	struct ncr_key_generate_st io;
	memset(&io, 0, sizeof(io));

	if (key1 == NCR_KEY_INVALID|| key2 == NCR_KEY_INVALID) {
		errno = EINVAL;
		return -1;
	}

	io.desc = key1;
	io.desc2 = key2;
	if (params)
	    memmove(&io.params, params, sizeof(io.params));
	
	if (__ncr_file_descriptor < 0) {
		errno = EBADF;
		return -1;
	}

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

	return 0;
}

int
ncr_key_derive(ncr_key_t newkey, unsigned int keyflags, ncr_key_t key, ncr_key_params_t params)
{
	struct ncr_key_derivation_params_st io;
	memset(&io, 0, sizeof(io));

	if (newkey == NCR_KEY_INVALID || key == NCR_KEY_INVALID) {
		errno = EINVAL;
		return -1;
	}

	io.newkey = newkey;
	io.key = key;
	io.keyflags = keyflags;
	if (params)
	    memmove(&io.params, params, sizeof(io.params));

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

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

	return 0;
}

static int
ncr_key_get_info(struct ncr_key_info_st *dest, ncr_key_t key)
{
	if (key == NCR_KEY_INVALID) {
		errno = EINVAL;
		return -1;
	}

	memset(dest, 0, sizeof(*dest));
	dest->key = key;

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

	if (ioctl(__ncr_file_descriptor, NCRIO_KEY_GET_INFO, dest) < 0)
		return -1;

	return 0;
}

ncr_algorithm_t
ncr_key_get_algorithm(ncr_key_t key)
{
	struct ncr_key_info_st io;

	if (ncr_key_get_info(&io, key) < 0)
		return -1;
	return io.algorithm;
}

int
ncr_key_get_flags(ncr_key_t key)
{
	struct ncr_key_info_st io;

	if (ncr_key_get_info(&io, key) < 0)
		return -1;
	return io.flags;
}

ncr_key_type_t
ncr_key_get_type(ncr_key_t key)
{
	struct ncr_key_info_st io;

	if (ncr_key_get_info(&io, key) < 0)
		return -1;
	return io.type;
}

int
ncr_key_get_id(ncr_key_t key, void *id, size_t *id_size)
{
	struct ncr_key_info_st io;

	if (ncr_key_get_info(&io, key) < 0)
		return -1;

	if (io.key_id_size < *id_size)
		*id_size = io.key_id_size;

	memmove(id, &io.key_id, *id_size);

	return 0;
}

int
ncr_key_export(ncr_key_t key, void *idata, size_t idata_size)
{
	struct ncr_key_data_st io;
	memset(&io, 0, sizeof(io));

	if (key == NCR_KEY_INVALID || !idata || !idata_size) {
		errno = EINVAL;
		return -1;
	}

	io.key = key;
	io.idata = idata;
	io.idata_size = idata_size;

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

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

	return io.idata_size;
}

int
ncr_key_import(ncr_key_t key, void *idata, size_t idata_size, void *id, size_t id_size, ncr_algorithm_t algorithm, unsigned int type, unsigned int flags)
{
	struct ncr_key_data_st io;
	memset(&io, 0, sizeof(io));

	if (key == NCR_KEY_INVALID || !idata || !idata_size || (!id && id_size != 0) || algorithm == NCR_ALG_NONE) {
		errno = EINVAL;
		return -1;
	}

	io.key = key;
	io.idata = idata;
	io.idata_size = idata_size;
	if (id_size > MAX_KEY_ID_SIZE) {
		errno = EOVERFLOW;
		return -1;
	}
	memmove(&io.key_id, id, id_size);
	io.key_id_size = id_size;
	io.algorithm = algorithm;
	io.type = type;
	io.flags = flags;
	
	if (__ncr_file_descriptor < 0) {
		errno = EBADF;
		return -1;
	}

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

	return 0;
}

int
ncr_key_wrap(ncr_key_t key, ncr_wrap_algorithm_t algorithm, ncr_key_params_t params, ncr_key_t keytowrap, void *idata, size_t idata_size)
{
	struct ncr_key_wrap_st io;
	memset(&io, 0, sizeof(io));

	if (key == NCR_KEY_INVALID || keytowrap == NCR_KEY_INVALID || !idata || !idata_size) {
		errno = EINVAL;
		return -1;
	}

        io.key = key;
        io.algorithm = algorithm;
	if (params)
        	memmove(&io.params, params, sizeof(io.params));
        io.keytowrap = keytowrap;
        io.io = idata;
        io.io_size = idata_size;

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

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

	return io.io_size;
}

int
ncr_key_unwrap(ncr_key_t key, ncr_wrap_algorithm_t algorithm, ncr_key_params_t params, ncr_key_t keytowrap, void *idata, size_t idata_size)
{
	struct ncr_key_wrap_st io;
	memset(&io, 0, sizeof(io));

	if (key == NCR_KEY_INVALID || keytowrap == NCR_KEY_INVALID || !idata || !idata_size) {
		errno = EINVAL;
		return -1;
	}

        io.key = key;
        io.algorithm = algorithm;
	if (params)
        	memmove(&io.params, params, sizeof(io.params));
        io.keytowrap = keytowrap;
        io.io = idata;
        io.io_size = idata_size;

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

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

	return 0;
}

int
ncr_key_storage_wrap(ncr_key_t keytowrap, void *idata, size_t idata_size)
{
	struct ncr_key_storage_wrap_st io;
	memset(&io, 0, sizeof(io));

	if (keytowrap == NCR_KEY_INVALID || !idata || !idata_size) {
		errno = EINVAL;
		return -1;
	}

        io.keytowrap = keytowrap;
        io.io = idata;
        io.io_size = idata_size;

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

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

	return io.io_size;
}

int
ncr_key_storage_unwrap(ncr_key_t keytowrap, void *idata, size_t idata_size)
{
	struct ncr_key_storage_wrap_st io;
	memset(&io, 0, sizeof(io));

	if (keytowrap == NCR_KEY_INVALID|| !idata || !idata_size) {
		errno = EINVAL;
		return -1;
	}

        io.keytowrap = keytowrap;
        io.io = idata;
        io.io_size = idata_size;

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

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

	return 0;
}

int
ncr_key_deinit(ncr_key_t key)
{
	if (key == NCR_KEY_INVALID) {
		errno = EINVAL;
		return -1;
	}

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

	if (ioctl(__ncr_file_descriptor, NCRIO_KEY_DEINIT, &key) < 0)
		return -1;

	return 0;
}