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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
/* $OpenBSD: lofn.c,v 1.21 2002/09/24 18:33:26 jason Exp $ */
/*
* Copyright (c) 2001-2002 Jason L. Wright (jason@thought.net)
* All rights reserved.
*
* 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.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Jason L. Wright
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, 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 DAMAGE.
*
* Effort sponsored in part by the Defense Advanced Research Projects
* Agency (DARPA) and Air Force Research Laboratory, Air Force
* Materiel Command, USAF, under agreement number F30602-01-2-0537.
*
*/
/*
* Driver for the Hifn 6500 assymmetric encryption processor.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/device.h>
#include <uvm/uvm_extern.h>
#include <crypto/cryptodev.h>
#include <dev/rndvar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/lofnreg.h>
#include <dev/pci/lofnvar.h>
/*
* Prototypes and count for the pci_device structure
*/
int lofn_probe(struct device *, void *, void *);
void lofn_attach(struct device *, struct device *, void *);
struct cfattach lofn_ca = {
sizeof(struct lofn_softc), lofn_probe, lofn_attach,
};
struct cfdriver lofn_cd = {
0, "lofn", DV_DULL
};
int lofn_intr(void *);
int lofn_norm_sigbits(const u_int8_t *, u_int);
void lofn_dump_reg(struct lofn_softc *, int);
void lofn_zero_reg(struct lofn_softc *, int);
void lofn_read_reg(struct lofn_softc *, int, union lofn_reg *);
void lofn_write_reg(struct lofn_softc *, int, union lofn_reg *);
int lofn_kprocess(struct cryptkop *);
struct lofn_softc *lofn_kfind(struct cryptkop *);
int lofn_modexp_start(struct lofn_softc *, struct lofn_q *);
void lofn_modexp_finish(struct lofn_softc *, struct lofn_q *);
void lofn_feed(struct lofn_softc *);
int
lofn_probe(parent, match, aux)
struct device *parent;
void *match;
void *aux;
{
struct pci_attach_args *pa = (struct pci_attach_args *) aux;
if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_HIFN &&
PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_HIFN_6500)
return (1);
return (0);
}
void
lofn_attach(parent, self, aux)
struct device *parent, *self;
void *aux;
{
struct lofn_softc *sc = (struct lofn_softc *)self;
struct pci_attach_args *pa = aux;
pci_chipset_tag_t pc = pa->pa_pc;
pci_intr_handle_t ih;
const char *intrstr = NULL;
bus_size_t iosize;
u_int32_t cmd;
cmd = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
cmd |= PCI_COMMAND_MEM_ENABLE;
pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, cmd);
cmd = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
if (!(cmd & PCI_COMMAND_MEM_ENABLE)) {
printf(": failed to enable memory mapping\n");
return;
}
if (pci_mapreg_map(pa, LOFN_BAR0, PCI_MAPREG_TYPE_MEM, 0,
&sc->sc_st, &sc->sc_sh, NULL, &iosize, 0)) {
printf(": can't map mem space\n");
return;
}
sc->sc_dmat = pa->pa_dmat;
if (pci_intr_map(pa, &ih)) {
printf(": couldn't map interrupt\n");
goto fail;
}
intrstr = pci_intr_string(pc, ih);
sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, lofn_intr, sc,
self->dv_xname);
if (sc->sc_ih == NULL) {
printf(": couldn't establish interrupt");
if (intrstr != NULL)
printf(" at %s", intrstr);
printf("\n");
goto fail;
}
WRITE_REG_0(sc, LOFN_REL_RNC, LOFN_RNG_SCALAR);
/* Enable RNG */
WRITE_REG_0(sc, LOFN_REL_CFG2,
READ_REG_0(sc, LOFN_REL_CFG2) | LOFN_CFG2_RNGENA);
sc->sc_ier |= LOFN_IER_RDY;
WRITE_REG(sc, LOFN_REL_IER, sc->sc_ier);
/* Enable ALU */
WRITE_REG_0(sc, LOFN_REL_CFG2,
READ_REG_0(sc, LOFN_REL_CFG2) | LOFN_CFG2_PRCENA);
SIMPLEQ_INIT(&sc->sc_queue);
sc->sc_cid = crypto_get_driverid(0);
if (sc->sc_cid < 0) {
printf(": failed to register cid\n");
return;
}
crypto_kregister(sc->sc_cid, CRK_MOD_EXP, 0, lofn_kprocess);
printf(": %s\n", intrstr);
return;
fail:
bus_space_unmap(sc->sc_st, sc->sc_sh, iosize);
}
int
lofn_intr(vsc)
void *vsc;
{
struct lofn_softc *sc = vsc;
struct lofn_q *q;
u_int32_t sr;
int r = 0, i;
sr = READ_REG_0(sc, LOFN_REL_SR);
if (sc->sc_ier & LOFN_IER_RDY) {
if (sr & LOFN_SR_RNG_UF) {
r = 1;
printf("%s: rng underflow (disabling)\n",
sc->sc_dv.dv_xname);
WRITE_REG_0(sc, LOFN_REL_CFG2,
READ_REG_0(sc, LOFN_REL_CFG2) &
(~LOFN_CFG2_RNGENA));
sc->sc_ier &= ~LOFN_IER_RDY;
WRITE_REG_0(sc, LOFN_REL_IER, sc->sc_ier);
} else if (sr & LOFN_SR_RNG_RDY) {
r = 1;
bus_space_read_region_4(sc->sc_st, sc->sc_sh,
LOFN_REL_RNG, sc->sc_rngbuf, LOFN_RNGBUF_SIZE);
for (i = 0; i < LOFN_RNGBUF_SIZE; i++)
add_true_randomness(sc->sc_rngbuf[i]);
}
}
if (sc->sc_ier & LOFN_IER_DONE) {
r = 1;
if (sr & LOFN_SR_DONE && sc->sc_current != NULL) {
q = sc->sc_current;
sc->sc_current = NULL;
q->q_finish(sc, q);
free(q, M_DEVBUF);
lofn_feed(sc);
}
}
return (r);
}
void
lofn_read_reg(sc, ridx, rp)
struct lofn_softc *sc;
int ridx;
union lofn_reg *rp;
{
#if BYTE_ORDER == BIG_ENDIAN
bus_space_read_region_4(sc->sc_st, sc->sc_sh,
LOFN_REGADDR(LOFN_WIN_0, ridx, 0), rp->w, 1024/32);
#else
bus_space_read_region_4(sc->sc_st, sc->sc_sh,
LOFN_REGADDR(LOFN_WIN_2, ridx, 0), rp->w, 1024/32);
#endif
}
void
lofn_write_reg(sc, ridx, rp)
struct lofn_softc *sc;
int ridx;
union lofn_reg *rp;
{
#if BYTE_ORDER == BIG_ENDIAN
bus_space_write_region_4(sc->sc_st, sc->sc_sh,
LOFN_REGADDR(LOFN_WIN_0, ridx, 0), rp->w, 1024/32);
#else
bus_space_write_region_4(sc->sc_st, sc->sc_sh,
LOFN_REGADDR(LOFN_WIN_2, ridx, 0), rp->w, 1024/32);
#endif
}
void
lofn_zero_reg(sc, ridx)
struct lofn_softc *sc;
int ridx;
{
lofn_write_reg(sc, ridx, &sc->sc_zero);
}
void
lofn_dump_reg(sc, ridx)
struct lofn_softc *sc;
int ridx;
{
int i;
printf("reg %d bits %4u ", ridx,
READ_REG(sc, LOFN_LENADDR(LOFN_WIN_2, ridx)) & LOFN_LENMASK);
for (i = 0; i < 1024/32; i++) {
printf("%08X", READ_REG(sc, LOFN_REGADDR(LOFN_WIN_3, ridx, i)));
}
printf("\n");
}
struct lofn_softc *
lofn_kfind(krp)
struct cryptkop *krp;
{
struct lofn_softc *sc;
int i;
for (i = 0; i < lofn_cd.cd_ndevs; i++) {
sc = lofn_cd.cd_devs[i];
if (sc == NULL)
continue;
if (sc->sc_cid == krp->krp_hid)
return (sc);
}
return (NULL);
}
int
lofn_kprocess(krp)
struct cryptkop *krp;
{
struct lofn_softc *sc;
struct lofn_q *q;
int s;
if (krp == NULL || krp->krp_callback == NULL)
return (EINVAL);
if ((sc = lofn_kfind(krp)) == NULL) {
krp->krp_status = EINVAL;
crypto_kdone(krp);
return (0);
}
q = (struct lofn_q *)malloc(sizeof(*q), M_DEVBUF, M_NOWAIT);
if (q == NULL) {
krp->krp_status = ENOMEM;
crypto_kdone(krp);
return (0);
}
switch (krp->krp_op) {
case CRK_MOD_EXP:
q->q_start = lofn_modexp_start;
q->q_finish = lofn_modexp_finish;
q->q_krp = krp;
s = splnet();
SIMPLEQ_INSERT_TAIL(&sc->sc_queue, q, q_next);
lofn_feed(sc);
splx(s);
return (0);
default:
printf("%s: kprocess: invalid op 0x%x\n",
sc->sc_dv.dv_xname, krp->krp_op);
krp->krp_status = EOPNOTSUPP;
crypto_kdone(krp);
free(q, M_DEVBUF);
return (0);
}
}
int
lofn_modexp_start(sc, q)
struct lofn_softc *sc;
struct lofn_q *q;
{
struct cryptkop *krp = q->q_krp;
int ip = 0, err = 0;
int mshift, eshift, nshift;
int mbits, ebits, nbits;
if (krp->krp_param[LOFN_MODEXP_PAR_M].crp_nbits > 1024) {
err = ERANGE;
goto errout;
}
/* Zero out registers. */
lofn_zero_reg(sc, 0);
lofn_zero_reg(sc, 1);
lofn_zero_reg(sc, 2);
lofn_zero_reg(sc, 3);
/* Write out N... */
nbits = lofn_norm_sigbits(krp->krp_param[LOFN_MODEXP_PAR_N].crp_p,
krp->krp_param[LOFN_MODEXP_PAR_N].crp_nbits);
if (nbits > 1024) {
err = E2BIG;
goto errout;
}
if (nbits < 5) {
err = ERANGE;
goto errout;
}
bzero(&sc->sc_tmp, sizeof(sc->sc_tmp));
bcopy(krp->krp_param[LOFN_MODEXP_PAR_N].crp_p, &sc->sc_tmp,
(nbits + 7) / 8);
lofn_write_reg(sc, 2, &sc->sc_tmp);
nshift = 1024 - nbits;
WRITE_REG(sc, LOFN_LENADDR(LOFN_WIN_2, 2), 1024);
if (nshift != 0) {
WRITE_REG(sc, LOFN_REL_INSTR + ip,
LOFN_INSTR2(0, OP_CODE_SL, 2, 2, nshift));
ip += 4;
WRITE_REG(sc, LOFN_REL_INSTR + ip,
LOFN_INSTR2(0, OP_CODE_TAG, 2, 2, nbits));
ip += 4;
}
/* Write out M... */
mbits = lofn_norm_sigbits(krp->krp_param[LOFN_MODEXP_PAR_M].crp_p,
krp->krp_param[LOFN_MODEXP_PAR_M].crp_nbits);
if (mbits > 1024 || mbits > nbits) {
err = E2BIG;
goto errout;
}
bzero(&sc->sc_tmp, sizeof(sc->sc_tmp));
bcopy(krp->krp_param[LOFN_MODEXP_PAR_M].crp_p, &sc->sc_tmp,
(mbits + 7) / 8);
lofn_write_reg(sc, 0, &sc->sc_tmp);
mshift = 1024 - nbits;
WRITE_REG(sc, LOFN_LENADDR(LOFN_WIN_2, 0), 1024);
if (mshift != 0) {
WRITE_REG(sc, LOFN_REL_INSTR + ip,
LOFN_INSTR2(0, OP_CODE_SL, 0, 0, mshift));
ip += 4;
WRITE_REG(sc, LOFN_REL_INSTR + ip,
LOFN_INSTR2(0, OP_CODE_TAG, 0, 0, nbits));
ip += 4;
}
/* Write out E... */
ebits = lofn_norm_sigbits(krp->krp_param[LOFN_MODEXP_PAR_E].crp_p,
krp->krp_param[LOFN_MODEXP_PAR_E].crp_nbits);
if (ebits > 1024 || ebits > nbits) {
err = E2BIG;
goto errout;
}
if (ebits < 1) {
err = ERANGE;
goto errout;
}
bzero(&sc->sc_tmp, sizeof(sc->sc_tmp));
bcopy(krp->krp_param[LOFN_MODEXP_PAR_E].crp_p, &sc->sc_tmp,
(ebits + 7) / 8);
lofn_write_reg(sc, 1, &sc->sc_tmp);
eshift = 1024 - nbits;
WRITE_REG(sc, LOFN_LENADDR(LOFN_WIN_2, 1), 1024);
if (eshift != 0) {
WRITE_REG(sc, LOFN_REL_INSTR + ip,
LOFN_INSTR2(0, OP_CODE_SL, 1, 1, eshift));
ip += 4;
WRITE_REG(sc, LOFN_REL_INSTR + ip,
LOFN_INSTR2(0, OP_CODE_TAG, 1, 1, nbits));
ip += 4;
}
if (nshift == 0) {
WRITE_REG(sc, LOFN_REL_INSTR + ip,
LOFN_INSTR(OP_DONE, OP_CODE_MODEXP, 3, 0, 1, 2));
ip += 4;
} else {
WRITE_REG(sc, LOFN_REL_INSTR + ip,
LOFN_INSTR(0, OP_CODE_MODEXP, 3, 0, 1, 2));
ip += 4;
WRITE_REG(sc, LOFN_REL_INSTR + ip,
LOFN_INSTR2(0, OP_CODE_SR, 3, 3, nshift));
ip += 4;
WRITE_REG(sc, LOFN_REL_INSTR + ip,
LOFN_INSTR2(OP_DONE, OP_CODE_TAG, 3, 3, nbits));
ip += 4;
}
/* Start microprogram */
WRITE_REG(sc, LOFN_REL_CR, 0);
return (0);
errout:
bzero(&sc->sc_tmp, sizeof(sc->sc_tmp));
lofn_zero_reg(sc, 0);
lofn_zero_reg(sc, 1);
lofn_zero_reg(sc, 2);
lofn_zero_reg(sc, 3);
krp->krp_status = err;
crypto_kdone(krp);
return (1);
}
void
lofn_modexp_finish(sc, q)
struct lofn_softc *sc;
struct lofn_q *q;
{
struct cryptkop *krp = q->q_krp;
int reglen, crplen;
lofn_read_reg(sc, 3, &sc->sc_tmp);
reglen = ((READ_REG(sc, LOFN_LENADDR(LOFN_WIN_2, 3)) & LOFN_LENMASK) +
7) / 8;
crplen = (krp->krp_param[krp->krp_iparams].crp_nbits + 7) / 8;
if (crplen <= reglen)
bcopy(sc->sc_tmp.b, krp->krp_param[krp->krp_iparams].crp_p,
reglen);
else {
bcopy(sc->sc_tmp.b, krp->krp_param[krp->krp_iparams].crp_p,
reglen);
bzero(krp->krp_param[krp->krp_iparams].crp_p + reglen,
crplen - reglen);
}
bzero(&sc->sc_tmp, sizeof(sc->sc_tmp));
lofn_zero_reg(sc, 0);
lofn_zero_reg(sc, 1);
lofn_zero_reg(sc, 2);
lofn_zero_reg(sc, 3);
crypto_kdone(krp);
}
/*
* Return the number of significant bits of a big number.
*/
int
lofn_norm_sigbits(const u_int8_t *p, u_int pbits)
{
u_int plen = (pbits + 7) / 8;
int i, sig = plen * 8;
u_int8_t c;
for (i = plen - 1; i >= 0; i--) {
c = p[i];
if (c != 0) {
while ((c & 0x80) == 0) {
sig--;
c <<= 1;
}
break;
}
sig -= 8;
}
return (sig);
}
void
lofn_feed(sc)
struct lofn_softc *sc;
{
struct lofn_q *q;
/* Queue is empty and nothing being processed, turn off interrupt */
if (SIMPLEQ_EMPTY(&sc->sc_queue) &&
sc->sc_current == NULL) {
sc->sc_ier &= ~LOFN_IER_DONE;
WRITE_REG(sc, LOFN_REL_IER, sc->sc_ier);
return;
}
/* Operation already pending, wait. */
if (sc->sc_current != NULL)
return;
while (!SIMPLEQ_EMPTY(&sc->sc_queue)) {
q = SIMPLEQ_FIRST(&sc->sc_queue);
if (q->q_start(sc, q) == 0) {
sc->sc_current = q;
SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, q, q_next);
sc->sc_ier |= LOFN_IER_DONE;
WRITE_REG(sc, LOFN_REL_IER, sc->sc_ier);
break;
} else {
SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, q, q_next);
free(q, M_DEVBUF);
}
}
}
|