crypto/dpaax_sec: enhance GCM descs to not skip AAD
[dpdk.git] / drivers / crypto / dpaa2_sec / hw / desc / algo.h
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2008-2016 Freescale Semiconductor Inc.
4  * Copyright 2016,2019 NXP
5  *
6  */
7
8 #ifndef __DESC_ALGO_H__
9 #define __DESC_ALGO_H__
10
11 #include "hw/rta.h"
12 #include "common.h"
13
14 /**
15  * DOC: Algorithms - Shared Descriptor Constructors
16  *
17  * Shared descriptors for algorithms (i.e. not for protocols).
18  */
19
20 /**
21  * cnstr_shdsc_zuce - ZUC Enc (EEA2) as a shared descriptor
22  * @descbuf: pointer to descriptor-under-construction buffer
23  * @ps: if 36/40bit addressing is desired, this parameter must be true
24  * @swap: must be true when core endianness doesn't match SEC endianness
25  * @cipherdata: pointer to block cipher transform definitions
26  * @dir: Cipher direction (DIR_ENC/DIR_DEC)
27  *
28  * Return: size of descriptor written in words or negative number on error
29  */
30 static inline int
31 cnstr_shdsc_zuce(uint32_t *descbuf, bool ps, bool swap,
32                     struct alginfo *cipherdata, uint8_t dir)
33 {
34         struct program prg;
35         struct program *p = &prg;
36
37         PROGRAM_CNTXT_INIT(p, descbuf, 0);
38         if (swap)
39                 PROGRAM_SET_BSWAP(p);
40
41         if (ps)
42                 PROGRAM_SET_36BIT_ADDR(p);
43         SHR_HDR(p, SHR_ALWAYS, 1, 0);
44
45         KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
46             cipherdata->keylen, INLINE_KEY(cipherdata));
47
48         SEQLOAD(p, CONTEXT1, 0, 16, 0);
49
50         MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
51         MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);
52         ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCE, OP_ALG_AAI_F8,
53                       OP_ALG_AS_INITFINAL, 0, dir);
54         SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
55         SEQFIFOSTORE(p, MSG, 0, 0, VLF);
56
57         return PROGRAM_FINALIZE(p);
58 }
59
60 /**
61  * cnstr_shdsc_zuca - ZUC Auth (EIA2) as a shared descriptor
62  * @descbuf: pointer to descriptor-under-construction buffer
63  * @ps: if 36/40bit addressing is desired, this parameter must be true
64  * @swap: must be true when core endianness doesn't match SEC endianness
65  * @authdata: pointer to authentication transform definitions
66  * @chk_icv: Whether to compare and verify ICV (true/false)
67  * @authlen: size of digest
68  *
69  * The IV prepended before hmac payload must be 8 bytes consisting
70  * of COUNT||BEAERER||DIR. The COUNT is of 32-bits, bearer is of 5 bits and
71  * direction is of 1 bit - totalling to 38 bits.
72  *
73  * Return: size of descriptor written in words or negative number on error
74  */
75 static inline int
76 cnstr_shdsc_zuca(uint32_t *descbuf, bool ps, bool swap,
77                  struct alginfo *authdata, uint8_t chk_icv,
78                  uint32_t authlen)
79 {
80         struct program prg;
81         struct program *p = &prg;
82         int dir = chk_icv ? DIR_DEC : DIR_ENC;
83
84         PROGRAM_CNTXT_INIT(p, descbuf, 0);
85         if (swap)
86                 PROGRAM_SET_BSWAP(p);
87
88         if (ps)
89                 PROGRAM_SET_36BIT_ADDR(p);
90         SHR_HDR(p, SHR_ALWAYS, 1, 0);
91
92         KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
93             authdata->keylen, INLINE_KEY(authdata));
94
95         SEQLOAD(p, CONTEXT2, 0, 8, 0);
96
97         if (chk_icv == ICV_CHECK_ENABLE)
98                 MATHB(p, SEQINSZ, SUB, authlen, VSEQINSZ, 4, IMMED2);
99         else
100                 MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
101
102         ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCA, OP_ALG_AAI_F9,
103                       OP_ALG_AS_INITFINAL, chk_icv, dir);
104
105         SEQFIFOLOAD(p, MSG2, 0, VLF | CLASS2 | LAST2);
106
107         if (chk_icv == ICV_CHECK_ENABLE)
108                 SEQFIFOLOAD(p, ICV2, authlen, LAST2);
109         else
110                 /* Save lower half of MAC out into a 32-bit sequence */
111                 SEQSTORE(p, CONTEXT2, 0, authlen, 0);
112
113         return PROGRAM_FINALIZE(p);
114 }
115
116
117 /**
118  * cnstr_shdsc_snow_f8 - SNOW/f8 (UEA2) as a shared descriptor
119  * @descbuf: pointer to descriptor-under-construction buffer
120  * @ps: if 36/40bit addressing is desired, this parameter must be true
121  * @swap: must be true when core endianness doesn't match SEC endianness
122  * @cipherdata: pointer to block cipher transform definitions
123  * @dir: Cipher direction (DIR_ENC/DIR_DEC)
124  *
125  * Return: size of descriptor written in words or negative number on error
126  */
127 static inline int
128 cnstr_shdsc_snow_f8(uint32_t *descbuf, bool ps, bool swap,
129                     struct alginfo *cipherdata, uint8_t dir)
130 {
131         struct program prg;
132         struct program *p = &prg;
133
134         PROGRAM_CNTXT_INIT(p, descbuf, 0);
135         if (swap)
136                 PROGRAM_SET_BSWAP(p);
137
138         if (ps)
139                 PROGRAM_SET_36BIT_ADDR(p);
140         SHR_HDR(p, SHR_ALWAYS, 1, 0);
141
142         KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
143             cipherdata->keylen, INLINE_KEY(cipherdata));
144
145         SEQLOAD(p, CONTEXT1, 0, 16, 0);
146
147         MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
148         MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);
149         ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F8, OP_ALG_AAI_F8,
150                       OP_ALG_AS_INITFINAL, 0, dir);
151         SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
152         SEQFIFOSTORE(p, MSG, 0, 0, VLF);
153
154         return PROGRAM_FINALIZE(p);
155 }
156
157 /**
158  * conv_to_zuc_eia_iv - ZUCA IV 16-byte to 8-byte convert
159  * function for 3G.
160  * @iv: 16 bytes of original IV data.
161  *
162  * From the original IV, we extract 32-bits of COUNT,
163  * 5-bits of bearer and 1-bit of direction.
164  * Refer to CAAM refman for ZUCA IV format. Then these values are
165  * appended as COUNT||BEARER||DIR continuously to make a 38-bit block.
166  * This 38-bit block is copied left justified into 8-byte array used as
167  * converted IV.
168  *
169  * Return: 8-bytes of IV data as understood by SEC HW
170  */
171
172 static inline uint8_t *conv_to_zuc_eia_iv(uint8_t *iv)
173 {
174         uint8_t dir = (iv[14] & 0x80) ? 4 : 0;
175
176         iv[12] = iv[4] | dir;
177         iv[13] = 0;
178         iv[14] = 0;
179         iv[15] = 0;
180
181         iv[8] = iv[0];
182         iv[9] = iv[1];
183         iv[10] = iv[2];
184         iv[11] = iv[3];
185
186         return (iv + 8);
187 }
188
189 /**
190  * conv_to_snow_f9_iv - SNOW/f9 (UIA2) IV 16 byte to 12 byte convert
191  * function for 3G.
192  * @iv: 16 byte original IV data
193  *
194  * Return: 12 byte IV data as understood by SEC HW
195  */
196
197 static inline uint8_t *conv_to_snow_f9_iv(uint8_t *iv)
198 {
199         uint8_t temp = (iv[8] == iv[0]) ? 0 : 4;
200
201         iv[12] = iv[4];
202         iv[13] = iv[5];
203         iv[14] = iv[6];
204         iv[15] = iv[7];
205
206         iv[8] = temp;
207         iv[9] = 0x00;
208         iv[10] = 0x00;
209         iv[11] = 0x00;
210
211         iv[4] = iv[0];
212         iv[5] = iv[1];
213         iv[6] = iv[2];
214         iv[7] = iv[3];
215
216         return (iv + 4);
217 }
218
219 /**
220  * cnstr_shdsc_snow_f9 - SNOW/f9 (UIA2) as a shared descriptor
221  * @descbuf: pointer to descriptor-under-construction buffer
222  * @ps: if 36/40bit addressing is desired, this parameter must be true
223  * @swap: must be true when core endianness doesn't match SEC endianness
224  * @authdata: pointer to authentication transform definitions
225  * @chk_icv: check or generate ICV value
226  * @authlen: size of digest
227  *
228  * Return: size of descriptor written in words or negative number on error
229  */
230 static inline int
231 cnstr_shdsc_snow_f9(uint32_t *descbuf, bool ps, bool swap,
232                     struct alginfo *authdata, uint8_t chk_icv,
233                     uint32_t authlen)
234 {
235         struct program prg;
236         struct program *p = &prg;
237         int dir = chk_icv ? DIR_DEC : DIR_ENC;
238
239         PROGRAM_CNTXT_INIT(p, descbuf, 0);
240         if (swap)
241                 PROGRAM_SET_BSWAP(p);
242
243         if (ps)
244                 PROGRAM_SET_36BIT_ADDR(p);
245
246         SHR_HDR(p, SHR_ALWAYS, 1, 0);
247
248         KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
249             authdata->keylen, INLINE_KEY(authdata));
250
251         SEQLOAD(p, CONTEXT2, 0, 12, 0);
252
253         if (chk_icv == ICV_CHECK_ENABLE)
254                 MATHB(p, SEQINSZ, SUB, authlen, VSEQINSZ, 4, IMMED2);
255         else
256                 MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
257
258         ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F9, OP_ALG_AAI_F9,
259                       OP_ALG_AS_INITFINAL, chk_icv, dir);
260
261         SEQFIFOLOAD(p, MSG2, 0, VLF | CLASS2 | LAST2);
262
263         if (chk_icv == ICV_CHECK_ENABLE)
264                 SEQFIFOLOAD(p, ICV2, authlen, LAST2);
265         else
266                 /* Save lower half of MAC out into a 32-bit sequence */
267                 SEQSTORE(p, CONTEXT2, 0, authlen, 0);
268
269         return PROGRAM_FINALIZE(p);
270 }
271
272 /**
273  * cnstr_shdsc_blkcipher - block cipher transformation
274  * @descbuf: pointer to descriptor-under-construction buffer
275  * @ps: if 36/40bit addressing is desired, this parameter must be true
276  * @swap: must be true when core endianness doesn't match SEC endianness
277  * @share: sharing type of shared descriptor
278  * @cipherdata: pointer to block cipher transform definitions
279  *              Valid algorithm values one of OP_ALG_ALGSEL_* {DES, 3DES, AES}
280  *              Valid modes for:
281  *                  AES: OP_ALG_AAI_* {CBC, CTR}
282  *                  DES, 3DES: OP_ALG_AAI_CBC
283  * @iv: IV data; if NULL, "ivlen" bytes from the input frame will be read as IV
284  * @ivlen: IV length
285  * @dir: DIR_ENC/DIR_DEC
286  *
287  * Return: size of descriptor written in words or negative number on error
288  */
289 static inline int
290 cnstr_shdsc_blkcipher(uint32_t *descbuf, bool ps, bool swap,
291                       enum rta_share_type share,
292                       struct alginfo *cipherdata, uint8_t *iv,
293                       uint32_t ivlen, uint8_t dir)
294 {
295         struct program prg;
296         struct program *p = &prg;
297         uint32_t iv_off = 0;
298         const bool need_dk = (dir == DIR_DEC) &&
299                              (cipherdata->algtype == OP_ALG_ALGSEL_AES) &&
300                              (cipherdata->algmode == OP_ALG_AAI_CBC);
301         LABEL(keyjmp);
302         LABEL(skipdk);
303         REFERENCE(pkeyjmp);
304         REFERENCE(pskipdk);
305
306         PROGRAM_CNTXT_INIT(p, descbuf, 0);
307         if (swap)
308                 PROGRAM_SET_BSWAP(p);
309         if (ps)
310                 PROGRAM_SET_36BIT_ADDR(p);
311         SHR_HDR(p, share, 1, SC);
312
313         pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SHRD);
314         /* Insert Key */
315         KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
316             cipherdata->keylen, INLINE_KEY(cipherdata));
317
318         if (need_dk) {
319                 ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
320                               OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, dir);
321
322                 pskipdk = JUMP(p, skipdk, LOCAL_JUMP, ALL_TRUE, 0);
323         }
324         SET_LABEL(p, keyjmp);
325
326         if (need_dk) {
327                 ALG_OPERATION(p, OP_ALG_ALGSEL_AES, cipherdata->algmode |
328                               OP_ALG_AAI_DK, OP_ALG_AS_INITFINAL,
329                               ICV_CHECK_DISABLE, dir);
330                 SET_LABEL(p, skipdk);
331         } else {
332                 ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
333                               OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, dir);
334         }
335
336         if (cipherdata->algmode == OP_ALG_AAI_CTR)
337                 iv_off = 16;
338
339         if (iv)
340                 /* IV load, convert size */
341                 LOAD(p, (uintptr_t)iv, CONTEXT1, iv_off, ivlen, IMMED | COPY);
342         else
343                 /* IV is present first before the actual message */
344                 SEQLOAD(p, CONTEXT1, iv_off, ivlen, 0);
345
346         MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
347         MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);
348
349         /* Insert sequence load/store with VLF */
350         SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
351         SEQFIFOSTORE(p, MSG, 0, 0, VLF);
352
353         PATCH_JUMP(p, pkeyjmp, keyjmp);
354         if (need_dk)
355                 PATCH_JUMP(p, pskipdk, skipdk);
356
357         return PROGRAM_FINALIZE(p);
358 }
359
360 /**
361  * cnstr_shdsc_hmac - HMAC shared
362  * @descbuf: pointer to descriptor-under-construction buffer
363  * @ps: if 36/40bit addressing is desired, this parameter must be true
364  * @swap: must be true when core endianness doesn't match SEC endianness
365  * @share: sharing type of shared descriptor
366  * @authdata: pointer to authentication transform definitions;
367  *            message digest algorithm: OP_ALG_ALGSEL_MD5/ SHA1-512.
368  * @do_icv: 0 if ICV checking is not desired, any other value if ICV checking
369  *          is needed for all the packets processed by this shared descriptor
370  * @trunc_len: Length of the truncated ICV to be written in the output buffer, 0
371  *             if no truncation is needed
372  *
373  * Note: There's no support for keys longer than the block size of the
374  * underlying hash function, according to the selected algorithm.
375  *
376  * Return: size of descriptor written in words or negative number on error
377  */
378 static inline int
379 cnstr_shdsc_hmac(uint32_t *descbuf, bool ps, bool swap,
380                  enum rta_share_type share,
381                  struct alginfo *authdata, uint8_t do_icv,
382                  uint8_t trunc_len)
383 {
384         struct program prg;
385         struct program *p = &prg;
386         uint8_t storelen, opicv, dir;
387         LABEL(keyjmp);
388         LABEL(jmpprecomp);
389         REFERENCE(pkeyjmp);
390         REFERENCE(pjmpprecomp);
391
392         /* Compute fixed-size store based on alg selection */
393         switch (authdata->algtype) {
394         case OP_ALG_ALGSEL_MD5:
395                 storelen = 16;
396                 break;
397         case OP_ALG_ALGSEL_SHA1:
398                 storelen = 20;
399                 break;
400         case OP_ALG_ALGSEL_SHA224:
401                 storelen = 28;
402                 break;
403         case OP_ALG_ALGSEL_SHA256:
404                 storelen = 32;
405                 break;
406         case OP_ALG_ALGSEL_SHA384:
407                 storelen = 48;
408                 break;
409         case OP_ALG_ALGSEL_SHA512:
410                 storelen = 64;
411                 break;
412         default:
413                 return -EINVAL;
414         }
415
416         trunc_len = trunc_len && (trunc_len < storelen) ? trunc_len : storelen;
417
418         opicv = do_icv ? ICV_CHECK_ENABLE : ICV_CHECK_DISABLE;
419         dir = do_icv ? DIR_DEC : DIR_ENC;
420
421         PROGRAM_CNTXT_INIT(p, descbuf, 0);
422         if (swap)
423                 PROGRAM_SET_BSWAP(p);
424         if (ps)
425                 PROGRAM_SET_36BIT_ADDR(p);
426         SHR_HDR(p, share, 1, SC);
427
428         pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SHRD);
429         KEY(p, KEY2, authdata->key_enc_flags, authdata->key, authdata->keylen,
430             INLINE_KEY(authdata));
431
432         /* Do operation */
433         ALG_OPERATION(p, authdata->algtype, OP_ALG_AAI_HMAC,
434                       OP_ALG_AS_INITFINAL, opicv, dir);
435
436         pjmpprecomp = JUMP(p, jmpprecomp, LOCAL_JUMP, ALL_TRUE, 0);
437         SET_LABEL(p, keyjmp);
438
439         ALG_OPERATION(p, authdata->algtype, OP_ALG_AAI_HMAC_PRECOMP,
440                       OP_ALG_AS_INITFINAL, opicv, dir);
441
442         SET_LABEL(p, jmpprecomp);
443
444         /* compute sequences */
445         if (opicv == ICV_CHECK_ENABLE)
446                 MATHB(p, SEQINSZ, SUB, trunc_len, VSEQINSZ, 4, IMMED2);
447         else
448                 MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
449
450         /* Do load (variable length) */
451         SEQFIFOLOAD(p, MSG2, 0, VLF | LAST2);
452
453         if (opicv == ICV_CHECK_ENABLE)
454                 SEQFIFOLOAD(p, ICV2, trunc_len, LAST2);
455         else
456                 SEQSTORE(p, CONTEXT2, 0, trunc_len, 0);
457
458         PATCH_JUMP(p, pkeyjmp, keyjmp);
459         PATCH_JUMP(p, pjmpprecomp, jmpprecomp);
460
461         return PROGRAM_FINALIZE(p);
462 }
463
464 /**
465  * cnstr_shdsc_kasumi_f8 - KASUMI F8 (Confidentiality) as a shared descriptor
466  *                         (ETSI "Document 1: f8 and f9 specification")
467  * @descbuf: pointer to descriptor-under-construction buffer
468  * @ps: if 36/40bit addressing is desired, this parameter must be true
469  * @swap: must be true when core endianness doesn't match SEC endianness
470  * @cipherdata: pointer to block cipher transform definitions
471  * @dir: cipher direction (DIR_ENC/DIR_DEC)
472  * @count: count value (32 bits)
473  * @bearer: bearer ID (5 bits)
474  * @direction: direction (1 bit)
475  *
476  * Return: size of descriptor written in words or negative number on error
477  */
478 static inline int
479 cnstr_shdsc_kasumi_f8(uint32_t *descbuf, bool ps, bool swap,
480                       struct alginfo *cipherdata, uint8_t dir)
481 {
482         struct program prg;
483         struct program *p = &prg;
484
485         PROGRAM_CNTXT_INIT(p, descbuf, 0);
486         if (swap)
487                 PROGRAM_SET_BSWAP(p);
488         if (ps)
489                 PROGRAM_SET_36BIT_ADDR(p);
490         SHR_HDR(p, SHR_ALWAYS, 1, 0);
491
492         KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
493             cipherdata->keylen, INLINE_KEY(cipherdata));
494         SEQLOAD(p, CONTEXT1, 0, 8, 0);
495         MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
496         MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);
497         ALG_OPERATION(p, OP_ALG_ALGSEL_KASUMI, OP_ALG_AAI_F8,
498                       OP_ALG_AS_INITFINAL, 0, dir);
499         SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
500         SEQFIFOSTORE(p, MSG, 0, 0, VLF);
501
502         return PROGRAM_FINALIZE(p);
503 }
504
505 /**
506  * cnstr_shdsc_kasumi_f9 -  KASUMI F9 (Integrity) as a shared descriptor
507  *                          (ETSI "Document 1: f8 and f9 specification")
508  * @descbuf: pointer to descriptor-under-construction buffer
509  * @ps: if 36/40bit addressing is desired, this parameter must be true
510  * @swap: must be true when core endianness doesn't match SEC endianness
511  * @authdata: pointer to authentication transform definitions
512  * @chk_icv: check or generate ICV value
513  * @authlen: size of digest
514  *
515  * Return: size of descriptor written in words or negative number on error
516  */
517 static inline int
518 cnstr_shdsc_kasumi_f9(uint32_t *descbuf, bool ps, bool swap,
519                     struct alginfo *authdata, uint8_t chk_icv,
520                     uint32_t authlen)
521 {
522         struct program prg;
523         struct program *p = &prg;
524         int dir = chk_icv ? DIR_DEC : DIR_ENC;
525
526         PROGRAM_CNTXT_INIT(p, descbuf, 0);
527         if (swap)
528                 PROGRAM_SET_BSWAP(p);
529
530         if (ps)
531                 PROGRAM_SET_36BIT_ADDR(p);
532
533         SHR_HDR(p, SHR_ALWAYS, 1, 0);
534
535         KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
536             authdata->keylen, INLINE_KEY(authdata));
537
538         SEQLOAD(p, CONTEXT2, 0, 12, 0);
539
540         if (chk_icv == ICV_CHECK_ENABLE)
541                 MATHB(p, SEQINSZ, SUB, authlen, VSEQINSZ, 4, IMMED2);
542         else
543                 MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
544
545         ALG_OPERATION(p, OP_ALG_ALGSEL_KASUMI, OP_ALG_AAI_F9,
546                       OP_ALG_AS_INITFINAL, chk_icv, dir);
547
548         SEQFIFOLOAD(p, MSG2, 0, VLF | CLASS2 | LAST2);
549
550         if (chk_icv == ICV_CHECK_ENABLE)
551                 SEQFIFOLOAD(p, ICV2, authlen, LAST2);
552         else
553                 /* Save lower half of MAC out into a 32-bit sequence */
554                 SEQSTORE(p, CONTEXT2, 0, authlen, 0);
555
556         return PROGRAM_FINALIZE(p);
557 }
558
559 /**
560  * cnstr_shdsc_crc - CRC32 Accelerator (IEEE 802 CRC32 protocol mode)
561  * @descbuf: pointer to descriptor-under-construction buffer
562  * @swap: must be true when core endianness doesn't match SEC endianness
563  *
564  * Return: size of descriptor written in words or negative number on error
565  */
566 static inline int
567 cnstr_shdsc_crc(uint32_t *descbuf, bool swap)
568 {
569         struct program prg;
570         struct program *p = &prg;
571
572         PROGRAM_CNTXT_INIT(p, descbuf, 0);
573         if (swap)
574                 PROGRAM_SET_BSWAP(p);
575
576         SHR_HDR(p, SHR_ALWAYS, 1, 0);
577
578         MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
579         ALG_OPERATION(p, OP_ALG_ALGSEL_CRC,
580                       OP_ALG_AAI_802 | OP_ALG_AAI_DOC,
581                       OP_ALG_AS_FINALIZE, 0, DIR_ENC);
582         SEQFIFOLOAD(p, MSG2, 0, VLF | LAST2);
583         SEQSTORE(p, CONTEXT2, 0, 4, 0);
584
585         return PROGRAM_FINALIZE(p);
586 }
587
588 /**
589  * cnstr_shdsc_gcm_encap - AES-GCM encap as a shared descriptor
590  * @descbuf: pointer to descriptor-under-construction buffer
591  * @ps: if 36/40bit addressing is desired, this parameter must be true
592  * @swap: must be true when core endianness doesn't match SEC endianness
593  * @share: sharing type of shared descriptor
594  * @cipherdata: pointer to block cipher transform definitions
595  *              Valid algorithm values - OP_ALG_ALGSEL_AES ANDed with
596  *              OP_ALG_AAI_GCM.
597  * @ivlen: Initialization vector length
598  * @icvsize: integrity check value (ICV) size (truncated or full)
599  *
600  * Return: size of descriptor written in words or negative number on error
601  */
602 static inline int
603 cnstr_shdsc_gcm_encap(uint32_t *descbuf, bool ps, bool swap,
604                       enum rta_share_type share,
605                       struct alginfo *cipherdata,
606                       uint32_t ivlen, uint32_t icvsize)
607 {
608         struct program prg;
609         struct program *p = &prg;
610
611         LABEL(keyjmp);
612         LABEL(zeroassocjump2);
613         LABEL(zeroassocjump1);
614         LABEL(zeropayloadjump);
615         REFERENCE(pkeyjmp);
616         REFERENCE(pzeroassocjump2);
617         REFERENCE(pzeroassocjump1);
618         REFERENCE(pzeropayloadjump);
619
620         PROGRAM_CNTXT_INIT(p, descbuf, 0);
621
622         if (swap)
623                 PROGRAM_SET_BSWAP(p);
624         if (ps)
625                 PROGRAM_SET_36BIT_ADDR(p);
626
627         SHR_HDR(p, share, 1, SC);
628
629         pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SELF | SHRD);
630         /* Insert Key */
631         KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
632             cipherdata->keylen, INLINE_KEY(cipherdata));
633
634         SET_LABEL(p, keyjmp);
635
636         /* class 1 operation */
637         ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
638                       OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, DIR_ENC);
639
640         MATHB(p, DPOVRD, AND, 0x7fffffff, MATH3, 4, IMMED2);
641
642         /* if assoclen + cryptlen is ZERO, skip to ICV write */
643         MATHB(p, SEQINSZ, SUB, ivlen, VSEQOUTSZ, 4, IMMED2);
644         pzeroassocjump2 = JUMP(p, zeroassocjump2, LOCAL_JUMP, ALL_TRUE, MATH_Z);
645
646         SEQFIFOLOAD(p, IV1, ivlen, FLUSH1);
647
648         /* if assoclen is ZERO, skip reading the assoc data */
649         MATHB(p, ZERO, ADD, MATH3, VSEQINSZ, 4, 0);
650         pzeroassocjump1 = JUMP(p, zeroassocjump1, LOCAL_JUMP, ALL_TRUE, MATH_Z);
651
652         /* cryptlen = seqinlen - assoclen */
653         MATHB(p, SEQINSZ, SUB, MATH3, VSEQOUTSZ, 4, 0);
654
655         /* if cryptlen is ZERO jump to zero-payload commands */
656         pzeropayloadjump = JUMP(p, zeropayloadjump, LOCAL_JUMP, ALL_TRUE,
657                                 MATH_Z);
658
659         /* read assoc data */
660         SEQFIFOLOAD(p, AAD1, 0, CLASS1 | VLF | FLUSH1);
661         SET_LABEL(p, zeroassocjump1);
662
663         MATHB(p, SEQINSZ, SUB, MATH0, VSEQINSZ, 4, 0);
664
665         /* write encrypted data */
666         SEQFIFOSTORE(p, MSG, 0, 0, VLF);
667
668         /* read payload data */
669         SEQFIFOLOAD(p, MSG1, 0, CLASS1 | VLF | LAST1);
670
671         /* jump the zero-payload commands */
672         JUMP(p, 4, LOCAL_JUMP, ALL_TRUE, 0);
673
674         /* zero-payload commands */
675         SET_LABEL(p, zeropayloadjump);
676
677         /* read assoc data */
678         SEQFIFOLOAD(p, AAD1, 0, CLASS1 | VLF | LAST1);
679
680         JUMP(p, 2, LOCAL_JUMP, ALL_TRUE, 0);
681
682         /* There is no input data */
683         SET_LABEL(p, zeroassocjump2);
684
685         SEQFIFOLOAD(p, IV1, ivlen, FLUSH1 | LAST1);
686
687         /* write ICV */
688         SEQSTORE(p, CONTEXT1, 0, icvsize, 0);
689
690         PATCH_JUMP(p, pkeyjmp, keyjmp);
691         PATCH_JUMP(p, pzeroassocjump2, zeroassocjump2);
692         PATCH_JUMP(p, pzeroassocjump1, zeroassocjump1);
693         PATCH_JUMP(p, pzeropayloadjump, zeropayloadjump);
694
695         return PROGRAM_FINALIZE(p);
696 }
697
698 /**
699  * cnstr_shdsc_gcm_decap - AES-GCM decap as a shared descriptor
700  * @descbuf: pointer to descriptor-under-construction buffer
701  * @ps: if 36/40bit addressing is desired, this parameter must be true
702  * @swap: must be true when core endianness doesn't match SEC endianness
703  * @share: sharing type of shared descriptor
704  * @cipherdata: pointer to block cipher transform definitions
705  *              Valid algorithm values - OP_ALG_ALGSEL_AES ANDed with
706  *              OP_ALG_AAI_GCM.
707  * @icvsize: integrity check value (ICV) size (truncated or full)
708  *
709  * Return: size of descriptor written in words or negative number on error
710  */
711 static inline int
712 cnstr_shdsc_gcm_decap(uint32_t *descbuf, bool ps, bool swap,
713                       enum rta_share_type share,
714                       struct alginfo *cipherdata,
715                       uint32_t ivlen, uint32_t icvsize)
716 {
717         struct program prg;
718         struct program *p = &prg;
719
720         LABEL(keyjmp);
721         LABEL(zeroassocjump1);
722         LABEL(zeropayloadjump);
723         REFERENCE(pkeyjmp);
724         REFERENCE(pzeroassocjump1);
725         REFERENCE(pzeropayloadjump);
726
727         PROGRAM_CNTXT_INIT(p, descbuf, 0);
728
729         if (swap)
730                 PROGRAM_SET_BSWAP(p);
731         if (ps)
732                 PROGRAM_SET_36BIT_ADDR(p);
733
734         SHR_HDR(p, share, 1, SC);
735
736         pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SELF | SHRD);
737         /* Insert Key */
738         KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
739             cipherdata->keylen, INLINE_KEY(cipherdata));
740
741         SET_LABEL(p, keyjmp);
742
743         /* class 1 operation */
744         ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
745                       OP_ALG_AS_INITFINAL, ICV_CHECK_ENABLE, DIR_DEC);
746
747         MATHB(p, DPOVRD, AND, 0x7fffffff, MATH3, 4, IMMED2);
748         SEQFIFOLOAD(p, IV1, ivlen, FLUSH1);
749
750         /* if assoclen is ZERO, skip reading the assoc data */
751         MATHB(p, ZERO, ADD, MATH3, VSEQINSZ, 4, 0);
752         pzeroassocjump1 = JUMP(p, zeroassocjump1, LOCAL_JUMP, ALL_TRUE, MATH_Z);
753
754         /* read assoc data */
755         SEQFIFOLOAD(p, AAD1, 0, CLASS1 | VLF | FLUSH1);
756
757         SET_LABEL(p, zeroassocjump1);
758
759         /* cryptlen = seqoutlen - assoclen */
760         MATHB(p, SEQOUTSZ, SUB, MATH0, VSEQINSZ, 4, 0);
761
762         /* jump to zero-payload command if cryptlen is zero */
763         pzeropayloadjump = JUMP(p, zeropayloadjump, LOCAL_JUMP, ALL_TRUE,
764                                 MATH_Z);
765
766         MATHB(p, SEQOUTSZ, SUB, MATH0, VSEQOUTSZ, 4, 0);
767
768         /* store encrypted data */
769         SEQFIFOSTORE(p, MSG, 0, 0, VLF);
770
771         /* read payload data */
772         SEQFIFOLOAD(p, MSG1, 0, CLASS1 | VLF | FLUSH1);
773
774         /* zero-payload command */
775         SET_LABEL(p, zeropayloadjump);
776
777         /* read ICV */
778         SEQFIFOLOAD(p, ICV1, icvsize, CLASS1 | LAST1);
779
780         PATCH_JUMP(p, pkeyjmp, keyjmp);
781         PATCH_JUMP(p, pzeroassocjump1, zeroassocjump1);
782         PATCH_JUMP(p, pzeropayloadjump, zeropayloadjump);
783
784         return PROGRAM_FINALIZE(p);
785 }
786
787 #endif /* __DESC_ALGO_H__ */