drbg.c 57 KB

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