jobdesc.c 6.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright 2017-2020 NXP
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include "caam.h"
#include <common/debug.h>
#include "jobdesc.h"
#include "rsa.h"
#include "sec_hw_specific.h"


/* Return Length of desctiptr from first word */
uint32_t desc_length(uint32_t *desc)
{
	return desc[0] & DESC_LEN_MASK;
}

/*Update start index in first word of descriptor */
void desc_update_start_index(uint32_t *desc, uint32_t index)
{
	desc[0] |= (index << DESC_START_SHIFT);
}

/* Initialize the descriptor */
void desc_init(uint32_t *desc)
{
	*desc = 0;
}

/* Add word in the descriptor and increment the length */
void desc_add_word(uint32_t *desc, uint32_t word)
{
	uint32_t len = desc_length(desc);

	/* Add Word at Last */
	uint32_t *last = desc + len;
	*last = word;

	/* Increase the length */
	desc[0] += 1;
}

/* Add Pointer to the descriptor */
void desc_add_ptr(uint32_t *desc, phys_addr_t *ptr)
{
	uint32_t len = desc_length(desc);

	/* Add Word at Last */
	phys_addr_t *last = (phys_addr_t *) (desc + len);

#ifdef CONFIG_PHYS_64BIT
	ptr_addr_t *ptr_addr = (ptr_addr_t *) last;

	ptr_addr->m_halves.high = PHYS_ADDR_HI(ptr);
	ptr_addr->m_halves.low = PHYS_ADDR_LO(ptr);
#else
	*last = ptr;
#endif

	/* Increase the length */
	desc[0] += (uint32_t) (sizeof(phys_addr_t) / sizeof(uint32_t));
}

/* Descriptor to generate Random words */
int 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)
{
	phys_addr_t *phys_addr_out = vtop(out_data);

	/* Current descriptor support only 64K length */
	if (len > U(0xffff))
		return -1;
	/* Additional Input not supported by current descriptor */
	if (add_ip_len > 0U)
		return -1;

	VERBOSE("Constructing descriptor\n");
	desc_init(desc);
	/* Class1 Alg Operation,RNG Optype, Generate */
	desc_add_word(desc, U(0xb0800000));
	desc_add_word(desc, U(0x82500000) | (state_handle << ALG_AAI_SH_SHIFT));
	desc_add_word(desc, U(0x60340000) | len);
	desc_add_ptr(desc, phys_addr_out);

	return 0;

}

/* Construct descriptor to instantiate RNG */
int cnstr_rng_instantiate_jobdesc(uint32_t *desc)
{
	desc_init(desc);
	desc_add_word(desc, U(0xb0800000));
	/* Class1 Alg Operation,RNG Optype, Instantiate */
	desc_add_word(desc, U(0x82500004));
	/* Wait for done */
	desc_add_word(desc, U(0xa2000001));
	/*Load to clear written */
	desc_add_word(desc, U(0x10880004));
	/*Pri Mode Reg clear */
	desc_add_word(desc, U(0x00000001));
	/* Generate secure keys */
	desc_add_word(desc, U(0x82501000));

	return 0;
}

/* Construct descriptor to generate hw key blob */
int 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)
{
	phys_addr_t *phys_key_idnfr, *phys_addr_in, *phys_addr_out;
	int i = 0;

	phys_key_idnfr = vtop((void *)key_idnfr);
	phys_addr_in = vtop((void *)plain_txt);
	phys_addr_out = vtop((void *)enc_blob);

	desc_init(desc);

	desc_add_word(desc, U(0xb0800000));

	/* Key Identifier */
	desc_add_word(desc, (key_class | key_sz));
	desc_add_ptr(desc, phys_key_idnfr);

	/* Source Address */
	desc_add_word(desc, U(0xf0400000));
	desc_add_ptr(desc, phys_addr_in);

	/* In Size = 0x10 */
	desc_add_word(desc, in_sz);

	/* Out Address */
	desc_add_word(desc, U(0xf8400000));
	desc_add_ptr(desc, phys_addr_out);

	/* Out Size = 0x10 */
	desc_add_word(desc, out_sz);

	/* Operation */
	desc_add_word(desc, operation);

	for (i = 0; i < 15; i++)
		VERBOSE("desc word %x\n", desc[i]);

	return 0;
}

/***************************************************************************
 * Function	: inline_cnstr_jobdesc_pkha_rsaexp
 * Arguments	: desc - Pointer to Descriptor
 *		  pkin - Pointer to Input Params
 *		  out - Pointer to Output
 *		  out_siz - Output Size
 * Return	: Void
 * Description	: Creates the descriptor for PKHA RSA
 ***************************************************************************/
void cnstr_jobdesc_pkha_rsaexp(uint32_t *desc,
			       struct pk_in_params *pkin, uint8_t *out,
			       uint32_t out_siz)
{
	phys_addr_t *ptr_addr_e, *ptr_addr_a, *ptr_addr_n, *ptr_addr_out;

	ptr_addr_e = vtop((void *)(pkin->e));
	ptr_addr_a = vtop((void *)(pkin->a));
	ptr_addr_n = vtop((void *)(pkin->n));
	ptr_addr_out = vtop((void *)(out));

	desc_init(desc);
	desc_add_word(desc, U(0xb0800000));
	desc_add_word(desc, U(0x02010000) | pkin->e_siz);
	desc_add_ptr(desc, ptr_addr_e);
	desc_add_word(desc, U(0x220c0000) | pkin->a_siz);
	desc_add_ptr(desc, ptr_addr_a);
	desc_add_word(desc, U(0x22080000) | pkin->n_siz);
	desc_add_ptr(desc, ptr_addr_n);
	desc_add_word(desc, U(0x81800006));
	desc_add_word(desc, U(0x620d0000) | out_siz);
	desc_add_ptr(desc, ptr_addr_out);
}

/***************************************************************************
 * Function	: inline_cnstr_jobdesc_sha256
 * Arguments	: desc - Pointer to Descriptor
 *		  msg - Pointer to SG Table
 *		  msgsz - Size of SG Table
 *		  digest - Pointer to Output Digest
 * Return	: Void
 * Description	: Creates the descriptor for SHA256 HASH calculation
 ***************************************************************************/
void cnstr_hash_jobdesc(uint32_t *desc, uint8_t *msg, uint32_t msgsz,
			uint8_t *digest)
{
	/* SHA 256 , output is of length 32 words */
	phys_addr_t *ptr_addr_in, *ptr_addr_out;

	ptr_addr_in = (void *)vtop(msg);
	ptr_addr_out = (void *)vtop(digest);

	desc_init(desc);
	desc_add_word(desc, U(0xb0800000));

	/* Operation Command
	 * OP_TYPE_CLASS2_ALG | OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HASH |
	 * OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT | OP_ALG_ICV_OFF)
	 */
	desc_add_word(desc, U(0x8443000d));

	if (msgsz > U(0xffff)) {
		desc_add_word(desc, U(0x25540000));	/* FIFO Load */
		desc_add_ptr(desc, ptr_addr_in);	/* Pointer to msg */
		desc_add_word(desc, msgsz);	/* Size */
		desc_add_word(desc, U(0x54200020));	/* FIFO Store */
		desc_add_ptr(desc, ptr_addr_out);	/* Pointer to Result */
	} else {
		desc_add_word(desc, U(0x25140000) | msgsz);
		desc_add_ptr(desc, ptr_addr_in);
		desc_add_word(desc, U(0x54200020));
		desc_add_ptr(desc, ptr_addr_out);
	}

}