ansi_cprng.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 succeded, <0 if something went wrong
  73. */
  74. static int _get_more_prng_bytes(struct prng_context *ctx)
  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. /*
  110. * First check that we didn't produce the same
  111. * random data that we did last time around through this
  112. */
  113. if (!memcmp(ctx->rand_data, ctx->last_rand_data,
  114. DEFAULT_BLK_SZ)) {
  115. if (fips_enabled) {
  116. panic("cprng %p Failed repetition check!\n",
  117. ctx);
  118. }
  119. printk(KERN_ERR
  120. "ctx %p Failed repetition check!\n",
  121. ctx);
  122. ctx->flags |= PRNG_NEED_RESET;
  123. return -EINVAL;
  124. }
  125. memcpy(ctx->last_rand_data, ctx->rand_data,
  126. DEFAULT_BLK_SZ);
  127. /*
  128. * Lastly xor the random data with I
  129. * and encrypt that to obtain a new secret vector V
  130. */
  131. xor_vectors(ctx->rand_data, ctx->I, tmp,
  132. DEFAULT_BLK_SZ);
  133. output = ctx->V;
  134. hexdump("tmp stage 2: ", tmp, DEFAULT_BLK_SZ);
  135. break;
  136. }
  137. /* do the encryption */
  138. crypto_cipher_encrypt_one(ctx->tfm, output, tmp);
  139. }
  140. /*
  141. * Now update our DT value
  142. */
  143. for (i = DEFAULT_BLK_SZ - 1; i >= 0; i--) {
  144. ctx->DT[i] += 1;
  145. if (ctx->DT[i] != 0)
  146. break;
  147. }
  148. dbgprint("Returning new block for context %p\n", ctx);
  149. ctx->rand_data_valid = 0;
  150. hexdump("Output DT: ", ctx->DT, DEFAULT_BLK_SZ);
  151. hexdump("Output I: ", ctx->I, DEFAULT_BLK_SZ);
  152. hexdump("Output V: ", ctx->V, DEFAULT_BLK_SZ);
  153. hexdump("New Random Data: ", ctx->rand_data, DEFAULT_BLK_SZ);
  154. return 0;
  155. }
  156. /* Our exported functions */
  157. static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx)
  158. {
  159. unsigned long flags;
  160. unsigned char *ptr = buf;
  161. unsigned int byte_count = (unsigned int)nbytes;
  162. int err;
  163. if (nbytes < 0)
  164. return -EINVAL;
  165. spin_lock_irqsave(&ctx->prng_lock, flags);
  166. err = -EINVAL;
  167. if (ctx->flags & PRNG_NEED_RESET)
  168. goto done;
  169. /*
  170. * If the FIXED_SIZE flag is on, only return whole blocks of
  171. * pseudo random data
  172. */
  173. err = -EINVAL;
  174. if (ctx->flags & PRNG_FIXED_SIZE) {
  175. if (nbytes < DEFAULT_BLK_SZ)
  176. goto done;
  177. byte_count = DEFAULT_BLK_SZ;
  178. }
  179. err = byte_count;
  180. dbgprint(KERN_CRIT "getting %d random bytes for context %p\n",
  181. byte_count, ctx);
  182. remainder:
  183. if (ctx->rand_data_valid == DEFAULT_BLK_SZ) {
  184. if (_get_more_prng_bytes(ctx) < 0) {
  185. memset(buf, 0, nbytes);
  186. err = -EINVAL;
  187. goto done;
  188. }
  189. }
  190. /*
  191. * Copy any data less than an entire block
  192. */
  193. if (byte_count < DEFAULT_BLK_SZ) {
  194. empty_rbuf:
  195. for (; ctx->rand_data_valid < DEFAULT_BLK_SZ;
  196. ctx->rand_data_valid++) {
  197. *ptr = ctx->rand_data[ctx->rand_data_valid];
  198. ptr++;
  199. byte_count--;
  200. if (byte_count == 0)
  201. goto done;
  202. }
  203. }
  204. /*
  205. * Now copy whole blocks
  206. */
  207. for (; byte_count >= DEFAULT_BLK_SZ; byte_count -= DEFAULT_BLK_SZ) {
  208. if (ctx->rand_data_valid == DEFAULT_BLK_SZ) {
  209. if (_get_more_prng_bytes(ctx) < 0) {
  210. memset(buf, 0, nbytes);
  211. err = -EINVAL;
  212. goto done;
  213. }
  214. }
  215. if (ctx->rand_data_valid > 0)
  216. goto empty_rbuf;
  217. memcpy(ptr, ctx->rand_data, DEFAULT_BLK_SZ);
  218. ctx->rand_data_valid += DEFAULT_BLK_SZ;
  219. ptr += DEFAULT_BLK_SZ;
  220. }
  221. /*
  222. * Now go back and get any remaining partial block
  223. */
  224. if (byte_count)
  225. goto remainder;
  226. done:
  227. spin_unlock_irqrestore(&ctx->prng_lock, flags);
  228. dbgprint(KERN_CRIT "returning %d from get_prng_bytes in context %p\n",
  229. err, ctx);
  230. return err;
  231. }
  232. static void free_prng_context(struct prng_context *ctx)
  233. {
  234. crypto_free_cipher(ctx->tfm);
  235. }
  236. static int reset_prng_context(struct prng_context *ctx,
  237. unsigned char *key, size_t klen,
  238. unsigned char *V, unsigned char *DT)
  239. {
  240. int ret;
  241. int rc = -EINVAL;
  242. unsigned char *prng_key;
  243. spin_lock(&ctx->prng_lock);
  244. ctx->flags |= PRNG_NEED_RESET;
  245. prng_key = (key != NULL) ? key : (unsigned char *)DEFAULT_PRNG_KEY;
  246. if (!key)
  247. klen = DEFAULT_PRNG_KSZ;
  248. if (V)
  249. memcpy(ctx->V, V, DEFAULT_BLK_SZ);
  250. else
  251. memcpy(ctx->V, DEFAULT_V_SEED, DEFAULT_BLK_SZ);
  252. if (DT)
  253. memcpy(ctx->DT, DT, DEFAULT_BLK_SZ);
  254. else
  255. memset(ctx->DT, 0, DEFAULT_BLK_SZ);
  256. memset(ctx->rand_data, 0, DEFAULT_BLK_SZ);
  257. memset(ctx->last_rand_data, 0, DEFAULT_BLK_SZ);
  258. if (ctx->tfm)
  259. crypto_free_cipher(ctx->tfm);
  260. ctx->tfm = crypto_alloc_cipher("aes", 0, 0);
  261. if (IS_ERR(ctx->tfm)) {
  262. dbgprint(KERN_CRIT "Failed to alloc tfm for context %p\n",
  263. ctx);
  264. ctx->tfm = NULL;
  265. goto out;
  266. }
  267. ctx->rand_data_valid = DEFAULT_BLK_SZ;
  268. ret = crypto_cipher_setkey(ctx->tfm, prng_key, klen);
  269. if (ret) {
  270. dbgprint(KERN_CRIT "PRNG: setkey() failed flags=%x\n",
  271. crypto_cipher_get_flags(ctx->tfm));
  272. crypto_free_cipher(ctx->tfm);
  273. goto out;
  274. }
  275. rc = 0;
  276. ctx->flags &= ~PRNG_NEED_RESET;
  277. out:
  278. spin_unlock(&ctx->prng_lock);
  279. return rc;
  280. }
  281. static int cprng_init(struct crypto_tfm *tfm)
  282. {
  283. struct prng_context *ctx = crypto_tfm_ctx(tfm);
  284. spin_lock_init(&ctx->prng_lock);
  285. if (reset_prng_context(ctx, NULL, DEFAULT_PRNG_KSZ, NULL, NULL) < 0)
  286. return -EINVAL;
  287. /*
  288. * after allocation, we should always force the user to reset
  289. * so they don't inadvertently use the insecure default values
  290. * without specifying them intentially
  291. */
  292. ctx->flags |= PRNG_NEED_RESET;
  293. return 0;
  294. }
  295. static void cprng_exit(struct crypto_tfm *tfm)
  296. {
  297. free_prng_context(crypto_tfm_ctx(tfm));
  298. }
  299. static int cprng_get_random(struct crypto_rng *tfm, u8 *rdata,
  300. unsigned int dlen)
  301. {
  302. struct prng_context *prng = crypto_rng_ctx(tfm);
  303. return get_prng_bytes(rdata, dlen, prng);
  304. }
  305. /*
  306. * This is the cprng_registered reset method the seed value is
  307. * interpreted as the tuple { V KEY DT}
  308. * V and KEY are required during reset, and DT is optional, detected
  309. * as being present by testing the length of the seed
  310. */
  311. static int cprng_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
  312. {
  313. struct prng_context *prng = crypto_rng_ctx(tfm);
  314. u8 *key = seed + DEFAULT_BLK_SZ;
  315. u8 *dt = NULL;
  316. if (slen < DEFAULT_PRNG_KSZ + DEFAULT_BLK_SZ)
  317. return -EINVAL;
  318. if (slen >= (2 * DEFAULT_BLK_SZ + DEFAULT_PRNG_KSZ))
  319. dt = key + DEFAULT_PRNG_KSZ;
  320. reset_prng_context(prng, key, DEFAULT_PRNG_KSZ, seed, dt);
  321. if (prng->flags & PRNG_NEED_RESET)
  322. return -EINVAL;
  323. return 0;
  324. }
  325. static struct crypto_alg rng_alg = {
  326. .cra_name = "stdrng",
  327. .cra_driver_name = "ansi_cprng",
  328. .cra_priority = 100,
  329. .cra_flags = CRYPTO_ALG_TYPE_RNG,
  330. .cra_ctxsize = sizeof(struct prng_context),
  331. .cra_type = &crypto_rng_type,
  332. .cra_module = THIS_MODULE,
  333. .cra_list = LIST_HEAD_INIT(rng_alg.cra_list),
  334. .cra_init = cprng_init,
  335. .cra_exit = cprng_exit,
  336. .cra_u = {
  337. .rng = {
  338. .rng_make_random = cprng_get_random,
  339. .rng_reset = cprng_reset,
  340. .seedsize = DEFAULT_PRNG_KSZ + 2*DEFAULT_BLK_SZ,
  341. }
  342. }
  343. };
  344. /* Module initalization */
  345. static int __init prng_mod_init(void)
  346. {
  347. int ret = 0;
  348. if (fips_enabled)
  349. rng_alg.cra_priority += 200;
  350. ret = crypto_register_alg(&rng_alg);
  351. if (ret)
  352. goto out;
  353. out:
  354. return 0;
  355. }
  356. static void __exit prng_mod_fini(void)
  357. {
  358. crypto_unregister_alg(&rng_alg);
  359. return;
  360. }
  361. MODULE_LICENSE("GPL");
  362. MODULE_DESCRIPTION("Software Pseudo Random Number Generator");
  363. MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>");
  364. module_param(dbg, int, 0);
  365. MODULE_PARM_DESC(dbg, "Boolean to enable debugging (0/1 == off/on)");
  366. module_init(prng_mod_init);
  367. module_exit(prng_mod_fini);
  368. MODULE_ALIAS("stdrng");