kernel: remove unused utsname_set_machine()
[unleashed.git] / usr / src / uts / sun4v / io / n2rng / n2rng_provider.c
blob63b7deb7292cb7dafebd675d9380a0b320196b7f
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/types.h>
26 #include <sys/sysmacros.h>
27 #include <sys/modctl.h>
28 #include <sys/conf.h>
29 #include <sys/devops.h>
30 #include <sys/cmn_err.h>
31 #include <sys/kmem.h>
32 #include <sys/stat.h>
33 #include <sys/open.h>
34 #include <sys/file.h>
35 #include <sys/cpuvar.h>
36 #include <sys/disp.h>
37 #include <sys/hsvc.h>
38 #include <sys/machsystm.h>
39 #include <sys/ksynch.h>
40 #include <sys/hypervisor_api.h>
41 #include <sys/n2rng.h>
42 #include <sys/sha1.h>
43 #include <sys/ddi.h> /* near end to get min and max macros right */
44 #include <sys/sunddi.h>
45 #include <rng/fips_random.h>
47 /* n must be a power of 2 */
48 #define ROUNDUP(k, n) (((k) + (n) - 1) & ~((n) - 1))
51 * Policy. ENTROPY_STARVATION is the maximum number of calls each
52 * FIPS instance will accept without successfully getting more
53 * entropy. It needs to be large enough to allow RNG operations to
54 * not stall because of health checks, etc. But we don't want it too
55 * large. FIPS 186-2 change 1 (5 October 2001) states that no more
56 * that 2,000,000 DSA signatures (done using this algorithm) should be
57 * done without reseeding. We make sure we add 64 bits of entropy at
58 * most every 10000 operations, hence we will have stirred in 160 bits
59 * of entropy at most once every 30000 operations. Normally, we stir
60 * in 64 bits of entropy for every number generated.
62 #define ENTROPY_STARVATION 10000ULL
65 int
66 fips_random(n2rng_t *n2rng, uint8_t *out, size_t nbytes)
68 int i;
69 fipsrandomstruct_t *frsp;
70 int rv;
71 union {
72 uint32_t as32[SHA1WORDS];
73 uint64_t as64[ROUNDUP(SHA1WORDS, 2) >> 1];
74 } entropy = {0};
75 uint32_t tempout[SHA1WORDS];
78 for (i = 0; i < nbytes; i += SHA1BYTES) {
79 frsp = &n2rng->n_frs.fipsarray[
80 atomic_inc_32_nv(&n2rng->n_frs.fips_round_robin_j) %
81 N2RNG_FIPS_INSTANCES];
83 * Since in the new scheme of things, the RNG latency
84 * will be high on reads after the first, we get just
85 * one word of entropy per call.
87 if ((rv = n2rng_getentropy(n2rng, (void *)&entropy.as64[1],
88 sizeof (uint64_t))) != 0) {
91 * If all rngs have failed, dispatch task to unregister
92 * from kcf and put the driver in an error state. If
93 * recoverable errors persist, a configuration retry
94 * will be initiated.
96 if (rv == EPERM) {
97 n2rng_failure(n2rng);
98 return (EIO);
100 /* Failure with possible recovery */
101 entropy.as64[1] = 0;
105 * The idea here is that a Niagara2 chip is highly
106 * parallel, with many strands. If we have just one
107 * instance of the FIPS data, then only one FIPS
108 * computation can happen at a time, serializeing all
109 * the RNG stuff. So we make N2RNG_FIPS_INSTANCES,
110 * and use them round-robin, with the counter being
111 * n2rng->n_frs.fips_round_robin_j. We increment the
112 * counter with an atomic op, avoiding having to have
113 * a global muxtex. The atomic ops are also
114 * significantly faster than mutexes. The mutex is
115 * put inside the loop, otherwise one thread reading
116 * many blocks could stall all other strands.
118 frsp = &n2rng->n_frs.fipsarray[
119 atomic_inc_32_nv(&n2rng->n_frs.fips_round_robin_j) %
120 N2RNG_FIPS_INSTANCES];
122 mutex_enter(&frsp->mtx);
124 if (entropy.as64[1] == 0) {
126 * If we did not get any entropy, entropyword
127 * is zero. We get a false positive with
128 * probablitity 2^-64. It's not worth a few
129 * extra stores and tests eliminate the false
130 * positive.
132 if (++frsp->entropyhunger > ENTROPY_STARVATION) {
133 mutex_exit(&frsp->mtx);
134 n2rng_unconfigured(n2rng);
135 return (EIO);
137 } else {
138 frsp->entropyhunger = 0;
141 /* nbytes - i is bytes to go */
142 fips_random_inner(frsp->XKEY, tempout, entropy.as32);
144 bcopy(tempout, &out[i], min(nbytes - i, SHA1BYTES));
146 mutex_exit(&frsp->mtx);
149 /* Zeroize sensitive information */
151 entropy.as64[1] = 0;
152 bzero(tempout, SHA1BYTES);
154 return (0);
158 * Initializes one FIPS RNG instance. Must be called once for each
159 * instance.
162 n2rng_fips_random_init(n2rng_t *n2rng, fipsrandomstruct_t *frsp)
165 * All FIPS-approved algorithms will operate as cryptograpic
166 * quality PRNGs even if there is no entropy source. (In
167 * fact, this the only one that accepts entropy on the fly.)
168 * One motivation for this is that they system keeps on
169 * delivering cryptographic quality random numbers, even if
170 * the entropy source fails.
173 int rv;
174 static uint32_t FIPS_RNG_NO_USER_INPUT[] = {0, 0, 0, 0, 0};
176 rv = n2rng_getentropy(n2rng, (void *)frsp->XKEY, ROUNDUP(SHA1BYTES, 8));
177 if (rv) {
178 return (rv);
180 frsp->entropyhunger = 0;
181 mutex_init(&frsp->mtx, NULL, MUTEX_DRIVER, NULL);
183 /* compute the first (compare only) random value */
184 fips_random_inner(frsp->XKEY, frsp->x_jminus1, FIPS_RNG_NO_USER_INPUT);
186 return (0);
189 void
190 n2rng_fips_random_fini(fipsrandomstruct_t *frsp)
192 mutex_destroy(&frsp->mtx);
194 * Zeroise fips data. Not really necessary, since the
195 * algorithm has backtracking resistance, but do it anyway.
197 bzero(frsp, sizeof (fipsrandomstruct_t));