summaryrefslogtreecommitdiffstats
path: root/ncr-key.c
blob: 4942bc451d081c1cfe6e936b420adc2ab8d65166 (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*
 * New driver for /dev/crypto device (aka CryptoDev)
 *
 * Copyright (c) 2010 Katholieke Universiteit Leuven
 *
 * Author: Nikos Mavrogiannopoulos <nmav@gnutls.org>
 *
 * This file is part of linux cryptodev.
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/ioctl.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/random.h>
#include <linux/uaccess.h>
#include <linux/scatterlist.h>
#include <net/netlink.h>
#include "ncr.h"
#include "ncr-int.h"
#include "utils.h"

static int key_list_deinit_fn(int id, void *item, void *unused)
{
	(void)unused;
	_ncr_key_item_put(item);
	return 0;
}

void ncr_key_list_deinit(struct ncr_lists *lst)
{
	/* The mutex is not necessary, but doesn't hurt and makes it easier to
	   verify locking correctness. */
	mutex_lock(&lst->key_idr_mutex);
	idr_for_each(&lst->key_idr, key_list_deinit_fn, NULL);
	idr_remove_all(&lst->key_idr);
	idr_destroy(&lst->key_idr);
	mutex_unlock(&lst->key_idr_mutex);
}

/* returns the data item corresponding to desc */
int ncr_key_item_get_read(struct key_item_st**st, struct ncr_lists *lst,
	ncr_key_t desc)
{
struct key_item_st* item;
int ret;
	
	*st = NULL;
	
	mutex_lock(&lst->key_idr_mutex);
	item = idr_find(&lst->key_idr, desc);
	if (item == NULL) {
		err();
		ret = -EINVAL;
		goto exit;
	}
	atomic_inc(&item->refcnt);

	if (atomic_read(&item->writer) != 0) {
		/* writer in place busy */
		atomic_dec(&item->refcnt);
		ret = -EBUSY;
		goto exit;
	}

	*st = item;
	ret = 0;

exit:
	mutex_unlock(&lst->key_idr_mutex);
	return ret;
}

/* as above but will never return anything that
 * is in use.
 */
int ncr_key_item_get_write( struct key_item_st** st, 
	struct ncr_lists *lst, ncr_key_t desc)
{
struct key_item_st* item;
int ret;

	*st = NULL;

	mutex_lock(&lst->key_idr_mutex);
	item = idr_find(&lst->key_idr, desc);
	if (item == NULL) {
		err();
		ret = -EINVAL;
		goto exit;
	}
	/* do not return items that are in use already */

	if (atomic_add_unless(&item->writer, 1, 1)==0) {
		/* another writer so busy */
		ret = -EBUSY;
		goto exit;
	}

	if (atomic_add_unless(&item->refcnt, 1, 2)==0) {
		/* some reader is active so busy */
		atomic_dec(&item->writer);
		ret = -EBUSY;
		goto exit;
	}

	*st = item;
	ret = 0;

exit:
	mutex_unlock(&lst->key_idr_mutex);
	return ret;
}

void _ncr_key_item_put( struct key_item_st* item)
{
	if (atomic_read(&item->writer) > 0)
		atomic_dec(&item->writer);
	if (atomic_dec_and_test(&item->refcnt)) {
			ncr_limits_remove(item->uid, item->pid, LIMIT_TYPE_KEY);
			ncr_key_clear(item);
			kfree(item);
	}
}

static void _ncr_key_remove(struct ncr_lists *lst, ncr_key_t desc)
{
	struct key_item_st * item;

	mutex_lock(&lst->key_idr_mutex);
	item = idr_find(&lst->key_idr, desc);
	if (item != NULL)
		idr_remove(&lst->key_idr, desc); /* Steal the reference */
	mutex_unlock(&lst->key_idr_mutex);

	if (item != NULL)
		_ncr_key_item_put(item);
}

int ncr_key_init(struct ncr_lists *lst)
{
	ncr_key_t desc;
	struct key_item_st* key;
	int ret;

	ret = ncr_limits_add_and_check(current_euid(), task_pid_nr(current), LIMIT_TYPE_KEY);
	if (ret < 0) {
		err();
		return ret;
	}

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

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

	atomic_set(&key->refcnt, 1);
	atomic_set(&key->writer, 0);
	key->uid = current_euid();
	key->pid = task_pid_nr(current);

	mutex_lock(&lst->key_idr_mutex);
	/* idr_pre_get() should preallocate enough, and, due to key_idr_mutex,
	   nobody else can use the preallocated data.  Therefore the loop
	   recommended in idr_get_new() documentation is not necessary. */
	if (idr_pre_get(&lst->key_idr, GFP_KERNEL) == 0 ||
	    idr_get_new(&lst->key_idr, key, &key->desc) != 0) {
		mutex_unlock(&lst->key_idr_mutex);
		_ncr_key_item_put(key);
		return -ENOMEM;
	}
	desc = key->desc;
	mutex_unlock(&lst->key_idr_mutex);

	return desc;

err_limits:
	ncr_limits_remove(current_euid(), task_pid_nr(current), LIMIT_TYPE_KEY);
	return ret;
}

int ncr_key_deinit(struct ncr_lists *lst, ncr_key_t desc)
{
	_ncr_key_remove(lst, desc);
	return 0;
}

int ncr_key_export(struct ncr_lists *lst, const struct ncr_key_export *data,
		   struct nlattr *tb[])
{
struct key_item_st* item = NULL;
void* tmp = NULL;
uint32_t tmp_size;
int ret;

	if (data->buffer_size < 0) {
		err();
		return -EINVAL;
	}

	ret = ncr_key_item_get_read(&item, lst, data->key);
	if (ret < 0) {
		err();
		return ret;
	}

	if (!(item->flags & NCR_KEY_FLAG_EXPORTABLE)) {
		err();
		ret = -EPERM;
		goto fail;
	}

	switch (item->type) {
		case NCR_KEY_TYPE_SECRET:
			if (item->key.secret.size > data->buffer_size) {
				err();
				ret = -ERANGE;
				goto fail;
			}

			/* found */
			if (item->key.secret.size > 0) {
				ret = copy_to_user(data->buffer, item->key.secret.data, item->key.secret.size);
				if (unlikely(ret)) {
					err();
					ret = -EFAULT;
					goto fail;
				}
			}

			ret = item->key.secret.size;
			break;
		case NCR_KEY_TYPE_PUBLIC:
		case NCR_KEY_TYPE_PRIVATE:
			tmp_size = data->buffer_size;
			
			tmp = kmalloc(tmp_size, GFP_KERNEL);
			if (tmp == NULL) {
				err();
				ret = -ENOMEM;
				goto fail;
			}

			ret = ncr_pk_pack(item, tmp, &tmp_size);
			if (ret < 0) {
				err();
				goto fail;
			}

			ret = copy_to_user(data->buffer, tmp, tmp_size);
			if (unlikely(ret)) {
				err();
				ret = -EFAULT;
				goto fail;
			}
			
			ret = tmp_size;
			break;
		default:
			err();
			ret = -EINVAL;
			goto fail;
	}

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

int ncr_key_assign_flags(struct key_item_st* item, unsigned int flags)
{
	if (!capable(CAP_SYS_ADMIN) && (flags & NCR_KEY_FLAG_WRAPPING) != 0)
		return -EPERM;
	item->flags = flags;
	return 0;
}

int ncr_key_import(struct ncr_lists *lst, const struct ncr_key_import *data,
		   struct nlattr *tb[])
{
const struct nlattr *nla;
struct key_item_st* item = NULL;
int ret;
void* tmp = NULL;
size_t tmp_size;

	ret = ncr_key_item_get_write( &item, lst, data->key);
	if (ret < 0) {
		err();
		return ret;
	}

	ncr_key_clear(item);

	tmp = kmalloc(data->data_size, GFP_KERNEL);
	if (tmp == NULL) {
		err();
		ret = -ENOMEM;
		goto fail;
	}
	
	if (unlikely(copy_from_user(tmp, data->data, data->data_size))) {
		err();
		ret = -EFAULT;
		goto fail;
	}
	tmp_size = data->data_size;

	nla = tb[NCR_ATTR_KEY_TYPE];
	if (tb == NULL) {
		err();
		ret = -EINVAL;
		goto fail;
	}
	item->type = nla_get_u32(nla);

	item->algorithm = _ncr_nla_to_properties(tb[NCR_ATTR_ALGORITHM]);
	if (item->algorithm == NULL) {
		err();
		ret = -EINVAL;
		goto fail;
	}

	nla = tb[NCR_ATTR_KEY_FLAGS];
	if (nla != NULL) {
		ret = ncr_key_assign_flags(item, nla_get_u32(nla));
		if (ret < 0) {
			err();
			goto fail;
		}
	}

	nla = tb[NCR_ATTR_KEY_ID];
	if (nla != NULL) {
		if (nla_len(nla) > MAX_KEY_ID_SIZE) {
			err();
			ret = -EOVERFLOW;
			goto fail;
		}

		item->key_id_size = nla_len(nla);
		memcpy(item->key_id, nla_data(nla), item->key_id_size);
	}

	switch(item->type) {
		case NCR_KEY_TYPE_SECRET:
			if (tmp_size > NCR_CIPHER_MAX_KEY_LEN) {
				err();
				ret = -EINVAL;
				goto fail;
			}
			
			memcpy(item->key.secret.data, tmp, tmp_size);
			item->key.secret.size = tmp_size;
			break;
		case NCR_KEY_TYPE_PRIVATE:
		case NCR_KEY_TYPE_PUBLIC:
			ret = ncr_pk_unpack( item, tmp, tmp_size);
			if (ret < 0) {
				err();
				goto fail;
			}
			break;

		default:
			err();
			ret = -EINVAL;
			goto fail;
	}

	ret = 0;

fail:
	if (item)
		_ncr_key_item_put(item);
	kfree(tmp);

	return ret;
}

void ncr_key_clear(struct key_item_st* item)
{
	/* clears any previously allocated parameters */
	if (item->type == NCR_KEY_TYPE_PRIVATE ||
		item->type == NCR_KEY_TYPE_PUBLIC) {
		
		ncr_pk_clear(item);
	}
	memset(&item->key, 0, sizeof(item->key));
	memset(item->key_id, 0, sizeof(item->key_id));
	item->key_id_size = 0;
	item->flags = 0;
	
	return;
}

/* Generate a secret key
 */
int ncr_key_generate(struct ncr_lists *lst, const struct ncr_key_generate *gen,
		     struct nlattr *tb[])
{
const struct nlattr *nla;
struct key_item_st* item = NULL;
const struct algo_properties_st *algo;
int ret;
size_t size;

	ret = ncr_key_item_get_write(&item, lst, gen->key);
	if (ret < 0) {
		err();
		return ret;
	}

	ncr_key_clear(item);

	/* we generate only secret keys */
	nla = tb[NCR_ATTR_KEY_FLAGS];
	if (nla != NULL) {
		ret = ncr_key_assign_flags(item, nla_get_u32(nla));
		if (ret < 0) {
			err();
			goto fail;
		}
	}

	algo = _ncr_nla_to_properties(tb[NCR_ATTR_ALGORITHM]);
	if (algo == NULL) {
		err();
		ret = -EINVAL;
		goto fail;
	}
	item->type = algo->key_type;
	if (item->type == NCR_KEY_TYPE_SECRET) {
		u32 key_bits;

		item->algorithm = algo;

		nla = tb[NCR_ATTR_SECRET_KEY_BITS];
		if (nla == NULL) {
			err();
			ret = -EINVAL;
			goto fail;
		}
		key_bits = nla_get_u32(nla);
		size = key_bits / 8;
		if (key_bits % 8 != 0 || size > NCR_CIPHER_MAX_KEY_LEN) {
			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);
	} else {
		err();
		ret = -EINVAL;
		goto fail;
	}
	
	ret = 0;

fail:
	if (item) {
		if (ret < 0) item->type = NCR_KEY_TYPE_INVALID;
		_ncr_key_item_put(item);
	}
	return ret;
}

/* Those values are derived from "ECRYPT II Yearly Report on Algorithms and
 * Keysizes (2009-2010)". It maps the strength of public key algorithms to 
 * symmetric ones. Should be kept up to date.
 */
struct {
	unsigned int bits; /* sec level */
	unsigned int rsa_bits;
	unsigned int dlog_bits;
} ecrypt_vals[] = {
	{64, 816, 816},
	{80, 1248, 1248},
	{112, 2432, 2432},
	{128, 3248, 3248},
	{160, 5312, 5312},
	{192, 7936, 7936},
	{256, 15424, 15424},
	{0,0,0}
};

unsigned int rsa_to_bits(unsigned int rsa_bits)
{
int i = 1;

	if (rsa_bits <= ecrypt_vals[0].rsa_bits)
		return ecrypt_vals[0].rsa_bits;

	do {
		if (rsa_bits <= ecrypt_vals[i].rsa_bits && 
			rsa_bits > ecrypt_vals[i-1].rsa_bits) {

			return ecrypt_vals[i].bits;
		}
	} while(ecrypt_vals[++i].bits != 0);
	
	/* return the highest found so far */
	return ecrypt_vals[i-1].bits;
}

unsigned int dlog_to_bits(unsigned int dlog_bits)
{
int i = 1;

	if (dlog_bits <= ecrypt_vals[0].dlog_bits)
		return ecrypt_vals[0].dlog_bits;

	do {
		if (dlog_bits <= ecrypt_vals[i].dlog_bits && 
			dlog_bits > ecrypt_vals[i-1].dlog_bits) {

			return ecrypt_vals[i].bits;
		}
	} while(ecrypt_vals[++i].bits != 0);
	
	/* return the highest found so far */
	return ecrypt_vals[i-1].bits;
}

/* returns the security level of the key in bits. Private/Public keys
 * are mapped to symmetric key bits using the ECRYPT II 2010 recommendation.
 */
int _ncr_key_get_sec_level(struct key_item_st* item)
{
int bits;

	/* FIXME: should we move everything here into algorithm properties? 
	 */
	if (item->type == NCR_KEY_TYPE_SECRET) {
		if (item->algorithm->algo == NCR_ALG_3DES_CBC)
			return 112;

		return item->key.secret.size*8;
	} else if (item->type == NCR_KEY_TYPE_PRIVATE) {
		switch(item->algorithm->algo) {
			case NCR_ALG_RSA:
				bits = ncr_pk_get_rsa_size(&item->key.pk.rsa);
				if (bits < 0) {
					err();
					return bits;
				}
				
				return rsa_to_bits(bits);
			case NCR_ALG_DSA:
				bits = ncr_pk_get_dsa_size(&item->key.pk.dsa);
				if (bits < 0) {
					err();
					return bits;
				}
				
				return dlog_to_bits(bits);
			case NCR_ALG_DH:
				bits = ncr_pk_get_dh_size(&item->key.pk.dh);
				if (bits < 0) {
					err();
					return bits;
				}
				
				return dlog_to_bits(bits);
			default:
				return -EINVAL;
		}
	} else {
		return -EINVAL;
	}
}

int ncr_key_get_info(struct ncr_lists *lst, struct ncr_out *out,
		     const struct ncr_key_get_info *info, struct nlattr *tb[])
{
const struct nlattr *nla;
const u16 *attr, *attr_end;
struct key_item_st* item = NULL;
int ret;

	ret = ncr_key_item_get_read(&item, lst, info->key);
	if (ret < 0) {
		err();
		return ret;
	}
	
	if (item->type == NCR_KEY_TYPE_INVALID) {
		err();
		ret = -EINVAL;
		goto fail;
	}

	nla = tb[NCR_ATTR_WANTED_ATTRS];
	if (nla == NULL || nla_len(nla) % sizeof(u16) != 0) {
		err();
		ret = -EINVAL;
		goto fail;
	}
	attr = nla_data(nla);
	attr_end = attr + nla_len(nla) / sizeof(u16);
	while (attr < attr_end) {
		switch (*attr) {
		case NCR_ATTR_KEY_FLAGS:
			ret = ncr_out_put_u32(out, *attr, item->flags);
			break;
		case NCR_ATTR_KEY_TYPE:
			ret = ncr_out_put_u32(out, *attr, item->type);
			break;
		case NCR_ATTR_ALGORITHM:
			ret = ncr_out_put_string(out, *attr,
						 item->algorithm->kstr);
			break;
		default:
			break; /* Silently ignore */
		}
		if (ret != 0) {
			err();
			goto fail;
		}
		attr++;
	}

	ret = ncr_out_finish(out);
	if (ret != 0) {
		err();
		goto fail;
	}

fail:
	_ncr_key_item_put( item);

	return ret;
}

int ncr_key_generate_pair(struct ncr_lists *lst,
			  const struct ncr_key_generate_pair *gen,
			  struct nlattr *tb[])
{
const struct nlattr *nla;
struct key_item_st* private = NULL;
struct key_item_st* public = NULL;
int ret;

	ret = ncr_key_item_get_write(&private, lst, gen->private_key);
	if (ret < 0) {
		err();
		goto fail;
	}

	ret = ncr_key_item_get_write(&public, lst, gen->public_key);
	if (ret < 0) {
		err();
		goto fail;
	}

	ncr_key_clear(public);
	ncr_key_clear(private);

	/* we generate only secret keys */
	private->algorithm = public->algorithm
		= _ncr_nla_to_properties(tb[NCR_ATTR_ALGORITHM]);
	if (private->algorithm == NULL) {
		err();
		ret = -EINVAL;
		goto fail;
	}
	public->type = public->algorithm->key_type;
	private->type = NCR_KEY_TYPE_PRIVATE;
	nla = tb[NCR_ATTR_KEY_FLAGS];
	if (nla != NULL) {
		ret = ncr_key_assign_flags(private, nla_get_u32(nla));
		if (ret < 0) {
			err();
			goto fail;
		}
		ret = ncr_key_assign_flags(public, nla_get_u32(nla));
		if (ret < 0) {
			err();
			goto fail;
		}
	}

	public->flags |= (NCR_KEY_FLAG_EXPORTABLE|NCR_KEY_FLAG_WRAPPABLE);
	
	if (public->type == NCR_KEY_TYPE_PUBLIC) {
		ret = ncr_pk_generate(public->algorithm, tb, private, public);
		if (ret < 0) {
			err();
			goto fail;
		}
	} else {
		err();
		ret = -EINVAL;
		goto fail;
	}
	
	ret = 0;
fail:
	if (public) {
		if (ret < 0) public->type = NCR_KEY_TYPE_INVALID;
		_ncr_key_item_put(public);
	}
	if (private) {
		if (ret < 0) private->type = NCR_KEY_TYPE_INVALID;
		_ncr_key_item_put(private);
	}
	return ret;
}

int ncr_key_derive(struct ncr_lists *lst, const struct ncr_key_derive *data,
		   struct nlattr *tb[])
{
const struct nlattr *nla;
int ret;
struct key_item_st* key = NULL;
struct key_item_st* newkey = NULL;

	ret = ncr_key_item_get_read(&key, lst, data->input_key);
	if (ret < 0) {
		err();
		return ret;
	}
	
	/* wrapping keys cannot be used for anything except wrapping.
	 */
	if (key->flags & NCR_KEY_FLAG_WRAPPING) {
		err();
		ret = -EINVAL;
		goto fail;
	}

	ret = ncr_key_item_get_write(&newkey, lst, data->new_key);
	if (ret < 0) {
		err();
		goto fail;
	}

	ncr_key_clear(newkey);

	nla = tb[NCR_ATTR_KEY_FLAGS];
	if (nla != NULL) {
		ret = ncr_key_assign_flags(newkey, nla_get_u32(nla));
		if (ret < 0) {
			err();
			goto fail;
		}
	}

	switch (key->type) {
		case NCR_KEY_TYPE_PUBLIC:
		case NCR_KEY_TYPE_PRIVATE:
			ret = ncr_pk_derive(newkey, key, tb);
			if (ret < 0) {
				err();
				goto fail;
			}
			break;
		default:
			err();
			ret = -EINVAL;
			goto fail;
	}

fail:
	if (key)
		_ncr_key_item_put(key);
	if (newkey)
		_ncr_key_item_put(newkey);
	return ret;
	
}