summaryrefslogtreecommitdiffstats
path: root/cryptodev_main.c
diff options
context:
space:
mode:
Diffstat (limited to 'cryptodev_main.c')
-rw-r--r--cryptodev_main.c414
1 files changed, 350 insertions, 64 deletions
diff --git a/cryptodev_main.c b/cryptodev_main.c
index ebdb523..4b099e9 100644
--- a/cryptodev_main.c
+++ b/cryptodev_main.c
@@ -21,29 +21,23 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-/*
- * Device /dev/crypto provides an interface for
- * accessing kernel CryptoAPI algorithms (ciphers,
- * hashes) from userspace programs.
- *
- * /dev/crypto interface was originally introduced in
- * OpenBSD and this module attempts to keep the API.
- *
- */
-
+/* #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 "version.h" */
#include <linux/crypto.h>
#include <linux/mm.h>
-#include <linux/highmem.h>
-#include <linux/ioctl.h>
-#include <linux/random.h>
-#include <linux/syscalls.h>
+#include <linux/module.h>
+#include <linux/net.h>
#include <linux/pagemap.h>
-#include <linux/uaccess.h>
#include <linux/scatterlist.h>
-#include "cryptodev_int.h"
+#include <net/sock.h>
+#include "alg.h"
#include "ncr-int.h"
-#include <linux/version.h>
-#include "version.h"
MODULE_AUTHOR("Nikos Mavrogiannopoulos <nmav@gnutls.org>");
MODULE_DESCRIPTION("CryptoDev driver");
@@ -101,72 +95,355 @@ int __get_userbuf(uint8_t __user * addr, uint32_t len, int write,
/* ====== /dev/crypto ====== */
-static int cryptodev_open(struct inode *inode, struct file *filp)
+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)
{
- void *ncr;
+ return container_of(sk, struct alg_sock, sk);
+}
- ncr = ncr_init_lists();
- if (ncr == NULL) {
- return -ENOMEM;
+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; */
+
+/* struct kmem_cache *slab; */
+ .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);
}
- filp->private_data = ncr;
+ 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 cryptodev_release(struct inode *inode, struct file *filp)
+static int alg_bind(struct socket *sock, struct sockaddr *myaddr,
+ int sockaddr_len)
{
- void *ncr = filp->private_data;
+ struct sockaddr_alg *addr;
+ struct alg_sock *ask;
- if (ncr) {
- ncr_deinit_lists(ncr);
- filp->private_data = NULL;
- }
+ 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 long
-cryptodev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+static int alg_accept(struct socket *sock, struct socket *newsock, int flags)
{
- void *ncr = filp->private_data;
+ struct alg_sock *ask;
+ struct sock *newsk;
- if (unlikely(!ncr))
- BUG();
+ // FIXME: locking
+ ask = alg_sk(sock->sk);
+ if (ask->queued == NULL)
+ return -EINVAL;
- return ncr_ioctl(ncr, cmd, arg);
+ newsk = ask->queued;
+ ask->queued = NULL;
+ sock_graft(newsk, newsock);
+ return 0;
}
-/* compatibility code for 32bit userlands */
-#ifdef CONFIG_COMPAT
+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;
-static long
-cryptodev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+ 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)
{
- void *ncr = file->private_data;
+ struct alg_sock *ask;
+ char *buf;
+ int res;
- if (unlikely(!ncr))
- BUG();
+ // 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;
- return ncr_compat_ioctl(ncr, cmd, arg);
+ 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;
}
-#endif /* CONFIG_COMPAT */
+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 file_operations cryptodev_fops = {
+static const struct proto_ops alg_proto_ops = {
+ .family = PF_ALG,
.owner = THIS_MODULE,
- .open = cryptodev_open,
- .release = cryptodev_release,
- .unlocked_ioctl = cryptodev_ioctl,
-#ifdef CONFIG_COMPAT
- .compat_ioctl = cryptodev_compat_ioctl,
-#endif /* CONFIG_COMPAT */
+ .release = alg_release,
+ .bind = alg_bind,
+ /* int (*connect) (struct socket *sock, */
+ /* struct sockaddr *vaddr, */
+ /* int sockaddr_len, int flags); */
+ /* int (*socketpair)(struct socket *sock1, */
+ /* struct socket *sock2); */
+ .accept = alg_accept,
+ .getname = sock_no_getname,
+ /* unsigned int (*poll) (struct file *file, struct socket *sock, */
+ /* struct poll_table_struct *wait); */
+ /* int (*ioctl) (struct socket *sock, unsigned int cmd, */
+ /* unsigned long arg); */
+ /* int (*compat_ioctl) (struct socket *sock, unsigned int cmd, */
+ /* unsigned long arg); */
+ .listen = alg_listen,
+ /* int (*shutdown) (struct socket *sock, int flags); */
+ /* int (*setsockopt)(struct socket *sock, int level, */
+ /* int optname, char __user *optval, unsigned int optlen); */
+ /* int (*getsockopt)(struct socket *sock, int level, */
+ /* int optname, char __user *optval, int __user *optlen); */
+ /* int (*compat_setsockopt)(struct socket *sock, int level, */
+ /* int optname, char __user *optval, unsigned int optlen); */
+ /* int (*compat_getsockopt)(struct socket *sock, int level, */
+ /* int optname, char __user *optval, int __user *optlen); */
+ .sendmsg = alg_sendmsg,
+ .recvmsg = alg_recvmsg,
+ /* int (*mmap) (struct file *file, struct socket *sock, */
+ /* struct vm_area_struct * vma); */
+ /* ssize_t (*sendpage) (struct socket *sock, struct page *page, */
+ /* int offset, size_t size, int flags); */
+ /* ssize_t (*splice_read)(struct socket *sock, loff_t *ppos, */
+ /* struct pipe_inode_info *pipe, size_t len, unsigned int flags); */
};
-static struct miscdevice cryptodev = {
- .minor = MISC_DYNAMIC_MINOR,
- .name = "crypto",
- .fops = &cryptodev_fops,
+static int alg_create(struct net *net, struct socket *sock, int protocol,
+ int kern)
+{
+ struct sock *sk;
+
+ // FIXME: verify socket_file_ops are complete
+
+ 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 cryptodev_register(void)
@@ -176,19 +453,28 @@ static int __init cryptodev_register(void)
ncr_limits_init();
ncr_master_key_reset();
- rc = misc_register(&cryptodev);
- if (unlikely(rc)) {
- ncr_limits_deinit();
- printk(KERN_ERR PFX "registration of /dev/crypto failed\n");
- return rc;
- }
+ rc = proto_register(&alg_proto, 1);
+ if (unlikely(rc != 0))
+ goto err_limits;
+
+ rc = sock_register(&alg_pf);
+ if (unlikely(rc != 0))
+ goto err_proto;
return 0;
+
+err_proto:
+ proto_unregister(&alg_proto);
+err_limits:
+ ncr_limits_deinit();
+ printk(KERN_ERR PFX "registration of /dev/crypto failed\n");
+ return rc;
}
static void __exit cryptodev_deregister(void)
{
- misc_deregister(&cryptodev);
+ sock_unregister(PF_ALG);
+ proto_unregister(&alg_proto);
ncr_limits_deinit();
}
@@ -201,7 +487,7 @@ static int __init init_cryptodev(void)
if (unlikely(rc))
return rc;
- printk(KERN_INFO PFX "driver %s loaded.\n", VERSION);
+ printk(KERN_INFO PFX "driver loaded.\n");
return 0;
}