prng.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. if (cpacf_kimd(CPACF_KIMD_SHA_256, h,
  113. pg, PAGE_SIZE) != PAGE_SIZE) {
  114. prng_errorflag = PRNG_GEN_ENTROPY_FAILED;
  115. ret = -EIO;
  116. goto out;
  117. }
  118. if (n < sizeof(hash))
  119. memcpy(ebuf, hash, n);
  120. ret += n;
  121. ebuf += n;
  122. nbytes -= n;
  123. }
  124. out:
  125. free_page((unsigned long)pg);
  126. return ret;
  127. }
  128. /*** tdes functions ***/
  129. static void prng_tdes_add_entropy(void)
  130. {
  131. __u64 entropy[4];
  132. unsigned int i;
  133. int ret;
  134. for (i = 0; i < 16; i++) {
  135. ret = cpacf_kmc(CPACF_KMC_PRNG, prng_data->prngws.parm_block,
  136. (char *)entropy, (char *)entropy,
  137. sizeof(entropy));
  138. BUG_ON(ret < 0 || ret != 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. int ret = 0;
  259. u8 buf[sizeof(random)];
  260. struct ppno_ws_s ws;
  261. memset(&ws, 0, sizeof(ws));
  262. /* initial seed */
  263. ret = cpacf_ppno(CPACF_PPNO_SHA512_DRNG_SEED, &ws, NULL, 0,
  264. seed, sizeof(seed));
  265. if (ret < 0) {
  266. pr_err("The prng self test seed operation for the "
  267. "SHA-512 mode failed with rc=%d\n", ret);
  268. prng_errorflag = PRNG_SELFTEST_FAILED;
  269. return -EIO;
  270. }
  271. /* check working states V and C */
  272. if (memcmp(ws.V, V0, sizeof(V0)) != 0
  273. || memcmp(ws.C, C0, sizeof(C0)) != 0) {
  274. pr_err("The prng self test state test "
  275. "for the SHA-512 mode failed\n");
  276. prng_errorflag = PRNG_SELFTEST_FAILED;
  277. return -EIO;
  278. }
  279. /* generate random bytes */
  280. ret = cpacf_ppno(CPACF_PPNO_SHA512_DRNG_GEN,
  281. &ws, buf, sizeof(buf), NULL, 0);
  282. if (ret < 0) {
  283. pr_err("The prng self test generate operation for "
  284. "the SHA-512 mode failed with rc=%d\n", ret);
  285. prng_errorflag = PRNG_SELFTEST_FAILED;
  286. return -EIO;
  287. }
  288. ret = cpacf_ppno(CPACF_PPNO_SHA512_DRNG_GEN,
  289. &ws, buf, sizeof(buf), NULL, 0);
  290. if (ret < 0) {
  291. pr_err("The prng self test generate operation for "
  292. "the SHA-512 mode failed with rc=%d\n", ret);
  293. prng_errorflag = PRNG_SELFTEST_FAILED;
  294. return -EIO;
  295. }
  296. /* check against expected data */
  297. if (memcmp(buf, random, sizeof(random)) != 0) {
  298. pr_err("The prng self test data test "
  299. "for the SHA-512 mode failed\n");
  300. prng_errorflag = PRNG_SELFTEST_FAILED;
  301. return -EIO;
  302. }
  303. return 0;
  304. }
  305. static int __init prng_sha512_instantiate(void)
  306. {
  307. int ret, datalen;
  308. u8 seed[64];
  309. pr_debug("prng runs in SHA-512 mode "
  310. "with chunksize=%d and reseed_limit=%u\n",
  311. prng_chunk_size, prng_reseed_limit);
  312. /* memory allocation, prng_data struct init, mutex init */
  313. datalen = sizeof(struct prng_data_s) + prng_chunk_size;
  314. if (fips_enabled)
  315. datalen += prng_chunk_size;
  316. prng_data = kzalloc(datalen, GFP_KERNEL);
  317. if (!prng_data) {
  318. prng_errorflag = PRNG_INSTANTIATE_FAILED;
  319. return -ENOMEM;
  320. }
  321. mutex_init(&prng_data->mutex);
  322. prng_data->buf = ((u8 *)prng_data) + sizeof(struct prng_data_s);
  323. /* selftest */
  324. ret = prng_sha512_selftest();
  325. if (ret)
  326. goto outfree;
  327. /* generate initial seed bytestring, first 48 bytes of entropy */
  328. ret = generate_entropy(seed, 48);
  329. if (ret != 48)
  330. goto outfree;
  331. /* followed by 16 bytes of unique nonce */
  332. get_tod_clock_ext(seed + 48);
  333. /* initial seed of the ppno drng */
  334. ret = cpacf_ppno(CPACF_PPNO_SHA512_DRNG_SEED,
  335. &prng_data->ppnows, NULL, 0, seed, sizeof(seed));
  336. if (ret < 0) {
  337. prng_errorflag = PRNG_SEED_FAILED;
  338. ret = -EIO;
  339. goto outfree;
  340. }
  341. /* if fips mode is enabled, generate a first block of random
  342. bytes for the FIPS 140-2 Conditional Self Test */
  343. if (fips_enabled) {
  344. prng_data->prev = prng_data->buf + prng_chunk_size;
  345. ret = cpacf_ppno(CPACF_PPNO_SHA512_DRNG_GEN,
  346. &prng_data->ppnows,
  347. prng_data->prev, prng_chunk_size, NULL, 0);
  348. if (ret < 0 || ret != prng_chunk_size) {
  349. prng_errorflag = PRNG_GEN_FAILED;
  350. ret = -EIO;
  351. goto outfree;
  352. }
  353. }
  354. return 0;
  355. outfree:
  356. kfree(prng_data);
  357. return ret;
  358. }
  359. static void prng_sha512_deinstantiate(void)
  360. {
  361. pr_debug("The prng module stopped after running in SHA-512 mode\n");
  362. kzfree(prng_data);
  363. }
  364. static int prng_sha512_reseed(void)
  365. {
  366. int ret;
  367. u8 seed[32];
  368. /* generate 32 bytes of fresh entropy */
  369. ret = generate_entropy(seed, sizeof(seed));
  370. if (ret != sizeof(seed))
  371. return ret;
  372. /* do a reseed of the ppno drng with this bytestring */
  373. ret = cpacf_ppno(CPACF_PPNO_SHA512_DRNG_SEED,
  374. &prng_data->ppnows, NULL, 0, seed, sizeof(seed));
  375. if (ret) {
  376. prng_errorflag = PRNG_RESEED_FAILED;
  377. return -EIO;
  378. }
  379. return 0;
  380. }
  381. static int prng_sha512_generate(u8 *buf, size_t nbytes)
  382. {
  383. int ret;
  384. /* reseed needed ? */
  385. if (prng_data->ppnows.reseed_counter > prng_reseed_limit) {
  386. ret = prng_sha512_reseed();
  387. if (ret)
  388. return ret;
  389. }
  390. /* PPNO generate */
  391. ret = cpacf_ppno(CPACF_PPNO_SHA512_DRNG_GEN,
  392. &prng_data->ppnows, buf, nbytes, NULL, 0);
  393. if (ret < 0 || ret != nbytes) {
  394. prng_errorflag = PRNG_GEN_FAILED;
  395. return -EIO;
  396. }
  397. /* FIPS 140-2 Conditional Self Test */
  398. if (fips_enabled) {
  399. if (!memcmp(prng_data->prev, buf, nbytes)) {
  400. prng_errorflag = PRNG_GEN_FAILED;
  401. return -EILSEQ;
  402. }
  403. memcpy(prng_data->prev, buf, nbytes);
  404. }
  405. return ret;
  406. }
  407. /*** file io functions ***/
  408. static int prng_open(struct inode *inode, struct file *file)
  409. {
  410. return nonseekable_open(inode, file);
  411. }
  412. static ssize_t prng_tdes_read(struct file *file, char __user *ubuf,
  413. size_t nbytes, loff_t *ppos)
  414. {
  415. int chunk, n, tmp, ret = 0;
  416. /* lock prng_data struct */
  417. if (mutex_lock_interruptible(&prng_data->mutex))
  418. return -ERESTARTSYS;
  419. while (nbytes) {
  420. if (need_resched()) {
  421. if (signal_pending(current)) {
  422. if (ret == 0)
  423. ret = -ERESTARTSYS;
  424. break;
  425. }
  426. /* give mutex free before calling schedule() */
  427. mutex_unlock(&prng_data->mutex);
  428. schedule();
  429. /* occopy mutex again */
  430. if (mutex_lock_interruptible(&prng_data->mutex)) {
  431. if (ret == 0)
  432. ret = -ERESTARTSYS;
  433. return ret;
  434. }
  435. }
  436. /*
  437. * we lose some random bytes if an attacker issues
  438. * reads < 8 bytes, but we don't care
  439. */
  440. chunk = min_t(int, nbytes, prng_chunk_size);
  441. /* PRNG only likes multiples of 8 bytes */
  442. n = (chunk + 7) & -8;
  443. if (prng_data->prngws.reseed_counter > prng_reseed_limit)
  444. prng_tdes_seed(8);
  445. /* if the CPU supports PRNG stckf is present too */
  446. *((unsigned long long *)prng_data->buf) = get_tod_clock_fast();
  447. /*
  448. * Beside the STCKF the input for the TDES-EDE is the output
  449. * of the last operation. We differ here from X9.17 since we
  450. * only store one timestamp into the buffer. Padding the whole
  451. * buffer with timestamps does not improve security, since
  452. * successive stckf have nearly constant offsets.
  453. * If an attacker knows the first timestamp it would be
  454. * trivial to guess the additional values. One timestamp
  455. * is therefore enough and still guarantees unique input values.
  456. *
  457. * Note: you can still get strict X9.17 conformity by setting
  458. * prng_chunk_size to 8 bytes.
  459. */
  460. tmp = cpacf_kmc(CPACF_KMC_PRNG, prng_data->prngws.parm_block,
  461. prng_data->buf, prng_data->buf, n);
  462. if (tmp < 0 || tmp != n) {
  463. ret = -EIO;
  464. break;
  465. }
  466. prng_data->prngws.byte_counter += n;
  467. prng_data->prngws.reseed_counter += n;
  468. if (copy_to_user(ubuf, prng_data->buf, chunk))
  469. return -EFAULT;
  470. nbytes -= chunk;
  471. ret += chunk;
  472. ubuf += chunk;
  473. }
  474. /* unlock prng_data struct */
  475. mutex_unlock(&prng_data->mutex);
  476. return ret;
  477. }
  478. static ssize_t prng_sha512_read(struct file *file, char __user *ubuf,
  479. size_t nbytes, loff_t *ppos)
  480. {
  481. int n, ret = 0;
  482. u8 *p;
  483. /* if errorflag is set do nothing and return 'broken pipe' */
  484. if (prng_errorflag)
  485. return -EPIPE;
  486. /* lock prng_data struct */
  487. if (mutex_lock_interruptible(&prng_data->mutex))
  488. return -ERESTARTSYS;
  489. while (nbytes) {
  490. if (need_resched()) {
  491. if (signal_pending(current)) {
  492. if (ret == 0)
  493. ret = -ERESTARTSYS;
  494. break;
  495. }
  496. /* give mutex free before calling schedule() */
  497. mutex_unlock(&prng_data->mutex);
  498. schedule();
  499. /* occopy mutex again */
  500. if (mutex_lock_interruptible(&prng_data->mutex)) {
  501. if (ret == 0)
  502. ret = -ERESTARTSYS;
  503. return ret;
  504. }
  505. }
  506. if (prng_data->rest) {
  507. /* push left over random bytes from the previous read */
  508. p = prng_data->buf + prng_chunk_size - prng_data->rest;
  509. n = (nbytes < prng_data->rest) ?
  510. nbytes : prng_data->rest;
  511. prng_data->rest -= n;
  512. } else {
  513. /* generate one chunk of random bytes into read buf */
  514. p = prng_data->buf;
  515. n = prng_sha512_generate(p, prng_chunk_size);
  516. if (n < 0) {
  517. ret = n;
  518. break;
  519. }
  520. if (nbytes < prng_chunk_size) {
  521. n = nbytes;
  522. prng_data->rest = prng_chunk_size - n;
  523. } else {
  524. n = prng_chunk_size;
  525. prng_data->rest = 0;
  526. }
  527. }
  528. if (copy_to_user(ubuf, p, n)) {
  529. ret = -EFAULT;
  530. break;
  531. }
  532. ubuf += n;
  533. nbytes -= n;
  534. ret += n;
  535. }
  536. /* unlock prng_data struct */
  537. mutex_unlock(&prng_data->mutex);
  538. return ret;
  539. }
  540. /*** sysfs stuff ***/
  541. static const struct file_operations prng_sha512_fops = {
  542. .owner = THIS_MODULE,
  543. .open = &prng_open,
  544. .release = NULL,
  545. .read = &prng_sha512_read,
  546. .llseek = noop_llseek,
  547. };
  548. static const struct file_operations prng_tdes_fops = {
  549. .owner = THIS_MODULE,
  550. .open = &prng_open,
  551. .release = NULL,
  552. .read = &prng_tdes_read,
  553. .llseek = noop_llseek,
  554. };
  555. static struct miscdevice prng_sha512_dev = {
  556. .name = "prandom",
  557. .minor = MISC_DYNAMIC_MINOR,
  558. .mode = 0644,
  559. .fops = &prng_sha512_fops,
  560. };
  561. static struct miscdevice prng_tdes_dev = {
  562. .name = "prandom",
  563. .minor = MISC_DYNAMIC_MINOR,
  564. .mode = 0644,
  565. .fops = &prng_tdes_fops,
  566. };
  567. /* chunksize attribute (ro) */
  568. static ssize_t prng_chunksize_show(struct device *dev,
  569. struct device_attribute *attr,
  570. char *buf)
  571. {
  572. return snprintf(buf, PAGE_SIZE, "%u\n", prng_chunk_size);
  573. }
  574. static DEVICE_ATTR(chunksize, 0444, prng_chunksize_show, NULL);
  575. /* counter attribute (ro) */
  576. static ssize_t prng_counter_show(struct device *dev,
  577. struct device_attribute *attr,
  578. char *buf)
  579. {
  580. u64 counter;
  581. if (mutex_lock_interruptible(&prng_data->mutex))
  582. return -ERESTARTSYS;
  583. if (prng_mode == PRNG_MODE_SHA512)
  584. counter = prng_data->ppnows.stream_bytes;
  585. else
  586. counter = prng_data->prngws.byte_counter;
  587. mutex_unlock(&prng_data->mutex);
  588. return snprintf(buf, PAGE_SIZE, "%llu\n", counter);
  589. }
  590. static DEVICE_ATTR(byte_counter, 0444, prng_counter_show, NULL);
  591. /* errorflag attribute (ro) */
  592. static ssize_t prng_errorflag_show(struct device *dev,
  593. struct device_attribute *attr,
  594. char *buf)
  595. {
  596. return snprintf(buf, PAGE_SIZE, "%d\n", prng_errorflag);
  597. }
  598. static DEVICE_ATTR(errorflag, 0444, prng_errorflag_show, NULL);
  599. /* mode attribute (ro) */
  600. static ssize_t prng_mode_show(struct device *dev,
  601. struct device_attribute *attr,
  602. char *buf)
  603. {
  604. if (prng_mode == PRNG_MODE_TDES)
  605. return snprintf(buf, PAGE_SIZE, "TDES\n");
  606. else
  607. return snprintf(buf, PAGE_SIZE, "SHA512\n");
  608. }
  609. static DEVICE_ATTR(mode, 0444, prng_mode_show, NULL);
  610. /* reseed attribute (w) */
  611. static ssize_t prng_reseed_store(struct device *dev,
  612. struct device_attribute *attr,
  613. const char *buf, size_t count)
  614. {
  615. if (mutex_lock_interruptible(&prng_data->mutex))
  616. return -ERESTARTSYS;
  617. prng_sha512_reseed();
  618. mutex_unlock(&prng_data->mutex);
  619. return count;
  620. }
  621. static DEVICE_ATTR(reseed, 0200, NULL, prng_reseed_store);
  622. /* reseed limit attribute (rw) */
  623. static ssize_t prng_reseed_limit_show(struct device *dev,
  624. struct device_attribute *attr,
  625. char *buf)
  626. {
  627. return snprintf(buf, PAGE_SIZE, "%u\n", prng_reseed_limit);
  628. }
  629. static ssize_t prng_reseed_limit_store(struct device *dev,
  630. struct device_attribute *attr,
  631. const char *buf, size_t count)
  632. {
  633. unsigned limit;
  634. if (sscanf(buf, "%u\n", &limit) != 1)
  635. return -EINVAL;
  636. if (prng_mode == PRNG_MODE_SHA512) {
  637. if (limit < PRNG_RESEED_LIMIT_SHA512_LOWER)
  638. return -EINVAL;
  639. } else {
  640. if (limit < PRNG_RESEED_LIMIT_TDES_LOWER)
  641. return -EINVAL;
  642. }
  643. prng_reseed_limit = limit;
  644. return count;
  645. }
  646. static DEVICE_ATTR(reseed_limit, 0644,
  647. prng_reseed_limit_show, prng_reseed_limit_store);
  648. /* strength attribute (ro) */
  649. static ssize_t prng_strength_show(struct device *dev,
  650. struct device_attribute *attr,
  651. char *buf)
  652. {
  653. return snprintf(buf, PAGE_SIZE, "256\n");
  654. }
  655. static DEVICE_ATTR(strength, 0444, prng_strength_show, NULL);
  656. static struct attribute *prng_sha512_dev_attrs[] = {
  657. &dev_attr_errorflag.attr,
  658. &dev_attr_chunksize.attr,
  659. &dev_attr_byte_counter.attr,
  660. &dev_attr_mode.attr,
  661. &dev_attr_reseed.attr,
  662. &dev_attr_reseed_limit.attr,
  663. &dev_attr_strength.attr,
  664. NULL
  665. };
  666. static struct attribute *prng_tdes_dev_attrs[] = {
  667. &dev_attr_chunksize.attr,
  668. &dev_attr_byte_counter.attr,
  669. &dev_attr_mode.attr,
  670. NULL
  671. };
  672. static struct attribute_group prng_sha512_dev_attr_group = {
  673. .attrs = prng_sha512_dev_attrs
  674. };
  675. static struct attribute_group prng_tdes_dev_attr_group = {
  676. .attrs = prng_tdes_dev_attrs
  677. };
  678. /*** module init and exit ***/
  679. static int __init prng_init(void)
  680. {
  681. int ret;
  682. /* check if the CPU has a PRNG */
  683. if (!cpacf_query(CPACF_KMC, CPACF_KMC_PRNG))
  684. return -EOPNOTSUPP;
  685. /* choose prng mode */
  686. if (prng_mode != PRNG_MODE_TDES) {
  687. /* check for MSA5 support for PPNO operations */
  688. if (!cpacf_query(CPACF_PPNO, CPACF_PPNO_SHA512_DRNG_GEN)) {
  689. if (prng_mode == PRNG_MODE_SHA512) {
  690. pr_err("The prng module cannot "
  691. "start in SHA-512 mode\n");
  692. return -EOPNOTSUPP;
  693. }
  694. prng_mode = PRNG_MODE_TDES;
  695. } else
  696. prng_mode = PRNG_MODE_SHA512;
  697. }
  698. if (prng_mode == PRNG_MODE_SHA512) {
  699. /* SHA512 mode */
  700. if (prng_chunk_size < PRNG_CHUNKSIZE_SHA512_MIN
  701. || prng_chunk_size > PRNG_CHUNKSIZE_SHA512_MAX)
  702. return -EINVAL;
  703. prng_chunk_size = (prng_chunk_size + 0x3f) & ~0x3f;
  704. if (prng_reseed_limit == 0)
  705. prng_reseed_limit = PRNG_RESEED_LIMIT_SHA512;
  706. else if (prng_reseed_limit < PRNG_RESEED_LIMIT_SHA512_LOWER)
  707. return -EINVAL;
  708. ret = prng_sha512_instantiate();
  709. if (ret)
  710. goto out;
  711. ret = misc_register(&prng_sha512_dev);
  712. if (ret) {
  713. prng_sha512_deinstantiate();
  714. goto out;
  715. }
  716. ret = sysfs_create_group(&prng_sha512_dev.this_device->kobj,
  717. &prng_sha512_dev_attr_group);
  718. if (ret) {
  719. misc_deregister(&prng_sha512_dev);
  720. prng_sha512_deinstantiate();
  721. goto out;
  722. }
  723. } else {
  724. /* TDES mode */
  725. if (prng_chunk_size < PRNG_CHUNKSIZE_TDES_MIN
  726. || prng_chunk_size > PRNG_CHUNKSIZE_TDES_MAX)
  727. return -EINVAL;
  728. prng_chunk_size = (prng_chunk_size + 0x07) & ~0x07;
  729. if (prng_reseed_limit == 0)
  730. prng_reseed_limit = PRNG_RESEED_LIMIT_TDES;
  731. else if (prng_reseed_limit < PRNG_RESEED_LIMIT_TDES_LOWER)
  732. return -EINVAL;
  733. ret = prng_tdes_instantiate();
  734. if (ret)
  735. goto out;
  736. ret = misc_register(&prng_tdes_dev);
  737. if (ret) {
  738. prng_tdes_deinstantiate();
  739. goto out;
  740. }
  741. ret = sysfs_create_group(&prng_tdes_dev.this_device->kobj,
  742. &prng_tdes_dev_attr_group);
  743. if (ret) {
  744. misc_deregister(&prng_tdes_dev);
  745. prng_tdes_deinstantiate();
  746. goto out;
  747. }
  748. }
  749. out:
  750. return ret;
  751. }
  752. static void __exit prng_exit(void)
  753. {
  754. if (prng_mode == PRNG_MODE_SHA512) {
  755. sysfs_remove_group(&prng_sha512_dev.this_device->kobj,
  756. &prng_sha512_dev_attr_group);
  757. misc_deregister(&prng_sha512_dev);
  758. prng_sha512_deinstantiate();
  759. } else {
  760. sysfs_remove_group(&prng_tdes_dev.this_device->kobj,
  761. &prng_tdes_dev_attr_group);
  762. misc_deregister(&prng_tdes_dev);
  763. prng_tdes_deinstantiate();
  764. }
  765. }
  766. module_cpu_feature_match(MSA, prng_init);
  767. module_exit(prng_exit);