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