1 /*
2 * Copyright 2017-2022 NXP
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8 #include <errno.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include <assert.h>
15 #include "caam.h"
16 #include <common/debug.h>
17 #include "jobdesc.h"
18 #include "rsa.h"
19 #include "sec_hw_specific.h"
20
21 /* Return Length of desctiptr from first word */
desc_length(uint32_t * desc)22 uint32_t desc_length(uint32_t *desc)
23 {
24 return desc[0] & DESC_LEN_MASK;
25 }
26
27 /*Update start index in first word of descriptor */
desc_update_start_index(uint32_t * desc,uint32_t index)28 void desc_update_start_index(uint32_t *desc, uint32_t index)
29 {
30 desc[0] |= (index << DESC_START_SHIFT);
31 }
32
33 /* Initialize the descriptor */
desc_init(uint32_t * desc)34 void desc_init(uint32_t *desc)
35 {
36 *desc = 0;
37 }
38
39 /* Add word in the descriptor and increment the length */
desc_add_word(uint32_t * desc,uint32_t word)40 void desc_add_word(uint32_t *desc, uint32_t word)
41 {
42 uint32_t len = desc_length(desc);
43
44 assert((len + 1) < MAX_DESC_SIZE_WORDS);
45
46 /* Add Word at Last */
47 uint32_t *last = desc + len;
48 *last = word;
49
50 /* Increase the length */
51 desc[0] += 1;
52 }
53
54 /* Add Pointer to the descriptor */
desc_add_ptr(uint32_t * desc,phys_addr_t * ptr)55 void desc_add_ptr(uint32_t *desc, phys_addr_t *ptr)
56 {
57 uint32_t len = desc_length(desc);
58
59 assert((len + (uint32_t) (sizeof(phys_addr_t) / sizeof(uint32_t)))
60 < MAX_DESC_SIZE_WORDS);
61
62 /* Add Word at Last */
63 phys_addr_t *last = (phys_addr_t *) (desc + len);
64
65 #ifdef CONFIG_PHYS_64BIT
66 ptr_addr_t *ptr_addr = (ptr_addr_t *) last;
67
68 ptr_addr->high = PHYS_ADDR_HI(ptr);
69 ptr_addr->low = PHYS_ADDR_LO(ptr);
70 #else
71 *last = ptr;
72 #endif
73
74 /* Increase the length */
75 desc[0] += (uint32_t) (sizeof(phys_addr_t) / sizeof(uint32_t));
76 }
77
78 /* Descriptor to generate Random words */
cnstr_rng_jobdesc(uint32_t * desc,uint32_t state_handle,uint32_t * add_inp,uint32_t add_ip_len,uint8_t * out_data,uint32_t len)79 int cnstr_rng_jobdesc(uint32_t *desc, uint32_t state_handle,
80 uint32_t *add_inp, uint32_t add_ip_len,
81 uint8_t *out_data, uint32_t len)
82 {
83 phys_addr_t *phys_addr_out = vtop(out_data);
84
85 /* Current descriptor support only 64K length */
86 if (len > U(0xffff))
87 return -1;
88 /* Additional Input not supported by current descriptor */
89 if (add_ip_len > 0U)
90 return -1;
91
92 VERBOSE("Constructing descriptor\n");
93 desc_init(desc);
94 /* Class1 Alg Operation,RNG Optype, Generate */
95 desc_add_word(desc, U(0xb0800000));
96 desc_add_word(desc, U(0x82500000) | (state_handle << ALG_AAI_SH_SHIFT));
97 desc_add_word(desc, U(0x60340000) | len);
98 desc_add_ptr(desc, phys_addr_out);
99
100 return 0;
101
102 }
103
104 /* Construct descriptor to instantiate RNG */
cnstr_rng_instantiate_jobdesc(uint32_t * desc)105 int cnstr_rng_instantiate_jobdesc(uint32_t *desc)
106 {
107 desc_init(desc);
108 desc_add_word(desc, U(0xb0800000));
109 /* Class1 Alg Operation,RNG Optype, Instantiate */
110 desc_add_word(desc, U(0x82500004));
111 /* Wait for done */
112 desc_add_word(desc, U(0xa2000001));
113 /*Load to clear written */
114 desc_add_word(desc, U(0x10880004));
115 /*Pri Mode Reg clear */
116 desc_add_word(desc, U(0x00000001));
117 /* Generate secure keys */
118 desc_add_word(desc, U(0x82501000));
119
120 return 0;
121 }
122
123 /* Construct descriptor to generate hw key blob */
cnstr_hw_encap_blob_jobdesc(uint32_t * desc,uint8_t * key_idnfr,uint32_t key_sz,uint32_t key_class,uint8_t * plain_txt,uint32_t in_sz,uint8_t * enc_blob,uint32_t out_sz,uint32_t operation)124 int cnstr_hw_encap_blob_jobdesc(uint32_t *desc,
125 uint8_t *key_idnfr, uint32_t key_sz,
126 uint32_t key_class, uint8_t *plain_txt,
127 uint32_t in_sz, uint8_t *enc_blob,
128 uint32_t out_sz, uint32_t operation)
129 {
130 phys_addr_t *phys_key_idnfr, *phys_addr_in, *phys_addr_out;
131 int i = 0;
132
133 phys_key_idnfr = vtop((void *)key_idnfr);
134 phys_addr_in = vtop((void *)plain_txt);
135 phys_addr_out = vtop((void *)enc_blob);
136
137 desc_init(desc);
138
139 desc_add_word(desc, U(0xb0800000));
140
141 /* Key Identifier */
142 desc_add_word(desc, (key_class | key_sz));
143 desc_add_ptr(desc, phys_key_idnfr);
144
145 /* Source Address */
146 desc_add_word(desc, U(0xf0400000));
147 desc_add_ptr(desc, phys_addr_in);
148
149 /* In Size = 0x10 */
150 desc_add_word(desc, in_sz);
151
152 /* Out Address */
153 desc_add_word(desc, U(0xf8400000));
154 desc_add_ptr(desc, phys_addr_out);
155
156 /* Out Size = 0x10 */
157 desc_add_word(desc, out_sz);
158
159 /* Operation */
160 desc_add_word(desc, operation);
161
162 for (i = 0; i < 15; i++)
163 VERBOSE("desc word %x\n", desc[i]);
164
165 return 0;
166 }
167
168 /***************************************************************************
169 * Function : inline_cnstr_jobdesc_pkha_rsaexp
170 * Arguments : desc - Pointer to Descriptor
171 * pkin - Pointer to Input Params
172 * out - Pointer to Output
173 * out_siz - Output Size
174 * Return : Void
175 * Description : Creates the descriptor for PKHA RSA
176 ***************************************************************************/
cnstr_jobdesc_pkha_rsaexp(uint32_t * desc,struct pk_in_params * pkin,uint8_t * out,uint32_t out_siz)177 void cnstr_jobdesc_pkha_rsaexp(uint32_t *desc,
178 struct pk_in_params *pkin, uint8_t *out,
179 uint32_t out_siz)
180 {
181 phys_addr_t *ptr_addr_e, *ptr_addr_a, *ptr_addr_n, *ptr_addr_out;
182
183 ptr_addr_e = vtop((void *)(pkin->e));
184 ptr_addr_a = vtop((void *)(pkin->a));
185 ptr_addr_n = vtop((void *)(pkin->n));
186 ptr_addr_out = vtop((void *)(out));
187
188 desc_init(desc);
189 desc_add_word(desc, U(0xb0800000));
190 desc_add_word(desc, U(0x02010000) | pkin->e_siz);
191 desc_add_ptr(desc, ptr_addr_e);
192 desc_add_word(desc, U(0x220c0000) | pkin->a_siz);
193 desc_add_ptr(desc, ptr_addr_a);
194 desc_add_word(desc, U(0x22080000) | pkin->n_siz);
195 desc_add_ptr(desc, ptr_addr_n);
196 desc_add_word(desc, U(0x81800006));
197 desc_add_word(desc, U(0x620d0000) | out_siz);
198 desc_add_ptr(desc, ptr_addr_out);
199 }
200
201 /***************************************************************************
202 * Function : inline_cnstr_jobdesc_sha256
203 * Arguments : desc - Pointer to Descriptor
204 * msg - Pointer to SG Table
205 * msgsz - Size of SG Table
206 * digest - Pointer to Output Digest
207 * Return : Void
208 * Description : Creates the descriptor for SHA256 HASH calculation
209 ***************************************************************************/
cnstr_hash_jobdesc(uint32_t * desc,uint8_t * msg,uint32_t msgsz,uint8_t * digest)210 void cnstr_hash_jobdesc(uint32_t *desc, uint8_t *msg, uint32_t msgsz,
211 uint8_t *digest)
212 {
213 /* SHA 256 , output is of length 32 words */
214 phys_addr_t *ptr_addr_in, *ptr_addr_out;
215
216 ptr_addr_in = (void *)vtop(msg);
217 ptr_addr_out = (void *)vtop(digest);
218
219 desc_init(desc);
220 desc_add_word(desc, U(0xb0800000));
221
222 /* Operation Command
223 * OP_TYPE_CLASS2_ALG | OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HASH |
224 * OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT | OP_ALG_ICV_OFF)
225 */
226 desc_add_word(desc, U(0x8443000d));
227
228 if (msgsz > U(0xffff)) {
229 desc_add_word(desc, U(0x25540000)); /* FIFO Load */
230 desc_add_ptr(desc, ptr_addr_in); /* Pointer to msg */
231 desc_add_word(desc, msgsz); /* Size */
232 desc_add_word(desc, U(0x54200020)); /* FIFO Store */
233 desc_add_ptr(desc, ptr_addr_out); /* Pointer to Result */
234 } else {
235 desc_add_word(desc, U(0x25140000) | msgsz);
236 desc_add_ptr(desc, ptr_addr_in);
237 desc_add_word(desc, U(0x54200020));
238 desc_add_ptr(desc, ptr_addr_out);
239 }
240
241 }
242