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/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. return -EFAULT;
  478. nbytes -= chunk;
  479. ret += chunk;
  480. ubuf += chunk;
  481. }
  482. /* unlock prng_data struct */
  483. mutex_unlock(&prng_data->mutex);
  484. return ret;
  485. }
  486. static ssize_t prng_sha512_read(struct file *file, char __user *ubuf,
  487. size_t nbytes, loff_t *ppos)
  488. {
  489. int n, ret = 0;
  490. u8 *p;
  491. /* if errorflag is set do nothing and return 'broken pipe' */
  492. if (prng_errorflag)
  493. return -EPIPE;
  494. /* lock prng_data struct */
  495. if (mutex_lock_interruptible(&prng_data->mutex))
  496. return -ERESTARTSYS;
  497. while (nbytes) {
  498. if (need_resched()) {
  499. if (signal_pending(current)) {
  500. if (ret == 0)
  501. ret = -ERESTARTSYS;
  502. break;
  503. }
  504. /* give mutex free before calling schedule() */
  505. mutex_unlock(&prng_data->mutex);
  506. schedule();
  507. /* occopy mutex again */
  508. if (mutex_lock_interruptible(&prng_data->mutex)) {
  509. if (ret == 0)
  510. ret = -ERESTARTSYS;
  511. return ret;
  512. }
  513. }
  514. if (prng_data->rest) {
  515. /* push left over random bytes from the previous read */
  516. p = prng_data->buf + prng_chunk_size - prng_data->rest;
  517. n = (nbytes < prng_data->rest) ?
  518. nbytes : prng_data->rest;
  519. prng_data->rest -= n;
  520. } else {
  521. /* generate one chunk of random bytes into read buf */
  522. p = prng_data->buf;
  523. n = prng_sha512_generate(p, prng_chunk_size);
  524. if (n < 0) {
  525. ret = n;
  526. break;
  527. }
  528. if (nbytes < prng_chunk_size) {
  529. n = nbytes;
  530. prng_data->rest = prng_chunk_size - n;
  531. } else {
  532. n = prng_chunk_size;
  533. prng_data->rest = 0;
  534. }
  535. }
  536. if (copy_to_user(ubuf, p, n)) {
  537. ret = -EFAULT;
  538. break;
  539. }
  540. ubuf += n;
  541. nbytes -= n;
  542. ret += n;
  543. }
  544. /* unlock prng_data struct */
  545. mutex_unlock(&prng_data->mutex);
  546. return ret;
  547. }
  548. /*** sysfs stuff ***/
  549. static const struct file_operations prng_sha512_fops = {
  550. .owner = THIS_MODULE,
  551. .open = &prng_open,
  552. .release = NULL,
  553. .read = &prng_sha512_read,
  554. .llseek = noop_llseek,
  555. };
  556. static const struct file_operations prng_tdes_fops = {
  557. .owner = THIS_MODULE,
  558. .open = &prng_open,
  559. .release = NULL,
  560. .read = &prng_tdes_read,
  561. .llseek = noop_llseek,
  562. };
  563. static struct miscdevice prng_sha512_dev = {
  564. .name = "prandom",
  565. .minor = MISC_DYNAMIC_MINOR,
  566. .fops = &prng_sha512_fops,
  567. };
  568. static struct miscdevice prng_tdes_dev = {
  569. .name = "prandom",
  570. .minor = MISC_DYNAMIC_MINOR,
  571. .fops = &prng_tdes_fops,
  572. };
  573. /* chunksize attribute (ro) */
  574. static ssize_t prng_chunksize_show(struct device *dev,
  575. struct device_attribute *attr,
  576. char *buf)
  577. {
  578. return snprintf(buf, PAGE_SIZE, "%u\n", prng_chunk_size);
  579. }
  580. static DEVICE_ATTR(chunksize, 0444, prng_chunksize_show, NULL);
  581. /* counter attribute (ro) */
  582. static ssize_t prng_counter_show(struct device *dev,
  583. struct device_attribute *attr,
  584. char *buf)
  585. {
  586. u64 counter;
  587. if (mutex_lock_interruptible(&prng_data->mutex))
  588. return -ERESTARTSYS;
  589. if (prng_mode == PRNG_MODE_SHA512)
  590. counter = prng_data->ppnows.stream_bytes;
  591. else
  592. counter = prng_data->prngws.byte_counter;
  593. mutex_unlock(&prng_data->mutex);
  594. return snprintf(buf, PAGE_SIZE, "%llu\n", counter);
  595. }
  596. static DEVICE_ATTR(byte_counter, 0444, prng_counter_show, NULL);
  597. /* errorflag attribute (ro) */
  598. static ssize_t prng_errorflag_show(struct device *dev,
  599. struct device_attribute *attr,
  600. char *buf)
  601. {
  602. return snprintf(buf, PAGE_SIZE, "%d\n", prng_errorflag);
  603. }
  604. static DEVICE_ATTR(errorflag, 0444, prng_errorflag_show, NULL);
  605. /* mode attribute (ro) */
  606. static ssize_t prng_mode_show(struct device *dev,
  607. struct device_attribute *attr,
  608. char *buf)
  609. {
  610. if (prng_mode == PRNG_MODE_TDES)
  611. return snprintf(buf, PAGE_SIZE, "TDES\n");
  612. else
  613. return snprintf(buf, PAGE_SIZE, "SHA512\n");
  614. }
  615. static DEVICE_ATTR(mode, 0444, prng_mode_show, NULL);
  616. /* reseed attribute (w) */
  617. static ssize_t prng_reseed_store(struct device *dev,
  618. struct device_attribute *attr,
  619. const char *buf, size_t count)
  620. {
  621. if (mutex_lock_interruptible(&prng_data->mutex))
  622. return -ERESTARTSYS;
  623. prng_sha512_reseed();
  624. mutex_unlock(&prng_data->mutex);
  625. return count;
  626. }
  627. static DEVICE_ATTR(reseed, 0200, NULL, prng_reseed_store);
  628. /* reseed limit attribute (rw) */
  629. static ssize_t prng_reseed_limit_show(struct device *dev,
  630. struct device_attribute *attr,
  631. char *buf)
  632. {
  633. return snprintf(buf, PAGE_SIZE, "%u\n", prng_reseed_limit);
  634. }
  635. static ssize_t prng_reseed_limit_store(struct device *dev,
  636. struct device_attribute *attr,
  637. const char *buf, size_t count)
  638. {
  639. unsigned limit;
  640. if (sscanf(buf, "%u\n", &limit) != 1)
  641. return -EINVAL;
  642. if (prng_mode == PRNG_MODE_SHA512) {
  643. if (limit < PRNG_RESEED_LIMIT_SHA512_LOWER)
  644. return -EINVAL;
  645. } else {
  646. if (limit < PRNG_RESEED_LIMIT_TDES_LOWER)
  647. return -EINVAL;
  648. }
  649. prng_reseed_limit = limit;
  650. return count;
  651. }
  652. static DEVICE_ATTR(reseed_limit, 0644,
  653. prng_reseed_limit_show, prng_reseed_limit_store);
  654. /* strength attribute (ro) */
  655. static ssize_t prng_strength_show(struct device *dev,
  656. struct device_attribute *attr,
  657. char *buf)
  658. {
  659. return snprintf(buf, PAGE_SIZE, "256\n");
  660. }
  661. static DEVICE_ATTR(strength, 0444, prng_strength_show, NULL);
  662. static struct attribute *prng_sha512_dev_attrs[] = {
  663. &dev_attr_errorflag.attr,
  664. &dev_attr_chunksize.attr,
  665. &dev_attr_byte_counter.attr,
  666. &dev_attr_mode.attr,
  667. &dev_attr_reseed.attr,
  668. &dev_attr_reseed_limit.attr,
  669. &dev_attr_strength.attr,
  670. NULL
  671. };
  672. static struct attribute *prng_tdes_dev_attrs[] = {
  673. &dev_attr_chunksize.attr,
  674. &dev_attr_byte_counter.attr,
  675. &dev_attr_mode.attr,
  676. NULL
  677. };
  678. static struct attribute_group prng_sha512_dev_attr_group = {
  679. .attrs = prng_sha512_dev_attrs
  680. };
  681. static struct attribute_group prng_tdes_dev_attr_group = {
  682. .attrs = prng_tdes_dev_attrs
  683. };
  684. /*** module init and exit ***/
  685. static int __init prng_init(void)
  686. {
  687. int ret;
  688. /* check if the CPU has a PRNG */
  689. if (!crypt_s390_func_available(KMC_PRNG, CRYPT_S390_MSA))
  690. return -EOPNOTSUPP;
  691. /* choose prng mode */
  692. if (prng_mode != PRNG_MODE_TDES) {
  693. /* check for MSA5 support for PPNO operations */
  694. if (!crypt_s390_func_available(PPNO_SHA512_DRNG_GEN,
  695. CRYPT_S390_MSA5)) {
  696. if (prng_mode == PRNG_MODE_SHA512) {
  697. pr_err("The prng module cannot "
  698. "start in SHA-512 mode\n");
  699. return -EOPNOTSUPP;
  700. }
  701. prng_mode = PRNG_MODE_TDES;
  702. } else
  703. prng_mode = PRNG_MODE_SHA512;
  704. }
  705. if (prng_mode == PRNG_MODE_SHA512) {
  706. /* SHA512 mode */
  707. if (prng_chunk_size < PRNG_CHUNKSIZE_SHA512_MIN
  708. || prng_chunk_size > PRNG_CHUNKSIZE_SHA512_MAX)
  709. return -EINVAL;
  710. prng_chunk_size = (prng_chunk_size + 0x3f) & ~0x3f;
  711. if (prng_reseed_limit == 0)
  712. prng_reseed_limit = PRNG_RESEED_LIMIT_SHA512;
  713. else if (prng_reseed_limit < PRNG_RESEED_LIMIT_SHA512_LOWER)
  714. return -EINVAL;
  715. ret = prng_sha512_instantiate();
  716. if (ret)
  717. goto out;
  718. ret = misc_register(&prng_sha512_dev);
  719. if (ret) {
  720. prng_sha512_deinstantiate();
  721. goto out;
  722. }
  723. ret = sysfs_create_group(&prng_sha512_dev.this_device->kobj,
  724. &prng_sha512_dev_attr_group);
  725. if (ret) {
  726. misc_deregister(&prng_sha512_dev);
  727. prng_sha512_deinstantiate();
  728. goto out;
  729. }
  730. } else {
  731. /* TDES mode */
  732. if (prng_chunk_size < PRNG_CHUNKSIZE_TDES_MIN
  733. || prng_chunk_size > PRNG_CHUNKSIZE_TDES_MAX)
  734. return -EINVAL;
  735. prng_chunk_size = (prng_chunk_size + 0x07) & ~0x07;
  736. if (prng_reseed_limit == 0)
  737. prng_reseed_limit = PRNG_RESEED_LIMIT_TDES;
  738. else if (prng_reseed_limit < PRNG_RESEED_LIMIT_TDES_LOWER)
  739. return -EINVAL;
  740. ret = prng_tdes_instantiate();
  741. if (ret)
  742. goto out;
  743. ret = misc_register(&prng_tdes_dev);
  744. if (ret) {
  745. prng_tdes_deinstantiate();
  746. goto out;
  747. }
  748. ret = sysfs_create_group(&prng_tdes_dev.this_device->kobj,
  749. &prng_tdes_dev_attr_group);
  750. if (ret) {
  751. misc_deregister(&prng_tdes_dev);
  752. prng_tdes_deinstantiate();
  753. goto out;
  754. }
  755. }
  756. out:
  757. return ret;
  758. }
  759. static void __exit prng_exit(void)
  760. {
  761. if (prng_mode == PRNG_MODE_SHA512) {
  762. sysfs_remove_group(&prng_sha512_dev.this_device->kobj,
  763. &prng_sha512_dev_attr_group);
  764. misc_deregister(&prng_sha512_dev);
  765. prng_sha512_deinstantiate();
  766. } else {
  767. sysfs_remove_group(&prng_tdes_dev.this_device->kobj,
  768. &prng_tdes_dev_attr_group);
  769. misc_deregister(&prng_tdes_dev);
  770. prng_tdes_deinstantiate();
  771. }
  772. }
  773. module_cpu_feature_match(MSA, prng_init);
  774. module_exit(prng_exit);