/*
* Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Maxime Villard.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
bi = (struct bi_modulelist_entry *)((uint8_t *)biml + sizeof(*biml));
bimax = bi + biml->num;
for (; bi < bimax; bi++) {
if (bi->type != BI_MODULE_RND) {
continue;
}
if (bi->len != sizeof(rndsave_t)) {
print_state(STATE_WARNING,
"size mismatch in entropy file");
continue;
}
rndsave = (rndsave_t *)(vaddr_t)bi->base;
/* check the signature */
SHA1Init(&sig);
SHA1Update(&sig, (uint8_t *)&rndsave->entropy,
sizeof(rndsave->entropy));
SHA1Update(&sig, rndsave->data, sizeof(rndsave->data));
SHA1Final(digest, &sig);
if (memcmp(digest, rndsave->digest, sizeof(digest))) {
print_state(STATE_WARNING,
"bad SHA1 checksum in entropy file");
continue;
}
SHA512_Update(ctx, rndsave->data, sizeof(rndsave->data));
count++;
}
if (count == 0)
print_state(STATE_WARNING, "No entropy file could be loaded");
}
/*
* Add 32 bytes of rdseed/rdrand and 8 bytes of rdtsc to the context.
*/
static void
prng_get_entropy_data(SHA512_CTX *ctx)
{
uint64_t buf[8], val;
size_t i;
if (has_rdseed) {
for (i = 0; i < 8; i++) {
if (rdseed(&buf[i]) == -1) {
break;
}
}
SHA512_Update(ctx, (uint8_t *)buf, i * sizeof(uint64_t));
} else if (has_rdrand) {
for (i = 0; i < 8; i++) {
if (rdrand(&buf[i]) == -1) {
break;
}
}
SHA512_Update(ctx, (uint8_t *)buf, i * sizeof(uint64_t));
}
val = rdtsc();
SHA512_Update(ctx, (uint8_t *)&val, sizeof(val));
}
ASSERT(sz <= RNGDATA_SIZE);
if (rng.nused + sz > RNGDATA_SIZE) {
/* Fill what can be */
consumed = RNGDATA_SIZE - rng.nused;
memcpy(ptr, &rng.data[rng.nused], consumed);
/* Go through another round */
prng_round();
/* Fill the rest */
memcpy(ptr + consumed, &rng.data[rng.nused],
sz - consumed);