blob: 954696a3987508275cd553a9d3f9ec7901508b95 [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);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800381 if (req_ctx->hmac_virt) {
382 finish_scattered_hmac(crypt);
383 }
384 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
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800405 if (req_ctx->dst) {
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800406 free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800407 }
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;
500 if (!buffer_pool) {
501 goto err;
502 }
503 ctx_pool = dma_pool_create("context", dev,
504 NPE_CTX_LEN, 16, 0);
505 if (!ctx_pool) {
506 goto err;
507 }
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
548 if (crypt_virt) {
549 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);
552 }
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800553}
554
555static void reset_sa_dir(struct ix_sa_dir *dir)
556{
557 memset(dir->npe_ctx, 0, NPE_CTX_LEN);
558 dir->npe_ctx_idx = 0;
559 dir->npe_mode = 0;
560}
561
562static int init_sa_dir(struct ix_sa_dir *dir)
563{
564 dir->npe_ctx = dma_pool_alloc(ctx_pool, GFP_KERNEL, &dir->npe_ctx_phys);
565 if (!dir->npe_ctx) {
566 return -ENOMEM;
567 }
568 reset_sa_dir(dir);
569 return 0;
570}
571
572static void free_sa_dir(struct ix_sa_dir *dir)
573{
574 memset(dir->npe_ctx, 0, NPE_CTX_LEN);
575 dma_pool_free(ctx_pool, dir->npe_ctx, dir->npe_ctx_phys);
576}
577
578static int init_tfm(struct crypto_tfm *tfm)
579{
580 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
581 int ret;
582
583 atomic_set(&ctx->configuring, 0);
584 ret = init_sa_dir(&ctx->encrypt);
585 if (ret)
586 return ret;
587 ret = init_sa_dir(&ctx->decrypt);
588 if (ret) {
589 free_sa_dir(&ctx->encrypt);
590 }
591 return ret;
592}
593
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100594static int init_tfm_ablk(struct crypto_skcipher *tfm)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800595{
Corentin Labbedfb098d2021-05-05 20:26:10 +0000596 struct crypto_tfm *ctfm = crypto_skcipher_tfm(tfm);
597 struct ixp_ctx *ctx = crypto_tfm_ctx(ctfm);
598 const char *name = crypto_tfm_alg_name(ctfm);
599
600 ctx->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
601 if (IS_ERR(ctx->fallback_tfm)) {
602 pr_err("ERROR: Cannot allocate fallback for %s %ld\n",
603 name, PTR_ERR(ctx->fallback_tfm));
604 return PTR_ERR(ctx->fallback_tfm);
605 }
606
607 pr_info("Fallback for %s is %s\n",
608 crypto_tfm_alg_driver_name(&tfm->base),
609 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(ctx->fallback_tfm))
610 );
611
612 crypto_skcipher_set_reqsize(tfm, sizeof(struct ablk_ctx) + crypto_skcipher_reqsize(ctx->fallback_tfm));
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100613 return init_tfm(crypto_skcipher_tfm(tfm));
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800614}
615
Herbert Xud7295a82015-07-30 17:53:18 +0800616static int init_tfm_aead(struct crypto_aead *tfm)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800617{
Herbert Xud7295a82015-07-30 17:53:18 +0800618 crypto_aead_set_reqsize(tfm, sizeof(struct aead_ctx));
619 return init_tfm(crypto_aead_tfm(tfm));
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800620}
621
622static void exit_tfm(struct crypto_tfm *tfm)
623{
624 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000625
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800626 free_sa_dir(&ctx->encrypt);
627 free_sa_dir(&ctx->decrypt);
628}
629
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100630static void exit_tfm_ablk(struct crypto_skcipher *tfm)
631{
Corentin Labbedfb098d2021-05-05 20:26:10 +0000632 struct crypto_tfm *ctfm = crypto_skcipher_tfm(tfm);
633 struct ixp_ctx *ctx = crypto_tfm_ctx(ctfm);
634
635 crypto_free_skcipher(ctx->fallback_tfm);
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100636 exit_tfm(crypto_skcipher_tfm(tfm));
637}
638
Herbert Xud7295a82015-07-30 17:53:18 +0800639static void exit_tfm_aead(struct crypto_aead *tfm)
640{
641 exit_tfm(crypto_aead_tfm(tfm));
642}
643
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800644static int register_chain_var(struct crypto_tfm *tfm, u8 xpad, u32 target,
645 int init_len, u32 ctx_addr, const u8 *key, int key_len)
646{
647 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
648 struct crypt_ctl *crypt;
649 struct buffer_desc *buf;
650 int i;
651 u8 *pad;
Herbert Xuff455ad92019-05-23 14:35:30 +0800652 dma_addr_t pad_phys, buf_phys;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800653
654 BUILD_BUG_ON(NPE_CTX_LEN < HMAC_PAD_BLOCKLEN);
655 pad = dma_pool_alloc(ctx_pool, GFP_KERNEL, &pad_phys);
656 if (!pad)
657 return -ENOMEM;
658 buf = dma_pool_alloc(buffer_pool, GFP_KERNEL, &buf_phys);
659 if (!buf) {
660 dma_pool_free(ctx_pool, pad, pad_phys);
661 return -ENOMEM;
662 }
663 crypt = get_crypt_desc_emerg();
664 if (!crypt) {
665 dma_pool_free(ctx_pool, pad, pad_phys);
666 dma_pool_free(buffer_pool, buf, buf_phys);
667 return -EAGAIN;
668 }
669
670 memcpy(pad, key, key_len);
671 memset(pad + key_len, 0, HMAC_PAD_BLOCKLEN - key_len);
672 for (i = 0; i < HMAC_PAD_BLOCKLEN; i++) {
673 pad[i] ^= xpad;
674 }
675
676 crypt->data.tfm = tfm;
677 crypt->regist_ptr = pad;
678 crypt->regist_buf = buf;
679
680 crypt->auth_offs = 0;
681 crypt->auth_len = HMAC_PAD_BLOCKLEN;
682 crypt->crypto_ctx = ctx_addr;
683 crypt->src_buf = buf_phys;
684 crypt->icv_rev_aes = target;
685 crypt->mode = NPE_OP_HASH_GEN_ICV;
686 crypt->init_len = init_len;
687 crypt->ctl_flags |= CTL_FLAG_GEN_ICV;
688
689 buf->next = 0;
690 buf->buf_len = HMAC_PAD_BLOCKLEN;
691 buf->pkt_len = 0;
692 buf->phys_addr = pad_phys;
693
694 atomic_inc(&ctx->configuring);
695 qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
696 BUG_ON(qmgr_stat_overflow(SEND_QID));
697 return 0;
698}
699
Corentin Labbe35570842021-05-05 20:26:11 +0000700static int setup_auth(struct crypto_tfm *tfm, int encrypt, unsigned int authsize,
701 const u8 *key, int key_len, unsigned int digest_len)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800702{
703 u32 itarget, otarget, npe_ctx_addr;
704 unsigned char *cinfo;
705 int init_len, ret = 0;
706 u32 cfgword;
707 struct ix_sa_dir *dir;
708 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
709 const struct ix_hash_algo *algo;
710
711 dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
712 cinfo = dir->npe_ctx + dir->npe_ctx_idx;
713 algo = ix_hash(tfm);
714
715 /* write cfg word to cryptinfo */
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000716 cfgword = algo->cfgword | (authsize << 6); /* (authsize/4) << 8 */
Krzysztof Hałasace057292010-01-10 14:20:10 +0100717#ifndef __ARMEB__
718 cfgword ^= 0xAA000000; /* change the "byte swap" flags */
719#endif
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000720 *(u32 *)cinfo = cpu_to_be32(cfgword);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800721 cinfo += sizeof(cfgword);
722
723 /* write ICV to cryptinfo */
724 memcpy(cinfo, algo->icv, digest_len);
725 cinfo += digest_len;
726
727 itarget = dir->npe_ctx_phys + dir->npe_ctx_idx
728 + sizeof(algo->cfgword);
729 otarget = itarget + digest_len;
730 init_len = cinfo - (dir->npe_ctx + dir->npe_ctx_idx);
731 npe_ctx_addr = dir->npe_ctx_phys + dir->npe_ctx_idx;
732
733 dir->npe_ctx_idx += init_len;
734 dir->npe_mode |= NPE_OP_HASH_ENABLE;
735
736 if (!encrypt)
737 dir->npe_mode |= NPE_OP_HASH_VERIFY;
738
739 ret = register_chain_var(tfm, HMAC_OPAD_VALUE, otarget,
740 init_len, npe_ctx_addr, key, key_len);
741 if (ret)
742 return ret;
743 return register_chain_var(tfm, HMAC_IPAD_VALUE, itarget,
744 init_len, npe_ctx_addr, key, key_len);
745}
746
747static int gen_rev_aes_key(struct crypto_tfm *tfm)
748{
749 struct crypt_ctl *crypt;
750 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
751 struct ix_sa_dir *dir = &ctx->decrypt;
752
753 crypt = get_crypt_desc_emerg();
754 if (!crypt) {
755 return -EAGAIN;
756 }
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000757 *(u32 *)dir->npe_ctx |= cpu_to_be32(CIPH_ENCR);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800758
759 crypt->data.tfm = tfm;
760 crypt->crypt_offs = 0;
761 crypt->crypt_len = AES_BLOCK128;
762 crypt->src_buf = 0;
763 crypt->crypto_ctx = dir->npe_ctx_phys;
764 crypt->icv_rev_aes = dir->npe_ctx_phys + sizeof(u32);
765 crypt->mode = NPE_OP_ENC_GEN_KEY;
766 crypt->init_len = dir->npe_ctx_idx;
767 crypt->ctl_flags |= CTL_FLAG_GEN_REVAES;
768
769 atomic_inc(&ctx->configuring);
770 qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
771 BUG_ON(qmgr_stat_overflow(SEND_QID));
772 return 0;
773}
774
775static int setup_cipher(struct crypto_tfm *tfm, int encrypt,
776 const u8 *key, int key_len)
777{
778 u8 *cinfo;
779 u32 cipher_cfg;
780 u32 keylen_cfg = 0;
781 struct ix_sa_dir *dir;
782 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
Eric Biggersc4c4db02019-12-30 21:19:37 -0600783 int err;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800784
785 dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
786 cinfo = dir->npe_ctx;
787
788 if (encrypt) {
789 cipher_cfg = cipher_cfg_enc(tfm);
790 dir->npe_mode |= NPE_OP_CRYPT_ENCRYPT;
791 } else {
792 cipher_cfg = cipher_cfg_dec(tfm);
793 }
794 if (cipher_cfg & MOD_AES) {
795 switch (key_len) {
Krzysztof Hałasa9792eb12010-12-28 13:08:18 +0100796 case 16: keylen_cfg = MOD_AES128; break;
797 case 24: keylen_cfg = MOD_AES192; break;
798 case 32: keylen_cfg = MOD_AES256; break;
799 default:
Krzysztof Hałasa9792eb12010-12-28 13:08:18 +0100800 return -EINVAL;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800801 }
802 cipher_cfg |= keylen_cfg;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800803 } else {
Eric Biggersc4c4db02019-12-30 21:19:37 -0600804 err = crypto_des_verify_key(tfm, key);
805 if (err)
806 return err;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800807 }
808 /* write cfg word to cryptinfo */
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000809 *(u32 *)cinfo = cpu_to_be32(cipher_cfg);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800810 cinfo += sizeof(cipher_cfg);
811
812 /* write cipher key to cryptinfo */
813 memcpy(cinfo, key, key_len);
814 /* NPE wants keylen set to DES3_EDE_KEY_SIZE even for single DES */
815 if (key_len < DES3_EDE_KEY_SIZE && !(cipher_cfg & MOD_AES)) {
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000816 memset(cinfo + key_len, 0, DES3_EDE_KEY_SIZE - key_len);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800817 key_len = DES3_EDE_KEY_SIZE;
818 }
819 dir->npe_ctx_idx = sizeof(cipher_cfg) + key_len;
820 dir->npe_mode |= NPE_OP_CRYPT_ENABLE;
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000821 if ((cipher_cfg & MOD_AES) && !encrypt)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800822 return gen_rev_aes_key(tfm);
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000823
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800824 return 0;
825}
826
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800827static struct buffer_desc *chainup_buffers(struct device *dev,
Corentin Labbe35570842021-05-05 20:26:11 +0000828 struct scatterlist *sg, unsigned int nbytes,
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800829 struct buffer_desc *buf, gfp_t flags,
830 enum dma_data_direction dir)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800831{
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200832 for (; nbytes > 0; sg = sg_next(sg)) {
Corentin Labbe35570842021-05-05 20:26:11 +0000833 unsigned int len = min(nbytes, sg->length);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800834 struct buffer_desc *next_buf;
Herbert Xuff455ad92019-05-23 14:35:30 +0800835 dma_addr_t next_buf_phys;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800836 void *ptr;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800837
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800838 nbytes -= len;
Geliang Tang796b40c2017-03-23 21:16:30 +0800839 ptr = sg_virt(sg);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800840 next_buf = dma_pool_alloc(buffer_pool, flags, &next_buf_phys);
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800841 if (!next_buf) {
842 buf = NULL;
843 break;
844 }
845 sg_dma_address(sg) = dma_map_single(dev, ptr, len, dir);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800846 buf->next = next_buf;
847 buf->phys_next = next_buf_phys;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800848 buf = next_buf;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800849
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800850 buf->phys_addr = sg_dma_address(sg);
851 buf->buf_len = len;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800852 buf->dir = dir;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800853 }
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800854 buf->next = NULL;
855 buf->phys_next = 0;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800856 return buf;
857}
858
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100859static int ablk_setkey(struct crypto_skcipher *tfm, const u8 *key,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800860 unsigned int key_len)
861{
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100862 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800863 int ret;
864
865 init_completion(&ctx->completion);
866 atomic_inc(&ctx->configuring);
867
868 reset_sa_dir(&ctx->encrypt);
869 reset_sa_dir(&ctx->decrypt);
870
871 ctx->encrypt.npe_mode = NPE_OP_HMAC_DISABLE;
872 ctx->decrypt.npe_mode = NPE_OP_HMAC_DISABLE;
873
874 ret = setup_cipher(&tfm->base, 0, key, key_len);
875 if (ret)
876 goto out;
877 ret = setup_cipher(&tfm->base, 1, key, key_len);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800878out:
879 if (!atomic_dec_and_test(&ctx->configuring))
880 wait_for_completion(&ctx->completion);
Corentin Labbedfb098d2021-05-05 20:26:10 +0000881 if (ret)
882 return ret;
883 crypto_skcipher_clear_flags(ctx->fallback_tfm, CRYPTO_TFM_REQ_MASK);
884 crypto_skcipher_set_flags(ctx->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
885
886 return crypto_skcipher_setkey(ctx->fallback_tfm, key, key_len);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800887}
888
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100889static int ablk_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
Herbert Xudba434a2019-04-11 16:51:11 +0800890 unsigned int key_len)
891{
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100892 return verify_skcipher_des3_key(tfm, key) ?:
Ard Biesheuvel3ca20b62019-08-15 12:00:56 +0300893 ablk_setkey(tfm, key, key_len);
Herbert Xudba434a2019-04-11 16:51:11 +0800894}
895
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100896static int ablk_rfc3686_setkey(struct crypto_skcipher *tfm, const u8 *key,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800897 unsigned int key_len)
898{
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100899 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800900
901 /* the nonce is stored in bytes at end of key */
902 if (key_len < CTR_RFC3686_NONCE_SIZE)
903 return -EINVAL;
904
905 memcpy(ctx->nonce, key + (key_len - CTR_RFC3686_NONCE_SIZE),
906 CTR_RFC3686_NONCE_SIZE);
907
908 key_len -= CTR_RFC3686_NONCE_SIZE;
909 return ablk_setkey(tfm, key, key_len);
910}
911
Corentin Labbedfb098d2021-05-05 20:26:10 +0000912static int ixp4xx_cipher_fallback(struct skcipher_request *areq, int encrypt)
913{
914 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
915 struct ixp_ctx *op = crypto_skcipher_ctx(tfm);
916 struct ablk_ctx *rctx = skcipher_request_ctx(areq);
917 int err;
918
919 skcipher_request_set_tfm(&rctx->fallback_req, op->fallback_tfm);
920 skcipher_request_set_callback(&rctx->fallback_req, areq->base.flags,
921 areq->base.complete, areq->base.data);
922 skcipher_request_set_crypt(&rctx->fallback_req, areq->src, areq->dst,
923 areq->cryptlen, areq->iv);
924 if (encrypt)
925 err = crypto_skcipher_encrypt(&rctx->fallback_req);
926 else
927 err = crypto_skcipher_decrypt(&rctx->fallback_req);
928 return err;
929}
930
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100931static int ablk_perform(struct skcipher_request *req, int encrypt)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800932{
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100933 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
934 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
Corentin Labbe35570842021-05-05 20:26:11 +0000935 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800936 struct ix_sa_dir *dir;
937 struct crypt_ctl *crypt;
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100938 unsigned int nbytes = req->cryptlen;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800939 enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100940 struct ablk_ctx *req_ctx = skcipher_request_ctx(req);
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800941 struct buffer_desc src_hook;
Russell King27c17892013-06-10 19:09:45 +0100942 struct device *dev = &pdev->dev;
Corentin Labbee8acf012021-05-05 20:26:09 +0000943 unsigned int offset;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800944 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
945 GFP_KERNEL : GFP_ATOMIC;
946
Corentin Labbedfb098d2021-05-05 20:26:10 +0000947 if (sg_nents(req->src) > 1 || sg_nents(req->dst) > 1)
948 return ixp4xx_cipher_fallback(req, encrypt);
949
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800950 if (qmgr_stat_full(SEND_QID))
951 return -EAGAIN;
952 if (atomic_read(&ctx->configuring))
953 return -EAGAIN;
954
955 dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
Corentin Labbee8acf012021-05-05 20:26:09 +0000956 req_ctx->encrypt = encrypt;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800957
958 crypt = get_crypt_desc();
959 if (!crypt)
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800960 return -ENOMEM;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800961
962 crypt->data.ablk_req = req;
963 crypt->crypto_ctx = dir->npe_ctx_phys;
964 crypt->mode = dir->npe_mode;
965 crypt->init_len = dir->npe_ctx_idx;
966
967 crypt->crypt_offs = 0;
968 crypt->crypt_len = nbytes;
969
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +0100970 BUG_ON(ivsize && !req->iv);
971 memcpy(crypt->iv, req->iv, ivsize);
Corentin Labbee8acf012021-05-05 20:26:09 +0000972 if (ivsize > 0 && !encrypt) {
973 offset = req->cryptlen - ivsize;
974 scatterwalk_map_and_copy(req_ctx->iv, req->src, offset, ivsize, 0);
975 }
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800976 if (req->src != req->dst) {
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800977 struct buffer_desc dst_hook;
Corentin Labbe39e39cf2021-05-05 20:26:13 +0000978
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800979 crypt->mode |= NPE_OP_NOT_IN_PLACE;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800980 /* This was never tested by Intel
981 * for more than one dst buffer, I think. */
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800982 req_ctx->dst = NULL;
983 if (!chainup_buffers(dev, req->dst, nbytes, &dst_hook,
984 flags, DMA_FROM_DEVICE))
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800985 goto free_buf_dest;
986 src_direction = DMA_TO_DEVICE;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800987 req_ctx->dst = dst_hook.next;
988 crypt->dst_buf = dst_hook.phys_next;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800989 } else {
990 req_ctx->dst = NULL;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800991 }
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800992 req_ctx->src = NULL;
993 if (!chainup_buffers(dev, req->src, nbytes, &src_hook,
994 flags, src_direction))
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800995 goto free_buf_src;
996
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +0800997 req_ctx->src = src_hook.next;
998 crypt->src_buf = src_hook.phys_next;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +0800999 crypt->ctl_flags |= CTL_FLAG_PERFORM_ABLK;
1000 qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
1001 BUG_ON(qmgr_stat_overflow(SEND_QID));
1002 return -EINPROGRESS;
1003
1004free_buf_src:
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001005 free_buf_chain(dev, req_ctx->src, crypt->src_buf);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001006free_buf_dest:
1007 if (req->src != req->dst) {
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001008 free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001009 }
1010 crypt->ctl_flags = CTL_FLAG_UNUSED;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001011 return -ENOMEM;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001012}
1013
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001014static int ablk_encrypt(struct skcipher_request *req)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001015{
1016 return ablk_perform(req, 1);
1017}
1018
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001019static int ablk_decrypt(struct skcipher_request *req)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001020{
1021 return ablk_perform(req, 0);
1022}
1023
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001024static int ablk_rfc3686_crypt(struct skcipher_request *req)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001025{
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001026 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1027 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001028 u8 iv[CTR_RFC3686_BLOCK_SIZE];
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001029 u8 *info = req->iv;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001030 int ret;
1031
1032 /* set up counter block */
Corentin Labbe39e39cf2021-05-05 20:26:13 +00001033 memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001034 memcpy(iv + CTR_RFC3686_NONCE_SIZE, info, CTR_RFC3686_IV_SIZE);
1035
1036 /* initialize counter portion of counter block */
1037 *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
1038 cpu_to_be32(1);
1039
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001040 req->iv = iv;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001041 ret = ablk_perform(req, 1);
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001042 req->iv = info;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001043 return ret;
1044}
1045
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001046static int aead_perform(struct aead_request *req, int encrypt,
1047 int cryptoffset, int eff_cryptlen, u8 *iv)
1048{
1049 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1050 struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
Corentin Labbe35570842021-05-05 20:26:11 +00001051 unsigned int ivsize = crypto_aead_ivsize(tfm);
1052 unsigned int authsize = crypto_aead_authsize(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001053 struct ix_sa_dir *dir;
1054 struct crypt_ctl *crypt;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001055 unsigned int cryptlen;
1056 struct buffer_desc *buf, src_hook;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001057 struct aead_ctx *req_ctx = aead_request_ctx(req);
Russell King27c17892013-06-10 19:09:45 +01001058 struct device *dev = &pdev->dev;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001059 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
1060 GFP_KERNEL : GFP_ATOMIC;
Herbert Xud7295a82015-07-30 17:53:18 +08001061 enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
1062 unsigned int lastlen;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001063
1064 if (qmgr_stat_full(SEND_QID))
1065 return -EAGAIN;
1066 if (atomic_read(&ctx->configuring))
1067 return -EAGAIN;
1068
1069 if (encrypt) {
1070 dir = &ctx->encrypt;
1071 cryptlen = req->cryptlen;
1072 } else {
1073 dir = &ctx->decrypt;
1074 /* req->cryptlen includes the authsize when decrypting */
Corentin Labbe39e39cf2021-05-05 20:26:13 +00001075 cryptlen = req->cryptlen - authsize;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001076 eff_cryptlen -= authsize;
1077 }
1078 crypt = get_crypt_desc();
1079 if (!crypt)
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001080 return -ENOMEM;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001081
1082 crypt->data.aead_req = req;
1083 crypt->crypto_ctx = dir->npe_ctx_phys;
1084 crypt->mode = dir->npe_mode;
1085 crypt->init_len = dir->npe_ctx_idx;
1086
1087 crypt->crypt_offs = cryptoffset;
1088 crypt->crypt_len = eff_cryptlen;
1089
1090 crypt->auth_offs = 0;
Herbert Xud7295a82015-07-30 17:53:18 +08001091 crypt->auth_len = req->assoclen + cryptlen;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001092 BUG_ON(ivsize && !req->iv);
1093 memcpy(crypt->iv, req->iv, ivsize);
1094
Herbert Xu0f987e22016-01-19 09:00:21 +08001095 buf = chainup_buffers(dev, req->src, crypt->auth_len,
1096 &src_hook, flags, src_direction);
1097 req_ctx->src = src_hook.next;
1098 crypt->src_buf = src_hook.phys_next;
1099 if (!buf)
1100 goto free_buf_src;
1101
1102 lastlen = buf->buf_len;
1103 if (lastlen >= authsize)
1104 crypt->icv_rev_aes = buf->phys_addr +
1105 buf->buf_len - authsize;
1106
Herbert Xud7295a82015-07-30 17:53:18 +08001107 req_ctx->dst = NULL;
1108
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001109 if (req->src != req->dst) {
Herbert Xud7295a82015-07-30 17:53:18 +08001110 struct buffer_desc dst_hook;
1111
1112 crypt->mode |= NPE_OP_NOT_IN_PLACE;
1113 src_direction = DMA_TO_DEVICE;
1114
1115 buf = chainup_buffers(dev, req->dst, crypt->auth_len,
1116 &dst_hook, flags, DMA_FROM_DEVICE);
1117 req_ctx->dst = dst_hook.next;
1118 crypt->dst_buf = dst_hook.phys_next;
1119
1120 if (!buf)
1121 goto free_buf_dst;
1122
1123 if (encrypt) {
1124 lastlen = buf->buf_len;
1125 if (lastlen >= authsize)
1126 crypt->icv_rev_aes = buf->phys_addr +
1127 buf->buf_len - authsize;
1128 }
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001129 }
1130
Herbert Xud7295a82015-07-30 17:53:18 +08001131 if (unlikely(lastlen < authsize)) {
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001132 /* The 12 hmac bytes are scattered,
1133 * we need to copy them into a safe buffer */
1134 req_ctx->hmac_virt = dma_pool_alloc(buffer_pool, flags,
1135 &crypt->icv_rev_aes);
1136 if (unlikely(!req_ctx->hmac_virt))
Herbert Xu28389572017-08-02 16:40:47 +08001137 goto free_buf_dst;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001138 if (!encrypt) {
1139 scatterwalk_map_and_copy(req_ctx->hmac_virt,
1140 req->src, cryptlen, authsize, 0);
1141 }
1142 req_ctx->encrypt = encrypt;
1143 } else {
1144 req_ctx->hmac_virt = NULL;
1145 }
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001146
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001147 crypt->ctl_flags |= CTL_FLAG_PERFORM_AEAD;
1148 qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
1149 BUG_ON(qmgr_stat_overflow(SEND_QID));
1150 return -EINPROGRESS;
Herbert Xud7295a82015-07-30 17:53:18 +08001151
Herbert Xud7295a82015-07-30 17:53:18 +08001152free_buf_dst:
1153 free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
Herbert Xu28389572017-08-02 16:40:47 +08001154free_buf_src:
1155 free_buf_chain(dev, req_ctx->src, crypt->src_buf);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001156 crypt->ctl_flags = CTL_FLAG_UNUSED;
Christian Hohnstaedt0d44dc52009-03-27 15:09:05 +08001157 return -ENOMEM;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001158}
1159
1160static int aead_setup(struct crypto_aead *tfm, unsigned int authsize)
1161{
1162 struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
Corentin Labbe35570842021-05-05 20:26:11 +00001163 unsigned int digest_len = crypto_aead_maxauthsize(tfm);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001164 int ret;
1165
1166 if (!ctx->enckey_len && !ctx->authkey_len)
1167 return 0;
1168 init_completion(&ctx->completion);
1169 atomic_inc(&ctx->configuring);
1170
1171 reset_sa_dir(&ctx->encrypt);
1172 reset_sa_dir(&ctx->decrypt);
1173
1174 ret = setup_cipher(&tfm->base, 0, ctx->enckey, ctx->enckey_len);
1175 if (ret)
1176 goto out;
1177 ret = setup_cipher(&tfm->base, 1, ctx->enckey, ctx->enckey_len);
1178 if (ret)
1179 goto out;
1180 ret = setup_auth(&tfm->base, 0, authsize, ctx->authkey,
1181 ctx->authkey_len, digest_len);
1182 if (ret)
1183 goto out;
1184 ret = setup_auth(&tfm->base, 1, authsize, ctx->authkey,
1185 ctx->authkey_len, digest_len);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001186out:
1187 if (!atomic_dec_and_test(&ctx->configuring))
1188 wait_for_completion(&ctx->completion);
1189 return ret;
1190}
1191
1192static int aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
1193{
Herbert Xu6da9c232015-05-21 15:11:06 +08001194 int max = crypto_aead_maxauthsize(tfm) >> 2;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001195
Corentin Labbe39e39cf2021-05-05 20:26:13 +00001196 if ((authsize >> 2) < 1 || (authsize >> 2) > max || (authsize & 3))
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001197 return -EINVAL;
1198 return aead_setup(tfm, authsize);
1199}
1200
1201static int aead_setkey(struct crypto_aead *tfm, const u8 *key,
1202 unsigned int keylen)
1203{
1204 struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
Mathias Krause56902782013-10-15 13:49:32 +02001205 struct crypto_authenc_keys keys;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001206
Mathias Krause56902782013-10-15 13:49:32 +02001207 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001208 goto badkey;
1209
Mathias Krause56902782013-10-15 13:49:32 +02001210 if (keys.authkeylen > sizeof(ctx->authkey))
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001211 goto badkey;
1212
Mathias Krause56902782013-10-15 13:49:32 +02001213 if (keys.enckeylen > sizeof(ctx->enckey))
1214 goto badkey;
1215
1216 memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
1217 memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
1218 ctx->authkey_len = keys.authkeylen;
1219 ctx->enckey_len = keys.enckeylen;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001220
Tudor-Dan Ambarus0e7da292018-03-23 12:42:21 +02001221 memzero_explicit(&keys, sizeof(keys));
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001222 return aead_setup(tfm, crypto_aead_authsize(tfm));
1223badkey:
Tudor-Dan Ambarus0e7da292018-03-23 12:42:21 +02001224 memzero_explicit(&keys, sizeof(keys));
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001225 return -EINVAL;
1226}
1227
Herbert Xudba434a2019-04-11 16:51:11 +08001228static int des3_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1229 unsigned int keylen)
1230{
1231 struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xudba434a2019-04-11 16:51:11 +08001232 struct crypto_authenc_keys keys;
1233 int err;
1234
1235 err = crypto_authenc_extractkeys(&keys, key, keylen);
1236 if (unlikely(err))
1237 goto badkey;
1238
1239 err = -EINVAL;
1240 if (keys.authkeylen > sizeof(ctx->authkey))
1241 goto badkey;
1242
Ard Biesheuvel3ca20b62019-08-15 12:00:56 +03001243 err = verify_aead_des3_key(tfm, keys.enckey, keys.enckeylen);
1244 if (err)
Herbert Xudba434a2019-04-11 16:51:11 +08001245 goto badkey;
1246
1247 memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
1248 memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
1249 ctx->authkey_len = keys.authkeylen;
1250 ctx->enckey_len = keys.enckeylen;
1251
1252 memzero_explicit(&keys, sizeof(keys));
1253 return aead_setup(tfm, crypto_aead_authsize(tfm));
1254badkey:
Herbert Xudba434a2019-04-11 16:51:11 +08001255 memzero_explicit(&keys, sizeof(keys));
1256 return err;
1257}
1258
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001259static int aead_encrypt(struct aead_request *req)
1260{
Herbert Xud7295a82015-07-30 17:53:18 +08001261 return aead_perform(req, 1, req->assoclen, req->cryptlen, req->iv);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001262}
1263
1264static int aead_decrypt(struct aead_request *req)
1265{
Herbert Xud7295a82015-07-30 17:53:18 +08001266 return aead_perform(req, 0, req->assoclen, req->cryptlen, req->iv);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001267}
1268
1269static struct ixp_alg ixp4xx_algos[] = {
1270{
1271 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001272 .base.cra_name = "cbc(des)",
1273 .base.cra_blocksize = DES_BLOCK_SIZE,
1274
1275 .min_keysize = DES_KEY_SIZE,
1276 .max_keysize = DES_KEY_SIZE,
1277 .ivsize = DES_BLOCK_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001278 },
1279 .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
1280 .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
1281
1282}, {
1283 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001284 .base.cra_name = "ecb(des)",
1285 .base.cra_blocksize = DES_BLOCK_SIZE,
1286 .min_keysize = DES_KEY_SIZE,
1287 .max_keysize = DES_KEY_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001288 },
1289 .cfg_enc = CIPH_ENCR | MOD_DES | MOD_ECB | KEYLEN_192,
1290 .cfg_dec = CIPH_DECR | MOD_DES | MOD_ECB | KEYLEN_192,
1291}, {
1292 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001293 .base.cra_name = "cbc(des3_ede)",
1294 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1295
1296 .min_keysize = DES3_EDE_KEY_SIZE,
1297 .max_keysize = DES3_EDE_KEY_SIZE,
1298 .ivsize = DES3_EDE_BLOCK_SIZE,
1299 .setkey = ablk_des3_setkey,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001300 },
1301 .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
1302 .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
1303}, {
1304 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001305 .base.cra_name = "ecb(des3_ede)",
1306 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1307
1308 .min_keysize = DES3_EDE_KEY_SIZE,
1309 .max_keysize = DES3_EDE_KEY_SIZE,
1310 .setkey = ablk_des3_setkey,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001311 },
1312 .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_ECB | KEYLEN_192,
1313 .cfg_dec = CIPH_DECR | MOD_3DES | MOD_ECB | KEYLEN_192,
1314}, {
1315 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001316 .base.cra_name = "cbc(aes)",
1317 .base.cra_blocksize = AES_BLOCK_SIZE,
1318
1319 .min_keysize = AES_MIN_KEY_SIZE,
1320 .max_keysize = AES_MAX_KEY_SIZE,
1321 .ivsize = AES_BLOCK_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001322 },
1323 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
1324 .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
1325}, {
1326 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001327 .base.cra_name = "ecb(aes)",
1328 .base.cra_blocksize = AES_BLOCK_SIZE,
1329
1330 .min_keysize = AES_MIN_KEY_SIZE,
1331 .max_keysize = AES_MAX_KEY_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001332 },
1333 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_ECB,
1334 .cfg_dec = CIPH_DECR | MOD_AES | MOD_ECB,
1335}, {
1336 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001337 .base.cra_name = "ctr(aes)",
1338 .base.cra_blocksize = 1,
1339
1340 .min_keysize = AES_MIN_KEY_SIZE,
1341 .max_keysize = AES_MAX_KEY_SIZE,
1342 .ivsize = AES_BLOCK_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001343 },
1344 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
1345 .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
1346}, {
1347 .crypto = {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001348 .base.cra_name = "rfc3686(ctr(aes))",
1349 .base.cra_blocksize = 1,
1350
1351 .min_keysize = AES_MIN_KEY_SIZE,
1352 .max_keysize = AES_MAX_KEY_SIZE,
1353 .ivsize = AES_BLOCK_SIZE,
1354 .setkey = ablk_rfc3686_setkey,
1355 .encrypt = ablk_rfc3686_crypt,
1356 .decrypt = ablk_rfc3686_crypt,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001357 },
1358 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
1359 .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
Herbert Xud7295a82015-07-30 17:53:18 +08001360} };
1361
1362static struct ixp_aead_alg ixp4xx_aeads[] = {
1363{
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001364 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001365 .base = {
1366 .cra_name = "authenc(hmac(md5),cbc(des))",
1367 .cra_blocksize = DES_BLOCK_SIZE,
1368 },
1369 .ivsize = DES_BLOCK_SIZE,
1370 .maxauthsize = MD5_DIGEST_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001371 },
1372 .hash = &hash_alg_md5,
1373 .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
1374 .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
1375}, {
1376 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001377 .base = {
1378 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
1379 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1380 },
1381 .ivsize = DES3_EDE_BLOCK_SIZE,
1382 .maxauthsize = MD5_DIGEST_SIZE,
Herbert Xudba434a2019-04-11 16:51:11 +08001383 .setkey = des3_aead_setkey,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001384 },
1385 .hash = &hash_alg_md5,
1386 .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
1387 .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
1388}, {
1389 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001390 .base = {
1391 .cra_name = "authenc(hmac(sha1),cbc(des))",
1392 .cra_blocksize = DES_BLOCK_SIZE,
1393 },
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001394 .ivsize = DES_BLOCK_SIZE,
1395 .maxauthsize = SHA1_DIGEST_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001396 },
1397 .hash = &hash_alg_sha1,
1398 .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
1399 .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
1400}, {
1401 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001402 .base = {
1403 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1404 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1405 },
1406 .ivsize = DES3_EDE_BLOCK_SIZE,
1407 .maxauthsize = SHA1_DIGEST_SIZE,
Herbert Xudba434a2019-04-11 16:51:11 +08001408 .setkey = des3_aead_setkey,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001409 },
1410 .hash = &hash_alg_sha1,
1411 .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
1412 .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
1413}, {
1414 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001415 .base = {
1416 .cra_name = "authenc(hmac(md5),cbc(aes))",
1417 .cra_blocksize = AES_BLOCK_SIZE,
1418 },
1419 .ivsize = AES_BLOCK_SIZE,
1420 .maxauthsize = MD5_DIGEST_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001421 },
1422 .hash = &hash_alg_md5,
1423 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
1424 .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
1425}, {
1426 .crypto = {
Herbert Xud7295a82015-07-30 17:53:18 +08001427 .base = {
1428 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1429 .cra_blocksize = AES_BLOCK_SIZE,
1430 },
1431 .ivsize = AES_BLOCK_SIZE,
1432 .maxauthsize = SHA1_DIGEST_SIZE,
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001433 },
1434 .hash = &hash_alg_sha1,
1435 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
1436 .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
1437} };
1438
1439#define IXP_POSTFIX "-ixp4xx"
Russell Kingd8cbc3f2013-06-10 18:52:52 +01001440
1441static const struct platform_device_info ixp_dev_info __initdata = {
1442 .name = DRIVER_NAME,
1443 .id = 0,
1444 .dma_mask = DMA_BIT_MASK(32),
1445};
1446
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001447static int __init ixp_module_init(void)
1448{
1449 int num = ARRAY_SIZE(ixp4xx_algos);
Krzysztof Hałasaefb753b2013-12-31 11:51:16 +01001450 int i, err;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001451
Russell Kingd8cbc3f2013-06-10 18:52:52 +01001452 pdev = platform_device_register_full(&ixp_dev_info);
1453 if (IS_ERR(pdev))
1454 return PTR_ERR(pdev);
1455
Russell King27c17892013-06-10 19:09:45 +01001456 err = init_ixp_crypto(&pdev->dev);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001457 if (err) {
Russell Kingd8cbc3f2013-06-10 18:52:52 +01001458 platform_device_unregister(pdev);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001459 return err;
1460 }
Corentin Labbe39e39cf2021-05-05 20:26:13 +00001461 for (i = 0; i < num; i++) {
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001462 struct skcipher_alg *cra = &ixp4xx_algos[i].crypto;
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001463
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001464 if (snprintf(cra->base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1465 "%s"IXP_POSTFIX, cra->base.cra_name) >=
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001466 CRYPTO_MAX_ALG_NAME)
1467 {
1468 continue;
1469 }
1470 if (!support_aes && (ixp4xx_algos[i].cfg_enc & MOD_AES)) {
1471 continue;
1472 }
Herbert Xud7295a82015-07-30 17:53:18 +08001473
1474 /* block ciphers */
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001475 cra->base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Mikulas Patockab8aa7dc2020-07-09 23:20:41 -07001476 CRYPTO_ALG_ASYNC |
Corentin Labbedfb098d2021-05-05 20:26:10 +00001477 CRYPTO_ALG_ALLOCATES_MEMORY |
1478 CRYPTO_ALG_NEED_FALLBACK;
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001479 if (!cra->setkey)
1480 cra->setkey = ablk_setkey;
1481 if (!cra->encrypt)
1482 cra->encrypt = ablk_encrypt;
1483 if (!cra->decrypt)
1484 cra->decrypt = ablk_decrypt;
1485 cra->init = init_tfm_ablk;
1486 cra->exit = exit_tfm_ablk;
Herbert Xud7295a82015-07-30 17:53:18 +08001487
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001488 cra->base.cra_ctxsize = sizeof(struct ixp_ctx);
1489 cra->base.cra_module = THIS_MODULE;
1490 cra->base.cra_alignmask = 3;
1491 cra->base.cra_priority = 300;
1492 if (crypto_register_skcipher(cra))
Corentin Labbef5b82be2021-05-05 20:26:12 +00001493 dev_err(&pdev->dev, "Failed to register '%s'\n",
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001494 cra->base.cra_name);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001495 else
1496 ixp4xx_algos[i].registered = 1;
1497 }
Herbert Xud7295a82015-07-30 17:53:18 +08001498
1499 for (i = 0; i < ARRAY_SIZE(ixp4xx_aeads); i++) {
1500 struct aead_alg *cra = &ixp4xx_aeads[i].crypto;
1501
1502 if (snprintf(cra->base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1503 "%s"IXP_POSTFIX, cra->base.cra_name) >=
1504 CRYPTO_MAX_ALG_NAME)
1505 continue;
1506 if (!support_aes && (ixp4xx_algos[i].cfg_enc & MOD_AES))
1507 continue;
1508
1509 /* authenc */
1510 cra->base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Mikulas Patockab8aa7dc2020-07-09 23:20:41 -07001511 CRYPTO_ALG_ASYNC |
1512 CRYPTO_ALG_ALLOCATES_MEMORY;
Herbert Xudba434a2019-04-11 16:51:11 +08001513 cra->setkey = cra->setkey ?: aead_setkey;
Herbert Xud7295a82015-07-30 17:53:18 +08001514 cra->setauthsize = aead_setauthsize;
1515 cra->encrypt = aead_encrypt;
1516 cra->decrypt = aead_decrypt;
1517 cra->init = init_tfm_aead;
1518 cra->exit = exit_tfm_aead;
1519
1520 cra->base.cra_ctxsize = sizeof(struct ixp_ctx);
1521 cra->base.cra_module = THIS_MODULE;
1522 cra->base.cra_alignmask = 3;
1523 cra->base.cra_priority = 300;
1524
1525 if (crypto_register_aead(cra))
Corentin Labbef5b82be2021-05-05 20:26:12 +00001526 dev_err(&pdev->dev, "Failed to register '%s'\n",
Herbert Xud7295a82015-07-30 17:53:18 +08001527 cra->base.cra_driver_name);
1528 else
1529 ixp4xx_aeads[i].registered = 1;
1530 }
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001531 return 0;
1532}
1533
1534static void __exit ixp_module_exit(void)
1535{
1536 int num = ARRAY_SIZE(ixp4xx_algos);
1537 int i;
1538
Herbert Xud7295a82015-07-30 17:53:18 +08001539 for (i = 0; i < ARRAY_SIZE(ixp4xx_aeads); i++) {
1540 if (ixp4xx_aeads[i].registered)
1541 crypto_unregister_aead(&ixp4xx_aeads[i].crypto);
1542 }
1543
Corentin Labbe39e39cf2021-05-05 20:26:13 +00001544 for (i = 0; i < num; i++) {
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001545 if (ixp4xx_algos[i].registered)
Ard Biesheuvel4aaf3842019-11-09 18:09:40 +01001546 crypto_unregister_skcipher(&ixp4xx_algos[i].crypto);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001547 }
Russell King27c17892013-06-10 19:09:45 +01001548 release_ixp_crypto(&pdev->dev);
Russell Kingd8cbc3f2013-06-10 18:52:52 +01001549 platform_device_unregister(pdev);
Christian Hohnstaedt81bef012008-06-25 14:38:47 +08001550}
1551
1552module_init(ixp_module_init);
1553module_exit(ixp_module_exit);
1554
1555MODULE_LICENSE("GPL");
1556MODULE_AUTHOR("Christian Hohnstaedt <chohnstaedt@innominate.com>");
1557MODULE_DESCRIPTION("IXP4xx hardware crypto");
1558