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