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

#include "compat.h"

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

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

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

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

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

80 81 82 83 84 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
	skcipher_unmap(jrdev, edesc, req);
1014 1015

	/*
1016
	 * The crypto API expects us to set the IV (req->iv) to the last
1017 1018
	 * ciphertext block. This is used e.g. by the CTS mode.
	 */
1019 1020 1021
	if (ivsize)
		scatterwalk_map_and_copy(req->iv, req->dst, req->cryptlen -
					 ivsize, ivsize, 0);
1022

1023 1024 1025 1026 1027 1028 1029 1030 1031
#ifdef DEBUG
	print_hex_dump(KERN_ERR, "dstiv  @"__stringify(__LINE__)": ",
		       DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
		       edesc->src_nents > 1 ? 100 : ivsize, 1);
#endif
	caam_dump_sg(KERN_ERR, "dst    @" __stringify(__LINE__)": ",
		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);

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 1054
	skcipher_unmap(jrdev, edesc, req);

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

	kfree(edesc);

1065
	skcipher_request_complete(req, err);
Y
Yuan Kang 已提交
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 1091
/*
 * 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) {
1092 1093
		src_dma = edesc->mapped_src_nents ? sg_dma_address(req->src) :
						    0;
1094 1095 1096
		in_options = 0;
	} else {
		src_dma = edesc->sec4_sg_dma;
1097
		sec4_sg_index += edesc->mapped_src_nents;
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
		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)) {
1108
		if (!edesc->mapped_dst_nents) {
1109
			dst_dma = 0;
1110
			out_options = 0;
1111
		} else if (edesc->mapped_dst_nents == 1) {
1112
			dst_dma = sg_dma_address(req->dst);
1113
			out_options = 0;
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 1139
		} 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;
1140
	bool generic_gcm = (ivsize == GCM_AES_IV_SIZE);
1141 1142 1143
	unsigned int last;

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

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

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

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

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

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

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

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

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

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

#ifdef DEBUG
1262
	print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
1263 1264 1265
		       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 已提交
1266
#endif
1267 1268
	caam_dump_sg(KERN_ERR, "src    @" __stringify(__LINE__)": ",
		     DUMP_PREFIX_ADDRESS, 16, 4, req->src,
1269 1270 1271 1272
		     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 已提交
1273 1274 1275 1276

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

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

	if (likely(req->src == req->dst)) {
1288 1289 1290 1291
		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 已提交
1292
	} else {
1293 1294 1295
		dst_dma = edesc->sec4_sg_dma + sec4_sg_index *
			  sizeof(struct sec4_sg_entry);
		out_options = LDST_SGF;
Y
Yuan Kang 已提交
1296
	}
1297

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

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

1318
	if (unlikely(req->dst != req->src)) {
1319 1320
		src_nents = sg_nents_for_len(req->src, req->assoclen +
					     req->cryptlen);
1321 1322 1323 1324 1325 1326
		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);
		}

1327 1328 1329 1330
		dst_nents = sg_nents_for_len(req->dst, req->assoclen +
					     req->cryptlen +
						(encrypt ? authsize :
							   (-authsize)));
1331 1332 1333 1334 1335 1336
		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);
		}
1337
	} else {
1338 1339 1340
		src_nents = sg_nents_for_len(req->src, req->assoclen +
					     req->cryptlen +
					     (encrypt ? authsize : 0));
1341 1342 1343 1344 1345 1346
		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);
		}
1347
	}
1348

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

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

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

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

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

Y
Yuan Kang 已提交
1414
	sec4_sg_index = 0;
1415 1416 1417 1418
	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;
1419
	}
1420 1421
	if (mapped_dst_nents > 1) {
		sg_to_sec4_sg_last(req->dst, mapped_dst_nents,
Y
Yuan Kang 已提交
1422
				   edesc->sec4_sg + sec4_sg_index, 0);
1423
	}
1424 1425 1426 1427

	if (!sec4_sg_bytes)
		return edesc;

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

1437 1438
	edesc->sec4_sg_bytes = sec4_sg_bytes;

1439 1440 1441
	return edesc;
}

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

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

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

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

1474
	return ret;
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 1542
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;
}

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

	return gcm_encrypt(req);
}

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

	/* Create and submit job descriptor */
1568
	init_authenc_job(req, edesc, all_contig, true);
1569 1570 1571 1572 1573 1574 1575
#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;
1576
	ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
1577 1578 1579
	if (!ret) {
		ret = -EINPROGRESS;
	} else {
1580
		aead_unmap(jrdev, edesc, req);
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 1621
		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;
}

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

	return gcm_decrypt(req);
}

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

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

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

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

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

1667 1668
	return ret;
}
1669

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

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

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

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

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

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

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

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

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

1773
	/* Make sure IV is located in a DMAable area */
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
	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);
		}
1786

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

1793 1794 1795
	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 已提交
1796 1797
	}

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

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

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

	return edesc;
}

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

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

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

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

	return ret;
}

1856
static int skcipher_decrypt(struct skcipher_request *req)
Y
Yuan Kang 已提交
1857
{
1858 1859 1860 1861
	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 已提交
1862 1863 1864 1865 1866
	struct device *jrdev = ctx->jrdev;
	u32 *desc;
	int ret = 0;

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

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

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

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

	return ret;
}

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

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

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

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

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

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

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

	return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3520
int caam_algapi_init(struct device *ctrldev)
3521
{
3522
	struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
3523
	int i = 0, err = 0;
3524
	u32 aes_vid, aes_inst, des_inst, md_vid, md_inst, ccha_inst, ptha_inst;
3525
	u32 arc4_inst;
3526
	unsigned int md_limit = SHA512_DIGEST_SIZE;
3527
	bool registered = false, gcm_support;
3528

3529 3530 3531 3532
	/*
	 * Register crypto algorithms the device supports.
	 * First, detect presence and attributes of DES, AES, and MD blocks.
	 */
3533
	if (priv->era < 10) {
3534
		u32 cha_vid, cha_inst, aes_rn;
3535 3536 3537 3538 3539 3540 3541 3542 3543 3544

		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;
3545 3546
		arc4_inst = (cha_inst & CHA_ID_LS_ARC4_MASK) >>
			    CHA_ID_LS_ARC4_SHIFT;
3547 3548
		ccha_inst = 0;
		ptha_inst = 0;
3549 3550 3551 3552

		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);
3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564
	} 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;
3565 3566
		ccha_inst = rd_reg32(&priv->ctrl->vreg.ccha) & CHA_VER_NUM_MASK;
		ptha_inst = rd_reg32(&priv->ctrl->vreg.ptha) & CHA_VER_NUM_MASK;
3567
		arc4_inst = rd_reg32(&priv->ctrl->vreg.afha) & CHA_VER_NUM_MASK;
3568 3569

		gcm_support = aesa & CHA_VER_MISC_AES_GCM;
3570
	}
3571 3572

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

3576
	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
3577 3578
		struct caam_skcipher_alg *t_alg = driver_algs + i;
		u32 alg_sel = t_alg->caam.class1_alg_type & OP_ALG_ALGSEL_MASK;
3579 3580 3581 3582 3583 3584 3585 3586 3587 3588

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

3590 3591 3592 3593
		/* Skip ARC4 algorithms if not supported by device */
		if (!arc4_inst && alg_sel == OP_ALG_ALGSEL_ARC4)
			continue;

3594 3595 3596 3597
		/*
		 * Check support for AES modes not available
		 * on LP devices.
		 */
3598 3599 3600 3601
		if (aes_vid == CHA_VER_VID_AES_LP &&
		    (t_alg->caam.class1_alg_type & OP_ALG_AAI_MASK) ==
		    OP_ALG_AAI_XTS)
			continue;
3602

3603
		caam_skcipher_alg_init(t_alg);
3604

3605
		err = crypto_register_skcipher(&t_alg->skcipher);
3606
		if (err) {
3607
			pr_warn("%s alg registration failed\n",
3608
				t_alg->skcipher.base.cra_driver_name);
3609 3610 3611
			continue;
		}

3612
		t_alg->registered = true;
3613 3614 3615 3616 3617
		registered = true;
	}

	for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
		struct caam_aead_alg *t_alg = driver_aeads + i;
3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
		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;

3634 3635 3636 3637 3638 3639 3640 3641
		/* 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;

3642 3643 3644
		/* Skip GCM algorithms if not supported by device */
		if (c1_alg_sel == OP_ALG_ALGSEL_AES &&
		    alg_aai == OP_ALG_AAI_GCM && !gcm_support)
3645
			continue;
3646 3647 3648 3649 3650

		/*
		 * Skip algorithms requiring message digests
		 * if MD or MD size is not supported by device.
		 */
3651
		if (is_mdha(c2_alg_sel) &&
3652 3653
		    (!md_inst || t_alg->aead.maxauthsize > md_limit))
			continue;
3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665

		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;
3666
	}
3667 3668

	if (registered)
3669
		pr_info("caam algorithms registered in /proc/crypto\n");
3670 3671 3672

	return err;
}