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 bcm_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 __maybe_unused 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_ESP_IV_SIZE;
1378 if (rctx->is_encrypt) {
1379 aead_parms.return_iv = true;
1380 aead_parms.ret_iv_len = GCM_ESP_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;
1682 if (unlikely(!rctx)) {
1684 pr_err("%s(): no request context", __func__);
1689 /* process the SPU status */
1690 err = spu->spu_status_process(rctx->msg_buf.rx_stat);
1692 if (err == SPU_INVALID_ICV)
1693 atomic_inc(&iproc_priv.bad_icv);
1698 /* Process the SPU response message */
1699 switch (rctx->ctx->alg->type) {
1700 case CRYPTO_ALG_TYPE_ABLKCIPHER:
1701 handle_ablkcipher_resp(rctx);
1703 case CRYPTO_ALG_TYPE_AHASH:
1704 handle_ahash_resp(rctx);
1706 case CRYPTO_ALG_TYPE_AEAD:
1707 handle_aead_resp(rctx);
1715 * If this response does not complete the request, then send the next
1718 if (rctx->total_sent < rctx->total_todo) {
1719 /* Deallocate anything specific to previous chunk */
1720 spu_chunk_cleanup(rctx);
1722 switch (rctx->ctx->alg->type) {
1723 case CRYPTO_ALG_TYPE_ABLKCIPHER:
1724 err = handle_ablkcipher_req(rctx);
1726 case CRYPTO_ALG_TYPE_AHASH:
1727 err = handle_ahash_req(rctx);
1730 * we saved data in hash carry, but tell crypto
1731 * API we successfully completed request.
1735 case CRYPTO_ALG_TYPE_AEAD:
1736 err = handle_aead_req(rctx);
1742 if (err == -EINPROGRESS)
1743 /* Successfully submitted request for next chunk */
1748 finish_req(rctx, err);
1751 /* ==================== Kernel Cryptographic API ==================== */
1754 * ablkcipher_enqueue() - Handle ablkcipher encrypt or decrypt request.
1755 * @req: Crypto API request
1756 * @encrypt: true if encrypting; false if decrypting
1758 * Return: -EINPROGRESS if request accepted and result will be returned
1762 static int ablkcipher_enqueue(struct ablkcipher_request *req, bool encrypt)
1764 struct iproc_reqctx_s *rctx = ablkcipher_request_ctx(req);
1765 struct iproc_ctx_s *ctx =
1766 crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
1769 flow_log("%s() enc:%u\n", __func__, encrypt);
1771 rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
1772 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
1773 rctx->parent = &req->base;
1774 rctx->is_encrypt = encrypt;
1775 rctx->bd_suppress = false;
1776 rctx->total_todo = req->nbytes;
1778 rctx->total_sent = 0;
1779 rctx->total_received = 0;
1782 /* Initialize current position in src and dst scatterlists */
1783 rctx->src_sg = req->src;
1784 rctx->src_nents = 0;
1786 rctx->dst_sg = req->dst;
1787 rctx->dst_nents = 0;
1790 if (ctx->cipher.mode == CIPHER_MODE_CBC ||
1791 ctx->cipher.mode == CIPHER_MODE_CTR ||
1792 ctx->cipher.mode == CIPHER_MODE_OFB ||
1793 ctx->cipher.mode == CIPHER_MODE_XTS ||
1794 ctx->cipher.mode == CIPHER_MODE_GCM ||
1795 ctx->cipher.mode == CIPHER_MODE_CCM) {
1797 crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));
1798 memcpy(rctx->msg_buf.iv_ctr, req->info, rctx->iv_ctr_len);
1800 rctx->iv_ctr_len = 0;
1803 /* Choose a SPU to process this request */
1804 rctx->chan_idx = select_channel();
1805 err = handle_ablkcipher_req(rctx);
1806 if (err != -EINPROGRESS)
1807 /* synchronous result */
1808 spu_chunk_cleanup(rctx);
1813 static int des_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1814 unsigned int keylen)
1816 struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1817 u32 tmp[DES_EXPKEY_WORDS];
1819 if (keylen == DES_KEY_SIZE) {
1820 if (des_ekey(tmp, key) == 0) {
1821 if (crypto_ablkcipher_get_flags(cipher) &
1822 CRYPTO_TFM_REQ_WEAK_KEY) {
1823 u32 flags = CRYPTO_TFM_RES_WEAK_KEY;
1825 crypto_ablkcipher_set_flags(cipher, flags);
1830 ctx->cipher_type = CIPHER_TYPE_DES;
1832 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1838 static int threedes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1839 unsigned int keylen)
1841 struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1843 if (keylen == (DES_KEY_SIZE * 3)) {
1844 const u32 *K = (const u32 *)key;
1845 u32 flags = CRYPTO_TFM_RES_BAD_KEY_SCHED;
1847 if (!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
1848 !((K[2] ^ K[4]) | (K[3] ^ K[5]))) {
1849 crypto_ablkcipher_set_flags(cipher, flags);
1853 ctx->cipher_type = CIPHER_TYPE_3DES;
1855 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1861 static int aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1862 unsigned int keylen)
1864 struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1866 if (ctx->cipher.mode == CIPHER_MODE_XTS)
1867 /* XTS includes two keys of equal length */
1868 keylen = keylen / 2;
1871 case AES_KEYSIZE_128:
1872 ctx->cipher_type = CIPHER_TYPE_AES128;
1874 case AES_KEYSIZE_192:
1875 ctx->cipher_type = CIPHER_TYPE_AES192;
1877 case AES_KEYSIZE_256:
1878 ctx->cipher_type = CIPHER_TYPE_AES256;
1881 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1884 WARN_ON((ctx->max_payload != SPU_MAX_PAYLOAD_INF) &&
1885 ((ctx->max_payload % AES_BLOCK_SIZE) != 0));
1889 static int rc4_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1890 unsigned int keylen)
1892 struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1895 ctx->enckeylen = ARC4_MAX_KEY_SIZE + ARC4_STATE_SIZE;
1897 ctx->enckey[0] = 0x00; /* 0x00 */
1898 ctx->enckey[1] = 0x00; /* i */
1899 ctx->enckey[2] = 0x00; /* 0x00 */
1900 ctx->enckey[3] = 0x00; /* j */
1901 for (i = 0; i < ARC4_MAX_KEY_SIZE; i++)
1902 ctx->enckey[i + ARC4_STATE_SIZE] = key[i % keylen];
1904 ctx->cipher_type = CIPHER_TYPE_INIT;
1909 static int ablkcipher_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1910 unsigned int keylen)
1912 struct spu_hw *spu = &iproc_priv.spu;
1913 struct iproc_ctx_s *ctx = crypto_ablkcipher_ctx(cipher);
1914 struct spu_cipher_parms cipher_parms;
1918 flow_log("ablkcipher_setkey() keylen: %d\n", keylen);
1919 flow_dump(" key: ", key, keylen);
1921 switch (ctx->cipher.alg) {
1922 case CIPHER_ALG_DES:
1923 err = des_setkey(cipher, key, keylen);
1925 case CIPHER_ALG_3DES:
1926 err = threedes_setkey(cipher, key, keylen);
1928 case CIPHER_ALG_AES:
1929 err = aes_setkey(cipher, key, keylen);
1931 case CIPHER_ALG_RC4:
1932 err = rc4_setkey(cipher, key, keylen);
1935 pr_err("%s() Error: unknown cipher alg\n", __func__);
1941 /* RC4 already populated ctx->enkey */
1942 if (ctx->cipher.alg != CIPHER_ALG_RC4) {
1943 memcpy(ctx->enckey, key, keylen);
1944 ctx->enckeylen = keylen;
1946 /* SPU needs XTS keys in the reverse order the crypto API presents */
1947 if ((ctx->cipher.alg == CIPHER_ALG_AES) &&
1948 (ctx->cipher.mode == CIPHER_MODE_XTS)) {
1949 unsigned int xts_keylen = keylen / 2;
1951 memcpy(ctx->enckey, key + xts_keylen, xts_keylen);
1952 memcpy(ctx->enckey + xts_keylen, key, xts_keylen);
1955 if (spu->spu_type == SPU_TYPE_SPUM)
1956 alloc_len = BCM_HDR_LEN + SPU_HEADER_ALLOC_LEN;
1957 else if (spu->spu_type == SPU_TYPE_SPU2)
1958 alloc_len = BCM_HDR_LEN + SPU2_HEADER_ALLOC_LEN;
1959 memset(ctx->bcm_spu_req_hdr, 0, alloc_len);
1960 cipher_parms.iv_buf = NULL;
1961 cipher_parms.iv_len = crypto_ablkcipher_ivsize(cipher);
1962 flow_log("%s: iv_len %u\n", __func__, cipher_parms.iv_len);
1964 cipher_parms.alg = ctx->cipher.alg;
1965 cipher_parms.mode = ctx->cipher.mode;
1966 cipher_parms.type = ctx->cipher_type;
1967 cipher_parms.key_buf = ctx->enckey;
1968 cipher_parms.key_len = ctx->enckeylen;
1970 /* Prepend SPU request message with BCM header */
1971 memcpy(ctx->bcm_spu_req_hdr, BCMHEADER, BCM_HDR_LEN);
1972 ctx->spu_req_hdr_len =
1973 spu->spu_cipher_req_init(ctx->bcm_spu_req_hdr + BCM_HDR_LEN,
1976 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
1980 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_CIPHER]);
1985 static int ablkcipher_encrypt(struct ablkcipher_request *req)
1987 flow_log("ablkcipher_encrypt() nbytes:%u\n", req->nbytes);
1989 return ablkcipher_enqueue(req, true);
1992 static int ablkcipher_decrypt(struct ablkcipher_request *req)
1994 flow_log("ablkcipher_decrypt() nbytes:%u\n", req->nbytes);
1995 return ablkcipher_enqueue(req, false);
1998 static int ahash_enqueue(struct ahash_request *req)
2000 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2001 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2002 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2004 const char *alg_name;
2006 flow_log("ahash_enqueue() nbytes:%u\n", req->nbytes);
2008 rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2009 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2010 rctx->parent = &req->base;
2012 rctx->bd_suppress = true;
2013 memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
2015 /* Initialize position in src scatterlist */
2016 rctx->src_sg = req->src;
2018 rctx->src_nents = 0;
2019 rctx->dst_sg = NULL;
2021 rctx->dst_nents = 0;
2023 /* SPU2 hardware does not compute hash of zero length data */
2024 if ((rctx->is_final == 1) && (rctx->total_todo == 0) &&
2025 (iproc_priv.spu.spu_type == SPU_TYPE_SPU2)) {
2026 alg_name = crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
2027 flow_log("Doing %sfinal %s zero-len hash request in software\n",
2028 rctx->is_final ? "" : "non-", alg_name);
2029 err = do_shash((unsigned char *)alg_name, req->result,
2030 NULL, 0, NULL, 0, ctx->authkey,
2033 flow_log("Hash request failed with error %d\n", err);
2036 /* Choose a SPU to process this request */
2037 rctx->chan_idx = select_channel();
2039 err = handle_ahash_req(rctx);
2040 if (err != -EINPROGRESS)
2041 /* synchronous result */
2042 spu_chunk_cleanup(rctx);
2046 * we saved data in hash carry, but tell crypto API
2047 * we successfully completed request.
2054 static int __ahash_init(struct ahash_request *req)
2056 struct spu_hw *spu = &iproc_priv.spu;
2057 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2058 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2059 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2061 flow_log("%s()\n", __func__);
2063 /* Initialize the context */
2064 rctx->hash_carry_len = 0;
2067 rctx->total_todo = 0;
2069 rctx->total_sent = 0;
2070 rctx->total_received = 0;
2072 ctx->digestsize = crypto_ahash_digestsize(tfm);
2073 /* If we add a hash whose digest is larger, catch it here. */
2074 WARN_ON(ctx->digestsize > MAX_DIGEST_SIZE);
2076 rctx->is_sw_hmac = false;
2078 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen, 0,
2085 * spu_no_incr_hash() - Determine whether incremental hashing is supported.
2086 * @ctx: Crypto session context
2088 * SPU-2 does not support incremental hashing (we'll have to revisit and
2089 * condition based on chip revision or device tree entry if future versions do
2090 * support incremental hash)
2092 * SPU-M also doesn't support incremental hashing of AES-XCBC
2094 * Return: true if incremental hashing is not supported
2097 bool spu_no_incr_hash(struct iproc_ctx_s *ctx)
2099 struct spu_hw *spu = &iproc_priv.spu;
2101 if (spu->spu_type == SPU_TYPE_SPU2)
2104 if ((ctx->auth.alg == HASH_ALG_AES) &&
2105 (ctx->auth.mode == HASH_MODE_XCBC))
2108 /* Otherwise, incremental hashing is supported */
2112 static int ahash_init(struct ahash_request *req)
2114 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2115 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2116 const char *alg_name;
2117 struct crypto_shash *hash;
2121 if (spu_no_incr_hash(ctx)) {
2123 * If we get an incremental hashing request and it's not
2124 * supported by the hardware, we need to handle it in software
2125 * by calling synchronous hash functions.
2127 alg_name = crypto_tfm_alg_name(crypto_ahash_tfm(tfm));
2128 hash = crypto_alloc_shash(alg_name, 0, 0);
2130 ret = PTR_ERR(hash);
2134 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2135 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2136 ctx->shash = kmalloc(sizeof(*ctx->shash) +
2137 crypto_shash_descsize(hash), gfp);
2142 ctx->shash->tfm = hash;
2143 ctx->shash->flags = 0;
2145 /* Set the key using data we already have from setkey */
2146 if (ctx->authkeylen > 0) {
2147 ret = crypto_shash_setkey(hash, ctx->authkey,
2153 /* Initialize hash w/ this key and other params */
2154 ret = crypto_shash_init(ctx->shash);
2158 /* Otherwise call the internal function which uses SPU hw */
2159 ret = __ahash_init(req);
2167 crypto_free_shash(hash);
2172 static int __ahash_update(struct ahash_request *req)
2174 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2176 flow_log("ahash_update() nbytes:%u\n", req->nbytes);
2180 rctx->total_todo += req->nbytes;
2183 return ahash_enqueue(req);
2186 static int ahash_update(struct ahash_request *req)
2188 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2189 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2195 if (spu_no_incr_hash(ctx)) {
2197 * If we get an incremental hashing request and it's not
2198 * supported by the hardware, we need to handle it in software
2199 * by calling synchronous hash functions.
2202 nents = sg_nents(req->src);
2206 /* Copy data from req scatterlist to tmp buffer */
2207 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2208 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2209 tmpbuf = kmalloc(req->nbytes, gfp);
2213 if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
2219 /* Call synchronous update */
2220 ret = crypto_shash_update(ctx->shash, tmpbuf, req->nbytes);
2223 /* Otherwise call the internal function which uses SPU hw */
2224 ret = __ahash_update(req);
2230 static int __ahash_final(struct ahash_request *req)
2232 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2234 flow_log("ahash_final() nbytes:%u\n", req->nbytes);
2238 return ahash_enqueue(req);
2241 static int ahash_final(struct ahash_request *req)
2243 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2244 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2247 if (spu_no_incr_hash(ctx)) {
2249 * If we get an incremental hashing request and it's not
2250 * supported by the hardware, we need to handle it in software
2251 * by calling synchronous hash functions.
2253 ret = crypto_shash_final(ctx->shash, req->result);
2255 /* Done with hash, can deallocate it now */
2256 crypto_free_shash(ctx->shash->tfm);
2260 /* Otherwise call the internal function which uses SPU hw */
2261 ret = __ahash_final(req);
2267 static int __ahash_finup(struct ahash_request *req)
2269 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2271 flow_log("ahash_finup() nbytes:%u\n", req->nbytes);
2273 rctx->total_todo += req->nbytes;
2277 return ahash_enqueue(req);
2280 static int ahash_finup(struct ahash_request *req)
2282 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2283 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2289 if (spu_no_incr_hash(ctx)) {
2291 * If we get an incremental hashing request and it's not
2292 * supported by the hardware, we need to handle it in software
2293 * by calling synchronous hash functions.
2296 nents = sg_nents(req->src);
2299 goto ahash_finup_exit;
2302 /* Copy data from req scatterlist to tmp buffer */
2303 gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2304 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2305 tmpbuf = kmalloc(req->nbytes, gfp);
2308 goto ahash_finup_exit;
2311 if (sg_copy_to_buffer(req->src, nents, tmpbuf, req->nbytes) !=
2314 goto ahash_finup_free;
2317 /* Call synchronous update */
2318 ret = crypto_shash_finup(ctx->shash, tmpbuf, req->nbytes,
2321 /* Otherwise call the internal function which uses SPU hw */
2322 return __ahash_finup(req);
2328 /* Done with hash, can deallocate it now */
2329 crypto_free_shash(ctx->shash->tfm);
2334 static int ahash_digest(struct ahash_request *req)
2338 flow_log("ahash_digest() nbytes:%u\n", req->nbytes);
2340 /* whole thing at once */
2341 err = __ahash_init(req);
2343 err = __ahash_finup(req);
2348 static int ahash_setkey(struct crypto_ahash *ahash, const u8 *key,
2349 unsigned int keylen)
2351 struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
2353 flow_log("%s() ahash:%p key:%p keylen:%u\n",
2354 __func__, ahash, key, keylen);
2355 flow_dump(" key: ", key, keylen);
2357 if (ctx->auth.alg == HASH_ALG_AES) {
2359 case AES_KEYSIZE_128:
2360 ctx->cipher_type = CIPHER_TYPE_AES128;
2362 case AES_KEYSIZE_192:
2363 ctx->cipher_type = CIPHER_TYPE_AES192;
2365 case AES_KEYSIZE_256:
2366 ctx->cipher_type = CIPHER_TYPE_AES256;
2369 pr_err("%s() Error: Invalid key length\n", __func__);
2373 pr_err("%s() Error: unknown hash alg\n", __func__);
2376 memcpy(ctx->authkey, key, keylen);
2377 ctx->authkeylen = keylen;
2382 static int ahash_export(struct ahash_request *req, void *out)
2384 const struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2385 struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)out;
2387 spu_exp->total_todo = rctx->total_todo;
2388 spu_exp->total_sent = rctx->total_sent;
2389 spu_exp->is_sw_hmac = rctx->is_sw_hmac;
2390 memcpy(spu_exp->hash_carry, rctx->hash_carry, sizeof(rctx->hash_carry));
2391 spu_exp->hash_carry_len = rctx->hash_carry_len;
2392 memcpy(spu_exp->incr_hash, rctx->incr_hash, sizeof(rctx->incr_hash));
2397 static int ahash_import(struct ahash_request *req, const void *in)
2399 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2400 struct spu_hash_export_s *spu_exp = (struct spu_hash_export_s *)in;
2402 rctx->total_todo = spu_exp->total_todo;
2403 rctx->total_sent = spu_exp->total_sent;
2404 rctx->is_sw_hmac = spu_exp->is_sw_hmac;
2405 memcpy(rctx->hash_carry, spu_exp->hash_carry, sizeof(rctx->hash_carry));
2406 rctx->hash_carry_len = spu_exp->hash_carry_len;
2407 memcpy(rctx->incr_hash, spu_exp->incr_hash, sizeof(rctx->incr_hash));
2412 static int ahash_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,
2413 unsigned int keylen)
2415 struct iproc_ctx_s *ctx = crypto_ahash_ctx(ahash);
2416 unsigned int blocksize =
2417 crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
2418 unsigned int digestsize = crypto_ahash_digestsize(ahash);
2422 flow_log("%s() ahash:%p key:%p keylen:%u blksz:%u digestsz:%u\n",
2423 __func__, ahash, key, keylen, blocksize, digestsize);
2424 flow_dump(" key: ", key, keylen);
2426 if (keylen > blocksize) {
2427 switch (ctx->auth.alg) {
2429 rc = do_shash("md5", ctx->authkey, key, keylen, NULL,
2433 rc = do_shash("sha1", ctx->authkey, key, keylen, NULL,
2436 case HASH_ALG_SHA224:
2437 rc = do_shash("sha224", ctx->authkey, key, keylen, NULL,
2440 case HASH_ALG_SHA256:
2441 rc = do_shash("sha256", ctx->authkey, key, keylen, NULL,
2444 case HASH_ALG_SHA384:
2445 rc = do_shash("sha384", ctx->authkey, key, keylen, NULL,
2448 case HASH_ALG_SHA512:
2449 rc = do_shash("sha512", ctx->authkey, key, keylen, NULL,
2452 case HASH_ALG_SHA3_224:
2453 rc = do_shash("sha3-224", ctx->authkey, key, keylen,
2456 case HASH_ALG_SHA3_256:
2457 rc = do_shash("sha3-256", ctx->authkey, key, keylen,
2460 case HASH_ALG_SHA3_384:
2461 rc = do_shash("sha3-384", ctx->authkey, key, keylen,
2464 case HASH_ALG_SHA3_512:
2465 rc = do_shash("sha3-512", ctx->authkey, key, keylen,
2469 pr_err("%s() Error: unknown hash alg\n", __func__);
2473 pr_err("%s() Error %d computing shash for %s\n",
2474 __func__, rc, hash_alg_name[ctx->auth.alg]);
2477 ctx->authkeylen = digestsize;
2479 flow_log(" keylen > digestsize... hashed\n");
2480 flow_dump(" newkey: ", ctx->authkey, ctx->authkeylen);
2482 memcpy(ctx->authkey, key, keylen);
2483 ctx->authkeylen = keylen;
2487 * Full HMAC operation in SPUM is not verified,
2488 * So keeping the generation of IPAD, OPAD and
2489 * outer hashing in software.
2491 if (iproc_priv.spu.spu_type == SPU_TYPE_SPUM) {
2492 memcpy(ctx->ipad, ctx->authkey, ctx->authkeylen);
2493 memset(ctx->ipad + ctx->authkeylen, 0,
2494 blocksize - ctx->authkeylen);
2495 ctx->authkeylen = 0;
2496 memcpy(ctx->opad, ctx->ipad, blocksize);
2498 for (index = 0; index < blocksize; index++) {
2499 ctx->ipad[index] ^= HMAC_IPAD_VALUE;
2500 ctx->opad[index] ^= HMAC_OPAD_VALUE;
2503 flow_dump(" ipad: ", ctx->ipad, blocksize);
2504 flow_dump(" opad: ", ctx->opad, blocksize);
2506 ctx->digestsize = digestsize;
2507 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_HMAC]);
2512 static int ahash_hmac_init(struct ahash_request *req)
2514 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2515 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2516 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2517 unsigned int blocksize =
2518 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2520 flow_log("ahash_hmac_init()\n");
2522 /* init the context as a hash */
2525 if (!spu_no_incr_hash(ctx)) {
2526 /* SPU-M can do incr hashing but needs sw for outer HMAC */
2527 rctx->is_sw_hmac = true;
2528 ctx->auth.mode = HASH_MODE_HASH;
2529 /* start with a prepended ipad */
2530 memcpy(rctx->hash_carry, ctx->ipad, blocksize);
2531 rctx->hash_carry_len = blocksize;
2532 rctx->total_todo += blocksize;
2538 static int ahash_hmac_update(struct ahash_request *req)
2540 flow_log("ahash_hmac_update() nbytes:%u\n", req->nbytes);
2545 return ahash_update(req);
2548 static int ahash_hmac_final(struct ahash_request *req)
2550 flow_log("ahash_hmac_final() nbytes:%u\n", req->nbytes);
2552 return ahash_final(req);
2555 static int ahash_hmac_finup(struct ahash_request *req)
2557 flow_log("ahash_hmac_finupl() nbytes:%u\n", req->nbytes);
2559 return ahash_finup(req);
2562 static int ahash_hmac_digest(struct ahash_request *req)
2564 struct iproc_reqctx_s *rctx = ahash_request_ctx(req);
2565 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2566 struct iproc_ctx_s *ctx = crypto_ahash_ctx(tfm);
2567 unsigned int blocksize =
2568 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2570 flow_log("ahash_hmac_digest() nbytes:%u\n", req->nbytes);
2572 /* Perform initialization and then call finup */
2575 if (iproc_priv.spu.spu_type == SPU_TYPE_SPU2) {
2577 * SPU2 supports full HMAC implementation in the
2578 * hardware, need not to generate IPAD, OPAD and
2579 * outer hash in software.
2580 * Only for hash key len > hash block size, SPU2
2581 * expects to perform hashing on the key, shorten
2582 * it to digest size and feed it as hash key.
2584 rctx->is_sw_hmac = false;
2585 ctx->auth.mode = HASH_MODE_HMAC;
2587 rctx->is_sw_hmac = true;
2588 ctx->auth.mode = HASH_MODE_HASH;
2589 /* start with a prepended ipad */
2590 memcpy(rctx->hash_carry, ctx->ipad, blocksize);
2591 rctx->hash_carry_len = blocksize;
2592 rctx->total_todo += blocksize;
2595 return __ahash_finup(req);
2600 static int aead_need_fallback(struct aead_request *req)
2602 struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2603 struct spu_hw *spu = &iproc_priv.spu;
2604 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2605 struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
2609 * SPU hardware cannot handle the AES-GCM/CCM case where plaintext
2610 * and AAD are both 0 bytes long. So use fallback in this case.
2612 if (((ctx->cipher.mode == CIPHER_MODE_GCM) ||
2613 (ctx->cipher.mode == CIPHER_MODE_CCM)) &&
2614 (req->assoclen == 0)) {
2615 if ((rctx->is_encrypt && (req->cryptlen == 0)) ||
2616 (!rctx->is_encrypt && (req->cryptlen == ctx->digestsize))) {
2617 flow_log("AES GCM/CCM needs fallback for 0 len req\n");
2622 /* SPU-M hardware only supports CCM digest size of 8, 12, or 16 bytes */
2623 if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
2624 (spu->spu_type == SPU_TYPE_SPUM) &&
2625 (ctx->digestsize != 8) && (ctx->digestsize != 12) &&
2626 (ctx->digestsize != 16)) {
2627 flow_log("%s() AES CCM needs fallback for digest size %d\n",
2628 __func__, ctx->digestsize);
2633 * SPU-M on NSP has an issue where AES-CCM hash is not correct
2634 * when AAD size is 0
2636 if ((ctx->cipher.mode == CIPHER_MODE_CCM) &&
2637 (spu->spu_subtype == SPU_SUBTYPE_SPUM_NSP) &&
2638 (req->assoclen == 0)) {
2639 flow_log("%s() AES_CCM needs fallback for 0 len AAD on NSP\n",
2644 payload_len = req->cryptlen;
2645 if (spu->spu_type == SPU_TYPE_SPUM)
2646 payload_len += req->assoclen;
2648 flow_log("%s() payload len: %u\n", __func__, payload_len);
2650 if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
2653 return payload_len > ctx->max_payload;
2656 static void aead_complete(struct crypto_async_request *areq, int err)
2658 struct aead_request *req =
2659 container_of(areq, struct aead_request, base);
2660 struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2661 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2663 flow_log("%s() err:%d\n", __func__, err);
2665 areq->tfm = crypto_aead_tfm(aead);
2667 areq->complete = rctx->old_complete;
2668 areq->data = rctx->old_data;
2670 areq->complete(areq, err);
2673 static int aead_do_fallback(struct aead_request *req, bool is_encrypt)
2675 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2676 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
2677 struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2678 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
2682 flow_log("%s() enc:%u\n", __func__, is_encrypt);
2684 if (ctx->fallback_cipher) {
2685 /* Store the cipher tfm and then use the fallback tfm */
2686 rctx->old_tfm = tfm;
2687 aead_request_set_tfm(req, ctx->fallback_cipher);
2689 * Save the callback and chain ourselves in, so we can restore
2692 rctx->old_complete = req->base.complete;
2693 rctx->old_data = req->base.data;
2694 req_flags = aead_request_flags(req);
2695 aead_request_set_callback(req, req_flags, aead_complete, req);
2696 err = is_encrypt ? crypto_aead_encrypt(req) :
2697 crypto_aead_decrypt(req);
2701 * fallback was synchronous (did not return
2702 * -EINPROGRESS). So restore request state here.
2704 aead_request_set_callback(req, req_flags,
2705 rctx->old_complete, req);
2706 req->base.data = rctx->old_data;
2707 aead_request_set_tfm(req, aead);
2708 flow_log("%s() fallback completed successfully\n\n",
2718 static int aead_enqueue(struct aead_request *req, bool is_encrypt)
2720 struct iproc_reqctx_s *rctx = aead_request_ctx(req);
2721 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2722 struct iproc_ctx_s *ctx = crypto_aead_ctx(aead);
2725 flow_log("%s() enc:%u\n", __func__, is_encrypt);
2727 if (req->assoclen > MAX_ASSOC_SIZE) {
2729 ("%s() Error: associated data too long. (%u > %u bytes)\n",
2730 __func__, req->assoclen, MAX_ASSOC_SIZE);
2734 rctx->gfp = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2735 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2736 rctx->parent = &req->base;
2737 rctx->is_encrypt = is_encrypt;
2738 rctx->bd_suppress = false;
2739 rctx->total_todo = req->cryptlen;
2741 rctx->total_sent = 0;
2742 rctx->total_received = 0;
2743 rctx->is_sw_hmac = false;
2745 memset(&rctx->mb_mssg, 0, sizeof(struct brcm_message));
2747 /* assoc data is at start of src sg */
2748 rctx->assoc = req->src;
2751 * Init current position in src scatterlist to be after assoc data.
2752 * src_skip set to buffer offset where data begins. (Assoc data could
2753 * end in the middle of a buffer.)
2755 if (spu_sg_at_offset(req->src, req->assoclen, &rctx->src_sg,
2756 &rctx->src_skip) < 0) {
2757 pr_err("%s() Error: Unable to find start of src data\n",
2762 rctx->src_nents = 0;
2763 rctx->dst_nents = 0;
2764 if (req->dst == req->src) {
2765 rctx->dst_sg = rctx->src_sg;
2766 rctx->dst_skip = rctx->src_skip;
2769 * Expect req->dst to have room for assoc data followed by
2770 * output data and ICV, if encrypt. So initialize dst_sg
2771 * to point beyond assoc len offset.
2773 if (spu_sg_at_offset(req->dst, req->assoclen, &rctx->dst_sg,
2774 &rctx->dst_skip) < 0) {
2775 pr_err("%s() Error: Unable to find start of dst data\n",
2781 if (ctx->cipher.mode == CIPHER_MODE_CBC ||
2782 ctx->cipher.mode == CIPHER_MODE_CTR ||
2783 ctx->cipher.mode == CIPHER_MODE_OFB ||
2784 ctx->cipher.mode == CIPHER_MODE_XTS ||
2785 ctx->cipher.mode == CIPHER_MODE_GCM) {
2788 crypto_aead_ivsize(crypto_aead_reqtfm(req));
2789 } else if (ctx->cipher.mode == CIPHER_MODE_CCM) {
2790 rctx->iv_ctr_len = CCM_AES_IV_SIZE;
2792 rctx->iv_ctr_len = 0;
2795 rctx->hash_carry_len = 0;
2797 flow_log(" src sg: %p\n", req->src);
2798 flow_log(" rctx->src_sg: %p, src_skip %u\n",
2799 rctx->src_sg, rctx->src_skip);
2800 flow_log(" assoc: %p, assoclen %u\n", rctx->assoc, req->assoclen);
2801 flow_log(" dst sg: %p\n", req->dst);
2802 flow_log(" rctx->dst_sg: %p, dst_skip %u\n",
2803 rctx->dst_sg, rctx->dst_skip);
2804 flow_log(" iv_ctr_len:%u\n", rctx->iv_ctr_len);
2805 flow_dump(" iv: ", req->iv, rctx->iv_ctr_len);
2806 flow_log(" authkeylen:%u\n", ctx->authkeylen);
2807 flow_log(" is_esp: %s\n", ctx->is_esp ? "yes" : "no");
2809 if (ctx->max_payload == SPU_MAX_PAYLOAD_INF)
2810 flow_log(" max_payload infinite");
2812 flow_log(" max_payload: %u\n", ctx->max_payload);
2814 if (unlikely(aead_need_fallback(req)))
2815 return aead_do_fallback(req, is_encrypt);
2818 * Do memory allocations for request after fallback check, because if we
2819 * do fallback, we won't call finish_req() to dealloc.
2821 if (rctx->iv_ctr_len) {
2823 memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset,
2824 ctx->salt, ctx->salt_len);
2825 memcpy(rctx->msg_buf.iv_ctr + ctx->salt_offset + ctx->salt_len,
2827 rctx->iv_ctr_len - ctx->salt_len - ctx->salt_offset);
2830 rctx->chan_idx = select_channel();
2831 err = handle_aead_req(rctx);
2832 if (err != -EINPROGRESS)
2833 /* synchronous result */
2834 spu_chunk_cleanup(rctx);
2839 static int aead_authenc_setkey(struct crypto_aead *cipher,
2840 const u8 *key, unsigned int keylen)
2842 struct spu_hw *spu = &iproc_priv.spu;
2843 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2844 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
2845 struct crypto_authenc_keys keys;
2848 flow_log("%s() aead:%p key:%p keylen:%u\n", __func__, cipher, key,
2850 flow_dump(" key: ", key, keylen);
2852 ret = crypto_authenc_extractkeys(&keys, key, keylen);
2856 if (keys.enckeylen > MAX_KEY_SIZE ||
2857 keys.authkeylen > MAX_KEY_SIZE)
2860 ctx->enckeylen = keys.enckeylen;
2861 ctx->authkeylen = keys.authkeylen;
2863 memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
2864 /* May end up padding auth key. So make sure it's zeroed. */
2865 memset(ctx->authkey, 0, sizeof(ctx->authkey));
2866 memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
2868 switch (ctx->alg->cipher_info.alg) {
2869 case CIPHER_ALG_DES:
2870 if (ctx->enckeylen == DES_KEY_SIZE) {
2871 u32 tmp[DES_EXPKEY_WORDS];
2872 u32 flags = CRYPTO_TFM_RES_WEAK_KEY;
2874 if (des_ekey(tmp, keys.enckey) == 0) {
2875 if (crypto_aead_get_flags(cipher) &
2876 CRYPTO_TFM_REQ_WEAK_KEY) {
2877 crypto_aead_set_flags(cipher, flags);
2882 ctx->cipher_type = CIPHER_TYPE_DES;
2887 case CIPHER_ALG_3DES:
2888 if (ctx->enckeylen == (DES_KEY_SIZE * 3)) {
2889 const u32 *K = (const u32 *)keys.enckey;
2890 u32 flags = CRYPTO_TFM_RES_BAD_KEY_SCHED;
2892 if (!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
2893 !((K[2] ^ K[4]) | (K[3] ^ K[5]))) {
2894 crypto_aead_set_flags(cipher, flags);
2898 ctx->cipher_type = CIPHER_TYPE_3DES;
2900 crypto_aead_set_flags(cipher,
2901 CRYPTO_TFM_RES_BAD_KEY_LEN);
2905 case CIPHER_ALG_AES:
2906 switch (ctx->enckeylen) {
2907 case AES_KEYSIZE_128:
2908 ctx->cipher_type = CIPHER_TYPE_AES128;
2910 case AES_KEYSIZE_192:
2911 ctx->cipher_type = CIPHER_TYPE_AES192;
2913 case AES_KEYSIZE_256:
2914 ctx->cipher_type = CIPHER_TYPE_AES256;
2920 case CIPHER_ALG_RC4:
2921 ctx->cipher_type = CIPHER_TYPE_INIT;
2924 pr_err("%s() Error: Unknown cipher alg\n", __func__);
2928 flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
2930 flow_dump(" enc: ", ctx->enckey, ctx->enckeylen);
2931 flow_dump(" auth: ", ctx->authkey, ctx->authkeylen);
2933 /* setkey the fallback just in case we needto use it */
2934 if (ctx->fallback_cipher) {
2935 flow_log(" running fallback setkey()\n");
2937 ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
2938 ctx->fallback_cipher->base.crt_flags |=
2939 tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
2940 ret = crypto_aead_setkey(ctx->fallback_cipher, key, keylen);
2942 flow_log(" fallback setkey() returned:%d\n", ret);
2943 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
2945 (ctx->fallback_cipher->base.crt_flags &
2946 CRYPTO_TFM_RES_MASK);
2950 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
2954 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
2960 ctx->authkeylen = 0;
2961 ctx->digestsize = 0;
2963 crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
2967 static int aead_gcm_ccm_setkey(struct crypto_aead *cipher,
2968 const u8 *key, unsigned int keylen)
2970 struct spu_hw *spu = &iproc_priv.spu;
2971 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
2972 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
2976 flow_log("%s() keylen:%u\n", __func__, keylen);
2977 flow_dump(" key: ", key, keylen);
2980 ctx->digestsize = keylen;
2982 ctx->enckeylen = keylen;
2983 ctx->authkeylen = 0;
2985 switch (ctx->enckeylen) {
2986 case AES_KEYSIZE_128:
2987 ctx->cipher_type = CIPHER_TYPE_AES128;
2989 case AES_KEYSIZE_192:
2990 ctx->cipher_type = CIPHER_TYPE_AES192;
2992 case AES_KEYSIZE_256:
2993 ctx->cipher_type = CIPHER_TYPE_AES256;
2999 memcpy(ctx->enckey, key, ctx->enckeylen);
3001 flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
3003 flow_dump(" enc: ", ctx->enckey, ctx->enckeylen);
3004 flow_dump(" auth: ", ctx->authkey, ctx->authkeylen);
3006 /* setkey the fallback just in case we need to use it */
3007 if (ctx->fallback_cipher) {
3008 flow_log(" running fallback setkey()\n");
3010 ctx->fallback_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
3011 ctx->fallback_cipher->base.crt_flags |=
3012 tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
3013 ret = crypto_aead_setkey(ctx->fallback_cipher, key,
3014 keylen + ctx->salt_len);
3016 flow_log(" fallback setkey() returned:%d\n", ret);
3017 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
3019 (ctx->fallback_cipher->base.crt_flags &
3020 CRYPTO_TFM_RES_MASK);
3024 ctx->spu_resp_hdr_len = spu->spu_response_hdr_len(ctx->authkeylen,
3028 atomic_inc(&iproc_priv.setkey_cnt[SPU_OP_AEAD]);
3030 flow_log(" enckeylen:%u authkeylen:%u\n", ctx->enckeylen,
3037 ctx->authkeylen = 0;
3038 ctx->digestsize = 0;
3040 crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
3045 * aead_gcm_esp_setkey() - setkey() operation for ESP variant of GCM AES.
3046 * @cipher: AEAD structure
3047 * @key: Key followed by 4 bytes of salt
3048 * @keylen: Length of key plus salt, in bytes
3050 * Extracts salt from key and stores it to be prepended to IV on each request.
3051 * Digest is always 16 bytes
3053 * Return: Value from generic gcm setkey.
3055 static int aead_gcm_esp_setkey(struct crypto_aead *cipher,
3056 const u8 *key, unsigned int keylen)
3058 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3060 flow_log("%s\n", __func__);
3062 if (keylen < GCM_ESP_SALT_SIZE)
3065 ctx->salt_len = GCM_ESP_SALT_SIZE;
3066 ctx->salt_offset = GCM_ESP_SALT_OFFSET;
3067 memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
3068 keylen -= GCM_ESP_SALT_SIZE;
3069 ctx->digestsize = GCM_ESP_DIGESTSIZE;
3071 flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
3073 return aead_gcm_ccm_setkey(cipher, key, keylen);
3077 * rfc4543_gcm_esp_setkey() - setkey operation for RFC4543 variant of GCM/GMAC.
3078 * cipher: AEAD structure
3079 * key: Key followed by 4 bytes of salt
3080 * keylen: Length of key plus salt, in bytes
3082 * Extracts salt from key and stores it to be prepended to IV on each request.
3083 * Digest is always 16 bytes
3085 * Return: Value from generic gcm setkey.
3087 static int rfc4543_gcm_esp_setkey(struct crypto_aead *cipher,
3088 const u8 *key, unsigned int keylen)
3090 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3092 flow_log("%s\n", __func__);
3094 if (keylen < GCM_ESP_SALT_SIZE)
3097 ctx->salt_len = GCM_ESP_SALT_SIZE;
3098 ctx->salt_offset = GCM_ESP_SALT_OFFSET;
3099 memcpy(ctx->salt, key + keylen - GCM_ESP_SALT_SIZE, GCM_ESP_SALT_SIZE);
3100 keylen -= GCM_ESP_SALT_SIZE;
3101 ctx->digestsize = GCM_ESP_DIGESTSIZE;
3103 ctx->is_rfc4543 = true;
3104 flow_dump("salt: ", ctx->salt, GCM_ESP_SALT_SIZE);
3106 return aead_gcm_ccm_setkey(cipher, key, keylen);
3110 * aead_ccm_esp_setkey() - setkey() operation for ESP variant of CCM AES.
3111 * @cipher: AEAD structure
3112 * @key: Key followed by 4 bytes of salt
3113 * @keylen: Length of key plus salt, in bytes
3115 * Extracts salt from key and stores it to be prepended to IV on each request.
3116 * Digest is always 16 bytes
3118 * Return: Value from generic ccm setkey.
3120 static int aead_ccm_esp_setkey(struct crypto_aead *cipher,
3121 const u8 *key, unsigned int keylen)
3123 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3125 flow_log("%s\n", __func__);
3127 if (keylen < CCM_ESP_SALT_SIZE)
3130 ctx->salt_len = CCM_ESP_SALT_SIZE;
3131 ctx->salt_offset = CCM_ESP_SALT_OFFSET;
3132 memcpy(ctx->salt, key + keylen - CCM_ESP_SALT_SIZE, CCM_ESP_SALT_SIZE);
3133 keylen -= CCM_ESP_SALT_SIZE;
3135 flow_dump("salt: ", ctx->salt, CCM_ESP_SALT_SIZE);
3137 return aead_gcm_ccm_setkey(cipher, key, keylen);
3140 static int aead_setauthsize(struct crypto_aead *cipher, unsigned int authsize)
3142 struct iproc_ctx_s *ctx = crypto_aead_ctx(cipher);
3145 flow_log("%s() authkeylen:%u authsize:%u\n",
3146 __func__, ctx->authkeylen, authsize);
3148 ctx->digestsize = authsize;
3150 /* setkey the fallback just in case we needto use it */
3151 if (ctx->fallback_cipher) {
3152 flow_log(" running fallback setauth()\n");
3154 ret = crypto_aead_setauthsize(ctx->fallback_cipher, authsize);
3156 flow_log(" fallback setauth() returned:%d\n", ret);
3162 static int aead_encrypt(struct aead_request *req)
3164 flow_log("%s() cryptlen:%u %08x\n", __func__, req->cryptlen,
3166 dump_sg(req->src, 0, req->cryptlen + req->assoclen);
3167 flow_log(" assoc_len:%u\n", req->assoclen);
3169 return aead_enqueue(req, true);
3172 static int aead_decrypt(struct aead_request *req)
3174 flow_log("%s() cryptlen:%u\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, false);
3181 /* ==================== Supported Cipher Algorithms ==================== */
3183 static struct iproc_alg_s driver_algs[] = {
3185 .type = CRYPTO_ALG_TYPE_AEAD,
3188 .cra_name = "gcm(aes)",
3189 .cra_driver_name = "gcm-aes-iproc",
3190 .cra_blocksize = AES_BLOCK_SIZE,
3191 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3193 .setkey = aead_gcm_ccm_setkey,
3194 .ivsize = GCM_AES_IV_SIZE,
3195 .maxauthsize = AES_BLOCK_SIZE,
3198 .alg = CIPHER_ALG_AES,
3199 .mode = CIPHER_MODE_GCM,
3202 .alg = HASH_ALG_AES,
3203 .mode = HASH_MODE_GCM,
3208 .type = CRYPTO_ALG_TYPE_AEAD,
3211 .cra_name = "ccm(aes)",
3212 .cra_driver_name = "ccm-aes-iproc",
3213 .cra_blocksize = AES_BLOCK_SIZE,
3214 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3216 .setkey = aead_gcm_ccm_setkey,
3217 .ivsize = CCM_AES_IV_SIZE,
3218 .maxauthsize = AES_BLOCK_SIZE,
3221 .alg = CIPHER_ALG_AES,
3222 .mode = CIPHER_MODE_CCM,
3225 .alg = HASH_ALG_AES,
3226 .mode = HASH_MODE_CCM,
3231 .type = CRYPTO_ALG_TYPE_AEAD,
3234 .cra_name = "rfc4106(gcm(aes))",
3235 .cra_driver_name = "gcm-aes-esp-iproc",
3236 .cra_blocksize = AES_BLOCK_SIZE,
3237 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3239 .setkey = aead_gcm_esp_setkey,
3240 .ivsize = GCM_ESP_IV_SIZE,
3241 .maxauthsize = AES_BLOCK_SIZE,
3244 .alg = CIPHER_ALG_AES,
3245 .mode = CIPHER_MODE_GCM,
3248 .alg = HASH_ALG_AES,
3249 .mode = HASH_MODE_GCM,
3254 .type = CRYPTO_ALG_TYPE_AEAD,
3257 .cra_name = "rfc4309(ccm(aes))",
3258 .cra_driver_name = "ccm-aes-esp-iproc",
3259 .cra_blocksize = AES_BLOCK_SIZE,
3260 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3262 .setkey = aead_ccm_esp_setkey,
3263 .ivsize = CCM_AES_IV_SIZE,
3264 .maxauthsize = AES_BLOCK_SIZE,
3267 .alg = CIPHER_ALG_AES,
3268 .mode = CIPHER_MODE_CCM,
3271 .alg = HASH_ALG_AES,
3272 .mode = HASH_MODE_CCM,
3277 .type = CRYPTO_ALG_TYPE_AEAD,
3280 .cra_name = "rfc4543(gcm(aes))",
3281 .cra_driver_name = "gmac-aes-esp-iproc",
3282 .cra_blocksize = AES_BLOCK_SIZE,
3283 .cra_flags = CRYPTO_ALG_NEED_FALLBACK
3285 .setkey = rfc4543_gcm_esp_setkey,
3286 .ivsize = GCM_ESP_IV_SIZE,
3287 .maxauthsize = AES_BLOCK_SIZE,
3290 .alg = CIPHER_ALG_AES,
3291 .mode = CIPHER_MODE_GCM,
3294 .alg = HASH_ALG_AES,
3295 .mode = HASH_MODE_GCM,
3300 .type = CRYPTO_ALG_TYPE_AEAD,
3303 .cra_name = "authenc(hmac(md5),cbc(aes))",
3304 .cra_driver_name = "authenc-hmac-md5-cbc-aes-iproc",
3305 .cra_blocksize = AES_BLOCK_SIZE,
3306 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3308 .setkey = aead_authenc_setkey,
3309 .ivsize = AES_BLOCK_SIZE,
3310 .maxauthsize = MD5_DIGEST_SIZE,
3313 .alg = CIPHER_ALG_AES,
3314 .mode = CIPHER_MODE_CBC,
3317 .alg = HASH_ALG_MD5,
3318 .mode = HASH_MODE_HMAC,
3323 .type = CRYPTO_ALG_TYPE_AEAD,
3326 .cra_name = "authenc(hmac(sha1),cbc(aes))",
3327 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-iproc",
3328 .cra_blocksize = AES_BLOCK_SIZE,
3329 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3331 .setkey = aead_authenc_setkey,
3332 .ivsize = AES_BLOCK_SIZE,
3333 .maxauthsize = SHA1_DIGEST_SIZE,
3336 .alg = CIPHER_ALG_AES,
3337 .mode = CIPHER_MODE_CBC,
3340 .alg = HASH_ALG_SHA1,
3341 .mode = HASH_MODE_HMAC,
3346 .type = CRYPTO_ALG_TYPE_AEAD,
3349 .cra_name = "authenc(hmac(sha256),cbc(aes))",
3350 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-iproc",
3351 .cra_blocksize = AES_BLOCK_SIZE,
3352 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3354 .setkey = aead_authenc_setkey,
3355 .ivsize = AES_BLOCK_SIZE,
3356 .maxauthsize = SHA256_DIGEST_SIZE,
3359 .alg = CIPHER_ALG_AES,
3360 .mode = CIPHER_MODE_CBC,
3363 .alg = HASH_ALG_SHA256,
3364 .mode = HASH_MODE_HMAC,
3369 .type = CRYPTO_ALG_TYPE_AEAD,
3372 .cra_name = "authenc(hmac(md5),cbc(des))",
3373 .cra_driver_name = "authenc-hmac-md5-cbc-des-iproc",
3374 .cra_blocksize = DES_BLOCK_SIZE,
3375 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3377 .setkey = aead_authenc_setkey,
3378 .ivsize = DES_BLOCK_SIZE,
3379 .maxauthsize = MD5_DIGEST_SIZE,
3382 .alg = CIPHER_ALG_DES,
3383 .mode = CIPHER_MODE_CBC,
3386 .alg = HASH_ALG_MD5,
3387 .mode = HASH_MODE_HMAC,
3392 .type = CRYPTO_ALG_TYPE_AEAD,
3395 .cra_name = "authenc(hmac(sha1),cbc(des))",
3396 .cra_driver_name = "authenc-hmac-sha1-cbc-des-iproc",
3397 .cra_blocksize = DES_BLOCK_SIZE,
3398 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3400 .setkey = aead_authenc_setkey,
3401 .ivsize = DES_BLOCK_SIZE,
3402 .maxauthsize = SHA1_DIGEST_SIZE,
3405 .alg = CIPHER_ALG_DES,
3406 .mode = CIPHER_MODE_CBC,
3409 .alg = HASH_ALG_SHA1,
3410 .mode = HASH_MODE_HMAC,
3415 .type = CRYPTO_ALG_TYPE_AEAD,
3418 .cra_name = "authenc(hmac(sha224),cbc(des))",
3419 .cra_driver_name = "authenc-hmac-sha224-cbc-des-iproc",
3420 .cra_blocksize = DES_BLOCK_SIZE,
3421 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3423 .setkey = aead_authenc_setkey,
3424 .ivsize = DES_BLOCK_SIZE,
3425 .maxauthsize = SHA224_DIGEST_SIZE,
3428 .alg = CIPHER_ALG_DES,
3429 .mode = CIPHER_MODE_CBC,
3432 .alg = HASH_ALG_SHA224,
3433 .mode = HASH_MODE_HMAC,
3438 .type = CRYPTO_ALG_TYPE_AEAD,
3441 .cra_name = "authenc(hmac(sha256),cbc(des))",
3442 .cra_driver_name = "authenc-hmac-sha256-cbc-des-iproc",
3443 .cra_blocksize = DES_BLOCK_SIZE,
3444 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3446 .setkey = aead_authenc_setkey,
3447 .ivsize = DES_BLOCK_SIZE,
3448 .maxauthsize = SHA256_DIGEST_SIZE,
3451 .alg = CIPHER_ALG_DES,
3452 .mode = CIPHER_MODE_CBC,
3455 .alg = HASH_ALG_SHA256,
3456 .mode = HASH_MODE_HMAC,
3461 .type = CRYPTO_ALG_TYPE_AEAD,
3464 .cra_name = "authenc(hmac(sha384),cbc(des))",
3465 .cra_driver_name = "authenc-hmac-sha384-cbc-des-iproc",
3466 .cra_blocksize = DES_BLOCK_SIZE,
3467 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3469 .setkey = aead_authenc_setkey,
3470 .ivsize = DES_BLOCK_SIZE,
3471 .maxauthsize = SHA384_DIGEST_SIZE,
3474 .alg = CIPHER_ALG_DES,
3475 .mode = CIPHER_MODE_CBC,
3478 .alg = HASH_ALG_SHA384,
3479 .mode = HASH_MODE_HMAC,
3484 .type = CRYPTO_ALG_TYPE_AEAD,
3487 .cra_name = "authenc(hmac(sha512),cbc(des))",
3488 .cra_driver_name = "authenc-hmac-sha512-cbc-des-iproc",
3489 .cra_blocksize = DES_BLOCK_SIZE,
3490 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3492 .setkey = aead_authenc_setkey,
3493 .ivsize = DES_BLOCK_SIZE,
3494 .maxauthsize = SHA512_DIGEST_SIZE,
3497 .alg = CIPHER_ALG_DES,
3498 .mode = CIPHER_MODE_CBC,
3501 .alg = HASH_ALG_SHA512,
3502 .mode = HASH_MODE_HMAC,
3507 .type = CRYPTO_ALG_TYPE_AEAD,
3510 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
3511 .cra_driver_name = "authenc-hmac-md5-cbc-des3-iproc",
3512 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3513 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3515 .setkey = aead_authenc_setkey,
3516 .ivsize = DES3_EDE_BLOCK_SIZE,
3517 .maxauthsize = MD5_DIGEST_SIZE,
3520 .alg = CIPHER_ALG_3DES,
3521 .mode = CIPHER_MODE_CBC,
3524 .alg = HASH_ALG_MD5,
3525 .mode = HASH_MODE_HMAC,
3530 .type = CRYPTO_ALG_TYPE_AEAD,
3533 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
3534 .cra_driver_name = "authenc-hmac-sha1-cbc-des3-iproc",
3535 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3536 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3538 .setkey = aead_authenc_setkey,
3539 .ivsize = DES3_EDE_BLOCK_SIZE,
3540 .maxauthsize = SHA1_DIGEST_SIZE,
3543 .alg = CIPHER_ALG_3DES,
3544 .mode = CIPHER_MODE_CBC,
3547 .alg = HASH_ALG_SHA1,
3548 .mode = HASH_MODE_HMAC,
3553 .type = CRYPTO_ALG_TYPE_AEAD,
3556 .cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
3557 .cra_driver_name = "authenc-hmac-sha224-cbc-des3-iproc",
3558 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3559 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3561 .setkey = aead_authenc_setkey,
3562 .ivsize = DES3_EDE_BLOCK_SIZE,
3563 .maxauthsize = SHA224_DIGEST_SIZE,
3566 .alg = CIPHER_ALG_3DES,
3567 .mode = CIPHER_MODE_CBC,
3570 .alg = HASH_ALG_SHA224,
3571 .mode = HASH_MODE_HMAC,
3576 .type = CRYPTO_ALG_TYPE_AEAD,
3579 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
3580 .cra_driver_name = "authenc-hmac-sha256-cbc-des3-iproc",
3581 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3582 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3584 .setkey = aead_authenc_setkey,
3585 .ivsize = DES3_EDE_BLOCK_SIZE,
3586 .maxauthsize = SHA256_DIGEST_SIZE,
3589 .alg = CIPHER_ALG_3DES,
3590 .mode = CIPHER_MODE_CBC,
3593 .alg = HASH_ALG_SHA256,
3594 .mode = HASH_MODE_HMAC,
3599 .type = CRYPTO_ALG_TYPE_AEAD,
3602 .cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
3603 .cra_driver_name = "authenc-hmac-sha384-cbc-des3-iproc",
3604 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3605 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3607 .setkey = aead_authenc_setkey,
3608 .ivsize = DES3_EDE_BLOCK_SIZE,
3609 .maxauthsize = SHA384_DIGEST_SIZE,
3612 .alg = CIPHER_ALG_3DES,
3613 .mode = CIPHER_MODE_CBC,
3616 .alg = HASH_ALG_SHA384,
3617 .mode = HASH_MODE_HMAC,
3622 .type = CRYPTO_ALG_TYPE_AEAD,
3625 .cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
3626 .cra_driver_name = "authenc-hmac-sha512-cbc-des3-iproc",
3627 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3628 .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC
3630 .setkey = aead_authenc_setkey,
3631 .ivsize = DES3_EDE_BLOCK_SIZE,
3632 .maxauthsize = SHA512_DIGEST_SIZE,
3635 .alg = CIPHER_ALG_3DES,
3636 .mode = CIPHER_MODE_CBC,
3639 .alg = HASH_ALG_SHA512,
3640 .mode = HASH_MODE_HMAC,
3645 /* ABLKCIPHER algorithms. */
3647 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3649 .cra_name = "ecb(arc4)",
3650 .cra_driver_name = "ecb-arc4-iproc",
3651 .cra_blocksize = ARC4_BLOCK_SIZE,
3653 .min_keysize = ARC4_MIN_KEY_SIZE,
3654 .max_keysize = ARC4_MAX_KEY_SIZE,
3659 .alg = CIPHER_ALG_RC4,
3660 .mode = CIPHER_MODE_NONE,
3663 .alg = HASH_ALG_NONE,
3664 .mode = HASH_MODE_NONE,
3668 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3670 .cra_name = "ofb(des)",
3671 .cra_driver_name = "ofb-des-iproc",
3672 .cra_blocksize = DES_BLOCK_SIZE,
3674 .min_keysize = DES_KEY_SIZE,
3675 .max_keysize = DES_KEY_SIZE,
3676 .ivsize = DES_BLOCK_SIZE,
3680 .alg = CIPHER_ALG_DES,
3681 .mode = CIPHER_MODE_OFB,
3684 .alg = HASH_ALG_NONE,
3685 .mode = HASH_MODE_NONE,
3689 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3691 .cra_name = "cbc(des)",
3692 .cra_driver_name = "cbc-des-iproc",
3693 .cra_blocksize = DES_BLOCK_SIZE,
3695 .min_keysize = DES_KEY_SIZE,
3696 .max_keysize = DES_KEY_SIZE,
3697 .ivsize = DES_BLOCK_SIZE,
3701 .alg = CIPHER_ALG_DES,
3702 .mode = CIPHER_MODE_CBC,
3705 .alg = HASH_ALG_NONE,
3706 .mode = HASH_MODE_NONE,
3710 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3712 .cra_name = "ecb(des)",
3713 .cra_driver_name = "ecb-des-iproc",
3714 .cra_blocksize = DES_BLOCK_SIZE,
3716 .min_keysize = DES_KEY_SIZE,
3717 .max_keysize = DES_KEY_SIZE,
3722 .alg = CIPHER_ALG_DES,
3723 .mode = CIPHER_MODE_ECB,
3726 .alg = HASH_ALG_NONE,
3727 .mode = HASH_MODE_NONE,
3731 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3733 .cra_name = "ofb(des3_ede)",
3734 .cra_driver_name = "ofb-des3-iproc",
3735 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3737 .min_keysize = DES3_EDE_KEY_SIZE,
3738 .max_keysize = DES3_EDE_KEY_SIZE,
3739 .ivsize = DES3_EDE_BLOCK_SIZE,
3743 .alg = CIPHER_ALG_3DES,
3744 .mode = CIPHER_MODE_OFB,
3747 .alg = HASH_ALG_NONE,
3748 .mode = HASH_MODE_NONE,
3752 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3754 .cra_name = "cbc(des3_ede)",
3755 .cra_driver_name = "cbc-des3-iproc",
3756 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3758 .min_keysize = DES3_EDE_KEY_SIZE,
3759 .max_keysize = DES3_EDE_KEY_SIZE,
3760 .ivsize = DES3_EDE_BLOCK_SIZE,
3764 .alg = CIPHER_ALG_3DES,
3765 .mode = CIPHER_MODE_CBC,
3768 .alg = HASH_ALG_NONE,
3769 .mode = HASH_MODE_NONE,
3773 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3775 .cra_name = "ecb(des3_ede)",
3776 .cra_driver_name = "ecb-des3-iproc",
3777 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3779 .min_keysize = DES3_EDE_KEY_SIZE,
3780 .max_keysize = DES3_EDE_KEY_SIZE,
3785 .alg = CIPHER_ALG_3DES,
3786 .mode = CIPHER_MODE_ECB,
3789 .alg = HASH_ALG_NONE,
3790 .mode = HASH_MODE_NONE,
3794 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3796 .cra_name = "ofb(aes)",
3797 .cra_driver_name = "ofb-aes-iproc",
3798 .cra_blocksize = AES_BLOCK_SIZE,
3800 .min_keysize = AES_MIN_KEY_SIZE,
3801 .max_keysize = AES_MAX_KEY_SIZE,
3802 .ivsize = AES_BLOCK_SIZE,
3806 .alg = CIPHER_ALG_AES,
3807 .mode = CIPHER_MODE_OFB,
3810 .alg = HASH_ALG_NONE,
3811 .mode = HASH_MODE_NONE,
3815 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3817 .cra_name = "cbc(aes)",
3818 .cra_driver_name = "cbc-aes-iproc",
3819 .cra_blocksize = AES_BLOCK_SIZE,
3821 .min_keysize = AES_MIN_KEY_SIZE,
3822 .max_keysize = AES_MAX_KEY_SIZE,
3823 .ivsize = AES_BLOCK_SIZE,
3827 .alg = CIPHER_ALG_AES,
3828 .mode = CIPHER_MODE_CBC,
3831 .alg = HASH_ALG_NONE,
3832 .mode = HASH_MODE_NONE,
3836 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3838 .cra_name = "ecb(aes)",
3839 .cra_driver_name = "ecb-aes-iproc",
3840 .cra_blocksize = AES_BLOCK_SIZE,
3842 .min_keysize = AES_MIN_KEY_SIZE,
3843 .max_keysize = AES_MAX_KEY_SIZE,
3848 .alg = CIPHER_ALG_AES,
3849 .mode = CIPHER_MODE_ECB,
3852 .alg = HASH_ALG_NONE,
3853 .mode = HASH_MODE_NONE,
3857 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3859 .cra_name = "ctr(aes)",
3860 .cra_driver_name = "ctr-aes-iproc",
3861 .cra_blocksize = AES_BLOCK_SIZE,
3863 /* .geniv = "chainiv", */
3864 .min_keysize = AES_MIN_KEY_SIZE,
3865 .max_keysize = AES_MAX_KEY_SIZE,
3866 .ivsize = AES_BLOCK_SIZE,
3870 .alg = CIPHER_ALG_AES,
3871 .mode = CIPHER_MODE_CTR,
3874 .alg = HASH_ALG_NONE,
3875 .mode = HASH_MODE_NONE,
3879 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3881 .cra_name = "xts(aes)",
3882 .cra_driver_name = "xts-aes-iproc",
3883 .cra_blocksize = AES_BLOCK_SIZE,
3885 .min_keysize = 2 * AES_MIN_KEY_SIZE,
3886 .max_keysize = 2 * AES_MAX_KEY_SIZE,
3887 .ivsize = AES_BLOCK_SIZE,
3891 .alg = CIPHER_ALG_AES,
3892 .mode = CIPHER_MODE_XTS,
3895 .alg = HASH_ALG_NONE,
3896 .mode = HASH_MODE_NONE,
3900 /* AHASH algorithms. */
3902 .type = CRYPTO_ALG_TYPE_AHASH,
3904 .halg.digestsize = MD5_DIGEST_SIZE,
3907 .cra_driver_name = "md5-iproc",
3908 .cra_blocksize = MD5_BLOCK_WORDS * 4,
3909 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
3914 .alg = CIPHER_ALG_NONE,
3915 .mode = CIPHER_MODE_NONE,
3918 .alg = HASH_ALG_MD5,
3919 .mode = HASH_MODE_HASH,
3923 .type = CRYPTO_ALG_TYPE_AHASH,
3925 .halg.digestsize = MD5_DIGEST_SIZE,
3927 .cra_name = "hmac(md5)",
3928 .cra_driver_name = "hmac-md5-iproc",
3929 .cra_blocksize = MD5_BLOCK_WORDS * 4,
3933 .alg = CIPHER_ALG_NONE,
3934 .mode = CIPHER_MODE_NONE,
3937 .alg = HASH_ALG_MD5,
3938 .mode = HASH_MODE_HMAC,
3941 {.type = CRYPTO_ALG_TYPE_AHASH,
3943 .halg.digestsize = SHA1_DIGEST_SIZE,
3946 .cra_driver_name = "sha1-iproc",
3947 .cra_blocksize = SHA1_BLOCK_SIZE,
3951 .alg = CIPHER_ALG_NONE,
3952 .mode = CIPHER_MODE_NONE,
3955 .alg = HASH_ALG_SHA1,
3956 .mode = HASH_MODE_HASH,
3959 {.type = CRYPTO_ALG_TYPE_AHASH,
3961 .halg.digestsize = SHA1_DIGEST_SIZE,
3963 .cra_name = "hmac(sha1)",
3964 .cra_driver_name = "hmac-sha1-iproc",
3965 .cra_blocksize = SHA1_BLOCK_SIZE,
3969 .alg = CIPHER_ALG_NONE,
3970 .mode = CIPHER_MODE_NONE,
3973 .alg = HASH_ALG_SHA1,
3974 .mode = HASH_MODE_HMAC,
3977 {.type = CRYPTO_ALG_TYPE_AHASH,
3979 .halg.digestsize = SHA224_DIGEST_SIZE,
3981 .cra_name = "sha224",
3982 .cra_driver_name = "sha224-iproc",
3983 .cra_blocksize = SHA224_BLOCK_SIZE,
3987 .alg = CIPHER_ALG_NONE,
3988 .mode = CIPHER_MODE_NONE,
3991 .alg = HASH_ALG_SHA224,
3992 .mode = HASH_MODE_HASH,
3995 {.type = CRYPTO_ALG_TYPE_AHASH,
3997 .halg.digestsize = SHA224_DIGEST_SIZE,
3999 .cra_name = "hmac(sha224)",
4000 .cra_driver_name = "hmac-sha224-iproc",
4001 .cra_blocksize = SHA224_BLOCK_SIZE,
4005 .alg = CIPHER_ALG_NONE,
4006 .mode = CIPHER_MODE_NONE,
4009 .alg = HASH_ALG_SHA224,
4010 .mode = HASH_MODE_HMAC,
4013 {.type = CRYPTO_ALG_TYPE_AHASH,
4015 .halg.digestsize = SHA256_DIGEST_SIZE,
4017 .cra_name = "sha256",
4018 .cra_driver_name = "sha256-iproc",
4019 .cra_blocksize = SHA256_BLOCK_SIZE,
4023 .alg = CIPHER_ALG_NONE,
4024 .mode = CIPHER_MODE_NONE,
4027 .alg = HASH_ALG_SHA256,
4028 .mode = HASH_MODE_HASH,
4031 {.type = CRYPTO_ALG_TYPE_AHASH,
4033 .halg.digestsize = SHA256_DIGEST_SIZE,
4035 .cra_name = "hmac(sha256)",
4036 .cra_driver_name = "hmac-sha256-iproc",
4037 .cra_blocksize = SHA256_BLOCK_SIZE,
4041 .alg = CIPHER_ALG_NONE,
4042 .mode = CIPHER_MODE_NONE,
4045 .alg = HASH_ALG_SHA256,
4046 .mode = HASH_MODE_HMAC,
4050 .type = CRYPTO_ALG_TYPE_AHASH,
4052 .halg.digestsize = SHA384_DIGEST_SIZE,
4054 .cra_name = "sha384",
4055 .cra_driver_name = "sha384-iproc",
4056 .cra_blocksize = SHA384_BLOCK_SIZE,
4060 .alg = CIPHER_ALG_NONE,
4061 .mode = CIPHER_MODE_NONE,
4064 .alg = HASH_ALG_SHA384,
4065 .mode = HASH_MODE_HASH,
4069 .type = CRYPTO_ALG_TYPE_AHASH,
4071 .halg.digestsize = SHA384_DIGEST_SIZE,
4073 .cra_name = "hmac(sha384)",
4074 .cra_driver_name = "hmac-sha384-iproc",
4075 .cra_blocksize = SHA384_BLOCK_SIZE,
4079 .alg = CIPHER_ALG_NONE,
4080 .mode = CIPHER_MODE_NONE,
4083 .alg = HASH_ALG_SHA384,
4084 .mode = HASH_MODE_HMAC,
4088 .type = CRYPTO_ALG_TYPE_AHASH,
4090 .halg.digestsize = SHA512_DIGEST_SIZE,
4092 .cra_name = "sha512",
4093 .cra_driver_name = "sha512-iproc",
4094 .cra_blocksize = SHA512_BLOCK_SIZE,
4098 .alg = CIPHER_ALG_NONE,
4099 .mode = CIPHER_MODE_NONE,
4102 .alg = HASH_ALG_SHA512,
4103 .mode = HASH_MODE_HASH,
4107 .type = CRYPTO_ALG_TYPE_AHASH,
4109 .halg.digestsize = SHA512_DIGEST_SIZE,
4111 .cra_name = "hmac(sha512)",
4112 .cra_driver_name = "hmac-sha512-iproc",
4113 .cra_blocksize = SHA512_BLOCK_SIZE,
4117 .alg = CIPHER_ALG_NONE,
4118 .mode = CIPHER_MODE_NONE,
4121 .alg = HASH_ALG_SHA512,
4122 .mode = HASH_MODE_HMAC,
4126 .type = CRYPTO_ALG_TYPE_AHASH,
4128 .halg.digestsize = SHA3_224_DIGEST_SIZE,
4130 .cra_name = "sha3-224",
4131 .cra_driver_name = "sha3-224-iproc",
4132 .cra_blocksize = SHA3_224_BLOCK_SIZE,
4136 .alg = CIPHER_ALG_NONE,
4137 .mode = CIPHER_MODE_NONE,
4140 .alg = HASH_ALG_SHA3_224,
4141 .mode = HASH_MODE_HASH,
4145 .type = CRYPTO_ALG_TYPE_AHASH,
4147 .halg.digestsize = SHA3_224_DIGEST_SIZE,
4149 .cra_name = "hmac(sha3-224)",
4150 .cra_driver_name = "hmac-sha3-224-iproc",
4151 .cra_blocksize = SHA3_224_BLOCK_SIZE,
4155 .alg = CIPHER_ALG_NONE,
4156 .mode = CIPHER_MODE_NONE,
4159 .alg = HASH_ALG_SHA3_224,
4160 .mode = HASH_MODE_HMAC
4164 .type = CRYPTO_ALG_TYPE_AHASH,
4166 .halg.digestsize = SHA3_256_DIGEST_SIZE,
4168 .cra_name = "sha3-256",
4169 .cra_driver_name = "sha3-256-iproc",
4170 .cra_blocksize = SHA3_256_BLOCK_SIZE,
4174 .alg = CIPHER_ALG_NONE,
4175 .mode = CIPHER_MODE_NONE,
4178 .alg = HASH_ALG_SHA3_256,
4179 .mode = HASH_MODE_HASH,
4183 .type = CRYPTO_ALG_TYPE_AHASH,
4185 .halg.digestsize = SHA3_256_DIGEST_SIZE,
4187 .cra_name = "hmac(sha3-256)",
4188 .cra_driver_name = "hmac-sha3-256-iproc",
4189 .cra_blocksize = SHA3_256_BLOCK_SIZE,
4193 .alg = CIPHER_ALG_NONE,
4194 .mode = CIPHER_MODE_NONE,
4197 .alg = HASH_ALG_SHA3_256,
4198 .mode = HASH_MODE_HMAC,
4202 .type = CRYPTO_ALG_TYPE_AHASH,
4204 .halg.digestsize = SHA3_384_DIGEST_SIZE,
4206 .cra_name = "sha3-384",
4207 .cra_driver_name = "sha3-384-iproc",
4208 .cra_blocksize = SHA3_224_BLOCK_SIZE,
4212 .alg = CIPHER_ALG_NONE,
4213 .mode = CIPHER_MODE_NONE,
4216 .alg = HASH_ALG_SHA3_384,
4217 .mode = HASH_MODE_HASH,
4221 .type = CRYPTO_ALG_TYPE_AHASH,
4223 .halg.digestsize = SHA3_384_DIGEST_SIZE,
4225 .cra_name = "hmac(sha3-384)",
4226 .cra_driver_name = "hmac-sha3-384-iproc",
4227 .cra_blocksize = SHA3_384_BLOCK_SIZE,
4231 .alg = CIPHER_ALG_NONE,
4232 .mode = CIPHER_MODE_NONE,
4235 .alg = HASH_ALG_SHA3_384,
4236 .mode = HASH_MODE_HMAC,
4240 .type = CRYPTO_ALG_TYPE_AHASH,
4242 .halg.digestsize = SHA3_512_DIGEST_SIZE,
4244 .cra_name = "sha3-512",
4245 .cra_driver_name = "sha3-512-iproc",
4246 .cra_blocksize = SHA3_512_BLOCK_SIZE,
4250 .alg = CIPHER_ALG_NONE,
4251 .mode = CIPHER_MODE_NONE,
4254 .alg = HASH_ALG_SHA3_512,
4255 .mode = HASH_MODE_HASH,
4259 .type = CRYPTO_ALG_TYPE_AHASH,
4261 .halg.digestsize = SHA3_512_DIGEST_SIZE,
4263 .cra_name = "hmac(sha3-512)",
4264 .cra_driver_name = "hmac-sha3-512-iproc",
4265 .cra_blocksize = SHA3_512_BLOCK_SIZE,
4269 .alg = CIPHER_ALG_NONE,
4270 .mode = CIPHER_MODE_NONE,
4273 .alg = HASH_ALG_SHA3_512,
4274 .mode = HASH_MODE_HMAC,
4278 .type = CRYPTO_ALG_TYPE_AHASH,
4280 .halg.digestsize = AES_BLOCK_SIZE,
4282 .cra_name = "xcbc(aes)",
4283 .cra_driver_name = "xcbc-aes-iproc",
4284 .cra_blocksize = AES_BLOCK_SIZE,
4288 .alg = CIPHER_ALG_NONE,
4289 .mode = CIPHER_MODE_NONE,
4292 .alg = HASH_ALG_AES,
4293 .mode = HASH_MODE_XCBC,
4297 .type = CRYPTO_ALG_TYPE_AHASH,
4299 .halg.digestsize = AES_BLOCK_SIZE,
4301 .cra_name = "cmac(aes)",
4302 .cra_driver_name = "cmac-aes-iproc",
4303 .cra_blocksize = AES_BLOCK_SIZE,
4307 .alg = CIPHER_ALG_NONE,
4308 .mode = CIPHER_MODE_NONE,
4311 .alg = HASH_ALG_AES,
4312 .mode = HASH_MODE_CMAC,
4317 static int generic_cra_init(struct crypto_tfm *tfm,
4318 struct iproc_alg_s *cipher_alg)
4320 struct spu_hw *spu = &iproc_priv.spu;
4321 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4322 unsigned int blocksize = crypto_tfm_alg_blocksize(tfm);
4324 flow_log("%s()\n", __func__);
4326 ctx->alg = cipher_alg;
4327 ctx->cipher = cipher_alg->cipher_info;
4328 ctx->auth = cipher_alg->auth_info;
4329 ctx->auth_first = cipher_alg->auth_first;
4330 ctx->max_payload = spu->spu_ctx_max_payload(ctx->cipher.alg,
4333 ctx->fallback_cipher = NULL;
4336 ctx->authkeylen = 0;
4338 atomic_inc(&iproc_priv.stream_count);
4339 atomic_inc(&iproc_priv.session_count);
4344 static int ablkcipher_cra_init(struct crypto_tfm *tfm)
4346 struct crypto_alg *alg = tfm->__crt_alg;
4347 struct iproc_alg_s *cipher_alg;
4349 flow_log("%s()\n", __func__);
4351 tfm->crt_ablkcipher.reqsize = sizeof(struct iproc_reqctx_s);
4353 cipher_alg = container_of(alg, struct iproc_alg_s, alg.crypto);
4354 return generic_cra_init(tfm, cipher_alg);
4357 static int ahash_cra_init(struct crypto_tfm *tfm)
4360 struct crypto_alg *alg = tfm->__crt_alg;
4361 struct iproc_alg_s *cipher_alg;
4363 cipher_alg = container_of(__crypto_ahash_alg(alg), struct iproc_alg_s,
4366 err = generic_cra_init(tfm, cipher_alg);
4367 flow_log("%s()\n", __func__);
4370 * export state size has to be < 512 bytes. So don't include msg bufs
4373 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
4374 sizeof(struct iproc_reqctx_s));
4379 static int aead_cra_init(struct crypto_aead *aead)
4381 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
4382 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4383 struct crypto_alg *alg = tfm->__crt_alg;
4384 struct aead_alg *aalg = container_of(alg, struct aead_alg, base);
4385 struct iproc_alg_s *cipher_alg = container_of(aalg, struct iproc_alg_s,
4388 int err = generic_cra_init(tfm, cipher_alg);
4390 flow_log("%s()\n", __func__);
4392 crypto_aead_set_reqsize(aead, sizeof(struct iproc_reqctx_s));
4393 ctx->is_esp = false;
4395 ctx->salt_offset = 0;
4397 /* random first IV */
4398 get_random_bytes(ctx->iv, MAX_IV_SIZE);
4399 flow_dump(" iv: ", ctx->iv, MAX_IV_SIZE);
4402 if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
4403 flow_log("%s() creating fallback cipher\n", __func__);
4405 ctx->fallback_cipher =
4406 crypto_alloc_aead(alg->cra_name, 0,
4408 CRYPTO_ALG_NEED_FALLBACK);
4409 if (IS_ERR(ctx->fallback_cipher)) {
4410 pr_err("%s() Error: failed to allocate fallback for %s\n",
4411 __func__, alg->cra_name);
4412 return PTR_ERR(ctx->fallback_cipher);
4420 static void generic_cra_exit(struct crypto_tfm *tfm)
4422 atomic_dec(&iproc_priv.session_count);
4425 static void aead_cra_exit(struct crypto_aead *aead)
4427 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
4428 struct iproc_ctx_s *ctx = crypto_tfm_ctx(tfm);
4430 generic_cra_exit(tfm);
4432 if (ctx->fallback_cipher) {
4433 crypto_free_aead(ctx->fallback_cipher);
4434 ctx->fallback_cipher = NULL;
4439 * spu_functions_register() - Specify hardware-specific SPU functions based on
4440 * SPU type read from device tree.
4441 * @dev: device structure
4442 * @spu_type: SPU hardware generation
4443 * @spu_subtype: SPU hardware version
4445 static void spu_functions_register(struct device *dev,
4446 enum spu_spu_type spu_type,
4447 enum spu_spu_subtype spu_subtype)
4449 struct spu_hw *spu = &iproc_priv.spu;
4451 if (spu_type == SPU_TYPE_SPUM) {
4452 dev_dbg(dev, "Registering SPUM functions");
4453 spu->spu_dump_msg_hdr = spum_dump_msg_hdr;
4454 spu->spu_payload_length = spum_payload_length;
4455 spu->spu_response_hdr_len = spum_response_hdr_len;
4456 spu->spu_hash_pad_len = spum_hash_pad_len;
4457 spu->spu_gcm_ccm_pad_len = spum_gcm_ccm_pad_len;
4458 spu->spu_assoc_resp_len = spum_assoc_resp_len;
4459 spu->spu_aead_ivlen = spum_aead_ivlen;
4460 spu->spu_hash_type = spum_hash_type;
4461 spu->spu_digest_size = spum_digest_size;
4462 spu->spu_create_request = spum_create_request;
4463 spu->spu_cipher_req_init = spum_cipher_req_init;
4464 spu->spu_cipher_req_finish = spum_cipher_req_finish;
4465 spu->spu_request_pad = spum_request_pad;
4466 spu->spu_tx_status_len = spum_tx_status_len;
4467 spu->spu_rx_status_len = spum_rx_status_len;
4468 spu->spu_status_process = spum_status_process;
4469 spu->spu_xts_tweak_in_payload = spum_xts_tweak_in_payload;
4470 spu->spu_ccm_update_iv = spum_ccm_update_iv;
4471 spu->spu_wordalign_padlen = spum_wordalign_padlen;
4472 if (spu_subtype == SPU_SUBTYPE_SPUM_NS2)
4473 spu->spu_ctx_max_payload = spum_ns2_ctx_max_payload;
4475 spu->spu_ctx_max_payload = spum_nsp_ctx_max_payload;
4477 dev_dbg(dev, "Registering SPU2 functions");
4478 spu->spu_dump_msg_hdr = spu2_dump_msg_hdr;
4479 spu->spu_ctx_max_payload = spu2_ctx_max_payload;
4480 spu->spu_payload_length = spu2_payload_length;
4481 spu->spu_response_hdr_len = spu2_response_hdr_len;
4482 spu->spu_hash_pad_len = spu2_hash_pad_len;
4483 spu->spu_gcm_ccm_pad_len = spu2_gcm_ccm_pad_len;
4484 spu->spu_assoc_resp_len = spu2_assoc_resp_len;
4485 spu->spu_aead_ivlen = spu2_aead_ivlen;
4486 spu->spu_hash_type = spu2_hash_type;
4487 spu->spu_digest_size = spu2_digest_size;
4488 spu->spu_create_request = spu2_create_request;
4489 spu->spu_cipher_req_init = spu2_cipher_req_init;
4490 spu->spu_cipher_req_finish = spu2_cipher_req_finish;
4491 spu->spu_request_pad = spu2_request_pad;
4492 spu->spu_tx_status_len = spu2_tx_status_len;
4493 spu->spu_rx_status_len = spu2_rx_status_len;
4494 spu->spu_status_process = spu2_status_process;
4495 spu->spu_xts_tweak_in_payload = spu2_xts_tweak_in_payload;
4496 spu->spu_ccm_update_iv = spu2_ccm_update_iv;
4497 spu->spu_wordalign_padlen = spu2_wordalign_padlen;
4502 * spu_mb_init() - Initialize mailbox client. Request ownership of a mailbox
4503 * channel for the SPU being probed.
4504 * @dev: SPU driver device structure
4506 * Return: 0 if successful
4509 static int spu_mb_init(struct device *dev)
4511 struct mbox_client *mcl = &iproc_priv.mcl;
4514 iproc_priv.mbox = devm_kcalloc(dev, iproc_priv.spu.num_chan,
4515 sizeof(struct mbox_chan *), GFP_KERNEL);
4516 if (!iproc_priv.mbox)
4520 mcl->tx_block = false;
4522 mcl->knows_txdone = true;
4523 mcl->rx_callback = spu_rx_callback;
4524 mcl->tx_done = NULL;
4526 for (i = 0; i < iproc_priv.spu.num_chan; i++) {
4527 iproc_priv.mbox[i] = mbox_request_channel(mcl, i);
4528 if (IS_ERR(iproc_priv.mbox[i])) {
4529 err = (int)PTR_ERR(iproc_priv.mbox[i]);
4531 "Mbox channel %d request failed with err %d",
4533 iproc_priv.mbox[i] = NULL;
4540 for (i = 0; i < iproc_priv.spu.num_chan; i++) {
4541 if (iproc_priv.mbox[i])
4542 mbox_free_channel(iproc_priv.mbox[i]);
4548 static void spu_mb_release(struct platform_device *pdev)
4552 for (i = 0; i < iproc_priv.spu.num_chan; i++)
4553 mbox_free_channel(iproc_priv.mbox[i]);
4556 static void spu_counters_init(void)
4561 atomic_set(&iproc_priv.session_count, 0);
4562 atomic_set(&iproc_priv.stream_count, 0);
4563 atomic_set(&iproc_priv.next_chan, (int)iproc_priv.spu.num_chan);
4564 atomic64_set(&iproc_priv.bytes_in, 0);
4565 atomic64_set(&iproc_priv.bytes_out, 0);
4566 for (i = 0; i < SPU_OP_NUM; i++) {
4567 atomic_set(&iproc_priv.op_counts[i], 0);
4568 atomic_set(&iproc_priv.setkey_cnt[i], 0);
4570 for (i = 0; i < CIPHER_ALG_LAST; i++)
4571 for (j = 0; j < CIPHER_MODE_LAST; j++)
4572 atomic_set(&iproc_priv.cipher_cnt[i][j], 0);
4574 for (i = 0; i < HASH_ALG_LAST; i++) {
4575 atomic_set(&iproc_priv.hash_cnt[i], 0);
4576 atomic_set(&iproc_priv.hmac_cnt[i], 0);
4578 for (i = 0; i < AEAD_TYPE_LAST; i++)
4579 atomic_set(&iproc_priv.aead_cnt[i], 0);
4581 atomic_set(&iproc_priv.mb_no_spc, 0);
4582 atomic_set(&iproc_priv.mb_send_fail, 0);
4583 atomic_set(&iproc_priv.bad_icv, 0);
4586 static int spu_register_ablkcipher(struct iproc_alg_s *driver_alg)
4588 struct spu_hw *spu = &iproc_priv.spu;
4589 struct crypto_alg *crypto = &driver_alg->alg.crypto;
4592 /* SPU2 does not support RC4 */
4593 if ((driver_alg->cipher_info.alg == CIPHER_ALG_RC4) &&
4594 (spu->spu_type == SPU_TYPE_SPU2))
4597 crypto->cra_module = THIS_MODULE;
4598 crypto->cra_priority = cipher_pri;
4599 crypto->cra_alignmask = 0;
4600 crypto->cra_ctxsize = sizeof(struct iproc_ctx_s);
4601 INIT_LIST_HEAD(&crypto->cra_list);
4603 crypto->cra_init = ablkcipher_cra_init;
4604 crypto->cra_exit = generic_cra_exit;
4605 crypto->cra_type = &crypto_ablkcipher_type;
4606 crypto->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
4607 CRYPTO_ALG_KERN_DRIVER_ONLY;
4609 crypto->cra_ablkcipher.setkey = ablkcipher_setkey;
4610 crypto->cra_ablkcipher.encrypt = ablkcipher_encrypt;
4611 crypto->cra_ablkcipher.decrypt = ablkcipher_decrypt;
4613 err = crypto_register_alg(crypto);
4614 /* Mark alg as having been registered, if successful */
4616 driver_alg->registered = true;
4617 pr_debug(" registered ablkcipher %s\n", crypto->cra_driver_name);
4621 static int spu_register_ahash(struct iproc_alg_s *driver_alg)
4623 struct spu_hw *spu = &iproc_priv.spu;
4624 struct ahash_alg *hash = &driver_alg->alg.hash;
4627 /* AES-XCBC is the only AES hash type currently supported on SPU-M */
4628 if ((driver_alg->auth_info.alg == HASH_ALG_AES) &&
4629 (driver_alg->auth_info.mode != HASH_MODE_XCBC) &&
4630 (spu->spu_type == SPU_TYPE_SPUM))
4633 /* SHA3 algorithm variants are not registered for SPU-M or SPU2. */
4634 if ((driver_alg->auth_info.alg >= HASH_ALG_SHA3_224) &&
4635 (spu->spu_subtype != SPU_SUBTYPE_SPU2_V2))
4638 hash->halg.base.cra_module = THIS_MODULE;
4639 hash->halg.base.cra_priority = hash_pri;
4640 hash->halg.base.cra_alignmask = 0;
4641 hash->halg.base.cra_ctxsize = sizeof(struct iproc_ctx_s);
4642 hash->halg.base.cra_init = ahash_cra_init;
4643 hash->halg.base.cra_exit = generic_cra_exit;
4644 hash->halg.base.cra_type = &crypto_ahash_type;
4645 hash->halg.base.cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC;
4646 hash->halg.statesize = sizeof(struct spu_hash_export_s);
4648 if (driver_alg->auth_info.mode != HASH_MODE_HMAC) {
4649 hash->init = ahash_init;
4650 hash->update = ahash_update;
4651 hash->final = ahash_final;
4652 hash->finup = ahash_finup;
4653 hash->digest = ahash_digest;
4654 if ((driver_alg->auth_info.alg == HASH_ALG_AES) &&
4655 ((driver_alg->auth_info.mode == HASH_MODE_XCBC) ||
4656 (driver_alg->auth_info.mode == HASH_MODE_CMAC))) {
4657 hash->setkey = ahash_setkey;
4660 hash->setkey = ahash_hmac_setkey;
4661 hash->init = ahash_hmac_init;
4662 hash->update = ahash_hmac_update;
4663 hash->final = ahash_hmac_final;
4664 hash->finup = ahash_hmac_finup;
4665 hash->digest = ahash_hmac_digest;
4667 hash->export = ahash_export;
4668 hash->import = ahash_import;
4670 err = crypto_register_ahash(hash);
4671 /* Mark alg as having been registered, if successful */
4673 driver_alg->registered = true;
4674 pr_debug(" registered ahash %s\n",
4675 hash->halg.base.cra_driver_name);
4679 static int spu_register_aead(struct iproc_alg_s *driver_alg)
4681 struct aead_alg *aead = &driver_alg->alg.aead;
4684 aead->base.cra_module = THIS_MODULE;
4685 aead->base.cra_priority = aead_pri;
4686 aead->base.cra_alignmask = 0;
4687 aead->base.cra_ctxsize = sizeof(struct iproc_ctx_s);
4688 INIT_LIST_HEAD(&aead->base.cra_list);
4690 aead->base.cra_flags |= CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
4691 /* setkey set in alg initialization */
4692 aead->setauthsize = aead_setauthsize;
4693 aead->encrypt = aead_encrypt;
4694 aead->decrypt = aead_decrypt;
4695 aead->init = aead_cra_init;
4696 aead->exit = aead_cra_exit;
4698 err = crypto_register_aead(aead);
4699 /* Mark alg as having been registered, if successful */
4701 driver_alg->registered = true;
4702 pr_debug(" registered aead %s\n", aead->base.cra_driver_name);
4706 /* register crypto algorithms the device supports */
4707 static int spu_algs_register(struct device *dev)
4712 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4713 switch (driver_algs[i].type) {
4714 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4715 err = spu_register_ablkcipher(&driver_algs[i]);
4717 case CRYPTO_ALG_TYPE_AHASH:
4718 err = spu_register_ahash(&driver_algs[i]);
4720 case CRYPTO_ALG_TYPE_AEAD:
4721 err = spu_register_aead(&driver_algs[i]);
4725 "iproc-crypto: unknown alg type: %d",
4726 driver_algs[i].type);
4731 dev_err(dev, "alg registration failed with error %d\n",
4740 for (j = 0; j < i; j++) {
4741 /* Skip any algorithm not registered */
4742 if (!driver_algs[j].registered)
4744 switch (driver_algs[j].type) {
4745 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4746 crypto_unregister_alg(&driver_algs[j].alg.crypto);
4747 driver_algs[j].registered = false;
4749 case CRYPTO_ALG_TYPE_AHASH:
4750 crypto_unregister_ahash(&driver_algs[j].alg.hash);
4751 driver_algs[j].registered = false;
4753 case CRYPTO_ALG_TYPE_AEAD:
4754 crypto_unregister_aead(&driver_algs[j].alg.aead);
4755 driver_algs[j].registered = false;
4762 /* ==================== Kernel Platform API ==================== */
4764 static struct spu_type_subtype spum_ns2_types = {
4765 SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NS2
4768 static struct spu_type_subtype spum_nsp_types = {
4769 SPU_TYPE_SPUM, SPU_SUBTYPE_SPUM_NSP
4772 static struct spu_type_subtype spu2_types = {
4773 SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V1
4776 static struct spu_type_subtype spu2_v2_types = {
4777 SPU_TYPE_SPU2, SPU_SUBTYPE_SPU2_V2
4780 static const struct of_device_id bcm_spu_dt_ids[] = {
4782 .compatible = "brcm,spum-crypto",
4783 .data = &spum_ns2_types,
4786 .compatible = "brcm,spum-nsp-crypto",
4787 .data = &spum_nsp_types,
4790 .compatible = "brcm,spu2-crypto",
4791 .data = &spu2_types,
4794 .compatible = "brcm,spu2-v2-crypto",
4795 .data = &spu2_v2_types,
4800 MODULE_DEVICE_TABLE(of, bcm_spu_dt_ids);
4802 static int spu_dt_read(struct platform_device *pdev)
4804 struct device *dev = &pdev->dev;
4805 struct spu_hw *spu = &iproc_priv.spu;
4806 struct resource *spu_ctrl_regs;
4807 const struct of_device_id *match;
4808 const struct spu_type_subtype *matched_spu_type;
4809 struct device_node *dn = pdev->dev.of_node;
4812 /* Count number of mailbox channels */
4813 spu->num_chan = of_count_phandle_with_args(dn, "mboxes", "#mbox-cells");
4815 match = of_match_device(of_match_ptr(bcm_spu_dt_ids), dev);
4817 dev_err(&pdev->dev, "Failed to match device\n");
4821 matched_spu_type = match->data;
4823 spu->spu_type = matched_spu_type->type;
4824 spu->spu_subtype = matched_spu_type->subtype;
4827 for (i = 0; (i < MAX_SPUS) && ((spu_ctrl_regs =
4828 platform_get_resource(pdev, IORESOURCE_MEM, i)) != NULL); i++) {
4830 spu->reg_vbase[i] = devm_ioremap_resource(dev, spu_ctrl_regs);
4831 if (IS_ERR(spu->reg_vbase[i])) {
4832 err = PTR_ERR(spu->reg_vbase[i]);
4833 dev_err(&pdev->dev, "Failed to map registers: %d\n",
4835 spu->reg_vbase[i] = NULL;
4840 dev_dbg(dev, "Device has %d SPUs", spu->num_spu);
4845 int bcm_spu_probe(struct platform_device *pdev)
4847 struct device *dev = &pdev->dev;
4848 struct spu_hw *spu = &iproc_priv.spu;
4851 iproc_priv.pdev = pdev;
4852 platform_set_drvdata(iproc_priv.pdev,
4855 err = spu_dt_read(pdev);
4859 err = spu_mb_init(&pdev->dev);
4863 if (spu->spu_type == SPU_TYPE_SPUM)
4864 iproc_priv.bcm_hdr_len = 8;
4865 else if (spu->spu_type == SPU_TYPE_SPU2)
4866 iproc_priv.bcm_hdr_len = 0;
4868 spu_functions_register(&pdev->dev, spu->spu_type, spu->spu_subtype);
4870 spu_counters_init();
4872 spu_setup_debugfs();
4874 err = spu_algs_register(dev);
4883 spu_mb_release(pdev);
4884 dev_err(dev, "%s failed with error %d.\n", __func__, err);
4889 int bcm_spu_remove(struct platform_device *pdev)
4892 struct device *dev = &pdev->dev;
4895 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4897 * Not all algorithms were registered, depending on whether
4898 * hardware is SPU or SPU2. So here we make sure to skip
4899 * those algorithms that were not previously registered.
4901 if (!driver_algs[i].registered)
4904 switch (driver_algs[i].type) {
4905 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4906 crypto_unregister_alg(&driver_algs[i].alg.crypto);
4907 dev_dbg(dev, " unregistered cipher %s\n",
4908 driver_algs[i].alg.crypto.cra_driver_name);
4909 driver_algs[i].registered = false;
4911 case CRYPTO_ALG_TYPE_AHASH:
4912 crypto_unregister_ahash(&driver_algs[i].alg.hash);
4913 cdn = driver_algs[i].alg.hash.halg.base.cra_driver_name;
4914 dev_dbg(dev, " unregistered hash %s\n", cdn);
4915 driver_algs[i].registered = false;
4917 case CRYPTO_ALG_TYPE_AEAD:
4918 crypto_unregister_aead(&driver_algs[i].alg.aead);
4919 dev_dbg(dev, " unregistered aead %s\n",
4920 driver_algs[i].alg.aead.base.cra_driver_name);
4921 driver_algs[i].registered = false;
4926 spu_mb_release(pdev);
4930 /* ===== Kernel Module API ===== */
4932 static struct platform_driver bcm_spu_pdriver = {
4934 .name = "brcm-spu-crypto",
4935 .of_match_table = of_match_ptr(bcm_spu_dt_ids),
4937 .probe = bcm_spu_probe,
4938 .remove = bcm_spu_remove,
4940 module_platform_driver(bcm_spu_pdriver);
4942 MODULE_AUTHOR("Rob Rice <rob.rice@broadcom.com>");
4943 MODULE_DESCRIPTION("Broadcom symmetric crypto offload driver");
4944 MODULE_LICENSE("GPL v2");