DPDK logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  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
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
/* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
 *
 * Copyright 2008-2016 Freescale Semiconductor Inc.
 * Copyright 2016,2019-2020 NXP
 *
 */

#ifndef __DESC_ALGO_H__
#define __DESC_ALGO_H__

#include "rta.h"
#include "common.h"

/**
 * DOC: Algorithms - Shared Descriptor Constructors
 *
 * Shared descriptors for algorithms (i.e. not for protocols).
 */

/**
 * cnstr_shdsc_zuce - ZUC Enc (EEA2) as a shared descriptor
 * @descbuf: pointer to descriptor-under-construction buffer
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @cipherdata: pointer to block cipher transform definitions
 * @dir: Cipher direction (DIR_ENC/DIR_DEC)
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_zuce(uint32_t *descbuf, bool ps, bool swap,
		    struct alginfo *cipherdata, uint8_t dir)
{
	struct program prg;
	struct program *p = &prg;

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);

	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);
	SHR_HDR(p, SHR_ALWAYS, 1, 0);

	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));

	SEQLOAD(p, CONTEXT1, 0, 16, 0);

	MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
	MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);
	ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCE, OP_ALG_AAI_F8,
		      OP_ALG_AS_INITFINAL, 0, dir);
	SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
	SEQFIFOSTORE(p, MSG, 0, 0, VLF);

	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_zuca - ZUC Auth (EIA2) as a shared descriptor
 * @descbuf: pointer to descriptor-under-construction buffer
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @authdata: pointer to authentication transform definitions
 * @chk_icv: Whether to compare and verify ICV (true/false)
 * @authlen: size of digest
 *
 * The IV prepended before hmac payload must be 8 bytes consisting
 * of COUNT||BEAERER||DIR. The COUNT is of 32-bits, bearer is of 5 bits and
 * direction is of 1 bit - totalling to 38 bits.
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_zuca(uint32_t *descbuf, bool ps, bool swap,
		 struct alginfo *authdata, uint8_t chk_icv,
		 uint32_t authlen)
{
	struct program prg;
	struct program *p = &prg;
	int dir = chk_icv ? DIR_DEC : DIR_ENC;

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);

	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);
	SHR_HDR(p, SHR_ALWAYS, 1, 0);

	KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
	    authdata->keylen, INLINE_KEY(authdata));

	SEQLOAD(p, CONTEXT2, 0, 8, 0);

	if (chk_icv == ICV_CHECK_ENABLE)
		MATHB(p, SEQINSZ, SUB, authlen, VSEQINSZ, 4, IMMED2);
	else
		MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);

	ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCA, OP_ALG_AAI_F9,
		      OP_ALG_AS_INITFINAL, chk_icv, dir);

	SEQFIFOLOAD(p, MSG2, 0, VLF | CLASS2 | LAST2);

	if (chk_icv == ICV_CHECK_ENABLE)
		SEQFIFOLOAD(p, ICV2, authlen, LAST2);
	else
		/* Save lower half of MAC out into a 32-bit sequence */
		SEQSTORE(p, CONTEXT2, 0, authlen, 0);

	return PROGRAM_FINALIZE(p);
}


/**
 * cnstr_shdsc_snow_f8 - SNOW/f8 (UEA2) as a shared descriptor
 * @descbuf: pointer to descriptor-under-construction buffer
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @cipherdata: pointer to block cipher transform definitions
 * @dir: Cipher direction (DIR_ENC/DIR_DEC)
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_snow_f8(uint32_t *descbuf, bool ps, bool swap,
		    struct alginfo *cipherdata, uint8_t dir)
{
	struct program prg;
	struct program *p = &prg;

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);

	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);
	SHR_HDR(p, SHR_ALWAYS, 1, 0);

	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));

	SEQLOAD(p, CONTEXT1, 0, 16, 0);

	MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
	MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);
	ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F8, OP_ALG_AAI_F8,
		      OP_ALG_AS_INITFINAL, 0, dir);
	SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
	SEQFIFOSTORE(p, MSG, 0, 0, VLF);

	return PROGRAM_FINALIZE(p);
}

/**
 * conv_to_zuc_eia_iv - ZUCA IV 16-byte to 8-byte convert
 * function for 3G.
 * @iv: 16 bytes of original IV data.
 *
 * From the original IV, we extract 32-bits of COUNT,
 * 5-bits of bearer and 1-bit of direction.
 * Refer to CAAM refman for ZUCA IV format. Then these values are
 * appended as COUNT||BEARER||DIR continuously to make a 38-bit block.
 * This 38-bit block is copied left justified into 8-byte array used as
 * converted IV.
 *
 * Return: 8-bytes of IV data as understood by SEC HW
 */

static inline uint8_t *conv_to_zuc_eia_iv(uint8_t *iv)
{
	uint8_t dir = (iv[14] & 0x80) ? 4 : 0;

	iv[12] = iv[4] | dir;
	iv[13] = 0;
	iv[14] = 0;
	iv[15] = 0;

	iv[8] = iv[0];
	iv[9] = iv[1];
	iv[10] = iv[2];
	iv[11] = iv[3];

	return (iv + 8);
}

/**
 * conv_to_snow_f9_iv - SNOW/f9 (UIA2) IV 16 byte to 12 byte convert
 * function for 3G.
 * @iv: 16 byte original IV data
 *
 * Return: 12 byte IV data as understood by SEC HW
 */

static inline uint8_t *conv_to_snow_f9_iv(uint8_t *iv)
{
	uint8_t temp = (iv[8] == iv[0]) ? 0 : 4;

	iv[12] = iv[4];
	iv[13] = iv[5];
	iv[14] = iv[6];
	iv[15] = iv[7];

	iv[8] = temp;
	iv[9] = 0x00;
	iv[10] = 0x00;
	iv[11] = 0x00;

	iv[4] = iv[0];
	iv[5] = iv[1];
	iv[6] = iv[2];
	iv[7] = iv[3];

	return (iv + 4);
}

/**
 * cnstr_shdsc_snow_f9 - SNOW/f9 (UIA2) as a shared descriptor
 * @descbuf: pointer to descriptor-under-construction buffer
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @authdata: pointer to authentication transform definitions
 * @chk_icv: check or generate ICV value
 * @authlen: size of digest
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_snow_f9(uint32_t *descbuf, bool ps, bool swap,
		    struct alginfo *authdata, uint8_t chk_icv,
		    uint32_t authlen)
{
	struct program prg;
	struct program *p = &prg;
	int dir = chk_icv ? DIR_DEC : DIR_ENC;

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);

	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);

	SHR_HDR(p, SHR_ALWAYS, 1, 0);

	KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
	    authdata->keylen, INLINE_KEY(authdata));

	SEQLOAD(p, CONTEXT2, 0, 12, 0);

	if (chk_icv == ICV_CHECK_ENABLE)
		MATHB(p, SEQINSZ, SUB, authlen, VSEQINSZ, 4, IMMED2);
	else
		MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);

	ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F9, OP_ALG_AAI_F9,
		      OP_ALG_AS_INITFINAL, chk_icv, dir);

	SEQFIFOLOAD(p, MSG2, 0, VLF | CLASS2 | LAST2);

	if (chk_icv == ICV_CHECK_ENABLE)
		SEQFIFOLOAD(p, ICV2, authlen, LAST2);
	else
		/* Save lower half of MAC out into a 32-bit sequence */
		SEQSTORE(p, CONTEXT2, 0, authlen, 0);

	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_blkcipher - block cipher transformation
 * @descbuf: pointer to descriptor-under-construction buffer
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @share: sharing type of shared descriptor
 * @cipherdata: pointer to block cipher transform definitions
 *              Valid algorithm values one of OP_ALG_ALGSEL_* {DES, 3DES, AES}
 *              Valid modes for:
 *                  AES: OP_ALG_AAI_* {CBC, CTR}
 *                  DES, 3DES: OP_ALG_AAI_CBC
 * @iv: IV data; if NULL, "ivlen" bytes from the input frame will be read as IV
 * @ivlen: IV length
 * @dir: DIR_ENC/DIR_DEC
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_blkcipher(uint32_t *descbuf, bool ps, bool swap,
		      enum rta_share_type share,
		      struct alginfo *cipherdata,
		      uint32_t ivlen, uint8_t dir)
{
	struct program prg;
	struct program *p = &prg;
	uint32_t iv_off = 0, counter;
	const bool need_dk = (dir == DIR_DEC) &&
			     (cipherdata->algtype == OP_ALG_ALGSEL_AES) &&
			     (cipherdata->algmode == OP_ALG_AAI_CBC);
	LABEL(keyjmp);
	LABEL(skipdk);
	REFERENCE(pkeyjmp);
	REFERENCE(pskipdk);

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);
	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);
	SHR_HDR(p, share, 1, SC);

	pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SHRD);
	/* Insert Key */
	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));

	if (need_dk) {
		ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
			      OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, dir);

		pskipdk = JUMP(p, skipdk, LOCAL_JUMP, ALL_TRUE, 0);
	}
	SET_LABEL(p, keyjmp);

	if (need_dk) {
		ALG_OPERATION(p, OP_ALG_ALGSEL_AES, cipherdata->algmode |
			      OP_ALG_AAI_DK, OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE, dir);
		SET_LABEL(p, skipdk);
	} else {
		ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
			      OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, dir);
	}

	if (cipherdata->algmode == OP_ALG_AAI_CTR)
		iv_off = 16;

	/* IV is present first before the actual message */
	SEQLOAD(p, CONTEXT1, iv_off, ivlen, 0);

	/* If IV len is less than 16 bytes, set 'counter' as 1 */
	if (cipherdata->algmode == OP_ALG_AAI_CTR && ivlen < 16) {
		counter = 1;
		if (!swap)
			counter = swab32(1);

		LOAD(p, counter, CONTEXT1, (iv_off + ivlen), 16 - ivlen, IMMED);
	}

	MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
	MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);

	/* Insert sequence load/store with VLF */
	SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
	SEQFIFOSTORE(p, MSG, 0, 0, VLF);

	PATCH_JUMP(p, pkeyjmp, keyjmp);
	if (need_dk)
		PATCH_JUMP(p, pskipdk, skipdk);

	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_hmac - HMAC shared
 * @descbuf: pointer to descriptor-under-construction buffer
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @share: sharing type of shared descriptor
 * @authdata: pointer to authentication transform definitions;
 *            message digest algorithm: OP_ALG_ALGSEL_MD5/ SHA1-512.
 * @do_icv: 0 if ICV checking is not desired, any other value if ICV checking
 *          is needed for all the packets processed by this shared descriptor
 * @trunc_len: Length of the truncated ICV to be written in the output buffer, 0
 *             if no truncation is needed
 *
 * Note: There's no support for keys longer than the block size of the
 * underlying hash function, according to the selected algorithm.
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_hmac(uint32_t *descbuf, bool ps, bool swap,
		 enum rta_share_type share,
		 struct alginfo *authdata, uint8_t do_icv,
		 uint8_t trunc_len)
{
	struct program prg;
	struct program *p = &prg;
	uint8_t storelen, opicv, dir;
	LABEL(keyjmp);
	LABEL(jmpprecomp);
	REFERENCE(pkeyjmp);
	REFERENCE(pjmpprecomp);

	/* Compute fixed-size store based on alg selection */
	switch (authdata->algtype) {
	case OP_ALG_ALGSEL_MD5:
		storelen = 16;
		break;
	case OP_ALG_ALGSEL_SHA1:
		storelen = 20;
		break;
	case OP_ALG_ALGSEL_SHA224:
		storelen = 28;
		break;
	case OP_ALG_ALGSEL_SHA256:
		storelen = 32;
		break;
	case OP_ALG_ALGSEL_SHA384:
		storelen = 48;
		break;
	case OP_ALG_ALGSEL_SHA512:
		storelen = 64;
		break;
	default:
		return -EINVAL;
	}

	trunc_len = trunc_len && (trunc_len < storelen) ? trunc_len : storelen;

	opicv = do_icv ? ICV_CHECK_ENABLE : ICV_CHECK_DISABLE;
	dir = do_icv ? DIR_DEC : DIR_ENC;

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);
	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);
	SHR_HDR(p, share, 1, SC);

	pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SHRD);
	KEY(p, KEY2, authdata->key_enc_flags, authdata->key, authdata->keylen,
	    INLINE_KEY(authdata));

	/* Do operation */
	ALG_OPERATION(p, authdata->algtype, OP_ALG_AAI_HMAC,
		      OP_ALG_AS_INITFINAL, opicv, dir);

	pjmpprecomp = JUMP(p, jmpprecomp, LOCAL_JUMP, ALL_TRUE, 0);
	SET_LABEL(p, keyjmp);

	ALG_OPERATION(p, authdata->algtype, OP_ALG_AAI_HMAC_PRECOMP,
		      OP_ALG_AS_INITFINAL, opicv, dir);

	SET_LABEL(p, jmpprecomp);

	/* compute sequences */
	if (opicv == ICV_CHECK_ENABLE)
		MATHB(p, SEQINSZ, SUB, trunc_len, VSEQINSZ, 4, IMMED2);
	else
		MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);

	/* Do load (variable length) */
	SEQFIFOLOAD(p, MSG2, 0, VLF | LAST2);

	if (opicv == ICV_CHECK_ENABLE)
		SEQFIFOLOAD(p, ICV2, trunc_len, LAST2);
	else
		SEQSTORE(p, CONTEXT2, 0, trunc_len, 0);

	PATCH_JUMP(p, pkeyjmp, keyjmp);
	PATCH_JUMP(p, pjmpprecomp, jmpprecomp);

	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_hash - HASH shared
 * @descbuf: pointer to descriptor-under-construction buffer
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @share: sharing type of shared descriptor
 * @authdata: pointer to authentication transform definitions;
 *            message digest algorithm: OP_ALG_ALGSEL_MD5/ SHA1-512.
 * @do_icv: 0 if ICV checking is not desired, any other value if ICV checking
 *          is needed for all the packets processed by this shared descriptor
 * @trunc_len: Length of the truncated ICV to be written in the output buffer, 0
 *             if no truncation is needed
 *
 * Note: There's no support for keys longer than the block size of the
 * underlying hash function, according to the selected algorithm.
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_hash(uint32_t *descbuf, bool ps, bool swap,
		 enum rta_share_type share,
		 struct alginfo *authdata, uint8_t do_icv,
		 uint8_t trunc_len)
{
	struct program prg;
	struct program *p = &prg;
	uint8_t storelen, opicv, dir;

	/* Compute fixed-size store based on alg selection */
	switch (authdata->algtype) {
	case OP_ALG_ALGSEL_MD5:
		storelen = 16;
		break;
	case OP_ALG_ALGSEL_SHA1:
		storelen = 20;
		break;
	case OP_ALG_ALGSEL_SHA224:
		storelen = 28;
		break;
	case OP_ALG_ALGSEL_SHA256:
		storelen = 32;
		break;
	case OP_ALG_ALGSEL_SHA384:
		storelen = 48;
		break;
	case OP_ALG_ALGSEL_SHA512:
		storelen = 64;
		break;
	default:
		return -EINVAL;
	}

	trunc_len = trunc_len && (trunc_len < storelen) ? trunc_len : storelen;

	opicv = do_icv ? ICV_CHECK_ENABLE : ICV_CHECK_DISABLE;
	dir = do_icv ? DIR_DEC : DIR_ENC;

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);
	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);
	SHR_HDR(p, share, 1, SC);

	/* Do operation */
	/* compute sequences */
	if (opicv == ICV_CHECK_ENABLE)
		MATHB(p, SEQINSZ, SUB, trunc_len, VSEQINSZ, 4, IMMED2);
	else
		MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);

	ALG_OPERATION(p, authdata->algtype,
		      OP_ALG_AAI_HASH,
		      OP_ALG_AS_INITFINAL, opicv, dir);
	SEQFIFOLOAD(p, MSG2, 0, VLF | LAST2);

	if (opicv == ICV_CHECK_ENABLE)
		SEQFIFOLOAD(p, ICV2, trunc_len, LAST2);
	else
		SEQSTORE(p, CONTEXT2, 0, trunc_len, 0);

	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_kasumi_f8 - KASUMI F8 (Confidentiality) as a shared descriptor
 *                         (ETSI "Document 1: f8 and f9 specification")
 * @descbuf: pointer to descriptor-under-construction buffer
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @cipherdata: pointer to block cipher transform definitions
 * @dir: cipher direction (DIR_ENC/DIR_DEC)
 * @count: count value (32 bits)
 * @bearer: bearer ID (5 bits)
 * @direction: direction (1 bit)
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_kasumi_f8(uint32_t *descbuf, bool ps, bool swap,
		      struct alginfo *cipherdata, uint8_t dir)
{
	struct program prg;
	struct program *p = &prg;

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);
	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);
	SHR_HDR(p, SHR_ALWAYS, 1, 0);

	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));
	SEQLOAD(p, CONTEXT1, 0, 8, 0);
	MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
	MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);
	ALG_OPERATION(p, OP_ALG_ALGSEL_KASUMI, OP_ALG_AAI_F8,
		      OP_ALG_AS_INITFINAL, 0, dir);
	SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
	SEQFIFOSTORE(p, MSG, 0, 0, VLF);

	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_kasumi_f9 -  KASUMI F9 (Integrity) as a shared descriptor
 *                          (ETSI "Document 1: f8 and f9 specification")
 * @descbuf: pointer to descriptor-under-construction buffer
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @authdata: pointer to authentication transform definitions
 * @chk_icv: check or generate ICV value
 * @authlen: size of digest
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_kasumi_f9(uint32_t *descbuf, bool ps, bool swap,
		    struct alginfo *authdata, uint8_t chk_icv,
		    uint32_t authlen)
{
	struct program prg;
	struct program *p = &prg;
	int dir = chk_icv ? DIR_DEC : DIR_ENC;

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);

	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);

	SHR_HDR(p, SHR_ALWAYS, 1, 0);

	KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
	    authdata->keylen, INLINE_KEY(authdata));

	SEQLOAD(p, CONTEXT2, 0, 12, 0);

	if (chk_icv == ICV_CHECK_ENABLE)
		MATHB(p, SEQINSZ, SUB, authlen, VSEQINSZ, 4, IMMED2);
	else
		MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);

	ALG_OPERATION(p, OP_ALG_ALGSEL_KASUMI, OP_ALG_AAI_F9,
		      OP_ALG_AS_INITFINAL, chk_icv, dir);

	SEQFIFOLOAD(p, MSG2, 0, VLF | CLASS2 | LAST2);

	if (chk_icv == ICV_CHECK_ENABLE)
		SEQFIFOLOAD(p, ICV2, authlen, LAST2);
	else
		/* Save lower half of MAC out into a 32-bit sequence */
		SEQSTORE(p, CONTEXT2, 0, authlen, 0);

	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_crc - CRC32 Accelerator (IEEE 802 CRC32 protocol mode)
 * @descbuf: pointer to descriptor-under-construction buffer
 * @swap: must be true when core endianness doesn't match SEC endianness
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_crc(uint32_t *descbuf, bool swap)
{
	struct program prg;
	struct program *p = &prg;

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);

	SHR_HDR(p, SHR_ALWAYS, 1, 0);

	MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
	ALG_OPERATION(p, OP_ALG_ALGSEL_CRC,
		      OP_ALG_AAI_802 | OP_ALG_AAI_DOC,
		      OP_ALG_AS_FINALIZE, 0, DIR_ENC);
	SEQFIFOLOAD(p, MSG2, 0, VLF | LAST2);
	SEQSTORE(p, CONTEXT2, 0, 4, 0);

	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_gcm_encap - AES-GCM encap as a shared descriptor
 * @descbuf: pointer to descriptor-under-construction buffer
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @share: sharing type of shared descriptor
 * @cipherdata: pointer to block cipher transform definitions
 *		Valid algorithm values - OP_ALG_ALGSEL_AES ANDed with
 *		OP_ALG_AAI_GCM.
 * @ivlen: Initialization vector length
 * @icvsize: integrity check value (ICV) size (truncated or full)
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_gcm_encap(uint32_t *descbuf, bool ps, bool swap,
		      enum rta_share_type share,
		      struct alginfo *cipherdata,
		      uint32_t ivlen, uint32_t icvsize)
{
	struct program prg;
	struct program *p = &prg;

	LABEL(keyjmp);
	LABEL(zeroassocjump2);
	LABEL(zeroassocjump1);
	LABEL(zeropayloadjump);
	REFERENCE(pkeyjmp);
	REFERENCE(pzeroassocjump2);
	REFERENCE(pzeroassocjump1);
	REFERENCE(pzeropayloadjump);

	PROGRAM_CNTXT_INIT(p, descbuf, 0);

	if (swap)
		PROGRAM_SET_BSWAP(p);
	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);

	SHR_HDR(p, share, 1, SC);

	pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SELF | SHRD);
	/* Insert Key */
	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));

	SET_LABEL(p, keyjmp);

	/* class 1 operation */
	ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
		      OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, DIR_ENC);

	MATHB(p, DPOVRD, AND, 0x7fffffff, MATH3, 4, IMMED2);

	/* if assoclen + cryptlen is ZERO, skip to ICV write */
	MATHB(p, SEQINSZ, SUB, ivlen, VSEQOUTSZ, 4, IMMED2);
	pzeroassocjump2 = JUMP(p, zeroassocjump2, LOCAL_JUMP, ALL_TRUE, MATH_Z);

	SEQFIFOLOAD(p, IV1, ivlen, FLUSH1);

	/* if assoclen is ZERO, skip reading the assoc data */
	MATHB(p, ZERO, ADD, MATH3, VSEQINSZ, 4, 0);
	pzeroassocjump1 = JUMP(p, zeroassocjump1, LOCAL_JUMP, ALL_TRUE, MATH_Z);

	/* cryptlen = seqinlen - assoclen */
	MATHB(p, SEQINSZ, SUB, MATH3, VSEQOUTSZ, 4, 0);

	/* if cryptlen is ZERO jump to zero-payload commands */
	pzeropayloadjump = JUMP(p, zeropayloadjump, LOCAL_JUMP, ALL_TRUE,
				MATH_Z);

	/* read assoc data */
	SEQFIFOLOAD(p, AAD1, 0, CLASS1 | VLF | FLUSH1);
	SET_LABEL(p, zeroassocjump1);

	MATHB(p, SEQINSZ, SUB, MATH0, VSEQINSZ, 4, 0);

	/* write encrypted data */
	SEQFIFOSTORE(p, MSG, 0, 0, VLF);

	/* read payload data */
	SEQFIFOLOAD(p, MSG1, 0, CLASS1 | VLF | LAST1);

	/* jump the zero-payload commands */
	JUMP(p, 4, LOCAL_JUMP, ALL_TRUE, 0);

	/* zero-payload commands */
	SET_LABEL(p, zeropayloadjump);

	/* read assoc data */
	SEQFIFOLOAD(p, AAD1, 0, CLASS1 | VLF | LAST1);

	JUMP(p, 2, LOCAL_JUMP, ALL_TRUE, 0);

	/* There is no input data */
	SET_LABEL(p, zeroassocjump2);

	SEQFIFOLOAD(p, IV1, ivlen, FLUSH1 | LAST1);

	/* write ICV */
	SEQSTORE(p, CONTEXT1, 0, icvsize, 0);

	PATCH_JUMP(p, pkeyjmp, keyjmp);
	PATCH_JUMP(p, pzeroassocjump2, zeroassocjump2);
	PATCH_JUMP(p, pzeroassocjump1, zeroassocjump1);
	PATCH_JUMP(p, pzeropayloadjump, zeropayloadjump);

	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_gcm_decap - AES-GCM decap as a shared descriptor
 * @descbuf: pointer to descriptor-under-construction buffer
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @share: sharing type of shared descriptor
 * @cipherdata: pointer to block cipher transform definitions
 *		Valid algorithm values - OP_ALG_ALGSEL_AES ANDed with
 *		OP_ALG_AAI_GCM.
 * @icvsize: integrity check value (ICV) size (truncated or full)
 *
 * Return: size of descriptor written in words or negative number on error
 */
static inline int
cnstr_shdsc_gcm_decap(uint32_t *descbuf, bool ps, bool swap,
		      enum rta_share_type share,
		      struct alginfo *cipherdata,
		      uint32_t ivlen, uint32_t icvsize)
{
	struct program prg;
	struct program *p = &prg;

	LABEL(keyjmp);
	LABEL(zeroassocjump1);
	LABEL(zeropayloadjump);
	REFERENCE(pkeyjmp);
	REFERENCE(pzeroassocjump1);
	REFERENCE(pzeropayloadjump);

	PROGRAM_CNTXT_INIT(p, descbuf, 0);

	if (swap)
		PROGRAM_SET_BSWAP(p);
	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);

	SHR_HDR(p, share, 1, SC);

	pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SELF | SHRD);
	/* Insert Key */
	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));

	SET_LABEL(p, keyjmp);

	/* class 1 operation */
	ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
		      OP_ALG_AS_INITFINAL, ICV_CHECK_ENABLE, DIR_DEC);

	MATHB(p, DPOVRD, AND, 0x7fffffff, MATH3, 4, IMMED2);
	SEQFIFOLOAD(p, IV1, ivlen, FLUSH1);

	/* if assoclen is ZERO, skip reading the assoc data */
	MATHB(p, ZERO, ADD, MATH3, VSEQINSZ, 4, 0);
	pzeroassocjump1 = JUMP(p, zeroassocjump1, LOCAL_JUMP, ALL_TRUE, MATH_Z);

	/* read assoc data */
	SEQFIFOLOAD(p, AAD1, 0, CLASS1 | VLF | FLUSH1);

	SET_LABEL(p, zeroassocjump1);

	/* cryptlen = seqoutlen - assoclen */
	MATHB(p, SEQOUTSZ, SUB, MATH0, VSEQINSZ, 4, 0);

	/* jump to zero-payload command if cryptlen is zero */
	pzeropayloadjump = JUMP(p, zeropayloadjump, LOCAL_JUMP, ALL_TRUE,
				MATH_Z);

	MATHB(p, SEQOUTSZ, SUB, MATH0, VSEQOUTSZ, 4, 0);

	/* store encrypted data */
	SEQFIFOSTORE(p, MSG, 0, 0, VLF);

	/* read payload data */
	SEQFIFOLOAD(p, MSG1, 0, CLASS1 | VLF | FLUSH1);

	/* zero-payload command */
	SET_LABEL(p, zeropayloadjump);

	/* read ICV */
	SEQFIFOLOAD(p, ICV1, icvsize, CLASS1 | LAST1);

	PATCH_JUMP(p, pkeyjmp, keyjmp);
	PATCH_JUMP(p, pzeroassocjump1, zeroassocjump1);
	PATCH_JUMP(p, pzeropayloadjump, zeropayloadjump);

	return PROGRAM_FINALIZE(p);
}

#endif /* __DESC_ALGO_H__ */