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.
7 * Copyright 2008-2016 Freescale Semiconductor Inc.
8 * Copyright (c) 2016 NXP.
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.
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
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.
41 #ifndef __DESC_ALGO_H__
42 #define __DESC_ALGO_H__
48 * DOC: Algorithms - Shared Descriptor Constructors
50 * Shared descriptors for algorithms (i.e. not for protocols).
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)
64 * Return: size of descriptor written in words or negative number on error
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)
72 struct program *p = &prg;
75 uint8_t dr = direction;
76 uint32_t context[2] = {ct, (br << 27) | (dr << 26)};
78 PROGRAM_CNTXT_INIT(p, descbuf, 0);
82 context[0] = swab32(context[0]);
83 context[1] = swab32(context[1]);
87 PROGRAM_SET_36BIT_ADDR(p);
88 SHR_HDR(p, SHR_ALWAYS, 1, 0);
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);
100 return PROGRAM_FINALIZE(p);
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
115 * Return: size of descriptor written in words or negative number on error
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)
123 struct program *p = &prg;
126 uint64_t dr = direction;
129 context[0] = (ct << 32) | (dr << 26);
130 context[1] = fr << 32;
132 PROGRAM_CNTXT_INIT(p, descbuf, 0);
134 PROGRAM_SET_BSWAP(p);
136 context[0] = swab64(context[0]);
137 context[1] = swab64(context[1]);
140 PROGRAM_SET_36BIT_ADDR(p);
141 SHR_HDR(p, SHR_ALWAYS, 1, 0);
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);
153 return PROGRAM_FINALIZE(p);
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
164 * @dir: DIR_ENC/DIR_DEC
166 * Return: size of descriptor written in words or negative number on error
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)
174 struct program *p = &prg;
175 const bool is_aes_dec = (dir == DIR_DEC) &&
176 (cipherdata->algtype == OP_ALG_ALGSEL_AES);
182 PROGRAM_CNTXT_INIT(p, descbuf, 0);
184 PROGRAM_SET_BSWAP(p);
186 PROGRAM_SET_36BIT_ADDR(p);
187 SHR_HDR(p, SHR_SERIAL, 1, SC);
189 pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SHRD);
191 KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
192 cipherdata->keylen, INLINE_KEY(cipherdata));
195 ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
196 OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, dir);
198 pskipdk = JUMP(p, skipdk, LOCAL_JUMP, ALL_TRUE, 0);
200 SET_LABEL(p, keyjmp);
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);
208 ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
209 OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, dir);
213 /* IV load, convert size */
214 LOAD(p, (uintptr_t)iv, CONTEXT1, 0, ivlen, IMMED | COPY);
216 /* IV is present first before the actual message */
217 SEQLOAD(p, CONTEXT1, 0, ivlen, 0);
219 MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
220 MATHB(p, SEQINSZ, SUB, MATH2, VSEQOUTSZ, 4, 0);
222 /* Insert sequence load/store with VLF */
223 SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1);
224 SEQFIFOSTORE(p, MSG, 0, 0, VLF);
226 PATCH_JUMP(p, pkeyjmp, keyjmp);
228 PATCH_JUMP(p, pskipdk, skipdk);
230 return PROGRAM_FINALIZE(p);
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
245 * Note: There's no support for keys longer than the block size of the
246 * underlying hash function, according to the selected algorithm.
248 * Return: size of descriptor written in words or negative number on error
251 cnstr_shdsc_hmac(uint32_t *descbuf, bool ps, bool swap,
252 struct alginfo *authdata, uint8_t do_icv,
256 struct program *p = &prg;
257 uint8_t storelen, opicv, dir;
261 REFERENCE(pjmpprecomp);
263 /* Compute fixed-size store based on alg selection */
264 switch (authdata->algtype) {
265 case OP_ALG_ALGSEL_MD5:
268 case OP_ALG_ALGSEL_SHA1:
271 case OP_ALG_ALGSEL_SHA224:
274 case OP_ALG_ALGSEL_SHA256:
277 case OP_ALG_ALGSEL_SHA384:
280 case OP_ALG_ALGSEL_SHA512:
287 trunc_len = trunc_len && (trunc_len < storelen) ? trunc_len : storelen;
289 opicv = do_icv ? ICV_CHECK_ENABLE : ICV_CHECK_DISABLE;
290 dir = do_icv ? DIR_DEC : DIR_ENC;
292 PROGRAM_CNTXT_INIT(p, descbuf, 0);
294 PROGRAM_SET_BSWAP(p);
296 PROGRAM_SET_36BIT_ADDR(p);
297 SHR_HDR(p, SHR_SERIAL, 1, SC);
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));
304 ALG_OPERATION(p, authdata->algtype, OP_ALG_AAI_HMAC,
305 OP_ALG_AS_INITFINAL, opicv, dir);
307 pjmpprecomp = JUMP(p, jmpprecomp, LOCAL_JUMP, ALL_TRUE, 0);
308 SET_LABEL(p, keyjmp);
310 ALG_OPERATION(p, authdata->algtype, OP_ALG_AAI_HMAC_PRECOMP,
311 OP_ALG_AS_INITFINAL, opicv, dir);
313 SET_LABEL(p, jmpprecomp);
315 /* compute sequences */
316 if (opicv == ICV_CHECK_ENABLE)
317 MATHB(p, SEQINSZ, SUB, trunc_len, VSEQINSZ, 4, IMMED2);
319 MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
321 /* Do load (variable length) */
322 SEQFIFOLOAD(p, MSG2, 0, VLF | LAST2);
324 if (opicv == ICV_CHECK_ENABLE)
325 SEQFIFOLOAD(p, ICV2, trunc_len, LAST2);
327 SEQSTORE(p, CONTEXT2, 0, trunc_len, 0);
329 PATCH_JUMP(p, pkeyjmp, keyjmp);
330 PATCH_JUMP(p, pjmpprecomp, jmpprecomp);
332 return PROGRAM_FINALIZE(p);
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)
347 * Return: size of descriptor written in words or negative number on error
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)
355 struct program *p = &prg;
357 uint64_t br = bearer;
358 uint64_t dr = direction;
359 uint32_t context[2] = { ct, (br << 27) | (dr << 26) };
361 PROGRAM_CNTXT_INIT(p, descbuf, 0);
363 PROGRAM_SET_BSWAP(p);
365 context[0] = swab32(context[0]);
366 context[1] = swab32(context[1]);
369 PROGRAM_SET_36BIT_ADDR(p);
370 SHR_HDR(p, SHR_ALWAYS, 1, 0);
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);
382 return PROGRAM_FINALIZE(p);
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
398 * Return: size of descriptor written in words or negative number on error
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,
407 struct program *p = &prg;
408 uint16_t ctx_offset = 16;
409 uint32_t context[6] = {count, direction << 26, fresh, 0, 0, 0};
411 PROGRAM_CNTXT_INIT(p, descbuf, 0);
413 PROGRAM_SET_BSWAP(p);
415 context[0] = swab32(context[0]);
416 context[1] = swab32(context[1]);
417 context[2] = swab32(context[2]);
420 PROGRAM_SET_36BIT_ADDR(p);
421 SHR_HDR(p, SHR_ALWAYS, 1, 0);
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);
433 return PROGRAM_FINALIZE(p);
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
441 * Return: size of descriptor written in words or negative number on error
444 cnstr_shdsc_crc(uint32_t *descbuf, bool swap)
447 struct program *p = &prg;
449 PROGRAM_CNTXT_INIT(p, descbuf, 0);
451 PROGRAM_SET_BSWAP(p);
453 SHR_HDR(p, SHR_ALWAYS, 1, 0);
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);
462 return PROGRAM_FINALIZE(p);
465 #endif /* __DESC_ALGO_H__ */