caamalg.c 96.4 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 85
#ifdef DEBUG
/* for print_hex_dumps with line references */
#define debug(format, arg...) printk(format, arg)
#else
#define debug(format, arg...)
#endif
C
Catalin Vasile 已提交
86

87 88 89 90 91
struct caam_alg_entry {
	int class1_alg_type;
	int class2_alg_type;
	bool rfc3686;
	bool geniv;
92
	bool nodkp;
93 94 95 96 97 98 99 100
};

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

101 102 103 104 105 106
struct caam_skcipher_alg {
	struct skcipher_alg skcipher;
	struct caam_alg_entry caam;
	bool registered;
};

107 108 109 110
/*
 * per-session context
 */
struct caam_ctx {
111 112
	u32 sh_desc_enc[DESC_MAX_USED_LEN];
	u32 sh_desc_dec[DESC_MAX_USED_LEN];
113
	u8 key[CAAM_MAX_KEY_SIZE];
114 115
	dma_addr_t sh_desc_enc_dma;
	dma_addr_t sh_desc_dec_dma;
Y
Yuan Kang 已提交
116
	dma_addr_t key_dma;
117
	enum dma_data_direction dir;
118
	struct device *jrdev;
119 120
	struct alginfo adata;
	struct alginfo cdata;
121 122 123
	unsigned int authsize;
};

124 125 126 127
static int aead_null_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
128
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
129
	u32 *desc;
130 131
	int rem_bytes = CAAM_DESC_BYTES_MAX - AEAD_DESC_JOB_IO_LEN -
			ctx->adata.keylen_pad;
132 133 134 135 136

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
137
	if (rem_bytes >= DESC_AEAD_NULL_ENC_LEN) {
138
		ctx->adata.key_inline = true;
139
		ctx->adata.key_virt = ctx->key;
140 141
	} else {
		ctx->adata.key_inline = false;
142
		ctx->adata.key_dma = ctx->key_dma;
143
	}
144

145
	/* aead_encrypt shared descriptor */
146
	desc = ctx->sh_desc_enc;
147 148
	cnstr_shdsc_aead_null_encap(desc, &ctx->adata, ctx->authsize,
				    ctrlpriv->era);
149
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
150
				   desc_bytes(desc), ctx->dir);
151 152 153 154 155

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
156
	if (rem_bytes >= DESC_AEAD_NULL_DEC_LEN) {
157
		ctx->adata.key_inline = true;
158
		ctx->adata.key_virt = ctx->key;
159 160
	} else {
		ctx->adata.key_inline = false;
161
		ctx->adata.key_dma = ctx->key_dma;
162
	}
163

164
	/* aead_decrypt shared descriptor */
165
	desc = ctx->sh_desc_dec;
166 167
	cnstr_shdsc_aead_null_decap(desc, &ctx->adata, ctx->authsize,
				    ctrlpriv->era);
168
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
169
				   desc_bytes(desc), ctx->dir);
170 171 172 173

	return 0;
}

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

190 191 192
	if (!ctx->authsize)
		return 0;

193
	/* NULL encryption / decryption */
194
	if (!ctx->cdata.keylen)
195 196
		return aead_null_set_sh_desc(aead);

197 198 199 200 201 202 203 204 205 206 207 208
	/*
	 * 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}
	 */
209
	if (is_rfc3686) {
210
		ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
211 212 213
		nonce = (u32 *)((void *)ctx->key + ctx->adata.keylen_pad +
				ctx->cdata.keylen - CTR_RFC3686_NONCE_SIZE);
	}
214

215 216 217
	data_len[0] = ctx->adata.keylen_pad;
	data_len[1] = ctx->cdata.keylen;

218 219 220
	if (alg->caam.geniv)
		goto skip_enc;

221 222 223 224
	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
225 226 227 228 229 230 231
	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)
232
		ctx->adata.key_virt = ctx->key;
233
	else
234
		ctx->adata.key_dma = ctx->key_dma;
235 236

	if (inl_mask & 2)
237
		ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
238
	else
239
		ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
240 241 242

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

244
	/* aead_encrypt shared descriptor */
245
	desc = ctx->sh_desc_enc;
246 247
	cnstr_shdsc_aead_encap(desc, &ctx->cdata, &ctx->adata, ivsize,
			       ctx->authsize, is_rfc3686, nonce, ctx1_iv_off,
248
			       false, ctrlpriv->era);
249
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
250
				   desc_bytes(desc), ctx->dir);
251

252
skip_enc:
253 254 255 256
	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
257 258 259 260 261 262 263
	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)
264
		ctx->adata.key_virt = ctx->key;
265
	else
266
		ctx->adata.key_dma = ctx->key_dma;
267 268

	if (inl_mask & 2)
269
		ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
270
	else
271
		ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
272 273 274

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

276
	/* aead_decrypt shared descriptor */
277
	desc = ctx->sh_desc_dec;
278 279
	cnstr_shdsc_aead_decap(desc, &ctx->cdata, &ctx->adata, ivsize,
			       ctx->authsize, alg->caam.geniv, is_rfc3686,
280
			       nonce, ctx1_iv_off, false, ctrlpriv->era);
281
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
282
				   desc_bytes(desc), ctx->dir);
283

284 285 286
	if (!alg->caam.geniv)
		goto skip_givenc;

287 288 289 290
	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
291 292 293 294 295 296 297
	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)
298
		ctx->adata.key_virt = ctx->key;
299
	else
300
		ctx->adata.key_dma = ctx->key_dma;
301 302

	if (inl_mask & 2)
303
		ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
304
	else
305
		ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
306 307 308

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

	/* aead_givencrypt shared descriptor */
311
	desc = ctx->sh_desc_enc;
312 313
	cnstr_shdsc_aead_givencap(desc, &ctx->cdata, &ctx->adata, ivsize,
				  ctx->authsize, is_rfc3686, nonce,
314
				  ctx1_iv_off, false, ctrlpriv->era);
315
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
316
				   desc_bytes(desc), ctx->dir);
317

318
skip_givenc:
319 320 321
	return 0;
}

Y
Yuan Kang 已提交
322
static int aead_setauthsize(struct crypto_aead *authenc,
323 324 325 326 327
				    unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(authenc);

	ctx->authsize = authsize;
328
	aead_set_sh_desc(authenc);
329 330 331 332

	return 0;
}

333 334 335 336
static int gcm_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
337
	unsigned int ivsize = crypto_aead_ivsize(aead);
338
	u32 *desc;
339 340
	int rem_bytes = CAAM_DESC_BYTES_MAX - GCM_DESC_JOB_IO_LEN -
			ctx->cdata.keylen;
341

342
	if (!ctx->cdata.keylen || !ctx->authsize)
343 344 345 346 347 348 349
		return 0;

	/*
	 * AES GCM encrypt shared descriptor
	 * Job Descriptor and Shared Descriptor
	 * must fit into the 64-word Descriptor h/w Buffer
	 */
350
	if (rem_bytes >= DESC_GCM_ENC_LEN) {
351
		ctx->cdata.key_inline = true;
352
		ctx->cdata.key_virt = ctx->key;
353 354
	} else {
		ctx->cdata.key_inline = false;
355
		ctx->cdata.key_dma = ctx->key_dma;
356
	}
357 358

	desc = ctx->sh_desc_enc;
359
	cnstr_shdsc_gcm_encap(desc, &ctx->cdata, ivsize, ctx->authsize, false);
360
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
361
				   desc_bytes(desc), ctx->dir);
362 363 364 365 366

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
367
	if (rem_bytes >= DESC_GCM_DEC_LEN) {
368
		ctx->cdata.key_inline = true;
369
		ctx->cdata.key_virt = ctx->key;
370 371
	} else {
		ctx->cdata.key_inline = false;
372
		ctx->cdata.key_dma = ctx->key_dma;
373
	}
374 375

	desc = ctx->sh_desc_dec;
376
	cnstr_shdsc_gcm_decap(desc, &ctx->cdata, ivsize, ctx->authsize, false);
377
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
378
				   desc_bytes(desc), ctx->dir);
379 380 381 382 383 384 385 386 387 388 389 390 391 392

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

393 394 395 396
static int rfc4106_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
397
	unsigned int ivsize = crypto_aead_ivsize(aead);
398
	u32 *desc;
399 400
	int rem_bytes = CAAM_DESC_BYTES_MAX - GCM_DESC_JOB_IO_LEN -
			ctx->cdata.keylen;
401

402
	if (!ctx->cdata.keylen || !ctx->authsize)
403 404 405 406 407 408 409
		return 0;

	/*
	 * RFC4106 encrypt shared descriptor
	 * Job Descriptor and Shared Descriptor
	 * must fit into the 64-word Descriptor h/w Buffer
	 */
410
	if (rem_bytes >= DESC_RFC4106_ENC_LEN) {
411
		ctx->cdata.key_inline = true;
412
		ctx->cdata.key_virt = ctx->key;
413 414
	} else {
		ctx->cdata.key_inline = false;
415
		ctx->cdata.key_dma = ctx->key_dma;
416
	}
417 418

	desc = ctx->sh_desc_enc;
419 420
	cnstr_shdsc_rfc4106_encap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
421
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
422
				   desc_bytes(desc), ctx->dir);
423 424 425 426 427

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
428
	if (rem_bytes >= DESC_RFC4106_DEC_LEN) {
429
		ctx->cdata.key_inline = true;
430
		ctx->cdata.key_virt = ctx->key;
431 432
	} else {
		ctx->cdata.key_inline = false;
433
		ctx->cdata.key_dma = ctx->key_dma;
434
	}
435 436

	desc = ctx->sh_desc_dec;
437 438
	cnstr_shdsc_rfc4106_decap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
439
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
440
				   desc_bytes(desc), ctx->dir);
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

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

456 457 458 459
static int rfc4543_set_sh_desc(struct crypto_aead *aead)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
460
	unsigned int ivsize = crypto_aead_ivsize(aead);
461
	u32 *desc;
462 463
	int rem_bytes = CAAM_DESC_BYTES_MAX - GCM_DESC_JOB_IO_LEN -
			ctx->cdata.keylen;
464

465
	if (!ctx->cdata.keylen || !ctx->authsize)
466 467 468 469 470 471 472
		return 0;

	/*
	 * RFC4543 encrypt shared descriptor
	 * Job Descriptor and Shared Descriptor
	 * must fit into the 64-word Descriptor h/w Buffer
	 */
473
	if (rem_bytes >= DESC_RFC4543_ENC_LEN) {
474
		ctx->cdata.key_inline = true;
475
		ctx->cdata.key_virt = ctx->key;
476 477
	} else {
		ctx->cdata.key_inline = false;
478
		ctx->cdata.key_dma = ctx->key_dma;
479
	}
480 481

	desc = ctx->sh_desc_enc;
482 483
	cnstr_shdsc_rfc4543_encap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
484
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
485
				   desc_bytes(desc), ctx->dir);
486 487 488 489 490

	/*
	 * Job Descriptor and Shared Descriptors
	 * must all fit into the 64-word Descriptor h/w Buffer
	 */
491
	if (rem_bytes >= DESC_RFC4543_DEC_LEN) {
492
		ctx->cdata.key_inline = true;
493
		ctx->cdata.key_virt = ctx->key;
494 495
	} else {
		ctx->cdata.key_inline = false;
496
		ctx->cdata.key_dma = ctx->key_dma;
497
	}
498 499

	desc = ctx->sh_desc_dec;
500 501
	cnstr_shdsc_rfc4543_decap(desc, &ctx->cdata, ivsize, ctx->authsize,
				  false);
502
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
503
				   desc_bytes(desc), ctx->dir);
504

505 506
	return 0;
}
507

508 509 510 511
static int rfc4543_setauthsize(struct crypto_aead *authenc,
			       unsigned int authsize)
{
	struct caam_ctx *ctx = crypto_aead_ctx(authenc);
512

513 514
	ctx->authsize = authsize;
	rfc4543_set_sh_desc(authenc);
515

516 517
	return 0;
}
518

519 520 521 522 523 524 525 526 527 528 529 530
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,
531
			       ctx->authsize, true, false);
532 533 534 535 536
	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,
537
			       ctx->authsize, false, false);
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
	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;

563
	if (keylen != CHACHA_KEY_SIZE + saltlen) {
564 565 566 567 568 569 570 571 572 573
		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 已提交
574
static int aead_setkey(struct crypto_aead *aead,
575 576 577 578
			       const u8 *key, unsigned int keylen)
{
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
579
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent);
580
	struct crypto_authenc_keys keys;
581 582
	int ret = 0;

583
	if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
584 585 586 587
		goto badkey;

#ifdef DEBUG
	printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n",
588 589
	       keys.authkeylen + keys.enckeylen, keys.enckeylen,
	       keys.authkeylen);
590
	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
591 592 593
		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	/*
	 * 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;
	}

615 616 617
	ret = gen_split_key(ctx->jrdev, ctx->key, &ctx->adata, keys.authkey,
			    keys.authkeylen, CAAM_MAX_KEY_SIZE -
			    keys.enckeylen);
618 619 620 621 622
	if (ret) {
		goto badkey;
	}

	/* postpend encryption key to auth split key */
623
	memcpy(ctx->key + ctx->adata.keylen_pad, keys.enckey, keys.enckeylen);
624
	dma_sync_single_for_device(jrdev, ctx->key_dma, ctx->adata.keylen_pad +
625
				   keys.enckeylen, ctx->dir);
626
#ifdef DEBUG
627
	print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
628
		       DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
629
		       ctx->adata.keylen_pad + keys.enckeylen, 1);
630
#endif
631 632

skip_split_key:
633
	ctx->cdata.keylen = keys.enckeylen;
634
	memzero_explicit(&keys, sizeof(keys));
635
	return aead_set_sh_desc(aead);
636 637
badkey:
	crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
638
	memzero_explicit(&keys, sizeof(keys));
639 640 641
	return -EINVAL;
}

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
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;
}

675 676 677 678 679 680 681 682 683 684 685 686
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;

#ifdef DEBUG
	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif

	memcpy(ctx->key, key, keylen);
687
	dma_sync_single_for_device(jrdev, ctx->key_dma, keylen, ctx->dir);
688
	ctx->cdata.keylen = keylen;
689

690
	return gcm_set_sh_desc(aead);
691 692
}

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
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;

#ifdef DEBUG
	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif

	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.
	 */
713
	ctx->cdata.keylen = keylen - 4;
714
	dma_sync_single_for_device(jrdev, ctx->key_dma, ctx->cdata.keylen,
715
				   ctx->dir);
716
	return rfc4106_set_sh_desc(aead);
717 718
}

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
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;

#ifdef DEBUG
	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif

	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.
	 */
739
	ctx->cdata.keylen = keylen - 4;
740
	dma_sync_single_for_device(jrdev, ctx->key_dma, ctx->cdata.keylen,
741
				   ctx->dir);
742
	return rfc4543_set_sh_desc(aead);
743 744
}

745 746
static int skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
			   unsigned int keylen)
Y
Yuan Kang 已提交
747
{
748 749 750 751
	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 已提交
752
	struct device *jrdev = ctx->jrdev;
753
	unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
754
	u32 *desc;
755
	u32 ctx1_iv_off = 0;
756
	const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
757
			       OP_ALG_AAI_CTR_MOD128);
758
	const bool is_rfc3686 = alg->caam.rfc3686;
Y
Yuan Kang 已提交
759 760

#ifdef DEBUG
761
	print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
Y
Yuan Kang 已提交
762 763
		       DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
#endif
764 765 766 767 768 769 770
	/*
	 * 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 已提交
771

772 773 774 775 776 777 778 779 780 781
	/*
	 * 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;
	}

782
	ctx->cdata.keylen = keylen;
783
	ctx->cdata.key_virt = key;
784
	ctx->cdata.key_inline = true;
Y
Yuan Kang 已提交
785

786
	/* skcipher_encrypt shared descriptor */
Y
Yuan Kang 已提交
787
	desc = ctx->sh_desc_enc;
788 789
	cnstr_shdsc_skcipher_encap(desc, &ctx->cdata, ivsize, is_rfc3686,
				   ctx1_iv_off);
790
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
791
				   desc_bytes(desc), ctx->dir);
792

793
	/* skcipher_decrypt shared descriptor */
Y
Yuan Kang 已提交
794
	desc = ctx->sh_desc_dec;
795 796
	cnstr_shdsc_skcipher_decap(desc, &ctx->cdata, ivsize, is_rfc3686,
				   ctx1_iv_off);
797
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
798
				   desc_bytes(desc), ctx->dir);
Y
Yuan Kang 已提交
799

800
	return 0;
Y
Yuan Kang 已提交
801 802
}

803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
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);
}

824 825
static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
			       unsigned int keylen)
826
{
827
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
828
	struct device *jrdev = ctx->jrdev;
829
	u32 *desc;
830 831

	if (keylen != 2 * AES_MIN_KEY_SIZE  && keylen != 2 * AES_MAX_KEY_SIZE) {
832
		crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
833 834 835 836
		dev_err(jrdev, "key size mismatch\n");
		return -EINVAL;
	}

837
	ctx->cdata.keylen = keylen;
838
	ctx->cdata.key_virt = key;
839
	ctx->cdata.key_inline = true;
840

841
	/* xts_skcipher_encrypt shared descriptor */
842
	desc = ctx->sh_desc_enc;
843
	cnstr_shdsc_xts_skcipher_encap(desc, &ctx->cdata);
844
	dma_sync_single_for_device(jrdev, ctx->sh_desc_enc_dma,
845
				   desc_bytes(desc), ctx->dir);
846

847
	/* xts_skcipher_decrypt shared descriptor */
848
	desc = ctx->sh_desc_dec;
849
	cnstr_shdsc_xts_skcipher_decap(desc, &ctx->cdata);
850
	dma_sync_single_for_device(jrdev, ctx->sh_desc_dec_dma,
851
				   desc_bytes(desc), ctx->dir);
852 853 854 855

	return 0;
}

856
/*
857
 * aead_edesc - s/w-extended aead descriptor
858 859
 * @src_nents: number of segments in input s/w scatterlist
 * @dst_nents: number of segments in output s/w scatterlist
860 861
 * @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 已提交
862 863
 * @sec4_sg_bytes: length of dma mapped sec4_sg space
 * @sec4_sg_dma: bus physical mapped address of h/w link table
864
 * @sec4_sg: pointer to h/w link table
865 866
 * @hw_desc: the h/w job descriptor followed by any referenced link tables
 */
Y
Yuan Kang 已提交
867
struct aead_edesc {
868 869
	int src_nents;
	int dst_nents;
870 871
	int mapped_src_nents;
	int mapped_dst_nents;
Y
Yuan Kang 已提交
872 873 874
	int sec4_sg_bytes;
	dma_addr_t sec4_sg_dma;
	struct sec4_sg_entry *sec4_sg;
875
	u32 hw_desc[];
876 877
};

Y
Yuan Kang 已提交
878
/*
879
 * skcipher_edesc - s/w-extended skcipher descriptor
880 881
 * @src_nents: number of segments in input s/w scatterlist
 * @dst_nents: number of segments in output s/w scatterlist
882 883
 * @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 已提交
884
 * @iv_dma: dma address of iv for checking continuity and link table
Y
Yuan Kang 已提交
885 886
 * @sec4_sg_bytes: length of dma mapped sec4_sg space
 * @sec4_sg_dma: bus physical mapped address of h/w link table
887
 * @sec4_sg: pointer to h/w link table
Y
Yuan Kang 已提交
888
 * @hw_desc: the h/w job descriptor followed by any referenced link tables
889
 *	     and IV
Y
Yuan Kang 已提交
890
 */
891
struct skcipher_edesc {
Y
Yuan Kang 已提交
892 893
	int src_nents;
	int dst_nents;
894 895
	int mapped_src_nents;
	int mapped_dst_nents;
Y
Yuan Kang 已提交
896
	dma_addr_t iv_dma;
Y
Yuan Kang 已提交
897 898 899
	int sec4_sg_bytes;
	dma_addr_t sec4_sg_dma;
	struct sec4_sg_entry *sec4_sg;
Y
Yuan Kang 已提交
900 901 902
	u32 hw_desc[0];
};

903
static void caam_unmap(struct device *dev, struct scatterlist *src,
Y
Yuan Kang 已提交
904
		       struct scatterlist *dst, int src_nents,
905
		       int dst_nents,
906
		       dma_addr_t iv_dma, int ivsize, dma_addr_t sec4_sg_dma,
Y
Yuan Kang 已提交
907
		       int sec4_sg_bytes)
908
{
Y
Yuan Kang 已提交
909
	if (dst != src) {
910 911
		if (src_nents)
			dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
912 913
		if (dst_nents)
			dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
914
	} else {
915
		dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
916 917
	}

918
	if (iv_dma)
919
		dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
Y
Yuan Kang 已提交
920 921
	if (sec4_sg_bytes)
		dma_unmap_single(dev, sec4_sg_dma, sec4_sg_bytes,
922 923 924
				 DMA_TO_DEVICE);
}

925 926 927
static void aead_unmap(struct device *dev,
		       struct aead_edesc *edesc,
		       struct aead_request *req)
928 929
{
	caam_unmap(dev, req->src, req->dst,
930
		   edesc->src_nents, edesc->dst_nents, 0, 0,
931 932 933
		   edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
}

934 935
static void skcipher_unmap(struct device *dev, struct skcipher_edesc *edesc,
			   struct skcipher_request *req)
Y
Yuan Kang 已提交
936
{
937 938
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
939 940

	caam_unmap(dev, req->src, req->dst,
941
		   edesc->src_nents, edesc->dst_nents,
942
		   edesc->iv_dma, ivsize,
Y
Yuan Kang 已提交
943
		   edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
Y
Yuan Kang 已提交
944 945
}

Y
Yuan Kang 已提交
946
static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
947 948
				   void *context)
{
Y
Yuan Kang 已提交
949 950
	struct aead_request *req = context;
	struct aead_edesc *edesc;
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967

#ifdef DEBUG
	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
#endif

	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 已提交
968
static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
969 970
				   void *context)
{
Y
Yuan Kang 已提交
971 972
	struct aead_request *req = context;
	struct aead_edesc *edesc;
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995

#ifdef DEBUG
	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
#endif

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

996 997
static void skcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
				  void *context)
Y
Yuan Kang 已提交
998
{
999 1000 1001 1002
	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 已提交
1003

1004
#ifdef DEBUG
Y
Yuan Kang 已提交
1005 1006 1007
	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
#endif

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

1010 1011
	if (err)
		caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
1012 1013

#ifdef DEBUG
1014
	print_hex_dump(KERN_ERR, "dstiv  @"__stringify(__LINE__)": ",
1015
		       DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
Y
Yuan Kang 已提交
1016 1017
		       edesc->src_nents > 1 ? 100 : ivsize, 1);
#endif
1018 1019
	caam_dump_sg(KERN_ERR, "dst    @" __stringify(__LINE__)": ",
		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
1020
		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
Y
Yuan Kang 已提交
1021

1022
	skcipher_unmap(jrdev, edesc, req);
1023 1024

	/*
1025
	 * The crypto API expects us to set the IV (req->iv) to the last
1026 1027
	 * ciphertext block. This is used e.g. by the CTS mode.
	 */
1028 1029 1030
	if (ivsize)
		scatterwalk_map_and_copy(req->iv, req->dst, req->cryptlen -
					 ivsize, ivsize, 0);
1031

Y
Yuan Kang 已提交
1032 1033
	kfree(edesc);

1034
	skcipher_request_complete(req, err);
Y
Yuan Kang 已提交
1035 1036
}

1037 1038
static void skcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
				  void *context)
Y
Yuan Kang 已提交
1039
{
1040 1041
	struct skcipher_request *req = context;
	struct skcipher_edesc *edesc;
1042
#ifdef DEBUG
1043 1044
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
1045 1046 1047 1048

	dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
#endif

1049
	edesc = container_of(desc, struct skcipher_edesc, hw_desc[0]);
1050 1051
	if (err)
		caam_jr_strstatus(jrdev, err);
Y
Yuan Kang 已提交
1052 1053

#ifdef DEBUG
1054
	print_hex_dump(KERN_ERR, "dstiv  @"__stringify(__LINE__)": ",
1055
		       DUMP_PREFIX_ADDRESS, 16, 4, req->iv, ivsize, 1);
Y
Yuan Kang 已提交
1056
#endif
1057 1058
	caam_dump_sg(KERN_ERR, "dst    @" __stringify(__LINE__)": ",
		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
1059
		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
Y
Yuan Kang 已提交
1060

1061
	skcipher_unmap(jrdev, edesc, req);
Y
Yuan Kang 已提交
1062 1063
	kfree(edesc);

1064
	skcipher_request_complete(req, err);
Y
Yuan Kang 已提交
1065 1066
}

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
/*
 * 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) {
1091 1092
		src_dma = edesc->mapped_src_nents ? sg_dma_address(req->src) :
						    0;
1093 1094 1095
		in_options = 0;
	} else {
		src_dma = edesc->sec4_sg_dma;
1096
		sec4_sg_index += edesc->mapped_src_nents;
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
		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)) {
1107
		if (!edesc->mapped_dst_nents) {
1108
			dst_dma = 0;
1109
			out_options = 0;
1110
		} else if (edesc->mapped_dst_nents == 1) {
1111
			dst_dma = sg_dma_address(req->dst);
1112
			out_options = 0;
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
		} 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;
1139
	bool generic_gcm = (ivsize == GCM_AES_IV_SIZE);
1140 1141 1142
	unsigned int last;

	init_aead_job(req, edesc, all_contig, encrypt);
1143
	append_math_add_imm_u32(desc, REG3, ZERO, IMM, req->assoclen);
1144 1145 1146 1147 1148 1149 1150 1151

	/* 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 |
1152
			 FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1 | GCM_AES_IV_SIZE | last);
1153 1154
	/* Append Salt */
	if (!generic_gcm)
1155
		append_data(desc, ctx->key + ctx->cdata.keylen, 4);
1156 1157 1158 1159 1160
	/* Append IV */
	append_data(desc, req->iv, ivsize);
	/* End of blank commands */
}

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
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);
}

1195 1196 1197
static void init_authenc_job(struct aead_request *req,
			     struct aead_edesc *edesc,
			     bool all_contig, bool encrypt)
1198 1199
{
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1200 1201 1202
	struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
						 struct caam_aead_alg, aead);
	unsigned int ivsize = crypto_aead_ivsize(aead);
1203
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
1204
	struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctx->jrdev->parent);
1205
	const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
1206 1207
			       OP_ALG_AAI_CTR_MOD128);
	const bool is_rfc3686 = alg->caam.rfc3686;
1208
	u32 *desc = edesc->hw_desc;
1209
	u32 ivoffset = 0;
1210

1211 1212 1213 1214 1215 1216 1217
	/*
	 * AES-CTR needs to load IV in CONTEXT1 reg
	 * at an offset of 128bits (16bytes)
	 * CONTEXT1[255:128] = IV
	 */
	if (ctr_mode)
		ivoffset = 16;
1218

1219 1220 1221 1222 1223 1224
	/*
	 * RFC3686 specific:
	 *	CONTEXT1[255:128] = {NONCE, IV, COUNTER}
	 */
	if (is_rfc3686)
		ivoffset = 16 + CTR_RFC3686_NONCE_SIZE;
1225

1226
	init_aead_job(req, edesc, all_contig, encrypt);
1227

1228 1229 1230 1231 1232 1233 1234 1235 1236
	/*
	 * {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);

1237
	if (ivsize && ((is_rfc3686 && encrypt) || !alg->caam.geniv))
1238 1239 1240 1241
		append_load_as_imm(desc, req->iv, ivsize,
				   LDST_CLASS_1_CCB |
				   LDST_SRCDST_BYTE_CONTEXT |
				   (ivoffset << LDST_OFFSET_SHIFT));
1242 1243
}

Y
Yuan Kang 已提交
1244
/*
1245
 * Fill in skcipher job descriptor
Y
Yuan Kang 已提交
1246
 */
1247 1248 1249
static void init_skcipher_job(struct skcipher_request *req,
			      struct skcipher_edesc *edesc,
			      const bool encrypt)
Y
Yuan Kang 已提交
1250
{
1251 1252 1253
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
1254
	u32 *desc = edesc->hw_desc;
1255
	u32 *sh_desc;
1256 1257 1258
	u32 in_options = 0, out_options = 0;
	dma_addr_t src_dma, dst_dma, ptr;
	int len, sec4_sg_index = 0;
Y
Yuan Kang 已提交
1259 1260

#ifdef DEBUG
1261
	print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
1262 1263 1264
		       DUMP_PREFIX_ADDRESS, 16, 4, req->iv, ivsize, 1);
	pr_err("asked=%d, cryptlen%d\n",
	       (int)edesc->src_nents > 1 ? 100 : req->cryptlen, req->cryptlen);
Y
Yuan Kang 已提交
1265
#endif
1266 1267
	caam_dump_sg(KERN_ERR, "src    @" __stringify(__LINE__)": ",
		     DUMP_PREFIX_ADDRESS, 16, 4, req->src,
1268 1269 1270 1271
		     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 已提交
1272 1273 1274 1275

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

1276 1277 1278 1279 1280 1281 1282 1283 1284
	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 已提交
1285 1286

	if (likely(req->src == req->dst)) {
1287 1288 1289 1290
		dst_dma = src_dma + !!ivsize * sizeof(struct sec4_sg_entry);
		out_options = in_options;
	} else if (edesc->mapped_dst_nents == 1) {
		dst_dma = sg_dma_address(req->dst);
Y
Yuan Kang 已提交
1291
	} else {
1292 1293 1294
		dst_dma = edesc->sec4_sg_dma + sec4_sg_index *
			  sizeof(struct sec4_sg_entry);
		out_options = LDST_SGF;
Y
Yuan Kang 已提交
1295
	}
1296

1297
	append_seq_out_ptr(desc, dst_dma, req->cryptlen, out_options);
Y
Yuan Kang 已提交
1298 1299
}

1300
/*
1301
 * allocate and map the aead extended descriptor
1302
 */
1303 1304 1305
static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
					   int desc_bytes, bool *all_contig_ptr,
					   bool encrypt)
1306
{
Y
Yuan Kang 已提交
1307
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1308 1309
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1310 1311
	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
		       GFP_KERNEL : GFP_ATOMIC;
1312
	int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
Y
Yuan Kang 已提交
1313
	struct aead_edesc *edesc;
1314
	int sec4_sg_index, sec4_sg_len, sec4_sg_bytes;
1315
	unsigned int authsize = ctx->authsize;
1316

1317
	if (unlikely(req->dst != req->src)) {
1318 1319
		src_nents = sg_nents_for_len(req->src, req->assoclen +
					     req->cryptlen);
1320 1321 1322 1323 1324 1325
		if (unlikely(src_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
				req->assoclen + req->cryptlen);
			return ERR_PTR(src_nents);
		}

1326 1327 1328 1329
		dst_nents = sg_nents_for_len(req->dst, req->assoclen +
					     req->cryptlen +
						(encrypt ? authsize :
							   (-authsize)));
1330 1331 1332 1333 1334 1335
		if (unlikely(dst_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in dst S/G\n",
				req->assoclen + req->cryptlen +
				(encrypt ? authsize : (-authsize)));
			return ERR_PTR(dst_nents);
		}
1336
	} else {
1337 1338 1339
		src_nents = sg_nents_for_len(req->src, req->assoclen +
					     req->cryptlen +
					     (encrypt ? authsize : 0));
1340 1341 1342 1343 1344 1345
		if (unlikely(src_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
				req->assoclen + req->cryptlen +
				(encrypt ? authsize : 0));
			return ERR_PTR(src_nents);
		}
1346
	}
1347

1348
	if (likely(req->src == req->dst)) {
1349 1350 1351
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_BIDIRECTIONAL);
		if (unlikely(!mapped_src_nents)) {
1352 1353 1354 1355
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}
	} else {
1356 1357
		/* Cover also the case of null (zero length) input data */
		if (src_nents) {
1358 1359 1360
			mapped_src_nents = dma_map_sg(jrdev, req->src,
						      src_nents, DMA_TO_DEVICE);
			if (unlikely(!mapped_src_nents)) {
1361 1362 1363
				dev_err(jrdev, "unable to map source\n");
				return ERR_PTR(-ENOMEM);
			}
1364 1365
		} else {
			mapped_src_nents = 0;
1366 1367
		}

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
		/* 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;
1381 1382 1383
		}
	}

1384 1385 1386 1387
	/*
	 * 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.
	 */
1388
	sec4_sg_len = mapped_src_nents > 1 ? mapped_src_nents : 0;
1389 1390 1391 1392 1393
	if (mapped_dst_nents > 1)
		sec4_sg_len += pad_sg_nents(mapped_dst_nents);
	else
		sec4_sg_len = pad_sg_nents(sec4_sg_len);

1394 1395 1396 1397 1398 1399 1400
	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,
1401
			   0, 0, 0);
1402 1403 1404
		return ERR_PTR(-ENOMEM);
	}

1405 1406
	edesc->src_nents = src_nents;
	edesc->dst_nents = dst_nents;
1407 1408
	edesc->mapped_src_nents = mapped_src_nents;
	edesc->mapped_dst_nents = mapped_dst_nents;
Y
Yuan Kang 已提交
1409 1410
	edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
			 desc_bytes;
1411
	*all_contig_ptr = !(mapped_src_nents > 1);
1412

Y
Yuan Kang 已提交
1413
	sec4_sg_index = 0;
1414 1415 1416 1417
	if (mapped_src_nents > 1) {
		sg_to_sec4_sg_last(req->src, mapped_src_nents,
				   edesc->sec4_sg + sec4_sg_index, 0);
		sec4_sg_index += mapped_src_nents;
1418
	}
1419 1420
	if (mapped_dst_nents > 1) {
		sg_to_sec4_sg_last(req->dst, mapped_dst_nents,
Y
Yuan Kang 已提交
1421
				   edesc->sec4_sg + sec4_sg_index, 0);
1422
	}
1423 1424 1425 1426

	if (!sec4_sg_bytes)
		return edesc;

1427 1428
	edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
					    sec4_sg_bytes, DMA_TO_DEVICE);
1429 1430
	if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
		dev_err(jrdev, "unable to map S/G table\n");
1431 1432
		aead_unmap(jrdev, edesc, req);
		kfree(edesc);
1433 1434
		return ERR_PTR(-ENOMEM);
	}
1435

1436 1437
	edesc->sec4_sg_bytes = sec4_sg_bytes;

1438 1439 1440
	return edesc;
}

1441
static int gcm_encrypt(struct aead_request *req)
1442
{
Y
Yuan Kang 已提交
1443 1444
	struct aead_edesc *edesc;
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
1445 1446
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1447
	bool all_contig;
1448
	u32 *desc;
1449 1450
	int ret = 0;

1451
	/* allocate extended descriptor */
1452
	edesc = aead_edesc_alloc(req, GCM_DESC_JOB_IO_LEN, &all_contig, true);
1453 1454 1455
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

1456
	/* Create and submit job descriptor */
1457
	init_gcm_job(req, edesc, all_contig, true);
1458
#ifdef DEBUG
1459
	print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
1460 1461 1462
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif
1463

1464 1465 1466 1467 1468 1469 1470 1471
	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);
	}
1472

1473
	return ret;
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 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
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;
}

1542 1543 1544 1545 1546 1547 1548 1549
static int ipsec_gcm_encrypt(struct aead_request *req)
{
	if (req->assoclen < 8)
		return -EINVAL;

	return gcm_encrypt(req);
}

1550
static int aead_encrypt(struct aead_request *req)
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
{
	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 */
1561 1562
	edesc = aead_edesc_alloc(req, AUTHENC_DESC_JOB_IO_LEN,
				 &all_contig, true);
1563 1564 1565 1566
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor */
1567
	init_authenc_job(req, edesc, all_contig, true);
1568 1569 1570 1571 1572 1573 1574
#ifdef DEBUG
	print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif

	desc = edesc->hw_desc;
1575
	ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
1576 1577 1578
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1579
		aead_unmap(jrdev, edesc, req);
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
		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);
#ifdef DEBUG
	print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif

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

1621 1622 1623 1624 1625 1626 1627 1628
static int ipsec_gcm_decrypt(struct aead_request *req)
{
	if (req->assoclen < 8)
		return -EINVAL;

	return gcm_decrypt(req);
}

1629
static int aead_decrypt(struct aead_request *req)
1630
{
1631
	struct aead_edesc *edesc;
1632 1633 1634
	struct crypto_aead *aead = crypto_aead_reqtfm(req);
	struct caam_ctx *ctx = crypto_aead_ctx(aead);
	struct device *jrdev = ctx->jrdev;
1635
	bool all_contig;
1636
	u32 *desc;
1637
	int ret = 0;
1638

1639 1640 1641
	caam_dump_sg(KERN_ERR, "dec src@" __stringify(__LINE__)": ",
		     DUMP_PREFIX_ADDRESS, 16, 4, req->src,
		     req->assoclen + req->cryptlen, 1);
C
Catalin Vasile 已提交
1642

1643
	/* allocate extended descriptor */
1644 1645
	edesc = aead_edesc_alloc(req, AUTHENC_DESC_JOB_IO_LEN,
				 &all_contig, false);
1646 1647 1648
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

1649
	/* Create and submit job descriptor*/
1650
	init_authenc_job(req, edesc, all_contig, false);
1651
#ifdef DEBUG
1652
	print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
1653 1654 1655 1656
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif

1657
	desc = edesc->hw_desc;
1658
	ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
1659 1660 1661
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1662
		aead_unmap(jrdev, edesc, req);
1663 1664
		kfree(edesc);
	}
1665

1666 1667
	return ret;
}
1668

Y
Yuan Kang 已提交
1669
/*
1670
 * allocate and map the skcipher extended descriptor for skcipher
Y
Yuan Kang 已提交
1671
 */
1672 1673
static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req,
						   int desc_bytes)
Y
Yuan Kang 已提交
1674
{
1675 1676
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
Y
Yuan Kang 已提交
1677
	struct device *jrdev = ctx->jrdev;
1678
	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
Y
Yuan Kang 已提交
1679
		       GFP_KERNEL : GFP_ATOMIC;
1680
	int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
1681
	struct skcipher_edesc *edesc;
1682
	dma_addr_t iv_dma = 0;
1683
	u8 *iv;
1684
	int ivsize = crypto_skcipher_ivsize(skcipher);
1685
	int dst_sg_idx, sec4_sg_ents, sec4_sg_bytes;
Y
Yuan Kang 已提交
1686

1687
	src_nents = sg_nents_for_len(req->src, req->cryptlen);
1688 1689
	if (unlikely(src_nents < 0)) {
		dev_err(jrdev, "Insufficient bytes (%d) in src S/G\n",
1690
			req->cryptlen);
1691 1692
		return ERR_PTR(src_nents);
	}
Y
Yuan Kang 已提交
1693

1694
	if (req->dst != req->src) {
1695
		dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
1696 1697
		if (unlikely(dst_nents < 0)) {
			dev_err(jrdev, "Insufficient bytes (%d) in dst S/G\n",
1698
				req->cryptlen);
1699 1700 1701
			return ERR_PTR(dst_nents);
		}
	}
Y
Yuan Kang 已提交
1702 1703

	if (likely(req->src == req->dst)) {
1704 1705 1706
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_BIDIRECTIONAL);
		if (unlikely(!mapped_src_nents)) {
1707 1708 1709
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}
Y
Yuan Kang 已提交
1710
	} else {
1711 1712 1713
		mapped_src_nents = dma_map_sg(jrdev, req->src, src_nents,
					      DMA_TO_DEVICE);
		if (unlikely(!mapped_src_nents)) {
1714 1715 1716
			dev_err(jrdev, "unable to map source\n");
			return ERR_PTR(-ENOMEM);
		}
1717 1718 1719
		mapped_dst_nents = dma_map_sg(jrdev, req->dst, dst_nents,
					      DMA_FROM_DEVICE);
		if (unlikely(!mapped_dst_nents)) {
1720
			dev_err(jrdev, "unable to map destination\n");
1721
			dma_unmap_sg(jrdev, req->src, src_nents, DMA_TO_DEVICE);
1722 1723
			return ERR_PTR(-ENOMEM);
		}
Y
Yuan Kang 已提交
1724 1725
	}

1726 1727 1728 1729
	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;
1730
	dst_sg_idx = sec4_sg_ents;
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749

	/*
	 * 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:
	 * if (src != dst && output S/G)
	 *      pad output S/G, if needed
	 * else if (src == dst && S/G)
	 *      overlapping S/Gs; pad one of them
	 * else if (input S/G) ...
	 *      pad input S/G, if needed
	 */
	if (mapped_dst_nents > 1)
		sec4_sg_ents += pad_sg_nents(mapped_dst_nents);
	else if ((req->src == req->dst) && (mapped_src_nents > 1))
		sec4_sg_ents = max(pad_sg_nents(sec4_sg_ents),
				   !!ivsize + pad_sg_nents(mapped_src_nents));
	else
		sec4_sg_ents = pad_sg_nents(sec4_sg_ents);

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

1752 1753 1754 1755
	/*
	 * allocate space for base edesc and hw desc commands, link tables, IV
	 */
	edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes + ivsize,
1756
			GFP_DMA | flags);
Y
Yuan Kang 已提交
1757 1758
	if (!edesc) {
		dev_err(jrdev, "could not allocate extended descriptor\n");
1759
		caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0,
1760
			   0, 0, 0);
Y
Yuan Kang 已提交
1761 1762 1763 1764 1765
		return ERR_PTR(-ENOMEM);
	}

	edesc->src_nents = src_nents;
	edesc->dst_nents = dst_nents;
1766 1767
	edesc->mapped_src_nents = mapped_src_nents;
	edesc->mapped_dst_nents = mapped_dst_nents;
Y
Yuan Kang 已提交
1768
	edesc->sec4_sg_bytes = sec4_sg_bytes;
1769 1770
	edesc->sec4_sg = (struct sec4_sg_entry *)((u8 *)edesc->hw_desc +
						  desc_bytes);
Y
Yuan Kang 已提交
1771

1772
	/* Make sure IV is located in a DMAable area */
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
	if (ivsize) {
		iv = (u8 *)edesc->hw_desc + desc_bytes + sec4_sg_bytes;
		memcpy(iv, req->iv, ivsize);

		iv_dma = dma_map_single(jrdev, iv, ivsize, DMA_TO_DEVICE);
		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);
		}
1785

1786
		dma_to_sec4_sg_one(edesc->sec4_sg, iv_dma, ivsize, 0);
Y
Yuan Kang 已提交
1787
	}
1788 1789 1790
	if (dst_sg_idx)
		sg_to_sec4_sg_last(req->src, mapped_src_nents, edesc->sec4_sg +
				   !!ivsize, 0);
1791

1792 1793 1794
	if (mapped_dst_nents > 1) {
		sg_to_sec4_sg_last(req->dst, mapped_dst_nents,
				   edesc->sec4_sg + dst_sg_idx, 0);
Y
Yuan Kang 已提交
1795 1796
	}

1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
	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);
		}
1808 1809
	}

Y
Yuan Kang 已提交
1810 1811 1812
	edesc->iv_dma = iv_dma;

#ifdef DEBUG
1813
	print_hex_dump(KERN_ERR, "skcipher sec4_sg@" __stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1814 1815
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
		       sec4_sg_bytes, 1);
Y
Yuan Kang 已提交
1816 1817 1818 1819 1820
#endif

	return edesc;
}

1821
static int skcipher_encrypt(struct skcipher_request *req)
Y
Yuan Kang 已提交
1822
{
1823 1824 1825
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
Y
Yuan Kang 已提交
1826 1827 1828 1829 1830
	struct device *jrdev = ctx->jrdev;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
1831
	edesc = skcipher_edesc_alloc(req, DESC_JOB_IO_LEN * CAAM_CMD_SZ);
Y
Yuan Kang 已提交
1832 1833 1834 1835
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

	/* Create and submit job descriptor*/
1836
	init_skcipher_job(req, edesc, true);
Y
Yuan Kang 已提交
1837
#ifdef DEBUG
1838
	print_hex_dump(KERN_ERR, "skcipher jobdesc@" __stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1839 1840 1841 1842
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif
	desc = edesc->hw_desc;
1843
	ret = caam_jr_enqueue(jrdev, desc, skcipher_encrypt_done, req);
Y
Yuan Kang 已提交
1844 1845 1846 1847

	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1848
		skcipher_unmap(jrdev, edesc, req);
Y
Yuan Kang 已提交
1849 1850 1851 1852 1853 1854
		kfree(edesc);
	}

	return ret;
}

1855
static int skcipher_decrypt(struct skcipher_request *req)
Y
Yuan Kang 已提交
1856
{
1857 1858 1859 1860
	struct skcipher_edesc *edesc;
	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
	struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
	int ivsize = crypto_skcipher_ivsize(skcipher);
Y
Yuan Kang 已提交
1861 1862 1863 1864 1865
	struct device *jrdev = ctx->jrdev;
	u32 *desc;
	int ret = 0;

	/* allocate extended descriptor */
1866
	edesc = skcipher_edesc_alloc(req, DESC_JOB_IO_LEN * CAAM_CMD_SZ);
Y
Yuan Kang 已提交
1867 1868 1869
	if (IS_ERR(edesc))
		return PTR_ERR(edesc);

1870
	/*
1871
	 * The crypto API expects us to set the IV (req->iv) to the last
1872 1873
	 * ciphertext block.
	 */
1874 1875 1876
	if (ivsize)
		scatterwalk_map_and_copy(req->iv, req->src, req->cryptlen -
					 ivsize, ivsize, 0);
1877

Y
Yuan Kang 已提交
1878
	/* Create and submit job descriptor*/
1879
	init_skcipher_job(req, edesc, false);
Y
Yuan Kang 已提交
1880 1881
	desc = edesc->hw_desc;
#ifdef DEBUG
1882
	print_hex_dump(KERN_ERR, "skcipher jobdesc@" __stringify(__LINE__)": ",
Y
Yuan Kang 已提交
1883 1884 1885 1886
		       DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
		       desc_bytes(edesc->hw_desc), 1);
#endif

1887
	ret = caam_jr_enqueue(jrdev, desc, skcipher_decrypt_done, req);
Y
Yuan Kang 已提交
1888 1889 1890
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1891
		skcipher_unmap(jrdev, edesc, req);
Y
Yuan Kang 已提交
1892 1893 1894 1895 1896 1897
		kfree(edesc);
	}

	return ret;
}

1898
static struct caam_skcipher_alg driver_algs[] = {
1899
	{
1900 1901 1902 1903 1904 1905 1906 1907 1908
		.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,
1909 1910 1911
			.min_keysize = AES_MIN_KEY_SIZE,
			.max_keysize = AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
1912 1913
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
1914 1915
	},
	{
1916 1917 1918 1919 1920 1921
		.skcipher = {
			.base = {
				.cra_name = "cbc(des3_ede)",
				.cra_driver_name = "cbc-3des-caam",
				.cra_blocksize = DES3_EDE_BLOCK_SIZE,
			},
1922
			.setkey = des_skcipher_setkey,
1923 1924
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1925 1926 1927
			.min_keysize = DES3_EDE_KEY_SIZE,
			.max_keysize = DES3_EDE_KEY_SIZE,
			.ivsize = DES3_EDE_BLOCK_SIZE,
1928 1929
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
1930 1931
	},
	{
1932 1933 1934 1935 1936 1937
		.skcipher = {
			.base = {
				.cra_name = "cbc(des)",
				.cra_driver_name = "cbc-des-caam",
				.cra_blocksize = DES_BLOCK_SIZE,
			},
1938
			.setkey = des_skcipher_setkey,
1939 1940
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1941 1942 1943
			.min_keysize = DES_KEY_SIZE,
			.max_keysize = DES_KEY_SIZE,
			.ivsize = DES_BLOCK_SIZE,
1944 1945
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
1946 1947
	},
	{
1948 1949 1950 1951 1952 1953 1954 1955 1956
		.skcipher = {
			.base = {
				.cra_name = "ctr(aes)",
				.cra_driver_name = "ctr-aes-caam",
				.cra_blocksize = 1,
			},
			.setkey = skcipher_setkey,
			.encrypt = skcipher_encrypt,
			.decrypt = skcipher_decrypt,
1957 1958 1959
			.min_keysize = AES_MIN_KEY_SIZE,
			.max_keysize = AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
1960 1961 1962 1963
			.chunksize = AES_BLOCK_SIZE,
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES |
					OP_ALG_AAI_CTR_MOD128,
1964 1965
	},
	{
1966 1967 1968 1969 1970 1971 1972 1973 1974
		.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,
1975 1976 1977 1978 1979
			.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,
1980 1981 1982 1983 1984 1985 1986
			.chunksize = AES_BLOCK_SIZE,
		},
		.caam = {
			.class1_alg_type = OP_ALG_ALGSEL_AES |
					   OP_ALG_AAI_CTR_MOD128,
			.rfc3686 = true,
		},
1987 1988
	},
	{
1989 1990 1991 1992 1993 1994 1995 1996 1997
		.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,
1998 1999 2000
			.min_keysize = 2 * AES_MIN_KEY_SIZE,
			.max_keysize = 2 * AES_MAX_KEY_SIZE,
			.ivsize = AES_BLOCK_SIZE,
2001 2002
		},
		.caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XTS,
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 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
	{
		.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,
	},
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
};

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

3396 3397
static int caam_init_common(struct caam_ctx *ctx, struct caam_alg_entry *caam,
			    bool uses_dkp)
3398
{
3399
	dma_addr_t dma_addr;
3400
	struct caam_drv_private *priv;
3401

3402 3403 3404 3405 3406
	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);
	}
3407

3408 3409 3410 3411 3412 3413
	priv = dev_get_drvdata(ctx->jrdev->parent);
	if (priv->era >= 6 && uses_dkp)
		ctx->dir = DMA_BIDIRECTIONAL;
	else
		ctx->dir = DMA_TO_DEVICE;

3414 3415 3416
	dma_addr = dma_map_single_attrs(ctx->jrdev, ctx->sh_desc_enc,
					offsetof(struct caam_ctx,
						 sh_desc_enc_dma),
3417
					ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
	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);

3429
	/* copy descriptor header template value */
3430 3431
	ctx->cdata.algtype = OP_TYPE_CLASS1_ALG | caam->class1_alg_type;
	ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam->class2_alg_type;
3432 3433 3434 3435

	return 0;
}

3436
static int caam_cra_init(struct crypto_skcipher *tfm)
3437
{
3438 3439 3440
	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
	struct caam_skcipher_alg *caam_alg =
		container_of(alg, typeof(*caam_alg), skcipher);
3441

3442 3443
	return caam_init_common(crypto_skcipher_ctx(tfm), &caam_alg->caam,
				false);
3444 3445 3446 3447 3448 3449 3450 3451 3452
}

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

3453
	return caam_init_common(ctx, &caam_alg->caam, !caam_alg->caam.nodkp);
3454 3455 3456 3457
}

static void caam_exit_common(struct caam_ctx *ctx)
{
3458 3459
	dma_unmap_single_attrs(ctx->jrdev, ctx->sh_desc_enc_dma,
			       offsetof(struct caam_ctx, sh_desc_enc_dma),
3460
			       ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
3461
	caam_jr_free(ctx->jrdev);
3462 3463
}

3464
static void caam_cra_exit(struct crypto_skcipher *tfm)
3465
{
3466
	caam_exit_common(crypto_skcipher_ctx(tfm));
3467 3468 3469 3470 3471 3472 3473
}

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

3474 3475
static void __exit caam_algapi_exit(void)
{
3476 3477 3478 3479 3480 3481 3482 3483
	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);
	}
3484

3485 3486
	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
		struct caam_skcipher_alg *t_alg = driver_algs + i;
3487

3488 3489
		if (t_alg->registered)
			crypto_unregister_skcipher(&t_alg->skcipher);
3490 3491 3492
	}
}

3493
static void caam_skcipher_alg_init(struct caam_skcipher_alg *t_alg)
3494
{
3495
	struct skcipher_alg *alg = &t_alg->skcipher;
3496

3497 3498 3499 3500
	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;
3501

3502 3503
	alg->init = caam_cra_init;
	alg->exit = caam_cra_exit;
3504 3505
}

3506 3507 3508 3509 3510 3511 3512
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);
3513
	alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
3514 3515 3516 3517 3518

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

3519 3520
static int __init caam_algapi_init(void)
{
3521 3522
	struct device_node *dev_node;
	struct platform_device *pdev;
3523
	struct caam_drv_private *priv;
3524
	int i = 0, err = 0;
3525
	u32 aes_vid, aes_inst, des_inst, md_vid, md_inst, ccha_inst, ptha_inst;
3526
	u32 arc4_inst;
3527
	unsigned int md_limit = SHA512_DIGEST_SIZE;
3528
	bool registered = false, gcm_support;
3529

3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542
	dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
	if (!dev_node) {
		dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
		if (!dev_node)
			return -ENODEV;
	}

	pdev = of_find_device_by_node(dev_node);
	if (!pdev) {
		of_node_put(dev_node);
		return -ENODEV;
	}

3543
	priv = dev_get_drvdata(&pdev->dev);
3544 3545 3546 3547 3548 3549
	of_node_put(dev_node);

	/*
	 * If priv is NULL, it's probably because the caam driver wasn't
	 * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
	 */
3550 3551 3552 3553
	if (!priv) {
		err = -ENODEV;
		goto out_put_dev;
	}
3554 3555


3556 3557 3558 3559
	/*
	 * Register crypto algorithms the device supports.
	 * First, detect presence and attributes of DES, AES, and MD blocks.
	 */
3560
	if (priv->era < 10) {
3561
		u32 cha_vid, cha_inst, aes_rn;
3562 3563 3564 3565 3566 3567 3568 3569 3570 3571

		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;
3572 3573
		arc4_inst = (cha_inst & CHA_ID_LS_ARC4_MASK) >>
			    CHA_ID_LS_ARC4_SHIFT;
3574 3575
		ccha_inst = 0;
		ptha_inst = 0;
3576 3577 3578 3579

		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);
3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591
	} 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;
3592 3593
		ccha_inst = rd_reg32(&priv->ctrl->vreg.ccha) & CHA_VER_NUM_MASK;
		ptha_inst = rd_reg32(&priv->ctrl->vreg.ptha) & CHA_VER_NUM_MASK;
3594
		arc4_inst = rd_reg32(&priv->ctrl->vreg.afha) & CHA_VER_NUM_MASK;
3595 3596

		gcm_support = aesa & CHA_VER_MISC_AES_GCM;
3597
	}
3598 3599

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

3603
	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
3604 3605
		struct caam_skcipher_alg *t_alg = driver_algs + i;
		u32 alg_sel = t_alg->caam.class1_alg_type & OP_ALG_ALGSEL_MASK;
3606 3607 3608 3609 3610 3611 3612 3613 3614 3615

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

3617 3618 3619 3620
		/* Skip ARC4 algorithms if not supported by device */
		if (!arc4_inst && alg_sel == OP_ALG_ALGSEL_ARC4)
			continue;

3621 3622 3623 3624
		/*
		 * Check support for AES modes not available
		 * on LP devices.
		 */
3625 3626 3627 3628
		if (aes_vid == CHA_VER_VID_AES_LP &&
		    (t_alg->caam.class1_alg_type & OP_ALG_AAI_MASK) ==
		    OP_ALG_AAI_XTS)
			continue;
3629

3630
		caam_skcipher_alg_init(t_alg);
3631

3632
		err = crypto_register_skcipher(&t_alg->skcipher);
3633
		if (err) {
3634
			pr_warn("%s alg registration failed\n",
3635
				t_alg->skcipher.base.cra_driver_name);
3636 3637 3638
			continue;
		}

3639
		t_alg->registered = true;
3640 3641 3642 3643 3644
		registered = true;
	}

	for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
		struct caam_aead_alg *t_alg = driver_aeads + i;
3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660
		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;

3661 3662 3663 3664 3665 3666 3667 3668
		/* 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;

3669 3670 3671
		/* Skip GCM algorithms if not supported by device */
		if (c1_alg_sel == OP_ALG_ALGSEL_AES &&
		    alg_aai == OP_ALG_AAI_GCM && !gcm_support)
3672
			continue;
3673 3674 3675 3676 3677

		/*
		 * Skip algorithms requiring message digests
		 * if MD or MD size is not supported by device.
		 */
3678
		if (is_mdha(c2_alg_sel) &&
3679 3680
		    (!md_inst || t_alg->aead.maxauthsize > md_limit))
			continue;
3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692

		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;
3693
	}
3694 3695

	if (registered)
3696
		pr_info("caam algorithms registered in /proc/crypto\n");
3697

3698 3699
out_put_dev:
	put_device(&pdev->dev);
3700 3701 3702 3703 3704 3705 3706 3707 3708
	return err;
}

module_init(caam_algapi_init);
module_exit(caam_algapi_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("FSL CAAM support for crypto API");
MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");