summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/crypto/io/dca_rng.c
blob: bfb56cdf0ee61d39895397bf4d4ffd782492a84f (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Deimos - cryptographic acceleration based upon Broadcom 582x.
 */

#include <sys/types.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/kmem.h>
#include <sys/crypto/dca.h>
#include <sys/atomic.h>

/*
 * Random number implementation.
 */

static int dca_rngstart(dca_t *, dca_request_t *);
static void dca_rngdone(dca_request_t *, int);

static void dca_random_done();
int dca_random_buffer(dca_t *dca, caddr_t buf, int len);
int dca_random_init();
void dca_random_fini();

int
dca_rng(dca_t *dca, uchar_t *buf, size_t len, crypto_req_handle_t req)
{
	dca_request_t	*reqp;
	int		rv;
	crypto_data_t	*data;

	if ((reqp = dca_getreq(dca, MCR2, 1)) == NULL) {
		dca_error(dca, "unable to allocate request for RNG");
		return (CRYPTO_HOST_MEMORY);
	}

	reqp->dr_kcf_req = req;

	data = &reqp->dr_ctx.in_dup;
	data->cd_format = CRYPTO_DATA_RAW;
	data->cd_offset = 0;
	data->cd_length = 0;
	data->cd_raw.iov_base = (char *)buf;
	data->cd_raw.iov_len = len;
	reqp->dr_out = data;
	reqp->dr_in = NULL;

	rv = dca_rngstart(dca, reqp);
	if (rv != CRYPTO_QUEUED) {
		if (reqp->destroy)
			dca_destroyreq(reqp);
		else
			dca_freereq(reqp);
	}
	return (rv);
}

int
dca_rngstart(dca_t *dca, dca_request_t *reqp)
{
	uint16_t	cmd;
	size_t		len;
	uint16_t	chunk;
	crypto_data_t	*out = reqp->dr_out;

	if (dca->dca_flags & DCA_RNGSHA1) {
		reqp->dr_job_stat = DS_RNGSHA1JOBS;
		reqp->dr_byte_stat = DS_RNGSHA1BYTES;
		cmd = CMD_RNGSHA1;
	} else {
		reqp->dr_job_stat = DS_RNGJOBS;
		reqp->dr_byte_stat = DS_RNGBYTES;
		cmd = CMD_RNGDIRECT;
	}

	len = out->cd_raw.iov_len - out->cd_length;
	len = min(len, MAXPACKET & ~0xf);
	chunk = ROUNDUP(len, sizeof (uint32_t));

	if ((len < dca_mindma) ||
	    dca_sgcheck(dca, reqp->dr_out, DCA_SG_WALIGN)) {
		reqp->dr_flags |= DR_SCATTER;
	}

	/* Try to do direct DMA. */
	if (!(reqp->dr_flags & DR_SCATTER)) {
		if (dca_bindchains(reqp, 0, len) != DDI_SUCCESS) {
			return (CRYPTO_DEVICE_ERROR);
		}
	}

	reqp->dr_in_paddr = 0;
	reqp->dr_in_next = 0;
	reqp->dr_in_len = 0;

	/*
	 * Setup for scattering the result back out
	 * Using the pre-mapped buffers to store random numbers. Since the
	 * data buffer is a linked list, we need to transfer its head to MCR
	 */
	if (reqp->dr_flags & DR_SCATTER) {
		reqp->dr_out_paddr = reqp->dr_obuf_head.dc_buffer_paddr;
		reqp->dr_out_next = reqp->dr_obuf_head.dc_next_paddr;
		if (chunk > reqp->dr_obuf_head.dc_buffer_length)
			reqp->dr_out_len = reqp->dr_obuf_head.dc_buffer_length;
		else
			reqp->dr_out_len = chunk;
	}
	reqp->dr_param.dp_rng.dr_chunklen = len;
	reqp->dr_pkt_length = (uint16_t)chunk;
	reqp->dr_callback = dca_rngdone;

	/* write out the context structure */
	PUTCTX16(reqp, CTX_LENGTH, CTX_RNG_LENGTH);
	PUTCTX16(reqp, CTX_CMD, cmd);

	/* schedule the work by doing a submit */
	return (dca_start(dca, reqp, MCR2, 1));
}

void
dca_rngdone(dca_request_t *reqp, int errno)
{
	if (errno == CRYPTO_SUCCESS) {

		if (reqp->dr_flags & DR_SCATTER) {
			(void) ddi_dma_sync(reqp->dr_obuf_dmah, 0,
				reqp->dr_out_len, DDI_DMA_SYNC_FORKERNEL);
			if (dca_check_dma_handle(reqp->dr_dca,
			    reqp->dr_obuf_dmah, DCA_FM_ECLASS_NONE) !=
			    DDI_SUCCESS) {
				reqp->destroy = TRUE;
				errno = CRYPTO_DEVICE_ERROR;
				goto errout;
			}
			errno = dca_scatter(reqp->dr_obuf_kaddr,
			    reqp->dr_out, reqp->dr_param.dp_rng.dr_chunklen, 0);
			if (errno != CRYPTO_SUCCESS) {
				goto errout;
			}
		} else {
			reqp->dr_out->cd_length +=
			    reqp->dr_param.dp_rng.dr_chunklen;
		}

		/*
		 * If there is more to do, then reschedule another
		 * pass.
		 */
		if (reqp->dr_out->cd_length < reqp->dr_out->cd_raw.iov_len) {
			errno = dca_rngstart(reqp->dr_dca, reqp);
			if (errno == CRYPTO_QUEUED) {
				return;
			}
		}
	}

errout:

	if (reqp->dr_kcf_req) {
		/* notify framework that request is completed */
		crypto_op_notification(reqp->dr_kcf_req, errno);
	} else {
		/* For internal random number generation */
		dca_random_done(reqp->dr_dca);
	}

	DBG(NULL, DINTR,
	    "dca_rngdone: returning %d to the kef via crypto_op_notification",
	    errno);
	if (reqp->destroy)
		dca_destroyreq(reqp);
	else
		dca_freereq(reqp);
}

/*
 * This gives a 32k random bytes per buffer. The two buffers will switch back
 * and forth. When a buffer is used up, a request will be submitted to refill
 * this buffer before switching to the other one
 */

#define	RANDOM_BUFFER_SIZE		(1<<15)
#define	DCA_RANDOM_MAX_WAIT		10000

int
dca_random_init(dca_t *dca)
{
	/* Mutex for the local random number pool */
	mutex_init(&dca->dca_random_lock, NULL, MUTEX_DRIVER, NULL);

	if ((dca->dca_buf1 = kmem_alloc(RANDOM_BUFFER_SIZE, KM_SLEEP)) ==
	    NULL) {
		mutex_destroy(&dca->dca_random_lock);
		return (CRYPTO_FAILED);
	}

	if ((dca->dca_buf2 = kmem_alloc(RANDOM_BUFFER_SIZE, KM_SLEEP)) ==
	    NULL) {
		mutex_destroy(&dca->dca_random_lock);
		kmem_free(dca->dca_buf1, RANDOM_BUFFER_SIZE);
		return (CRYPTO_FAILED);
	}

	return (CRYPTO_SUCCESS);
}

void
dca_random_fini(dca_t *dca)
{
	kmem_free(dca->dca_buf1, RANDOM_BUFFER_SIZE);
	kmem_free(dca->dca_buf2, RANDOM_BUFFER_SIZE);
	dca->dca_buf1 = dca->dca_buf2 = dca->dca_buf_ptr = NULL;
	(void) mutex_destroy(&dca->dca_random_lock);
}

int
dca_random_buffer(dca_t *dca, caddr_t buf, int len)
{
	int rv;
	int i, j;
	char *fill_buf;

	mutex_enter(&dca->dca_random_lock);

	if (dca->dca_buf_ptr == NULL) {
		if (dca->dca_buf1 == NULL || dca->dca_buf2 == NULL) {
			mutex_exit(&dca->dca_random_lock);
			return (CRYPTO_FAILED);
		}

		/* Very first time. Let us fill the first buffer */
		if (dca_rng(dca, (uchar_t *)dca->dca_buf1, RANDOM_BUFFER_SIZE,
		    NULL) != CRYPTO_QUEUED) {
			mutex_exit(&dca->dca_random_lock);
			return (CRYPTO_FAILED);
		}

		atomic_or_32(&dca->dca_random_filling, 0x1);

		/* Pretend we are using buffer2 and it is empty */
		dca->dca_buf_ptr = dca->dca_buf2;
		dca->dca_index = RANDOM_BUFFER_SIZE;
	}

	i = 0;
	while (i < len) {
		if (dca->dca_index >= RANDOM_BUFFER_SIZE) {
			j = 0;
			while (dca->dca_random_filling) {
				/* Only wait here at the first time */
				delay(drv_usectohz(100));
				if (j++ >= DCA_RANDOM_MAX_WAIT)
					break;
			}
			DBG(NULL, DENTRY, "dca_random_buffer: j: %d", j);
			if (j > DCA_RANDOM_MAX_WAIT) {
				mutex_exit(&dca->dca_random_lock);
				return (CRYPTO_FAILED);
			}

			/* switch to the other buffer */
			if (dca->dca_buf_ptr == dca->dca_buf1) {
				dca->dca_buf_ptr = dca->dca_buf2;
				fill_buf = dca->dca_buf1;
			} else {
				dca->dca_buf_ptr = dca->dca_buf1;
				fill_buf = dca->dca_buf2;
			}

			atomic_or_32(&dca->dca_random_filling, 0x1);
			dca->dca_index = 0;

			if ((rv = dca_rng(dca, (uchar_t *)fill_buf,
			    RANDOM_BUFFER_SIZE, NULL)) != CRYPTO_QUEUED) {
				mutex_exit(&dca->dca_random_lock);
				return (rv);
			}
		}

		if (dca->dca_buf_ptr[dca->dca_index] != '\0')
			buf[i++] = dca->dca_buf_ptr[dca->dca_index];

		dca->dca_index++;
	}

	mutex_exit(&dca->dca_random_lock);

	DBG(NULL, DENTRY, "dca_random_buffer: i: %d", i);
	return (CRYPTO_SUCCESS);
}

static void
dca_random_done(dca_t *dca)
{
	DBG(NULL, DENTRY, "dca_random_done");
	atomic_and_32(&dca->dca_random_filling, 0x0);
}