c71ada07a5d05e20b6945df5a6f1a4dfc0816aba
[deb_dpdk.git] / drivers / crypto / dpaa2_sec / hw / desc / algo.h
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license. When using or
3  * redistributing this file, you may do so under either license.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2008-2016 Freescale Semiconductor Inc.
8  * Copyright (c) 2016 NXP.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the above-listed copyright holders nor the
18  * names of any contributors may be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  *   GPL LICENSE SUMMARY
22  *
23  * ALTERNATIVELY, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") as published by the Free Software
25  * Foundation, either version 2 of that License or (at your option) any
26  * later version.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 #ifndef __DESC_ALGO_H__
42 #define __DESC_ALGO_H__
43
44 #include "hw/rta.h"
45 #include "common.h"
46
47 /**
48  * DOC: Algorithms - Shared Descriptor Constructors
49  *
50  * Shared descriptors for algorithms (i.e. not for protocols).
51  */
52
53 /**
54  * cnstr_shdsc_snow_f8 - SNOW/f8 (UEA2) as a shared descriptor
55  * @descbuf: pointer to descriptor-under-construction buffer
56  * @ps: if 36/40bit addressing is desired, this parameter must be true
57  * @swap: must be true when core endianness doesn't match SEC endianness
58  * @cipherdata: pointer to block cipher transform definitions
59  * @dir: Cipher direction (DIR_ENC/DIR_DEC)
60  * @count: UEA2 count value (32 bits)
61  * @bearer: UEA2 bearer ID (5 bits)
62  * @direction: UEA2 direction (1 bit)
63  *
64  * Return: size of descriptor written in words or negative number on error
65  */
66 static inline int
67 cnstr_shdsc_snow_f8(uint32_t *descbuf, bool ps, bool swap,
68                     struct alginfo *cipherdata, uint8_t dir,
69                     uint32_t count, uint8_t bearer, uint8_t direction)
70 {
71         struct program prg;
72         struct program *p = &prg;
73         uint32_t ct = count;
74         uint8_t br = bearer;
75         uint8_t dr = direction;
76         uint32_t context[2] = {ct, (br << 27) | (dr << 26)};
77
78         PROGRAM_CNTXT_INIT(p, descbuf, 0);
79         if (swap) {
80                 PROGRAM_SET_BSWAP(p);
81
82                 context[0] = swab32(context[0]);
83                 context[1] = swab32(context[1]);
84         }
85
86         if (ps)
87                 PROGRAM_SET_36BIT_ADDR(p);
88         SHR_HDR(p, SHR_ALWAYS, 1, 0);
89
90         KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
91             cipherdata->keylen, INLINE_KEY(cipherdata));
92         MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
93         MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);
94         ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F8, OP_ALG_AAI_F8,
95                       OP_ALG_AS_INITFINAL, 0, dir);
96         LOAD(p, (uintptr_t)context, CONTEXT1, 0, 8, IMMED | COPY);
97         SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
98         SEQFIFOSTORE(p, MSG, 0, 0, VLF);
99
100         return PROGRAM_FINALIZE(p);
101 }
102
103 /**
104  * cnstr_shdsc_snow_f9 - SNOW/f9 (UIA2) as a shared descriptor
105  * @descbuf: pointer to descriptor-under-construction buffer
106  * @ps: if 36/40bit addressing is desired, this parameter must be true
107  * @swap: must be true when core endianness doesn't match SEC endianness
108  * @authdata: pointer to authentication transform definitions
109  * @dir: cipher direction (DIR_ENC/DIR_DEC)
110  * @count: UEA2 count value (32 bits)
111  * @fresh: UEA2 fresh value ID (32 bits)
112  * @direction: UEA2 direction (1 bit)
113  * @datalen: size of data
114  *
115  * Return: size of descriptor written in words or negative number on error
116  */
117 static inline int
118 cnstr_shdsc_snow_f9(uint32_t *descbuf, bool ps, bool swap,
119                     struct alginfo *authdata, uint8_t dir, uint32_t count,
120                     uint32_t fresh, uint8_t direction, uint32_t datalen)
121 {
122         struct program prg;
123         struct program *p = &prg;
124         uint64_t ct = count;
125         uint64_t fr = fresh;
126         uint64_t dr = direction;
127         uint64_t context[2];
128
129         context[0] = (ct << 32) | (dr << 26);
130         context[1] = fr << 32;
131
132         PROGRAM_CNTXT_INIT(p, descbuf, 0);
133         if (swap) {
134                 PROGRAM_SET_BSWAP(p);
135
136                 context[0] = swab64(context[0]);
137                 context[1] = swab64(context[1]);
138         }
139         if (ps)
140                 PROGRAM_SET_36BIT_ADDR(p);
141         SHR_HDR(p, SHR_ALWAYS, 1, 0);
142
143         KEY(p, KEY2, authdata->key_enc_flags, authdata->key, authdata->keylen,
144             INLINE_KEY(authdata));
145         MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
146         ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F9, OP_ALG_AAI_F9,
147                       OP_ALG_AS_INITFINAL, 0, dir);
148         LOAD(p, (uintptr_t)context, CONTEXT2, 0, 16, IMMED | COPY);
149         SEQFIFOLOAD(p, BIT_DATA, datalen, CLASS2 | LAST2);
150         /* Save lower half of MAC out into a 32-bit sequence */
151         SEQSTORE(p, CONTEXT2, 0, 4, 0);
152
153         return PROGRAM_FINALIZE(p);
154 }
155
156 /**
157  * cnstr_shdsc_blkcipher - block cipher transformation
158  * @descbuf: pointer to descriptor-under-construction buffer
159  * @ps: if 36/40bit addressing is desired, this parameter must be true
160  * @swap: must be true when core endianness doesn't match SEC endianness
161  * @cipherdata: pointer to block cipher transform definitions
162  * @iv: IV data; if NULL, "ivlen" bytes from the input frame will be read as IV
163  * @ivlen: IV length
164  * @dir: DIR_ENC/DIR_DEC
165  *
166  * Return: size of descriptor written in words or negative number on error
167  */
168 static inline int
169 cnstr_shdsc_blkcipher(uint32_t *descbuf, bool ps, bool swap,
170                       struct alginfo *cipherdata, uint8_t *iv,
171                       uint32_t ivlen, uint8_t dir)
172 {
173         struct program prg;
174         struct program *p = &prg;
175         const bool is_aes_dec = (dir == DIR_DEC) &&
176                                 (cipherdata->algtype == OP_ALG_ALGSEL_AES);
177         LABEL(keyjmp);
178         LABEL(skipdk);
179         REFERENCE(pkeyjmp);
180         REFERENCE(pskipdk);
181
182         PROGRAM_CNTXT_INIT(p, descbuf, 0);
183         if (swap)
184                 PROGRAM_SET_BSWAP(p);
185         if (ps)
186                 PROGRAM_SET_36BIT_ADDR(p);
187         SHR_HDR(p, SHR_SERIAL, 1, SC);
188
189         pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SHRD);
190         /* Insert Key */
191         KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
192             cipherdata->keylen, INLINE_KEY(cipherdata));
193
194         if (is_aes_dec) {
195                 ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
196                               OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, dir);
197
198                 pskipdk = JUMP(p, skipdk, LOCAL_JUMP, ALL_TRUE, 0);
199         }
200         SET_LABEL(p, keyjmp);
201
202         if (is_aes_dec) {
203                 ALG_OPERATION(p, OP_ALG_ALGSEL_AES, cipherdata->algmode |
204                               OP_ALG_AAI_DK, OP_ALG_AS_INITFINAL,
205                               ICV_CHECK_DISABLE, dir);
206                 SET_LABEL(p, skipdk);
207         } else {
208                 ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
209                               OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, dir);
210         }
211
212         if (iv)
213                 /* IV load, convert size */
214                 LOAD(p, (uintptr_t)iv, CONTEXT1, 0, ivlen, IMMED | COPY);
215         else
216                 /* IV is present first before the actual message */
217                 SEQLOAD(p, CONTEXT1, 0, ivlen, 0);
218
219         MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
220         MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);
221
222         /* Insert sequence load/store with VLF */
223         SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
224         SEQFIFOSTORE(p, MSG, 0, 0, VLF);
225
226         PATCH_JUMP(p, pkeyjmp, keyjmp);
227         if (is_aes_dec)
228                 PATCH_JUMP(p, pskipdk, skipdk);
229
230         return PROGRAM_FINALIZE(p);
231 }
232
233 /**
234  * cnstr_shdsc_hmac - HMAC shared
235  * @descbuf: pointer to descriptor-under-construction buffer
236  * @ps: if 36/40bit addressing is desired, this parameter must be true
237  * @swap: must be true when core endianness doesn't match SEC endianness
238  * @authdata: pointer to authentication transform definitions;
239  *            message digest algorithm: OP_ALG_ALGSEL_MD5/ SHA1-512.
240  * @do_icv: 0 if ICV checking is not desired, any other value if ICV checking
241  *          is needed for all the packets processed by this shared descriptor
242  * @trunc_len: Length of the truncated ICV to be written in the output buffer, 0
243  *             if no truncation is needed
244  *
245  * Note: There's no support for keys longer than the block size of the
246  * underlying hash function, according to the selected algorithm.
247  *
248  * Return: size of descriptor written in words or negative number on error
249  */
250 static inline int
251 cnstr_shdsc_hmac(uint32_t *descbuf, bool ps, bool swap,
252                  struct alginfo *authdata, uint8_t do_icv,
253                  uint8_t trunc_len)
254 {
255         struct program prg;
256         struct program *p = &prg;
257         uint8_t storelen, opicv, dir;
258         LABEL(keyjmp);
259         LABEL(jmpprecomp);
260         REFERENCE(pkeyjmp);
261         REFERENCE(pjmpprecomp);
262
263         /* Compute fixed-size store based on alg selection */
264         switch (authdata->algtype) {
265         case OP_ALG_ALGSEL_MD5:
266                 storelen = 16;
267                 break;
268         case OP_ALG_ALGSEL_SHA1:
269                 storelen = 20;
270                 break;
271         case OP_ALG_ALGSEL_SHA224:
272                 storelen = 28;
273                 break;
274         case OP_ALG_ALGSEL_SHA256:
275                 storelen = 32;
276                 break;
277         case OP_ALG_ALGSEL_SHA384:
278                 storelen = 48;
279                 break;
280         case OP_ALG_ALGSEL_SHA512:
281                 storelen = 64;
282                 break;
283         default:
284                 return -EINVAL;
285         }
286
287         trunc_len = trunc_len && (trunc_len < storelen) ? trunc_len : storelen;
288
289         opicv = do_icv ? ICV_CHECK_ENABLE : ICV_CHECK_DISABLE;
290         dir = do_icv ? DIR_DEC : DIR_ENC;
291
292         PROGRAM_CNTXT_INIT(p, descbuf, 0);
293         if (swap)
294                 PROGRAM_SET_BSWAP(p);
295         if (ps)
296                 PROGRAM_SET_36BIT_ADDR(p);
297         SHR_HDR(p, SHR_SERIAL, 1, SC);
298
299         pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SHRD);
300         KEY(p, KEY2, authdata->key_enc_flags, authdata->key, authdata->keylen,
301             INLINE_KEY(authdata));
302
303         /* Do operation */
304         ALG_OPERATION(p, authdata->algtype, OP_ALG_AAI_HMAC,
305                       OP_ALG_AS_INITFINAL, opicv, dir);
306
307         pjmpprecomp = JUMP(p, jmpprecomp, LOCAL_JUMP, ALL_TRUE, 0);
308         SET_LABEL(p, keyjmp);
309
310         ALG_OPERATION(p, authdata->algtype, OP_ALG_AAI_HMAC_PRECOMP,
311                       OP_ALG_AS_INITFINAL, opicv, dir);
312
313         SET_LABEL(p, jmpprecomp);
314
315         /* compute sequences */
316         if (opicv == ICV_CHECK_ENABLE)
317                 MATHB(p, SEQINSZ, SUB, trunc_len, VSEQINSZ, 4, IMMED2);
318         else
319                 MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
320
321         /* Do load (variable length) */
322         SEQFIFOLOAD(p, MSG2, 0, VLF | LAST2);
323
324         if (opicv == ICV_CHECK_ENABLE)
325                 SEQFIFOLOAD(p, ICV2, trunc_len, LAST2);
326         else
327                 SEQSTORE(p, CONTEXT2, 0, trunc_len, 0);
328
329         PATCH_JUMP(p, pkeyjmp, keyjmp);
330         PATCH_JUMP(p, pjmpprecomp, jmpprecomp);
331
332         return PROGRAM_FINALIZE(p);
333 }
334
335 /**
336  * cnstr_shdsc_kasumi_f8 - KASUMI F8 (Confidentiality) as a shared descriptor
337  *                         (ETSI "Document 1: f8 and f9 specification")
338  * @descbuf: pointer to descriptor-under-construction buffer
339  * @ps: if 36/40bit addressing is desired, this parameter must be true
340  * @swap: must be true when core endianness doesn't match SEC endianness
341  * @cipherdata: pointer to block cipher transform definitions
342  * @dir: cipher direction (DIR_ENC/DIR_DEC)
343  * @count: count value (32 bits)
344  * @bearer: bearer ID (5 bits)
345  * @direction: direction (1 bit)
346  *
347  * Return: size of descriptor written in words or negative number on error
348  */
349 static inline int
350 cnstr_shdsc_kasumi_f8(uint32_t *descbuf, bool ps, bool swap,
351                       struct alginfo *cipherdata, uint8_t dir,
352                       uint32_t count, uint8_t bearer, uint8_t direction)
353 {
354         struct program prg;
355         struct program *p = &prg;
356         uint64_t ct = count;
357         uint64_t br = bearer;
358         uint64_t dr = direction;
359         uint32_t context[2] = { ct, (br << 27) | (dr << 26) };
360
361         PROGRAM_CNTXT_INIT(p, descbuf, 0);
362         if (swap) {
363                 PROGRAM_SET_BSWAP(p);
364
365                 context[0] = swab32(context[0]);
366                 context[1] = swab32(context[1]);
367         }
368         if (ps)
369                 PROGRAM_SET_36BIT_ADDR(p);
370         SHR_HDR(p, SHR_ALWAYS, 1, 0);
371
372         KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
373             cipherdata->keylen, INLINE_KEY(cipherdata));
374         MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
375         MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);
376         ALG_OPERATION(p, OP_ALG_ALGSEL_KASUMI, OP_ALG_AAI_F8,
377                       OP_ALG_AS_INITFINAL, 0, dir);
378         LOAD(p, (uintptr_t)context, CONTEXT1, 0, 8, IMMED | COPY);
379         SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
380         SEQFIFOSTORE(p, MSG, 0, 0, VLF);
381
382         return PROGRAM_FINALIZE(p);
383 }
384
385 /**
386  * cnstr_shdsc_kasumi_f9 -  KASUMI F9 (Integrity) as a shared descriptor
387  *                          (ETSI "Document 1: f8 and f9 specification")
388  * @descbuf: pointer to descriptor-under-construction buffer
389  * @ps: if 36/40bit addressing is desired, this parameter must be true
390  * @swap: must be true when core endianness doesn't match SEC endianness
391  * @authdata: pointer to authentication transform definitions
392  * @dir: cipher direction (DIR_ENC/DIR_DEC)
393  * @count: count value (32 bits)
394  * @fresh: fresh value ID (32 bits)
395  * @direction: direction (1 bit)
396  * @datalen: size of data
397  *
398  * Return: size of descriptor written in words or negative number on error
399  */
400 static inline int
401 cnstr_shdsc_kasumi_f9(uint32_t *descbuf, bool ps, bool swap,
402                       struct alginfo *authdata, uint8_t dir,
403                       uint32_t count, uint32_t fresh, uint8_t direction,
404                       uint32_t datalen)
405 {
406         struct program prg;
407         struct program *p = &prg;
408         uint16_t ctx_offset = 16;
409         uint32_t context[6] = {count, direction << 26, fresh, 0, 0, 0};
410
411         PROGRAM_CNTXT_INIT(p, descbuf, 0);
412         if (swap) {
413                 PROGRAM_SET_BSWAP(p);
414
415                 context[0] = swab32(context[0]);
416                 context[1] = swab32(context[1]);
417                 context[2] = swab32(context[2]);
418         }
419         if (ps)
420                 PROGRAM_SET_36BIT_ADDR(p);
421         SHR_HDR(p, SHR_ALWAYS, 1, 0);
422
423         KEY(p, KEY1, authdata->key_enc_flags, authdata->key, authdata->keylen,
424             INLINE_KEY(authdata));
425         MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
426         ALG_OPERATION(p, OP_ALG_ALGSEL_KASUMI, OP_ALG_AAI_F9,
427                       OP_ALG_AS_INITFINAL, 0, dir);
428         LOAD(p, (uintptr_t)context, CONTEXT1, 0, 24, IMMED | COPY);
429         SEQFIFOLOAD(p, BIT_DATA, datalen, CLASS1 | LAST1);
430         /* Save output MAC of DWORD 2 into a 32-bit sequence */
431         SEQSTORE(p, CONTEXT1, ctx_offset, 4, 0);
432
433         return PROGRAM_FINALIZE(p);
434 }
435
436 /**
437  * cnstr_shdsc_crc - CRC32 Accelerator (IEEE 802 CRC32 protocol mode)
438  * @descbuf: pointer to descriptor-under-construction buffer
439  * @swap: must be true when core endianness doesn't match SEC endianness
440  *
441  * Return: size of descriptor written in words or negative number on error
442  */
443 static inline int
444 cnstr_shdsc_crc(uint32_t *descbuf, bool swap)
445 {
446         struct program prg;
447         struct program *p = &prg;
448
449         PROGRAM_CNTXT_INIT(p, descbuf, 0);
450         if (swap)
451                 PROGRAM_SET_BSWAP(p);
452
453         SHR_HDR(p, SHR_ALWAYS, 1, 0);
454
455         MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
456         ALG_OPERATION(p, OP_ALG_ALGSEL_CRC,
457                       OP_ALG_AAI_802 | OP_ALG_AAI_DOC,
458                       OP_ALG_AS_FINALIZE, 0, DIR_ENC);
459         SEQFIFOLOAD(p, MSG2, 0, VLF | LAST2);
460         SEQSTORE(p, CONTEXT2, 0, 4, 0);
461
462         return PROGRAM_FINALIZE(p);
463 }
464
465 #endif /* __DESC_ALGO_H__ */