xref: /openbmc/linux/drivers/crypto/cavium/cpt/cptvf_algs.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c694b233SGeorge Cherian 
3c694b233SGeorge Cherian /*
4c694b233SGeorge Cherian  * Copyright (C) 2016 Cavium, Inc.
5c694b233SGeorge Cherian  */
6c694b233SGeorge Cherian 
7c694b233SGeorge Cherian #include <crypto/aes.h>
8c694b233SGeorge Cherian #include <crypto/algapi.h>
9c694b233SGeorge Cherian #include <crypto/authenc.h>
100e1cbe97SArd Biesheuvel #include <crypto/internal/des.h>
11c694b233SGeorge Cherian #include <crypto/xts.h>
12c694b233SGeorge Cherian #include <linux/crypto.h>
13c694b233SGeorge Cherian #include <linux/err.h>
14c694b233SGeorge Cherian #include <linux/list.h>
15c694b233SGeorge Cherian #include <linux/scatterlist.h>
16c694b233SGeorge Cherian 
17c694b233SGeorge Cherian #include "cptvf.h"
18c694b233SGeorge Cherian #include "cptvf_algs.h"
19c694b233SGeorge Cherian 
20c694b233SGeorge Cherian struct cpt_device_handle {
21c694b233SGeorge Cherian 	void *cdev[MAX_DEVICES];
22c694b233SGeorge Cherian 	u32 dev_count;
23c694b233SGeorge Cherian };
24c694b233SGeorge Cherian 
25c694b233SGeorge Cherian static struct cpt_device_handle dev_handle;
26c694b233SGeorge Cherian 
cvm_callback(u32 status,void * arg)27c694b233SGeorge Cherian static void cvm_callback(u32 status, void *arg)
28c694b233SGeorge Cherian {
29c694b233SGeorge Cherian 	struct crypto_async_request *req = (struct crypto_async_request *)arg;
30c694b233SGeorge Cherian 
31*b34a6416SHerbert Xu 	crypto_request_complete(req, !status);
32c694b233SGeorge Cherian }
33c694b233SGeorge Cherian 
update_input_iv(struct cpt_request_info * req_info,u8 * iv,u32 enc_iv_len,u32 * argcnt)34c694b233SGeorge Cherian static inline void update_input_iv(struct cpt_request_info *req_info,
35c694b233SGeorge Cherian 				   u8 *iv, u32 enc_iv_len,
36c694b233SGeorge Cherian 				   u32 *argcnt)
37c694b233SGeorge Cherian {
38c694b233SGeorge Cherian 	/* Setting the iv information */
39c694b233SGeorge Cherian 	req_info->in[*argcnt].vptr = (void *)iv;
40c694b233SGeorge Cherian 	req_info->in[*argcnt].size = enc_iv_len;
41c694b233SGeorge Cherian 	req_info->req.dlen += enc_iv_len;
42c694b233SGeorge Cherian 
43c694b233SGeorge Cherian 	++(*argcnt);
44c694b233SGeorge Cherian }
45c694b233SGeorge Cherian 
update_output_iv(struct cpt_request_info * req_info,u8 * iv,u32 enc_iv_len,u32 * argcnt)46c694b233SGeorge Cherian static inline void update_output_iv(struct cpt_request_info *req_info,
47c694b233SGeorge Cherian 				    u8 *iv, u32 enc_iv_len,
48c694b233SGeorge Cherian 				    u32 *argcnt)
49c694b233SGeorge Cherian {
50c694b233SGeorge Cherian 	/* Setting the iv information */
51c694b233SGeorge Cherian 	req_info->out[*argcnt].vptr = (void *)iv;
52c694b233SGeorge Cherian 	req_info->out[*argcnt].size = enc_iv_len;
53c694b233SGeorge Cherian 	req_info->rlen += enc_iv_len;
54c694b233SGeorge Cherian 
55c694b233SGeorge Cherian 	++(*argcnt);
56c694b233SGeorge Cherian }
57c694b233SGeorge Cherian 
update_input_data(struct cpt_request_info * req_info,struct scatterlist * inp_sg,u32 nbytes,u32 * argcnt)58c694b233SGeorge Cherian static inline void update_input_data(struct cpt_request_info *req_info,
59c694b233SGeorge Cherian 				     struct scatterlist *inp_sg,
60c694b233SGeorge Cherian 				     u32 nbytes, u32 *argcnt)
61c694b233SGeorge Cherian {
62c694b233SGeorge Cherian 	req_info->req.dlen += nbytes;
63c694b233SGeorge Cherian 
64c694b233SGeorge Cherian 	while (nbytes) {
65c694b233SGeorge Cherian 		u32 len = min(nbytes, inp_sg->length);
66c694b233SGeorge Cherian 		u8 *ptr = sg_virt(inp_sg);
67c694b233SGeorge Cherian 
68c694b233SGeorge Cherian 		req_info->in[*argcnt].vptr = (void *)ptr;
69c694b233SGeorge Cherian 		req_info->in[*argcnt].size = len;
70c694b233SGeorge Cherian 		nbytes -= len;
71c694b233SGeorge Cherian 
72c694b233SGeorge Cherian 		++(*argcnt);
73c694b233SGeorge Cherian 		++inp_sg;
74c694b233SGeorge Cherian 	}
75c694b233SGeorge Cherian }
76c694b233SGeorge Cherian 
update_output_data(struct cpt_request_info * req_info,struct scatterlist * outp_sg,u32 nbytes,u32 * argcnt)77c694b233SGeorge Cherian static inline void update_output_data(struct cpt_request_info *req_info,
78c694b233SGeorge Cherian 				      struct scatterlist *outp_sg,
79c694b233SGeorge Cherian 				      u32 nbytes, u32 *argcnt)
80c694b233SGeorge Cherian {
81c694b233SGeorge Cherian 	req_info->rlen += nbytes;
82c694b233SGeorge Cherian 
83c694b233SGeorge Cherian 	while (nbytes) {
84c694b233SGeorge Cherian 		u32 len = min(nbytes, outp_sg->length);
85c694b233SGeorge Cherian 		u8 *ptr = sg_virt(outp_sg);
86c694b233SGeorge Cherian 
87c694b233SGeorge Cherian 		req_info->out[*argcnt].vptr = (void *)ptr;
88c694b233SGeorge Cherian 		req_info->out[*argcnt].size = len;
89c694b233SGeorge Cherian 		nbytes -= len;
90c694b233SGeorge Cherian 		++(*argcnt);
91c694b233SGeorge Cherian 		++outp_sg;
92c694b233SGeorge Cherian 	}
93c694b233SGeorge Cherian }
94c694b233SGeorge Cherian 
create_ctx_hdr(struct skcipher_request * req,u32 enc,u32 * argcnt)95ac0d3d13SArd Biesheuvel static inline u32 create_ctx_hdr(struct skcipher_request *req, u32 enc,
96c694b233SGeorge Cherian 				 u32 *argcnt)
97c694b233SGeorge Cherian {
98ac0d3d13SArd Biesheuvel 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
99ac0d3d13SArd Biesheuvel 	struct cvm_enc_ctx *ctx = crypto_skcipher_ctx(tfm);
1001c64a7e1SHerbert Xu 	struct cvm_req_ctx *rctx = skcipher_request_ctx_dma(req);
101c694b233SGeorge Cherian 	struct fc_context *fctx = &rctx->fctx;
102ac0d3d13SArd Biesheuvel 	u32 enc_iv_len = crypto_skcipher_ivsize(tfm);
103c694b233SGeorge Cherian 	struct cpt_request_info *req_info = &rctx->cpt_req;
104c4149431SHerbert Xu 	__be64 *ctrl_flags = NULL;
105c4149431SHerbert Xu 	__be64 *offset_control;
106c694b233SGeorge Cherian 
107c694b233SGeorge Cherian 	req_info->ctrl.s.grp = 0;
108c694b233SGeorge Cherian 	req_info->ctrl.s.dma_mode = DMA_GATHER_SCATTER;
109c694b233SGeorge Cherian 	req_info->ctrl.s.se_req = SE_CORE_REQ;
110c694b233SGeorge Cherian 
111c694b233SGeorge Cherian 	req_info->req.opcode.s.major = MAJOR_OP_FC |
112c694b233SGeorge Cherian 					DMA_MODE_FLAG(DMA_GATHER_SCATTER);
113c694b233SGeorge Cherian 	if (enc)
114c694b233SGeorge Cherian 		req_info->req.opcode.s.minor = 2;
115c694b233SGeorge Cherian 	else
116c694b233SGeorge Cherian 		req_info->req.opcode.s.minor = 3;
117c694b233SGeorge Cherian 
118ac0d3d13SArd Biesheuvel 	req_info->req.param1 = req->cryptlen; /* Encryption Data length */
119c694b233SGeorge Cherian 	req_info->req.param2 = 0; /*Auth data length */
120c694b233SGeorge Cherian 
121e2eb769eSGeorge Cherian 	fctx->enc.enc_ctrl.e.enc_cipher = ctx->cipher_type;
122e2eb769eSGeorge Cherian 	fctx->enc.enc_ctrl.e.aes_key = ctx->key_type;
123c694b233SGeorge Cherian 	fctx->enc.enc_ctrl.e.iv_source = FROM_DPTR;
124c694b233SGeorge Cherian 
125e2eb769eSGeorge Cherian 	if (ctx->cipher_type == AES_XTS)
126c694b233SGeorge Cherian 		memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len * 2);
127c694b233SGeorge Cherian 	else
128c694b233SGeorge Cherian 		memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len);
129c4149431SHerbert Xu 	ctrl_flags = (__be64 *)&fctx->enc.enc_ctrl.flags;
130c4149431SHerbert Xu 	*ctrl_flags = cpu_to_be64(fctx->enc.enc_ctrl.flags);
131c694b233SGeorge Cherian 
132c4149431SHerbert Xu 	offset_control = (__be64 *)&rctx->control_word;
133c694b233SGeorge Cherian 	*offset_control = cpu_to_be64(((u64)(enc_iv_len) << 16));
134c694b233SGeorge Cherian 	/* Storing  Packet Data Information in offset
135c694b233SGeorge Cherian 	 * Control Word First 8 bytes
136c694b233SGeorge Cherian 	 */
137c694b233SGeorge Cherian 	req_info->in[*argcnt].vptr = (u8 *)offset_control;
138c694b233SGeorge Cherian 	req_info->in[*argcnt].size = CONTROL_WORD_LEN;
139c694b233SGeorge Cherian 	req_info->req.dlen += CONTROL_WORD_LEN;
140c694b233SGeorge Cherian 	++(*argcnt);
141c694b233SGeorge Cherian 
142c694b233SGeorge Cherian 	req_info->in[*argcnt].vptr = (u8 *)fctx;
143c694b233SGeorge Cherian 	req_info->in[*argcnt].size = sizeof(struct fc_context);
144c694b233SGeorge Cherian 	req_info->req.dlen += sizeof(struct fc_context);
145c694b233SGeorge Cherian 
146c694b233SGeorge Cherian 	++(*argcnt);
147c694b233SGeorge Cherian 
148c694b233SGeorge Cherian 	return 0;
149c694b233SGeorge Cherian }
150c694b233SGeorge Cherian 
create_input_list(struct skcipher_request * req,u32 enc,u32 enc_iv_len)151ac0d3d13SArd Biesheuvel static inline u32 create_input_list(struct skcipher_request  *req, u32 enc,
152c694b233SGeorge Cherian 				    u32 enc_iv_len)
153c694b233SGeorge Cherian {
1541c64a7e1SHerbert Xu 	struct cvm_req_ctx *rctx = skcipher_request_ctx_dma(req);
155c694b233SGeorge Cherian 	struct cpt_request_info *req_info = &rctx->cpt_req;
156c694b233SGeorge Cherian 	u32 argcnt =  0;
157c694b233SGeorge Cherian 
158e2eb769eSGeorge Cherian 	create_ctx_hdr(req, enc, &argcnt);
159ac0d3d13SArd Biesheuvel 	update_input_iv(req_info, req->iv, enc_iv_len, &argcnt);
160ac0d3d13SArd Biesheuvel 	update_input_data(req_info, req->src, req->cryptlen, &argcnt);
161c694b233SGeorge Cherian 	req_info->incnt = argcnt;
162c694b233SGeorge Cherian 
163c694b233SGeorge Cherian 	return 0;
164c694b233SGeorge Cherian }
165c694b233SGeorge Cherian 
store_cb_info(struct skcipher_request * req,struct cpt_request_info * req_info)166ac0d3d13SArd Biesheuvel static inline void store_cb_info(struct skcipher_request *req,
167c694b233SGeorge Cherian 				 struct cpt_request_info *req_info)
168c694b233SGeorge Cherian {
169c694b233SGeorge Cherian 	req_info->callback = (void *)cvm_callback;
170c694b233SGeorge Cherian 	req_info->callback_arg = (void *)&req->base;
171c694b233SGeorge Cherian }
172c694b233SGeorge Cherian 
create_output_list(struct skcipher_request * req,u32 enc_iv_len)173ac0d3d13SArd Biesheuvel static inline void create_output_list(struct skcipher_request *req,
174c694b233SGeorge Cherian 				      u32 enc_iv_len)
175c694b233SGeorge Cherian {
1761c64a7e1SHerbert Xu 	struct cvm_req_ctx *rctx = skcipher_request_ctx_dma(req);
177c694b233SGeorge Cherian 	struct cpt_request_info *req_info = &rctx->cpt_req;
178c694b233SGeorge Cherian 	u32 argcnt = 0;
179c694b233SGeorge Cherian 
180c694b233SGeorge Cherian 	/* OUTPUT Buffer Processing
181c694b233SGeorge Cherian 	 * AES encryption/decryption output would be
182c694b233SGeorge Cherian 	 * received in the following format
183c694b233SGeorge Cherian 	 *
184c694b233SGeorge Cherian 	 * ------IV--------|------ENCRYPTED/DECRYPTED DATA-----|
185c694b233SGeorge Cherian 	 * [ 16 Bytes/     [   Request Enc/Dec/ DATA Len AES CBC ]
186c694b233SGeorge Cherian 	 */
187c694b233SGeorge Cherian 	/* Reading IV information */
188ac0d3d13SArd Biesheuvel 	update_output_iv(req_info, req->iv, enc_iv_len, &argcnt);
189ac0d3d13SArd Biesheuvel 	update_output_data(req_info, req->dst, req->cryptlen, &argcnt);
190c694b233SGeorge Cherian 	req_info->outcnt = argcnt;
191c694b233SGeorge Cherian }
192c694b233SGeorge Cherian 
cvm_enc_dec(struct skcipher_request * req,u32 enc)193ac0d3d13SArd Biesheuvel static inline int cvm_enc_dec(struct skcipher_request *req, u32 enc)
194c694b233SGeorge Cherian {
195ac0d3d13SArd Biesheuvel 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1961c64a7e1SHerbert Xu 	struct cvm_req_ctx *rctx = skcipher_request_ctx_dma(req);
197ac0d3d13SArd Biesheuvel 	u32 enc_iv_len = crypto_skcipher_ivsize(tfm);
198c694b233SGeorge Cherian 	struct fc_context *fctx = &rctx->fctx;
199c694b233SGeorge Cherian 	struct cpt_request_info *req_info = &rctx->cpt_req;
200c694b233SGeorge Cherian 	void *cdev = NULL;
201c694b233SGeorge Cherian 	int status;
202c694b233SGeorge Cherian 
203c694b233SGeorge Cherian 	memset(req_info, 0, sizeof(struct cpt_request_info));
2049e27c991SMikulas Patocka 	req_info->may_sleep = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) != 0;
205c694b233SGeorge Cherian 	memset(fctx, 0, sizeof(struct fc_context));
206e2eb769eSGeorge Cherian 	create_input_list(req, enc, enc_iv_len);
207e2eb769eSGeorge Cherian 	create_output_list(req, enc_iv_len);
208c694b233SGeorge Cherian 	store_cb_info(req, req_info);
209c694b233SGeorge Cherian 	cdev = dev_handle.cdev[smp_processor_id()];
210c694b233SGeorge Cherian 	status = cptvf_do_request(cdev, req_info);
211c694b233SGeorge Cherian 	/* We perform an asynchronous send and once
212c694b233SGeorge Cherian 	 * the request is completed the driver would
213c694b233SGeorge Cherian 	 * intimate through  registered call back functions
214c694b233SGeorge Cherian 	 */
215c694b233SGeorge Cherian 
216c694b233SGeorge Cherian 	if (status)
217c694b233SGeorge Cherian 		return status;
218c694b233SGeorge Cherian 	else
219c694b233SGeorge Cherian 		return -EINPROGRESS;
220c694b233SGeorge Cherian }
221c694b233SGeorge Cherian 
cvm_encrypt(struct skcipher_request * req)222ac0d3d13SArd Biesheuvel static int cvm_encrypt(struct skcipher_request *req)
223c694b233SGeorge Cherian {
224e2eb769eSGeorge Cherian 	return cvm_enc_dec(req, true);
225c694b233SGeorge Cherian }
226c694b233SGeorge Cherian 
cvm_decrypt(struct skcipher_request * req)227ac0d3d13SArd Biesheuvel static int cvm_decrypt(struct skcipher_request *req)
228c694b233SGeorge Cherian {
229e2eb769eSGeorge Cherian 	return cvm_enc_dec(req, false);
230c694b233SGeorge Cherian }
231c694b233SGeorge Cherian 
cvm_xts_setkey(struct crypto_skcipher * cipher,const u8 * key,u32 keylen)232ac0d3d13SArd Biesheuvel static int cvm_xts_setkey(struct crypto_skcipher *cipher, const u8 *key,
233c694b233SGeorge Cherian 		   u32 keylen)
234c694b233SGeorge Cherian {
2350ee43367SVladis Dronov 	struct cvm_enc_ctx *ctx = crypto_skcipher_ctx(cipher);
236c694b233SGeorge Cherian 	int err;
237c694b233SGeorge Cherian 	const u8 *key1 = key;
238c694b233SGeorge Cherian 	const u8 *key2 = key + (keylen / 2);
239c694b233SGeorge Cherian 
2400ee43367SVladis Dronov 	err = xts_verify_key(cipher, key, keylen);
241c694b233SGeorge Cherian 	if (err)
242c694b233SGeorge Cherian 		return err;
243c694b233SGeorge Cherian 	ctx->key_len = keylen;
244c694b233SGeorge Cherian 	memcpy(ctx->enc_key, key1, keylen / 2);
245c694b233SGeorge Cherian 	memcpy(ctx->enc_key + KEY2_OFFSET, key2, keylen / 2);
246e2eb769eSGeorge Cherian 	ctx->cipher_type = AES_XTS;
247e2eb769eSGeorge Cherian 	switch (ctx->key_len) {
248e2eb769eSGeorge Cherian 	case 32:
249e2eb769eSGeorge Cherian 		ctx->key_type = AES_128_BIT;
250e2eb769eSGeorge Cherian 		break;
251e2eb769eSGeorge Cherian 	case 64:
252e2eb769eSGeorge Cherian 		ctx->key_type = AES_256_BIT;
253e2eb769eSGeorge Cherian 		break;
254e2eb769eSGeorge Cherian 	default:
255e2eb769eSGeorge Cherian 		return -EINVAL;
256e2eb769eSGeorge Cherian 	}
257c694b233SGeorge Cherian 
258c694b233SGeorge Cherian 	return 0;
259c694b233SGeorge Cherian }
260c694b233SGeorge Cherian 
cvm_validate_keylen(struct cvm_enc_ctx * ctx,u32 keylen)261e2eb769eSGeorge Cherian static int cvm_validate_keylen(struct cvm_enc_ctx *ctx, u32 keylen)
262e2eb769eSGeorge Cherian {
263e2eb769eSGeorge Cherian 	if ((keylen == 16) || (keylen == 24) || (keylen == 32)) {
264e2eb769eSGeorge Cherian 		ctx->key_len = keylen;
265e2eb769eSGeorge Cherian 		switch (ctx->key_len) {
266e2eb769eSGeorge Cherian 		case 16:
267e2eb769eSGeorge Cherian 			ctx->key_type = AES_128_BIT;
268e2eb769eSGeorge Cherian 			break;
269e2eb769eSGeorge Cherian 		case 24:
270e2eb769eSGeorge Cherian 			ctx->key_type = AES_192_BIT;
271e2eb769eSGeorge Cherian 			break;
272e2eb769eSGeorge Cherian 		case 32:
273e2eb769eSGeorge Cherian 			ctx->key_type = AES_256_BIT;
274e2eb769eSGeorge Cherian 			break;
275e2eb769eSGeorge Cherian 		default:
276e2eb769eSGeorge Cherian 			return -EINVAL;
277e2eb769eSGeorge Cherian 		}
278e2eb769eSGeorge Cherian 
279e2eb769eSGeorge Cherian 		if (ctx->cipher_type == DES3_CBC)
280e2eb769eSGeorge Cherian 			ctx->key_type = 0;
281e2eb769eSGeorge Cherian 
282e2eb769eSGeorge Cherian 		return 0;
283e2eb769eSGeorge Cherian 	}
284e2eb769eSGeorge Cherian 
285e2eb769eSGeorge Cherian 	return -EINVAL;
286e2eb769eSGeorge Cherian }
287e2eb769eSGeorge Cherian 
cvm_setkey(struct crypto_skcipher * cipher,const u8 * key,u32 keylen,u8 cipher_type)288ac0d3d13SArd Biesheuvel static int cvm_setkey(struct crypto_skcipher *cipher, const u8 *key,
289e2eb769eSGeorge Cherian 		      u32 keylen, u8 cipher_type)
290c694b233SGeorge Cherian {
2910ee43367SVladis Dronov 	struct cvm_enc_ctx *ctx = crypto_skcipher_ctx(cipher);
292c694b233SGeorge Cherian 
293e2eb769eSGeorge Cherian 	ctx->cipher_type = cipher_type;
294e2eb769eSGeorge Cherian 	if (!cvm_validate_keylen(ctx, keylen)) {
295c694b233SGeorge Cherian 		memcpy(ctx->enc_key, key, keylen);
296c694b233SGeorge Cherian 		return 0;
297e2eb769eSGeorge Cherian 	} else {
298c694b233SGeorge Cherian 		return -EINVAL;
299c694b233SGeorge Cherian 	}
300e2eb769eSGeorge Cherian }
301e2eb769eSGeorge Cherian 
cvm_cbc_aes_setkey(struct crypto_skcipher * cipher,const u8 * key,u32 keylen)302ac0d3d13SArd Biesheuvel static int cvm_cbc_aes_setkey(struct crypto_skcipher *cipher, const u8 *key,
303e2eb769eSGeorge Cherian 			      u32 keylen)
304e2eb769eSGeorge Cherian {
305e2eb769eSGeorge Cherian 	return cvm_setkey(cipher, key, keylen, AES_CBC);
306e2eb769eSGeorge Cherian }
307e2eb769eSGeorge Cherian 
cvm_ecb_aes_setkey(struct crypto_skcipher * cipher,const u8 * key,u32 keylen)308ac0d3d13SArd Biesheuvel static int cvm_ecb_aes_setkey(struct crypto_skcipher *cipher, const u8 *key,
30910d82222SGeorge Cherian 			      u32 keylen)
31010d82222SGeorge Cherian {
31110d82222SGeorge Cherian 	return cvm_setkey(cipher, key, keylen, AES_ECB);
31210d82222SGeorge Cherian }
31310d82222SGeorge Cherian 
cvm_cfb_aes_setkey(struct crypto_skcipher * cipher,const u8 * key,u32 keylen)314ac0d3d13SArd Biesheuvel static int cvm_cfb_aes_setkey(struct crypto_skcipher *cipher, const u8 *key,
31510d82222SGeorge Cherian 			      u32 keylen)
31610d82222SGeorge Cherian {
31710d82222SGeorge Cherian 	return cvm_setkey(cipher, key, keylen, AES_CFB);
31810d82222SGeorge Cherian }
31910d82222SGeorge Cherian 
cvm_cbc_des3_setkey(struct crypto_skcipher * cipher,const u8 * key,u32 keylen)320ac0d3d13SArd Biesheuvel static int cvm_cbc_des3_setkey(struct crypto_skcipher *cipher, const u8 *key,
321e2eb769eSGeorge Cherian 			       u32 keylen)
322e2eb769eSGeorge Cherian {
323ac0d3d13SArd Biesheuvel 	return verify_skcipher_des3_key(cipher, key) ?:
3240e1cbe97SArd Biesheuvel 	       cvm_setkey(cipher, key, keylen, DES3_CBC);
325e2eb769eSGeorge Cherian }
326c694b233SGeorge Cherian 
cvm_ecb_des3_setkey(struct crypto_skcipher * cipher,const u8 * key,u32 keylen)327ac0d3d13SArd Biesheuvel static int cvm_ecb_des3_setkey(struct crypto_skcipher *cipher, const u8 *key,
32810d82222SGeorge Cherian 			       u32 keylen)
32910d82222SGeorge Cherian {
330ac0d3d13SArd Biesheuvel 	return verify_skcipher_des3_key(cipher, key) ?:
3310e1cbe97SArd Biesheuvel 	       cvm_setkey(cipher, key, keylen, DES3_ECB);
33210d82222SGeorge Cherian }
33310d82222SGeorge Cherian 
cvm_enc_dec_init(struct crypto_skcipher * tfm)334ac0d3d13SArd Biesheuvel static int cvm_enc_dec_init(struct crypto_skcipher *tfm)
335c694b233SGeorge Cherian {
3361c64a7e1SHerbert Xu 	crypto_skcipher_set_reqsize_dma(tfm, sizeof(struct cvm_req_ctx));
337ac0d3d13SArd Biesheuvel 
338c694b233SGeorge Cherian 	return 0;
339c694b233SGeorge Cherian }
340c694b233SGeorge Cherian 
341ac0d3d13SArd Biesheuvel static struct skcipher_alg algs[] = { {
342b8aa7dc5SMikulas Patocka 	.base.cra_flags		= CRYPTO_ALG_ASYNC |
343b8aa7dc5SMikulas Patocka 				  CRYPTO_ALG_ALLOCATES_MEMORY,
344ac0d3d13SArd Biesheuvel 	.base.cra_blocksize	= AES_BLOCK_SIZE,
345ac0d3d13SArd Biesheuvel 	.base.cra_ctxsize	= sizeof(struct cvm_enc_ctx),
346ac0d3d13SArd Biesheuvel 	.base.cra_alignmask	= 7,
347ac0d3d13SArd Biesheuvel 	.base.cra_priority	= 4001,
348ac0d3d13SArd Biesheuvel 	.base.cra_name		= "xts(aes)",
349ac0d3d13SArd Biesheuvel 	.base.cra_driver_name	= "cavium-xts-aes",
350ac0d3d13SArd Biesheuvel 	.base.cra_module	= THIS_MODULE,
351ac0d3d13SArd Biesheuvel 
352c694b233SGeorge Cherian 	.ivsize			= AES_BLOCK_SIZE,
353c694b233SGeorge Cherian 	.min_keysize		= 2 * AES_MIN_KEY_SIZE,
354c694b233SGeorge Cherian 	.max_keysize		= 2 * AES_MAX_KEY_SIZE,
355c694b233SGeorge Cherian 	.setkey			= cvm_xts_setkey,
356e2eb769eSGeorge Cherian 	.encrypt		= cvm_encrypt,
357e2eb769eSGeorge Cherian 	.decrypt		= cvm_decrypt,
358ac0d3d13SArd Biesheuvel 	.init			= cvm_enc_dec_init,
359c694b233SGeorge Cherian }, {
360b8aa7dc5SMikulas Patocka 	.base.cra_flags		= CRYPTO_ALG_ASYNC |
361b8aa7dc5SMikulas Patocka 				  CRYPTO_ALG_ALLOCATES_MEMORY,
362ac0d3d13SArd Biesheuvel 	.base.cra_blocksize	= AES_BLOCK_SIZE,
363ac0d3d13SArd Biesheuvel 	.base.cra_ctxsize	= sizeof(struct cvm_enc_ctx),
364ac0d3d13SArd Biesheuvel 	.base.cra_alignmask	= 7,
365ac0d3d13SArd Biesheuvel 	.base.cra_priority	= 4001,
366ac0d3d13SArd Biesheuvel 	.base.cra_name		= "cbc(aes)",
367ac0d3d13SArd Biesheuvel 	.base.cra_driver_name	= "cavium-cbc-aes",
368ac0d3d13SArd Biesheuvel 	.base.cra_module	= THIS_MODULE,
369ac0d3d13SArd Biesheuvel 
370c694b233SGeorge Cherian 	.ivsize			= AES_BLOCK_SIZE,
371c694b233SGeorge Cherian 	.min_keysize		= AES_MIN_KEY_SIZE,
372c694b233SGeorge Cherian 	.max_keysize		= AES_MAX_KEY_SIZE,
373e2eb769eSGeorge Cherian 	.setkey			= cvm_cbc_aes_setkey,
374e2eb769eSGeorge Cherian 	.encrypt		= cvm_encrypt,
375e2eb769eSGeorge Cherian 	.decrypt		= cvm_decrypt,
376ac0d3d13SArd Biesheuvel 	.init			= cvm_enc_dec_init,
377c694b233SGeorge Cherian }, {
378b8aa7dc5SMikulas Patocka 	.base.cra_flags		= CRYPTO_ALG_ASYNC |
379b8aa7dc5SMikulas Patocka 				  CRYPTO_ALG_ALLOCATES_MEMORY,
380ac0d3d13SArd Biesheuvel 	.base.cra_blocksize	= AES_BLOCK_SIZE,
381ac0d3d13SArd Biesheuvel 	.base.cra_ctxsize	= sizeof(struct cvm_enc_ctx),
382ac0d3d13SArd Biesheuvel 	.base.cra_alignmask	= 7,
383ac0d3d13SArd Biesheuvel 	.base.cra_priority	= 4001,
384ac0d3d13SArd Biesheuvel 	.base.cra_name		= "ecb(aes)",
385ac0d3d13SArd Biesheuvel 	.base.cra_driver_name	= "cavium-ecb-aes",
386ac0d3d13SArd Biesheuvel 	.base.cra_module	= THIS_MODULE,
387ac0d3d13SArd Biesheuvel 
38810d82222SGeorge Cherian 	.min_keysize		= AES_MIN_KEY_SIZE,
38910d82222SGeorge Cherian 	.max_keysize		= AES_MAX_KEY_SIZE,
39010d82222SGeorge Cherian 	.setkey			= cvm_ecb_aes_setkey,
39110d82222SGeorge Cherian 	.encrypt		= cvm_encrypt,
39210d82222SGeorge Cherian 	.decrypt		= cvm_decrypt,
393ac0d3d13SArd Biesheuvel 	.init			= cvm_enc_dec_init,
39410d82222SGeorge Cherian }, {
395b8aa7dc5SMikulas Patocka 	.base.cra_flags		= CRYPTO_ALG_ASYNC |
396b8aa7dc5SMikulas Patocka 				  CRYPTO_ALG_ALLOCATES_MEMORY,
397ac0d3d13SArd Biesheuvel 	.base.cra_blocksize	= AES_BLOCK_SIZE,
398ac0d3d13SArd Biesheuvel 	.base.cra_ctxsize	= sizeof(struct cvm_enc_ctx),
399ac0d3d13SArd Biesheuvel 	.base.cra_alignmask	= 7,
400ac0d3d13SArd Biesheuvel 	.base.cra_priority	= 4001,
401ac0d3d13SArd Biesheuvel 	.base.cra_name		= "cfb(aes)",
402ac0d3d13SArd Biesheuvel 	.base.cra_driver_name	= "cavium-cfb-aes",
403ac0d3d13SArd Biesheuvel 	.base.cra_module	= THIS_MODULE,
404ac0d3d13SArd Biesheuvel 
40510d82222SGeorge Cherian 	.ivsize			= AES_BLOCK_SIZE,
40610d82222SGeorge Cherian 	.min_keysize		= AES_MIN_KEY_SIZE,
40710d82222SGeorge Cherian 	.max_keysize		= AES_MAX_KEY_SIZE,
40810d82222SGeorge Cherian 	.setkey			= cvm_cfb_aes_setkey,
40910d82222SGeorge Cherian 	.encrypt		= cvm_encrypt,
41010d82222SGeorge Cherian 	.decrypt		= cvm_decrypt,
411ac0d3d13SArd Biesheuvel 	.init			= cvm_enc_dec_init,
41210d82222SGeorge Cherian }, {
413b8aa7dc5SMikulas Patocka 	.base.cra_flags		= CRYPTO_ALG_ASYNC |
414b8aa7dc5SMikulas Patocka 				  CRYPTO_ALG_ALLOCATES_MEMORY,
415ac0d3d13SArd Biesheuvel 	.base.cra_blocksize	= DES3_EDE_BLOCK_SIZE,
416ac0d3d13SArd Biesheuvel 	.base.cra_ctxsize	= sizeof(struct cvm_des3_ctx),
417ac0d3d13SArd Biesheuvel 	.base.cra_alignmask	= 7,
418ac0d3d13SArd Biesheuvel 	.base.cra_priority	= 4001,
419ac0d3d13SArd Biesheuvel 	.base.cra_name		= "cbc(des3_ede)",
420ac0d3d13SArd Biesheuvel 	.base.cra_driver_name	= "cavium-cbc-des3_ede",
421ac0d3d13SArd Biesheuvel 	.base.cra_module	= THIS_MODULE,
422ac0d3d13SArd Biesheuvel 
423c694b233SGeorge Cherian 	.min_keysize		= DES3_EDE_KEY_SIZE,
424c694b233SGeorge Cherian 	.max_keysize		= DES3_EDE_KEY_SIZE,
425c694b233SGeorge Cherian 	.ivsize			= DES_BLOCK_SIZE,
426e2eb769eSGeorge Cherian 	.setkey			= cvm_cbc_des3_setkey,
427e2eb769eSGeorge Cherian 	.encrypt		= cvm_encrypt,
428e2eb769eSGeorge Cherian 	.decrypt		= cvm_decrypt,
429ac0d3d13SArd Biesheuvel 	.init			= cvm_enc_dec_init,
43010d82222SGeorge Cherian }, {
431b8aa7dc5SMikulas Patocka 	.base.cra_flags		= CRYPTO_ALG_ASYNC |
432b8aa7dc5SMikulas Patocka 				  CRYPTO_ALG_ALLOCATES_MEMORY,
433ac0d3d13SArd Biesheuvel 	.base.cra_blocksize	= DES3_EDE_BLOCK_SIZE,
434ac0d3d13SArd Biesheuvel 	.base.cra_ctxsize	= sizeof(struct cvm_des3_ctx),
435ac0d3d13SArd Biesheuvel 	.base.cra_alignmask	= 7,
436ac0d3d13SArd Biesheuvel 	.base.cra_priority	= 4001,
437ac0d3d13SArd Biesheuvel 	.base.cra_name		= "ecb(des3_ede)",
438ac0d3d13SArd Biesheuvel 	.base.cra_driver_name	= "cavium-ecb-des3_ede",
439ac0d3d13SArd Biesheuvel 	.base.cra_module	= THIS_MODULE,
440ac0d3d13SArd Biesheuvel 
44110d82222SGeorge Cherian 	.min_keysize		= DES3_EDE_KEY_SIZE,
44210d82222SGeorge Cherian 	.max_keysize		= DES3_EDE_KEY_SIZE,
44310d82222SGeorge Cherian 	.ivsize			= DES_BLOCK_SIZE,
44410d82222SGeorge Cherian 	.setkey			= cvm_ecb_des3_setkey,
44510d82222SGeorge Cherian 	.encrypt		= cvm_encrypt,
44610d82222SGeorge Cherian 	.decrypt		= cvm_decrypt,
447ac0d3d13SArd Biesheuvel 	.init			= cvm_enc_dec_init,
448c694b233SGeorge Cherian } };
449c694b233SGeorge Cherian 
cav_register_algs(void)450c694b233SGeorge Cherian static inline int cav_register_algs(void)
451c694b233SGeorge Cherian {
452f74bf733SLiu Shixin 	return crypto_register_skciphers(algs, ARRAY_SIZE(algs));
453c694b233SGeorge Cherian }
454c694b233SGeorge Cherian 
cav_unregister_algs(void)455c694b233SGeorge Cherian static inline void cav_unregister_algs(void)
456c694b233SGeorge Cherian {
457ac0d3d13SArd Biesheuvel 	crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
458c694b233SGeorge Cherian }
459c694b233SGeorge Cherian 
cvm_crypto_init(struct cpt_vf * cptvf)460c694b233SGeorge Cherian int cvm_crypto_init(struct cpt_vf *cptvf)
461c694b233SGeorge Cherian {
462c694b233SGeorge Cherian 	struct pci_dev *pdev = cptvf->pdev;
463c694b233SGeorge Cherian 	u32 dev_count;
464c694b233SGeorge Cherian 
465c694b233SGeorge Cherian 	dev_count = dev_handle.dev_count;
466c694b233SGeorge Cherian 	dev_handle.cdev[dev_count] = cptvf;
467c694b233SGeorge Cherian 	dev_handle.dev_count++;
468c694b233SGeorge Cherian 
469c694b233SGeorge Cherian 	if (dev_count == 3) {
470c694b233SGeorge Cherian 		if (cav_register_algs()) {
471c694b233SGeorge Cherian 			dev_err(&pdev->dev, "Error in registering crypto algorithms\n");
472c694b233SGeorge Cherian 			return -EINVAL;
473c694b233SGeorge Cherian 		}
474c694b233SGeorge Cherian 	}
475c694b233SGeorge Cherian 
476c694b233SGeorge Cherian 	return 0;
477c694b233SGeorge Cherian }
478c694b233SGeorge Cherian 
cvm_crypto_exit(void)479c694b233SGeorge Cherian void cvm_crypto_exit(void)
480c694b233SGeorge Cherian {
481c694b233SGeorge Cherian 	u32 dev_count;
482c694b233SGeorge Cherian 
483c694b233SGeorge Cherian 	dev_count = --dev_handle.dev_count;
484c694b233SGeorge Cherian 	if (!dev_count)
485c694b233SGeorge Cherian 		cav_unregister_algs();
486c694b233SGeorge Cherian }
487