aboutsummaryrefslogtreecommitdiff
path: root/sys/mips/cavium/cryptocteon/cryptocteon.c
blob: fee711a9939e3b35180c4da51997496f06205eef (plain) (blame)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Octeon Crypto for OCF
 *
 * Written by David McCullough <david_mccullough@securecomputing.com>
 * Copyright (C) 2009 David McCullough
 *
 * LICENSE TERMS
 *
 * The free distribution and use of this software in both source and binary
 * form is allowed (with or without changes) provided that:
 *
 *   1. distributions of this source code include the above copyright
 *      notice, this list of conditions and the following disclaimer;
 *
 *   2. distributions in binary form include the above copyright
 *      notice, this list of conditions and the following disclaimer
 *      in the documentation and/or other associated materials;
 *
 *   3. the copyright holder's name is not used to endorse products
 *      built using this software without specific written permission.
 *
 * DISCLAIMER
 *
 * This software is provided 'as is' with no explicit or implied warranties
 * in respect of its properties, including, but not limited to, correctness
 * and/or fitness for purpose.
 * ---------------------------------------------------------------------------
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/uio.h>

#include <opencrypto/cryptodev.h>

#include <contrib/octeon-sdk/cvmx.h>

#include <mips/cavium/cryptocteon/cryptocteonvar.h>

#include "cryptodev_if.h"

struct cryptocteon_softc {
	int32_t			sc_cid;		/* opencrypto id */
};

int cryptocteon_debug = 0;
TUNABLE_INT("hw.cryptocteon.debug", &cryptocteon_debug);

static void cryptocteon_identify(driver_t *, device_t);
static int cryptocteon_probe(device_t);
static int cryptocteon_attach(device_t);

static int cryptocteon_process(device_t, struct cryptop *, int);
static int cryptocteon_probesession(device_t,
    const struct crypto_session_params *);
static int cryptocteon_newsession(device_t, crypto_session_t,
    const struct crypto_session_params *);

static void
cryptocteon_identify(driver_t *drv, device_t parent)
{
	if (octeon_has_feature(OCTEON_FEATURE_CRYPTO))
		BUS_ADD_CHILD(parent, 0, "cryptocteon", 0);
}

static int
cryptocteon_probe(device_t dev)
{
	device_set_desc(dev, "Octeon Secure Coprocessor");
	return (0);
}

static int
cryptocteon_attach(device_t dev)
{
	struct cryptocteon_softc *sc;

	sc = device_get_softc(dev);

	sc->sc_cid = crypto_get_driverid(dev, sizeof(struct octo_sess),
	    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
	if (sc->sc_cid < 0) {
		device_printf(dev, "crypto_get_driverid ret %d\n", sc->sc_cid);
		return (ENXIO);
	}

	return (0);
}

static bool
cryptocteon_auth_supported(const struct crypto_session_params *csp)
{
	u_int hash_len;

	switch (csp->csp_auth_alg) {
	case CRYPTO_SHA1_HMAC:
		hash_len = SHA1_HASH_LEN;
		break;
	default:
		return (false);
	}

	if (csp->csp_auth_klen > hash_len)
		return (false);
	return (true);
}

static bool
cryptocteon_cipher_supported(const struct crypto_session_params *csp)
{

	switch (csp->csp_cipher_alg) {
	case CRYPTO_AES_CBC:
		if (csp->csp_ivlen != 16)
			return (false);
		if (csp->csp_cipher_klen != 16 &&
		    csp->csp_cipher_klen != 24 &&
		    csp->csp_cipher_klen != 32)
			return (false);
		break;
	default:
		return (false);
	}

	return (true);
}

static int
cryptocteon_probesession(device_t dev, const struct crypto_session_params *csp)
{

	if (csp->csp_flags != 0)
		return (EINVAL);
	switch (csp->csp_mode) {
	case CSP_MODE_DIGEST:
		if (!cryptocteon_auth_supported(csp))
			return (EINVAL);
		break;
	case CSP_MODE_CIPHER:
		if (!cryptocteon_cipher_supported(csp))
			return (EINVAL);
		break;
	case CSP_MODE_ETA:
		if (!cryptocteon_auth_supported(csp) ||
		    !cryptocteon_cipher_supported(csp))
			return (EINVAL);
		break;
	default:
		return (EINVAL);
	}
	return (CRYPTODEV_PROBE_ACCEL_SOFTWARE);
}

static void
cryptocteon_calc_hash(const struct crypto_session_params *csp, const char *key,
    struct octo_sess *ocd)
{
	char hash_key[SHA1_HASH_LEN];

	memset(hash_key, 0, sizeof(hash_key));
	memcpy(hash_key, key, csp->csp_auth_klen);
	octo_calc_hash(csp->csp_auth_alg == CRYPTO_SHA1_HMAC, hash_key,
	    ocd->octo_hminner, ocd->octo_hmouter);
}

/* Generate a new octo session. */
static int
cryptocteon_newsession(device_t dev, crypto_session_t cses,
    const struct crypto_session_params *csp)
{
	struct cryptocteon_softc *sc;
	struct octo_sess *ocd;

	sc = device_get_softc(dev);

	ocd = crypto_get_driver_session(cses);

	ocd->octo_encklen = csp->csp_cipher_klen;
	if (csp->csp_cipher_key != NULL)
		memcpy(ocd->octo_enckey, csp->csp_cipher_key,
		    ocd->octo_encklen);

	if (csp->csp_auth_key != NULL)
		cryptocteon_calc_hash(csp, csp->csp_auth_key, ocd);

	ocd->octo_mlen = csp->csp_auth_mlen;
	if (csp->csp_auth_mlen == 0) {
		switch (csp->csp_auth_alg) {
		case CRYPTO_SHA1_HMAC:
			ocd->octo_mlen = SHA1_HASH_LEN;
			break;
		}
	}

	switch (csp->csp_mode) {
	case CSP_MODE_DIGEST:
		switch (csp->csp_auth_alg) {
		case CRYPTO_SHA1_HMAC:
			ocd->octo_encrypt = octo_null_sha1_encrypt;
			ocd->octo_decrypt = octo_null_sha1_encrypt;
			break;
		}
		break;
	case CSP_MODE_CIPHER:
		switch (csp->csp_cipher_alg) {
		case CRYPTO_AES_CBC:
			ocd->octo_encrypt = octo_aes_cbc_encrypt;
			ocd->octo_decrypt = octo_aes_cbc_decrypt;
			break;
		}
		break;
	case CSP_MODE_ETA:
		switch (csp->csp_cipher_alg) {
		case CRYPTO_AES_CBC:
			switch (csp->csp_auth_alg) {
			case CRYPTO_SHA1_HMAC:
				ocd->octo_encrypt = octo_aes_cbc_sha1_encrypt;
				ocd->octo_decrypt = octo_aes_cbc_sha1_decrypt;
				break;
			}
			break;
		}
		break;
	}

	KASSERT(ocd->octo_encrypt != NULL && ocd->octo_decrypt != NULL,
	    ("%s: missing function pointers", __func__));

	return (0);
}

/*
 * Process a request.
 */
static int
cryptocteon_process(device_t dev, struct cryptop *crp, int hint)
{
	const struct crypto_session_params *csp;
	struct octo_sess *od;
	size_t iovcnt, iovlen;
	struct mbuf *m = NULL;
	struct uio *uiop = NULL;
	unsigned char *ivp = NULL;
	unsigned char iv_data[16];
	unsigned char icv[SHA1_HASH_LEN], icv2[SHA1_HASH_LEN];
	int auth_off, auth_len, crypt_off, crypt_len;
	struct cryptocteon_softc *sc;

	sc = device_get_softc(dev);

	crp->crp_etype = 0;

	od = crypto_get_driver_session(crp->crp_session);
	csp = crypto_get_params(crp->crp_session);

	/*
	 * The crypto routines assume that the regions to auth and
	 * cipher are exactly 8 byte multiples and aligned on 8
	 * byte logical boundaries within the iovecs.
	 */
	if (crp->crp_aad_length % 8 != 0 || crp->crp_payload_length % 8 != 0) {
		crp->crp_etype = EFBIG;
		goto done;
	}

	/*
	 * As currently written, the crypto routines assume the AAD and
	 * payload are adjacent.
	 */
	if (crp->crp_aad_length != 0 && crp->crp_payload_start !=
	    crp->crp_aad_start + crp->crp_aad_length) {
		crp->crp_etype = EFBIG;
		goto done;
	}

	crypt_off = crp->crp_payload_start;
	crypt_len = crp->crp_payload_length;
	if (crp->crp_aad_length != 0) {
		auth_off = crp->crp_aad_start;
		auth_len = crp->crp_aad_length + crp->crp_payload_length;
	} else {
		auth_off = crypt_off;
		auth_len = crypt_len;
	}

	/*
	 * do some error checking outside of the loop for m and IOV processing
	 * this leaves us with valid m or uiop pointers for later
	 */
	switch (crp->crp_buf.cb_type) {
	case CRYPTO_BUF_MBUF:
	{
		unsigned frags;

		m = crp->crp_buf.cb_mbuf;
		for (frags = 0; m != NULL; frags++)
			m = m->m_next;

		if (frags >= UIO_MAXIOV) {
			printf("%s,%d: %d frags > UIO_MAXIOV", __FILE__, __LINE__, frags);
			crp->crp_etype = EFBIG;
			goto done;
		}

		m = crp->crp_buf.cb_mbuf;
		break;
	}
	case CRYPTO_BUF_UIO:
		uiop = crp->crp_buf.cb_uio;
		if (uiop->uio_iovcnt > UIO_MAXIOV) {
			printf("%s,%d: %d uio_iovcnt > UIO_MAXIOV", __FILE__, __LINE__,
			       uiop->uio_iovcnt);
			crp->crp_etype = EFBIG;
			goto done;
		}
		break;
	}

	if (csp->csp_cipher_alg != 0) {
		if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
			ivp = crp->crp_iv;
		else {
			crypto_copydata(crp, crp->crp_iv_start, csp->csp_ivlen,
			    iv_data);
			ivp = iv_data;
		}
	}

	/*
	 * setup the I/O vector to cover the buffer
	 */
	switch (crp->crp_buf.cb_type) {
	case CRYPTO_BUF_MBUF:
		iovcnt = 0;
		iovlen = 0;

		while (m != NULL) {
			od->octo_iov[iovcnt].iov_base = mtod(m, void *);
			od->octo_iov[iovcnt].iov_len = m->m_len;

			m = m->m_next;
			iovlen += od->octo_iov[iovcnt++].iov_len;
		}
		break;
	case CRYPTO_BUF_UIO:
		iovlen = 0;
		for (iovcnt = 0; iovcnt < uiop->uio_iovcnt; iovcnt++) {
			od->octo_iov[iovcnt].iov_base = uiop->uio_iov[iovcnt].iov_base;
			od->octo_iov[iovcnt].iov_len = uiop->uio_iov[iovcnt].iov_len;

			iovlen += od->octo_iov[iovcnt].iov_len;
		}
		break;
	case CRYPTO_BUF_CONTIG:
		iovlen = crp->crp_buf.cb_buf_len;
		od->octo_iov[0].iov_base = crp->crp_buf.cb_buf;
		od->octo_iov[0].iov_len = crp->crp_buf.cb_buf_len;
		iovcnt = 1;
		break;
	default:
		panic("can't happen");
	}


	/*
	 * setup a new explicit key
	 */
	if (crp->crp_cipher_key != NULL)
		memcpy(od->octo_enckey, crp->crp_cipher_key, od->octo_encklen);
	if (crp->crp_auth_key != NULL)
		cryptocteon_calc_hash(csp, crp->crp_auth_key, od);


	if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
		(*od->octo_encrypt)(od, od->octo_iov, iovcnt, iovlen,
		    auth_off, auth_len, crypt_off, crypt_len, icv, ivp);
	else
		(*od->octo_decrypt)(od, od->octo_iov, iovcnt, iovlen,
		    auth_off, auth_len, crypt_off, crypt_len, icv, ivp);

	if (csp->csp_auth_alg != 0) {
		if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
			crypto_copydata(crp, crp->crp_digest_start,
			    od->octo_mlen, icv2);
			if (timingsafe_bcmp(icv, icv2, od->octo_mlen) != 0)
				crp->crp_etype = EBADMSG;
		} else
			crypto_copyback(crp, crp->crp_digest_start,
			    od->octo_mlen, icv);
	}
done:
	crypto_done(crp);
	return (0);
}

static device_method_t cryptocteon_methods[] = {
	/* device methods */
	DEVMETHOD(device_identify,	cryptocteon_identify),
	DEVMETHOD(device_probe,		cryptocteon_probe),
	DEVMETHOD(device_attach,	cryptocteon_attach),

	/* crypto device methods */
	DEVMETHOD(cryptodev_probesession, cryptocteon_probesession),
	DEVMETHOD(cryptodev_newsession,	cryptocteon_newsession),
	DEVMETHOD(cryptodev_process,	cryptocteon_process),

	{ 0, 0 }
};

static driver_t cryptocteon_driver = {
	"cryptocteon",
	cryptocteon_methods,
	sizeof (struct cryptocteon_softc),
};
static devclass_t cryptocteon_devclass;
DRIVER_MODULE(cryptocteon, nexus, cryptocteon_driver, cryptocteon_devclass, 0, 0);