prng.c 22 KB

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