xref: /titanic_41/usr/src/uts/common/crypto/io/dca_rng.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
1*88f8b78aSgm89044 /*
2*88f8b78aSgm89044  * CDDL HEADER START
3*88f8b78aSgm89044  *
4*88f8b78aSgm89044  * The contents of this file are subject to the terms of the
5*88f8b78aSgm89044  * Common Development and Distribution License (the "License").
6*88f8b78aSgm89044  * You may not use this file except in compliance with the License.
7*88f8b78aSgm89044  *
8*88f8b78aSgm89044  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*88f8b78aSgm89044  * or http://www.opensolaris.org/os/licensing.
10*88f8b78aSgm89044  * See the License for the specific language governing permissions
11*88f8b78aSgm89044  * and limitations under the License.
12*88f8b78aSgm89044  *
13*88f8b78aSgm89044  * When distributing Covered Code, include this CDDL HEADER in each
14*88f8b78aSgm89044  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*88f8b78aSgm89044  * If applicable, add the following below this CDDL HEADER, with the
16*88f8b78aSgm89044  * fields enclosed by brackets "[]" replaced with your own identifying
17*88f8b78aSgm89044  * information: Portions Copyright [yyyy] [name of copyright owner]
18*88f8b78aSgm89044  *
19*88f8b78aSgm89044  * CDDL HEADER END
20*88f8b78aSgm89044  */
21*88f8b78aSgm89044 
22*88f8b78aSgm89044 /*
23*88f8b78aSgm89044  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*88f8b78aSgm89044  * Use is subject to license terms.
25*88f8b78aSgm89044  */
26*88f8b78aSgm89044 
27*88f8b78aSgm89044 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*88f8b78aSgm89044 
29*88f8b78aSgm89044 /*
30*88f8b78aSgm89044  * Deimos - cryptographic acceleration based upon Broadcom 582x.
31*88f8b78aSgm89044  */
32*88f8b78aSgm89044 
33*88f8b78aSgm89044 #include <sys/types.h>
34*88f8b78aSgm89044 #include <sys/ddi.h>
35*88f8b78aSgm89044 #include <sys/sunddi.h>
36*88f8b78aSgm89044 #include <sys/kmem.h>
37*88f8b78aSgm89044 #include <sys/crypto/dca.h>
38*88f8b78aSgm89044 #include <sys/atomic.h>
39*88f8b78aSgm89044 
40*88f8b78aSgm89044 /*
41*88f8b78aSgm89044  * Random number implementation.
42*88f8b78aSgm89044  */
43*88f8b78aSgm89044 
44*88f8b78aSgm89044 static int dca_rngstart(dca_t *, dca_request_t *);
45*88f8b78aSgm89044 static void dca_rngdone(dca_request_t *, int);
46*88f8b78aSgm89044 
47*88f8b78aSgm89044 static void dca_random_done();
48*88f8b78aSgm89044 int dca_random_buffer(dca_t *dca, caddr_t buf, int len);
49*88f8b78aSgm89044 int dca_random_init();
50*88f8b78aSgm89044 void dca_random_fini();
51*88f8b78aSgm89044 
52*88f8b78aSgm89044 int
dca_rng(dca_t * dca,uchar_t * buf,size_t len,crypto_req_handle_t req)53*88f8b78aSgm89044 dca_rng(dca_t *dca, uchar_t *buf, size_t len, crypto_req_handle_t req)
54*88f8b78aSgm89044 {
55*88f8b78aSgm89044 	dca_request_t	*reqp;
56*88f8b78aSgm89044 	int		rv;
57*88f8b78aSgm89044 	crypto_data_t	*data;
58*88f8b78aSgm89044 
59*88f8b78aSgm89044 	if ((reqp = dca_getreq(dca, MCR2, 1)) == NULL) {
60*88f8b78aSgm89044 		dca_error(dca, "unable to allocate request for RNG");
61*88f8b78aSgm89044 		return (CRYPTO_HOST_MEMORY);
62*88f8b78aSgm89044 	}
63*88f8b78aSgm89044 
64*88f8b78aSgm89044 	reqp->dr_kcf_req = req;
65*88f8b78aSgm89044 
66*88f8b78aSgm89044 	data = &reqp->dr_ctx.in_dup;
67*88f8b78aSgm89044 	data->cd_format = CRYPTO_DATA_RAW;
68*88f8b78aSgm89044 	data->cd_offset = 0;
69*88f8b78aSgm89044 	data->cd_length = 0;
70*88f8b78aSgm89044 	data->cd_raw.iov_base = (char *)buf;
71*88f8b78aSgm89044 	data->cd_raw.iov_len = len;
72*88f8b78aSgm89044 	reqp->dr_out = data;
73*88f8b78aSgm89044 	reqp->dr_in = NULL;
74*88f8b78aSgm89044 
75*88f8b78aSgm89044 	rv = dca_rngstart(dca, reqp);
76*88f8b78aSgm89044 	if (rv != CRYPTO_QUEUED) {
77*88f8b78aSgm89044 		if (reqp->destroy)
78*88f8b78aSgm89044 			dca_destroyreq(reqp);
79*88f8b78aSgm89044 		else
80*88f8b78aSgm89044 			dca_freereq(reqp);
81*88f8b78aSgm89044 	}
82*88f8b78aSgm89044 	return (rv);
83*88f8b78aSgm89044 }
84*88f8b78aSgm89044 
85*88f8b78aSgm89044 int
dca_rngstart(dca_t * dca,dca_request_t * reqp)86*88f8b78aSgm89044 dca_rngstart(dca_t *dca, dca_request_t *reqp)
87*88f8b78aSgm89044 {
88*88f8b78aSgm89044 	uint16_t	cmd;
89*88f8b78aSgm89044 	size_t		len;
90*88f8b78aSgm89044 	uint16_t	chunk;
91*88f8b78aSgm89044 	crypto_data_t	*out = reqp->dr_out;
92*88f8b78aSgm89044 
93*88f8b78aSgm89044 	if (dca->dca_flags & DCA_RNGSHA1) {
94*88f8b78aSgm89044 		reqp->dr_job_stat = DS_RNGSHA1JOBS;
95*88f8b78aSgm89044 		reqp->dr_byte_stat = DS_RNGSHA1BYTES;
96*88f8b78aSgm89044 		cmd = CMD_RNGSHA1;
97*88f8b78aSgm89044 	} else {
98*88f8b78aSgm89044 		reqp->dr_job_stat = DS_RNGJOBS;
99*88f8b78aSgm89044 		reqp->dr_byte_stat = DS_RNGBYTES;
100*88f8b78aSgm89044 		cmd = CMD_RNGDIRECT;
101*88f8b78aSgm89044 	}
102*88f8b78aSgm89044 
103*88f8b78aSgm89044 	len = out->cd_raw.iov_len - out->cd_length;
104*88f8b78aSgm89044 	len = min(len, MAXPACKET & ~0xf);
105*88f8b78aSgm89044 	chunk = ROUNDUP(len, sizeof (uint32_t));
106*88f8b78aSgm89044 
107*88f8b78aSgm89044 	if ((len < dca_mindma) ||
108*88f8b78aSgm89044 	    dca_sgcheck(dca, reqp->dr_out, DCA_SG_WALIGN)) {
109*88f8b78aSgm89044 		reqp->dr_flags |= DR_SCATTER;
110*88f8b78aSgm89044 	}
111*88f8b78aSgm89044 
112*88f8b78aSgm89044 	/* Try to do direct DMA. */
113*88f8b78aSgm89044 	if (!(reqp->dr_flags & DR_SCATTER)) {
114*88f8b78aSgm89044 		if (dca_bindchains(reqp, 0, len) != DDI_SUCCESS) {
115*88f8b78aSgm89044 			return (CRYPTO_DEVICE_ERROR);
116*88f8b78aSgm89044 		}
117*88f8b78aSgm89044 	}
118*88f8b78aSgm89044 
119*88f8b78aSgm89044 	reqp->dr_in_paddr = 0;
120*88f8b78aSgm89044 	reqp->dr_in_next = 0;
121*88f8b78aSgm89044 	reqp->dr_in_len = 0;
122*88f8b78aSgm89044 
123*88f8b78aSgm89044 	/*
124*88f8b78aSgm89044 	 * Setup for scattering the result back out
125*88f8b78aSgm89044 	 * Using the pre-mapped buffers to store random numbers. Since the
126*88f8b78aSgm89044 	 * data buffer is a linked list, we need to transfer its head to MCR
127*88f8b78aSgm89044 	 */
128*88f8b78aSgm89044 	if (reqp->dr_flags & DR_SCATTER) {
129*88f8b78aSgm89044 		reqp->dr_out_paddr = reqp->dr_obuf_head.dc_buffer_paddr;
130*88f8b78aSgm89044 		reqp->dr_out_next = reqp->dr_obuf_head.dc_next_paddr;
131*88f8b78aSgm89044 		if (chunk > reqp->dr_obuf_head.dc_buffer_length)
132*88f8b78aSgm89044 			reqp->dr_out_len = reqp->dr_obuf_head.dc_buffer_length;
133*88f8b78aSgm89044 		else
134*88f8b78aSgm89044 			reqp->dr_out_len = chunk;
135*88f8b78aSgm89044 	}
136*88f8b78aSgm89044 	reqp->dr_param.dp_rng.dr_chunklen = len;
137*88f8b78aSgm89044 	reqp->dr_pkt_length = (uint16_t)chunk;
138*88f8b78aSgm89044 	reqp->dr_callback = dca_rngdone;
139*88f8b78aSgm89044 
140*88f8b78aSgm89044 	/* write out the context structure */
141*88f8b78aSgm89044 	PUTCTX16(reqp, CTX_LENGTH, CTX_RNG_LENGTH);
142*88f8b78aSgm89044 	PUTCTX16(reqp, CTX_CMD, cmd);
143*88f8b78aSgm89044 
144*88f8b78aSgm89044 	/* schedule the work by doing a submit */
145*88f8b78aSgm89044 	return (dca_start(dca, reqp, MCR2, 1));
146*88f8b78aSgm89044 }
147*88f8b78aSgm89044 
148*88f8b78aSgm89044 void
dca_rngdone(dca_request_t * reqp,int errno)149*88f8b78aSgm89044 dca_rngdone(dca_request_t *reqp, int errno)
150*88f8b78aSgm89044 {
151*88f8b78aSgm89044 	if (errno == CRYPTO_SUCCESS) {
152*88f8b78aSgm89044 
153*88f8b78aSgm89044 		if (reqp->dr_flags & DR_SCATTER) {
154*88f8b78aSgm89044 			(void) ddi_dma_sync(reqp->dr_obuf_dmah, 0,
155*88f8b78aSgm89044 				reqp->dr_out_len, DDI_DMA_SYNC_FORKERNEL);
156*88f8b78aSgm89044 			if (dca_check_dma_handle(reqp->dr_dca,
157*88f8b78aSgm89044 			    reqp->dr_obuf_dmah, DCA_FM_ECLASS_NONE) !=
158*88f8b78aSgm89044 			    DDI_SUCCESS) {
159*88f8b78aSgm89044 				reqp->destroy = TRUE;
160*88f8b78aSgm89044 				errno = CRYPTO_DEVICE_ERROR;
161*88f8b78aSgm89044 				goto errout;
162*88f8b78aSgm89044 			}
163*88f8b78aSgm89044 			errno = dca_scatter(reqp->dr_obuf_kaddr,
164*88f8b78aSgm89044 			    reqp->dr_out, reqp->dr_param.dp_rng.dr_chunklen, 0);
165*88f8b78aSgm89044 			if (errno != CRYPTO_SUCCESS) {
166*88f8b78aSgm89044 				goto errout;
167*88f8b78aSgm89044 			}
168*88f8b78aSgm89044 		} else {
169*88f8b78aSgm89044 			reqp->dr_out->cd_length +=
170*88f8b78aSgm89044 			    reqp->dr_param.dp_rng.dr_chunklen;
171*88f8b78aSgm89044 		}
172*88f8b78aSgm89044 
173*88f8b78aSgm89044 		/*
174*88f8b78aSgm89044 		 * If there is more to do, then reschedule another
175*88f8b78aSgm89044 		 * pass.
176*88f8b78aSgm89044 		 */
177*88f8b78aSgm89044 		if (reqp->dr_out->cd_length < reqp->dr_out->cd_raw.iov_len) {
178*88f8b78aSgm89044 			errno = dca_rngstart(reqp->dr_dca, reqp);
179*88f8b78aSgm89044 			if (errno == CRYPTO_QUEUED) {
180*88f8b78aSgm89044 				return;
181*88f8b78aSgm89044 			}
182*88f8b78aSgm89044 		}
183*88f8b78aSgm89044 	}
184*88f8b78aSgm89044 
185*88f8b78aSgm89044 errout:
186*88f8b78aSgm89044 
187*88f8b78aSgm89044 	if (reqp->dr_kcf_req) {
188*88f8b78aSgm89044 		/* notify framework that request is completed */
189*88f8b78aSgm89044 		crypto_op_notification(reqp->dr_kcf_req, errno);
190*88f8b78aSgm89044 	} else {
191*88f8b78aSgm89044 		/* For internal random number generation */
192*88f8b78aSgm89044 		dca_random_done(reqp->dr_dca);
193*88f8b78aSgm89044 	}
194*88f8b78aSgm89044 
195*88f8b78aSgm89044 	DBG(NULL, DINTR,
196*88f8b78aSgm89044 	    "dca_rngdone: returning %d to the kef via crypto_op_notification",
197*88f8b78aSgm89044 	    errno);
198*88f8b78aSgm89044 	if (reqp->destroy)
199*88f8b78aSgm89044 		dca_destroyreq(reqp);
200*88f8b78aSgm89044 	else
201*88f8b78aSgm89044 		dca_freereq(reqp);
202*88f8b78aSgm89044 }
203*88f8b78aSgm89044 
204*88f8b78aSgm89044 /*
205*88f8b78aSgm89044  * This gives a 32k random bytes per buffer. The two buffers will switch back
206*88f8b78aSgm89044  * and forth. When a buffer is used up, a request will be submitted to refill
207*88f8b78aSgm89044  * this buffer before switching to the other one
208*88f8b78aSgm89044  */
209*88f8b78aSgm89044 
210*88f8b78aSgm89044 #define	RANDOM_BUFFER_SIZE		(1<<15)
211*88f8b78aSgm89044 #define	DCA_RANDOM_MAX_WAIT		10000
212*88f8b78aSgm89044 
213*88f8b78aSgm89044 int
dca_random_init(dca_t * dca)214*88f8b78aSgm89044 dca_random_init(dca_t *dca)
215*88f8b78aSgm89044 {
216*88f8b78aSgm89044 	/* Mutex for the local random number pool */
217*88f8b78aSgm89044 	mutex_init(&dca->dca_random_lock, NULL, MUTEX_DRIVER, NULL);
218*88f8b78aSgm89044 
219*88f8b78aSgm89044 	if ((dca->dca_buf1 = kmem_alloc(RANDOM_BUFFER_SIZE, KM_SLEEP)) ==
220*88f8b78aSgm89044 	    NULL) {
221*88f8b78aSgm89044 		mutex_destroy(&dca->dca_random_lock);
222*88f8b78aSgm89044 		return (CRYPTO_FAILED);
223*88f8b78aSgm89044 	}
224*88f8b78aSgm89044 
225*88f8b78aSgm89044 	if ((dca->dca_buf2 = kmem_alloc(RANDOM_BUFFER_SIZE, KM_SLEEP)) ==
226*88f8b78aSgm89044 	    NULL) {
227*88f8b78aSgm89044 		mutex_destroy(&dca->dca_random_lock);
228*88f8b78aSgm89044 		kmem_free(dca->dca_buf1, RANDOM_BUFFER_SIZE);
229*88f8b78aSgm89044 		return (CRYPTO_FAILED);
230*88f8b78aSgm89044 	}
231*88f8b78aSgm89044 
232*88f8b78aSgm89044 	return (CRYPTO_SUCCESS);
233*88f8b78aSgm89044 }
234*88f8b78aSgm89044 
235*88f8b78aSgm89044 void
dca_random_fini(dca_t * dca)236*88f8b78aSgm89044 dca_random_fini(dca_t *dca)
237*88f8b78aSgm89044 {
238*88f8b78aSgm89044 	kmem_free(dca->dca_buf1, RANDOM_BUFFER_SIZE);
239*88f8b78aSgm89044 	kmem_free(dca->dca_buf2, RANDOM_BUFFER_SIZE);
240*88f8b78aSgm89044 	dca->dca_buf1 = dca->dca_buf2 = dca->dca_buf_ptr = NULL;
241*88f8b78aSgm89044 	(void) mutex_destroy(&dca->dca_random_lock);
242*88f8b78aSgm89044 }
243*88f8b78aSgm89044 
244*88f8b78aSgm89044 int
dca_random_buffer(dca_t * dca,caddr_t buf,int len)245*88f8b78aSgm89044 dca_random_buffer(dca_t *dca, caddr_t buf, int len)
246*88f8b78aSgm89044 {
247*88f8b78aSgm89044 	int rv;
248*88f8b78aSgm89044 	int i, j;
249*88f8b78aSgm89044 	char *fill_buf;
250*88f8b78aSgm89044 
251*88f8b78aSgm89044 	mutex_enter(&dca->dca_random_lock);
252*88f8b78aSgm89044 
253*88f8b78aSgm89044 	if (dca->dca_buf_ptr == NULL) {
254*88f8b78aSgm89044 		if (dca->dca_buf1 == NULL || dca->dca_buf2 == NULL) {
255*88f8b78aSgm89044 			mutex_exit(&dca->dca_random_lock);
256*88f8b78aSgm89044 			return (CRYPTO_FAILED);
257*88f8b78aSgm89044 		}
258*88f8b78aSgm89044 
259*88f8b78aSgm89044 		/* Very first time. Let us fill the first buffer */
260*88f8b78aSgm89044 		if (dca_rng(dca, (uchar_t *)dca->dca_buf1, RANDOM_BUFFER_SIZE,
261*88f8b78aSgm89044 		    NULL) != CRYPTO_QUEUED) {
262*88f8b78aSgm89044 			mutex_exit(&dca->dca_random_lock);
263*88f8b78aSgm89044 			return (CRYPTO_FAILED);
264*88f8b78aSgm89044 		}
265*88f8b78aSgm89044 
266*88f8b78aSgm89044 		atomic_or_32(&dca->dca_random_filling, 0x1);
267*88f8b78aSgm89044 
268*88f8b78aSgm89044 		/* Pretend we are using buffer2 and it is empty */
269*88f8b78aSgm89044 		dca->dca_buf_ptr = dca->dca_buf2;
270*88f8b78aSgm89044 		dca->dca_index = RANDOM_BUFFER_SIZE;
271*88f8b78aSgm89044 	}
272*88f8b78aSgm89044 
273*88f8b78aSgm89044 	i = 0;
274*88f8b78aSgm89044 	while (i < len) {
275*88f8b78aSgm89044 		if (dca->dca_index >= RANDOM_BUFFER_SIZE) {
276*88f8b78aSgm89044 			j = 0;
277*88f8b78aSgm89044 			while (dca->dca_random_filling) {
278*88f8b78aSgm89044 				/* Only wait here at the first time */
279*88f8b78aSgm89044 				delay(drv_usectohz(100));
280*88f8b78aSgm89044 				if (j++ >= DCA_RANDOM_MAX_WAIT)
281*88f8b78aSgm89044 					break;
282*88f8b78aSgm89044 			}
283*88f8b78aSgm89044 			DBG(NULL, DENTRY, "dca_random_buffer: j: %d", j);
284*88f8b78aSgm89044 			if (j > DCA_RANDOM_MAX_WAIT) {
285*88f8b78aSgm89044 				mutex_exit(&dca->dca_random_lock);
286*88f8b78aSgm89044 				return (CRYPTO_FAILED);
287*88f8b78aSgm89044 			}
288*88f8b78aSgm89044 
289*88f8b78aSgm89044 			/* switch to the other buffer */
290*88f8b78aSgm89044 			if (dca->dca_buf_ptr == dca->dca_buf1) {
291*88f8b78aSgm89044 				dca->dca_buf_ptr = dca->dca_buf2;
292*88f8b78aSgm89044 				fill_buf = dca->dca_buf1;
293*88f8b78aSgm89044 			} else {
294*88f8b78aSgm89044 				dca->dca_buf_ptr = dca->dca_buf1;
295*88f8b78aSgm89044 				fill_buf = dca->dca_buf2;
296*88f8b78aSgm89044 			}
297*88f8b78aSgm89044 
298*88f8b78aSgm89044 			atomic_or_32(&dca->dca_random_filling, 0x1);
299*88f8b78aSgm89044 			dca->dca_index = 0;
300*88f8b78aSgm89044 
301*88f8b78aSgm89044 			if ((rv = dca_rng(dca, (uchar_t *)fill_buf,
302*88f8b78aSgm89044 			    RANDOM_BUFFER_SIZE, NULL)) != CRYPTO_QUEUED) {
303*88f8b78aSgm89044 				mutex_exit(&dca->dca_random_lock);
304*88f8b78aSgm89044 				return (rv);
305*88f8b78aSgm89044 			}
306*88f8b78aSgm89044 		}
307*88f8b78aSgm89044 
308*88f8b78aSgm89044 		if (dca->dca_buf_ptr[dca->dca_index] != '\0')
309*88f8b78aSgm89044 			buf[i++] = dca->dca_buf_ptr[dca->dca_index];
310*88f8b78aSgm89044 
311*88f8b78aSgm89044 		dca->dca_index++;
312*88f8b78aSgm89044 	}
313*88f8b78aSgm89044 
314*88f8b78aSgm89044 	mutex_exit(&dca->dca_random_lock);
315*88f8b78aSgm89044 
316*88f8b78aSgm89044 	DBG(NULL, DENTRY, "dca_random_buffer: i: %d", i);
317*88f8b78aSgm89044 	return (CRYPTO_SUCCESS);
318*88f8b78aSgm89044 }
319*88f8b78aSgm89044 
320*88f8b78aSgm89044 static void
dca_random_done(dca_t * dca)321*88f8b78aSgm89044 dca_random_done(dca_t *dca)
322*88f8b78aSgm89044 {
323*88f8b78aSgm89044 	DBG(NULL, DENTRY, "dca_random_done");
324*88f8b78aSgm89044 	atomic_and_32(&dca->dca_random_filling, 0x0);
325*88f8b78aSgm89044 }
326