^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright IBM Corp. 2006, 2015
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Harald Freudenberger <freude@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Driver for the s390 pseudo random number generator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define KMSG_COMPONENT "prng"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/fips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/cpufeature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <asm/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <asm/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <asm/cpacf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) MODULE_AUTHOR("IBM Corporation");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) MODULE_DESCRIPTION("s390 PRNG interface");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define PRNG_MODE_AUTO 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define PRNG_MODE_TDES 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define PRNG_MODE_SHA512 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static unsigned int prng_mode = PRNG_MODE_AUTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) module_param_named(mode, prng_mode, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) MODULE_PARM_DESC(prng_mode, "PRNG mode: 0 - auto, 1 - TDES, 2 - SHA512");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define PRNG_CHUNKSIZE_TDES_MIN 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define PRNG_CHUNKSIZE_TDES_MAX (64*1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define PRNG_CHUNKSIZE_SHA512_MIN 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define PRNG_CHUNKSIZE_SHA512_MAX (64*1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static unsigned int prng_chunk_size = 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) module_param_named(chunksize, prng_chunk_size, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) MODULE_PARM_DESC(prng_chunk_size, "PRNG read chunk size in bytes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define PRNG_RESEED_LIMIT_TDES 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define PRNG_RESEED_LIMIT_TDES_LOWER 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define PRNG_RESEED_LIMIT_SHA512 100000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define PRNG_RESEED_LIMIT_SHA512_LOWER 10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static unsigned int prng_reseed_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) module_param_named(reseed_limit, prng_reseed_limit, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) MODULE_PARM_DESC(prng_reseed_limit, "PRNG reseed limit");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static bool trng_available;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * Any one who considers arithmetical methods of producing random digits is,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * of course, in a state of sin. -- John von Neumann
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int prng_errorflag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define PRNG_GEN_ENTROPY_FAILED 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define PRNG_SELFTEST_FAILED 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define PRNG_INSTANTIATE_FAILED 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define PRNG_SEED_FAILED 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define PRNG_RESEED_FAILED 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define PRNG_GEN_FAILED 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct prng_ws_s {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) u8 parm_block[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) u32 reseed_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) u64 byte_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct prno_ws_s {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) u32 res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) u32 reseed_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) u64 stream_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) u8 V[112];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) u8 C[112];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct prng_data_s {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct prng_ws_s prngws;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct prno_ws_s prnows;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) u32 rest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) u8 *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static struct prng_data_s *prng_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* initial parameter block for tdes mode, copied from libica */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static const u8 initial_parm_block[32] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 0x0F, 0x2B, 0x8E, 0x63, 0x8C, 0x8E, 0xD2, 0x52,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 0x64, 0xB7, 0xA0, 0x7B, 0x75, 0x28, 0xB8, 0xF4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 0x75, 0x5F, 0xD2, 0xA6, 0x8D, 0x97, 0x11, 0xFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 0x49, 0xD8, 0x23, 0xF3, 0x7E, 0x21, 0xEC, 0xA0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*** helper functions ***/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * generate_entropy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * This function fills a given buffer with random bytes. The entropy within
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * the random bytes given back is assumed to have at least 50% - meaning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * a 64 bytes buffer has at least 64 * 8 / 2 = 256 bits of entropy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * Within the function the entropy generation is done in junks of 64 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * So the caller should also ask for buffer fill in multiples of 64 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * The generation of the entropy is based on the assumption that every stckf()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * invocation produces 0.5 bits of entropy. To accumulate 256 bits of entropy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * at least 512 stckf() values are needed. The entropy relevant part of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * stckf value is bit 51 (counting starts at the left with bit nr 0) so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * here we use the lower 4 bytes and exor the values into 2k of bufferspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * To be on the save side, if there is ever a problem with stckf() the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * other half of the page buffer is filled with bytes from urandom via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * get_random_bytes(), so this function consumes 2k of urandom for each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * requested 64 bytes output data. Finally the buffer page is condensed into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * a 64 byte value by hashing with a SHA512 hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int generate_entropy(u8 *ebuf, size_t nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int n, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) u8 *pg, pblock[80] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* 8 x 64 bit init values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 0x6A, 0x09, 0xE6, 0x67, 0xF3, 0xBC, 0xC9, 0x08,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 0xBB, 0x67, 0xAE, 0x85, 0x84, 0xCA, 0xA7, 0x3B,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 0x3C, 0x6E, 0xF3, 0x72, 0xFE, 0x94, 0xF8, 0x2B,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 0xA5, 0x4F, 0xF5, 0x3A, 0x5F, 0x1D, 0x36, 0xF1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 0x51, 0x0E, 0x52, 0x7F, 0xAD, 0xE6, 0x82, 0xD1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 0x9B, 0x05, 0x68, 0x8C, 0x2B, 0x3E, 0x6C, 0x1F,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 0x1F, 0x83, 0xD9, 0xAB, 0xFB, 0x41, 0xBD, 0x6B,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 0x5B, 0xE0, 0xCD, 0x19, 0x13, 0x7E, 0x21, 0x79,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* 128 bit counter total message bit length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* allocate one page stckf buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) pg = (u8 *) __get_free_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (!pg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) prng_errorflag = PRNG_GEN_ENTROPY_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* fill the ebuf in chunks of 64 byte each */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) while (nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* fill lower 2k with urandom bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) get_random_bytes(pg, PAGE_SIZE / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* exor upper 2k with 512 stckf values, offset 4 bytes each */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) for (n = 0; n < 512; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int offset = (PAGE_SIZE / 2) + (n * 4) - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) u64 *p = (u64 *)(pg + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) *p ^= get_tod_clock_fast();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* hash over the filled page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) cpacf_klmd(CPACF_KLMD_SHA_512, pblock, pg, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) n = (nbytes < 64) ? nbytes : 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) memcpy(ebuf, pblock, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ret += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ebuf += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) nbytes -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) memzero_explicit(pblock, sizeof(pblock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) memzero_explicit(pg, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) free_page((unsigned long)pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /*** tdes functions ***/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static void prng_tdes_add_entropy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) __u64 entropy[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) for (i = 0; i < 16; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) cpacf_kmc(CPACF_KMC_PRNG, prng_data->prngws.parm_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) (char *) entropy, (char *) entropy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) sizeof(entropy));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) memcpy(prng_data->prngws.parm_block, entropy, sizeof(entropy));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static void prng_tdes_seed(int nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) char buf[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) BUG_ON(nbytes > sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) get_random_bytes(buf, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /* Add the entropy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) while (nbytes >= 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) *((__u64 *)prng_data->prngws.parm_block) ^= *((__u64 *)(buf+i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) prng_tdes_add_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) i += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) nbytes -= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) prng_tdes_add_entropy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) prng_data->prngws.reseed_counter = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int __init prng_tdes_instantiate(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) int datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) pr_debug("prng runs in TDES mode with "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) "chunksize=%d and reseed_limit=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) prng_chunk_size, prng_reseed_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* memory allocation, prng_data struct init, mutex init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) datalen = sizeof(struct prng_data_s) + prng_chunk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) prng_data = kzalloc(datalen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (!prng_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) prng_errorflag = PRNG_INSTANTIATE_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) mutex_init(&prng_data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) prng_data->buf = ((u8 *)prng_data) + sizeof(struct prng_data_s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) memcpy(prng_data->prngws.parm_block, initial_parm_block, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /* initialize the PRNG, add 128 bits of entropy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) prng_tdes_seed(16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static void prng_tdes_deinstantiate(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) pr_debug("The prng module stopped "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) "after running in triple DES mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) kfree_sensitive(prng_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /*** sha512 functions ***/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static int __init prng_sha512_selftest(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* NIST DRBG testvector for Hash Drbg, Sha-512, Count #0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static const u8 seed[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 0x6b, 0x50, 0xa7, 0xd8, 0xf8, 0xa5, 0x5d, 0x7a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 0x3d, 0xf8, 0xbb, 0x40, 0xbc, 0xc3, 0xb7, 0x22,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 0xd8, 0x70, 0x8d, 0xe6, 0x7f, 0xda, 0x01, 0x0b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 0x03, 0xc4, 0xc8, 0x4d, 0x72, 0x09, 0x6f, 0x8c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 0x3e, 0xc6, 0x49, 0xcc, 0x62, 0x56, 0xd9, 0xfa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 0x31, 0xdb, 0x7a, 0x29, 0x04, 0xaa, 0xf0, 0x25 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static const u8 V0[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 0x00, 0xad, 0xe3, 0x6f, 0x9a, 0x01, 0xc7, 0x76,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 0x61, 0x34, 0x35, 0xf5, 0x4e, 0x24, 0x74, 0x22,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 0x21, 0x9a, 0x29, 0x89, 0xc7, 0x93, 0x2e, 0x60,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 0x1e, 0xe8, 0x14, 0x24, 0x8d, 0xd5, 0x03, 0xf1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 0x65, 0x5d, 0x08, 0x22, 0x72, 0xd5, 0xad, 0x95,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 0xe1, 0x23, 0x1e, 0x8a, 0xa7, 0x13, 0xd9, 0x2b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 0x5e, 0xbc, 0xbb, 0x80, 0xab, 0x8d, 0xe5, 0x79,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 0xab, 0x5b, 0x47, 0x4e, 0xdd, 0xee, 0x6b, 0x03,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 0x8f, 0x0f, 0x5c, 0x5e, 0xa9, 0x1a, 0x83, 0xdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 0xd3, 0x88, 0xb2, 0x75, 0x4b, 0xce, 0x83, 0x36,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 0x57, 0x4b, 0xf1, 0x5c, 0xca, 0x7e, 0x09, 0xc0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 0xd3, 0x89, 0xc6, 0xe0, 0xda, 0xc4, 0x81, 0x7e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 0x5b, 0xf9, 0xe1, 0x01, 0xc1, 0x92, 0x05, 0xea,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 0xf5, 0x2f, 0xc6, 0xc6, 0xc7, 0x8f, 0xbc, 0xf4 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static const u8 C0[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 0x00, 0xf4, 0xa3, 0xe5, 0xa0, 0x72, 0x63, 0x95,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 0xc6, 0x4f, 0x48, 0xd0, 0x8b, 0x5b, 0x5f, 0x8e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 0x6b, 0x96, 0x1f, 0x16, 0xed, 0xbc, 0x66, 0x94,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 0x45, 0x31, 0xd7, 0x47, 0x73, 0x22, 0xa5, 0x86,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 0xce, 0xc0, 0x4c, 0xac, 0x63, 0xb8, 0x39, 0x50,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 0xbf, 0xe6, 0x59, 0x6c, 0x38, 0x58, 0x99, 0x1f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 0x27, 0xa7, 0x9d, 0x71, 0x2a, 0xb3, 0x7b, 0xf9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 0xfb, 0x17, 0x86, 0xaa, 0x99, 0x81, 0xaa, 0x43,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 0xe4, 0x37, 0xd3, 0x1e, 0x6e, 0xe5, 0xe6, 0xee,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 0xc2, 0xed, 0x95, 0x4f, 0x53, 0x0e, 0x46, 0x8a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 0xcc, 0x45, 0xa5, 0xdb, 0x69, 0x0d, 0x81, 0xc9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 0x32, 0x92, 0xbc, 0x8f, 0x33, 0xe6, 0xf6, 0x09,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 0x7c, 0x8e, 0x05, 0x19, 0x0d, 0xf1, 0xb6, 0xcc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 0xf3, 0x02, 0x21, 0x90, 0x25, 0xec, 0xed, 0x0e };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static const u8 random[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 0x95, 0xb7, 0xf1, 0x7e, 0x98, 0x02, 0xd3, 0x57,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 0x73, 0x92, 0xc6, 0xa9, 0xc0, 0x80, 0x83, 0xb6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 0x7d, 0xd1, 0x29, 0x22, 0x65, 0xb5, 0xf4, 0x2d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 0x23, 0x7f, 0x1c, 0x55, 0xbb, 0x9b, 0x10, 0xbf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 0xcf, 0xd8, 0x2c, 0x77, 0xa3, 0x78, 0xb8, 0x26,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 0x6a, 0x00, 0x99, 0x14, 0x3b, 0x3c, 0x2d, 0x64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 0x61, 0x1e, 0xee, 0xb6, 0x9a, 0xcd, 0xc0, 0x55,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 0x95, 0x7c, 0x13, 0x9e, 0x8b, 0x19, 0x0c, 0x7a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 0x06, 0x95, 0x5f, 0x2c, 0x79, 0x7c, 0x27, 0x78,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 0xde, 0x94, 0x03, 0x96, 0xa5, 0x01, 0xf4, 0x0e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 0x91, 0x39, 0x6a, 0xcf, 0x8d, 0x7e, 0x45, 0xeb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 0xdb, 0xb5, 0x3b, 0xbf, 0x8c, 0x97, 0x52, 0x30,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 0xd2, 0xf0, 0xff, 0x91, 0x06, 0xc7, 0x61, 0x19,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 0xae, 0x49, 0x8e, 0x7f, 0xbc, 0x03, 0xd9, 0x0f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 0x8e, 0x4c, 0x51, 0x62, 0x7a, 0xed, 0x5c, 0x8d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 0x42, 0x63, 0xd5, 0xd2, 0xb9, 0x78, 0x87, 0x3a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 0x0d, 0xe5, 0x96, 0xee, 0x6d, 0xc7, 0xf7, 0xc2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 0x9e, 0x37, 0xee, 0xe8, 0xb3, 0x4c, 0x90, 0xdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 0x1c, 0xf6, 0xa9, 0xdd, 0xb2, 0x2b, 0x4c, 0xbd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 0x08, 0x6b, 0x14, 0xb3, 0x5d, 0xe9, 0x3d, 0xa2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 0xd5, 0xcb, 0x18, 0x06, 0x69, 0x8c, 0xbd, 0x7b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 0xbb, 0x67, 0xbf, 0xe3, 0xd3, 0x1f, 0xd2, 0xd1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 0xdb, 0xd2, 0xa1, 0xe0, 0x58, 0xa3, 0xeb, 0x99,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 0xd7, 0xe5, 0x1f, 0x1a, 0x93, 0x8e, 0xed, 0x5e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 0x1c, 0x1d, 0xe2, 0x3a, 0x6b, 0x43, 0x45, 0xd3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 0x19, 0x14, 0x09, 0xf9, 0x2f, 0x39, 0xb3, 0x67,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 0x0d, 0x8d, 0xbf, 0xb6, 0x35, 0xd8, 0xe6, 0xa3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 0x69, 0x32, 0xd8, 0x10, 0x33, 0xd1, 0x44, 0x8d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 0x63, 0xb4, 0x03, 0xdd, 0xf8, 0x8e, 0x12, 0x1b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 0x6e, 0x81, 0x9a, 0xc3, 0x81, 0x22, 0x6c, 0x13,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 0x21, 0xe4, 0xb0, 0x86, 0x44, 0xf6, 0x72, 0x7c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 0x36, 0x8c, 0x5a, 0x9f, 0x7a, 0x4b, 0x3e, 0xe2 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) u8 buf[sizeof(random)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct prno_ws_s ws;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) memset(&ws, 0, sizeof(ws));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /* initial seed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) cpacf_prno(CPACF_PRNO_SHA512_DRNG_SEED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) &ws, NULL, 0, seed, sizeof(seed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /* check working states V and C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (memcmp(ws.V, V0, sizeof(V0)) != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) || memcmp(ws.C, C0, sizeof(C0)) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) pr_err("The prng self test state test "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) "for the SHA-512 mode failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) prng_errorflag = PRNG_SELFTEST_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* generate random bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) cpacf_prno(CPACF_PRNO_SHA512_DRNG_GEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) &ws, buf, sizeof(buf), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) cpacf_prno(CPACF_PRNO_SHA512_DRNG_GEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) &ws, buf, sizeof(buf), NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /* check against expected data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (memcmp(buf, random, sizeof(random)) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) pr_err("The prng self test data test "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) "for the SHA-512 mode failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) prng_errorflag = PRNG_SELFTEST_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static int __init prng_sha512_instantiate(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int ret, datalen, seedlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) u8 seed[128 + 16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) pr_debug("prng runs in SHA-512 mode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) "with chunksize=%d and reseed_limit=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) prng_chunk_size, prng_reseed_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /* memory allocation, prng_data struct init, mutex init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) datalen = sizeof(struct prng_data_s) + prng_chunk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (fips_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) datalen += prng_chunk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) prng_data = kzalloc(datalen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (!prng_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) prng_errorflag = PRNG_INSTANTIATE_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) mutex_init(&prng_data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) prng_data->buf = ((u8 *)prng_data) + sizeof(struct prng_data_s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /* selftest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) ret = prng_sha512_selftest();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) goto outfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /* generate initial seed, we need at least 256 + 128 bits entropy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (trng_available) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * Trng available, so use it. The trng works in chunks of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * 32 bytes and produces 100% entropy. So we pull 64 bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * which gives us 512 bits entropy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) seedlen = 2 * 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) cpacf_trng(NULL, 0, seed, seedlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * No trng available, so use the generate_entropy() function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * This function works in 64 byte junks and produces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * 50% entropy. So we pull 2*64 bytes which gives us 512 bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * of entropy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) seedlen = 2 * 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) ret = generate_entropy(seed, seedlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (ret != seedlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) goto outfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* append the seed by 16 bytes of unique nonce */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) get_tod_clock_ext(seed + seedlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) seedlen += 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* now initial seed of the prno drng */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) cpacf_prno(CPACF_PRNO_SHA512_DRNG_SEED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) &prng_data->prnows, NULL, 0, seed, seedlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) memzero_explicit(seed, sizeof(seed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /* if fips mode is enabled, generate a first block of random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) bytes for the FIPS 140-2 Conditional Self Test */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (fips_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) prng_data->prev = prng_data->buf + prng_chunk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) cpacf_prno(CPACF_PRNO_SHA512_DRNG_GEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) &prng_data->prnows,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) prng_data->prev, prng_chunk_size, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) outfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) kfree(prng_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static void prng_sha512_deinstantiate(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) pr_debug("The prng module stopped after running in SHA-512 mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) kfree_sensitive(prng_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) static int prng_sha512_reseed(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) int ret, seedlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) u8 seed[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) /* We need at least 256 bits of fresh entropy for reseeding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (trng_available) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /* trng produces 256 bits entropy in 32 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) seedlen = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) cpacf_trng(NULL, 0, seed, seedlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /* generate_entropy() produces 256 bits entropy in 64 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) seedlen = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) ret = generate_entropy(seed, seedlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (ret != sizeof(seed))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /* do a reseed of the prno drng with this bytestring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) cpacf_prno(CPACF_PRNO_SHA512_DRNG_SEED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) &prng_data->prnows, NULL, 0, seed, seedlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) memzero_explicit(seed, sizeof(seed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) static int prng_sha512_generate(u8 *buf, size_t nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /* reseed needed ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (prng_data->prnows.reseed_counter > prng_reseed_limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) ret = prng_sha512_reseed();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /* PRNO generate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) cpacf_prno(CPACF_PRNO_SHA512_DRNG_GEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) &prng_data->prnows, buf, nbytes, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /* FIPS 140-2 Conditional Self Test */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (fips_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (!memcmp(prng_data->prev, buf, nbytes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) prng_errorflag = PRNG_GEN_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) memcpy(prng_data->prev, buf, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) /*** file io functions ***/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static int prng_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return nonseekable_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) static ssize_t prng_tdes_read(struct file *file, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) size_t nbytes, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) int chunk, n, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) /* lock prng_data struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (mutex_lock_interruptible(&prng_data->mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) while (nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (need_resched()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) /* give mutex free before calling schedule() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) mutex_unlock(&prng_data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) /* occopy mutex again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (mutex_lock_interruptible(&prng_data->mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) * we lose some random bytes if an attacker issues
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * reads < 8 bytes, but we don't care
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) chunk = min_t(int, nbytes, prng_chunk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) /* PRNG only likes multiples of 8 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) n = (chunk + 7) & -8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) if (prng_data->prngws.reseed_counter > prng_reseed_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) prng_tdes_seed(8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) /* if the CPU supports PRNG stckf is present too */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) *((unsigned long long *)prng_data->buf) = get_tod_clock_fast();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * Beside the STCKF the input for the TDES-EDE is the output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * of the last operation. We differ here from X9.17 since we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * only store one timestamp into the buffer. Padding the whole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * buffer with timestamps does not improve security, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * successive stckf have nearly constant offsets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * If an attacker knows the first timestamp it would be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * trivial to guess the additional values. One timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * is therefore enough and still guarantees unique input values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * Note: you can still get strict X9.17 conformity by setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * prng_chunk_size to 8 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) cpacf_kmc(CPACF_KMC_PRNG, prng_data->prngws.parm_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) prng_data->buf, prng_data->buf, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) prng_data->prngws.byte_counter += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) prng_data->prngws.reseed_counter += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (copy_to_user(ubuf, prng_data->buf, chunk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) nbytes -= chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) ret += chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) ubuf += chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) /* unlock prng_data struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) mutex_unlock(&prng_data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) static ssize_t prng_sha512_read(struct file *file, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) size_t nbytes, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) int n, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) u8 *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) /* if errorflag is set do nothing and return 'broken pipe' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (prng_errorflag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) /* lock prng_data struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (mutex_lock_interruptible(&prng_data->mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) while (nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (need_resched()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) /* give mutex free before calling schedule() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) mutex_unlock(&prng_data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) /* occopy mutex again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (mutex_lock_interruptible(&prng_data->mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (prng_data->rest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) /* push left over random bytes from the previous read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) p = prng_data->buf + prng_chunk_size - prng_data->rest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) n = (nbytes < prng_data->rest) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) nbytes : prng_data->rest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) prng_data->rest -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) /* generate one chunk of random bytes into read buf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) p = prng_data->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) n = prng_sha512_generate(p, prng_chunk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (n < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) ret = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (nbytes < prng_chunk_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) n = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) prng_data->rest = prng_chunk_size - n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) n = prng_chunk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) prng_data->rest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if (copy_to_user(ubuf, p, n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) memzero_explicit(p, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) ubuf += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) nbytes -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) ret += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) /* unlock prng_data struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) mutex_unlock(&prng_data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /*** sysfs stuff ***/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) static const struct file_operations prng_sha512_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) .open = &prng_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) .release = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) .read = &prng_sha512_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) static const struct file_operations prng_tdes_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) .open = &prng_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) .release = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) .read = &prng_tdes_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) static struct miscdevice prng_sha512_dev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) .name = "prandom",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) .minor = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) .fops = &prng_sha512_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static struct miscdevice prng_tdes_dev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) .name = "prandom",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) .minor = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) .mode = 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) .fops = &prng_tdes_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) /* chunksize attribute (ro) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) static ssize_t prng_chunksize_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return scnprintf(buf, PAGE_SIZE, "%u\n", prng_chunk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) static DEVICE_ATTR(chunksize, 0444, prng_chunksize_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) /* counter attribute (ro) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) static ssize_t prng_counter_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) u64 counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (mutex_lock_interruptible(&prng_data->mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) if (prng_mode == PRNG_MODE_SHA512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) counter = prng_data->prnows.stream_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) counter = prng_data->prngws.byte_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) mutex_unlock(&prng_data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) return scnprintf(buf, PAGE_SIZE, "%llu\n", counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) static DEVICE_ATTR(byte_counter, 0444, prng_counter_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) /* errorflag attribute (ro) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) static ssize_t prng_errorflag_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) return scnprintf(buf, PAGE_SIZE, "%d\n", prng_errorflag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) static DEVICE_ATTR(errorflag, 0444, prng_errorflag_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) /* mode attribute (ro) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) static ssize_t prng_mode_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (prng_mode == PRNG_MODE_TDES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) return scnprintf(buf, PAGE_SIZE, "TDES\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) return scnprintf(buf, PAGE_SIZE, "SHA512\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) static DEVICE_ATTR(mode, 0444, prng_mode_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) /* reseed attribute (w) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) static ssize_t prng_reseed_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (mutex_lock_interruptible(&prng_data->mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) prng_sha512_reseed();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) mutex_unlock(&prng_data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) static DEVICE_ATTR(reseed, 0200, NULL, prng_reseed_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) /* reseed limit attribute (rw) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) static ssize_t prng_reseed_limit_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) return scnprintf(buf, PAGE_SIZE, "%u\n", prng_reseed_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) static ssize_t prng_reseed_limit_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) unsigned limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (sscanf(buf, "%u\n", &limit) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) if (prng_mode == PRNG_MODE_SHA512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) if (limit < PRNG_RESEED_LIMIT_SHA512_LOWER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (limit < PRNG_RESEED_LIMIT_TDES_LOWER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) prng_reseed_limit = limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) static DEVICE_ATTR(reseed_limit, 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) prng_reseed_limit_show, prng_reseed_limit_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) /* strength attribute (ro) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) static ssize_t prng_strength_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) return scnprintf(buf, PAGE_SIZE, "256\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) static DEVICE_ATTR(strength, 0444, prng_strength_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) static struct attribute *prng_sha512_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) &dev_attr_errorflag.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) &dev_attr_chunksize.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) &dev_attr_byte_counter.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) &dev_attr_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) &dev_attr_reseed.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) &dev_attr_reseed_limit.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) &dev_attr_strength.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) static struct attribute *prng_tdes_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) &dev_attr_chunksize.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) &dev_attr_byte_counter.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) &dev_attr_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) static struct attribute_group prng_sha512_dev_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) .attrs = prng_sha512_dev_attrs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) static struct attribute_group prng_tdes_dev_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) .attrs = prng_tdes_dev_attrs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) /*** module init and exit ***/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) static int __init prng_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) /* check if the CPU has a PRNG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) if (!cpacf_query_func(CPACF_KMC, CPACF_KMC_PRNG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) /* check if TRNG subfunction is available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_TRNG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) trng_available = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /* choose prng mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (prng_mode != PRNG_MODE_TDES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) /* check for MSA5 support for PRNO operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (!cpacf_query_func(CPACF_PRNO, CPACF_PRNO_SHA512_DRNG_GEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (prng_mode == PRNG_MODE_SHA512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) pr_err("The prng module cannot "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) "start in SHA-512 mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) prng_mode = PRNG_MODE_TDES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) prng_mode = PRNG_MODE_SHA512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (prng_mode == PRNG_MODE_SHA512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) /* SHA512 mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (prng_chunk_size < PRNG_CHUNKSIZE_SHA512_MIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) || prng_chunk_size > PRNG_CHUNKSIZE_SHA512_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) prng_chunk_size = (prng_chunk_size + 0x3f) & ~0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (prng_reseed_limit == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) prng_reseed_limit = PRNG_RESEED_LIMIT_SHA512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) else if (prng_reseed_limit < PRNG_RESEED_LIMIT_SHA512_LOWER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) ret = prng_sha512_instantiate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) ret = misc_register(&prng_sha512_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) prng_sha512_deinstantiate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) ret = sysfs_create_group(&prng_sha512_dev.this_device->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) &prng_sha512_dev_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) misc_deregister(&prng_sha512_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) prng_sha512_deinstantiate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) /* TDES mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (prng_chunk_size < PRNG_CHUNKSIZE_TDES_MIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) || prng_chunk_size > PRNG_CHUNKSIZE_TDES_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) prng_chunk_size = (prng_chunk_size + 0x07) & ~0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) if (prng_reseed_limit == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) prng_reseed_limit = PRNG_RESEED_LIMIT_TDES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) else if (prng_reseed_limit < PRNG_RESEED_LIMIT_TDES_LOWER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) ret = prng_tdes_instantiate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) ret = misc_register(&prng_tdes_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) prng_tdes_deinstantiate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) ret = sysfs_create_group(&prng_tdes_dev.this_device->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) &prng_tdes_dev_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) misc_deregister(&prng_tdes_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) prng_tdes_deinstantiate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) static void __exit prng_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) if (prng_mode == PRNG_MODE_SHA512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) sysfs_remove_group(&prng_sha512_dev.this_device->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) &prng_sha512_dev_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) misc_deregister(&prng_sha512_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) prng_sha512_deinstantiate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) sysfs_remove_group(&prng_tdes_dev.this_device->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) &prng_tdes_dev_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) misc_deregister(&prng_tdes_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) prng_tdes_deinstantiate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) module_cpu_feature_match(MSA, prng_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) module_exit(prng_exit);