zero copy functions separated from rest.
[cryptodev-linux.git] / cryptodev_int.h
blobe0523db14cd53aeff398a01f3df72f88acb837f2
1 /* cipher stuff */
2 #ifndef CRYPTODEV_INT_H
3 # define CRYPTODEV_INT_H
5 #include <linux/init.h>
6 #include <linux/sched.h>
7 #include <linux/fs.h>
8 #include <linux/file.h>
9 #include <linux/fdtable.h>
10 #include <linux/miscdevice.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/scatterlist.h>
14 #include <crypto/cryptodev.h>
15 #include <crypto/aead.h>
17 #define PFX "cryptodev: "
18 #define dprintk(level, severity, format, a...) \
19 do { \
20 if (level <= cryptodev_verbosity) \
21 printk(severity PFX "%s[%u]: " format, \
22 current->comm, current->pid, \
23 ##a); \
24 } while (0)
26 extern int cryptodev_verbosity;
28 struct cipher_data {
29 int init; /* 0 uninitialized */
30 int blocksize;
31 int aead;
32 int stream;
33 int ivsize;
34 int alignmask;
35 struct {
36 struct crypto_ablkcipher *s;
37 struct ablkcipher_request *request;
39 struct crypto_aead *as;
40 struct aead_request *arequest;
42 struct cryptodev_result *result;
43 uint8_t iv[EALG_MAX_BLOCK_LEN];
44 } async;
47 struct fcrypt {
48 struct list_head list;
49 struct mutex sem;
52 int cryptodev_cipher_init(struct cipher_data *out, const char *alg_name,
53 uint8_t *key, size_t keylen, int stream, int aead);
54 void cryptodev_cipher_deinit(struct cipher_data *cdata);
55 ssize_t cryptodev_cipher_decrypt(struct cipher_data *cdata,
56 const struct scatterlist *sg1,
57 struct scatterlist *sg2, size_t len);
58 ssize_t cryptodev_cipher_encrypt(struct cipher_data *cdata,
59 const struct scatterlist *sg1,
60 struct scatterlist *sg2, size_t len);
62 /* AEAD */
63 inline static void cryptodev_cipher_auth(struct cipher_data *cdata,
64 const struct scatterlist *sg1, size_t len)
66 aead_request_set_assoc(cdata->async.arequest,
67 (struct scatterlist *)sg1, len);
70 inline static void cryptodev_cipher_set_tag_size(struct cipher_data *cdata, int size)
72 if (cdata->aead != 0)
73 crypto_aead_setauthsize(cdata->async.as, size);
76 inline static void cryptodev_cipher_set_iv(struct cipher_data *cdata,
77 void *iv, size_t iv_size)
79 memcpy(cdata->async.iv, iv, min(iv_size, sizeof(cdata->async.iv)));
82 inline static void cryptodev_cipher_get_iv(struct cipher_data *cdata,
83 void *iv, size_t iv_size)
85 memcpy(iv, cdata->async.iv, min(iv_size, sizeof(cdata->async.iv)));
88 /* hash stuff */
89 struct hash_data {
90 int init; /* 0 uninitialized */
91 int digestsize;
92 int alignmask;
93 struct {
94 struct crypto_ahash *s;
95 struct cryptodev_result *result;
96 struct ahash_request *request;
97 } async;
100 int cryptodev_hash_final(struct hash_data *hdata, void *output);
101 ssize_t cryptodev_hash_update(struct hash_data *hdata,
102 struct scatterlist *sg, size_t len);
103 int cryptodev_hash_reset(struct hash_data *hdata);
104 void cryptodev_hash_deinit(struct hash_data *hdata);
105 int cryptodev_hash_init(struct hash_data *hdata, const char *alg_name,
106 int hmac_mode, void *mackey, size_t mackeylen);
108 /* compatibility stuff */
109 #ifdef CONFIG_COMPAT
110 #include <linux/compat.h>
112 /* input of CIOCGSESSION */
113 struct compat_session_op {
114 /* Specify either cipher or mac
116 uint32_t cipher; /* cryptodev_crypto_op_t */
117 uint32_t mac; /* cryptodev_crypto_op_t */
119 uint32_t keylen;
120 compat_uptr_t key; /* pointer to key data */
121 uint32_t mackeylen;
122 compat_uptr_t mackey; /* pointer to mac key data */
124 uint32_t ses; /* session identifier */
127 /* input of CIOCCRYPT */
128 struct compat_crypt_op {
129 uint32_t ses; /* session identifier */
130 uint16_t op; /* COP_ENCRYPT or COP_DECRYPT */
131 uint16_t flags; /* see COP_FLAG_* */
132 uint32_t len; /* length of source data */
133 compat_uptr_t src; /* source data */
134 compat_uptr_t dst; /* pointer to output data */
135 compat_uptr_t mac;/* pointer to output data for hash/MAC operations */
136 compat_uptr_t iv;/* initialization vector for encryption operations */
139 /* compat ioctls, defined for the above structs */
140 #define COMPAT_CIOCGSESSION _IOWR('c', 102, struct compat_session_op)
141 #define COMPAT_CIOCCRYPT _IOWR('c', 104, struct compat_crypt_op)
142 #define COMPAT_CIOCASYNCCRYPT _IOW('c', 107, struct compat_crypt_op)
143 #define COMPAT_CIOCASYNCFETCH _IOR('c', 108, struct compat_crypt_op)
145 #endif /* CONFIG_COMPAT */
147 /* kernel-internal extension to struct crypt_op */
148 struct kernel_crypt_op {
149 struct crypt_op cop;
151 int ivlen;
152 __u8 iv[EALG_MAX_BLOCK_LEN];
154 int digestsize;
155 uint8_t hash_output[AALG_MAX_RESULT_LEN];
157 struct task_struct *task;
158 struct mm_struct *mm;
161 struct kernel_crypt_auth_op {
162 struct crypt_auth_op caop;
164 int dst_len; /* based on src_len + pad + tag */
165 int ivlen;
166 __u8 iv[EALG_MAX_BLOCK_LEN];
168 struct task_struct *task;
169 struct mm_struct *mm;
172 /* auth */
174 int kcaop_from_user(struct kernel_crypt_auth_op *kcop,
175 struct fcrypt *fcr, void __user *arg);
176 int kcaop_to_user(struct kernel_crypt_auth_op *kcaop,
177 struct fcrypt *fcr, void __user *arg);
178 int crypto_auth_run(struct fcrypt *fcr, struct kernel_crypt_auth_op *kcaop);
179 int crypto_run(struct fcrypt *fcr, struct kernel_crypt_op *kcop);
181 /* other internal structs */
182 struct csession {
183 struct list_head entry;
184 struct mutex sem;
185 struct cipher_data cdata;
186 struct hash_data hdata;
187 uint32_t sid;
188 uint32_t alignmask;
189 int array_size;
190 struct page **pages;
191 struct scatterlist *sg;
194 struct csession *crypto_get_session_by_sid(struct fcrypt *fcr, uint32_t sid);
196 inline static void crypto_put_session(struct csession * ses_ptr)
198 mutex_unlock(&ses_ptr->sem);
200 int adjust_sg_array(struct csession * ses, int pagecount);
202 #endif /* CRYPTODEV_INT_H */