summaryrefslogtreecommitdiff
path: root/drivers/crypto/caam/pkc_desc.c
blob: 2e81519ca4d48551f1d8d14b6c3fe2ddd85db578 (plain)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * \file - pkc_desc.c
 * \brief - Freescale FSL CAAM support for Public Key Cryptography descriptor
 *
 * Author: Yashpal Dutta <yashpal.dutta@freescale.com>
 *
 * Copyright 2012 Freescale Semiconductor, Inc.
 *
 * There is no shared descriptor for PKC but Job descriptor must carry
 * all the desired key parameters, input and output pointers
 *
 */
#include "pkc_desc.h"

/*#define CAAM_DEBUG */
/* Descriptor for RSA Public operation */
void *caam_rsa_pub_desc(struct rsa_edesc *edesc)
{
	u32 start_idx, desc_size;
	struct rsa_pub_desc_s *rsa_pub_desc =
	    (struct rsa_pub_desc_s *)edesc->hw_desc;
	struct rsa_pub_edesc_s *pub_edesc = &edesc->dma_u.rsa_pub_edesc;
#ifdef CAAM_DEBUG
	uint32_t i;
	uint32_t *buf = (uint32_t *)rsa_pub_desc;
#endif

	desc_size = sizeof(struct rsa_pub_desc_s) / sizeof(u32);
	start_idx = desc_size - 1;
	start_idx &= HDR_START_IDX_MASK;
	init_job_desc(edesc->hw_desc, (start_idx << HDR_START_IDX_SHIFT) |
		      (start_idx & HDR_DESCLEN_MASK) | HDR_ONE);
	rsa_pub_desc->n_dma = pub_edesc->n_dma;
	rsa_pub_desc->e_dma = pub_edesc->e_dma;
	rsa_pub_desc->f_dma = pub_edesc->f_dma;
	rsa_pub_desc->g_dma = pub_edesc->g_dma;
	rsa_pub_desc->sgf_flg = (pub_edesc->sg_flgs.e_len << RSA_PDB_E_SHIFT)
	    | pub_edesc->sg_flgs.n_len;
	rsa_pub_desc->msg_len = pub_edesc->f_len;
	rsa_pub_desc->op = CMD_OPERATION | OP_TYPE_UNI_PROTOCOL |
	    OP_PCLID_RSAENC_PUBKEY;
#ifdef CAAM_DEBUG
	for (i = 0; i < desc_size; i++)
		pr_debug("[%d] %x\n", i, buf[i]);
#endif
	return rsa_pub_desc;
}

/* Descriptor for RSA Private operation Form1 */
void *caam_rsa_priv_f1_desc(struct rsa_edesc *edesc)
{
	u32 start_idx, desc_size;
	struct rsa_priv_frm1_desc_s *rsa_priv_desc =
	    (struct rsa_priv_frm1_desc_s *)edesc->hw_desc;
	struct rsa_priv_frm1_edesc_s *priv_edesc =
	    &edesc->dma_u.rsa_priv_f1_edesc;

	desc_size = sizeof(struct rsa_priv_frm1_desc_s) / sizeof(u32);
	start_idx = desc_size - 1;
	start_idx &= HDR_START_IDX_MASK;
	init_job_desc(edesc->hw_desc, (start_idx << HDR_START_IDX_SHIFT) |
		      (start_idx & HDR_DESCLEN_MASK) | HDR_ONE);
	rsa_priv_desc->n_dma = priv_edesc->n_dma;
	rsa_priv_desc->d_dma = priv_edesc->d_dma;
	rsa_priv_desc->f_dma = priv_edesc->f_dma;
	rsa_priv_desc->g_dma = priv_edesc->g_dma;
	/* TBD. Support SG flags */
	rsa_priv_desc->sgf_flg = (priv_edesc->sg_flgs.d_len << RSA_PDB_D_SHIFT)
	    | priv_edesc->sg_flgs.n_len;
	rsa_priv_desc->op = CMD_OPERATION | OP_TYPE_UNI_PROTOCOL |
	    OP_PCLID_RSADEC_PRVKEY | RSA_PRIV_KEY_FRM_1;
	return rsa_priv_desc;
}

/* Descriptor for RSA Private operation Form2 */
void *caam_rsa_priv_f2_desc(struct rsa_edesc *edesc)
{
	u32 start_idx, desc_size;
	struct rsa_priv_frm2_desc_s *rsa_priv_desc =
	    (struct rsa_priv_frm2_desc_s *)edesc->hw_desc;
	struct rsa_priv_frm2_edesc_s *priv_edesc =
	    &edesc->dma_u.rsa_priv_f2_edesc;

	desc_size = sizeof(struct rsa_priv_frm2_desc_s) / sizeof(u32);
	start_idx = desc_size - 1;
	start_idx &= HDR_START_IDX_MASK;
	init_job_desc(edesc->hw_desc, (start_idx << HDR_START_IDX_SHIFT) |
		      (start_idx & HDR_DESCLEN_MASK) | HDR_ONE);
	rsa_priv_desc->p_dma = priv_edesc->p_dma;
	rsa_priv_desc->q_dma = priv_edesc->q_dma;
	rsa_priv_desc->d_dma = priv_edesc->d_dma;
	rsa_priv_desc->f_dma = priv_edesc->f_dma;
	rsa_priv_desc->g_dma = priv_edesc->g_dma;
	rsa_priv_desc->tmp1_dma = priv_edesc->tmp1_dma;
	rsa_priv_desc->tmp2_dma = priv_edesc->tmp2_dma;
	rsa_priv_desc->sgf_flg = (priv_edesc->sg_flgs.d_len << RSA_PDB_D_SHIFT)
	    | priv_edesc->sg_flgs.n_len;
	rsa_priv_desc->p_q_len = (priv_edesc->q_len << RSA_PDB_Q_SHIFT)
	    | priv_edesc->p_len;
	rsa_priv_desc->op = CMD_OPERATION | OP_TYPE_UNI_PROTOCOL |
	    OP_PCLID_RSADEC_PRVKEY | RSA_PRIV_KEY_FRM_2;
	return rsa_priv_desc;
}

/* Descriptor for RSA Private operation Form3 */
void *caam_rsa_priv_f3_desc(struct rsa_edesc *edesc)
{
	u32 start_idx, desc_size;
	struct rsa_priv_frm3_desc_s *rsa_priv_desc =
	    (struct rsa_priv_frm3_desc_s *)edesc->hw_desc;
	struct rsa_priv_frm3_edesc_s *priv_edesc =
	    &edesc->dma_u.rsa_priv_f3_edesc;
#ifdef CAAM_DEBUG
	uint32_t *buf = (uint32_t *)rsa_priv_desc;
	uint32_t i;
#endif

	desc_size = sizeof(struct rsa_priv_frm3_desc_s) / sizeof(u32);
	start_idx = desc_size - 1;
	start_idx &= HDR_START_IDX_MASK;
	init_job_desc(edesc->hw_desc, (start_idx << HDR_START_IDX_SHIFT) |
		      (start_idx & HDR_DESCLEN_MASK) | HDR_ONE);
	rsa_priv_desc->p_dma = priv_edesc->p_dma;
	rsa_priv_desc->q_dma = priv_edesc->q_dma;
	rsa_priv_desc->dp_dma = priv_edesc->dp_dma;
	rsa_priv_desc->dq_dma = priv_edesc->dq_dma;
	rsa_priv_desc->c_dma = priv_edesc->c_dma;
	rsa_priv_desc->f_dma = priv_edesc->f_dma;
	rsa_priv_desc->g_dma = priv_edesc->g_dma;
	rsa_priv_desc->tmp1_dma = priv_edesc->tmp1_dma;
	rsa_priv_desc->tmp2_dma = priv_edesc->tmp2_dma;
	rsa_priv_desc->p_q_len = (priv_edesc->q_len << RSA_PDB_Q_SHIFT)
	    | priv_edesc->p_len;
	/* TBD: SG Flags to be filled */
	rsa_priv_desc->sgf_flg = priv_edesc->sg_flgs.n_len;
	rsa_priv_desc->op = CMD_OPERATION | OP_TYPE_UNI_PROTOCOL |
	    OP_PCLID_RSADEC_PRVKEY | RSA_PRIV_KEY_FRM_3;
#ifdef CAAM_DEBUG
	for (i = 0; i < desc_size; i++)
		pr_debug("[%d] %x\n", i, buf[i]);
#endif
	return rsa_priv_desc;
}

/* DH sign CAAM descriptor */
void *caam_dh_key_desc(struct dh_edesc_s *edesc)
{
	u32 start_idx, desc_size;
	void *desc;
#ifdef CAAM_DEBUG
	uint32_t i;
	uint32_t *buf;
#endif
	struct dh_key_desc_s *dh_desc =
	    (struct dh_key_desc_s *)edesc->hw_desc;
	desc_size = sizeof(struct dh_key_desc_s) / sizeof(u32);
	start_idx = desc_size - 1;
	start_idx &= HDR_START_IDX_MASK;
	init_job_desc(edesc->hw_desc, (start_idx << HDR_START_IDX_SHIFT) |
		      (start_idx & HDR_DESCLEN_MASK) | HDR_ONE);
	dh_desc->sgf_ln = (edesc->l_len << DH_PDB_L_SHIFT) |
		((edesc->n_len & DH_PDB_N_MASK));
	dh_desc->q_dma = edesc->q_dma;
	dh_desc->w_dma = edesc->w_dma;
	dh_desc->s_dma = edesc->s_dma;
	dh_desc->z_dma = edesc->z_dma;
	dh_desc->op = CMD_OPERATION | OP_TYPE_UNI_PROTOCOL |
	    OP_PCLID_DH;
	if (edesc->req_type == ECDH_COMPUTE_KEY) {
		dh_desc->ab_dma = edesc->ab_dma;
		dh_desc->op |= OP_PCL_PKPROT_ECC;
		if (edesc->curve_type == ECC_BINARY)
			dh_desc->op |= OP_PCL_PKPROT_F2M;
	}

	desc = dh_desc;
#ifdef CAAM_DEBUG
	buf = desc;
	pr_debug("%d DH Descriptor is:\n", desc_size);
	for (i = 0; i < desc_size; i++)
		pr_debug("[%d] %x\n", i, buf[i]);
#endif
	return desc;
}

/* DSA sign CAAM descriptor */
void *caam_dsa_sign_desc(struct dsa_edesc_s *edesc)
{
	u32 start_idx, desc_size;
	void *desc;
#ifdef CAAM_DEBUG
	uint32_t i;
	uint32_t *buf;
#endif

	if (edesc->req_type == ECDSA_SIGN) {
		struct ecdsa_sign_desc_s *ecdsa_desc =
		    (struct ecdsa_sign_desc_s *)edesc->hw_desc;
		desc_size = sizeof(struct ecdsa_sign_desc_s) / sizeof(u32);
		start_idx = desc_size - 1;
		start_idx &= HDR_START_IDX_MASK;
		init_job_desc(edesc->hw_desc,
			      (start_idx << HDR_START_IDX_SHIFT) |
			      (start_idx & HDR_DESCLEN_MASK) | HDR_ONE);
		ecdsa_desc->sgf_ln = (edesc->l_len << DSA_PDB_L_SHIFT) |
			((edesc->n_len & DSA_PDB_N_MASK));
		ecdsa_desc->q_dma = edesc->q_dma;
		ecdsa_desc->r_dma = edesc->r_dma;
		ecdsa_desc->g_dma = edesc->g_dma;
		ecdsa_desc->s_dma = edesc->key_dma;
		ecdsa_desc->f_dma = edesc->f_dma;
		ecdsa_desc->c_dma = edesc->c_dma;
		ecdsa_desc->d_dma = edesc->d_dma;
		ecdsa_desc->ab_dma = edesc->ab_dma;
		ecdsa_desc->op = CMD_OPERATION | OP_TYPE_UNI_PROTOCOL |
		    OP_PCLID_DSASIGN | OP_PCL_PKPROT_ECC;
		if (edesc->curve_type == ECC_BINARY)
			ecdsa_desc->op |= OP_PCL_PKPROT_F2M;

		desc = ecdsa_desc;
	} else {
		struct dsa_sign_desc_s *dsa_desc =
		    (struct dsa_sign_desc_s *)edesc->hw_desc;
		desc_size = sizeof(struct dsa_sign_desc_s) / sizeof(u32);
		start_idx = desc_size - 1;
		start_idx &= HDR_START_IDX_MASK;
		init_job_desc(edesc->hw_desc,
			      (start_idx << HDR_START_IDX_SHIFT) |
			      (start_idx & HDR_DESCLEN_MASK) | HDR_ONE);
		dsa_desc->sgf_ln = (edesc->l_len << DSA_PDB_L_SHIFT) |
			((edesc->n_len & DSA_PDB_N_MASK));
		dsa_desc->q_dma = edesc->q_dma;
		dsa_desc->r_dma = edesc->r_dma;
		dsa_desc->g_dma = edesc->g_dma;
		dsa_desc->s_dma = edesc->key_dma;
		dsa_desc->f_dma = edesc->f_dma;
		dsa_desc->c_dma = edesc->c_dma;
		dsa_desc->d_dma = edesc->d_dma;
		dsa_desc->op = CMD_OPERATION | OP_TYPE_UNI_PROTOCOL |
		    OP_PCLID_DSASIGN;
		desc = dsa_desc;
	}
#ifdef CAAM_DEBUG
	buf = desc;
	pr_debug("DSA Descriptor is:");
	for (i = 0; i < desc_size; i++)
		pr_debug("[%d] %x ", i, buf[i]);
	pr_debug("\n");
#endif

	return desc;
}

/* DSA/ECDSA/DH/ECDH keygen CAAM descriptor */
void *caam_keygen_desc(struct dsa_edesc_s *edesc)
{
	u32 start_idx, desc_size;
	void *desc;
#ifdef CAAM_DEBUG
	uint32_t i;
	uint32_t *buf;
#endif

	if (edesc->req_type == ECC_KEYGEN) {
		struct ecc_keygen_desc_s *ecc_desc =
		    (struct ecc_keygen_desc_s *)edesc->hw_desc;
		desc_size = sizeof(struct ecc_keygen_desc_s) / sizeof(u32);
		start_idx = desc_size - 1;
		start_idx &= HDR_START_IDX_MASK;
		init_job_desc(edesc->hw_desc,
			      (start_idx << HDR_START_IDX_SHIFT) |
			      (start_idx & HDR_DESCLEN_MASK) | HDR_ONE);
		ecc_desc->sgf_ln = (edesc->l_len << DSA_PDB_L_SHIFT) |
				   (edesc->n_len & DSA_PDB_N_MASK);
		if (edesc->erratum_A_006899) {
			ecc_desc->sgf_ln |= DSA_PDB_SGF_G;
			ecc_desc->g_dma = edesc->g_sg_dma;
		} else {
			ecc_desc->g_dma = edesc->g_dma;
		}
		ecc_desc->q_dma = edesc->q_dma;
		ecc_desc->r_dma = edesc->r_dma;
		ecc_desc->s_dma = edesc->s_dma;
		ecc_desc->w_dma = edesc->key_dma;
		ecc_desc->ab_dma = edesc->ab_dma;
		ecc_desc->op = CMD_OPERATION | OP_TYPE_UNI_PROTOCOL |
		    OP_PCLID_PUBLICKEYPAIR | OP_PCL_PKPROT_ECC;
		if (edesc->curve_type == ECC_BINARY)
			ecc_desc->op |= OP_PCL_PKPROT_F2M;

		desc = ecc_desc;
	} else {
		struct dlc_keygen_desc_s *key_desc =
		    (struct dlc_keygen_desc_s *)edesc->hw_desc;
		desc_size = sizeof(struct dlc_keygen_desc_s) / sizeof(u32);
		start_idx = desc_size - 1;
		start_idx &= HDR_START_IDX_MASK;
		init_job_desc(edesc->hw_desc,
			      (start_idx << HDR_START_IDX_SHIFT) |
			      (start_idx & HDR_DESCLEN_MASK) | HDR_ONE);
		key_desc->sgf_ln = (edesc->l_len << DSA_PDB_L_SHIFT) |
			((edesc->n_len & DSA_PDB_N_MASK));
		key_desc->q_dma = edesc->q_dma;
		key_desc->r_dma = edesc->r_dma;
		key_desc->g_dma = edesc->g_dma;
		key_desc->s_dma = edesc->s_dma;
		key_desc->w_dma = edesc->key_dma;
		key_desc->op = CMD_OPERATION | OP_TYPE_UNI_PROTOCOL |
		    OP_PCLID_PUBLICKEYPAIR;
		desc = key_desc;
	}
#ifdef CAAM_DEBUG
	buf = desc;
	pr_debug("DSA Keygen Descriptor is:");
	for (i = 0; i < desc_size; i++)
		pr_debug("[%d] %x ", i, buf[i]);
	pr_debug("\n");
#endif

	return desc;
}

/* DSA verify CAAM descriptor */
void *caam_dsa_verify_desc(struct dsa_edesc_s *edesc)
{
	u32 start_idx, desc_size;
	void *desc;
#ifdef CAAM_DEBUG
	uint32_t i;
	uint32_t *buf;
#endif

	if (edesc->req_type == ECDSA_VERIFY) {
		struct ecdsa_verify_desc_s *ecdsa_desc =
		    (struct ecdsa_verify_desc_s *)edesc->hw_desc;
		desc_size = sizeof(struct ecdsa_verify_desc_s) / sizeof(u32);
		start_idx = desc_size - 1;
		start_idx &= HDR_START_IDX_MASK;
		init_job_desc(edesc->hw_desc,
			      (start_idx << HDR_START_IDX_SHIFT) |
			      (start_idx & HDR_DESCLEN_MASK) | HDR_ONE);
		ecdsa_desc->sgf_ln = (edesc->l_len << DSA_PDB_L_SHIFT) |
			((edesc->n_len & DSA_PDB_N_MASK));
		ecdsa_desc->q_dma = edesc->q_dma;
		ecdsa_desc->r_dma = edesc->r_dma;
		ecdsa_desc->g_dma = edesc->g_dma;
		ecdsa_desc->w_dma = edesc->key_dma;
		ecdsa_desc->f_dma = edesc->f_dma;
		ecdsa_desc->c_dma = edesc->c_dma;
		ecdsa_desc->d_dma = edesc->d_dma;
		ecdsa_desc->tmp_dma = edesc->tmp_dma;
		ecdsa_desc->ab_dma = edesc->ab_dma;
		ecdsa_desc->op = CMD_OPERATION | OP_TYPE_UNI_PROTOCOL |
		    OP_PCLID_DSAVERIFY | OP_PCL_PKPROT_ECC;
		if (edesc->curve_type == ECC_BINARY)
			ecdsa_desc->op |= OP_PCL_PKPROT_F2M;
		desc = ecdsa_desc;
	} else {
		struct dsa_verify_desc_s *dsa_desc =
		    (struct dsa_verify_desc_s *)edesc->hw_desc;
		desc_size = sizeof(struct dsa_verify_desc_s) / sizeof(u32);
		start_idx = desc_size - 1;
		start_idx &= HDR_START_IDX_MASK;
		init_job_desc(edesc->hw_desc,
			      (start_idx << HDR_START_IDX_SHIFT) |
			      (start_idx & HDR_DESCLEN_MASK) | HDR_ONE);
		dsa_desc->sgf_ln = (edesc->l_len << DSA_PDB_L_SHIFT) |
			((edesc->n_len & DSA_PDB_N_MASK));
		dsa_desc->q_dma = edesc->q_dma;
		dsa_desc->r_dma = edesc->r_dma;
		dsa_desc->g_dma = edesc->g_dma;
		dsa_desc->w_dma = edesc->key_dma;
		dsa_desc->f_dma = edesc->f_dma;
		dsa_desc->c_dma = edesc->c_dma;
		dsa_desc->d_dma = edesc->d_dma;
		dsa_desc->tmp_dma = edesc->tmp_dma;
		dsa_desc->op = CMD_OPERATION | OP_TYPE_UNI_PROTOCOL |
		    OP_PCLID_DSAVERIFY;
		desc = dsa_desc;
	}
#ifdef CAAM_DEBUG
	buf = desc;
	pr_debug("DSA Descriptor is:\n");
	for (i = 0; i < desc_size; i++)
		pr_debug("[%d] %x\n", i, buf[i]);
#endif
	return desc;
}