drbg.c 56 KB

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