prng.c 22 KB

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