xref: /openbmc/u-boot/drivers/crypto/fsl/jobdesc.c (revision 86bedaeb)
1 /*
2  * SEC Descriptor Construction Library
3  * Basic job descriptor construction
4  *
5  * Copyright 2014 Freescale Semiconductor, Inc.
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  *
9  */
10 
11 #include <common.h>
12 #include "desc_constr.h"
13 #include "jobdesc.h"
14 #include "rsa_caam.h"
15 
16 #define KEY_BLOB_SIZE			32
17 #define MAC_SIZE			16
18 
19 void inline_cnstr_jobdesc_hash(uint32_t *desc,
20 			  const uint8_t *msg, uint32_t msgsz, uint8_t *digest,
21 			  u32 alg_type, uint32_t alg_size, int sg_tbl)
22 {
23 	/* SHA 256 , output is of length 32 words */
24 	uint32_t storelen = alg_size;
25 	u32 options;
26 	dma_addr_t dma_addr_in, dma_addr_out;
27 
28 	dma_addr_in = virt_to_phys((void *)msg);
29 	dma_addr_out = virt_to_phys((void *)digest);
30 
31 	init_job_desc(desc, 0);
32 	append_operation(desc, OP_TYPE_CLASS2_ALG |
33 			 OP_ALG_AAI_HASH | OP_ALG_AS_INITFINAL |
34 			 OP_ALG_ENCRYPT | OP_ALG_ICV_OFF | alg_type);
35 
36 	options = LDST_CLASS_2_CCB | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2;
37 	if (sg_tbl)
38 		options |= FIFOLDST_SGF;
39 	if (msgsz > 0xffff) {
40 		options |= FIFOLDST_EXT;
41 		append_fifo_load(desc, dma_addr_in, 0, options);
42 		append_cmd(desc, msgsz);
43 	} else {
44 		append_fifo_load(desc, dma_addr_in, msgsz, options);
45 	}
46 
47 	append_store(desc, dma_addr_out, storelen,
48 		     LDST_CLASS_2_CCB | LDST_SRCDST_BYTE_CONTEXT);
49 }
50 
51 void inline_cnstr_jobdesc_blob_encap(uint32_t *desc, uint8_t *key_idnfr,
52 				     uint8_t *plain_txt, uint8_t *enc_blob,
53 				     uint32_t in_sz)
54 {
55 	dma_addr_t dma_addr_key_idnfr, dma_addr_in, dma_addr_out;
56 	uint32_t key_sz = KEY_IDNFR_SZ_BYTES;
57 	/* output blob will have 32 bytes key blob in beginning and
58 	 * 16 byte HMAC identifier at end of data blob */
59 	uint32_t out_sz = in_sz + KEY_BLOB_SIZE + MAC_SIZE;
60 
61 	dma_addr_key_idnfr = virt_to_phys((void *)key_idnfr);
62 	dma_addr_in	= virt_to_phys((void *)plain_txt);
63 	dma_addr_out	= virt_to_phys((void *)enc_blob);
64 
65 	init_job_desc(desc, 0);
66 
67 	append_key(desc, dma_addr_key_idnfr, key_sz, CLASS_2);
68 
69 	append_seq_in_ptr(desc, dma_addr_in, in_sz, 0);
70 
71 	append_seq_out_ptr(desc, dma_addr_out, out_sz, 0);
72 
73 	append_operation(desc, OP_TYPE_ENCAP_PROTOCOL | OP_PCLID_BLOB);
74 }
75 
76 void inline_cnstr_jobdesc_blob_decap(uint32_t *desc, uint8_t *key_idnfr,
77 				     uint8_t *enc_blob, uint8_t *plain_txt,
78 				     uint32_t out_sz)
79 {
80 	dma_addr_t dma_addr_key_idnfr, dma_addr_in, dma_addr_out;
81 	uint32_t key_sz = KEY_IDNFR_SZ_BYTES;
82 	uint32_t in_sz = out_sz + KEY_BLOB_SIZE + MAC_SIZE;
83 
84 	dma_addr_key_idnfr = virt_to_phys((void *)key_idnfr);
85 	dma_addr_in	= virt_to_phys((void *)enc_blob);
86 	dma_addr_out	= virt_to_phys((void *)plain_txt);
87 
88 	init_job_desc(desc, 0);
89 
90 	append_key(desc, dma_addr_key_idnfr, key_sz, CLASS_2);
91 
92 	append_seq_in_ptr(desc, dma_addr_in, in_sz, 0);
93 
94 	append_seq_out_ptr(desc, dma_addr_out, out_sz, 0);
95 
96 	append_operation(desc, OP_TYPE_DECAP_PROTOCOL | OP_PCLID_BLOB);
97 }
98 
99 /*
100  * Descriptor to instantiate RNG State Handle 0 in normal mode and
101  * load the JDKEK, TDKEK and TDSK registers
102  */
103 void inline_cnstr_jobdesc_rng_instantiation(uint32_t *desc)
104 {
105 	u32 *jump_cmd;
106 
107 	init_job_desc(desc, 0);
108 
109 	/* INIT RNG in non-test mode */
110 	append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
111 			 OP_ALG_AS_INIT);
112 
113 	/* wait for done */
114 	jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
115 	set_jump_tgt_here(desc, jump_cmd);
116 
117 	/*
118 	 * load 1 to clear written reg:
119 	 * resets the done interrrupt and returns the RNG to idle.
120 	 */
121 	append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
122 
123 	/* generate secure keys (non-test) */
124 	append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
125 			 OP_ALG_RNG4_SK);
126 }
127 
128 /* Change key size to bytes form bits in calling function*/
129 void inline_cnstr_jobdesc_pkha_rsaexp(uint32_t *desc,
130 				      struct pk_in_params *pkin, uint8_t *out,
131 				      uint32_t out_siz)
132 {
133 	dma_addr_t dma_addr_e, dma_addr_a, dma_addr_n, dma_addr_out;
134 
135 	dma_addr_e = virt_to_phys((void *)pkin->e);
136 	dma_addr_a = virt_to_phys((void *)pkin->a);
137 	dma_addr_n = virt_to_phys((void *)pkin->n);
138 	dma_addr_out = virt_to_phys((void *)out);
139 
140 	init_job_desc(desc, 0);
141 	append_key(desc, dma_addr_e, pkin->e_siz, KEY_DEST_PKHA_E | CLASS_1);
142 
143 	append_fifo_load(desc, dma_addr_a,
144 			 pkin->a_siz, LDST_CLASS_1_CCB | FIFOLD_TYPE_PK_A);
145 
146 	append_fifo_load(desc, dma_addr_n,
147 			 pkin->n_siz, LDST_CLASS_1_CCB | FIFOLD_TYPE_PK_N);
148 
149 	append_operation(desc, OP_TYPE_PK | OP_ALG_PK | OP_ALG_PKMODE_MOD_EXPO);
150 
151 	append_fifo_store(desc, dma_addr_out, out_siz,
152 			  LDST_CLASS_1_CCB | FIFOST_TYPE_PKHA_B);
153 }
154