summaryrefslogtreecommitdiffstats
path: root/cryptodev_main.c
blob: 733e9f16249848629c44d0b703e24d6dfb352148 (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
/*
 * Driver for /dev/crypto device (aka CryptoDev)
 *
 * Copyright (c) 2004 Michal Ludvig <mludvig@logix.net.nz>, SuSE Labs
 * Copyright (c) 2009,2010 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/highmem.h> */
/* #include <linux/ioctl.h> */
/* #include <linux/random.h> */
/* #include <linux/syscalls.h> */
/* #include <linux/uaccess.h> */
/* #include "cryptodev_int.h" */
/* #include <linux/version.h> */
#include <linux/crypto.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/net.h>
#include <linux/pagemap.h>
#include <linux/scatterlist.h>
#include <net/sock.h>
#include "alg.h"
#include "ncr-int.h"

MODULE_AUTHOR("Nikos Mavrogiannopoulos <nmav@gnutls.org>");
MODULE_DESCRIPTION("CryptoDev driver");
MODULE_LICENSE("GPL");

/* ====== Module parameters ====== */

int cryptodev_verbosity = 0;
module_param(cryptodev_verbosity, int, 0644);
MODULE_PARM_DESC(cryptodev_verbosity, "0: normal, 1: verbose, 2: debug");

/* ====== CryptoAPI ====== */

void release_user_pages(struct page **pg, int pagecount)
{
	while (pagecount--) {
		if (!PageReserved(pg[pagecount]))
			SetPageDirty(pg[pagecount]);
		page_cache_release(pg[pagecount]);
	}
}

/* offset of buf in it's first page */
#define PAGEOFFSET(buf) ((unsigned long)buf & ~PAGE_MASK)

/* fetch the pages addr resides in into pg and initialise sg with them */
int __get_userbuf(uint8_t __user * addr, uint32_t len, int write,
		  int pgcount, struct page **pg, struct scatterlist *sg)
{
	int ret, pglen, i = 0;
	struct scatterlist *sgp;

	down_write(&current->mm->mmap_sem);
	ret = get_user_pages(current, current->mm,
			     (unsigned long)addr, pgcount, write, 0, pg, NULL);
	up_write(&current->mm->mmap_sem);
	if (ret != pgcount)
		return -EINVAL;

	sg_init_table(sg, pgcount);

	pglen =
	    min((ptrdiff_t) (PAGE_SIZE - PAGEOFFSET(addr)), (ptrdiff_t) len);
	sg_set_page(sg, pg[i++], pglen, PAGEOFFSET(addr));

	len -= pglen;
	for (sgp = sg_next(sg); len; sgp = sg_next(sgp)) {
		pglen = min((uint32_t) PAGE_SIZE, len);
		sg_set_page(sgp, pg[i++], pglen, 0);
		len -= pglen;
	}
	sg_mark_end(sg_last(sg, pgcount));
	return 0;
}

/* ====== /dev/crypto ====== */

struct alg_sock {
	struct sock sk;
	struct sockaddr_alg addr;
	struct sock *queued;
	struct hash_data hash;
};

static struct alg_sock *alg_sk(struct sock *sk)
{
	return container_of(sk, struct alg_sock, sk);
}

static struct proto alg_proto = {
/* 	void			(*close)(struct sock *sk,  */
/* 					long timeout); */
/* 	int			(*connect)(struct sock *sk, */
/* 				        struct sockaddr *uaddr,  */
/* 					int addr_len); */
/* 	int			(*disconnect)(struct sock *sk, int flags); */

/* 	struct sock *		(*accept) (struct sock *sk, int flags, int *err); */

/* 	int			(*ioctl)(struct sock *sk, int cmd, */
/* 					 unsigned long arg); */
/* 	int			(*init)(struct sock *sk); */
/* 	void			(*destroy)(struct sock *sk); */
/* 	void			(*shutdown)(struct sock *sk, int how); */
/* 	int			(*setsockopt)(struct sock *sk, int level,  */
/* 					int optname, char __user *optval, */
/* 					unsigned int optlen); */
/* 	int			(*getsockopt)(struct sock *sk, int level,  */
/* 					int optname, char __user *optval,  */
/* 					int __user *option);  	  */
/* #ifdef CONFIG_COMPAT */
/* 	int			(*compat_setsockopt)(struct sock *sk, */
/* 					int level, */
/* 					int optname, char __user *optval, */
/* 					unsigned int optlen); */
/* 	int			(*compat_getsockopt)(struct sock *sk, */
/* 					int level, */
/* 					int optname, char __user *optval, */
/* 					int __user *option); */
/* #endif */
/* 	int			(*sendmsg)(struct kiocb *iocb, struct sock *sk, */
/* 					   struct msghdr *msg, size_t len); */
/* 	int			(*recvmsg)(struct kiocb *iocb, struct sock *sk, */
/* 					   struct msghdr *msg, */
/* 					size_t len, int noblock, int flags,  */
/* 					int *addr_len); */
/* 	int			(*sendpage)(struct sock *sk, struct page *page, */
/* 					int offset, size_t size, int flags); */
/* 	int			(*bind)(struct sock *sk,  */
/* 					struct sockaddr *uaddr, int addr_len); */

/* 	int			(*backlog_rcv) (struct sock *sk,  */
/* 						struct sk_buff *skb); */

/* 	/\* Keeping track of sk's, looking them up, and port selection methods. *\/ */
/* 	void			(*hash)(struct sock *sk); */
/* 	void			(*unhash)(struct sock *sk); */
/* 	int			(*get_port)(struct sock *sk, unsigned short snum); */

/* 	/\* Keeping track of sockets in use *\/ */
/* #ifdef CONFIG_PROC_FS */
/* 	unsigned int		inuse_idx; */
/* #endif */

/* 	/\* Memory pressure *\/ */
/* 	void			(*enter_memory_pressure)(struct sock *sk); */
/* 	atomic_t		*memory_allocated;	/\* Current allocated memory. *\/ */
/* 	struct percpu_counter	*sockets_allocated;	/\* Current number of sockets. *\/ */
/* 	/\* */
/* 	 * Pressure flag: try to collapse. */
/* 	 * Technical note: it is used by multiple contexts non atomically. */
/* 	 * All the __sk_mem_schedule() is of this nature: accounting */
/* 	 * is strict, actions are advisory and have some latency. */
/* 	 *\/ */
/* 	int			*memory_pressure; */
/* 	int			*sysctl_mem; */
/* 	int			*sysctl_wmem; */
/* 	int			*sysctl_rmem; */
/* 	int			max_header; */

	.obj_size = sizeof(struct alg_sock),
/* 	int			slab_flags; */

/* 	struct percpu_counter	*orphan_count; */

/* 	struct request_sock_ops	*rsk_prot; */
/* 	struct timewait_sock_ops *twsk_prot; */

/* 	union { */
/* 		struct inet_hashinfo	*hashinfo; */
/* 		struct udp_table	*udp_table; */
/* 		struct raw_hashinfo	*raw_hash; */
/* 	} h; */

 	.owner = THIS_MODULE,
	.name = "ALG",

/* 	struct list_head	node; */
/* #ifdef SOCK_REFCNT_DEBUG */
/* 	atomic_t		socks; */
/* #endif */
};

static int alg_release(struct socket *sock)
{
	struct sock *sk;
	struct alg_sock *ask;

	sk = sock->sk;
	if (sk == NULL)
		return 0;

	sock->sk = NULL;

	// skb_queue_purge(&sk->sk_write_queue);???

	ask = alg_sk(sk);
	if (ask->queued != NULL) {
		local_bh_disable();
		sock_prot_inuse_add(sock_net(ask->queued), &alg_proto, -1);
		local_bh_enable();
		sock_put(ask->queued);
	}

	if (ask->hash.init != 0)
		cryptodev_hash_deinit(&ask->hash);

	local_bh_disable();
	sock_prot_inuse_add(sock_net(sk), &alg_proto, -1);
	local_bh_enable();

	sock_put(sk);
	return 0;
}

static int alg_bind(struct socket *sock, struct sockaddr *myaddr,
		    int sockaddr_len)
{
	struct sockaddr_alg *addr;
	struct alg_sock *ask;

	if (myaddr->sa_family != AF_ALG || sockaddr_len < sizeof(*addr))
		return -EINVAL;
	addr = (struct sockaddr_alg *)myaddr;

	// FIXME: locking

	// FIXME
	if (strncmp(addr->salg_type, "hash", sizeof(addr->salg_type)) != 0)
		return -EINVAL;

	ask = alg_sk(sock->sk);
	ask->addr = *addr;
	return 0;
}

static int alg_accept(struct socket *sock, struct socket *newsock, int flags)
{
	struct alg_sock *ask;
	struct sock *newsk;

	// FIXME: locking
	ask = alg_sk(sock->sk);
	if (ask->queued == NULL)
		return -EINVAL;

	newsk = ask->queued;
	ask->queued = NULL;
	sock_graft(newsk, newsock);
	return 0;
}

static int alg_listen(struct socket *sock, int len)
{
	struct net *net;
	struct sock *newsk;
	struct alg_sock *ask, *newask;
	int res;

	// FIXME: locking
	net = sock_net(sock->sk);
	ask = alg_sk(sock->sk);
	if (ask->addr.salg_type[0] == 0)
		return -EINVAL; // FIXME: better error code for "not bound"?
	// FIXME: type-specific
	if (len != 1)
		return -EINVAL;

	if (ask->queued != NULL)
		return -EINVAL;

	newsk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto);
	if (newsk == NULL)
		return -ENOMEM;
	newask = alg_sk(newsk);
	// FIXME
	res = cryptodev_hash_init(&newask->hash, ask->addr.salg_tfm, NULL, 0);
	if (res != 0) {
		sock_put(newsk);
		return res;
	}

	sock_init_data(NULL, newsk);
	local_bh_disable();
	sock_prot_inuse_add(net, &alg_proto, 1);
	local_bh_enable();

	ask->queued = newsk;

	return 0;
}

static int alg_sendmsg(struct kiocb *iocb, struct socket *sock,
		       struct msghdr *m, size_t total_len)
{
	struct alg_sock *ask;
	char *buf;
	int res;

	// FIXME: locking
	ask = alg_sk(sock->sk);
	if (ask->hash.init == 0)
		return -EINVAL;

	// FIXME: limit size, or use socket buffer
	buf = kmalloc(total_len, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	res = memcpy_fromiovec(buf, m->msg_iov, total_len);
	if (res != 0)
		goto err;

	// FIXME
	res = _cryptodev_hash_update(&ask->hash, buf, total_len);
	if (res < 0)
		goto err;

	res = total_len;

err:
	kfree(buf);
	return res;
}

static int alg_recvmsg(struct kiocb *iocb, struct socket *sock,
		       struct msghdr *m, size_t total_len, int flags)
{
	char digest[NCR_HASH_MAX_OUTPUT_SIZE];
	struct alg_sock *ask;
	int res;

	// FIXME: locking
	ask = alg_sk(sock->sk);
	if (ask->hash.init == 0)
		return -EINVAL;
	if (total_len < ask->hash.digestsize)
		return -EINVAL;

	// FIXME
	BUG_ON(ask->hash.digestsize > sizeof(digest));
	res = cryptodev_hash_final(&ask->hash, digest);
	if (res < 0)
		return res;

	res = memcpy_toiovec(m->msg_iov, digest, ask->hash.digestsize);
	if (res != 0)
		return res;

	res = cryptodev_hash_reset(&ask->hash);
	if (res != 0)
		return res;

	return ask->hash.digestsize;
}

static const struct proto_ops alg_proto_ops = {
	.family = PF_ALG,
	.owner = THIS_MODULE,
	.release = alg_release,
	.bind = alg_bind,
	.connect = sock_no_connect,
	.socketpair = sock_no_socketpair,
	.accept = alg_accept,
	.getname = sock_no_getname,
	.poll = sock_no_poll,
	.ioctl = sock_no_ioctl,
	.compat_ioctl = NULL,
	.listen = alg_listen,
	.shutdown = sock_no_shutdown,
	.setsockopt = sock_no_setsockopt,
	.getsockopt = sock_no_getsockopt,
	.compat_setsockopt = NULL,
	.compat_getsockopt = NULL,
	.sendmsg = alg_sendmsg,
	.recvmsg = alg_recvmsg,
	.mmap = sock_no_mmap,
	.sendpage = NULL,
	.splice_read = NULL,
};

static int alg_create(struct net *net, struct socket *sock, int protocol,
		      int kern)
{
	struct sock *sk;

	if (sock->type != SOCK_STREAM)
		return -ESOCKTNOSUPPORT;
	if (protocol != 0)
		return -EPROTONOSUPPORT;

	sock->ops = &alg_proto_ops;

	sk = sk_alloc(net, PF_ALG, GFP_KERNEL, &alg_proto);
	if (sk == NULL)
		return -ENOMEM;

	sock_init_data(sock, sk);

	local_bh_disable();
	sock_prot_inuse_add(net, &alg_proto, 1);
	local_bh_enable();

	return 0;
}

static const struct net_proto_family alg_pf = {
	.family = PF_ALG,
	.create = alg_create,
	.owner = THIS_MODULE
};

static int __init init_cryptodev(void)
{
	int rc;

	ncr_master_key_reset();

	rc = proto_register(&alg_proto, 1);
	if (unlikely(rc != 0))
		goto err;

	rc = sock_register(&alg_pf);
	if (unlikely(rc != 0))
		goto err_proto;

	printk(KERN_INFO PFX "driver loaded.\n");

	return 0;

err_proto:
	proto_unregister(&alg_proto);
err:
	printk(KERN_ERR PFX "driver registration failed\n");
	return rc;
}

static void __exit exit_cryptodev(void)
{
	sock_unregister(PF_ALG);
	proto_unregister(&alg_proto);

	printk(KERN_INFO PFX "driver unloaded.\n");
}

module_init(init_cryptodev);
module_exit(exit_cryptodev);