summaryrefslogtreecommitdiffstats
path: root/ncr-key.c
blob: 4a93a4aa29912704892b78f7e381a2f4fca545ae (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * New driver for /dev/crypto device (aka CryptoDev)

 * Copyright (c) 2010 Nikos Mavrogiannopoulos <nmav@gnutls.org>
 *
 * This file is part of linux cryptodev.
 *
 * cryptodev is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * cryptodev is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/mm.h>
#include <linux/highmem.h>
#include <linux/random.h>
#include "cryptodev.h"
#include <asm/uaccess.h>
#include <asm/ioctl.h>
#include <linux/scatterlist.h>
#include "ncr.h"
#include "ncr_int.h"

void ncr_key_list_deinit(struct list_sem_st* lst)
{
	if(lst) {
		struct key_item_st * item, *tmp;

		down(&lst->sem);

		list_for_each_entry_safe(item, tmp, &lst->list, list) {
			list_del(&item->list);
			_ncr_key_item_put( item); /* decrement ref count */
		}
		up(&lst->sem);
	}
}

/* must be called with data semaphore down
 */
static ncr_key_t _ncr_key_get_new_desc( struct list_sem_st* lst)
{
struct key_item_st* item;
int mx = 0;

	list_for_each_entry(item, &lst->list, list) {
		mx = max(mx, item->desc);
	}
	mx++;

	return mx;
}

/* returns the data item corresponding to desc */
struct key_item_st* ncr_key_item_get( struct list_sem_st* lst, ncr_key_t desc)
{
struct key_item_st* item;

	down(&lst->sem);
	list_for_each_entry(item, &lst->list, list) {
		if (item->desc == desc) {
			atomic_inc(&item->refcnt);
			up(&lst->sem);
			return item;
		}
	}
	up(&lst->sem);

	err();
	return NULL;
}

void _ncr_key_item_put( struct key_item_st* item)
{
	if (atomic_dec_and_test(&item->refcnt)) {
			ncr_limits_remove(item->filp, LIMIT_TYPE_KEY);
			kfree(item);
	}
}

int ncr_key_init(struct file *filp, struct list_sem_st* lst, void __user* arg)
{
	ncr_key_t desc;
	struct key_item_st* key;
	int ret;

	ret = ncr_limits_add_and_check(filp, LIMIT_TYPE_KEY);
	if (ret < 0) {
		err();
		return ret;
	}

	copy_from_user( &desc, arg, sizeof(desc));

	key = kmalloc(sizeof(*key), GFP_KERNEL);
	if (key == NULL) {
		err();
		return -ENOMEM;
	}

	memset(key, 0, sizeof(*key));

	atomic_set(&key->refcnt, 1);

	down(&lst->sem);

	key->desc = _ncr_key_get_new_desc(lst);
	key->filp = filp;

	list_add(&key->list, &lst->list);
	
	up(&lst->sem);

	desc = key->desc;
	copy_to_user(arg, &desc, sizeof(desc));

	return 0;
}


int ncr_key_deinit(struct list_sem_st* lst, void __user* arg)
{
	ncr_key_t desc;
	struct key_item_st * item, *tmp;

	copy_from_user( &desc, arg, sizeof(desc));

	down(&lst->sem);
	
	list_for_each_entry_safe(item, tmp, &lst->list, list) {
		if(item->desc == desc) {
			list_del(&item->list);
			_ncr_key_item_put( item); /* decrement ref count */
			break;
		}
	}
	
	up(&lst->sem);

	return 0;
}

/* "exports" a key to a data item. If the key is not exportable
 * to userspace then the data item will also not be.
 */
int ncr_key_export(struct list_sem_st* data_lst,
	struct list_sem_st* key_lst, void __user* arg)
{
struct ncr_key_data_st data;
struct key_item_st* item = NULL;
struct data_item_st* ditem = NULL;
int ret;

	copy_from_user( &data, arg, sizeof(data));

	item = ncr_key_item_get( key_lst, data.key);
	if (item == NULL) {
		err();
		return -EINVAL;
	}

	ditem = ncr_data_item_get( data_lst, data.data);
	if (ditem == NULL) {
		err();
		ret = -EINVAL;
		goto fail;
	}

	switch (item->type) {
		case NCR_KEY_TYPE_SECRET:
			if (item->key.secret.size > ditem->max_data_size) {
				err();
				ret = -EINVAL;
				goto fail;
			}

			/* found */
			ditem->flags = 0;
			if ((item->flags & NCR_KEY_FLAG_EXPORTABLE)) {
				ditem->flags |= NCR_DATA_FLAG_EXPORTABLE;
			}

			if (item->key.secret.size > 0) {
				memcpy(ditem->data, item->key.secret.data, item->key.secret.size);
			}
			ditem->data_size = item->key.secret.size;
			break;
		case NCR_KEY_TYPE_PUBLIC: /* FIXME: export in a blob -ASN.1? */
		case NCR_KEY_TYPE_PRIVATE: /* FIXME export in a blob -ASN.1? */
		default:
			err();
			ret = -EINVAL;
			goto fail;
	}

	_ncr_key_item_put( item);
	_ncr_data_item_put( ditem);

	return 0;

fail:
	if (item)
		_ncr_key_item_put(item);
	if (ditem)
		_ncr_data_item_put(ditem);
	return ret;
	
}

/* "imports" a key from a data item. If the key is not exportable
 * to userspace then the key item will also not be.
 */
int ncr_key_import(struct list_sem_st* data_lst,
	struct list_sem_st* key_lst, void __user* arg)
{
struct ncr_key_data_st data;
struct key_item_st* item = NULL;
struct data_item_st* ditem = NULL;
int ret;

	copy_from_user( &data, arg, sizeof(data));

	item = ncr_key_item_get( key_lst, data.key);
	if (item == NULL) {
		err();
		return -EINVAL;
	}

	ditem = ncr_data_item_get( data_lst, data.data);
	if (ditem == NULL) {
		err();
		ret = -EINVAL;
		goto fail;
	}

	item->type = data.type;
	item->algorithm = data.algorithm;
	item->flags = data.flags;
	/* if data cannot be exported then the flags above
	 * should be overriden */
	if ((ditem->flags & NCR_DATA_FLAG_EXPORTABLE)) {
		item->flags |= NCR_KEY_FLAG_EXPORTABLE;
	}

	switch(item->type) {
		case NCR_KEY_TYPE_SECRET:

			if (data.key_id_size > MAX_KEY_ID_SIZE) {
				err();
				ret = -EINVAL;
				goto fail;
			}
			item->key_id_size = data.key_id_size;
			if (data.key_id_size > 0)
				memcpy(item->key_id, data.key_id, data.key_id_size);


			if (ditem->data_size > MAX_KEY_SIZE) {
				err();
				ret = -EINVAL;
				goto fail;
			}
			
			memcpy(item->key.secret.data, ditem->data, ditem->data_size);
			item->key.secret.size = ditem->data_size;
			break;
		case NCR_KEY_TYPE_PRIVATE: /* FIXME */
		case NCR_KEY_TYPE_PUBLIC: /* FIXME */
		default:
			err();
			ret = -EINVAL;
			goto fail;
	}

	_ncr_key_item_put( item);
	_ncr_data_item_put( ditem);

	return 0;

fail:
	if (item)
		_ncr_key_item_put(item);
	if (ditem)
		_ncr_data_item_put(ditem);
	return ret;
}

/* Generate a secret key
 */
int ncr_key_generate(struct list_sem_st* lst, void __user* arg)
{
struct ncr_key_generate_st gen;
struct key_item_st* item = NULL;
int ret;
size_t size;

	copy_from_user( &gen, arg, sizeof(gen));

	item = ncr_key_item_get( lst, gen.desc);
	if (item == NULL) {
		err();
		return -EINVAL;
	}

	/* we generate only secret keys */
	item->type = ncr_algorithm_to_key_type(gen.params.algorithm);
	if (item->type != NCR_KEY_TYPE_SECRET) {
		err();
		ret = -EINVAL;
		goto fail;
	}

	item->flags = gen.params.keyflags;
	item->algorithm = /* arbitrary */ NCR_ALG_AES_CBC;

	size = gen.params.params.secret.bits/8;
	if ((gen.params.params.secret.bits % 8 != 0) ||
			(size > MAX_KEY_SIZE)) {
		err();
		ret = -EINVAL;
		goto fail;
	}

	get_random_bytes(item->key.secret.data, size);
	item->key.secret.size = size;

	/* generate random key id */
	item->key_id_size = 5;
	get_random_bytes(item->key_id, item->key_id_size);
	
	_ncr_key_item_put( item);

	return 0;

fail:
	if (item)
		_ncr_key_item_put(item);
	return ret;
}

int ncr_key_info(struct list_sem_st* lst, void __user* arg)
{
struct ncr_key_info_st info;
struct key_item_st* item = NULL;

	copy_from_user( &info, arg, sizeof(info));

	item = ncr_key_item_get( lst, info.key);
	if (item == NULL) {
		err();
		return -EINVAL;
	}

	info.flags = item->flags;
	info.type = item->type;
	info.algorithm = item->algorithm;

	_ncr_key_item_put( item);

	return 0;
}

/* FIXME those require public key subsystem */
int ncr_key_generate_pair(struct list_sem_st* lst, void __user* arg)
{
	return -EINVAL;
}

int ncr_key_derive(struct list_sem_st* lst, void __user* arg)
{
	return -EINVAL;
}

int ncr_key_get_public(struct list_sem_st* lst, void __user* arg)
{
	return -EINVAL;
}