blob: 5630c5addd2832dc37be78e106967d076cfc5cd4 [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +03002/*
3 * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +03004 */
5
6#include <linux/device.h>
7#include <linux/interrupt.h>
Eneas U de Queirozce163ba2020-02-07 12:02:26 -03008#include <linux/moduleparam.h>
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +03009#include <linux/types.h>
10#include <crypto/aes.h>
Ard Biesheuvelf96c8972019-08-15 12:01:01 +030011#include <crypto/internal/des.h>
Herbert Xu2d20ce072016-06-29 18:04:04 +080012#include <crypto/internal/skcipher.h>
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030013
14#include "cipher.h"
15
Eneas U de Queirozce163ba2020-02-07 12:02:26 -030016static unsigned int aes_sw_max_len = CONFIG_CRYPTO_DEV_QCE_SW_MAX_LEN;
17module_param(aes_sw_max_len, uint, 0644);
18MODULE_PARM_DESC(aes_sw_max_len,
19 "Only use hardware for AES requests larger than this "
20 "[0=always use hardware; anything <16 breaks AES-GCM; default="
Eneas U de Queirozd069b202020-03-04 15:24:55 -030021 __stringify(CONFIG_CRYPTO_DEV_QCE_SW_MAX_LEN)"]");
Eneas U de Queirozce163ba2020-02-07 12:02:26 -030022
Ard Biesheuvel8bf08712019-11-09 18:09:45 +010023static LIST_HEAD(skcipher_algs);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030024
Ard Biesheuvel8bf08712019-11-09 18:09:45 +010025static void qce_skcipher_done(void *data)
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030026{
27 struct crypto_async_request *async_req = data;
Ard Biesheuvel8bf08712019-11-09 18:09:45 +010028 struct skcipher_request *req = skcipher_request_cast(async_req);
29 struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
30 struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req));
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030031 struct qce_device *qce = tmpl->qce;
Eneas U de Queiroz3e806a12019-12-20 16:02:16 -030032 struct qce_result_dump *result_buf = qce->dma.result_buf;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030033 enum dma_data_direction dir_src, dir_dst;
34 u32 status;
35 int error;
36 bool diff_dst;
37
38 diff_dst = (req->src != req->dst) ? true : false;
39 dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
40 dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
41
42 error = qce_dma_terminate_all(&qce->dma);
43 if (error)
Ard Biesheuvel8bf08712019-11-09 18:09:45 +010044 dev_dbg(qce->dev, "skcipher dma termination error (%d)\n",
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030045 error);
46
47 if (diff_dst)
LABBE Corentinfea40452015-10-02 08:01:02 +020048 dma_unmap_sg(qce->dev, rctx->src_sg, rctx->src_nents, dir_src);
49 dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030050
51 sg_free_table(&rctx->dst_tbl);
52
53 error = qce_check_status(qce, &status);
54 if (error < 0)
Ard Biesheuvel8bf08712019-11-09 18:09:45 +010055 dev_dbg(qce->dev, "skcipher operation error (%x)\n", status);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030056
Eneas U de Queiroz3e806a12019-12-20 16:02:16 -030057 memcpy(rctx->iv, result_buf->encr_cntr_iv, rctx->ivsize);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030058 qce->async_req_done(tmpl->qce, error);
59}
60
61static int
Ard Biesheuvel8bf08712019-11-09 18:09:45 +010062qce_skcipher_async_req_handle(struct crypto_async_request *async_req)
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030063{
Ard Biesheuvel8bf08712019-11-09 18:09:45 +010064 struct skcipher_request *req = skcipher_request_cast(async_req);
65 struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
66 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
67 struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req));
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030068 struct qce_device *qce = tmpl->qce;
69 enum dma_data_direction dir_src, dir_dst;
70 struct scatterlist *sg;
71 bool diff_dst;
72 gfp_t gfp;
73 int ret;
74
Ard Biesheuvel8bf08712019-11-09 18:09:45 +010075 rctx->iv = req->iv;
76 rctx->ivsize = crypto_skcipher_ivsize(skcipher);
77 rctx->cryptlen = req->cryptlen;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030078
79 diff_dst = (req->src != req->dst) ? true : false;
80 dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
81 dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL;
82
Ard Biesheuvel8bf08712019-11-09 18:09:45 +010083 rctx->src_nents = sg_nents_for_len(req->src, req->cryptlen);
LABBE Corentinfea40452015-10-02 08:01:02 +020084 if (diff_dst)
Ard Biesheuvel8bf08712019-11-09 18:09:45 +010085 rctx->dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
LABBE Corentinfea40452015-10-02 08:01:02 +020086 else
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030087 rctx->dst_nents = rctx->src_nents;
LABBE Corentin4fa99482015-11-04 21:13:36 +010088 if (rctx->src_nents < 0) {
89 dev_err(qce->dev, "Invalid numbers of src SG.\n");
90 return rctx->src_nents;
91 }
92 if (rctx->dst_nents < 0) {
93 dev_err(qce->dev, "Invalid numbers of dst SG.\n");
94 return -rctx->dst_nents;
95 }
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +030096
97 rctx->dst_nents += 1;
98
99 gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
100 GFP_KERNEL : GFP_ATOMIC;
101
102 ret = sg_alloc_table(&rctx->dst_tbl, rctx->dst_nents, gfp);
103 if (ret)
104 return ret;
105
106 sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
107
Eneas U de Queirozd6364b82020-02-07 12:02:25 -0300108 sg = qce_sgtable_add(&rctx->dst_tbl, req->dst, req->cryptlen);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300109 if (IS_ERR(sg)) {
110 ret = PTR_ERR(sg);
111 goto error_free;
112 }
113
Eneas U de Queirozd6364b82020-02-07 12:02:25 -0300114 sg = qce_sgtable_add(&rctx->dst_tbl, &rctx->result_sg,
115 QCE_RESULT_BUF_SZ);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300116 if (IS_ERR(sg)) {
117 ret = PTR_ERR(sg);
118 goto error_free;
119 }
120
121 sg_mark_end(sg);
122 rctx->dst_sg = rctx->dst_tbl.sgl;
123
LABBE Corentinfea40452015-10-02 08:01:02 +0200124 ret = dma_map_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300125 if (ret < 0)
126 goto error_free;
127
128 if (diff_dst) {
LABBE Corentinfea40452015-10-02 08:01:02 +0200129 ret = dma_map_sg(qce->dev, req->src, rctx->src_nents, dir_src);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300130 if (ret < 0)
131 goto error_unmap_dst;
132 rctx->src_sg = req->src;
133 } else {
134 rctx->src_sg = rctx->dst_sg;
135 }
136
137 ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, rctx->src_nents,
138 rctx->dst_sg, rctx->dst_nents,
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100139 qce_skcipher_done, async_req);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300140 if (ret)
141 goto error_unmap_src;
142
143 qce_dma_issue_pending(&qce->dma);
144
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100145 ret = qce_start(async_req, tmpl->crypto_alg_type, req->cryptlen, 0);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300146 if (ret)
147 goto error_terminate;
148
149 return 0;
150
151error_terminate:
152 qce_dma_terminate_all(&qce->dma);
153error_unmap_src:
154 if (diff_dst)
LABBE Corentinfea40452015-10-02 08:01:02 +0200155 dma_unmap_sg(qce->dev, req->src, rctx->src_nents, dir_src);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300156error_unmap_dst:
LABBE Corentinfea40452015-10-02 08:01:02 +0200157 dma_unmap_sg(qce->dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300158error_free:
159 sg_free_table(&rctx->dst_tbl);
160 return ret;
161}
162
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100163static int qce_skcipher_setkey(struct crypto_skcipher *ablk, const u8 *key,
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300164 unsigned int keylen)
165{
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100166 struct crypto_tfm *tfm = crypto_skcipher_tfm(ablk);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300167 struct qce_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
Eneas U de Queiroz7de4c2b2019-12-20 16:02:14 -0300168 unsigned long flags = to_cipher_tmpl(ablk)->alg_flags;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300169 int ret;
170
171 if (!key || !keylen)
172 return -EINVAL;
173
Eneas U de Queiroz7de4c2b2019-12-20 16:02:14 -0300174 switch (IS_XTS(flags) ? keylen >> 1 : keylen) {
Ard Biesheuvelf96c8972019-08-15 12:01:01 +0300175 case AES_KEYSIZE_128:
176 case AES_KEYSIZE_256:
Eneas U de Queirozce163ba2020-02-07 12:02:26 -0300177 memcpy(ctx->enc_key, key, keylen);
Ard Biesheuvelf96c8972019-08-15 12:01:01 +0300178 break;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300179 }
180
Ard Biesheuvel90e2f782020-07-07 09:32:01 +0300181 ret = crypto_skcipher_setkey(ctx->fallback, key, keylen);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300182 if (!ret)
183 ctx->enc_keylen = keylen;
184 return ret;
Ard Biesheuvelf96c8972019-08-15 12:01:01 +0300185}
186
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100187static int qce_des_setkey(struct crypto_skcipher *ablk, const u8 *key,
Ard Biesheuvelf96c8972019-08-15 12:01:01 +0300188 unsigned int keylen)
189{
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100190 struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(ablk);
Ard Biesheuvelf96c8972019-08-15 12:01:01 +0300191 int err;
192
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100193 err = verify_skcipher_des_key(ablk, key);
Ard Biesheuvelf96c8972019-08-15 12:01:01 +0300194 if (err)
195 return err;
196
197 ctx->enc_keylen = keylen;
198 memcpy(ctx->enc_key, key, keylen);
199 return 0;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300200}
201
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100202static int qce_des3_setkey(struct crypto_skcipher *ablk, const u8 *key,
Herbert Xu5feaaae2019-04-11 16:51:16 +0800203 unsigned int keylen)
204{
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100205 struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(ablk);
Herbert Xu5feaaae2019-04-11 16:51:16 +0800206 int err;
207
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100208 err = verify_skcipher_des3_key(ablk, key);
Ard Biesheuvelf96c8972019-08-15 12:01:01 +0300209 if (err)
Herbert Xu5feaaae2019-04-11 16:51:16 +0800210 return err;
Herbert Xu5feaaae2019-04-11 16:51:16 +0800211
212 ctx->enc_keylen = keylen;
213 memcpy(ctx->enc_key, key, keylen);
214 return 0;
215}
216
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100217static int qce_skcipher_crypt(struct skcipher_request *req, int encrypt)
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300218{
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100219 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
220 struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
221 struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300222 struct qce_alg_template *tmpl = to_cipher_tmpl(tfm);
Eneas U de Queiroz7de4c2b2019-12-20 16:02:14 -0300223 int keylen;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300224 int ret;
225
226 rctx->flags = tmpl->alg_flags;
227 rctx->flags |= encrypt ? QCE_ENCRYPT : QCE_DECRYPT;
Eneas U de Queiroz7de4c2b2019-12-20 16:02:14 -0300228 keylen = IS_XTS(rctx->flags) ? ctx->enc_keylen >> 1 : ctx->enc_keylen;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300229
Eneas U de Queiroz7f193802020-02-07 12:02:27 -0300230 /* qce is hanging when AES-XTS request len > QCE_SECTOR_SIZE and
231 * is not a multiple of it; pass such requests to the fallback
232 */
Eneas U de Queirozce163ba2020-02-07 12:02:26 -0300233 if (IS_AES(rctx->flags) &&
Eneas U de Queiroz7f193802020-02-07 12:02:27 -0300234 (((keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_256) ||
235 req->cryptlen <= aes_sw_max_len) ||
236 (IS_XTS(rctx->flags) && req->cryptlen > QCE_SECTOR_SIZE &&
237 req->cryptlen % QCE_SECTOR_SIZE))) {
Ard Biesheuvel90e2f782020-07-07 09:32:01 +0300238 skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback);
239 skcipher_request_set_callback(&rctx->fallback_req,
240 req->base.flags,
241 req->base.complete,
242 req->base.data);
243 skcipher_request_set_crypt(&rctx->fallback_req, req->src,
244 req->dst, req->cryptlen, req->iv);
245 ret = encrypt ? crypto_skcipher_encrypt(&rctx->fallback_req) :
246 crypto_skcipher_decrypt(&rctx->fallback_req);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300247 return ret;
248 }
249
250 return tmpl->qce->async_req_enqueue(tmpl->qce, &req->base);
251}
252
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100253static int qce_skcipher_encrypt(struct skcipher_request *req)
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300254{
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100255 return qce_skcipher_crypt(req, 1);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300256}
257
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100258static int qce_skcipher_decrypt(struct skcipher_request *req)
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300259{
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100260 return qce_skcipher_crypt(req, 0);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300261}
262
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100263static int qce_skcipher_init(struct crypto_skcipher *tfm)
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300264{
Ard Biesheuvel90e2f782020-07-07 09:32:01 +0300265 /* take the size without the fallback skcipher_request at the end */
266 crypto_skcipher_set_reqsize(tfm, offsetof(struct qce_cipher_reqctx,
267 fallback_req));
Eneas U de Queiroz8ceda882019-12-20 16:02:17 -0300268 return 0;
269}
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300270
Eneas U de Queiroz8ceda882019-12-20 16:02:17 -0300271static int qce_skcipher_init_fallback(struct crypto_skcipher *tfm)
272{
273 struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
274
Ard Biesheuvel90e2f782020-07-07 09:32:01 +0300275 ctx->fallback = crypto_alloc_skcipher(crypto_tfm_alg_name(&tfm->base),
276 0, CRYPTO_ALG_NEED_FALLBACK);
277 if (IS_ERR(ctx->fallback))
278 return PTR_ERR(ctx->fallback);
279
280 crypto_skcipher_set_reqsize(tfm, sizeof(struct qce_cipher_reqctx) +
281 crypto_skcipher_reqsize(ctx->fallback));
282 return 0;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300283}
284
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100285static void qce_skcipher_exit(struct crypto_skcipher *tfm)
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300286{
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100287 struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300288
Ard Biesheuvel90e2f782020-07-07 09:32:01 +0300289 crypto_free_skcipher(ctx->fallback);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300290}
291
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100292struct qce_skcipher_def {
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300293 unsigned long flags;
294 const char *name;
295 const char *drv_name;
296 unsigned int blocksize;
Eneas U de Queirozbb5c8632019-12-20 16:02:13 -0300297 unsigned int chunksize;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300298 unsigned int ivsize;
299 unsigned int min_keysize;
300 unsigned int max_keysize;
301};
302
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100303static const struct qce_skcipher_def skcipher_def[] = {
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300304 {
305 .flags = QCE_ALG_AES | QCE_MODE_ECB,
306 .name = "ecb(aes)",
307 .drv_name = "ecb-aes-qce",
308 .blocksize = AES_BLOCK_SIZE,
309 .ivsize = AES_BLOCK_SIZE,
310 .min_keysize = AES_MIN_KEY_SIZE,
311 .max_keysize = AES_MAX_KEY_SIZE,
312 },
313 {
314 .flags = QCE_ALG_AES | QCE_MODE_CBC,
315 .name = "cbc(aes)",
316 .drv_name = "cbc-aes-qce",
317 .blocksize = AES_BLOCK_SIZE,
318 .ivsize = AES_BLOCK_SIZE,
319 .min_keysize = AES_MIN_KEY_SIZE,
320 .max_keysize = AES_MAX_KEY_SIZE,
321 },
322 {
323 .flags = QCE_ALG_AES | QCE_MODE_CTR,
324 .name = "ctr(aes)",
325 .drv_name = "ctr-aes-qce",
Eneas U de Queirozbb5c8632019-12-20 16:02:13 -0300326 .blocksize = 1,
327 .chunksize = AES_BLOCK_SIZE,
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300328 .ivsize = AES_BLOCK_SIZE,
329 .min_keysize = AES_MIN_KEY_SIZE,
330 .max_keysize = AES_MAX_KEY_SIZE,
331 },
332 {
333 .flags = QCE_ALG_AES | QCE_MODE_XTS,
334 .name = "xts(aes)",
335 .drv_name = "xts-aes-qce",
336 .blocksize = AES_BLOCK_SIZE,
337 .ivsize = AES_BLOCK_SIZE,
Eneas U de Queiroz7de4c2b2019-12-20 16:02:14 -0300338 .min_keysize = AES_MIN_KEY_SIZE * 2,
339 .max_keysize = AES_MAX_KEY_SIZE * 2,
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300340 },
341 {
342 .flags = QCE_ALG_DES | QCE_MODE_ECB,
343 .name = "ecb(des)",
344 .drv_name = "ecb-des-qce",
345 .blocksize = DES_BLOCK_SIZE,
346 .ivsize = 0,
347 .min_keysize = DES_KEY_SIZE,
348 .max_keysize = DES_KEY_SIZE,
349 },
350 {
351 .flags = QCE_ALG_DES | QCE_MODE_CBC,
352 .name = "cbc(des)",
353 .drv_name = "cbc-des-qce",
354 .blocksize = DES_BLOCK_SIZE,
355 .ivsize = DES_BLOCK_SIZE,
356 .min_keysize = DES_KEY_SIZE,
357 .max_keysize = DES_KEY_SIZE,
358 },
359 {
360 .flags = QCE_ALG_3DES | QCE_MODE_ECB,
361 .name = "ecb(des3_ede)",
362 .drv_name = "ecb-3des-qce",
363 .blocksize = DES3_EDE_BLOCK_SIZE,
364 .ivsize = 0,
365 .min_keysize = DES3_EDE_KEY_SIZE,
366 .max_keysize = DES3_EDE_KEY_SIZE,
367 },
368 {
369 .flags = QCE_ALG_3DES | QCE_MODE_CBC,
370 .name = "cbc(des3_ede)",
371 .drv_name = "cbc-3des-qce",
372 .blocksize = DES3_EDE_BLOCK_SIZE,
373 .ivsize = DES3_EDE_BLOCK_SIZE,
374 .min_keysize = DES3_EDE_KEY_SIZE,
375 .max_keysize = DES3_EDE_KEY_SIZE,
376 },
377};
378
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100379static int qce_skcipher_register_one(const struct qce_skcipher_def *def,
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300380 struct qce_device *qce)
381{
382 struct qce_alg_template *tmpl;
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100383 struct skcipher_alg *alg;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300384 int ret;
385
386 tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
387 if (!tmpl)
388 return -ENOMEM;
389
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100390 alg = &tmpl->alg.skcipher;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300391
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100392 snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
393 snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300394 def->drv_name);
395
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100396 alg->base.cra_blocksize = def->blocksize;
Eneas U de Queirozbb5c8632019-12-20 16:02:13 -0300397 alg->chunksize = def->chunksize;
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100398 alg->ivsize = def->ivsize;
399 alg->min_keysize = def->min_keysize;
400 alg->max_keysize = def->max_keysize;
401 alg->setkey = IS_3DES(def->flags) ? qce_des3_setkey :
402 IS_DES(def->flags) ? qce_des_setkey :
403 qce_skcipher_setkey;
404 alg->encrypt = qce_skcipher_encrypt;
405 alg->decrypt = qce_skcipher_decrypt;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300406
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100407 alg->base.cra_priority = 300;
408 alg->base.cra_flags = CRYPTO_ALG_ASYNC |
Mikulas Patockab8aa7dc2020-07-09 23:20:41 -0700409 CRYPTO_ALG_ALLOCATES_MEMORY |
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100410 CRYPTO_ALG_KERN_DRIVER_ONLY;
411 alg->base.cra_ctxsize = sizeof(struct qce_cipher_ctx);
412 alg->base.cra_alignmask = 0;
413 alg->base.cra_module = THIS_MODULE;
414
Eneas U de Queiroz8ceda882019-12-20 16:02:17 -0300415 if (IS_AES(def->flags)) {
416 alg->base.cra_flags |= CRYPTO_ALG_NEED_FALLBACK;
417 alg->init = qce_skcipher_init_fallback;
418 alg->exit = qce_skcipher_exit;
419 } else {
420 alg->init = qce_skcipher_init;
421 }
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300422
423 INIT_LIST_HEAD(&tmpl->entry);
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100424 tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_SKCIPHER;
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300425 tmpl->alg_flags = def->flags;
426 tmpl->qce = qce;
427
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100428 ret = crypto_register_skcipher(alg);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300429 if (ret) {
430 kfree(tmpl);
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100431 dev_err(qce->dev, "%s registration failed\n", alg->base.cra_name);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300432 return ret;
433 }
434
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100435 list_add_tail(&tmpl->entry, &skcipher_algs);
436 dev_dbg(qce->dev, "%s is registered\n", alg->base.cra_name);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300437 return 0;
438}
439
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100440static void qce_skcipher_unregister(struct qce_device *qce)
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300441{
442 struct qce_alg_template *tmpl, *n;
443
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100444 list_for_each_entry_safe(tmpl, n, &skcipher_algs, entry) {
445 crypto_unregister_skcipher(&tmpl->alg.skcipher);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300446 list_del(&tmpl->entry);
447 kfree(tmpl);
448 }
449}
450
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100451static int qce_skcipher_register(struct qce_device *qce)
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300452{
453 int ret, i;
454
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100455 for (i = 0; i < ARRAY_SIZE(skcipher_def); i++) {
456 ret = qce_skcipher_register_one(&skcipher_def[i], qce);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300457 if (ret)
458 goto err;
459 }
460
461 return 0;
462err:
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100463 qce_skcipher_unregister(qce);
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300464 return ret;
465}
466
Ard Biesheuvel8bf08712019-11-09 18:09:45 +0100467const struct qce_algo_ops skcipher_ops = {
468 .type = CRYPTO_ALG_TYPE_SKCIPHER,
469 .register_algs = qce_skcipher_register,
470 .unregister_algs = qce_skcipher_unregister,
471 .async_req_handle = qce_skcipher_async_req_handle,
Stanimir Varbanovec8f5d82014-06-25 19:28:57 +0300472};