crypto: ixp4xx - remove brackets from single statement
[linux-2.6-block.git] / drivers / crypto / ixp4xx_crypto.c
CommitLineData
25763b3c 1// SPDX-License-Identifier: GPL-2.0-only
81bef015
CH
2/*
3 * Intel IXP4xx NPE-C crypto driver
4 *
5 * Copyright (C) 2008 Christian Hohnstaedt <chohnstaedt@innominate.com>
81bef015
CH
6 */
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>
5a0e3ad6 16#include <linux/gfp.h>
75258723 17#include <linux/module.h>
81bef015
CH
18
19#include <crypto/ctr.h>
3ca20b65 20#include <crypto/internal/des.h>
81bef015 21#include <crypto/aes.h>
bb9634df 22#include <crypto/hmac.h>
a24d22b2 23#include <crypto/sha1.h>
81bef015 24#include <crypto/algapi.h>
5290b428 25#include <crypto/internal/aead.h>
4aaf3840 26#include <crypto/internal/skcipher.h>
81bef015
CH
27#include <crypto/authenc.h>
28#include <crypto/scatterwalk.h>
29
4af20dc5
LW
30#include <linux/soc/ixp4xx/npe.h>
31#include <linux/soc/ixp4xx/qmgr.h>
81bef015
CH
32
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
81bef015
CH
91#define HMAC_PAD_BLOCKLEN SHA1_BLOCK_SIZE
92
93#define MD5_DIGEST_SIZE 16
94
95struct buffer_desc {
96 u32 phys_next;
ce057297 97#ifdef __ARMEB__
81bef015
CH
98 u16 buf_len;
99 u16 pkt_len;
ce057297
KH
100#else
101 u16 pkt_len;
102 u16 buf_len;
103#endif
ff455ad9 104 dma_addr_t phys_addr;
81bef015
CH
105 u32 __reserved[4];
106 struct buffer_desc *next;
0d44dc59 107 enum dma_data_direction dir;
81bef015
CH
108};
109
110struct crypt_ctl {
ce057297 111#ifdef __ARMEB__
81bef015
CH
112 u8 mode; /* NPE_OP_* operation mode */
113 u8 init_len;
114 u16 reserved;
ce057297
KH
115#else
116 u16 reserved;
117 u8 init_len;
118 u8 mode; /* NPE_OP_* operation mode */
119#endif
81bef015 120 u8 iv[MAX_IVLEN]; /* IV for CBC mode or CTR IV for CTR mode */
ff455ad9
HX
121 dma_addr_t icv_rev_aes; /* icv or rev aes */
122 dma_addr_t src_buf;
123 dma_addr_t dst_buf;
ce057297 124#ifdef __ARMEB__
81bef015
CH
125 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 */
ce057297
KH
129#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
81bef015
CH
135 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*/
3557084e 139 unsigned int ctl_flags;
81bef015 140 union {
4aaf3840 141 struct skcipher_request *ablk_req;
81bef015
CH
142 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;
e8acf011
CL
152 u8 iv[MAX_IVLEN];
153 bool encrypt;
dfb098d6 154 struct skcipher_request fallback_req; // keep at the end
81bef015
CH
155};
156
157struct aead_ctx {
d7295a8d
HX
158 struct buffer_desc *src;
159 struct buffer_desc *dst;
81bef015
CH
160 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];
3557084e 187 unsigned int salted;
81bef015
CH
188 atomic_t configuring;
189 struct completion completion;
dfb098d6 190 struct crypto_skcipher *fallback_tfm;
81bef015
CH
191};
192
193struct ixp_alg {
4aaf3840 194 struct skcipher_alg crypto;
81bef015
CH
195 const struct ix_hash_algo *hash;
196 u32 cfg_enc;
197 u32 cfg_dec;
198
199 int registered;
200};
201
d7295a8d
HX
202struct 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
81bef015
CH
211static 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};
39e39cfb 216
81bef015
CH
217static 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;
87d11a5e
CL
224static struct dma_pool *buffer_pool;
225static struct dma_pool *ctx_pool;
81bef015 226
87d11a5e 227static struct crypt_ctl *crypt_virt;
81bef015
CH
228static dma_addr_t crypt_phys;
229
230static int support_aes = 1;
231
81bef015 232#define DRIVER_NAME "ixp4xx_crypto"
81bef015 233
d8cbc3f7 234static struct platform_device *pdev;
81bef015
CH
235
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{
39e39cfb 248 return container_of(tfm->__crt_alg, struct ixp_alg, crypto.base)->cfg_enc;
81bef015
CH
249}
250
251static inline u32 cipher_cfg_dec(struct crypto_tfm *tfm)
252{
39e39cfb 253 return container_of(tfm->__crt_alg, struct ixp_alg, crypto.base)->cfg_dec;
81bef015
CH
254}
255
256static inline const struct ix_hash_algo *ix_hash(struct crypto_tfm *tfm)
257{
4aaf3840 258 return container_of(tfm->__crt_alg, struct ixp_alg, crypto.base)->hash;
81bef015
CH
259}
260
261static int setup_crypt_desc(void)
262{
27c1789c 263 struct device *dev = &pdev->dev;
39e39cfb 264
81bef015 265 BUILD_BUG_ON(sizeof(struct crypt_ctl) != 64);
750afb08
LC
266 crypt_virt = dma_alloc_coherent(dev,
267 NPE_QLEN * sizeof(struct crypt_ctl),
268 &crypt_phys, GFP_ATOMIC);
81bef015
CH
269 if (!crypt_virt)
270 return -ENOMEM;
81bef015
CH
271 return 0;
272}
273
7dad7d00 274static DEFINE_SPINLOCK(desc_lock);
81bef015
CH
275static struct crypt_ctl *get_crypt_desc(void)
276{
277 int i;
87d11a5e 278 static int idx;
81bef015
CH
279 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);
39e39cfb 295 return crypt_virt + i;
81bef015
CH
296 } else {
297 spin_unlock_irqrestore(&desc_lock, flags);
298 return NULL;
299 }
300}
301
7dad7d00 302static DEFINE_SPINLOCK(emerg_lock);
81bef015
CH
303static 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);
39e39cfb 323 return crypt_virt + i;
81bef015
CH
324 } else {
325 spin_unlock_irqrestore(&emerg_lock, flags);
326 return NULL;
327 }
328}
329
ff455ad9
HX
330static void free_buf_chain(struct device *dev, struct buffer_desc *buf,
331 dma_addr_t phys)
81bef015
CH
332{
333 while (buf) {
334 struct buffer_desc *buf1;
335 u32 phys1;
336
337 buf1 = buf->next;
338 phys1 = buf->phys_next;
9395c58f 339 dma_unmap_single(dev, buf->phys_addr, buf->buf_len, buf->dir);
81bef015
CH
340 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);
d7295a8d 354 int decryptlen = req->assoclen + req->cryptlen - authsize;
81bef015
CH
355
356 if (req_ctx->encrypt) {
357 scatterwalk_map_and_copy(req_ctx->hmac_virt,
d7295a8d 358 req->dst, decryptlen, authsize, 1);
81bef015
CH
359 }
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{
27c1789c 365 struct device *dev = &pdev->dev;
81bef015
CH
366 struct crypt_ctl *crypt;
367 struct ixp_ctx *ctx;
368 int failed;
81bef015
CH
369
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);
81bef015 378
d7295a8d
HX
379 free_buf_chain(dev, req_ctx->src, crypt->src_buf);
380 free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
ffb017e9 381 if (req_ctx->hmac_virt)
81bef015 382 finish_scattered_hmac(crypt);
ffb017e9 383
81bef015
CH
384 req->base.complete(&req->base, failed);
385 break;
386 }
387 case CTL_FLAG_PERFORM_ABLK: {
4aaf3840
AB
388 struct skcipher_request *req = crypt->data.ablk_req;
389 struct ablk_ctx *req_ctx = skcipher_request_ctx(req);
e8acf011
CL
390 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 }
0d44dc59 404
ffb017e9 405 if (req_ctx->dst)
0d44dc59 406 free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
ffb017e9 407
0d44dc59 408 free_buf_chain(dev, req_ctx->src, crypt->src_buf);
81bef015
CH
409 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);
39e39cfb 422 *(u32 *)ctx->decrypt.npe_ctx &= cpu_to_be32(~CIPH_ENCR);
81bef015
CH
423 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
39e39cfb 441 for (i = 0; i < 4; i++) {
81bef015 442 dma_addr_t phys = qmgr_get_entry(RECV_QID);
39e39cfb 443
81bef015
CH
444 if (!phys)
445 return;
446 one_packet(phys);
447 }
448 tasklet_schedule(&crypto_done_tasklet);
449}
450
27c1789c 451static int init_ixp_crypto(struct device *dev)
81bef015
CH
452{
453 int ret = -ENODEV;
295c01f9 454 u32 msg[2] = { 0, 0 };
81bef015
CH
455
456 if (! ( ~(*IXP4XX_EXP_CFG2) & (IXP4XX_FEATURE_HASH |
457 IXP4XX_FEATURE_AES | IXP4XX_FEATURE_DES))) {
f5b82be6 458 dev_err(dev, "ixp_crypto: No HW crypto available\n");
81bef015
CH
459 return ret;
460 }
461 npe_c = npe_request(NPE_ID);
462 if (!npe_c)
463 return ret;
464
465 if (!npe_running(npe_c)) {
295c01f9 466 ret = npe_load_firmware(npe_c, npe_name(npe_c), dev);
b363700e 467 if (ret)
c5736a40 468 goto npe_release;
295c01f9
CH
469 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;
81bef015
CH
477 }
478
39e39cfb 479 switch ((msg[1] >> 16) & 0xff) {
295c01f9 480 case 3:
f5b82be6 481 dev_warn(dev, "Firmware of %s lacks AES support\n", npe_name(npe_c));
295c01f9
CH
482 support_aes = 0;
483 break;
484 case 4:
485 case 5:
486 support_aes = 1;
487 break;
488 default:
f5b82be6 489 dev_err(dev, "Firmware of %s lacks crypto support\n", npe_name(npe_c));
c5736a40
QL
490 ret = -ENODEV;
491 goto npe_release;
295c01f9 492 }
81bef015
CH
493 /* 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;
ffb017e9 500 if (!buffer_pool)
81bef015 501 goto err;
ffb017e9 502
81bef015
CH
503 ctx_pool = dma_pool_create("context", dev,
504 NPE_CTX_LEN, 16, 0);
ffb017e9 505 if (!ctx_pool)
81bef015 506 goto err;
ffb017e9 507
1777f1a9
KH
508 ret = qmgr_request_queue(SEND_QID, NPE_QLEN_TOTAL, 0, 0,
509 "ixp_crypto:out", NULL);
81bef015
CH
510 if (ret)
511 goto err;
1777f1a9
KH
512 ret = qmgr_request_queue(RECV_QID, NPE_QLEN, 0, 0,
513 "ixp_crypto:in", NULL);
81bef015
CH
514 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;
295c01f9
CH
523
524npe_error:
f5b82be6 525 dev_err(dev, "%s not responding\n", npe_name(npe_c));
295c01f9 526 ret = -EIO;
81bef015 527err:
f9d1293b
ME
528 dma_pool_destroy(ctx_pool);
529 dma_pool_destroy(buffer_pool);
c5736a40 530npe_release:
81bef015
CH
531 npe_release(npe_c);
532 return ret;
533}
534
27c1789c 535static void release_ixp_crypto(struct device *dev)
81bef015
CH
536{
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
ffb017e9 548 if (crypt_virt)
81bef015 549 dma_free_coherent(dev,
f7ade9aa 550 NPE_QLEN * sizeof(struct crypt_ctl),
81bef015 551 crypt_virt, crypt_phys);
81bef015
CH
552}
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);
ffb017e9 564 if (!dir->npe_ctx)
81bef015 565 return -ENOMEM;
ffb017e9 566
81bef015
CH
567 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);
ffb017e9 587 if (ret)
81bef015 588 free_sa_dir(&ctx->encrypt);
ffb017e9 589
81bef015
CH
590 return ret;
591}
592
4aaf3840 593static int init_tfm_ablk(struct crypto_skcipher *tfm)
81bef015 594{
dfb098d6
CL
595 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));
4aaf3840 612 return init_tfm(crypto_skcipher_tfm(tfm));
81bef015
CH
613}
614
d7295a8d 615static int init_tfm_aead(struct crypto_aead *tfm)
81bef015 616{
d7295a8d
HX
617 crypto_aead_set_reqsize(tfm, sizeof(struct aead_ctx));
618 return init_tfm(crypto_aead_tfm(tfm));
81bef015
CH
619}
620
621static void exit_tfm(struct crypto_tfm *tfm)
622{
623 struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
39e39cfb 624
81bef015
CH
625 free_sa_dir(&ctx->encrypt);
626 free_sa_dir(&ctx->decrypt);
627}
628
4aaf3840
AB
629static void exit_tfm_ablk(struct crypto_skcipher *tfm)
630{
dfb098d6
CL
631 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);
4aaf3840
AB
635 exit_tfm(crypto_skcipher_tfm(tfm));
636}
637
d7295a8d
HX
638static void exit_tfm_aead(struct crypto_aead *tfm)
639{
640 exit_tfm(crypto_aead_tfm(tfm));
641}
642
81bef015
CH
643static 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;
ff455ad9 651 dma_addr_t pad_phys, buf_phys;
81bef015
CH
652
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);
ffb017e9 671 for (i = 0; i < HMAC_PAD_BLOCKLEN; i++)
81bef015 672 pad[i] ^= xpad;
81bef015
CH
673
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
3557084e
CL
698static int setup_auth(struct crypto_tfm *tfm, int encrypt, unsigned int authsize,
699 const u8 *key, int key_len, unsigned int digest_len)
81bef015
CH
700{
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 */
39e39cfb 714 cfgword = algo->cfgword | (authsize << 6); /* (authsize/4) << 8 */
ce057297
KH
715#ifndef __ARMEB__
716 cfgword ^= 0xAA000000; /* change the "byte swap" flags */
717#endif
39e39cfb 718 *(u32 *)cinfo = cpu_to_be32(cfgword);
81bef015
CH
719 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();
ffb017e9 752 if (!crypt)
81bef015 753 return -EAGAIN;
ffb017e9 754
39e39cfb 755 *(u32 *)dir->npe_ctx |= cpu_to_be32(CIPH_ENCR);
81bef015
CH
756
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);
c4c4db0d 781 int err;
81bef015
CH
782
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) {
9792eb1d
KH
794 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:
9792eb1d 798 return -EINVAL;
81bef015
CH
799 }
800 cipher_cfg |= keylen_cfg;
81bef015 801 } else {
c4c4db0d
EB
802 err = crypto_des_verify_key(tfm, key);
803 if (err)
804 return err;
81bef015
CH
805 }
806 /* write cfg word to cryptinfo */
39e39cfb 807 *(u32 *)cinfo = cpu_to_be32(cipher_cfg);
81bef015
CH
808 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)) {
39e39cfb 814 memset(cinfo + key_len, 0, DES3_EDE_KEY_SIZE - key_len);
81bef015
CH
815 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;
39e39cfb 819 if ((cipher_cfg & MOD_AES) && !encrypt)
81bef015 820 return gen_rev_aes_key(tfm);
39e39cfb 821
81bef015
CH
822 return 0;
823}
824
0d44dc59 825static struct buffer_desc *chainup_buffers(struct device *dev,
3557084e 826 struct scatterlist *sg, unsigned int nbytes,
0d44dc59
CH
827 struct buffer_desc *buf, gfp_t flags,
828 enum dma_data_direction dir)
81bef015 829{
5be4d4c9 830 for (; nbytes > 0; sg = sg_next(sg)) {
3557084e 831 unsigned int len = min(nbytes, sg->length);
81bef015 832 struct buffer_desc *next_buf;
ff455ad9 833 dma_addr_t next_buf_phys;
0d44dc59 834 void *ptr;
81bef015 835
81bef015 836 nbytes -= len;
796b40c6 837 ptr = sg_virt(sg);
81bef015 838 next_buf = dma_pool_alloc(buffer_pool, flags, &next_buf_phys);
0d44dc59
CH
839 if (!next_buf) {
840 buf = NULL;
841 break;
842 }
843 sg_dma_address(sg) = dma_map_single(dev, ptr, len, dir);
81bef015
CH
844 buf->next = next_buf;
845 buf->phys_next = next_buf_phys;
81bef015 846 buf = next_buf;
0d44dc59 847
81bef015
CH
848 buf->phys_addr = sg_dma_address(sg);
849 buf->buf_len = len;
0d44dc59 850 buf->dir = dir;
81bef015 851 }
0d44dc59
CH
852 buf->next = NULL;
853 buf->phys_next = 0;
81bef015
CH
854 return buf;
855}
856
4aaf3840 857static int ablk_setkey(struct crypto_skcipher *tfm, const u8 *key,
81bef015
CH
858 unsigned int key_len)
859{
4aaf3840 860 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
81bef015
CH
861 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);
81bef015
CH
876out:
877 if (!atomic_dec_and_test(&ctx->configuring))
878 wait_for_completion(&ctx->completion);
dfb098d6
CL
879 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);
81bef015
CH
885}
886
4aaf3840 887static int ablk_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
dba434a9
HX
888 unsigned int key_len)
889{
4aaf3840 890 return verify_skcipher_des3_key(tfm, key) ?:
3ca20b65 891 ablk_setkey(tfm, key, key_len);
dba434a9
HX
892}
893
4aaf3840 894static int ablk_rfc3686_setkey(struct crypto_skcipher *tfm, const u8 *key,
81bef015
CH
895 unsigned int key_len)
896{
4aaf3840 897 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
81bef015
CH
898
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
dfb098d6
CL
910static 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
4aaf3840 929static int ablk_perform(struct skcipher_request *req, int encrypt)
81bef015 930{
4aaf3840
AB
931 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
932 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
3557084e 933 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
81bef015
CH
934 struct ix_sa_dir *dir;
935 struct crypt_ctl *crypt;
4aaf3840 936 unsigned int nbytes = req->cryptlen;
81bef015 937 enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
4aaf3840 938 struct ablk_ctx *req_ctx = skcipher_request_ctx(req);
0d44dc59 939 struct buffer_desc src_hook;
27c1789c 940 struct device *dev = &pdev->dev;
e8acf011 941 unsigned int offset;
81bef015
CH
942 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
943 GFP_KERNEL : GFP_ATOMIC;
944
dfb098d6
CL
945 if (sg_nents(req->src) > 1 || sg_nents(req->dst) > 1)
946 return ixp4xx_cipher_fallback(req, encrypt);
947
81bef015
CH
948 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;
e8acf011 954 req_ctx->encrypt = encrypt;
81bef015
CH
955
956 crypt = get_crypt_desc();
957 if (!crypt)
0d44dc59 958 return -ENOMEM;
81bef015
CH
959
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
4aaf3840
AB
968 BUG_ON(ivsize && !req->iv);
969 memcpy(crypt->iv, req->iv, ivsize);
e8acf011
CL
970 if (ivsize > 0 && !encrypt) {
971 offset = req->cryptlen - ivsize;
972 scatterwalk_map_and_copy(req_ctx->iv, req->src, offset, ivsize, 0);
973 }
81bef015 974 if (req->src != req->dst) {
0d44dc59 975 struct buffer_desc dst_hook;
39e39cfb 976
81bef015 977 crypt->mode |= NPE_OP_NOT_IN_PLACE;
81bef015
CH
978 /* This was never tested by Intel
979 * for more than one dst buffer, I think. */
0d44dc59
CH
980 req_ctx->dst = NULL;
981 if (!chainup_buffers(dev, req->dst, nbytes, &dst_hook,
982 flags, DMA_FROM_DEVICE))
81bef015
CH
983 goto free_buf_dest;
984 src_direction = DMA_TO_DEVICE;
0d44dc59
CH
985 req_ctx->dst = dst_hook.next;
986 crypt->dst_buf = dst_hook.phys_next;
81bef015
CH
987 } else {
988 req_ctx->dst = NULL;
81bef015 989 }
0d44dc59
CH
990 req_ctx->src = NULL;
991 if (!chainup_buffers(dev, req->src, nbytes, &src_hook,
992 flags, src_direction))
81bef015
CH
993 goto free_buf_src;
994
0d44dc59
CH
995 req_ctx->src = src_hook.next;
996 crypt->src_buf = src_hook.phys_next;
81bef015
CH
997 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:
0d44dc59 1003 free_buf_chain(dev, req_ctx->src, crypt->src_buf);
81bef015 1004free_buf_dest:
ffb017e9 1005 if (req->src != req->dst)
0d44dc59 1006 free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
ffb017e9 1007
81bef015 1008 crypt->ctl_flags = CTL_FLAG_UNUSED;
0d44dc59 1009 return -ENOMEM;
81bef015
CH
1010}
1011
4aaf3840 1012static int ablk_encrypt(struct skcipher_request *req)
81bef015
CH
1013{
1014 return ablk_perform(req, 1);
1015}
1016
4aaf3840 1017static int ablk_decrypt(struct skcipher_request *req)
81bef015
CH
1018{
1019 return ablk_perform(req, 0);
1020}
1021
4aaf3840 1022static int ablk_rfc3686_crypt(struct skcipher_request *req)
81bef015 1023{
4aaf3840
AB
1024 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1025 struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
81bef015 1026 u8 iv[CTR_RFC3686_BLOCK_SIZE];
4aaf3840 1027 u8 *info = req->iv;
81bef015
CH
1028 int ret;
1029
1030 /* set up counter block */
39e39cfb 1031 memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
81bef015
CH
1032 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
4aaf3840 1038 req->iv = iv;
81bef015 1039 ret = ablk_perform(req, 1);
4aaf3840 1040 req->iv = info;
81bef015
CH
1041 return ret;
1042}
1043
81bef015
CH
1044static 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);
3557084e
CL
1049 unsigned int ivsize = crypto_aead_ivsize(tfm);
1050 unsigned int authsize = crypto_aead_authsize(tfm);
81bef015
CH
1051 struct ix_sa_dir *dir;
1052 struct crypt_ctl *crypt;
0d44dc59
CH
1053 unsigned int cryptlen;
1054 struct buffer_desc *buf, src_hook;
81bef015 1055 struct aead_ctx *req_ctx = aead_request_ctx(req);
27c1789c 1056 struct device *dev = &pdev->dev;
81bef015
CH
1057 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
1058 GFP_KERNEL : GFP_ATOMIC;
d7295a8d
HX
1059 enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
1060 unsigned int lastlen;
81bef015
CH
1061
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 */
39e39cfb 1073 cryptlen = req->cryptlen - authsize;
81bef015
CH
1074 eff_cryptlen -= authsize;
1075 }
1076 crypt = get_crypt_desc();
1077 if (!crypt)
0d44dc59 1078 return -ENOMEM;
81bef015
CH
1079
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;
d7295a8d 1089 crypt->auth_len = req->assoclen + cryptlen;
81bef015
CH
1090 BUG_ON(ivsize && !req->iv);
1091 memcpy(crypt->iv, req->iv, ivsize);
1092
0f987e25
HX
1093 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
d7295a8d
HX
1105 req_ctx->dst = NULL;
1106
81bef015 1107 if (req->src != req->dst) {
d7295a8d
HX
1108 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 }
81bef015
CH
1127 }
1128
d7295a8d 1129 if (unlikely(lastlen < authsize)) {
81bef015
CH
1130 /* 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))
28389575 1135 goto free_buf_dst;
81bef015
CH
1136 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 }
0d44dc59 1144
81bef015
CH
1145 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;
d7295a8d 1149
d7295a8d
HX
1150free_buf_dst:
1151 free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
28389575
HX
1152free_buf_src:
1153 free_buf_chain(dev, req_ctx->src, crypt->src_buf);
81bef015 1154 crypt->ctl_flags = CTL_FLAG_UNUSED;
0d44dc59 1155 return -ENOMEM;
81bef015
CH
1156}
1157
1158static int aead_setup(struct crypto_aead *tfm, unsigned int authsize)
1159{
1160 struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
3557084e 1161 unsigned int digest_len = crypto_aead_maxauthsize(tfm);
81bef015
CH
1162 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);
81bef015
CH
1184out:
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{
6da9c233 1192 int max = crypto_aead_maxauthsize(tfm) >> 2;
81bef015 1193
39e39cfb 1194 if ((authsize >> 2) < 1 || (authsize >> 2) > max || (authsize & 3))
81bef015
CH
1195 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);
56902781 1203 struct crypto_authenc_keys keys;
81bef015 1204
56902781 1205 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
81bef015
CH
1206 goto badkey;
1207
56902781
MK
1208 if (keys.authkeylen > sizeof(ctx->authkey))
1209 goto badkey;
81bef015 1210
56902781 1211 if (keys.enckeylen > sizeof(ctx->enckey))
81bef015
CH
1212 goto badkey;
1213
56902781
MK
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;
81bef015 1218
0e7da29d 1219 memzero_explicit(&keys, sizeof(keys));
81bef015
CH
1220 return aead_setup(tfm, crypto_aead_authsize(tfm));
1221badkey:
0e7da29d 1222 memzero_explicit(&keys, sizeof(keys));
81bef015
CH
1223 return -EINVAL;
1224}
1225
dba434a9
HX
1226static 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);
dba434a9
HX
1230 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
3ca20b65
AB
1241 err = verify_aead_des3_key(tfm, keys.enckey, keys.enckeylen);
1242 if (err)
dba434a9
HX
1243 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:
dba434a9
HX
1253 memzero_explicit(&keys, sizeof(keys));
1254 return err;
1255}
1256
81bef015
CH
1257static int aead_encrypt(struct aead_request *req)
1258{
d7295a8d 1259 return aead_perform(req, 1, req->assoclen, req->cryptlen, req->iv);
81bef015
CH
1260}
1261
1262static int aead_decrypt(struct aead_request *req)
1263{
d7295a8d 1264 return aead_perform(req, 0, req->assoclen, req->cryptlen, req->iv);
81bef015
CH
1265}
1266
1267static struct ixp_alg ixp4xx_algos[] = {
1268{
1269 .crypto = {
4aaf3840
AB
1270 .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,
81bef015
CH
1276 },
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 = {
4aaf3840
AB
1282 .base.cra_name = "ecb(des)",
1283 .base.cra_blocksize = DES_BLOCK_SIZE,
1284 .min_keysize = DES_KEY_SIZE,
1285 .max_keysize = DES_KEY_SIZE,
81bef015
CH
1286 },
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 = {
4aaf3840
AB
1291 .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,
81bef015
CH
1298 },
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 = {
4aaf3840
AB
1303 .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,
81bef015
CH
1309 },
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 = {
4aaf3840
AB
1314 .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,
81bef015
CH
1320 },
1321 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
1322 .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
1323}, {
1324 .crypto = {
4aaf3840
AB
1325 .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,
81bef015
CH
1330 },
1331 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_ECB,
1332 .cfg_dec = CIPH_DECR | MOD_AES | MOD_ECB,
1333}, {
1334 .crypto = {
4aaf3840
AB
1335 .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,
81bef015
CH
1341 },
1342 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
1343 .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
1344}, {
1345 .crypto = {
4aaf3840
AB
1346 .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,
81bef015
CH
1355 },
1356 .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
1357 .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
d7295a8d
HX
1358} };
1359
1360static struct ixp_aead_alg ixp4xx_aeads[] = {
1361{
81bef015 1362 .crypto = {
d7295a8d
HX
1363 .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,
81bef015
CH
1369 },
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 = {
d7295a8d
HX
1375 .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,
dba434a9 1381 .setkey = des3_aead_setkey,
81bef015
CH
1382 },
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 = {
d7295a8d
HX
1388 .base = {
1389 .cra_name = "authenc(hmac(sha1),cbc(des))",
1390 .cra_blocksize = DES_BLOCK_SIZE,
1391 },
81bef015
CH
1392 .ivsize = DES_BLOCK_SIZE,
1393 .maxauthsize = SHA1_DIGEST_SIZE,
81bef015
CH
1394 },
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 = {
d7295a8d
HX
1400 .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,
dba434a9 1406 .setkey = des3_aead_setkey,
81bef015
CH
1407 },
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 = {
d7295a8d
HX
1413 .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,
81bef015
CH
1419 },
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 = {
d7295a8d
HX
1425 .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,
81bef015
CH
1431 },
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"
d8cbc3f7
RK
1438
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
81bef015
CH
1445static int __init ixp_module_init(void)
1446{
1447 int num = ARRAY_SIZE(ixp4xx_algos);
efb753b8 1448 int i, err;
81bef015 1449
d8cbc3f7
RK
1450 pdev = platform_device_register_full(&ixp_dev_info);
1451 if (IS_ERR(pdev))
1452 return PTR_ERR(pdev);
1453
27c1789c 1454 err = init_ixp_crypto(&pdev->dev);
81bef015 1455 if (err) {
d8cbc3f7 1456 platform_device_unregister(pdev);
81bef015
CH
1457 return err;
1458 }
39e39cfb 1459 for (i = 0; i < num; i++) {
4aaf3840 1460 struct skcipher_alg *cra = &ixp4xx_algos[i].crypto;
81bef015 1461
4aaf3840 1462 if (snprintf(cra->base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
ffb017e9
CL
1463 "%s"IXP_POSTFIX, cra->base.cra_name) >=
1464 CRYPTO_MAX_ALG_NAME)
81bef015 1465 continue;
ffb017e9 1466 if (!support_aes && (ixp4xx_algos[i].cfg_enc & MOD_AES))
81bef015 1467 continue;
d7295a8d
HX
1468
1469 /* block ciphers */
4aaf3840 1470 cra->base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
b8aa7dc5 1471 CRYPTO_ALG_ASYNC |
dfb098d6
CL
1472 CRYPTO_ALG_ALLOCATES_MEMORY |
1473 CRYPTO_ALG_NEED_FALLBACK;
4aaf3840
AB
1474 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;
1482
1483 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))
f5b82be6 1488 dev_err(&pdev->dev, "Failed to register '%s'\n",
4aaf3840 1489 cra->base.cra_name);
81bef015
CH
1490 else
1491 ixp4xx_algos[i].registered = 1;
1492 }
d7295a8d
HX
1493
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 |
b8aa7dc5
MP
1506 CRYPTO_ALG_ASYNC |
1507 CRYPTO_ALG_ALLOCATES_MEMORY;
dba434a9 1508 cra->setkey = cra->setkey ?: aead_setkey;
d7295a8d
HX
1509 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))
f5b82be6 1521 dev_err(&pdev->dev, "Failed to register '%s'\n",
d7295a8d
HX
1522 cra->base.cra_driver_name);
1523 else
1524 ixp4xx_aeads[i].registered = 1;
1525 }
81bef015
CH
1526 return 0;
1527}
1528
1529static void __exit ixp_module_exit(void)
1530{
1531 int num = ARRAY_SIZE(ixp4xx_algos);
1532 int i;
1533
d7295a8d
HX
1534 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
39e39cfb 1539 for (i = 0; i < num; i++) {
81bef015 1540 if (ixp4xx_algos[i].registered)
4aaf3840 1541 crypto_unregister_skcipher(&ixp4xx_algos[i].crypto);
81bef015 1542 }
27c1789c 1543 release_ixp_crypto(&pdev->dev);
d8cbc3f7 1544 platform_device_unregister(pdev);
81bef015
CH
1545}
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