1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * Copyright 2018-2021 NXP
4 *
5 * Brief CAAM Descriptor interface.
6 */
7 #ifndef __CAAM_DESC_HELPER_H__
8 #define __CAAM_DESC_HELPER_H__
9
10 #include <caam_desc_defines.h>
11 #include <caam_jr.h>
12 #include <caam_utils_dmaobj.h>
13 #include <trace.h>
14
15 /*
16 * Returns the number of entries of the descriptor
17 */
18 uint32_t caam_desc_get_len(uint32_t *desc);
19
20 /* Descriptor Modification function */
21 void caam_desc_init(uint32_t *desc);
22 void caam_desc_update_hdr(uint32_t *desc, uint32_t word);
23 void caam_desc_add_ptr(uint32_t *desc, paddr_t ptr);
24 void caam_desc_add_word(uint32_t *desc, uint32_t word);
25 void caam_desc_add_dmaobj(uint32_t *desc, struct caamdmaobj *data,
26 uint32_t pre_operation);
27
28 #define caam_desc_fifo_load(desc, data, cla, dst, act) \
29 caam_desc_add_dmaobj(desc, data, FIFO_LD(cla, dst, act, 0))
30 #define caam_desc_load_key(desc, data, cla, dst) \
31 caam_desc_add_dmaobj(desc, data, LD_KEY_PLAIN(cla, dst, 0))
32 #define caam_desc_store(desc, data, cla, src) \
33 caam_desc_add_dmaobj(desc, data, ST_NOIMM(cla, src, 0))
34 #define caam_desc_fifo_store(desc, data, src) \
35 caam_desc_add_dmaobj(desc, data, FIFO_ST(src, 0))
36 #define caam_desc_seq_out(desc, data) \
37 caam_desc_add_dmaobj(desc, data, SEQ_OUT_PTR(0))
38
39 /* Push/Pop descriptor rings queue */
40 void caam_desc_push(struct caam_inring_entry *in_entry, paddr_t paddr);
41 paddr_t caam_desc_pop(struct caam_outring_entry *out_entry);
42
43 uint32_t caam_read_jobstatus(struct caam_outring_entry *out);
44
45 /* Debug print function to dump a Descriptor in hex */
dump_desc(uint32_t * desc)46 static inline void dump_desc(uint32_t *desc)
47 {
48 size_t idx = 0;
49 size_t len = 0;
50
51 len = caam_desc_get_len(desc);
52
53 for (idx = 0; idx < len; idx++)
54 trace_printf(NULL, 0, 0, false, "[%02zu] %08" PRIX32, idx,
55 desc[idx]);
56 }
57
58 /*
59 * Returns the descriptor size in bytes of nbEntries
60 */
61 #define DESC_SZBYTES(nbentries) ((nbentries) * sizeof(uint32_t))
62
63 /*
64 * Descriptor Header starting at idx w/o descriptor length
65 */
66 #define DESC_HDR(idx) (CMD_HDR_JD_TYPE | HDR_JD_ONE | HDR_JD_START_IDX(idx))
67
68 /*
69 * Descriptor Header starting at index 0 with descriptor length len
70 */
71 #define DESC_HEADER(len) (DESC_HDR(0) | HDR_JD_DESCLEN(len))
72
73 /*
74 * Descriptor Header starting at idx with descriptor length len
75 */
76 #define DESC_HEADER_IDX(len, idx) (DESC_HDR(idx) | HDR_JD_DESCLEN(len))
77
78 /*
79 * Jump Local of class cla to descriptor offset if test meet the
80 * condition cond
81 */
82 #define JUMP_LOCAL(cla, test, cond, offset) \
83 (CMD_JUMP_TYPE | CMD_CLASS(cla) | JUMP_TYPE(LOCAL) | \
84 JUMP_TST_TYPE(test) | (cond) | JMP_LOCAL_OFFSET(offset))
85
86 /*
87 * Jump Local of no class to descriptor offset if test meet the
88 * condition cond
89 */
90 #define JUMP_CNO_LOCAL(test, cond, offset) \
91 JUMP_LOCAL(CLASS_NO, test, cond, offset)
92
93 /*
94 * Jump Local of class 1 to descriptor offset if test meet the
95 * condition cond
96 */
97 #define JUMP_C1_LOCAL(test, cond, offset) \
98 JUMP_LOCAL(CLASS_1, test, cond, offset)
99
100 /*
101 * First decrement specified source then
102 * Jump Local of no class to descriptor offset if test meet the
103 * condition cond
104 */
105 #define JUMP_CNO_LOCAL_DEC(test, src, cond, offset) \
106 (CMD_JUMP_TYPE | CMD_CLASS(CLASS_NO) | JUMP_TYPE(LOCAL_DEC) | \
107 JUMP_TST_TYPE(test) | JMP_SRC(src) | (cond) | \
108 JMP_LOCAL_OFFSET(offset))
109
110 /*
111 * Wait until test condition meet and jump next
112 */
113 #define WAIT_COND(test, cond) \
114 (JUMP_LOCAL(CLASS_NO, test, JMP_COND(cond), 1) | JMP_JSL)
115
116 /*
117 * Jump No Local of class cla to descriptor offset if test meet the
118 * condition cond
119 */
120 #define JUMP_NOTLOCAL(cla, test, cond) \
121 (CMD_JUMP_TYPE | CMD_CLASS(cla) | JUMP_TYPE(NON_LOCAL) | \
122 JUMP_TST_TYPE(test) | (cond))
123
124 /*
125 * User Halt with error if test meet the condition cond
126 */
127 #define HALT_USER(test, cond, error) \
128 (CMD_JUMP_TYPE | JUMP_TYPE(HALT_USER_STATUS) | JUMP_TST_TYPE(test) | \
129 JMP_COND(cond) | JMP_LOCAL_OFFSET(error))
130
131 /*
132 * Load Immediate value of length len to register dst of class cla
133 */
134 #define LD_IMM(cla, dst, len) \
135 (CMD_LOAD_TYPE | CMD_CLASS(cla) | CMD_IMM | LOAD_DST(dst) | \
136 LOAD_LENGTH(len))
137
138 /*
139 * Load Immediate value of length len to register dst w/o class
140 */
141 #define LD_NOCLASS_IMM(dst, len) LD_IMM(CLASS_NO, dst, len)
142
143 /*
144 * Load value of length len to register dst of class cla
145 */
146 #define LD_NOIMM(cla, dst, len) \
147 (CMD_LOAD_TYPE | CMD_CLASS(cla) | LOAD_DST(dst) | LOAD_LENGTH(len))
148
149 /*
150 * Load value of length len to register dst of class cla starting
151 * at register offset off
152 */
153 #define LD_NOIMM_OFF(cla, dst, len, off) \
154 (CMD_LOAD_TYPE | CMD_CLASS(cla) | LOAD_DST(dst) | LOAD_OFFSET(off) | \
155 LOAD_LENGTH(len))
156
157 /*
158 * FIFO Load to register dst class cla with action act
159 */
160 #define FIFO_LD(cla, dst, act, len) \
161 (CMD_FIFO_LOAD_TYPE | CMD_CLASS(cla) | FIFO_LOAD_INPUT(dst) | \
162 FIFO_LOAD_ACTION(act) | FIFO_LOAD_LENGTH(len))
163
164 /*
165 * FIFO Load to register dst class cla with action act.
166 * Pointer is a Scatter/Gather Table
167 */
168 #define FIFO_LD_SGT(cla, dst, act, len) \
169 (CMD_FIFO_LOAD_TYPE | CMD_CLASS(cla) | CMD_SGT | \
170 FIFO_LOAD_INPUT(dst) | FIFO_LOAD_ACTION(act) | FIFO_LOAD_LENGTH(len))
171
172 /*
173 * FIFO Load to register dst class cla with action act.
174 * Pointer is a Scatter/Gather Table
175 * The length is externally defined
176 */
177 #define FIFO_LD_SGT_EXT(cla, dst, act) \
178 (CMD_FIFO_LOAD_TYPE | CMD_CLASS(cla) | CMD_SGT | FIFO_LOAD_EXT | \
179 FIFO_LOAD_INPUT(dst) | FIFO_LOAD_ACTION(act))
180
181 /*
182 * FIFO Load to register dst class cla with action act.
183 * The length is externally defined
184 */
185 #define FIFO_LD_EXT(cla, dst, act) \
186 (CMD_FIFO_LOAD_TYPE | FIFO_LOAD_EXT | CMD_CLASS(cla) | \
187 FIFO_LOAD_INPUT(dst) | FIFO_LOAD_ACTION(act))
188
189 /*
190 * FIFO Load Immediate data length len to register dst class cla
191 * with action act.
192 */
193 #define FIFO_LD_IMM(cla, dst, act, len) \
194 (CMD_FIFO_LOAD_TYPE | CMD_IMM | CMD_CLASS(cla) | \
195 FIFO_LOAD_INPUT(dst) | FIFO_LOAD_ACTION(act) | FIFO_LOAD_LENGTH(len))
196
197 /*
198 * Store value of length len from register src of class cla
199 */
200 #define ST_NOIMM(cla, src, len) \
201 (CMD_STORE_TYPE | CMD_CLASS(cla) | STORE_SRC(src) | STORE_LENGTH(len))
202
203 /*
204 * Store value of length len from register src of class cla
205 * Pointer is a Scatter/Gather Table
206 */
207 #define ST_SGT_NOIMM(cla, src, len) \
208 (CMD_STORE_TYPE | CMD_CLASS(cla) | CMD_SGT | STORE_SRC(src) | \
209 STORE_LENGTH(len))
210
211 /*
212 * Store value of length len from register src of class cla starting
213 * at register offset off
214 */
215 #define ST_NOIMM_OFF(cla, src, len, off) \
216 (CMD_STORE_TYPE | CMD_CLASS(cla) | STORE_SRC(src) | \
217 STORE_OFFSET(off) | STORE_LENGTH(len))
218
219 /*
220 * Store value of length len from register src of class cla
221 */
222 #define ST_NOIMM_SEQ(cla, src, len) \
223 (CMD_STORE_SEQ_TYPE | CMD_CLASS(cla) | STORE_SRC(src) | \
224 STORE_LENGTH(len))
225
226 /*
227 * FIFO Store from register src of length len
228 */
229 #define FIFO_ST(src, len) \
230 (CMD_FIFO_STORE_TYPE | FIFO_STORE_OUTPUT(src) | FIFO_STORE_LENGTH(len))
231
232 /*
233 * FIFO Store from register src.
234 * The length is externally defined
235 */
236 #define FIFO_ST_EXT(src) \
237 (CMD_FIFO_STORE_TYPE | FIFO_STORE_EXT | FIFO_STORE_OUTPUT(src))
238
239 /*
240 * FIFO Store from register src of length len.
241 * Pointer is a Scatter/Gather Table
242 */
243 #define FIFO_ST_SGT(src, len) \
244 (CMD_FIFO_STORE_TYPE | CMD_SGT | FIFO_STORE_OUTPUT(src) | \
245 FIFO_STORE_LENGTH(len))
246
247 /*
248 * FIFO Store from register src.
249 * Pointer is a Scatter/Gather Table
250 * The length is externally defined
251 */
252 #define FIFO_ST_SGT_EXT(src) \
253 (CMD_FIFO_STORE_TYPE | CMD_SGT | FIFO_STORE_EXT | \
254 FIFO_STORE_OUTPUT(src))
255
256 /*
257 * SEQ FIFO Store from register src of length len
258 */
259 #define FIFO_ST_SEQ(src, len) \
260 (CMD_SEQ_FIFO_STORE_TYPE | FIFO_STORE_OUTPUT(src) | \
261 FIFO_STORE_LENGTH(len))
262
263 /*
264 * RNG State Handle instantation operation for sh ID
265 */
266 #define RNG_SH_INST(sh) \
267 (CMD_OP_TYPE | OP_TYPE(CLASS1) | OP_ALGO(RNG) | ALGO_RNG_SH(sh) | \
268 ALGO_AS(RNG_INSTANTIATE) | ALGO_RNG_PR)
269
270 /*
271 * RNG Generates Secure Keys
272 */
273 #define RNG_GEN_SECKEYS \
274 (CMD_OP_TYPE | OP_TYPE(CLASS1) | OP_ALGO(RNG) | ALGO_RNG_SK | \
275 ALGO_AS(RNG_GENERATE))
276
277 /*
278 * RNG Generates Data
279 */
280 #define RNG_GEN_DATA \
281 (CMD_OP_TYPE | OP_TYPE(CLASS1) | OP_ALGO(RNG) | ALGO_AS(RNG_GENERATE))
282
283 /*
284 * Hash Init Operation of algorithm algo
285 */
286 #define HASH_INIT(algo) \
287 (CMD_OP_TYPE | OP_TYPE(CLASS2) | (algo) | ALGO_AS(INIT) | ALGO_ENCRYPT)
288
289 /*
290 * Hash Update Operation of algorithm algo
291 */
292 #define HASH_UPDATE(algo) \
293 (CMD_OP_TYPE | OP_TYPE(CLASS2) | (algo) | ALGO_AS(UPDATE) | \
294 ALGO_ENCRYPT)
295
296 /*
297 * Hash Final Operation of algorithm algo
298 */
299 #define HASH_FINAL(algo) \
300 (CMD_OP_TYPE | OP_TYPE(CLASS2) | (algo) | ALGO_AS(FINAL) | ALGO_ENCRYPT)
301
302 /*
303 * Hash Init and Final Operation of algorithm algo
304 */
305 #define HASH_INITFINAL(algo) \
306 (CMD_OP_TYPE | OP_TYPE(CLASS2) | (algo) | ALGO_AS(INIT_FINAL) | \
307 ALGO_ENCRYPT)
308
309 /*
310 * HMAC Init Decryption Operation of algorithm algo
311 */
312 #define HMAC_INIT_DECRYPT(algo) \
313 (CMD_OP_TYPE | OP_TYPE(CLASS2) | (algo) | ALGO_AS(INIT) | \
314 ALGO_AAI(DIGEST_HMAC) | ALGO_DECRYPT)
315
316 /*
317 * HMAC Init and Final Operation of algorithm algo with Precomp key
318 */
319 #define HMAC_INITFINAL_PRECOMP(algo) \
320 (CMD_OP_TYPE | OP_TYPE(CLASS2) | (algo) | ALGO_AS(INIT_FINAL) | \
321 ALGO_AAI(DIGEST_HMAC_PRECOMP) | ALGO_ENCRYPT)
322
323 /*
324 * HMAC Init Operation of algorithm algo with Precomp key
325 */
326 #define HMAC_INIT_PRECOMP(algo) \
327 (CMD_OP_TYPE | OP_TYPE(CLASS2) | (algo) | ALGO_AS(INIT) | \
328 ALGO_AAI(DIGEST_HMAC_PRECOMP) | ALGO_ENCRYPT)
329
330 /*
331 * HMAC Final Operation of algorithm algo with Precomp key
332 */
333 #define HMAC_FINAL_PRECOMP(algo) \
334 (CMD_OP_TYPE | OP_TYPE(CLASS2) | (algo) | ALGO_AS(FINAL) | \
335 ALGO_AAI(DIGEST_HMAC_PRECOMP) | ALGO_ENCRYPT)
336
337 /*
338 * Cipher Init and Final Operation of algorithm algo
339 */
340 #define CIPHER_INITFINAL(algo, encrypt) \
341 (CMD_OP_TYPE | OP_TYPE(CLASS1) | (algo) | ALGO_AS(INIT_FINAL) | \
342 ((encrypt) ? ALGO_ENCRYPT : ALGO_DECRYPT))
343
344 /*
345 * Cipher Init Operation of algorithm algo
346 */
347 #define CIPHER_INIT(algo, encrypt) \
348 (CMD_OP_TYPE | OP_TYPE(CLASS1) | (algo) | ALGO_AS(INIT) | \
349 ((encrypt) ? ALGO_ENCRYPT : ALGO_DECRYPT))
350
351 /*
352 * Cipher Update Operation of algorithm algo
353 */
354 #define CIPHER_UPDATE(algo, encrypt) \
355 (CMD_OP_TYPE | OP_TYPE(CLASS1) | (algo) | ALGO_AS(UPDATE) | \
356 ((encrypt) ? ALGO_ENCRYPT : ALGO_DECRYPT))
357
358 /*
359 * Cipher Final Operation of algorithm algo
360 */
361 #define CIPHER_FINAL(algo, encrypt) \
362 (CMD_OP_TYPE | OP_TYPE(CLASS1) | (algo) | ALGO_AS(FINAL) | \
363 ((encrypt) ? ALGO_ENCRYPT : ALGO_DECRYPT))
364
365 /*
366 * Load a class cla key of length len to register dst.
367 * Key can be stored in plain text.
368 */
369 #define LD_KEY_PLAIN(cla, dst, len) \
370 (CMD_KEY_TYPE | CMD_CLASS(cla) | KEY_PTS | KEY_DEST(dst) | \
371 KEY_LENGTH(len))
372
373 /*
374 * Load a class cla key of length len to register dst.
375 * Key can be stored in plain text.
376 * Pointer is a Scatter/Gatter Table
377 */
378 #define LD_KEY_SGT_PLAIN(cla, dst, len) \
379 (CMD_KEY_TYPE | CMD_CLASS(cla) | CMD_SGT | KEY_PTS | KEY_DEST(dst) | \
380 KEY_LENGTH(len))
381
382 /*
383 * Load a split key of length len.
384 */
385 #define LD_KEY_SPLIT(len) \
386 (CMD_KEY_TYPE | CMD_CLASS(CLASS_2) | KEY_DEST(MDHA_SPLIT) | \
387 KEY_LENGTH(len))
388
389 /*
390 * MPPRIVK generation function.
391 */
392 #define MPPRIVK (CMD_OP_TYPE | OP_TYPE(ENCAPS) | PROTID(MPKEY))
393
394 /*
395 * MPPUBK generation function.
396 */
397 #define MPPUBK (CMD_OP_TYPE | OP_TYPE(DECAPS) | PROTID(MPKEY))
398
399 /*
400 * MPSIGN function.
401 */
402 #define MPSIGN_OP (CMD_OP_TYPE | OP_TYPE(DECAPS) | PROTID(MPSIGN))
403
404 /*
405 * Operation Mathematical of length len
406 * dest = src0 (operation func) src1
407 */
408 #define MATH(func, src0, src1, dst, len) \
409 (CMD_MATH_TYPE | MATH_FUNC(func) | MATH_SRC0(src0) | MATH_SRC1(src1) | \
410 MATH_DST(dst) | MATH_LENGTH(len))
411
412 /*
413 * Operation Mathematical of length len using an immediate value as operand 1
414 * dest = src (operation func) val
415 */
416 #define MATHI_OP1(func, src, val, dst, len) \
417 (CMD_MATHI_TYPE | MATH_FUNC(func) | MATHI_SRC(src) | \
418 MATHI_IMM_VALUE(val) | MATHI_DST(dst) | MATH_LENGTH(len))
419
420 /*
421 * PKHA Copy function from src to dst. Copy number of words specified
422 * in Source size register
423 */
424 #define PKHA_CPY_SSIZE(src, dst) \
425 (CMD_OP_TYPE | OP_TYPE(PKHA) | PKHA_ALG | PKHA_FUNC(CPY_SSIZE) | \
426 PKHA_CPY_SRC(src) | PKHA_CPY_DST(dst))
427
428 /*
429 * PKHA Copy N-Size function from src to dst. Copy number of words specified
430 * in PKHA N size register
431 */
432 #define PKHA_CPY_NSIZE(src, dst) \
433 (CMD_OP_TYPE | OP_TYPE(PKHA) | PKHA_ALG | PKHA_FUNC(CPY_NSIZE) | \
434 PKHA_CPY_SRC(src) | PKHA_CPY_DST(dst))
435
436 /*
437 * PKHA Operation op result into dst
438 */
439 #define PKHA_OP(op, dst) \
440 (CMD_OP_TYPE | OP_TYPE(PKHA) | PKHA_ALG | PKHA_FUNC(op) | \
441 PKHA_OUTSEL(dst))
442
443 /*
444 * PKHA Binomial operation op result into dst
445 */
446 #define PKHA_F2M_OP(op, dst) \
447 (CMD_OP_TYPE | OP_TYPE(PKHA) | PKHA_ALG | PKHA_F2M | PKHA_FUNC(op) | \
448 PKHA_OUTSEL(dst))
449
450 /*
451 * Move src to dst
452 */
453 #define MOVE(src, dst, off, len) \
454 (CMD_MOVE_TYPE | MOVE_SRC(src) | MOVE_DST(dst) | MOVE_OFFSET(off) | \
455 MOVE_LENGTH(len))
456
457 /*
458 * Move src to dst and wait until completion
459 */
460 #define MOVE_WAIT(src, dst, off, len) \
461 (CMD_MOVE_TYPE | MOVE_WC | MOVE_SRC(src) | MOVE_DST(dst) | \
462 MOVE_OFFSET(off) | MOVE_LENGTH(len))
463
464 /*
465 * RSA Encryption using format
466 */
467 #define RSA_ENCRYPT(format) \
468 (CMD_OP_TYPE | PROTID(RSA_ENC) | PROT_RSA_FMT(format))
469
470 /*
471 * RSA Decryption using format
472 */
473 #define RSA_DECRYPT(format) \
474 (CMD_OP_TYPE | PROTID(RSA_DEC) | PROT_RSA_FMT(format))
475
476 /*
477 * RSA Finalize Key in format
478 */
479 #define RSA_FINAL_KEY(format) \
480 (CMD_OP_TYPE | PROTID(RSA_FINISH_KEY) | PROT_RSA_KEY(format))
481
482 /*
483 * Public Keypair generation
484 */
485 #define PK_KEYPAIR_GEN(type) \
486 (CMD_OP_TYPE | OP_TYPE(UNI) | PROTID(PKKEY) | PROT_PK_TYPE(type))
487
488 /*
489 * DSA/ECDSA signature of message hashed
490 */
491 #define DSA_SIGN(type) \
492 (CMD_OP_TYPE | OP_TYPE(UNI) | PROTID(DSASIGN) | PROT_PK_MSG(HASHED) | \
493 PROT_PK_TYPE(type))
494
495 /*
496 * DSA/ECDSA signature verify message hashed
497 */
498 #define DSA_VERIFY(type) \
499 (CMD_OP_TYPE | OP_TYPE(UNI) | PROTID(DSAVERIFY) | \
500 PROT_PK_MSG(HASHED) | PROT_PK_TYPE(type))
501
502 /*
503 * DH/ECC Shared Secret
504 */
505 #define SHARED_SECRET(type) \
506 (CMD_OP_TYPE | OP_TYPE(UNI) | PROTID(SHARED_SECRET) | \
507 PROT_PK_TYPE(type))
508
509 /*
510 * Blob Master Key Verification
511 */
512 #define BLOB_MSTR_KEY \
513 (CMD_OP_TYPE | OP_TYPE(ENCAPS) | PROTID(BLOB) | PROT_BLOB_FMT_MSTR)
514
515 /*
516 * Blob encapsulation
517 */
518 #define BLOB_ENCAPS \
519 (CMD_OP_TYPE | OP_TYPE(ENCAPS) | PROTID(BLOB) | \
520 PROT_BLOB_FORMAT(NORMAL))
521
522 /*
523 * Blob decapsulation
524 */
525 #define BLOB_DECAPS \
526 (CMD_OP_TYPE | OP_TYPE(DECAPS) | PROTID(BLOB) | \
527 PROT_BLOB_FORMAT(NORMAL))
528
529 /*
530 * Black key CCM size
531 */
532 #define BLACK_KEY_CCM_SIZE(size) \
533 (ROUNDUP(size, 8) + BLACK_KEY_NONCE_SIZE + BLACK_KEY_ICV_SIZE)
534
535 /*
536 * Black key ECB size
537 */
538 #define BLACK_KEY_ECB_SIZE(size) ROUNDUP(size, 16)
539
540 /*
541 * Sequence Inout Pointer of length len
542 */
543 #define SEQ_IN_PTR(len) (CMD_SEQ_IN_TYPE | SEQ_LENGTH(len))
544
545 /*
546 * Sequence Output Pointer of length len
547 */
548 #define SEQ_OUT_PTR(len) (CMD_SEQ_OUT_TYPE | SEQ_LENGTH(len))
549
550 #endif /* __CAAM_DESC_HELPER_H__ */
551