prng.c 22 KB

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