2 * Copyright 2016 Broadcom
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation (the "GPL").
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License version 2 (GPLv2) for more details.
13 * You should have received a copy of the GNU General Public License
14 * version 2 (GPLv2) along with this source code.
17 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/interrupt.h>
23 #include <linux/platform_device.h>
24 #include <linux/scatterlist.h>
25 #include <linux/crypto.h>
26 #include <linux/kthread.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sched.h>
29 #include <linux/of_address.h>
30 #include <linux/of_device.h>
32 #include <linux/bitops.h>
34 #include <crypto/algapi.h>
35 #include <crypto/aead.h>
36 #include <crypto/internal/aead.h>
37 #include <crypto/aes.h>
38 #include <crypto/des.h>
39 #include <crypto/hmac.h>
40 #include <crypto/sha.h>
41 #include <crypto/md5.h>
42 #include <crypto/authenc.h>
43 #include <crypto/skcipher.h>
44 #include <crypto/hash.h>
45 #include <crypto/aes.h>
46 #include <crypto/sha3.h>
54 /* ================= Device Structure ================== */
56 struct device_private iproc_priv;
58 /* ==================== Parameters ===================== */
60 int flow_debug_logging;
61 module_param(flow_debug_logging, int, 0644);
62 MODULE_PARM_DESC(flow_debug_logging, "Enable Flow Debug Logging");
64 int packet_debug_logging;
65 module_param(packet_debug_logging, int, 0644);
66 MODULE_PARM_DESC(packet_debug_logging, "Enable Packet Debug Logging");
68 int debug_logging_sleep;
69 module_param(debug_logging_sleep, int, 0644);
70 MODULE_PARM_DESC(debug_logging_sleep, "Packet Debug Logging Sleep");
73 * The value of these module parameters is used to set the priority for each
74 * algo type when this driver registers algos with the kernel crypto API.
75 * To use a priority other than the default, set the priority in the insmod or
76 * modprobe. Changing the module priority after init time has no effect.
78 * The default priorities are chosen to be lower (less preferred) than ARMv8 CE
79 * algos, but more preferred than generic software algos.
81 static int cipher_pri = 150;
82 module_param(cipher_pri, int, 0644);
83 MODULE_PARM_DESC(cipher_pri, "Priority for cipher algos");
85 static int hash_pri = 100;
86 module_param(hash_pri, int, 0644);
87 MODULE_PARM_DESC(hash_pri, "Priority for hash algos");
89 static int aead_pri = 150;
90 module_param(aead_pri, int, 0644);
91 MODULE_PARM_DESC(aead_pri, "Priority for AEAD algos");
93 /* A type 3 BCM header, expected to precede the SPU header for SPU-M.
94 * Bits 3 and 4 in the first byte encode the channel number (the dma ringset).
100 char BCMHEADER[] = { 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28 };
102 * Some SPU hw does not use BCM header on SPU messages. So BCM_HDR_LEN
103 * is set dynamically after reading SPU type from device tree.
105 #define BCM_HDR_LEN iproc_priv.bcm_hdr_len
107 /* min and max time to sleep before retrying when mbox queue is full. usec */
108 #define MBOX_SLEEP_MIN 800
109 #define MBOX_SLEEP_MAX 1000
112 * select_channel() - Select a SPU channel to handle a crypto request. Selects
113 * channel in round robin order.
115 * Return: channel index
117 static u8 select_channel(void)
119 u8 chan_idx = atomic_inc_return(&iproc_priv.next_chan);
121 return chan_idx % iproc_priv.spu.num_chan;
125 * spu_ablkcipher_rx_sg_create() - Build up the scatterlist of buffers used to
126 * receive a SPU response message for an ablkcipher request. Includes buffers to
127 * catch SPU message headers and the response data.
128 * @mssg: mailbox message containing the receive sg
129 * @rctx: crypto request context
130 * @rx_frag_num: number of scatterlist elements required to hold the
131 * SPU response message
132 * @chunksize: Number of bytes of response data expected
133 * @stat_pad_len: Number of bytes required to pad the STAT field to
136 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
137 * when the request completes, whether the request is handled successfully or
145 spu_ablkcipher_rx_sg_create(struct brcm_message *mssg,
146 struct iproc_reqctx_s *rctx,
148 unsigned int chunksize, u32 stat_pad_len)
150 struct spu_hw *spu = &iproc_priv.spu;
151 struct scatterlist *sg; /* used to build sgs in mbox message */
152 struct iproc_ctx_s *ctx = rctx->ctx;
153 u32 datalen; /* Number of bytes of response data expected */
155 mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
161 sg_init_table(sg, rx_frag_num);
162 /* Space for SPU message header */
163 sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
165 /* If XTS tweak in payload, add buffer to receive encrypted tweak */
166 if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
167 spu->spu_xts_tweak_in_payload())
168 sg_set_buf(sg++, rctx->msg_buf.c.supdt_tweak,
171 /* Copy in each dst sg entry from request, up to chunksize */
172 datalen = spu_msg_sg_add(&sg, &rctx->dst_sg, &rctx->dst_skip,
173 rctx->dst_nents, chunksize);
174 if (datalen < chunksize) {
175 pr_err("%s(): failed to copy dst sg to mbox msg. chunksize %u, datalen %u",
176 __func__, chunksize, datalen);
180 if (ctx->cipher.alg == CIPHER_ALG_RC4)
181 /* Add buffer to catch 260-byte SUPDT field for RC4 */
182 sg_set_buf(sg++, rctx->msg_buf.c.supdt_tweak, SPU_SUPDT_LEN);
185 sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
187 memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
188 sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
194 * spu_ablkcipher_tx_sg_create() - Build up the scatterlist of buffers used to
195 * send a SPU request message for an ablkcipher request. Includes SPU message
196 * headers and the request data.
197 * @mssg: mailbox message containing the transmit sg
198 * @rctx: crypto request context
199 * @tx_frag_num: number of scatterlist elements required to construct the
200 * SPU request message
201 * @chunksize: Number of bytes of request data
202 * @pad_len: Number of pad bytes
204 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
205 * when the request completes, whether the request is handled successfully or
213 spu_ablkcipher_tx_sg_create(struct brcm_message *mssg,
214 struct iproc_reqctx_s *rctx,
215 u8 tx_frag_num, unsigned int chunksize, u32 pad_len)
217 struct spu_hw *spu = &iproc_priv.spu;
218 struct scatterlist *sg; /* used to build sgs in mbox message */
219 struct iproc_ctx_s *ctx = rctx->ctx;
220 u32 datalen; /* Number of bytes of response data expected */
223 mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
225 if (unlikely(!mssg->spu.src))
229 sg_init_table(sg, tx_frag_num);
231 sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
232 BCM_HDR_LEN + ctx->spu_req_hdr_len);
234 /* if XTS tweak in payload, copy from IV (where crypto API puts it) */
235 if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
236 spu->spu_xts_tweak_in_payload())
237 sg_set_buf(sg++, rctx->msg_buf.iv_ctr, SPU_XTS_TWEAK_SIZE);
239 /* Copy in each src sg entry from request, up to chunksize */
240 datalen = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
241 rctx->src_nents, chunksize);
242 if (unlikely(datalen < chunksize)) {
243 pr_err("%s(): failed to copy src sg to mbox msg",
249 sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
251 stat_len = spu->spu_tx_status_len();
253 memset(rctx->msg_buf.tx_stat, 0, stat_len);
254 sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
259 static int mailbox_send_message(struct brcm_message *mssg, u32 flags,
264 struct device *dev = &(iproc_priv.pdev->dev);
266 err = mbox_send_message(iproc_priv.mbox[chan_idx], mssg);
267 if (flags & CRYPTO_TFM_REQ_MAY_SLEEP) {
268 while ((err == -ENOBUFS) && (retry_cnt < SPU_MB_RETRY_MAX)) {
270 * Mailbox queue is full. Since MAY_SLEEP is set, assume
271 * not in atomic context and we can wait and try again.
274 usleep_range(MBOX_SLEEP_MIN, MBOX_SLEEP_MAX);
275 err = mbox_send_message(iproc_priv.mbox[chan_idx],
277 atomic_inc(&iproc_priv.mb_no_spc);
281 atomic_inc(&iproc_priv.mb_send_fail);
285 /* Check error returned by mailbox controller */
287 if (unlikely(err < 0)) {
288 dev_err(dev, "message error %d", err);
289 /* Signal txdone for mailbox channel */
292 /* Signal txdone for mailbox channel */
293 mbox_client_txdone(iproc_priv.mbox[chan_idx], err);
298 * handle_ablkcipher_req() - Submit as much of a block cipher request as fits in
299 * a single SPU request message, starting at the current position in the request
301 * @rctx: Crypto request context
303 * This may be called on the crypto API thread, or, when a request is so large
304 * it must be broken into multiple SPU messages, on the thread used to invoke
305 * the response callback. When requests are broken into multiple SPU
306 * messages, we assume subsequent messages depend on previous results, and
307 * thus always wait for previous results before submitting the next message.
308 * Because requests are submitted in lock step like this, there is no need
309 * to synchronize access to request data structures.
311 * Return: -EINPROGRESS: request has been accepted and result will be returned
313 * Any other value indicates an error
315 static int handle_ablkcipher_req(struct iproc_reqctx_s *rctx)
317 struct spu_hw *spu = &iproc_priv.spu;
318 struct crypto_async_request *areq = rctx->parent;
319 struct ablkcipher_request *req =
320 container_of(areq, struct ablkcipher_request, base);
321 struct iproc_ctx_s *ctx = rctx->ctx;
322 struct spu_cipher_parms cipher_parms;
324 unsigned int chunksize = 0; /* Num bytes of request to submit */
325 int remaining = 0; /* Bytes of request still to process */
326 int chunk_start; /* Beginning of data for current SPU msg */
328 /* IV or ctr value to use in this SPU msg */
329 u8 local_iv_ctr[MAX_IV_SIZE];
330 u32 stat_pad_len; /* num bytes to align status field */
331 u32 pad_len; /* total length of all padding */
332 bool update_key = false;
333 struct brcm_message *mssg; /* mailbox message */
335 /* number of entries in src and dst sg in mailbox message. */
336 u8 rx_frag_num = 2; /* response header and STATUS */
337 u8 tx_frag_num = 1; /* request header */
339 flow_log("%s\n", __func__);
341 cipher_parms.alg = ctx->cipher.alg;
342 cipher_parms.mode = ctx->cipher.mode;
343 cipher_parms.type = ctx->cipher_type;
344 cipher_parms.key_len = ctx->enckeylen;
345 cipher_parms.key_buf = ctx->enckey;
346 cipher_parms.iv_buf = local_iv_ctr;
347 cipher_parms.iv_len = rctx->iv_ctr_len;
349 mssg = &rctx->mb_mssg;
350 chunk_start = rctx->src_sent;
351 remaining = rctx->total_todo - chunk_start;
353 /* determine the chunk we are breaking off and update the indexes */
354 if ((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
355 (remaining > ctx->max_payload))
356 chunksize = ctx->max_payload;
358 chunksize = remaining;
360 rctx->src_sent += chunksize;
361 rctx->total_sent = rctx->src_sent;
363 /* Count number of sg entries to be included in this request */
364 rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip, chunksize);
365 rctx->dst_nents = spu_sg_count(rctx->dst_sg, rctx->dst_skip, chunksize);
367 if ((ctx->cipher.mode == CIPHER_MODE_CBC) &&
368 rctx->is_encrypt && chunk_start)
370 * Encrypting non-first first chunk. Copy last block of
371 * previous result to IV for this chunk.
373 sg_copy_part_to_buf(req->dst, rctx->msg_buf.iv_ctr,
375 chunk_start - rctx->iv_ctr_len);
377 if (rctx->iv_ctr_len) {
378 /* get our local copy of the iv */
379 __builtin_memcpy(local_iv_ctr, rctx->msg_buf.iv_ctr,
382 /* generate the next IV if possible */
383 if ((ctx->cipher.mode == CIPHER_MODE_CBC) &&
386 * CBC Decrypt: next IV is the last ciphertext block in
389 sg_copy_part_to_buf(req->src, rctx->msg_buf.iv_ctr,
391 rctx->src_sent - rctx->iv_ctr_len);
392 } else if (ctx->cipher.mode == CIPHER_MODE_CTR) {
394 * The SPU hardware increments the counter once for
395 * each AES block of 16 bytes. So update the counter
396 * for the next chunk, if there is one. Note that for
397 * this chunk, the counter has already been copied to
398 * local_iv_ctr. We can assume a block size of 16,
399 * because we only support CTR mode for AES, not for
400 * any other cipher alg.
402 add_to_ctr(rctx->msg_buf.iv_ctr, chunksize >> 4);
406 if (ctx->cipher.alg == CIPHER_ALG_RC4) {
410 * for non-first RC4 chunks, use SUPDT from previous
411 * response as key for this chunk.
413 cipher_parms.key_buf = rctx->msg_buf.c.supdt_tweak;
415 cipher_parms.type = CIPHER_TYPE_UPDT;
416 } else if (!rctx->is_encrypt) {
418 * First RC4 chunk. For decrypt, key in pre-built msg
419 * header may have been changed if encrypt required
420 * multiple chunks. So revert the key to the
424 cipher_parms.type = CIPHER_TYPE_INIT;
428 if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
429 flow_log("max_payload infinite\n");
431 flow_log("max_payload %u\n", ctx->max_payload);
433 flow_log("sent:%u start:%u remains:%u size:%u\n",
434 rctx->src_sent, chunk_start, remaining, chunksize);
436 /* Copy SPU header template created at setkey time */
437 memcpy(rctx->msg_buf.bcm_spu_req_hdr, ctx->bcm_spu_req_hdr,
438 sizeof(rctx->msg_buf.bcm_spu_req_hdr));
441 * Pass SUPDT field as key. Key field in finish() call is only used
442 * when update_key has been set above for RC4. Will be ignored in
445 spu->spu_cipher_req_finish(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
446 ctx->spu_req_hdr_len, !(rctx->is_encrypt),
447 &cipher_parms, update_key, chunksize);
449 atomic64_add(chunksize, &iproc_priv.bytes_out);
451 stat_pad_len = spu->spu_wordalign_padlen(chunksize);
454 pad_len = stat_pad_len;
457 spu->spu_request_pad(rctx->msg_buf.spu_req_pad, 0,
458 0, ctx->auth.alg, ctx->auth.mode,
459 rctx->total_sent, stat_pad_len);
462 spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
463 ctx->spu_req_hdr_len);
464 packet_log("payload:\n");
465 dump_sg(rctx->src_sg, rctx->src_skip, chunksize);
466 packet_dump(" pad: ", rctx->msg_buf.spu_req_pad, pad_len);
469 * Build mailbox message containing SPU request msg and rx buffers
470 * to catch response message
472 memset(mssg, 0, sizeof(*mssg));
473 mssg->type = BRCM_MESSAGE_SPU;
474 mssg->ctx = rctx; /* Will be returned in response */
476 /* Create rx scatterlist to catch result */
477 rx_frag_num += rctx->dst_nents;
479 if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
480 spu->spu_xts_tweak_in_payload())
481 rx_frag_num++; /* extra sg to insert tweak */
483 err = spu_ablkcipher_rx_sg_create(mssg, rctx, rx_frag_num, chunksize,
488 /* Create tx scatterlist containing SPU request message */
489 tx_frag_num += rctx->src_nents;
490 if (spu->spu_tx_status_len())
493 if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
494 spu->spu_xts_tweak_in_payload())
495 tx_frag_num++; /* extra sg to insert tweak */
497 err = spu_ablkcipher_tx_sg_create(mssg, rctx, tx_frag_num, chunksize,
502 err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
503 if (unlikely(err < 0))
510 * handle_ablkcipher_resp() - Process a block cipher SPU response. Updates the
511 * total received count for the request and updates global stats.
512 * @rctx: Crypto request context
514 static void handle_ablkcipher_resp(struct iproc_reqctx_s *rctx)
516 struct spu_hw *spu = &iproc_priv.spu;
518 struct crypto_async_request *areq = rctx->parent;
519 struct ablkcipher_request *req = ablkcipher_request_cast(areq);
521 struct iproc_ctx_s *ctx = rctx->ctx;
524 /* See how much data was returned */
525 payload_len = spu->spu_payload_length(rctx->msg_buf.spu_resp_hdr);
528 * In XTS mode, the first SPU_XTS_TWEAK_SIZE bytes may be the
529 * encrypted tweak ("i") value; we don't count those.
531 if ((ctx->cipher.mode == CIPHER_MODE_XTS) &&
532 spu->spu_xts_tweak_in_payload() &&
533 (payload_len >= SPU_XTS_TWEAK_SIZE))
534 payload_len -= SPU_XTS_TWEAK_SIZE;
536 atomic64_add(payload_len, &iproc_priv.bytes_in);
538 flow_log("%s() offset: %u, bd_len: %u BD:\n",
539 __func__, rctx->total_received, payload_len);
541 dump_sg(req->dst, rctx->total_received, payload_len);
542 if (ctx->cipher.alg == CIPHER_ALG_RC4)
543 packet_dump(" supdt ", rctx->msg_buf.c.supdt_tweak,
546 rctx->total_received += payload_len;
547 if (rctx->total_received == rctx->total_todo) {
548 atomic_inc(&iproc_priv.op_counts[SPU_OP_CIPHER]);
550 &iproc_priv.cipher_cnt[ctx->cipher.alg][ctx->cipher.mode]);
555 * spu_ahash_rx_sg_create() - Build up the scatterlist of buffers used to
556 * receive a SPU response message for an ahash request.
557 * @mssg: mailbox message containing the receive sg
558 * @rctx: crypto request context
559 * @rx_frag_num: number of scatterlist elements required to hold the
560 * SPU response message
561 * @digestsize: length of hash digest, in bytes
562 * @stat_pad_len: Number of bytes required to pad the STAT field to
565 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
566 * when the request completes, whether the request is handled successfully or
574 spu_ahash_rx_sg_create(struct brcm_message *mssg,
575 struct iproc_reqctx_s *rctx,
576 u8 rx_frag_num, unsigned int digestsize,
579 struct spu_hw *spu = &iproc_priv.spu;
580 struct scatterlist *sg; /* used to build sgs in mbox message */
581 struct iproc_ctx_s *ctx = rctx->ctx;
583 mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
589 sg_init_table(sg, rx_frag_num);
590 /* Space for SPU message header */
591 sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
593 /* Space for digest */
594 sg_set_buf(sg++, rctx->msg_buf.digest, digestsize);
597 sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
599 memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
600 sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
605 * spu_ahash_tx_sg_create() - Build up the scatterlist of buffers used to send
606 * a SPU request message for an ahash request. Includes SPU message headers and
608 * @mssg: mailbox message containing the transmit sg
609 * @rctx: crypto request context
610 * @tx_frag_num: number of scatterlist elements required to construct the
611 * SPU request message
612 * @spu_hdr_len: length in bytes of SPU message header
613 * @hash_carry_len: Number of bytes of data carried over from previous req
614 * @new_data_len: Number of bytes of new request data
615 * @pad_len: Number of pad bytes
617 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
618 * when the request completes, whether the request is handled successfully or
626 spu_ahash_tx_sg_create(struct brcm_message *mssg,
627 struct iproc_reqctx_s *rctx,
630 unsigned int hash_carry_len,
631 unsigned int new_data_len, u32 pad_len)
633 struct spu_hw *spu = &iproc_priv.spu;
634 struct scatterlist *sg; /* used to build sgs in mbox message */
635 u32 datalen; /* Number of bytes of response data expected */
638 mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
644 sg_init_table(sg, tx_frag_num);
646 sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
647 BCM_HDR_LEN + spu_hdr_len);
650 sg_set_buf(sg++, rctx->hash_carry, hash_carry_len);
653 /* Copy in each src sg entry from request, up to chunksize */
654 datalen = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
655 rctx->src_nents, new_data_len);
656 if (datalen < new_data_len) {
657 pr_err("%s(): failed to copy src sg to mbox msg",
664 sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
666 stat_len = spu->spu_tx_status_len();
668 memset(rctx->msg_buf.tx_stat, 0, stat_len);
669 sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
676 * handle_ahash_req() - Process an asynchronous hash request from the crypto
678 * @rctx: Crypto request context
680 * Builds a SPU request message embedded in a mailbox message and submits the
681 * mailbox message on a selected mailbox channel. The SPU request message is
682 * constructed as a scatterlist, including entries from the crypto API's
683 * src scatterlist to avoid copying the data to be hashed. This function is
684 * called either on the thread from the crypto API, or, in the case that the
685 * crypto API request is too large to fit in a single SPU request message,
686 * on the thread that invokes the receive callback with a response message.
687 * Because some operations require the response from one chunk before the next
688 * chunk can be submitted, we always wait for the response for the previous
689 * chunk before submitting the next chunk. Because requests are submitted in
690 * lock step like this, there is no need to synchronize access to request data
694 * -EINPROGRESS: request has been submitted to SPU and response will be
695 * returned asynchronously
696 * -EAGAIN: non-final request included a small amount of data, which for
697 * efficiency we did not submit to the SPU, but instead stored
698 * to be submitted to the SPU with the next part of the request
699 * other: an error code
701 static int handle_ahash_req(struct iproc_reqctx_s *rctx)
703 struct spu_hw *spu = &iproc_priv.spu;
704 struct crypto_async_request *areq = rctx->parent;
705 struct ahash_request *req = ahash_request_cast(areq);
706 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
707 struct crypto_tfm *tfm = crypto_ahash_tfm(ahash);
708 unsigned int blocksize = crypto_tfm_alg_blocksize(tfm);
709 struct iproc_ctx_s *ctx = rctx->ctx;
711 /* number of bytes still to be hashed in this req */
712 unsigned int nbytes_to_hash = 0;
714 unsigned int chunksize = 0; /* length of hash carry + new data */
716 * length of new data, not from hash carry, to be submitted in
719 unsigned int new_data_len;
721 unsigned int chunk_start = 0;
722 u32 db_size; /* Length of data field, incl gcm and hash padding */
723 int pad_len = 0; /* total pad len, including gcm, hash, stat padding */
724 u32 data_pad_len = 0; /* length of GCM/CCM padding */
725 u32 stat_pad_len = 0; /* length of padding to align STATUS word */
726 struct brcm_message *mssg; /* mailbox message */
727 struct spu_request_opts req_opts;
728 struct spu_cipher_parms cipher_parms;
729 struct spu_hash_parms hash_parms;
730 struct spu_aead_parms aead_parms;
731 unsigned int local_nbuf;
733 unsigned int digestsize;
737 * number of entries in src and dst sg. Always includes SPU msg header.
738 * rx always includes a buffer to catch digest and STATUS.
743 flow_log("total_todo %u, total_sent %u\n",
744 rctx->total_todo, rctx->total_sent);
746 memset(&req_opts, 0, sizeof(req_opts));
747 memset(&cipher_parms, 0, sizeof(cipher_parms));
748 memset(&hash_parms, 0, sizeof(hash_parms));
749 memset(&aead_parms, 0, sizeof(aead_parms));
751 req_opts.bd_suppress = true;
752 hash_parms.alg = ctx->auth.alg;
753 hash_parms.mode = ctx->auth.mode;
754 hash_parms.type = HASH_TYPE_NONE;
755 hash_parms.key_buf = (u8 *)ctx->authkey;
756 hash_parms.key_len = ctx->authkeylen;
759 * For hash algorithms below assignment looks bit odd but
760 * it's needed for AES-XCBC and AES-CMAC hash algorithms
761 * to differentiate between 128, 192, 256 bit key values.
762 * Based on the key values, hash algorithm is selected.
763 * For example for 128 bit key, hash algorithm is AES-128.
765 cipher_parms.type = ctx->cipher_type;
767 mssg = &rctx->mb_mssg;
768 chunk_start = rctx->src_sent;
771 * Compute the amount remaining to hash. This may include data
772 * carried over from previous requests.
774 nbytes_to_hash = rctx->total_todo - rctx->total_sent;
775 chunksize = nbytes_to_hash;
776 if ((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
777 (chunksize > ctx->max_payload))
778 chunksize = ctx->max_payload;
781 * If this is not a final request and the request data is not a multiple
782 * of a full block, then simply park the extra data and prefix it to the
783 * data for the next request.
785 if (!rctx->is_final) {
786 u8 *dest = rctx->hash_carry + rctx->hash_carry_len;
787 u16 new_len; /* len of data to add to hash carry */
789 rem = chunksize % blocksize; /* remainder */
791 /* chunksize not a multiple of blocksize */
793 if (chunksize == 0) {
794 /* Don't have a full block to submit to hw */
795 new_len = rem - rctx->hash_carry_len;
796 sg_copy_part_to_buf(req->src, dest, new_len,
798 rctx->hash_carry_len = rem;
799 flow_log("Exiting with hash carry len: %u\n",
800 rctx->hash_carry_len);
801 packet_dump(" buf: ",
803 rctx->hash_carry_len);
809 /* if we have hash carry, then prefix it to the data in this request */
810 local_nbuf = rctx->hash_carry_len;
811 rctx->hash_carry_len = 0;
814 new_data_len = chunksize - local_nbuf;
816 /* Count number of sg entries to be used in this request */
817 rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip,
820 /* AES hashing keeps key size in type field, so need to copy it here */
821 if (hash_parms.alg == HASH_ALG_AES)
822 hash_parms.type = cipher_parms.type;
824 hash_parms.type = spu->spu_hash_type(rctx->total_sent);
826 digestsize = spu->spu_digest_size(ctx->digestsize, ctx->auth.alg,
828 hash_parms.digestsize = digestsize;
830 /* update the indexes */
831 rctx->total_sent += chunksize;
832 /* if you sent a prebuf then that wasn't from this req->src */
833 rctx->src_sent += new_data_len;
835 if ((rctx->total_sent == rctx->total_todo) && rctx->is_final)
836 hash_parms.pad_len = spu->spu_hash_pad_len(hash_parms.alg,
842 * If a non-first chunk, then include the digest returned from the
843 * previous chunk so that hw can add to it (except for AES types).
845 if ((hash_parms.type == HASH_TYPE_UPDT) &&
846 (hash_parms.alg != HASH_ALG_AES)) {
847 hash_parms.key_buf = rctx->incr_hash;
848 hash_parms.key_len = digestsize;
851 atomic64_add(chunksize, &iproc_priv.bytes_out);
853 flow_log("%s() final: %u nbuf: %u ",
854 __func__, rctx->is_final, local_nbuf);
856 if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
857 flow_log("max_payload infinite\n");
859 flow_log("max_payload %u\n", ctx->max_payload);
861 flow_log("chunk_start: %u chunk_size: %u\n", chunk_start, chunksize);
863 /* Prepend SPU header with type 3 BCM header */
864 memcpy(rctx->msg_buf.bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
866 hash_parms.prebuf_len = local_nbuf;
867 spu_hdr_len = spu->spu_create_request(rctx->msg_buf.bcm_spu_req_hdr +
869 &req_opts, &cipher_parms,
870 &hash_parms, &aead_parms,
873 if (spu_hdr_len == 0) {
874 pr_err("Failed to create SPU request header\n");
879 * Determine total length of padding required. Put all padding in one
882 data_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode, chunksize);
883 db_size = spu_real_db_size(0, 0, local_nbuf, new_data_len,
884 0, 0, hash_parms.pad_len);
885 if (spu->spu_tx_status_len())
886 stat_pad_len = spu->spu_wordalign_padlen(db_size);
889 pad_len = hash_parms.pad_len + data_pad_len + stat_pad_len;
892 spu->spu_request_pad(rctx->msg_buf.spu_req_pad, data_pad_len,
893 hash_parms.pad_len, ctx->auth.alg,
894 ctx->auth.mode, rctx->total_sent,
898 spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
900 packet_dump(" prebuf: ", rctx->hash_carry, local_nbuf);
902 dump_sg(rctx->src_sg, rctx->src_skip, new_data_len);
903 packet_dump(" pad: ", rctx->msg_buf.spu_req_pad, pad_len);
906 * Build mailbox message containing SPU request msg and rx buffers
907 * to catch response message
909 memset(mssg, 0, sizeof(*mssg));
910 mssg->type = BRCM_MESSAGE_SPU;
911 mssg->ctx = rctx; /* Will be returned in response */
913 /* Create rx scatterlist to catch result */
914 err = spu_ahash_rx_sg_create(mssg, rctx, rx_frag_num, digestsize,
919 /* Create tx scatterlist containing SPU request message */
920 tx_frag_num += rctx->src_nents;
921 if (spu->spu_tx_status_len())
923 err = spu_ahash_tx_sg_create(mssg, rctx, tx_frag_num, spu_hdr_len,
924 local_nbuf, new_data_len, pad_len);
928 err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
929 if (unlikely(err < 0))
936 * spu_hmac_outer_hash() - Request synchonous software compute of the outer hash
937 * for an HMAC request.
938 * @req: The HMAC request from the crypto API
939 * @ctx: The session context
941 * Return: 0 if synchronous hash operation successful
942 * -EINVAL if the hash algo is unrecognized
943 * any other value indicates an error
945 static int spu_hmac_outer_hash(struct ahash_request *req,
946 struct iproc_ctx_s *ctx)
948 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
949 unsigned int blocksize =
950 crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
953 switch (ctx->auth.alg) {
955 rc = do_shash("md5", req->result, ctx->opad, blocksize,
956 req->result, ctx->digestsize, NULL, 0);
959 rc = do_shash("sha1", req->result, ctx->opad, blocksize,
960 req->result, ctx->digestsize, NULL, 0);
962 case HASH_ALG_SHA224:
963 rc = do_shash("sha224", req->result, ctx->opad, blocksize,
964 req->result, ctx->digestsize, NULL, 0);
966 case HASH_ALG_SHA256:
967 rc = do_shash("sha256", req->result, ctx->opad, blocksize,
968 req->result, ctx->digestsize, NULL, 0);
970 case HASH_ALG_SHA384:
971 rc = do_shash("sha384", req->result, ctx->opad, blocksize,
972 req->result, ctx->digestsize, NULL, 0);
974 case HASH_ALG_SHA512:
975 rc = do_shash("sha512", req->result, ctx->opad, blocksize,
976 req->result, ctx->digestsize, NULL, 0);
979 pr_err("%s() Error : unknown hmac type\n", __func__);
986 * ahash_req_done() - Process a hash result from the SPU hardware.
987 * @rctx: Crypto request context
989 * Return: 0 if successful
992 static int ahash_req_done(struct iproc_reqctx_s *rctx)
994 struct spu_hw *spu = &iproc_priv.spu;
995 struct crypto_async_request *areq = rctx->parent;
996 struct ahash_request *req = ahash_request_cast(areq);
997 struct iproc_ctx_s *ctx = rctx->ctx;
1000 memcpy(req->result, rctx->msg_buf.digest, ctx->digestsize);
1002 if (spu->spu_type == SPU_TYPE_SPUM) {
1003 /* byte swap the output from the UPDT function to network byte
1006 if (ctx->auth.alg == HASH_ALG_MD5) {
1007 __swab32s((u32 *)req->result);
1008 __swab32s(((u32 *)req->result) + 1);
1009 __swab32s(((u32 *)req->result) + 2);
1010 __swab32s(((u32 *)req->result) + 3);
1011 __swab32s(((u32 *)req->result) + 4);
1015 flow_dump(" digest ", req->result, ctx->digestsize);
1017 /* if this an HMAC then do the outer hash */
1018 if (rctx->is_sw_hmac) {
1019 err = spu_hmac_outer_hash(req, ctx);
1022 flow_dump(" hmac: ", req->result, ctx->digestsize);
1025 if (rctx->is_sw_hmac || ctx->auth.mode == HASH_MODE_HMAC) {
1026 atomic_inc(&iproc_priv.op_counts[SPU_OP_HMAC]);
1027 atomic_inc(&iproc_priv.hmac_cnt[ctx->auth.alg]);
1029 atomic_inc(&iproc_priv.op_counts[SPU_OP_HASH]);
1030 atomic_inc(&iproc_priv.hash_cnt[ctx->auth.alg]);
1037 * handle_ahash_resp() - Process a SPU response message for a hash request.
1038 * Checks if the entire crypto API request has been processed, and if so,
1039 * invokes post processing on the result.
1040 * @rctx: Crypto request context
1042 static void handle_ahash_resp(struct iproc_reqctx_s *rctx)
1044 struct iproc_ctx_s *ctx = rctx->ctx;
1046 struct crypto_async_request *areq = rctx->parent;
1047 struct ahash_request *req = ahash_request_cast(areq);
1048 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
1049 unsigned int blocksize =
1050 crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
1053 * Save hash to use as input to next op if incremental. Might be copying
1054 * too much, but that's easier than figuring out actual digest size here
1056 memcpy(rctx->incr_hash, rctx->msg_buf.digest, MAX_DIGEST_SIZE);
1058 flow_log("%s() blocksize:%u digestsize:%u\n",
1059 __func__, blocksize, ctx->digestsize);
1061 atomic64_add(ctx->digestsize, &iproc_priv.bytes_in);
1063 if (rctx->is_final && (rctx->total_sent == rctx->total_todo))
1064 ahash_req_done(rctx);
1068 * spu_aead_rx_sg_create() - Build up the scatterlist of buffers used to receive
1069 * a SPU response message for an AEAD request. Includes buffers to catch SPU
1070 * message headers and the response data.
1071 * @mssg: mailbox message containing the receive sg
1072 * @rctx: crypto request context
1073 * @rx_frag_num: number of scatterlist elements required to hold the
1074 * SPU response message
1075 * @assoc_len: Length of associated data included in the crypto request
1076 * @ret_iv_len: Length of IV returned in response
1077 * @resp_len: Number of bytes of response data expected to be written to
1078 * dst buffer from crypto API
1079 * @digestsize: Length of hash digest, in bytes
1080 * @stat_pad_len: Number of bytes required to pad the STAT field to
1083 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
1084 * when the request completes, whether the request is handled successfully or
1085 * there is an error.
1091 static int spu_aead_rx_sg_create(struct brcm_message *mssg,
1092 struct aead_request *req,
1093 struct iproc_reqctx_s *rctx,
1095 unsigned int assoc_len,
1096 u32 ret_iv_len, unsigned int resp_len,
1097 unsigned int digestsize, u32 stat_pad_len)
1099 struct spu_hw *spu = &iproc_priv.spu;
1100 struct scatterlist *sg; /* used to build sgs in mbox message */
1101 struct iproc_ctx_s *ctx = rctx->ctx;
1102 u32 datalen; /* Number of bytes of response data expected */
1106 if (ctx->is_rfc4543) {
1107 /* RFC4543: only pad after data, not after AAD */
1108 data_padlen = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1109 assoc_len + resp_len);
1110 assoc_buf_len = assoc_len;
1112 data_padlen = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1114 assoc_buf_len = spu->spu_assoc_resp_len(ctx->cipher.mode,
1115 assoc_len, ret_iv_len,
1119 if (ctx->cipher.mode == CIPHER_MODE_CCM)
1120 /* ICV (after data) must be in the next 32-bit word for CCM */
1121 data_padlen += spu->spu_wordalign_padlen(assoc_buf_len +
1126 /* have to catch gcm pad in separate buffer */
1129 mssg->spu.dst = kcalloc(rx_frag_num, sizeof(struct scatterlist),
1135 sg_init_table(sg, rx_frag_num);
1137 /* Space for SPU message header */
1138 sg_set_buf(sg++, rctx->msg_buf.spu_resp_hdr, ctx->spu_resp_hdr_len);
1140 if (assoc_buf_len) {
1142 * Don't write directly to req->dst, because SPU may pad the
1143 * assoc data in the response
1145 memset(rctx->msg_buf.a.resp_aad, 0, assoc_buf_len);
1146 sg_set_buf(sg++, rctx->msg_buf.a.resp_aad, assoc_buf_len);
1151 * Copy in each dst sg entry from request, up to chunksize.
1152 * dst sg catches just the data. digest caught in separate buf.
1154 datalen = spu_msg_sg_add(&sg, &rctx->dst_sg, &rctx->dst_skip,
1155 rctx->dst_nents, resp_len);
1156 if (datalen < (resp_len)) {
1157 pr_err("%s(): failed to copy dst sg to mbox msg. expected len %u, datalen %u",
1158 __func__, resp_len, datalen);
1163 /* If GCM/CCM data is padded, catch padding in separate buffer */
1165 memset(rctx->msg_buf.a.gcmpad, 0, data_padlen);
1166 sg_set_buf(sg++, rctx->msg_buf.a.gcmpad, data_padlen);
1169 /* Always catch ICV in separate buffer */
1170 sg_set_buf(sg++, rctx->msg_buf.digest, digestsize);
1172 flow_log("stat_pad_len %u\n", stat_pad_len);
1174 memset(rctx->msg_buf.rx_stat_pad, 0, stat_pad_len);
1175 sg_set_buf(sg++, rctx->msg_buf.rx_stat_pad, stat_pad_len);
1178 memset(rctx->msg_buf.rx_stat, 0, SPU_RX_STATUS_LEN);
1179 sg_set_buf(sg, rctx->msg_buf.rx_stat, spu->spu_rx_status_len());
1185 * spu_aead_tx_sg_create() - Build up the scatterlist of buffers used to send a
1186 * SPU request message for an AEAD request. Includes SPU message headers and the
1188 * @mssg: mailbox message containing the transmit sg
1189 * @rctx: crypto request context
1190 * @tx_frag_num: number of scatterlist elements required to construct the
1191 * SPU request message
1192 * @spu_hdr_len: length of SPU message header in bytes
1193 * @assoc: crypto API associated data scatterlist
1194 * @assoc_len: length of associated data
1195 * @assoc_nents: number of scatterlist entries containing assoc data
1196 * @aead_iv_len: length of AEAD IV, if included
1197 * @chunksize: Number of bytes of request data
1198 * @aad_pad_len: Number of bytes of padding at end of AAD. For GCM/CCM.
1199 * @pad_len: Number of pad bytes
1200 * @incl_icv: If true, write separate ICV buffer after data and
1203 * The scatterlist that gets allocated here is freed in spu_chunk_cleanup()
1204 * when the request completes, whether the request is handled successfully or
1205 * there is an error.
1211 static int spu_aead_tx_sg_create(struct brcm_message *mssg,
1212 struct iproc_reqctx_s *rctx,
1215 struct scatterlist *assoc,
1216 unsigned int assoc_len,
1218 unsigned int aead_iv_len,
1219 unsigned int chunksize,
1220 u32 aad_pad_len, u32 pad_len, bool incl_icv)
1222 struct spu_hw *spu = &iproc_priv.spu;
1223 struct scatterlist *sg; /* used to build sgs in mbox message */
1224 struct scatterlist *assoc_sg = assoc;
1225 struct iproc_ctx_s *ctx = rctx->ctx;
1226 u32 datalen; /* Number of bytes of data to write */
1227 u32 written; /* Number of bytes of data written */
1228 u32 assoc_offset = 0;
1231 mssg->spu.src = kcalloc(tx_frag_num, sizeof(struct scatterlist),
1237 sg_init_table(sg, tx_frag_num);
1239 sg_set_buf(sg++, rctx->msg_buf.bcm_spu_req_hdr,
1240 BCM_HDR_LEN + spu_hdr_len);
1243 /* Copy in each associated data sg entry from request */
1244 written = spu_msg_sg_add(&sg, &assoc_sg, &assoc_offset,
1245 assoc_nents, assoc_len);
1246 if (written < assoc_len) {
1247 pr_err("%s(): failed to copy assoc sg to mbox msg",
1254 sg_set_buf(sg++, rctx->msg_buf.iv_ctr, aead_iv_len);
1257 memset(rctx->msg_buf.a.req_aad_pad, 0, aad_pad_len);
1258 sg_set_buf(sg++, rctx->msg_buf.a.req_aad_pad, aad_pad_len);
1261 datalen = chunksize;
1262 if ((chunksize > ctx->digestsize) && incl_icv)
1263 datalen -= ctx->digestsize;
1265 /* For aead, a single msg should consume the entire src sg */
1266 written = spu_msg_sg_add(&sg, &rctx->src_sg, &rctx->src_skip,
1267 rctx->src_nents, datalen);
1268 if (written < datalen) {
1269 pr_err("%s(): failed to copy src sg to mbox msg",
1276 memset(rctx->msg_buf.spu_req_pad, 0, pad_len);
1277 sg_set_buf(sg++, rctx->msg_buf.spu_req_pad, pad_len);
1281 sg_set_buf(sg++, rctx->msg_buf.digest, ctx->digestsize);
1283 stat_len = spu->spu_tx_status_len();
1285 memset(rctx->msg_buf.tx_stat, 0, stat_len);
1286 sg_set_buf(sg, rctx->msg_buf.tx_stat, stat_len);
1292 * handle_aead_req() - Submit a SPU request message for the next chunk of the
1293 * current AEAD request.
1294 * @rctx: Crypto request context
1296 * Unlike other operation types, we assume the length of the request fits in
1297 * a single SPU request message. aead_enqueue() makes sure this is true.
1298 * Comments for other op types regarding threads applies here as well.
1300 * Unlike incremental hash ops, where the spu returns the entire hash for
1301 * truncated algs like sha-224, the SPU returns just the truncated hash in
1302 * response to aead requests. So digestsize is always ctx->digestsize here.
1304 * Return: -EINPROGRESS: crypto request has been accepted and result will be
1305 * returned asynchronously
1306 * Any other value indicates an error
1308 static int handle_aead_req(struct iproc_reqctx_s *rctx)
1310 struct spu_hw *spu = &iproc_priv.spu;
1311 struct crypto_async_request *areq = rctx->parent;
1312 struct aead_request *req = container_of(areq,
1313 struct aead_request, base);
1314 struct iproc_ctx_s *ctx = rctx->ctx;
1316 unsigned int chunksize;
1317 unsigned int resp_len;
1322 struct brcm_message *mssg; /* mailbox message */
1323 struct spu_request_opts req_opts;
1324 struct spu_cipher_parms cipher_parms;
1325 struct spu_hash_parms hash_parms;
1326 struct spu_aead_parms aead_parms;
1327 int assoc_nents = 0;
1328 bool incl_icv = false;
1329 unsigned int digestsize = ctx->digestsize;
1331 /* number of entries in src and dst sg. Always includes SPU msg header.
1333 u8 rx_frag_num = 2; /* and STATUS */
1336 /* doing the whole thing at once */
1337 chunksize = rctx->total_todo;
1339 flow_log("%s: chunksize %u\n", __func__, chunksize);
1341 memset(&req_opts, 0, sizeof(req_opts));
1342 memset(&hash_parms, 0, sizeof(hash_parms));
1343 memset(&aead_parms, 0, sizeof(aead_parms));
1345 req_opts.is_inbound = !(rctx->is_encrypt);
1346 req_opts.auth_first = ctx->auth_first;
1347 req_opts.is_aead = true;
1348 req_opts.is_esp = ctx->is_esp;
1350 cipher_parms.alg = ctx->cipher.alg;
1351 cipher_parms.mode = ctx->cipher.mode;
1352 cipher_parms.type = ctx->cipher_type;
1353 cipher_parms.key_buf = ctx->enckey;
1354 cipher_parms.key_len = ctx->enckeylen;
1355 cipher_parms.iv_buf = rctx->msg_buf.iv_ctr;
1356 cipher_parms.iv_len = rctx->iv_ctr_len;
1358 hash_parms.alg = ctx->auth.alg;
1359 hash_parms.mode = ctx->auth.mode;
1360 hash_parms.type = HASH_TYPE_NONE;
1361 hash_parms.key_buf = (u8 *)ctx->authkey;
1362 hash_parms.key_len = ctx->authkeylen;
1363 hash_parms.digestsize = digestsize;
1365 if ((ctx->auth.alg == HASH_ALG_SHA224) &&
1366 (ctx->authkeylen < SHA224_DIGEST_SIZE))
1367 hash_parms.key_len = SHA224_DIGEST_SIZE;
1369 aead_parms.assoc_size = req->assoclen;
1370 if (ctx->is_esp && !ctx->is_rfc4543) {
1372 * 8-byte IV is included assoc data in request. SPU2
1373 * expects AAD to include just SPI and seqno. So
1374 * subtract off the IV len.
1376 aead_parms.assoc_size -= GCM_RFC4106_IV_SIZE;
1378 if (rctx->is_encrypt) {
1379 aead_parms.return_iv = true;
1380 aead_parms.ret_iv_len = GCM_RFC4106_IV_SIZE;
1381 aead_parms.ret_iv_off = GCM_ESP_SALT_SIZE;
1384 aead_parms.ret_iv_len = 0;
1388 * Count number of sg entries from the crypto API request that are to
1389 * be included in this mailbox message. For dst sg, don't count space
1390 * for digest. Digest gets caught in a separate buffer and copied back
1391 * to dst sg when processing response.
1393 rctx->src_nents = spu_sg_count(rctx->src_sg, rctx->src_skip, chunksize);
1394 rctx->dst_nents = spu_sg_count(rctx->dst_sg, rctx->dst_skip, chunksize);
1395 if (aead_parms.assoc_size)
1396 assoc_nents = spu_sg_count(rctx->assoc, 0,
1397 aead_parms.assoc_size);
1399 mssg = &rctx->mb_mssg;
1401 rctx->total_sent = chunksize;
1402 rctx->src_sent = chunksize;
1403 if (spu->spu_assoc_resp_len(ctx->cipher.mode,
1404 aead_parms.assoc_size,
1405 aead_parms.ret_iv_len,
1409 aead_parms.iv_len = spu->spu_aead_ivlen(ctx->cipher.mode,
1412 if (ctx->auth.alg == HASH_ALG_AES)
1413 hash_parms.type = ctx->cipher_type;
1415 /* General case AAD padding (CCM and RFC4543 special cases below) */
1416 aead_parms.aad_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1417 aead_parms.assoc_size);
1419 /* General case data padding (CCM decrypt special case below) */
1420 aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1423 if (ctx->cipher.mode == CIPHER_MODE_CCM) {
1425 * for CCM, AAD len + 2 (rather than AAD len) needs to be
1428 aead_parms.aad_pad_len = spu->spu_gcm_ccm_pad_len(
1430 aead_parms.assoc_size + 2);
1433 * And when decrypting CCM, need to pad without including
1434 * size of ICV which is tacked on to end of chunk
1436 if (!rctx->is_encrypt)
1437 aead_parms.data_pad_len =
1438 spu->spu_gcm_ccm_pad_len(ctx->cipher.mode,
1439 chunksize - digestsize);
1441 /* CCM also requires software to rewrite portions of IV: */
1442 spu->spu_ccm_update_iv(digestsize, &cipher_parms, req->assoclen,
1443 chunksize, rctx->is_encrypt,
1447 if (ctx->is_rfc4543) {
1449 * RFC4543: data is included in AAD, so don't pad after AAD
1450 * and pad data based on both AAD + data size
1452 aead_parms.aad_pad_len = 0;
1453 if (!rctx->is_encrypt)
1454 aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(
1456 aead_parms.assoc_size + chunksize -
1459 aead_parms.data_pad_len = spu->spu_gcm_ccm_pad_len(
1461 aead_parms.assoc_size + chunksize);
1463 req_opts.is_rfc4543 = true;
1466 if (spu_req_incl_icv(ctx->cipher.mode, rctx->is_encrypt)) {
1469 /* Copy ICV from end of src scatterlist to digest buf */
1470 sg_copy_part_to_buf(req->src, rctx->msg_buf.digest, digestsize,
1471 req->assoclen + rctx->total_sent -
1475 atomic64_add(chunksize, &iproc_priv.bytes_out);
1477 flow_log("%s()-sent chunksize:%u\n", __func__, chunksize);
1479 /* Prepend SPU header with type 3 BCM header */
1480 memcpy(rctx->msg_buf.bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
1482 spu_hdr_len = spu->spu_create_request(rctx->msg_buf.bcm_spu_req_hdr +
1483 BCM_HDR_LEN, &req_opts,
1484 &cipher_parms, &hash_parms,
1485 &aead_parms, chunksize);
1487 /* Determine total length of padding. Put all padding in one buffer. */
1488 db_size = spu_real_db_size(aead_parms.assoc_size, aead_parms.iv_len, 0,
1489 chunksize, aead_parms.aad_pad_len,
1490 aead_parms.data_pad_len, 0);
1492 stat_pad_len = spu->spu_wordalign_padlen(db_size);
1496 pad_len = aead_parms.data_pad_len + stat_pad_len;
1499 spu->spu_request_pad(rctx->msg_buf.spu_req_pad,
1500 aead_parms.data_pad_len, 0,
1501 ctx->auth.alg, ctx->auth.mode,
1502 rctx->total_sent, stat_pad_len);
1505 spu->spu_dump_msg_hdr(rctx->msg_buf.bcm_spu_req_hdr + BCM_HDR_LEN,
1507 dump_sg(rctx->assoc, 0, aead_parms.assoc_size);
1508 packet_dump(" aead iv: ", rctx->msg_buf.iv_ctr, aead_parms.iv_len);
1509 packet_log("BD:\n");
1510 dump_sg(rctx->src_sg, rctx->src_skip, chunksize);
1511 packet_dump(" pad: ", rctx->msg_buf.spu_req_pad, pad_len);
1514 * Build mailbox message containing SPU request msg and rx buffers
1515 * to catch response message
1517 memset(mssg, 0, sizeof(*mssg));
1518 mssg->type = BRCM_MESSAGE_SPU;
1519 mssg->ctx = rctx; /* Will be returned in response */
1521 /* Create rx scatterlist to catch result */
1522 rx_frag_num += rctx->dst_nents;
1523 resp_len = chunksize;
1526 * Always catch ICV in separate buffer. Have to for GCM/CCM because of
1527 * padding. Have to for SHA-224 and other truncated SHAs because SPU
1528 * sends entire digest back.
1532 if (((ctx->cipher.mode == CIPHER_MODE_GCM) ||
1533 (ctx->cipher.mode == CIPHER_MODE_CCM)) && !rctx->is_encrypt) {
1535 * Input is ciphertxt plus ICV, but ICV not incl
1538 resp_len -= ctx->digestsize;
1540 /* no rx frags to catch output data */
1541 rx_frag_num -= rctx->dst_nents;
1544 err = spu_aead_rx_sg_create(mssg, req, rctx, rx_frag_num,
1545 aead_parms.assoc_size,
1546 aead_parms.ret_iv_len, resp_len, digestsize,
1551 /* Create tx scatterlist containing SPU request message */
1552 tx_frag_num += rctx->src_nents;
1553 tx_frag_num += assoc_nents;
1554 if (aead_parms.aad_pad_len)
1556 if (aead_parms.iv_len)
1558 if (spu->spu_tx_status_len())
1560 err = spu_aead_tx_sg_create(mssg, rctx, tx_frag_num, spu_hdr_len,
1561 rctx->assoc, aead_parms.assoc_size,
1562 assoc_nents, aead_parms.iv_len, chunksize,
1563 aead_parms.aad_pad_len, pad_len, incl_icv);
1567 err = mailbox_send_message(mssg, req->base.flags, rctx->chan_idx);
1568 if (unlikely(err < 0))
1571 return -EINPROGRESS;
1575 * handle_aead_resp() - Process a SPU response message for an AEAD request.
1576 * @rctx: Crypto request context
1578 static void handle_aead_resp(struct iproc_reqctx_s *rctx)
1580 struct spu_hw *spu = &iproc_priv.spu;
1581 struct crypto_async_request *areq = rctx->parent;
1582 struct aead_request *req = container_of(areq,
1583 struct aead_request, base);
1584 struct iproc_ctx_s *ctx = rctx->ctx;
1586 unsigned int icv_offset;
1589 /* See how much data was returned */
1590 payload_len = spu->spu_payload_length(rctx->msg_buf.spu_resp_hdr);
1591 flow_log("payload_len %u\n", payload_len);
1593 /* only count payload */
1594 atomic64_add(payload_len, &iproc_priv.bytes_in);
1597 packet_dump(" assoc_data ", rctx->msg_buf.a.resp_aad,
1601 * Copy the ICV back to the destination
1602 * buffer. In decrypt case, SPU gives us back the digest, but crypto
1603 * API doesn't expect ICV in dst buffer.
1605 result_len = req->cryptlen;
1606 if (rctx->is_encrypt) {
1607 icv_offset = req->assoclen + rctx->total_sent;
1608 packet_dump(" ICV: ", rctx->msg_buf.digest, ctx->digestsize);
1609 flow_log("copying ICV to dst sg at offset %u\n", icv_offset);
1610 sg_copy_part_from_buf(req->dst, rctx->msg_buf.digest,
1611 ctx->digestsize, icv_offset);
1612 result_len += ctx->digestsize;
1615 packet_log("response data: ");
1616 dump_sg(req->dst, req->assoclen, result_len);
1618 atomic_inc(&iproc_priv.op_counts[SPU_OP_AEAD]);
1619 if (ctx->cipher.alg == CIPHER_ALG_AES) {
1620 if (ctx->cipher.mode == CIPHER_MODE_CCM)
1621 atomic_inc(&iproc_priv.aead_cnt[AES_CCM]);
1622 else if (ctx->cipher.mode == CIPHER_MODE_GCM)
1623 atomic_inc(&iproc_priv.aead_cnt[AES_GCM]);
1625 atomic_inc(&iproc_priv.aead_cnt[AUTHENC]);
1627 atomic_inc(&iproc_priv.aead_cnt[AUTHENC]);
1632 * spu_chunk_cleanup() - Do cleanup after processing one chunk of a request
1633 * @rctx: request context
1635 * Mailbox scatterlists are allocated for each chunk. So free them after
1636 * processing each chunk.
1638 static void spu_chunk_cleanup(struct iproc_reqctx_s *rctx)
1640 /* mailbox message used to tx request */
1641 struct brcm_message *mssg = &rctx->mb_mssg;
1643 kfree(mssg->spu.src);
1644 kfree(mssg->spu.dst);
1645 memset(mssg, 0, sizeof(struct brcm_message));
1649 * finish_req() - Used to invoke the complete callback from the requester when
1650 * a request has been handled asynchronously.
1651 * @rctx: Request context
1652 * @err: Indicates whether the request was successful or not
1654 * Ensures that cleanup has been done for request
1656 static void finish_req(struct iproc_reqctx_s *rctx, int err)
1658 struct crypto_async_request *areq = rctx->parent;
1660 flow_log("%s() err:%d\n\n", __func__, err);
1662 /* No harm done if already called */
1663 spu_chunk_cleanup(rctx);
1666 areq->complete(areq, err);
1670 * spu_rx_callback() - Callback from mailbox framework with a SPU response.
1671 * @cl: mailbox client structure for SPU driver
1672 * @msg: mailbox message containing SPU response
1674 static void spu_rx_callback(struct mbox_client *cl, void *msg)
1676 struct spu_hw *spu = &iproc_priv.spu;
1677 struct brcm_message *mssg = msg;
1678 struct iproc_reqctx_s *rctx;
1679 struct iproc_ctx_s *ctx;
1680 struct crypto_async_request *areq;
1684 if (unlikely(!rctx)) {
1686 pr_err("%s(): no request context", __func__);
1690 areq = rctx->parent;
1693 /* process the SPU status */
1694 err = spu->spu_status_process(rctx->msg_buf.rx_stat);
1696 if (err == SPU_INVALID_ICV)
1697 atomic_inc(&iproc_priv.bad_icv);
1702 /* Process the SPU response message */
1703 switch (rctx->ctx->alg->type) {
1704 case CRYPTO_ALG_TYPE_ABLKCIPHER:
1705 handle_ablkcipher_resp(rctx);
1707 case CRYPTO_ALG_TYPE_AHASH:
1708 handle_ahash_resp(rctx);
1710 case CRYPTO_ALG_TYPE_AEAD:
1711 handle_aead_resp(rctx);
1719 * If this response does not complete the request, then send the next
1722 if (rctx->total_sent < rctx->total_todo) {
1723 /* Deallocate anything specific to previous chunk */
1724 spu_chunk_cleanup(rctx);
1726 switch (rctx->ctx->alg->type) {
1727 case CRYPTO_ALG_TYPE_ABLKCIPHER:
1728 err = handle_ablkcipher_req(rctx);
1730 case CRYPTO_ALG_TYPE_AHASH:
1731 err = handle_ahash_req(rctx);
1734 * we saved data in hash carry, but tell crypto
1735 * API we successfully completed request.
1739 case CRYPTO_ALG_TYPE_AEAD:
1740 err = handle_aead_req(rctx);
1746 if (err == -EINPROGRESS)
1747 /* Successfully submitted request for next chunk */
1752 finish_req(rctx, err);
1755 /* ==================== Kernel Cryptographic API ==================== */
1758 * ablkcipher_enqueue() - Handle ablkcipher encrypt or decrypt request.
1759 * @req: Crypto API request
1760 * @encrypt: true if encrypting; false if decrypting
1762 * Return: -EINPROGRESS if request accepted and result will be returned
1766 static int ablkcipher_enqueue(struct ablkcipher_request *req, bool encrypt)
1768 struct iproc_reqctx_s *rctx = ablkcipher_request_ctx(req);
1769 struct iproc_ctx_s *ctx =
1770 crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
1773 flow_log("%s() enc:%u\n", __func__, encrypt);
1775 rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1776 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1777 rctx->parent = &req->base;
1778 rctx->is_encrypt = encrypt;
1779 rctx->bd_suppress = false;
1780 rctx->total_todo = req->nbytes;
1782 rctx->total_sent = 0;
1783 rctx->total_received = 0;
1786 /* Initialize current position in src and dst scatterlists */
1787 rctx->src_sg = req->src;
1788 rctx->src_nents = 0;
1790 rctx->dst_sg = req->dst;
1791 rctx->dst_nents = 0;
1794 if (ctx->cipher.mode == CIPHER_MODE_CBC ||
1795 ctx->cipher.mode == CIPHER_MODE_CTR ||
1796 ctx->cipher.mode == CIPHER_MODE_OFB ||
1797 ctx->cipher.mode == CIPHER_MODE_XTS ||
1798 ctx->cipher.mode == CIPHER_MODE_GCM ||
1799 ctx->cipher.mode == CIPHER_MODE_CCM) {
1801 crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));
1802 memcpy(rctx->msg_buf.iv_ctr, req->info, rctx->iv_ctr_len);
1804 rctx->iv_ctr_len = 0;
1807 /* Choose a SPU to process this request */
1808 rctx->chan_idx = select_channel();
1809 err = handle_ablkcipher_req(rctx);
1810 if (err != -EINPROGRESS)
1811 /* synchronous result */
1812 spu_chunk_cleanup(rctx);
1817 static int des_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1818 unsigned int keylen)
1820 struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1821 u32 tmp[DES_EXPKEY_WORDS];
1823 if (keylen == DES_KEY_SIZE) {
1824 if (des_ekey(tmp, key) == 0) {
1825 if (crypto_ablkcipher_get_flags(cipher) &
1826 CRYPTO_TFM_REQ_WEAK_KEY) {
1827 u32 flags = CRYPTO_TFM_RES_WEAK_KEY;
1829 crypto_ablkcipher_set_flags(cipher, flags);
1834 ctx->cipher_type = CIPHER_TYPE_DES;
1836 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1842 static int threedes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1843 unsigned int keylen)
1845 struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1847 if (keylen == (DES_KEY_SIZE * 3)) {
1848 const u32 *K = (const u32 *)key;
1849 u32 flags = CRYPTO_TFM_RES_BAD_KEY_SCHED;
1851 if (!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
1852 !((K[2] ^ K[4]) | (K[3] ^ K[5]))) {
1853 crypto_ablkcipher_set_flags(cipher, flags);
1857 ctx->cipher_type = CIPHER_TYPE_3DES;
1859 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1865 static int aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1866 unsigned int keylen)
1868 struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1870 if (ctx->cipher.mode == CIPHER_MODE_XTS)
1871 /* XTS includes two keys of equal length */
1872 keylen = keylen / 2;
1875 case AES_KEYSIZE_128:
1876 ctx->cipher_type = CIPHER_TYPE_AES128;
1878 case AES_KEYSIZE_192:
1879 ctx->cipher_type = CIPHER_TYPE_AES192;
1881 case AES_KEYSIZE_256:
1882 ctx->cipher_type = CIPHER_TYPE_AES256;
1885 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1888 WARN_ON((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
1889 ((ctx->max_payload % AES_BLOCK_SIZE) != 0));
1893 static int rc4_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1894 unsigned int keylen)
1896 struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1899 ctx->enckeylen = ARC4_MAX_KEY_SIZE + ARC4_STATE_SIZE;
1901 ctx->enckey[0] = 0x00; /* 0x00 */
1902 ctx->enckey[1] = 0x00; /* i */
1903 ctx->enckey[2] = 0x00; /* 0x00 */
1904 ctx->enckey[3] = 0x00; /* j */
1905 for (i = 0; i < ARC4_MAX_KEY_SIZE; i++)
1906 ctx->enckey[i + ARC4_STATE_SIZE] = key[i % keylen];
1908 ctx->cipher_type = CIPHER_TYPE_INIT;
1913 static int ablkcipher_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1914 unsigned int keylen)
1916 struct spu_hw *spu = &iproc_priv.spu;
1917 struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1918 struct spu_cipher_parms cipher_parms;
1922 flow_log("ablkcipher_setkey() keylen: %d\n", keylen);
1923 flow_dump(" key: ", key, keylen);
1925 switch (ctx->cipher.alg) {
1926 case CIPHER_ALG_DES:
1927 err = des_setkey(cipher, key, keylen);
1929 case CIPHER_ALG_3DES:
1930 err = threedes_setkey(cipher, key, keylen);
1932 case CIPHER_ALG_AES:
1933 err = aes_setkey(cipher, key, keylen);
1935 case CIPHER_ALG_RC4:
1936 err = rc4_setkey(cipher, key, keylen);
1939 pr_err("%s() Error: unknown cipher alg\n", __func__);
1945 /* RC4 already populated ctx->enkey */
1946 if (ctx->cipher.alg != CIPHER_ALG_RC4) {
1947 memcpy(ctx->enckey, key, keylen);
1948 ctx->enckeylen = keylen;
1950 /* SPU needs XTS keys in the reverse order the crypto API presents */
1951 if ((ctx->cipher.alg == CIPHER_ALG_AES) &&
1952 (ctx->cipher.mode == CIPHER_MODE_XTS)) {
1953 unsigned int xts_keylen = keylen / 2;
1955 memcpy(ctx->enckey, key + xts_keylen, xts_keylen);
1956 memcpy(ctx->enckey + xts_keylen, key, xts_keylen);
1959 if (spu->spu_type == SPU_TYPE_SPUM)
1960 alloc_len = BCM_HDR_LEN + SPU_HEADER_ALLOC_LEN;
1961 else if (spu->spu_type == SPU_TYPE_SPU2)
1962 alloc_len = BCM_HDR_LEN + SPU2_HEADER_ALLOC_LEN;
1963 memset(ctx->bcm_spu_req_hdr, 0, alloc_len);
1964 cipher_parms.iv_buf = NULL;
1965 cipher_parms.iv_len = crypto_ablkcipher_ivsize(cipher);
1966 flow_log("%s: iv_len %u\n", __func__, cipher_parms.iv_len);
1968 cipher_parms.alg = ctx->cipher.alg;
1969 cipher_parms.mode = ctx->cipher.mode;
1970 cipher_parms.type = ctx->cipher_type;
1971 cipher_parms.key_buf = ctx->enckey;
1972 cipher_parms.key_len = ctx->enckeylen;
1974 /* Prepend SPU request message with BCM header */
1975 memcpy(ctx->bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
1976 ctx->spu_req_hdr_len =
1977 spu->spu_cipher_req_init(ctx->bcm_spu_req_hdr + BCM_HDR_LEN,
1980 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
1984 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_CIPHER]);
1989 static int ablkcipher_encrypt(struct ablkcipher_request *req)
1991 flow_log("ablkcipher_encrypt() nbytes:%u\n", req->nbytes);
1993 return ablkcipher_enqueue(req, true);
1996 static int ablkcipher_decrypt(struct ablkcipher_request *req)
1998 flow_log("ablkcipher_decrypt() nbytes:%u\n", req->nbytes);
1999 return ablkcipher_enqueue(req, false);
2002 static int ahash_enqueue(struct ahash_request *req)
2004 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2005 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2006 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2008 const char *alg_name;
2010 flow_log("ahash_enqueue() nbytes:%u\n", req->nbytes);
2012 rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2013 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2014 rctx->parent = &req->base;
2016 rctx->bd_suppress = true;
2017 memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
2019 /* Initialize position in src scatterlist */
2020 rctx->src_sg = req->src;
2022 rctx->src_nents = 0;
2023 rctx->dst_sg = NULL;
2025 rctx->dst_nents = 0;
2027 /* SPU2 hardware does not compute hash of zero length data */
2028 if ((rctx->is_final == 1) && (rctx->total_todo == 0) &&
2029 (iproc_priv.spu.spu_type == SPU_TYPE_SPU2)) {
2030 alg_name = crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
2031 flow_log("Doing %sfinal %s zero-len hash request in software\n",
2032 rctx->is_final ? "" : "non-", alg_name);
2033 err = do_shash((unsigned char *)alg_name, req->result,
2034 NULL, 0, NULL, 0, ctx->authkey,
2037 flow_log("Hash request failed with error %d\n", err);
2040 /* Choose a SPU to process this request */
2041 rctx->chan_idx = select_channel();
2043 err = handle_ahash_req(rctx);
2044 if (err != -EINPROGRESS)
2045 /* synchronous result */
2046 spu_chunk_cleanup(rctx);
2050 * we saved data in hash carry, but tell crypto API
2051 * we successfully completed request.
2058 static int __ahash_init(struct ahash_request *req)
2060 struct spu_hw *spu = &iproc_priv.spu;
2061 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2062 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2063 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2065 flow_log("%s()\n", __func__);
2067 /* Initialize the context */
2068 rctx->hash_carry_len = 0;
2071 rctx->total_todo = 0;
2073 rctx->total_sent = 0;
2074 rctx->total_received = 0;
2076 ctx->digestsize = crypto_ahash_digestsize(tfm);
2077 /* If we add a hash whose digest is larger, catch it here. */
2078 WARN_ON(ctx->digestsize > MAX_DIGEST_SIZE);
2080 rctx->is_sw_hmac = false;
2082 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen, 0,
2089 * spu_no_incr_hash() - Determine whether incremental hashing is supported.
2090 * @ctx: Crypto session context
2092 * SPU-2 does not support incremental hashing (we'll have to revisit and
2093 * condition based on chip revision or device tree entry if future versions do
2094 * support incremental hash)
2096 * SPU-M also doesn't support incremental hashing of AES-XCBC
2098 * Return: true if incremental hashing is not supported
2101 bool spu_no_incr_hash(struct iproc_ctx_s *ctx)
2103 struct spu_hw *spu = &iproc_priv.spu;
2105 if (spu->spu_type == SPU_TYPE_SPU2)
2108 if ((ctx->auth.alg == HASH_ALG_AES) &&
2109 (ctx->auth.mode == HASH_MODE_XCBC))
2112 /* Otherwise, incremental hashing is supported */
2116 static int ahash_init(struct ahash_request *req)
2118 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2119 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2120 const char *alg_name;
2121 struct crypto_shash *hash;
2125 if (spu_no_incr_hash(ctx)) {
2127 * If we get an incremental hashing request and it's not
2128 * supported by the hardware, we need to handle it in software
2129 * by calling synchronous hash functions.
2131 alg_name = crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
2132 hash = crypto_alloc_shash(alg_name, 0, 0);
2134 ret = PTR_ERR(hash);
2138 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2139 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2140 ctx->shash = kmalloc(sizeof(*ctx->shash) +
2141 crypto_shash_descsize(hash), gfp);
2146 ctx->shash->tfm = hash;
2147 ctx->shash->flags = 0;
2149 /* Set the key using data we already have from setkey */
2150 if (ctx->authkeylen > 0) {
2151 ret = crypto_shash_setkey(hash, ctx->authkey,
2157 /* Initialize hash w/ this key and other params */
2158 ret = crypto_shash_init(ctx->shash);
2162 /* Otherwise call the internal function which uses SPU hw */
2163 ret = __ahash_init(req);
2171 crypto_free_shash(hash);
2176 static int __ahash_update(struct ahash_request *req)
2178 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2180 flow_log("ahash_update() nbytes:%u\n", req->nbytes);
2184 rctx->total_todo += req->nbytes;
2187 return ahash_enqueue(req);
2190 static int ahash_update(struct ahash_request *req)
2192 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2193 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2199 if (spu_no_incr_hash(ctx)) {
2201 * If we get an incremental hashing request and it's not
2202 * supported by the hardware, we need to handle it in software
2203 * by calling synchronous hash functions.
2206 nents = sg_nents(req->src);
2210 /* Copy data from req scatterlist to tmp buffer */
2211 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2212 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2213 tmpbuf = kmalloc(req->nbytes, gfp);
2217 if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
2223 /* Call synchronous update */
2224 ret = crypto_shash_update(ctx->shash, tmpbuf, req->nbytes);
2227 /* Otherwise call the internal function which uses SPU hw */
2228 ret = __ahash_update(req);
2234 static int __ahash_final(struct ahash_request *req)
2236 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2238 flow_log("ahash_final() nbytes:%u\n", req->nbytes);
2242 return ahash_enqueue(req);
2245 static int ahash_final(struct ahash_request *req)
2247 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2248 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2251 if (spu_no_incr_hash(ctx)) {
2253 * If we get an incremental hashing request and it's not
2254 * supported by the hardware, we need to handle it in software
2255 * by calling synchronous hash functions.
2257 ret = crypto_shash_final(ctx->shash, req->result);
2259 /* Done with hash, can deallocate it now */
2260 crypto_free_shash(ctx->shash->tfm);
2264 /* Otherwise call the internal function which uses SPU hw */
2265 ret = __ahash_final(req);
2271 static int __ahash_finup(struct ahash_request *req)
2273 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2275 flow_log("ahash_finup() nbytes:%u\n", req->nbytes);
2277 rctx->total_todo += req->nbytes;
2281 return ahash_enqueue(req);
2284 static int ahash_finup(struct ahash_request *req)
2286 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2287 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2293 if (spu_no_incr_hash(ctx)) {
2295 * If we get an incremental hashing request and it's not
2296 * supported by the hardware, we need to handle it in software
2297 * by calling synchronous hash functions.
2300 nents = sg_nents(req->src);
2303 goto ahash_finup_exit;
2306 /* Copy data from req scatterlist to tmp buffer */
2307 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2308 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2309 tmpbuf = kmalloc(req->nbytes, gfp);
2312 goto ahash_finup_exit;
2315 if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
2318 goto ahash_finup_free;
2321 /* Call synchronous update */
2322 ret = crypto_shash_finup(ctx->shash, tmpbuf, req->nbytes,
2325 /* Otherwise call the internal function which uses SPU hw */
2326 return __ahash_finup(req);
2332 /* Done with hash, can deallocate it now */
2333 crypto_free_shash(ctx->shash->tfm);
2338 static int ahash_digest(struct ahash_request *req)
2342 flow_log("ahash_digest() nbytes:%u\n", req->nbytes);
2344 /* whole thing at once */
2345 err = __ahash_init(req);
2347 err = __ahash_finup(req);
2352 static int ahash_setkey(struct crypto_ahash *ahash, const u8 *key,
2353 unsigned int keylen)
2355 struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
2357 flow_log("%s() ahash:%p key:%p keylen:%u\n",
2358 __func__, ahash, key, keylen);
2359 flow_dump(" key: ", key, keylen);
2361 if (ctx->auth.alg == HASH_ALG_AES) {
2363 case AES_KEYSIZE_128:
2364 ctx->cipher_type = CIPHER_TYPE_AES128;
2366 case AES_KEYSIZE_192:
2367 ctx->cipher_type = CIPHER_TYPE_AES192;
2369 case AES_KEYSIZE_256:
2370 ctx->cipher_type = CIPHER_TYPE_AES256;
2373 pr_err("%s() Error: Invalid key length\n", __func__);
2377 pr_err("%s() Error: unknown hash alg\n", __func__);
2380 memcpy(ctx->authkey, key, keylen);
2381 ctx->authkeylen = keylen;
2386 static int ahash_export(struct ahash_request *req, void *out)
2388 const struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2389 struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)out;
2391 spu_exp->total_todo = rctx->total_todo;
2392 spu_exp->total_sent = rctx->total_sent;
2393 spu_exp->is_sw_hmac = rctx->is_sw_hmac;
2394 memcpy(spu_exp->hash_carry, rctx->hash_carry, sizeof(rctx->hash_carry));
2395 spu_exp->hash_carry_len = rctx->hash_carry_len;
2396 memcpy(spu_exp->incr_hash, rctx->incr_hash, sizeof(rctx->incr_hash));
2401 static int ahash_import(struct ahash_request *req, const void *in)
2403 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2404 struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)in;
2406 rctx->total_todo = spu_exp->total_todo;
2407 rctx->total_sent = spu_exp->total_sent;
2408 rctx->is_sw_hmac = spu_exp->is_sw_hmac;
2409 memcpy(rctx->hash_carry, spu_exp->hash_carry, sizeof(rctx->hash_carry));
2410 rctx->hash_carry_len = spu_exp->hash_carry_len;
2411 memcpy(rctx->incr_hash, spu_exp->incr_hash, sizeof(rctx->incr_hash));
2416 static int ahash_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,
2417 unsigned int keylen)
2419 struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
2420 unsigned int blocksize =
2421 crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
2422 unsigned int digestsize = crypto_ahash_digestsize(ahash);
2426 flow_log("%s() ahash:%p key:%p keylen:%u blksz:%u digestsz:%u\n",
2427 __func__, ahash, key, keylen, blocksize, digestsize);
2428 flow_dump(" key: ", key, keylen);
2430 if (keylen > blocksize) {
2431 switch (ctx->auth.alg) {
2433 rc = do_shash("md5", ctx->authkey, key, keylen, NULL,
2437 rc = do_shash("sha1", ctx->authkey, key, keylen, NULL,
2440 case HASH_ALG_SHA224:
2441 rc = do_shash("sha224", ctx->authkey, key, keylen, NULL,
2444 case HASH_ALG_SHA256:
2445 rc = do_shash("sha256", ctx->authkey, key, keylen, NULL,
2448 case HASH_ALG_SHA384:
2449 rc = do_shash("sha384", ctx->authkey, key, keylen, NULL,
2452 case HASH_ALG_SHA512:
2453 rc = do_shash("sha512", ctx->authkey, key, keylen, NULL,
2456 case HASH_ALG_SHA3_224:
2457 rc = do_shash("sha3-224", ctx->authkey, key, keylen,
2460 case HASH_ALG_SHA3_256:
2461 rc = do_shash("sha3-256", ctx->authkey, key, keylen,
2464 case HASH_ALG_SHA3_384:
2465 rc = do_shash("sha3-384", ctx->authkey, key, keylen,
2468 case HASH_ALG_SHA3_512:
2469 rc = do_shash("sha3-512", ctx->authkey, key, keylen,
2473 pr_err("%s() Error: unknown hash alg\n", __func__);
2477 pr_err("%s() Error %d computing shash for %s\n",
2478 __func__, rc, hash_alg_name[ctx->auth.alg]);
2481 ctx->authkeylen = digestsize;
2483 flow_log(" keylen > digestsize... hashed\n");
2484 flow_dump(" newkey: ", ctx->authkey, ctx->authkeylen);
2486 memcpy(ctx->authkey, key, keylen);
2487 ctx->authkeylen = keylen;
2491 * Full HMAC operation in SPUM is not verified,
2492 * So keeping the generation of IPAD, OPAD and
2493 * outer hashing in software.
2495 if (iproc_priv.spu.spu_type == SPU_TYPE_SPUM) {
2496 memcpy(ctx->ipad, ctx->authkey, ctx->authkeylen);
2497 memset(ctx->ipad + ctx->authkeylen, 0,
2498 blocksize - ctx->authkeylen);
2499 ctx->authkeylen = 0;
2500 memcpy(ctx->opad, ctx->ipad, blocksize);
2502 for (index = 0; index < blocksize; index++) {
2503 ctx->ipad[index] ^= HMAC_IPAD_VALUE;
2504 ctx->opad[index] ^= HMAC_OPAD_VALUE;
2507 flow_dump(" ipad: ", ctx->ipad, blocksize);
2508 flow_dump(" opad: ", ctx->opad, blocksize);
2510 ctx->digestsize = digestsize;
2511 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_HMAC]);
2516 static int ahash_hmac_init(struct ahash_request *req)
2518 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2519 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2520 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2521 unsigned int blocksize =
2522 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2524 flow_log("ahash_hmac_init()\n");
2526 /* init the context as a hash */
2529 if (!spu_no_incr_hash(ctx)) {
2530 /* SPU-M can do incr hashing but needs sw for outer HMAC */
2531 rctx->is_sw_hmac = true;
2532 ctx->auth.mode = HASH_MODE_HASH;
2533 /* start with a prepended ipad */
2534 memcpy(rctx->hash_carry, ctx->ipad, blocksize);
2535 rctx->hash_carry_len = blocksize;
2536 rctx->total_todo += blocksize;
2542 static int ahash_hmac_update(struct ahash_request *req)
2544 flow_log("ahash_hmac_update() nbytes:%u\n", req->nbytes);
2549 return ahash_update(req);
2552 static int ahash_hmac_final(struct ahash_request *req)
2554 flow_log("ahash_hmac_final() nbytes:%u\n", req->nbytes);
2556 return ahash_final(req);
2559 static int ahash_hmac_finup(struct ahash_request *req)
2561 flow_log("ahash_hmac_finupl() nbytes:%u\n", req->nbytes);
2563 return ahash_finup(req);
2566 static int ahash_hmac_digest(struct ahash_request *req)
2568 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2569 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2570 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2571 unsigned int blocksize =
2572 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2574 flow_log("ahash_hmac_digest() nbytes:%u\n", req->nbytes);
2576 /* Perform initialization and then call finup */
2579 if (iproc_priv.spu.spu_type == SPU_TYPE_SPU2) {
2581 * SPU2 supports full HMAC implementation in the
2582 * hardware, need not to generate IPAD, OPAD and
2583 * outer hash in software.
2584 * Only for hash key len > hash block size, SPU2
2585 * expects to perform hashing on the key, shorten
2586 * it to digest size and feed it as hash key.
2588 rctx->is_sw_hmac = false;
2589 ctx->auth.mode = HASH_MODE_HMAC;
2591 rctx->is_sw_hmac = true;
2592 ctx->auth.mode = HASH_MODE_HASH;
2593 /* start with a prepended ipad */
2594 memcpy(rctx->hash_carry, ctx->ipad, blocksize);
2595 rctx->hash_carry_len = blocksize;
2596 rctx->total_todo += blocksize;
2599 return __ahash_finup(req);
2604 static int aead_need_fallback(struct aead_request *req)
2606 struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2607 struct spu_hw *spu = &iproc_priv.spu;
2608 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2609 struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
2613 * SPU hardware cannot handle the AES-GCM/CCM case where plaintext
2614 * and AAD are both 0 bytes long. So use fallback in this case.
2616 if (((ctx->cipher.mode == CIPHER_MODE_GCM) ||
2617 (ctx->cipher.mode == CIPHER_MODE_CCM)) &&
2618 (req->assoclen == 0)) {
2619 if ((rctx->is_encrypt && (req->cryptlen == 0)) ||
2620 (!rctx->is_encrypt && (req->cryptlen == ctx->digestsize))) {
2621 flow_log("AES GCM/CCM needs fallback for 0 len req\n");
2626 /* SPU-M hardware only supports CCM digest size of 8, 12, or 16 bytes */
2627 if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
2628 (spu->spu_type == SPU_TYPE_SPUM) &&
2629 (ctx->digestsize != 8) && (ctx->digestsize != 12) &&
2630 (ctx->digestsize != 16)) {
2631 flow_log("%s() AES CCM needs fallback for digest size %d\n",
2632 __func__, ctx->digestsize);
2637 * SPU-M on NSP has an issue where AES-CCM hash is not correct
2638 * when AAD size is 0
2640 if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
2641 (spu->spu_subtype == SPU_SUBTYPE_SPUM_NSP) &&
2642 (req->assoclen == 0)) {
2643 flow_log("%s() AES_CCM needs fallback for 0 len AAD on NSP\n",
2648 payload_len = req->cryptlen;
2649 if (spu->spu_type == SPU_TYPE_SPUM)
2650 payload_len += req->assoclen;
2652 flow_log("%s() payload len: %u\n", __func__, payload_len);
2654 if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
2657 return payload_len > ctx->max_payload;
2660 static void aead_complete(struct crypto_async_request *areq, int err)
2662 struct aead_request *req =
2663 container_of(areq, struct aead_request, base);
2664 struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2665 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2667 flow_log("%s() err:%d\n", __func__, err);
2669 areq->tfm = crypto_aead_tfm(aead);
2671 areq->complete = rctx->old_complete;
2672 areq->data = rctx->old_data;
2674 areq->complete(areq, err);
2677 static int aead_do_fallback(struct aead_request *req, bool is_encrypt)
2679 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2680 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
2681 struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2682 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
2686 flow_log("%s() enc:%u\n", __func__, is_encrypt);
2688 if (ctx->fallback_cipher) {
2689 /* Store the cipher tfm and then use the fallback tfm */
2690 rctx->old_tfm = tfm;
2691 aead_request_set_tfm(req, ctx->fallback_cipher);
2693 * Save the callback and chain ourselves in, so we can restore
2696 rctx->old_complete = req->base.complete;
2697 rctx->old_data = req->base.data;
2698 req_flags = aead_request_flags(req);
2699 aead_request_set_callback(req, req_flags, aead_complete, req);
2700 err = is_encrypt ? crypto_aead_encrypt(req) :
2701 crypto_aead_decrypt(req);
2705 * fallback was synchronous (did not return
2706 * -EINPROGRESS). So restore request state here.
2708 aead_request_set_callback(req, req_flags,
2709 rctx->old_complete, req);
2710 req->base.data = rctx->old_data;
2711 aead_request_set_tfm(req, aead);
2712 flow_log("%s() fallback completed successfully\n\n",
2722 static int aead_enqueue(struct aead_request *req, bool is_encrypt)
2724 struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2725 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2726 struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
2729 flow_log("%s() enc:%u\n", __func__, is_encrypt);
2731 if (req->assoclen > MAX_ASSOC_SIZE) {
2733 ("%s() Error: associated data too long. (%u > %u bytes)\n",
2734 __func__, req->assoclen, MAX_ASSOC_SIZE);
2738 rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2739 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2740 rctx->parent = &req->base;
2741 rctx->is_encrypt = is_encrypt;
2742 rctx->bd_suppress = false;
2743 rctx->total_todo = req->cryptlen;
2745 rctx->total_sent = 0;
2746 rctx->total_received = 0;
2747 rctx->is_sw_hmac = false;
2749 memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
2751 /* assoc data is at start of src sg */
2752 rctx->assoc = req->src;
2755 * Init current position in src scatterlist to be after assoc data.
2756 * src_skip set to buffer offset where data begins. (Assoc data could
2757 * end in the middle of a buffer.)
2759 if (spu_sg_at_offset(req->src, req->assoclen, &rctx->src_sg,
2760 &rctx->src_skip) < 0) {
2761 pr_err("%s() Error: Unable to find start of src data\n",
2766 rctx->src_nents = 0;
2767 rctx->dst_nents = 0;
2768 if (req->dst == req->src) {
2769 rctx->dst_sg = rctx->src_sg;
2770 rctx->dst_skip = rctx->src_skip;
2773 * Expect req->dst to have room for assoc data followed by
2774 * output data and ICV, if encrypt. So initialize dst_sg
2775 * to point beyond assoc len offset.
2777 if (spu_sg_at_offset(req->dst, req->assoclen, &rctx->dst_sg,
2778 &rctx->dst_skip) < 0) {
2779 pr_err("%s() Error: Unable to find start of dst data\n",
2785 if (ctx->cipher.mode == CIPHER_MODE_CBC ||
2786 ctx->cipher.mode == CIPHER_MODE_CTR ||
2787 ctx->cipher.mode == CIPHER_MODE_OFB ||
2788 ctx->cipher.mode == CIPHER_MODE_XTS ||
2789 ctx->cipher.mode == CIPHER_MODE_GCM) {
2792 crypto_aead_ivsize(crypto_aead_reqtfm(req));
2793 } else if (ctx->cipher.mode == CIPHER_MODE_CCM) {
2794 rctx->iv_ctr_len = CCM_AES_IV_SIZE;
2796 rctx->iv_ctr_len = 0;
2799 rctx->hash_carry_len = 0;
2801 flow_log(" src sg: %p\n", req->src);
2802 flow_log(" rctx->src_sg: %p, src_skip %u\n",
2803 rctx->src_sg, rctx->src_skip);
2804 flow_log(" assoc: %p, assoclen %u\n", rctx->assoc, req->assoclen);
2805 flow_log(" dst sg: %p\n", req->dst);
2806 flow_log(" rctx->dst_sg: %p, dst_skip %u\n",
2807 rctx->dst_sg, rctx->dst_skip);
2808 flow_log(" iv_ctr_len:%u\n", rctx->iv_ctr_len);
2809 flow_dump(" iv: ", req->iv, rctx->iv_ctr_len);
2810 flow_log(" authkeylen:%u\n", ctx->authkeylen);
2811 flow_log(" is_esp: %s\n", ctx->is_esp ? "yes" : "no");
2813 if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
2814 flow_log(" max_payload infinite");
2816 flow_log(" max_payload: %u\n", ctx->max_payload);
2818 if (unlikely(aead_need_fallback(req)))
2819 return aead_do_fallback(req, is_encrypt);
2822 * Do memory allocations for request after fallback check, because if we
2823 * do fallback, we won't call finish_req() to dealloc.
2825 if (rctx->iv_ctr_len) {
2827 memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset,
2828 ctx->salt, ctx->salt_len);
2829 memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset + ctx->salt_len,
2831 rctx->iv_ctr_len - ctx->salt_len - ctx->salt_offset);
2834 rctx->chan_idx = select_channel();
2835 err = handle_aead_req(rctx);
2836 if (err != -EINPROGRESS)
2837 /* synchronous result */
2838 spu_chunk_cleanup(rctx);
2843 static int aead_authenc_setkey(struct crypto_aead *cipher,
2844 const u8 *key, unsigned int keylen)
2846 struct spu_hw *spu = &iproc_priv.spu;
2847 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2848 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
2849 struct rtattr *rta = (void *)key;
2850 struct crypto_authenc_key_param *param;
2851 const u8 *origkey = key;
2852 const unsigned int origkeylen = keylen;
2856 flow_log("%s() aead:%p key:%p keylen:%u\n", __func__, cipher, key,
2858 flow_dump(" key: ", key, keylen);
2860 if (!RTA_OK(rta, keylen))
2862 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
2864 if (RTA_PAYLOAD(rta) < sizeof(*param))
2867 param = RTA_DATA(rta);
2868 ctx->enckeylen = be32_to_cpu(param->enckeylen);
2870 key += RTA_ALIGN(rta->rta_len);
2871 keylen -= RTA_ALIGN(rta->rta_len);
2873 if (keylen < ctx->enckeylen)
2875 if (ctx->enckeylen > MAX_KEY_SIZE)
2878 ctx->authkeylen = keylen - ctx->enckeylen;
2880 if (ctx->authkeylen > MAX_KEY_SIZE)
2883 memcpy(ctx->enckey, key + ctx->authkeylen, ctx->enckeylen);
2884 /* May end up padding auth key. So make sure it's zeroed. */
2885 memset(ctx->authkey, 0, sizeof(ctx->authkey));
2886 memcpy(ctx->authkey, key, ctx->authkeylen);
2888 switch (ctx->alg->cipher_info.alg) {
2889 case CIPHER_ALG_DES:
2890 if (ctx->enckeylen == DES_KEY_SIZE) {
2891 u32 tmp[DES_EXPKEY_WORDS];
2892 u32 flags = CRYPTO_TFM_RES_WEAK_KEY;
2894 if (des_ekey(tmp, key) == 0) {
2895 if (crypto_aead_get_flags(cipher) &
2896 CRYPTO_TFM_REQ_WEAK_KEY) {
2897 crypto_aead_set_flags(cipher, flags);
2902 ctx->cipher_type = CIPHER_TYPE_DES;
2907 case CIPHER_ALG_3DES:
2908 if (ctx->enckeylen == (DES_KEY_SIZE * 3)) {
2909 const u32 *K = (const u32 *)key;
2910 u32 flags = CRYPTO_TFM_RES_BAD_KEY_SCHED;
2912 if (!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
2913 !((K[2] ^ K[4]) | (K[3] ^ K[5]))) {
2914 crypto_aead_set_flags(cipher, flags);
2918 ctx->cipher_type = CIPHER_TYPE_3DES;
2920 crypto_aead_set_flags(cipher,
2921 CRYPTO_TFM_RES_BAD_KEY_LEN);
2925 case CIPHER_ALG_AES:
2926 switch (ctx->enckeylen) {
2927 case AES_KEYSIZE_128:
2928 ctx->cipher_type = CIPHER_TYPE_AES128;
2930 case AES_KEYSIZE_192:
2931 ctx->cipher_type = CIPHER_TYPE_AES192;
2933 case AES_KEYSIZE_256:
2934 ctx->cipher_type = CIPHER_TYPE_AES256;
2940 case CIPHER_ALG_RC4:
2941 ctx->cipher_type = CIPHER_TYPE_INIT;
2944 pr_err("%s() Error: Unknown cipher alg\n", __func__);
2948 flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
2950 flow_dump(" enc: ", ctx->enckey, ctx->enckeylen);
2951 flow_dump(" auth: ", ctx->authkey, ctx->authkeylen);
2953 /* setkey the fallback just in case we needto use it */
2954 if (ctx->fallback_cipher) {
2955 flow_log(" running fallback setkey()\n");
2957 ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
2958 ctx->fallback_cipher->base.crt_flags |=
2959 tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
2961 crypto_aead_setkey(ctx->fallback_cipher, origkey,
2964 flow_log(" fallback setkey() returned:%d\n", ret);
2965 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
2967 (ctx->fallback_cipher->base.crt_flags &
2968 CRYPTO_TFM_RES_MASK);
2972 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
2976 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
2982 ctx->authkeylen = 0;
2983 ctx->digestsize = 0;
2985 crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
2989 static int aead_gcm_ccm_setkey(struct crypto_aead *cipher,
2990 const u8 *key, unsigned int keylen)
2992 struct spu_hw *spu = &iproc_priv.spu;
2993 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2994 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
2998 flow_log("%s() keylen:%u\n", __func__, keylen);
2999 flow_dump(" key: ", key, keylen);
3002 ctx->digestsize = keylen;
3004 ctx->enckeylen = keylen;
3005 ctx->authkeylen = 0;
3006 memcpy(ctx->enckey, key, ctx->enckeylen);
3008 switch (ctx->enckeylen) {
3009 case AES_KEYSIZE_128:
3010 ctx->cipher_type = CIPHER_TYPE_AES128;
3012 case AES_KEYSIZE_192:
3013 ctx->cipher_type = CIPHER_TYPE_AES192;
3015 case AES_KEYSIZE_256:
3016 ctx->cipher_type = CIPHER_TYPE_AES256;
3022 flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
3024 flow_dump(" enc: ", ctx->enckey, ctx->enckeylen);
3025 flow_dump(" auth: ", ctx->authkey, ctx->authkeylen);
3027 /* setkey the fallback just in case we need to use it */
3028 if (ctx->fallback_cipher) {
3029 flow_log(" running fallback setkey()\n");
3031 ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
3032 ctx->fallback_cipher->base.crt_flags |=
3033 tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
3034 ret = crypto_aead_setkey(ctx->fallback_cipher, key,
3035 keylen + ctx->salt_len);
3037 flow_log(" fallback setkey() returned:%d\n", ret);
3038 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
3040 (ctx->fallback_cipher->base.crt_flags &
3041 CRYPTO_TFM_RES_MASK);
3045 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
3049 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
3051 flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
3058 ctx->authkeylen = 0;
3059 ctx->digestsize = 0;
3061 crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
3066 * aead_gcm_esp_setkey() - setkey() operation for ESP variant of GCM AES.
3067 * @cipher: AEAD structure
3068 * @key: Key followed by 4 bytes of salt
3069 * @keylen: Length of key plus salt, in bytes
3071 * Extracts salt from key and stores it to be prepended to IV on each request.
3072 * Digest is always 16 bytes
3074 * Return: Value from generic gcm setkey.
3076 static int aead_gcm_esp_setkey(struct crypto_aead *cipher,
3077 const u8 *key, unsigned int keylen)
3079 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3081 flow_log("%s\n", __func__);
3082 ctx->salt_len = GCM_ESP_SALT_SIZE;
3083 ctx->salt_offset = GCM_ESP_SALT_OFFSET;
3084 memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
3085 keylen -= GCM_ESP_SALT_SIZE;
3086 ctx->digestsize = GCM_ESP_DIGESTSIZE;
3088 flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
3090 return aead_gcm_ccm_setkey(cipher, key, keylen);
3094 * rfc4543_gcm_esp_setkey() - setkey operation for RFC4543 variant of GCM/GMAC.
3095 * cipher: AEAD structure
3096 * key: Key followed by 4 bytes of salt
3097 * keylen: Length of key plus salt, in bytes
3099 * Extracts salt from key and stores it to be prepended to IV on each request.
3100 * Digest is always 16 bytes
3102 * Return: Value from generic gcm setkey.
3104 static int rfc4543_gcm_esp_setkey(struct crypto_aead *cipher,
3105 const u8 *key, unsigned int keylen)
3107 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3109 flow_log("%s\n", __func__);
3110 ctx->salt_len = GCM_ESP_SALT_SIZE;
3111 ctx->salt_offset = GCM_ESP_SALT_OFFSET;
3112 memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
3113 keylen -= GCM_ESP_SALT_SIZE;
3114 ctx->digestsize = GCM_ESP_DIGESTSIZE;
3116 ctx->is_rfc4543 = true;
3117 flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
3119 return aead_gcm_ccm_setkey(cipher, key, keylen);
3123 * aead_ccm_esp_setkey() - setkey() operation for ESP variant of CCM AES.
3124 * @cipher: AEAD structure
3125 * @key: Key followed by 4 bytes of salt
3126 * @keylen: Length of key plus salt, in bytes
3128 * Extracts salt from key and stores it to be prepended to IV on each request.
3129 * Digest is always 16 bytes
3131 * Return: Value from generic ccm setkey.
3133 static int aead_ccm_esp_setkey(struct crypto_aead *cipher,
3134 const u8 *key, unsigned int keylen)
3136 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3138 flow_log("%s\n", __func__);
3139 ctx->salt_len = CCM_ESP_SALT_SIZE;
3140 ctx->salt_offset = CCM_ESP_SALT_OFFSET;
3141 memcpy(ctx->salt, key + keylen - CCM_ESP_SALT_SIZE, CCM_ESP_SALT_SIZE);
3142 keylen -= CCM_ESP_SALT_SIZE;
3144 flow_dump("salt: ", ctx->salt, CCM_ESP_SALT_SIZE);
3146 return aead_gcm_ccm_setkey(cipher, key, keylen);
3149 static int aead_setauthsize(struct crypto_aead *cipher, unsigned int authsize)
3151 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3154 flow_log("%s() authkeylen:%u authsize:%u\n",
3155 __func__, ctx->authkeylen, authsize);
3157 ctx->digestsize = authsize;
3159 /* setkey the fallback just in case we needto use it */
3160 if (ctx->fallback_cipher) {
3161 flow_log(" running fallback setauth()\n");
3163 ret = crypto_aead_setauthsize(ctx->fallback_cipher, authsize);
3165 flow_log(" fallback setauth() returned:%d\n", ret);
3171 static int aead_encrypt(struct aead_request *req)
3173 flow_log("%s() cryptlen:%u %08x\n", __func__, req->cryptlen,
3175 dump_sg(req->src, 0, req->cryptlen + req->assoclen);
3176 flow_log(" assoc_len:%u\n", req->assoclen);
3178 return aead_enqueue(req, true);
3181 static int aead_decrypt(struct aead_request *req)
3183 flow_log("%s() cryptlen:%u\n", __func__, req->cryptlen);
3184 dump_sg(req->src, 0, req->cryptlen + req->assoclen);
3185 flow_log(" assoc_len:%u\n", req->assoclen);
3187 return aead_enqueue(req, false);
3190 /* ==================== Supported Cipher Algorithms ==================== */
3192 static struct iproc_alg_s driver_algs[] = {
3194 .type = CRYPTO_ALG_TYPE_AEAD,
3197 .cra_name = "gcm(aes)",
3198 .cra_driver_name = "gcm-aes-iproc",
3199 .cra_blocksize = AES_BLOCK_SIZE,
3200 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3202 .setkey = aead_gcm_ccm_setkey,
3203 .ivsize = GCM_AES_IV_SIZE,
3204 .maxauthsize = AES_BLOCK_SIZE,
3207 .alg = CIPHER_ALG_AES,
3208 .mode = CIPHER_MODE_GCM,
3211 .alg = HASH_ALG_AES,
3212 .mode = HASH_MODE_GCM,
3217 .type = CRYPTO_ALG_TYPE_AEAD,
3220 .cra_name = "ccm(aes)",
3221 .cra_driver_name = "ccm-aes-iproc",
3222 .cra_blocksize = AES_BLOCK_SIZE,
3223 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3225 .setkey = aead_gcm_ccm_setkey,
3226 .ivsize = CCM_AES_IV_SIZE,
3227 .maxauthsize = AES_BLOCK_SIZE,
3230 .alg = CIPHER_ALG_AES,
3231 .mode = CIPHER_MODE_CCM,
3234 .alg = HASH_ALG_AES,
3235 .mode = HASH_MODE_CCM,
3240 .type = CRYPTO_ALG_TYPE_AEAD,
3243 .cra_name = "rfc4106(gcm(aes))",
3244 .cra_driver_name = "gcm-aes-esp-iproc",
3245 .cra_blocksize = AES_BLOCK_SIZE,
3246 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3248 .setkey = aead_gcm_esp_setkey,
3249 .ivsize = GCM_RFC4106_IV_SIZE,
3250 .maxauthsize = AES_BLOCK_SIZE,
3253 .alg = CIPHER_ALG_AES,
3254 .mode = CIPHER_MODE_GCM,
3257 .alg = HASH_ALG_AES,
3258 .mode = HASH_MODE_GCM,
3263 .type = CRYPTO_ALG_TYPE_AEAD,
3266 .cra_name = "rfc4309(ccm(aes))",
3267 .cra_driver_name = "ccm-aes-esp-iproc",
3268 .cra_blocksize = AES_BLOCK_SIZE,
3269 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3271 .setkey = aead_ccm_esp_setkey,
3272 .ivsize = CCM_AES_IV_SIZE,
3273 .maxauthsize = AES_BLOCK_SIZE,
3276 .alg = CIPHER_ALG_AES,
3277 .mode = CIPHER_MODE_CCM,
3280 .alg = HASH_ALG_AES,
3281 .mode = HASH_MODE_CCM,
3286 .type = CRYPTO_ALG_TYPE_AEAD,
3289 .cra_name = "rfc4543(gcm(aes))",
3290 .cra_driver_name = "gmac-aes-esp-iproc",
3291 .cra_blocksize = AES_BLOCK_SIZE,
3292 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3294 .setkey = rfc4543_gcm_esp_setkey,
3295 .ivsize = GCM_RFC4106_IV_SIZE,
3296 .maxauthsize = AES_BLOCK_SIZE,
3299 .alg = CIPHER_ALG_AES,
3300 .mode = CIPHER_MODE_GCM,
3303 .alg = HASH_ALG_AES,
3304 .mode = HASH_MODE_GCM,
3309 .type = CRYPTO_ALG_TYPE_AEAD,
3312 .cra_name = "authenc(hmac(md5),cbc(aes))",
3313 .cra_driver_name = "authenc-hmac-md5-cbc-aes-iproc",
3314 .cra_blocksize = AES_BLOCK_SIZE,
3315 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3317 .setkey = aead_authenc_setkey,
3318 .ivsize = AES_BLOCK_SIZE,
3319 .maxauthsize = MD5_DIGEST_SIZE,
3322 .alg = CIPHER_ALG_AES,
3323 .mode = CIPHER_MODE_CBC,
3326 .alg = HASH_ALG_MD5,
3327 .mode = HASH_MODE_HMAC,
3332 .type = CRYPTO_ALG_TYPE_AEAD,
3335 .cra_name = "authenc(hmac(sha1),cbc(aes))",
3336 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-iproc",
3337 .cra_blocksize = AES_BLOCK_SIZE,
3338 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3340 .setkey = aead_authenc_setkey,
3341 .ivsize = AES_BLOCK_SIZE,
3342 .maxauthsize = SHA1_DIGEST_SIZE,
3345 .alg = CIPHER_ALG_AES,
3346 .mode = CIPHER_MODE_CBC,
3349 .alg = HASH_ALG_SHA1,
3350 .mode = HASH_MODE_HMAC,
3355 .type = CRYPTO_ALG_TYPE_AEAD,
3358 .cra_name = "authenc(hmac(sha256),cbc(aes))",
3359 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-iproc",
3360 .cra_blocksize = AES_BLOCK_SIZE,
3361 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3363 .setkey = aead_authenc_setkey,
3364 .ivsize = AES_BLOCK_SIZE,
3365 .maxauthsize = SHA256_DIGEST_SIZE,
3368 .alg = CIPHER_ALG_AES,
3369 .mode = CIPHER_MODE_CBC,
3372 .alg = HASH_ALG_SHA256,
3373 .mode = HASH_MODE_HMAC,
3378 .type = CRYPTO_ALG_TYPE_AEAD,
3381 .cra_name = "authenc(hmac(md5),cbc(des))",
3382 .cra_driver_name = "authenc-hmac-md5-cbc-des-iproc",
3383 .cra_blocksize = DES_BLOCK_SIZE,
3384 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3386 .setkey = aead_authenc_setkey,
3387 .ivsize = DES_BLOCK_SIZE,
3388 .maxauthsize = MD5_DIGEST_SIZE,
3391 .alg = CIPHER_ALG_DES,
3392 .mode = CIPHER_MODE_CBC,
3395 .alg = HASH_ALG_MD5,
3396 .mode = HASH_MODE_HMAC,
3401 .type = CRYPTO_ALG_TYPE_AEAD,
3404 .cra_name = "authenc(hmac(sha1),cbc(des))",
3405 .cra_driver_name = "authenc-hmac-sha1-cbc-des-iproc",
3406 .cra_blocksize = DES_BLOCK_SIZE,
3407 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3409 .setkey = aead_authenc_setkey,
3410 .ivsize = DES_BLOCK_SIZE,
3411 .maxauthsize = SHA1_DIGEST_SIZE,
3414 .alg = CIPHER_ALG_DES,
3415 .mode = CIPHER_MODE_CBC,
3418 .alg = HASH_ALG_SHA1,
3419 .mode = HASH_MODE_HMAC,
3424 .type = CRYPTO_ALG_TYPE_AEAD,
3427 .cra_name = "authenc(hmac(sha224),cbc(des))",
3428 .cra_driver_name = "authenc-hmac-sha224-cbc-des-iproc",
3429 .cra_blocksize = DES_BLOCK_SIZE,
3430 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3432 .setkey = aead_authenc_setkey,
3433 .ivsize = DES_BLOCK_SIZE,
3434 .maxauthsize = SHA224_DIGEST_SIZE,
3437 .alg = CIPHER_ALG_DES,
3438 .mode = CIPHER_MODE_CBC,
3441 .alg = HASH_ALG_SHA224,
3442 .mode = HASH_MODE_HMAC,
3447 .type = CRYPTO_ALG_TYPE_AEAD,
3450 .cra_name = "authenc(hmac(sha256),cbc(des))",
3451 .cra_driver_name = "authenc-hmac-sha256-cbc-des-iproc",
3452 .cra_blocksize = DES_BLOCK_SIZE,
3453 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3455 .setkey = aead_authenc_setkey,
3456 .ivsize = DES_BLOCK_SIZE,
3457 .maxauthsize = SHA256_DIGEST_SIZE,
3460 .alg = CIPHER_ALG_DES,
3461 .mode = CIPHER_MODE_CBC,
3464 .alg = HASH_ALG_SHA256,
3465 .mode = HASH_MODE_HMAC,
3470 .type = CRYPTO_ALG_TYPE_AEAD,
3473 .cra_name = "authenc(hmac(sha384),cbc(des))",
3474 .cra_driver_name = "authenc-hmac-sha384-cbc-des-iproc",
3475 .cra_blocksize = DES_BLOCK_SIZE,
3476 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3478 .setkey = aead_authenc_setkey,
3479 .ivsize = DES_BLOCK_SIZE,
3480 .maxauthsize = SHA384_DIGEST_SIZE,
3483 .alg = CIPHER_ALG_DES,
3484 .mode = CIPHER_MODE_CBC,
3487 .alg = HASH_ALG_SHA384,
3488 .mode = HASH_MODE_HMAC,
3493 .type = CRYPTO_ALG_TYPE_AEAD,
3496 .cra_name = "authenc(hmac(sha512),cbc(des))",
3497 .cra_driver_name = "authenc-hmac-sha512-cbc-des-iproc",
3498 .cra_blocksize = DES_BLOCK_SIZE,
3499 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3501 .setkey = aead_authenc_setkey,
3502 .ivsize = DES_BLOCK_SIZE,
3503 .maxauthsize = SHA512_DIGEST_SIZE,
3506 .alg = CIPHER_ALG_DES,
3507 .mode = CIPHER_MODE_CBC,
3510 .alg = HASH_ALG_SHA512,
3511 .mode = HASH_MODE_HMAC,
3516 .type = CRYPTO_ALG_TYPE_AEAD,
3519 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
3520 .cra_driver_name = "authenc-hmac-md5-cbc-des3-iproc",
3521 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3522 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3524 .setkey = aead_authenc_setkey,
3525 .ivsize = DES3_EDE_BLOCK_SIZE,
3526 .maxauthsize = MD5_DIGEST_SIZE,
3529 .alg = CIPHER_ALG_3DES,
3530 .mode = CIPHER_MODE_CBC,
3533 .alg = HASH_ALG_MD5,
3534 .mode = HASH_MODE_HMAC,
3539 .type = CRYPTO_ALG_TYPE_AEAD,
3542 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
3543 .cra_driver_name = "authenc-hmac-sha1-cbc-des3-iproc",
3544 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3545 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3547 .setkey = aead_authenc_setkey,
3548 .ivsize = DES3_EDE_BLOCK_SIZE,
3549 .maxauthsize = SHA1_DIGEST_SIZE,
3552 .alg = CIPHER_ALG_3DES,
3553 .mode = CIPHER_MODE_CBC,
3556 .alg = HASH_ALG_SHA1,
3557 .mode = HASH_MODE_HMAC,
3562 .type = CRYPTO_ALG_TYPE_AEAD,
3565 .cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
3566 .cra_driver_name = "authenc-hmac-sha224-cbc-des3-iproc",
3567 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3568 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3570 .setkey = aead_authenc_setkey,
3571 .ivsize = DES3_EDE_BLOCK_SIZE,
3572 .maxauthsize = SHA224_DIGEST_SIZE,
3575 .alg = CIPHER_ALG_3DES,
3576 .mode = CIPHER_MODE_CBC,
3579 .alg = HASH_ALG_SHA224,
3580 .mode = HASH_MODE_HMAC,
3585 .type = CRYPTO_ALG_TYPE_AEAD,
3588 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
3589 .cra_driver_name = "authenc-hmac-sha256-cbc-des3-iproc",
3590 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3591 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3593 .setkey = aead_authenc_setkey,
3594 .ivsize = DES3_EDE_BLOCK_SIZE,
3595 .maxauthsize = SHA256_DIGEST_SIZE,
3598 .alg = CIPHER_ALG_3DES,
3599 .mode = CIPHER_MODE_CBC,
3602 .alg = HASH_ALG_SHA256,
3603 .mode = HASH_MODE_HMAC,
3608 .type = CRYPTO_ALG_TYPE_AEAD,
3611 .cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
3612 .cra_driver_name = "authenc-hmac-sha384-cbc-des3-iproc",
3613 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3614 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3616 .setkey = aead_authenc_setkey,
3617 .ivsize = DES3_EDE_BLOCK_SIZE,
3618 .maxauthsize = SHA384_DIGEST_SIZE,
3621 .alg = CIPHER_ALG_3DES,
3622 .mode = CIPHER_MODE_CBC,
3625 .alg = HASH_ALG_SHA384,
3626 .mode = HASH_MODE_HMAC,
3631 .type = CRYPTO_ALG_TYPE_AEAD,
3634 .cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
3635 .cra_driver_name = "authenc-hmac-sha512-cbc-des3-iproc",
3636 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3637 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3639 .setkey = aead_authenc_setkey,
3640 .ivsize = DES3_EDE_BLOCK_SIZE,
3641 .maxauthsize = SHA512_DIGEST_SIZE,
3644 .alg = CIPHER_ALG_3DES,
3645 .mode = CIPHER_MODE_CBC,
3648 .alg = HASH_ALG_SHA512,
3649 .mode = HASH_MODE_HMAC,
3654 /* ABLKCIPHER algorithms. */
3656 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3658 .cra_name = "ecb(arc4)",
3659 .cra_driver_name = "ecb-arc4-iproc",
3660 .cra_blocksize = ARC4_BLOCK_SIZE,
3662 .min_keysize = ARC4_MIN_KEY_SIZE,
3663 .max_keysize = ARC4_MAX_KEY_SIZE,
3668 .alg = CIPHER_ALG_RC4,
3669 .mode = CIPHER_MODE_NONE,
3672 .alg = HASH_ALG_NONE,
3673 .mode = HASH_MODE_NONE,
3677 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3679 .cra_name = "ofb(des)",
3680 .cra_driver_name = "ofb-des-iproc",
3681 .cra_blocksize = DES_BLOCK_SIZE,
3683 .min_keysize = DES_KEY_SIZE,
3684 .max_keysize = DES_KEY_SIZE,
3685 .ivsize = DES_BLOCK_SIZE,
3689 .alg = CIPHER_ALG_DES,
3690 .mode = CIPHER_MODE_OFB,
3693 .alg = HASH_ALG_NONE,
3694 .mode = HASH_MODE_NONE,
3698 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3700 .cra_name = "cbc(des)",
3701 .cra_driver_name = "cbc-des-iproc",
3702 .cra_blocksize = DES_BLOCK_SIZE,
3704 .min_keysize = DES_KEY_SIZE,
3705 .max_keysize = DES_KEY_SIZE,
3706 .ivsize = DES_BLOCK_SIZE,
3710 .alg = CIPHER_ALG_DES,
3711 .mode = CIPHER_MODE_CBC,
3714 .alg = HASH_ALG_NONE,
3715 .mode = HASH_MODE_NONE,
3719 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3721 .cra_name = "ecb(des)",
3722 .cra_driver_name = "ecb-des-iproc",
3723 .cra_blocksize = DES_BLOCK_SIZE,
3725 .min_keysize = DES_KEY_SIZE,
3726 .max_keysize = DES_KEY_SIZE,
3731 .alg = CIPHER_ALG_DES,
3732 .mode = CIPHER_MODE_ECB,
3735 .alg = HASH_ALG_NONE,
3736 .mode = HASH_MODE_NONE,
3740 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3742 .cra_name = "ofb(des3_ede)",
3743 .cra_driver_name = "ofb-des3-iproc",
3744 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3746 .min_keysize = DES3_EDE_KEY_SIZE,
3747 .max_keysize = DES3_EDE_KEY_SIZE,
3748 .ivsize = DES3_EDE_BLOCK_SIZE,
3752 .alg = CIPHER_ALG_3DES,
3753 .mode = CIPHER_MODE_OFB,
3756 .alg = HASH_ALG_NONE,
3757 .mode = HASH_MODE_NONE,
3761 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3763 .cra_name = "cbc(des3_ede)",
3764 .cra_driver_name = "cbc-des3-iproc",
3765 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3767 .min_keysize = DES3_EDE_KEY_SIZE,
3768 .max_keysize = DES3_EDE_KEY_SIZE,
3769 .ivsize = DES3_EDE_BLOCK_SIZE,
3773 .alg = CIPHER_ALG_3DES,
3774 .mode = CIPHER_MODE_CBC,
3777 .alg = HASH_ALG_NONE,
3778 .mode = HASH_MODE_NONE,
3782 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3784 .cra_name = "ecb(des3_ede)",
3785 .cra_driver_name = "ecb-des3-iproc",
3786 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3788 .min_keysize = DES3_EDE_KEY_SIZE,
3789 .max_keysize = DES3_EDE_KEY_SIZE,
3794 .alg = CIPHER_ALG_3DES,
3795 .mode = CIPHER_MODE_ECB,
3798 .alg = HASH_ALG_NONE,
3799 .mode = HASH_MODE_NONE,
3803 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3805 .cra_name = "ofb(aes)",
3806 .cra_driver_name = "ofb-aes-iproc",
3807 .cra_blocksize = AES_BLOCK_SIZE,
3809 .min_keysize = AES_MIN_KEY_SIZE,
3810 .max_keysize = AES_MAX_KEY_SIZE,
3811 .ivsize = AES_BLOCK_SIZE,
3815 .alg = CIPHER_ALG_AES,
3816 .mode = CIPHER_MODE_OFB,
3819 .alg = HASH_ALG_NONE,
3820 .mode = HASH_MODE_NONE,
3824 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3826 .cra_name = "cbc(aes)",
3827 .cra_driver_name = "cbc-aes-iproc",
3828 .cra_blocksize = AES_BLOCK_SIZE,
3830 .min_keysize = AES_MIN_KEY_SIZE,
3831 .max_keysize = AES_MAX_KEY_SIZE,
3832 .ivsize = AES_BLOCK_SIZE,
3836 .alg = CIPHER_ALG_AES,
3837 .mode = CIPHER_MODE_CBC,
3840 .alg = HASH_ALG_NONE,
3841 .mode = HASH_MODE_NONE,
3845 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3847 .cra_name = "ecb(aes)",
3848 .cra_driver_name = "ecb-aes-iproc",
3849 .cra_blocksize = AES_BLOCK_SIZE,
3851 .min_keysize = AES_MIN_KEY_SIZE,
3852 .max_keysize = AES_MAX_KEY_SIZE,
3857 .alg = CIPHER_ALG_AES,
3858 .mode = CIPHER_MODE_ECB,
3861 .alg = HASH_ALG_NONE,
3862 .mode = HASH_MODE_NONE,
3866 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3868 .cra_name = "ctr(aes)",
3869 .cra_driver_name = "ctr-aes-iproc",
3870 .cra_blocksize = AES_BLOCK_SIZE,
3872 /* .geniv = "chainiv", */
3873 .min_keysize = AES_MIN_KEY_SIZE,
3874 .max_keysize = AES_MAX_KEY_SIZE,
3875 .ivsize = AES_BLOCK_SIZE,
3879 .alg = CIPHER_ALG_AES,
3880 .mode = CIPHER_MODE_CTR,
3883 .alg = HASH_ALG_NONE,
3884 .mode = HASH_MODE_NONE,
3888 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3890 .cra_name = "xts(aes)",
3891 .cra_driver_name = "xts-aes-iproc",
3892 .cra_blocksize = AES_BLOCK_SIZE,
3894 .min_keysize = 2 * AES_MIN_KEY_SIZE,
3895 .max_keysize = 2 * AES_MAX_KEY_SIZE,
3896 .ivsize = AES_BLOCK_SIZE,
3900 .alg = CIPHER_ALG_AES,
3901 .mode = CIPHER_MODE_XTS,
3904 .alg = HASH_ALG_NONE,
3905 .mode = HASH_MODE_NONE,
3909 /* AHASH algorithms. */
3911 .type = CRYPTO_ALG_TYPE_AHASH,
3913 .halg.digestsize = MD5_DIGEST_SIZE,
3916 .cra_driver_name = "md5-iproc",
3917 .cra_blocksize = MD5_BLOCK_WORDS * 4,
3918 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
3923 .alg = CIPHER_ALG_NONE,
3924 .mode = CIPHER_MODE_NONE,
3927 .alg = HASH_ALG_MD5,
3928 .mode = HASH_MODE_HASH,
3932 .type = CRYPTO_ALG_TYPE_AHASH,
3934 .halg.digestsize = MD5_DIGEST_SIZE,
3936 .cra_name = "hmac(md5)",
3937 .cra_driver_name = "hmac-md5-iproc",
3938 .cra_blocksize = MD5_BLOCK_WORDS * 4,
3942 .alg = CIPHER_ALG_NONE,
3943 .mode = CIPHER_MODE_NONE,
3946 .alg = HASH_ALG_MD5,
3947 .mode = HASH_MODE_HMAC,
3950 {.type = CRYPTO_ALG_TYPE_AHASH,
3952 .halg.digestsize = SHA1_DIGEST_SIZE,
3955 .cra_driver_name = "sha1-iproc",
3956 .cra_blocksize = SHA1_BLOCK_SIZE,
3960 .alg = CIPHER_ALG_NONE,
3961 .mode = CIPHER_MODE_NONE,
3964 .alg = HASH_ALG_SHA1,
3965 .mode = HASH_MODE_HASH,
3968 {.type = CRYPTO_ALG_TYPE_AHASH,
3970 .halg.digestsize = SHA1_DIGEST_SIZE,
3972 .cra_name = "hmac(sha1)",
3973 .cra_driver_name = "hmac-sha1-iproc",
3974 .cra_blocksize = SHA1_BLOCK_SIZE,
3978 .alg = CIPHER_ALG_NONE,
3979 .mode = CIPHER_MODE_NONE,
3982 .alg = HASH_ALG_SHA1,
3983 .mode = HASH_MODE_HMAC,
3986 {.type = CRYPTO_ALG_TYPE_AHASH,
3988 .halg.digestsize = SHA224_DIGEST_SIZE,
3990 .cra_name = "sha224",
3991 .cra_driver_name = "sha224-iproc",
3992 .cra_blocksize = SHA224_BLOCK_SIZE,
3996 .alg = CIPHER_ALG_NONE,
3997 .mode = CIPHER_MODE_NONE,
4000 .alg = HASH_ALG_SHA224,
4001 .mode = HASH_MODE_HASH,
4004 {.type = CRYPTO_ALG_TYPE_AHASH,
4006 .halg.digestsize = SHA224_DIGEST_SIZE,
4008 .cra_name = "hmac(sha224)",
4009 .cra_driver_name = "hmac-sha224-iproc",
4010 .cra_blocksize = SHA224_BLOCK_SIZE,
4014 .alg = CIPHER_ALG_NONE,
4015 .mode = CIPHER_MODE_NONE,
4018 .alg = HASH_ALG_SHA224,
4019 .mode = HASH_MODE_HMAC,
4022 {.type = CRYPTO_ALG_TYPE_AHASH,
4024 .halg.digestsize = SHA256_DIGEST_SIZE,
4026 .cra_name = "sha256",
4027 .cra_driver_name = "sha256-iproc",
4028 .cra_blocksize = SHA256_BLOCK_SIZE,
4032 .alg = CIPHER_ALG_NONE,
4033 .mode = CIPHER_MODE_NONE,
4036 .alg = HASH_ALG_SHA256,
4037 .mode = HASH_MODE_HASH,
4040 {.type = CRYPTO_ALG_TYPE_AHASH,
4042 .halg.digestsize = SHA256_DIGEST_SIZE,
4044 .cra_name = "hmac(sha256)",
4045 .cra_driver_name = "hmac-sha256-iproc",
4046 .cra_blocksize = SHA256_BLOCK_SIZE,
4050 .alg = CIPHER_ALG_NONE,
4051 .mode = CIPHER_MODE_NONE,
4054 .alg = HASH_ALG_SHA256,
4055 .mode = HASH_MODE_HMAC,
4059 .type = CRYPTO_ALG_TYPE_AHASH,
4061 .halg.digestsize = SHA384_DIGEST_SIZE,
4063 .cra_name = "sha384",
4064 .cra_driver_name = "sha384-iproc",
4065 .cra_blocksize = SHA384_BLOCK_SIZE,
4069 .alg = CIPHER_ALG_NONE,
4070 .mode = CIPHER_MODE_NONE,
4073 .alg = HASH_ALG_SHA384,
4074 .mode = HASH_MODE_HASH,
4078 .type = CRYPTO_ALG_TYPE_AHASH,
4080 .halg.digestsize = SHA384_DIGEST_SIZE,
4082 .cra_name = "hmac(sha384)",
4083 .cra_driver_name = "hmac-sha384-iproc",
4084 .cra_blocksize = SHA384_BLOCK_SIZE,
4088 .alg = CIPHER_ALG_NONE,
4089 .mode = CIPHER_MODE_NONE,
4092 .alg = HASH_ALG_SHA384,
4093 .mode = HASH_MODE_HMAC,
4097 .type = CRYPTO_ALG_TYPE_AHASH,
4099 .halg.digestsize = SHA512_DIGEST_SIZE,
4101 .cra_name = "sha512",
4102 .cra_driver_name = "sha512-iproc",
4103 .cra_blocksize = SHA512_BLOCK_SIZE,
4107 .alg = CIPHER_ALG_NONE,
4108 .mode = CIPHER_MODE_NONE,
4111 .alg = HASH_ALG_SHA512,
4112 .mode = HASH_MODE_HASH,
4116 .type = CRYPTO_ALG_TYPE_AHASH,
4118 .halg.digestsize = SHA512_DIGEST_SIZE,
4120 .cra_name = "hmac(sha512)",
4121 .cra_driver_name = "hmac-sha512-iproc",
4122 .cra_blocksize = SHA512_BLOCK_SIZE,
4126 .alg = CIPHER_ALG_NONE,
4127 .mode = CIPHER_MODE_NONE,
4130 .alg = HASH_ALG_SHA512,
4131 .mode = HASH_MODE_HMAC,
4135 .type = CRYPTO_ALG_TYPE_AHASH,
4137 .halg.digestsize = SHA3_224_DIGEST_SIZE,
4139 .cra_name = "sha3-224",
4140 .cra_driver_name = "sha3-224-iproc",
4141 .cra_blocksize = SHA3_224_BLOCK_SIZE,
4145 .alg = CIPHER_ALG_NONE,
4146 .mode = CIPHER_MODE_NONE,
4149 .alg = HASH_ALG_SHA3_224,
4150 .mode = HASH_MODE_HASH,
4154 .type = CRYPTO_ALG_TYPE_AHASH,
4156 .halg.digestsize = SHA3_224_DIGEST_SIZE,
4158 .cra_name = "hmac(sha3-224)",
4159 .cra_driver_name = "hmac-sha3-224-iproc",
4160 .cra_blocksize = SHA3_224_BLOCK_SIZE,
4164 .alg = CIPHER_ALG_NONE,
4165 .mode = CIPHER_MODE_NONE,
4168 .alg = HASH_ALG_SHA3_224,
4169 .mode = HASH_MODE_HMAC
4173 .type = CRYPTO_ALG_TYPE_AHASH,
4175 .halg.digestsize = SHA3_256_DIGEST_SIZE,
4177 .cra_name = "sha3-256",
4178 .cra_driver_name = "sha3-256-iproc",
4179 .cra_blocksize = SHA3_256_BLOCK_SIZE,
4183 .alg = CIPHER_ALG_NONE,
4184 .mode = CIPHER_MODE_NONE,
4187 .alg = HASH_ALG_SHA3_256,
4188 .mode = HASH_MODE_HASH,
4192 .type = CRYPTO_ALG_TYPE_AHASH,
4194 .halg.digestsize = SHA3_256_DIGEST_SIZE,
4196 .cra_name = "hmac(sha3-256)",
4197 .cra_driver_name = "hmac-sha3-256-iproc",
4198 .cra_blocksize = SHA3_256_BLOCK_SIZE,
4202 .alg = CIPHER_ALG_NONE,
4203 .mode = CIPHER_MODE_NONE,
4206 .alg = HASH_ALG_SHA3_256,
4207 .mode = HASH_MODE_HMAC,
4211 .type = CRYPTO_ALG_TYPE_AHASH,
4213 .halg.digestsize = SHA3_384_DIGEST_SIZE,
4215 .cra_name = "sha3-384",
4216 .cra_driver_name = "sha3-384-iproc",
4217 .cra_blocksize = SHA3_224_BLOCK_SIZE,
4221 .alg = CIPHER_ALG_NONE,
4222 .mode = CIPHER_MODE_NONE,
4225 .alg = HASH_ALG_SHA3_384,
4226 .mode = HASH_MODE_HASH,
4230 .type = CRYPTO_ALG_TYPE_AHASH,
4232 .halg.digestsize = SHA3_384_DIGEST_SIZE,
4234 .cra_name = "hmac(sha3-384)",
4235 .cra_driver_name = "hmac-sha3-384-iproc",
4236 .cra_blocksize = SHA3_384_BLOCK_SIZE,
4240 .alg = CIPHER_ALG_NONE,
4241 .mode = CIPHER_MODE_NONE,
4244 .alg = HASH_ALG_SHA3_384,
4245 .mode = HASH_MODE_HMAC,
4249 .type = CRYPTO_ALG_TYPE_AHASH,
4251 .halg.digestsize = SHA3_512_DIGEST_SIZE,
4253 .cra_name = "sha3-512",
4254 .cra_driver_name = "sha3-512-iproc",
4255 .cra_blocksize = SHA3_512_BLOCK_SIZE,
4259 .alg = CIPHER_ALG_NONE,
4260 .mode = CIPHER_MODE_NONE,
4263 .alg = HASH_ALG_SHA3_512,
4264 .mode = HASH_MODE_HASH,
4268 .type = CRYPTO_ALG_TYPE_AHASH,
4270 .halg.digestsize = SHA3_512_DIGEST_SIZE,
4272 .cra_name = "hmac(sha3-512)",
4273 .cra_driver_name = "hmac-sha3-512-iproc",
4274 .cra_blocksize = SHA3_512_BLOCK_SIZE,
4278 .alg = CIPHER_ALG_NONE,
4279 .mode = CIPHER_MODE_NONE,
4282 .alg = HASH_ALG_SHA3_512,
4283 .mode = HASH_MODE_HMAC,
4287 .type = CRYPTO_ALG_TYPE_AHASH,
4289 .halg.digestsize = AES_BLOCK_SIZE,
4291 .cra_name = "xcbc(aes)",
4292 .cra_driver_name = "xcbc-aes-iproc",
4293 .cra_blocksize = AES_BLOCK_SIZE,
4297 .alg = CIPHER_ALG_NONE,
4298 .mode = CIPHER_MODE_NONE,
4301 .alg = HASH_ALG_AES,
4302 .mode = HASH_MODE_XCBC,
4306 .type = CRYPTO_ALG_TYPE_AHASH,
4308 .halg.digestsize = AES_BLOCK_SIZE,
4310 .cra_name = "cmac(aes)",
4311 .cra_driver_name = "cmac-aes-iproc",
4312 .cra_blocksize = AES_BLOCK_SIZE,
4316 .alg = CIPHER_ALG_NONE,
4317 .mode = CIPHER_MODE_NONE,
4320 .alg = HASH_ALG_AES,
4321 .mode = HASH_MODE_CMAC,
4326 static int generic_cra_init(struct crypto_tfm *tfm,
4327 struct iproc_alg_s *cipher_alg)
4329 struct spu_hw *spu = &iproc_priv.spu;
4330 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4331 unsigned int blocksize = crypto_tfm_alg_blocksize(tfm);
4333 flow_log("%s()\n", __func__);
4335 ctx->alg = cipher_alg;
4336 ctx->cipher = cipher_alg->cipher_info;
4337 ctx->auth = cipher_alg->auth_info;
4338 ctx->auth_first = cipher_alg->auth_first;
4339 ctx->max_payload = spu->spu_ctx_max_payload(ctx->cipher.alg,
4342 ctx->fallback_cipher = NULL;
4345 ctx->authkeylen = 0;
4347 atomic_inc(&iproc_priv.stream_count);
4348 atomic_inc(&iproc_priv.session_count);
4353 static int ablkcipher_cra_init(struct crypto_tfm *tfm)
4355 struct crypto_alg *alg = tfm->__crt_alg;
4356 struct iproc_alg_s *cipher_alg;
4358 flow_log("%s()\n", __func__);
4360 tfm->crt_ablkcipher.reqsize = sizeof(struct iproc_reqctx_s);
4362 cipher_alg = container_of(alg, struct iproc_alg_s, alg.crypto);
4363 return generic_cra_init(tfm, cipher_alg);
4366 static int ahash_cra_init(struct crypto_tfm *tfm)
4369 struct crypto_alg *alg = tfm->__crt_alg;
4370 struct iproc_alg_s *cipher_alg;
4372 cipher_alg = container_of(__crypto_ahash_alg(alg), struct iproc_alg_s,
4375 err = generic_cra_init(tfm, cipher_alg);
4376 flow_log("%s()\n", __func__);
4379 * export state size has to be < 512 bytes. So don't include msg bufs
4382 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
4383 sizeof(struct iproc_reqctx_s));
4388 static int aead_cra_init(struct crypto_aead *aead)
4390 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
4391 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4392 struct crypto_alg *alg = tfm->__crt_alg;
4393 struct aead_alg *aalg = container_of(alg, struct aead_alg, base);
4394 struct iproc_alg_s *cipher_alg = container_of(aalg, struct iproc_alg_s,
4397 int err = generic_cra_init(tfm, cipher_alg);
4399 flow_log("%s()\n", __func__);
4401 crypto_aead_set_reqsize(aead, sizeof(struct iproc_reqctx_s));
4402 ctx->is_esp = false;
4404 ctx->salt_offset = 0;
4406 /* random first IV */
4407 get_random_bytes(ctx->iv, MAX_IV_SIZE);
4408 flow_dump(" iv: ", ctx->iv, MAX_IV_SIZE);
4411 if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
4412 flow_log("%s() creating fallback cipher\n", __func__);
4414 ctx->fallback_cipher =
4415 crypto_alloc_aead(alg->cra_name, 0,
4417 CRYPTO_ALG_NEED_FALLBACK);
4418 if (IS_ERR(ctx->fallback_cipher)) {
4419 pr_err("%s() Error: failed to allocate fallback for %s\n",
4420 __func__, alg->cra_name);
4421 return PTR_ERR(ctx->fallback_cipher);
4429 static void generic_cra_exit(struct crypto_tfm *tfm)
4431 atomic_dec(&iproc_priv.session_count);
4434 static void aead_cra_exit(struct crypto_aead *aead)
4436 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
4437 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4439 generic_cra_exit(tfm);
4441 if (ctx->fallback_cipher) {
4442 crypto_free_aead(ctx->fallback_cipher);
4443 ctx->fallback_cipher = NULL;
4448 * spu_functions_register() - Specify hardware-specific SPU functions based on
4449 * SPU type read from device tree.
4450 * @dev: device structure
4451 * @spu_type: SPU hardware generation
4452 * @spu_subtype: SPU hardware version
4454 static void spu_functions_register(struct device *dev,
4455 enum spu_spu_type spu_type,
4456 enum spu_spu_subtype spu_subtype)
4458 struct spu_hw *spu = &iproc_priv.spu;
4460 if (spu_type == SPU_TYPE_SPUM) {
4461 dev_dbg(dev, "Registering SPUM functions");
4462 spu->spu_dump_msg_hdr = spum_dump_msg_hdr;
4463 spu->spu_payload_length = spum_payload_length;
4464 spu->spu_response_hdr_len = spum_response_hdr_len;
4465 spu->spu_hash_pad_len = spum_hash_pad_len;
4466 spu->spu_gcm_ccm_pad_len = spum_gcm_ccm_pad_len;
4467 spu->spu_assoc_resp_len = spum_assoc_resp_len;
4468 spu->spu_aead_ivlen = spum_aead_ivlen;
4469 spu->spu_hash_type = spum_hash_type;
4470 spu->spu_digest_size = spum_digest_size;
4471 spu->spu_create_request = spum_create_request;
4472 spu->spu_cipher_req_init = spum_cipher_req_init;
4473 spu->spu_cipher_req_finish = spum_cipher_req_finish;
4474 spu->spu_request_pad = spum_request_pad;
4475 spu->spu_tx_status_len = spum_tx_status_len;
4476 spu->spu_rx_status_len = spum_rx_status_len;
4477 spu->spu_status_process = spum_status_process;
4478 spu->spu_xts_tweak_in_payload = spum_xts_tweak_in_payload;
4479 spu->spu_ccm_update_iv = spum_ccm_update_iv;
4480 spu->spu_wordalign_padlen = spum_wordalign_padlen;
4481 if (spu_subtype == SPU_SUBTYPE_SPUM_NS2)
4482 spu->spu_ctx_max_payload = spum_ns2_ctx_max_payload;
4484 spu->spu_ctx_max_payload = spum_nsp_ctx_max_payload;
4486 dev_dbg(dev, "Registering SPU2 functions");
4487 spu->spu_dump_msg_hdr = spu2_dump_msg_hdr;
4488 spu->spu_ctx_max_payload = spu2_ctx_max_payload;
4489 spu->spu_payload_length = spu2_payload_length;
4490 spu->spu_response_hdr_len = spu2_response_hdr_len;
4491 spu->spu_hash_pad_len = spu2_hash_pad_len;
4492 spu->spu_gcm_ccm_pad_len = spu2_gcm_ccm_pad_len;
4493 spu->spu_assoc_resp_len = spu2_assoc_resp_len;
4494 spu->spu_aead_ivlen = spu2_aead_ivlen;
4495 spu->spu_hash_type = spu2_hash_type;
4496 spu->spu_digest_size = spu2_digest_size;
4497 spu->spu_create_request = spu2_create_request;
4498 spu->spu_cipher_req_init = spu2_cipher_req_init;
4499 spu->spu_cipher_req_finish = spu2_cipher_req_finish;
4500 spu->spu_request_pad = spu2_request_pad;
4501 spu->spu_tx_status_len = spu2_tx_status_len;
4502 spu->spu_rx_status_len = spu2_rx_status_len;
4503 spu->spu_status_process = spu2_status_process;
4504 spu->spu_xts_tweak_in_payload = spu2_xts_tweak_in_payload;
4505 spu->spu_ccm_update_iv = spu2_ccm_update_iv;
4506 spu->spu_wordalign_padlen = spu2_wordalign_padlen;
4511 * spu_mb_init() - Initialize mailbox client. Request ownership of a mailbox
4512 * channel for the SPU being probed.
4513 * @dev: SPU driver device structure
4515 * Return: 0 if successful
4518 static int spu_mb_init(struct device *dev)
4520 struct mbox_client *mcl = &iproc_priv.mcl;
4523 iproc_priv.mbox = devm_kcalloc(dev, iproc_priv.spu.num_chan,
4524 sizeof(struct mbox_chan *), GFP_KERNEL);
4525 if (!iproc_priv.mbox)
4529 mcl->tx_block = false;
4531 mcl->knows_txdone = true;
4532 mcl->rx_callback = spu_rx_callback;
4533 mcl->tx_done = NULL;
4535 for (i = 0; i < iproc_priv.spu.num_chan; i++) {
4536 iproc_priv.mbox[i] = mbox_request_channel(mcl, i);
4537 if (IS_ERR(iproc_priv.mbox[i])) {
4538 err = (int)PTR_ERR(iproc_priv.mbox[i]);
4540 "Mbox channel %d request failed with err %d",
4542 iproc_priv.mbox[i] = NULL;
4549 for (i = 0; i < iproc_priv.spu.num_chan; i++) {
4550 if (iproc_priv.mbox[i])
4551 mbox_free_channel(iproc_priv.mbox[i]);
4557 static void spu_mb_release(struct platform_device *pdev)
4561 for (i = 0; i < iproc_priv.spu.num_chan; i++)
4562 mbox_free_channel(iproc_priv.mbox[i]);
4565 static void spu_counters_init(void)
4570 atomic_set(&iproc_priv.session_count, 0);
4571 atomic_set(&iproc_priv.stream_count, 0);
4572 atomic_set(&iproc_priv.next_chan, (int)iproc_priv.spu.num_chan);
4573 atomic64_set(&iproc_priv.bytes_in, 0);
4574 atomic64_set(&iproc_priv.bytes_out, 0);
4575 for (i = 0; i < SPU_OP_NUM; i++) {
4576 atomic_set(&iproc_priv.op_counts[i], 0);
4577 atomic_set(&iproc_priv.setkey_cnt[i], 0);
4579 for (i = 0; i < CIPHER_ALG_LAST; i++)
4580 for (j = 0; j < CIPHER_MODE_LAST; j++)
4581 atomic_set(&iproc_priv.cipher_cnt[i][j], 0);
4583 for (i = 0; i < HASH_ALG_LAST; i++) {
4584 atomic_set(&iproc_priv.hash_cnt[i], 0);
4585 atomic_set(&iproc_priv.hmac_cnt[i], 0);
4587 for (i = 0; i < AEAD_TYPE_LAST; i++)
4588 atomic_set(&iproc_priv.aead_cnt[i], 0);
4590 atomic_set(&iproc_priv.mb_no_spc, 0);
4591 atomic_set(&iproc_priv.mb_send_fail, 0);
4592 atomic_set(&iproc_priv.bad_icv, 0);
4595 static int spu_register_ablkcipher(struct iproc_alg_s *driver_alg)
4597 struct spu_hw *spu = &iproc_priv.spu;
4598 struct crypto_alg *crypto = &driver_alg->alg.crypto;
4601 /* SPU2 does not support RC4 */
4602 if ((driver_alg->cipher_info.alg == CIPHER_ALG_RC4) &&
4603 (spu->spu_type == SPU_TYPE_SPU2))
4606 crypto->cra_module = THIS_MODULE;
4607 crypto->cra_priority = cipher_pri;
4608 crypto->cra_alignmask = 0;
4609 crypto->cra_ctxsize = sizeof(struct iproc_ctx_s);
4610 INIT_LIST_HEAD(&crypto->cra_list);
4612 crypto->cra_init = ablkcipher_cra_init;
4613 crypto->cra_exit = generic_cra_exit;
4614 crypto->cra_type = &crypto_ablkcipher_type;
4615 crypto->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
4616 CRYPTO_ALG_KERN_DRIVER_ONLY;
4618 crypto->cra_ablkcipher.setkey = ablkcipher_setkey;
4619 crypto->cra_ablkcipher.encrypt = ablkcipher_encrypt;
4620 crypto->cra_ablkcipher.decrypt = ablkcipher_decrypt;
4622 err = crypto_register_alg(crypto);
4623 /* Mark alg as having been registered, if successful */
4625 driver_alg->registered = true;
4626 pr_debug(" registered ablkcipher %s\n", crypto->cra_driver_name);
4630 static int spu_register_ahash(struct iproc_alg_s *driver_alg)
4632 struct spu_hw *spu = &iproc_priv.spu;
4633 struct ahash_alg *hash = &driver_alg->alg.hash;
4636 /* AES-XCBC is the only AES hash type currently supported on SPU-M */
4637 if ((driver_alg->auth_info.alg == HASH_ALG_AES) &&
4638 (driver_alg->auth_info.mode != HASH_MODE_XCBC) &&
4639 (spu->spu_type == SPU_TYPE_SPUM))
4642 /* SHA3 algorithm variants are not registered for SPU-M or SPU2. */
4643 if ((driver_alg->auth_info.alg >= HASH_ALG_SHA3_224) &&
4644 (spu->spu_subtype != SPU_SUBTYPE_SPU2_V2))
4647 hash->halg.base.cra_module = THIS_MODULE;
4648 hash->halg.base.cra_priority = hash_pri;
4649 hash->halg.base.cra_alignmask = 0;
4650 hash->halg.base.cra_ctxsize = sizeof(struct iproc_ctx_s);
4651 hash->halg.base.cra_init = ahash_cra_init;
4652 hash->halg.base.cra_exit = generic_cra_exit;
4653 hash->halg.base.cra_type = &crypto_ahash_type;
4654 hash->halg.base.cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC;
4655 hash->halg.statesize = sizeof(struct spu_hash_export_s);
4657 if (driver_alg->auth_info.mode != HASH_MODE_HMAC) {
4658 hash->setkey = ahash_setkey;
4659 hash->init = ahash_init;
4660 hash->update = ahash_update;
4661 hash->final = ahash_final;
4662 hash->finup = ahash_finup;
4663 hash->digest = ahash_digest;
4665 hash->setkey = ahash_hmac_setkey;
4666 hash->init = ahash_hmac_init;
4667 hash->update = ahash_hmac_update;
4668 hash->final = ahash_hmac_final;
4669 hash->finup = ahash_hmac_finup;
4670 hash->digest = ahash_hmac_digest;
4672 hash->export = ahash_export;
4673 hash->import = ahash_import;
4675 err = crypto_register_ahash(hash);
4676 /* Mark alg as having been registered, if successful */
4678 driver_alg->registered = true;
4679 pr_debug(" registered ahash %s\n",
4680 hash->halg.base.cra_driver_name);
4684 static int spu_register_aead(struct iproc_alg_s *driver_alg)
4686 struct aead_alg *aead = &driver_alg->alg.aead;
4689 aead->base.cra_module = THIS_MODULE;
4690 aead->base.cra_priority = aead_pri;
4691 aead->base.cra_alignmask = 0;
4692 aead->base.cra_ctxsize = sizeof(struct iproc_ctx_s);
4693 INIT_LIST_HEAD(&aead->base.cra_list);
4695 aead->base.cra_flags |= CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
4696 /* setkey set in alg initialization */
4697 aead->setauthsize = aead_setauthsize;
4698 aead->encrypt = aead_encrypt;
4699 aead->decrypt = aead_decrypt;
4700 aead->init = aead_cra_init;
4701 aead->exit = aead_cra_exit;
4703 err = crypto_register_aead(aead);
4704 /* Mark alg as having been registered, if successful */
4706 driver_alg->registered = true;
4707 pr_debug(" registered aead %s\n", aead->base.cra_driver_name);
4711 /* register crypto algorithms the device supports */
4712 static int spu_algs_register(struct device *dev)
4717 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4718 switch (driver_algs[i].type) {
4719 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4720 err = spu_register_ablkcipher(&driver_algs[i]);
4722 case CRYPTO_ALG_TYPE_AHASH:
4723 err = spu_register_ahash(&driver_algs[i]);
4725 case CRYPTO_ALG_TYPE_AEAD:
4726 err = spu_register_aead(&driver_algs[i]);
4730 "iproc-crypto: unknown alg type: %d",
4731 driver_algs[i].type);
4736 dev_err(dev, "alg registration failed with error %d\n",
4745 for (j = 0; j < i; j++) {
4746 /* Skip any algorithm not registered */
4747 if (!driver_algs[j].registered)
4749 switch (driver_algs[j].type) {
4750 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4751 crypto_unregister_alg(&driver_algs[j].alg.crypto);
4752 driver_algs[j].registered = false;
4754 case CRYPTO_ALG_TYPE_AHASH:
4755 crypto_unregister_ahash(&driver_algs[j].alg.hash);
4756 driver_algs[j].registered = false;
4758 case CRYPTO_ALG_TYPE_AEAD:
4759 crypto_unregister_aead(&driver_algs[j].alg.aead);
4760 driver_algs[j].registered = false;
4767 /* ==================== Kernel Platform API ==================== */
4769 static struct spu_type_subtype spum_ns2_types = {
4770 SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NS2
4773 static struct spu_type_subtype spum_nsp_types = {
4774 SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NSP
4777 static struct spu_type_subtype spu2_types = {
4778 SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V1
4781 static struct spu_type_subtype spu2_v2_types = {
4782 SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V2
4785 static const struct of_device_id bcm_spu_dt_ids[] = {
4787 .compatible = "brcm,spum-crypto",
4788 .data = &spum_ns2_types,
4791 .compatible = "brcm,spum-nsp-crypto",
4792 .data = &spum_nsp_types,
4795 .compatible = "brcm,spu2-crypto",
4796 .data = &spu2_types,
4799 .compatible = "brcm,spu2-v2-crypto",
4800 .data = &spu2_v2_types,
4805 MODULE_DEVICE_TABLE(of, bcm_spu_dt_ids);
4807 static int spu_dt_read(struct platform_device *pdev)
4809 struct device *dev = &pdev->dev;
4810 struct spu_hw *spu = &iproc_priv.spu;
4811 struct resource *spu_ctrl_regs;
4812 const struct spu_type_subtype *matched_spu_type;
4813 struct device_node *dn = pdev->dev.of_node;
4816 /* Count number of mailbox channels */
4817 spu->num_chan = of_count_phandle_with_args(dn, "mboxes", "#mbox-cells");
4819 matched_spu_type = of_device_get_match_data(dev);
4820 if (!matched_spu_type) {
4821 dev_err(&pdev->dev, "Failed to match device\n");
4825 spu->spu_type = matched_spu_type->type;
4826 spu->spu_subtype = matched_spu_type->subtype;
4829 for (i = 0; (i < MAX_SPUS) && ((spu_ctrl_regs =
4830 platform_get_resource(pdev, IORESOURCE_MEM, i)) != NULL); i++) {
4832 spu->reg_vbase[i] = devm_ioremap_resource(dev, spu_ctrl_regs);
4833 if (IS_ERR(spu->reg_vbase[i])) {
4834 err = PTR_ERR(spu->reg_vbase[i]);
4835 dev_err(&pdev->dev, "Failed to map registers: %d\n",
4837 spu->reg_vbase[i] = NULL;
4842 dev_dbg(dev, "Device has %d SPUs", spu->num_spu);
4847 int bcm_spu_probe(struct platform_device *pdev)
4849 struct device *dev = &pdev->dev;
4850 struct spu_hw *spu = &iproc_priv.spu;
4853 iproc_priv.pdev = pdev;
4854 platform_set_drvdata(iproc_priv.pdev,
4857 err = spu_dt_read(pdev);
4861 err = spu_mb_init(&pdev->dev);
4865 if (spu->spu_type == SPU_TYPE_SPUM)
4866 iproc_priv.bcm_hdr_len = 8;
4867 else if (spu->spu_type == SPU_TYPE_SPU2)
4868 iproc_priv.bcm_hdr_len = 0;
4870 spu_functions_register(&pdev->dev, spu->spu_type, spu->spu_subtype);
4872 spu_counters_init();
4874 spu_setup_debugfs();
4876 err = spu_algs_register(dev);
4885 spu_mb_release(pdev);
4886 dev_err(dev, "%s failed with error %d.\n", __func__, err);
4891 int bcm_spu_remove(struct platform_device *pdev)
4894 struct device *dev = &pdev->dev;
4897 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4899 * Not all algorithms were registered, depending on whether
4900 * hardware is SPU or SPU2. So here we make sure to skip
4901 * those algorithms that were not previously registered.
4903 if (!driver_algs[i].registered)
4906 switch (driver_algs[i].type) {
4907 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4908 crypto_unregister_alg(&driver_algs[i].alg.crypto);
4909 dev_dbg(dev, " unregistered cipher %s\n",
4910 driver_algs[i].alg.crypto.cra_driver_name);
4911 driver_algs[i].registered = false;
4913 case CRYPTO_ALG_TYPE_AHASH:
4914 crypto_unregister_ahash(&driver_algs[i].alg.hash);
4915 cdn = driver_algs[i].alg.hash.halg.base.cra_driver_name;
4916 dev_dbg(dev, " unregistered hash %s\n", cdn);
4917 driver_algs[i].registered = false;
4919 case CRYPTO_ALG_TYPE_AEAD:
4920 crypto_unregister_aead(&driver_algs[i].alg.aead);
4921 dev_dbg(dev, " unregistered aead %s\n",
4922 driver_algs[i].alg.aead.base.cra_driver_name);
4923 driver_algs[i].registered = false;
4928 spu_mb_release(pdev);
4932 /* ===== Kernel Module API ===== */
4934 static struct platform_driver bcm_spu_pdriver = {
4936 .name = "brcm-spu-crypto",
4937 .of_match_table = of_match_ptr(bcm_spu_dt_ids),
4939 .probe = bcm_spu_probe,
4940 .remove = bcm_spu_remove,
4942 module_platform_driver(bcm_spu_pdriver);
4945 MODULE_DESCRIPTION("Broadcom symmetric crypto offload driver");
4946 MODULE_LICENSE("GPL v2");