Revert "unicode: Don't special case ignorable code points"
[linux.git] / drivers / crypto / bcm / cipher.c
blob9e6798efbfb769676b9026e168e246f6d5fb5071
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2016 Broadcom
4 */
6 #include <linux/err.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/interrupt.h>
12 #include <linux/platform_device.h>
13 #include <linux/scatterlist.h>
14 #include <linux/crypto.h>
15 #include <linux/kthread.h>
16 #include <linux/rtnetlink.h>
17 #include <linux/sched.h>
18 #include <linux/of.h>
19 #include <linux/io.h>
20 #include <linux/bitops.h>
22 #include <crypto/algapi.h>
23 #include <crypto/aead.h>
24 #include <crypto/internal/aead.h>
25 #include <crypto/aes.h>
26 #include <crypto/internal/des.h>
27 #include <crypto/hmac.h>
28 #include <crypto/md5.h>
29 #include <crypto/authenc.h>
30 #include <crypto/skcipher.h>
31 #include <crypto/hash.h>
32 #include <crypto/sha1.h>
33 #include <crypto/sha2.h>
34 #include <crypto/sha3.h>
36 #include "util.h"
37 #include "cipher.h"
38 #include "spu.h"
39 #include "spum.h"
40 #include "spu2.h"
42 /* ================= Device Structure ================== */
44 struct bcm_device_private iproc_priv;
46 /* ==================== Parameters ===================== */
48 int flow_debug_logging;
49 module_param(flow_debug_logging, int, 0644);
50 MODULE_PARM_DESC(flow_debug_logging, "Enable Flow Debug Logging");
52 int packet_debug_logging;
53 module_param(packet_debug_logging, int, 0644);
54 MODULE_PARM_DESC(packet_debug_logging, "Enable Packet Debug Logging");
56 int debug_logging_sleep;
57 module_param(debug_logging_sleep, int, 0644);
58 MODULE_PARM_DESC(debug_logging_sleep, "Packet Debug Logging Sleep");
61 * The value of these module parameters is used to set the priority for each
62 * algo type when this driver registers algos with the kernel crypto API.
63 * To use a priority other than the default, set the priority in the insmod or
64 * modprobe. Changing the module priority after init time has no effect.
66 * The default priorities are chosen to be lower (less preferred) than ARMv8 CE
67 * algos, but more preferred than generic software algos.
69 static int cipher_pri = 150;
70 module_param(cipher_pri, int, 0644);
71 MODULE_PARM_DESC(cipher_pri, "Priority for cipher algos");
73 static int hash_pri = 100;
74 module_param(hash_pri, int, 0644);
75 MODULE_PARM_DESC(hash_pri, "Priority for hash algos");
77 static int aead_pri = 150;
78 module_param(aead_pri, int, 0644);
79 MODULE_PARM_DESC(aead_pri, "Priority for AEAD algos");
81 /* A type 3 BCM header, expected to precede the SPU header for SPU-M.
82 * Bits 3 and 4 in the first byte encode the channel number (the dma ringset).
83 * 0x60 - ring 0
84 * 0x68 - ring 1
85 * 0x70 - ring 2
86 * 0x78 - ring 3
88 static char BCMHEADER[] = { 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28 };
90 * Some SPU hw does not use BCM header on SPU messages. So BCM_HDR_LEN
91 * is set dynamically after reading SPU type from device tree.
93 #define BCM_HDR_LEN iproc_priv.bcm_hdr_len
95 /* min and max time to sleep before retrying when mbox queue is full. usec */
96 #define MBOX_SLEEP_MIN 800
97 #define MBOX_SLEEP_MAX 1000
99 /**
100 * select_channel() - Select a SPU channel to handle a crypto request. Selects
101 * channel in round robin order.
103 * Return: channel index
105 static u8 select_channel(void)
107 u8 chan_idx = atomic_inc_return(&iproc_priv.next_chan);
109 return chan_idx % iproc_priv.spu.num_chan;
113 * spu_skcipher_rx_sg_create() - Build up the scatterlist of buffers used to
114 * receive a SPU response message for an skcipher request. Includes buffers to
115 * catch SPU message headers and the response data.
116 * @mssg: mailbox message containing the receive sg
117 * @rctx: crypto request context
118 * @rx_frag_num: number of scatterlist elements required to hold the
119 * SPU response message
120 * @chunksize: Number of bytes of response data expected
121 * @stat_pad_len: Number of bytes required to pad the STAT field to
122 * a 4-byte boundary
124 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
125 * when the request completes, whether the request is handled successfully or
126 * there is an error.
128 * Returns:
129 * 0 if successful
130 * < 0 if an error
132 static int
133 spu_skcipher_rx_sg_create(struct brcm_message *mssg,
134 struct iproc_reqctx_s *rctx,
135 u8 rx_frag_num,
136 unsigned int chunksize, u32 stat_pad_len)
138 struct spu_hw *spu = &iproc_priv.spu;
139 struct scatterlist *sg; /* used to build sgs in mbox message */
140 struct iproc_ctx_s *ctx = rctx->ctx;
141 u32 datalen; /* Number of bytes of response data expected */
143 mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
144 rctx->gfp);
145 if (!mssg->spu.dst)
146 return -ENOMEM;
148 sg = mssg->spu.dst;
149 sg_init_table(sg, rx_frag_num);
150 /* Space for SPU message header */
151 sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
153 /* If XTS tweak in payload, add buffer to receive encrypted tweak */
154 if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
155 spu->spu_xts_tweak_in_payload())
156 sg_set_buf(sg++, rctx->msg_buf.c.supdt_tweak,
157 SPU_XTS_TWEAK_SIZE);
159 /* Copy in each dst sg entry from request, up to chunksize */
160 datalen = spu_msg_sg_add(&sg, &rctx->dst_sg, &rctx->dst_skip,
161 rctx->dst_nents, chunksize);
162 if (datalen < chunksize) {
163 pr_err("%s(): failed to copy dst sg to mbox msg. chunksize %u, datalen %u",
164 __func__, chunksize, datalen);
165 return -EFAULT;
168 if (stat_pad_len)
169 sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
171 memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
172 sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
174 return 0;
178 * spu_skcipher_tx_sg_create() - Build up the scatterlist of buffers used to
179 * send a SPU request message for an skcipher request. Includes SPU message
180 * headers and the request data.
181 * @mssg: mailbox message containing the transmit sg
182 * @rctx: crypto request context
183 * @tx_frag_num: number of scatterlist elements required to construct the
184 * SPU request message
185 * @chunksize: Number of bytes of request data
186 * @pad_len: Number of pad bytes
188 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
189 * when the request completes, whether the request is handled successfully or
190 * there is an error.
192 * Returns:
193 * 0 if successful
194 * < 0 if an error
196 static int
197 spu_skcipher_tx_sg_create(struct brcm_message *mssg,
198 struct iproc_reqctx_s *rctx,
199 u8 tx_frag_num, unsigned int chunksize, u32 pad_len)
201 struct spu_hw *spu = &iproc_priv.spu;
202 struct scatterlist *sg; /* used to build sgs in mbox message */
203 struct iproc_ctx_s *ctx = rctx->ctx;
204 u32 datalen; /* Number of bytes of response data expected */
205 u32 stat_len;
207 mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
208 rctx->gfp);
209 if (unlikely(!mssg->spu.src))
210 return -ENOMEM;
212 sg = mssg->spu.src;
213 sg_init_table(sg, tx_frag_num);
215 sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
216 BCM_HDR_LEN + ctx->spu_req_hdr_len);
218 /* if XTS tweak in payload, copy from IV (where crypto API puts it) */
219 if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
220 spu->spu_xts_tweak_in_payload())
221 sg_set_buf(sg++, rctx->msg_buf.iv_ctr, SPU_XTS_TWEAK_SIZE);
223 /* Copy in each src sg entry from request, up to chunksize */
224 datalen = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
225 rctx->src_nents, chunksize);
226 if (unlikely(datalen < chunksize)) {
227 pr_err("%s(): failed to copy src sg to mbox msg",
228 __func__);
229 return -EFAULT;
232 if (pad_len)
233 sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
235 stat_len = spu->spu_tx_status_len();
236 if (stat_len) {
237 memset(rctx->msg_buf.tx_stat, 0, stat_len);
238 sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
240 return 0;
243 static int mailbox_send_message(struct brcm_message *mssg, u32 flags,
244 u8 chan_idx)
246 int err;
247 int retry_cnt = 0;
248 struct device *dev = &(iproc_priv.pdev->dev);
250 err = mbox_send_message(iproc_priv.mbox[chan_idx], mssg);
251 if (flags & CRYPTO_TFM_REQ_MAY_SLEEP) {
252 while ((err == -ENOBUFS) && (retry_cnt < SPU_MB_RETRY_MAX)) {
254 * Mailbox queue is full. Since MAY_SLEEP is set, assume
255 * not in atomic context and we can wait and try again.
257 retry_cnt++;
258 usleep_range(MBOX_SLEEP_MIN, MBOX_SLEEP_MAX);
259 err = mbox_send_message(iproc_priv.mbox[chan_idx],
260 mssg);
261 atomic_inc(&iproc_priv.mb_no_spc);
264 if (err < 0) {
265 atomic_inc(&iproc_priv.mb_send_fail);
266 return err;
269 /* Check error returned by mailbox controller */
270 err = mssg->error;
271 if (unlikely(err < 0)) {
272 dev_err(dev, "message error %d", err);
273 /* Signal txdone for mailbox channel */
276 /* Signal txdone for mailbox channel */
277 mbox_client_txdone(iproc_priv.mbox[chan_idx], err);
278 return err;
282 * handle_skcipher_req() - Submit as much of a block cipher request as fits in
283 * a single SPU request message, starting at the current position in the request
284 * data.
285 * @rctx: Crypto request context
287 * This may be called on the crypto API thread, or, when a request is so large
288 * it must be broken into multiple SPU messages, on the thread used to invoke
289 * the response callback. When requests are broken into multiple SPU
290 * messages, we assume subsequent messages depend on previous results, and
291 * thus always wait for previous results before submitting the next message.
292 * Because requests are submitted in lock step like this, there is no need
293 * to synchronize access to request data structures.
295 * Return: -EINPROGRESS: request has been accepted and result will be returned
296 * asynchronously
297 * Any other value indicates an error
299 static int handle_skcipher_req(struct iproc_reqctx_s *rctx)
301 struct spu_hw *spu = &iproc_priv.spu;
302 struct crypto_async_request *areq = rctx->parent;
303 struct skcipher_request *req =
304 container_of(areq, struct skcipher_request, base);
305 struct iproc_ctx_s *ctx = rctx->ctx;
306 struct spu_cipher_parms cipher_parms;
307 int err;
308 unsigned int chunksize; /* Num bytes of request to submit */
309 int remaining; /* Bytes of request still to process */
310 int chunk_start; /* Beginning of data for current SPU msg */
312 /* IV or ctr value to use in this SPU msg */
313 u8 local_iv_ctr[MAX_IV_SIZE];
314 u32 stat_pad_len; /* num bytes to align status field */
315 u32 pad_len; /* total length of all padding */
316 struct brcm_message *mssg; /* mailbox message */
318 /* number of entries in src and dst sg in mailbox message. */
319 u8 rx_frag_num = 2; /* response header and STATUS */
320 u8 tx_frag_num = 1; /* request header */
322 flow_log("%s\n", __func__);
324 cipher_parms.alg = ctx->cipher.alg;
325 cipher_parms.mode = ctx->cipher.mode;
326 cipher_parms.type = ctx->cipher_type;
327 cipher_parms.key_len = ctx->enckeylen;
328 cipher_parms.key_buf = ctx->enckey;
329 cipher_parms.iv_buf = local_iv_ctr;
330 cipher_parms.iv_len = rctx->iv_ctr_len;
332 mssg = &rctx->mb_mssg;
333 chunk_start = rctx->src_sent;
334 remaining = rctx->total_todo - chunk_start;
336 /* determine the chunk we are breaking off and update the indexes */
337 if ((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
338 (remaining > ctx->max_payload))
339 chunksize = ctx->max_payload;
340 else
341 chunksize = remaining;
343 rctx->src_sent += chunksize;
344 rctx->total_sent = rctx->src_sent;
346 /* Count number of sg entries to be included in this request */
347 rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip, chunksize);
348 rctx->dst_nents = spu_sg_count(rctx->dst_sg, rctx->dst_skip, chunksize);
350 if ((ctx->cipher.mode == CIPHER_MODE_CBC) &&
351 rctx->is_encrypt && chunk_start)
353 * Encrypting non-first first chunk. Copy last block of
354 * previous result to IV for this chunk.
356 sg_copy_part_to_buf(req->dst, rctx->msg_buf.iv_ctr,
357 rctx->iv_ctr_len,
358 chunk_start - rctx->iv_ctr_len);
360 if (rctx->iv_ctr_len) {
361 /* get our local copy of the iv */
362 __builtin_memcpy(local_iv_ctr, rctx->msg_buf.iv_ctr,
363 rctx->iv_ctr_len);
365 /* generate the next IV if possible */
366 if ((ctx->cipher.mode == CIPHER_MODE_CBC) &&
367 !rctx->is_encrypt) {
369 * CBC Decrypt: next IV is the last ciphertext block in
370 * this chunk
372 sg_copy_part_to_buf(req->src, rctx->msg_buf.iv_ctr,
373 rctx->iv_ctr_len,
374 rctx->src_sent - rctx->iv_ctr_len);
375 } else if (ctx->cipher.mode == CIPHER_MODE_CTR) {
377 * The SPU hardware increments the counter once for
378 * each AES block of 16 bytes. So update the counter
379 * for the next chunk, if there is one. Note that for
380 * this chunk, the counter has already been copied to
381 * local_iv_ctr. We can assume a block size of 16,
382 * because we only support CTR mode for AES, not for
383 * any other cipher alg.
385 add_to_ctr(rctx->msg_buf.iv_ctr, chunksize >> 4);
389 if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
390 flow_log("max_payload infinite\n");
391 else
392 flow_log("max_payload %u\n", ctx->max_payload);
394 flow_log("sent:%u start:%u remains:%u size:%u\n",
395 rctx->src_sent, chunk_start, remaining, chunksize);
397 /* Copy SPU header template created at setkey time */
398 memcpy(rctx->msg_buf.bcm_spu_req_hdr, ctx->bcm_spu_req_hdr,
399 sizeof(rctx->msg_buf.bcm_spu_req_hdr));
401 spu->spu_cipher_req_finish(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
402 ctx->spu_req_hdr_len, !(rctx->is_encrypt),
403 &cipher_parms, chunksize);
405 atomic64_add(chunksize, &iproc_priv.bytes_out);
407 stat_pad_len = spu->spu_wordalign_padlen(chunksize);
408 if (stat_pad_len)
409 rx_frag_num++;
410 pad_len = stat_pad_len;
411 if (pad_len) {
412 tx_frag_num++;
413 spu->spu_request_pad(rctx->msg_buf.spu_req_pad, 0,
414 0, ctx->auth.alg, ctx->auth.mode,
415 rctx->total_sent, stat_pad_len);
418 spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
419 ctx->spu_req_hdr_len);
420 packet_log("payload:\n");
421 dump_sg(rctx->src_sg, rctx->src_skip, chunksize);
422 packet_dump(" pad: ", rctx->msg_buf.spu_req_pad, pad_len);
425 * Build mailbox message containing SPU request msg and rx buffers
426 * to catch response message
428 memset(mssg, 0, sizeof(*mssg));
429 mssg->type = BRCM_MESSAGE_SPU;
430 mssg->ctx = rctx; /* Will be returned in response */
432 /* Create rx scatterlist to catch result */
433 rx_frag_num += rctx->dst_nents;
435 if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
436 spu->spu_xts_tweak_in_payload())
437 rx_frag_num++; /* extra sg to insert tweak */
439 err = spu_skcipher_rx_sg_create(mssg, rctx, rx_frag_num, chunksize,
440 stat_pad_len);
441 if (err)
442 return err;
444 /* Create tx scatterlist containing SPU request message */
445 tx_frag_num += rctx->src_nents;
446 if (spu->spu_tx_status_len())
447 tx_frag_num++;
449 if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
450 spu->spu_xts_tweak_in_payload())
451 tx_frag_num++; /* extra sg to insert tweak */
453 err = spu_skcipher_tx_sg_create(mssg, rctx, tx_frag_num, chunksize,
454 pad_len);
455 if (err)
456 return err;
458 err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
459 if (unlikely(err < 0))
460 return err;
462 return -EINPROGRESS;
466 * handle_skcipher_resp() - Process a block cipher SPU response. Updates the
467 * total received count for the request and updates global stats.
468 * @rctx: Crypto request context
470 static void handle_skcipher_resp(struct iproc_reqctx_s *rctx)
472 struct spu_hw *spu = &iproc_priv.spu;
473 struct crypto_async_request *areq = rctx->parent;
474 struct skcipher_request *req = skcipher_request_cast(areq);
475 struct iproc_ctx_s *ctx = rctx->ctx;
476 u32 payload_len;
478 /* See how much data was returned */
479 payload_len = spu->spu_payload_length(rctx->msg_buf.spu_resp_hdr);
482 * In XTS mode, the first SPU_XTS_TWEAK_SIZE bytes may be the
483 * encrypted tweak ("i") value; we don't count those.
485 if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
486 spu->spu_xts_tweak_in_payload() &&
487 (payload_len >= SPU_XTS_TWEAK_SIZE))
488 payload_len -= SPU_XTS_TWEAK_SIZE;
490 atomic64_add(payload_len, &iproc_priv.bytes_in);
492 flow_log("%s() offset: %u, bd_len: %u BD:\n",
493 __func__, rctx->total_received, payload_len);
495 dump_sg(req->dst, rctx->total_received, payload_len);
497 rctx->total_received += payload_len;
498 if (rctx->total_received == rctx->total_todo) {
499 atomic_inc(&iproc_priv.op_counts[SPU_OP_CIPHER]);
500 atomic_inc(
501 &iproc_priv.cipher_cnt[ctx->cipher.alg][ctx->cipher.mode]);
506 * spu_ahash_rx_sg_create() - Build up the scatterlist of buffers used to
507 * receive a SPU response message for an ahash request.
508 * @mssg: mailbox message containing the receive sg
509 * @rctx: crypto request context
510 * @rx_frag_num: number of scatterlist elements required to hold the
511 * SPU response message
512 * @digestsize: length of hash digest, in bytes
513 * @stat_pad_len: Number of bytes required to pad the STAT field to
514 * a 4-byte boundary
516 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
517 * when the request completes, whether the request is handled successfully or
518 * there is an error.
520 * Return:
521 * 0 if successful
522 * < 0 if an error
524 static int
525 spu_ahash_rx_sg_create(struct brcm_message *mssg,
526 struct iproc_reqctx_s *rctx,
527 u8 rx_frag_num, unsigned int digestsize,
528 u32 stat_pad_len)
530 struct spu_hw *spu = &iproc_priv.spu;
531 struct scatterlist *sg; /* used to build sgs in mbox message */
532 struct iproc_ctx_s *ctx = rctx->ctx;
534 mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
535 rctx->gfp);
536 if (!mssg->spu.dst)
537 return -ENOMEM;
539 sg = mssg->spu.dst;
540 sg_init_table(sg, rx_frag_num);
541 /* Space for SPU message header */
542 sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
544 /* Space for digest */
545 sg_set_buf(sg++, rctx->msg_buf.digest, digestsize);
547 if (stat_pad_len)
548 sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
550 memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
551 sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
552 return 0;
556 * spu_ahash_tx_sg_create() - Build up the scatterlist of buffers used to send
557 * a SPU request message for an ahash request. Includes SPU message headers and
558 * the request data.
559 * @mssg: mailbox message containing the transmit sg
560 * @rctx: crypto request context
561 * @tx_frag_num: number of scatterlist elements required to construct the
562 * SPU request message
563 * @spu_hdr_len: length in bytes of SPU message header
564 * @hash_carry_len: Number of bytes of data carried over from previous req
565 * @new_data_len: Number of bytes of new request data
566 * @pad_len: Number of pad bytes
568 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
569 * when the request completes, whether the request is handled successfully or
570 * there is an error.
572 * Return:
573 * 0 if successful
574 * < 0 if an error
576 static int
577 spu_ahash_tx_sg_create(struct brcm_message *mssg,
578 struct iproc_reqctx_s *rctx,
579 u8 tx_frag_num,
580 u32 spu_hdr_len,
581 unsigned int hash_carry_len,
582 unsigned int new_data_len, u32 pad_len)
584 struct spu_hw *spu = &iproc_priv.spu;
585 struct scatterlist *sg; /* used to build sgs in mbox message */
586 u32 datalen; /* Number of bytes of response data expected */
587 u32 stat_len;
589 mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
590 rctx->gfp);
591 if (!mssg->spu.src)
592 return -ENOMEM;
594 sg = mssg->spu.src;
595 sg_init_table(sg, tx_frag_num);
597 sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
598 BCM_HDR_LEN + spu_hdr_len);
600 if (hash_carry_len)
601 sg_set_buf(sg++, rctx->hash_carry, hash_carry_len);
603 if (new_data_len) {
604 /* Copy in each src sg entry from request, up to chunksize */
605 datalen = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
606 rctx->src_nents, new_data_len);
607 if (datalen < new_data_len) {
608 pr_err("%s(): failed to copy src sg to mbox msg",
609 __func__);
610 return -EFAULT;
614 if (pad_len)
615 sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
617 stat_len = spu->spu_tx_status_len();
618 if (stat_len) {
619 memset(rctx->msg_buf.tx_stat, 0, stat_len);
620 sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
623 return 0;
627 * handle_ahash_req() - Process an asynchronous hash request from the crypto
628 * API.
629 * @rctx: Crypto request context
631 * Builds a SPU request message embedded in a mailbox message and submits the
632 * mailbox message on a selected mailbox channel. The SPU request message is
633 * constructed as a scatterlist, including entries from the crypto API's
634 * src scatterlist to avoid copying the data to be hashed. This function is
635 * called either on the thread from the crypto API, or, in the case that the
636 * crypto API request is too large to fit in a single SPU request message,
637 * on the thread that invokes the receive callback with a response message.
638 * Because some operations require the response from one chunk before the next
639 * chunk can be submitted, we always wait for the response for the previous
640 * chunk before submitting the next chunk. Because requests are submitted in
641 * lock step like this, there is no need to synchronize access to request data
642 * structures.
644 * Return:
645 * -EINPROGRESS: request has been submitted to SPU and response will be
646 * returned asynchronously
647 * -EAGAIN: non-final request included a small amount of data, which for
648 * efficiency we did not submit to the SPU, but instead stored
649 * to be submitted to the SPU with the next part of the request
650 * other: an error code
652 static int handle_ahash_req(struct iproc_reqctx_s *rctx)
654 struct spu_hw *spu = &iproc_priv.spu;
655 struct crypto_async_request *areq = rctx->parent;
656 struct ahash_request *req = ahash_request_cast(areq);
657 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
658 struct crypto_tfm *tfm = crypto_ahash_tfm(ahash);
659 unsigned int blocksize = crypto_tfm_alg_blocksize(tfm);
660 struct iproc_ctx_s *ctx = rctx->ctx;
662 /* number of bytes still to be hashed in this req */
663 unsigned int nbytes_to_hash = 0;
664 int err;
665 unsigned int chunksize = 0; /* length of hash carry + new data */
667 * length of new data, not from hash carry, to be submitted in
668 * this hw request
670 unsigned int new_data_len;
672 unsigned int __maybe_unused chunk_start = 0;
673 u32 db_size; /* Length of data field, incl gcm and hash padding */
674 int pad_len = 0; /* total pad len, including gcm, hash, stat padding */
675 u32 data_pad_len = 0; /* length of GCM/CCM padding */
676 u32 stat_pad_len = 0; /* length of padding to align STATUS word */
677 struct brcm_message *mssg; /* mailbox message */
678 struct spu_request_opts req_opts;
679 struct spu_cipher_parms cipher_parms;
680 struct spu_hash_parms hash_parms;
681 struct spu_aead_parms aead_parms;
682 unsigned int local_nbuf;
683 u32 spu_hdr_len;
684 unsigned int digestsize;
685 u16 rem = 0;
688 * number of entries in src and dst sg. Always includes SPU msg header.
689 * rx always includes a buffer to catch digest and STATUS.
691 u8 rx_frag_num = 3;
692 u8 tx_frag_num = 1;
694 flow_log("total_todo %u, total_sent %u\n",
695 rctx->total_todo, rctx->total_sent);
697 memset(&req_opts, 0, sizeof(req_opts));
698 memset(&cipher_parms, 0, sizeof(cipher_parms));
699 memset(&hash_parms, 0, sizeof(hash_parms));
700 memset(&aead_parms, 0, sizeof(aead_parms));
702 req_opts.bd_suppress = true;
703 hash_parms.alg = ctx->auth.alg;
704 hash_parms.mode = ctx->auth.mode;
705 hash_parms.type = HASH_TYPE_NONE;
706 hash_parms.key_buf = (u8 *)ctx->authkey;
707 hash_parms.key_len = ctx->authkeylen;
710 * For hash algorithms below assignment looks bit odd but
711 * it's needed for AES-XCBC and AES-CMAC hash algorithms
712 * to differentiate between 128, 192, 256 bit key values.
713 * Based on the key values, hash algorithm is selected.
714 * For example for 128 bit key, hash algorithm is AES-128.
716 cipher_parms.type = ctx->cipher_type;
718 mssg = &rctx->mb_mssg;
719 chunk_start = rctx->src_sent;
722 * Compute the amount remaining to hash. This may include data
723 * carried over from previous requests.
725 nbytes_to_hash = rctx->total_todo - rctx->total_sent;
726 chunksize = nbytes_to_hash;
727 if ((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
728 (chunksize > ctx->max_payload))
729 chunksize = ctx->max_payload;
732 * If this is not a final request and the request data is not a multiple
733 * of a full block, then simply park the extra data and prefix it to the
734 * data for the next request.
736 if (!rctx->is_final) {
737 u8 *dest = rctx->hash_carry + rctx->hash_carry_len;
738 u16 new_len; /* len of data to add to hash carry */
740 rem = chunksize % blocksize; /* remainder */
741 if (rem) {
742 /* chunksize not a multiple of blocksize */
743 chunksize -= rem;
744 if (chunksize == 0) {
745 /* Don't have a full block to submit to hw */
746 new_len = rem - rctx->hash_carry_len;
747 sg_copy_part_to_buf(req->src, dest, new_len,
748 rctx->src_sent);
749 rctx->hash_carry_len = rem;
750 flow_log("Exiting with hash carry len: %u\n",
751 rctx->hash_carry_len);
752 packet_dump(" buf: ",
753 rctx->hash_carry,
754 rctx->hash_carry_len);
755 return -EAGAIN;
760 /* if we have hash carry, then prefix it to the data in this request */
761 local_nbuf = rctx->hash_carry_len;
762 rctx->hash_carry_len = 0;
763 if (local_nbuf)
764 tx_frag_num++;
765 new_data_len = chunksize - local_nbuf;
767 /* Count number of sg entries to be used in this request */
768 rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip,
769 new_data_len);
771 /* AES hashing keeps key size in type field, so need to copy it here */
772 if (hash_parms.alg == HASH_ALG_AES)
773 hash_parms.type = (enum hash_type)cipher_parms.type;
774 else
775 hash_parms.type = spu->spu_hash_type(rctx->total_sent);
777 digestsize = spu->spu_digest_size(ctx->digestsize, ctx->auth.alg,
778 hash_parms.type);
779 hash_parms.digestsize = digestsize;
781 /* update the indexes */
782 rctx->total_sent += chunksize;
783 /* if you sent a prebuf then that wasn't from this req->src */
784 rctx->src_sent += new_data_len;
786 if ((rctx->total_sent == rctx->total_todo) && rctx->is_final)
787 hash_parms.pad_len = spu->spu_hash_pad_len(hash_parms.alg,
788 hash_parms.mode,
789 chunksize,
790 blocksize);
793 * If a non-first chunk, then include the digest returned from the
794 * previous chunk so that hw can add to it (except for AES types).
796 if ((hash_parms.type == HASH_TYPE_UPDT) &&
797 (hash_parms.alg != HASH_ALG_AES)) {
798 hash_parms.key_buf = rctx->incr_hash;
799 hash_parms.key_len = digestsize;
802 atomic64_add(chunksize, &iproc_priv.bytes_out);
804 flow_log("%s() final: %u nbuf: %u ",
805 __func__, rctx->is_final, local_nbuf);
807 if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
808 flow_log("max_payload infinite\n");
809 else
810 flow_log("max_payload %u\n", ctx->max_payload);
812 flow_log("chunk_start: %u chunk_size: %u\n", chunk_start, chunksize);
814 /* Prepend SPU header with type 3 BCM header */
815 memcpy(rctx->msg_buf.bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
817 hash_parms.prebuf_len = local_nbuf;
818 spu_hdr_len = spu->spu_create_request(rctx->msg_buf.bcm_spu_req_hdr +
819 BCM_HDR_LEN,
820 &req_opts, &cipher_parms,
821 &hash_parms, &aead_parms,
822 new_data_len);
824 if (spu_hdr_len == 0) {
825 pr_err("Failed to create SPU request header\n");
826 return -EFAULT;
830 * Determine total length of padding required. Put all padding in one
831 * buffer.
833 data_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode, chunksize);
834 db_size = spu_real_db_size(0, 0, local_nbuf, new_data_len,
835 0, 0, hash_parms.pad_len);
836 if (spu->spu_tx_status_len())
837 stat_pad_len = spu->spu_wordalign_padlen(db_size);
838 if (stat_pad_len)
839 rx_frag_num++;
840 pad_len = hash_parms.pad_len + data_pad_len + stat_pad_len;
841 if (pad_len) {
842 tx_frag_num++;
843 spu->spu_request_pad(rctx->msg_buf.spu_req_pad, data_pad_len,
844 hash_parms.pad_len, ctx->auth.alg,
845 ctx->auth.mode, rctx->total_sent,
846 stat_pad_len);
849 spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
850 spu_hdr_len);
851 packet_dump(" prebuf: ", rctx->hash_carry, local_nbuf);
852 flow_log("Data:\n");
853 dump_sg(rctx->src_sg, rctx->src_skip, new_data_len);
854 packet_dump(" pad: ", rctx->msg_buf.spu_req_pad, pad_len);
857 * Build mailbox message containing SPU request msg and rx buffers
858 * to catch response message
860 memset(mssg, 0, sizeof(*mssg));
861 mssg->type = BRCM_MESSAGE_SPU;
862 mssg->ctx = rctx; /* Will be returned in response */
864 /* Create rx scatterlist to catch result */
865 err = spu_ahash_rx_sg_create(mssg, rctx, rx_frag_num, digestsize,
866 stat_pad_len);
867 if (err)
868 return err;
870 /* Create tx scatterlist containing SPU request message */
871 tx_frag_num += rctx->src_nents;
872 if (spu->spu_tx_status_len())
873 tx_frag_num++;
874 err = spu_ahash_tx_sg_create(mssg, rctx, tx_frag_num, spu_hdr_len,
875 local_nbuf, new_data_len, pad_len);
876 if (err)
877 return err;
879 err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
880 if (unlikely(err < 0))
881 return err;
883 return -EINPROGRESS;
887 * spu_hmac_outer_hash() - Request synchonous software compute of the outer hash
888 * for an HMAC request.
889 * @req: The HMAC request from the crypto API
890 * @ctx: The session context
892 * Return: 0 if synchronous hash operation successful
893 * -EINVAL if the hash algo is unrecognized
894 * any other value indicates an error
896 static int spu_hmac_outer_hash(struct ahash_request *req,
897 struct iproc_ctx_s *ctx)
899 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
900 unsigned int blocksize =
901 crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
902 int rc;
904 switch (ctx->auth.alg) {
905 case HASH_ALG_MD5:
906 rc = do_shash("md5", req->result, ctx->opad, blocksize,
907 req->result, ctx->digestsize, NULL, 0);
908 break;
909 case HASH_ALG_SHA1:
910 rc = do_shash("sha1", req->result, ctx->opad, blocksize,
911 req->result, ctx->digestsize, NULL, 0);
912 break;
913 case HASH_ALG_SHA224:
914 rc = do_shash("sha224", req->result, ctx->opad, blocksize,
915 req->result, ctx->digestsize, NULL, 0);
916 break;
917 case HASH_ALG_SHA256:
918 rc = do_shash("sha256", req->result, ctx->opad, blocksize,
919 req->result, ctx->digestsize, NULL, 0);
920 break;
921 case HASH_ALG_SHA384:
922 rc = do_shash("sha384", req->result, ctx->opad, blocksize,
923 req->result, ctx->digestsize, NULL, 0);
924 break;
925 case HASH_ALG_SHA512:
926 rc = do_shash("sha512", req->result, ctx->opad, blocksize,
927 req->result, ctx->digestsize, NULL, 0);
928 break;
929 default:
930 pr_err("%s() Error : unknown hmac type\n", __func__);
931 rc = -EINVAL;
933 return rc;
937 * ahash_req_done() - Process a hash result from the SPU hardware.
938 * @rctx: Crypto request context
940 * Return: 0 if successful
941 * < 0 if an error
943 static int ahash_req_done(struct iproc_reqctx_s *rctx)
945 struct spu_hw *spu = &iproc_priv.spu;
946 struct crypto_async_request *areq = rctx->parent;
947 struct ahash_request *req = ahash_request_cast(areq);
948 struct iproc_ctx_s *ctx = rctx->ctx;
949 int err;
951 memcpy(req->result, rctx->msg_buf.digest, ctx->digestsize);
953 if (spu->spu_type == SPU_TYPE_SPUM) {
954 /* byte swap the output from the UPDT function to network byte
955 * order
957 if (ctx->auth.alg == HASH_ALG_MD5) {
958 __swab32s((u32 *)req->result);
959 __swab32s(((u32 *)req->result) + 1);
960 __swab32s(((u32 *)req->result) + 2);
961 __swab32s(((u32 *)req->result) + 3);
962 __swab32s(((u32 *)req->result) + 4);
966 flow_dump(" digest ", req->result, ctx->digestsize);
968 /* if this an HMAC then do the outer hash */
969 if (rctx->is_sw_hmac) {
970 err = spu_hmac_outer_hash(req, ctx);
971 if (err < 0)
972 return err;
973 flow_dump(" hmac: ", req->result, ctx->digestsize);
976 if (rctx->is_sw_hmac || ctx->auth.mode == HASH_MODE_HMAC) {
977 atomic_inc(&iproc_priv.op_counts[SPU_OP_HMAC]);
978 atomic_inc(&iproc_priv.hmac_cnt[ctx->auth.alg]);
979 } else {
980 atomic_inc(&iproc_priv.op_counts[SPU_OP_HASH]);
981 atomic_inc(&iproc_priv.hash_cnt[ctx->auth.alg]);
984 return 0;
988 * handle_ahash_resp() - Process a SPU response message for a hash request.
989 * Checks if the entire crypto API request has been processed, and if so,
990 * invokes post processing on the result.
991 * @rctx: Crypto request context
993 static void handle_ahash_resp(struct iproc_reqctx_s *rctx)
995 struct iproc_ctx_s *ctx = rctx->ctx;
996 struct crypto_async_request *areq = rctx->parent;
997 struct ahash_request *req = ahash_request_cast(areq);
998 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
999 unsigned int blocksize =
1000 crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
1002 * Save hash to use as input to next op if incremental. Might be copying
1003 * too much, but that's easier than figuring out actual digest size here
1005 memcpy(rctx->incr_hash, rctx->msg_buf.digest, MAX_DIGEST_SIZE);
1007 flow_log("%s() blocksize:%u digestsize:%u\n",
1008 __func__, blocksize, ctx->digestsize);
1010 atomic64_add(ctx->digestsize, &iproc_priv.bytes_in);
1012 if (rctx->is_final && (rctx->total_sent == rctx->total_todo))
1013 ahash_req_done(rctx);
1017 * spu_aead_rx_sg_create() - Build up the scatterlist of buffers used to receive
1018 * a SPU response message for an AEAD request. Includes buffers to catch SPU
1019 * message headers and the response data.
1020 * @mssg: mailbox message containing the receive sg
1021 * @req: Crypto API request
1022 * @rctx: crypto request context
1023 * @rx_frag_num: number of scatterlist elements required to hold the
1024 * SPU response message
1025 * @assoc_len: Length of associated data included in the crypto request
1026 * @ret_iv_len: Length of IV returned in response
1027 * @resp_len: Number of bytes of response data expected to be written to
1028 * dst buffer from crypto API
1029 * @digestsize: Length of hash digest, in bytes
1030 * @stat_pad_len: Number of bytes required to pad the STAT field to
1031 * a 4-byte boundary
1033 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
1034 * when the request completes, whether the request is handled successfully or
1035 * there is an error.
1037 * Returns:
1038 * 0 if successful
1039 * < 0 if an error
1041 static int spu_aead_rx_sg_create(struct brcm_message *mssg,
1042 struct aead_request *req,
1043 struct iproc_reqctx_s *rctx,
1044 u8 rx_frag_num,
1045 unsigned int assoc_len,
1046 u32 ret_iv_len, unsigned int resp_len,
1047 unsigned int digestsize, u32 stat_pad_len)
1049 struct spu_hw *spu = &iproc_priv.spu;
1050 struct scatterlist *sg; /* used to build sgs in mbox message */
1051 struct iproc_ctx_s *ctx = rctx->ctx;
1052 u32 datalen; /* Number of bytes of response data expected */
1053 u32 assoc_buf_len;
1054 u8 data_padlen = 0;
1056 if (ctx->is_rfc4543) {
1057 /* RFC4543: only pad after data, not after AAD */
1058 data_padlen = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1059 assoc_len + resp_len);
1060 assoc_buf_len = assoc_len;
1061 } else {
1062 data_padlen = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1063 resp_len);
1064 assoc_buf_len = spu->spu_assoc_resp_len(ctx->cipher.mode,
1065 assoc_len, ret_iv_len,
1066 rctx->is_encrypt);
1069 if (ctx->cipher.mode == CIPHER_MODE_CCM)
1070 /* ICV (after data) must be in the next 32-bit word for CCM */
1071 data_padlen += spu->spu_wordalign_padlen(assoc_buf_len +
1072 resp_len +
1073 data_padlen);
1075 if (data_padlen)
1076 /* have to catch gcm pad in separate buffer */
1077 rx_frag_num++;
1079 mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
1080 rctx->gfp);
1081 if (!mssg->spu.dst)
1082 return -ENOMEM;
1084 sg = mssg->spu.dst;
1085 sg_init_table(sg, rx_frag_num);
1087 /* Space for SPU message header */
1088 sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
1090 if (assoc_buf_len) {
1092 * Don't write directly to req->dst, because SPU may pad the
1093 * assoc data in the response
1095 memset(rctx->msg_buf.a.resp_aad, 0, assoc_buf_len);
1096 sg_set_buf(sg++, rctx->msg_buf.a.resp_aad, assoc_buf_len);
1099 if (resp_len) {
1101 * Copy in each dst sg entry from request, up to chunksize.
1102 * dst sg catches just the data. digest caught in separate buf.
1104 datalen = spu_msg_sg_add(&sg, &rctx->dst_sg, &rctx->dst_skip,
1105 rctx->dst_nents, resp_len);
1106 if (datalen < (resp_len)) {
1107 pr_err("%s(): failed to copy dst sg to mbox msg. expected len %u, datalen %u",
1108 __func__, resp_len, datalen);
1109 return -EFAULT;
1113 /* If GCM/CCM data is padded, catch padding in separate buffer */
1114 if (data_padlen) {
1115 memset(rctx->msg_buf.a.gcmpad, 0, data_padlen);
1116 sg_set_buf(sg++, rctx->msg_buf.a.gcmpad, data_padlen);
1119 /* Always catch ICV in separate buffer */
1120 sg_set_buf(sg++, rctx->msg_buf.digest, digestsize);
1122 flow_log("stat_pad_len %u\n", stat_pad_len);
1123 if (stat_pad_len) {
1124 memset(rctx->msg_buf.rx_stat_pad, 0, stat_pad_len);
1125 sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
1128 memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
1129 sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
1131 return 0;
1135 * spu_aead_tx_sg_create() - Build up the scatterlist of buffers used to send a
1136 * SPU request message for an AEAD request. Includes SPU message headers and the
1137 * request data.
1138 * @mssg: mailbox message containing the transmit sg
1139 * @rctx: crypto request context
1140 * @tx_frag_num: number of scatterlist elements required to construct the
1141 * SPU request message
1142 * @spu_hdr_len: length of SPU message header in bytes
1143 * @assoc: crypto API associated data scatterlist
1144 * @assoc_len: length of associated data
1145 * @assoc_nents: number of scatterlist entries containing assoc data
1146 * @aead_iv_len: length of AEAD IV, if included
1147 * @chunksize: Number of bytes of request data
1148 * @aad_pad_len: Number of bytes of padding at end of AAD. For GCM/CCM.
1149 * @pad_len: Number of pad bytes
1150 * @incl_icv: If true, write separate ICV buffer after data and
1151 * any padding
1153 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
1154 * when the request completes, whether the request is handled successfully or
1155 * there is an error.
1157 * Return:
1158 * 0 if successful
1159 * < 0 if an error
1161 static int spu_aead_tx_sg_create(struct brcm_message *mssg,
1162 struct iproc_reqctx_s *rctx,
1163 u8 tx_frag_num,
1164 u32 spu_hdr_len,
1165 struct scatterlist *assoc,
1166 unsigned int assoc_len,
1167 int assoc_nents,
1168 unsigned int aead_iv_len,
1169 unsigned int chunksize,
1170 u32 aad_pad_len, u32 pad_len, bool incl_icv)
1172 struct spu_hw *spu = &iproc_priv.spu;
1173 struct scatterlist *sg; /* used to build sgs in mbox message */
1174 struct scatterlist *assoc_sg = assoc;
1175 struct iproc_ctx_s *ctx = rctx->ctx;
1176 u32 datalen; /* Number of bytes of data to write */
1177 u32 written; /* Number of bytes of data written */
1178 u32 assoc_offset = 0;
1179 u32 stat_len;
1181 mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
1182 rctx->gfp);
1183 if (!mssg->spu.src)
1184 return -ENOMEM;
1186 sg = mssg->spu.src;
1187 sg_init_table(sg, tx_frag_num);
1189 sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
1190 BCM_HDR_LEN + spu_hdr_len);
1192 if (assoc_len) {
1193 /* Copy in each associated data sg entry from request */
1194 written = spu_msg_sg_add(&sg, &assoc_sg, &assoc_offset,
1195 assoc_nents, assoc_len);
1196 if (written < assoc_len) {
1197 pr_err("%s(): failed to copy assoc sg to mbox msg",
1198 __func__);
1199 return -EFAULT;
1203 if (aead_iv_len)
1204 sg_set_buf(sg++, rctx->msg_buf.iv_ctr, aead_iv_len);
1206 if (aad_pad_len) {
1207 memset(rctx->msg_buf.a.req_aad_pad, 0, aad_pad_len);
1208 sg_set_buf(sg++, rctx->msg_buf.a.req_aad_pad, aad_pad_len);
1211 datalen = chunksize;
1212 if ((chunksize > ctx->digestsize) && incl_icv)
1213 datalen -= ctx->digestsize;
1214 if (datalen) {
1215 /* For aead, a single msg should consume the entire src sg */
1216 written = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
1217 rctx->src_nents, datalen);
1218 if (written < datalen) {
1219 pr_err("%s(): failed to copy src sg to mbox msg",
1220 __func__);
1221 return -EFAULT;
1225 if (pad_len) {
1226 memset(rctx->msg_buf.spu_req_pad, 0, pad_len);
1227 sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
1230 if (incl_icv)
1231 sg_set_buf(sg++, rctx->msg_buf.digest, ctx->digestsize);
1233 stat_len = spu->spu_tx_status_len();
1234 if (stat_len) {
1235 memset(rctx->msg_buf.tx_stat, 0, stat_len);
1236 sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
1238 return 0;
1242 * handle_aead_req() - Submit a SPU request message for the next chunk of the
1243 * current AEAD request.
1244 * @rctx: Crypto request context
1246 * Unlike other operation types, we assume the length of the request fits in
1247 * a single SPU request message. aead_enqueue() makes sure this is true.
1248 * Comments for other op types regarding threads applies here as well.
1250 * Unlike incremental hash ops, where the spu returns the entire hash for
1251 * truncated algs like sha-224, the SPU returns just the truncated hash in
1252 * response to aead requests. So digestsize is always ctx->digestsize here.
1254 * Return: -EINPROGRESS: crypto request has been accepted and result will be
1255 * returned asynchronously
1256 * Any other value indicates an error
1258 static int handle_aead_req(struct iproc_reqctx_s *rctx)
1260 struct spu_hw *spu = &iproc_priv.spu;
1261 struct crypto_async_request *areq = rctx->parent;
1262 struct aead_request *req = container_of(areq,
1263 struct aead_request, base);
1264 struct iproc_ctx_s *ctx = rctx->ctx;
1265 int err;
1266 unsigned int chunksize;
1267 unsigned int resp_len;
1268 u32 spu_hdr_len;
1269 u32 db_size;
1270 u32 stat_pad_len;
1271 u32 pad_len;
1272 struct brcm_message *mssg; /* mailbox message */
1273 struct spu_request_opts req_opts;
1274 struct spu_cipher_parms cipher_parms;
1275 struct spu_hash_parms hash_parms;
1276 struct spu_aead_parms aead_parms;
1277 int assoc_nents = 0;
1278 bool incl_icv = false;
1279 unsigned int digestsize = ctx->digestsize;
1281 /* number of entries in src and dst sg. Always includes SPU msg header.
1283 u8 rx_frag_num = 2; /* and STATUS */
1284 u8 tx_frag_num = 1;
1286 /* doing the whole thing at once */
1287 chunksize = rctx->total_todo;
1289 flow_log("%s: chunksize %u\n", __func__, chunksize);
1291 memset(&req_opts, 0, sizeof(req_opts));
1292 memset(&hash_parms, 0, sizeof(hash_parms));
1293 memset(&aead_parms, 0, sizeof(aead_parms));
1295 req_opts.is_inbound = !(rctx->is_encrypt);
1296 req_opts.auth_first = ctx->auth_first;
1297 req_opts.is_aead = true;
1298 req_opts.is_esp = ctx->is_esp;
1300 cipher_parms.alg = ctx->cipher.alg;
1301 cipher_parms.mode = ctx->cipher.mode;
1302 cipher_parms.type = ctx->cipher_type;
1303 cipher_parms.key_buf = ctx->enckey;
1304 cipher_parms.key_len = ctx->enckeylen;
1305 cipher_parms.iv_buf = rctx->msg_buf.iv_ctr;
1306 cipher_parms.iv_len = rctx->iv_ctr_len;
1308 hash_parms.alg = ctx->auth.alg;
1309 hash_parms.mode = ctx->auth.mode;
1310 hash_parms.type = HASH_TYPE_NONE;
1311 hash_parms.key_buf = (u8 *)ctx->authkey;
1312 hash_parms.key_len = ctx->authkeylen;
1313 hash_parms.digestsize = digestsize;
1315 if ((ctx->auth.alg == HASH_ALG_SHA224) &&
1316 (ctx->authkeylen < SHA224_DIGEST_SIZE))
1317 hash_parms.key_len = SHA224_DIGEST_SIZE;
1319 aead_parms.assoc_size = req->assoclen;
1320 if (ctx->is_esp && !ctx->is_rfc4543) {
1322 * 8-byte IV is included assoc data in request. SPU2
1323 * expects AAD to include just SPI and seqno. So
1324 * subtract off the IV len.
1326 aead_parms.assoc_size -= GCM_RFC4106_IV_SIZE;
1328 if (rctx->is_encrypt) {
1329 aead_parms.return_iv = true;
1330 aead_parms.ret_iv_len = GCM_RFC4106_IV_SIZE;
1331 aead_parms.ret_iv_off = GCM_ESP_SALT_SIZE;
1333 } else {
1334 aead_parms.ret_iv_len = 0;
1338 * Count number of sg entries from the crypto API request that are to
1339 * be included in this mailbox message. For dst sg, don't count space
1340 * for digest. Digest gets caught in a separate buffer and copied back
1341 * to dst sg when processing response.
1343 rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip, chunksize);
1344 rctx->dst_nents = spu_sg_count(rctx->dst_sg, rctx->dst_skip, chunksize);
1345 if (aead_parms.assoc_size)
1346 assoc_nents = spu_sg_count(rctx->assoc, 0,
1347 aead_parms.assoc_size);
1349 mssg = &rctx->mb_mssg;
1351 rctx->total_sent = chunksize;
1352 rctx->src_sent = chunksize;
1353 if (spu->spu_assoc_resp_len(ctx->cipher.mode,
1354 aead_parms.assoc_size,
1355 aead_parms.ret_iv_len,
1356 rctx->is_encrypt))
1357 rx_frag_num++;
1359 aead_parms.iv_len = spu->spu_aead_ivlen(ctx->cipher.mode,
1360 rctx->iv_ctr_len);
1362 if (ctx->auth.alg == HASH_ALG_AES)
1363 hash_parms.type = (enum hash_type)ctx->cipher_type;
1365 /* General case AAD padding (CCM and RFC4543 special cases below) */
1366 aead_parms.aad_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1367 aead_parms.assoc_size);
1369 /* General case data padding (CCM decrypt special case below) */
1370 aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1371 chunksize);
1373 if (ctx->cipher.mode == CIPHER_MODE_CCM) {
1375 * for CCM, AAD len + 2 (rather than AAD len) needs to be
1376 * 128-bit aligned
1378 aead_parms.aad_pad_len = spu->spu_gcm_ccm_pad_len(
1379 ctx->cipher.mode,
1380 aead_parms.assoc_size + 2);
1383 * And when decrypting CCM, need to pad without including
1384 * size of ICV which is tacked on to end of chunk
1386 if (!rctx->is_encrypt)
1387 aead_parms.data_pad_len =
1388 spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1389 chunksize - digestsize);
1391 /* CCM also requires software to rewrite portions of IV: */
1392 spu->spu_ccm_update_iv(digestsize, &cipher_parms, req->assoclen,
1393 chunksize, rctx->is_encrypt,
1394 ctx->is_esp);
1397 if (ctx->is_rfc4543) {
1399 * RFC4543: data is included in AAD, so don't pad after AAD
1400 * and pad data based on both AAD + data size
1402 aead_parms.aad_pad_len = 0;
1403 if (!rctx->is_encrypt)
1404 aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(
1405 ctx->cipher.mode,
1406 aead_parms.assoc_size + chunksize -
1407 digestsize);
1408 else
1409 aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(
1410 ctx->cipher.mode,
1411 aead_parms.assoc_size + chunksize);
1413 req_opts.is_rfc4543 = true;
1416 if (spu_req_incl_icv(ctx->cipher.mode, rctx->is_encrypt)) {
1417 incl_icv = true;
1418 tx_frag_num++;
1419 /* Copy ICV from end of src scatterlist to digest buf */
1420 sg_copy_part_to_buf(req->src, rctx->msg_buf.digest, digestsize,
1421 req->assoclen + rctx->total_sent -
1422 digestsize);
1425 atomic64_add(chunksize, &iproc_priv.bytes_out);
1427 flow_log("%s()-sent chunksize:%u\n", __func__, chunksize);
1429 /* Prepend SPU header with type 3 BCM header */
1430 memcpy(rctx->msg_buf.bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
1432 spu_hdr_len = spu->spu_create_request(rctx->msg_buf.bcm_spu_req_hdr +
1433 BCM_HDR_LEN, &req_opts,
1434 &cipher_parms, &hash_parms,
1435 &aead_parms, chunksize);
1437 /* Determine total length of padding. Put all padding in one buffer. */
1438 db_size = spu_real_db_size(aead_parms.assoc_size, aead_parms.iv_len, 0,
1439 chunksize, aead_parms.aad_pad_len,
1440 aead_parms.data_pad_len, 0);
1442 stat_pad_len = spu->spu_wordalign_padlen(db_size);
1444 if (stat_pad_len)
1445 rx_frag_num++;
1446 pad_len = aead_parms.data_pad_len + stat_pad_len;
1447 if (pad_len) {
1448 tx_frag_num++;
1449 spu->spu_request_pad(rctx->msg_buf.spu_req_pad,
1450 aead_parms.data_pad_len, 0,
1451 ctx->auth.alg, ctx->auth.mode,
1452 rctx->total_sent, stat_pad_len);
1455 spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
1456 spu_hdr_len);
1457 dump_sg(rctx->assoc, 0, aead_parms.assoc_size);
1458 packet_dump(" aead iv: ", rctx->msg_buf.iv_ctr, aead_parms.iv_len);
1459 packet_log("BD:\n");
1460 dump_sg(rctx->src_sg, rctx->src_skip, chunksize);
1461 packet_dump(" pad: ", rctx->msg_buf.spu_req_pad, pad_len);
1464 * Build mailbox message containing SPU request msg and rx buffers
1465 * to catch response message
1467 memset(mssg, 0, sizeof(*mssg));
1468 mssg->type = BRCM_MESSAGE_SPU;
1469 mssg->ctx = rctx; /* Will be returned in response */
1471 /* Create rx scatterlist to catch result */
1472 rx_frag_num += rctx->dst_nents;
1473 resp_len = chunksize;
1476 * Always catch ICV in separate buffer. Have to for GCM/CCM because of
1477 * padding. Have to for SHA-224 and other truncated SHAs because SPU
1478 * sends entire digest back.
1480 rx_frag_num++;
1482 if (((ctx->cipher.mode == CIPHER_MODE_GCM) ||
1483 (ctx->cipher.mode == CIPHER_MODE_CCM)) && !rctx->is_encrypt) {
1485 * Input is ciphertxt plus ICV, but ICV not incl
1486 * in output.
1488 resp_len -= ctx->digestsize;
1489 if (resp_len == 0)
1490 /* no rx frags to catch output data */
1491 rx_frag_num -= rctx->dst_nents;
1494 err = spu_aead_rx_sg_create(mssg, req, rctx, rx_frag_num,
1495 aead_parms.assoc_size,
1496 aead_parms.ret_iv_len, resp_len, digestsize,
1497 stat_pad_len);
1498 if (err)
1499 return err;
1501 /* Create tx scatterlist containing SPU request message */
1502 tx_frag_num += rctx->src_nents;
1503 tx_frag_num += assoc_nents;
1504 if (aead_parms.aad_pad_len)
1505 tx_frag_num++;
1506 if (aead_parms.iv_len)
1507 tx_frag_num++;
1508 if (spu->spu_tx_status_len())
1509 tx_frag_num++;
1510 err = spu_aead_tx_sg_create(mssg, rctx, tx_frag_num, spu_hdr_len,
1511 rctx->assoc, aead_parms.assoc_size,
1512 assoc_nents, aead_parms.iv_len, chunksize,
1513 aead_parms.aad_pad_len, pad_len, incl_icv);
1514 if (err)
1515 return err;
1517 err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
1518 if (unlikely(err < 0))
1519 return err;
1521 return -EINPROGRESS;
1525 * handle_aead_resp() - Process a SPU response message for an AEAD request.
1526 * @rctx: Crypto request context
1528 static void handle_aead_resp(struct iproc_reqctx_s *rctx)
1530 struct spu_hw *spu = &iproc_priv.spu;
1531 struct crypto_async_request *areq = rctx->parent;
1532 struct aead_request *req = container_of(areq,
1533 struct aead_request, base);
1534 struct iproc_ctx_s *ctx = rctx->ctx;
1535 u32 payload_len;
1536 unsigned int icv_offset;
1537 u32 result_len;
1539 /* See how much data was returned */
1540 payload_len = spu->spu_payload_length(rctx->msg_buf.spu_resp_hdr);
1541 flow_log("payload_len %u\n", payload_len);
1543 /* only count payload */
1544 atomic64_add(payload_len, &iproc_priv.bytes_in);
1546 if (req->assoclen)
1547 packet_dump(" assoc_data ", rctx->msg_buf.a.resp_aad,
1548 req->assoclen);
1551 * Copy the ICV back to the destination
1552 * buffer. In decrypt case, SPU gives us back the digest, but crypto
1553 * API doesn't expect ICV in dst buffer.
1555 result_len = req->cryptlen;
1556 if (rctx->is_encrypt) {
1557 icv_offset = req->assoclen + rctx->total_sent;
1558 packet_dump(" ICV: ", rctx->msg_buf.digest, ctx->digestsize);
1559 flow_log("copying ICV to dst sg at offset %u\n", icv_offset);
1560 sg_copy_part_from_buf(req->dst, rctx->msg_buf.digest,
1561 ctx->digestsize, icv_offset);
1562 result_len += ctx->digestsize;
1565 packet_log("response data: ");
1566 dump_sg(req->dst, req->assoclen, result_len);
1568 atomic_inc(&iproc_priv.op_counts[SPU_OP_AEAD]);
1569 if (ctx->cipher.alg == CIPHER_ALG_AES) {
1570 if (ctx->cipher.mode == CIPHER_MODE_CCM)
1571 atomic_inc(&iproc_priv.aead_cnt[AES_CCM]);
1572 else if (ctx->cipher.mode == CIPHER_MODE_GCM)
1573 atomic_inc(&iproc_priv.aead_cnt[AES_GCM]);
1574 else
1575 atomic_inc(&iproc_priv.aead_cnt[AUTHENC]);
1576 } else {
1577 atomic_inc(&iproc_priv.aead_cnt[AUTHENC]);
1582 * spu_chunk_cleanup() - Do cleanup after processing one chunk of a request
1583 * @rctx: request context
1585 * Mailbox scatterlists are allocated for each chunk. So free them after
1586 * processing each chunk.
1588 static void spu_chunk_cleanup(struct iproc_reqctx_s *rctx)
1590 /* mailbox message used to tx request */
1591 struct brcm_message *mssg = &rctx->mb_mssg;
1593 kfree(mssg->spu.src);
1594 kfree(mssg->spu.dst);
1595 memset(mssg, 0, sizeof(struct brcm_message));
1599 * finish_req() - Used to invoke the complete callback from the requester when
1600 * a request has been handled asynchronously.
1601 * @rctx: Request context
1602 * @err: Indicates whether the request was successful or not
1604 * Ensures that cleanup has been done for request
1606 static void finish_req(struct iproc_reqctx_s *rctx, int err)
1608 struct crypto_async_request *areq = rctx->parent;
1610 flow_log("%s() err:%d\n\n", __func__, err);
1612 /* No harm done if already called */
1613 spu_chunk_cleanup(rctx);
1615 if (areq)
1616 crypto_request_complete(areq, err);
1620 * spu_rx_callback() - Callback from mailbox framework with a SPU response.
1621 * @cl: mailbox client structure for SPU driver
1622 * @msg: mailbox message containing SPU response
1624 static void spu_rx_callback(struct mbox_client *cl, void *msg)
1626 struct spu_hw *spu = &iproc_priv.spu;
1627 struct brcm_message *mssg = msg;
1628 struct iproc_reqctx_s *rctx;
1629 int err;
1631 rctx = mssg->ctx;
1632 if (unlikely(!rctx)) {
1633 /* This is fatal */
1634 pr_err("%s(): no request context", __func__);
1635 err = -EFAULT;
1636 goto cb_finish;
1639 /* process the SPU status */
1640 err = spu->spu_status_process(rctx->msg_buf.rx_stat);
1641 if (err != 0) {
1642 if (err == SPU_INVALID_ICV)
1643 atomic_inc(&iproc_priv.bad_icv);
1644 err = -EBADMSG;
1645 goto cb_finish;
1648 /* Process the SPU response message */
1649 switch (rctx->ctx->alg->type) {
1650 case CRYPTO_ALG_TYPE_SKCIPHER:
1651 handle_skcipher_resp(rctx);
1652 break;
1653 case CRYPTO_ALG_TYPE_AHASH:
1654 handle_ahash_resp(rctx);
1655 break;
1656 case CRYPTO_ALG_TYPE_AEAD:
1657 handle_aead_resp(rctx);
1658 break;
1659 default:
1660 err = -EINVAL;
1661 goto cb_finish;
1665 * If this response does not complete the request, then send the next
1666 * request chunk.
1668 if (rctx->total_sent < rctx->total_todo) {
1669 /* Deallocate anything specific to previous chunk */
1670 spu_chunk_cleanup(rctx);
1672 switch (rctx->ctx->alg->type) {
1673 case CRYPTO_ALG_TYPE_SKCIPHER:
1674 err = handle_skcipher_req(rctx);
1675 break;
1676 case CRYPTO_ALG_TYPE_AHASH:
1677 err = handle_ahash_req(rctx);
1678 if (err == -EAGAIN)
1680 * we saved data in hash carry, but tell crypto
1681 * API we successfully completed request.
1683 err = 0;
1684 break;
1685 case CRYPTO_ALG_TYPE_AEAD:
1686 err = handle_aead_req(rctx);
1687 break;
1688 default:
1689 err = -EINVAL;
1692 if (err == -EINPROGRESS)
1693 /* Successfully submitted request for next chunk */
1694 return;
1697 cb_finish:
1698 finish_req(rctx, err);
1701 /* ==================== Kernel Cryptographic API ==================== */
1704 * skcipher_enqueue() - Handle skcipher encrypt or decrypt request.
1705 * @req: Crypto API request
1706 * @encrypt: true if encrypting; false if decrypting
1708 * Return: -EINPROGRESS if request accepted and result will be returned
1709 * asynchronously
1710 * < 0 if an error
1712 static int skcipher_enqueue(struct skcipher_request *req, bool encrypt)
1714 struct iproc_reqctx_s *rctx = skcipher_request_ctx(req);
1715 struct iproc_ctx_s *ctx =
1716 crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
1717 int err;
1719 flow_log("%s() enc:%u\n", __func__, encrypt);
1721 rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1722 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1723 rctx->parent = &req->base;
1724 rctx->is_encrypt = encrypt;
1725 rctx->bd_suppress = false;
1726 rctx->total_todo = req->cryptlen;
1727 rctx->src_sent = 0;
1728 rctx->total_sent = 0;
1729 rctx->total_received = 0;
1730 rctx->ctx = ctx;
1732 /* Initialize current position in src and dst scatterlists */
1733 rctx->src_sg = req->src;
1734 rctx->src_nents = 0;
1735 rctx->src_skip = 0;
1736 rctx->dst_sg = req->dst;
1737 rctx->dst_nents = 0;
1738 rctx->dst_skip = 0;
1740 if (ctx->cipher.mode == CIPHER_MODE_CBC ||
1741 ctx->cipher.mode == CIPHER_MODE_CTR ||
1742 ctx->cipher.mode == CIPHER_MODE_OFB ||
1743 ctx->cipher.mode == CIPHER_MODE_XTS ||
1744 ctx->cipher.mode == CIPHER_MODE_GCM ||
1745 ctx->cipher.mode == CIPHER_MODE_CCM) {
1746 rctx->iv_ctr_len =
1747 crypto_skcipher_ivsize(crypto_skcipher_reqtfm(req));
1748 memcpy(rctx->msg_buf.iv_ctr, req->iv, rctx->iv_ctr_len);
1749 } else {
1750 rctx->iv_ctr_len = 0;
1753 /* Choose a SPU to process this request */
1754 rctx->chan_idx = select_channel();
1755 err = handle_skcipher_req(rctx);
1756 if (err != -EINPROGRESS)
1757 /* synchronous result */
1758 spu_chunk_cleanup(rctx);
1760 return err;
1763 static int des_setkey(struct crypto_skcipher *cipher, const u8 *key,
1764 unsigned int keylen)
1766 struct iproc_ctx_s *ctx = crypto_skcipher_ctx(cipher);
1767 int err;
1769 err = verify_skcipher_des_key(cipher, key);
1770 if (err)
1771 return err;
1773 ctx->cipher_type = CIPHER_TYPE_DES;
1774 return 0;
1777 static int threedes_setkey(struct crypto_skcipher *cipher, const u8 *key,
1778 unsigned int keylen)
1780 struct iproc_ctx_s *ctx = crypto_skcipher_ctx(cipher);
1781 int err;
1783 err = verify_skcipher_des3_key(cipher, key);
1784 if (err)
1785 return err;
1787 ctx->cipher_type = CIPHER_TYPE_3DES;
1788 return 0;
1791 static int aes_setkey(struct crypto_skcipher *cipher, const u8 *key,
1792 unsigned int keylen)
1794 struct iproc_ctx_s *ctx = crypto_skcipher_ctx(cipher);
1796 if (ctx->cipher.mode == CIPHER_MODE_XTS)
1797 /* XTS includes two keys of equal length */
1798 keylen = keylen / 2;
1800 switch (keylen) {
1801 case AES_KEYSIZE_128:
1802 ctx->cipher_type = CIPHER_TYPE_AES128;
1803 break;
1804 case AES_KEYSIZE_192:
1805 ctx->cipher_type = CIPHER_TYPE_AES192;
1806 break;
1807 case AES_KEYSIZE_256:
1808 ctx->cipher_type = CIPHER_TYPE_AES256;
1809 break;
1810 default:
1811 return -EINVAL;
1813 WARN_ON((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
1814 ((ctx->max_payload % AES_BLOCK_SIZE) != 0));
1815 return 0;
1818 static int skcipher_setkey(struct crypto_skcipher *cipher, const u8 *key,
1819 unsigned int keylen)
1821 struct spu_hw *spu = &iproc_priv.spu;
1822 struct iproc_ctx_s *ctx = crypto_skcipher_ctx(cipher);
1823 struct spu_cipher_parms cipher_parms;
1824 u32 alloc_len = 0;
1825 int err;
1827 flow_log("skcipher_setkey() keylen: %d\n", keylen);
1828 flow_dump(" key: ", key, keylen);
1830 switch (ctx->cipher.alg) {
1831 case CIPHER_ALG_DES:
1832 err = des_setkey(cipher, key, keylen);
1833 break;
1834 case CIPHER_ALG_3DES:
1835 err = threedes_setkey(cipher, key, keylen);
1836 break;
1837 case CIPHER_ALG_AES:
1838 err = aes_setkey(cipher, key, keylen);
1839 break;
1840 default:
1841 pr_err("%s() Error: unknown cipher alg\n", __func__);
1842 err = -EINVAL;
1844 if (err)
1845 return err;
1847 memcpy(ctx->enckey, key, keylen);
1848 ctx->enckeylen = keylen;
1850 /* SPU needs XTS keys in the reverse order the crypto API presents */
1851 if ((ctx->cipher.alg == CIPHER_ALG_AES) &&
1852 (ctx->cipher.mode == CIPHER_MODE_XTS)) {
1853 unsigned int xts_keylen = keylen / 2;
1855 memcpy(ctx->enckey, key + xts_keylen, xts_keylen);
1856 memcpy(ctx->enckey + xts_keylen, key, xts_keylen);
1859 if (spu->spu_type == SPU_TYPE_SPUM)
1860 alloc_len = BCM_HDR_LEN + SPU_HEADER_ALLOC_LEN;
1861 else if (spu->spu_type == SPU_TYPE_SPU2)
1862 alloc_len = BCM_HDR_LEN + SPU2_HEADER_ALLOC_LEN;
1863 memset(ctx->bcm_spu_req_hdr, 0, alloc_len);
1864 cipher_parms.iv_buf = NULL;
1865 cipher_parms.iv_len = crypto_skcipher_ivsize(cipher);
1866 flow_log("%s: iv_len %u\n", __func__, cipher_parms.iv_len);
1868 cipher_parms.alg = ctx->cipher.alg;
1869 cipher_parms.mode = ctx->cipher.mode;
1870 cipher_parms.type = ctx->cipher_type;
1871 cipher_parms.key_buf = ctx->enckey;
1872 cipher_parms.key_len = ctx->enckeylen;
1874 /* Prepend SPU request message with BCM header */
1875 memcpy(ctx->bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
1876 ctx->spu_req_hdr_len =
1877 spu->spu_cipher_req_init(ctx->bcm_spu_req_hdr + BCM_HDR_LEN,
1878 &cipher_parms);
1880 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
1881 ctx->enckeylen,
1882 false);
1884 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_CIPHER]);
1886 return 0;
1889 static int skcipher_encrypt(struct skcipher_request *req)
1891 flow_log("skcipher_encrypt() nbytes:%u\n", req->cryptlen);
1893 return skcipher_enqueue(req, true);
1896 static int skcipher_decrypt(struct skcipher_request *req)
1898 flow_log("skcipher_decrypt() nbytes:%u\n", req->cryptlen);
1899 return skcipher_enqueue(req, false);
1902 static int ahash_enqueue(struct ahash_request *req)
1904 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
1905 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1906 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
1907 int err;
1908 const char *alg_name;
1910 flow_log("ahash_enqueue() nbytes:%u\n", req->nbytes);
1912 rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1913 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1914 rctx->parent = &req->base;
1915 rctx->ctx = ctx;
1916 rctx->bd_suppress = true;
1917 memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
1919 /* Initialize position in src scatterlist */
1920 rctx->src_sg = req->src;
1921 rctx->src_skip = 0;
1922 rctx->src_nents = 0;
1923 rctx->dst_sg = NULL;
1924 rctx->dst_skip = 0;
1925 rctx->dst_nents = 0;
1927 /* SPU2 hardware does not compute hash of zero length data */
1928 if ((rctx->is_final == 1) && (rctx->total_todo == 0) &&
1929 (iproc_priv.spu.spu_type == SPU_TYPE_SPU2)) {
1930 alg_name = crypto_ahash_alg_name(tfm);
1931 flow_log("Doing %sfinal %s zero-len hash request in software\n",
1932 rctx->is_final ? "" : "non-", alg_name);
1933 err = do_shash((unsigned char *)alg_name, req->result,
1934 NULL, 0, NULL, 0, ctx->authkey,
1935 ctx->authkeylen);
1936 if (err < 0)
1937 flow_log("Hash request failed with error %d\n", err);
1938 return err;
1940 /* Choose a SPU to process this request */
1941 rctx->chan_idx = select_channel();
1943 err = handle_ahash_req(rctx);
1944 if (err != -EINPROGRESS)
1945 /* synchronous result */
1946 spu_chunk_cleanup(rctx);
1948 if (err == -EAGAIN)
1950 * we saved data in hash carry, but tell crypto API
1951 * we successfully completed request.
1953 err = 0;
1955 return err;
1958 static int __ahash_init(struct ahash_request *req)
1960 struct spu_hw *spu = &iproc_priv.spu;
1961 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
1962 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1963 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
1965 flow_log("%s()\n", __func__);
1967 /* Initialize the context */
1968 rctx->hash_carry_len = 0;
1969 rctx->is_final = 0;
1971 rctx->total_todo = 0;
1972 rctx->src_sent = 0;
1973 rctx->total_sent = 0;
1974 rctx->total_received = 0;
1976 ctx->digestsize = crypto_ahash_digestsize(tfm);
1977 /* If we add a hash whose digest is larger, catch it here. */
1978 WARN_ON(ctx->digestsize > MAX_DIGEST_SIZE);
1980 rctx->is_sw_hmac = false;
1982 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen, 0,
1983 true);
1985 return 0;
1989 * spu_no_incr_hash() - Determine whether incremental hashing is supported.
1990 * @ctx: Crypto session context
1992 * SPU-2 does not support incremental hashing (we'll have to revisit and
1993 * condition based on chip revision or device tree entry if future versions do
1994 * support incremental hash)
1996 * SPU-M also doesn't support incremental hashing of AES-XCBC
1998 * Return: true if incremental hashing is not supported
1999 * false otherwise
2001 static bool spu_no_incr_hash(struct iproc_ctx_s *ctx)
2003 struct spu_hw *spu = &iproc_priv.spu;
2005 if (spu->spu_type == SPU_TYPE_SPU2)
2006 return true;
2008 if ((ctx->auth.alg == HASH_ALG_AES) &&
2009 (ctx->auth.mode == HASH_MODE_XCBC))
2010 return true;
2012 /* Otherwise, incremental hashing is supported */
2013 return false;
2016 static int ahash_init(struct ahash_request *req)
2018 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2019 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2020 const char *alg_name;
2021 struct crypto_shash *hash;
2022 int ret;
2023 gfp_t gfp;
2025 if (spu_no_incr_hash(ctx)) {
2027 * If we get an incremental hashing request and it's not
2028 * supported by the hardware, we need to handle it in software
2029 * by calling synchronous hash functions.
2031 alg_name = crypto_ahash_alg_name(tfm);
2032 hash = crypto_alloc_shash(alg_name, 0, 0);
2033 if (IS_ERR(hash)) {
2034 ret = PTR_ERR(hash);
2035 goto err;
2038 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2039 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2040 ctx->shash = kmalloc(sizeof(*ctx->shash) +
2041 crypto_shash_descsize(hash), gfp);
2042 if (!ctx->shash) {
2043 ret = -ENOMEM;
2044 goto err_hash;
2046 ctx->shash->tfm = hash;
2048 /* Set the key using data we already have from setkey */
2049 if (ctx->authkeylen > 0) {
2050 ret = crypto_shash_setkey(hash, ctx->authkey,
2051 ctx->authkeylen);
2052 if (ret)
2053 goto err_shash;
2056 /* Initialize hash w/ this key and other params */
2057 ret = crypto_shash_init(ctx->shash);
2058 if (ret)
2059 goto err_shash;
2060 } else {
2061 /* Otherwise call the internal function which uses SPU hw */
2062 ret = __ahash_init(req);
2065 return ret;
2067 err_shash:
2068 kfree(ctx->shash);
2069 err_hash:
2070 crypto_free_shash(hash);
2071 err:
2072 return ret;
2075 static int __ahash_update(struct ahash_request *req)
2077 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2079 flow_log("ahash_update() nbytes:%u\n", req->nbytes);
2081 if (!req->nbytes)
2082 return 0;
2083 rctx->total_todo += req->nbytes;
2084 rctx->src_sent = 0;
2086 return ahash_enqueue(req);
2089 static int ahash_update(struct ahash_request *req)
2091 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2092 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2093 u8 *tmpbuf;
2094 int ret;
2095 int nents;
2096 gfp_t gfp;
2098 if (spu_no_incr_hash(ctx)) {
2100 * If we get an incremental hashing request and it's not
2101 * supported by the hardware, we need to handle it in software
2102 * by calling synchronous hash functions.
2104 if (req->src)
2105 nents = sg_nents(req->src);
2106 else
2107 return -EINVAL;
2109 /* Copy data from req scatterlist to tmp buffer */
2110 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2111 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2112 tmpbuf = kmalloc(req->nbytes, gfp);
2113 if (!tmpbuf)
2114 return -ENOMEM;
2116 if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
2117 req->nbytes) {
2118 kfree(tmpbuf);
2119 return -EINVAL;
2122 /* Call synchronous update */
2123 ret = crypto_shash_update(ctx->shash, tmpbuf, req->nbytes);
2124 kfree(tmpbuf);
2125 } else {
2126 /* Otherwise call the internal function which uses SPU hw */
2127 ret = __ahash_update(req);
2130 return ret;
2133 static int __ahash_final(struct ahash_request *req)
2135 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2137 flow_log("ahash_final() nbytes:%u\n", req->nbytes);
2139 rctx->is_final = 1;
2141 return ahash_enqueue(req);
2144 static int ahash_final(struct ahash_request *req)
2146 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2147 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2148 int ret;
2150 if (spu_no_incr_hash(ctx)) {
2152 * If we get an incremental hashing request and it's not
2153 * supported by the hardware, we need to handle it in software
2154 * by calling synchronous hash functions.
2156 ret = crypto_shash_final(ctx->shash, req->result);
2158 /* Done with hash, can deallocate it now */
2159 crypto_free_shash(ctx->shash->tfm);
2160 kfree(ctx->shash);
2162 } else {
2163 /* Otherwise call the internal function which uses SPU hw */
2164 ret = __ahash_final(req);
2167 return ret;
2170 static int __ahash_finup(struct ahash_request *req)
2172 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2174 flow_log("ahash_finup() nbytes:%u\n", req->nbytes);
2176 rctx->total_todo += req->nbytes;
2177 rctx->src_sent = 0;
2178 rctx->is_final = 1;
2180 return ahash_enqueue(req);
2183 static int ahash_finup(struct ahash_request *req)
2185 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2186 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2187 u8 *tmpbuf;
2188 int ret;
2189 int nents;
2190 gfp_t gfp;
2192 if (spu_no_incr_hash(ctx)) {
2194 * If we get an incremental hashing request and it's not
2195 * supported by the hardware, we need to handle it in software
2196 * by calling synchronous hash functions.
2198 if (req->src) {
2199 nents = sg_nents(req->src);
2200 } else {
2201 ret = -EINVAL;
2202 goto ahash_finup_exit;
2205 /* Copy data from req scatterlist to tmp buffer */
2206 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2207 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2208 tmpbuf = kmalloc(req->nbytes, gfp);
2209 if (!tmpbuf) {
2210 ret = -ENOMEM;
2211 goto ahash_finup_exit;
2214 if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
2215 req->nbytes) {
2216 ret = -EINVAL;
2217 goto ahash_finup_free;
2220 /* Call synchronous update */
2221 ret = crypto_shash_finup(ctx->shash, tmpbuf, req->nbytes,
2222 req->result);
2223 } else {
2224 /* Otherwise call the internal function which uses SPU hw */
2225 return __ahash_finup(req);
2227 ahash_finup_free:
2228 kfree(tmpbuf);
2230 ahash_finup_exit:
2231 /* Done with hash, can deallocate it now */
2232 crypto_free_shash(ctx->shash->tfm);
2233 kfree(ctx->shash);
2234 return ret;
2237 static int ahash_digest(struct ahash_request *req)
2239 int err;
2241 flow_log("ahash_digest() nbytes:%u\n", req->nbytes);
2243 /* whole thing at once */
2244 err = __ahash_init(req);
2245 if (!err)
2246 err = __ahash_finup(req);
2248 return err;
2251 static int ahash_setkey(struct crypto_ahash *ahash, const u8 *key,
2252 unsigned int keylen)
2254 struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
2256 flow_log("%s() ahash:%p key:%p keylen:%u\n",
2257 __func__, ahash, key, keylen);
2258 flow_dump(" key: ", key, keylen);
2260 if (ctx->auth.alg == HASH_ALG_AES) {
2261 switch (keylen) {
2262 case AES_KEYSIZE_128:
2263 ctx->cipher_type = CIPHER_TYPE_AES128;
2264 break;
2265 case AES_KEYSIZE_192:
2266 ctx->cipher_type = CIPHER_TYPE_AES192;
2267 break;
2268 case AES_KEYSIZE_256:
2269 ctx->cipher_type = CIPHER_TYPE_AES256;
2270 break;
2271 default:
2272 pr_err("%s() Error: Invalid key length\n", __func__);
2273 return -EINVAL;
2275 } else {
2276 pr_err("%s() Error: unknown hash alg\n", __func__);
2277 return -EINVAL;
2279 memcpy(ctx->authkey, key, keylen);
2280 ctx->authkeylen = keylen;
2282 return 0;
2285 static int ahash_export(struct ahash_request *req, void *out)
2287 const struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2288 struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)out;
2290 spu_exp->total_todo = rctx->total_todo;
2291 spu_exp->total_sent = rctx->total_sent;
2292 spu_exp->is_sw_hmac = rctx->is_sw_hmac;
2293 memcpy(spu_exp->hash_carry, rctx->hash_carry, sizeof(rctx->hash_carry));
2294 spu_exp->hash_carry_len = rctx->hash_carry_len;
2295 memcpy(spu_exp->incr_hash, rctx->incr_hash, sizeof(rctx->incr_hash));
2297 return 0;
2300 static int ahash_import(struct ahash_request *req, const void *in)
2302 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2303 struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)in;
2305 rctx->total_todo = spu_exp->total_todo;
2306 rctx->total_sent = spu_exp->total_sent;
2307 rctx->is_sw_hmac = spu_exp->is_sw_hmac;
2308 memcpy(rctx->hash_carry, spu_exp->hash_carry, sizeof(rctx->hash_carry));
2309 rctx->hash_carry_len = spu_exp->hash_carry_len;
2310 memcpy(rctx->incr_hash, spu_exp->incr_hash, sizeof(rctx->incr_hash));
2312 return 0;
2315 static int ahash_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,
2316 unsigned int keylen)
2318 struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
2319 unsigned int blocksize =
2320 crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
2321 unsigned int digestsize = crypto_ahash_digestsize(ahash);
2322 unsigned int index;
2323 int rc;
2325 flow_log("%s() ahash:%p key:%p keylen:%u blksz:%u digestsz:%u\n",
2326 __func__, ahash, key, keylen, blocksize, digestsize);
2327 flow_dump(" key: ", key, keylen);
2329 if (keylen > blocksize) {
2330 switch (ctx->auth.alg) {
2331 case HASH_ALG_MD5:
2332 rc = do_shash("md5", ctx->authkey, key, keylen, NULL,
2333 0, NULL, 0);
2334 break;
2335 case HASH_ALG_SHA1:
2336 rc = do_shash("sha1", ctx->authkey, key, keylen, NULL,
2337 0, NULL, 0);
2338 break;
2339 case HASH_ALG_SHA224:
2340 rc = do_shash("sha224", ctx->authkey, key, keylen, NULL,
2341 0, NULL, 0);
2342 break;
2343 case HASH_ALG_SHA256:
2344 rc = do_shash("sha256", ctx->authkey, key, keylen, NULL,
2345 0, NULL, 0);
2346 break;
2347 case HASH_ALG_SHA384:
2348 rc = do_shash("sha384", ctx->authkey, key, keylen, NULL,
2349 0, NULL, 0);
2350 break;
2351 case HASH_ALG_SHA512:
2352 rc = do_shash("sha512", ctx->authkey, key, keylen, NULL,
2353 0, NULL, 0);
2354 break;
2355 case HASH_ALG_SHA3_224:
2356 rc = do_shash("sha3-224", ctx->authkey, key, keylen,
2357 NULL, 0, NULL, 0);
2358 break;
2359 case HASH_ALG_SHA3_256:
2360 rc = do_shash("sha3-256", ctx->authkey, key, keylen,
2361 NULL, 0, NULL, 0);
2362 break;
2363 case HASH_ALG_SHA3_384:
2364 rc = do_shash("sha3-384", ctx->authkey, key, keylen,
2365 NULL, 0, NULL, 0);
2366 break;
2367 case HASH_ALG_SHA3_512:
2368 rc = do_shash("sha3-512", ctx->authkey, key, keylen,
2369 NULL, 0, NULL, 0);
2370 break;
2371 default:
2372 pr_err("%s() Error: unknown hash alg\n", __func__);
2373 return -EINVAL;
2375 if (rc < 0) {
2376 pr_err("%s() Error %d computing shash for %s\n",
2377 __func__, rc, hash_alg_name[ctx->auth.alg]);
2378 return rc;
2380 ctx->authkeylen = digestsize;
2382 flow_log(" keylen > digestsize... hashed\n");
2383 flow_dump(" newkey: ", ctx->authkey, ctx->authkeylen);
2384 } else {
2385 memcpy(ctx->authkey, key, keylen);
2386 ctx->authkeylen = keylen;
2390 * Full HMAC operation in SPUM is not verified,
2391 * So keeping the generation of IPAD, OPAD and
2392 * outer hashing in software.
2394 if (iproc_priv.spu.spu_type == SPU_TYPE_SPUM) {
2395 memcpy(ctx->ipad, ctx->authkey, ctx->authkeylen);
2396 memset(ctx->ipad + ctx->authkeylen, 0,
2397 blocksize - ctx->authkeylen);
2398 ctx->authkeylen = 0;
2399 unsafe_memcpy(ctx->opad, ctx->ipad, blocksize,
2400 "fortified memcpy causes -Wrestrict warning");
2402 for (index = 0; index < blocksize; index++) {
2403 ctx->ipad[index] ^= HMAC_IPAD_VALUE;
2404 ctx->opad[index] ^= HMAC_OPAD_VALUE;
2407 flow_dump(" ipad: ", ctx->ipad, blocksize);
2408 flow_dump(" opad: ", ctx->opad, blocksize);
2410 ctx->digestsize = digestsize;
2411 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_HMAC]);
2413 return 0;
2416 static int ahash_hmac_init(struct ahash_request *req)
2418 int ret;
2419 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2420 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2421 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2422 unsigned int blocksize =
2423 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2425 flow_log("ahash_hmac_init()\n");
2427 /* init the context as a hash */
2428 ret = ahash_init(req);
2429 if (ret)
2430 return ret;
2432 if (!spu_no_incr_hash(ctx)) {
2433 /* SPU-M can do incr hashing but needs sw for outer HMAC */
2434 rctx->is_sw_hmac = true;
2435 ctx->auth.mode = HASH_MODE_HASH;
2436 /* start with a prepended ipad */
2437 memcpy(rctx->hash_carry, ctx->ipad, blocksize);
2438 rctx->hash_carry_len = blocksize;
2439 rctx->total_todo += blocksize;
2442 return 0;
2445 static int ahash_hmac_update(struct ahash_request *req)
2447 flow_log("ahash_hmac_update() nbytes:%u\n", req->nbytes);
2449 if (!req->nbytes)
2450 return 0;
2452 return ahash_update(req);
2455 static int ahash_hmac_final(struct ahash_request *req)
2457 flow_log("ahash_hmac_final() nbytes:%u\n", req->nbytes);
2459 return ahash_final(req);
2462 static int ahash_hmac_finup(struct ahash_request *req)
2464 flow_log("ahash_hmac_finupl() nbytes:%u\n", req->nbytes);
2466 return ahash_finup(req);
2469 static int ahash_hmac_digest(struct ahash_request *req)
2471 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2472 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2473 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2474 unsigned int blocksize =
2475 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2477 flow_log("ahash_hmac_digest() nbytes:%u\n", req->nbytes);
2479 /* Perform initialization and then call finup */
2480 __ahash_init(req);
2482 if (iproc_priv.spu.spu_type == SPU_TYPE_SPU2) {
2484 * SPU2 supports full HMAC implementation in the
2485 * hardware, need not to generate IPAD, OPAD and
2486 * outer hash in software.
2487 * Only for hash key len > hash block size, SPU2
2488 * expects to perform hashing on the key, shorten
2489 * it to digest size and feed it as hash key.
2491 rctx->is_sw_hmac = false;
2492 ctx->auth.mode = HASH_MODE_HMAC;
2493 } else {
2494 rctx->is_sw_hmac = true;
2495 ctx->auth.mode = HASH_MODE_HASH;
2496 /* start with a prepended ipad */
2497 memcpy(rctx->hash_carry, ctx->ipad, blocksize);
2498 rctx->hash_carry_len = blocksize;
2499 rctx->total_todo += blocksize;
2502 return __ahash_finup(req);
2505 /* aead helpers */
2507 static int aead_need_fallback(struct aead_request *req)
2509 struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2510 struct spu_hw *spu = &iproc_priv.spu;
2511 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2512 struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
2513 u32 payload_len;
2516 * SPU hardware cannot handle the AES-GCM/CCM case where plaintext
2517 * and AAD are both 0 bytes long. So use fallback in this case.
2519 if (((ctx->cipher.mode == CIPHER_MODE_GCM) ||
2520 (ctx->cipher.mode == CIPHER_MODE_CCM)) &&
2521 (req->assoclen == 0)) {
2522 if ((rctx->is_encrypt && (req->cryptlen == 0)) ||
2523 (!rctx->is_encrypt && (req->cryptlen == ctx->digestsize))) {
2524 flow_log("AES GCM/CCM needs fallback for 0 len req\n");
2525 return 1;
2529 /* SPU-M hardware only supports CCM digest size of 8, 12, or 16 bytes */
2530 if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
2531 (spu->spu_type == SPU_TYPE_SPUM) &&
2532 (ctx->digestsize != 8) && (ctx->digestsize != 12) &&
2533 (ctx->digestsize != 16)) {
2534 flow_log("%s() AES CCM needs fallback for digest size %d\n",
2535 __func__, ctx->digestsize);
2536 return 1;
2540 * SPU-M on NSP has an issue where AES-CCM hash is not correct
2541 * when AAD size is 0
2543 if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
2544 (spu->spu_subtype == SPU_SUBTYPE_SPUM_NSP) &&
2545 (req->assoclen == 0)) {
2546 flow_log("%s() AES_CCM needs fallback for 0 len AAD on NSP\n",
2547 __func__);
2548 return 1;
2552 * RFC4106 and RFC4543 cannot handle the case where AAD is other than
2553 * 16 or 20 bytes long. So use fallback in this case.
2555 if (ctx->cipher.mode == CIPHER_MODE_GCM &&
2556 ctx->cipher.alg == CIPHER_ALG_AES &&
2557 rctx->iv_ctr_len == GCM_RFC4106_IV_SIZE &&
2558 req->assoclen != 16 && req->assoclen != 20) {
2559 flow_log("RFC4106/RFC4543 needs fallback for assoclen"
2560 " other than 16 or 20 bytes\n");
2561 return 1;
2564 payload_len = req->cryptlen;
2565 if (spu->spu_type == SPU_TYPE_SPUM)
2566 payload_len += req->assoclen;
2568 flow_log("%s() payload len: %u\n", __func__, payload_len);
2570 if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
2571 return 0;
2572 else
2573 return payload_len > ctx->max_payload;
2576 static int aead_do_fallback(struct aead_request *req, bool is_encrypt)
2578 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2579 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
2580 struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2581 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
2582 struct aead_request *subreq;
2584 flow_log("%s() enc:%u\n", __func__, is_encrypt);
2586 if (!ctx->fallback_cipher)
2587 return -EINVAL;
2589 subreq = &rctx->req;
2590 aead_request_set_tfm(subreq, ctx->fallback_cipher);
2591 aead_request_set_callback(subreq, aead_request_flags(req),
2592 req->base.complete, req->base.data);
2593 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
2594 req->iv);
2595 aead_request_set_ad(subreq, req->assoclen);
2597 return is_encrypt ? crypto_aead_encrypt(req) :
2598 crypto_aead_decrypt(req);
2601 static int aead_enqueue(struct aead_request *req, bool is_encrypt)
2603 struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2604 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2605 struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
2606 int err;
2608 flow_log("%s() enc:%u\n", __func__, is_encrypt);
2610 if (req->assoclen > MAX_ASSOC_SIZE) {
2611 pr_err
2612 ("%s() Error: associated data too long. (%u > %u bytes)\n",
2613 __func__, req->assoclen, MAX_ASSOC_SIZE);
2614 return -EINVAL;
2617 rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2618 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2619 rctx->parent = &req->base;
2620 rctx->is_encrypt = is_encrypt;
2621 rctx->bd_suppress = false;
2622 rctx->total_todo = req->cryptlen;
2623 rctx->src_sent = 0;
2624 rctx->total_sent = 0;
2625 rctx->total_received = 0;
2626 rctx->is_sw_hmac = false;
2627 rctx->ctx = ctx;
2628 memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
2630 /* assoc data is at start of src sg */
2631 rctx->assoc = req->src;
2634 * Init current position in src scatterlist to be after assoc data.
2635 * src_skip set to buffer offset where data begins. (Assoc data could
2636 * end in the middle of a buffer.)
2638 if (spu_sg_at_offset(req->src, req->assoclen, &rctx->src_sg,
2639 &rctx->src_skip) < 0) {
2640 pr_err("%s() Error: Unable to find start of src data\n",
2641 __func__);
2642 return -EINVAL;
2645 rctx->src_nents = 0;
2646 rctx->dst_nents = 0;
2647 if (req->dst == req->src) {
2648 rctx->dst_sg = rctx->src_sg;
2649 rctx->dst_skip = rctx->src_skip;
2650 } else {
2652 * Expect req->dst to have room for assoc data followed by
2653 * output data and ICV, if encrypt. So initialize dst_sg
2654 * to point beyond assoc len offset.
2656 if (spu_sg_at_offset(req->dst, req->assoclen, &rctx->dst_sg,
2657 &rctx->dst_skip) < 0) {
2658 pr_err("%s() Error: Unable to find start of dst data\n",
2659 __func__);
2660 return -EINVAL;
2664 if (ctx->cipher.mode == CIPHER_MODE_CBC ||
2665 ctx->cipher.mode == CIPHER_MODE_CTR ||
2666 ctx->cipher.mode == CIPHER_MODE_OFB ||
2667 ctx->cipher.mode == CIPHER_MODE_XTS ||
2668 ctx->cipher.mode == CIPHER_MODE_GCM) {
2669 rctx->iv_ctr_len =
2670 ctx->salt_len +
2671 crypto_aead_ivsize(crypto_aead_reqtfm(req));
2672 } else if (ctx->cipher.mode == CIPHER_MODE_CCM) {
2673 rctx->iv_ctr_len = CCM_AES_IV_SIZE;
2674 } else {
2675 rctx->iv_ctr_len = 0;
2678 rctx->hash_carry_len = 0;
2680 flow_log(" src sg: %p\n", req->src);
2681 flow_log(" rctx->src_sg: %p, src_skip %u\n",
2682 rctx->src_sg, rctx->src_skip);
2683 flow_log(" assoc: %p, assoclen %u\n", rctx->assoc, req->assoclen);
2684 flow_log(" dst sg: %p\n", req->dst);
2685 flow_log(" rctx->dst_sg: %p, dst_skip %u\n",
2686 rctx->dst_sg, rctx->dst_skip);
2687 flow_log(" iv_ctr_len:%u\n", rctx->iv_ctr_len);
2688 flow_dump(" iv: ", req->iv, rctx->iv_ctr_len);
2689 flow_log(" authkeylen:%u\n", ctx->authkeylen);
2690 flow_log(" is_esp: %s\n", ctx->is_esp ? "yes" : "no");
2692 if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
2693 flow_log(" max_payload infinite");
2694 else
2695 flow_log(" max_payload: %u\n", ctx->max_payload);
2697 if (unlikely(aead_need_fallback(req)))
2698 return aead_do_fallback(req, is_encrypt);
2701 * Do memory allocations for request after fallback check, because if we
2702 * do fallback, we won't call finish_req() to dealloc.
2704 if (rctx->iv_ctr_len) {
2705 if (ctx->salt_len)
2706 memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset,
2707 ctx->salt, ctx->salt_len);
2708 memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset + ctx->salt_len,
2709 req->iv,
2710 rctx->iv_ctr_len - ctx->salt_len - ctx->salt_offset);
2713 rctx->chan_idx = select_channel();
2714 err = handle_aead_req(rctx);
2715 if (err != -EINPROGRESS)
2716 /* synchronous result */
2717 spu_chunk_cleanup(rctx);
2719 return err;
2722 static int aead_authenc_setkey(struct crypto_aead *cipher,
2723 const u8 *key, unsigned int keylen)
2725 struct spu_hw *spu = &iproc_priv.spu;
2726 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2727 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
2728 struct crypto_authenc_keys keys;
2729 int ret;
2731 flow_log("%s() aead:%p key:%p keylen:%u\n", __func__, cipher, key,
2732 keylen);
2733 flow_dump(" key: ", key, keylen);
2735 ret = crypto_authenc_extractkeys(&keys, key, keylen);
2736 if (ret)
2737 goto badkey;
2739 if (keys.enckeylen > MAX_KEY_SIZE ||
2740 keys.authkeylen > MAX_KEY_SIZE)
2741 goto badkey;
2743 ctx->enckeylen = keys.enckeylen;
2744 ctx->authkeylen = keys.authkeylen;
2746 memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
2747 /* May end up padding auth key. So make sure it's zeroed. */
2748 memset(ctx->authkey, 0, sizeof(ctx->authkey));
2749 memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
2751 switch (ctx->alg->cipher_info.alg) {
2752 case CIPHER_ALG_DES:
2753 if (verify_aead_des_key(cipher, keys.enckey, keys.enckeylen))
2754 return -EINVAL;
2756 ctx->cipher_type = CIPHER_TYPE_DES;
2757 break;
2758 case CIPHER_ALG_3DES:
2759 if (verify_aead_des3_key(cipher, keys.enckey, keys.enckeylen))
2760 return -EINVAL;
2762 ctx->cipher_type = CIPHER_TYPE_3DES;
2763 break;
2764 case CIPHER_ALG_AES:
2765 switch (ctx->enckeylen) {
2766 case AES_KEYSIZE_128:
2767 ctx->cipher_type = CIPHER_TYPE_AES128;
2768 break;
2769 case AES_KEYSIZE_192:
2770 ctx->cipher_type = CIPHER_TYPE_AES192;
2771 break;
2772 case AES_KEYSIZE_256:
2773 ctx->cipher_type = CIPHER_TYPE_AES256;
2774 break;
2775 default:
2776 goto badkey;
2778 break;
2779 default:
2780 pr_err("%s() Error: Unknown cipher alg\n", __func__);
2781 return -EINVAL;
2784 flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
2785 ctx->authkeylen);
2786 flow_dump(" enc: ", ctx->enckey, ctx->enckeylen);
2787 flow_dump(" auth: ", ctx->authkey, ctx->authkeylen);
2789 /* setkey the fallback just in case we needto use it */
2790 if (ctx->fallback_cipher) {
2791 flow_log(" running fallback setkey()\n");
2793 ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
2794 ctx->fallback_cipher->base.crt_flags |=
2795 tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
2796 ret = crypto_aead_setkey(ctx->fallback_cipher, key, keylen);
2797 if (ret)
2798 flow_log(" fallback setkey() returned:%d\n", ret);
2801 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
2802 ctx->enckeylen,
2803 false);
2805 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
2807 return ret;
2809 badkey:
2810 ctx->enckeylen = 0;
2811 ctx->authkeylen = 0;
2812 ctx->digestsize = 0;
2814 return -EINVAL;
2817 static int aead_gcm_ccm_setkey(struct crypto_aead *cipher,
2818 const u8 *key, unsigned int keylen)
2820 struct spu_hw *spu = &iproc_priv.spu;
2821 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2822 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
2824 int ret = 0;
2826 flow_log("%s() keylen:%u\n", __func__, keylen);
2827 flow_dump(" key: ", key, keylen);
2829 if (!ctx->is_esp)
2830 ctx->digestsize = keylen;
2832 ctx->enckeylen = keylen;
2833 ctx->authkeylen = 0;
2835 switch (ctx->enckeylen) {
2836 case AES_KEYSIZE_128:
2837 ctx->cipher_type = CIPHER_TYPE_AES128;
2838 break;
2839 case AES_KEYSIZE_192:
2840 ctx->cipher_type = CIPHER_TYPE_AES192;
2841 break;
2842 case AES_KEYSIZE_256:
2843 ctx->cipher_type = CIPHER_TYPE_AES256;
2844 break;
2845 default:
2846 goto badkey;
2849 memcpy(ctx->enckey, key, ctx->enckeylen);
2851 flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
2852 ctx->authkeylen);
2853 flow_dump(" enc: ", ctx->enckey, ctx->enckeylen);
2854 flow_dump(" auth: ", ctx->authkey, ctx->authkeylen);
2856 /* setkey the fallback just in case we need to use it */
2857 if (ctx->fallback_cipher) {
2858 flow_log(" running fallback setkey()\n");
2860 ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
2861 ctx->fallback_cipher->base.crt_flags |=
2862 tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
2863 ret = crypto_aead_setkey(ctx->fallback_cipher, key,
2864 keylen + ctx->salt_len);
2865 if (ret)
2866 flow_log(" fallback setkey() returned:%d\n", ret);
2869 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
2870 ctx->enckeylen,
2871 false);
2873 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
2875 flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
2876 ctx->authkeylen);
2878 return ret;
2880 badkey:
2881 ctx->enckeylen = 0;
2882 ctx->authkeylen = 0;
2883 ctx->digestsize = 0;
2885 return -EINVAL;
2889 * aead_gcm_esp_setkey() - setkey() operation for ESP variant of GCM AES.
2890 * @cipher: AEAD structure
2891 * @key: Key followed by 4 bytes of salt
2892 * @keylen: Length of key plus salt, in bytes
2894 * Extracts salt from key and stores it to be prepended to IV on each request.
2895 * Digest is always 16 bytes
2897 * Return: Value from generic gcm setkey.
2899 static int aead_gcm_esp_setkey(struct crypto_aead *cipher,
2900 const u8 *key, unsigned int keylen)
2902 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2904 flow_log("%s\n", __func__);
2906 if (keylen < GCM_ESP_SALT_SIZE)
2907 return -EINVAL;
2909 ctx->salt_len = GCM_ESP_SALT_SIZE;
2910 ctx->salt_offset = GCM_ESP_SALT_OFFSET;
2911 memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
2912 keylen -= GCM_ESP_SALT_SIZE;
2913 ctx->digestsize = GCM_ESP_DIGESTSIZE;
2914 ctx->is_esp = true;
2915 flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
2917 return aead_gcm_ccm_setkey(cipher, key, keylen);
2921 * rfc4543_gcm_esp_setkey() - setkey operation for RFC4543 variant of GCM/GMAC.
2922 * @cipher: AEAD structure
2923 * @key: Key followed by 4 bytes of salt
2924 * @keylen: Length of key plus salt, in bytes
2926 * Extracts salt from key and stores it to be prepended to IV on each request.
2927 * Digest is always 16 bytes
2929 * Return: Value from generic gcm setkey.
2931 static int rfc4543_gcm_esp_setkey(struct crypto_aead *cipher,
2932 const u8 *key, unsigned int keylen)
2934 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2936 flow_log("%s\n", __func__);
2938 if (keylen < GCM_ESP_SALT_SIZE)
2939 return -EINVAL;
2941 ctx->salt_len = GCM_ESP_SALT_SIZE;
2942 ctx->salt_offset = GCM_ESP_SALT_OFFSET;
2943 memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
2944 keylen -= GCM_ESP_SALT_SIZE;
2945 ctx->digestsize = GCM_ESP_DIGESTSIZE;
2946 ctx->is_esp = true;
2947 ctx->is_rfc4543 = true;
2948 flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
2950 return aead_gcm_ccm_setkey(cipher, key, keylen);
2954 * aead_ccm_esp_setkey() - setkey() operation for ESP variant of CCM AES.
2955 * @cipher: AEAD structure
2956 * @key: Key followed by 4 bytes of salt
2957 * @keylen: Length of key plus salt, in bytes
2959 * Extracts salt from key and stores it to be prepended to IV on each request.
2960 * Digest is always 16 bytes
2962 * Return: Value from generic ccm setkey.
2964 static int aead_ccm_esp_setkey(struct crypto_aead *cipher,
2965 const u8 *key, unsigned int keylen)
2967 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2969 flow_log("%s\n", __func__);
2971 if (keylen < CCM_ESP_SALT_SIZE)
2972 return -EINVAL;
2974 ctx->salt_len = CCM_ESP_SALT_SIZE;
2975 ctx->salt_offset = CCM_ESP_SALT_OFFSET;
2976 memcpy(ctx->salt, key + keylen - CCM_ESP_SALT_SIZE, CCM_ESP_SALT_SIZE);
2977 keylen -= CCM_ESP_SALT_SIZE;
2978 ctx->is_esp = true;
2979 flow_dump("salt: ", ctx->salt, CCM_ESP_SALT_SIZE);
2981 return aead_gcm_ccm_setkey(cipher, key, keylen);
2984 static int aead_setauthsize(struct crypto_aead *cipher, unsigned int authsize)
2986 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2987 int ret = 0;
2989 flow_log("%s() authkeylen:%u authsize:%u\n",
2990 __func__, ctx->authkeylen, authsize);
2992 ctx->digestsize = authsize;
2994 /* setkey the fallback just in case we needto use it */
2995 if (ctx->fallback_cipher) {
2996 flow_log(" running fallback setauth()\n");
2998 ret = crypto_aead_setauthsize(ctx->fallback_cipher, authsize);
2999 if (ret)
3000 flow_log(" fallback setauth() returned:%d\n", ret);
3003 return ret;
3006 static int aead_encrypt(struct aead_request *req)
3008 flow_log("%s() cryptlen:%u %08x\n", __func__, req->cryptlen,
3009 req->cryptlen);
3010 dump_sg(req->src, 0, req->cryptlen + req->assoclen);
3011 flow_log(" assoc_len:%u\n", req->assoclen);
3013 return aead_enqueue(req, true);
3016 static int aead_decrypt(struct aead_request *req)
3018 flow_log("%s() cryptlen:%u\n", __func__, req->cryptlen);
3019 dump_sg(req->src, 0, req->cryptlen + req->assoclen);
3020 flow_log(" assoc_len:%u\n", req->assoclen);
3022 return aead_enqueue(req, false);
3025 /* ==================== Supported Cipher Algorithms ==================== */
3027 static struct iproc_alg_s driver_algs[] = {
3029 .type = CRYPTO_ALG_TYPE_AEAD,
3030 .alg.aead = {
3031 .base = {
3032 .cra_name = "gcm(aes)",
3033 .cra_driver_name = "gcm-aes-iproc",
3034 .cra_blocksize = AES_BLOCK_SIZE,
3035 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3037 .setkey = aead_gcm_ccm_setkey,
3038 .ivsize = GCM_AES_IV_SIZE,
3039 .maxauthsize = AES_BLOCK_SIZE,
3041 .cipher_info = {
3042 .alg = CIPHER_ALG_AES,
3043 .mode = CIPHER_MODE_GCM,
3045 .auth_info = {
3046 .alg = HASH_ALG_AES,
3047 .mode = HASH_MODE_GCM,
3049 .auth_first = 0,
3052 .type = CRYPTO_ALG_TYPE_AEAD,
3053 .alg.aead = {
3054 .base = {
3055 .cra_name = "ccm(aes)",
3056 .cra_driver_name = "ccm-aes-iproc",
3057 .cra_blocksize = AES_BLOCK_SIZE,
3058 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3060 .setkey = aead_gcm_ccm_setkey,
3061 .ivsize = CCM_AES_IV_SIZE,
3062 .maxauthsize = AES_BLOCK_SIZE,
3064 .cipher_info = {
3065 .alg = CIPHER_ALG_AES,
3066 .mode = CIPHER_MODE_CCM,
3068 .auth_info = {
3069 .alg = HASH_ALG_AES,
3070 .mode = HASH_MODE_CCM,
3072 .auth_first = 0,
3075 .type = CRYPTO_ALG_TYPE_AEAD,
3076 .alg.aead = {
3077 .base = {
3078 .cra_name = "rfc4106(gcm(aes))",
3079 .cra_driver_name = "gcm-aes-esp-iproc",
3080 .cra_blocksize = AES_BLOCK_SIZE,
3081 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3083 .setkey = aead_gcm_esp_setkey,
3084 .ivsize = GCM_RFC4106_IV_SIZE,
3085 .maxauthsize = AES_BLOCK_SIZE,
3087 .cipher_info = {
3088 .alg = CIPHER_ALG_AES,
3089 .mode = CIPHER_MODE_GCM,
3091 .auth_info = {
3092 .alg = HASH_ALG_AES,
3093 .mode = HASH_MODE_GCM,
3095 .auth_first = 0,
3098 .type = CRYPTO_ALG_TYPE_AEAD,
3099 .alg.aead = {
3100 .base = {
3101 .cra_name = "rfc4309(ccm(aes))",
3102 .cra_driver_name = "ccm-aes-esp-iproc",
3103 .cra_blocksize = AES_BLOCK_SIZE,
3104 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3106 .setkey = aead_ccm_esp_setkey,
3107 .ivsize = CCM_AES_IV_SIZE,
3108 .maxauthsize = AES_BLOCK_SIZE,
3110 .cipher_info = {
3111 .alg = CIPHER_ALG_AES,
3112 .mode = CIPHER_MODE_CCM,
3114 .auth_info = {
3115 .alg = HASH_ALG_AES,
3116 .mode = HASH_MODE_CCM,
3118 .auth_first = 0,
3121 .type = CRYPTO_ALG_TYPE_AEAD,
3122 .alg.aead = {
3123 .base = {
3124 .cra_name = "rfc4543(gcm(aes))",
3125 .cra_driver_name = "gmac-aes-esp-iproc",
3126 .cra_blocksize = AES_BLOCK_SIZE,
3127 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3129 .setkey = rfc4543_gcm_esp_setkey,
3130 .ivsize = GCM_RFC4106_IV_SIZE,
3131 .maxauthsize = AES_BLOCK_SIZE,
3133 .cipher_info = {
3134 .alg = CIPHER_ALG_AES,
3135 .mode = CIPHER_MODE_GCM,
3137 .auth_info = {
3138 .alg = HASH_ALG_AES,
3139 .mode = HASH_MODE_GCM,
3141 .auth_first = 0,
3144 .type = CRYPTO_ALG_TYPE_AEAD,
3145 .alg.aead = {
3146 .base = {
3147 .cra_name = "authenc(hmac(md5),cbc(aes))",
3148 .cra_driver_name = "authenc-hmac-md5-cbc-aes-iproc",
3149 .cra_blocksize = AES_BLOCK_SIZE,
3150 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3151 CRYPTO_ALG_ASYNC |
3152 CRYPTO_ALG_ALLOCATES_MEMORY
3154 .setkey = aead_authenc_setkey,
3155 .ivsize = AES_BLOCK_SIZE,
3156 .maxauthsize = MD5_DIGEST_SIZE,
3158 .cipher_info = {
3159 .alg = CIPHER_ALG_AES,
3160 .mode = CIPHER_MODE_CBC,
3162 .auth_info = {
3163 .alg = HASH_ALG_MD5,
3164 .mode = HASH_MODE_HMAC,
3166 .auth_first = 0,
3169 .type = CRYPTO_ALG_TYPE_AEAD,
3170 .alg.aead = {
3171 .base = {
3172 .cra_name = "authenc(hmac(sha1),cbc(aes))",
3173 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-iproc",
3174 .cra_blocksize = AES_BLOCK_SIZE,
3175 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3176 CRYPTO_ALG_ASYNC |
3177 CRYPTO_ALG_ALLOCATES_MEMORY
3179 .setkey = aead_authenc_setkey,
3180 .ivsize = AES_BLOCK_SIZE,
3181 .maxauthsize = SHA1_DIGEST_SIZE,
3183 .cipher_info = {
3184 .alg = CIPHER_ALG_AES,
3185 .mode = CIPHER_MODE_CBC,
3187 .auth_info = {
3188 .alg = HASH_ALG_SHA1,
3189 .mode = HASH_MODE_HMAC,
3191 .auth_first = 0,
3194 .type = CRYPTO_ALG_TYPE_AEAD,
3195 .alg.aead = {
3196 .base = {
3197 .cra_name = "authenc(hmac(sha256),cbc(aes))",
3198 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-iproc",
3199 .cra_blocksize = AES_BLOCK_SIZE,
3200 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3201 CRYPTO_ALG_ASYNC |
3202 CRYPTO_ALG_ALLOCATES_MEMORY
3204 .setkey = aead_authenc_setkey,
3205 .ivsize = AES_BLOCK_SIZE,
3206 .maxauthsize = SHA256_DIGEST_SIZE,
3208 .cipher_info = {
3209 .alg = CIPHER_ALG_AES,
3210 .mode = CIPHER_MODE_CBC,
3212 .auth_info = {
3213 .alg = HASH_ALG_SHA256,
3214 .mode = HASH_MODE_HMAC,
3216 .auth_first = 0,
3219 .type = CRYPTO_ALG_TYPE_AEAD,
3220 .alg.aead = {
3221 .base = {
3222 .cra_name = "authenc(hmac(md5),cbc(des))",
3223 .cra_driver_name = "authenc-hmac-md5-cbc-des-iproc",
3224 .cra_blocksize = DES_BLOCK_SIZE,
3225 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3226 CRYPTO_ALG_ASYNC |
3227 CRYPTO_ALG_ALLOCATES_MEMORY
3229 .setkey = aead_authenc_setkey,
3230 .ivsize = DES_BLOCK_SIZE,
3231 .maxauthsize = MD5_DIGEST_SIZE,
3233 .cipher_info = {
3234 .alg = CIPHER_ALG_DES,
3235 .mode = CIPHER_MODE_CBC,
3237 .auth_info = {
3238 .alg = HASH_ALG_MD5,
3239 .mode = HASH_MODE_HMAC,
3241 .auth_first = 0,
3244 .type = CRYPTO_ALG_TYPE_AEAD,
3245 .alg.aead = {
3246 .base = {
3247 .cra_name = "authenc(hmac(sha1),cbc(des))",
3248 .cra_driver_name = "authenc-hmac-sha1-cbc-des-iproc",
3249 .cra_blocksize = DES_BLOCK_SIZE,
3250 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3251 CRYPTO_ALG_ASYNC |
3252 CRYPTO_ALG_ALLOCATES_MEMORY
3254 .setkey = aead_authenc_setkey,
3255 .ivsize = DES_BLOCK_SIZE,
3256 .maxauthsize = SHA1_DIGEST_SIZE,
3258 .cipher_info = {
3259 .alg = CIPHER_ALG_DES,
3260 .mode = CIPHER_MODE_CBC,
3262 .auth_info = {
3263 .alg = HASH_ALG_SHA1,
3264 .mode = HASH_MODE_HMAC,
3266 .auth_first = 0,
3269 .type = CRYPTO_ALG_TYPE_AEAD,
3270 .alg.aead = {
3271 .base = {
3272 .cra_name = "authenc(hmac(sha224),cbc(des))",
3273 .cra_driver_name = "authenc-hmac-sha224-cbc-des-iproc",
3274 .cra_blocksize = DES_BLOCK_SIZE,
3275 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3276 CRYPTO_ALG_ASYNC |
3277 CRYPTO_ALG_ALLOCATES_MEMORY
3279 .setkey = aead_authenc_setkey,
3280 .ivsize = DES_BLOCK_SIZE,
3281 .maxauthsize = SHA224_DIGEST_SIZE,
3283 .cipher_info = {
3284 .alg = CIPHER_ALG_DES,
3285 .mode = CIPHER_MODE_CBC,
3287 .auth_info = {
3288 .alg = HASH_ALG_SHA224,
3289 .mode = HASH_MODE_HMAC,
3291 .auth_first = 0,
3294 .type = CRYPTO_ALG_TYPE_AEAD,
3295 .alg.aead = {
3296 .base = {
3297 .cra_name = "authenc(hmac(sha256),cbc(des))",
3298 .cra_driver_name = "authenc-hmac-sha256-cbc-des-iproc",
3299 .cra_blocksize = DES_BLOCK_SIZE,
3300 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3301 CRYPTO_ALG_ASYNC |
3302 CRYPTO_ALG_ALLOCATES_MEMORY
3304 .setkey = aead_authenc_setkey,
3305 .ivsize = DES_BLOCK_SIZE,
3306 .maxauthsize = SHA256_DIGEST_SIZE,
3308 .cipher_info = {
3309 .alg = CIPHER_ALG_DES,
3310 .mode = CIPHER_MODE_CBC,
3312 .auth_info = {
3313 .alg = HASH_ALG_SHA256,
3314 .mode = HASH_MODE_HMAC,
3316 .auth_first = 0,
3319 .type = CRYPTO_ALG_TYPE_AEAD,
3320 .alg.aead = {
3321 .base = {
3322 .cra_name = "authenc(hmac(sha384),cbc(des))",
3323 .cra_driver_name = "authenc-hmac-sha384-cbc-des-iproc",
3324 .cra_blocksize = DES_BLOCK_SIZE,
3325 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3326 CRYPTO_ALG_ASYNC |
3327 CRYPTO_ALG_ALLOCATES_MEMORY
3329 .setkey = aead_authenc_setkey,
3330 .ivsize = DES_BLOCK_SIZE,
3331 .maxauthsize = SHA384_DIGEST_SIZE,
3333 .cipher_info = {
3334 .alg = CIPHER_ALG_DES,
3335 .mode = CIPHER_MODE_CBC,
3337 .auth_info = {
3338 .alg = HASH_ALG_SHA384,
3339 .mode = HASH_MODE_HMAC,
3341 .auth_first = 0,
3344 .type = CRYPTO_ALG_TYPE_AEAD,
3345 .alg.aead = {
3346 .base = {
3347 .cra_name = "authenc(hmac(sha512),cbc(des))",
3348 .cra_driver_name = "authenc-hmac-sha512-cbc-des-iproc",
3349 .cra_blocksize = DES_BLOCK_SIZE,
3350 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3351 CRYPTO_ALG_ASYNC |
3352 CRYPTO_ALG_ALLOCATES_MEMORY
3354 .setkey = aead_authenc_setkey,
3355 .ivsize = DES_BLOCK_SIZE,
3356 .maxauthsize = SHA512_DIGEST_SIZE,
3358 .cipher_info = {
3359 .alg = CIPHER_ALG_DES,
3360 .mode = CIPHER_MODE_CBC,
3362 .auth_info = {
3363 .alg = HASH_ALG_SHA512,
3364 .mode = HASH_MODE_HMAC,
3366 .auth_first = 0,
3369 .type = CRYPTO_ALG_TYPE_AEAD,
3370 .alg.aead = {
3371 .base = {
3372 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
3373 .cra_driver_name = "authenc-hmac-md5-cbc-des3-iproc",
3374 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3375 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3376 CRYPTO_ALG_ASYNC |
3377 CRYPTO_ALG_ALLOCATES_MEMORY
3379 .setkey = aead_authenc_setkey,
3380 .ivsize = DES3_EDE_BLOCK_SIZE,
3381 .maxauthsize = MD5_DIGEST_SIZE,
3383 .cipher_info = {
3384 .alg = CIPHER_ALG_3DES,
3385 .mode = CIPHER_MODE_CBC,
3387 .auth_info = {
3388 .alg = HASH_ALG_MD5,
3389 .mode = HASH_MODE_HMAC,
3391 .auth_first = 0,
3394 .type = CRYPTO_ALG_TYPE_AEAD,
3395 .alg.aead = {
3396 .base = {
3397 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
3398 .cra_driver_name = "authenc-hmac-sha1-cbc-des3-iproc",
3399 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3400 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3401 CRYPTO_ALG_ASYNC |
3402 CRYPTO_ALG_ALLOCATES_MEMORY
3404 .setkey = aead_authenc_setkey,
3405 .ivsize = DES3_EDE_BLOCK_SIZE,
3406 .maxauthsize = SHA1_DIGEST_SIZE,
3408 .cipher_info = {
3409 .alg = CIPHER_ALG_3DES,
3410 .mode = CIPHER_MODE_CBC,
3412 .auth_info = {
3413 .alg = HASH_ALG_SHA1,
3414 .mode = HASH_MODE_HMAC,
3416 .auth_first = 0,
3419 .type = CRYPTO_ALG_TYPE_AEAD,
3420 .alg.aead = {
3421 .base = {
3422 .cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
3423 .cra_driver_name = "authenc-hmac-sha224-cbc-des3-iproc",
3424 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3425 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3426 CRYPTO_ALG_ASYNC |
3427 CRYPTO_ALG_ALLOCATES_MEMORY
3429 .setkey = aead_authenc_setkey,
3430 .ivsize = DES3_EDE_BLOCK_SIZE,
3431 .maxauthsize = SHA224_DIGEST_SIZE,
3433 .cipher_info = {
3434 .alg = CIPHER_ALG_3DES,
3435 .mode = CIPHER_MODE_CBC,
3437 .auth_info = {
3438 .alg = HASH_ALG_SHA224,
3439 .mode = HASH_MODE_HMAC,
3441 .auth_first = 0,
3444 .type = CRYPTO_ALG_TYPE_AEAD,
3445 .alg.aead = {
3446 .base = {
3447 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
3448 .cra_driver_name = "authenc-hmac-sha256-cbc-des3-iproc",
3449 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3450 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3451 CRYPTO_ALG_ASYNC |
3452 CRYPTO_ALG_ALLOCATES_MEMORY
3454 .setkey = aead_authenc_setkey,
3455 .ivsize = DES3_EDE_BLOCK_SIZE,
3456 .maxauthsize = SHA256_DIGEST_SIZE,
3458 .cipher_info = {
3459 .alg = CIPHER_ALG_3DES,
3460 .mode = CIPHER_MODE_CBC,
3462 .auth_info = {
3463 .alg = HASH_ALG_SHA256,
3464 .mode = HASH_MODE_HMAC,
3466 .auth_first = 0,
3469 .type = CRYPTO_ALG_TYPE_AEAD,
3470 .alg.aead = {
3471 .base = {
3472 .cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
3473 .cra_driver_name = "authenc-hmac-sha384-cbc-des3-iproc",
3474 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3475 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3476 CRYPTO_ALG_ASYNC |
3477 CRYPTO_ALG_ALLOCATES_MEMORY
3479 .setkey = aead_authenc_setkey,
3480 .ivsize = DES3_EDE_BLOCK_SIZE,
3481 .maxauthsize = SHA384_DIGEST_SIZE,
3483 .cipher_info = {
3484 .alg = CIPHER_ALG_3DES,
3485 .mode = CIPHER_MODE_CBC,
3487 .auth_info = {
3488 .alg = HASH_ALG_SHA384,
3489 .mode = HASH_MODE_HMAC,
3491 .auth_first = 0,
3494 .type = CRYPTO_ALG_TYPE_AEAD,
3495 .alg.aead = {
3496 .base = {
3497 .cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
3498 .cra_driver_name = "authenc-hmac-sha512-cbc-des3-iproc",
3499 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3500 .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
3501 CRYPTO_ALG_ASYNC |
3502 CRYPTO_ALG_ALLOCATES_MEMORY
3504 .setkey = aead_authenc_setkey,
3505 .ivsize = DES3_EDE_BLOCK_SIZE,
3506 .maxauthsize = SHA512_DIGEST_SIZE,
3508 .cipher_info = {
3509 .alg = CIPHER_ALG_3DES,
3510 .mode = CIPHER_MODE_CBC,
3512 .auth_info = {
3513 .alg = HASH_ALG_SHA512,
3514 .mode = HASH_MODE_HMAC,
3516 .auth_first = 0,
3519 /* SKCIPHER algorithms. */
3521 .type = CRYPTO_ALG_TYPE_SKCIPHER,
3522 .alg.skcipher = {
3523 .base.cra_name = "cbc(des)",
3524 .base.cra_driver_name = "cbc-des-iproc",
3525 .base.cra_blocksize = DES_BLOCK_SIZE,
3526 .min_keysize = DES_KEY_SIZE,
3527 .max_keysize = DES_KEY_SIZE,
3528 .ivsize = DES_BLOCK_SIZE,
3530 .cipher_info = {
3531 .alg = CIPHER_ALG_DES,
3532 .mode = CIPHER_MODE_CBC,
3534 .auth_info = {
3535 .alg = HASH_ALG_NONE,
3536 .mode = HASH_MODE_NONE,
3540 .type = CRYPTO_ALG_TYPE_SKCIPHER,
3541 .alg.skcipher = {
3542 .base.cra_name = "ecb(des)",
3543 .base.cra_driver_name = "ecb-des-iproc",
3544 .base.cra_blocksize = DES_BLOCK_SIZE,
3545 .min_keysize = DES_KEY_SIZE,
3546 .max_keysize = DES_KEY_SIZE,
3547 .ivsize = 0,
3549 .cipher_info = {
3550 .alg = CIPHER_ALG_DES,
3551 .mode = CIPHER_MODE_ECB,
3553 .auth_info = {
3554 .alg = HASH_ALG_NONE,
3555 .mode = HASH_MODE_NONE,
3559 .type = CRYPTO_ALG_TYPE_SKCIPHER,
3560 .alg.skcipher = {
3561 .base.cra_name = "cbc(des3_ede)",
3562 .base.cra_driver_name = "cbc-des3-iproc",
3563 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
3564 .min_keysize = DES3_EDE_KEY_SIZE,
3565 .max_keysize = DES3_EDE_KEY_SIZE,
3566 .ivsize = DES3_EDE_BLOCK_SIZE,
3568 .cipher_info = {
3569 .alg = CIPHER_ALG_3DES,
3570 .mode = CIPHER_MODE_CBC,
3572 .auth_info = {
3573 .alg = HASH_ALG_NONE,
3574 .mode = HASH_MODE_NONE,
3578 .type = CRYPTO_ALG_TYPE_SKCIPHER,
3579 .alg.skcipher = {
3580 .base.cra_name = "ecb(des3_ede)",
3581 .base.cra_driver_name = "ecb-des3-iproc",
3582 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
3583 .min_keysize = DES3_EDE_KEY_SIZE,
3584 .max_keysize = DES3_EDE_KEY_SIZE,
3585 .ivsize = 0,
3587 .cipher_info = {
3588 .alg = CIPHER_ALG_3DES,
3589 .mode = CIPHER_MODE_ECB,
3591 .auth_info = {
3592 .alg = HASH_ALG_NONE,
3593 .mode = HASH_MODE_NONE,
3597 .type = CRYPTO_ALG_TYPE_SKCIPHER,
3598 .alg.skcipher = {
3599 .base.cra_name = "cbc(aes)",
3600 .base.cra_driver_name = "cbc-aes-iproc",
3601 .base.cra_blocksize = AES_BLOCK_SIZE,
3602 .min_keysize = AES_MIN_KEY_SIZE,
3603 .max_keysize = AES_MAX_KEY_SIZE,
3604 .ivsize = AES_BLOCK_SIZE,
3606 .cipher_info = {
3607 .alg = CIPHER_ALG_AES,
3608 .mode = CIPHER_MODE_CBC,
3610 .auth_info = {
3611 .alg = HASH_ALG_NONE,
3612 .mode = HASH_MODE_NONE,
3616 .type = CRYPTO_ALG_TYPE_SKCIPHER,
3617 .alg.skcipher = {
3618 .base.cra_name = "ecb(aes)",
3619 .base.cra_driver_name = "ecb-aes-iproc",
3620 .base.cra_blocksize = AES_BLOCK_SIZE,
3621 .min_keysize = AES_MIN_KEY_SIZE,
3622 .max_keysize = AES_MAX_KEY_SIZE,
3623 .ivsize = 0,
3625 .cipher_info = {
3626 .alg = CIPHER_ALG_AES,
3627 .mode = CIPHER_MODE_ECB,
3629 .auth_info = {
3630 .alg = HASH_ALG_NONE,
3631 .mode = HASH_MODE_NONE,
3635 .type = CRYPTO_ALG_TYPE_SKCIPHER,
3636 .alg.skcipher = {
3637 .base.cra_name = "ctr(aes)",
3638 .base.cra_driver_name = "ctr-aes-iproc",
3639 .base.cra_blocksize = AES_BLOCK_SIZE,
3640 .min_keysize = AES_MIN_KEY_SIZE,
3641 .max_keysize = AES_MAX_KEY_SIZE,
3642 .ivsize = AES_BLOCK_SIZE,
3644 .cipher_info = {
3645 .alg = CIPHER_ALG_AES,
3646 .mode = CIPHER_MODE_CTR,
3648 .auth_info = {
3649 .alg = HASH_ALG_NONE,
3650 .mode = HASH_MODE_NONE,
3654 .type = CRYPTO_ALG_TYPE_SKCIPHER,
3655 .alg.skcipher = {
3656 .base.cra_name = "xts(aes)",
3657 .base.cra_driver_name = "xts-aes-iproc",
3658 .base.cra_blocksize = AES_BLOCK_SIZE,
3659 .min_keysize = 2 * AES_MIN_KEY_SIZE,
3660 .max_keysize = 2 * AES_MAX_KEY_SIZE,
3661 .ivsize = AES_BLOCK_SIZE,
3663 .cipher_info = {
3664 .alg = CIPHER_ALG_AES,
3665 .mode = CIPHER_MODE_XTS,
3667 .auth_info = {
3668 .alg = HASH_ALG_NONE,
3669 .mode = HASH_MODE_NONE,
3673 /* AHASH algorithms. */
3675 .type = CRYPTO_ALG_TYPE_AHASH,
3676 .alg.hash = {
3677 .halg.digestsize = MD5_DIGEST_SIZE,
3678 .halg.base = {
3679 .cra_name = "md5",
3680 .cra_driver_name = "md5-iproc",
3681 .cra_blocksize = MD5_BLOCK_WORDS * 4,
3682 .cra_flags = CRYPTO_ALG_ASYNC |
3683 CRYPTO_ALG_ALLOCATES_MEMORY,
3686 .cipher_info = {
3687 .alg = CIPHER_ALG_NONE,
3688 .mode = CIPHER_MODE_NONE,
3690 .auth_info = {
3691 .alg = HASH_ALG_MD5,
3692 .mode = HASH_MODE_HASH,
3696 .type = CRYPTO_ALG_TYPE_AHASH,
3697 .alg.hash = {
3698 .halg.digestsize = MD5_DIGEST_SIZE,
3699 .halg.base = {
3700 .cra_name = "hmac(md5)",
3701 .cra_driver_name = "hmac-md5-iproc",
3702 .cra_blocksize = MD5_BLOCK_WORDS * 4,
3705 .cipher_info = {
3706 .alg = CIPHER_ALG_NONE,
3707 .mode = CIPHER_MODE_NONE,
3709 .auth_info = {
3710 .alg = HASH_ALG_MD5,
3711 .mode = HASH_MODE_HMAC,
3714 {.type = CRYPTO_ALG_TYPE_AHASH,
3715 .alg.hash = {
3716 .halg.digestsize = SHA1_DIGEST_SIZE,
3717 .halg.base = {
3718 .cra_name = "sha1",
3719 .cra_driver_name = "sha1-iproc",
3720 .cra_blocksize = SHA1_BLOCK_SIZE,
3723 .cipher_info = {
3724 .alg = CIPHER_ALG_NONE,
3725 .mode = CIPHER_MODE_NONE,
3727 .auth_info = {
3728 .alg = HASH_ALG_SHA1,
3729 .mode = HASH_MODE_HASH,
3732 {.type = CRYPTO_ALG_TYPE_AHASH,
3733 .alg.hash = {
3734 .halg.digestsize = SHA1_DIGEST_SIZE,
3735 .halg.base = {
3736 .cra_name = "hmac(sha1)",
3737 .cra_driver_name = "hmac-sha1-iproc",
3738 .cra_blocksize = SHA1_BLOCK_SIZE,
3741 .cipher_info = {
3742 .alg = CIPHER_ALG_NONE,
3743 .mode = CIPHER_MODE_NONE,
3745 .auth_info = {
3746 .alg = HASH_ALG_SHA1,
3747 .mode = HASH_MODE_HMAC,
3750 {.type = CRYPTO_ALG_TYPE_AHASH,
3751 .alg.hash = {
3752 .halg.digestsize = SHA224_DIGEST_SIZE,
3753 .halg.base = {
3754 .cra_name = "sha224",
3755 .cra_driver_name = "sha224-iproc",
3756 .cra_blocksize = SHA224_BLOCK_SIZE,
3759 .cipher_info = {
3760 .alg = CIPHER_ALG_NONE,
3761 .mode = CIPHER_MODE_NONE,
3763 .auth_info = {
3764 .alg = HASH_ALG_SHA224,
3765 .mode = HASH_MODE_HASH,
3768 {.type = CRYPTO_ALG_TYPE_AHASH,
3769 .alg.hash = {
3770 .halg.digestsize = SHA224_DIGEST_SIZE,
3771 .halg.base = {
3772 .cra_name = "hmac(sha224)",
3773 .cra_driver_name = "hmac-sha224-iproc",
3774 .cra_blocksize = SHA224_BLOCK_SIZE,
3777 .cipher_info = {
3778 .alg = CIPHER_ALG_NONE,
3779 .mode = CIPHER_MODE_NONE,
3781 .auth_info = {
3782 .alg = HASH_ALG_SHA224,
3783 .mode = HASH_MODE_HMAC,
3786 {.type = CRYPTO_ALG_TYPE_AHASH,
3787 .alg.hash = {
3788 .halg.digestsize = SHA256_DIGEST_SIZE,
3789 .halg.base = {
3790 .cra_name = "sha256",
3791 .cra_driver_name = "sha256-iproc",
3792 .cra_blocksize = SHA256_BLOCK_SIZE,
3795 .cipher_info = {
3796 .alg = CIPHER_ALG_NONE,
3797 .mode = CIPHER_MODE_NONE,
3799 .auth_info = {
3800 .alg = HASH_ALG_SHA256,
3801 .mode = HASH_MODE_HASH,
3804 {.type = CRYPTO_ALG_TYPE_AHASH,
3805 .alg.hash = {
3806 .halg.digestsize = SHA256_DIGEST_SIZE,
3807 .halg.base = {
3808 .cra_name = "hmac(sha256)",
3809 .cra_driver_name = "hmac-sha256-iproc",
3810 .cra_blocksize = SHA256_BLOCK_SIZE,
3813 .cipher_info = {
3814 .alg = CIPHER_ALG_NONE,
3815 .mode = CIPHER_MODE_NONE,
3817 .auth_info = {
3818 .alg = HASH_ALG_SHA256,
3819 .mode = HASH_MODE_HMAC,
3823 .type = CRYPTO_ALG_TYPE_AHASH,
3824 .alg.hash = {
3825 .halg.digestsize = SHA384_DIGEST_SIZE,
3826 .halg.base = {
3827 .cra_name = "sha384",
3828 .cra_driver_name = "sha384-iproc",
3829 .cra_blocksize = SHA384_BLOCK_SIZE,
3832 .cipher_info = {
3833 .alg = CIPHER_ALG_NONE,
3834 .mode = CIPHER_MODE_NONE,
3836 .auth_info = {
3837 .alg = HASH_ALG_SHA384,
3838 .mode = HASH_MODE_HASH,
3842 .type = CRYPTO_ALG_TYPE_AHASH,
3843 .alg.hash = {
3844 .halg.digestsize = SHA384_DIGEST_SIZE,
3845 .halg.base = {
3846 .cra_name = "hmac(sha384)",
3847 .cra_driver_name = "hmac-sha384-iproc",
3848 .cra_blocksize = SHA384_BLOCK_SIZE,
3851 .cipher_info = {
3852 .alg = CIPHER_ALG_NONE,
3853 .mode = CIPHER_MODE_NONE,
3855 .auth_info = {
3856 .alg = HASH_ALG_SHA384,
3857 .mode = HASH_MODE_HMAC,
3861 .type = CRYPTO_ALG_TYPE_AHASH,
3862 .alg.hash = {
3863 .halg.digestsize = SHA512_DIGEST_SIZE,
3864 .halg.base = {
3865 .cra_name = "sha512",
3866 .cra_driver_name = "sha512-iproc",
3867 .cra_blocksize = SHA512_BLOCK_SIZE,
3870 .cipher_info = {
3871 .alg = CIPHER_ALG_NONE,
3872 .mode = CIPHER_MODE_NONE,
3874 .auth_info = {
3875 .alg = HASH_ALG_SHA512,
3876 .mode = HASH_MODE_HASH,
3880 .type = CRYPTO_ALG_TYPE_AHASH,
3881 .alg.hash = {
3882 .halg.digestsize = SHA512_DIGEST_SIZE,
3883 .halg.base = {
3884 .cra_name = "hmac(sha512)",
3885 .cra_driver_name = "hmac-sha512-iproc",
3886 .cra_blocksize = SHA512_BLOCK_SIZE,
3889 .cipher_info = {
3890 .alg = CIPHER_ALG_NONE,
3891 .mode = CIPHER_MODE_NONE,
3893 .auth_info = {
3894 .alg = HASH_ALG_SHA512,
3895 .mode = HASH_MODE_HMAC,
3899 .type = CRYPTO_ALG_TYPE_AHASH,
3900 .alg.hash = {
3901 .halg.digestsize = SHA3_224_DIGEST_SIZE,
3902 .halg.base = {
3903 .cra_name = "sha3-224",
3904 .cra_driver_name = "sha3-224-iproc",
3905 .cra_blocksize = SHA3_224_BLOCK_SIZE,
3908 .cipher_info = {
3909 .alg = CIPHER_ALG_NONE,
3910 .mode = CIPHER_MODE_NONE,
3912 .auth_info = {
3913 .alg = HASH_ALG_SHA3_224,
3914 .mode = HASH_MODE_HASH,
3918 .type = CRYPTO_ALG_TYPE_AHASH,
3919 .alg.hash = {
3920 .halg.digestsize = SHA3_224_DIGEST_SIZE,
3921 .halg.base = {
3922 .cra_name = "hmac(sha3-224)",
3923 .cra_driver_name = "hmac-sha3-224-iproc",
3924 .cra_blocksize = SHA3_224_BLOCK_SIZE,
3927 .cipher_info = {
3928 .alg = CIPHER_ALG_NONE,
3929 .mode = CIPHER_MODE_NONE,
3931 .auth_info = {
3932 .alg = HASH_ALG_SHA3_224,
3933 .mode = HASH_MODE_HMAC
3937 .type = CRYPTO_ALG_TYPE_AHASH,
3938 .alg.hash = {
3939 .halg.digestsize = SHA3_256_DIGEST_SIZE,
3940 .halg.base = {
3941 .cra_name = "sha3-256",
3942 .cra_driver_name = "sha3-256-iproc",
3943 .cra_blocksize = SHA3_256_BLOCK_SIZE,
3946 .cipher_info = {
3947 .alg = CIPHER_ALG_NONE,
3948 .mode = CIPHER_MODE_NONE,
3950 .auth_info = {
3951 .alg = HASH_ALG_SHA3_256,
3952 .mode = HASH_MODE_HASH,
3956 .type = CRYPTO_ALG_TYPE_AHASH,
3957 .alg.hash = {
3958 .halg.digestsize = SHA3_256_DIGEST_SIZE,
3959 .halg.base = {
3960 .cra_name = "hmac(sha3-256)",
3961 .cra_driver_name = "hmac-sha3-256-iproc",
3962 .cra_blocksize = SHA3_256_BLOCK_SIZE,
3965 .cipher_info = {
3966 .alg = CIPHER_ALG_NONE,
3967 .mode = CIPHER_MODE_NONE,
3969 .auth_info = {
3970 .alg = HASH_ALG_SHA3_256,
3971 .mode = HASH_MODE_HMAC,
3975 .type = CRYPTO_ALG_TYPE_AHASH,
3976 .alg.hash = {
3977 .halg.digestsize = SHA3_384_DIGEST_SIZE,
3978 .halg.base = {
3979 .cra_name = "sha3-384",
3980 .cra_driver_name = "sha3-384-iproc",
3981 .cra_blocksize = SHA3_224_BLOCK_SIZE,
3984 .cipher_info = {
3985 .alg = CIPHER_ALG_NONE,
3986 .mode = CIPHER_MODE_NONE,
3988 .auth_info = {
3989 .alg = HASH_ALG_SHA3_384,
3990 .mode = HASH_MODE_HASH,
3994 .type = CRYPTO_ALG_TYPE_AHASH,
3995 .alg.hash = {
3996 .halg.digestsize = SHA3_384_DIGEST_SIZE,
3997 .halg.base = {
3998 .cra_name = "hmac(sha3-384)",
3999 .cra_driver_name = "hmac-sha3-384-iproc",
4000 .cra_blocksize = SHA3_384_BLOCK_SIZE,
4003 .cipher_info = {
4004 .alg = CIPHER_ALG_NONE,
4005 .mode = CIPHER_MODE_NONE,
4007 .auth_info = {
4008 .alg = HASH_ALG_SHA3_384,
4009 .mode = HASH_MODE_HMAC,
4013 .type = CRYPTO_ALG_TYPE_AHASH,
4014 .alg.hash = {
4015 .halg.digestsize = SHA3_512_DIGEST_SIZE,
4016 .halg.base = {
4017 .cra_name = "sha3-512",
4018 .cra_driver_name = "sha3-512-iproc",
4019 .cra_blocksize = SHA3_512_BLOCK_SIZE,
4022 .cipher_info = {
4023 .alg = CIPHER_ALG_NONE,
4024 .mode = CIPHER_MODE_NONE,
4026 .auth_info = {
4027 .alg = HASH_ALG_SHA3_512,
4028 .mode = HASH_MODE_HASH,
4032 .type = CRYPTO_ALG_TYPE_AHASH,
4033 .alg.hash = {
4034 .halg.digestsize = SHA3_512_DIGEST_SIZE,
4035 .halg.base = {
4036 .cra_name = "hmac(sha3-512)",
4037 .cra_driver_name = "hmac-sha3-512-iproc",
4038 .cra_blocksize = SHA3_512_BLOCK_SIZE,
4041 .cipher_info = {
4042 .alg = CIPHER_ALG_NONE,
4043 .mode = CIPHER_MODE_NONE,
4045 .auth_info = {
4046 .alg = HASH_ALG_SHA3_512,
4047 .mode = HASH_MODE_HMAC,
4051 .type = CRYPTO_ALG_TYPE_AHASH,
4052 .alg.hash = {
4053 .halg.digestsize = AES_BLOCK_SIZE,
4054 .halg.base = {
4055 .cra_name = "xcbc(aes)",
4056 .cra_driver_name = "xcbc-aes-iproc",
4057 .cra_blocksize = AES_BLOCK_SIZE,
4060 .cipher_info = {
4061 .alg = CIPHER_ALG_NONE,
4062 .mode = CIPHER_MODE_NONE,
4064 .auth_info = {
4065 .alg = HASH_ALG_AES,
4066 .mode = HASH_MODE_XCBC,
4070 .type = CRYPTO_ALG_TYPE_AHASH,
4071 .alg.hash = {
4072 .halg.digestsize = AES_BLOCK_SIZE,
4073 .halg.base = {
4074 .cra_name = "cmac(aes)",
4075 .cra_driver_name = "cmac-aes-iproc",
4076 .cra_blocksize = AES_BLOCK_SIZE,
4079 .cipher_info = {
4080 .alg = CIPHER_ALG_NONE,
4081 .mode = CIPHER_MODE_NONE,
4083 .auth_info = {
4084 .alg = HASH_ALG_AES,
4085 .mode = HASH_MODE_CMAC,
4090 static int generic_cra_init(struct crypto_tfm *tfm,
4091 struct iproc_alg_s *cipher_alg)
4093 struct spu_hw *spu = &iproc_priv.spu;
4094 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4095 unsigned int blocksize = crypto_tfm_alg_blocksize(tfm);
4097 flow_log("%s()\n", __func__);
4099 ctx->alg = cipher_alg;
4100 ctx->cipher = cipher_alg->cipher_info;
4101 ctx->auth = cipher_alg->auth_info;
4102 ctx->auth_first = cipher_alg->auth_first;
4103 ctx->max_payload = spu->spu_ctx_max_payload(ctx->cipher.alg,
4104 ctx->cipher.mode,
4105 blocksize);
4106 ctx->fallback_cipher = NULL;
4108 ctx->enckeylen = 0;
4109 ctx->authkeylen = 0;
4111 atomic_inc(&iproc_priv.stream_count);
4112 atomic_inc(&iproc_priv.session_count);
4114 return 0;
4117 static int skcipher_init_tfm(struct crypto_skcipher *skcipher)
4119 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
4120 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
4121 struct iproc_alg_s *cipher_alg;
4123 flow_log("%s()\n", __func__);
4125 crypto_skcipher_set_reqsize(skcipher, sizeof(struct iproc_reqctx_s));
4127 cipher_alg = container_of(alg, struct iproc_alg_s, alg.skcipher);
4128 return generic_cra_init(tfm, cipher_alg);
4131 static int ahash_cra_init(struct crypto_tfm *tfm)
4133 int err;
4134 struct crypto_alg *alg = tfm->__crt_alg;
4135 struct iproc_alg_s *cipher_alg;
4137 cipher_alg = container_of(__crypto_ahash_alg(alg), struct iproc_alg_s,
4138 alg.hash);
4140 err = generic_cra_init(tfm, cipher_alg);
4141 flow_log("%s()\n", __func__);
4144 * export state size has to be < 512 bytes. So don't include msg bufs
4145 * in state size.
4147 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
4148 sizeof(struct iproc_reqctx_s));
4150 return err;
4153 static int aead_cra_init(struct crypto_aead *aead)
4155 unsigned int reqsize = sizeof(struct iproc_reqctx_s);
4156 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
4157 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4158 struct crypto_alg *alg = tfm->__crt_alg;
4159 struct aead_alg *aalg = container_of(alg, struct aead_alg, base);
4160 struct iproc_alg_s *cipher_alg = container_of(aalg, struct iproc_alg_s,
4161 alg.aead);
4163 int err = generic_cra_init(tfm, cipher_alg);
4165 flow_log("%s()\n", __func__);
4167 ctx->is_esp = false;
4168 ctx->salt_len = 0;
4169 ctx->salt_offset = 0;
4171 /* random first IV */
4172 get_random_bytes(ctx->iv, MAX_IV_SIZE);
4173 flow_dump(" iv: ", ctx->iv, MAX_IV_SIZE);
4175 if (err)
4176 goto out;
4178 if (!(alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK))
4179 goto reqsize;
4181 flow_log("%s() creating fallback cipher\n", __func__);
4183 ctx->fallback_cipher = crypto_alloc_aead(alg->cra_name, 0,
4184 CRYPTO_ALG_ASYNC |
4185 CRYPTO_ALG_NEED_FALLBACK);
4186 if (IS_ERR(ctx->fallback_cipher)) {
4187 pr_err("%s() Error: failed to allocate fallback for %s\n",
4188 __func__, alg->cra_name);
4189 return PTR_ERR(ctx->fallback_cipher);
4192 reqsize += crypto_aead_reqsize(ctx->fallback_cipher);
4194 reqsize:
4195 crypto_aead_set_reqsize(aead, reqsize);
4197 out:
4198 return err;
4201 static void generic_cra_exit(struct crypto_tfm *tfm)
4203 atomic_dec(&iproc_priv.session_count);
4206 static void skcipher_exit_tfm(struct crypto_skcipher *tfm)
4208 generic_cra_exit(crypto_skcipher_tfm(tfm));
4211 static void aead_cra_exit(struct crypto_aead *aead)
4213 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
4214 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4216 generic_cra_exit(tfm);
4218 if (ctx->fallback_cipher) {
4219 crypto_free_aead(ctx->fallback_cipher);
4220 ctx->fallback_cipher = NULL;
4225 * spu_functions_register() - Specify hardware-specific SPU functions based on
4226 * SPU type read from device tree.
4227 * @dev: device structure
4228 * @spu_type: SPU hardware generation
4229 * @spu_subtype: SPU hardware version
4231 static void spu_functions_register(struct device *dev,
4232 enum spu_spu_type spu_type,
4233 enum spu_spu_subtype spu_subtype)
4235 struct spu_hw *spu = &iproc_priv.spu;
4237 if (spu_type == SPU_TYPE_SPUM) {
4238 dev_dbg(dev, "Registering SPUM functions");
4239 spu->spu_dump_msg_hdr = spum_dump_msg_hdr;
4240 spu->spu_payload_length = spum_payload_length;
4241 spu->spu_response_hdr_len = spum_response_hdr_len;
4242 spu->spu_hash_pad_len = spum_hash_pad_len;
4243 spu->spu_gcm_ccm_pad_len = spum_gcm_ccm_pad_len;
4244 spu->spu_assoc_resp_len = spum_assoc_resp_len;
4245 spu->spu_aead_ivlen = spum_aead_ivlen;
4246 spu->spu_hash_type = spum_hash_type;
4247 spu->spu_digest_size = spum_digest_size;
4248 spu->spu_create_request = spum_create_request;
4249 spu->spu_cipher_req_init = spum_cipher_req_init;
4250 spu->spu_cipher_req_finish = spum_cipher_req_finish;
4251 spu->spu_request_pad = spum_request_pad;
4252 spu->spu_tx_status_len = spum_tx_status_len;
4253 spu->spu_rx_status_len = spum_rx_status_len;
4254 spu->spu_status_process = spum_status_process;
4255 spu->spu_xts_tweak_in_payload = spum_xts_tweak_in_payload;
4256 spu->spu_ccm_update_iv = spum_ccm_update_iv;
4257 spu->spu_wordalign_padlen = spum_wordalign_padlen;
4258 if (spu_subtype == SPU_SUBTYPE_SPUM_NS2)
4259 spu->spu_ctx_max_payload = spum_ns2_ctx_max_payload;
4260 else
4261 spu->spu_ctx_max_payload = spum_nsp_ctx_max_payload;
4262 } else {
4263 dev_dbg(dev, "Registering SPU2 functions");
4264 spu->spu_dump_msg_hdr = spu2_dump_msg_hdr;
4265 spu->spu_ctx_max_payload = spu2_ctx_max_payload;
4266 spu->spu_payload_length = spu2_payload_length;
4267 spu->spu_response_hdr_len = spu2_response_hdr_len;
4268 spu->spu_hash_pad_len = spu2_hash_pad_len;
4269 spu->spu_gcm_ccm_pad_len = spu2_gcm_ccm_pad_len;
4270 spu->spu_assoc_resp_len = spu2_assoc_resp_len;
4271 spu->spu_aead_ivlen = spu2_aead_ivlen;
4272 spu->spu_hash_type = spu2_hash_type;
4273 spu->spu_digest_size = spu2_digest_size;
4274 spu->spu_create_request = spu2_create_request;
4275 spu->spu_cipher_req_init = spu2_cipher_req_init;
4276 spu->spu_cipher_req_finish = spu2_cipher_req_finish;
4277 spu->spu_request_pad = spu2_request_pad;
4278 spu->spu_tx_status_len = spu2_tx_status_len;
4279 spu->spu_rx_status_len = spu2_rx_status_len;
4280 spu->spu_status_process = spu2_status_process;
4281 spu->spu_xts_tweak_in_payload = spu2_xts_tweak_in_payload;
4282 spu->spu_ccm_update_iv = spu2_ccm_update_iv;
4283 spu->spu_wordalign_padlen = spu2_wordalign_padlen;
4288 * spu_mb_init() - Initialize mailbox client. Request ownership of a mailbox
4289 * channel for the SPU being probed.
4290 * @dev: SPU driver device structure
4292 * Return: 0 if successful
4293 * < 0 otherwise
4295 static int spu_mb_init(struct device *dev)
4297 struct mbox_client *mcl = &iproc_priv.mcl;
4298 int err, i;
4300 iproc_priv.mbox = devm_kcalloc(dev, iproc_priv.spu.num_chan,
4301 sizeof(struct mbox_chan *), GFP_KERNEL);
4302 if (!iproc_priv.mbox)
4303 return -ENOMEM;
4305 mcl->dev = dev;
4306 mcl->tx_block = false;
4307 mcl->tx_tout = 0;
4308 mcl->knows_txdone = true;
4309 mcl->rx_callback = spu_rx_callback;
4310 mcl->tx_done = NULL;
4312 for (i = 0; i < iproc_priv.spu.num_chan; i++) {
4313 iproc_priv.mbox[i] = mbox_request_channel(mcl, i);
4314 if (IS_ERR(iproc_priv.mbox[i])) {
4315 err = PTR_ERR(iproc_priv.mbox[i]);
4316 dev_err(dev,
4317 "Mbox channel %d request failed with err %d",
4318 i, err);
4319 iproc_priv.mbox[i] = NULL;
4320 goto free_channels;
4324 return 0;
4325 free_channels:
4326 for (i = 0; i < iproc_priv.spu.num_chan; i++) {
4327 if (iproc_priv.mbox[i])
4328 mbox_free_channel(iproc_priv.mbox[i]);
4331 return err;
4334 static void spu_mb_release(struct platform_device *pdev)
4336 int i;
4338 for (i = 0; i < iproc_priv.spu.num_chan; i++)
4339 mbox_free_channel(iproc_priv.mbox[i]);
4342 static void spu_counters_init(void)
4344 int i;
4345 int j;
4347 atomic_set(&iproc_priv.session_count, 0);
4348 atomic_set(&iproc_priv.stream_count, 0);
4349 atomic_set(&iproc_priv.next_chan, (int)iproc_priv.spu.num_chan);
4350 atomic64_set(&iproc_priv.bytes_in, 0);
4351 atomic64_set(&iproc_priv.bytes_out, 0);
4352 for (i = 0; i < SPU_OP_NUM; i++) {
4353 atomic_set(&iproc_priv.op_counts[i], 0);
4354 atomic_set(&iproc_priv.setkey_cnt[i], 0);
4356 for (i = 0; i < CIPHER_ALG_LAST; i++)
4357 for (j = 0; j < CIPHER_MODE_LAST; j++)
4358 atomic_set(&iproc_priv.cipher_cnt[i][j], 0);
4360 for (i = 0; i < HASH_ALG_LAST; i++) {
4361 atomic_set(&iproc_priv.hash_cnt[i], 0);
4362 atomic_set(&iproc_priv.hmac_cnt[i], 0);
4364 for (i = 0; i < AEAD_TYPE_LAST; i++)
4365 atomic_set(&iproc_priv.aead_cnt[i], 0);
4367 atomic_set(&iproc_priv.mb_no_spc, 0);
4368 atomic_set(&iproc_priv.mb_send_fail, 0);
4369 atomic_set(&iproc_priv.bad_icv, 0);
4372 static int spu_register_skcipher(struct iproc_alg_s *driver_alg)
4374 struct skcipher_alg *crypto = &driver_alg->alg.skcipher;
4375 int err;
4377 crypto->base.cra_module = THIS_MODULE;
4378 crypto->base.cra_priority = cipher_pri;
4379 crypto->base.cra_alignmask = 0;
4380 crypto->base.cra_ctxsize = sizeof(struct iproc_ctx_s);
4381 crypto->base.cra_flags = CRYPTO_ALG_ASYNC |
4382 CRYPTO_ALG_ALLOCATES_MEMORY |
4383 CRYPTO_ALG_KERN_DRIVER_ONLY;
4385 crypto->init = skcipher_init_tfm;
4386 crypto->exit = skcipher_exit_tfm;
4387 crypto->setkey = skcipher_setkey;
4388 crypto->encrypt = skcipher_encrypt;
4389 crypto->decrypt = skcipher_decrypt;
4391 err = crypto_register_skcipher(crypto);
4392 /* Mark alg as having been registered, if successful */
4393 if (err == 0)
4394 driver_alg->registered = true;
4395 pr_debug(" registered skcipher %s\n", crypto->base.cra_driver_name);
4396 return err;
4399 static int spu_register_ahash(struct iproc_alg_s *driver_alg)
4401 struct spu_hw *spu = &iproc_priv.spu;
4402 struct ahash_alg *hash = &driver_alg->alg.hash;
4403 int err;
4405 /* AES-XCBC is the only AES hash type currently supported on SPU-M */
4406 if ((driver_alg->auth_info.alg == HASH_ALG_AES) &&
4407 (driver_alg->auth_info.mode != HASH_MODE_XCBC) &&
4408 (spu->spu_type == SPU_TYPE_SPUM))
4409 return 0;
4411 /* SHA3 algorithm variants are not registered for SPU-M or SPU2. */
4412 if ((driver_alg->auth_info.alg >= HASH_ALG_SHA3_224) &&
4413 (spu->spu_subtype != SPU_SUBTYPE_SPU2_V2))
4414 return 0;
4416 hash->halg.base.cra_module = THIS_MODULE;
4417 hash->halg.base.cra_priority = hash_pri;
4418 hash->halg.base.cra_alignmask = 0;
4419 hash->halg.base.cra_ctxsize = sizeof(struct iproc_ctx_s);
4420 hash->halg.base.cra_init = ahash_cra_init;
4421 hash->halg.base.cra_exit = generic_cra_exit;
4422 hash->halg.base.cra_flags = CRYPTO_ALG_ASYNC |
4423 CRYPTO_ALG_ALLOCATES_MEMORY;
4424 hash->halg.statesize = sizeof(struct spu_hash_export_s);
4426 if (driver_alg->auth_info.mode != HASH_MODE_HMAC) {
4427 hash->init = ahash_init;
4428 hash->update = ahash_update;
4429 hash->final = ahash_final;
4430 hash->finup = ahash_finup;
4431 hash->digest = ahash_digest;
4432 if ((driver_alg->auth_info.alg == HASH_ALG_AES) &&
4433 ((driver_alg->auth_info.mode == HASH_MODE_XCBC) ||
4434 (driver_alg->auth_info.mode == HASH_MODE_CMAC))) {
4435 hash->setkey = ahash_setkey;
4437 } else {
4438 hash->setkey = ahash_hmac_setkey;
4439 hash->init = ahash_hmac_init;
4440 hash->update = ahash_hmac_update;
4441 hash->final = ahash_hmac_final;
4442 hash->finup = ahash_hmac_finup;
4443 hash->digest = ahash_hmac_digest;
4445 hash->export = ahash_export;
4446 hash->import = ahash_import;
4448 err = crypto_register_ahash(hash);
4449 /* Mark alg as having been registered, if successful */
4450 if (err == 0)
4451 driver_alg->registered = true;
4452 pr_debug(" registered ahash %s\n",
4453 hash->halg.base.cra_driver_name);
4454 return err;
4457 static int spu_register_aead(struct iproc_alg_s *driver_alg)
4459 struct aead_alg *aead = &driver_alg->alg.aead;
4460 int err;
4462 aead->base.cra_module = THIS_MODULE;
4463 aead->base.cra_priority = aead_pri;
4464 aead->base.cra_alignmask = 0;
4465 aead->base.cra_ctxsize = sizeof(struct iproc_ctx_s);
4467 aead->base.cra_flags |= CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY;
4468 /* setkey set in alg initialization */
4469 aead->setauthsize = aead_setauthsize;
4470 aead->encrypt = aead_encrypt;
4471 aead->decrypt = aead_decrypt;
4472 aead->init = aead_cra_init;
4473 aead->exit = aead_cra_exit;
4475 err = crypto_register_aead(aead);
4476 /* Mark alg as having been registered, if successful */
4477 if (err == 0)
4478 driver_alg->registered = true;
4479 pr_debug(" registered aead %s\n", aead->base.cra_driver_name);
4480 return err;
4483 /* register crypto algorithms the device supports */
4484 static int spu_algs_register(struct device *dev)
4486 int i, j;
4487 int err;
4489 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4490 switch (driver_algs[i].type) {
4491 case CRYPTO_ALG_TYPE_SKCIPHER:
4492 err = spu_register_skcipher(&driver_algs[i]);
4493 break;
4494 case CRYPTO_ALG_TYPE_AHASH:
4495 err = spu_register_ahash(&driver_algs[i]);
4496 break;
4497 case CRYPTO_ALG_TYPE_AEAD:
4498 err = spu_register_aead(&driver_algs[i]);
4499 break;
4500 default:
4501 dev_err(dev,
4502 "iproc-crypto: unknown alg type: %d",
4503 driver_algs[i].type);
4504 err = -EINVAL;
4507 if (err) {
4508 dev_err(dev, "alg registration failed with error %d\n",
4509 err);
4510 goto err_algs;
4514 return 0;
4516 err_algs:
4517 for (j = 0; j < i; j++) {
4518 /* Skip any algorithm not registered */
4519 if (!driver_algs[j].registered)
4520 continue;
4521 switch (driver_algs[j].type) {
4522 case CRYPTO_ALG_TYPE_SKCIPHER:
4523 crypto_unregister_skcipher(&driver_algs[j].alg.skcipher);
4524 driver_algs[j].registered = false;
4525 break;
4526 case CRYPTO_ALG_TYPE_AHASH:
4527 crypto_unregister_ahash(&driver_algs[j].alg.hash);
4528 driver_algs[j].registered = false;
4529 break;
4530 case CRYPTO_ALG_TYPE_AEAD:
4531 crypto_unregister_aead(&driver_algs[j].alg.aead);
4532 driver_algs[j].registered = false;
4533 break;
4536 return err;
4539 /* ==================== Kernel Platform API ==================== */
4541 static struct spu_type_subtype spum_ns2_types = {
4542 SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NS2
4545 static struct spu_type_subtype spum_nsp_types = {
4546 SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NSP
4549 static struct spu_type_subtype spu2_types = {
4550 SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V1
4553 static struct spu_type_subtype spu2_v2_types = {
4554 SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V2
4557 static const struct of_device_id bcm_spu_dt_ids[] = {
4559 .compatible = "brcm,spum-crypto",
4560 .data = &spum_ns2_types,
4563 .compatible = "brcm,spum-nsp-crypto",
4564 .data = &spum_nsp_types,
4567 .compatible = "brcm,spu2-crypto",
4568 .data = &spu2_types,
4571 .compatible = "brcm,spu2-v2-crypto",
4572 .data = &spu2_v2_types,
4574 { /* sentinel */ }
4577 MODULE_DEVICE_TABLE(of, bcm_spu_dt_ids);
4579 static int spu_dt_read(struct platform_device *pdev)
4581 struct device *dev = &pdev->dev;
4582 struct spu_hw *spu = &iproc_priv.spu;
4583 struct resource *spu_ctrl_regs;
4584 const struct spu_type_subtype *matched_spu_type;
4585 struct device_node *dn = pdev->dev.of_node;
4586 int err, i;
4588 /* Count number of mailbox channels */
4589 spu->num_chan = of_count_phandle_with_args(dn, "mboxes", "#mbox-cells");
4591 matched_spu_type = of_device_get_match_data(dev);
4592 if (!matched_spu_type) {
4593 dev_err(dev, "Failed to match device\n");
4594 return -ENODEV;
4597 spu->spu_type = matched_spu_type->type;
4598 spu->spu_subtype = matched_spu_type->subtype;
4600 for (i = 0; (i < MAX_SPUS) && ((spu_ctrl_regs =
4601 platform_get_resource(pdev, IORESOURCE_MEM, i)) != NULL); i++) {
4603 spu->reg_vbase[i] = devm_ioremap_resource(dev, spu_ctrl_regs);
4604 if (IS_ERR(spu->reg_vbase[i])) {
4605 err = PTR_ERR(spu->reg_vbase[i]);
4606 dev_err(dev, "Failed to map registers: %d\n",
4607 err);
4608 spu->reg_vbase[i] = NULL;
4609 return err;
4612 spu->num_spu = i;
4613 dev_dbg(dev, "Device has %d SPUs", spu->num_spu);
4615 return 0;
4618 static int bcm_spu_probe(struct platform_device *pdev)
4620 struct device *dev = &pdev->dev;
4621 struct spu_hw *spu = &iproc_priv.spu;
4622 int err;
4624 iproc_priv.pdev = pdev;
4625 platform_set_drvdata(iproc_priv.pdev,
4626 &iproc_priv);
4628 err = spu_dt_read(pdev);
4629 if (err < 0)
4630 goto failure;
4632 err = spu_mb_init(dev);
4633 if (err < 0)
4634 goto failure;
4636 if (spu->spu_type == SPU_TYPE_SPUM)
4637 iproc_priv.bcm_hdr_len = 8;
4638 else if (spu->spu_type == SPU_TYPE_SPU2)
4639 iproc_priv.bcm_hdr_len = 0;
4641 spu_functions_register(dev, spu->spu_type, spu->spu_subtype);
4643 spu_counters_init();
4645 spu_setup_debugfs();
4647 err = spu_algs_register(dev);
4648 if (err < 0)
4649 goto fail_reg;
4651 return 0;
4653 fail_reg:
4654 spu_free_debugfs();
4655 failure:
4656 spu_mb_release(pdev);
4657 dev_err(dev, "%s failed with error %d.\n", __func__, err);
4659 return err;
4662 static void bcm_spu_remove(struct platform_device *pdev)
4664 int i;
4665 struct device *dev = &pdev->dev;
4666 char *cdn;
4668 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4670 * Not all algorithms were registered, depending on whether
4671 * hardware is SPU or SPU2. So here we make sure to skip
4672 * those algorithms that were not previously registered.
4674 if (!driver_algs[i].registered)
4675 continue;
4677 switch (driver_algs[i].type) {
4678 case CRYPTO_ALG_TYPE_SKCIPHER:
4679 crypto_unregister_skcipher(&driver_algs[i].alg.skcipher);
4680 dev_dbg(dev, " unregistered cipher %s\n",
4681 driver_algs[i].alg.skcipher.base.cra_driver_name);
4682 driver_algs[i].registered = false;
4683 break;
4684 case CRYPTO_ALG_TYPE_AHASH:
4685 crypto_unregister_ahash(&driver_algs[i].alg.hash);
4686 cdn = driver_algs[i].alg.hash.halg.base.cra_driver_name;
4687 dev_dbg(dev, " unregistered hash %s\n", cdn);
4688 driver_algs[i].registered = false;
4689 break;
4690 case CRYPTO_ALG_TYPE_AEAD:
4691 crypto_unregister_aead(&driver_algs[i].alg.aead);
4692 dev_dbg(dev, " unregistered aead %s\n",
4693 driver_algs[i].alg.aead.base.cra_driver_name);
4694 driver_algs[i].registered = false;
4695 break;
4698 spu_free_debugfs();
4699 spu_mb_release(pdev);
4702 /* ===== Kernel Module API ===== */
4704 static struct platform_driver bcm_spu_pdriver = {
4705 .driver = {
4706 .name = "brcm-spu-crypto",
4707 .of_match_table = of_match_ptr(bcm_spu_dt_ids),
4709 .probe = bcm_spu_probe,
4710 .remove = bcm_spu_remove,
4712 module_platform_driver(bcm_spu_pdriver);
4714 MODULE_AUTHOR("Rob Rice <rob.rice@broadcom.com>");
4715 MODULE_DESCRIPTION("Broadcom symmetric crypto offload driver");
4716 MODULE_LICENSE("GPL v2");