xref: /OK3568_Linux_fs/kernel/drivers/crypto/rockchip/cryptodev_linux/cryptodev.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 
3 /* cipher stuff */
4 #ifndef CRYPTODEV_H
5 # define CRYPTODEV_H
6 
7 #include <linux/version.h>
8 
9 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0))
10 #  define reinit_completion(x) INIT_COMPLETION(*(x))
11 #endif
12 
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/fs.h>
16 #include <linux/file.h>
17 #include <linux/fdtable.h>
18 #include <linux/miscdevice.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/scatterlist.h>
22 #include <uapi/linux/cryptodev.h>
23 #include <crypto/aead.h>
24 
25 #define PFX "cryptodev: "
26 #define dprintk(level, severity, format, a...)			\
27 	do {							\
28 		if (level <= cryptodev_verbosity)		\
29 			printk(severity PFX "%s[%u] (%s:%u): " format "\n",	\
30 			       current->comm, current->pid,	\
31 			       __func__, __LINE__,		\
32 			       ##a);				\
33 	} while (0)
34 #define derr(level, format, a...) dprintk(level, KERN_ERR, format, ##a)
35 #define dwarning(level, format, a...) dprintk(level, KERN_WARNING, format, ##a)
36 #define dinfo(level, format, a...) dprintk(level, KERN_INFO, format, ##a)
37 #define ddebug(level, format, a...) dprintk(level, KERN_DEBUG, format, ##a)
38 
39 
40 extern int cryptodev_verbosity;
41 
42 struct fcrypt {
43 	struct list_head list;
44 	struct list_head dma_map_list;
45 	struct mutex sem;
46 };
47 
48 /* compatibility stuff */
49 #ifdef CONFIG_COMPAT
50 #include <linux/compat.h>
51 
52 /* input of CIOCGSESSION */
53 struct compat_session_op {
54 	/* Specify either cipher or mac
55 	 */
56 	uint32_t	cipher;		/* cryptodev_crypto_op_t */
57 	uint32_t	mac;		/* cryptodev_crypto_op_t */
58 
59 	uint32_t	keylen;
60 	compat_uptr_t	key;		/* pointer to key data */
61 	uint32_t	mackeylen;
62 	compat_uptr_t	mackey;		/* pointer to mac key data */
63 
64 	uint32_t	ses;		/* session identifier */
65 };
66 
67 /* input of CIOCCRYPT */
68 struct compat_crypt_op {
69 	uint32_t	ses;		/* session identifier */
70 	uint16_t	op;		/* COP_ENCRYPT or COP_DECRYPT */
71 	uint16_t	flags;		/* see COP_FLAG_* */
72 	uint32_t	len;		/* length of source data */
73 	compat_uptr_t	src;		/* source data */
74 	compat_uptr_t	dst;		/* pointer to output data */
75 	compat_uptr_t	mac;/* pointer to output data for hash/MAC operations */
76 	compat_uptr_t	iv;/* initialization vector for encryption operations */
77 };
78 
79 /* input of COMPAT_CIOCAUTHCRYPT */
80 struct compat_crypt_auth_op {
81 	uint32_t	ses;		/* session identifier */
82 	uint16_t	op;		/* COP_ENCRYPT or COP_DECRYPT */
83 	uint16_t	flags;		/* see COP_FLAG_AEAD_* */
84 	uint32_t	len;		/* length of source data */
85 	uint32_t	auth_len;	/* length of auth data */
86 	compat_uptr_t	auth_src;	/* authenticated-only data */
87 
88 	/* The current implementation is more efficient if data are
89 	 * encrypted in-place (src== dst).
90 	 */
91 	compat_uptr_t	src;	/* data to be encrypted and authenticated */
92 	compat_uptr_t	dst;	/* pointer to output data. Must have
93 				 * space for tag. For TLS this should be at least
94 				 * len + tag_size + block_size for padding
95 				 */
96 
97 	compat_uptr_t	tag;    /* where the tag will be copied to. TLS mode
98 				 * doesn't use that as tag is copied to dst.
99 				 * SRTP mode copies tag there.
100 				 */
101 	uint32_t	tag_len; /* the length of the tag. Use zero for digest size or max tag. */
102 
103 	/* initialization vector for encryption operations */
104 	compat_uptr_t	iv;
105 	uint32_t	iv_len;
106 };
107 
108 /* compat ioctls, defined for the above structs */
109 #define COMPAT_CIOCGSESSION    _IOWR('c', 102, struct compat_session_op)
110 #define COMPAT_CIOCCRYPT       _IOWR('c', 104, struct compat_crypt_op)
111 #define COMPAT_CIOCASYNCCRYPT  _IOW('c', 107, struct compat_crypt_op)
112 #define COMPAT_CIOCASYNCFETCH  _IOR('c', 108, struct compat_crypt_op)
113 
114 #define COMPAT_CIOCAUTHCRYPT   _IOWR('c', 109, struct compat_crypt_auth_op)
115 
116 #endif /* CONFIG_COMPAT */
117 
118 /* kernel-internal extension to struct crypt_op */
119 struct kernel_crypt_op {
120 	struct crypt_op cop;
121 
122 	int ivlen;
123 	__u8 iv[EALG_MAX_BLOCK_LEN];
124 
125 	int digestsize;
126 	uint8_t hash_output[AALG_MAX_RESULT_LEN];
127 
128 	struct task_struct *task;
129 	struct mm_struct *mm;
130 };
131 
132 struct kernel_crypt_auth_op {
133 	struct crypt_auth_op caop;
134 
135 	int dst_len; /* based on src_len + pad + tag */
136 	int ivlen;
137 	__u8 iv[EALG_MAX_BLOCK_LEN];
138 
139 	struct task_struct *task;
140 	struct mm_struct *mm;
141 };
142 
143 /* auth */
144 #ifdef CONFIG_COMPAT
145 int compat_kcaop_to_user(struct kernel_crypt_auth_op *kcaop,
146 			 struct fcrypt *fcr, void __user *arg);
147 int compat_kcaop_from_user(struct kernel_crypt_auth_op *kcaop,
148 			   struct fcrypt *fcr, void __user *arg);
149 #endif /* CONFIG_COMPAT */
150 int cryptodev_kcaop_from_user(struct kernel_crypt_auth_op *kcop,
151 			struct fcrypt *fcr, void __user *arg);
152 int cryptodev_kcaop_to_user(struct kernel_crypt_auth_op *kcaop,
153 		struct fcrypt *fcr, void __user *arg);
154 int crypto_auth_run(struct fcrypt *fcr, struct kernel_crypt_auth_op *kcaop);
155 int crypto_run(struct fcrypt *fcr, struct kernel_crypt_op *kcop);
156 
157 #include "cryptlib.h"
158 
159 /* other internal structs */
160 struct csession {
161 	struct list_head entry;
162 	struct mutex sem;
163 	struct cipher_data cdata;
164 	struct hash_data hdata;
165 	uint32_t sid;
166 	uint32_t alignmask;
167 
168 	unsigned int array_size;
169 	unsigned int used_pages; /* the number of pages that are used */
170 	/* the number of pages marked as NOT-writable; they preceed writeables */
171 	unsigned int readonly_pages;
172 	struct page **pages;
173 	struct scatterlist *sg;
174 };
175 
176 struct csession *crypto_get_session_by_sid(struct fcrypt *fcr, uint32_t sid);
177 int
178 crypto_get_sessions_by_sid(struct fcrypt *fcr,
179 			   uint32_t sid_1, struct csession **ses_ptr_1,
180 			   uint32_t sid_2, struct csession **ses_ptr_2);
181 
crypto_put_session(struct csession * ses_ptr)182 static inline void crypto_put_session(struct csession *ses_ptr)
183 {
184 	mutex_unlock(&ses_ptr->sem);
185 }
186 int cryptodev_adjust_sg_array(struct csession *ses, int pagecount);
187 
188 #endif /* CRYPTODEV_INT_H */
189