prng.c 22 KB

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