blob: 64cab64dfab29b6cd6ed7d6bc4b0d431a7a3e539 [file] [log] [blame]
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301/*
2 * This file is part of the Chelsio T6 Crypto driver for Linux.
3 *
4 * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 *
34 * Written and Maintained by:
35 * Manoj Malviya (manojmalviya@chelsio.com)
36 * Atul Gupta (atul.gupta@chelsio.com)
37 * Jitendra Lulla (jlulla@chelsio.com)
38 * Yeshaswi M R Gowda (yeshaswi@chelsio.com)
39 * Harsh Jain (harsh@chelsio.com)
40 */
41
42#define pr_fmt(fmt) "chcr:" fmt
43
44#include <linux/kernel.h>
45#include <linux/module.h>
46#include <linux/crypto.h>
47#include <linux/cryptohash.h>
48#include <linux/skbuff.h>
49#include <linux/rtnetlink.h>
50#include <linux/highmem.h>
51#include <linux/scatterlist.h>
52
53#include <crypto/aes.h>
54#include <crypto/algapi.h>
55#include <crypto/hash.h>
56#include <crypto/sha.h>
57#include <crypto/internal/hash.h>
58
59#include "t4fw_api.h"
60#include "t4_msg.h"
61#include "chcr_core.h"
62#include "chcr_algo.h"
63#include "chcr_crypto.h"
64
65static inline struct ablk_ctx *ABLK_CTX(struct chcr_context *ctx)
66{
67 return ctx->crypto_ctx->ablkctx;
68}
69
70static inline struct hmac_ctx *HMAC_CTX(struct chcr_context *ctx)
71{
72 return ctx->crypto_ctx->hmacctx;
73}
74
75static inline struct uld_ctx *ULD_CTX(struct chcr_context *ctx)
76{
77 return ctx->dev->u_ctx;
78}
79
80static inline int is_ofld_imm(const struct sk_buff *skb)
81{
82 return (skb->len <= CRYPTO_MAX_IMM_TX_PKT_LEN);
83}
84
85/*
86 * sgl_len - calculates the size of an SGL of the given capacity
87 * @n: the number of SGL entries
88 * Calculates the number of flits needed for a scatter/gather list that
89 * can hold the given number of entries.
90 */
91static inline unsigned int sgl_len(unsigned int n)
92{
93 n--;
94 return (3 * n) / 2 + (n & 1) + 2;
95}
96
97/*
98 * chcr_handle_resp - Unmap the DMA buffers associated with the request
99 * @req: crypto request
100 */
101int chcr_handle_resp(struct crypto_async_request *req, unsigned char *input,
102 int error_status)
103{
104 struct crypto_tfm *tfm = req->tfm;
105 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
106 struct uld_ctx *u_ctx = ULD_CTX(ctx);
107 struct chcr_req_ctx ctx_req;
108 struct cpl_fw6_pld *fw6_pld;
109 unsigned int digestsize, updated_digestsize;
110
111 switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
112 case CRYPTO_ALG_TYPE_BLKCIPHER:
113 ctx_req.req.ablk_req = (struct ablkcipher_request *)req;
114 ctx_req.ctx.ablk_ctx =
115 ablkcipher_request_ctx(ctx_req.req.ablk_req);
116 if (!error_status) {
117 fw6_pld = (struct cpl_fw6_pld *)input;
118 memcpy(ctx_req.req.ablk_req->info, &fw6_pld->data[2],
119 AES_BLOCK_SIZE);
120 }
121 dma_unmap_sg(&u_ctx->lldi.pdev->dev, ctx_req.req.ablk_req->dst,
122 ABLK_CTX(ctx)->dst_nents, DMA_FROM_DEVICE);
123 if (ctx_req.ctx.ablk_ctx->skb) {
124 kfree_skb(ctx_req.ctx.ablk_ctx->skb);
125 ctx_req.ctx.ablk_ctx->skb = NULL;
126 }
127 break;
128
129 case CRYPTO_ALG_TYPE_AHASH:
130 ctx_req.req.ahash_req = (struct ahash_request *)req;
131 ctx_req.ctx.ahash_ctx =
132 ahash_request_ctx(ctx_req.req.ahash_req);
133 digestsize =
134 crypto_ahash_digestsize(crypto_ahash_reqtfm(
135 ctx_req.req.ahash_req));
136 updated_digestsize = digestsize;
137 if (digestsize == SHA224_DIGEST_SIZE)
138 updated_digestsize = SHA256_DIGEST_SIZE;
139 else if (digestsize == SHA384_DIGEST_SIZE)
140 updated_digestsize = SHA512_DIGEST_SIZE;
141 if (ctx_req.ctx.ahash_ctx->skb)
142 ctx_req.ctx.ahash_ctx->skb = NULL;
143 if (ctx_req.ctx.ahash_ctx->result == 1) {
144 ctx_req.ctx.ahash_ctx->result = 0;
145 memcpy(ctx_req.req.ahash_req->result, input +
146 sizeof(struct cpl_fw6_pld),
147 digestsize);
148 } else {
149 memcpy(ctx_req.ctx.ahash_ctx->partial_hash, input +
150 sizeof(struct cpl_fw6_pld),
151 updated_digestsize);
152 }
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530153 break;
154 }
155 return 0;
156}
157
158/*
159 * calc_tx_flits_ofld - calculate # of flits for an offload packet
160 * @skb: the packet
161 * Returns the number of flits needed for the given offload packet.
162 * These packets are already fully constructed and no additional headers
163 * will be added.
164 */
165static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
166{
167 unsigned int flits, cnt;
168
169 if (is_ofld_imm(skb))
170 return DIV_ROUND_UP(skb->len, 8);
171
172 flits = skb_transport_offset(skb) / 8; /* headers */
173 cnt = skb_shinfo(skb)->nr_frags;
174 if (skb_tail_pointer(skb) != skb_transport_header(skb))
175 cnt++;
176 return flits + sgl_len(cnt);
177}
178
Harsh Jain39f91a32016-11-29 19:00:35 +0530179static inline void get_aes_decrypt_key(unsigned char *dec_key,
180 const unsigned char *key,
181 unsigned int keylength)
182{
183 u32 temp;
184 u32 w_ring[MAX_NK];
185 int i, j, k;
186 u8 nr, nk;
187
188 switch (keylength) {
189 case AES_KEYLENGTH_128BIT:
190 nk = KEYLENGTH_4BYTES;
191 nr = NUMBER_OF_ROUNDS_10;
192 break;
193 case AES_KEYLENGTH_192BIT:
194 nk = KEYLENGTH_6BYTES;
195 nr = NUMBER_OF_ROUNDS_12;
196 break;
197 case AES_KEYLENGTH_256BIT:
198 nk = KEYLENGTH_8BYTES;
199 nr = NUMBER_OF_ROUNDS_14;
200 break;
201 default:
202 return;
203 }
204 for (i = 0; i < nk; i++)
205 w_ring[i] = be32_to_cpu(*(u32 *)&key[4 * i]);
206
207 i = 0;
208 temp = w_ring[nk - 1];
209 while (i + nk < (nr + 1) * 4) {
210 if (!(i % nk)) {
211 /* RotWord(temp) */
212 temp = (temp << 8) | (temp >> 24);
213 temp = aes_ks_subword(temp);
214 temp ^= round_constant[i / nk];
215 } else if (nk == 8 && (i % 4 == 0)) {
216 temp = aes_ks_subword(temp);
217 }
218 w_ring[i % nk] ^= temp;
219 temp = w_ring[i % nk];
220 i++;
221 }
222 i--;
223 for (k = 0, j = i % nk; k < nk; k++) {
224 *((u32 *)dec_key + k) = htonl(w_ring[j]);
225 j--;
226 if (j < 0)
227 j += nk;
228 }
229}
230
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530231static struct shash_desc *chcr_alloc_shash(unsigned int ds)
232{
233 struct crypto_shash *base_hash = NULL;
234 struct shash_desc *desc;
235
236 switch (ds) {
237 case SHA1_DIGEST_SIZE:
238 base_hash = crypto_alloc_shash("sha1-generic", 0, 0);
239 break;
240 case SHA224_DIGEST_SIZE:
241 base_hash = crypto_alloc_shash("sha224-generic", 0, 0);
242 break;
243 case SHA256_DIGEST_SIZE:
244 base_hash = crypto_alloc_shash("sha256-generic", 0, 0);
245 break;
246 case SHA384_DIGEST_SIZE:
247 base_hash = crypto_alloc_shash("sha384-generic", 0, 0);
248 break;
249 case SHA512_DIGEST_SIZE:
250 base_hash = crypto_alloc_shash("sha512-generic", 0, 0);
251 break;
252 }
253 if (IS_ERR(base_hash)) {
254 pr_err("Can not allocate sha-generic algo.\n");
255 return (void *)base_hash;
256 }
257
258 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(base_hash),
259 GFP_KERNEL);
260 if (!desc)
261 return ERR_PTR(-ENOMEM);
262 desc->tfm = base_hash;
263 desc->flags = crypto_shash_get_flags(base_hash);
264 return desc;
265}
266
267static int chcr_compute_partial_hash(struct shash_desc *desc,
268 char *iopad, char *result_hash,
269 int digest_size)
270{
271 struct sha1_state sha1_st;
272 struct sha256_state sha256_st;
273 struct sha512_state sha512_st;
274 int error;
275
276 if (digest_size == SHA1_DIGEST_SIZE) {
277 error = crypto_shash_init(desc) ?:
278 crypto_shash_update(desc, iopad, SHA1_BLOCK_SIZE) ?:
279 crypto_shash_export(desc, (void *)&sha1_st);
280 memcpy(result_hash, sha1_st.state, SHA1_DIGEST_SIZE);
281 } else if (digest_size == SHA224_DIGEST_SIZE) {
282 error = crypto_shash_init(desc) ?:
283 crypto_shash_update(desc, iopad, SHA256_BLOCK_SIZE) ?:
284 crypto_shash_export(desc, (void *)&sha256_st);
285 memcpy(result_hash, sha256_st.state, SHA256_DIGEST_SIZE);
286
287 } else if (digest_size == SHA256_DIGEST_SIZE) {
288 error = crypto_shash_init(desc) ?:
289 crypto_shash_update(desc, iopad, SHA256_BLOCK_SIZE) ?:
290 crypto_shash_export(desc, (void *)&sha256_st);
291 memcpy(result_hash, sha256_st.state, SHA256_DIGEST_SIZE);
292
293 } else if (digest_size == SHA384_DIGEST_SIZE) {
294 error = crypto_shash_init(desc) ?:
295 crypto_shash_update(desc, iopad, SHA512_BLOCK_SIZE) ?:
296 crypto_shash_export(desc, (void *)&sha512_st);
297 memcpy(result_hash, sha512_st.state, SHA512_DIGEST_SIZE);
298
299 } else if (digest_size == SHA512_DIGEST_SIZE) {
300 error = crypto_shash_init(desc) ?:
301 crypto_shash_update(desc, iopad, SHA512_BLOCK_SIZE) ?:
302 crypto_shash_export(desc, (void *)&sha512_st);
303 memcpy(result_hash, sha512_st.state, SHA512_DIGEST_SIZE);
304 } else {
305 error = -EINVAL;
306 pr_err("Unknown digest size %d\n", digest_size);
307 }
308 return error;
309}
310
311static void chcr_change_order(char *buf, int ds)
312{
313 int i;
314
315 if (ds == SHA512_DIGEST_SIZE) {
316 for (i = 0; i < (ds / sizeof(u64)); i++)
317 *((__be64 *)buf + i) =
318 cpu_to_be64(*((u64 *)buf + i));
319 } else {
320 for (i = 0; i < (ds / sizeof(u32)); i++)
321 *((__be32 *)buf + i) =
322 cpu_to_be32(*((u32 *)buf + i));
323 }
324}
325
326static inline int is_hmac(struct crypto_tfm *tfm)
327{
328 struct crypto_alg *alg = tfm->__crt_alg;
329 struct chcr_alg_template *chcr_crypto_alg =
330 container_of(__crypto_ahash_alg(alg), struct chcr_alg_template,
331 alg.hash);
332 if ((chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK) ==
333 CRYPTO_ALG_SUB_TYPE_HASH_HMAC)
334 return 1;
335 return 0;
336}
337
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530338static void write_phys_cpl(struct cpl_rx_phys_dsgl *phys_cpl,
339 struct scatterlist *sg,
340 struct phys_sge_parm *sg_param)
341{
342 struct phys_sge_pairs *to;
Harsh Jainadf1ca62016-11-29 19:00:40 +0530343 int out_buf_size = sg_param->obsize;
344 unsigned int nents = sg_param->nents, i, j = 0;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530345
346 phys_cpl->op_to_tid = htonl(CPL_RX_PHYS_DSGL_OPCODE_V(CPL_RX_PHYS_DSGL)
347 | CPL_RX_PHYS_DSGL_ISRDMA_V(0));
348 phys_cpl->pcirlxorder_to_noofsgentr =
349 htonl(CPL_RX_PHYS_DSGL_PCIRLXORDER_V(0) |
350 CPL_RX_PHYS_DSGL_PCINOSNOOP_V(0) |
351 CPL_RX_PHYS_DSGL_PCITPHNTENB_V(0) |
352 CPL_RX_PHYS_DSGL_PCITPHNT_V(0) |
353 CPL_RX_PHYS_DSGL_DCAID_V(0) |
354 CPL_RX_PHYS_DSGL_NOOFSGENTR_V(nents));
355 phys_cpl->rss_hdr_int.opcode = CPL_RX_PHYS_ADDR;
356 phys_cpl->rss_hdr_int.qid = htons(sg_param->qid);
357 phys_cpl->rss_hdr_int.hash_val = 0;
358 to = (struct phys_sge_pairs *)((unsigned char *)phys_cpl +
359 sizeof(struct cpl_rx_phys_dsgl));
360
361 for (i = 0; nents; to++) {
Harsh Jainadf1ca62016-11-29 19:00:40 +0530362 for (j = 0; j < 8 && nents; j++, nents--) {
363 out_buf_size -= sg_dma_len(sg);
364 to->len[j] = htons(sg_dma_len(sg));
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530365 to->addr[j] = cpu_to_be64(sg_dma_address(sg));
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530366 sg = sg_next(sg);
367 }
368 }
Harsh Jainadf1ca62016-11-29 19:00:40 +0530369 if (out_buf_size) {
370 j--;
371 to--;
372 to->len[j] = htons(ntohs(to->len[j]) + (out_buf_size));
373 }
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530374}
375
Harsh Jainadf1ca62016-11-29 19:00:40 +0530376static inline int map_writesg_phys_cpl(struct device *dev,
377 struct cpl_rx_phys_dsgl *phys_cpl,
378 struct scatterlist *sg,
379 struct phys_sge_parm *sg_param)
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530380{
381 if (!sg || !sg_param->nents)
382 return 0;
383
384 sg_param->nents = dma_map_sg(dev, sg, sg_param->nents, DMA_FROM_DEVICE);
385 if (sg_param->nents == 0) {
386 pr_err("CHCR : DMA mapping failed\n");
387 return -EINVAL;
388 }
389 write_phys_cpl(phys_cpl, sg, sg_param);
390 return 0;
391}
392
393static inline int get_cryptoalg_subtype(struct crypto_tfm *tfm)
394{
395 struct crypto_alg *alg = tfm->__crt_alg;
396 struct chcr_alg_template *chcr_crypto_alg =
397 container_of(alg, struct chcr_alg_template, alg.crypto);
398
399 return chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK;
400}
401
Harsh Jain358961d2016-11-29 19:00:36 +0530402static inline void write_buffer_to_skb(struct sk_buff *skb,
403 unsigned int *frags,
404 char *bfr,
405 u8 bfr_len)
406{
407 skb->len += bfr_len;
408 skb->data_len += bfr_len;
409 skb->truesize += bfr_len;
410 get_page(virt_to_page(bfr));
411 skb_fill_page_desc(skb, *frags, virt_to_page(bfr),
412 offset_in_page(bfr), bfr_len);
413 (*frags)++;
414}
415
416
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530417static inline void
Harsh Jain358961d2016-11-29 19:00:36 +0530418write_sg_to_skb(struct sk_buff *skb, unsigned int *frags,
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530419 struct scatterlist *sg, unsigned int count)
420{
421 struct page *spage;
422 unsigned int page_len;
423
424 skb->len += count;
425 skb->data_len += count;
426 skb->truesize += count;
Harsh Jain18f0aa02016-11-29 19:00:37 +0530427
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530428 while (count > 0) {
Harsh Jain18f0aa02016-11-29 19:00:37 +0530429 if (!sg || (!(sg->length)))
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530430 break;
431 spage = sg_page(sg);
432 get_page(spage);
433 page_len = min(sg->length, count);
434 skb_fill_page_desc(skb, *frags, spage, sg->offset, page_len);
435 (*frags)++;
436 count -= page_len;
437 sg = sg_next(sg);
438 }
439}
440
441static int generate_copy_rrkey(struct ablk_ctx *ablkctx,
442 struct _key_ctx *key_ctx)
443{
444 if (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) {
Harsh Jaincc1b1562016-11-29 19:00:39 +0530445 memcpy(key_ctx->key, ablkctx->rrkey, ablkctx->enckey_len);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530446 } else {
447 memcpy(key_ctx->key,
448 ablkctx->key + (ablkctx->enckey_len >> 1),
449 ablkctx->enckey_len >> 1);
Harsh Jaincc1b1562016-11-29 19:00:39 +0530450 memcpy(key_ctx->key + (ablkctx->enckey_len >> 1),
451 ablkctx->rrkey, ablkctx->enckey_len >> 1);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530452 }
453 return 0;
454}
455
456static inline void create_wreq(struct chcr_context *ctx,
Harsh Jain358961d2016-11-29 19:00:36 +0530457 struct chcr_wr *chcr_req,
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530458 void *req, struct sk_buff *skb,
459 int kctx_len, int hash_sz,
460 unsigned int phys_dsgl)
461{
462 struct uld_ctx *u_ctx = ULD_CTX(ctx);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530463 int iv_loc = IV_DSGL;
464 int qid = u_ctx->lldi.rxq_ids[ctx->tx_channel_id];
465 unsigned int immdatalen = 0, nr_frags = 0;
466
467 if (is_ofld_imm(skb)) {
468 immdatalen = skb->data_len;
469 iv_loc = IV_IMMEDIATE;
470 } else {
471 nr_frags = skb_shinfo(skb)->nr_frags;
472 }
473
Harsh Jain358961d2016-11-29 19:00:36 +0530474 chcr_req->wreq.op_to_cctx_size = FILL_WR_OP_CCTX_SIZE(immdatalen,
475 ((sizeof(chcr_req->key_ctx) + kctx_len) >> 4));
476 chcr_req->wreq.pld_size_hash_size =
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530477 htonl(FW_CRYPTO_LOOKASIDE_WR_PLD_SIZE_V(sgl_lengths[nr_frags]) |
478 FW_CRYPTO_LOOKASIDE_WR_HASH_SIZE_V(hash_sz));
Harsh Jain358961d2016-11-29 19:00:36 +0530479 chcr_req->wreq.len16_pkd =
480 htonl(FW_CRYPTO_LOOKASIDE_WR_LEN16_V(DIV_ROUND_UP(
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530481 (calc_tx_flits_ofld(skb) * 8), 16)));
Harsh Jain358961d2016-11-29 19:00:36 +0530482 chcr_req->wreq.cookie = cpu_to_be64((uintptr_t)req);
483 chcr_req->wreq.rx_chid_to_rx_q_id =
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530484 FILL_WR_RX_Q_ID(ctx->dev->tx_channel_id, qid,
485 (hash_sz) ? IV_NOP : iv_loc);
486
Harsh Jain358961d2016-11-29 19:00:36 +0530487 chcr_req->ulptx.cmd_dest = FILL_ULPTX_CMD_DEST(ctx->dev->tx_channel_id);
488 chcr_req->ulptx.len = htonl((DIV_ROUND_UP((calc_tx_flits_ofld(skb) * 8),
489 16) - ((sizeof(chcr_req->wreq)) >> 4)));
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530490
Harsh Jain358961d2016-11-29 19:00:36 +0530491 chcr_req->sc_imm.cmd_more = FILL_CMD_MORE(immdatalen);
492 chcr_req->sc_imm.len = cpu_to_be32(sizeof(struct cpl_tx_sec_pdu) +
493 sizeof(chcr_req->key_ctx) +
494 kctx_len +
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530495 ((hash_sz) ? DUMMY_BYTES :
496 (sizeof(struct cpl_rx_phys_dsgl) +
497 phys_dsgl)) + immdatalen);
498}
499
500/**
501 * create_cipher_wr - form the WR for cipher operations
502 * @req: cipher req.
503 * @ctx: crypto driver context of the request.
504 * @qid: ingress qid where response of this WR should be received.
505 * @op_type: encryption or decryption
506 */
507static struct sk_buff
Harsh Jain358961d2016-11-29 19:00:36 +0530508*create_cipher_wr(struct ablkcipher_request *req,
509 unsigned short qid,
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530510 unsigned short op_type)
511{
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530512 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
Harsh Jain358961d2016-11-29 19:00:36 +0530513 struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530514 struct uld_ctx *u_ctx = ULD_CTX(ctx);
515 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
516 struct sk_buff *skb = NULL;
Harsh Jain358961d2016-11-29 19:00:36 +0530517 struct chcr_wr *chcr_req;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530518 struct cpl_rx_phys_dsgl *phys_cpl;
519 struct chcr_blkcipher_req_ctx *req_ctx = ablkcipher_request_ctx(req);
520 struct phys_sge_parm sg_param;
Harsh Jainadf1ca62016-11-29 19:00:40 +0530521 unsigned int frags = 0, transhdr_len, phys_dsgl;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530522 unsigned int ivsize = crypto_ablkcipher_ivsize(tfm), kctx_len;
Harsh Jain358961d2016-11-29 19:00:36 +0530523 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
524 GFP_ATOMIC;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530525
526 if (!req->info)
527 return ERR_PTR(-EINVAL);
Harsh Jainadf1ca62016-11-29 19:00:40 +0530528 ablkctx->dst_nents = sg_nents_for_len(req->dst, req->nbytes);
529 if (ablkctx->dst_nents <= 0) {
530 pr_err("AES:Invalid Destination sg lists\n");
531 return ERR_PTR(-EINVAL);
532 }
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530533 ablkctx->enc = op_type;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530534 if ((ablkctx->enckey_len == 0) || (ivsize > AES_BLOCK_SIZE) ||
Harsh Jain358961d2016-11-29 19:00:36 +0530535 (req->nbytes <= 0) || (req->nbytes % AES_BLOCK_SIZE)) {
536 pr_err("AES: Invalid value of Key Len %d nbytes %d IV Len %d\n",
537 ablkctx->enckey_len, req->nbytes, ivsize);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530538 return ERR_PTR(-EINVAL);
Harsh Jain358961d2016-11-29 19:00:36 +0530539 }
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530540
541 phys_dsgl = get_space_for_phys_dsgl(ablkctx->dst_nents);
542
Harsh Jain358961d2016-11-29 19:00:36 +0530543 kctx_len = (DIV_ROUND_UP(ablkctx->enckey_len, 16) * 16);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530544 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, phys_dsgl);
Harsh Jain358961d2016-11-29 19:00:36 +0530545 skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530546 if (!skb)
547 return ERR_PTR(-ENOMEM);
548 skb_reserve(skb, sizeof(struct sge_opaque_hdr));
Harsh Jain358961d2016-11-29 19:00:36 +0530549 chcr_req = (struct chcr_wr *)__skb_put(skb, transhdr_len);
550 memset(chcr_req, 0, transhdr_len);
551 chcr_req->sec_cpl.op_ivinsrtofst =
552 FILL_SEC_CPL_OP_IVINSR(ctx->dev->tx_channel_id, 2, 1);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530553
Harsh Jain358961d2016-11-29 19:00:36 +0530554 chcr_req->sec_cpl.pldlen = htonl(ivsize + req->nbytes);
555 chcr_req->sec_cpl.aadstart_cipherstop_hi =
556 FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, ivsize + 1, 0);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530557
Harsh Jain358961d2016-11-29 19:00:36 +0530558 chcr_req->sec_cpl.cipherstop_lo_authinsert =
559 FILL_SEC_CPL_AUTHINSERT(0, 0, 0, 0);
560 chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(op_type, 0,
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530561 ablkctx->ciph_mode,
Harsh Jain358961d2016-11-29 19:00:36 +0530562 0, 0, ivsize >> 1);
563 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 0,
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530564 0, 1, phys_dsgl);
565
Harsh Jain358961d2016-11-29 19:00:36 +0530566 chcr_req->key_ctx.ctx_hdr = ablkctx->key_ctx_hdr;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530567 if (op_type == CHCR_DECRYPT_OP) {
Harsh Jain358961d2016-11-29 19:00:36 +0530568 generate_copy_rrkey(ablkctx, &chcr_req->key_ctx);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530569 } else {
570 if (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) {
Harsh Jain358961d2016-11-29 19:00:36 +0530571 memcpy(chcr_req->key_ctx.key, ablkctx->key,
572 ablkctx->enckey_len);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530573 } else {
Harsh Jain358961d2016-11-29 19:00:36 +0530574 memcpy(chcr_req->key_ctx.key, ablkctx->key +
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530575 (ablkctx->enckey_len >> 1),
576 ablkctx->enckey_len >> 1);
Harsh Jain358961d2016-11-29 19:00:36 +0530577 memcpy(chcr_req->key_ctx.key +
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530578 (ablkctx->enckey_len >> 1),
579 ablkctx->key,
580 ablkctx->enckey_len >> 1);
581 }
582 }
Harsh Jain358961d2016-11-29 19:00:36 +0530583 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530584 sg_param.nents = ablkctx->dst_nents;
Harsh Jain358961d2016-11-29 19:00:36 +0530585 sg_param.obsize = req->nbytes;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530586 sg_param.qid = qid;
587 sg_param.align = 1;
588 if (map_writesg_phys_cpl(&u_ctx->lldi.pdev->dev, phys_cpl, req->dst,
589 &sg_param))
590 goto map_fail1;
591
592 skb_set_transport_header(skb, transhdr_len);
Harsh Jain358961d2016-11-29 19:00:36 +0530593 memcpy(ablkctx->iv, req->info, ivsize);
594 write_buffer_to_skb(skb, &frags, ablkctx->iv, ivsize);
595 write_sg_to_skb(skb, &frags, req->src, req->nbytes);
596 create_wreq(ctx, chcr_req, req, skb, kctx_len, 0, phys_dsgl);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530597 req_ctx->skb = skb;
598 skb_get(skb);
599 return skb;
600map_fail1:
601 kfree_skb(skb);
602 return ERR_PTR(-ENOMEM);
603}
604
605static int chcr_aes_cbc_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
606 unsigned int keylen)
607{
608 struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
609 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530610 unsigned int ck_size, context_size;
611 u16 alignment = 0;
612
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530613 if (keylen == AES_KEYSIZE_128) {
614 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
615 } else if (keylen == AES_KEYSIZE_192) {
616 alignment = 8;
617 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
618 } else if (keylen == AES_KEYSIZE_256) {
619 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
620 } else {
621 goto badkey_err;
622 }
Harsh Jaincc1b1562016-11-29 19:00:39 +0530623 memcpy(ablkctx->key, key, keylen);
624 ablkctx->enckey_len = keylen;
625 get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, keylen << 3);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530626 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
627 keylen + alignment) >> 4;
628
629 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
630 0, 0, context_size);
631 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CBC;
632 return 0;
633badkey_err:
634 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
635 ablkctx->enckey_len = 0;
636 return -EINVAL;
637}
638
Wei Yongjun73b86bb2016-08-26 14:21:08 +0000639static int cxgb4_is_crypto_q_full(struct net_device *dev, unsigned int idx)
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530640{
641 int ret = 0;
642 struct sge_ofld_txq *q;
643 struct adapter *adap = netdev2adap(dev);
644
645 local_bh_disable();
646 q = &adap->sge.ofldtxq[idx];
647 spin_lock(&q->sendq.lock);
648 if (q->full)
649 ret = -1;
650 spin_unlock(&q->sendq.lock);
651 local_bh_enable();
652 return ret;
653}
654
655static int chcr_aes_encrypt(struct ablkcipher_request *req)
656{
657 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
658 struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530659 struct uld_ctx *u_ctx = ULD_CTX(ctx);
660 struct sk_buff *skb;
661
662 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
663 ctx->tx_channel_id))) {
664 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
665 return -EBUSY;
666 }
667
Harsh Jain358961d2016-11-29 19:00:36 +0530668 skb = create_cipher_wr(req, u_ctx->lldi.rxq_ids[ctx->tx_channel_id],
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530669 CHCR_ENCRYPT_OP);
670 if (IS_ERR(skb)) {
671 pr_err("chcr : %s : Failed to form WR. No memory\n", __func__);
672 return PTR_ERR(skb);
673 }
674 skb->dev = u_ctx->lldi.ports[0];
675 set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
676 chcr_send_wr(skb);
677 return -EINPROGRESS;
678}
679
680static int chcr_aes_decrypt(struct ablkcipher_request *req)
681{
682 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
683 struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530684 struct uld_ctx *u_ctx = ULD_CTX(ctx);
685 struct sk_buff *skb;
686
687 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
688 ctx->tx_channel_id))) {
689 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
690 return -EBUSY;
691 }
692
Harsh Jain358961d2016-11-29 19:00:36 +0530693 skb = create_cipher_wr(req, u_ctx->lldi.rxq_ids[0],
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530694 CHCR_DECRYPT_OP);
695 if (IS_ERR(skb)) {
696 pr_err("chcr : %s : Failed to form WR. No memory\n", __func__);
697 return PTR_ERR(skb);
698 }
699 skb->dev = u_ctx->lldi.ports[0];
700 set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
701 chcr_send_wr(skb);
702 return -EINPROGRESS;
703}
704
705static int chcr_device_init(struct chcr_context *ctx)
706{
707 struct uld_ctx *u_ctx;
708 unsigned int id;
709 int err = 0, rxq_perchan, rxq_idx;
710
711 id = smp_processor_id();
712 if (!ctx->dev) {
713 err = assign_chcr_device(&ctx->dev);
714 if (err) {
715 pr_err("chcr device assignment fails\n");
716 goto out;
717 }
718 u_ctx = ULD_CTX(ctx);
719 rxq_perchan = u_ctx->lldi.nrxq / u_ctx->lldi.nchan;
720 ctx->dev->tx_channel_id = 0;
721 rxq_idx = ctx->dev->tx_channel_id * rxq_perchan;
722 rxq_idx += id % rxq_perchan;
723 spin_lock(&ctx->dev->lock_chcr_dev);
724 ctx->tx_channel_id = rxq_idx;
725 spin_unlock(&ctx->dev->lock_chcr_dev);
726 }
727out:
728 return err;
729}
730
731static int chcr_cra_init(struct crypto_tfm *tfm)
732{
733 tfm->crt_ablkcipher.reqsize = sizeof(struct chcr_blkcipher_req_ctx);
734 return chcr_device_init(crypto_tfm_ctx(tfm));
735}
736
737static int get_alg_config(struct algo_param *params,
738 unsigned int auth_size)
739{
740 switch (auth_size) {
741 case SHA1_DIGEST_SIZE:
742 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_160;
743 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA1;
744 params->result_size = SHA1_DIGEST_SIZE;
745 break;
746 case SHA224_DIGEST_SIZE:
747 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
748 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA224;
749 params->result_size = SHA256_DIGEST_SIZE;
750 break;
751 case SHA256_DIGEST_SIZE:
752 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
753 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA256;
754 params->result_size = SHA256_DIGEST_SIZE;
755 break;
756 case SHA384_DIGEST_SIZE:
757 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
758 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_384;
759 params->result_size = SHA512_DIGEST_SIZE;
760 break;
761 case SHA512_DIGEST_SIZE:
762 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
763 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_512;
764 params->result_size = SHA512_DIGEST_SIZE;
765 break;
766 default:
767 pr_err("chcr : ERROR, unsupported digest size\n");
768 return -EINVAL;
769 }
770 return 0;
771}
772
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530773/**
Harsh Jain358961d2016-11-29 19:00:36 +0530774 * create_hash_wr - Create hash work request
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530775 * @req - Cipher req base
776 */
Harsh Jain358961d2016-11-29 19:00:36 +0530777static struct sk_buff *create_hash_wr(struct ahash_request *req,
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530778 struct hash_wr_param *param)
779{
780 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
781 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
782 struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
783 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
784 struct sk_buff *skb = NULL;
Harsh Jain358961d2016-11-29 19:00:36 +0530785 struct chcr_wr *chcr_req;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530786 unsigned int frags = 0, transhdr_len, iopad_alignment = 0;
787 unsigned int digestsize = crypto_ahash_digestsize(tfm);
Harsh Jain358961d2016-11-29 19:00:36 +0530788 unsigned int kctx_len = 0;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530789 u8 hash_size_in_response = 0;
Harsh Jain358961d2016-11-29 19:00:36 +0530790 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
791 GFP_ATOMIC;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530792
793 iopad_alignment = KEYCTX_ALIGN_PAD(digestsize);
Harsh Jain358961d2016-11-29 19:00:36 +0530794 kctx_len = param->alg_prm.result_size + iopad_alignment;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530795 if (param->opad_needed)
796 kctx_len += param->alg_prm.result_size + iopad_alignment;
797
798 if (req_ctx->result)
799 hash_size_in_response = digestsize;
800 else
801 hash_size_in_response = param->alg_prm.result_size;
802 transhdr_len = HASH_TRANSHDR_SIZE(kctx_len);
Harsh Jain358961d2016-11-29 19:00:36 +0530803 skb = alloc_skb((transhdr_len + sizeof(struct sge_opaque_hdr)), flags);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530804 if (!skb)
805 return skb;
806
807 skb_reserve(skb, sizeof(struct sge_opaque_hdr));
Harsh Jain358961d2016-11-29 19:00:36 +0530808 chcr_req = (struct chcr_wr *)__skb_put(skb, transhdr_len);
809 memset(chcr_req, 0, transhdr_len);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530810
Harsh Jain358961d2016-11-29 19:00:36 +0530811 chcr_req->sec_cpl.op_ivinsrtofst =
812 FILL_SEC_CPL_OP_IVINSR(ctx->dev->tx_channel_id, 2, 0);
813 chcr_req->sec_cpl.pldlen = htonl(param->bfr_len + param->sg_len);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530814
Harsh Jain358961d2016-11-29 19:00:36 +0530815 chcr_req->sec_cpl.aadstart_cipherstop_hi =
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530816 FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, 0, 0);
Harsh Jain358961d2016-11-29 19:00:36 +0530817 chcr_req->sec_cpl.cipherstop_lo_authinsert =
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530818 FILL_SEC_CPL_AUTHINSERT(0, 1, 0, 0);
Harsh Jain358961d2016-11-29 19:00:36 +0530819 chcr_req->sec_cpl.seqno_numivs =
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530820 FILL_SEC_CPL_SCMD0_SEQNO(0, 0, 0, param->alg_prm.auth_mode,
Harsh Jain358961d2016-11-29 19:00:36 +0530821 param->opad_needed, 0);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530822
Harsh Jain358961d2016-11-29 19:00:36 +0530823 chcr_req->sec_cpl.ivgen_hdrlen =
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530824 FILL_SEC_CPL_IVGEN_HDRLEN(param->last, param->more, 0, 1, 0, 0);
825
Harsh Jain358961d2016-11-29 19:00:36 +0530826 memcpy(chcr_req->key_ctx.key, req_ctx->partial_hash,
827 param->alg_prm.result_size);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530828
829 if (param->opad_needed)
Harsh Jain358961d2016-11-29 19:00:36 +0530830 memcpy(chcr_req->key_ctx.key +
831 ((param->alg_prm.result_size <= 32) ? 32 :
832 CHCR_HASH_MAX_DIGEST_SIZE),
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530833 hmacctx->opad, param->alg_prm.result_size);
834
Harsh Jain358961d2016-11-29 19:00:36 +0530835 chcr_req->key_ctx.ctx_hdr = FILL_KEY_CTX_HDR(CHCR_KEYCTX_NO_KEY,
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530836 param->alg_prm.mk_size, 0,
837 param->opad_needed,
Harsh Jain358961d2016-11-29 19:00:36 +0530838 ((kctx_len +
839 sizeof(chcr_req->key_ctx)) >> 4));
840 chcr_req->sec_cpl.scmd1 = cpu_to_be64((u64)param->scmd1);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530841
842 skb_set_transport_header(skb, transhdr_len);
843 if (param->bfr_len != 0)
Harsh Jain44fce122016-11-29 19:00:38 +0530844 write_buffer_to_skb(skb, &frags, req_ctx->reqbfr,
845 param->bfr_len);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530846 if (param->sg_len != 0)
Harsh Jain358961d2016-11-29 19:00:36 +0530847 write_sg_to_skb(skb, &frags, req->src, param->sg_len);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530848
Harsh Jain358961d2016-11-29 19:00:36 +0530849 create_wreq(ctx, chcr_req, req, skb, kctx_len, hash_size_in_response,
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530850 0);
851 req_ctx->skb = skb;
852 skb_get(skb);
853 return skb;
854}
855
856static int chcr_ahash_update(struct ahash_request *req)
857{
858 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
859 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
860 struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
861 struct uld_ctx *u_ctx = NULL;
862 struct sk_buff *skb;
863 u8 remainder = 0, bs;
864 unsigned int nbytes = req->nbytes;
865 struct hash_wr_param params;
866
867 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
868
869 u_ctx = ULD_CTX(ctx);
870 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
871 ctx->tx_channel_id))) {
872 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
873 return -EBUSY;
874 }
875
Harsh Jain44fce122016-11-29 19:00:38 +0530876 if (nbytes + req_ctx->reqlen >= bs) {
877 remainder = (nbytes + req_ctx->reqlen) % bs;
878 nbytes = nbytes + req_ctx->reqlen - remainder;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530879 } else {
Harsh Jain44fce122016-11-29 19:00:38 +0530880 sg_pcopy_to_buffer(req->src, sg_nents(req->src), req_ctx->reqbfr
881 + req_ctx->reqlen, nbytes, 0);
882 req_ctx->reqlen += nbytes;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530883 return 0;
884 }
885
886 params.opad_needed = 0;
887 params.more = 1;
888 params.last = 0;
Harsh Jain44fce122016-11-29 19:00:38 +0530889 params.sg_len = nbytes - req_ctx->reqlen;
890 params.bfr_len = req_ctx->reqlen;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530891 params.scmd1 = 0;
892 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
893 req_ctx->result = 0;
894 req_ctx->data_len += params.sg_len + params.bfr_len;
Harsh Jain358961d2016-11-29 19:00:36 +0530895 skb = create_hash_wr(req, &params);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530896
Harsh Jain44fce122016-11-29 19:00:38 +0530897 if (IS_ERR(skb))
898 return PTR_ERR(skb);
899
900 if (remainder) {
901 u8 *temp;
902 /* Swap buffers */
903 temp = req_ctx->reqbfr;
904 req_ctx->reqbfr = req_ctx->skbfr;
905 req_ctx->skbfr = temp;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530906 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
Harsh Jain44fce122016-11-29 19:00:38 +0530907 req_ctx->reqbfr, remainder, req->nbytes -
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530908 remainder);
Harsh Jain44fce122016-11-29 19:00:38 +0530909 }
910 req_ctx->reqlen = remainder;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530911 skb->dev = u_ctx->lldi.ports[0];
912 set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
913 chcr_send_wr(skb);
914
915 return -EINPROGRESS;
916}
917
918static void create_last_hash_block(char *bfr_ptr, unsigned int bs, u64 scmd1)
919{
920 memset(bfr_ptr, 0, bs);
921 *bfr_ptr = 0x80;
922 if (bs == 64)
923 *(__be64 *)(bfr_ptr + 56) = cpu_to_be64(scmd1 << 3);
924 else
925 *(__be64 *)(bfr_ptr + 120) = cpu_to_be64(scmd1 << 3);
926}
927
928static int chcr_ahash_final(struct ahash_request *req)
929{
930 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
931 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
932 struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
933 struct hash_wr_param params;
934 struct sk_buff *skb;
935 struct uld_ctx *u_ctx = NULL;
936 u8 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
937
938 u_ctx = ULD_CTX(ctx);
939 if (is_hmac(crypto_ahash_tfm(rtfm)))
940 params.opad_needed = 1;
941 else
942 params.opad_needed = 0;
943 params.sg_len = 0;
944 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
945 req_ctx->result = 1;
Harsh Jain44fce122016-11-29 19:00:38 +0530946 params.bfr_len = req_ctx->reqlen;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530947 req_ctx->data_len += params.bfr_len + params.sg_len;
Harsh Jain44fce122016-11-29 19:00:38 +0530948 if (req_ctx->reqlen == 0) {
949 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530950 params.last = 0;
951 params.more = 1;
952 params.scmd1 = 0;
953 params.bfr_len = bs;
954
955 } else {
956 params.scmd1 = req_ctx->data_len;
957 params.last = 1;
958 params.more = 0;
959 }
Harsh Jain358961d2016-11-29 19:00:36 +0530960 skb = create_hash_wr(req, &params);
961 if (IS_ERR(skb))
962 return PTR_ERR(skb);
963
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530964 skb->dev = u_ctx->lldi.ports[0];
965 set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
966 chcr_send_wr(skb);
967 return -EINPROGRESS;
968}
969
970static int chcr_ahash_finup(struct ahash_request *req)
971{
972 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
973 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
974 struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
975 struct uld_ctx *u_ctx = NULL;
976 struct sk_buff *skb;
977 struct hash_wr_param params;
978 u8 bs;
979
980 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
981 u_ctx = ULD_CTX(ctx);
982
983 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
984 ctx->tx_channel_id))) {
985 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
986 return -EBUSY;
987 }
988
989 if (is_hmac(crypto_ahash_tfm(rtfm)))
990 params.opad_needed = 1;
991 else
992 params.opad_needed = 0;
993
994 params.sg_len = req->nbytes;
Harsh Jain44fce122016-11-29 19:00:38 +0530995 params.bfr_len = req_ctx->reqlen;
Hariprasad Shenai324429d2016-08-17 12:33:05 +0530996 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
997 req_ctx->data_len += params.bfr_len + params.sg_len;
998 req_ctx->result = 1;
Harsh Jain44fce122016-11-29 19:00:38 +0530999 if ((req_ctx->reqlen + req->nbytes) == 0) {
1000 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301001 params.last = 0;
1002 params.more = 1;
1003 params.scmd1 = 0;
1004 params.bfr_len = bs;
1005 } else {
1006 params.scmd1 = req_ctx->data_len;
1007 params.last = 1;
1008 params.more = 0;
1009 }
1010
Harsh Jain358961d2016-11-29 19:00:36 +05301011 skb = create_hash_wr(req, &params);
1012 if (IS_ERR(skb))
1013 return PTR_ERR(skb);
1014
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301015 skb->dev = u_ctx->lldi.ports[0];
1016 set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
1017 chcr_send_wr(skb);
1018
1019 return -EINPROGRESS;
1020}
1021
1022static int chcr_ahash_digest(struct ahash_request *req)
1023{
1024 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1025 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1026 struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
1027 struct uld_ctx *u_ctx = NULL;
1028 struct sk_buff *skb;
1029 struct hash_wr_param params;
1030 u8 bs;
1031
1032 rtfm->init(req);
1033 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1034
1035 u_ctx = ULD_CTX(ctx);
1036 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1037 ctx->tx_channel_id))) {
1038 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
1039 return -EBUSY;
1040 }
1041
1042 if (is_hmac(crypto_ahash_tfm(rtfm)))
1043 params.opad_needed = 1;
1044 else
1045 params.opad_needed = 0;
1046
1047 params.last = 0;
1048 params.more = 0;
1049 params.sg_len = req->nbytes;
1050 params.bfr_len = 0;
1051 params.scmd1 = 0;
1052 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1053 req_ctx->result = 1;
1054 req_ctx->data_len += params.bfr_len + params.sg_len;
1055
Harsh Jain44fce122016-11-29 19:00:38 +05301056 if (req->nbytes == 0) {
1057 create_last_hash_block(req_ctx->reqbfr, bs, 0);
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301058 params.more = 1;
1059 params.bfr_len = bs;
1060 }
1061
Harsh Jain358961d2016-11-29 19:00:36 +05301062 skb = create_hash_wr(req, &params);
1063 if (IS_ERR(skb))
1064 return PTR_ERR(skb);
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301065
1066 skb->dev = u_ctx->lldi.ports[0];
1067 set_wr_txq(skb, CPL_PRIORITY_DATA, ctx->tx_channel_id);
1068 chcr_send_wr(skb);
1069 return -EINPROGRESS;
1070}
1071
1072static int chcr_ahash_export(struct ahash_request *areq, void *out)
1073{
1074 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1075 struct chcr_ahash_req_ctx *state = out;
1076
Harsh Jain44fce122016-11-29 19:00:38 +05301077 state->reqlen = req_ctx->reqlen;
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301078 state->data_len = req_ctx->data_len;
Harsh Jain44fce122016-11-29 19:00:38 +05301079 memcpy(state->bfr1, req_ctx->reqbfr, req_ctx->reqlen);
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301080 memcpy(state->partial_hash, req_ctx->partial_hash,
1081 CHCR_HASH_MAX_DIGEST_SIZE);
Harsh Jain44fce122016-11-29 19:00:38 +05301082 return 0;
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301083}
1084
1085static int chcr_ahash_import(struct ahash_request *areq, const void *in)
1086{
1087 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1088 struct chcr_ahash_req_ctx *state = (struct chcr_ahash_req_ctx *)in;
1089
Harsh Jain44fce122016-11-29 19:00:38 +05301090 req_ctx->reqlen = state->reqlen;
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301091 req_ctx->data_len = state->data_len;
Harsh Jain44fce122016-11-29 19:00:38 +05301092 req_ctx->reqbfr = req_ctx->bfr1;
1093 req_ctx->skbfr = req_ctx->bfr2;
1094 memcpy(req_ctx->bfr1, state->bfr1, CHCR_HASH_MAX_BLOCK_SIZE_128);
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301095 memcpy(req_ctx->partial_hash, state->partial_hash,
1096 CHCR_HASH_MAX_DIGEST_SIZE);
1097 return 0;
1098}
1099
1100static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
1101 unsigned int keylen)
1102{
1103 struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1104 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
1105 unsigned int digestsize = crypto_ahash_digestsize(tfm);
1106 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1107 unsigned int i, err = 0, updated_digestsize;
1108
1109 /*
1110 * use the key to calculate the ipad and opad. ipad will sent with the
1111 * first request's data. opad will be sent with the final hash result
1112 * ipad in hmacctx->ipad and opad in hmacctx->opad location
1113 */
1114 if (!hmacctx->desc)
1115 return -EINVAL;
1116 if (keylen > bs) {
1117 err = crypto_shash_digest(hmacctx->desc, key, keylen,
1118 hmacctx->ipad);
1119 if (err)
1120 goto out;
1121 keylen = digestsize;
1122 } else {
1123 memcpy(hmacctx->ipad, key, keylen);
1124 }
1125 memset(hmacctx->ipad + keylen, 0, bs - keylen);
1126 memcpy(hmacctx->opad, hmacctx->ipad, bs);
1127
1128 for (i = 0; i < bs / sizeof(int); i++) {
1129 *((unsigned int *)(&hmacctx->ipad) + i) ^= IPAD_DATA;
1130 *((unsigned int *)(&hmacctx->opad) + i) ^= OPAD_DATA;
1131 }
1132
1133 updated_digestsize = digestsize;
1134 if (digestsize == SHA224_DIGEST_SIZE)
1135 updated_digestsize = SHA256_DIGEST_SIZE;
1136 else if (digestsize == SHA384_DIGEST_SIZE)
1137 updated_digestsize = SHA512_DIGEST_SIZE;
1138 err = chcr_compute_partial_hash(hmacctx->desc, hmacctx->ipad,
1139 hmacctx->ipad, digestsize);
1140 if (err)
1141 goto out;
1142 chcr_change_order(hmacctx->ipad, updated_digestsize);
1143
1144 err = chcr_compute_partial_hash(hmacctx->desc, hmacctx->opad,
1145 hmacctx->opad, digestsize);
1146 if (err)
1147 goto out;
1148 chcr_change_order(hmacctx->opad, updated_digestsize);
1149out:
1150 return err;
1151}
1152
1153static int chcr_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
1154 unsigned int key_len)
1155{
1156 struct chcr_context *ctx = crypto_ablkcipher_ctx(tfm);
1157 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301158 unsigned short context_size = 0;
1159
Harsh Jaincc1b1562016-11-29 19:00:39 +05301160 if ((key_len != (AES_KEYSIZE_128 << 1)) &&
1161 (key_len != (AES_KEYSIZE_256 << 1))) {
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301162 crypto_tfm_set_flags((struct crypto_tfm *)tfm,
1163 CRYPTO_TFM_RES_BAD_KEY_LEN);
1164 ablkctx->enckey_len = 0;
Harsh Jaincc1b1562016-11-29 19:00:39 +05301165 return -EINVAL;
1166
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301167 }
Harsh Jaincc1b1562016-11-29 19:00:39 +05301168
1169 memcpy(ablkctx->key, key, key_len);
1170 ablkctx->enckey_len = key_len;
1171 get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, key_len << 2);
1172 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len) >> 4;
1173 ablkctx->key_ctx_hdr =
1174 FILL_KEY_CTX_HDR((key_len == AES_KEYSIZE_256) ?
1175 CHCR_KEYCTX_CIPHER_KEY_SIZE_128 :
1176 CHCR_KEYCTX_CIPHER_KEY_SIZE_256,
1177 CHCR_KEYCTX_NO_KEY, 1,
1178 0, context_size);
1179 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_XTS;
1180 return 0;
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301181}
1182
1183static int chcr_sha_init(struct ahash_request *areq)
1184{
1185 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1186 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1187 int digestsize = crypto_ahash_digestsize(tfm);
1188
1189 req_ctx->data_len = 0;
Harsh Jain44fce122016-11-29 19:00:38 +05301190 req_ctx->reqlen = 0;
1191 req_ctx->reqbfr = req_ctx->bfr1;
1192 req_ctx->skbfr = req_ctx->bfr2;
Hariprasad Shenai324429d2016-08-17 12:33:05 +05301193 req_ctx->skb = NULL;
1194 req_ctx->result = 0;
1195 copy_hash_init_values(req_ctx->partial_hash, digestsize);
1196 return 0;
1197}
1198
1199static int chcr_sha_cra_init(struct crypto_tfm *tfm)
1200{
1201 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1202 sizeof(struct chcr_ahash_req_ctx));
1203 return chcr_device_init(crypto_tfm_ctx(tfm));
1204}
1205
1206static int chcr_hmac_init(struct ahash_request *areq)
1207{
1208 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1209 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(areq);
1210 struct chcr_context *ctx = crypto_tfm_ctx(crypto_ahash_tfm(rtfm));
1211 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
1212 unsigned int digestsize = crypto_ahash_digestsize(rtfm);
1213 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1214
1215 chcr_sha_init(areq);
1216 req_ctx->data_len = bs;
1217 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1218 if (digestsize == SHA224_DIGEST_SIZE)
1219 memcpy(req_ctx->partial_hash, hmacctx->ipad,
1220 SHA256_DIGEST_SIZE);
1221 else if (digestsize == SHA384_DIGEST_SIZE)
1222 memcpy(req_ctx->partial_hash, hmacctx->ipad,
1223 SHA512_DIGEST_SIZE);
1224 else
1225 memcpy(req_ctx->partial_hash, hmacctx->ipad,
1226 digestsize);
1227 }
1228 return 0;
1229}
1230
1231static int chcr_hmac_cra_init(struct crypto_tfm *tfm)
1232{
1233 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
1234 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
1235 unsigned int digestsize =
1236 crypto_ahash_digestsize(__crypto_ahash_cast(tfm));
1237
1238 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1239 sizeof(struct chcr_ahash_req_ctx));
1240 hmacctx->desc = chcr_alloc_shash(digestsize);
1241 if (IS_ERR(hmacctx->desc))
1242 return PTR_ERR(hmacctx->desc);
1243 return chcr_device_init(crypto_tfm_ctx(tfm));
1244}
1245
1246static void chcr_free_shash(struct shash_desc *desc)
1247{
1248 crypto_free_shash(desc->tfm);
1249 kfree(desc);
1250}
1251
1252static void chcr_hmac_cra_exit(struct crypto_tfm *tfm)
1253{
1254 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
1255 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
1256
1257 if (hmacctx->desc) {
1258 chcr_free_shash(hmacctx->desc);
1259 hmacctx->desc = NULL;
1260 }
1261}
1262
1263static struct chcr_alg_template driver_algs[] = {
1264 /* AES-CBC */
1265 {
1266 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1267 .is_registered = 0,
1268 .alg.crypto = {
1269 .cra_name = "cbc(aes)",
1270 .cra_driver_name = "cbc(aes-chcr)",
1271 .cra_priority = CHCR_CRA_PRIORITY,
1272 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
1273 CRYPTO_ALG_ASYNC,
1274 .cra_blocksize = AES_BLOCK_SIZE,
1275 .cra_ctxsize = sizeof(struct chcr_context)
1276 + sizeof(struct ablk_ctx),
1277 .cra_alignmask = 0,
1278 .cra_type = &crypto_ablkcipher_type,
1279 .cra_module = THIS_MODULE,
1280 .cra_init = chcr_cra_init,
1281 .cra_exit = NULL,
1282 .cra_u.ablkcipher = {
1283 .min_keysize = AES_MIN_KEY_SIZE,
1284 .max_keysize = AES_MAX_KEY_SIZE,
1285 .ivsize = AES_BLOCK_SIZE,
1286 .setkey = chcr_aes_cbc_setkey,
1287 .encrypt = chcr_aes_encrypt,
1288 .decrypt = chcr_aes_decrypt,
1289 }
1290 }
1291 },
1292 {
1293 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1294 .is_registered = 0,
1295 .alg.crypto = {
1296 .cra_name = "xts(aes)",
1297 .cra_driver_name = "xts(aes-chcr)",
1298 .cra_priority = CHCR_CRA_PRIORITY,
1299 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
1300 CRYPTO_ALG_ASYNC,
1301 .cra_blocksize = AES_BLOCK_SIZE,
1302 .cra_ctxsize = sizeof(struct chcr_context) +
1303 sizeof(struct ablk_ctx),
1304 .cra_alignmask = 0,
1305 .cra_type = &crypto_ablkcipher_type,
1306 .cra_module = THIS_MODULE,
1307 .cra_init = chcr_cra_init,
1308 .cra_exit = NULL,
1309 .cra_u = {
1310 .ablkcipher = {
1311 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1312 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1313 .ivsize = AES_BLOCK_SIZE,
1314 .setkey = chcr_aes_xts_setkey,
1315 .encrypt = chcr_aes_encrypt,
1316 .decrypt = chcr_aes_decrypt,
1317 }
1318 }
1319 }
1320 },
1321 /* SHA */
1322 {
1323 .type = CRYPTO_ALG_TYPE_AHASH,
1324 .is_registered = 0,
1325 .alg.hash = {
1326 .halg.digestsize = SHA1_DIGEST_SIZE,
1327 .halg.base = {
1328 .cra_name = "sha1",
1329 .cra_driver_name = "sha1-chcr",
1330 .cra_blocksize = SHA1_BLOCK_SIZE,
1331 }
1332 }
1333 },
1334 {
1335 .type = CRYPTO_ALG_TYPE_AHASH,
1336 .is_registered = 0,
1337 .alg.hash = {
1338 .halg.digestsize = SHA256_DIGEST_SIZE,
1339 .halg.base = {
1340 .cra_name = "sha256",
1341 .cra_driver_name = "sha256-chcr",
1342 .cra_blocksize = SHA256_BLOCK_SIZE,
1343 }
1344 }
1345 },
1346 {
1347 .type = CRYPTO_ALG_TYPE_AHASH,
1348 .is_registered = 0,
1349 .alg.hash = {
1350 .halg.digestsize = SHA224_DIGEST_SIZE,
1351 .halg.base = {
1352 .cra_name = "sha224",
1353 .cra_driver_name = "sha224-chcr",
1354 .cra_blocksize = SHA224_BLOCK_SIZE,
1355 }
1356 }
1357 },
1358 {
1359 .type = CRYPTO_ALG_TYPE_AHASH,
1360 .is_registered = 0,
1361 .alg.hash = {
1362 .halg.digestsize = SHA384_DIGEST_SIZE,
1363 .halg.base = {
1364 .cra_name = "sha384",
1365 .cra_driver_name = "sha384-chcr",
1366 .cra_blocksize = SHA384_BLOCK_SIZE,
1367 }
1368 }
1369 },
1370 {
1371 .type = CRYPTO_ALG_TYPE_AHASH,
1372 .is_registered = 0,
1373 .alg.hash = {
1374 .halg.digestsize = SHA512_DIGEST_SIZE,
1375 .halg.base = {
1376 .cra_name = "sha512",
1377 .cra_driver_name = "sha512-chcr",
1378 .cra_blocksize = SHA512_BLOCK_SIZE,
1379 }
1380 }
1381 },
1382 /* HMAC */
1383 {
1384 .type = CRYPTO_ALG_TYPE_HMAC,
1385 .is_registered = 0,
1386 .alg.hash = {
1387 .halg.digestsize = SHA1_DIGEST_SIZE,
1388 .halg.base = {
1389 .cra_name = "hmac(sha1)",
1390 .cra_driver_name = "hmac(sha1-chcr)",
1391 .cra_blocksize = SHA1_BLOCK_SIZE,
1392 }
1393 }
1394 },
1395 {
1396 .type = CRYPTO_ALG_TYPE_HMAC,
1397 .is_registered = 0,
1398 .alg.hash = {
1399 .halg.digestsize = SHA224_DIGEST_SIZE,
1400 .halg.base = {
1401 .cra_name = "hmac(sha224)",
1402 .cra_driver_name = "hmac(sha224-chcr)",
1403 .cra_blocksize = SHA224_BLOCK_SIZE,
1404 }
1405 }
1406 },
1407 {
1408 .type = CRYPTO_ALG_TYPE_HMAC,
1409 .is_registered = 0,
1410 .alg.hash = {
1411 .halg.digestsize = SHA256_DIGEST_SIZE,
1412 .halg.base = {
1413 .cra_name = "hmac(sha256)",
1414 .cra_driver_name = "hmac(sha256-chcr)",
1415 .cra_blocksize = SHA256_BLOCK_SIZE,
1416 }
1417 }
1418 },
1419 {
1420 .type = CRYPTO_ALG_TYPE_HMAC,
1421 .is_registered = 0,
1422 .alg.hash = {
1423 .halg.digestsize = SHA384_DIGEST_SIZE,
1424 .halg.base = {
1425 .cra_name = "hmac(sha384)",
1426 .cra_driver_name = "hmac(sha384-chcr)",
1427 .cra_blocksize = SHA384_BLOCK_SIZE,
1428 }
1429 }
1430 },
1431 {
1432 .type = CRYPTO_ALG_TYPE_HMAC,
1433 .is_registered = 0,
1434 .alg.hash = {
1435 .halg.digestsize = SHA512_DIGEST_SIZE,
1436 .halg.base = {
1437 .cra_name = "hmac(sha512)",
1438 .cra_driver_name = "hmac(sha512-chcr)",
1439 .cra_blocksize = SHA512_BLOCK_SIZE,
1440 }
1441 }
1442 },
1443};
1444
1445/*
1446 * chcr_unregister_alg - Deregister crypto algorithms with
1447 * kernel framework.
1448 */
1449static int chcr_unregister_alg(void)
1450{
1451 int i;
1452
1453 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
1454 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
1455 case CRYPTO_ALG_TYPE_ABLKCIPHER:
1456 if (driver_algs[i].is_registered)
1457 crypto_unregister_alg(
1458 &driver_algs[i].alg.crypto);
1459 break;
1460 case CRYPTO_ALG_TYPE_AHASH:
1461 if (driver_algs[i].is_registered)
1462 crypto_unregister_ahash(
1463 &driver_algs[i].alg.hash);
1464 break;
1465 }
1466 driver_algs[i].is_registered = 0;
1467 }
1468 return 0;
1469}
1470
1471#define SZ_AHASH_CTX sizeof(struct chcr_context)
1472#define SZ_AHASH_H_CTX (sizeof(struct chcr_context) + sizeof(struct hmac_ctx))
1473#define SZ_AHASH_REQ_CTX sizeof(struct chcr_ahash_req_ctx)
1474#define AHASH_CRA_FLAGS (CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC)
1475
1476/*
1477 * chcr_register_alg - Register crypto algorithms with kernel framework.
1478 */
1479static int chcr_register_alg(void)
1480{
1481 struct crypto_alg ai;
1482 struct ahash_alg *a_hash;
1483 int err = 0, i;
1484 char *name = NULL;
1485
1486 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
1487 if (driver_algs[i].is_registered)
1488 continue;
1489 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
1490 case CRYPTO_ALG_TYPE_ABLKCIPHER:
1491 err = crypto_register_alg(&driver_algs[i].alg.crypto);
1492 name = driver_algs[i].alg.crypto.cra_driver_name;
1493 break;
1494 case CRYPTO_ALG_TYPE_AHASH:
1495 a_hash = &driver_algs[i].alg.hash;
1496 a_hash->update = chcr_ahash_update;
1497 a_hash->final = chcr_ahash_final;
1498 a_hash->finup = chcr_ahash_finup;
1499 a_hash->digest = chcr_ahash_digest;
1500 a_hash->export = chcr_ahash_export;
1501 a_hash->import = chcr_ahash_import;
1502 a_hash->halg.statesize = SZ_AHASH_REQ_CTX;
1503 a_hash->halg.base.cra_priority = CHCR_CRA_PRIORITY;
1504 a_hash->halg.base.cra_module = THIS_MODULE;
1505 a_hash->halg.base.cra_flags = AHASH_CRA_FLAGS;
1506 a_hash->halg.base.cra_alignmask = 0;
1507 a_hash->halg.base.cra_exit = NULL;
1508 a_hash->halg.base.cra_type = &crypto_ahash_type;
1509
1510 if (driver_algs[i].type == CRYPTO_ALG_TYPE_HMAC) {
1511 a_hash->halg.base.cra_init = chcr_hmac_cra_init;
1512 a_hash->halg.base.cra_exit = chcr_hmac_cra_exit;
1513 a_hash->init = chcr_hmac_init;
1514 a_hash->setkey = chcr_ahash_setkey;
1515 a_hash->halg.base.cra_ctxsize = SZ_AHASH_H_CTX;
1516 } else {
1517 a_hash->init = chcr_sha_init;
1518 a_hash->halg.base.cra_ctxsize = SZ_AHASH_CTX;
1519 a_hash->halg.base.cra_init = chcr_sha_cra_init;
1520 }
1521 err = crypto_register_ahash(&driver_algs[i].alg.hash);
1522 ai = driver_algs[i].alg.hash.halg.base;
1523 name = ai.cra_driver_name;
1524 break;
1525 }
1526 if (err) {
1527 pr_err("chcr : %s : Algorithm registration failed\n",
1528 name);
1529 goto register_err;
1530 } else {
1531 driver_algs[i].is_registered = 1;
1532 }
1533 }
1534 return 0;
1535
1536register_err:
1537 chcr_unregister_alg();
1538 return err;
1539}
1540
1541/*
1542 * start_crypto - Register the crypto algorithms.
1543 * This should called once when the first device comesup. After this
1544 * kernel will start calling driver APIs for crypto operations.
1545 */
1546int start_crypto(void)
1547{
1548 return chcr_register_alg();
1549}
1550
1551/*
1552 * stop_crypto - Deregister all the crypto algorithms with kernel.
1553 * This should be called once when the last device goes down. After this
1554 * kernel will not call the driver API for crypto operations.
1555 */
1556int stop_crypto(void)
1557{
1558 chcr_unregister_alg();
1559 return 0;
1560}