drbg.c 58 KB

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