caamalg.c 96.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4 5
/*
 * caam - Freescale FSL CAAM support for crypto API
 *
 * Copyright 2008-2011 Freescale Semiconductor, Inc.
6
 * Copyright 2016-2019 NXP
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * Based on talitos crypto API driver.
 *
 * relationship of job descriptors to shared descriptors (SteveC Dec 10 2008):
 *
 * ---------------                     ---------------
 * | JobDesc #1  |-------------------->|  ShareDesc  |
 * | *(packet 1) |                     |   (PDB)     |
 * ---------------      |------------->|  (hashKey)  |
 *       .              |              | (cipherKey) |
 *       .              |    |-------->| (operation) |
 * ---------------      |    |         ---------------
 * | JobDesc #2  |------|    |
 * | *(packet 2) |           |
 * ---------------           |
 *       .                   |
 *       .                   |
 * ---------------           |
 * | JobDesc #3  |------------
 * | *(packet 3) |
 * ---------------
 *
 * The SharedDesc never changes for a connection unless rekeyed, but
 * each packet will likely be in a different place. So all we need
 * to know to process the packet is where the input is, where the
 * output goes, and what context we want to process with. Context is
 * in the SharedDesc, packet references in the JobDesc.
 *
 * So, a job desc looks like:
 *
 * ---------------------
 * | Header            |
 * | ShareDesc Pointer |
 * | SEQ_OUT_PTR       |
 * | (output buffer)   |
42
 * | (output length)   |
43 44
 * | SEQ_IN_PTR        |
 * | (input buffer)    |
45
 * | (input length)    |
46 47 48 49 50 51 52 53 54 55
 * ---------------------
 */

#include "compat.h"

#include "regs.h"
#include "intern.h"
#include "desc_constr.h"
#include "jr.h"
#include "error.h"
Y
Yuan Kang 已提交
56
#include "sg_sw_sec4.h"
Y
Yuan Kang 已提交
57
#include "key_gen.h"
58
#include "caamalg_desc.h"
59 60 61 62 63 64 65

/*
 * crypto alg
 */
#define CAAM_CRA_PRIORITY		3000
/* max key is sum of AES_MAX_KEY_SIZE, max split key size */
#define CAAM_MAX_KEY_SIZE		(AES_MAX_KEY_SIZE + \
66
					 CTR_RFC3686_NONCE_SIZE + \
67 68
					 SHA512_DIGEST_SIZE * 2)

69 70 71
#define AEAD_DESC_JOB_IO_LEN		(DESC_JOB_IO_LEN + CAAM_CMD_SZ * 2)
#define GCM_DESC_JOB_IO_LEN		(AEAD_DESC_JOB_IO_LEN + \
					 CAAM_CMD_SZ * 4)
72 73
#define AUTHENC_DESC_JOB_IO_LEN		(AEAD_DESC_JOB_IO_LEN + \
					 CAAM_CMD_SZ * 5)
74

75 76
#define CHACHAPOLY_DESC_JOB_IO_LEN	(AEAD_DESC_JOB_IO_LEN + CAAM_CMD_SZ * 6)

77 78
#define DESC_MAX_USED_BYTES		(CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN)
#define DESC_MAX_USED_LEN		(DESC_MAX_USED_BYTES / CAAM_CMD_SZ)
79

80 81 82 83 84
struct caam_alg_entry {
	int class1_alg_type;
	int class2_alg_type;
	bool rfc3686;
	bool geniv;
85
	bool nodkp;
86 87 88 89 90 91 92 93
};

struct caam_aead_alg {
	struct aead_alg aead;
	struct caam_alg_entry caam;
	bool registered;
};

94 95 96 97 98 99
struct caam_skcipher_alg {
	struct skcipher_alg skcipher;
	struct caam_alg_entry caam;
	bool registered;
};

100 101 102 103
/*
 * per-session context
 */
struct caam_ctx {
104 105
	u32 sh_desc_enc[DESC_MAX_USED_LEN];
	u32 sh_desc_dec[DESC_MAX_USED_LEN];
106
	u8 key[CAAM_MAX_KEY_SIZE];
107 108
	dma_addr_t sh_desc_enc_dma;
	dma_addr_t sh_desc_dec_dma;
Y
Yuan Kang 已提交
109
	dma_addr_t key_dma;
110
	enum dma_data_direction dir;
111
	struct device *jrdev;
112 113
	struct alginfo adata;
	struct alginfo cdata;
114 115 116
	unsigned int authsize;
};

117 118 119 120
static int aead_null_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
121
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
122
	u32 *desc;
123 124
	int rem_bytes = CAAM_DESC_BYTES_MAX - AEAD_DESC_JOB_IO_LEN -
			ctx->adata.keylen_pad;
125 126 127 128 129

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
130
	if (rem_bytes >= DESC_AEAD_NULL_ENC_LEN) {
131
		ctx->adata.key_inline = true;
132
		ctx->adata.key_virt = ctx->key;
133 134
	} else {
		ctx->adata.key_inline = false;
135
		ctx->adata.key_dma = ctx->key_dma;
136
	}
137

138
	/* aead_encrypt shared descriptor */
139
	desc = ctx->sh_desc_enc;
140 141
	cnstr_shdsc_aead_null_encap(desc, &ctx->adata, ctx->authsize,
				    ctrlpriv->era);
142
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
143
				   desc_bytes(desc), ctx->dir);
144 145 146 147 148

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
149
	if (rem_bytes >= DESC_AEAD_NULL_DEC_LEN) {
150
		ctx->adata.key_inline = true;
151
		ctx->adata.key_virt = ctx->key;
152 153
	} else {
		ctx->adata.key_inline = false;
154
		ctx->adata.key_dma = ctx->key_dma;
155
	}
156

157
	/* aead_decrypt shared descriptor */
158
	desc = ctx->sh_desc_dec;
159 160
	cnstr_shdsc_aead_null_decap(desc, &ctx->adata, ctx->authsize,
				    ctrlpriv->era);
161
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
162
				   desc_bytes(desc), ctx->dir);
163 164 165 166

	return 0;
}

167 168
static int aead_set_sh_desc(struct crypto_aead *aead)
{
169 170
	struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
						 struct caam_aead_alg, aead);
171
	unsigned int ivsize = crypto_aead_ivsize(aead);
172 173
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
174
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
175
	u32 ctx1_iv_off = 0;
176
	u32 *desc, *nonce = NULL;
177 178
	u32 inl_mask;
	unsigned int data_len[2];
179
	const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
180
			       OP_ALG_AAI_CTR_MOD128);
181
	const bool is_rfc3686 = alg->caam.rfc3686;
182

183 184 185
	if (!ctx->authsize)
		return 0;

186
	/* NULL encryption / decryption */
187
	if (!ctx->cdata.keylen)
188 189
		return aead_null_set_sh_desc(aead);

190 191 192 193 194 195 196 197 198 199 200 201
	/*
	 * AES-CTR needs to load IV in CONTEXT1 reg
	 * at an offset of 128bits (16bytes)
	 * CONTEXT1[255:128] = IV
	 */
	if (ctr_mode)
		ctx1_iv_off = 16;

	/*
	 * RFC3686 specific:
	 *	CONTEXT1[255:128] = {NONCE, IV, COUNTER}
	 */
202
	if (is_rfc3686) {
203
		ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
204 205 206
		nonce = (u32 *)((void *)ctx->key + ctx->adata.keylen_pad +
				ctx->cdata.keylen - CTR_RFC3686_NONCE_SIZE);
	}
207

208 209 210
	data_len[0] = ctx->adata.keylen_pad;
	data_len[1] = ctx->cdata.keylen;

211 212 213
	if (alg->caam.geniv)
		goto skip_enc;

214 215 216 217
	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
218 219 220 221 222 223 224
	if (desc_inline_query(DESC_AEAD_ENC_LEN +
			      (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
			      AUTHENC_DESC_JOB_IO_LEN, data_len, &inl_mask,
			      ARRAY_SIZE(data_len)) < 0)
		return -EINVAL;

	if (inl_mask & 1)
225
		ctx->adata.key_virt = ctx->key;
226
	else
227
		ctx->adata.key_dma = ctx->key_dma;
228 229

	if (inl_mask & 2)
230
		ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
231
	else
232
		ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
233 234 235

	ctx->adata.key_inline = !!(inl_mask & 1);
	ctx->cdata.key_inline = !!(inl_mask & 2);
236

237
	/* aead_encrypt shared descriptor */
238
	desc = ctx->sh_desc_enc;
239 240
	cnstr_shdsc_aead_encap(desc, &ctx->cdata, &ctx->adata, ivsize,
			       ctx->authsize, is_rfc3686, nonce, ctx1_iv_off,
241
			       false, ctrlpriv->era);
242
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
243
				   desc_bytes(desc), ctx->dir);
244

245
skip_enc:
246 247 248 249
	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
250 251 252 253 254 255 256
	if (desc_inline_query(DESC_AEAD_DEC_LEN +
			      (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
			      AUTHENC_DESC_JOB_IO_LEN, data_len, &inl_mask,
			      ARRAY_SIZE(data_len)) < 0)
		return -EINVAL;

	if (inl_mask & 1)
257
		ctx->adata.key_virt = ctx->key;
258
	else
259
		ctx->adata.key_dma = ctx->key_dma;
260 261

	if (inl_mask & 2)
262
		ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
263
	else
264
		ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
265 266 267

	ctx->adata.key_inline = !!(inl_mask & 1);
	ctx->cdata.key_inline = !!(inl_mask & 2);
268

269
	/* aead_decrypt shared descriptor */
270
	desc = ctx->sh_desc_dec;
271 272
	cnstr_shdsc_aead_decap(desc, &ctx->cdata, &ctx->adata, ivsize,
			       ctx->authsize, alg->caam.geniv, is_rfc3686,
273
			       nonce, ctx1_iv_off, false, ctrlpriv->era);
274
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
275
				   desc_bytes(desc), ctx->dir);
276

277 278 279
	if (!alg->caam.geniv)
		goto skip_givenc;

280 281 282 283
	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
284 285 286 287 288 289 290
	if (desc_inline_query(DESC_AEAD_GIVENC_LEN +
			      (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
			      AUTHENC_DESC_JOB_IO_LEN, data_len, &inl_mask,
			      ARRAY_SIZE(data_len)) < 0)
		return -EINVAL;

	if (inl_mask & 1)
291
		ctx->adata.key_virt = ctx->key;
292
	else
293
		ctx->adata.key_dma = ctx->key_dma;
294 295

	if (inl_mask & 2)
296
		ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
297
	else
298
		ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
299 300 301

	ctx->adata.key_inline = !!(inl_mask & 1);
	ctx->cdata.key_inline = !!(inl_mask & 2);
302 303

	/* aead_givencrypt shared descriptor */
304
	desc = ctx->sh_desc_enc;
305 306
	cnstr_shdsc_aead_givencap(desc, &ctx->cdata, &ctx->adata, ivsize,
				  ctx->authsize, is_rfc3686, nonce,
307
				  ctx1_iv_off, false, ctrlpriv->era);
308
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
309
				   desc_bytes(desc), ctx->dir);
310

311
skip_givenc:
312 313 314
	return 0;
}

Y
Yuan Kang 已提交
315
static int aead_setauthsize(struct crypto_aead *authenc,
316 317 318 319 320
				    unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(authenc);

	ctx->authsize = authsize;
321
	aead_set_sh_desc(authenc);
322 323 324 325

	return 0;
}

326 327 328 329
static int gcm_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
330
	unsigned int ivsize = crypto_aead_ivsize(aead);
331
	u32 *desc;
332 333
	int rem_bytes = CAAM_DESC_BYTES_MAX - GCM_DESC_JOB_IO_LEN -
			ctx->cdata.keylen;
334

335
	if (!ctx->cdata.keylen || !ctx->authsize)
336 337 338 339 340 341 342
		return 0;

	/*
	 * AES GCM encrypt shared descriptor
	 * Job Descriptor and Shared Descriptor
	 * must fit into the 64-word Descriptor h/w Buffer
	 */
343
	if (rem_bytes >= DESC_GCM_ENC_LEN) {
344
		ctx->cdata.key_inline = true;
345
		ctx->cdata.key_virt = ctx->key;
346 347
	} else {
		ctx->cdata.key_inline = false;
348
		ctx->cdata.key_dma = ctx->key_dma;
349
	}
350 351

	desc = ctx->sh_desc_enc;
352
	cnstr_shdsc_gcm_encap(desc, &ctx->cdata, ivsize, ctx->authsize, false);
353
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
354
				   desc_bytes(desc), ctx->dir);
355 356 357 358 359

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
360
	if (rem_bytes >= DESC_GCM_DEC_LEN) {
361
		ctx->cdata.key_inline = true;
362
		ctx->cdata.key_virt = ctx->key;
363 364
	} else {
		ctx->cdata.key_inline = false;
365
		ctx->cdata.key_dma = ctx->key_dma;
366
	}
367 368

	desc = ctx->sh_desc_dec;
369
	cnstr_shdsc_gcm_decap(desc, &ctx->cdata, ivsize, ctx->authsize, false);
370
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
371
				   desc_bytes(desc), ctx->dir);
372 373 374 375 376 377 378 379 380 381 382 383 384 385

	return 0;
}

static int gcm_setauthsize(struct crypto_aead *authenc, unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(authenc);

	ctx->authsize = authsize;
	gcm_set_sh_desc(authenc);

	return 0;
}

386 387 388 389
static int rfc4106_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
390
	unsigned int ivsize = crypto_aead_ivsize(aead);
391
	u32 *desc;
392 393
	int rem_bytes = CAAM_DESC_BYTES_MAX - GCM_DESC_JOB_IO_LEN -
			ctx->cdata.keylen;
394

395
	if (!ctx->cdata.keylen || !ctx->authsize)
396 397 398 399 400 401 402
		return 0;

	/*
	 * RFC4106 encrypt shared descriptor
	 * Job Descriptor and Shared Descriptor
	 * must fit into the 64-word Descriptor h/w Buffer
	 */
403
	if (rem_bytes >= DESC_RFC4106_ENC_LEN) {
404
		ctx->cdata.key_inline = true;
405
		ctx->cdata.key_virt = ctx->key;
406 407
	} else {
		ctx->cdata.key_inline = false;
408
		ctx->cdata.key_dma = ctx->key_dma;
409
	}
410 411

	desc = ctx->sh_desc_enc;
412 413
	cnstr_shdsc_rfc4106_encap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
414
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
415
				   desc_bytes(desc), ctx->dir);
416 417 418 419 420

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
421
	if (rem_bytes >= DESC_RFC4106_DEC_LEN) {
422
		ctx->cdata.key_inline = true;
423
		ctx->cdata.key_virt = ctx->key;
424 425
	} else {
		ctx->cdata.key_inline = false;
426
		ctx->cdata.key_dma = ctx->key_dma;
427
	}
428 429

	desc = ctx->sh_desc_dec;
430 431
	cnstr_shdsc_rfc4106_decap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
432
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
433
				   desc_bytes(desc), ctx->dir);
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

	return 0;
}

static int rfc4106_setauthsize(struct crypto_aead *authenc,
			       unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(authenc);

	ctx->authsize = authsize;
	rfc4106_set_sh_desc(authenc);

	return 0;
}

449 450 451 452
static int rfc4543_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
453
	unsigned int ivsize = crypto_aead_ivsize(aead);
454
	u32 *desc;
455 456
	int rem_bytes = CAAM_DESC_BYTES_MAX - GCM_DESC_JOB_IO_LEN -
			ctx->cdata.keylen;
457

458
	if (!ctx->cdata.keylen || !ctx->authsize)
459 460 461 462 463 464 465
		return 0;

	/*
	 * RFC4543 encrypt shared descriptor
	 * Job Descriptor and Shared Descriptor
	 * must fit into the 64-word Descriptor h/w Buffer
	 */
466
	if (rem_bytes >= DESC_RFC4543_ENC_LEN) {
467
		ctx->cdata.key_inline = true;
468
		ctx->cdata.key_virt = ctx->key;
469 470
	} else {
		ctx->cdata.key_inline = false;
471
		ctx->cdata.key_dma = ctx->key_dma;
472
	}
473 474

	desc = ctx->sh_desc_enc;
475 476
	cnstr_shdsc_rfc4543_encap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
477
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
478
				   desc_bytes(desc), ctx->dir);
479 480 481 482 483

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
484
	if (rem_bytes >= DESC_RFC4543_DEC_LEN) {
485
		ctx->cdata.key_inline = true;
486
		ctx->cdata.key_virt = ctx->key;
487 488
	} else {
		ctx->cdata.key_inline = false;
489
		ctx->cdata.key_dma = ctx->key_dma;
490
	}
491 492

	desc = ctx->sh_desc_dec;
493 494
	cnstr_shdsc_rfc4543_decap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
495
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
496
				   desc_bytes(desc), ctx->dir);
497

498 499
	return 0;
}
500

501 502 503 504
static int rfc4543_setauthsize(struct crypto_aead *authenc,
			       unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(authenc);
505

506 507
	ctx->authsize = authsize;
	rfc4543_set_sh_desc(authenc);
508

509 510
	return 0;
}
511

512 513 514 515 516 517 518 519 520 521 522 523
static int chachapoly_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
	unsigned int ivsize = crypto_aead_ivsize(aead);
	u32 *desc;

	if (!ctx->cdata.keylen || !ctx->authsize)
		return 0;

	desc = ctx->sh_desc_enc;
	cnstr_shdsc_chachapoly(desc, &ctx->cdata, &ctx->adata, ivsize,
524
			       ctx->authsize, true, false);
525 526 527 528 529
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
				   desc_bytes(desc), ctx->dir);

	desc = ctx->sh_desc_dec;
	cnstr_shdsc_chachapoly(desc, &ctx->cdata, &ctx->adata, ivsize,
530
			       ctx->authsize, false, false);
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
				   desc_bytes(desc), ctx->dir);

	return 0;
}

static int chachapoly_setauthsize(struct crypto_aead *aead,
				  unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);

	if (authsize != POLY1305_DIGEST_SIZE)
		return -EINVAL;

	ctx->authsize = authsize;
	return chachapoly_set_sh_desc(aead);
}

static int chachapoly_setkey(struct crypto_aead *aead, const u8 *key,
			     unsigned int keylen)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	unsigned int ivsize = crypto_aead_ivsize(aead);
	unsigned int saltlen = CHACHAPOLY_IV_SIZE - ivsize;

556
	if (keylen != CHACHA_KEY_SIZE + saltlen) {
557 558 559 560 561 562 563 564 565 566
		crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
		return -EINVAL;
	}

	ctx->cdata.key_virt = key;
	ctx->cdata.keylen = keylen - saltlen;

	return chachapoly_set_sh_desc(aead);
}

Y
Yuan Kang 已提交
567
static int aead_setkey(struct crypto_aead *aead,
568 569 570 571
			       const u8 *key, unsigned int keylen)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
572
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
573
	struct crypto_authenc_keys keys;
574 575
	int ret = 0;

576
	if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
577 578
		goto badkey;

579
	dev_dbg(jrdev, "keylen %d enckeylen %d authkeylen %d\n",
580 581
	       keys.authkeylen + keys.enckeylen, keys.enckeylen,
	       keys.authkeylen);
582 583
	print_hex_dump_debug("key in @"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
584

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
	/*
	 * If DKP is supported, use it in the shared descriptor to generate
	 * the split key.
	 */
	if (ctrlpriv->era >= 6) {
		ctx->adata.keylen = keys.authkeylen;
		ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
						      OP_ALG_ALGSEL_MASK);

		if (ctx->adata.keylen_pad + keys.enckeylen > CAAM_MAX_KEY_SIZE)
			goto badkey;

		memcpy(ctx->key, keys.authkey, keys.authkeylen);
		memcpy(ctx->key + ctx->adata.keylen_pad, keys.enckey,
		       keys.enckeylen);
		dma_sync_single_for_device(jrdev, ctx->key_dma,
					   ctx->adata.keylen_pad +
					   keys.enckeylen, ctx->dir);
		goto skip_split_key;
	}

606 607 608
	ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, keys.authkey,
			    keys.authkeylen, CAAM_MAX_KEY_SIZE -
			    keys.enckeylen);
609 610 611 612 613
	if (ret) {
		goto badkey;
	}

	/* postpend encryption key to auth split key */
614
	memcpy(ctx->key + ctx->adata.keylen_pad, keys.enckey, keys.enckeylen);
615
	dma_sync_single_for_device(jrdev, ctx->key_dma, ctx->adata.keylen_pad +
616
				   keys.enckeylen, ctx->dir);
617 618 619 620

	print_hex_dump_debug("ctx.key@"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
			     ctx->adata.keylen_pad + keys.enckeylen, 1);
621 622

skip_split_key:
623
	ctx->cdata.keylen = keys.enckeylen;
624
	memzero_explicit(&keys, sizeof(keys));
625
	return aead_set_sh_desc(aead);
626 627
badkey:
	crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
628
	memzero_explicit(&keys, sizeof(keys));
629 630 631
	return -EINVAL;
}

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
static int des3_aead_setkey(struct crypto_aead *aead, const u8 *key,
			    unsigned int keylen)
{
	struct crypto_authenc_keys keys;
	u32 flags;
	int err;

	err = crypto_authenc_extractkeys(&keys, key, keylen);
	if (unlikely(err))
		goto badkey;

	err = -EINVAL;
	if (keys.enckeylen != DES3_EDE_KEY_SIZE)
		goto badkey;

	flags = crypto_aead_get_flags(aead);
	err = __des3_verify_key(&flags, keys.enckey);
	if (unlikely(err)) {
		crypto_aead_set_flags(aead, flags);
		goto out;
	}

	err = aead_setkey(aead, key, keylen);

out:
	memzero_explicit(&keys, sizeof(keys));
	return err;

badkey:
	crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
	goto out;
}

665 666 667 668 669
static int gcm_setkey(struct crypto_aead *aead,
		      const u8 *key, unsigned int keylen)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
I
Iuliana Prodan 已提交
670 671 672 673 674 675 676
	int err;

	err = aes_check_keylen(keylen);
	if (err) {
		crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
		return err;
	}
677

678 679
	print_hex_dump_debug("key in @"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
680 681

	memcpy(ctx->key, key, keylen);
682
	dma_sync_single_for_device(jrdev, ctx->key_dma, keylen, ctx->dir);
683
	ctx->cdata.keylen = keylen;
684

685
	return gcm_set_sh_desc(aead);
686 687
}

688 689 690 691 692
static int rfc4106_setkey(struct crypto_aead *aead,
			  const u8 *key, unsigned int keylen)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
I
Iuliana Prodan 已提交
693
	int err;
694

I
Iuliana Prodan 已提交
695 696 697 698 699
	err = aes_check_keylen(keylen - 4);
	if (err) {
		crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
		return err;
	}
700

701 702
	print_hex_dump_debug("key in @"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
703 704 705 706 707 708 709

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

	/*
	 * The last four bytes of the key material are used as the salt value
	 * in the nonce. Update the AES key length.
	 */
710
	ctx->cdata.keylen = keylen - 4;
711
	dma_sync_single_for_device(jrdev, ctx->key_dma, ctx->cdata.keylen,
712
				   ctx->dir);
713
	return rfc4106_set_sh_desc(aead);
714 715
}

716 717 718 719 720
static int rfc4543_setkey(struct crypto_aead *aead,
			  const u8 *key, unsigned int keylen)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
I
Iuliana Prodan 已提交
721
	int err;
722

I
Iuliana Prodan 已提交
723 724 725 726 727
	err = aes_check_keylen(keylen - 4);
	if (err) {
		crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
		return err;
	}
728

729 730
	print_hex_dump_debug("key in @"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
731 732 733 734 735 736 737

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

	/*
	 * The last four bytes of the key material are used as the salt value
	 * in the nonce. Update the AES key length.
	 */
738
	ctx->cdata.keylen = keylen - 4;
739
	dma_sync_single_for_device(jrdev, ctx->key_dma, ctx->cdata.keylen,
740
				   ctx->dir);
741
	return rfc4543_set_sh_desc(aead);
742 743
}

744
static int skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
I
Iuliana Prodan 已提交
745
			   unsigned int keylen, const u32 ctx1_iv_off)
Y
Yuan Kang 已提交
746
{
747 748 749 750
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
	struct caam_skcipher_alg *alg =
		container_of(crypto_skcipher_alg(skcipher), typeof(*alg),
			     skcipher);
Y
Yuan Kang 已提交
751
	struct device *jrdev = ctx->jrdev;
752
	unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
753
	u32 *desc;
754
	const bool is_rfc3686 = alg->caam.rfc3686;
Y
Yuan Kang 已提交
755

756 757
	print_hex_dump_debug("key in @"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
758

759
	ctx->cdata.keylen = keylen;
760
	ctx->cdata.key_virt = key;
761
	ctx->cdata.key_inline = true;
Y
Yuan Kang 已提交
762

763
	/* skcipher_encrypt shared descriptor */
Y
Yuan Kang 已提交
764
	desc = ctx->sh_desc_enc;
765 766
	cnstr_shdsc_skcipher_encap(desc, &ctx->cdata, ivsize, is_rfc3686,
				   ctx1_iv_off);
767
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
768
				   desc_bytes(desc), ctx->dir);
769

770
	/* skcipher_decrypt shared descriptor */
Y
Yuan Kang 已提交
771
	desc = ctx->sh_desc_dec;
772 773
	cnstr_shdsc_skcipher_decap(desc, &ctx->cdata, ivsize, is_rfc3686,
				   ctx1_iv_off);
774
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
775
				   desc_bytes(desc), ctx->dir);
Y
Yuan Kang 已提交
776

777
	return 0;
Y
Yuan Kang 已提交
778 779
}

I
Iuliana Prodan 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
static int aes_skcipher_setkey(struct crypto_skcipher *skcipher,
			       const u8 *key, unsigned int keylen)
{
	int err;

	err = aes_check_keylen(keylen);
	if (err) {
		crypto_skcipher_set_flags(skcipher,
					  CRYPTO_TFM_RES_BAD_KEY_LEN);
		return err;
	}

	return skcipher_setkey(skcipher, key, keylen, 0);
}

static int rfc3686_skcipher_setkey(struct crypto_skcipher *skcipher,
				   const u8 *key, unsigned int keylen)
{
	u32 ctx1_iv_off;
	int err;

	/*
	 * RFC3686 specific:
	 *	| CONTEXT1[255:128] = {NONCE, IV, COUNTER}
	 *	| *key = {KEY, NONCE}
	 */
	ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
	keylen -= CTR_RFC3686_NONCE_SIZE;

	err = aes_check_keylen(keylen);
	if (err) {
		crypto_skcipher_set_flags(skcipher,
					  CRYPTO_TFM_RES_BAD_KEY_LEN);
		return err;
	}

	return skcipher_setkey(skcipher, key, keylen, ctx1_iv_off);
}

static int ctr_skcipher_setkey(struct crypto_skcipher *skcipher,
			       const u8 *key, unsigned int keylen)
{
	u32 ctx1_iv_off;
	int err;

	/*
	 * AES-CTR needs to load IV in CONTEXT1 reg
	 * at an offset of 128bits (16bytes)
	 * CONTEXT1[255:128] = IV
	 */
	ctx1_iv_off = 16;

	err = aes_check_keylen(keylen);
	if (err) {
		crypto_skcipher_set_flags(skcipher,
					  CRYPTO_TFM_RES_BAD_KEY_LEN);
		return err;
	}

	return skcipher_setkey(skcipher, key, keylen, ctx1_iv_off);
}

static int arc4_skcipher_setkey(struct crypto_skcipher *skcipher,
				const u8 *key, unsigned int keylen)
{
	return skcipher_setkey(skcipher, key, keylen, 0);
}

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
static int des_skcipher_setkey(struct crypto_skcipher *skcipher,
			       const u8 *key, unsigned int keylen)
{
	u32 tmp[DES3_EDE_EXPKEY_WORDS];
	struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);

	if (keylen == DES3_EDE_KEY_SIZE &&
	    __des3_ede_setkey(tmp, &tfm->crt_flags, key, DES3_EDE_KEY_SIZE)) {
		return -EINVAL;
	}

	if (!des_ekey(tmp, key) && (crypto_skcipher_get_flags(skcipher) &
	    CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) {
		crypto_skcipher_set_flags(skcipher,
					  CRYPTO_TFM_RES_WEAK_KEY);
		return -EINVAL;
	}

I
Iuliana Prodan 已提交
866
	return skcipher_setkey(skcipher, key, keylen, 0);
867 868
}

869 870
static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
			       unsigned int keylen)
871
{
872
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
873
	struct device *jrdev = ctx->jrdev;
874
	u32 *desc;
875 876

	if (keylen != 2 * AES_MIN_KEY_SIZE  && keylen != 2 * AES_MAX_KEY_SIZE) {
877
		crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
878 879 880 881
		dev_err(jrdev, "key size mismatch\n");
		return -EINVAL;
	}

882
	ctx->cdata.keylen = keylen;
883
	ctx->cdata.key_virt = key;
884
	ctx->cdata.key_inline = true;
885

886
	/* xts_skcipher_encrypt shared descriptor */
887
	desc = ctx->sh_desc_enc;
888
	cnstr_shdsc_xts_skcipher_encap(desc, &ctx->cdata);
889
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
890
				   desc_bytes(desc), ctx->dir);
891

892
	/* xts_skcipher_decrypt shared descriptor */
893
	desc = ctx->sh_desc_dec;
894
	cnstr_shdsc_xts_skcipher_decap(desc, &ctx->cdata);
895
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
896
				   desc_bytes(desc), ctx->dir);
897 898 899 900

	return 0;
}

901
/*
902
 * aead_edesc - s/w-extended aead descriptor
903 904
 * @src_nents: number of segments in input s/w scatterlist
 * @dst_nents: number of segments in output s/w scatterlist
905 906
 * @mapped_src_nents: number of segments in input h/w link table
 * @mapped_dst_nents: number of segments in output h/w link table
Y
Yuan Kang 已提交
907 908
 * @sec4_sg_bytes: length of dma mapped sec4_sg space
 * @sec4_sg_dma: bus physical mapped address of h/w link table
909
 * @sec4_sg: pointer to h/w link table
910 911
 * @hw_desc: the h/w job descriptor followed by any referenced link tables
 */
Y
Yuan Kang 已提交
912
struct aead_edesc {
913 914
	int src_nents;
	int dst_nents;
915 916
	int mapped_src_nents;
	int mapped_dst_nents;
Y
Yuan Kang 已提交
917 918 919
	int sec4_sg_bytes;
	dma_addr_t sec4_sg_dma;
	struct sec4_sg_entry *sec4_sg;
920
	u32 hw_desc[];
921 922
};

Y
Yuan Kang 已提交
923
/*
924
 * skcipher_edesc - s/w-extended skcipher descriptor
925 926
 * @src_nents: number of segments in input s/w scatterlist
 * @dst_nents: number of segments in output s/w scatterlist
927 928
 * @mapped_src_nents: number of segments in input h/w link table
 * @mapped_dst_nents: number of segments in output h/w link table
Y
Yuan Kang 已提交
929
 * @iv_dma: dma address of iv for checking continuity and link table
Y
Yuan Kang 已提交
930 931
 * @sec4_sg_bytes: length of dma mapped sec4_sg space
 * @sec4_sg_dma: bus physical mapped address of h/w link table
932
 * @sec4_sg: pointer to h/w link table
Y
Yuan Kang 已提交
933
 * @hw_desc: the h/w job descriptor followed by any referenced link tables
934
 *	     and IV
Y
Yuan Kang 已提交
935
 */
936
struct skcipher_edesc {
Y
Yuan Kang 已提交
937 938
	int src_nents;
	int dst_nents;
939 940
	int mapped_src_nents;
	int mapped_dst_nents;
Y
Yuan Kang 已提交
941
	dma_addr_t iv_dma;
Y
Yuan Kang 已提交
942 943 944
	int sec4_sg_bytes;
	dma_addr_t sec4_sg_dma;
	struct sec4_sg_entry *sec4_sg;
Y
Yuan Kang 已提交
945 946 947
	u32 hw_desc[0];
};

948
static void caam_unmap(struct device *dev, struct scatterlist *src,
Y
Yuan Kang 已提交
949
		       struct scatterlist *dst, int src_nents,
950
		       int dst_nents,
951
		       dma_addr_t iv_dma, int ivsize, dma_addr_t sec4_sg_dma,
Y
Yuan Kang 已提交
952
		       int sec4_sg_bytes)
953
{
Y
Yuan Kang 已提交
954
	if (dst != src) {
955 956
		if (src_nents)
			dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
957 958
		if (dst_nents)
			dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
959
	} else {
960
		dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
961 962
	}

963
	if (iv_dma)
964
		dma_unmap_single(dev, iv_dma, ivsize, DMA_BIDIRECTIONAL);
Y
Yuan Kang 已提交
965 966
	if (sec4_sg_bytes)
		dma_unmap_single(dev, sec4_sg_dma, sec4_sg_bytes,
967 968 969
				 DMA_TO_DEVICE);
}

970 971 972
static void aead_unmap(struct device *dev,
		       struct aead_edesc *edesc,
		       struct aead_request *req)
973 974
{
	caam_unmap(dev, req->src, req->dst,
975
		   edesc->src_nents, edesc->dst_nents, 0, 0,
976 977 978
		   edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
}

979 980
static void skcipher_unmap(struct device *dev, struct skcipher_edesc *edesc,
			   struct skcipher_request *req)
Y
Yuan Kang 已提交
981
{
982 983
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
984 985

	caam_unmap(dev, req->src, req->dst,
986
		   edesc->src_nents, edesc->dst_nents,
987
		   edesc->iv_dma, ivsize,
Y
Yuan Kang 已提交
988
		   edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
Y
Yuan Kang 已提交
989 990
}

Y
Yuan Kang 已提交
991
static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
992 993
				   void *context)
{
Y
Yuan Kang 已提交
994 995
	struct aead_request *req = context;
	struct aead_edesc *edesc;
996
	int ecode = 0;
997

998
	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
999 1000 1001 1002

	edesc = container_of(desc, struct aead_edesc, hw_desc[0]);

	if (err)
1003
		ecode = caam_jr_strstatus(jrdev, err);
1004 1005 1006 1007 1008

	aead_unmap(jrdev, edesc, req);

	kfree(edesc);

1009
	aead_request_complete(req, ecode);
1010 1011
}

Y
Yuan Kang 已提交
1012
static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
1013 1014
				   void *context)
{
Y
Yuan Kang 已提交
1015 1016
	struct aead_request *req = context;
	struct aead_edesc *edesc;
1017
	int ecode = 0;
1018

1019
	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
1020 1021 1022 1023

	edesc = container_of(desc, struct aead_edesc, hw_desc[0]);

	if (err)
1024
		ecode = caam_jr_strstatus(jrdev, err);
1025 1026 1027 1028 1029

	aead_unmap(jrdev, edesc, req);

	kfree(edesc);

1030
	aead_request_complete(req, ecode);
1031 1032
}

1033 1034
static void skcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
				  void *context)
Y
Yuan Kang 已提交
1035
{
1036 1037 1038 1039
	struct skcipher_request *req = context;
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	int ivsize = crypto_skcipher_ivsize(skcipher);
1040
	int ecode = 0;
Y
Yuan Kang 已提交
1041

1042
	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
Y
Yuan Kang 已提交
1043

1044
	edesc = container_of(desc, struct skcipher_edesc, hw_desc[0]);
Y
Yuan Kang 已提交
1045

1046
	if (err)
1047
		ecode = caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
1048

1049
	skcipher_unmap(jrdev, edesc, req);
1050 1051

	/*
1052
	 * The crypto API expects us to set the IV (req->iv) to the last
1053 1054
	 * ciphertext block (CBC mode) or last counter (CTR mode).
	 * This is used e.g. by the CTS mode.
1055
	 */
1056
	if (ivsize && !ecode) {
1057 1058
		memcpy(req->iv, (u8 *)edesc->sec4_sg + edesc->sec4_sg_bytes,
		       ivsize);
1059 1060 1061
		print_hex_dump_debug("dstiv  @"__stringify(__LINE__)": ",
				     DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
				     edesc->src_nents > 1 ? 100 : ivsize, 1);
1062
	}
1063

1064
	caam_dump_sg("dst    @" __stringify(__LINE__)": ",
1065 1066 1067
		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);

Y
Yuan Kang 已提交
1068 1069
	kfree(edesc);

1070
	skcipher_request_complete(req, ecode);
Y
Yuan Kang 已提交
1071 1072
}

1073 1074
static void skcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
				  void *context)
Y
Yuan Kang 已提交
1075
{
1076 1077 1078 1079
	struct skcipher_request *req = context;
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	int ivsize = crypto_skcipher_ivsize(skcipher);
1080
	int ecode = 0;
Y
Yuan Kang 已提交
1081

1082
	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
Y
Yuan Kang 已提交
1083

1084
	edesc = container_of(desc, struct skcipher_edesc, hw_desc[0]);
1085
	if (err)
1086
		ecode = caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
1087

1088 1089
	skcipher_unmap(jrdev, edesc, req);

1090 1091 1092 1093 1094
	/*
	 * The crypto API expects us to set the IV (req->iv) to the last
	 * ciphertext block (CBC mode) or last counter (CTR mode).
	 * This is used e.g. by the CTS mode.
	 */
1095
	if (ivsize && !ecode) {
1096 1097 1098 1099 1100 1101 1102 1103
		memcpy(req->iv, (u8 *)edesc->sec4_sg + edesc->sec4_sg_bytes,
		       ivsize);

		print_hex_dump_debug("dstiv  @" __stringify(__LINE__)": ",
				     DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
				     ivsize, 1);
	}

1104
	caam_dump_sg("dst    @" __stringify(__LINE__)": ",
1105
		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
1106
		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
Y
Yuan Kang 已提交
1107 1108 1109

	kfree(edesc);

1110
	skcipher_request_complete(req, ecode);
Y
Yuan Kang 已提交
1111 1112
}

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
/*
 * Fill in aead job descriptor
 */
static void init_aead_job(struct aead_request *req,
			  struct aead_edesc *edesc,
			  bool all_contig, bool encrypt)
{
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	int authsize = ctx->authsize;
	u32 *desc = edesc->hw_desc;
	u32 out_options, in_options;
	dma_addr_t dst_dma, src_dma;
	int len, sec4_sg_index = 0;
	dma_addr_t ptr;
	u32 *sh_desc;

	sh_desc = encrypt ? ctx->sh_desc_enc : ctx->sh_desc_dec;
	ptr = encrypt ? ctx->sh_desc_enc_dma : ctx->sh_desc_dec_dma;

	len = desc_len(sh_desc);
	init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);

	if (all_contig) {
1137 1138
		src_dma = edesc->mapped_src_nents ? sg_dma_address(req->src) :
						    0;
1139 1140 1141
		in_options = 0;
	} else {
		src_dma = edesc->sec4_sg_dma;
1142
		sec4_sg_index += edesc->mapped_src_nents;
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
		in_options = LDST_SGF;
	}

	append_seq_in_ptr(desc, src_dma, req->assoclen + req->cryptlen,
			  in_options);

	dst_dma = src_dma;
	out_options = in_options;

	if (unlikely(req->src != req->dst)) {
1153
		if (!edesc->mapped_dst_nents) {
1154
			dst_dma = 0;
1155
			out_options = 0;
1156
		} else if (edesc->mapped_dst_nents == 1) {
1157
			dst_dma = sg_dma_address(req->dst);
1158
			out_options = 0;
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
		} else {
			dst_dma = edesc->sec4_sg_dma +
				  sec4_sg_index *
				  sizeof(struct sec4_sg_entry);
			out_options = LDST_SGF;
		}
	}

	if (encrypt)
		append_seq_out_ptr(desc, dst_dma,
				   req->assoclen + req->cryptlen + authsize,
				   out_options);
	else
		append_seq_out_ptr(desc, dst_dma,
				   req->assoclen + req->cryptlen - authsize,
				   out_options);
}

static void init_gcm_job(struct aead_request *req,
			 struct aead_edesc *edesc,
			 bool all_contig, bool encrypt)
{
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	unsigned int ivsize = crypto_aead_ivsize(aead);
	u32 *desc = edesc->hw_desc;
1185
	bool generic_gcm = (ivsize == GCM_AES_IV_SIZE);
1186 1187 1188
	unsigned int last;

	init_aead_job(req, edesc, all_contig, encrypt);
1189
	append_math_add_imm_u32(desc, REG3, ZERO, IMM, req->assoclen);
1190 1191 1192 1193 1194 1195 1196 1197

	/* BUG This should not be specific to generic GCM. */
	last = 0;
	if (encrypt && generic_gcm && !(req->assoclen + req->cryptlen))
		last = FIFOLD_TYPE_LAST1;

	/* Read GCM IV */
	append_cmd(desc, CMD_FIFO_LOAD | FIFOLD_CLASS_CLASS1 | IMMEDIATE |
1198
			 FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1 | GCM_AES_IV_SIZE | last);
1199 1200
	/* Append Salt */
	if (!generic_gcm)
1201
		append_data(desc, ctx->key + ctx->cdata.keylen, 4);
1202 1203 1204 1205 1206
	/* Append IV */
	append_data(desc, req->iv, ivsize);
	/* End of blank commands */
}

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 1237 1238 1239 1240
static void init_chachapoly_job(struct aead_request *req,
				struct aead_edesc *edesc, bool all_contig,
				bool encrypt)
{
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	unsigned int ivsize = crypto_aead_ivsize(aead);
	unsigned int assoclen = req->assoclen;
	u32 *desc = edesc->hw_desc;
	u32 ctx_iv_off = 4;

	init_aead_job(req, edesc, all_contig, encrypt);

	if (ivsize != CHACHAPOLY_IV_SIZE) {
		/* IPsec specific: CONTEXT1[223:128] = {NONCE, IV} */
		ctx_iv_off += 4;

		/*
		 * The associated data comes already with the IV but we need
		 * to skip it when we authenticate or encrypt...
		 */
		assoclen -= ivsize;
	}

	append_math_add_imm_u32(desc, REG3, ZERO, IMM, assoclen);

	/*
	 * For IPsec load the IV further in the same register.
	 * For RFC7539 simply load the 12 bytes nonce in a single operation
	 */
	append_load_as_imm(desc, req->iv, ivsize, LDST_CLASS_1_CCB |
			   LDST_SRCDST_BYTE_CONTEXT |
			   ctx_iv_off << LDST_OFFSET_SHIFT);
}

1241 1242 1243
static void init_authenc_job(struct aead_request *req,
			     struct aead_edesc *edesc,
			     bool all_contig, bool encrypt)
1244 1245
{
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1246 1247 1248
	struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
						 struct caam_aead_alg, aead);
	unsigned int ivsize = crypto_aead_ivsize(aead);
1249
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
1250
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
1251
	const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
1252 1253
			       OP_ALG_AAI_CTR_MOD128);
	const bool is_rfc3686 = alg->caam.rfc3686;
1254
	u32 *desc = edesc->hw_desc;
1255
	u32 ivoffset = 0;
1256

1257 1258 1259 1260 1261 1262 1263
	/*
	 * AES-CTR needs to load IV in CONTEXT1 reg
	 * at an offset of 128bits (16bytes)
	 * CONTEXT1[255:128] = IV
	 */
	if (ctr_mode)
		ivoffset = 16;
1264

1265 1266 1267 1268 1269 1270
	/*
	 * RFC3686 specific:
	 *	CONTEXT1[255:128] = {NONCE, IV, COUNTER}
	 */
	if (is_rfc3686)
		ivoffset = 16 + CTR_RFC3686_NONCE_SIZE;
1271

1272
	init_aead_job(req, edesc, all_contig, encrypt);
1273

1274 1275 1276 1277 1278 1279 1280 1281 1282
	/*
	 * {REG3, DPOVRD} = assoclen, depending on whether MATH command supports
	 * having DPOVRD as destination.
	 */
	if (ctrlpriv->era < 3)
		append_math_add_imm_u32(desc, REG3, ZERO, IMM, req->assoclen);
	else
		append_math_add_imm_u32(desc, DPOVRD, ZERO, IMM, req->assoclen);

1283
	if (ivsize && ((is_rfc3686 && encrypt) || !alg->caam.geniv))
1284 1285 1286 1287
		append_load_as_imm(desc, req->iv, ivsize,
				   LDST_CLASS_1_CCB |
				   LDST_SRCDST_BYTE_CONTEXT |
				   (ivoffset << LDST_OFFSET_SHIFT));
1288 1289
}

Y
Yuan Kang 已提交
1290
/*
1291
 * Fill in skcipher job descriptor
Y
Yuan Kang 已提交
1292
 */
1293 1294 1295
static void init_skcipher_job(struct skcipher_request *req,
			      struct skcipher_edesc *edesc,
			      const bool encrypt)
Y
Yuan Kang 已提交
1296
{
1297 1298
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
1299
	struct device *jrdev = ctx->jrdev;
1300
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
1301
	u32 *desc = edesc->hw_desc;
1302
	u32 *sh_desc;
1303 1304 1305
	u32 in_options = 0, out_options = 0;
	dma_addr_t src_dma, dst_dma, ptr;
	int len, sec4_sg_index = 0;
Y
Yuan Kang 已提交
1306

1307 1308 1309
	print_hex_dump_debug("presciv@"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, req->iv, ivsize, 1);
	dev_dbg(jrdev, "asked=%d, cryptlen%d\n",
1310
	       (int)edesc->src_nents > 1 ? 100 : req->cryptlen, req->cryptlen);
1311

1312
	caam_dump_sg("src    @" __stringify(__LINE__)": ",
1313
		     DUMP_PREFIX_ADDRESS, 16, 4, req->src,
1314 1315 1316 1317
		     edesc->src_nents > 1 ? 100 : req->cryptlen, 1);

	sh_desc = encrypt ? ctx->sh_desc_enc : ctx->sh_desc_dec;
	ptr = encrypt ? ctx->sh_desc_enc_dma : ctx->sh_desc_dec_dma;
Y
Yuan Kang 已提交
1318 1319 1320 1321

	len = desc_len(sh_desc);
	init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);

1322 1323 1324 1325 1326 1327 1328 1329 1330
	if (ivsize || edesc->mapped_src_nents > 1) {
		src_dma = edesc->sec4_sg_dma;
		sec4_sg_index = edesc->mapped_src_nents + !!ivsize;
		in_options = LDST_SGF;
	} else {
		src_dma = sg_dma_address(req->src);
	}

	append_seq_in_ptr(desc, src_dma, req->cryptlen + ivsize, in_options);
Y
Yuan Kang 已提交
1331 1332

	if (likely(req->src == req->dst)) {
1333 1334
		dst_dma = src_dma + !!ivsize * sizeof(struct sec4_sg_entry);
		out_options = in_options;
1335
	} else if (!ivsize && edesc->mapped_dst_nents == 1) {
1336
		dst_dma = sg_dma_address(req->dst);
Y
Yuan Kang 已提交
1337
	} else {
1338 1339 1340
		dst_dma = edesc->sec4_sg_dma + sec4_sg_index *
			  sizeof(struct sec4_sg_entry);
		out_options = LDST_SGF;
Y
Yuan Kang 已提交
1341
	}
1342

1343
	append_seq_out_ptr(desc, dst_dma, req->cryptlen + ivsize, out_options);
Y
Yuan Kang 已提交
1344 1345
}

1346
/*
1347
 * allocate and map the aead extended descriptor
1348
 */
1349 1350 1351
static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
					   int desc_bytes, bool *all_contig_ptr,
					   bool encrypt)
1352
{
Y
Yuan Kang 已提交
1353
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1354 1355
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1356 1357
	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
		       GFP_KERNEL : GFP_ATOMIC;
1358
	int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
1359
	int src_len, dst_len = 0;
Y
Yuan Kang 已提交
1360
	struct aead_edesc *edesc;
1361
	int sec4_sg_index, sec4_sg_len, sec4_sg_bytes;
1362
	unsigned int authsize = ctx->authsize;
1363

1364
	if (unlikely(req->dst != req->src)) {
1365 1366 1367 1368
		src_len = req->assoclen + req->cryptlen;
		dst_len = src_len + (encrypt ? authsize : (-authsize));

		src_nents = sg_nents_for_len(req->src, src_len);
1369 1370
		if (unlikely(src_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
1371
				src_len);
1372 1373 1374
			return ERR_PTR(src_nents);
		}

1375
		dst_nents = sg_nents_for_len(req->dst, dst_len);
1376 1377
		if (unlikely(dst_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in dst S/G\n",
1378
				dst_len);
1379 1380
			return ERR_PTR(dst_nents);
		}
1381
	} else {
1382 1383 1384 1385
		src_len = req->assoclen + req->cryptlen +
			  (encrypt ? authsize : 0);

		src_nents = sg_nents_for_len(req->src, src_len);
1386 1387
		if (unlikely(src_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
1388
				src_len);
1389 1390
			return ERR_PTR(src_nents);
		}
1391
	}
1392

1393
	if (likely(req->src == req->dst)) {
1394 1395 1396
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_BIDIRECTIONAL);
		if (unlikely(!mapped_src_nents)) {
1397 1398 1399 1400
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}
	} else {
1401 1402
		/* Cover also the case of null (zero length) input data */
		if (src_nents) {
1403 1404 1405
			mapped_src_nents = dma_map_sg(jrdev, req->src,
						      src_nents, DMA_TO_DEVICE);
			if (unlikely(!mapped_src_nents)) {
1406 1407 1408
				dev_err(jrdev, "unable to map source\n");
				return ERR_PTR(-ENOMEM);
			}
1409 1410
		} else {
			mapped_src_nents = 0;
1411 1412
		}

1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
		/* Cover also the case of null (zero length) output data */
		if (dst_nents) {
			mapped_dst_nents = dma_map_sg(jrdev, req->dst,
						      dst_nents,
						      DMA_FROM_DEVICE);
			if (unlikely(!mapped_dst_nents)) {
				dev_err(jrdev, "unable to map destination\n");
				dma_unmap_sg(jrdev, req->src, src_nents,
					     DMA_TO_DEVICE);
				return ERR_PTR(-ENOMEM);
			}
		} else {
			mapped_dst_nents = 0;
1426 1427 1428
		}
	}

1429 1430 1431 1432
	/*
	 * HW reads 4 S/G entries at a time; make sure the reads don't go beyond
	 * the end of the table by allocating more S/G entries.
	 */
1433
	sec4_sg_len = mapped_src_nents > 1 ? mapped_src_nents : 0;
1434 1435 1436 1437 1438
	if (mapped_dst_nents > 1)
		sec4_sg_len += pad_sg_nents(mapped_dst_nents);
	else
		sec4_sg_len = pad_sg_nents(sec4_sg_len);

1439 1440 1441 1442 1443 1444 1445
	sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);

	/* allocate space for base edesc and hw desc commands, link tables */
	edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes,
			GFP_DMA | flags);
	if (!edesc) {
		caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0,
1446
			   0, 0, 0);
1447 1448 1449
		return ERR_PTR(-ENOMEM);
	}

1450 1451
	edesc->src_nents = src_nents;
	edesc->dst_nents = dst_nents;
1452 1453
	edesc->mapped_src_nents = mapped_src_nents;
	edesc->mapped_dst_nents = mapped_dst_nents;
Y
Yuan Kang 已提交
1454 1455
	edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
			 desc_bytes;
1456
	*all_contig_ptr = !(mapped_src_nents > 1);
1457

Y
Yuan Kang 已提交
1458
	sec4_sg_index = 0;
1459
	if (mapped_src_nents > 1) {
1460
		sg_to_sec4_sg_last(req->src, src_len,
1461 1462
				   edesc->sec4_sg + sec4_sg_index, 0);
		sec4_sg_index += mapped_src_nents;
1463
	}
1464
	if (mapped_dst_nents > 1) {
1465
		sg_to_sec4_sg_last(req->dst, dst_len,
Y
Yuan Kang 已提交
1466
				   edesc->sec4_sg + sec4_sg_index, 0);
1467
	}
1468 1469 1470 1471

	if (!sec4_sg_bytes)
		return edesc;

1472 1473
	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
					    sec4_sg_bytes, DMA_TO_DEVICE);
1474 1475
	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
		dev_err(jrdev, "unable to map S/G table\n");
1476 1477
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
1478 1479
		return ERR_PTR(-ENOMEM);
	}
1480

1481 1482
	edesc->sec4_sg_bytes = sec4_sg_bytes;

1483 1484 1485
	return edesc;
}

1486
static int gcm_encrypt(struct aead_request *req)
1487
{
Y
Yuan Kang 已提交
1488 1489
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1490 1491
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1492
	bool all_contig;
1493
	u32 *desc;
1494 1495
	int ret = 0;

1496
	/* allocate extended descriptor */
1497
	edesc = aead_edesc_alloc(req, GCM_DESC_JOB_IO_LEN, &all_contig, true);
1498 1499 1500
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

1501
	/* Create and submit job descriptor */
1502
	init_gcm_job(req, edesc, all_contig, true);
1503 1504 1505 1506

	print_hex_dump_debug("aead jobdesc@"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
			     desc_bytes(edesc->hw_desc), 1);
1507

1508 1509 1510 1511 1512 1513 1514 1515
	desc = edesc->hw_desc;
	ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
	}
1516

1517
	return ret;
1518 1519
}

1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
static int chachapoly_encrypt(struct aead_request *req)
{
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
	bool all_contig;
	u32 *desc;
	int ret;

	edesc = aead_edesc_alloc(req, CHACHAPOLY_DESC_JOB_IO_LEN, &all_contig,
				 true);
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	desc = edesc->hw_desc;

	init_chachapoly_job(req, edesc, all_contig, true);
	print_hex_dump_debug("chachapoly jobdesc@" __stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
			     1);

	ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
	}

	return ret;
}

static int chachapoly_decrypt(struct aead_request *req)
{
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
	bool all_contig;
	u32 *desc;
	int ret;

	edesc = aead_edesc_alloc(req, CHACHAPOLY_DESC_JOB_IO_LEN, &all_contig,
				 false);
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	desc = edesc->hw_desc;

	init_chachapoly_job(req, edesc, all_contig, false);
	print_hex_dump_debug("chachapoly jobdesc@" __stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
			     1);

	ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
	}

	return ret;
}

1586 1587 1588 1589 1590 1591 1592 1593
static int ipsec_gcm_encrypt(struct aead_request *req)
{
	if (req->assoclen < 8)
		return -EINVAL;

	return gcm_encrypt(req);
}

1594
static int aead_encrypt(struct aead_request *req)
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
{
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
	bool all_contig;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
1605 1606
	edesc = aead_edesc_alloc(req, AUTHENC_DESC_JOB_IO_LEN,
				 &all_contig, true);
1607 1608 1609 1610
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor */
1611
	init_authenc_job(req, edesc, all_contig, true);
1612 1613 1614 1615

	print_hex_dump_debug("aead jobdesc@"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
			     desc_bytes(edesc->hw_desc), 1);
1616 1617

	desc = edesc->hw_desc;
1618
	ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
1619 1620 1621
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1622
		aead_unmap(jrdev, edesc, req);
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
		kfree(edesc);
	}

	return ret;
}

static int gcm_decrypt(struct aead_request *req)
{
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
	bool all_contig;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
	edesc = aead_edesc_alloc(req, GCM_DESC_JOB_IO_LEN, &all_contig, false);
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor*/
	init_gcm_job(req, edesc, all_contig, false);
1646 1647 1648 1649

	print_hex_dump_debug("aead jobdesc@"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
			     desc_bytes(edesc->hw_desc), 1);
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662

	desc = edesc->hw_desc;
	ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
	}

	return ret;
}

1663 1664 1665 1666 1667 1668 1669 1670
static int ipsec_gcm_decrypt(struct aead_request *req)
{
	if (req->assoclen < 8)
		return -EINVAL;

	return gcm_decrypt(req);
}

1671
static int aead_decrypt(struct aead_request *req)
1672
{
1673
	struct aead_edesc *edesc;
1674 1675 1676
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1677
	bool all_contig;
1678
	u32 *desc;
1679
	int ret = 0;
1680

1681
	caam_dump_sg("dec src@" __stringify(__LINE__)": ",
1682 1683
		     DUMP_PREFIX_ADDRESS, 16, 4, req->src,
		     req->assoclen + req->cryptlen, 1);
C
Catalin Vasile 已提交
1684

1685
	/* allocate extended descriptor */
1686 1687
	edesc = aead_edesc_alloc(req, AUTHENC_DESC_JOB_IO_LEN,
				 &all_contig, false);
1688 1689 1690
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

1691
	/* Create and submit job descriptor*/
1692
	init_authenc_job(req, edesc, all_contig, false);
1693 1694 1695 1696

	print_hex_dump_debug("aead jobdesc@"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
			     desc_bytes(edesc->hw_desc), 1);
1697

1698
	desc = edesc->hw_desc;
1699
	ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
1700 1701 1702
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1703
		aead_unmap(jrdev, edesc, req);
1704 1705
		kfree(edesc);
	}
1706

1707 1708
	return ret;
}
1709

Y
Yuan Kang 已提交
1710
/*
1711
 * allocate and map the skcipher extended descriptor for skcipher
Y
Yuan Kang 已提交
1712
 */
1713 1714
static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req,
						   int desc_bytes)
Y
Yuan Kang 已提交
1715
{
1716 1717
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
Y
Yuan Kang 已提交
1718
	struct device *jrdev = ctx->jrdev;
1719
	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
Y
Yuan Kang 已提交
1720
		       GFP_KERNEL : GFP_ATOMIC;
1721
	int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
1722
	struct skcipher_edesc *edesc;
1723
	dma_addr_t iv_dma = 0;
1724
	u8 *iv;
1725
	int ivsize = crypto_skcipher_ivsize(skcipher);
1726
	int dst_sg_idx, sec4_sg_ents, sec4_sg_bytes;
Y
Yuan Kang 已提交
1727

1728
	src_nents = sg_nents_for_len(req->src, req->cryptlen);
1729 1730
	if (unlikely(src_nents < 0)) {
		dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
1731
			req->cryptlen);
1732 1733
		return ERR_PTR(src_nents);
	}
Y
Yuan Kang 已提交
1734

1735
	if (req->dst != req->src) {
1736
		dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
1737 1738
		if (unlikely(dst_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in dst S/G\n",
1739
				req->cryptlen);
1740 1741 1742
			return ERR_PTR(dst_nents);
		}
	}
Y
Yuan Kang 已提交
1743 1744

	if (likely(req->src == req->dst)) {
1745 1746 1747
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_BIDIRECTIONAL);
		if (unlikely(!mapped_src_nents)) {
1748 1749 1750
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}
Y
Yuan Kang 已提交
1751
	} else {
1752 1753 1754
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_TO_DEVICE);
		if (unlikely(!mapped_src_nents)) {
1755 1756 1757
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}
1758 1759 1760
		mapped_dst_nents = dma_map_sg(jrdev, req->dst, dst_nents,
					      DMA_FROM_DEVICE);
		if (unlikely(!mapped_dst_nents)) {
1761
			dev_err(jrdev, "unable to map destination\n");
1762
			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1763 1764
			return ERR_PTR(-ENOMEM);
		}
Y
Yuan Kang 已提交
1765 1766
	}

1767 1768 1769 1770
	if (!ivsize && mapped_src_nents == 1)
		sec4_sg_ents = 0; // no need for an input hw s/g table
	else
		sec4_sg_ents = mapped_src_nents + !!ivsize;
1771
	dst_sg_idx = sec4_sg_ents;
1772 1773

	/*
1774 1775 1776 1777
	 * Input, output HW S/G tables: [IV, src][dst, IV]
	 * IV entries point to the same buffer
	 * If src == dst, S/G entries are reused (S/G tables overlap)
	 *
1778 1779
	 * HW reads 4 S/G entries at a time; make sure the reads don't go beyond
	 * the end of the table by allocating more S/G entries. Logic:
1780
	 * if (output S/G)
1781 1782 1783 1784
	 *      pad output S/G, if needed
	 * else if (input S/G) ...
	 *      pad input S/G, if needed
	 */
1785 1786 1787 1788 1789 1790 1791
	if (ivsize || mapped_dst_nents > 1) {
		if (req->src == req->dst)
			sec4_sg_ents = !!ivsize + pad_sg_nents(sec4_sg_ents);
		else
			sec4_sg_ents += pad_sg_nents(mapped_dst_nents +
						     !!ivsize);
	} else {
1792
		sec4_sg_ents = pad_sg_nents(sec4_sg_ents);
1793
	}
1794

1795
	sec4_sg_bytes = sec4_sg_ents * sizeof(struct sec4_sg_entry);
Y
Yuan Kang 已提交
1796

1797 1798 1799 1800
	/*
	 * allocate space for base edesc and hw desc commands, link tables, IV
	 */
	edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes + ivsize,
1801
			GFP_DMA | flags);
Y
Yuan Kang 已提交
1802 1803
	if (!edesc) {
		dev_err(jrdev, "could not allocate extended descriptor\n");
1804
		caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0,
1805
			   0, 0, 0);
Y
Yuan Kang 已提交
1806 1807 1808 1809 1810
		return ERR_PTR(-ENOMEM);
	}

	edesc->src_nents = src_nents;
	edesc->dst_nents = dst_nents;
1811 1812
	edesc->mapped_src_nents = mapped_src_nents;
	edesc->mapped_dst_nents = mapped_dst_nents;
Y
Yuan Kang 已提交
1813
	edesc->sec4_sg_bytes = sec4_sg_bytes;
1814 1815
	edesc->sec4_sg = (struct sec4_sg_entry *)((u8 *)edesc->hw_desc +
						  desc_bytes);
Y
Yuan Kang 已提交
1816

1817
	/* Make sure IV is located in a DMAable area */
1818
	if (ivsize) {
1819
		iv = (u8 *)edesc->sec4_sg + sec4_sg_bytes;
1820 1821
		memcpy(iv, req->iv, ivsize);

1822
		iv_dma = dma_map_single(jrdev, iv, ivsize, DMA_BIDIRECTIONAL);
1823 1824 1825 1826 1827 1828 1829
		if (dma_mapping_error(jrdev, iv_dma)) {
			dev_err(jrdev, "unable to map IV\n");
			caam_unmap(jrdev, req->src, req->dst, src_nents,
				   dst_nents, 0, 0, 0, 0);
			kfree(edesc);
			return ERR_PTR(-ENOMEM);
		}
1830

1831
		dma_to_sec4_sg_one(edesc->sec4_sg, iv_dma, ivsize, 0);
Y
Yuan Kang 已提交
1832
	}
1833
	if (dst_sg_idx)
1834 1835
		sg_to_sec4_sg(req->src, req->cryptlen, edesc->sec4_sg +
			      !!ivsize, 0);
1836

1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
	if (req->src != req->dst && (ivsize || mapped_dst_nents > 1))
		sg_to_sec4_sg(req->dst, req->cryptlen, edesc->sec4_sg +
			      dst_sg_idx, 0);

	if (ivsize)
		dma_to_sec4_sg_one(edesc->sec4_sg + dst_sg_idx +
				   mapped_dst_nents, iv_dma, ivsize, 0);

	if (ivsize || mapped_dst_nents > 1)
		sg_to_sec4_set_last(edesc->sec4_sg + dst_sg_idx +
				    mapped_dst_nents);
Y
Yuan Kang 已提交
1848

1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
	if (sec4_sg_bytes) {
		edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
						    sec4_sg_bytes,
						    DMA_TO_DEVICE);
		if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
			dev_err(jrdev, "unable to map S/G table\n");
			caam_unmap(jrdev, req->src, req->dst, src_nents,
				   dst_nents, iv_dma, ivsize, 0, 0);
			kfree(edesc);
			return ERR_PTR(-ENOMEM);
		}
1860 1861
	}

Y
Yuan Kang 已提交
1862 1863
	edesc->iv_dma = iv_dma;

1864 1865 1866
	print_hex_dump_debug("skcipher sec4_sg@" __stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
			     sec4_sg_bytes, 1);
Y
Yuan Kang 已提交
1867 1868 1869 1870

	return edesc;
}

1871
static int skcipher_encrypt(struct skcipher_request *req)
Y
Yuan Kang 已提交
1872
{
1873 1874 1875
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
Y
Yuan Kang 已提交
1876 1877 1878 1879 1880
	struct device *jrdev = ctx->jrdev;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
1881
	edesc = skcipher_edesc_alloc(req, DESC_JOB_IO_LEN * CAAM_CMD_SZ);
Y
Yuan Kang 已提交
1882 1883 1884 1885
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor*/
1886
	init_skcipher_job(req, edesc, true);
1887 1888 1889 1890 1891

	print_hex_dump_debug("skcipher jobdesc@" __stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
			     desc_bytes(edesc->hw_desc), 1);

Y
Yuan Kang 已提交
1892
	desc = edesc->hw_desc;
1893
	ret = caam_jr_enqueue(jrdev, desc, skcipher_encrypt_done, req);
Y
Yuan Kang 已提交
1894 1895 1896 1897

	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1898
		skcipher_unmap(jrdev, edesc, req);
Y
Yuan Kang 已提交
1899 1900 1901 1902 1903 1904
		kfree(edesc);
	}

	return ret;
}

1905
static int skcipher_decrypt(struct skcipher_request *req)
Y
Yuan Kang 已提交
1906
{
1907 1908 1909
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
Y
Yuan Kang 已提交
1910 1911 1912 1913 1914
	struct device *jrdev = ctx->jrdev;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
1915
	edesc = skcipher_edesc_alloc(req, DESC_JOB_IO_LEN * CAAM_CMD_SZ);
Y
Yuan Kang 已提交
1916 1917 1918 1919
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor*/
1920
	init_skcipher_job(req, edesc, false);
Y
Yuan Kang 已提交
1921
	desc = edesc->hw_desc;
1922 1923 1924 1925

	print_hex_dump_debug("skcipher jobdesc@" __stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
			     desc_bytes(edesc->hw_desc), 1);
Y
Yuan Kang 已提交
1926

1927
	ret = caam_jr_enqueue(jrdev, desc, skcipher_decrypt_done, req);
Y
Yuan Kang 已提交
1928 1929 1930
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1931
		skcipher_unmap(jrdev, edesc, req);
Y
Yuan Kang 已提交
1932 1933 1934 1935 1936 1937
		kfree(edesc);
	}

	return ret;
}

1938
static struct caam_skcipher_alg driver_algs[] = {
1939
	{
1940 1941 1942 1943 1944 1945
		.skcipher = {
			.base = {
				.cra_name = "cbc(aes)",
				.cra_driver_name = "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
I
Iuliana Prodan 已提交
1946
			.setkey = aes_skcipher_setkey,
1947 1948
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1949 1950 1951
			.min_keysize = AES_MIN_KEY_SIZE,
			.max_keysize = AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
1952 1953
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
1954 1955
	},
	{
1956 1957 1958 1959 1960 1961
		.skcipher = {
			.base = {
				.cra_name = "cbc(des3_ede)",
				.cra_driver_name = "cbc-3des-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
1962
			.setkey = des_skcipher_setkey,
1963 1964
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1965 1966 1967
			.min_keysize = DES3_EDE_KEY_SIZE,
			.max_keysize = DES3_EDE_KEY_SIZE,
			.ivsize = DES3_EDE_BLOCK_SIZE,
1968 1969
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
1970 1971
	},
	{
1972 1973 1974 1975 1976 1977
		.skcipher = {
			.base = {
				.cra_name = "cbc(des)",
				.cra_driver_name = "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
1978
			.setkey = des_skcipher_setkey,
1979 1980
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1981 1982 1983
			.min_keysize = DES_KEY_SIZE,
			.max_keysize = DES_KEY_SIZE,
			.ivsize = DES_BLOCK_SIZE,
1984 1985
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
1986 1987
	},
	{
1988 1989 1990 1991 1992 1993
		.skcipher = {
			.base = {
				.cra_name = "ctr(aes)",
				.cra_driver_name = "ctr-aes-caam",
				.cra_blocksize = 1,
			},
I
Iuliana Prodan 已提交
1994
			.setkey = ctr_skcipher_setkey,
1995 1996
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1997 1998 1999
			.min_keysize = AES_MIN_KEY_SIZE,
			.max_keysize = AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
2000 2001 2002 2003
			.chunksize = AES_BLOCK_SIZE,
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES |
					OP_ALG_AAI_CTR_MOD128,
2004 2005
	},
	{
2006 2007 2008 2009 2010 2011
		.skcipher = {
			.base = {
				.cra_name = "rfc3686(ctr(aes))",
				.cra_driver_name = "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
I
Iuliana Prodan 已提交
2012
			.setkey = rfc3686_skcipher_setkey,
2013 2014
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
2015 2016 2017 2018 2019
			.min_keysize = AES_MIN_KEY_SIZE +
				       CTR_RFC3686_NONCE_SIZE,
			.max_keysize = AES_MAX_KEY_SIZE +
				       CTR_RFC3686_NONCE_SIZE,
			.ivsize = CTR_RFC3686_IV_SIZE,
2020 2021 2022 2023 2024 2025 2026
			.chunksize = AES_BLOCK_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.rfc3686 = true,
		},
2027 2028
	},
	{
2029 2030 2031 2032 2033 2034 2035 2036 2037
		.skcipher = {
			.base = {
				.cra_name = "xts(aes)",
				.cra_driver_name = "xts-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
			.setkey = xts_skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
2038 2039 2040
			.min_keysize = 2 * AES_MIN_KEY_SIZE,
			.max_keysize = 2 * AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
2041 2042
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XTS,
2043
	},
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
	{
		.skcipher = {
			.base = {
				.cra_name = "ecb(des)",
				.cra_driver_name = "ecb-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
			.setkey = des_skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
			.min_keysize = DES_KEY_SIZE,
			.max_keysize = DES_KEY_SIZE,
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_ECB,
	},
	{
		.skcipher = {
			.base = {
				.cra_name = "ecb(aes)",
				.cra_driver_name = "ecb-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
I
Iuliana Prodan 已提交
2066
			.setkey = aes_skcipher_setkey,
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
			.min_keysize = AES_MIN_KEY_SIZE,
			.max_keysize = AES_MAX_KEY_SIZE,
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_ECB,
	},
	{
		.skcipher = {
			.base = {
				.cra_name = "ecb(des3_ede)",
				.cra_driver_name = "ecb-des3-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
			.setkey = des_skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
			.min_keysize = DES3_EDE_KEY_SIZE,
			.max_keysize = DES3_EDE_KEY_SIZE,
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_ECB,
	},
	{
		.skcipher = {
			.base = {
				.cra_name = "ecb(arc4)",
				.cra_driver_name = "ecb-arc4-caam",
				.cra_blocksize = ARC4_BLOCK_SIZE,
			},
I
Iuliana Prodan 已提交
2096
			.setkey = arc4_skcipher_setkey,
2097 2098 2099 2100 2101 2102 2103
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
			.min_keysize = ARC4_MIN_KEY_SIZE,
			.max_keysize = ARC4_MAX_KEY_SIZE,
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_ARC4 | OP_ALG_AAI_ECB,
	},
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
};

static struct caam_aead_alg driver_aeads[] = {
	{
		.aead = {
			.base = {
				.cra_name = "rfc4106(gcm(aes))",
				.cra_driver_name = "rfc4106-gcm-aes-caam",
				.cra_blocksize = 1,
			},
			.setkey = rfc4106_setkey,
			.setauthsize = rfc4106_setauthsize,
			.encrypt = ipsec_gcm_encrypt,
			.decrypt = ipsec_gcm_decrypt,
2118
			.ivsize = GCM_RFC4106_IV_SIZE,
2119 2120 2121 2122
			.maxauthsize = AES_BLOCK_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
2123
			.nodkp = true,
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "rfc4543(gcm(aes))",
				.cra_driver_name = "rfc4543-gcm-aes-caam",
				.cra_blocksize = 1,
			},
			.setkey = rfc4543_setkey,
			.setauthsize = rfc4543_setauthsize,
			.encrypt = ipsec_gcm_encrypt,
			.decrypt = ipsec_gcm_decrypt,
2137
			.ivsize = GCM_RFC4543_IV_SIZE,
2138 2139 2140 2141
			.maxauthsize = AES_BLOCK_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
2142
			.nodkp = true,
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
		},
	},
	/* Galois Counter Mode */
	{
		.aead = {
			.base = {
				.cra_name = "gcm(aes)",
				.cra_driver_name = "gcm-aes-caam",
				.cra_blocksize = 1,
			},
			.setkey = gcm_setkey,
			.setauthsize = gcm_setauthsize,
			.encrypt = gcm_encrypt,
			.decrypt = gcm_decrypt,
2157
			.ivsize = GCM_AES_IV_SIZE,
2158 2159 2160 2161
			.maxauthsize = AES_BLOCK_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
2162
			.nodkp = true,
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
		},
	},
	/* single-pass ipsec_esp descriptor */
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(md5),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-md5-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
			},
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2179
			.ivsize = NULL_IV_SIZE,
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194
			.maxauthsize = MD5_DIGEST_SIZE,
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha1),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-sha1-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
2195
			},
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = NULL_IV_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2207 2208
	},
	{
2209 2210 2211 2212 2213 2214 2215 2216
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha224),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-sha224-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
			},
2217 2218
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2219 2220
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2221 2222
			.ivsize = NULL_IV_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
2223 2224 2225 2226 2227
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2228 2229
	},
	{
2230 2231 2232 2233 2234 2235 2236 2237
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha256),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-sha256-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
			},
2238 2239
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2240 2241
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2242 2243
			.ivsize = NULL_IV_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
2244 2245 2246 2247 2248
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2249 2250
	},
	{
2251 2252 2253 2254 2255 2256 2257 2258
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha384),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-sha384-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
			},
2259 2260
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2261 2262
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2263 2264
			.ivsize = NULL_IV_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
2265 2266 2267 2268 2269
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2270 2271
	},
	{
2272 2273 2274 2275 2276 2277 2278 2279
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha512),"
					    "ecb(cipher_null))",
				.cra_driver_name = "authenc-hmac-sha512-"
						   "ecb-cipher_null-caam",
				.cra_blocksize = NULL_BLOCK_SIZE,
			},
2280 2281
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2282 2283
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2284 2285
			.ivsize = NULL_IV_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298
		},
		.caam = {
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(md5),cbc(aes))",
				.cra_driver_name = "authenc-hmac-md5-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
2299
			},
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2312
	},
2313
	{
2314 2315 2316 2317 2318 2319 2320 2321
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(md5),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-hmac-md5-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
2322 2323
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2324
			.encrypt = aead_encrypt,
2325
			.decrypt = aead_decrypt,
2326 2327
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha1),cbc(aes))",
				.cra_driver_name = "authenc-hmac-sha1-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
2343
			},
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2356
	},
2357
	{
2358 2359 2360 2361 2362 2363 2364 2365
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha1),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha1-cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2366 2367
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2368
			.encrypt = aead_encrypt,
2369
			.decrypt = aead_decrypt,
2370 2371
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha224),cbc(aes))",
				.cra_driver_name = "authenc-hmac-sha224-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
2387
			},
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2400
	},
2401
	{
2402 2403 2404 2405 2406 2407 2408 2409
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha224),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha224-cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
2410 2411
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2412
			.encrypt = aead_encrypt,
2413
			.decrypt = aead_decrypt,
2414 2415
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha256),cbc(aes))",
				.cra_driver_name = "authenc-hmac-sha256-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
2431
			},
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2444
	},
2445
	{
2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha256),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha256-cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
2457
			.decrypt = aead_decrypt,
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha384),cbc(aes))",
				.cra_driver_name = "authenc-hmac-sha384-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha384),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha384-cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2498 2499
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2500
			.encrypt = aead_encrypt,
2501
			.decrypt = aead_decrypt,
2502
			.ivsize = AES_BLOCK_SIZE,
2503 2504 2505 2506 2507 2508 2509 2510
			.maxauthsize = SHA384_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
2511
	},
2512
	{
2513 2514 2515 2516 2517 2518 2519
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha512),cbc(aes))",
				.cra_driver_name = "authenc-hmac-sha512-"
						   "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
2520 2521
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2522 2523
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
2524
			.ivsize = AES_BLOCK_SIZE,
2525 2526 2527 2528 2529 2530 2531
			.maxauthsize = SHA512_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2532
	},
2533
	{
2534 2535 2536 2537 2538 2539 2540 2541
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha512),"
					    "cbc(aes)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha512-cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2542 2543
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2544
			.encrypt = aead_encrypt,
2545
			.decrypt = aead_decrypt,
2546 2547
			.ivsize = AES_BLOCK_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(md5),cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-md5-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2563
			},
2564
			.setkey = des3_aead_setkey,
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		}
2576
	},
2577
	{
2578 2579 2580 2581 2582 2583 2584 2585
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(md5),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-hmac-md5-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
2586
			.setkey = des3_aead_setkey,
2587
			.setauthsize = aead_setauthsize,
2588
			.encrypt = aead_encrypt,
2589
			.decrypt = aead_decrypt,
2590 2591
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		}
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha1),"
					    "cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-sha1-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2608
			},
2609
			.setkey = des3_aead_setkey,
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2621
	},
2622
	{
2623 2624 2625 2626 2627 2628 2629 2630 2631
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha1),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha1-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
2632
			.setkey = des3_aead_setkey,
Y
Yuan Kang 已提交
2633
			.setauthsize = aead_setauthsize,
2634
			.encrypt = aead_encrypt,
2635
			.decrypt = aead_decrypt,
2636 2637
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha224),"
					    "cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-sha224-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2654
			},
2655
			.setkey = des3_aead_setkey,
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2667
	},
2668
	{
2669 2670 2671 2672 2673 2674 2675 2676 2677
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha224),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha224-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
2678
			.setkey = des3_aead_setkey,
2679
			.setauthsize = aead_setauthsize,
2680
			.encrypt = aead_encrypt,
2681
			.decrypt = aead_decrypt,
2682 2683
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha256),"
					    "cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-sha256-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2700
			},
2701
			.setkey = des3_aead_setkey,
2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2713
	},
2714
	{
2715 2716 2717 2718 2719 2720 2721 2722 2723
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha256),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha256-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
2724
			.setkey = des3_aead_setkey,
Y
Yuan Kang 已提交
2725
			.setauthsize = aead_setauthsize,
2726
			.encrypt = aead_encrypt,
2727
			.decrypt = aead_decrypt,
2728 2729
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha384),"
					    "cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-sha384-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2746
			},
2747
			.setkey = des3_aead_setkey,
2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2759
	},
2760
	{
2761 2762 2763 2764 2765 2766 2767 2768 2769
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha384),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha384-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
2770
			.setkey = des3_aead_setkey,
2771
			.setauthsize = aead_setauthsize,
2772
			.encrypt = aead_encrypt,
2773
			.decrypt = aead_decrypt,
2774 2775
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha512),"
					    "cbc(des3_ede))",
				.cra_driver_name = "authenc-hmac-sha512-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2792
			},
2793
			.setkey = des3_aead_setkey,
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2805
	},
2806
	{
2807 2808 2809 2810 2811 2812 2813 2814 2815
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha512),"
					    "cbc(des3_ede)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha512-"
						   "cbc-des3_ede-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
2816
			.setkey = des3_aead_setkey,
Y
Yuan Kang 已提交
2817
			.setauthsize = aead_setauthsize,
2818
			.encrypt = aead_encrypt,
2819
			.decrypt = aead_decrypt,
2820 2821
			.ivsize = DES3_EDE_BLOCK_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(md5),cbc(des))",
				.cra_driver_name = "authenc-hmac-md5-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
2837
			},
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2850
	},
2851
	{
2852 2853 2854 2855 2856 2857 2858 2859
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(md5),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-hmac-md5-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
2860 2861
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2862
			.encrypt = aead_encrypt,
2863
			.decrypt = aead_decrypt,
2864 2865
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha1),cbc(des))",
				.cra_driver_name = "authenc-hmac-sha1-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
2881
			},
2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2894
	},
2895
	{
2896 2897 2898 2899 2900 2901 2902 2903
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha1),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha1-cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2904 2905
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2906
			.encrypt = aead_encrypt,
2907
			.decrypt = aead_decrypt,
2908 2909
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA1_DIGEST_SIZE,
2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha224),cbc(des))",
				.cra_driver_name = "authenc-hmac-sha224-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
2925
			},
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2938
	},
2939
	{
2940 2941 2942 2943 2944 2945 2946 2947
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha224),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha224-cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
2948 2949
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2950
			.encrypt = aead_encrypt,
2951
			.decrypt = aead_decrypt,
2952 2953
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA224_DIGEST_SIZE,
2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha256),cbc(des))",
				.cra_driver_name = "authenc-hmac-sha256-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
2969
			},
2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
2982
	},
2983
	{
2984 2985 2986 2987 2988 2989 2990 2991
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha256),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha256-cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
2992 2993
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
2994
			.encrypt = aead_encrypt,
2995
			.decrypt = aead_decrypt,
2996 2997
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha384),cbc(des))",
				.cra_driver_name = "authenc-hmac-sha384-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
3013
			},
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
3026
	},
3027
	{
3028 3029 3030 3031 3032 3033 3034 3035
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha384),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha384-cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
3036 3037
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
3038
			.encrypt = aead_encrypt,
3039
			.decrypt = aead_decrypt,
3040 3041
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha512),cbc(des))",
				.cra_driver_name = "authenc-hmac-sha512-"
						   "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
3057
			},
3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
		},
3070
	},
3071
	{
3072 3073 3074 3075 3076 3077 3078 3079
		.aead = {
			.base = {
				.cra_name = "echainiv(authenc(hmac(sha512),"
					    "cbc(des)))",
				.cra_driver_name = "echainiv-authenc-"
						   "hmac-sha512-cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
Y
Yuan Kang 已提交
3080 3081
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
3082
			.encrypt = aead_encrypt,
3083
			.decrypt = aead_decrypt,
3084 3085
			.ivsize = DES_BLOCK_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
3086 3087 3088 3089 3090 3091 3092
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.geniv = true,
		},
3093
	},
3094
	{
3095 3096 3097 3098 3099 3100 3101 3102
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(md5),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-md5-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
3103 3104
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
3105 3106
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
3107 3108
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = MD5_DIGEST_SIZE,
3109 3110 3111 3112 3113 3114 3115 3116
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
		},
3117 3118
	},
	{
3119 3120 3121 3122 3123 3124 3125 3126
		.aead = {
			.base = {
				.cra_name = "seqiv(authenc("
					    "hmac(md5),rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-md5-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
3127 3128
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
3129
			.encrypt = aead_encrypt,
3130
			.decrypt = aead_decrypt,
3131
			.ivsize = CTR_RFC3686_IV_SIZE,
3132 3133 3134 3135 3136 3137 3138 3139 3140 3141
			.maxauthsize = MD5_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_MD5 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
		},
3142 3143
	},
	{
3144 3145 3146 3147 3148 3149 3150 3151
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha1),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-sha1-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
3152 3153
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
3154 3155
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
3156
			.ivsize = CTR_RFC3686_IV_SIZE,
3157 3158 3159 3160 3161 3162 3163 3164 3165
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
		},
3166 3167
	},
	{
3168 3169 3170 3171 3172 3173 3174 3175
		.aead = {
			.base = {
				.cra_name = "seqiv(authenc("
					    "hmac(sha1),rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-sha1-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
3176 3177
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
3178
			.encrypt = aead_encrypt,
3179
			.decrypt = aead_decrypt,
3180
			.ivsize = CTR_RFC3686_IV_SIZE,
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190
			.maxauthsize = SHA1_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA1 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
		},
3191 3192
	},
	{
3193 3194 3195 3196 3197 3198 3199 3200
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha224),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-sha224-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
3201 3202
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
3203 3204
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
3205
			.ivsize = CTR_RFC3686_IV_SIZE,
3206 3207 3208 3209 3210 3211 3212 3213 3214
			.maxauthsize = SHA224_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
		},
3215 3216
	},
	{
3217 3218 3219 3220 3221 3222 3223 3224
		.aead = {
			.base = {
				.cra_name = "seqiv(authenc("
					    "hmac(sha224),rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-sha224-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
3225 3226
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
3227
			.encrypt = aead_encrypt,
3228
			.decrypt = aead_decrypt,
3229
			.ivsize = CTR_RFC3686_IV_SIZE,
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239
			.maxauthsize = SHA224_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA224 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
		},
Y
Yuan Kang 已提交
3240 3241
	},
	{
3242 3243 3244 3245 3246 3247 3248
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha256),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-sha256-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
Y
Yuan Kang 已提交
3249
			},
3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
		},
Y
Yuan Kang 已提交
3264 3265
	},
	{
3266 3267 3268 3269 3270 3271 3272
		.aead = {
			.base = {
				.cra_name = "seqiv(authenc(hmac(sha256),"
					    "rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-sha256-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
Y
Yuan Kang 已提交
3273
			},
3274 3275 3276
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
3277
			.decrypt = aead_decrypt,
3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = SHA256_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA256 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
		},
3289 3290
	},
	{
3291 3292 3293 3294 3295 3296 3297
		.aead = {
			.base = {
				.cra_name = "authenc(hmac(sha384),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-sha384-"
						   "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
3298
			},
3299 3300 3301 3302
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
3303
			.ivsize = CTR_RFC3686_IV_SIZE,
3304 3305 3306 3307 3308 3309 3310 3311 3312 3313
			.maxauthsize = SHA384_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
		},
	},
3314 3315 3316
	{
		.aead = {
			.base = {
3317 3318 3319 3320
				.cra_name = "seqiv(authenc(hmac(sha384),"
					    "rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-sha384-"
						   "rfc3686-ctr-aes-caam",
3321 3322
				.cra_blocksize = 1,
			},
3323 3324 3325
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
3326
			.decrypt = aead_decrypt,
3327 3328
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = SHA384_DIGEST_SIZE,
3329 3330
		},
		.caam = {
3331 3332 3333 3334 3335 3336
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA384 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
3337 3338 3339 3340 3341
		},
	},
	{
		.aead = {
			.base = {
3342 3343 3344 3345
				.cra_name = "authenc(hmac(sha512),"
					    "rfc3686(ctr(aes)))",
				.cra_driver_name = "authenc-hmac-sha512-"
						   "rfc3686-ctr-aes-caam",
3346 3347
				.cra_blocksize = 1,
			},
3348 3349 3350 3351 3352 3353
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
			.decrypt = aead_decrypt,
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
3354 3355
		},
		.caam = {
3356 3357 3358 3359 3360
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
3361 3362 3363 3364 3365
		},
	},
	{
		.aead = {
			.base = {
3366 3367 3368 3369
				.cra_name = "seqiv(authenc(hmac(sha512),"
					    "rfc3686(ctr(aes))))",
				.cra_driver_name = "seqiv-authenc-hmac-sha512-"
						   "rfc3686-ctr-aes-caam",
3370 3371
				.cra_blocksize = 1,
			},
3372 3373 3374
			.setkey = aead_setkey,
			.setauthsize = aead_setauthsize,
			.encrypt = aead_encrypt,
3375
			.decrypt = aead_decrypt,
3376 3377
			.ivsize = CTR_RFC3686_IV_SIZE,
			.maxauthsize = SHA512_DIGEST_SIZE,
3378 3379
		},
		.caam = {
3380 3381 3382 3383 3384 3385
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.class2_alg_type = OP_ALG_ALGSEL_SHA512 |
					   OP_ALG_AAI_HMAC_PRECOMP,
			.rfc3686 = true,
			.geniv = true,
3386 3387
		},
	},
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407
	{
		.aead = {
			.base = {
				.cra_name = "rfc7539(chacha20,poly1305)",
				.cra_driver_name = "rfc7539-chacha20-poly1305-"
						   "caam",
				.cra_blocksize = 1,
			},
			.setkey = chachapoly_setkey,
			.setauthsize = chachapoly_setauthsize,
			.encrypt = chachapoly_encrypt,
			.decrypt = chachapoly_decrypt,
			.ivsize = CHACHAPOLY_IV_SIZE,
			.maxauthsize = POLY1305_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_CHACHA20 |
					   OP_ALG_AAI_AEAD,
			.class2_alg_type = OP_ALG_ALGSEL_POLY1305 |
					   OP_ALG_AAI_AEAD,
3408
			.nodkp = true,
3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
		},
	},
	{
		.aead = {
			.base = {
				.cra_name = "rfc7539esp(chacha20,poly1305)",
				.cra_driver_name = "rfc7539esp-chacha20-"
						   "poly1305-caam",
				.cra_blocksize = 1,
			},
			.setkey = chachapoly_setkey,
			.setauthsize = chachapoly_setauthsize,
			.encrypt = chachapoly_encrypt,
			.decrypt = chachapoly_decrypt,
			.ivsize = 8,
			.maxauthsize = POLY1305_DIGEST_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_CHACHA20 |
					   OP_ALG_AAI_AEAD,
			.class2_alg_type = OP_ALG_ALGSEL_POLY1305 |
					   OP_ALG_AAI_AEAD,
3431
			.nodkp = true,
3432 3433
		},
	},
3434 3435
};

3436 3437
static int caam_init_common(struct caam_ctx *ctx, struct caam_alg_entry *caam,
			    bool uses_dkp)
3438
{
3439
	dma_addr_t dma_addr;
3440
	struct caam_drv_private *priv;
3441

3442 3443 3444 3445 3446
	ctx->jrdev = caam_jr_alloc();
	if (IS_ERR(ctx->jrdev)) {
		pr_err("Job Ring Device allocation for transform failed\n");
		return PTR_ERR(ctx->jrdev);
	}
3447

3448 3449 3450 3451 3452 3453
	priv = dev_get_drvdata(ctx->jrdev->parent);
	if (priv->era >= 6 && uses_dkp)
		ctx->dir = DMA_BIDIRECTIONAL;
	else
		ctx->dir = DMA_TO_DEVICE;

3454 3455 3456
	dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_enc,
					offsetof(struct caam_ctx,
						 sh_desc_enc_dma),
3457
					ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468
	if (dma_mapping_error(ctx->jrdev, dma_addr)) {
		dev_err(ctx->jrdev, "unable to map key, shared descriptors\n");
		caam_jr_free(ctx->jrdev);
		return -ENOMEM;
	}

	ctx->sh_desc_enc_dma = dma_addr;
	ctx->sh_desc_dec_dma = dma_addr + offsetof(struct caam_ctx,
						   sh_desc_dec);
	ctx->key_dma = dma_addr + offsetof(struct caam_ctx, key);

3469
	/* copy descriptor header template value */
3470 3471
	ctx->cdata.algtype = OP_TYPE_CLASS1_ALG | caam->class1_alg_type;
	ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam->class2_alg_type;
3472 3473 3474 3475

	return 0;
}

3476
static int caam_cra_init(struct crypto_skcipher *tfm)
3477
{
3478 3479 3480
	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
	struct caam_skcipher_alg *caam_alg =
		container_of(alg, typeof(*caam_alg), skcipher);
3481

3482 3483
	return caam_init_common(crypto_skcipher_ctx(tfm), &caam_alg->caam,
				false);
3484 3485 3486 3487 3488 3489 3490 3491 3492
}

static int caam_aead_init(struct crypto_aead *tfm)
{
	struct aead_alg *alg = crypto_aead_alg(tfm);
	struct caam_aead_alg *caam_alg =
		 container_of(alg, struct caam_aead_alg, aead);
	struct caam_ctx *ctx = crypto_aead_ctx(tfm);

3493
	return caam_init_common(ctx, &caam_alg->caam, !caam_alg->caam.nodkp);
3494 3495 3496 3497
}

static void caam_exit_common(struct caam_ctx *ctx)
{
3498 3499
	dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_enc_dma,
			       offsetof(struct caam_ctx, sh_desc_enc_dma),
3500
			       ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
3501
	caam_jr_free(ctx->jrdev);
3502 3503
}

3504
static void caam_cra_exit(struct crypto_skcipher *tfm)
3505
{
3506
	caam_exit_common(crypto_skcipher_ctx(tfm));
3507 3508 3509 3510 3511 3512 3513
}

static void caam_aead_exit(struct crypto_aead *tfm)
{
	caam_exit_common(crypto_aead_ctx(tfm));
}

3514
void caam_algapi_exit(void)
3515
{
3516 3517 3518 3519 3520 3521 3522 3523
	int i;

	for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
		struct caam_aead_alg *t_alg = driver_aeads + i;

		if (t_alg->registered)
			crypto_unregister_aead(&t_alg->aead);
	}
3524

3525 3526
	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
		struct caam_skcipher_alg *t_alg = driver_algs + i;
3527

3528 3529
		if (t_alg->registered)
			crypto_unregister_skcipher(&t_alg->skcipher);
3530 3531 3532
	}
}

3533
static void caam_skcipher_alg_init(struct caam_skcipher_alg *t_alg)
3534
{
3535
	struct skcipher_alg *alg = &t_alg->skcipher;
3536

3537 3538 3539 3540
	alg->base.cra_module = THIS_MODULE;
	alg->base.cra_priority = CAAM_CRA_PRIORITY;
	alg->base.cra_ctxsize = sizeof(struct caam_ctx);
	alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
3541

3542 3543
	alg->init = caam_cra_init;
	alg->exit = caam_cra_exit;
3544 3545
}

3546 3547 3548 3549 3550 3551 3552
static void caam_aead_alg_init(struct caam_aead_alg *t_alg)
{
	struct aead_alg *alg = &t_alg->aead;

	alg->base.cra_module = THIS_MODULE;
	alg->base.cra_priority = CAAM_CRA_PRIORITY;
	alg->base.cra_ctxsize = sizeof(struct caam_ctx);
3553
	alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
3554 3555 3556 3557 3558

	alg->init = caam_aead_init;
	alg->exit = caam_aead_exit;
}

3559
int caam_algapi_init(struct device *ctrldev)
3560
{
3561
	struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
3562
	int i = 0, err = 0;
3563
	u32 aes_vid, aes_inst, des_inst, md_vid, md_inst, ccha_inst, ptha_inst;
3564
	u32 arc4_inst;
3565
	unsigned int md_limit = SHA512_DIGEST_SIZE;
3566
	bool registered = false, gcm_support;
3567

3568 3569 3570 3571
	/*
	 * Register crypto algorithms the device supports.
	 * First, detect presence and attributes of DES, AES, and MD blocks.
	 */
3572
	if (priv->era < 10) {
3573
		u32 cha_vid, cha_inst, aes_rn;
3574 3575 3576 3577 3578 3579 3580 3581 3582 3583

		cha_vid = rd_reg32(&priv->ctrl->perfmon.cha_id_ls);
		aes_vid = cha_vid & CHA_ID_LS_AES_MASK;
		md_vid = (cha_vid & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;

		cha_inst = rd_reg32(&priv->ctrl->perfmon.cha_num_ls);
		des_inst = (cha_inst & CHA_ID_LS_DES_MASK) >>
			   CHA_ID_LS_DES_SHIFT;
		aes_inst = cha_inst & CHA_ID_LS_AES_MASK;
		md_inst = (cha_inst & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
3584 3585
		arc4_inst = (cha_inst & CHA_ID_LS_ARC4_MASK) >>
			    CHA_ID_LS_ARC4_SHIFT;
3586 3587
		ccha_inst = 0;
		ptha_inst = 0;
3588 3589 3590 3591

		aes_rn = rd_reg32(&priv->ctrl->perfmon.cha_rev_ls) &
			 CHA_ID_LS_AES_MASK;
		gcm_support = !(aes_vid == CHA_VER_VID_AES_LP && aes_rn < 8);
3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603
	} else {
		u32 aesa, mdha;

		aesa = rd_reg32(&priv->ctrl->vreg.aesa);
		mdha = rd_reg32(&priv->ctrl->vreg.mdha);

		aes_vid = (aesa & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;
		md_vid = (mdha & CHA_VER_VID_MASK) >> CHA_VER_VID_SHIFT;

		des_inst = rd_reg32(&priv->ctrl->vreg.desa) & CHA_VER_NUM_MASK;
		aes_inst = aesa & CHA_VER_NUM_MASK;
		md_inst = mdha & CHA_VER_NUM_MASK;
3604 3605
		ccha_inst = rd_reg32(&priv->ctrl->vreg.ccha) & CHA_VER_NUM_MASK;
		ptha_inst = rd_reg32(&priv->ctrl->vreg.ptha) & CHA_VER_NUM_MASK;
3606
		arc4_inst = rd_reg32(&priv->ctrl->vreg.afha) & CHA_VER_NUM_MASK;
3607 3608

		gcm_support = aesa & CHA_VER_MISC_AES_GCM;
3609
	}
3610 3611

	/* If MD is present, limit digest size based on LP256 */
3612
	if (md_inst && md_vid  == CHA_VER_VID_MD_LP256)
3613 3614
		md_limit = SHA256_DIGEST_SIZE;

3615
	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
3616 3617
		struct caam_skcipher_alg *t_alg = driver_algs + i;
		u32 alg_sel = t_alg->caam.class1_alg_type & OP_ALG_ALGSEL_MASK;
3618 3619 3620 3621 3622 3623 3624 3625 3626 3627

		/* Skip DES algorithms if not supported by device */
		if (!des_inst &&
		    ((alg_sel == OP_ALG_ALGSEL_3DES) ||
		     (alg_sel == OP_ALG_ALGSEL_DES)))
				continue;

		/* Skip AES algorithms if not supported by device */
		if (!aes_inst && (alg_sel == OP_ALG_ALGSEL_AES))
				continue;
3628

3629 3630 3631 3632
		/* Skip ARC4 algorithms if not supported by device */
		if (!arc4_inst && alg_sel == OP_ALG_ALGSEL_ARC4)
			continue;

3633 3634 3635 3636
		/*
		 * Check support for AES modes not available
		 * on LP devices.
		 */
3637 3638 3639 3640
		if (aes_vid == CHA_VER_VID_AES_LP &&
		    (t_alg->caam.class1_alg_type & OP_ALG_AAI_MASK) ==
		    OP_ALG_AAI_XTS)
			continue;
3641

3642
		caam_skcipher_alg_init(t_alg);
3643

3644
		err = crypto_register_skcipher(&t_alg->skcipher);
3645
		if (err) {
3646
			pr_warn("%s alg registration failed\n",
3647
				t_alg->skcipher.base.cra_driver_name);
3648 3649 3650
			continue;
		}

3651
		t_alg->registered = true;
3652 3653 3654 3655 3656
		registered = true;
	}

	for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
		struct caam_aead_alg *t_alg = driver_aeads + i;
3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672
		u32 c1_alg_sel = t_alg->caam.class1_alg_type &
				 OP_ALG_ALGSEL_MASK;
		u32 c2_alg_sel = t_alg->caam.class2_alg_type &
				 OP_ALG_ALGSEL_MASK;
		u32 alg_aai = t_alg->caam.class1_alg_type & OP_ALG_AAI_MASK;

		/* Skip DES algorithms if not supported by device */
		if (!des_inst &&
		    ((c1_alg_sel == OP_ALG_ALGSEL_3DES) ||
		     (c1_alg_sel == OP_ALG_ALGSEL_DES)))
				continue;

		/* Skip AES algorithms if not supported by device */
		if (!aes_inst && (c1_alg_sel == OP_ALG_ALGSEL_AES))
				continue;

3673 3674 3675 3676 3677 3678 3679 3680
		/* Skip CHACHA20 algorithms if not supported by device */
		if (c1_alg_sel == OP_ALG_ALGSEL_CHACHA20 && !ccha_inst)
			continue;

		/* Skip POLY1305 algorithms if not supported by device */
		if (c2_alg_sel == OP_ALG_ALGSEL_POLY1305 && !ptha_inst)
			continue;

3681 3682 3683
		/* Skip GCM algorithms if not supported by device */
		if (c1_alg_sel == OP_ALG_ALGSEL_AES &&
		    alg_aai == OP_ALG_AAI_GCM && !gcm_support)
3684
			continue;
3685 3686 3687 3688 3689

		/*
		 * Skip algorithms requiring message digests
		 * if MD or MD size is not supported by device.
		 */
3690
		if (is_mdha(c2_alg_sel) &&
3691 3692
		    (!md_inst || t_alg->aead.maxauthsize > md_limit))
			continue;
3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704

		caam_aead_alg_init(t_alg);

		err = crypto_register_aead(&t_alg->aead);
		if (err) {
			pr_warn("%s alg registration failed\n",
				t_alg->aead.base.cra_driver_name);
			continue;
		}

		t_alg->registered = true;
		registered = true;
3705
	}
3706 3707

	if (registered)
3708
		pr_info("caam algorithms registered in /proc/crypto\n");
3709 3710 3711

	return err;
}