prng.c 24 KB

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