ansi_cprng.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * PRNG: Pseudo Random Number Generator
  3. * Based on NIST Recommended PRNG From ANSI X9.31 Appendix A.2.4 using
  4. * AES 128 cipher
  5. *
  6. * (C) Neil Horman <nhorman@tuxdriver.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * any later version.
  12. *
  13. *
  14. */
  15. #include <crypto/internal/rng.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/string.h>
  21. #include "internal.h"
  22. #define DEFAULT_PRNG_KEY "0123456789abcdef"
  23. #define DEFAULT_PRNG_KSZ 16
  24. #define DEFAULT_BLK_SZ 16
  25. #define DEFAULT_V_SEED "zaybxcwdveuftgsh"
  26. /*
  27. * Flags for the prng_context flags field
  28. */
  29. #define PRNG_FIXED_SIZE 0x1
  30. #define PRNG_NEED_RESET 0x2
  31. /*
  32. * Note: DT is our counter value
  33. * I is our intermediate value
  34. * V is our seed vector
  35. * See http://csrc.nist.gov/groups/STM/cavp/documents/rng/931rngext.pdf
  36. * for implementation details
  37. */
  38. struct prng_context {
  39. spinlock_t prng_lock;
  40. unsigned char rand_data[DEFAULT_BLK_SZ];
  41. unsigned char last_rand_data[DEFAULT_BLK_SZ];
  42. unsigned char DT[DEFAULT_BLK_SZ];
  43. unsigned char I[DEFAULT_BLK_SZ];
  44. unsigned char V[DEFAULT_BLK_SZ];
  45. u32 rand_data_valid;
  46. struct crypto_cipher *tfm;
  47. u32 flags;
  48. };
  49. static int dbg;
  50. static void hexdump(char *note, unsigned char *buf, unsigned int len)
  51. {
  52. if (dbg) {
  53. printk(KERN_CRIT "%s", note);
  54. print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
  55. 16, 1,
  56. buf, len, false);
  57. }
  58. }
  59. #define dbgprint(format, args...) do {\
  60. if (dbg)\
  61. printk(format, ##args);\
  62. } while (0)
  63. static void xor_vectors(unsigned char *in1, unsigned char *in2,
  64. unsigned char *out, unsigned int size)
  65. {
  66. int i;
  67. for (i = 0; i < size; i++)
  68. out[i] = in1[i] ^ in2[i];
  69. }
  70. /*
  71. * Returns DEFAULT_BLK_SZ bytes of random data per call
  72. * returns 0 if generation succeeded, <0 if something went wrong
  73. */
  74. static int _get_more_prng_bytes(struct prng_context *ctx, int cont_test)
  75. {
  76. int i;
  77. unsigned char tmp[DEFAULT_BLK_SZ];
  78. unsigned char *output = NULL;
  79. dbgprint(KERN_CRIT "Calling _get_more_prng_bytes for context %p\n",
  80. ctx);
  81. hexdump("Input DT: ", ctx->DT, DEFAULT_BLK_SZ);
  82. hexdump("Input I: ", ctx->I, DEFAULT_BLK_SZ);
  83. hexdump("Input V: ", ctx->V, DEFAULT_BLK_SZ);
  84. /*
  85. * This algorithm is a 3 stage state machine
  86. */
  87. for (i = 0; i < 3; i++) {
  88. switch (i) {
  89. case 0:
  90. /*
  91. * Start by encrypting the counter value
  92. * This gives us an intermediate value I
  93. */
  94. memcpy(tmp, ctx->DT, DEFAULT_BLK_SZ);
  95. output = ctx->I;
  96. hexdump("tmp stage 0: ", tmp, DEFAULT_BLK_SZ);
  97. break;
  98. case 1:
  99. /*
  100. * Next xor I with our secret vector V
  101. * encrypt that result to obtain our
  102. * pseudo random data which we output
  103. */
  104. xor_vectors(ctx->I, ctx->V, tmp, DEFAULT_BLK_SZ);
  105. hexdump("tmp stage 1: ", tmp, DEFAULT_BLK_SZ);
  106. output = ctx->rand_data;
  107. break;
  108. case 2:
  109. #ifdef CONFIG_CRYPTO_FIPS
  110. if (unlikely(in_fips_err()))
  111. return -EINVAL;
  112. #endif
  113. /*
  114. * First check that we didn't produce the same
  115. * random data that we did last time around through this
  116. */
  117. #if FIPS_FUNC_TEST == 5
  118. memcpy(ctx->rand_data, ctx->last_rand_data, DEFAULT_BLK_SZ);
  119. #endif
  120. if (!memcmp(ctx->rand_data, ctx->last_rand_data,
  121. DEFAULT_BLK_SZ)) {
  122. if (cont_test) {
  123. #ifdef CONFIG_CRYPTO_FIPS
  124. set_in_fips_err();
  125. #else
  126. panic("cprng %p Failed repetition check!\n", ctx);
  127. #endif
  128. }
  129. printk(KERN_ERR
  130. "ctx %p Failed repetition check!\n",
  131. ctx);
  132. ctx->flags |= PRNG_NEED_RESET;
  133. return -EINVAL;
  134. }
  135. memcpy(ctx->last_rand_data, ctx->rand_data,
  136. DEFAULT_BLK_SZ);
  137. /*
  138. * Lastly xor the random data with I
  139. * and encrypt that to obtain a new secret vector V
  140. */
  141. xor_vectors(ctx->rand_data, ctx->I, tmp,
  142. DEFAULT_BLK_SZ);
  143. output = ctx->V;
  144. hexdump("tmp stage 2: ", tmp, DEFAULT_BLK_SZ);
  145. break;
  146. }
  147. /* do the encryption */
  148. crypto_cipher_encrypt_one(ctx->tfm, output, tmp);
  149. }
  150. /*
  151. * Now update our DT value
  152. */
  153. for (i = DEFAULT_BLK_SZ - 1; i >= 0; i--) {
  154. ctx->DT[i] += 1;
  155. if (ctx->DT[i] != 0)
  156. break;
  157. }
  158. dbgprint("Returning new block for context %p\n", ctx);
  159. ctx->rand_data_valid = 0;
  160. hexdump("Output DT: ", ctx->DT, DEFAULT_BLK_SZ);
  161. hexdump("Output I: ", ctx->I, DEFAULT_BLK_SZ);
  162. hexdump("Output V: ", ctx->V, DEFAULT_BLK_SZ);
  163. hexdump("New Random Data: ", ctx->rand_data, DEFAULT_BLK_SZ);
  164. return 0;
  165. }
  166. /* Our exported functions */
  167. static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx,
  168. int do_cont_test)
  169. {
  170. unsigned char *ptr = buf;
  171. unsigned int byte_count = (unsigned int)nbytes;
  172. int err;
  173. spin_lock_bh(&ctx->prng_lock);
  174. err = -EINVAL;
  175. if (ctx->flags & PRNG_NEED_RESET)
  176. goto done;
  177. /*
  178. * If the FIXED_SIZE flag is on, only return whole blocks of
  179. * pseudo random data
  180. */
  181. err = -EINVAL;
  182. if (ctx->flags & PRNG_FIXED_SIZE) {
  183. if (nbytes < DEFAULT_BLK_SZ)
  184. goto done;
  185. byte_count = DEFAULT_BLK_SZ;
  186. }
  187. err = byte_count;
  188. dbgprint(KERN_CRIT "getting %d random bytes for context %p\n",
  189. byte_count, ctx);
  190. remainder:
  191. if (ctx->rand_data_valid == DEFAULT_BLK_SZ) {
  192. if (_get_more_prng_bytes(ctx, do_cont_test) < 0) {
  193. memset(buf, 0, nbytes);
  194. err = -EINVAL;
  195. goto done;
  196. }
  197. }
  198. /*
  199. * Copy any data less than an entire block
  200. */
  201. if (byte_count < DEFAULT_BLK_SZ) {
  202. empty_rbuf:
  203. while (ctx->rand_data_valid < DEFAULT_BLK_SZ) {
  204. *ptr = ctx->rand_data[ctx->rand_data_valid];
  205. ptr++;
  206. byte_count--;
  207. ctx->rand_data_valid++;
  208. if (byte_count == 0)
  209. goto done;
  210. }
  211. }
  212. /*
  213. * Now copy whole blocks
  214. */
  215. for (; byte_count >= DEFAULT_BLK_SZ; byte_count -= DEFAULT_BLK_SZ) {
  216. if (ctx->rand_data_valid == DEFAULT_BLK_SZ) {
  217. if (_get_more_prng_bytes(ctx, do_cont_test) < 0) {
  218. memset(buf, 0, nbytes);
  219. err = -EINVAL;
  220. goto done;
  221. }
  222. }
  223. if (ctx->rand_data_valid > 0)
  224. goto empty_rbuf;
  225. memcpy(ptr, ctx->rand_data, DEFAULT_BLK_SZ);
  226. ctx->rand_data_valid += DEFAULT_BLK_SZ;
  227. ptr += DEFAULT_BLK_SZ;
  228. }
  229. /*
  230. * Now go back and get any remaining partial block
  231. */
  232. if (byte_count)
  233. goto remainder;
  234. done:
  235. spin_unlock_bh(&ctx->prng_lock);
  236. dbgprint(KERN_CRIT "returning %d from get_prng_bytes in context %p\n",
  237. err, ctx);
  238. return err;
  239. }
  240. static void free_prng_context(struct prng_context *ctx)
  241. {
  242. crypto_free_cipher(ctx->tfm);
  243. }
  244. static int reset_prng_context(struct prng_context *ctx,
  245. unsigned char *key, size_t klen,
  246. unsigned char *V, unsigned char *DT)
  247. {
  248. int ret;
  249. unsigned char *prng_key;
  250. spin_lock_bh(&ctx->prng_lock);
  251. ctx->flags |= PRNG_NEED_RESET;
  252. prng_key = (key != NULL) ? key : (unsigned char *)DEFAULT_PRNG_KEY;
  253. if (!key)
  254. klen = DEFAULT_PRNG_KSZ;
  255. if (V)
  256. memcpy(ctx->V, V, DEFAULT_BLK_SZ);
  257. else
  258. memcpy(ctx->V, DEFAULT_V_SEED, DEFAULT_BLK_SZ);
  259. if (DT)
  260. memcpy(ctx->DT, DT, DEFAULT_BLK_SZ);
  261. else
  262. memset(ctx->DT, 0, DEFAULT_BLK_SZ);
  263. memset(ctx->rand_data, 0, DEFAULT_BLK_SZ);
  264. memset(ctx->last_rand_data, 0, DEFAULT_BLK_SZ);
  265. ctx->rand_data_valid = DEFAULT_BLK_SZ;
  266. ret = crypto_cipher_setkey(ctx->tfm, prng_key, klen);
  267. if (ret) {
  268. dbgprint(KERN_CRIT "PRNG: setkey() failed flags=%x\n",
  269. crypto_cipher_get_flags(ctx->tfm));
  270. goto out;
  271. }
  272. ret = 0;
  273. ctx->flags &= ~PRNG_NEED_RESET;
  274. out:
  275. spin_unlock_bh(&ctx->prng_lock);
  276. return ret;
  277. }
  278. static int cprng_init(struct crypto_tfm *tfm)
  279. {
  280. struct prng_context *ctx = crypto_tfm_ctx(tfm);
  281. spin_lock_init(&ctx->prng_lock);
  282. ctx->tfm = crypto_alloc_cipher("aes", 0, 0);
  283. if (IS_ERR(ctx->tfm)) {
  284. dbgprint(KERN_CRIT "Failed to alloc tfm for context %p\n",
  285. ctx);
  286. return PTR_ERR(ctx->tfm);
  287. }
  288. if (reset_prng_context(ctx, NULL, DEFAULT_PRNG_KSZ, NULL, NULL) < 0)
  289. return -EINVAL;
  290. /*
  291. * after allocation, we should always force the user to reset
  292. * so they don't inadvertently use the insecure default values
  293. * without specifying them intentially
  294. */
  295. ctx->flags |= PRNG_NEED_RESET;
  296. return 0;
  297. }
  298. static void cprng_exit(struct crypto_tfm *tfm)
  299. {
  300. free_prng_context(crypto_tfm_ctx(tfm));
  301. }
  302. static int cprng_get_random(struct crypto_rng *tfm, u8 *rdata,
  303. unsigned int dlen)
  304. {
  305. struct prng_context *prng = crypto_rng_ctx(tfm);
  306. return get_prng_bytes(rdata, dlen, prng, 0);
  307. }
  308. /*
  309. * This is the cprng_registered reset method the seed value is
  310. * interpreted as the tuple { V KEY DT}
  311. * V and KEY are required during reset, and DT is optional, detected
  312. * as being present by testing the length of the seed
  313. */
  314. static int cprng_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
  315. {
  316. struct prng_context *prng = crypto_rng_ctx(tfm);
  317. u8 *key = seed + DEFAULT_BLK_SZ;
  318. u8 *dt = NULL;
  319. if (slen < DEFAULT_PRNG_KSZ + DEFAULT_BLK_SZ)
  320. return -EINVAL;
  321. #ifdef CONFIG_CRYPTO_FIPS
  322. if (!memcmp(key, seed, DEFAULT_PRNG_KSZ))
  323. return -EINVAL;
  324. #endif
  325. if (slen >= (2 * DEFAULT_BLK_SZ + DEFAULT_PRNG_KSZ))
  326. dt = key + DEFAULT_PRNG_KSZ;
  327. reset_prng_context(prng, key, DEFAULT_PRNG_KSZ, seed, dt);
  328. if (prng->flags & PRNG_NEED_RESET)
  329. return -EINVAL;
  330. return 0;
  331. }
  332. static struct crypto_alg rng_alg = {
  333. .cra_name = "stdrng",
  334. .cra_driver_name = "ansi_cprng",
  335. .cra_priority = 100,
  336. .cra_flags = CRYPTO_ALG_TYPE_RNG,
  337. .cra_ctxsize = sizeof(struct prng_context),
  338. .cra_type = &crypto_rng_type,
  339. .cra_module = THIS_MODULE,
  340. .cra_list = LIST_HEAD_INIT(rng_alg.cra_list),
  341. .cra_init = cprng_init,
  342. .cra_exit = cprng_exit,
  343. .cra_u = {
  344. .rng = {
  345. .rng_make_random = cprng_get_random,
  346. .rng_reset = cprng_reset,
  347. .seedsize = DEFAULT_PRNG_KSZ + 2*DEFAULT_BLK_SZ,
  348. }
  349. }
  350. };
  351. #ifdef CONFIG_CRYPTO_FIPS
  352. static int fips_cprng_get_random(struct crypto_rng *tfm, u8 *rdata,
  353. unsigned int dlen)
  354. {
  355. struct prng_context *prng = crypto_rng_ctx(tfm);
  356. #ifdef CONFIG_CRYPTO_FIPS
  357. if (unlikely(in_fips_err()))
  358. return -EINVAL;
  359. #endif
  360. return get_prng_bytes(rdata, dlen, prng, 1);
  361. }
  362. static int fips_cprng_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
  363. {
  364. u8 rdata[DEFAULT_BLK_SZ];
  365. u8 *key = seed + DEFAULT_BLK_SZ;
  366. int rc;
  367. struct prng_context *prng = crypto_rng_ctx(tfm);
  368. if (slen < DEFAULT_PRNG_KSZ + DEFAULT_BLK_SZ)
  369. return -EINVAL;
  370. /* fips strictly requires seed != key */
  371. if (!memcmp(seed, key, DEFAULT_PRNG_KSZ))
  372. return -EINVAL;
  373. rc = cprng_reset(tfm, seed, slen);
  374. if (!rc)
  375. goto out;
  376. /* this primes our continuity test */
  377. rc = get_prng_bytes(rdata, DEFAULT_BLK_SZ, prng, 0);
  378. prng->rand_data_valid = DEFAULT_BLK_SZ;
  379. out:
  380. return rc;
  381. }
  382. static struct crypto_alg fips_rng_alg = {
  383. .cra_name = "fips(ansi_cprng)",
  384. .cra_driver_name = "fips_ansi_cprng",
  385. .cra_priority = 300,
  386. .cra_flags = CRYPTO_ALG_TYPE_RNG,
  387. .cra_ctxsize = sizeof(struct prng_context),
  388. .cra_type = &crypto_rng_type,
  389. .cra_module = THIS_MODULE,
  390. .cra_list = LIST_HEAD_INIT(rng_alg.cra_list),
  391. .cra_init = cprng_init,
  392. .cra_exit = cprng_exit,
  393. .cra_u = {
  394. .rng = {
  395. .rng_make_random = fips_cprng_get_random,
  396. .rng_reset = fips_cprng_reset,
  397. .seedsize = DEFAULT_PRNG_KSZ + 2*DEFAULT_BLK_SZ,
  398. }
  399. }
  400. };
  401. #endif
  402. /* Module initalization */
  403. static int __init prng_mod_init(void)
  404. {
  405. int rc = 0;
  406. rc = crypto_register_alg(&rng_alg);
  407. #ifdef CONFIG_CRYPTO_FIPS
  408. if (rc)
  409. goto out;
  410. rc = crypto_register_alg(&fips_rng_alg);
  411. out:
  412. #endif
  413. return rc;
  414. }
  415. static void __exit prng_mod_fini(void)
  416. {
  417. crypto_unregister_alg(&rng_alg);
  418. #ifdef CONFIG_CRYPTO_FIPS
  419. crypto_unregister_alg(&fips_rng_alg);
  420. #endif
  421. return;
  422. }
  423. MODULE_LICENSE("GPL");
  424. MODULE_DESCRIPTION("Software Pseudo Random Number Generator");
  425. MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>");
  426. module_param(dbg, int, 0);
  427. MODULE_PARM_DESC(dbg, "Boolean to enable debugging (0/1 == off/on)");
  428. module_init(prng_mod_init);
  429. module_exit(prng_mod_fini);
  430. MODULE_ALIAS("stdrng");