prng.c 23 KB

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