drbg.c 60 KB

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