blob: 03ae9c3a8d97f41cf5302a55fa0eb7cfa30588db [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08002/*
3 * Intel IXP4xx NPE-C crypto driver
4 *
5 * Copyright (C) 2008 Christian Hohnstaedt <chohnstaedt@innominate.com>
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08006 */
7
8#include <linux/platform_device.h>
9#include <linux/dma-mapping.h>
10#include <linux/dmapool.h>
11#include <linux/crypto.h>
12#include <linux/kernel.h>
13#include <linux/rtnetlink.h>
14#include <linux/interrupt.h>
15#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Michał Wróbel752587232012-04-05 20:34:21 +080017#include <linux/module.h>
Christian Hohnstaedt81bef012008-06-25 14:38:47 +080018
19#include <crypto/ctr.h>
Ard Biesheuvel3ca20b62019-08-15 12:00:56 +030020#include <crypto/internal/des.h>
Christian Hohnstaedt81bef012008-06-25 14:38:47 +080021#include <crypto/aes.h>
Corentin LABBEbb9634d2017-05-19 08:53:25 +020022#include <crypto/hmac.h>
Eric Biggersa24d22b2020-11-12 21:20:21 -080023#include <crypto/sha1.h>
Christian Hohnstaedt81bef012008-06-25 14:38:47 +080024#include <crypto/algapi.h>
Herbert Xu5290b422015-05-11 17:47:44 +080025#include <crypto/internal/aead.h>
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +010026#include <crypto/internal/skcipher.h>
Christian Hohnstaedt81bef012008-06-25 14:38:47 +080027#include <crypto/authenc.h>
28#include <crypto/scatterwalk.h>
29
Linus Walleij4af20dc2019-02-10 14:55:58 +010030#include <linux/soc/ixp4xx/npe.h>
31#include <linux/soc/ixp4xx/qmgr.h>
Christian Hohnstaedt81bef012008-06-25 14:38:47 +080032
33#define MAX_KEYLEN 32
34
35/* hash: cfgword + 2 * digestlen; crypt: keylen + cfgword */
36#define NPE_CTX_LEN 80
37#define AES_BLOCK128 16
38
39#define NPE_OP_HASH_VERIFY 0x01
40#define NPE_OP_CCM_ENABLE 0x04
41#define NPE_OP_CRYPT_ENABLE 0x08
42#define NPE_OP_HASH_ENABLE 0x10
43#define NPE_OP_NOT_IN_PLACE 0x20
44#define NPE_OP_HMAC_DISABLE 0x40
45#define NPE_OP_CRYPT_ENCRYPT 0x80
46
47#define NPE_OP_CCM_GEN_MIC 0xcc
48#define NPE_OP_HASH_GEN_ICV 0x50
49#define NPE_OP_ENC_GEN_KEY 0xc9
50
51#define MOD_ECB 0x0000
52#define MOD_CTR 0x1000
53#define MOD_CBC_ENC 0x2000
54#define MOD_CBC_DEC 0x3000
55#define MOD_CCM_ENC 0x4000
56#define MOD_CCM_DEC 0x5000
57
58#define KEYLEN_128 4
59#define KEYLEN_192 6
60#define KEYLEN_256 8
61
62#define CIPH_DECR 0x0000
63#define CIPH_ENCR 0x0400
64
65#define MOD_DES 0x0000
66#define MOD_TDEA2 0x0100
67#define MOD_3DES 0x0200
68#define MOD_AES 0x0800
69#define MOD_AES128 (0x0800 | KEYLEN_128)
70#define MOD_AES192 (0x0900 | KEYLEN_192)
71#define MOD_AES256 (0x0a00 | KEYLEN_256)
72
73#define MAX_IVLEN 16
74#define NPE_ID 2 /* NPE C */
75#define NPE_QLEN 16
76/* Space for registering when the first
77 * NPE_QLEN crypt_ctl are busy */
78#define NPE_QLEN_TOTAL 64
79
80#define SEND_QID 29
81#define RECV_QID 30
82
83#define CTL_FLAG_UNUSED 0x0000
84#define CTL_FLAG_USED 0x1000
85#define CTL_FLAG_PERFORM_ABLK 0x0001
86#define CTL_FLAG_GEN_ICV 0x0002
87#define CTL_FLAG_GEN_REVAES 0x0004
88#define CTL_FLAG_PERFORM_AEAD 0x0008
89#define CTL_FLAG_MASK 0x000f
90
Christian Hohnstaedt81bef012008-06-25 14:38:47 +080091#define HMAC_PAD_BLOCKLEN SHA1_BLOCK_SIZE
92
93#define MD5_DIGEST_SIZE 16
94
95struct buffer_desc {
96 u32 phys_next;
Krzysztof Hałasace057292010-01-10 14:20:10 +010097#ifdef __ARMEB__
Christian Hohnstaedt81bef012008-06-25 14:38:47 +080098 u16 buf_len;
99 u16 pkt_len;
Krzysztof Hałasace057292010-01-10 14:20:10 +0100100#else
101 u16 pkt_len;
102 u16 buf_len;
103#endif
Herbert Xuff455ad92019-05-23 14:35:30 +0800104 dma_addr_t phys_addr;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800105 u32 __reserved[4];
106 struct buffer_desc *next;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800107 enum dma_data_direction dir;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800108};
109
110struct crypt_ctl {
Krzysztof Hałasace057292010-01-10 14:20:10 +0100111#ifdef __ARMEB__
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800112 u8 mode; /* NPE_OP_* operation mode */
113 u8 init_len;
114 u16 reserved;
Krzysztof Hałasace057292010-01-10 14:20:10 +0100115#else
116 u16 reserved;
117 u8 init_len;
118 u8 mode; /* NPE_OP_* operation mode */
119#endif
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800120 u8 iv[MAX_IVLEN]; /* IV for CBC mode or CTR IV for CTR mode */
Herbert Xuff455ad92019-05-23 14:35:30 +0800121 dma_addr_t icv_rev_aes; /* icv or rev aes */
122 dma_addr_t src_buf;
123 dma_addr_t dst_buf;
Krzysztof Hałasace057292010-01-10 14:20:10 +0100124#ifdef __ARMEB__
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800125 u16 auth_offs; /* Authentication start offset */
126 u16 auth_len; /* Authentication data length */
127 u16 crypt_offs; /* Cryption start offset */
128 u16 crypt_len; /* Cryption data length */
Krzysztof Hałasace057292010-01-10 14:20:10 +0100129#else
130 u16 auth_len; /* Authentication data length */
131 u16 auth_offs; /* Authentication start offset */
132 u16 crypt_len; /* Cryption data length */
133 u16 crypt_offs; /* Cryption start offset */
134#endif
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800135 u32 aadAddr; /* Additional Auth Data Addr for CCM mode */
136 u32 crypto_ctx; /* NPE Crypto Param structure address */
137
138 /* Used by Host: 4*4 bytes*/
Corentin Labbe35570842021-05-05 20:26:11 +0000139 unsigned int ctl_flags;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800140 union {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100141 struct skcipher_request *ablk_req;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800142 struct aead_request *aead_req;
143 struct crypto_tfm *tfm;
144 } data;
145 struct buffer_desc *regist_buf;
146 u8 *regist_ptr;
147};
148
149struct ablk_ctx {
150 struct buffer_desc *src;
151 struct buffer_desc *dst;
Corentin Labbee8acf012021-05-05 20:26:09 +0000152 u8 iv[MAX_IVLEN];
153 bool encrypt;
Corentin Labbedfb098d2021-05-05 20:26:10 +0000154 struct skcipher_request fallback_req; // keep at the end
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800155};
156
157struct aead_ctx {
Herbert Xud7295a82015-07-30 17:53:18 +0800158 struct buffer_desc *src;
159 struct buffer_desc *dst;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800160 struct scatterlist ivlist;
161 /* used when the hmac is not on one sg entry */
162 u8 *hmac_virt;
163 int encrypt;
164};
165
166struct ix_hash_algo {
167 u32 cfgword;
168 unsigned char *icv;
169};
170
171struct ix_sa_dir {
172 unsigned char *npe_ctx;
173 dma_addr_t npe_ctx_phys;
174 int npe_ctx_idx;
175 u8 npe_mode;
176};
177
178struct ixp_ctx {
179 struct ix_sa_dir encrypt;
180 struct ix_sa_dir decrypt;
181 int authkey_len;
182 u8 authkey[MAX_KEYLEN];
183 int enckey_len;
184 u8 enckey[MAX_KEYLEN];
185 u8 salt[MAX_IVLEN];
186 u8 nonce[CTR_RFC3686_NONCE_SIZE];
Corentin Labbe35570842021-05-05 20:26:11 +0000187 unsigned int salted;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800188 atomic_t configuring;
189 struct completion completion;
Corentin Labbedfb098d2021-05-05 20:26:10 +0000190 struct crypto_skcipher *fallback_tfm;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800191};
192
193struct ixp_alg {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100194 struct skcipher_alg crypto;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800195 const struct ix_hash_algo *hash;
196 u32 cfg_enc;
197 u32 cfg_dec;
198
199 int registered;
200};
201
Herbert Xud7295a82015-07-30 17:53:18 +0800202struct ixp_aead_alg {
203 struct aead_alg crypto;
204 const struct ix_hash_algo *hash;
205 u32 cfg_enc;
206 u32 cfg_dec;
207
208 int registered;
209};
210
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800211static const struct ix_hash_algo hash_alg_md5 = {
212 .cfgword = 0xAA010004,
213 .icv = "\x01\x23\x45\x67\x89\xAB\xCD\xEF"
214 "\xFE\xDC\xBA\x98\x76\x54\x32\x10",
215};
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000216
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800217static const struct ix_hash_algo hash_alg_sha1 = {
218 .cfgword = 0x00000005,
219 .icv = "\x67\x45\x23\x01\xEF\xCD\xAB\x89\x98\xBA"
220 "\xDC\xFE\x10\x32\x54\x76\xC3\xD2\xE1\xF0",
221};
222
223static struct npe *npe_c;
Corentin Labbe87d11a52021-05-05 20:26:14 +0000224static struct dma_pool *buffer_pool;
225static struct dma_pool *ctx_pool;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800226
Corentin Labbe87d11a52021-05-05 20:26:14 +0000227static struct crypt_ctl *crypt_virt;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800228static dma_addr_t crypt_phys;
229
230static int support_aes = 1;
231
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800232#define DRIVER_NAME "ixp4xx_crypto"
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800233
Russell Kingd8cbc3f2013-06-10 18:52:52 +0100234static struct platform_device *pdev;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800235
236static inline dma_addr_t crypt_virt2phys(struct crypt_ctl *virt)
237{
238 return crypt_phys + (virt - crypt_virt) * sizeof(struct crypt_ctl);
239}
240
241static inline struct crypt_ctl *crypt_phys2virt(dma_addr_t phys)
242{
243 return crypt_virt + (phys - crypt_phys) / sizeof(struct crypt_ctl);
244}
245
246static inline u32 cipher_cfg_enc(struct crypto_tfm *tfm)
247{
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000248 return container_of(tfm->__crt_alg, struct ixp_alg, crypto.base)->cfg_enc;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800249}
250
251static inline u32 cipher_cfg_dec(struct crypto_tfm *tfm)
252{
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000253 return container_of(tfm->__crt_alg, struct ixp_alg, crypto.base)->cfg_dec;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800254}
255
256static inline const struct ix_hash_algo *ix_hash(struct crypto_tfm *tfm)
257{
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100258 return container_of(tfm->__crt_alg, struct ixp_alg, crypto.base)->hash;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800259}
260
261static int setup_crypt_desc(void)
262{
Russell King27c17892013-06-10 19:09:45 +0100263 struct device *dev = &pdev->dev;
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000264
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800265 BUILD_BUG_ON(sizeof(struct crypt_ctl) != 64);
Luis Chamberlain750afb02019-01-04 09:23:09 +0100266 crypt_virt = dma_alloc_coherent(dev,
267 NPE_QLEN * sizeof(struct crypt_ctl),
268 &crypt_phys, GFP_ATOMIC);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800269 if (!crypt_virt)
270 return -ENOMEM;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800271 return 0;
272}
273
Guobin Huang7dad7d02021-04-06 20:02:57 +0800274static DEFINE_SPINLOCK(desc_lock);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800275static struct crypt_ctl *get_crypt_desc(void)
276{
277 int i;
Corentin Labbe87d11a52021-05-05 20:26:14 +0000278 static int idx;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800279 unsigned long flags;
280
281 spin_lock_irqsave(&desc_lock, flags);
282
283 if (unlikely(!crypt_virt))
284 setup_crypt_desc();
285 if (unlikely(!crypt_virt)) {
286 spin_unlock_irqrestore(&desc_lock, flags);
287 return NULL;
288 }
289 i = idx;
290 if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
291 if (++idx >= NPE_QLEN)
292 idx = 0;
293 crypt_virt[i].ctl_flags = CTL_FLAG_USED;
294 spin_unlock_irqrestore(&desc_lock, flags);
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000295 return crypt_virt + i;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800296 } else {
297 spin_unlock_irqrestore(&desc_lock, flags);
298 return NULL;
299 }
300}
301
Guobin Huang7dad7d02021-04-06 20:02:57 +0800302static DEFINE_SPINLOCK(emerg_lock);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800303static struct crypt_ctl *get_crypt_desc_emerg(void)
304{
305 int i;
306 static int idx = NPE_QLEN;
307 struct crypt_ctl *desc;
308 unsigned long flags;
309
310 desc = get_crypt_desc();
311 if (desc)
312 return desc;
313 if (unlikely(!crypt_virt))
314 return NULL;
315
316 spin_lock_irqsave(&emerg_lock, flags);
317 i = idx;
318 if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
319 if (++idx >= NPE_QLEN_TOTAL)
320 idx = NPE_QLEN;
321 crypt_virt[i].ctl_flags = CTL_FLAG_USED;
322 spin_unlock_irqrestore(&emerg_lock, flags);
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000323 return crypt_virt + i;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800324 } else {
325 spin_unlock_irqrestore(&emerg_lock, flags);
326 return NULL;
327 }
328}
329
Herbert Xuff455ad92019-05-23 14:35:30 +0800330static void free_buf_chain(struct device *dev, struct buffer_desc *buf,
331 dma_addr_t phys)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800332{
333 while (buf) {
334 struct buffer_desc *buf1;
335 u32 phys1;
336
337 buf1 = buf->next;
338 phys1 = buf->phys_next;
Corentin Labbe9395c582021-05-05 20:26:08 +0000339 dma_unmap_single(dev, buf->phys_addr, buf->buf_len, buf->dir);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800340 dma_pool_free(buffer_pool, buf, phys);
341 buf = buf1;
342 phys = phys1;
343 }
344}
345
346static struct tasklet_struct crypto_done_tasklet;
347
348static void finish_scattered_hmac(struct crypt_ctl *crypt)
349{
350 struct aead_request *req = crypt->data.aead_req;
351 struct aead_ctx *req_ctx = aead_request_ctx(req);
352 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
353 int authsize = crypto_aead_authsize(tfm);
Herbert Xud7295a82015-07-30 17:53:18 +0800354 int decryptlen = req->assoclen + req->cryptlen - authsize;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800355
356 if (req_ctx->encrypt) {
357 scatterwalk_map_and_copy(req_ctx->hmac_virt,
Herbert Xud7295a82015-07-30 17:53:18 +0800358 req->dst, decryptlen, authsize, 1);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800359 }
360 dma_pool_free(buffer_pool, req_ctx->hmac_virt, crypt->icv_rev_aes);
361}
362
363static void one_packet(dma_addr_t phys)
364{
Russell King27c17892013-06-10 19:09:45 +0100365 struct device *dev = &pdev->dev;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800366 struct crypt_ctl *crypt;
367 struct ixp_ctx *ctx;
368 int failed;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800369
370 failed = phys & 0x1 ? -EBADMSG : 0;
371 phys &= ~0x3;
372 crypt = crypt_phys2virt(phys);
373
374 switch (crypt->ctl_flags & CTL_FLAG_MASK) {
375 case CTL_FLAG_PERFORM_AEAD: {
376 struct aead_request *req = crypt->data.aead_req;
377 struct aead_ctx *req_ctx = aead_request_ctx(req);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800378
Herbert Xud7295a82015-07-30 17:53:18 +0800379 free_buf_chain(dev, req_ctx->src, crypt->src_buf);
380 free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
Corentin Labbeffb017e2021-05-05 20:26:15 +0000381 if (req_ctx->hmac_virt)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800382 finish_scattered_hmac(crypt);
Corentin Labbeffb017e2021-05-05 20:26:15 +0000383
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800384 req->base.complete(&req->base, failed);
385 break;
386 }
387 case CTL_FLAG_PERFORM_ABLK: {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100388 struct skcipher_request *req = crypt->data.ablk_req;
389 struct ablk_ctx *req_ctx = skcipher_request_ctx(req);
Corentin Labbee8acf012021-05-05 20:26:09 +0000390 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
391 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
392 unsigned int offset;
393
394 if (ivsize > 0) {
395 offset = req->cryptlen - ivsize;
396 if (req_ctx->encrypt) {
397 scatterwalk_map_and_copy(req->iv, req->dst,
398 offset, ivsize, 0);
399 } else {
400 memcpy(req->iv, req_ctx->iv, ivsize);
401 memzero_explicit(req_ctx->iv, ivsize);
402 }
403 }
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800404
Corentin Labbeffb017e2021-05-05 20:26:15 +0000405 if (req_ctx->dst)
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800406 free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
Corentin Labbeffb017e2021-05-05 20:26:15 +0000407
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800408 free_buf_chain(dev, req_ctx->src, crypt->src_buf);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800409 req->base.complete(&req->base, failed);
410 break;
411 }
412 case CTL_FLAG_GEN_ICV:
413 ctx = crypto_tfm_ctx(crypt->data.tfm);
414 dma_pool_free(ctx_pool, crypt->regist_ptr,
415 crypt->regist_buf->phys_addr);
416 dma_pool_free(buffer_pool, crypt->regist_buf, crypt->src_buf);
417 if (atomic_dec_and_test(&ctx->configuring))
418 complete(&ctx->completion);
419 break;
420 case CTL_FLAG_GEN_REVAES:
421 ctx = crypto_tfm_ctx(crypt->data.tfm);
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000422 *(u32 *)ctx->decrypt.npe_ctx &= cpu_to_be32(~CIPH_ENCR);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800423 if (atomic_dec_and_test(&ctx->configuring))
424 complete(&ctx->completion);
425 break;
426 default:
427 BUG();
428 }
429 crypt->ctl_flags = CTL_FLAG_UNUSED;
430}
431
432static void irqhandler(void *_unused)
433{
434 tasklet_schedule(&crypto_done_tasklet);
435}
436
437static void crypto_done_action(unsigned long arg)
438{
439 int i;
440
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000441 for (i = 0; i < 4; i++) {
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800442 dma_addr_t phys = qmgr_get_entry(RECV_QID);
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000443
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800444 if (!phys)
445 return;
446 one_packet(phys);
447 }
448 tasklet_schedule(&crypto_done_tasklet);
449}
450
Russell King27c17892013-06-10 19:09:45 +0100451static int init_ixp_crypto(struct device *dev)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800452{
453 int ret = -ENODEV;
Christian Hohnstaedt295c01f2009-04-12 13:01:44 +0800454 u32 msg[2] = { 0, 0 };
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800455
456 if (! ( ~(*IXP4XX_EXP_CFG2) & (IXP4XX_FEATURE_HASH |
457 IXP4XX_FEATURE_AES | IXP4XX_FEATURE_DES))) {
Corentin Labbef5b82be2021-05-05 20:26:12 +0000458 dev_err(dev, "ixp_crypto: No HW crypto available\n");
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800459 return ret;
460 }
461 npe_c = npe_request(NPE_ID);
462 if (!npe_c)
463 return ret;
464
465 if (!npe_running(npe_c)) {
Christian Hohnstaedt295c01f2009-04-12 13:01:44 +0800466 ret = npe_load_firmware(npe_c, npe_name(npe_c), dev);
Quentin Lambertb3637002016-07-22 15:32:42 +0200467 if (ret)
Quentin Lambertc5736a42016-07-22 15:32:41 +0200468 goto npe_release;
Christian Hohnstaedt295c01f2009-04-12 13:01:44 +0800469 if (npe_recv_message(npe_c, msg, "STATUS_MSG"))
470 goto npe_error;
471 } else {
472 if (npe_send_message(npe_c, msg, "STATUS_MSG"))
473 goto npe_error;
474
475 if (npe_recv_message(npe_c, msg, "STATUS_MSG"))
476 goto npe_error;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800477 }
478
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000479 switch ((msg[1] >> 16) & 0xff) {
Christian Hohnstaedt295c01f2009-04-12 13:01:44 +0800480 case 3:
Corentin Labbef5b82be2021-05-05 20:26:12 +0000481 dev_warn(dev, "Firmware of %s lacks AES support\n", npe_name(npe_c));
Christian Hohnstaedt295c01f2009-04-12 13:01:44 +0800482 support_aes = 0;
483 break;
484 case 4:
485 case 5:
486 support_aes = 1;
487 break;
488 default:
Corentin Labbef5b82be2021-05-05 20:26:12 +0000489 dev_err(dev, "Firmware of %s lacks crypto support\n", npe_name(npe_c));
Quentin Lambertc5736a42016-07-22 15:32:41 +0200490 ret = -ENODEV;
491 goto npe_release;
Christian Hohnstaedt295c01f2009-04-12 13:01:44 +0800492 }
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800493 /* buffer_pool will also be used to sometimes store the hmac,
494 * so assure it is large enough
495 */
496 BUILD_BUG_ON(SHA1_DIGEST_SIZE > sizeof(struct buffer_desc));
497 buffer_pool = dma_pool_create("buffer", dev,
498 sizeof(struct buffer_desc), 32, 0);
499 ret = -ENOMEM;
Corentin Labbeffb017e2021-05-05 20:26:15 +0000500 if (!buffer_pool)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800501 goto err;
Corentin Labbeffb017e2021-05-05 20:26:15 +0000502
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800503 ctx_pool = dma_pool_create("context", dev,
504 NPE_CTX_LEN, 16, 0);
Corentin Labbeffb017e2021-05-05 20:26:15 +0000505 if (!ctx_pool)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800506 goto err;
Corentin Labbeffb017e2021-05-05 20:26:15 +0000507
Krzysztof Hałasa1777f1a2009-03-04 08:01:22 +0800508 ret = qmgr_request_queue(SEND_QID, NPE_QLEN_TOTAL, 0, 0,
509 "ixp_crypto:out", NULL);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800510 if (ret)
511 goto err;
Krzysztof Hałasa1777f1a2009-03-04 08:01:22 +0800512 ret = qmgr_request_queue(RECV_QID, NPE_QLEN, 0, 0,
513 "ixp_crypto:in", NULL);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800514 if (ret) {
515 qmgr_release_queue(SEND_QID);
516 goto err;
517 }
518 qmgr_set_irq(RECV_QID, QUEUE_IRQ_SRC_NOT_EMPTY, irqhandler, NULL);
519 tasklet_init(&crypto_done_tasklet, crypto_done_action, 0);
520
521 qmgr_enable_irq(RECV_QID);
522 return 0;
Christian Hohnstaedt295c01f2009-04-12 13:01:44 +0800523
524npe_error:
Corentin Labbef5b82be2021-05-05 20:26:12 +0000525 dev_err(dev, "%s not responding\n", npe_name(npe_c));
Christian Hohnstaedt295c01f2009-04-12 13:01:44 +0800526 ret = -EIO;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800527err:
Markus Elfringf9d12932015-11-15 16:51:21 +0100528 dma_pool_destroy(ctx_pool);
529 dma_pool_destroy(buffer_pool);
Quentin Lambertc5736a42016-07-22 15:32:41 +0200530npe_release:
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800531 npe_release(npe_c);
532 return ret;
533}
534
Russell King27c17892013-06-10 19:09:45 +0100535static void release_ixp_crypto(struct device *dev)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800536{
537 qmgr_disable_irq(RECV_QID);
538 tasklet_kill(&crypto_done_tasklet);
539
540 qmgr_release_queue(SEND_QID);
541 qmgr_release_queue(RECV_QID);
542
543 dma_pool_destroy(ctx_pool);
544 dma_pool_destroy(buffer_pool);
545
546 npe_release(npe_c);
547
Corentin Labbeffb017e2021-05-05 20:26:15 +0000548 if (crypt_virt)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800549 dma_free_coherent(dev,
Christophe JAILLETf7ade9a2020-08-02 16:56:48 +0200550 NPE_QLEN * sizeof(struct crypt_ctl),
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800551 crypt_virt, crypt_phys);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800552}
553
554static void reset_sa_dir(struct ix_sa_dir *dir)
555{
556 memset(dir->npe_ctx, 0, NPE_CTX_LEN);
557 dir->npe_ctx_idx = 0;
558 dir->npe_mode = 0;
559}
560
561static int init_sa_dir(struct ix_sa_dir *dir)
562{
563 dir->npe_ctx = dma_pool_alloc(ctx_pool, GFP_KERNEL, &dir->npe_ctx_phys);
Corentin Labbeffb017e2021-05-05 20:26:15 +0000564 if (!dir->npe_ctx)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800565 return -ENOMEM;
Corentin Labbeffb017e2021-05-05 20:26:15 +0000566
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800567 reset_sa_dir(dir);
568 return 0;
569}
570
571static void free_sa_dir(struct ix_sa_dir *dir)
572{
573 memset(dir->npe_ctx, 0, NPE_CTX_LEN);
574 dma_pool_free(ctx_pool, dir->npe_ctx, dir->npe_ctx_phys);
575}
576
577static int init_tfm(struct crypto_tfm *tfm)
578{
579 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
580 int ret;
581
582 atomic_set(&ctx->configuring, 0);
583 ret = init_sa_dir(&ctx->encrypt);
584 if (ret)
585 return ret;
586 ret = init_sa_dir(&ctx->decrypt);
Corentin Labbeffb017e2021-05-05 20:26:15 +0000587 if (ret)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800588 free_sa_dir(&ctx->encrypt);
Corentin Labbeffb017e2021-05-05 20:26:15 +0000589
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800590 return ret;
591}
592
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100593static int init_tfm_ablk(struct crypto_skcipher *tfm)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800594{
Corentin Labbedfb098d2021-05-05 20:26:10 +0000595 struct crypto_tfm *ctfm = crypto_skcipher_tfm(tfm);
596 struct ixp_ctx *ctx = crypto_tfm_ctx(ctfm);
597 const char *name = crypto_tfm_alg_name(ctfm);
598
599 ctx->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
600 if (IS_ERR(ctx->fallback_tfm)) {
601 pr_err("ERROR: Cannot allocate fallback for %s %ld\n",
602 name, PTR_ERR(ctx->fallback_tfm));
603 return PTR_ERR(ctx->fallback_tfm);
604 }
605
606 pr_info("Fallback for %s is %s\n",
607 crypto_tfm_alg_driver_name(&tfm->base),
608 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(ctx->fallback_tfm))
609 );
610
611 crypto_skcipher_set_reqsize(tfm, sizeof(struct ablk_ctx) + crypto_skcipher_reqsize(ctx->fallback_tfm));
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100612 return init_tfm(crypto_skcipher_tfm(tfm));
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800613}
614
Herbert Xud7295a82015-07-30 17:53:18 +0800615static int init_tfm_aead(struct crypto_aead *tfm)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800616{
Herbert Xud7295a82015-07-30 17:53:18 +0800617 crypto_aead_set_reqsize(tfm, sizeof(struct aead_ctx));
618 return init_tfm(crypto_aead_tfm(tfm));
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800619}
620
621static void exit_tfm(struct crypto_tfm *tfm)
622{
623 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000624
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800625 free_sa_dir(&ctx->encrypt);
626 free_sa_dir(&ctx->decrypt);
627}
628
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100629static void exit_tfm_ablk(struct crypto_skcipher *tfm)
630{
Corentin Labbedfb098d2021-05-05 20:26:10 +0000631 struct crypto_tfm *ctfm = crypto_skcipher_tfm(tfm);
632 struct ixp_ctx *ctx = crypto_tfm_ctx(ctfm);
633
634 crypto_free_skcipher(ctx->fallback_tfm);
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100635 exit_tfm(crypto_skcipher_tfm(tfm));
636}
637
Herbert Xud7295a82015-07-30 17:53:18 +0800638static void exit_tfm_aead(struct crypto_aead *tfm)
639{
640 exit_tfm(crypto_aead_tfm(tfm));
641}
642
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800643static int register_chain_var(struct crypto_tfm *tfm, u8 xpad, u32 target,
644 int init_len, u32 ctx_addr, const u8 *key, int key_len)
645{
646 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
647 struct crypt_ctl *crypt;
648 struct buffer_desc *buf;
649 int i;
650 u8 *pad;
Herbert Xuff455ad92019-05-23 14:35:30 +0800651 dma_addr_t pad_phys, buf_phys;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800652
653 BUILD_BUG_ON(NPE_CTX_LEN < HMAC_PAD_BLOCKLEN);
654 pad = dma_pool_alloc(ctx_pool, GFP_KERNEL, &pad_phys);
655 if (!pad)
656 return -ENOMEM;
657 buf = dma_pool_alloc(buffer_pool, GFP_KERNEL, &buf_phys);
658 if (!buf) {
659 dma_pool_free(ctx_pool, pad, pad_phys);
660 return -ENOMEM;
661 }
662 crypt = get_crypt_desc_emerg();
663 if (!crypt) {
664 dma_pool_free(ctx_pool, pad, pad_phys);
665 dma_pool_free(buffer_pool, buf, buf_phys);
666 return -EAGAIN;
667 }
668
669 memcpy(pad, key, key_len);
670 memset(pad + key_len, 0, HMAC_PAD_BLOCKLEN - key_len);
Corentin Labbeffb017e2021-05-05 20:26:15 +0000671 for (i = 0; i < HMAC_PAD_BLOCKLEN; i++)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800672 pad[i] ^= xpad;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800673
674 crypt->data.tfm = tfm;
675 crypt->regist_ptr = pad;
676 crypt->regist_buf = buf;
677
678 crypt->auth_offs = 0;
679 crypt->auth_len = HMAC_PAD_BLOCKLEN;
680 crypt->crypto_ctx = ctx_addr;
681 crypt->src_buf = buf_phys;
682 crypt->icv_rev_aes = target;
683 crypt->mode = NPE_OP_HASH_GEN_ICV;
684 crypt->init_len = init_len;
685 crypt->ctl_flags |= CTL_FLAG_GEN_ICV;
686
687 buf->next = 0;
688 buf->buf_len = HMAC_PAD_BLOCKLEN;
689 buf->pkt_len = 0;
690 buf->phys_addr = pad_phys;
691
692 atomic_inc(&ctx->configuring);
693 qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
694 BUG_ON(qmgr_stat_overflow(SEND_QID));
695 return 0;
696}
697
Corentin Labbe35570842021-05-05 20:26:11 +0000698static int setup_auth(struct crypto_tfm *tfm, int encrypt, unsigned int authsize,
699 const u8 *key, int key_len, unsigned int digest_len)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800700{
701 u32 itarget, otarget, npe_ctx_addr;
702 unsigned char *cinfo;
703 int init_len, ret = 0;
704 u32 cfgword;
705 struct ix_sa_dir *dir;
706 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
707 const struct ix_hash_algo *algo;
708
709 dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
710 cinfo = dir->npe_ctx + dir->npe_ctx_idx;
711 algo = ix_hash(tfm);
712
713 /* write cfg word to cryptinfo */
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000714 cfgword = algo->cfgword | (authsize << 6); /* (authsize/4) << 8 */
Krzysztof Hałasace057292010-01-10 14:20:10 +0100715#ifndef __ARMEB__
716 cfgword ^= 0xAA000000; /* change the "byte swap" flags */
717#endif
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000718 *(u32 *)cinfo = cpu_to_be32(cfgword);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800719 cinfo += sizeof(cfgword);
720
721 /* write ICV to cryptinfo */
722 memcpy(cinfo, algo->icv, digest_len);
723 cinfo += digest_len;
724
725 itarget = dir->npe_ctx_phys + dir->npe_ctx_idx
726 + sizeof(algo->cfgword);
727 otarget = itarget + digest_len;
728 init_len = cinfo - (dir->npe_ctx + dir->npe_ctx_idx);
729 npe_ctx_addr = dir->npe_ctx_phys + dir->npe_ctx_idx;
730
731 dir->npe_ctx_idx += init_len;
732 dir->npe_mode |= NPE_OP_HASH_ENABLE;
733
734 if (!encrypt)
735 dir->npe_mode |= NPE_OP_HASH_VERIFY;
736
737 ret = register_chain_var(tfm, HMAC_OPAD_VALUE, otarget,
738 init_len, npe_ctx_addr, key, key_len);
739 if (ret)
740 return ret;
741 return register_chain_var(tfm, HMAC_IPAD_VALUE, itarget,
742 init_len, npe_ctx_addr, key, key_len);
743}
744
745static int gen_rev_aes_key(struct crypto_tfm *tfm)
746{
747 struct crypt_ctl *crypt;
748 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
749 struct ix_sa_dir *dir = &ctx->decrypt;
750
751 crypt = get_crypt_desc_emerg();
Corentin Labbeffb017e2021-05-05 20:26:15 +0000752 if (!crypt)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800753 return -EAGAIN;
Corentin Labbeffb017e2021-05-05 20:26:15 +0000754
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000755 *(u32 *)dir->npe_ctx |= cpu_to_be32(CIPH_ENCR);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800756
757 crypt->data.tfm = tfm;
758 crypt->crypt_offs = 0;
759 crypt->crypt_len = AES_BLOCK128;
760 crypt->src_buf = 0;
761 crypt->crypto_ctx = dir->npe_ctx_phys;
762 crypt->icv_rev_aes = dir->npe_ctx_phys + sizeof(u32);
763 crypt->mode = NPE_OP_ENC_GEN_KEY;
764 crypt->init_len = dir->npe_ctx_idx;
765 crypt->ctl_flags |= CTL_FLAG_GEN_REVAES;
766
767 atomic_inc(&ctx->configuring);
768 qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
769 BUG_ON(qmgr_stat_overflow(SEND_QID));
770 return 0;
771}
772
773static int setup_cipher(struct crypto_tfm *tfm, int encrypt,
774 const u8 *key, int key_len)
775{
776 u8 *cinfo;
777 u32 cipher_cfg;
778 u32 keylen_cfg = 0;
779 struct ix_sa_dir *dir;
780 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
Eric Biggersc4c4db02019-12-30 21:19:37 -0600781 int err;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800782
783 dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
784 cinfo = dir->npe_ctx;
785
786 if (encrypt) {
787 cipher_cfg = cipher_cfg_enc(tfm);
788 dir->npe_mode |= NPE_OP_CRYPT_ENCRYPT;
789 } else {
790 cipher_cfg = cipher_cfg_dec(tfm);
791 }
792 if (cipher_cfg & MOD_AES) {
793 switch (key_len) {
Krzysztof Hałasa9792eb12010-12-28 13:08:18 +0100794 case 16: keylen_cfg = MOD_AES128; break;
795 case 24: keylen_cfg = MOD_AES192; break;
796 case 32: keylen_cfg = MOD_AES256; break;
797 default:
Krzysztof Hałasa9792eb12010-12-28 13:08:18 +0100798 return -EINVAL;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800799 }
800 cipher_cfg |= keylen_cfg;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800801 } else {
Eric Biggersc4c4db02019-12-30 21:19:37 -0600802 err = crypto_des_verify_key(tfm, key);
803 if (err)
804 return err;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800805 }
806 /* write cfg word to cryptinfo */
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000807 *(u32 *)cinfo = cpu_to_be32(cipher_cfg);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800808 cinfo += sizeof(cipher_cfg);
809
810 /* write cipher key to cryptinfo */
811 memcpy(cinfo, key, key_len);
812 /* NPE wants keylen set to DES3_EDE_KEY_SIZE even for single DES */
813 if (key_len < DES3_EDE_KEY_SIZE && !(cipher_cfg & MOD_AES)) {
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000814 memset(cinfo + key_len, 0, DES3_EDE_KEY_SIZE - key_len);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800815 key_len = DES3_EDE_KEY_SIZE;
816 }
817 dir->npe_ctx_idx = sizeof(cipher_cfg) + key_len;
818 dir->npe_mode |= NPE_OP_CRYPT_ENABLE;
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000819 if ((cipher_cfg & MOD_AES) && !encrypt)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800820 return gen_rev_aes_key(tfm);
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000821
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800822 return 0;
823}
824
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800825static struct buffer_desc *chainup_buffers(struct device *dev,
Corentin Labbe35570842021-05-05 20:26:11 +0000826 struct scatterlist *sg, unsigned int nbytes,
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800827 struct buffer_desc *buf, gfp_t flags,
828 enum dma_data_direction dir)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800829{
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200830 for (; nbytes > 0; sg = sg_next(sg)) {
Corentin Labbe35570842021-05-05 20:26:11 +0000831 unsigned int len = min(nbytes, sg->length);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800832 struct buffer_desc *next_buf;
Herbert Xuff455ad92019-05-23 14:35:30 +0800833 dma_addr_t next_buf_phys;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800834 void *ptr;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800835
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800836 nbytes -= len;
Geliang Tang796b40c2017-03-23 21:16:30 +0800837 ptr = sg_virt(sg);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800838 next_buf = dma_pool_alloc(buffer_pool, flags, &next_buf_phys);
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800839 if (!next_buf) {
840 buf = NULL;
841 break;
842 }
843 sg_dma_address(sg) = dma_map_single(dev, ptr, len, dir);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800844 buf->next = next_buf;
845 buf->phys_next = next_buf_phys;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800846 buf = next_buf;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800847
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800848 buf->phys_addr = sg_dma_address(sg);
849 buf->buf_len = len;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800850 buf->dir = dir;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800851 }
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800852 buf->next = NULL;
853 buf->phys_next = 0;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800854 return buf;
855}
856
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100857static int ablk_setkey(struct crypto_skcipher *tfm, const u8 *key,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800858 unsigned int key_len)
859{
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100860 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800861 int ret;
862
863 init_completion(&ctx->completion);
864 atomic_inc(&ctx->configuring);
865
866 reset_sa_dir(&ctx->encrypt);
867 reset_sa_dir(&ctx->decrypt);
868
869 ctx->encrypt.npe_mode = NPE_OP_HMAC_DISABLE;
870 ctx->decrypt.npe_mode = NPE_OP_HMAC_DISABLE;
871
872 ret = setup_cipher(&tfm->base, 0, key, key_len);
873 if (ret)
874 goto out;
875 ret = setup_cipher(&tfm->base, 1, key, key_len);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800876out:
877 if (!atomic_dec_and_test(&ctx->configuring))
878 wait_for_completion(&ctx->completion);
Corentin Labbedfb098d2021-05-05 20:26:10 +0000879 if (ret)
880 return ret;
881 crypto_skcipher_clear_flags(ctx->fallback_tfm, CRYPTO_TFM_REQ_MASK);
882 crypto_skcipher_set_flags(ctx->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
883
884 return crypto_skcipher_setkey(ctx->fallback_tfm, key, key_len);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800885}
886
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100887static int ablk_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
Herbert Xudba434a2019-04-11 16:51:11 +0800888 unsigned int key_len)
889{
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100890 return verify_skcipher_des3_key(tfm, key) ?:
Ard Biesheuvel3ca20b62019-08-15 12:00:56 +0300891 ablk_setkey(tfm, key, key_len);
Herbert Xudba434a2019-04-11 16:51:11 +0800892}
893
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100894static int ablk_rfc3686_setkey(struct crypto_skcipher *tfm, const u8 *key,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800895 unsigned int key_len)
896{
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100897 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800898
899 /* the nonce is stored in bytes at end of key */
900 if (key_len < CTR_RFC3686_NONCE_SIZE)
901 return -EINVAL;
902
903 memcpy(ctx->nonce, key + (key_len - CTR_RFC3686_NONCE_SIZE),
904 CTR_RFC3686_NONCE_SIZE);
905
906 key_len -= CTR_RFC3686_NONCE_SIZE;
907 return ablk_setkey(tfm, key, key_len);
908}
909
Corentin Labbedfb098d2021-05-05 20:26:10 +0000910static int ixp4xx_cipher_fallback(struct skcipher_request *areq, int encrypt)
911{
912 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
913 struct ixp_ctx *op = crypto_skcipher_ctx(tfm);
914 struct ablk_ctx *rctx = skcipher_request_ctx(areq);
915 int err;
916
917 skcipher_request_set_tfm(&rctx->fallback_req, op->fallback_tfm);
918 skcipher_request_set_callback(&rctx->fallback_req, areq->base.flags,
919 areq->base.complete, areq->base.data);
920 skcipher_request_set_crypt(&rctx->fallback_req, areq->src, areq->dst,
921 areq->cryptlen, areq->iv);
922 if (encrypt)
923 err = crypto_skcipher_encrypt(&rctx->fallback_req);
924 else
925 err = crypto_skcipher_decrypt(&rctx->fallback_req);
926 return err;
927}
928
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100929static int ablk_perform(struct skcipher_request *req, int encrypt)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800930{
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100931 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
932 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
Corentin Labbe35570842021-05-05 20:26:11 +0000933 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800934 struct ix_sa_dir *dir;
935 struct crypt_ctl *crypt;
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100936 unsigned int nbytes = req->cryptlen;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800937 enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100938 struct ablk_ctx *req_ctx = skcipher_request_ctx(req);
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800939 struct buffer_desc src_hook;
Russell King27c17892013-06-10 19:09:45 +0100940 struct device *dev = &pdev->dev;
Corentin Labbee8acf012021-05-05 20:26:09 +0000941 unsigned int offset;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800942 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
943 GFP_KERNEL : GFP_ATOMIC;
944
Corentin Labbedfb098d2021-05-05 20:26:10 +0000945 if (sg_nents(req->src) > 1 || sg_nents(req->dst) > 1)
946 return ixp4xx_cipher_fallback(req, encrypt);
947
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800948 if (qmgr_stat_full(SEND_QID))
949 return -EAGAIN;
950 if (atomic_read(&ctx->configuring))
951 return -EAGAIN;
952
953 dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
Corentin Labbee8acf012021-05-05 20:26:09 +0000954 req_ctx->encrypt = encrypt;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800955
956 crypt = get_crypt_desc();
957 if (!crypt)
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800958 return -ENOMEM;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800959
960 crypt->data.ablk_req = req;
961 crypt->crypto_ctx = dir->npe_ctx_phys;
962 crypt->mode = dir->npe_mode;
963 crypt->init_len = dir->npe_ctx_idx;
964
965 crypt->crypt_offs = 0;
966 crypt->crypt_len = nbytes;
967
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100968 BUG_ON(ivsize && !req->iv);
969 memcpy(crypt->iv, req->iv, ivsize);
Corentin Labbee8acf012021-05-05 20:26:09 +0000970 if (ivsize > 0 && !encrypt) {
971 offset = req->cryptlen - ivsize;
972 scatterwalk_map_and_copy(req_ctx->iv, req->src, offset, ivsize, 0);
973 }
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800974 if (req->src != req->dst) {
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800975 struct buffer_desc dst_hook;
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000976
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800977 crypt->mode |= NPE_OP_NOT_IN_PLACE;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800978 /* This was never tested by Intel
979 * for more than one dst buffer, I think. */
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800980 req_ctx->dst = NULL;
981 if (!chainup_buffers(dev, req->dst, nbytes, &dst_hook,
982 flags, DMA_FROM_DEVICE))
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800983 goto free_buf_dest;
984 src_direction = DMA_TO_DEVICE;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800985 req_ctx->dst = dst_hook.next;
986 crypt->dst_buf = dst_hook.phys_next;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800987 } else {
988 req_ctx->dst = NULL;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800989 }
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800990 req_ctx->src = NULL;
991 if (!chainup_buffers(dev, req->src, nbytes, &src_hook,
992 flags, src_direction))
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800993 goto free_buf_src;
994
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800995 req_ctx->src = src_hook.next;
996 crypt->src_buf = src_hook.phys_next;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800997 crypt->ctl_flags |= CTL_FLAG_PERFORM_ABLK;
998 qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
999 BUG_ON(qmgr_stat_overflow(SEND_QID));
1000 return -EINPROGRESS;
1001
1002free_buf_src:
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001003 free_buf_chain(dev, req_ctx->src, crypt->src_buf);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001004free_buf_dest:
Corentin Labbeffb017e2021-05-05 20:26:15 +00001005 if (req->src != req->dst)
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001006 free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
Corentin Labbeffb017e2021-05-05 20:26:15 +00001007
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001008 crypt->ctl_flags = CTL_FLAG_UNUSED;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001009 return -ENOMEM;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001010}
1011
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001012static int ablk_encrypt(struct skcipher_request *req)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001013{
1014 return ablk_perform(req, 1);
1015}
1016
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001017static int ablk_decrypt(struct skcipher_request *req)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001018{
1019 return ablk_perform(req, 0);
1020}
1021
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001022static int ablk_rfc3686_crypt(struct skcipher_request *req)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001023{
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001024 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1025 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001026 u8 iv[CTR_RFC3686_BLOCK_SIZE];
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001027 u8 *info = req->iv;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001028 int ret;
1029
1030 /* set up counter block */
Corentin Labbe39e39cf2021-05-05 20:26:13 +00001031 memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001032 memcpy(iv + CTR_RFC3686_NONCE_SIZE, info, CTR_RFC3686_IV_SIZE);
1033
1034 /* initialize counter portion of counter block */
1035 *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
1036 cpu_to_be32(1);
1037
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001038 req->iv = iv;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001039 ret = ablk_perform(req, 1);
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001040 req->iv = info;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001041 return ret;
1042}
1043
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001044static int aead_perform(struct aead_request *req, int encrypt,
1045 int cryptoffset, int eff_cryptlen, u8 *iv)
1046{
1047 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1048 struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
Corentin Labbe35570842021-05-05 20:26:11 +00001049 unsigned int ivsize = crypto_aead_ivsize(tfm);
1050 unsigned int authsize = crypto_aead_authsize(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001051 struct ix_sa_dir *dir;
1052 struct crypt_ctl *crypt;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001053 unsigned int cryptlen;
1054 struct buffer_desc *buf, src_hook;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001055 struct aead_ctx *req_ctx = aead_request_ctx(req);
Russell King27c17892013-06-10 19:09:45 +01001056 struct device *dev = &pdev->dev;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001057 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
1058 GFP_KERNEL : GFP_ATOMIC;
Herbert Xud7295a82015-07-30 17:53:18 +08001059 enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
1060 unsigned int lastlen;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001061
1062 if (qmgr_stat_full(SEND_QID))
1063 return -EAGAIN;
1064 if (atomic_read(&ctx->configuring))
1065 return -EAGAIN;
1066
1067 if (encrypt) {
1068 dir = &ctx->encrypt;
1069 cryptlen = req->cryptlen;
1070 } else {
1071 dir = &ctx->decrypt;
1072 /* req->cryptlen includes the authsize when decrypting */
Corentin Labbe39e39cf2021-05-05 20:26:13 +00001073 cryptlen = req->cryptlen - authsize;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001074 eff_cryptlen -= authsize;
1075 }
1076 crypt = get_crypt_desc();
1077 if (!crypt)
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001078 return -ENOMEM;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001079
1080 crypt->data.aead_req = req;
1081 crypt->crypto_ctx = dir->npe_ctx_phys;
1082 crypt->mode = dir->npe_mode;
1083 crypt->init_len = dir->npe_ctx_idx;
1084
1085 crypt->crypt_offs = cryptoffset;
1086 crypt->crypt_len = eff_cryptlen;
1087
1088 crypt->auth_offs = 0;
Herbert Xud7295a82015-07-30 17:53:18 +08001089 crypt->auth_len = req->assoclen + cryptlen;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001090 BUG_ON(ivsize && !req->iv);
1091 memcpy(crypt->iv, req->iv, ivsize);
1092
Herbert Xu0f987e22016-01-19 09:00:21 +08001093 buf = chainup_buffers(dev, req->src, crypt->auth_len,
1094 &src_hook, flags, src_direction);
1095 req_ctx->src = src_hook.next;
1096 crypt->src_buf = src_hook.phys_next;
1097 if (!buf)
1098 goto free_buf_src;
1099
1100 lastlen = buf->buf_len;
1101 if (lastlen >= authsize)
1102 crypt->icv_rev_aes = buf->phys_addr +
1103 buf->buf_len - authsize;
1104
Herbert Xud7295a82015-07-30 17:53:18 +08001105 req_ctx->dst = NULL;
1106
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001107 if (req->src != req->dst) {
Herbert Xud7295a82015-07-30 17:53:18 +08001108 struct buffer_desc dst_hook;
1109
1110 crypt->mode |= NPE_OP_NOT_IN_PLACE;
1111 src_direction = DMA_TO_DEVICE;
1112
1113 buf = chainup_buffers(dev, req->dst, crypt->auth_len,
1114 &dst_hook, flags, DMA_FROM_DEVICE);
1115 req_ctx->dst = dst_hook.next;
1116 crypt->dst_buf = dst_hook.phys_next;
1117
1118 if (!buf)
1119 goto free_buf_dst;
1120
1121 if (encrypt) {
1122 lastlen = buf->buf_len;
1123 if (lastlen >= authsize)
1124 crypt->icv_rev_aes = buf->phys_addr +
1125 buf->buf_len - authsize;
1126 }
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001127 }
1128
Herbert Xud7295a82015-07-30 17:53:18 +08001129 if (unlikely(lastlen < authsize)) {
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001130 /* The 12 hmac bytes are scattered,
1131 * we need to copy them into a safe buffer */
1132 req_ctx->hmac_virt = dma_pool_alloc(buffer_pool, flags,
1133 &crypt->icv_rev_aes);
1134 if (unlikely(!req_ctx->hmac_virt))
Herbert Xu28389572017-08-02 16:40:47 +08001135 goto free_buf_dst;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001136 if (!encrypt) {
1137 scatterwalk_map_and_copy(req_ctx->hmac_virt,
1138 req->src, cryptlen, authsize, 0);
1139 }
1140 req_ctx->encrypt = encrypt;
1141 } else {
1142 req_ctx->hmac_virt = NULL;
1143 }
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001144
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001145 crypt->ctl_flags |= CTL_FLAG_PERFORM_AEAD;
1146 qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
1147 BUG_ON(qmgr_stat_overflow(SEND_QID));
1148 return -EINPROGRESS;
Herbert Xud7295a82015-07-30 17:53:18 +08001149
Herbert Xud7295a82015-07-30 17:53:18 +08001150free_buf_dst:
1151 free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
Herbert Xu28389572017-08-02 16:40:47 +08001152free_buf_src:
1153 free_buf_chain(dev, req_ctx->src, crypt->src_buf);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001154 crypt->ctl_flags = CTL_FLAG_UNUSED;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001155 return -ENOMEM;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001156}
1157
1158static int aead_setup(struct crypto_aead *tfm, unsigned int authsize)
1159{
1160 struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
Corentin Labbe35570842021-05-05 20:26:11 +00001161 unsigned int digest_len = crypto_aead_maxauthsize(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001162 int ret;
1163
1164 if (!ctx->enckey_len && !ctx->authkey_len)
1165 return 0;
1166 init_completion(&ctx->completion);
1167 atomic_inc(&ctx->configuring);
1168
1169 reset_sa_dir(&ctx->encrypt);
1170 reset_sa_dir(&ctx->decrypt);
1171
1172 ret = setup_cipher(&tfm->base, 0, ctx->enckey, ctx->enckey_len);
1173 if (ret)
1174 goto out;
1175 ret = setup_cipher(&tfm->base, 1, ctx->enckey, ctx->enckey_len);
1176 if (ret)
1177 goto out;
1178 ret = setup_auth(&tfm->base, 0, authsize, ctx->authkey,
1179 ctx->authkey_len, digest_len);
1180 if (ret)
1181 goto out;
1182 ret = setup_auth(&tfm->base, 1, authsize, ctx->authkey,
1183 ctx->authkey_len, digest_len);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001184out:
1185 if (!atomic_dec_and_test(&ctx->configuring))
1186 wait_for_completion(&ctx->completion);
1187 return ret;
1188}
1189
1190static int aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
1191{
Herbert Xu6da9c232015-05-21 15:11:06 +08001192 int max = crypto_aead_maxauthsize(tfm) >> 2;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001193
Corentin Labbe39e39cf2021-05-05 20:26:13 +00001194 if ((authsize >> 2) < 1 || (authsize >> 2) > max || (authsize & 3))
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001195 return -EINVAL;
1196 return aead_setup(tfm, authsize);
1197}
1198
1199static int aead_setkey(struct crypto_aead *tfm, const u8 *key,
1200 unsigned int keylen)
1201{
1202 struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
Mathias Krause56902782013-10-15 13:49:32 +02001203 struct crypto_authenc_keys keys;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001204
Mathias Krause56902782013-10-15 13:49:32 +02001205 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001206 goto badkey;
1207
Mathias Krause56902782013-10-15 13:49:32 +02001208 if (keys.authkeylen > sizeof(ctx->authkey))
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001209 goto badkey;
1210
Mathias Krause56902782013-10-15 13:49:32 +02001211 if (keys.enckeylen > sizeof(ctx->enckey))
1212 goto badkey;
1213
1214 memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
1215 memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
1216 ctx->authkey_len = keys.authkeylen;
1217 ctx->enckey_len = keys.enckeylen;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001218
Tudor-Dan Ambarus0e7da292018-03-23 12:42:21 +02001219 memzero_explicit(&keys, sizeof(keys));
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001220 return aead_setup(tfm, crypto_aead_authsize(tfm));
1221badkey:
Tudor-Dan Ambarus0e7da292018-03-23 12:42:21 +02001222 memzero_explicit(&keys, sizeof(keys));
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001223 return -EINVAL;
1224}
1225
Herbert Xudba434a2019-04-11 16:51:11 +08001226static int des3_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1227 unsigned int keylen)
1228{
1229 struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudba434a2019-04-11 16:51:11 +08001230 struct crypto_authenc_keys keys;
1231 int err;
1232
1233 err = crypto_authenc_extractkeys(&keys, key, keylen);
1234 if (unlikely(err))
1235 goto badkey;
1236
1237 err = -EINVAL;
1238 if (keys.authkeylen > sizeof(ctx->authkey))
1239 goto badkey;
1240
Ard Biesheuvel3ca20b62019-08-15 12:00:56 +03001241 err = verify_aead_des3_key(tfm, keys.enckey, keys.enckeylen);
1242 if (err)
Herbert Xudba434a2019-04-11 16:51:11 +08001243 goto badkey;
1244
1245 memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
1246 memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
1247 ctx->authkey_len = keys.authkeylen;
1248 ctx->enckey_len = keys.enckeylen;
1249
1250 memzero_explicit(&keys, sizeof(keys));
1251 return aead_setup(tfm, crypto_aead_authsize(tfm));
1252badkey:
Herbert Xudba434a2019-04-11 16:51:11 +08001253 memzero_explicit(&keys, sizeof(keys));
1254 return err;
1255}
1256
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001257static int aead_encrypt(struct aead_request *req)
1258{
Herbert Xud7295a82015-07-30 17:53:18 +08001259 return aead_perform(req, 1, req->assoclen, req->cryptlen, req->iv);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001260}
1261
1262static int aead_decrypt(struct aead_request *req)
1263{
Herbert Xud7295a82015-07-30 17:53:18 +08001264 return aead_perform(req, 0, req->assoclen, req->cryptlen, req->iv);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001265}
1266
1267static struct ixp_alg ixp4xx_algos[] = {
1268{
1269 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001270 .base.cra_name = "cbc(des)",
1271 .base.cra_blocksize = DES_BLOCK_SIZE,
1272
1273 .min_keysize = DES_KEY_SIZE,
1274 .max_keysize = DES_KEY_SIZE,
1275 .ivsize = DES_BLOCK_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001276 },
1277 .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
1278 .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
1279
1280}, {
1281 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001282 .base.cra_name = "ecb(des)",
1283 .base.cra_blocksize = DES_BLOCK_SIZE,
1284 .min_keysize = DES_KEY_SIZE,
1285 .max_keysize = DES_KEY_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001286 },
1287 .cfg_enc = CIPH_ENCR | MOD_DES | MOD_ECB | KEYLEN_192,
1288 .cfg_dec = CIPH_DECR | MOD_DES | MOD_ECB | KEYLEN_192,
1289}, {
1290 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001291 .base.cra_name = "cbc(des3_ede)",
1292 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1293
1294 .min_keysize = DES3_EDE_KEY_SIZE,
1295 .max_keysize = DES3_EDE_KEY_SIZE,
1296 .ivsize = DES3_EDE_BLOCK_SIZE,
1297 .setkey = ablk_des3_setkey,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001298 },
1299 .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
1300 .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
1301}, {
1302 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001303 .base.cra_name = "ecb(des3_ede)",
1304 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1305
1306 .min_keysize = DES3_EDE_KEY_SIZE,
1307 .max_keysize = DES3_EDE_KEY_SIZE,
1308 .setkey = ablk_des3_setkey,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001309 },
1310 .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_ECB | KEYLEN_192,
1311 .cfg_dec = CIPH_DECR | MOD_3DES | MOD_ECB | KEYLEN_192,
1312}, {
1313 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001314 .base.cra_name = "cbc(aes)",
1315 .base.cra_blocksize = AES_BLOCK_SIZE,
1316
1317 .min_keysize = AES_MIN_KEY_SIZE,
1318 .max_keysize = AES_MAX_KEY_SIZE,
1319 .ivsize = AES_BLOCK_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001320 },
1321 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
1322 .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
1323}, {
1324 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001325 .base.cra_name = "ecb(aes)",
1326 .base.cra_blocksize = AES_BLOCK_SIZE,
1327
1328 .min_keysize = AES_MIN_KEY_SIZE,
1329 .max_keysize = AES_MAX_KEY_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001330 },
1331 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_ECB,
1332 .cfg_dec = CIPH_DECR | MOD_AES | MOD_ECB,
1333}, {
1334 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001335 .base.cra_name = "ctr(aes)",
1336 .base.cra_blocksize = 1,
1337
1338 .min_keysize = AES_MIN_KEY_SIZE,
1339 .max_keysize = AES_MAX_KEY_SIZE,
1340 .ivsize = AES_BLOCK_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001341 },
1342 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
1343 .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
1344}, {
1345 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001346 .base.cra_name = "rfc3686(ctr(aes))",
1347 .base.cra_blocksize = 1,
1348
1349 .min_keysize = AES_MIN_KEY_SIZE,
1350 .max_keysize = AES_MAX_KEY_SIZE,
1351 .ivsize = AES_BLOCK_SIZE,
1352 .setkey = ablk_rfc3686_setkey,
1353 .encrypt = ablk_rfc3686_crypt,
1354 .decrypt = ablk_rfc3686_crypt,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001355 },
1356 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
1357 .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
Herbert Xud7295a82015-07-30 17:53:18 +08001358} };
1359
1360static struct ixp_aead_alg ixp4xx_aeads[] = {
1361{
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001362 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001363 .base = {
1364 .cra_name = "authenc(hmac(md5),cbc(des))",
1365 .cra_blocksize = DES_BLOCK_SIZE,
1366 },
1367 .ivsize = DES_BLOCK_SIZE,
1368 .maxauthsize = MD5_DIGEST_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001369 },
1370 .hash = &hash_alg_md5,
1371 .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
1372 .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
1373}, {
1374 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001375 .base = {
1376 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
1377 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1378 },
1379 .ivsize = DES3_EDE_BLOCK_SIZE,
1380 .maxauthsize = MD5_DIGEST_SIZE,
Herbert Xudba434a2019-04-11 16:51:11 +08001381 .setkey = des3_aead_setkey,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001382 },
1383 .hash = &hash_alg_md5,
1384 .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
1385 .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
1386}, {
1387 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001388 .base = {
1389 .cra_name = "authenc(hmac(sha1),cbc(des))",
1390 .cra_blocksize = DES_BLOCK_SIZE,
1391 },
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001392 .ivsize = DES_BLOCK_SIZE,
1393 .maxauthsize = SHA1_DIGEST_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001394 },
1395 .hash = &hash_alg_sha1,
1396 .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
1397 .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
1398}, {
1399 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001400 .base = {
1401 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1402 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1403 },
1404 .ivsize = DES3_EDE_BLOCK_SIZE,
1405 .maxauthsize = SHA1_DIGEST_SIZE,
Herbert Xudba434a2019-04-11 16:51:11 +08001406 .setkey = des3_aead_setkey,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001407 },
1408 .hash = &hash_alg_sha1,
1409 .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
1410 .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
1411}, {
1412 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001413 .base = {
1414 .cra_name = "authenc(hmac(md5),cbc(aes))",
1415 .cra_blocksize = AES_BLOCK_SIZE,
1416 },
1417 .ivsize = AES_BLOCK_SIZE,
1418 .maxauthsize = MD5_DIGEST_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001419 },
1420 .hash = &hash_alg_md5,
1421 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
1422 .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
1423}, {
1424 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001425 .base = {
1426 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1427 .cra_blocksize = AES_BLOCK_SIZE,
1428 },
1429 .ivsize = AES_BLOCK_SIZE,
1430 .maxauthsize = SHA1_DIGEST_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001431 },
1432 .hash = &hash_alg_sha1,
1433 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
1434 .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
1435} };
1436
1437#define IXP_POSTFIX "-ixp4xx"
Russell Kingd8cbc3f2013-06-10 18:52:52 +01001438
1439static const struct platform_device_info ixp_dev_info __initdata = {
1440 .name = DRIVER_NAME,
1441 .id = 0,
1442 .dma_mask = DMA_BIT_MASK(32),
1443};
1444
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001445static int __init ixp_module_init(void)
1446{
1447 int num = ARRAY_SIZE(ixp4xx_algos);
Krzysztof Hałasaefb753b2013-12-31 11:51:16 +01001448 int i, err;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001449
Russell Kingd8cbc3f2013-06-10 18:52:52 +01001450 pdev = platform_device_register_full(&ixp_dev_info);
1451 if (IS_ERR(pdev))
1452 return PTR_ERR(pdev);
1453
Russell King27c17892013-06-10 19:09:45 +01001454 err = init_ixp_crypto(&pdev->dev);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001455 if (err) {
Russell Kingd8cbc3f2013-06-10 18:52:52 +01001456 platform_device_unregister(pdev);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001457 return err;
1458 }
Corentin Labbe39e39cf2021-05-05 20:26:13 +00001459 for (i = 0; i < num; i++) {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001460 struct skcipher_alg *cra = &ixp4xx_algos[i].crypto;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001461
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001462 if (snprintf(cra->base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Corentin Labbeffb017e2021-05-05 20:26:15 +00001463 "%s"IXP_POSTFIX, cra->base.cra_name) >=
1464 CRYPTO_MAX_ALG_NAME)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001465 continue;
Corentin Labbeffb017e2021-05-05 20:26:15 +00001466 if (!support_aes && (ixp4xx_algos[i].cfg_enc & MOD_AES))
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001467 continue;
Herbert Xud7295a82015-07-30 17:53:18 +08001468
1469 /* block ciphers */
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001470 cra->base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Mikulas Patockab8aa7dc2020-07-09 23:20:41 -07001471 CRYPTO_ALG_ASYNC |
Corentin Labbedfb098d2021-05-05 20:26:10 +00001472 CRYPTO_ALG_ALLOCATES_MEMORY |
1473 CRYPTO_ALG_NEED_FALLBACK;
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001474 if (!cra->setkey)
1475 cra->setkey = ablk_setkey;
1476 if (!cra->encrypt)
1477 cra->encrypt = ablk_encrypt;
1478 if (!cra->decrypt)
1479 cra->decrypt = ablk_decrypt;
1480 cra->init = init_tfm_ablk;
1481 cra->exit = exit_tfm_ablk;
Herbert Xud7295a82015-07-30 17:53:18 +08001482
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001483 cra->base.cra_ctxsize = sizeof(struct ixp_ctx);
1484 cra->base.cra_module = THIS_MODULE;
1485 cra->base.cra_alignmask = 3;
1486 cra->base.cra_priority = 300;
1487 if (crypto_register_skcipher(cra))
Corentin Labbef5b82be2021-05-05 20:26:12 +00001488 dev_err(&pdev->dev, "Failed to register '%s'\n",
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001489 cra->base.cra_name);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001490 else
1491 ixp4xx_algos[i].registered = 1;
1492 }
Herbert Xud7295a82015-07-30 17:53:18 +08001493
1494 for (i = 0; i < ARRAY_SIZE(ixp4xx_aeads); i++) {
1495 struct aead_alg *cra = &ixp4xx_aeads[i].crypto;
1496
1497 if (snprintf(cra->base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1498 "%s"IXP_POSTFIX, cra->base.cra_name) >=
1499 CRYPTO_MAX_ALG_NAME)
1500 continue;
1501 if (!support_aes && (ixp4xx_algos[i].cfg_enc & MOD_AES))
1502 continue;
1503
1504 /* authenc */
1505 cra->base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Mikulas Patockab8aa7dc2020-07-09 23:20:41 -07001506 CRYPTO_ALG_ASYNC |
1507 CRYPTO_ALG_ALLOCATES_MEMORY;
Herbert Xudba434a2019-04-11 16:51:11 +08001508 cra->setkey = cra->setkey ?: aead_setkey;
Herbert Xud7295a82015-07-30 17:53:18 +08001509 cra->setauthsize = aead_setauthsize;
1510 cra->encrypt = aead_encrypt;
1511 cra->decrypt = aead_decrypt;
1512 cra->init = init_tfm_aead;
1513 cra->exit = exit_tfm_aead;
1514
1515 cra->base.cra_ctxsize = sizeof(struct ixp_ctx);
1516 cra->base.cra_module = THIS_MODULE;
1517 cra->base.cra_alignmask = 3;
1518 cra->base.cra_priority = 300;
1519
1520 if (crypto_register_aead(cra))
Corentin Labbef5b82be2021-05-05 20:26:12 +00001521 dev_err(&pdev->dev, "Failed to register '%s'\n",
Herbert Xud7295a82015-07-30 17:53:18 +08001522 cra->base.cra_driver_name);
1523 else
1524 ixp4xx_aeads[i].registered = 1;
1525 }
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001526 return 0;
1527}
1528
1529static void __exit ixp_module_exit(void)
1530{
1531 int num = ARRAY_SIZE(ixp4xx_algos);
1532 int i;
1533
Herbert Xud7295a82015-07-30 17:53:18 +08001534 for (i = 0; i < ARRAY_SIZE(ixp4xx_aeads); i++) {
1535 if (ixp4xx_aeads[i].registered)
1536 crypto_unregister_aead(&ixp4xx_aeads[i].crypto);
1537 }
1538
Corentin Labbe39e39cf2021-05-05 20:26:13 +00001539 for (i = 0; i < num; i++) {
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001540 if (ixp4xx_algos[i].registered)
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001541 crypto_unregister_skcipher(&ixp4xx_algos[i].crypto);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001542 }
Russell King27c17892013-06-10 19:09:45 +01001543 release_ixp_crypto(&pdev->dev);
Russell Kingd8cbc3f2013-06-10 18:52:52 +01001544 platform_device_unregister(pdev);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001545}
1546
1547module_init(ixp_module_init);
1548module_exit(ixp_module_exit);
1549
1550MODULE_LICENSE("GPL");
1551MODULE_AUTHOR("Christian Hohnstaedt <chohnstaedt@innominate.com>");
1552MODULE_DESCRIPTION("IXP4xx hardware crypto");
1553