caamalg.c 95.2 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 670
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;

671 672
	print_hex_dump_debug("key in @"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
673 674

	memcpy(ctx->key, key, keylen);
675
	dma_sync_single_for_device(jrdev, ctx->key_dma, keylen, ctx->dir);
676
	ctx->cdata.keylen = keylen;
677

678
	return gcm_set_sh_desc(aead);
679 680
}

681 682 683 684 685 686 687 688 689
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;

	if (keylen < 4)
		return -EINVAL;

690 691
	print_hex_dump_debug("key in @"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
692 693 694 695 696 697 698

	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.
	 */
699
	ctx->cdata.keylen = keylen - 4;
700
	dma_sync_single_for_device(jrdev, ctx->key_dma, ctx->cdata.keylen,
701
				   ctx->dir);
702
	return rfc4106_set_sh_desc(aead);
703 704
}

705 706 707 708 709 710 711 712 713
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;

	if (keylen < 4)
		return -EINVAL;

714 715
	print_hex_dump_debug("key in @"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
716 717 718 719 720 721 722

	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.
	 */
723
	ctx->cdata.keylen = keylen - 4;
724
	dma_sync_single_for_device(jrdev, ctx->key_dma, ctx->cdata.keylen,
725
				   ctx->dir);
726
	return rfc4543_set_sh_desc(aead);
727 728
}

729 730
static int skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
			   unsigned int keylen)
Y
Yuan Kang 已提交
731
{
732 733 734 735
	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 已提交
736
	struct device *jrdev = ctx->jrdev;
737
	unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
738
	u32 *desc;
739
	u32 ctx1_iv_off = 0;
740
	const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
741
			       OP_ALG_AAI_CTR_MOD128);
742
	const bool is_rfc3686 = alg->caam.rfc3686;
Y
Yuan Kang 已提交
743

744 745
	print_hex_dump_debug("key in @"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
746 747 748 749 750 751 752
	/*
	 * 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;
Y
Yuan Kang 已提交
753

754 755 756 757 758 759 760 761 762 763
	/*
	 * RFC3686 specific:
	 *	| CONTEXT1[255:128] = {NONCE, IV, COUNTER}
	 *	| *key = {KEY, NONCE}
	 */
	if (is_rfc3686) {
		ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
		keylen -= CTR_RFC3686_NONCE_SIZE;
	}

764
	ctx->cdata.keylen = keylen;
765
	ctx->cdata.key_virt = key;
766
	ctx->cdata.key_inline = true;
Y
Yuan Kang 已提交
767

768
	/* skcipher_encrypt shared descriptor */
Y
Yuan Kang 已提交
769
	desc = ctx->sh_desc_enc;
770 771
	cnstr_shdsc_skcipher_encap(desc, &ctx->cdata, ivsize, is_rfc3686,
				   ctx1_iv_off);
772
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
773
				   desc_bytes(desc), ctx->dir);
774

775
	/* skcipher_decrypt shared descriptor */
Y
Yuan Kang 已提交
776
	desc = ctx->sh_desc_dec;
777 778
	cnstr_shdsc_skcipher_decap(desc, &ctx->cdata, ivsize, is_rfc3686,
				   ctx1_iv_off);
779
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
780
				   desc_bytes(desc), ctx->dir);
Y
Yuan Kang 已提交
781

782
	return 0;
Y
Yuan Kang 已提交
783 784
}

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
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;
	}

	return skcipher_setkey(skcipher, key, keylen);
}

806 807
static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
			       unsigned int keylen)
808
{
809
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
810
	struct device *jrdev = ctx->jrdev;
811
	u32 *desc;
812 813

	if (keylen != 2 * AES_MIN_KEY_SIZE  && keylen != 2 * AES_MAX_KEY_SIZE) {
814
		crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
815 816 817 818
		dev_err(jrdev, "key size mismatch\n");
		return -EINVAL;
	}

819
	ctx->cdata.keylen = keylen;
820
	ctx->cdata.key_virt = key;
821
	ctx->cdata.key_inline = true;
822

823
	/* xts_skcipher_encrypt shared descriptor */
824
	desc = ctx->sh_desc_enc;
825
	cnstr_shdsc_xts_skcipher_encap(desc, &ctx->cdata);
826
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
827
				   desc_bytes(desc), ctx->dir);
828

829
	/* xts_skcipher_decrypt shared descriptor */
830
	desc = ctx->sh_desc_dec;
831
	cnstr_shdsc_xts_skcipher_decap(desc, &ctx->cdata);
832
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
833
				   desc_bytes(desc), ctx->dir);
834 835 836 837

	return 0;
}

838
/*
839
 * aead_edesc - s/w-extended aead descriptor
840 841
 * @src_nents: number of segments in input s/w scatterlist
 * @dst_nents: number of segments in output s/w scatterlist
842 843
 * @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 已提交
844 845
 * @sec4_sg_bytes: length of dma mapped sec4_sg space
 * @sec4_sg_dma: bus physical mapped address of h/w link table
846
 * @sec4_sg: pointer to h/w link table
847 848
 * @hw_desc: the h/w job descriptor followed by any referenced link tables
 */
Y
Yuan Kang 已提交
849
struct aead_edesc {
850 851
	int src_nents;
	int dst_nents;
852 853
	int mapped_src_nents;
	int mapped_dst_nents;
Y
Yuan Kang 已提交
854 855 856
	int sec4_sg_bytes;
	dma_addr_t sec4_sg_dma;
	struct sec4_sg_entry *sec4_sg;
857
	u32 hw_desc[];
858 859
};

Y
Yuan Kang 已提交
860
/*
861
 * skcipher_edesc - s/w-extended skcipher descriptor
862 863
 * @src_nents: number of segments in input s/w scatterlist
 * @dst_nents: number of segments in output s/w scatterlist
864 865
 * @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 已提交
866
 * @iv_dma: dma address of iv for checking continuity and link table
Y
Yuan Kang 已提交
867 868
 * @sec4_sg_bytes: length of dma mapped sec4_sg space
 * @sec4_sg_dma: bus physical mapped address of h/w link table
869
 * @sec4_sg: pointer to h/w link table
Y
Yuan Kang 已提交
870
 * @hw_desc: the h/w job descriptor followed by any referenced link tables
871
 *	     and IV
Y
Yuan Kang 已提交
872
 */
873
struct skcipher_edesc {
Y
Yuan Kang 已提交
874 875
	int src_nents;
	int dst_nents;
876 877
	int mapped_src_nents;
	int mapped_dst_nents;
Y
Yuan Kang 已提交
878
	dma_addr_t iv_dma;
Y
Yuan Kang 已提交
879 880 881
	int sec4_sg_bytes;
	dma_addr_t sec4_sg_dma;
	struct sec4_sg_entry *sec4_sg;
Y
Yuan Kang 已提交
882 883 884
	u32 hw_desc[0];
};

885
static void caam_unmap(struct device *dev, struct scatterlist *src,
Y
Yuan Kang 已提交
886
		       struct scatterlist *dst, int src_nents,
887
		       int dst_nents,
888
		       dma_addr_t iv_dma, int ivsize, dma_addr_t sec4_sg_dma,
Y
Yuan Kang 已提交
889
		       int sec4_sg_bytes)
890
{
Y
Yuan Kang 已提交
891
	if (dst != src) {
892 893
		if (src_nents)
			dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
894 895
		if (dst_nents)
			dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
896
	} else {
897
		dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
898 899
	}

900
	if (iv_dma)
901
		dma_unmap_single(dev, iv_dma, ivsize, DMA_BIDIRECTIONAL);
Y
Yuan Kang 已提交
902 903
	if (sec4_sg_bytes)
		dma_unmap_single(dev, sec4_sg_dma, sec4_sg_bytes,
904 905 906
				 DMA_TO_DEVICE);
}

907 908 909
static void aead_unmap(struct device *dev,
		       struct aead_edesc *edesc,
		       struct aead_request *req)
910 911
{
	caam_unmap(dev, req->src, req->dst,
912
		   edesc->src_nents, edesc->dst_nents, 0, 0,
913 914 915
		   edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
}

916 917
static void skcipher_unmap(struct device *dev, struct skcipher_edesc *edesc,
			   struct skcipher_request *req)
Y
Yuan Kang 已提交
918
{
919 920
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
921 922

	caam_unmap(dev, req->src, req->dst,
923
		   edesc->src_nents, edesc->dst_nents,
924
		   edesc->iv_dma, ivsize,
Y
Yuan Kang 已提交
925
		   edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
Y
Yuan Kang 已提交
926 927
}

Y
Yuan Kang 已提交
928
static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
929 930
				   void *context)
{
Y
Yuan Kang 已提交
931 932
	struct aead_request *req = context;
	struct aead_edesc *edesc;
933

934
	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
935 936 937 938 939 940 941 942 943 944 945 946 947

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

	if (err)
		caam_jr_strstatus(jrdev, err);

	aead_unmap(jrdev, edesc, req);

	kfree(edesc);

	aead_request_complete(req, err);
}

Y
Yuan Kang 已提交
948
static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
949 950
				   void *context)
{
Y
Yuan Kang 已提交
951 952
	struct aead_request *req = context;
	struct aead_edesc *edesc;
953

954
	dev_dbg(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973

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

	if (err)
		caam_jr_strstatus(jrdev, err);

	aead_unmap(jrdev, edesc, req);

	/*
	 * verify hw auth check passed else return -EBADMSG
	 */
	if ((err & JRSTA_CCBERR_ERRID_MASK) == JRSTA_CCBERR_ERRID_ICVCHK)
		err = -EBADMSG;

	kfree(edesc);

	aead_request_complete(req, err);
}

974 975
static void skcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
				  void *context)
Y
Yuan Kang 已提交
976
{
977 978 979 980
	struct skcipher_request *req = context;
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
981

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

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

986 987
	if (err)
		caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
988

989
	skcipher_unmap(jrdev, edesc, req);
990 991

	/*
992
	 * The crypto API expects us to set the IV (req->iv) to the last
993 994
	 * ciphertext block (CBC mode) or last counter (CTR mode).
	 * This is used e.g. by the CTS mode.
995
	 */
996 997 998
	if (ivsize) {
		memcpy(req->iv, (u8 *)edesc->sec4_sg + edesc->sec4_sg_bytes,
		       ivsize);
999

1000 1001 1002
		print_hex_dump_debug("dstiv  @"__stringify(__LINE__)": ",
				     DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
				     edesc->src_nents > 1 ? 100 : ivsize, 1);
1003
	}
1004

1005
	caam_dump_sg("dst    @" __stringify(__LINE__)": ",
1006 1007 1008
		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);

Y
Yuan Kang 已提交
1009 1010
	kfree(edesc);

1011
	skcipher_request_complete(req, err);
Y
Yuan Kang 已提交
1012 1013
}

1014 1015
static void skcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
				  void *context)
Y
Yuan Kang 已提交
1016
{
1017 1018 1019 1020
	struct skcipher_request *req = context;
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
1021

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

1024
	edesc = container_of(desc, struct skcipher_edesc, hw_desc[0]);
1025 1026
	if (err)
		caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
1027

1028 1029
	skcipher_unmap(jrdev, edesc, req);

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	/*
	 * 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.
	 */
	if (ivsize) {
		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);
	}

1044
	caam_dump_sg("dst    @" __stringify(__LINE__)": ",
1045
		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
1046
		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
Y
Yuan Kang 已提交
1047 1048 1049

	kfree(edesc);

1050
	skcipher_request_complete(req, err);
Y
Yuan Kang 已提交
1051 1052
}

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
/*
 * 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) {
1077 1078
		src_dma = edesc->mapped_src_nents ? sg_dma_address(req->src) :
						    0;
1079 1080 1081
		in_options = 0;
	} else {
		src_dma = edesc->sec4_sg_dma;
1082
		sec4_sg_index += edesc->mapped_src_nents;
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
		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)) {
1093
		if (!edesc->mapped_dst_nents) {
1094
			dst_dma = 0;
1095
			out_options = 0;
1096
		} else if (edesc->mapped_dst_nents == 1) {
1097
			dst_dma = sg_dma_address(req->dst);
1098
			out_options = 0;
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
		} 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;
1125
	bool generic_gcm = (ivsize == GCM_AES_IV_SIZE);
1126 1127 1128
	unsigned int last;

	init_aead_job(req, edesc, all_contig, encrypt);
1129
	append_math_add_imm_u32(desc, REG3, ZERO, IMM, req->assoclen);
1130 1131 1132 1133 1134 1135 1136 1137

	/* 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 |
1138
			 FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1 | GCM_AES_IV_SIZE | last);
1139 1140
	/* Append Salt */
	if (!generic_gcm)
1141
		append_data(desc, ctx->key + ctx->cdata.keylen, 4);
1142 1143 1144 1145 1146
	/* Append IV */
	append_data(desc, req->iv, ivsize);
	/* End of blank commands */
}

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
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);
}

1181 1182 1183
static void init_authenc_job(struct aead_request *req,
			     struct aead_edesc *edesc,
			     bool all_contig, bool encrypt)
1184 1185
{
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1186 1187 1188
	struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
						 struct caam_aead_alg, aead);
	unsigned int ivsize = crypto_aead_ivsize(aead);
1189
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
1190
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
1191
	const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
1192 1193
			       OP_ALG_AAI_CTR_MOD128);
	const bool is_rfc3686 = alg->caam.rfc3686;
1194
	u32 *desc = edesc->hw_desc;
1195
	u32 ivoffset = 0;
1196

1197 1198 1199 1200 1201 1202 1203
	/*
	 * AES-CTR needs to load IV in CONTEXT1 reg
	 * at an offset of 128bits (16bytes)
	 * CONTEXT1[255:128] = IV
	 */
	if (ctr_mode)
		ivoffset = 16;
1204

1205 1206 1207 1208 1209 1210
	/*
	 * RFC3686 specific:
	 *	CONTEXT1[255:128] = {NONCE, IV, COUNTER}
	 */
	if (is_rfc3686)
		ivoffset = 16 + CTR_RFC3686_NONCE_SIZE;
1211

1212
	init_aead_job(req, edesc, all_contig, encrypt);
1213

1214 1215 1216 1217 1218 1219 1220 1221 1222
	/*
	 * {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);

1223
	if (ivsize && ((is_rfc3686 && encrypt) || !alg->caam.geniv))
1224 1225 1226 1227
		append_load_as_imm(desc, req->iv, ivsize,
				   LDST_CLASS_1_CCB |
				   LDST_SRCDST_BYTE_CONTEXT |
				   (ivoffset << LDST_OFFSET_SHIFT));
1228 1229
}

Y
Yuan Kang 已提交
1230
/*
1231
 * Fill in skcipher job descriptor
Y
Yuan Kang 已提交
1232
 */
1233 1234 1235
static void init_skcipher_job(struct skcipher_request *req,
			      struct skcipher_edesc *edesc,
			      const bool encrypt)
Y
Yuan Kang 已提交
1236
{
1237 1238
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
1239
	struct device *jrdev = ctx->jrdev;
1240
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
1241
	u32 *desc = edesc->hw_desc;
1242
	u32 *sh_desc;
1243 1244 1245
	u32 in_options = 0, out_options = 0;
	dma_addr_t src_dma, dst_dma, ptr;
	int len, sec4_sg_index = 0;
Y
Yuan Kang 已提交
1246

1247 1248 1249
	print_hex_dump_debug("presciv@"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, req->iv, ivsize, 1);
	dev_dbg(jrdev, "asked=%d, cryptlen%d\n",
1250
	       (int)edesc->src_nents > 1 ? 100 : req->cryptlen, req->cryptlen);
1251

1252
	caam_dump_sg("src    @" __stringify(__LINE__)": ",
1253
		     DUMP_PREFIX_ADDRESS, 16, 4, req->src,
1254 1255 1256 1257
		     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 已提交
1258 1259 1260 1261

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

1262 1263 1264 1265 1266 1267 1268 1269 1270
	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 已提交
1271 1272

	if (likely(req->src == req->dst)) {
1273 1274
		dst_dma = src_dma + !!ivsize * sizeof(struct sec4_sg_entry);
		out_options = in_options;
1275
	} else if (!ivsize && edesc->mapped_dst_nents == 1) {
1276
		dst_dma = sg_dma_address(req->dst);
Y
Yuan Kang 已提交
1277
	} else {
1278 1279 1280
		dst_dma = edesc->sec4_sg_dma + sec4_sg_index *
			  sizeof(struct sec4_sg_entry);
		out_options = LDST_SGF;
Y
Yuan Kang 已提交
1281
	}
1282

1283
	append_seq_out_ptr(desc, dst_dma, req->cryptlen + ivsize, out_options);
Y
Yuan Kang 已提交
1284 1285
}

1286
/*
1287
 * allocate and map the aead extended descriptor
1288
 */
1289 1290 1291
static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
					   int desc_bytes, bool *all_contig_ptr,
					   bool encrypt)
1292
{
Y
Yuan Kang 已提交
1293
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1294 1295
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1296 1297
	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
		       GFP_KERNEL : GFP_ATOMIC;
1298
	int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
1299
	int src_len, dst_len = 0;
Y
Yuan Kang 已提交
1300
	struct aead_edesc *edesc;
1301
	int sec4_sg_index, sec4_sg_len, sec4_sg_bytes;
1302
	unsigned int authsize = ctx->authsize;
1303

1304
	if (unlikely(req->dst != req->src)) {
1305 1306 1307 1308
		src_len = req->assoclen + req->cryptlen;
		dst_len = src_len + (encrypt ? authsize : (-authsize));

		src_nents = sg_nents_for_len(req->src, src_len);
1309 1310
		if (unlikely(src_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
1311
				src_len);
1312 1313 1314
			return ERR_PTR(src_nents);
		}

1315
		dst_nents = sg_nents_for_len(req->dst, dst_len);
1316 1317
		if (unlikely(dst_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in dst S/G\n",
1318
				dst_len);
1319 1320
			return ERR_PTR(dst_nents);
		}
1321
	} else {
1322 1323 1324 1325
		src_len = req->assoclen + req->cryptlen +
			  (encrypt ? authsize : 0);

		src_nents = sg_nents_for_len(req->src, src_len);
1326 1327
		if (unlikely(src_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
1328
				src_len);
1329 1330
			return ERR_PTR(src_nents);
		}
1331
	}
1332

1333
	if (likely(req->src == req->dst)) {
1334 1335 1336
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_BIDIRECTIONAL);
		if (unlikely(!mapped_src_nents)) {
1337 1338 1339 1340
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}
	} else {
1341 1342
		/* Cover also the case of null (zero length) input data */
		if (src_nents) {
1343 1344 1345
			mapped_src_nents = dma_map_sg(jrdev, req->src,
						      src_nents, DMA_TO_DEVICE);
			if (unlikely(!mapped_src_nents)) {
1346 1347 1348
				dev_err(jrdev, "unable to map source\n");
				return ERR_PTR(-ENOMEM);
			}
1349 1350
		} else {
			mapped_src_nents = 0;
1351 1352
		}

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
		/* 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;
1366 1367 1368
		}
	}

1369 1370 1371 1372
	/*
	 * 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.
	 */
1373
	sec4_sg_len = mapped_src_nents > 1 ? mapped_src_nents : 0;
1374 1375 1376 1377 1378
	if (mapped_dst_nents > 1)
		sec4_sg_len += pad_sg_nents(mapped_dst_nents);
	else
		sec4_sg_len = pad_sg_nents(sec4_sg_len);

1379 1380 1381 1382 1383 1384 1385
	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,
1386
			   0, 0, 0);
1387 1388 1389
		return ERR_PTR(-ENOMEM);
	}

1390 1391
	edesc->src_nents = src_nents;
	edesc->dst_nents = dst_nents;
1392 1393
	edesc->mapped_src_nents = mapped_src_nents;
	edesc->mapped_dst_nents = mapped_dst_nents;
Y
Yuan Kang 已提交
1394 1395
	edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
			 desc_bytes;
1396
	*all_contig_ptr = !(mapped_src_nents > 1);
1397

Y
Yuan Kang 已提交
1398
	sec4_sg_index = 0;
1399
	if (mapped_src_nents > 1) {
1400
		sg_to_sec4_sg_last(req->src, src_len,
1401 1402
				   edesc->sec4_sg + sec4_sg_index, 0);
		sec4_sg_index += mapped_src_nents;
1403
	}
1404
	if (mapped_dst_nents > 1) {
1405
		sg_to_sec4_sg_last(req->dst, dst_len,
Y
Yuan Kang 已提交
1406
				   edesc->sec4_sg + sec4_sg_index, 0);
1407
	}
1408 1409 1410 1411

	if (!sec4_sg_bytes)
		return edesc;

1412 1413
	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
					    sec4_sg_bytes, DMA_TO_DEVICE);
1414 1415
	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
		dev_err(jrdev, "unable to map S/G table\n");
1416 1417
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
1418 1419
		return ERR_PTR(-ENOMEM);
	}
1420

1421 1422
	edesc->sec4_sg_bytes = sec4_sg_bytes;

1423 1424 1425
	return edesc;
}

1426
static int gcm_encrypt(struct aead_request *req)
1427
{
Y
Yuan Kang 已提交
1428 1429
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1430 1431
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1432
	bool all_contig;
1433
	u32 *desc;
1434 1435
	int ret = 0;

1436
	/* allocate extended descriptor */
1437
	edesc = aead_edesc_alloc(req, GCM_DESC_JOB_IO_LEN, &all_contig, true);
1438 1439 1440
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

1441
	/* Create and submit job descriptor */
1442
	init_gcm_job(req, edesc, all_contig, true);
1443 1444 1445 1446

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

1448 1449 1450 1451 1452 1453 1454 1455
	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);
	}
1456

1457
	return ret;
1458 1459
}

1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
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;
}

1526 1527 1528 1529 1530 1531 1532 1533
static int ipsec_gcm_encrypt(struct aead_request *req)
{
	if (req->assoclen < 8)
		return -EINVAL;

	return gcm_encrypt(req);
}

1534
static int aead_encrypt(struct aead_request *req)
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
{
	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 */
1545 1546
	edesc = aead_edesc_alloc(req, AUTHENC_DESC_JOB_IO_LEN,
				 &all_contig, true);
1547 1548 1549 1550
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor */
1551
	init_authenc_job(req, edesc, all_contig, true);
1552 1553 1554 1555

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

	desc = edesc->hw_desc;
1558
	ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
1559 1560 1561
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1562
		aead_unmap(jrdev, edesc, req);
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
		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);
1586 1587 1588 1589

	print_hex_dump_debug("aead jobdesc@"__stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
			     desc_bytes(edesc->hw_desc), 1);
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602

	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;
}

1603 1604 1605 1606 1607 1608 1609 1610
static int ipsec_gcm_decrypt(struct aead_request *req)
{
	if (req->assoclen < 8)
		return -EINVAL;

	return gcm_decrypt(req);
}

1611
static int aead_decrypt(struct aead_request *req)
1612
{
1613
	struct aead_edesc *edesc;
1614 1615 1616
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1617
	bool all_contig;
1618
	u32 *desc;
1619
	int ret = 0;
1620

1621
	caam_dump_sg("dec src@" __stringify(__LINE__)": ",
1622 1623
		     DUMP_PREFIX_ADDRESS, 16, 4, req->src,
		     req->assoclen + req->cryptlen, 1);
C
Catalin Vasile 已提交
1624

1625
	/* allocate extended descriptor */
1626 1627
	edesc = aead_edesc_alloc(req, AUTHENC_DESC_JOB_IO_LEN,
				 &all_contig, false);
1628 1629 1630
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

1631
	/* Create and submit job descriptor*/
1632
	init_authenc_job(req, edesc, all_contig, false);
1633 1634 1635 1636

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

1638
	desc = edesc->hw_desc;
1639
	ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
1640 1641 1642
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1643
		aead_unmap(jrdev, edesc, req);
1644 1645
		kfree(edesc);
	}
1646

1647 1648
	return ret;
}
1649

Y
Yuan Kang 已提交
1650
/*
1651
 * allocate and map the skcipher extended descriptor for skcipher
Y
Yuan Kang 已提交
1652
 */
1653 1654
static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req,
						   int desc_bytes)
Y
Yuan Kang 已提交
1655
{
1656 1657
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
Y
Yuan Kang 已提交
1658
	struct device *jrdev = ctx->jrdev;
1659
	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
Y
Yuan Kang 已提交
1660
		       GFP_KERNEL : GFP_ATOMIC;
1661
	int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
1662
	struct skcipher_edesc *edesc;
1663
	dma_addr_t iv_dma = 0;
1664
	u8 *iv;
1665
	int ivsize = crypto_skcipher_ivsize(skcipher);
1666
	int dst_sg_idx, sec4_sg_ents, sec4_sg_bytes;
Y
Yuan Kang 已提交
1667

1668
	src_nents = sg_nents_for_len(req->src, req->cryptlen);
1669 1670
	if (unlikely(src_nents < 0)) {
		dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
1671
			req->cryptlen);
1672 1673
		return ERR_PTR(src_nents);
	}
Y
Yuan Kang 已提交
1674

1675
	if (req->dst != req->src) {
1676
		dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
1677 1678
		if (unlikely(dst_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in dst S/G\n",
1679
				req->cryptlen);
1680 1681 1682
			return ERR_PTR(dst_nents);
		}
	}
Y
Yuan Kang 已提交
1683 1684

	if (likely(req->src == req->dst)) {
1685 1686 1687
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_BIDIRECTIONAL);
		if (unlikely(!mapped_src_nents)) {
1688 1689 1690
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}
Y
Yuan Kang 已提交
1691
	} else {
1692 1693 1694
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_TO_DEVICE);
		if (unlikely(!mapped_src_nents)) {
1695 1696 1697
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}
1698 1699 1700
		mapped_dst_nents = dma_map_sg(jrdev, req->dst, dst_nents,
					      DMA_FROM_DEVICE);
		if (unlikely(!mapped_dst_nents)) {
1701
			dev_err(jrdev, "unable to map destination\n");
1702
			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1703 1704
			return ERR_PTR(-ENOMEM);
		}
Y
Yuan Kang 已提交
1705 1706
	}

1707 1708 1709 1710
	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;
1711
	dst_sg_idx = sec4_sg_ents;
1712 1713

	/*
1714 1715 1716 1717
	 * 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)
	 *
1718 1719
	 * 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:
1720
	 * if (output S/G)
1721 1722 1723 1724
	 *      pad output S/G, if needed
	 * else if (input S/G) ...
	 *      pad input S/G, if needed
	 */
1725 1726 1727 1728 1729 1730 1731
	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 {
1732
		sec4_sg_ents = pad_sg_nents(sec4_sg_ents);
1733
	}
1734

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

1737 1738 1739 1740
	/*
	 * allocate space for base edesc and hw desc commands, link tables, IV
	 */
	edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes + ivsize,
1741
			GFP_DMA | flags);
Y
Yuan Kang 已提交
1742 1743
	if (!edesc) {
		dev_err(jrdev, "could not allocate extended descriptor\n");
1744
		caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0,
1745
			   0, 0, 0);
Y
Yuan Kang 已提交
1746 1747 1748 1749 1750
		return ERR_PTR(-ENOMEM);
	}

	edesc->src_nents = src_nents;
	edesc->dst_nents = dst_nents;
1751 1752
	edesc->mapped_src_nents = mapped_src_nents;
	edesc->mapped_dst_nents = mapped_dst_nents;
Y
Yuan Kang 已提交
1753
	edesc->sec4_sg_bytes = sec4_sg_bytes;
1754 1755
	edesc->sec4_sg = (struct sec4_sg_entry *)((u8 *)edesc->hw_desc +
						  desc_bytes);
Y
Yuan Kang 已提交
1756

1757
	/* Make sure IV is located in a DMAable area */
1758
	if (ivsize) {
1759
		iv = (u8 *)edesc->sec4_sg + sec4_sg_bytes;
1760 1761
		memcpy(iv, req->iv, ivsize);

1762
		iv_dma = dma_map_single(jrdev, iv, ivsize, DMA_BIDIRECTIONAL);
1763 1764 1765 1766 1767 1768 1769
		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);
		}
1770

1771
		dma_to_sec4_sg_one(edesc->sec4_sg, iv_dma, ivsize, 0);
Y
Yuan Kang 已提交
1772
	}
1773
	if (dst_sg_idx)
1774 1775
		sg_to_sec4_sg(req->src, req->cryptlen, edesc->sec4_sg +
			      !!ivsize, 0);
1776

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
	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 已提交
1788

1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
	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);
		}
1800 1801
	}

Y
Yuan Kang 已提交
1802 1803
	edesc->iv_dma = iv_dma;

1804 1805 1806
	print_hex_dump_debug("skcipher sec4_sg@" __stringify(__LINE__)": ",
			     DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
			     sec4_sg_bytes, 1);
Y
Yuan Kang 已提交
1807 1808 1809 1810

	return edesc;
}

1811
static int skcipher_encrypt(struct skcipher_request *req)
Y
Yuan Kang 已提交
1812
{
1813 1814 1815
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
Y
Yuan Kang 已提交
1816 1817 1818 1819 1820
	struct device *jrdev = ctx->jrdev;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
1821
	edesc = skcipher_edesc_alloc(req, DESC_JOB_IO_LEN * CAAM_CMD_SZ);
Y
Yuan Kang 已提交
1822 1823 1824 1825
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor*/
1826
	init_skcipher_job(req, edesc, true);
1827 1828 1829 1830 1831

	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 已提交
1832
	desc = edesc->hw_desc;
1833
	ret = caam_jr_enqueue(jrdev, desc, skcipher_encrypt_done, req);
Y
Yuan Kang 已提交
1834 1835 1836 1837

	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1838
		skcipher_unmap(jrdev, edesc, req);
Y
Yuan Kang 已提交
1839 1840 1841 1842 1843 1844
		kfree(edesc);
	}

	return ret;
}

1845
static int skcipher_decrypt(struct skcipher_request *req)
Y
Yuan Kang 已提交
1846
{
1847 1848 1849
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
Y
Yuan Kang 已提交
1850 1851 1852 1853 1854
	struct device *jrdev = ctx->jrdev;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
1855
	edesc = skcipher_edesc_alloc(req, DESC_JOB_IO_LEN * CAAM_CMD_SZ);
Y
Yuan Kang 已提交
1856 1857 1858 1859
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor*/
1860
	init_skcipher_job(req, edesc, false);
Y
Yuan Kang 已提交
1861
	desc = edesc->hw_desc;
1862 1863 1864 1865

	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 已提交
1866

1867
	ret = caam_jr_enqueue(jrdev, desc, skcipher_decrypt_done, req);
Y
Yuan Kang 已提交
1868 1869 1870
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1871
		skcipher_unmap(jrdev, edesc, req);
Y
Yuan Kang 已提交
1872 1873 1874 1875 1876 1877
		kfree(edesc);
	}

	return ret;
}

1878
static struct caam_skcipher_alg driver_algs[] = {
1879
	{
1880 1881 1882 1883 1884 1885 1886 1887 1888
		.skcipher = {
			.base = {
				.cra_name = "cbc(aes)",
				.cra_driver_name = "cbc-aes-caam",
				.cra_blocksize = AES_BLOCK_SIZE,
			},
			.setkey = skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1889 1890 1891
			.min_keysize = AES_MIN_KEY_SIZE,
			.max_keysize = AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
1892 1893
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
1894 1895
	},
	{
1896 1897 1898 1899 1900 1901
		.skcipher = {
			.base = {
				.cra_name = "cbc(des3_ede)",
				.cra_driver_name = "cbc-3des-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
1902
			.setkey = des_skcipher_setkey,
1903 1904
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1905 1906 1907
			.min_keysize = DES3_EDE_KEY_SIZE,
			.max_keysize = DES3_EDE_KEY_SIZE,
			.ivsize = DES3_EDE_BLOCK_SIZE,
1908 1909
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
1910 1911
	},
	{
1912 1913 1914 1915 1916 1917
		.skcipher = {
			.base = {
				.cra_name = "cbc(des)",
				.cra_driver_name = "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
1918
			.setkey = des_skcipher_setkey,
1919 1920
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1921 1922 1923
			.min_keysize = DES_KEY_SIZE,
			.max_keysize = DES_KEY_SIZE,
			.ivsize = DES_BLOCK_SIZE,
1924 1925
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
1926 1927
	},
	{
1928 1929 1930 1931 1932 1933 1934 1935 1936
		.skcipher = {
			.base = {
				.cra_name = "ctr(aes)",
				.cra_driver_name = "ctr-aes-caam",
				.cra_blocksize = 1,
			},
			.setkey = skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1937 1938 1939
			.min_keysize = AES_MIN_KEY_SIZE,
			.max_keysize = AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
1940 1941 1942 1943
			.chunksize = AES_BLOCK_SIZE,
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES |
					OP_ALG_AAI_CTR_MOD128,
1944 1945
	},
	{
1946 1947 1948 1949 1950 1951 1952 1953 1954
		.skcipher = {
			.base = {
				.cra_name = "rfc3686(ctr(aes))",
				.cra_driver_name = "rfc3686-ctr-aes-caam",
				.cra_blocksize = 1,
			},
			.setkey = skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1955 1956 1957 1958 1959
			.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,
1960 1961 1962 1963 1964 1965 1966
			.chunksize = AES_BLOCK_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.rfc3686 = true,
		},
1967 1968
	},
	{
1969 1970 1971 1972 1973 1974 1975 1976 1977
		.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,
1978 1979 1980
			.min_keysize = 2 * AES_MIN_KEY_SIZE,
			.max_keysize = 2 * AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
1981 1982
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XTS,
1983
	},
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
	{
		.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,
			},
			.setkey = skcipher_setkey,
			.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,
			},
			.setkey = skcipher_setkey,
			.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,
	},
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
};

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

3376 3377
static int caam_init_common(struct caam_ctx *ctx, struct caam_alg_entry *caam,
			    bool uses_dkp)
3378
{
3379
	dma_addr_t dma_addr;
3380
	struct caam_drv_private *priv;
3381

3382 3383 3384 3385 3386
	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);
	}
3387

3388 3389 3390 3391 3392 3393
	priv = dev_get_drvdata(ctx->jrdev->parent);
	if (priv->era >= 6 && uses_dkp)
		ctx->dir = DMA_BIDIRECTIONAL;
	else
		ctx->dir = DMA_TO_DEVICE;

3394 3395 3396
	dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_enc,
					offsetof(struct caam_ctx,
						 sh_desc_enc_dma),
3397
					ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408
	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);

3409
	/* copy descriptor header template value */
3410 3411
	ctx->cdata.algtype = OP_TYPE_CLASS1_ALG | caam->class1_alg_type;
	ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam->class2_alg_type;
3412 3413 3414 3415

	return 0;
}

3416
static int caam_cra_init(struct crypto_skcipher *tfm)
3417
{
3418 3419 3420
	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
	struct caam_skcipher_alg *caam_alg =
		container_of(alg, typeof(*caam_alg), skcipher);
3421

3422 3423
	return caam_init_common(crypto_skcipher_ctx(tfm), &caam_alg->caam,
				false);
3424 3425 3426 3427 3428 3429 3430 3431 3432
}

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);

3433
	return caam_init_common(ctx, &caam_alg->caam, !caam_alg->caam.nodkp);
3434 3435 3436 3437
}

static void caam_exit_common(struct caam_ctx *ctx)
{
3438 3439
	dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_enc_dma,
			       offsetof(struct caam_ctx, sh_desc_enc_dma),
3440
			       ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
3441
	caam_jr_free(ctx->jrdev);
3442 3443
}

3444
static void caam_cra_exit(struct crypto_skcipher *tfm)
3445
{
3446
	caam_exit_common(crypto_skcipher_ctx(tfm));
3447 3448 3449 3450 3451 3452 3453
}

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

3454
void caam_algapi_exit(void)
3455
{
3456 3457 3458 3459 3460 3461 3462 3463
	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);
	}
3464

3465 3466
	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
		struct caam_skcipher_alg *t_alg = driver_algs + i;
3467

3468 3469
		if (t_alg->registered)
			crypto_unregister_skcipher(&t_alg->skcipher);
3470 3471 3472
	}
}

3473
static void caam_skcipher_alg_init(struct caam_skcipher_alg *t_alg)
3474
{
3475
	struct skcipher_alg *alg = &t_alg->skcipher;
3476

3477 3478 3479 3480
	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;
3481

3482 3483
	alg->init = caam_cra_init;
	alg->exit = caam_cra_exit;
3484 3485
}

3486 3487 3488 3489 3490 3491 3492
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);
3493
	alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
3494 3495 3496 3497 3498

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

3499
int caam_algapi_init(struct device *ctrldev)
3500
{
3501
	struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
3502
	int i = 0, err = 0;
3503
	u32 aes_vid, aes_inst, des_inst, md_vid, md_inst, ccha_inst, ptha_inst;
3504
	u32 arc4_inst;
3505
	unsigned int md_limit = SHA512_DIGEST_SIZE;
3506
	bool registered = false, gcm_support;
3507

3508 3509 3510 3511
	/*
	 * Register crypto algorithms the device supports.
	 * First, detect presence and attributes of DES, AES, and MD blocks.
	 */
3512
	if (priv->era < 10) {
3513
		u32 cha_vid, cha_inst, aes_rn;
3514 3515 3516 3517 3518 3519 3520 3521 3522 3523

		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;
3524 3525
		arc4_inst = (cha_inst & CHA_ID_LS_ARC4_MASK) >>
			    CHA_ID_LS_ARC4_SHIFT;
3526 3527
		ccha_inst = 0;
		ptha_inst = 0;
3528 3529 3530 3531

		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);
3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543
	} 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;
3544 3545
		ccha_inst = rd_reg32(&priv->ctrl->vreg.ccha) & CHA_VER_NUM_MASK;
		ptha_inst = rd_reg32(&priv->ctrl->vreg.ptha) & CHA_VER_NUM_MASK;
3546
		arc4_inst = rd_reg32(&priv->ctrl->vreg.afha) & CHA_VER_NUM_MASK;
3547 3548

		gcm_support = aesa & CHA_VER_MISC_AES_GCM;
3549
	}
3550 3551

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

3555
	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
3556 3557
		struct caam_skcipher_alg *t_alg = driver_algs + i;
		u32 alg_sel = t_alg->caam.class1_alg_type & OP_ALG_ALGSEL_MASK;
3558 3559 3560 3561 3562 3563 3564 3565 3566 3567

		/* 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;
3568

3569 3570 3571 3572
		/* Skip ARC4 algorithms if not supported by device */
		if (!arc4_inst && alg_sel == OP_ALG_ALGSEL_ARC4)
			continue;

3573 3574 3575 3576
		/*
		 * Check support for AES modes not available
		 * on LP devices.
		 */
3577 3578 3579 3580
		if (aes_vid == CHA_VER_VID_AES_LP &&
		    (t_alg->caam.class1_alg_type & OP_ALG_AAI_MASK) ==
		    OP_ALG_AAI_XTS)
			continue;
3581

3582
		caam_skcipher_alg_init(t_alg);
3583

3584
		err = crypto_register_skcipher(&t_alg->skcipher);
3585
		if (err) {
3586
			pr_warn("%s alg registration failed\n",
3587
				t_alg->skcipher.base.cra_driver_name);
3588 3589 3590
			continue;
		}

3591
		t_alg->registered = true;
3592 3593 3594 3595 3596
		registered = true;
	}

	for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
		struct caam_aead_alg *t_alg = driver_aeads + i;
3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612
		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;

3613 3614 3615 3616 3617 3618 3619 3620
		/* 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;

3621 3622 3623
		/* Skip GCM algorithms if not supported by device */
		if (c1_alg_sel == OP_ALG_ALGSEL_AES &&
		    alg_aai == OP_ALG_AAI_GCM && !gcm_support)
3624
			continue;
3625 3626 3627 3628 3629

		/*
		 * Skip algorithms requiring message digests
		 * if MD or MD size is not supported by device.
		 */
3630
		if (is_mdha(c2_alg_sel) &&
3631 3632
		    (!md_inst || t_alg->aead.maxauthsize > md_limit))
			continue;
3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644

		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;
3645
	}
3646 3647

	if (registered)
3648
		pr_info("caam algorithms registered in /proc/crypto\n");
3649 3650 3651

	return err;
}