drbg.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  1. /*
  2. * DRBG: Deterministic Random Bits Generator
  3. * Based on NIST Recommended DRBG from NIST SP800-90A with the following
  4. * properties:
  5. * * CTR DRBG with DF with AES-128, AES-192, AES-256 cores
  6. * * Hash DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
  7. * * HMAC DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
  8. * * with and without prediction resistance
  9. *
  10. * Copyright Stephan Mueller <smueller@chronox.de>, 2014
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, and the entire permission notice in its entirety,
  17. * including the disclaimer of warranties.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. The name of the author may not be used to endorse or promote
  22. * products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * ALTERNATIVELY, this product may be distributed under the terms of
  26. * the GNU General Public License, in which case the provisions of the GPL are
  27. * required INSTEAD OF the above restrictions. (This clause is
  28. * necessary due to a potential bad interaction between the GPL and
  29. * the restrictions contained in a BSD-style copyright.)
  30. *
  31. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  32. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
  34. * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
  35. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  37. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  38. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  39. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  41. * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
  42. * DAMAGE.
  43. *
  44. * DRBG Usage
  45. * ==========
  46. * The SP 800-90A DRBG allows the user to specify a personalization string
  47. * for initialization as well as an additional information string for each
  48. * random number request. The following code fragments show how a caller
  49. * uses the kernel crypto API to use the full functionality of the DRBG.
  50. *
  51. * Usage without any additional data
  52. * ---------------------------------
  53. * struct crypto_rng *drng;
  54. * int err;
  55. * char data[DATALEN];
  56. *
  57. * drng = crypto_alloc_rng(drng_name, 0, 0);
  58. * err = crypto_rng_get_bytes(drng, &data, DATALEN);
  59. * crypto_free_rng(drng);
  60. *
  61. *
  62. * Usage with personalization string during initialization
  63. * -------------------------------------------------------
  64. * struct crypto_rng *drng;
  65. * int err;
  66. * char data[DATALEN];
  67. * struct drbg_string pers;
  68. * char personalization[11] = "some-string";
  69. *
  70. * drbg_string_fill(&pers, personalization, strlen(personalization));
  71. * drng = crypto_alloc_rng(drng_name, 0, 0);
  72. * // The reset completely re-initializes the DRBG with the provided
  73. * // personalization string
  74. * err = crypto_rng_reset(drng, &personalization, strlen(personalization));
  75. * err = crypto_rng_get_bytes(drng, &data, DATALEN);
  76. * crypto_free_rng(drng);
  77. *
  78. *
  79. * Usage with additional information string during random number request
  80. * ---------------------------------------------------------------------
  81. * struct crypto_rng *drng;
  82. * int err;
  83. * char data[DATALEN];
  84. * char addtl_string[11] = "some-string";
  85. * string drbg_string addtl;
  86. *
  87. * drbg_string_fill(&addtl, addtl_string, strlen(addtl_string));
  88. * drng = crypto_alloc_rng(drng_name, 0, 0);
  89. * // The following call is a wrapper to crypto_rng_get_bytes() and returns
  90. * // the same error codes.
  91. * err = crypto_drbg_get_bytes_addtl(drng, &data, DATALEN, &addtl);
  92. * crypto_free_rng(drng);
  93. *
  94. *
  95. * Usage with personalization and additional information strings
  96. * -------------------------------------------------------------
  97. * Just mix both scenarios above.
  98. */
  99. #include <crypto/drbg.h>
  100. #include <linux/kernel.h>
  101. /***************************************************************
  102. * Backend cipher definitions available to DRBG
  103. ***************************************************************/
  104. /*
  105. * The order of the DRBG definitions here matter: every DRBG is registered
  106. * as stdrng. Each DRBG receives an increasing cra_priority values the later
  107. * they are defined in this array (see drbg_fill_array).
  108. *
  109. * HMAC DRBGs are favored over Hash DRBGs over CTR DRBGs, and
  110. * the SHA256 / AES 256 over other ciphers. Thus, the favored
  111. * DRBGs are the latest entries in this array.
  112. */
  113. static const struct drbg_core drbg_cores[] = {
  114. #ifdef CONFIG_CRYPTO_DRBG_CTR
  115. {
  116. .flags = DRBG_CTR | DRBG_STRENGTH128,
  117. .statelen = 32, /* 256 bits as defined in 10.2.1 */
  118. .blocklen_bytes = 16,
  119. .cra_name = "ctr_aes128",
  120. .backend_cra_name = "aes",
  121. }, {
  122. .flags = DRBG_CTR | DRBG_STRENGTH192,
  123. .statelen = 40, /* 320 bits as defined in 10.2.1 */
  124. .blocklen_bytes = 16,
  125. .cra_name = "ctr_aes192",
  126. .backend_cra_name = "aes",
  127. }, {
  128. .flags = DRBG_CTR | DRBG_STRENGTH256,
  129. .statelen = 48, /* 384 bits as defined in 10.2.1 */
  130. .blocklen_bytes = 16,
  131. .cra_name = "ctr_aes256",
  132. .backend_cra_name = "aes",
  133. },
  134. #endif /* CONFIG_CRYPTO_DRBG_CTR */
  135. #ifdef CONFIG_CRYPTO_DRBG_HASH
  136. {
  137. .flags = DRBG_HASH | DRBG_STRENGTH128,
  138. .statelen = 55, /* 440 bits */
  139. .blocklen_bytes = 20,
  140. .cra_name = "sha1",
  141. .backend_cra_name = "sha1",
  142. }, {
  143. .flags = DRBG_HASH | DRBG_STRENGTH256,
  144. .statelen = 111, /* 888 bits */
  145. .blocklen_bytes = 48,
  146. .cra_name = "sha384",
  147. .backend_cra_name = "sha384",
  148. }, {
  149. .flags = DRBG_HASH | DRBG_STRENGTH256,
  150. .statelen = 111, /* 888 bits */
  151. .blocklen_bytes = 64,
  152. .cra_name = "sha512",
  153. .backend_cra_name = "sha512",
  154. }, {
  155. .flags = DRBG_HASH | DRBG_STRENGTH256,
  156. .statelen = 55, /* 440 bits */
  157. .blocklen_bytes = 32,
  158. .cra_name = "sha256",
  159. .backend_cra_name = "sha256",
  160. },
  161. #endif /* CONFIG_CRYPTO_DRBG_HASH */
  162. #ifdef CONFIG_CRYPTO_DRBG_HMAC
  163. {
  164. .flags = DRBG_HMAC | DRBG_STRENGTH128,
  165. .statelen = 20, /* block length of cipher */
  166. .blocklen_bytes = 20,
  167. .cra_name = "hmac_sha1",
  168. .backend_cra_name = "hmac(sha1)",
  169. }, {
  170. .flags = DRBG_HMAC | DRBG_STRENGTH256,
  171. .statelen = 48, /* block length of cipher */
  172. .blocklen_bytes = 48,
  173. .cra_name = "hmac_sha384",
  174. .backend_cra_name = "hmac(sha384)",
  175. }, {
  176. .flags = DRBG_HMAC | DRBG_STRENGTH256,
  177. .statelen = 64, /* block length of cipher */
  178. .blocklen_bytes = 64,
  179. .cra_name = "hmac_sha512",
  180. .backend_cra_name = "hmac(sha512)",
  181. }, {
  182. .flags = DRBG_HMAC | DRBG_STRENGTH256,
  183. .statelen = 32, /* block length of cipher */
  184. .blocklen_bytes = 32,
  185. .cra_name = "hmac_sha256",
  186. .backend_cra_name = "hmac(sha256)",
  187. },
  188. #endif /* CONFIG_CRYPTO_DRBG_HMAC */
  189. };
  190. static int drbg_uninstantiate(struct drbg_state *drbg);
  191. /******************************************************************
  192. * Generic helper functions
  193. ******************************************************************/
  194. /*
  195. * Return strength of DRBG according to SP800-90A section 8.4
  196. *
  197. * @flags DRBG flags reference
  198. *
  199. * Return: normalized strength in *bytes* value or 32 as default
  200. * to counter programming errors
  201. */
  202. static inline unsigned short drbg_sec_strength(drbg_flag_t flags)
  203. {
  204. switch (flags & DRBG_STRENGTH_MASK) {
  205. case DRBG_STRENGTH128:
  206. return 16;
  207. case DRBG_STRENGTH192:
  208. return 24;
  209. case DRBG_STRENGTH256:
  210. return 32;
  211. default:
  212. return 32;
  213. }
  214. }
  215. /*
  216. * Convert an integer into a byte representation of this integer.
  217. * The byte representation is big-endian
  218. *
  219. * @val value to be converted
  220. * @buf buffer holding the converted integer -- caller must ensure that
  221. * buffer size is at least 32 bit
  222. */
  223. #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
  224. static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf)
  225. {
  226. struct s {
  227. __be32 conv;
  228. };
  229. struct s *conversion = (struct s *) buf;
  230. conversion->conv = cpu_to_be32(val);
  231. }
  232. #endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */
  233. /******************************************************************
  234. * CTR DRBG callback functions
  235. ******************************************************************/
  236. #ifdef CONFIG_CRYPTO_DRBG_CTR
  237. #define CRYPTO_DRBG_CTR_STRING "CTR "
  238. MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes256");
  239. MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes256");
  240. MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes192");
  241. MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes192");
  242. MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes128");
  243. MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes128");
  244. static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
  245. const unsigned char *key);
  246. static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
  247. const struct drbg_string *in);
  248. static int drbg_init_sym_kernel(struct drbg_state *drbg);
  249. static int drbg_fini_sym_kernel(struct drbg_state *drbg);
  250. static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
  251. u8 *inbuf, u32 inbuflen,
  252. u8 *outbuf, u32 outlen);
  253. #define DRBG_OUTSCRATCHLEN 256
  254. /* BCC function for CTR DRBG as defined in 10.4.3 */
  255. static int drbg_ctr_bcc(struct drbg_state *drbg,
  256. unsigned char *out, const unsigned char *key,
  257. struct list_head *in)
  258. {
  259. int ret = 0;
  260. struct drbg_string *curr = NULL;
  261. struct drbg_string data;
  262. short cnt = 0;
  263. drbg_string_fill(&data, out, drbg_blocklen(drbg));
  264. /* 10.4.3 step 2 / 4 */
  265. drbg_kcapi_symsetkey(drbg, key);
  266. list_for_each_entry(curr, in, list) {
  267. const unsigned char *pos = curr->buf;
  268. size_t len = curr->len;
  269. /* 10.4.3 step 4.1 */
  270. while (len) {
  271. /* 10.4.3 step 4.2 */
  272. if (drbg_blocklen(drbg) == cnt) {
  273. cnt = 0;
  274. ret = drbg_kcapi_sym(drbg, out, &data);
  275. if (ret)
  276. return ret;
  277. }
  278. out[cnt] ^= *pos;
  279. pos++;
  280. cnt++;
  281. len--;
  282. }
  283. }
  284. /* 10.4.3 step 4.2 for last block */
  285. if (cnt)
  286. ret = drbg_kcapi_sym(drbg, out, &data);
  287. return ret;
  288. }
  289. /*
  290. * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
  291. * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
  292. * the scratchpad is used as follows:
  293. * drbg_ctr_update:
  294. * temp
  295. * start: drbg->scratchpad
  296. * length: drbg_statelen(drbg) + drbg_blocklen(drbg)
  297. * note: the cipher writing into this variable works
  298. * blocklen-wise. Now, when the statelen is not a multiple
  299. * of blocklen, the generateion loop below "spills over"
  300. * by at most blocklen. Thus, we need to give sufficient
  301. * memory.
  302. * df_data
  303. * start: drbg->scratchpad +
  304. * drbg_statelen(drbg) + drbg_blocklen(drbg)
  305. * length: drbg_statelen(drbg)
  306. *
  307. * drbg_ctr_df:
  308. * pad
  309. * start: df_data + drbg_statelen(drbg)
  310. * length: drbg_blocklen(drbg)
  311. * iv
  312. * start: pad + drbg_blocklen(drbg)
  313. * length: drbg_blocklen(drbg)
  314. * temp
  315. * start: iv + drbg_blocklen(drbg)
  316. * length: drbg_satelen(drbg) + drbg_blocklen(drbg)
  317. * note: temp is the buffer that the BCC function operates
  318. * on. BCC operates blockwise. drbg_statelen(drbg)
  319. * is sufficient when the DRBG state length is a multiple
  320. * of the block size. For AES192 (and maybe other ciphers)
  321. * this is not correct and the length for temp is
  322. * insufficient (yes, that also means for such ciphers,
  323. * the final output of all BCC rounds are truncated).
  324. * Therefore, add drbg_blocklen(drbg) to cover all
  325. * possibilities.
  326. */
  327. /* Derivation Function for CTR DRBG as defined in 10.4.2 */
  328. static int drbg_ctr_df(struct drbg_state *drbg,
  329. unsigned char *df_data, size_t bytes_to_return,
  330. struct list_head *seedlist)
  331. {
  332. int ret = -EFAULT;
  333. unsigned char L_N[8];
  334. /* S3 is input */
  335. struct drbg_string S1, S2, S4, cipherin;
  336. LIST_HEAD(bcc_list);
  337. unsigned char *pad = df_data + drbg_statelen(drbg);
  338. unsigned char *iv = pad + drbg_blocklen(drbg);
  339. unsigned char *temp = iv + drbg_blocklen(drbg);
  340. size_t padlen = 0;
  341. unsigned int templen = 0;
  342. /* 10.4.2 step 7 */
  343. unsigned int i = 0;
  344. /* 10.4.2 step 8 */
  345. const unsigned char *K = (unsigned char *)
  346. "\x00\x01\x02\x03\x04\x05\x06\x07"
  347. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  348. "\x10\x11\x12\x13\x14\x15\x16\x17"
  349. "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
  350. unsigned char *X;
  351. size_t generated_len = 0;
  352. size_t inputlen = 0;
  353. struct drbg_string *seed = NULL;
  354. memset(pad, 0, drbg_blocklen(drbg));
  355. memset(iv, 0, drbg_blocklen(drbg));
  356. /* 10.4.2 step 1 is implicit as we work byte-wise */
  357. /* 10.4.2 step 2 */
  358. if ((512/8) < bytes_to_return)
  359. return -EINVAL;
  360. /* 10.4.2 step 2 -- calculate the entire length of all input data */
  361. list_for_each_entry(seed, seedlist, list)
  362. inputlen += seed->len;
  363. drbg_cpu_to_be32(inputlen, &L_N[0]);
  364. /* 10.4.2 step 3 */
  365. drbg_cpu_to_be32(bytes_to_return, &L_N[4]);
  366. /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
  367. padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
  368. /* wrap the padlen appropriately */
  369. if (padlen)
  370. padlen = drbg_blocklen(drbg) - padlen;
  371. /*
  372. * pad / padlen contains the 0x80 byte and the following zero bytes.
  373. * As the calculated padlen value only covers the number of zero
  374. * bytes, this value has to be incremented by one for the 0x80 byte.
  375. */
  376. padlen++;
  377. pad[0] = 0x80;
  378. /* 10.4.2 step 4 -- first fill the linked list and then order it */
  379. drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
  380. list_add_tail(&S1.list, &bcc_list);
  381. drbg_string_fill(&S2, L_N, sizeof(L_N));
  382. list_add_tail(&S2.list, &bcc_list);
  383. list_splice_tail(seedlist, &bcc_list);
  384. drbg_string_fill(&S4, pad, padlen);
  385. list_add_tail(&S4.list, &bcc_list);
  386. /* 10.4.2 step 9 */
  387. while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
  388. /*
  389. * 10.4.2 step 9.1 - the padding is implicit as the buffer
  390. * holds zeros after allocation -- even the increment of i
  391. * is irrelevant as the increment remains within length of i
  392. */
  393. drbg_cpu_to_be32(i, iv);
  394. /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
  395. ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
  396. if (ret)
  397. goto out;
  398. /* 10.4.2 step 9.3 */
  399. i++;
  400. templen += drbg_blocklen(drbg);
  401. }
  402. /* 10.4.2 step 11 */
  403. X = temp + (drbg_keylen(drbg));
  404. drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
  405. /* 10.4.2 step 12: overwriting of outval is implemented in next step */
  406. /* 10.4.2 step 13 */
  407. drbg_kcapi_symsetkey(drbg, temp);
  408. while (generated_len < bytes_to_return) {
  409. short blocklen = 0;
  410. /*
  411. * 10.4.2 step 13.1: the truncation of the key length is
  412. * implicit as the key is only drbg_blocklen in size based on
  413. * the implementation of the cipher function callback
  414. */
  415. ret = drbg_kcapi_sym(drbg, X, &cipherin);
  416. if (ret)
  417. goto out;
  418. blocklen = (drbg_blocklen(drbg) <
  419. (bytes_to_return - generated_len)) ?
  420. drbg_blocklen(drbg) :
  421. (bytes_to_return - generated_len);
  422. /* 10.4.2 step 13.2 and 14 */
  423. memcpy(df_data + generated_len, X, blocklen);
  424. generated_len += blocklen;
  425. }
  426. ret = 0;
  427. out:
  428. memset(iv, 0, drbg_blocklen(drbg));
  429. memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
  430. memset(pad, 0, drbg_blocklen(drbg));
  431. return ret;
  432. }
  433. /*
  434. * update function of CTR DRBG as defined in 10.2.1.2
  435. *
  436. * The reseed variable has an enhanced meaning compared to the update
  437. * functions of the other DRBGs as follows:
  438. * 0 => initial seed from initialization
  439. * 1 => reseed via drbg_seed
  440. * 2 => first invocation from drbg_ctr_update when addtl is present. In
  441. * this case, the df_data scratchpad is not deleted so that it is
  442. * available for another calls to prevent calling the DF function
  443. * again.
  444. * 3 => second invocation from drbg_ctr_update. When the update function
  445. * was called with addtl, the df_data memory already contains the
  446. * DFed addtl information and we do not need to call DF again.
  447. */
  448. static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
  449. int reseed)
  450. {
  451. int ret = -EFAULT;
  452. /* 10.2.1.2 step 1 */
  453. unsigned char *temp = drbg->scratchpad;
  454. unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
  455. drbg_blocklen(drbg);
  456. if (3 > reseed)
  457. memset(df_data, 0, drbg_statelen(drbg));
  458. if (!reseed) {
  459. /*
  460. * The DRBG uses the CTR mode of the underlying AES cipher. The
  461. * CTR mode increments the counter value after the AES operation
  462. * but SP800-90A requires that the counter is incremented before
  463. * the AES operation. Hence, we increment it at the time we set
  464. * it by one.
  465. */
  466. crypto_inc(drbg->V, drbg_blocklen(drbg));
  467. ret = crypto_skcipher_setkey(drbg->ctr_handle, drbg->C,
  468. drbg_keylen(drbg));
  469. if (ret)
  470. goto out;
  471. }
  472. /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
  473. if (seed) {
  474. ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
  475. if (ret)
  476. goto out;
  477. }
  478. ret = drbg_kcapi_sym_ctr(drbg, df_data, drbg_statelen(drbg),
  479. temp, drbg_statelen(drbg));
  480. if (ret)
  481. return ret;
  482. /* 10.2.1.2 step 5 */
  483. ret = crypto_skcipher_setkey(drbg->ctr_handle, temp,
  484. drbg_keylen(drbg));
  485. if (ret)
  486. goto out;
  487. /* 10.2.1.2 step 6 */
  488. memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
  489. /* See above: increment counter by one to compensate timing of CTR op */
  490. crypto_inc(drbg->V, drbg_blocklen(drbg));
  491. ret = 0;
  492. out:
  493. memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
  494. if (2 != reseed)
  495. memset(df_data, 0, drbg_statelen(drbg));
  496. return ret;
  497. }
  498. /*
  499. * scratchpad use: drbg_ctr_update is called independently from
  500. * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
  501. */
  502. /* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
  503. static int drbg_ctr_generate(struct drbg_state *drbg,
  504. unsigned char *buf, unsigned int buflen,
  505. struct list_head *addtl)
  506. {
  507. int ret;
  508. int len = min_t(int, buflen, INT_MAX);
  509. /* 10.2.1.5.2 step 2 */
  510. if (addtl && !list_empty(addtl)) {
  511. ret = drbg_ctr_update(drbg, addtl, 2);
  512. if (ret)
  513. return 0;
  514. }
  515. /* 10.2.1.5.2 step 4.1 */
  516. ret = drbg_kcapi_sym_ctr(drbg, NULL, 0, buf, len);
  517. if (ret)
  518. return ret;
  519. /* 10.2.1.5.2 step 6 */
  520. ret = drbg_ctr_update(drbg, NULL, 3);
  521. if (ret)
  522. len = ret;
  523. return len;
  524. }
  525. static const struct drbg_state_ops drbg_ctr_ops = {
  526. .update = drbg_ctr_update,
  527. .generate = drbg_ctr_generate,
  528. .crypto_init = drbg_init_sym_kernel,
  529. .crypto_fini = drbg_fini_sym_kernel,
  530. };
  531. #endif /* CONFIG_CRYPTO_DRBG_CTR */
  532. /******************************************************************
  533. * HMAC DRBG callback functions
  534. ******************************************************************/
  535. #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
  536. static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval,
  537. const struct list_head *in);
  538. static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg,
  539. const unsigned char *key);
  540. static int drbg_init_hash_kernel(struct drbg_state *drbg);
  541. static int drbg_fini_hash_kernel(struct drbg_state *drbg);
  542. #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
  543. #ifdef CONFIG_CRYPTO_DRBG_HMAC
  544. #define CRYPTO_DRBG_HMAC_STRING "HMAC "
  545. MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha512");
  546. MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha512");
  547. MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha384");
  548. MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha384");
  549. MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha256");
  550. MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha256");
  551. MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha1");
  552. MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha1");
  553. /* update function of HMAC DRBG as defined in 10.1.2.2 */
  554. static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
  555. int reseed)
  556. {
  557. int ret = -EFAULT;
  558. int i = 0;
  559. struct drbg_string seed1, seed2, vdata;
  560. LIST_HEAD(seedlist);
  561. LIST_HEAD(vdatalist);
  562. if (!reseed) {
  563. /* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */
  564. memset(drbg->V, 1, drbg_statelen(drbg));
  565. drbg_kcapi_hmacsetkey(drbg, drbg->C);
  566. }
  567. drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
  568. list_add_tail(&seed1.list, &seedlist);
  569. /* buffer of seed2 will be filled in for loop below with one byte */
  570. drbg_string_fill(&seed2, NULL, 1);
  571. list_add_tail(&seed2.list, &seedlist);
  572. /* input data of seed is allowed to be NULL at this point */
  573. if (seed)
  574. list_splice_tail(seed, &seedlist);
  575. drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
  576. list_add_tail(&vdata.list, &vdatalist);
  577. for (i = 2; 0 < i; i--) {
  578. /* first round uses 0x0, second 0x1 */
  579. unsigned char prefix = DRBG_PREFIX0;
  580. if (1 == i)
  581. prefix = DRBG_PREFIX1;
  582. /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
  583. seed2.buf = &prefix;
  584. ret = drbg_kcapi_hash(drbg, drbg->C, &seedlist);
  585. if (ret)
  586. return ret;
  587. drbg_kcapi_hmacsetkey(drbg, drbg->C);
  588. /* 10.1.2.2 step 2 and 5 -- HMAC for V */
  589. ret = drbg_kcapi_hash(drbg, drbg->V, &vdatalist);
  590. if (ret)
  591. return ret;
  592. /* 10.1.2.2 step 3 */
  593. if (!seed)
  594. return ret;
  595. }
  596. return 0;
  597. }
  598. /* generate function of HMAC DRBG as defined in 10.1.2.5 */
  599. static int drbg_hmac_generate(struct drbg_state *drbg,
  600. unsigned char *buf,
  601. unsigned int buflen,
  602. struct list_head *addtl)
  603. {
  604. int len = 0;
  605. int ret = 0;
  606. struct drbg_string data;
  607. LIST_HEAD(datalist);
  608. /* 10.1.2.5 step 2 */
  609. if (addtl && !list_empty(addtl)) {
  610. ret = drbg_hmac_update(drbg, addtl, 1);
  611. if (ret)
  612. return ret;
  613. }
  614. drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
  615. list_add_tail(&data.list, &datalist);
  616. while (len < buflen) {
  617. unsigned int outlen = 0;
  618. /* 10.1.2.5 step 4.1 */
  619. ret = drbg_kcapi_hash(drbg, drbg->V, &datalist);
  620. if (ret)
  621. return ret;
  622. outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
  623. drbg_blocklen(drbg) : (buflen - len);
  624. /* 10.1.2.5 step 4.2 */
  625. memcpy(buf + len, drbg->V, outlen);
  626. len += outlen;
  627. }
  628. /* 10.1.2.5 step 6 */
  629. if (addtl && !list_empty(addtl))
  630. ret = drbg_hmac_update(drbg, addtl, 1);
  631. else
  632. ret = drbg_hmac_update(drbg, NULL, 1);
  633. if (ret)
  634. return ret;
  635. return len;
  636. }
  637. static const struct drbg_state_ops drbg_hmac_ops = {
  638. .update = drbg_hmac_update,
  639. .generate = drbg_hmac_generate,
  640. .crypto_init = drbg_init_hash_kernel,
  641. .crypto_fini = drbg_fini_hash_kernel,
  642. };
  643. #endif /* CONFIG_CRYPTO_DRBG_HMAC */
  644. /******************************************************************
  645. * Hash DRBG callback functions
  646. ******************************************************************/
  647. #ifdef CONFIG_CRYPTO_DRBG_HASH
  648. #define CRYPTO_DRBG_HASH_STRING "HASH "
  649. MODULE_ALIAS_CRYPTO("drbg_pr_sha512");
  650. MODULE_ALIAS_CRYPTO("drbg_nopr_sha512");
  651. MODULE_ALIAS_CRYPTO("drbg_pr_sha384");
  652. MODULE_ALIAS_CRYPTO("drbg_nopr_sha384");
  653. MODULE_ALIAS_CRYPTO("drbg_pr_sha256");
  654. MODULE_ALIAS_CRYPTO("drbg_nopr_sha256");
  655. MODULE_ALIAS_CRYPTO("drbg_pr_sha1");
  656. MODULE_ALIAS_CRYPTO("drbg_nopr_sha1");
  657. /*
  658. * Increment buffer
  659. *
  660. * @dst buffer to increment
  661. * @add value to add
  662. */
  663. static inline void drbg_add_buf(unsigned char *dst, size_t dstlen,
  664. const unsigned char *add, size_t addlen)
  665. {
  666. /* implied: dstlen > addlen */
  667. unsigned char *dstptr;
  668. const unsigned char *addptr;
  669. unsigned int remainder = 0;
  670. size_t len = addlen;
  671. dstptr = dst + (dstlen-1);
  672. addptr = add + (addlen-1);
  673. while (len) {
  674. remainder += *dstptr + *addptr;
  675. *dstptr = remainder & 0xff;
  676. remainder >>= 8;
  677. len--; dstptr--; addptr--;
  678. }
  679. len = dstlen - addlen;
  680. while (len && remainder > 0) {
  681. remainder = *dstptr + 1;
  682. *dstptr = remainder & 0xff;
  683. remainder >>= 8;
  684. len--; dstptr--;
  685. }
  686. }
  687. /*
  688. * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
  689. * interlinked, the scratchpad is used as follows:
  690. * drbg_hash_update
  691. * start: drbg->scratchpad
  692. * length: drbg_statelen(drbg)
  693. * drbg_hash_df:
  694. * start: drbg->scratchpad + drbg_statelen(drbg)
  695. * length: drbg_blocklen(drbg)
  696. *
  697. * drbg_hash_process_addtl uses the scratchpad, but fully completes
  698. * before either of the functions mentioned before are invoked. Therefore,
  699. * drbg_hash_process_addtl does not need to be specifically considered.
  700. */
  701. /* Derivation Function for Hash DRBG as defined in 10.4.1 */
  702. static int drbg_hash_df(struct drbg_state *drbg,
  703. unsigned char *outval, size_t outlen,
  704. struct list_head *entropylist)
  705. {
  706. int ret = 0;
  707. size_t len = 0;
  708. unsigned char input[5];
  709. unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
  710. struct drbg_string data;
  711. /* 10.4.1 step 3 */
  712. input[0] = 1;
  713. drbg_cpu_to_be32((outlen * 8), &input[1]);
  714. /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
  715. drbg_string_fill(&data, input, 5);
  716. list_add(&data.list, entropylist);
  717. /* 10.4.1 step 4 */
  718. while (len < outlen) {
  719. short blocklen = 0;
  720. /* 10.4.1 step 4.1 */
  721. ret = drbg_kcapi_hash(drbg, tmp, entropylist);
  722. if (ret)
  723. goto out;
  724. /* 10.4.1 step 4.2 */
  725. input[0]++;
  726. blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
  727. drbg_blocklen(drbg) : (outlen - len);
  728. memcpy(outval + len, tmp, blocklen);
  729. len += blocklen;
  730. }
  731. out:
  732. memset(tmp, 0, drbg_blocklen(drbg));
  733. return ret;
  734. }
  735. /* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
  736. static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
  737. int reseed)
  738. {
  739. int ret = 0;
  740. struct drbg_string data1, data2;
  741. LIST_HEAD(datalist);
  742. LIST_HEAD(datalist2);
  743. unsigned char *V = drbg->scratchpad;
  744. unsigned char prefix = DRBG_PREFIX1;
  745. if (!seed)
  746. return -EINVAL;
  747. if (reseed) {
  748. /* 10.1.1.3 step 1 */
  749. memcpy(V, drbg->V, drbg_statelen(drbg));
  750. drbg_string_fill(&data1, &prefix, 1);
  751. list_add_tail(&data1.list, &datalist);
  752. drbg_string_fill(&data2, V, drbg_statelen(drbg));
  753. list_add_tail(&data2.list, &datalist);
  754. }
  755. list_splice_tail(seed, &datalist);
  756. /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
  757. ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
  758. if (ret)
  759. goto out;
  760. /* 10.1.1.2 / 10.1.1.3 step 4 */
  761. prefix = DRBG_PREFIX0;
  762. drbg_string_fill(&data1, &prefix, 1);
  763. list_add_tail(&data1.list, &datalist2);
  764. drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
  765. list_add_tail(&data2.list, &datalist2);
  766. /* 10.1.1.2 / 10.1.1.3 step 4 */
  767. ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
  768. out:
  769. memset(drbg->scratchpad, 0, drbg_statelen(drbg));
  770. return ret;
  771. }
  772. /* processing of additional information string for Hash DRBG */
  773. static int drbg_hash_process_addtl(struct drbg_state *drbg,
  774. struct list_head *addtl)
  775. {
  776. int ret = 0;
  777. struct drbg_string data1, data2;
  778. LIST_HEAD(datalist);
  779. unsigned char prefix = DRBG_PREFIX2;
  780. /* 10.1.1.4 step 2 */
  781. if (!addtl || list_empty(addtl))
  782. return 0;
  783. /* 10.1.1.4 step 2a */
  784. drbg_string_fill(&data1, &prefix, 1);
  785. drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
  786. list_add_tail(&data1.list, &datalist);
  787. list_add_tail(&data2.list, &datalist);
  788. list_splice_tail(addtl, &datalist);
  789. ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist);
  790. if (ret)
  791. goto out;
  792. /* 10.1.1.4 step 2b */
  793. drbg_add_buf(drbg->V, drbg_statelen(drbg),
  794. drbg->scratchpad, drbg_blocklen(drbg));
  795. out:
  796. memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
  797. return ret;
  798. }
  799. /* Hashgen defined in 10.1.1.4 */
  800. static int drbg_hash_hashgen(struct drbg_state *drbg,
  801. unsigned char *buf,
  802. unsigned int buflen)
  803. {
  804. int len = 0;
  805. int ret = 0;
  806. unsigned char *src = drbg->scratchpad;
  807. unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
  808. struct drbg_string data;
  809. LIST_HEAD(datalist);
  810. /* 10.1.1.4 step hashgen 2 */
  811. memcpy(src, drbg->V, drbg_statelen(drbg));
  812. drbg_string_fill(&data, src, drbg_statelen(drbg));
  813. list_add_tail(&data.list, &datalist);
  814. while (len < buflen) {
  815. unsigned int outlen = 0;
  816. /* 10.1.1.4 step hashgen 4.1 */
  817. ret = drbg_kcapi_hash(drbg, dst, &datalist);
  818. if (ret) {
  819. len = ret;
  820. goto out;
  821. }
  822. outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
  823. drbg_blocklen(drbg) : (buflen - len);
  824. /* 10.1.1.4 step hashgen 4.2 */
  825. memcpy(buf + len, dst, outlen);
  826. len += outlen;
  827. /* 10.1.1.4 hashgen step 4.3 */
  828. if (len < buflen)
  829. crypto_inc(src, drbg_statelen(drbg));
  830. }
  831. out:
  832. memset(drbg->scratchpad, 0,
  833. (drbg_statelen(drbg) + drbg_blocklen(drbg)));
  834. return len;
  835. }
  836. /* generate function for Hash DRBG as defined in 10.1.1.4 */
  837. static int drbg_hash_generate(struct drbg_state *drbg,
  838. unsigned char *buf, unsigned int buflen,
  839. struct list_head *addtl)
  840. {
  841. int len = 0;
  842. int ret = 0;
  843. union {
  844. unsigned char req[8];
  845. __be64 req_int;
  846. } u;
  847. unsigned char prefix = DRBG_PREFIX3;
  848. struct drbg_string data1, data2;
  849. LIST_HEAD(datalist);
  850. /* 10.1.1.4 step 2 */
  851. ret = drbg_hash_process_addtl(drbg, addtl);
  852. if (ret)
  853. return ret;
  854. /* 10.1.1.4 step 3 */
  855. len = drbg_hash_hashgen(drbg, buf, buflen);
  856. /* this is the value H as documented in 10.1.1.4 */
  857. /* 10.1.1.4 step 4 */
  858. drbg_string_fill(&data1, &prefix, 1);
  859. list_add_tail(&data1.list, &datalist);
  860. drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
  861. list_add_tail(&data2.list, &datalist);
  862. ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist);
  863. if (ret) {
  864. len = ret;
  865. goto out;
  866. }
  867. /* 10.1.1.4 step 5 */
  868. drbg_add_buf(drbg->V, drbg_statelen(drbg),
  869. drbg->scratchpad, drbg_blocklen(drbg));
  870. drbg_add_buf(drbg->V, drbg_statelen(drbg),
  871. drbg->C, drbg_statelen(drbg));
  872. u.req_int = cpu_to_be64(drbg->reseed_ctr);
  873. drbg_add_buf(drbg->V, drbg_statelen(drbg), u.req, 8);
  874. out:
  875. memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
  876. return len;
  877. }
  878. /*
  879. * scratchpad usage: as update and generate are used isolated, both
  880. * can use the scratchpad
  881. */
  882. static const struct drbg_state_ops drbg_hash_ops = {
  883. .update = drbg_hash_update,
  884. .generate = drbg_hash_generate,
  885. .crypto_init = drbg_init_hash_kernel,
  886. .crypto_fini = drbg_fini_hash_kernel,
  887. };
  888. #endif /* CONFIG_CRYPTO_DRBG_HASH */
  889. /******************************************************************
  890. * Functions common for DRBG implementations
  891. ******************************************************************/
  892. static inline int __drbg_seed(struct drbg_state *drbg, struct list_head *seed,
  893. int reseed)
  894. {
  895. int ret = drbg->d_ops->update(drbg, seed, reseed);
  896. if (ret)
  897. return ret;
  898. drbg->seeded = true;
  899. /* 10.1.1.2 / 10.1.1.3 step 5 */
  900. drbg->reseed_ctr = 1;
  901. return ret;
  902. }
  903. static void drbg_async_seed(struct work_struct *work)
  904. {
  905. struct drbg_string data;
  906. LIST_HEAD(seedlist);
  907. struct drbg_state *drbg = container_of(work, struct drbg_state,
  908. seed_work);
  909. unsigned int entropylen = drbg_sec_strength(drbg->core->flags);
  910. unsigned char entropy[32];
  911. BUG_ON(!entropylen);
  912. BUG_ON(entropylen > sizeof(entropy));
  913. get_random_bytes(entropy, entropylen);
  914. drbg_string_fill(&data, entropy, entropylen);
  915. list_add_tail(&data.list, &seedlist);
  916. mutex_lock(&drbg->drbg_mutex);
  917. /* If nonblocking pool is initialized, deactivate Jitter RNG */
  918. crypto_free_rng(drbg->jent);
  919. drbg->jent = NULL;
  920. /* Set seeded to false so that if __drbg_seed fails the
  921. * next generate call will trigger a reseed.
  922. */
  923. drbg->seeded = false;
  924. __drbg_seed(drbg, &seedlist, true);
  925. if (drbg->seeded)
  926. drbg->reseed_threshold = drbg_max_requests(drbg);
  927. mutex_unlock(&drbg->drbg_mutex);
  928. memzero_explicit(entropy, entropylen);
  929. }
  930. /*
  931. * Seeding or reseeding of the DRBG
  932. *
  933. * @drbg: DRBG state struct
  934. * @pers: personalization / additional information buffer
  935. * @reseed: 0 for initial seed process, 1 for reseeding
  936. *
  937. * return:
  938. * 0 on success
  939. * error value otherwise
  940. */
  941. static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
  942. bool reseed)
  943. {
  944. int ret;
  945. unsigned char entropy[((32 + 16) * 2)];
  946. unsigned int entropylen = drbg_sec_strength(drbg->core->flags);
  947. struct drbg_string data1;
  948. LIST_HEAD(seedlist);
  949. /* 9.1 / 9.2 / 9.3.1 step 3 */
  950. if (pers && pers->len > (drbg_max_addtl(drbg))) {
  951. pr_devel("DRBG: personalization string too long %zu\n",
  952. pers->len);
  953. return -EINVAL;
  954. }
  955. if (list_empty(&drbg->test_data.list)) {
  956. drbg_string_fill(&data1, drbg->test_data.buf,
  957. drbg->test_data.len);
  958. pr_devel("DRBG: using test entropy\n");
  959. } else {
  960. /*
  961. * Gather entropy equal to the security strength of the DRBG.
  962. * With a derivation function, a nonce is required in addition
  963. * to the entropy. A nonce must be at least 1/2 of the security
  964. * strength of the DRBG in size. Thus, entropy + nonce is 3/2
  965. * of the strength. The consideration of a nonce is only
  966. * applicable during initial seeding.
  967. */
  968. BUG_ON(!entropylen);
  969. if (!reseed)
  970. entropylen = ((entropylen + 1) / 2) * 3;
  971. BUG_ON((entropylen * 2) > sizeof(entropy));
  972. /* Get seed from in-kernel /dev/urandom */
  973. get_random_bytes(entropy, entropylen);
  974. if (!drbg->jent) {
  975. drbg_string_fill(&data1, entropy, entropylen);
  976. pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
  977. entropylen);
  978. } else {
  979. /* Get seed from Jitter RNG */
  980. ret = crypto_rng_get_bytes(drbg->jent,
  981. entropy + entropylen,
  982. entropylen);
  983. if (ret) {
  984. pr_devel("DRBG: jent failed with %d\n", ret);
  985. return ret;
  986. }
  987. drbg_string_fill(&data1, entropy, entropylen * 2);
  988. pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
  989. entropylen * 2);
  990. }
  991. }
  992. list_add_tail(&data1.list, &seedlist);
  993. /*
  994. * concatenation of entropy with personalization str / addtl input)
  995. * the variable pers is directly handed in by the caller, so check its
  996. * contents whether it is appropriate
  997. */
  998. if (pers && pers->buf && 0 < pers->len) {
  999. list_add_tail(&pers->list, &seedlist);
  1000. pr_devel("DRBG: using personalization string\n");
  1001. }
  1002. if (!reseed) {
  1003. memset(drbg->V, 0, drbg_statelen(drbg));
  1004. memset(drbg->C, 0, drbg_statelen(drbg));
  1005. }
  1006. ret = __drbg_seed(drbg, &seedlist, reseed);
  1007. memzero_explicit(entropy, entropylen * 2);
  1008. return ret;
  1009. }
  1010. /* Free all substructures in a DRBG state without the DRBG state structure */
  1011. static inline void drbg_dealloc_state(struct drbg_state *drbg)
  1012. {
  1013. if (!drbg)
  1014. return;
  1015. kzfree(drbg->Vbuf);
  1016. drbg->Vbuf = NULL;
  1017. drbg->V = NULL;
  1018. kzfree(drbg->Cbuf);
  1019. drbg->Cbuf = NULL;
  1020. drbg->C = NULL;
  1021. kzfree(drbg->scratchpadbuf);
  1022. drbg->scratchpadbuf = NULL;
  1023. drbg->reseed_ctr = 0;
  1024. drbg->d_ops = NULL;
  1025. drbg->core = NULL;
  1026. }
  1027. /*
  1028. * Allocate all sub-structures for a DRBG state.
  1029. * The DRBG state structure must already be allocated.
  1030. */
  1031. static inline int drbg_alloc_state(struct drbg_state *drbg)
  1032. {
  1033. int ret = -ENOMEM;
  1034. unsigned int sb_size = 0;
  1035. switch (drbg->core->flags & DRBG_TYPE_MASK) {
  1036. #ifdef CONFIG_CRYPTO_DRBG_HMAC
  1037. case DRBG_HMAC:
  1038. drbg->d_ops = &drbg_hmac_ops;
  1039. break;
  1040. #endif /* CONFIG_CRYPTO_DRBG_HMAC */
  1041. #ifdef CONFIG_CRYPTO_DRBG_HASH
  1042. case DRBG_HASH:
  1043. drbg->d_ops = &drbg_hash_ops;
  1044. break;
  1045. #endif /* CONFIG_CRYPTO_DRBG_HASH */
  1046. #ifdef CONFIG_CRYPTO_DRBG_CTR
  1047. case DRBG_CTR:
  1048. drbg->d_ops = &drbg_ctr_ops;
  1049. break;
  1050. #endif /* CONFIG_CRYPTO_DRBG_CTR */
  1051. default:
  1052. ret = -EOPNOTSUPP;
  1053. goto err;
  1054. }
  1055. ret = drbg->d_ops->crypto_init(drbg);
  1056. if (ret < 0)
  1057. goto err;
  1058. drbg->Vbuf = kmalloc(drbg_statelen(drbg) + ret, GFP_KERNEL);
  1059. if (!drbg->Vbuf) {
  1060. ret = -ENOMEM;
  1061. goto fini;
  1062. }
  1063. drbg->V = PTR_ALIGN(drbg->Vbuf, ret + 1);
  1064. drbg->Cbuf = kmalloc(drbg_statelen(drbg) + ret, GFP_KERNEL);
  1065. if (!drbg->Cbuf) {
  1066. ret = -ENOMEM;
  1067. goto fini;
  1068. }
  1069. drbg->C = PTR_ALIGN(drbg->Cbuf, ret + 1);
  1070. /* scratchpad is only generated for CTR and Hash */
  1071. if (drbg->core->flags & DRBG_HMAC)
  1072. sb_size = 0;
  1073. else if (drbg->core->flags & DRBG_CTR)
  1074. sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
  1075. drbg_statelen(drbg) + /* df_data */
  1076. drbg_blocklen(drbg) + /* pad */
  1077. drbg_blocklen(drbg) + /* iv */
  1078. drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
  1079. else
  1080. sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
  1081. if (0 < sb_size) {
  1082. drbg->scratchpadbuf = kzalloc(sb_size + ret, GFP_KERNEL);
  1083. if (!drbg->scratchpadbuf) {
  1084. ret = -ENOMEM;
  1085. goto fini;
  1086. }
  1087. drbg->scratchpad = PTR_ALIGN(drbg->scratchpadbuf, ret + 1);
  1088. }
  1089. return 0;
  1090. fini:
  1091. drbg->d_ops->crypto_fini(drbg);
  1092. err:
  1093. drbg_dealloc_state(drbg);
  1094. return ret;
  1095. }
  1096. /*************************************************************************
  1097. * DRBG interface functions
  1098. *************************************************************************/
  1099. /*
  1100. * DRBG generate function as required by SP800-90A - this function
  1101. * generates random numbers
  1102. *
  1103. * @drbg DRBG state handle
  1104. * @buf Buffer where to store the random numbers -- the buffer must already
  1105. * be pre-allocated by caller
  1106. * @buflen Length of output buffer - this value defines the number of random
  1107. * bytes pulled from DRBG
  1108. * @addtl Additional input that is mixed into state, may be NULL -- note
  1109. * the entropy is pulled by the DRBG internally unconditionally
  1110. * as defined in SP800-90A. The additional input is mixed into
  1111. * the state in addition to the pulled entropy.
  1112. *
  1113. * return: 0 when all bytes are generated; < 0 in case of an error
  1114. */
  1115. static int drbg_generate(struct drbg_state *drbg,
  1116. unsigned char *buf, unsigned int buflen,
  1117. struct drbg_string *addtl)
  1118. {
  1119. int len = 0;
  1120. LIST_HEAD(addtllist);
  1121. if (!drbg->core) {
  1122. pr_devel("DRBG: not yet seeded\n");
  1123. return -EINVAL;
  1124. }
  1125. if (0 == buflen || !buf) {
  1126. pr_devel("DRBG: no output buffer provided\n");
  1127. return -EINVAL;
  1128. }
  1129. if (addtl && NULL == addtl->buf && 0 < addtl->len) {
  1130. pr_devel("DRBG: wrong format of additional information\n");
  1131. return -EINVAL;
  1132. }
  1133. /* 9.3.1 step 2 */
  1134. len = -EINVAL;
  1135. if (buflen > (drbg_max_request_bytes(drbg))) {
  1136. pr_devel("DRBG: requested random numbers too large %u\n",
  1137. buflen);
  1138. goto err;
  1139. }
  1140. /* 9.3.1 step 3 is implicit with the chosen DRBG */
  1141. /* 9.3.1 step 4 */
  1142. if (addtl && addtl->len > (drbg_max_addtl(drbg))) {
  1143. pr_devel("DRBG: additional information string too long %zu\n",
  1144. addtl->len);
  1145. goto err;
  1146. }
  1147. /* 9.3.1 step 5 is implicit with the chosen DRBG */
  1148. /*
  1149. * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
  1150. * here. The spec is a bit convoluted here, we make it simpler.
  1151. */
  1152. if (drbg->reseed_threshold < drbg->reseed_ctr)
  1153. drbg->seeded = false;
  1154. if (drbg->pr || !drbg->seeded) {
  1155. pr_devel("DRBG: reseeding before generation (prediction "
  1156. "resistance: %s, state %s)\n",
  1157. drbg->pr ? "true" : "false",
  1158. drbg->seeded ? "seeded" : "unseeded");
  1159. /* 9.3.1 steps 7.1 through 7.3 */
  1160. len = drbg_seed(drbg, addtl, true);
  1161. if (len)
  1162. goto err;
  1163. /* 9.3.1 step 7.4 */
  1164. addtl = NULL;
  1165. }
  1166. if (addtl && 0 < addtl->len)
  1167. list_add_tail(&addtl->list, &addtllist);
  1168. /* 9.3.1 step 8 and 10 */
  1169. len = drbg->d_ops->generate(drbg, buf, buflen, &addtllist);
  1170. /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
  1171. drbg->reseed_ctr++;
  1172. if (0 >= len)
  1173. goto err;
  1174. /*
  1175. * Section 11.3.3 requires to re-perform self tests after some
  1176. * generated random numbers. The chosen value after which self
  1177. * test is performed is arbitrary, but it should be reasonable.
  1178. * However, we do not perform the self tests because of the following
  1179. * reasons: it is mathematically impossible that the initial self tests
  1180. * were successfully and the following are not. If the initial would
  1181. * pass and the following would not, the kernel integrity is violated.
  1182. * In this case, the entire kernel operation is questionable and it
  1183. * is unlikely that the integrity violation only affects the
  1184. * correct operation of the DRBG.
  1185. *
  1186. * Albeit the following code is commented out, it is provided in
  1187. * case somebody has a need to implement the test of 11.3.3.
  1188. */
  1189. #if 0
  1190. if (drbg->reseed_ctr && !(drbg->reseed_ctr % 4096)) {
  1191. int err = 0;
  1192. pr_devel("DRBG: start to perform self test\n");
  1193. if (drbg->core->flags & DRBG_HMAC)
  1194. err = alg_test("drbg_pr_hmac_sha256",
  1195. "drbg_pr_hmac_sha256", 0, 0);
  1196. else if (drbg->core->flags & DRBG_CTR)
  1197. err = alg_test("drbg_pr_ctr_aes128",
  1198. "drbg_pr_ctr_aes128", 0, 0);
  1199. else
  1200. err = alg_test("drbg_pr_sha256",
  1201. "drbg_pr_sha256", 0, 0);
  1202. if (err) {
  1203. pr_err("DRBG: periodical self test failed\n");
  1204. /*
  1205. * uninstantiate implies that from now on, only errors
  1206. * are returned when reusing this DRBG cipher handle
  1207. */
  1208. drbg_uninstantiate(drbg);
  1209. return 0;
  1210. } else {
  1211. pr_devel("DRBG: self test successful\n");
  1212. }
  1213. }
  1214. #endif
  1215. /*
  1216. * All operations were successful, return 0 as mandated by
  1217. * the kernel crypto API interface.
  1218. */
  1219. len = 0;
  1220. err:
  1221. return len;
  1222. }
  1223. /*
  1224. * Wrapper around drbg_generate which can pull arbitrary long strings
  1225. * from the DRBG without hitting the maximum request limitation.
  1226. *
  1227. * Parameters: see drbg_generate
  1228. * Return codes: see drbg_generate -- if one drbg_generate request fails,
  1229. * the entire drbg_generate_long request fails
  1230. */
  1231. static int drbg_generate_long(struct drbg_state *drbg,
  1232. unsigned char *buf, unsigned int buflen,
  1233. struct drbg_string *addtl)
  1234. {
  1235. unsigned int len = 0;
  1236. unsigned int slice = 0;
  1237. do {
  1238. int err = 0;
  1239. unsigned int chunk = 0;
  1240. slice = ((buflen - len) / drbg_max_request_bytes(drbg));
  1241. chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
  1242. mutex_lock(&drbg->drbg_mutex);
  1243. err = drbg_generate(drbg, buf + len, chunk, addtl);
  1244. mutex_unlock(&drbg->drbg_mutex);
  1245. if (0 > err)
  1246. return err;
  1247. len += chunk;
  1248. } while (slice > 0 && (len < buflen));
  1249. return 0;
  1250. }
  1251. static void drbg_schedule_async_seed(struct random_ready_callback *rdy)
  1252. {
  1253. struct drbg_state *drbg = container_of(rdy, struct drbg_state,
  1254. random_ready);
  1255. schedule_work(&drbg->seed_work);
  1256. }
  1257. static int drbg_prepare_hrng(struct drbg_state *drbg)
  1258. {
  1259. int err;
  1260. /* We do not need an HRNG in test mode. */
  1261. if (list_empty(&drbg->test_data.list))
  1262. return 0;
  1263. INIT_WORK(&drbg->seed_work, drbg_async_seed);
  1264. drbg->random_ready.owner = THIS_MODULE;
  1265. drbg->random_ready.func = drbg_schedule_async_seed;
  1266. err = add_random_ready_callback(&drbg->random_ready);
  1267. switch (err) {
  1268. case 0:
  1269. break;
  1270. case -EALREADY:
  1271. err = 0;
  1272. /* fall through */
  1273. default:
  1274. drbg->random_ready.func = NULL;
  1275. return err;
  1276. }
  1277. drbg->jent = crypto_alloc_rng("jitterentropy_rng", 0, 0);
  1278. /*
  1279. * Require frequent reseeds until the seed source is fully
  1280. * initialized.
  1281. */
  1282. drbg->reseed_threshold = 50;
  1283. return err;
  1284. }
  1285. /*
  1286. * DRBG instantiation function as required by SP800-90A - this function
  1287. * sets up the DRBG handle, performs the initial seeding and all sanity
  1288. * checks required by SP800-90A
  1289. *
  1290. * @drbg memory of state -- if NULL, new memory is allocated
  1291. * @pers Personalization string that is mixed into state, may be NULL -- note
  1292. * the entropy is pulled by the DRBG internally unconditionally
  1293. * as defined in SP800-90A. The additional input is mixed into
  1294. * the state in addition to the pulled entropy.
  1295. * @coreref reference to core
  1296. * @pr prediction resistance enabled
  1297. *
  1298. * return
  1299. * 0 on success
  1300. * error value otherwise
  1301. */
  1302. static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
  1303. int coreref, bool pr)
  1304. {
  1305. int ret;
  1306. bool reseed = true;
  1307. pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
  1308. "%s\n", coreref, pr ? "enabled" : "disabled");
  1309. mutex_lock(&drbg->drbg_mutex);
  1310. /* 9.1 step 1 is implicit with the selected DRBG type */
  1311. /*
  1312. * 9.1 step 2 is implicit as caller can select prediction resistance
  1313. * and the flag is copied into drbg->flags --
  1314. * all DRBG types support prediction resistance
  1315. */
  1316. /* 9.1 step 4 is implicit in drbg_sec_strength */
  1317. if (!drbg->core) {
  1318. drbg->core = &drbg_cores[coreref];
  1319. drbg->pr = pr;
  1320. drbg->seeded = false;
  1321. drbg->reseed_threshold = drbg_max_requests(drbg);
  1322. ret = drbg_alloc_state(drbg);
  1323. if (ret)
  1324. goto unlock;
  1325. ret = drbg_prepare_hrng(drbg);
  1326. if (ret)
  1327. goto free_everything;
  1328. if (IS_ERR(drbg->jent)) {
  1329. ret = PTR_ERR(drbg->jent);
  1330. drbg->jent = NULL;
  1331. if (fips_enabled || ret != -ENOENT)
  1332. goto free_everything;
  1333. pr_info("DRBG: Continuing without Jitter RNG\n");
  1334. }
  1335. reseed = false;
  1336. }
  1337. ret = drbg_seed(drbg, pers, reseed);
  1338. if (ret && !reseed)
  1339. goto free_everything;
  1340. mutex_unlock(&drbg->drbg_mutex);
  1341. return ret;
  1342. unlock:
  1343. mutex_unlock(&drbg->drbg_mutex);
  1344. return ret;
  1345. free_everything:
  1346. mutex_unlock(&drbg->drbg_mutex);
  1347. drbg_uninstantiate(drbg);
  1348. return ret;
  1349. }
  1350. /*
  1351. * DRBG uninstantiate function as required by SP800-90A - this function
  1352. * frees all buffers and the DRBG handle
  1353. *
  1354. * @drbg DRBG state handle
  1355. *
  1356. * return
  1357. * 0 on success
  1358. */
  1359. static int drbg_uninstantiate(struct drbg_state *drbg)
  1360. {
  1361. if (drbg->random_ready.func) {
  1362. del_random_ready_callback(&drbg->random_ready);
  1363. cancel_work_sync(&drbg->seed_work);
  1364. crypto_free_rng(drbg->jent);
  1365. drbg->jent = NULL;
  1366. }
  1367. if (drbg->d_ops)
  1368. drbg->d_ops->crypto_fini(drbg);
  1369. drbg_dealloc_state(drbg);
  1370. /* no scrubbing of test_data -- this shall survive an uninstantiate */
  1371. return 0;
  1372. }
  1373. /*
  1374. * Helper function for setting the test data in the DRBG
  1375. *
  1376. * @drbg DRBG state handle
  1377. * @data test data
  1378. * @len test data length
  1379. */
  1380. static void drbg_kcapi_set_entropy(struct crypto_rng *tfm,
  1381. const u8 *data, unsigned int len)
  1382. {
  1383. struct drbg_state *drbg = crypto_rng_ctx(tfm);
  1384. mutex_lock(&drbg->drbg_mutex);
  1385. drbg_string_fill(&drbg->test_data, data, len);
  1386. mutex_unlock(&drbg->drbg_mutex);
  1387. }
  1388. /***************************************************************
  1389. * Kernel crypto API cipher invocations requested by DRBG
  1390. ***************************************************************/
  1391. #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
  1392. struct sdesc {
  1393. struct shash_desc shash;
  1394. char ctx[];
  1395. };
  1396. static int drbg_init_hash_kernel(struct drbg_state *drbg)
  1397. {
  1398. struct sdesc *sdesc;
  1399. struct crypto_shash *tfm;
  1400. tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
  1401. if (IS_ERR(tfm)) {
  1402. pr_info("DRBG: could not allocate digest TFM handle: %s\n",
  1403. drbg->core->backend_cra_name);
  1404. return PTR_ERR(tfm);
  1405. }
  1406. BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
  1407. sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
  1408. GFP_KERNEL);
  1409. if (!sdesc) {
  1410. crypto_free_shash(tfm);
  1411. return -ENOMEM;
  1412. }
  1413. sdesc->shash.tfm = tfm;
  1414. sdesc->shash.flags = 0;
  1415. drbg->priv_data = sdesc;
  1416. return crypto_shash_alignmask(tfm);
  1417. }
  1418. static int drbg_fini_hash_kernel(struct drbg_state *drbg)
  1419. {
  1420. struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
  1421. if (sdesc) {
  1422. crypto_free_shash(sdesc->shash.tfm);
  1423. kzfree(sdesc);
  1424. }
  1425. drbg->priv_data = NULL;
  1426. return 0;
  1427. }
  1428. static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg,
  1429. const unsigned char *key)
  1430. {
  1431. struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
  1432. crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
  1433. }
  1434. static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval,
  1435. const struct list_head *in)
  1436. {
  1437. struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
  1438. struct drbg_string *input = NULL;
  1439. crypto_shash_init(&sdesc->shash);
  1440. list_for_each_entry(input, in, list)
  1441. crypto_shash_update(&sdesc->shash, input->buf, input->len);
  1442. return crypto_shash_final(&sdesc->shash, outval);
  1443. }
  1444. #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
  1445. #ifdef CONFIG_CRYPTO_DRBG_CTR
  1446. static int drbg_fini_sym_kernel(struct drbg_state *drbg)
  1447. {
  1448. struct crypto_cipher *tfm =
  1449. (struct crypto_cipher *)drbg->priv_data;
  1450. if (tfm)
  1451. crypto_free_cipher(tfm);
  1452. drbg->priv_data = NULL;
  1453. if (drbg->ctr_handle)
  1454. crypto_free_skcipher(drbg->ctr_handle);
  1455. drbg->ctr_handle = NULL;
  1456. if (drbg->ctr_req)
  1457. skcipher_request_free(drbg->ctr_req);
  1458. drbg->ctr_req = NULL;
  1459. kfree(drbg->outscratchpadbuf);
  1460. drbg->outscratchpadbuf = NULL;
  1461. return 0;
  1462. }
  1463. static int drbg_init_sym_kernel(struct drbg_state *drbg)
  1464. {
  1465. struct crypto_cipher *tfm;
  1466. struct crypto_skcipher *sk_tfm;
  1467. struct skcipher_request *req;
  1468. unsigned int alignmask;
  1469. char ctr_name[CRYPTO_MAX_ALG_NAME];
  1470. tfm = crypto_alloc_cipher(drbg->core->backend_cra_name, 0, 0);
  1471. if (IS_ERR(tfm)) {
  1472. pr_info("DRBG: could not allocate cipher TFM handle: %s\n",
  1473. drbg->core->backend_cra_name);
  1474. return PTR_ERR(tfm);
  1475. }
  1476. BUG_ON(drbg_blocklen(drbg) != crypto_cipher_blocksize(tfm));
  1477. drbg->priv_data = tfm;
  1478. if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
  1479. drbg->core->backend_cra_name) >= CRYPTO_MAX_ALG_NAME) {
  1480. drbg_fini_sym_kernel(drbg);
  1481. return -EINVAL;
  1482. }
  1483. sk_tfm = crypto_alloc_skcipher(ctr_name, 0, 0);
  1484. if (IS_ERR(sk_tfm)) {
  1485. pr_info("DRBG: could not allocate CTR cipher TFM handle: %s\n",
  1486. ctr_name);
  1487. drbg_fini_sym_kernel(drbg);
  1488. return PTR_ERR(sk_tfm);
  1489. }
  1490. drbg->ctr_handle = sk_tfm;
  1491. crypto_init_wait(&drbg->ctr_wait);
  1492. req = skcipher_request_alloc(sk_tfm, GFP_KERNEL);
  1493. if (!req) {
  1494. pr_info("DRBG: could not allocate request queue\n");
  1495. drbg_fini_sym_kernel(drbg);
  1496. return -ENOMEM;
  1497. }
  1498. drbg->ctr_req = req;
  1499. skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  1500. CRYPTO_TFM_REQ_MAY_SLEEP,
  1501. crypto_req_done, &drbg->ctr_wait);
  1502. alignmask = crypto_skcipher_alignmask(sk_tfm);
  1503. drbg->outscratchpadbuf = kmalloc(DRBG_OUTSCRATCHLEN + alignmask,
  1504. GFP_KERNEL);
  1505. if (!drbg->outscratchpadbuf) {
  1506. drbg_fini_sym_kernel(drbg);
  1507. return -ENOMEM;
  1508. }
  1509. drbg->outscratchpad = (u8 *)PTR_ALIGN(drbg->outscratchpadbuf,
  1510. alignmask + 1);
  1511. sg_init_table(&drbg->sg_in, 1);
  1512. sg_init_one(&drbg->sg_out, drbg->outscratchpad, DRBG_OUTSCRATCHLEN);
  1513. return alignmask;
  1514. }
  1515. static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
  1516. const unsigned char *key)
  1517. {
  1518. struct crypto_cipher *tfm =
  1519. (struct crypto_cipher *)drbg->priv_data;
  1520. crypto_cipher_setkey(tfm, key, (drbg_keylen(drbg)));
  1521. }
  1522. static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
  1523. const struct drbg_string *in)
  1524. {
  1525. struct crypto_cipher *tfm =
  1526. (struct crypto_cipher *)drbg->priv_data;
  1527. /* there is only component in *in */
  1528. BUG_ON(in->len < drbg_blocklen(drbg));
  1529. crypto_cipher_encrypt_one(tfm, outval, in->buf);
  1530. return 0;
  1531. }
  1532. static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
  1533. u8 *inbuf, u32 inlen,
  1534. u8 *outbuf, u32 outlen)
  1535. {
  1536. struct scatterlist *sg_in = &drbg->sg_in, *sg_out = &drbg->sg_out;
  1537. u32 scratchpad_use = min_t(u32, outlen, DRBG_OUTSCRATCHLEN);
  1538. int ret;
  1539. if (inbuf) {
  1540. /* Use caller-provided input buffer */
  1541. sg_set_buf(sg_in, inbuf, inlen);
  1542. } else {
  1543. /* Use scratchpad for in-place operation */
  1544. inlen = scratchpad_use;
  1545. memset(drbg->outscratchpad, 0, scratchpad_use);
  1546. sg_set_buf(sg_in, drbg->outscratchpad, scratchpad_use);
  1547. }
  1548. while (outlen) {
  1549. u32 cryptlen = min3(inlen, outlen, (u32)DRBG_OUTSCRATCHLEN);
  1550. /* Output buffer may not be valid for SGL, use scratchpad */
  1551. skcipher_request_set_crypt(drbg->ctr_req, sg_in, sg_out,
  1552. cryptlen, drbg->V);
  1553. ret = crypto_wait_req(crypto_skcipher_encrypt(drbg->ctr_req),
  1554. &drbg->ctr_wait);
  1555. if (ret)
  1556. goto out;
  1557. crypto_init_wait(&drbg->ctr_wait);
  1558. memcpy(outbuf, drbg->outscratchpad, cryptlen);
  1559. memzero_explicit(drbg->outscratchpad, cryptlen);
  1560. outlen -= cryptlen;
  1561. outbuf += cryptlen;
  1562. }
  1563. ret = 0;
  1564. out:
  1565. return ret;
  1566. }
  1567. #endif /* CONFIG_CRYPTO_DRBG_CTR */
  1568. /***************************************************************
  1569. * Kernel crypto API interface to register DRBG
  1570. ***************************************************************/
  1571. /*
  1572. * Look up the DRBG flags by given kernel crypto API cra_name
  1573. * The code uses the drbg_cores definition to do this
  1574. *
  1575. * @cra_name kernel crypto API cra_name
  1576. * @coreref reference to integer which is filled with the pointer to
  1577. * the applicable core
  1578. * @pr reference for setting prediction resistance
  1579. *
  1580. * return: flags
  1581. */
  1582. static inline void drbg_convert_tfm_core(const char *cra_driver_name,
  1583. int *coreref, bool *pr)
  1584. {
  1585. int i = 0;
  1586. size_t start = 0;
  1587. int len = 0;
  1588. *pr = true;
  1589. /* disassemble the names */
  1590. if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
  1591. start = 10;
  1592. *pr = false;
  1593. } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
  1594. start = 8;
  1595. } else {
  1596. return;
  1597. }
  1598. /* remove the first part */
  1599. len = strlen(cra_driver_name) - start;
  1600. for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
  1601. if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
  1602. len)) {
  1603. *coreref = i;
  1604. return;
  1605. }
  1606. }
  1607. }
  1608. static int drbg_kcapi_init(struct crypto_tfm *tfm)
  1609. {
  1610. struct drbg_state *drbg = crypto_tfm_ctx(tfm);
  1611. mutex_init(&drbg->drbg_mutex);
  1612. return 0;
  1613. }
  1614. static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
  1615. {
  1616. drbg_uninstantiate(crypto_tfm_ctx(tfm));
  1617. }
  1618. /*
  1619. * Generate random numbers invoked by the kernel crypto API:
  1620. * The API of the kernel crypto API is extended as follows:
  1621. *
  1622. * src is additional input supplied to the RNG.
  1623. * slen is the length of src.
  1624. * dst is the output buffer where random data is to be stored.
  1625. * dlen is the length of dst.
  1626. */
  1627. static int drbg_kcapi_random(struct crypto_rng *tfm,
  1628. const u8 *src, unsigned int slen,
  1629. u8 *dst, unsigned int dlen)
  1630. {
  1631. struct drbg_state *drbg = crypto_rng_ctx(tfm);
  1632. struct drbg_string *addtl = NULL;
  1633. struct drbg_string string;
  1634. if (slen) {
  1635. /* linked list variable is now local to allow modification */
  1636. drbg_string_fill(&string, src, slen);
  1637. addtl = &string;
  1638. }
  1639. return drbg_generate_long(drbg, dst, dlen, addtl);
  1640. }
  1641. /*
  1642. * Seed the DRBG invoked by the kernel crypto API
  1643. */
  1644. static int drbg_kcapi_seed(struct crypto_rng *tfm,
  1645. const u8 *seed, unsigned int slen)
  1646. {
  1647. struct drbg_state *drbg = crypto_rng_ctx(tfm);
  1648. struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
  1649. bool pr = false;
  1650. struct drbg_string string;
  1651. struct drbg_string *seed_string = NULL;
  1652. int coreref = 0;
  1653. drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
  1654. &pr);
  1655. if (0 < slen) {
  1656. drbg_string_fill(&string, seed, slen);
  1657. seed_string = &string;
  1658. }
  1659. return drbg_instantiate(drbg, seed_string, coreref, pr);
  1660. }
  1661. /***************************************************************
  1662. * Kernel module: code to load the module
  1663. ***************************************************************/
  1664. /*
  1665. * Tests as defined in 11.3.2 in addition to the cipher tests: testing
  1666. * of the error handling.
  1667. *
  1668. * Note: testing of failing seed source as defined in 11.3.2 is not applicable
  1669. * as seed source of get_random_bytes does not fail.
  1670. *
  1671. * Note 2: There is no sensible way of testing the reseed counter
  1672. * enforcement, so skip it.
  1673. */
  1674. static inline int __init drbg_healthcheck_sanity(void)
  1675. {
  1676. int len = 0;
  1677. #define OUTBUFLEN 16
  1678. unsigned char buf[OUTBUFLEN];
  1679. struct drbg_state *drbg = NULL;
  1680. int ret = -EFAULT;
  1681. int rc = -EFAULT;
  1682. bool pr = false;
  1683. int coreref = 0;
  1684. struct drbg_string addtl;
  1685. size_t max_addtllen, max_request_bytes;
  1686. /* only perform test in FIPS mode */
  1687. if (!fips_enabled)
  1688. return 0;
  1689. #ifdef CONFIG_CRYPTO_DRBG_CTR
  1690. drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
  1691. #elif defined CONFIG_CRYPTO_DRBG_HASH
  1692. drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
  1693. #else
  1694. drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
  1695. #endif
  1696. drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
  1697. if (!drbg)
  1698. return -ENOMEM;
  1699. mutex_init(&drbg->drbg_mutex);
  1700. drbg->core = &drbg_cores[coreref];
  1701. drbg->reseed_threshold = drbg_max_requests(drbg);
  1702. /*
  1703. * if the following tests fail, it is likely that there is a buffer
  1704. * overflow as buf is much smaller than the requested or provided
  1705. * string lengths -- in case the error handling does not succeed
  1706. * we may get an OOPS. And we want to get an OOPS as this is a
  1707. * grave bug.
  1708. */
  1709. max_addtllen = drbg_max_addtl(drbg);
  1710. max_request_bytes = drbg_max_request_bytes(drbg);
  1711. drbg_string_fill(&addtl, buf, max_addtllen + 1);
  1712. /* overflow addtllen with additonal info string */
  1713. len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
  1714. BUG_ON(0 < len);
  1715. /* overflow max_bits */
  1716. len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
  1717. BUG_ON(0 < len);
  1718. /* overflow max addtllen with personalization string */
  1719. ret = drbg_seed(drbg, &addtl, false);
  1720. BUG_ON(0 == ret);
  1721. /* all tests passed */
  1722. rc = 0;
  1723. pr_devel("DRBG: Sanity tests for failure code paths successfully "
  1724. "completed\n");
  1725. kfree(drbg);
  1726. return rc;
  1727. }
  1728. static struct rng_alg drbg_algs[22];
  1729. /*
  1730. * Fill the array drbg_algs used to register the different DRBGs
  1731. * with the kernel crypto API. To fill the array, the information
  1732. * from drbg_cores[] is used.
  1733. */
  1734. static inline void __init drbg_fill_array(struct rng_alg *alg,
  1735. const struct drbg_core *core, int pr)
  1736. {
  1737. int pos = 0;
  1738. static int priority = 200;
  1739. memcpy(alg->base.cra_name, "stdrng", 6);
  1740. if (pr) {
  1741. memcpy(alg->base.cra_driver_name, "drbg_pr_", 8);
  1742. pos = 8;
  1743. } else {
  1744. memcpy(alg->base.cra_driver_name, "drbg_nopr_", 10);
  1745. pos = 10;
  1746. }
  1747. memcpy(alg->base.cra_driver_name + pos, core->cra_name,
  1748. strlen(core->cra_name));
  1749. alg->base.cra_priority = priority;
  1750. priority++;
  1751. /*
  1752. * If FIPS mode enabled, the selected DRBG shall have the
  1753. * highest cra_priority over other stdrng instances to ensure
  1754. * it is selected.
  1755. */
  1756. if (fips_enabled)
  1757. alg->base.cra_priority += 200;
  1758. alg->base.cra_ctxsize = sizeof(struct drbg_state);
  1759. alg->base.cra_module = THIS_MODULE;
  1760. alg->base.cra_init = drbg_kcapi_init;
  1761. alg->base.cra_exit = drbg_kcapi_cleanup;
  1762. alg->generate = drbg_kcapi_random;
  1763. alg->seed = drbg_kcapi_seed;
  1764. alg->set_ent = drbg_kcapi_set_entropy;
  1765. alg->seedsize = 0;
  1766. }
  1767. static int __init drbg_init(void)
  1768. {
  1769. unsigned int i = 0; /* pointer to drbg_algs */
  1770. unsigned int j = 0; /* pointer to drbg_cores */
  1771. int ret;
  1772. ret = drbg_healthcheck_sanity();
  1773. if (ret)
  1774. return ret;
  1775. if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
  1776. pr_info("DRBG: Cannot register all DRBG types"
  1777. "(slots needed: %zu, slots available: %zu)\n",
  1778. ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
  1779. return -EFAULT;
  1780. }
  1781. /*
  1782. * each DRBG definition can be used with PR and without PR, thus
  1783. * we instantiate each DRBG in drbg_cores[] twice.
  1784. *
  1785. * As the order of placing them into the drbg_algs array matters
  1786. * (the later DRBGs receive a higher cra_priority) we register the
  1787. * prediction resistance DRBGs first as the should not be too
  1788. * interesting.
  1789. */
  1790. for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
  1791. drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
  1792. for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
  1793. drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
  1794. return crypto_register_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
  1795. }
  1796. static void __exit drbg_exit(void)
  1797. {
  1798. crypto_unregister_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
  1799. }
  1800. module_init(drbg_init);
  1801. module_exit(drbg_exit);
  1802. #ifndef CRYPTO_DRBG_HASH_STRING
  1803. #define CRYPTO_DRBG_HASH_STRING ""
  1804. #endif
  1805. #ifndef CRYPTO_DRBG_HMAC_STRING
  1806. #define CRYPTO_DRBG_HMAC_STRING ""
  1807. #endif
  1808. #ifndef CRYPTO_DRBG_CTR_STRING
  1809. #define CRYPTO_DRBG_CTR_STRING ""
  1810. #endif
  1811. MODULE_LICENSE("GPL");
  1812. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  1813. MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
  1814. "using following cores: "
  1815. CRYPTO_DRBG_HASH_STRING
  1816. CRYPTO_DRBG_HMAC_STRING
  1817. CRYPTO_DRBG_CTR_STRING);
  1818. MODULE_ALIAS_CRYPTO("stdrng");