prng.c 23 KB

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