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
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2020 Netflix, Inc
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
* redistribution must be conditioned upon including a substantially
* similar Disclaimer requirement for further binary redistribution.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGES.
*/
/*
* A driver for the OpenCrypto framework which uses assembly routines
* from OpenSSL.
*/
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <machine/fpu.h>
#include <opencrypto/cryptodev.h>
#include <opencrypto/xform_auth.h>
#include <crypto/openssl/ossl.h>
#include <crypto/openssl/ossl_chacha.h>
#include "cryptodev_if.h"
struct ossl_softc {
int32_t sc_cid;
};
struct ossl_session_hash {
struct ossl_hash_context ictx;
struct ossl_hash_context octx;
struct auth_hash *axf;
u_int mlen;
};
struct ossl_session {
struct ossl_session_hash hash;
};
static MALLOC_DEFINE(M_OSSL, "ossl", "OpenSSL crypto");
static void
ossl_identify(driver_t *driver, device_t parent)
{
if (device_find_child(parent, "ossl", -1) == NULL)
BUS_ADD_CHILD(parent, 10, "ossl", -1);
}
static int
ossl_probe(device_t dev)
{
device_set_desc(dev, "OpenSSL crypto");
return (BUS_PROBE_DEFAULT);
}
static int
ossl_attach(device_t dev)
{
struct ossl_softc *sc;
sc = device_get_softc(dev);
ossl_cpuid();
sc->sc_cid = crypto_get_driverid(dev, sizeof(struct ossl_session),
CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC |
CRYPTOCAP_F_ACCEL_SOFTWARE);
if (sc->sc_cid < 0) {
device_printf(dev, "failed to allocate crypto driver id\n");
return (ENXIO);
}
return (0);
}
static int
ossl_detach(device_t dev)
{
struct ossl_softc *sc;
sc = device_get_softc(dev);
crypto_unregister_all(sc->sc_cid);
return (0);
}
static struct auth_hash *
ossl_lookup_hash(const struct crypto_session_params *csp)
{
switch (csp->csp_auth_alg) {
case CRYPTO_SHA1:
case CRYPTO_SHA1_HMAC:
return (&ossl_hash_sha1);
case CRYPTO_SHA2_224:
case CRYPTO_SHA2_224_HMAC:
return (&ossl_hash_sha224);
case CRYPTO_SHA2_256:
case CRYPTO_SHA2_256_HMAC:
return (&ossl_hash_sha256);
case CRYPTO_SHA2_384:
case CRYPTO_SHA2_384_HMAC:
return (&ossl_hash_sha384);
case CRYPTO_SHA2_512:
case CRYPTO_SHA2_512_HMAC:
return (&ossl_hash_sha512);
case CRYPTO_POLY1305:
return (&ossl_hash_poly1305);
default:
return (NULL);
}
}
static int
ossl_probesession(device_t dev, const struct crypto_session_params *csp)
{
if ((csp->csp_flags & ~(CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD)) !=
0)
return (EINVAL);
switch (csp->csp_mode) {
case CSP_MODE_DIGEST:
if (ossl_lookup_hash(csp) == NULL)
return (EINVAL);
break;
case CSP_MODE_CIPHER:
switch (csp->csp_cipher_alg) {
case CRYPTO_CHACHA20:
if (csp->csp_cipher_klen != CHACHA_KEY_SIZE)
return (EINVAL);
break;
default:
return (EINVAL);
}
break;
case CSP_MODE_AEAD:
switch (csp->csp_cipher_alg) {
case CRYPTO_CHACHA20_POLY1305:
break;
default:
return (EINVAL);
}
break;
default:
return (EINVAL);
}
return (CRYPTODEV_PROBE_ACCEL_SOFTWARE);
}
static void
ossl_newsession_hash(struct ossl_session *s,
const struct crypto_session_params *csp)
{
struct auth_hash *axf;
axf = ossl_lookup_hash(csp);
s->hash.axf = axf;
if (csp->csp_auth_mlen == 0)
s->hash.mlen = axf->hashsize;
else
s->hash.mlen = csp->csp_auth_mlen;
if (csp->csp_auth_klen == 0) {
axf->Init(&s->hash.ictx);
} else {
if (csp->csp_auth_key != NULL) {
fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX);
if (axf->Setkey != NULL) {
axf->Init(&s->hash.ictx);
axf->Setkey(&s->hash.ictx, csp->csp_auth_key,
csp->csp_auth_klen);
} else {
hmac_init_ipad(axf, csp->csp_auth_key,
csp->csp_auth_klen, &s->hash.ictx);
hmac_init_opad(axf, csp->csp_auth_key,
csp->csp_auth_klen, &s->hash.octx);
}
fpu_kern_leave(curthread, NULL);
}
}
}
static int
ossl_newsession(device_t dev, crypto_session_t cses,
const struct crypto_session_params *csp)
{
struct ossl_session *s;
s = crypto_get_driver_session(cses);
switch (csp->csp_mode) {
case CSP_MODE_DIGEST:
ossl_newsession_hash(s, csp);
break;
}
return (0);
}
static int
ossl_process_hash(struct ossl_session *s, struct cryptop *crp,
const struct crypto_session_params *csp)
{
struct ossl_hash_context ctx;
char digest[HASH_MAX_LEN];
struct auth_hash *axf;
int error;
axf = s->hash.axf;
if (crp->crp_auth_key == NULL) {
ctx = s->hash.ictx;
} else {
if (axf->Setkey != NULL) {
axf->Init(&ctx);
axf->Setkey(&ctx, crp->crp_auth_key,
csp->csp_auth_klen);
} else {
hmac_init_ipad(axf, crp->crp_auth_key,
csp->csp_auth_klen, &ctx);
}
}
if (crp->crp_aad != NULL)
error = axf->Update(&ctx, crp->crp_aad, crp->crp_aad_length);
else
error = crypto_apply(crp, crp->crp_aad_start,
crp->crp_aad_length, axf->Update, &ctx);
if (error)
goto out;
error = crypto_apply(crp, crp->crp_payload_start,
crp->crp_payload_length, axf->Update, &ctx);
if (error)
goto out;
axf->Final(digest, &ctx);
if (csp->csp_auth_klen != 0 && axf->Setkey == NULL) {
if (crp->crp_auth_key == NULL)
ctx = s->hash.octx;
else
hmac_init_opad(axf, crp->crp_auth_key,
csp->csp_auth_klen, &ctx);
axf->Update(&ctx, digest, axf->hashsize);
axf->Final(digest, &ctx);
}
if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
char digest2[HASH_MAX_LEN];
crypto_copydata(crp, crp->crp_digest_start, s->hash.mlen,
digest2);
if (timingsafe_bcmp(digest, digest2, s->hash.mlen) != 0)
error = EBADMSG;
explicit_bzero(digest2, sizeof(digest2));
} else {
crypto_copyback(crp, crp->crp_digest_start, s->hash.mlen,
digest);
}
explicit_bzero(digest, sizeof(digest));
out:
explicit_bzero(&ctx, sizeof(ctx));
return (error);
}
static int
ossl_process(device_t dev, struct cryptop *crp, int hint)
{
const struct crypto_session_params *csp;
struct ossl_session *s;
int error;
bool fpu_entered;
s = crypto_get_driver_session(crp->crp_session);
csp = crypto_get_params(crp->crp_session);
if (is_fpu_kern_thread(0)) {
fpu_entered = false;
} else {
fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX);
fpu_entered = true;
}
switch (csp->csp_mode) {
case CSP_MODE_DIGEST:
error = ossl_process_hash(s, crp, csp);
break;
case CSP_MODE_CIPHER:
error = ossl_chacha20(crp, csp);
break;
case CSP_MODE_AEAD:
if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
error = ossl_chacha20_poly1305_encrypt(crp, csp);
else
error = ossl_chacha20_poly1305_decrypt(crp, csp);
break;
default:
__assert_unreachable();
}
if (fpu_entered)
fpu_kern_leave(curthread, NULL);
crp->crp_etype = error;
crypto_done(crp);
return (0);
}
static device_method_t ossl_methods[] = {
DEVMETHOD(device_identify, ossl_identify),
DEVMETHOD(device_probe, ossl_probe),
DEVMETHOD(device_attach, ossl_attach),
DEVMETHOD(device_detach, ossl_detach),
DEVMETHOD(cryptodev_probesession, ossl_probesession),
DEVMETHOD(cryptodev_newsession, ossl_newsession),
DEVMETHOD(cryptodev_process, ossl_process),
DEVMETHOD_END
};
static driver_t ossl_driver = {
"ossl",
ossl_methods,
sizeof(struct ossl_softc)
};
static devclass_t ossl_devclass;
DRIVER_MODULE(ossl, nexus, ossl_driver, ossl_devclass, NULL, NULL);
MODULE_VERSION(ossl, 1);
MODULE_DEPEND(ossl, crypto, 1, 1, 1);
|