safexcel_cipher.c 40.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 2017 Marvell
 *
 * Antoine Tenart <antoine.tenart@free-electrons.com>
 */

#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/dmapool.h>

12
#include <crypto/aead.h>
13
#include <crypto/aes.h>
14
#include <crypto/authenc.h>
15
#include <crypto/des.h>
16
#include <crypto/sha.h>
17
#include <crypto/skcipher.h>
18
#include <crypto/internal/aead.h>
19
#include <crypto/internal/skcipher.h>
20 21 22 23 24 25 26 27

#include "safexcel.h"

enum safexcel_cipher_direction {
	SAFEXCEL_ENCRYPT,
	SAFEXCEL_DECRYPT,
};

28 29
enum safexcel_cipher_alg {
	SAFEXCEL_DES,
30
	SAFEXCEL_3DES,
31 32 33
	SAFEXCEL_AES,
};

34 35 36 37 38
struct safexcel_cipher_ctx {
	struct safexcel_context base;
	struct safexcel_crypto_priv *priv;

	u32 mode;
39
	enum safexcel_cipher_alg alg;
40
	bool aead;
41 42 43

	__le32 key[8];
	unsigned int key_len;
44 45

	/* All the below is AEAD specific */
46
	u32 hash_alg;
47
	u32 state_sz;
48 49
	u32 ipad[SHA512_DIGEST_SIZE / sizeof(u32)];
	u32 opad[SHA512_DIGEST_SIZE / sizeof(u32)];
50 51
};

52
struct safexcel_cipher_req {
53
	enum safexcel_cipher_direction direction;
54 55
	/* Number of result descriptors associated to the request */
	unsigned int rdescs;
56
	bool needs_inv;
57
	int  nr_src, nr_dst;
58 59
};

60 61 62
static void safexcel_skcipher_token(struct safexcel_cipher_ctx *ctx, u8 *iv,
				    struct safexcel_command_desc *cdesc,
				    u32 length)
63 64
{
	struct safexcel_token *token;
65
	u32 offset = 0, block_sz = 0;
66 67

	if (ctx->mode == CONTEXT_CONTROL_CRYPTO_MODE_CBC) {
68 69
		switch (ctx->alg) {
		case SAFEXCEL_DES:
70
			block_sz = DES_BLOCK_SIZE;
71 72
			cdesc->control_data.options |= EIP197_OPTION_2_TOKEN_IV_CMD;
			break;
73
		case SAFEXCEL_3DES:
74
			block_sz = DES3_EDE_BLOCK_SIZE;
75 76
			cdesc->control_data.options |= EIP197_OPTION_2_TOKEN_IV_CMD;
			break;
77
		case SAFEXCEL_AES:
78
			block_sz = AES_BLOCK_SIZE;
79 80 81
			cdesc->control_data.options |= EIP197_OPTION_4_TOKEN_IV_CMD;
			break;
		}
82 83 84

		offset = block_sz / sizeof(u32);
		memcpy(cdesc->control_data.token, iv, block_sz);
85 86 87 88 89 90
	}

	token = (struct safexcel_token *)(cdesc->control_data.token + offset);

	token[0].opcode = EIP197_TOKEN_OPCODE_DIRECTION;
	token[0].packet_length = length;
91 92
	token[0].stat = EIP197_TOKEN_STAT_LAST_PACKET |
			EIP197_TOKEN_STAT_LAST_HASH;
93
	token[0].instructions = EIP197_TOKEN_INS_LAST |
94
				EIP197_TOKEN_INS_TYPE_CRYPTO |
95 96 97
				EIP197_TOKEN_INS_TYPE_OUTPUT;
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
static void safexcel_aead_token(struct safexcel_cipher_ctx *ctx, u8 *iv,
				struct safexcel_command_desc *cdesc,
				enum safexcel_cipher_direction direction,
				u32 cryptlen, u32 assoclen, u32 digestsize)
{
	struct safexcel_token *token;
	unsigned offset = 0;

	if (ctx->mode == CONTEXT_CONTROL_CRYPTO_MODE_CBC) {
		offset = AES_BLOCK_SIZE / sizeof(u32);
		memcpy(cdesc->control_data.token, iv, AES_BLOCK_SIZE);

		cdesc->control_data.options |= EIP197_OPTION_4_TOKEN_IV_CMD;
	}

	token = (struct safexcel_token *)(cdesc->control_data.token + offset);

	if (direction == SAFEXCEL_DECRYPT)
		cryptlen -= digestsize;

	token[0].opcode = EIP197_TOKEN_OPCODE_DIRECTION;
	token[0].packet_length = assoclen;
120
	token[0].instructions = EIP197_TOKEN_INS_TYPE_HASH;
121 122 123 124 125

	token[1].opcode = EIP197_TOKEN_OPCODE_DIRECTION;
	token[1].packet_length = cryptlen;
	token[1].stat = EIP197_TOKEN_STAT_LAST_HASH;
	token[1].instructions = EIP197_TOKEN_INS_LAST |
126
				EIP197_TOKEN_INS_TYPE_CRYPTO |
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
				EIP197_TOKEN_INS_TYPE_HASH |
				EIP197_TOKEN_INS_TYPE_OUTPUT;

	if (direction == SAFEXCEL_ENCRYPT) {
		token[2].opcode = EIP197_TOKEN_OPCODE_INSERT;
		token[2].packet_length = digestsize;
		token[2].stat = EIP197_TOKEN_STAT_LAST_HASH |
				EIP197_TOKEN_STAT_LAST_PACKET;
		token[2].instructions = EIP197_TOKEN_INS_TYPE_OUTPUT |
					EIP197_TOKEN_INS_INSERT_HASH_DIGEST;
	} else {
		token[2].opcode = EIP197_TOKEN_OPCODE_RETRIEVE;
		token[2].packet_length = digestsize;
		token[2].stat = EIP197_TOKEN_STAT_LAST_HASH |
				EIP197_TOKEN_STAT_LAST_PACKET;
		token[2].instructions = EIP197_TOKEN_INS_INSERT_HASH_DIGEST;

		token[3].opcode = EIP197_TOKEN_OPCODE_VERIFY;
		token[3].packet_length = digestsize |
					 EIP197_TOKEN_HASH_RESULT_VERIFY;
		token[3].stat = EIP197_TOKEN_STAT_LAST_HASH |
				EIP197_TOKEN_STAT_LAST_PACKET;
		token[3].instructions = EIP197_TOKEN_INS_TYPE_OUTPUT;
	}
}

153 154
static int safexcel_skcipher_aes_setkey(struct crypto_skcipher *ctfm,
					const u8 *key, unsigned int len)
155 156 157
{
	struct crypto_tfm *tfm = crypto_skcipher_tfm(ctfm);
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
158
	struct safexcel_crypto_priv *priv = ctx->priv;
159 160 161
	struct crypto_aes_ctx aes;
	int ret, i;

162
	ret = aes_expandkey(&aes, key, len);
163 164 165 166 167
	if (ret) {
		crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
		return ret;
	}

168
	if (priv->flags & EIP197_TRC_CACHE && ctx->base.ctxr_dma) {
169 170 171 172 173
		for (i = 0; i < len / sizeof(u32); i++) {
			if (ctx->key[i] != cpu_to_le32(aes.key_enc[i])) {
				ctx->base.needs_inv = true;
				break;
			}
174 175 176 177 178 179 180 181 182 183 184 185
		}
	}

	for (i = 0; i < len / sizeof(u32); i++)
		ctx->key[i] = cpu_to_le32(aes.key_enc[i]);

	ctx->key_len = len;

	memzero_explicit(&aes, sizeof(aes));
	return 0;
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
static int safexcel_aead_aes_setkey(struct crypto_aead *ctfm, const u8 *key,
				    unsigned int len)
{
	struct crypto_tfm *tfm = crypto_aead_tfm(ctfm);
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
	struct safexcel_ahash_export_state istate, ostate;
	struct safexcel_crypto_priv *priv = ctx->priv;
	struct crypto_authenc_keys keys;

	if (crypto_authenc_extractkeys(&keys, key, len) != 0)
		goto badkey;

	if (keys.enckeylen > sizeof(ctx->key))
		goto badkey;

	/* Encryption key */
202
	if (priv->flags & EIP197_TRC_CACHE && ctx->base.ctxr_dma &&
203 204 205 206
	    memcmp(ctx->key, keys.enckey, keys.enckeylen))
		ctx->base.needs_inv = true;

	/* Auth key */
207
	switch (ctx->hash_alg) {
208 209 210 211 212
	case CONTEXT_CONTROL_CRYPTO_ALG_SHA1:
		if (safexcel_hmac_setkey("safexcel-sha1", keys.authkey,
					 keys.authkeylen, &istate, &ostate))
			goto badkey;
		break;
213 214 215 216 217 218 219 220 221 222
	case CONTEXT_CONTROL_CRYPTO_ALG_SHA224:
		if (safexcel_hmac_setkey("safexcel-sha224", keys.authkey,
					 keys.authkeylen, &istate, &ostate))
			goto badkey;
		break;
	case CONTEXT_CONTROL_CRYPTO_ALG_SHA256:
		if (safexcel_hmac_setkey("safexcel-sha256", keys.authkey,
					 keys.authkeylen, &istate, &ostate))
			goto badkey;
		break;
223 224 225 226 227
	case CONTEXT_CONTROL_CRYPTO_ALG_SHA384:
		if (safexcel_hmac_setkey("safexcel-sha384", keys.authkey,
					 keys.authkeylen, &istate, &ostate))
			goto badkey;
		break;
228 229 230 231 232
	case CONTEXT_CONTROL_CRYPTO_ALG_SHA512:
		if (safexcel_hmac_setkey("safexcel-sha512", keys.authkey,
					 keys.authkeylen, &istate, &ostate))
			goto badkey;
		break;
233 234
	default:
		dev_err(priv->dev, "aead: unsupported hash algorithm\n");
235
		goto badkey;
236
	}
237 238 239 240

	crypto_aead_set_flags(ctfm, crypto_aead_get_flags(ctfm) &
				    CRYPTO_TFM_RES_MASK);

241
	if (priv->flags & EIP197_TRC_CACHE && ctx->base.ctxr_dma &&
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	    (memcmp(ctx->ipad, istate.state, ctx->state_sz) ||
	     memcmp(ctx->opad, ostate.state, ctx->state_sz)))
		ctx->base.needs_inv = true;

	/* Now copy the keys into the context */
	memcpy(ctx->key, keys.enckey, keys.enckeylen);
	ctx->key_len = keys.enckeylen;

	memcpy(ctx->ipad, &istate.state, ctx->state_sz);
	memcpy(ctx->opad, &ostate.state, ctx->state_sz);

	memzero_explicit(&keys, sizeof(keys));
	return 0;

badkey:
	crypto_aead_set_flags(ctfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
	memzero_explicit(&keys, sizeof(keys));
	return -EINVAL;
}

262
static int safexcel_context_control(struct safexcel_cipher_ctx *ctx,
263
				    struct crypto_async_request *async,
264
				    struct safexcel_cipher_req *sreq,
265 266 267 268 269
				    struct safexcel_command_desc *cdesc)
{
	struct safexcel_crypto_priv *priv = ctx->priv;
	int ctrl_size;

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	if (ctx->aead) {
		if (sreq->direction == SAFEXCEL_ENCRYPT)
			cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_ENCRYPT_HASH_OUT;
		else
			cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_HASH_DECRYPT_IN;
	} else {
		cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_CRYPTO_OUT;

		/* The decryption control type is a combination of the
		 * encryption type and CONTEXT_CONTROL_TYPE_NULL_IN, for all
		 * types.
		 */
		if (sreq->direction == SAFEXCEL_DECRYPT)
			cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_NULL_IN;
	}
285 286 287 288

	cdesc->control_data.control0 |= CONTEXT_CONTROL_KEY_EN;
	cdesc->control_data.control1 |= ctx->mode;

289 290
	if (ctx->aead)
		cdesc->control_data.control0 |= CONTEXT_CONTROL_DIGEST_HMAC |
291 292 293 294
						ctx->hash_alg;

	if (ctx->alg == SAFEXCEL_DES) {
		cdesc->control_data.control0 |= CONTEXT_CONTROL_CRYPTO_ALG_DES;
295 296
	} else if (ctx->alg == SAFEXCEL_3DES) {
		cdesc->control_data.control0 |= CONTEXT_CONTROL_CRYPTO_ALG_3DES;
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	} else if (ctx->alg == SAFEXCEL_AES) {
		switch (ctx->key_len) {
		case AES_KEYSIZE_128:
			cdesc->control_data.control0 |= CONTEXT_CONTROL_CRYPTO_ALG_AES128;
			break;
		case AES_KEYSIZE_192:
			cdesc->control_data.control0 |= CONTEXT_CONTROL_CRYPTO_ALG_AES192;
			break;
		case AES_KEYSIZE_256:
			cdesc->control_data.control0 |= CONTEXT_CONTROL_CRYPTO_ALG_AES256;
			break;
		default:
			dev_err(priv->dev, "aes keysize not supported: %u\n",
				ctx->key_len);
			return -EINVAL;
		}
313
	}
314 315

	ctrl_size = ctx->key_len / sizeof(u32);
316 317 318
	if (ctx->aead)
		/* Take in account the ipad+opad digests */
		ctrl_size += ctx->state_sz / sizeof(u32) * 2;
319 320 321 322 323
	cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(ctrl_size);

	return 0;
}

324 325
static int safexcel_handle_req_result(struct safexcel_crypto_priv *priv, int ring,
				      struct crypto_async_request *async,
326 327 328 329
				      struct scatterlist *src,
				      struct scatterlist *dst,
				      unsigned int cryptlen,
				      struct safexcel_cipher_req *sreq,
330
				      bool *should_complete, int *ret)
331
{
332 333 334
	struct skcipher_request *areq = skcipher_request_cast(async);
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(areq);
	struct safexcel_cipher_ctx *ctx = crypto_skcipher_ctx(skcipher);
335 336 337 338 339
	struct safexcel_result_desc *rdesc;
	int ndesc = 0;

	*ret = 0;

340 341 342 343
	if (unlikely(!sreq->rdescs))
		return 0;

	while (sreq->rdescs--) {
344 345 346 347 348 349 350 351
		rdesc = safexcel_ring_next_rptr(priv, &priv->ring[ring].rdr);
		if (IS_ERR(rdesc)) {
			dev_err(priv->dev,
				"cipher: result: could not retrieve the result descriptor\n");
			*ret = PTR_ERR(rdesc);
			break;
		}

352 353
		if (likely(!*ret))
			*ret = safexcel_rdesc_check_errors(priv, rdesc);
354 355

		ndesc++;
356
	}
357 358 359

	safexcel_complete(priv, ring);

360
	if (src == dst) {
361
		dma_unmap_sg(priv->dev, src, sreq->nr_src, DMA_BIDIRECTIONAL);
362
	} else {
363 364
		dma_unmap_sg(priv->dev, src, sreq->nr_src, DMA_TO_DEVICE);
		dma_unmap_sg(priv->dev, dst, sreq->nr_dst, DMA_FROM_DEVICE);
365 366
	}

367 368 369 370 371 372
	/*
	 * Update IV in req from last crypto output word for CBC modes
	 */
	if ((!ctx->aead) && (ctx->mode == CONTEXT_CONTROL_CRYPTO_MODE_CBC) &&
	    (sreq->direction == SAFEXCEL_ENCRYPT)) {
		/* For encrypt take the last output word */
373
		sg_pcopy_to_buffer(dst, sreq->nr_dst, areq->iv,
374 375 376 377 378
				   crypto_skcipher_ivsize(skcipher),
				   (cryptlen -
				    crypto_skcipher_ivsize(skcipher)));
	}

379 380 381 382 383
	*should_complete = true;

	return ndesc;
}

384
static int safexcel_send_req(struct crypto_async_request *base, int ring,
385 386
			     struct safexcel_cipher_req *sreq,
			     struct scatterlist *src, struct scatterlist *dst,
387 388
			     unsigned int cryptlen, unsigned int assoclen,
			     unsigned int digestsize, u8 *iv, int *commands,
389
			     int *results)
390
{
391 392
	struct skcipher_request *areq = skcipher_request_cast(base);
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(areq);
393
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(base->tfm);
394 395
	struct safexcel_crypto_priv *priv = ctx->priv;
	struct safexcel_command_desc *cdesc;
396
	struct safexcel_command_desc *first_cdesc = NULL;
397
	struct safexcel_result_desc *rdesc, *first_rdesc = NULL;
398
	struct scatterlist *sg;
399 400 401 402 403 404
	unsigned int totlen;
	unsigned int totlen_src = cryptlen + assoclen;
	unsigned int totlen_dst = totlen_src;
	int n_cdesc = 0, n_rdesc = 0;
	int queued, i, ret = 0;
	bool first = true;
405

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	sreq->nr_src = sg_nents_for_len(src, totlen_src);

	if (ctx->aead) {
		/*
		 * AEAD has auth tag appended to output for encrypt and
		 * removed from the output for decrypt!
		 */
		if (sreq->direction == SAFEXCEL_DECRYPT)
			totlen_dst -= digestsize;
		else
			totlen_dst += digestsize;

		memcpy(ctx->base.ctxr->data + ctx->key_len / sizeof(u32),
		       ctx->ipad, ctx->state_sz);
		memcpy(ctx->base.ctxr->data + (ctx->key_len + ctx->state_sz) /
		       sizeof(u32),
		       ctx->opad, ctx->state_sz);
	} else if ((ctx->mode == CONTEXT_CONTROL_CRYPTO_MODE_CBC) &&
		   (sreq->direction == SAFEXCEL_DECRYPT)) {
425 426 427 428 429
		/*
		 * Save IV from last crypto input word for CBC modes in decrypt
		 * direction. Need to do this first in case of inplace operation
		 * as it will be overwritten.
		 */
430
		sg_pcopy_to_buffer(src, sreq->nr_src, areq->iv,
431
				   crypto_skcipher_ivsize(skcipher),
432
				   (totlen_src -
433 434 435
				    crypto_skcipher_ivsize(skcipher)));
	}

436 437 438 439 440 441 442 443 444
	sreq->nr_dst = sg_nents_for_len(dst, totlen_dst);

	/*
	 * Remember actual input length, source buffer length may be
	 * updated in case of inline operation below.
	 */
	totlen = totlen_src;
	queued = totlen_src;

445
	if (src == dst) {
446 447 448 449 450 451
		sreq->nr_src = max(sreq->nr_src, sreq->nr_dst);
		sreq->nr_dst = sreq->nr_src;
		if (unlikely((totlen_src || totlen_dst) &&
		    (sreq->nr_src <= 0))) {
			dev_err(priv->dev, "In-place buffer not large enough (need %d bytes)!",
				max(totlen_src, totlen_dst));
452
			return -EINVAL;
453 454
		}
		dma_map_sg(priv->dev, src, sreq->nr_src, DMA_BIDIRECTIONAL);
455
	} else {
456 457 458
		if (unlikely(totlen_src && (sreq->nr_src <= 0))) {
			dev_err(priv->dev, "Source buffer not large enough (need %d bytes)!",
				totlen_src);
459
			return -EINVAL;
460 461
		}
		dma_map_sg(priv->dev, src, sreq->nr_src, DMA_TO_DEVICE);
462

463 464 465 466 467
		if (unlikely(totlen_dst && (sreq->nr_dst <= 0))) {
			dev_err(priv->dev, "Dest buffer not large enough (need %d bytes)!",
				totlen_dst);
			dma_unmap_sg(priv->dev, src, sreq->nr_src,
				     DMA_TO_DEVICE);
468 469
			return -EINVAL;
		}
470
		dma_map_sg(priv->dev, dst, sreq->nr_dst, DMA_FROM_DEVICE);
471 472 473 474
	}

	memcpy(ctx->base.ctxr->data, ctx->key, ctx->key_len);

475 476 477
	/* The EIP cannot deal with zero length input packets! */
	if (totlen == 0)
		totlen = 1;
478

479
	/* command descriptors */
480
	for_each_sg(src, sg, sreq->nr_src, i) {
481 482 483 484 485 486
		int len = sg_dma_len(sg);

		/* Do not overflow the request */
		if (queued - len < 0)
			len = queued;

487 488
		cdesc = safexcel_add_cdesc(priv, ring, !n_cdesc,
					   !(queued - len),
489
					   sg_dma_address(sg), len, totlen,
490 491 492 493 494 495 496 497 498
					   ctx->base.ctxr_dma);
		if (IS_ERR(cdesc)) {
			/* No space left in the command descriptor ring */
			ret = PTR_ERR(cdesc);
			goto cdesc_rollback;
		}
		n_cdesc++;

		if (n_cdesc == 1) {
499
			first_cdesc = cdesc;
500 501 502 503 504 505 506
		}

		queued -= len;
		if (!queued)
			break;
	}

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
	if (unlikely(!n_cdesc)) {
		/*
		 * Special case: zero length input buffer.
		 * The engine always needs the 1st command descriptor, however!
		 */
		first_cdesc = safexcel_add_cdesc(priv, ring, 1, 1, 0, 0, totlen,
						 ctx->base.ctxr_dma);
		n_cdesc = 1;
	}

	/* Add context control words and token to first command descriptor */
	safexcel_context_control(ctx, base, sreq, first_cdesc);
	if (ctx->aead)
		safexcel_aead_token(ctx, iv, first_cdesc,
				    sreq->direction, cryptlen,
				    assoclen, digestsize);
	else
		safexcel_skcipher_token(ctx, iv, first_cdesc,
					cryptlen);

527
	/* result descriptors */
528 529
	for_each_sg(dst, sg, sreq->nr_dst, i) {
		bool last = (i == sreq->nr_dst - 1);
530 531
		u32 len = sg_dma_len(sg);

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
		/* only allow the part of the buffer we know we need */
		if (len > totlen_dst)
			len = totlen_dst;
		if (unlikely(!len))
			break;
		totlen_dst -= len;

		/* skip over AAD space in buffer - not written */
		if (assoclen) {
			if (assoclen >= len) {
				assoclen -= len;
				continue;
			}
			rdesc = safexcel_add_rdesc(priv, ring, first, last,
						   sg_dma_address(sg) +
						   assoclen,
						   len - assoclen);
			assoclen = 0;
		} else {
			rdesc = safexcel_add_rdesc(priv, ring, first, last,
						   sg_dma_address(sg),
						   len);
		}
555 556 557 558 559
		if (IS_ERR(rdesc)) {
			/* No space left in the result descriptor ring */
			ret = PTR_ERR(rdesc);
			goto rdesc_rollback;
		}
560
		if (first) {
561
			first_rdesc = rdesc;
562 563
			first = false;
		}
564 565 566
		n_rdesc++;
	}

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	if (unlikely(first)) {
		/*
		 * Special case: AEAD decrypt with only AAD data.
		 * In this case there is NO output data from the engine,
		 * but the engine still needs a result descriptor!
		 * Create a dummy one just for catching the result token.
		 */
		rdesc = safexcel_add_rdesc(priv, ring, true, true, 0, 0);
		if (IS_ERR(rdesc)) {
			/* No space left in the result descriptor ring */
			ret = PTR_ERR(rdesc);
			goto rdesc_rollback;
		}
		first_rdesc = rdesc;
		n_rdesc = 1;
	}

584
	safexcel_rdr_req_set(priv, ring, first_rdesc, base);
585

586
	*commands = n_cdesc;
587
	*results = n_rdesc;
588 589 590 591 592 593 594 595 596
	return 0;

rdesc_rollback:
	for (i = 0; i < n_rdesc; i++)
		safexcel_ring_rollback_wptr(priv, &priv->ring[ring].rdr);
cdesc_rollback:
	for (i = 0; i < n_cdesc; i++)
		safexcel_ring_rollback_wptr(priv, &priv->ring[ring].cdr);

597
	if (src == dst) {
598
		dma_unmap_sg(priv->dev, src, sreq->nr_src, DMA_BIDIRECTIONAL);
599
	} else {
600 601
		dma_unmap_sg(priv->dev, src, sreq->nr_src, DMA_TO_DEVICE);
		dma_unmap_sg(priv->dev, dst, sreq->nr_dst, DMA_FROM_DEVICE);
602 603 604 605 606 607 608
	}

	return ret;
}

static int safexcel_handle_inv_result(struct safexcel_crypto_priv *priv,
				      int ring,
609
				      struct crypto_async_request *base,
610
				      struct safexcel_cipher_req *sreq,
611 612
				      bool *should_complete, int *ret)
{
613
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(base->tfm);
614 615 616 617 618
	struct safexcel_result_desc *rdesc;
	int ndesc = 0, enq_ret;

	*ret = 0;

619 620 621 622
	if (unlikely(!sreq->rdescs))
		return 0;

	while (sreq->rdescs--) {
623 624 625 626 627 628 629 630
		rdesc = safexcel_ring_next_rptr(priv, &priv->ring[ring].rdr);
		if (IS_ERR(rdesc)) {
			dev_err(priv->dev,
				"cipher: invalidate: could not retrieve the result descriptor\n");
			*ret = PTR_ERR(rdesc);
			break;
		}

631 632
		if (likely(!*ret))
			*ret = safexcel_rdesc_check_errors(priv, rdesc);
633 634

		ndesc++;
635
	}
636 637 638 639 640 641 642 643 644 645 646 647

	safexcel_complete(priv, ring);

	if (ctx->base.exit_inv) {
		dma_pool_free(priv->context_pool, ctx->base.ctxr,
			      ctx->base.ctxr_dma);

		*should_complete = true;

		return ndesc;
	}

648 649
	ring = safexcel_select_ring(priv);
	ctx->base.ring = ring;
650

651
	spin_lock_bh(&priv->ring[ring].queue_lock);
652
	enq_ret = crypto_enqueue_request(&priv->ring[ring].queue, base);
653
	spin_unlock_bh(&priv->ring[ring].queue_lock);
654 655 656 657

	if (enq_ret != -EINPROGRESS)
		*ret = enq_ret;

658 659
	queue_work(priv->ring[ring].workqueue,
		   &priv->ring[ring].work_data.work);
660

661 662 663 664 665
	*should_complete = false;

	return ndesc;
}

666 667 668 669
static int safexcel_skcipher_handle_result(struct safexcel_crypto_priv *priv,
					   int ring,
					   struct crypto_async_request *async,
					   bool *should_complete, int *ret)
670 671 672 673 674 675 676
{
	struct skcipher_request *req = skcipher_request_cast(async);
	struct safexcel_cipher_req *sreq = skcipher_request_ctx(req);
	int err;

	if (sreq->needs_inv) {
		sreq->needs_inv = false;
677
		err = safexcel_handle_inv_result(priv, ring, async, sreq,
678 679
						 should_complete, ret);
	} else {
680 681
		err = safexcel_handle_req_result(priv, ring, async, req->src,
						 req->dst, req->cryptlen, sreq,
682 683 684 685 686 687
						 should_complete, ret);
	}

	return err;
}

688 689 690 691 692 693 694 695 696 697 698 699
static int safexcel_aead_handle_result(struct safexcel_crypto_priv *priv,
				       int ring,
				       struct crypto_async_request *async,
				       bool *should_complete, int *ret)
{
	struct aead_request *req = aead_request_cast(async);
	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
	struct safexcel_cipher_req *sreq = aead_request_ctx(req);
	int err;

	if (sreq->needs_inv) {
		sreq->needs_inv = false;
700
		err = safexcel_handle_inv_result(priv, ring, async, sreq,
701 702 703 704 705 706 707 708 709 710 711
						 should_complete, ret);
	} else {
		err = safexcel_handle_req_result(priv, ring, async, req->src,
						 req->dst,
						 req->cryptlen + crypto_aead_authsize(tfm),
						 sreq, should_complete, ret);
	}

	return err;
}

712
static int safexcel_cipher_send_inv(struct crypto_async_request *base,
713
				    int ring, int *commands, int *results)
714
{
715
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(base->tfm);
716 717 718
	struct safexcel_crypto_priv *priv = ctx->priv;
	int ret;

719
	ret = safexcel_invalidate_cache(base, priv, ctx->base.ctxr_dma, ring);
720 721 722 723 724 725 726 727 728
	if (unlikely(ret))
		return ret;

	*commands = 1;
	*results = 1;

	return 0;
}

729 730
static int safexcel_skcipher_send(struct crypto_async_request *async, int ring,
				  int *commands, int *results)
731 732
{
	struct skcipher_request *req = skcipher_request_cast(async);
733
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
734
	struct safexcel_cipher_req *sreq = skcipher_request_ctx(req);
735
	struct safexcel_crypto_priv *priv = ctx->priv;
736 737
	int ret;

738
	BUG_ON(!(priv->flags & EIP197_TRC_CACHE) && sreq->needs_inv);
739

740
	if (sreq->needs_inv) {
741
		ret = safexcel_cipher_send_inv(async, ring, commands, results);
742 743 744 745 746 747 748 749 750 751
	} else {
		struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
		u8 input_iv[AES_BLOCK_SIZE];

		/*
		 * Save input IV in case of CBC decrypt mode
		 * Will be overwritten with output IV prior to use!
		 */
		memcpy(input_iv, req->iv, crypto_skcipher_ivsize(skcipher));

752
		ret = safexcel_send_req(async, ring, sreq, req->src,
753
					req->dst, req->cryptlen, 0, 0, input_iv,
754
					commands, results);
755
	}
756 757

	sreq->rdescs = *results;
758 759 760 761
	return ret;
}

static int safexcel_aead_send(struct crypto_async_request *async, int ring,
762
			      int *commands, int *results)
763 764 765 766 767 768 769 770
{
	struct aead_request *req = aead_request_cast(async);
	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
	struct safexcel_cipher_req *sreq = aead_request_ctx(req);
	struct safexcel_crypto_priv *priv = ctx->priv;
	int ret;

771
	BUG_ON(!(priv->flags & EIP197_TRC_CACHE) && sreq->needs_inv);
772 773

	if (sreq->needs_inv)
774
		ret = safexcel_cipher_send_inv(async, ring, commands, results);
775
	else
776 777
		ret = safexcel_send_req(async, ring, sreq, req->src, req->dst,
					req->cryptlen, req->assoclen,
778
					crypto_aead_authsize(tfm), req->iv,
779
					commands, results);
780
	sreq->rdescs = *results;
781 782 783
	return ret;
}

784 785 786 787
static int safexcel_cipher_exit_inv(struct crypto_tfm *tfm,
				    struct crypto_async_request *base,
				    struct safexcel_cipher_req *sreq,
				    struct safexcel_inv_result *result)
788 789 790
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
	struct safexcel_crypto_priv *priv = ctx->priv;
791
	int ring = ctx->base.ring;
792

793
	init_completion(&result->completion);
794

795
	ctx = crypto_tfm_ctx(base->tfm);
796
	ctx->base.exit_inv = true;
797
	sreq->needs_inv = true;
798

799
	spin_lock_bh(&priv->ring[ring].queue_lock);
800
	crypto_enqueue_request(&priv->ring[ring].queue, base);
801
	spin_unlock_bh(&priv->ring[ring].queue_lock);
802

803 804
	queue_work(priv->ring[ring].workqueue,
		   &priv->ring[ring].work_data.work);
805

806
	wait_for_completion(&result->completion);
807

808
	if (result->error) {
809 810
		dev_warn(priv->dev,
			"cipher: sync: invalidate: completion error %d\n",
811 812
			 result->error);
		return result->error;
813 814 815 816 817
	}

	return 0;
}

818
static int safexcel_skcipher_exit_inv(struct crypto_tfm *tfm)
819
{
820
	EIP197_REQUEST_ON_STACK(req, skcipher, EIP197_SKCIPHER_REQ_SIZE);
821
	struct safexcel_cipher_req *sreq = skcipher_request_ctx(req);
822 823 824 825 826 827 828 829 830 831 832
	struct safexcel_inv_result result = {};

	memset(req, 0, sizeof(struct skcipher_request));

	skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
				      safexcel_inv_complete, &result);
	skcipher_request_set_tfm(req, __crypto_skcipher_cast(tfm));

	return safexcel_cipher_exit_inv(tfm, &req->base, sreq, &result);
}

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
static int safexcel_aead_exit_inv(struct crypto_tfm *tfm)
{
	EIP197_REQUEST_ON_STACK(req, aead, EIP197_AEAD_REQ_SIZE);
	struct safexcel_cipher_req *sreq = aead_request_ctx(req);
	struct safexcel_inv_result result = {};

	memset(req, 0, sizeof(struct aead_request));

	aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
				  safexcel_inv_complete, &result);
	aead_request_set_tfm(req, __crypto_aead_cast(tfm));

	return safexcel_cipher_exit_inv(tfm, &req->base, sreq, &result);
}

848
static int safexcel_queue_req(struct crypto_async_request *base,
849
			struct safexcel_cipher_req *sreq,
850 851
			enum safexcel_cipher_direction dir, u32 mode,
			enum safexcel_cipher_alg alg)
852 853
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(base->tfm);
854
	struct safexcel_crypto_priv *priv = ctx->priv;
855
	int ret, ring;
856

857
	sreq->needs_inv = false;
858
	sreq->direction = dir;
859
	ctx->alg = alg;
860 861 862
	ctx->mode = mode;

	if (ctx->base.ctxr) {
863
		if (priv->flags & EIP197_TRC_CACHE && ctx->base.needs_inv) {
864 865 866
			sreq->needs_inv = true;
			ctx->base.needs_inv = false;
		}
867 868 869
	} else {
		ctx->base.ring = safexcel_select_ring(priv);
		ctx->base.ctxr = dma_pool_zalloc(priv->context_pool,
870
						 EIP197_GFP_FLAGS(*base),
871 872 873 874 875
						 &ctx->base.ctxr_dma);
		if (!ctx->base.ctxr)
			return -ENOMEM;
	}

876 877 878
	ring = ctx->base.ring;

	spin_lock_bh(&priv->ring[ring].queue_lock);
879
	ret = crypto_enqueue_request(&priv->ring[ring].queue, base);
880
	spin_unlock_bh(&priv->ring[ring].queue_lock);
881

882 883
	queue_work(priv->ring[ring].workqueue,
		   &priv->ring[ring].work_data.work);
884 885 886 887 888 889

	return ret;
}

static int safexcel_ecb_aes_encrypt(struct skcipher_request *req)
{
890 891 892
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_ENCRYPT, CONTEXT_CONTROL_CRYPTO_MODE_ECB,
			SAFEXCEL_AES);
893 894 895 896
}

static int safexcel_ecb_aes_decrypt(struct skcipher_request *req)
{
897 898 899
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_DECRYPT, CONTEXT_CONTROL_CRYPTO_MODE_ECB,
			SAFEXCEL_AES);
900 901 902 903 904 905 906 907 908
}

static int safexcel_skcipher_cra_init(struct crypto_tfm *tfm)
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
	struct safexcel_alg_template *tmpl =
		container_of(tfm->__crt_alg, struct safexcel_alg_template,
			     alg.skcipher.base);

909 910
	crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
				    sizeof(struct safexcel_cipher_req));
911

912 913 914 915
	ctx->priv = tmpl->priv;

	ctx->base.send = safexcel_skcipher_send;
	ctx->base.handle_result = safexcel_skcipher_handle_result;
916 917 918
	return 0;
}

919
static int safexcel_cipher_cra_exit(struct crypto_tfm *tfm)
920 921 922
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);

923
	memzero_explicit(ctx->key, sizeof(ctx->key));
924 925 926

	/* context not allocated, skip invalidation */
	if (!ctx->base.ctxr)
927
		return -ENOMEM;
928

929
	memzero_explicit(ctx->base.ctxr->data, sizeof(ctx->base.ctxr->data));
930 931 932 933 934 935 936 937 938 939 940
	return 0;
}

static void safexcel_skcipher_cra_exit(struct crypto_tfm *tfm)
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
	struct safexcel_crypto_priv *priv = ctx->priv;
	int ret;

	if (safexcel_cipher_cra_exit(tfm))
		return;
941

942
	if (priv->flags & EIP197_TRC_CACHE) {
943
		ret = safexcel_skcipher_exit_inv(tfm);
944
		if (ret)
945 946
			dev_warn(priv->dev, "skcipher: invalidation error %d\n",
				 ret);
947 948 949 950
	} else {
		dma_pool_free(priv->context_pool, ctx->base.ctxr,
			      ctx->base.ctxr_dma);
	}
951 952
}

953 954 955 956 957 958 959 960 961
static void safexcel_aead_cra_exit(struct crypto_tfm *tfm)
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
	struct safexcel_crypto_priv *priv = ctx->priv;
	int ret;

	if (safexcel_cipher_cra_exit(tfm))
		return;

962
	if (priv->flags & EIP197_TRC_CACHE) {
963 964 965 966 967 968 969 970 971 972
		ret = safexcel_aead_exit_inv(tfm);
		if (ret)
			dev_warn(priv->dev, "aead: invalidation error %d\n",
				 ret);
	} else {
		dma_pool_free(priv->context_pool, ctx->base.ctxr,
			      ctx->base.ctxr_dma);
	}
}

973 974
struct safexcel_alg_template safexcel_alg_ecb_aes = {
	.type = SAFEXCEL_ALG_TYPE_SKCIPHER,
975
	.engines = EIP97IES | EIP197B | EIP197D,
976
	.alg.skcipher = {
977
		.setkey = safexcel_skcipher_aes_setkey,
978 979 980 981 982 983 984 985
		.encrypt = safexcel_ecb_aes_encrypt,
		.decrypt = safexcel_ecb_aes_decrypt,
		.min_keysize = AES_MIN_KEY_SIZE,
		.max_keysize = AES_MAX_KEY_SIZE,
		.base = {
			.cra_name = "ecb(aes)",
			.cra_driver_name = "safexcel-ecb-aes",
			.cra_priority = 300,
986
			.cra_flags = CRYPTO_ALG_ASYNC |
987 988 989 990 991 992 993 994 995 996 997 998 999
				     CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = AES_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
			.cra_alignmask = 0,
			.cra_init = safexcel_skcipher_cra_init,
			.cra_exit = safexcel_skcipher_cra_exit,
			.cra_module = THIS_MODULE,
		},
	},
};

static int safexcel_cbc_aes_encrypt(struct skcipher_request *req)
{
1000 1001 1002
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_ENCRYPT, CONTEXT_CONTROL_CRYPTO_MODE_CBC,
			SAFEXCEL_AES);
1003 1004 1005 1006
}

static int safexcel_cbc_aes_decrypt(struct skcipher_request *req)
{
1007 1008 1009
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_DECRYPT, CONTEXT_CONTROL_CRYPTO_MODE_CBC,
			SAFEXCEL_AES);
1010 1011 1012 1013
}

struct safexcel_alg_template safexcel_alg_cbc_aes = {
	.type = SAFEXCEL_ALG_TYPE_SKCIPHER,
1014
	.engines = EIP97IES | EIP197B | EIP197D,
1015
	.alg.skcipher = {
1016
		.setkey = safexcel_skcipher_aes_setkey,
1017 1018 1019 1020 1021 1022 1023 1024 1025
		.encrypt = safexcel_cbc_aes_encrypt,
		.decrypt = safexcel_cbc_aes_decrypt,
		.min_keysize = AES_MIN_KEY_SIZE,
		.max_keysize = AES_MAX_KEY_SIZE,
		.ivsize = AES_BLOCK_SIZE,
		.base = {
			.cra_name = "cbc(aes)",
			.cra_driver_name = "safexcel-cbc-aes",
			.cra_priority = 300,
1026
			.cra_flags = CRYPTO_ALG_ASYNC |
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
				     CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = AES_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
			.cra_alignmask = 0,
			.cra_init = safexcel_skcipher_cra_init,
			.cra_exit = safexcel_skcipher_cra_exit,
			.cra_module = THIS_MODULE,
		},
	},
};
1037

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
static int safexcel_cbc_des_encrypt(struct skcipher_request *req)
{
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_ENCRYPT, CONTEXT_CONTROL_CRYPTO_MODE_CBC,
			SAFEXCEL_DES);
}

static int safexcel_cbc_des_decrypt(struct skcipher_request *req)
{
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_DECRYPT, CONTEXT_CONTROL_CRYPTO_MODE_CBC,
			SAFEXCEL_DES);
}

static int safexcel_des_setkey(struct crypto_skcipher *ctfm, const u8 *key,
			       unsigned int len)
{
	struct crypto_tfm *tfm = crypto_skcipher_tfm(ctfm);
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
	u32 tmp[DES_EXPKEY_WORDS];
	int ret;

	if (len != DES_KEY_SIZE) {
		crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
		return -EINVAL;
	}

	ret = des_ekey(tmp, key);
1066
	if (!ret && (tfm->crt_flags & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) {
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
		tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
		return -EINVAL;
	}

	/* if context exits and key changed, need to invalidate it */
	if (ctx->base.ctxr_dma)
		if (memcmp(ctx->key, key, len))
			ctx->base.needs_inv = true;

	memcpy(ctx->key, key, len);
	ctx->key_len = len;

	return 0;
}

struct safexcel_alg_template safexcel_alg_cbc_des = {
	.type = SAFEXCEL_ALG_TYPE_SKCIPHER,
	.engines = EIP97IES | EIP197B | EIP197D,
	.alg.skcipher = {
		.setkey = safexcel_des_setkey,
		.encrypt = safexcel_cbc_des_encrypt,
		.decrypt = safexcel_cbc_des_decrypt,
		.min_keysize = DES_KEY_SIZE,
		.max_keysize = DES_KEY_SIZE,
		.ivsize = DES_BLOCK_SIZE,
		.base = {
			.cra_name = "cbc(des)",
			.cra_driver_name = "safexcel-cbc-des",
			.cra_priority = 300,
1096
			.cra_flags = CRYPTO_ALG_ASYNC |
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
				     CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = DES_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
			.cra_alignmask = 0,
			.cra_init = safexcel_skcipher_cra_init,
			.cra_exit = safexcel_skcipher_cra_exit,
			.cra_module = THIS_MODULE,
		},
	},
};

static int safexcel_ecb_des_encrypt(struct skcipher_request *req)
{
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_ENCRYPT, CONTEXT_CONTROL_CRYPTO_MODE_ECB,
			SAFEXCEL_DES);
}

static int safexcel_ecb_des_decrypt(struct skcipher_request *req)
{
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_DECRYPT, CONTEXT_CONTROL_CRYPTO_MODE_ECB,
			SAFEXCEL_DES);
}

struct safexcel_alg_template safexcel_alg_ecb_des = {
	.type = SAFEXCEL_ALG_TYPE_SKCIPHER,
	.engines = EIP97IES | EIP197B | EIP197D,
	.alg.skcipher = {
		.setkey = safexcel_des_setkey,
		.encrypt = safexcel_ecb_des_encrypt,
		.decrypt = safexcel_ecb_des_decrypt,
		.min_keysize = DES_KEY_SIZE,
		.max_keysize = DES_KEY_SIZE,
		.base = {
			.cra_name = "ecb(des)",
			.cra_driver_name = "safexcel-ecb-des",
			.cra_priority = 300,
1135
			.cra_flags = CRYPTO_ALG_ASYNC |
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
				     CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = DES_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
			.cra_alignmask = 0,
			.cra_init = safexcel_skcipher_cra_init,
			.cra_exit = safexcel_skcipher_cra_exit,
			.cra_module = THIS_MODULE,
		},
	},
};
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

static int safexcel_cbc_des3_ede_encrypt(struct skcipher_request *req)
{
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_ENCRYPT, CONTEXT_CONTROL_CRYPTO_MODE_CBC,
			SAFEXCEL_3DES);
}

static int safexcel_cbc_des3_ede_decrypt(struct skcipher_request *req)
{
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_DECRYPT, CONTEXT_CONTROL_CRYPTO_MODE_CBC,
			SAFEXCEL_3DES);
}

static int safexcel_des3_ede_setkey(struct crypto_skcipher *ctfm,
				   const u8 *key, unsigned int len)
{
1164 1165
	struct safexcel_cipher_ctx *ctx = crypto_skcipher_ctx(ctfm);
	int err;
1166

1167 1168 1169
	err = des3_verify_key(ctfm, key);
	if (unlikely(err))
		return err;
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

	/* if context exits and key changed, need to invalidate it */
	if (ctx->base.ctxr_dma) {
		if (memcmp(ctx->key, key, len))
			ctx->base.needs_inv = true;
	}

	memcpy(ctx->key, key, len);

	ctx->key_len = len;

	return 0;
}

struct safexcel_alg_template safexcel_alg_cbc_des3_ede = {
	.type = SAFEXCEL_ALG_TYPE_SKCIPHER,
	.engines = EIP97IES | EIP197B | EIP197D,
	.alg.skcipher = {
		.setkey = safexcel_des3_ede_setkey,
		.encrypt = safexcel_cbc_des3_ede_encrypt,
		.decrypt = safexcel_cbc_des3_ede_decrypt,
		.min_keysize = DES3_EDE_KEY_SIZE,
		.max_keysize = DES3_EDE_KEY_SIZE,
		.ivsize = DES3_EDE_BLOCK_SIZE,
		.base = {
			.cra_name = "cbc(des3_ede)",
			.cra_driver_name = "safexcel-cbc-des3_ede",
			.cra_priority = 300,
1198
			.cra_flags = CRYPTO_ALG_ASYNC |
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
				     CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
			.cra_alignmask = 0,
			.cra_init = safexcel_skcipher_cra_init,
			.cra_exit = safexcel_skcipher_cra_exit,
			.cra_module = THIS_MODULE,
		},
	},
};

static int safexcel_ecb_des3_ede_encrypt(struct skcipher_request *req)
{
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_ENCRYPT, CONTEXT_CONTROL_CRYPTO_MODE_ECB,
			SAFEXCEL_3DES);
}

static int safexcel_ecb_des3_ede_decrypt(struct skcipher_request *req)
{
	return safexcel_queue_req(&req->base, skcipher_request_ctx(req),
			SAFEXCEL_DECRYPT, CONTEXT_CONTROL_CRYPTO_MODE_ECB,
			SAFEXCEL_3DES);
}

struct safexcel_alg_template safexcel_alg_ecb_des3_ede = {
	.type = SAFEXCEL_ALG_TYPE_SKCIPHER,
	.engines = EIP97IES | EIP197B | EIP197D,
	.alg.skcipher = {
		.setkey = safexcel_des3_ede_setkey,
		.encrypt = safexcel_ecb_des3_ede_encrypt,
		.decrypt = safexcel_ecb_des3_ede_decrypt,
		.min_keysize = DES3_EDE_KEY_SIZE,
		.max_keysize = DES3_EDE_KEY_SIZE,
		.base = {
			.cra_name = "ecb(des3_ede)",
			.cra_driver_name = "safexcel-ecb-des3_ede",
			.cra_priority = 300,
1237
			.cra_flags = CRYPTO_ALG_ASYNC |
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
				     CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
			.cra_alignmask = 0,
			.cra_init = safexcel_skcipher_cra_init,
			.cra_exit = safexcel_skcipher_cra_exit,
			.cra_module = THIS_MODULE,
		},
	},
};

1249 1250 1251 1252
static int safexcel_aead_encrypt(struct aead_request *req)
{
	struct safexcel_cipher_req *creq = aead_request_ctx(req);

1253 1254
	return safexcel_queue_req(&req->base, creq, SAFEXCEL_ENCRYPT,
			CONTEXT_CONTROL_CRYPTO_MODE_CBC, SAFEXCEL_AES);
1255 1256 1257 1258 1259 1260
}

static int safexcel_aead_decrypt(struct aead_request *req)
{
	struct safexcel_cipher_req *creq = aead_request_ctx(req);

1261 1262
	return safexcel_queue_req(&req->base, creq, SAFEXCEL_DECRYPT,
			CONTEXT_CONTROL_CRYPTO_MODE_CBC, SAFEXCEL_AES);
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
}

static int safexcel_aead_cra_init(struct crypto_tfm *tfm)
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
	struct safexcel_alg_template *tmpl =
		container_of(tfm->__crt_alg, struct safexcel_alg_template,
			     alg.aead.base);

	crypto_aead_set_reqsize(__crypto_aead_cast(tfm),
				sizeof(struct safexcel_cipher_req));

	ctx->priv = tmpl->priv;

	ctx->aead = true;
	ctx->base.send = safexcel_aead_send;
	ctx->base.handle_result = safexcel_aead_handle_result;
	return 0;
}

1283 1284 1285 1286 1287
static int safexcel_aead_sha1_cra_init(struct crypto_tfm *tfm)
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);

	safexcel_aead_cra_init(tfm);
1288
	ctx->hash_alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA1;
1289 1290 1291 1292 1293 1294
	ctx->state_sz = SHA1_DIGEST_SIZE;
	return 0;
}

struct safexcel_alg_template safexcel_alg_authenc_hmac_sha1_cbc_aes = {
	.type = SAFEXCEL_ALG_TYPE_AEAD,
1295
	.engines = EIP97IES | EIP197B | EIP197D,
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
	.alg.aead = {
		.setkey = safexcel_aead_aes_setkey,
		.encrypt = safexcel_aead_encrypt,
		.decrypt = safexcel_aead_decrypt,
		.ivsize = AES_BLOCK_SIZE,
		.maxauthsize = SHA1_DIGEST_SIZE,
		.base = {
			.cra_name = "authenc(hmac(sha1),cbc(aes))",
			.cra_driver_name = "safexcel-authenc-hmac-sha1-cbc-aes",
			.cra_priority = 300,
1306
			.cra_flags = CRYPTO_ALG_ASYNC |
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
				     CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = AES_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
			.cra_alignmask = 0,
			.cra_init = safexcel_aead_sha1_cra_init,
			.cra_exit = safexcel_aead_cra_exit,
			.cra_module = THIS_MODULE,
		},
	},
};

1318 1319 1320 1321 1322
static int safexcel_aead_sha256_cra_init(struct crypto_tfm *tfm)
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);

	safexcel_aead_cra_init(tfm);
1323
	ctx->hash_alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA256;
1324 1325 1326 1327 1328 1329
	ctx->state_sz = SHA256_DIGEST_SIZE;
	return 0;
}

struct safexcel_alg_template safexcel_alg_authenc_hmac_sha256_cbc_aes = {
	.type = SAFEXCEL_ALG_TYPE_AEAD,
1330
	.engines = EIP97IES | EIP197B | EIP197D,
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
	.alg.aead = {
		.setkey = safexcel_aead_aes_setkey,
		.encrypt = safexcel_aead_encrypt,
		.decrypt = safexcel_aead_decrypt,
		.ivsize = AES_BLOCK_SIZE,
		.maxauthsize = SHA256_DIGEST_SIZE,
		.base = {
			.cra_name = "authenc(hmac(sha256),cbc(aes))",
			.cra_driver_name = "safexcel-authenc-hmac-sha256-cbc-aes",
			.cra_priority = 300,
1341
			.cra_flags = CRYPTO_ALG_ASYNC |
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
				     CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = AES_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
			.cra_alignmask = 0,
			.cra_init = safexcel_aead_sha256_cra_init,
			.cra_exit = safexcel_aead_cra_exit,
			.cra_module = THIS_MODULE,
		},
	},
};
1352 1353 1354 1355 1356 1357

static int safexcel_aead_sha224_cra_init(struct crypto_tfm *tfm)
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);

	safexcel_aead_cra_init(tfm);
1358
	ctx->hash_alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA224;
1359 1360 1361 1362 1363 1364
	ctx->state_sz = SHA256_DIGEST_SIZE;
	return 0;
}

struct safexcel_alg_template safexcel_alg_authenc_hmac_sha224_cbc_aes = {
	.type = SAFEXCEL_ALG_TYPE_AEAD,
1365
	.engines = EIP97IES | EIP197B | EIP197D,
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
	.alg.aead = {
		.setkey = safexcel_aead_aes_setkey,
		.encrypt = safexcel_aead_encrypt,
		.decrypt = safexcel_aead_decrypt,
		.ivsize = AES_BLOCK_SIZE,
		.maxauthsize = SHA224_DIGEST_SIZE,
		.base = {
			.cra_name = "authenc(hmac(sha224),cbc(aes))",
			.cra_driver_name = "safexcel-authenc-hmac-sha224-cbc-aes",
			.cra_priority = 300,
1376
			.cra_flags = CRYPTO_ALG_ASYNC |
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
				     CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = AES_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
			.cra_alignmask = 0,
			.cra_init = safexcel_aead_sha224_cra_init,
			.cra_exit = safexcel_aead_cra_exit,
			.cra_module = THIS_MODULE,
		},
	},
};
1387 1388 1389 1390 1391 1392

static int safexcel_aead_sha512_cra_init(struct crypto_tfm *tfm)
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);

	safexcel_aead_cra_init(tfm);
1393
	ctx->hash_alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA512;
1394 1395 1396 1397 1398 1399
	ctx->state_sz = SHA512_DIGEST_SIZE;
	return 0;
}

struct safexcel_alg_template safexcel_alg_authenc_hmac_sha512_cbc_aes = {
	.type = SAFEXCEL_ALG_TYPE_AEAD,
1400
	.engines = EIP97IES | EIP197B | EIP197D,
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
	.alg.aead = {
		.setkey = safexcel_aead_aes_setkey,
		.encrypt = safexcel_aead_encrypt,
		.decrypt = safexcel_aead_decrypt,
		.ivsize = AES_BLOCK_SIZE,
		.maxauthsize = SHA512_DIGEST_SIZE,
		.base = {
			.cra_name = "authenc(hmac(sha512),cbc(aes))",
			.cra_driver_name = "safexcel-authenc-hmac-sha512-cbc-aes",
			.cra_priority = 300,
1411
			.cra_flags = CRYPTO_ALG_ASYNC |
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
				     CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = AES_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
			.cra_alignmask = 0,
			.cra_init = safexcel_aead_sha512_cra_init,
			.cra_exit = safexcel_aead_cra_exit,
			.cra_module = THIS_MODULE,
		},
	},
};
1422 1423 1424 1425 1426 1427

static int safexcel_aead_sha384_cra_init(struct crypto_tfm *tfm)
{
	struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);

	safexcel_aead_cra_init(tfm);
1428
	ctx->hash_alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA384;
1429 1430 1431 1432 1433 1434
	ctx->state_sz = SHA512_DIGEST_SIZE;
	return 0;
}

struct safexcel_alg_template safexcel_alg_authenc_hmac_sha384_cbc_aes = {
	.type = SAFEXCEL_ALG_TYPE_AEAD,
1435
	.engines = EIP97IES | EIP197B | EIP197D,
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
	.alg.aead = {
		.setkey = safexcel_aead_aes_setkey,
		.encrypt = safexcel_aead_encrypt,
		.decrypt = safexcel_aead_decrypt,
		.ivsize = AES_BLOCK_SIZE,
		.maxauthsize = SHA384_DIGEST_SIZE,
		.base = {
			.cra_name = "authenc(hmac(sha384),cbc(aes))",
			.cra_driver_name = "safexcel-authenc-hmac-sha384-cbc-aes",
			.cra_priority = 300,
1446
			.cra_flags = CRYPTO_ALG_ASYNC |
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
				     CRYPTO_ALG_KERN_DRIVER_ONLY,
			.cra_blocksize = AES_BLOCK_SIZE,
			.cra_ctxsize = sizeof(struct safexcel_cipher_ctx),
			.cra_alignmask = 0,
			.cra_init = safexcel_aead_sha384_cra_init,
			.cra_exit = safexcel_aead_cra_exit,
			.cra_module = THIS_MODULE,
		},
	},
};