14625Sgm89044 /*
24625Sgm89044 * CDDL HEADER START
34625Sgm89044 *
44625Sgm89044 * The contents of this file are subject to the terms of the
54625Sgm89044 * Common Development and Distribution License (the "License").
64625Sgm89044 * You may not use this file except in compliance with the License.
74625Sgm89044 *
84625Sgm89044 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94625Sgm89044 * or http://www.opensolaris.org/os/licensing.
104625Sgm89044 * See the License for the specific language governing permissions
114625Sgm89044 * and limitations under the License.
124625Sgm89044 *
134625Sgm89044 * When distributing Covered Code, include this CDDL HEADER in each
144625Sgm89044 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154625Sgm89044 * If applicable, add the following below this CDDL HEADER, with the
164625Sgm89044 * fields enclosed by brackets "[]" replaced with your own identifying
174625Sgm89044 * information: Portions Copyright [yyyy] [name of copyright owner]
184625Sgm89044 *
194625Sgm89044 * CDDL HEADER END
204625Sgm89044 */
214625Sgm89044 /*
22*12929SMisaki.Miyashita@Oracle.COM * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
234625Sgm89044 */
244625Sgm89044
254625Sgm89044 #include <sys/types.h>
264625Sgm89044 #include <sys/sysmacros.h>
274625Sgm89044 #include <sys/modctl.h>
284625Sgm89044 #include <sys/conf.h>
294625Sgm89044 #include <sys/devops.h>
304625Sgm89044 #include <sys/cmn_err.h>
314625Sgm89044 #include <sys/kmem.h>
324625Sgm89044 #include <sys/stat.h>
334625Sgm89044 #include <sys/open.h>
344625Sgm89044 #include <sys/file.h>
354625Sgm89044 #include <sys/cpuvar.h>
364625Sgm89044 #include <sys/disp.h>
374625Sgm89044 #include <sys/hsvc.h>
384625Sgm89044 #include <sys/machsystm.h>
394625Sgm89044 #include <sys/ksynch.h>
404625Sgm89044 #include <sys/hypervisor_api.h>
414625Sgm89044 #include <sys/n2rng.h>
424625Sgm89044 #include <sys/sha1.h>
434625Sgm89044 #include <sys/ddi.h> /* near end to get min and max macros right */
444625Sgm89044 #include <sys/sunddi.h>
458029SHai-May.Chao@Sun.COM #include <rng/fips_random.h>
464625Sgm89044
474625Sgm89044 /* n must be a power of 2 */
484625Sgm89044 #define ROUNDUP(k, n) (((k) + (n) - 1) & ~((n) - 1))
494625Sgm89044
504625Sgm89044 /*
514625Sgm89044 * Policy. ENTROPY_STARVATION is the maximum number of calls each
524625Sgm89044 * FIPS instance will accept without successfully getting more
534625Sgm89044 * entropy. It needs to be large enough to allow RNG operations to
544625Sgm89044 * not stall because of health checks, etc. But we don't want it too
554625Sgm89044 * large. FIPS 186-2 change 1 (5 October 2001) states that no more
564625Sgm89044 * that 2,000,000 DSA signatures (done using this algorithm) should be
574625Sgm89044 * done without reseeding. We make sure we add 64 bits of entropy at
584625Sgm89044 * most every 10000 operations, hence we will have stirred in 160 bits
594625Sgm89044 * of entropy at most once every 30000 operations. Normally, we stir
604625Sgm89044 * in 64 bits of entropy for every number generated.
614625Sgm89044 */
624625Sgm89044 #define ENTROPY_STARVATION 10000ULL
634625Sgm89044
644625Sgm89044
654625Sgm89044 int
fips_random(n2rng_t * n2rng,uint8_t * out,size_t nbytes)664625Sgm89044 fips_random(n2rng_t *n2rng, uint8_t *out, size_t nbytes)
674625Sgm89044 {
684625Sgm89044 int i;
694625Sgm89044 fipsrandomstruct_t *frsp;
705650Stwelke int rv;
714625Sgm89044 union {
724625Sgm89044 uint32_t as32[SHA1WORDS];
734625Sgm89044 uint64_t as64[ROUNDUP(SHA1WORDS, 2) >> 1];
744625Sgm89044 } entropy = {0};
754625Sgm89044 uint32_t tempout[SHA1WORDS];
764625Sgm89044
774625Sgm89044
784625Sgm89044 for (i = 0; i < nbytes; i += SHA1BYTES) {
795650Stwelke frsp = &n2rng->n_frs.fipsarray[
805650Stwelke atomic_inc_32_nv(&n2rng->n_frs.fips_round_robin_j) %
815650Stwelke N2RNG_FIPS_INSTANCES];
824625Sgm89044 /*
834625Sgm89044 * Since in the new scheme of things, the RNG latency
844625Sgm89044 * will be high on reads after the first, we get just
855650Stwelke * one word of entropy per call.
864625Sgm89044 */
875650Stwelke if ((rv = n2rng_getentropy(n2rng, (void *)&entropy.as64[1],
885650Stwelke sizeof (uint64_t))) != 0) {
895650Stwelke
905650Stwelke /*
915650Stwelke * If all rngs have failed, dispatch task to unregister
925650Stwelke * from kcf and put the driver in an error state. If
935650Stwelke * recoverable errors persist, a configuration retry
945650Stwelke * will be initiated.
955650Stwelke */
965650Stwelke if (rv == EPERM) {
975650Stwelke n2rng_failure(n2rng);
985650Stwelke return (EIO);
995650Stwelke }
1005650Stwelke /* Failure with possible recovery */
1014625Sgm89044 entropy.as64[1] = 0;
1024625Sgm89044 }
1034625Sgm89044
1044625Sgm89044 /*
1054625Sgm89044 * The idea here is that a Niagara2 chip is highly
1064625Sgm89044 * parallel, with many strands. If we have just one
1074625Sgm89044 * instance of the FIPS data, then only one FIPS
1084625Sgm89044 * computation can happen at a time, serializeing all
1094625Sgm89044 * the RNG stuff. So we make N2RNG_FIPS_INSTANCES,
1104625Sgm89044 * and use them round-robin, with the counter being
1114625Sgm89044 * n2rng->n_frs.fips_round_robin_j. We increment the
1124625Sgm89044 * counter with an atomic op, avoiding having to have
1134625Sgm89044 * a global muxtex. The atomic ops are also
1144625Sgm89044 * significantly faster than mutexes. The mutex is
1154625Sgm89044 * put inside the loop, otherwise one thread reading
1164625Sgm89044 * many blocks could stall all other strands.
1174625Sgm89044 */
1184625Sgm89044 frsp = &n2rng->n_frs.fipsarray[
1194625Sgm89044 atomic_inc_32_nv(&n2rng->n_frs.fips_round_robin_j) %
1204625Sgm89044 N2RNG_FIPS_INSTANCES];
1214625Sgm89044
1224625Sgm89044 mutex_enter(&frsp->mtx);
1234625Sgm89044
1244625Sgm89044 if (entropy.as64[1] == 0) {
1254625Sgm89044 /*
1264625Sgm89044 * If we did not get any entropy, entropyword
1274625Sgm89044 * is zero. We get a false positive with
1284625Sgm89044 * probablitity 2^-64. It's not worth a few
1294625Sgm89044 * extra stores and tests eliminate the false
1304625Sgm89044 * positive.
1314625Sgm89044 */
1324625Sgm89044 if (++frsp->entropyhunger > ENTROPY_STARVATION) {
1334625Sgm89044 mutex_exit(&frsp->mtx);
1345650Stwelke n2rng_unconfigured(n2rng);
1354625Sgm89044 return (EIO);
1364625Sgm89044 }
1374625Sgm89044 } else {
1384625Sgm89044 frsp->entropyhunger = 0;
1394625Sgm89044 }
1404625Sgm89044
1414625Sgm89044 /* nbytes - i is bytes to go */
1428029SHai-May.Chao@Sun.COM fips_random_inner(frsp->XKEY, tempout, entropy.as32);
143*12929SMisaki.Miyashita@Oracle.COM
144*12929SMisaki.Miyashita@Oracle.COM /*
145*12929SMisaki.Miyashita@Oracle.COM * Compare last round with the results of this round, fail
146*12929SMisaki.Miyashita@Oracle.COM * if identical. Save for next round.
147*12929SMisaki.Miyashita@Oracle.COM */
148*12929SMisaki.Miyashita@Oracle.COM if (n2rng->n_is_fips == B_TRUE) {
149*12929SMisaki.Miyashita@Oracle.COM uint32_t differ = 0;
150*12929SMisaki.Miyashita@Oracle.COM int j;
151*12929SMisaki.Miyashita@Oracle.COM
152*12929SMisaki.Miyashita@Oracle.COM for (j = 0; j < 5; j++) {
153*12929SMisaki.Miyashita@Oracle.COM differ |= tempout[j] ^ frsp->x_jminus1[j];
154*12929SMisaki.Miyashita@Oracle.COM frsp->x_jminus1[j] = tempout[j];
155*12929SMisaki.Miyashita@Oracle.COM }
156*12929SMisaki.Miyashita@Oracle.COM if (differ == 0) {
157*12929SMisaki.Miyashita@Oracle.COM /*
158*12929SMisaki.Miyashita@Oracle.COM * If differ == 0, the RNG produced the same
159*12929SMisaki.Miyashita@Oracle.COM * answer twice. By FIPS 140-2 Section 4.9 we
160*12929SMisaki.Miyashita@Oracle.COM * must enter an error state.
161*12929SMisaki.Miyashita@Oracle.COM */
162*12929SMisaki.Miyashita@Oracle.COM mutex_exit(&frsp->mtx);
163*12929SMisaki.Miyashita@Oracle.COM n2rng_failure(n2rng);
164*12929SMisaki.Miyashita@Oracle.COM cmn_err(CE_WARN,
165*12929SMisaki.Miyashita@Oracle.COM "n2rng: Continuous random number generator"
166*12929SMisaki.Miyashita@Oracle.COM " test of FIPS-140 RNG failed.");
167*12929SMisaki.Miyashita@Oracle.COM return (EIO);
168*12929SMisaki.Miyashita@Oracle.COM }
169*12929SMisaki.Miyashita@Oracle.COM }
170*12929SMisaki.Miyashita@Oracle.COM
171*12929SMisaki.Miyashita@Oracle.COM bcopy(tempout, &out[i], min(nbytes - i, SHA1BYTES));
1724625Sgm89044
1734625Sgm89044 mutex_exit(&frsp->mtx);
1744625Sgm89044 }
1754625Sgm89044
1764625Sgm89044 /* Zeroize sensitive information */
1774625Sgm89044
1784625Sgm89044 entropy.as64[1] = 0;
1794625Sgm89044 bzero(tempout, SHA1BYTES);
1804625Sgm89044
1814625Sgm89044 return (0);
1824625Sgm89044 }
1834625Sgm89044
1844625Sgm89044 /*
1854625Sgm89044 * Initializes one FIPS RNG instance. Must be called once for each
1864625Sgm89044 * instance.
1874625Sgm89044 */
1884625Sgm89044 int
n2rng_fips_random_init(n2rng_t * n2rng,fipsrandomstruct_t * frsp)1894625Sgm89044 n2rng_fips_random_init(n2rng_t *n2rng, fipsrandomstruct_t *frsp)
1904625Sgm89044 {
1914625Sgm89044 /*
1924625Sgm89044 * All FIPS-approved algorithms will operate as cryptograpic
1934625Sgm89044 * quality PRNGs even if there is no entropy source. (In
1944625Sgm89044 * fact, this the only one that accepts entropy on the fly.)
1954625Sgm89044 * One motivation for this is that they system keeps on
1964625Sgm89044 * delivering cryptographic quality random numbers, even if
1974625Sgm89044 * the entropy source fails.
1984625Sgm89044 */
1994625Sgm89044
2004625Sgm89044 int rv;
201*12929SMisaki.Miyashita@Oracle.COM static uint32_t FIPS_RNG_NO_USER_INPUT[] = {0, 0, 0, 0, 0};
2024625Sgm89044
2034625Sgm89044 rv = n2rng_getentropy(n2rng, (void *)frsp->XKEY, ROUNDUP(SHA1BYTES, 8));
2044625Sgm89044 if (rv) {
2054625Sgm89044 return (rv);
2064625Sgm89044 }
2075650Stwelke frsp->entropyhunger = 0;
2084625Sgm89044 mutex_init(&frsp->mtx, NULL, MUTEX_DRIVER, NULL);
2094625Sgm89044
210*12929SMisaki.Miyashita@Oracle.COM /* compute the first (compare only) random value */
211*12929SMisaki.Miyashita@Oracle.COM fips_random_inner(frsp->XKEY, frsp->x_jminus1, FIPS_RNG_NO_USER_INPUT);
212*12929SMisaki.Miyashita@Oracle.COM
2134625Sgm89044 return (0);
2144625Sgm89044 }
2154625Sgm89044
2164625Sgm89044 void
n2rng_fips_random_fini(fipsrandomstruct_t * frsp)2174625Sgm89044 n2rng_fips_random_fini(fipsrandomstruct_t *frsp)
2184625Sgm89044 {
2194625Sgm89044 mutex_destroy(&frsp->mtx);
2204625Sgm89044 /*
2214625Sgm89044 * Zeroise fips data. Not really necessary, since the
2224625Sgm89044 * algorithm has backtracking resistance, but do it anyway.
2234625Sgm89044 */
2244625Sgm89044 bzero(frsp, sizeof (fipsrandomstruct_t));
2254625Sgm89044 }
226