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 __RTA_OPERATION_CMD_H__
42 #define __RTA_OPERATION_CMD_H__
44 extern enum rta_sec_era rta_sec_era;
47 __rta_alg_aai_aes(uint16_t aai)
49 uint16_t aes_mode = aai & OP_ALG_AESA_MODE_MASK;
51 if (aai & OP_ALG_AAI_C2K) {
52 if (rta_sec_era < RTA_SEC_ERA_5)
54 if ((aes_mode != OP_ALG_AAI_CCM) &&
55 (aes_mode != OP_ALG_AAI_GCM))
60 case OP_ALG_AAI_CBC_CMAC:
61 case OP_ALG_AAI_CTR_CMAC_LTE:
62 case OP_ALG_AAI_CTR_CMAC:
63 if (rta_sec_era < RTA_SEC_ERA_2)
73 case OP_ALG_AAI_XCBC_MAC:
76 case OP_ALG_AAI_CBC_XCBCMAC:
77 case OP_ALG_AAI_CTR_XCBCMAC:
85 __rta_alg_aai_des(uint16_t aai)
87 uint16_t aai_code = (uint16_t)(aai & ~OP_ALG_AAI_CHECKODD);
101 __rta_alg_aai_md5(uint16_t aai)
104 case OP_ALG_AAI_HMAC:
105 if (rta_sec_era < RTA_SEC_ERA_2)
108 case OP_ALG_AAI_SMAC:
109 case OP_ALG_AAI_HASH:
110 case OP_ALG_AAI_HMAC_PRECOMP:
118 __rta_alg_aai_sha(uint16_t aai)
121 case OP_ALG_AAI_HMAC:
122 if (rta_sec_era < RTA_SEC_ERA_2)
125 case OP_ALG_AAI_HASH:
126 case OP_ALG_AAI_HMAC_PRECOMP:
134 __rta_alg_aai_rng(uint16_t aai)
136 uint16_t rng_mode = aai & OP_ALG_RNG_MODE_MASK;
137 uint16_t rng_sh = aai & OP_ALG_AAI_RNG4_SH_MASK;
141 case OP_ALG_AAI_RNG_NZB:
142 case OP_ALG_AAI_RNG_OBP:
148 /* State Handle bits are valid only for SEC Era >= 5 */
149 if ((rta_sec_era < RTA_SEC_ERA_5) && rng_sh)
152 /* PS, AI, SK bits are also valid only for SEC Era >= 5 */
153 if ((rta_sec_era < RTA_SEC_ERA_5) && (aai &
154 (OP_ALG_AAI_RNG4_PS | OP_ALG_AAI_RNG4_AI | OP_ALG_AAI_RNG4_SK)))
158 case OP_ALG_AAI_RNG4_SH_0:
159 case OP_ALG_AAI_RNG4_SH_1:
167 __rta_alg_aai_crc(uint16_t aai)
169 uint16_t aai_code = aai & OP_ALG_CRC_POLY_MASK;
173 case OP_ALG_AAI_3385:
174 case OP_ALG_AAI_CUST_POLY:
182 __rta_alg_aai_kasumi(uint16_t aai)
186 case OP_ALG_AAI_EDGE:
196 __rta_alg_aai_snow_f9(uint16_t aai)
198 if (aai == OP_ALG_AAI_F9)
205 __rta_alg_aai_snow_f8(uint16_t aai)
207 if (aai == OP_ALG_AAI_F8)
214 __rta_alg_aai_zuce(uint16_t aai)
216 if (aai == OP_ALG_AAI_F8)
223 __rta_alg_aai_zuca(uint16_t aai)
225 if (aai == OP_ALG_AAI_F9)
232 uint32_t chipher_algo;
233 int (*aai_func)(uint16_t);
237 static const struct alg_aai_map alg_table[] = {
238 /*1*/ { OP_ALG_ALGSEL_AES, __rta_alg_aai_aes, OP_TYPE_CLASS1_ALG },
239 { OP_ALG_ALGSEL_DES, __rta_alg_aai_des, OP_TYPE_CLASS1_ALG },
240 { OP_ALG_ALGSEL_3DES, __rta_alg_aai_des, OP_TYPE_CLASS1_ALG },
241 { OP_ALG_ALGSEL_MD5, __rta_alg_aai_md5, OP_TYPE_CLASS2_ALG },
242 { OP_ALG_ALGSEL_SHA1, __rta_alg_aai_md5, OP_TYPE_CLASS2_ALG },
243 { OP_ALG_ALGSEL_SHA224, __rta_alg_aai_sha, OP_TYPE_CLASS2_ALG },
244 { OP_ALG_ALGSEL_SHA256, __rta_alg_aai_sha, OP_TYPE_CLASS2_ALG },
245 { OP_ALG_ALGSEL_SHA384, __rta_alg_aai_sha, OP_TYPE_CLASS2_ALG },
246 { OP_ALG_ALGSEL_SHA512, __rta_alg_aai_sha, OP_TYPE_CLASS2_ALG },
247 { OP_ALG_ALGSEL_RNG, __rta_alg_aai_rng, OP_TYPE_CLASS1_ALG },
248 /*11*/ { OP_ALG_ALGSEL_CRC, __rta_alg_aai_crc, OP_TYPE_CLASS2_ALG },
249 { OP_ALG_ALGSEL_ARC4, NULL, OP_TYPE_CLASS1_ALG },
250 { OP_ALG_ALGSEL_SNOW_F8, __rta_alg_aai_snow_f8, OP_TYPE_CLASS1_ALG },
251 /*14*/ { OP_ALG_ALGSEL_KASUMI, __rta_alg_aai_kasumi, OP_TYPE_CLASS1_ALG },
252 { OP_ALG_ALGSEL_SNOW_F9, __rta_alg_aai_snow_f9, OP_TYPE_CLASS2_ALG },
253 { OP_ALG_ALGSEL_ZUCE, __rta_alg_aai_zuce, OP_TYPE_CLASS1_ALG },
254 /*17*/ { OP_ALG_ALGSEL_ZUCA, __rta_alg_aai_zuca, OP_TYPE_CLASS2_ALG }
258 * Allowed OPERATION algorithms for each SEC Era.
259 * Values represent the number of entries from alg_table[] that are supported.
261 static const unsigned int alg_table_sz[] = {14, 15, 15, 15, 17, 17, 11, 17};
264 rta_operation(struct program *program, uint32_t cipher_algo,
265 uint16_t aai, uint8_t algo_state,
266 int icv_checking, int enc)
268 uint32_t opcode = CMD_OPERATION;
269 unsigned int i, found = 0;
270 unsigned int start_pc = program->current_pc;
273 for (i = 0; i < alg_table_sz[rta_sec_era]; i++) {
274 if (alg_table[i].chipher_algo == cipher_algo) {
275 opcode |= cipher_algo | alg_table[i].class;
276 /* nothing else to verify */
277 if (alg_table[i].aai_func == NULL) {
282 aai &= OP_ALG_AAI_MASK;
284 ret = (*alg_table[i].aai_func)(aai);
286 pr_err("OPERATION: Bad AAI Type. SEC Program Line: %d\n",
287 program->current_pc);
296 pr_err("OPERATION: Invalid Command. SEC Program Line: %d\n",
297 program->current_pc);
302 switch (algo_state) {
303 case OP_ALG_AS_UPDATE:
305 case OP_ALG_AS_FINALIZE:
306 case OP_ALG_AS_INITFINAL:
307 opcode |= algo_state;
310 pr_err("Invalid Operation Command\n");
315 switch (icv_checking) {
316 case ICV_CHECK_DISABLE:
318 * opcode |= OP_ALG_ICV_OFF;
319 * OP_ALG_ICV_OFF is 0
322 case ICV_CHECK_ENABLE:
323 opcode |= OP_ALG_ICV_ON;
326 pr_err("Invalid Operation Command\n");
334 * opcode |= OP_ALG_DECRYPT;
335 * OP_ALG_DECRYPT is 0
339 opcode |= OP_ALG_ENCRYPT;
342 pr_err("Invalid Operation Command\n");
347 __rta_out32(program, opcode);
348 program->current_instruction++;
349 return (int)start_pc;
352 program->first_error_pc = start_pc;
357 * OPERATION PKHA routines
360 __rta_pkha_clearmem(uint32_t pkha_op)
363 case (OP_ALG_PKMODE_CLEARMEM_ALL):
364 case (OP_ALG_PKMODE_CLEARMEM_ABE):
365 case (OP_ALG_PKMODE_CLEARMEM_ABN):
366 case (OP_ALG_PKMODE_CLEARMEM_AB):
367 case (OP_ALG_PKMODE_CLEARMEM_AEN):
368 case (OP_ALG_PKMODE_CLEARMEM_AE):
369 case (OP_ALG_PKMODE_CLEARMEM_AN):
370 case (OP_ALG_PKMODE_CLEARMEM_A):
371 case (OP_ALG_PKMODE_CLEARMEM_BEN):
372 case (OP_ALG_PKMODE_CLEARMEM_BE):
373 case (OP_ALG_PKMODE_CLEARMEM_BN):
374 case (OP_ALG_PKMODE_CLEARMEM_B):
375 case (OP_ALG_PKMODE_CLEARMEM_EN):
376 case (OP_ALG_PKMODE_CLEARMEM_N):
377 case (OP_ALG_PKMODE_CLEARMEM_E):
385 __rta_pkha_mod_arithmetic(uint32_t pkha_op)
387 pkha_op &= (uint32_t)~OP_ALG_PKMODE_OUT_A;
390 case (OP_ALG_PKMODE_MOD_ADD):
391 case (OP_ALG_PKMODE_MOD_SUB_AB):
392 case (OP_ALG_PKMODE_MOD_SUB_BA):
393 case (OP_ALG_PKMODE_MOD_MULT):
394 case (OP_ALG_PKMODE_MOD_MULT_IM):
395 case (OP_ALG_PKMODE_MOD_MULT_IM_OM):
396 case (OP_ALG_PKMODE_MOD_EXPO):
397 case (OP_ALG_PKMODE_MOD_EXPO_TEQ):
398 case (OP_ALG_PKMODE_MOD_EXPO_IM):
399 case (OP_ALG_PKMODE_MOD_EXPO_IM_TEQ):
400 case (OP_ALG_PKMODE_MOD_REDUCT):
401 case (OP_ALG_PKMODE_MOD_INV):
402 case (OP_ALG_PKMODE_MOD_MONT_CNST):
403 case (OP_ALG_PKMODE_MOD_CRT_CNST):
404 case (OP_ALG_PKMODE_MOD_GCD):
405 case (OP_ALG_PKMODE_MOD_PRIMALITY):
406 case (OP_ALG_PKMODE_MOD_SML_EXP):
407 case (OP_ALG_PKMODE_F2M_ADD):
408 case (OP_ALG_PKMODE_F2M_MUL):
409 case (OP_ALG_PKMODE_F2M_MUL_IM):
410 case (OP_ALG_PKMODE_F2M_MUL_IM_OM):
411 case (OP_ALG_PKMODE_F2M_EXP):
412 case (OP_ALG_PKMODE_F2M_EXP_TEQ):
413 case (OP_ALG_PKMODE_F2M_AMODN):
414 case (OP_ALG_PKMODE_F2M_INV):
415 case (OP_ALG_PKMODE_F2M_R2):
416 case (OP_ALG_PKMODE_F2M_GCD):
417 case (OP_ALG_PKMODE_F2M_SML_EXP):
418 case (OP_ALG_PKMODE_ECC_F2M_ADD):
419 case (OP_ALG_PKMODE_ECC_F2M_ADD_IM_OM_PROJ):
420 case (OP_ALG_PKMODE_ECC_F2M_DBL):
421 case (OP_ALG_PKMODE_ECC_F2M_DBL_IM_OM_PROJ):
422 case (OP_ALG_PKMODE_ECC_F2M_MUL):
423 case (OP_ALG_PKMODE_ECC_F2M_MUL_TEQ):
424 case (OP_ALG_PKMODE_ECC_F2M_MUL_R2):
425 case (OP_ALG_PKMODE_ECC_F2M_MUL_R2_TEQ):
426 case (OP_ALG_PKMODE_ECC_F2M_MUL_R2_PROJ):
427 case (OP_ALG_PKMODE_ECC_F2M_MUL_R2_PROJ_TEQ):
428 case (OP_ALG_PKMODE_ECC_MOD_ADD):
429 case (OP_ALG_PKMODE_ECC_MOD_ADD_IM_OM_PROJ):
430 case (OP_ALG_PKMODE_ECC_MOD_DBL):
431 case (OP_ALG_PKMODE_ECC_MOD_DBL_IM_OM_PROJ):
432 case (OP_ALG_PKMODE_ECC_MOD_MUL):
433 case (OP_ALG_PKMODE_ECC_MOD_MUL_TEQ):
434 case (OP_ALG_PKMODE_ECC_MOD_MUL_R2):
435 case (OP_ALG_PKMODE_ECC_MOD_MUL_R2_TEQ):
436 case (OP_ALG_PKMODE_ECC_MOD_MUL_R2_PROJ):
437 case (OP_ALG_PKMODE_ECC_MOD_MUL_R2_PROJ_TEQ):
445 __rta_pkha_copymem(uint32_t pkha_op)
448 case (OP_ALG_PKMODE_COPY_NSZ_A0_B0):
449 case (OP_ALG_PKMODE_COPY_NSZ_A0_B1):
450 case (OP_ALG_PKMODE_COPY_NSZ_A0_B2):
451 case (OP_ALG_PKMODE_COPY_NSZ_A0_B3):
452 case (OP_ALG_PKMODE_COPY_NSZ_A1_B0):
453 case (OP_ALG_PKMODE_COPY_NSZ_A1_B1):
454 case (OP_ALG_PKMODE_COPY_NSZ_A1_B2):
455 case (OP_ALG_PKMODE_COPY_NSZ_A1_B3):
456 case (OP_ALG_PKMODE_COPY_NSZ_A2_B0):
457 case (OP_ALG_PKMODE_COPY_NSZ_A2_B1):
458 case (OP_ALG_PKMODE_COPY_NSZ_A2_B2):
459 case (OP_ALG_PKMODE_COPY_NSZ_A2_B3):
460 case (OP_ALG_PKMODE_COPY_NSZ_A3_B0):
461 case (OP_ALG_PKMODE_COPY_NSZ_A3_B1):
462 case (OP_ALG_PKMODE_COPY_NSZ_A3_B2):
463 case (OP_ALG_PKMODE_COPY_NSZ_A3_B3):
464 case (OP_ALG_PKMODE_COPY_NSZ_B0_A0):
465 case (OP_ALG_PKMODE_COPY_NSZ_B0_A1):
466 case (OP_ALG_PKMODE_COPY_NSZ_B0_A2):
467 case (OP_ALG_PKMODE_COPY_NSZ_B0_A3):
468 case (OP_ALG_PKMODE_COPY_NSZ_B1_A0):
469 case (OP_ALG_PKMODE_COPY_NSZ_B1_A1):
470 case (OP_ALG_PKMODE_COPY_NSZ_B1_A2):
471 case (OP_ALG_PKMODE_COPY_NSZ_B1_A3):
472 case (OP_ALG_PKMODE_COPY_NSZ_B2_A0):
473 case (OP_ALG_PKMODE_COPY_NSZ_B2_A1):
474 case (OP_ALG_PKMODE_COPY_NSZ_B2_A2):
475 case (OP_ALG_PKMODE_COPY_NSZ_B2_A3):
476 case (OP_ALG_PKMODE_COPY_NSZ_B3_A0):
477 case (OP_ALG_PKMODE_COPY_NSZ_B3_A1):
478 case (OP_ALG_PKMODE_COPY_NSZ_B3_A2):
479 case (OP_ALG_PKMODE_COPY_NSZ_B3_A3):
480 case (OP_ALG_PKMODE_COPY_NSZ_A_E):
481 case (OP_ALG_PKMODE_COPY_NSZ_A_N):
482 case (OP_ALG_PKMODE_COPY_NSZ_B_E):
483 case (OP_ALG_PKMODE_COPY_NSZ_B_N):
484 case (OP_ALG_PKMODE_COPY_NSZ_N_A):
485 case (OP_ALG_PKMODE_COPY_NSZ_N_B):
486 case (OP_ALG_PKMODE_COPY_NSZ_N_E):
487 case (OP_ALG_PKMODE_COPY_SSZ_A0_B0):
488 case (OP_ALG_PKMODE_COPY_SSZ_A0_B1):
489 case (OP_ALG_PKMODE_COPY_SSZ_A0_B2):
490 case (OP_ALG_PKMODE_COPY_SSZ_A0_B3):
491 case (OP_ALG_PKMODE_COPY_SSZ_A1_B0):
492 case (OP_ALG_PKMODE_COPY_SSZ_A1_B1):
493 case (OP_ALG_PKMODE_COPY_SSZ_A1_B2):
494 case (OP_ALG_PKMODE_COPY_SSZ_A1_B3):
495 case (OP_ALG_PKMODE_COPY_SSZ_A2_B0):
496 case (OP_ALG_PKMODE_COPY_SSZ_A2_B1):
497 case (OP_ALG_PKMODE_COPY_SSZ_A2_B2):
498 case (OP_ALG_PKMODE_COPY_SSZ_A2_B3):
499 case (OP_ALG_PKMODE_COPY_SSZ_A3_B0):
500 case (OP_ALG_PKMODE_COPY_SSZ_A3_B1):
501 case (OP_ALG_PKMODE_COPY_SSZ_A3_B2):
502 case (OP_ALG_PKMODE_COPY_SSZ_A3_B3):
503 case (OP_ALG_PKMODE_COPY_SSZ_B0_A0):
504 case (OP_ALG_PKMODE_COPY_SSZ_B0_A1):
505 case (OP_ALG_PKMODE_COPY_SSZ_B0_A2):
506 case (OP_ALG_PKMODE_COPY_SSZ_B0_A3):
507 case (OP_ALG_PKMODE_COPY_SSZ_B1_A0):
508 case (OP_ALG_PKMODE_COPY_SSZ_B1_A1):
509 case (OP_ALG_PKMODE_COPY_SSZ_B1_A2):
510 case (OP_ALG_PKMODE_COPY_SSZ_B1_A3):
511 case (OP_ALG_PKMODE_COPY_SSZ_B2_A0):
512 case (OP_ALG_PKMODE_COPY_SSZ_B2_A1):
513 case (OP_ALG_PKMODE_COPY_SSZ_B2_A2):
514 case (OP_ALG_PKMODE_COPY_SSZ_B2_A3):
515 case (OP_ALG_PKMODE_COPY_SSZ_B3_A0):
516 case (OP_ALG_PKMODE_COPY_SSZ_B3_A1):
517 case (OP_ALG_PKMODE_COPY_SSZ_B3_A2):
518 case (OP_ALG_PKMODE_COPY_SSZ_B3_A3):
519 case (OP_ALG_PKMODE_COPY_SSZ_A_E):
520 case (OP_ALG_PKMODE_COPY_SSZ_A_N):
521 case (OP_ALG_PKMODE_COPY_SSZ_B_E):
522 case (OP_ALG_PKMODE_COPY_SSZ_B_N):
523 case (OP_ALG_PKMODE_COPY_SSZ_N_A):
524 case (OP_ALG_PKMODE_COPY_SSZ_N_B):
525 case (OP_ALG_PKMODE_COPY_SSZ_N_E):
533 rta_pkha_operation(struct program *program, uint32_t op_pkha)
535 uint32_t opcode = CMD_OPERATION | OP_TYPE_PK | OP_ALG_PK;
537 unsigned int start_pc = program->current_pc;
540 pkha_func = op_pkha & OP_ALG_PK_FUN_MASK;
543 case (OP_ALG_PKMODE_CLEARMEM):
544 ret = __rta_pkha_clearmem(op_pkha);
546 pr_err("OPERATION PKHA: Type not supported. SEC Program Line: %d\n",
547 program->current_pc);
551 case (OP_ALG_PKMODE_MOD_ADD):
552 case (OP_ALG_PKMODE_MOD_SUB_AB):
553 case (OP_ALG_PKMODE_MOD_SUB_BA):
554 case (OP_ALG_PKMODE_MOD_MULT):
555 case (OP_ALG_PKMODE_MOD_EXPO):
556 case (OP_ALG_PKMODE_MOD_REDUCT):
557 case (OP_ALG_PKMODE_MOD_INV):
558 case (OP_ALG_PKMODE_MOD_MONT_CNST):
559 case (OP_ALG_PKMODE_MOD_CRT_CNST):
560 case (OP_ALG_PKMODE_MOD_GCD):
561 case (OP_ALG_PKMODE_MOD_PRIMALITY):
562 case (OP_ALG_PKMODE_MOD_SML_EXP):
563 case (OP_ALG_PKMODE_ECC_MOD_ADD):
564 case (OP_ALG_PKMODE_ECC_MOD_DBL):
565 case (OP_ALG_PKMODE_ECC_MOD_MUL):
566 ret = __rta_pkha_mod_arithmetic(op_pkha);
568 pr_err("OPERATION PKHA: Type not supported. SEC Program Line: %d\n",
569 program->current_pc);
573 case (OP_ALG_PKMODE_COPY_NSZ):
574 case (OP_ALG_PKMODE_COPY_SSZ):
575 ret = __rta_pkha_copymem(op_pkha);
577 pr_err("OPERATION PKHA: Type not supported. SEC Program Line: %d\n",
578 program->current_pc);
583 pr_err("Invalid Operation Command\n");
589 __rta_out32(program, opcode);
590 program->current_instruction++;
591 return (int)start_pc;
594 program->first_error_pc = start_pc;
595 program->current_instruction++;
599 #endif /* __RTA_OPERATION_CMD_H__ */