drbg.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  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 = "ecb(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 = "ecb(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 = "ecb(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. * The FIPS test can be called in an endless loop until it returns
  220. * true. Although the code looks like a potential for a deadlock, it
  221. * is not the case, because returning a false cannot mathematically
  222. * occur (except once when a reseed took place and the updated state
  223. * would is now set up such that the generation of new value returns
  224. * an identical one -- this is most unlikely and would happen only once).
  225. * Thus, if this function repeatedly returns false and thus would cause
  226. * a deadlock, the integrity of the entire kernel is lost.
  227. *
  228. * @drbg DRBG handle
  229. * @buf output buffer of random data to be checked
  230. *
  231. * return:
  232. * true on success
  233. * false on error
  234. */
  235. static bool drbg_fips_continuous_test(struct drbg_state *drbg,
  236. const unsigned char *buf)
  237. {
  238. #ifdef CONFIG_CRYPTO_FIPS
  239. int ret = 0;
  240. /* skip test if we test the overall system */
  241. if (drbg->test_data)
  242. return true;
  243. /* only perform test in FIPS mode */
  244. if (0 == fips_enabled)
  245. return true;
  246. if (!drbg->fips_primed) {
  247. /* Priming of FIPS test */
  248. memcpy(drbg->prev, buf, drbg_blocklen(drbg));
  249. drbg->fips_primed = true;
  250. /* return false due to priming, i.e. another round is needed */
  251. return false;
  252. }
  253. ret = memcmp(drbg->prev, buf, drbg_blocklen(drbg));
  254. memcpy(drbg->prev, buf, drbg_blocklen(drbg));
  255. /* the test shall pass when the two compared values are not equal */
  256. return ret != 0;
  257. #else
  258. return true;
  259. #endif /* CONFIG_CRYPTO_FIPS */
  260. }
  261. /*
  262. * Convert an integer into a byte representation of this integer.
  263. * The byte representation is big-endian
  264. *
  265. * @val value to be converted
  266. * @buf buffer holding the converted integer -- caller must ensure that
  267. * buffer size is at least 32 bit
  268. */
  269. #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
  270. static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf)
  271. {
  272. struct s {
  273. __be32 conv;
  274. };
  275. struct s *conversion = (struct s *) buf;
  276. conversion->conv = cpu_to_be32(val);
  277. }
  278. /*
  279. * Increment buffer
  280. *
  281. * @dst buffer to increment
  282. * @add value to add
  283. */
  284. static inline void drbg_add_buf(unsigned char *dst, size_t dstlen,
  285. const unsigned char *add, size_t addlen)
  286. {
  287. /* implied: dstlen > addlen */
  288. unsigned char *dstptr;
  289. const unsigned char *addptr;
  290. unsigned int remainder = 0;
  291. size_t len = addlen;
  292. dstptr = dst + (dstlen-1);
  293. addptr = add + (addlen-1);
  294. while (len) {
  295. remainder += *dstptr + *addptr;
  296. *dstptr = remainder & 0xff;
  297. remainder >>= 8;
  298. len--; dstptr--; addptr--;
  299. }
  300. len = dstlen - addlen;
  301. while (len && remainder > 0) {
  302. remainder = *dstptr + 1;
  303. *dstptr = remainder & 0xff;
  304. remainder >>= 8;
  305. len--; dstptr--;
  306. }
  307. }
  308. #endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */
  309. /******************************************************************
  310. * CTR DRBG callback functions
  311. ******************************************************************/
  312. #ifdef CONFIG_CRYPTO_DRBG_CTR
  313. #define CRYPTO_DRBG_CTR_STRING "CTR "
  314. static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
  315. unsigned char *outval, const struct drbg_string *in);
  316. static int drbg_init_sym_kernel(struct drbg_state *drbg);
  317. static int drbg_fini_sym_kernel(struct drbg_state *drbg);
  318. /* BCC function for CTR DRBG as defined in 10.4.3 */
  319. static int drbg_ctr_bcc(struct drbg_state *drbg,
  320. unsigned char *out, const unsigned char *key,
  321. struct list_head *in)
  322. {
  323. int ret = 0;
  324. struct drbg_string *curr = NULL;
  325. struct drbg_string data;
  326. short cnt = 0;
  327. drbg_string_fill(&data, out, drbg_blocklen(drbg));
  328. /* 10.4.3 step 1 */
  329. memset(out, 0, drbg_blocklen(drbg));
  330. /* 10.4.3 step 2 / 4 */
  331. list_for_each_entry(curr, in, list) {
  332. const unsigned char *pos = curr->buf;
  333. size_t len = curr->len;
  334. /* 10.4.3 step 4.1 */
  335. while (len) {
  336. /* 10.4.3 step 4.2 */
  337. if (drbg_blocklen(drbg) == cnt) {
  338. cnt = 0;
  339. ret = drbg_kcapi_sym(drbg, key, out, &data);
  340. if (ret)
  341. return ret;
  342. }
  343. out[cnt] ^= *pos;
  344. pos++;
  345. cnt++;
  346. len--;
  347. }
  348. }
  349. /* 10.4.3 step 4.2 for last block */
  350. if (cnt)
  351. ret = drbg_kcapi_sym(drbg, key, out, &data);
  352. return ret;
  353. }
  354. /*
  355. * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
  356. * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
  357. * the scratchpad is used as follows:
  358. * drbg_ctr_update:
  359. * temp
  360. * start: drbg->scratchpad
  361. * length: drbg_statelen(drbg) + drbg_blocklen(drbg)
  362. * note: the cipher writing into this variable works
  363. * blocklen-wise. Now, when the statelen is not a multiple
  364. * of blocklen, the generateion loop below "spills over"
  365. * by at most blocklen. Thus, we need to give sufficient
  366. * memory.
  367. * df_data
  368. * start: drbg->scratchpad +
  369. * drbg_statelen(drbg) + drbg_blocklen(drbg)
  370. * length: drbg_statelen(drbg)
  371. *
  372. * drbg_ctr_df:
  373. * pad
  374. * start: df_data + drbg_statelen(drbg)
  375. * length: drbg_blocklen(drbg)
  376. * iv
  377. * start: pad + drbg_blocklen(drbg)
  378. * length: drbg_blocklen(drbg)
  379. * temp
  380. * start: iv + drbg_blocklen(drbg)
  381. * length: drbg_satelen(drbg) + drbg_blocklen(drbg)
  382. * note: temp is the buffer that the BCC function operates
  383. * on. BCC operates blockwise. drbg_statelen(drbg)
  384. * is sufficient when the DRBG state length is a multiple
  385. * of the block size. For AES192 (and maybe other ciphers)
  386. * this is not correct and the length for temp is
  387. * insufficient (yes, that also means for such ciphers,
  388. * the final output of all BCC rounds are truncated).
  389. * Therefore, add drbg_blocklen(drbg) to cover all
  390. * possibilities.
  391. */
  392. /* Derivation Function for CTR DRBG as defined in 10.4.2 */
  393. static int drbg_ctr_df(struct drbg_state *drbg,
  394. unsigned char *df_data, size_t bytes_to_return,
  395. struct list_head *seedlist)
  396. {
  397. int ret = -EFAULT;
  398. unsigned char L_N[8];
  399. /* S3 is input */
  400. struct drbg_string S1, S2, S4, cipherin;
  401. LIST_HEAD(bcc_list);
  402. unsigned char *pad = df_data + drbg_statelen(drbg);
  403. unsigned char *iv = pad + drbg_blocklen(drbg);
  404. unsigned char *temp = iv + drbg_blocklen(drbg);
  405. size_t padlen = 0;
  406. unsigned int templen = 0;
  407. /* 10.4.2 step 7 */
  408. unsigned int i = 0;
  409. /* 10.4.2 step 8 */
  410. const unsigned char *K = (unsigned char *)
  411. "\x00\x01\x02\x03\x04\x05\x06\x07"
  412. "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  413. "\x10\x11\x12\x13\x14\x15\x16\x17"
  414. "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
  415. unsigned char *X;
  416. size_t generated_len = 0;
  417. size_t inputlen = 0;
  418. struct drbg_string *seed = NULL;
  419. memset(pad, 0, drbg_blocklen(drbg));
  420. memset(iv, 0, drbg_blocklen(drbg));
  421. memset(temp, 0, drbg_statelen(drbg));
  422. /* 10.4.2 step 1 is implicit as we work byte-wise */
  423. /* 10.4.2 step 2 */
  424. if ((512/8) < bytes_to_return)
  425. return -EINVAL;
  426. /* 10.4.2 step 2 -- calculate the entire length of all input data */
  427. list_for_each_entry(seed, seedlist, list)
  428. inputlen += seed->len;
  429. drbg_cpu_to_be32(inputlen, &L_N[0]);
  430. /* 10.4.2 step 3 */
  431. drbg_cpu_to_be32(bytes_to_return, &L_N[4]);
  432. /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
  433. padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
  434. /* wrap the padlen appropriately */
  435. if (padlen)
  436. padlen = drbg_blocklen(drbg) - padlen;
  437. /*
  438. * pad / padlen contains the 0x80 byte and the following zero bytes.
  439. * As the calculated padlen value only covers the number of zero
  440. * bytes, this value has to be incremented by one for the 0x80 byte.
  441. */
  442. padlen++;
  443. pad[0] = 0x80;
  444. /* 10.4.2 step 4 -- first fill the linked list and then order it */
  445. drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
  446. list_add_tail(&S1.list, &bcc_list);
  447. drbg_string_fill(&S2, L_N, sizeof(L_N));
  448. list_add_tail(&S2.list, &bcc_list);
  449. list_splice_tail(seedlist, &bcc_list);
  450. drbg_string_fill(&S4, pad, padlen);
  451. list_add_tail(&S4.list, &bcc_list);
  452. /* 10.4.2 step 9 */
  453. while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
  454. /*
  455. * 10.4.2 step 9.1 - the padding is implicit as the buffer
  456. * holds zeros after allocation -- even the increment of i
  457. * is irrelevant as the increment remains within length of i
  458. */
  459. drbg_cpu_to_be32(i, iv);
  460. /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
  461. ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
  462. if (ret)
  463. goto out;
  464. /* 10.4.2 step 9.3 */
  465. i++;
  466. templen += drbg_blocklen(drbg);
  467. }
  468. /* 10.4.2 step 11 */
  469. X = temp + (drbg_keylen(drbg));
  470. drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
  471. /* 10.4.2 step 12: overwriting of outval is implemented in next step */
  472. /* 10.4.2 step 13 */
  473. while (generated_len < bytes_to_return) {
  474. short blocklen = 0;
  475. /*
  476. * 10.4.2 step 13.1: the truncation of the key length is
  477. * implicit as the key is only drbg_blocklen in size based on
  478. * the implementation of the cipher function callback
  479. */
  480. ret = drbg_kcapi_sym(drbg, temp, X, &cipherin);
  481. if (ret)
  482. goto out;
  483. blocklen = (drbg_blocklen(drbg) <
  484. (bytes_to_return - generated_len)) ?
  485. drbg_blocklen(drbg) :
  486. (bytes_to_return - generated_len);
  487. /* 10.4.2 step 13.2 and 14 */
  488. memcpy(df_data + generated_len, X, blocklen);
  489. generated_len += blocklen;
  490. }
  491. ret = 0;
  492. out:
  493. memset(iv, 0, drbg_blocklen(drbg));
  494. memset(temp, 0, drbg_statelen(drbg));
  495. memset(pad, 0, drbg_blocklen(drbg));
  496. return ret;
  497. }
  498. /*
  499. * update function of CTR DRBG as defined in 10.2.1.2
  500. *
  501. * The reseed variable has an enhanced meaning compared to the update
  502. * functions of the other DRBGs as follows:
  503. * 0 => initial seed from initialization
  504. * 1 => reseed via drbg_seed
  505. * 2 => first invocation from drbg_ctr_update when addtl is present. In
  506. * this case, the df_data scratchpad is not deleted so that it is
  507. * available for another calls to prevent calling the DF function
  508. * again.
  509. * 3 => second invocation from drbg_ctr_update. When the update function
  510. * was called with addtl, the df_data memory already contains the
  511. * DFed addtl information and we do not need to call DF again.
  512. */
  513. static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
  514. int reseed)
  515. {
  516. int ret = -EFAULT;
  517. /* 10.2.1.2 step 1 */
  518. unsigned char *temp = drbg->scratchpad;
  519. unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
  520. drbg_blocklen(drbg);
  521. unsigned char *temp_p, *df_data_p; /* pointer to iterate over buffers */
  522. unsigned int len = 0;
  523. struct drbg_string cipherin;
  524. unsigned char prefix = DRBG_PREFIX1;
  525. memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
  526. if (3 > reseed)
  527. memset(df_data, 0, drbg_statelen(drbg));
  528. /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
  529. if (seed) {
  530. ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
  531. if (ret)
  532. goto out;
  533. }
  534. drbg_string_fill(&cipherin, drbg->V, drbg_blocklen(drbg));
  535. /*
  536. * 10.2.1.3.2 steps 2 and 3 are already covered as the allocation
  537. * zeroizes all memory during initialization
  538. */
  539. while (len < (drbg_statelen(drbg))) {
  540. /* 10.2.1.2 step 2.1 */
  541. drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
  542. /*
  543. * 10.2.1.2 step 2.2 */
  544. ret = drbg_kcapi_sym(drbg, drbg->C, temp + len, &cipherin);
  545. if (ret)
  546. goto out;
  547. /* 10.2.1.2 step 2.3 and 3 */
  548. len += drbg_blocklen(drbg);
  549. }
  550. /* 10.2.1.2 step 4 */
  551. temp_p = temp;
  552. df_data_p = df_data;
  553. for (len = 0; len < drbg_statelen(drbg); len++) {
  554. *temp_p ^= *df_data_p;
  555. df_data_p++; temp_p++;
  556. }
  557. /* 10.2.1.2 step 5 */
  558. memcpy(drbg->C, temp, drbg_keylen(drbg));
  559. /* 10.2.1.2 step 6 */
  560. memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
  561. ret = 0;
  562. out:
  563. memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
  564. if (2 != reseed)
  565. memset(df_data, 0, drbg_statelen(drbg));
  566. return ret;
  567. }
  568. /*
  569. * scratchpad use: drbg_ctr_update is called independently from
  570. * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
  571. */
  572. /* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
  573. static int drbg_ctr_generate(struct drbg_state *drbg,
  574. unsigned char *buf, unsigned int buflen,
  575. struct list_head *addtl)
  576. {
  577. int len = 0;
  578. int ret = 0;
  579. struct drbg_string data;
  580. unsigned char prefix = DRBG_PREFIX1;
  581. memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
  582. /* 10.2.1.5.2 step 2 */
  583. if (addtl && !list_empty(addtl)) {
  584. ret = drbg_ctr_update(drbg, addtl, 2);
  585. if (ret)
  586. return 0;
  587. }
  588. /* 10.2.1.5.2 step 4.1 */
  589. drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
  590. drbg_string_fill(&data, drbg->V, drbg_blocklen(drbg));
  591. while (len < buflen) {
  592. int outlen = 0;
  593. /* 10.2.1.5.2 step 4.2 */
  594. ret = drbg_kcapi_sym(drbg, drbg->C, drbg->scratchpad, &data);
  595. if (ret) {
  596. len = ret;
  597. goto out;
  598. }
  599. outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
  600. drbg_blocklen(drbg) : (buflen - len);
  601. if (!drbg_fips_continuous_test(drbg, drbg->scratchpad)) {
  602. /* 10.2.1.5.2 step 6 */
  603. drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
  604. continue;
  605. }
  606. /* 10.2.1.5.2 step 4.3 */
  607. memcpy(buf + len, drbg->scratchpad, outlen);
  608. len += outlen;
  609. /* 10.2.1.5.2 step 6 */
  610. if (len < buflen)
  611. drbg_add_buf(drbg->V, drbg_blocklen(drbg), &prefix, 1);
  612. }
  613. /* 10.2.1.5.2 step 6 */
  614. ret = drbg_ctr_update(drbg, NULL, 3);
  615. if (ret)
  616. len = ret;
  617. out:
  618. memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
  619. return len;
  620. }
  621. static struct drbg_state_ops drbg_ctr_ops = {
  622. .update = drbg_ctr_update,
  623. .generate = drbg_ctr_generate,
  624. .crypto_init = drbg_init_sym_kernel,
  625. .crypto_fini = drbg_fini_sym_kernel,
  626. };
  627. #endif /* CONFIG_CRYPTO_DRBG_CTR */
  628. /******************************************************************
  629. * HMAC DRBG callback functions
  630. ******************************************************************/
  631. #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
  632. static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
  633. unsigned char *outval, const struct list_head *in);
  634. static int drbg_init_hash_kernel(struct drbg_state *drbg);
  635. static int drbg_fini_hash_kernel(struct drbg_state *drbg);
  636. #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
  637. #ifdef CONFIG_CRYPTO_DRBG_HMAC
  638. #define CRYPTO_DRBG_HMAC_STRING "HMAC "
  639. /* update function of HMAC DRBG as defined in 10.1.2.2 */
  640. static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
  641. int reseed)
  642. {
  643. int ret = -EFAULT;
  644. int i = 0;
  645. struct drbg_string seed1, seed2, vdata;
  646. LIST_HEAD(seedlist);
  647. LIST_HEAD(vdatalist);
  648. if (!reseed)
  649. /* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */
  650. memset(drbg->V, 1, drbg_statelen(drbg));
  651. drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
  652. list_add_tail(&seed1.list, &seedlist);
  653. /* buffer of seed2 will be filled in for loop below with one byte */
  654. drbg_string_fill(&seed2, NULL, 1);
  655. list_add_tail(&seed2.list, &seedlist);
  656. /* input data of seed is allowed to be NULL at this point */
  657. if (seed)
  658. list_splice_tail(seed, &seedlist);
  659. drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
  660. list_add_tail(&vdata.list, &vdatalist);
  661. for (i = 2; 0 < i; i--) {
  662. /* first round uses 0x0, second 0x1 */
  663. unsigned char prefix = DRBG_PREFIX0;
  664. if (1 == i)
  665. prefix = DRBG_PREFIX1;
  666. /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
  667. seed2.buf = &prefix;
  668. ret = drbg_kcapi_hash(drbg, drbg->C, drbg->C, &seedlist);
  669. if (ret)
  670. return ret;
  671. /* 10.1.2.2 step 2 and 5 -- HMAC for V */
  672. ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &vdatalist);
  673. if (ret)
  674. return ret;
  675. /* 10.1.2.2 step 3 */
  676. if (!seed)
  677. return ret;
  678. }
  679. return 0;
  680. }
  681. /* generate function of HMAC DRBG as defined in 10.1.2.5 */
  682. static int drbg_hmac_generate(struct drbg_state *drbg,
  683. unsigned char *buf,
  684. unsigned int buflen,
  685. struct list_head *addtl)
  686. {
  687. int len = 0;
  688. int ret = 0;
  689. struct drbg_string data;
  690. LIST_HEAD(datalist);
  691. /* 10.1.2.5 step 2 */
  692. if (addtl && !list_empty(addtl)) {
  693. ret = drbg_hmac_update(drbg, addtl, 1);
  694. if (ret)
  695. return ret;
  696. }
  697. drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
  698. list_add_tail(&data.list, &datalist);
  699. while (len < buflen) {
  700. unsigned int outlen = 0;
  701. /* 10.1.2.5 step 4.1 */
  702. ret = drbg_kcapi_hash(drbg, drbg->C, drbg->V, &datalist);
  703. if (ret)
  704. return ret;
  705. outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
  706. drbg_blocklen(drbg) : (buflen - len);
  707. if (!drbg_fips_continuous_test(drbg, drbg->V))
  708. continue;
  709. /* 10.1.2.5 step 4.2 */
  710. memcpy(buf + len, drbg->V, outlen);
  711. len += outlen;
  712. }
  713. /* 10.1.2.5 step 6 */
  714. if (addtl && !list_empty(addtl))
  715. ret = drbg_hmac_update(drbg, addtl, 1);
  716. else
  717. ret = drbg_hmac_update(drbg, NULL, 1);
  718. if (ret)
  719. return ret;
  720. return len;
  721. }
  722. static struct drbg_state_ops drbg_hmac_ops = {
  723. .update = drbg_hmac_update,
  724. .generate = drbg_hmac_generate,
  725. .crypto_init = drbg_init_hash_kernel,
  726. .crypto_fini = drbg_fini_hash_kernel,
  727. };
  728. #endif /* CONFIG_CRYPTO_DRBG_HMAC */
  729. /******************************************************************
  730. * Hash DRBG callback functions
  731. ******************************************************************/
  732. #ifdef CONFIG_CRYPTO_DRBG_HASH
  733. #define CRYPTO_DRBG_HASH_STRING "HASH "
  734. /*
  735. * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
  736. * interlinked, the scratchpad is used as follows:
  737. * drbg_hash_update
  738. * start: drbg->scratchpad
  739. * length: drbg_statelen(drbg)
  740. * drbg_hash_df:
  741. * start: drbg->scratchpad + drbg_statelen(drbg)
  742. * length: drbg_blocklen(drbg)
  743. *
  744. * drbg_hash_process_addtl uses the scratchpad, but fully completes
  745. * before either of the functions mentioned before are invoked. Therefore,
  746. * drbg_hash_process_addtl does not need to be specifically considered.
  747. */
  748. /* Derivation Function for Hash DRBG as defined in 10.4.1 */
  749. static int drbg_hash_df(struct drbg_state *drbg,
  750. unsigned char *outval, size_t outlen,
  751. struct list_head *entropylist)
  752. {
  753. int ret = 0;
  754. size_t len = 0;
  755. unsigned char input[5];
  756. unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
  757. struct drbg_string data;
  758. memset(tmp, 0, drbg_blocklen(drbg));
  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, NULL, 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. memset(drbg->scratchpad, 0, drbg_statelen(drbg));
  794. if (!seed)
  795. return -EINVAL;
  796. if (reseed) {
  797. /* 10.1.1.3 step 1 */
  798. memcpy(V, drbg->V, drbg_statelen(drbg));
  799. drbg_string_fill(&data1, &prefix, 1);
  800. list_add_tail(&data1.list, &datalist);
  801. drbg_string_fill(&data2, V, drbg_statelen(drbg));
  802. list_add_tail(&data2.list, &datalist);
  803. }
  804. list_splice_tail(seed, &datalist);
  805. /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
  806. ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
  807. if (ret)
  808. goto out;
  809. /* 10.1.1.2 / 10.1.1.3 step 4 */
  810. prefix = DRBG_PREFIX0;
  811. drbg_string_fill(&data1, &prefix, 1);
  812. list_add_tail(&data1.list, &datalist2);
  813. drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
  814. list_add_tail(&data2.list, &datalist2);
  815. /* 10.1.1.2 / 10.1.1.3 step 4 */
  816. ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
  817. out:
  818. memset(drbg->scratchpad, 0, drbg_statelen(drbg));
  819. return ret;
  820. }
  821. /* processing of additional information string for Hash DRBG */
  822. static int drbg_hash_process_addtl(struct drbg_state *drbg,
  823. struct list_head *addtl)
  824. {
  825. int ret = 0;
  826. struct drbg_string data1, data2;
  827. LIST_HEAD(datalist);
  828. unsigned char prefix = DRBG_PREFIX2;
  829. /* this is value w as per documentation */
  830. memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
  831. /* 10.1.1.4 step 2 */
  832. if (!addtl || list_empty(addtl))
  833. return 0;
  834. /* 10.1.1.4 step 2a */
  835. drbg_string_fill(&data1, &prefix, 1);
  836. drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
  837. list_add_tail(&data1.list, &datalist);
  838. list_add_tail(&data2.list, &datalist);
  839. list_splice_tail(addtl, &datalist);
  840. ret = drbg_kcapi_hash(drbg, NULL, drbg->scratchpad, &datalist);
  841. if (ret)
  842. goto out;
  843. /* 10.1.1.4 step 2b */
  844. drbg_add_buf(drbg->V, drbg_statelen(drbg),
  845. drbg->scratchpad, drbg_blocklen(drbg));
  846. out:
  847. memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
  848. return ret;
  849. }
  850. /* Hashgen defined in 10.1.1.4 */
  851. static int drbg_hash_hashgen(struct drbg_state *drbg,
  852. unsigned char *buf,
  853. unsigned int buflen)
  854. {
  855. int len = 0;
  856. int ret = 0;
  857. unsigned char *src = drbg->scratchpad;
  858. unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
  859. struct drbg_string data;
  860. LIST_HEAD(datalist);
  861. unsigned char prefix = DRBG_PREFIX1;
  862. memset(src, 0, drbg_statelen(drbg));
  863. memset(dst, 0, drbg_blocklen(drbg));
  864. /* 10.1.1.4 step hashgen 2 */
  865. memcpy(src, drbg->V, drbg_statelen(drbg));
  866. drbg_string_fill(&data, src, drbg_statelen(drbg));
  867. list_add_tail(&data.list, &datalist);
  868. while (len < buflen) {
  869. unsigned int outlen = 0;
  870. /* 10.1.1.4 step hashgen 4.1 */
  871. ret = drbg_kcapi_hash(drbg, NULL, dst, &datalist);
  872. if (ret) {
  873. len = ret;
  874. goto out;
  875. }
  876. outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
  877. drbg_blocklen(drbg) : (buflen - len);
  878. if (!drbg_fips_continuous_test(drbg, dst)) {
  879. drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1);
  880. continue;
  881. }
  882. /* 10.1.1.4 step hashgen 4.2 */
  883. memcpy(buf + len, dst, outlen);
  884. len += outlen;
  885. /* 10.1.1.4 hashgen step 4.3 */
  886. if (len < buflen)
  887. drbg_add_buf(src, drbg_statelen(drbg), &prefix, 1);
  888. }
  889. out:
  890. memset(drbg->scratchpad, 0,
  891. (drbg_statelen(drbg) + drbg_blocklen(drbg)));
  892. return len;
  893. }
  894. /* generate function for Hash DRBG as defined in 10.1.1.4 */
  895. static int drbg_hash_generate(struct drbg_state *drbg,
  896. unsigned char *buf, unsigned int buflen,
  897. struct list_head *addtl)
  898. {
  899. int len = 0;
  900. int ret = 0;
  901. union {
  902. unsigned char req[8];
  903. __be64 req_int;
  904. } u;
  905. unsigned char prefix = DRBG_PREFIX3;
  906. struct drbg_string data1, data2;
  907. LIST_HEAD(datalist);
  908. /* 10.1.1.4 step 2 */
  909. ret = drbg_hash_process_addtl(drbg, addtl);
  910. if (ret)
  911. return ret;
  912. /* 10.1.1.4 step 3 */
  913. len = drbg_hash_hashgen(drbg, buf, buflen);
  914. /* this is the value H as documented in 10.1.1.4 */
  915. memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
  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: generated number of bytes
  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. now.cycles = get_cycles();
  1241. drbg_string_fill(&timestamp, now.char_cycles, sizeof(cycles_t));
  1242. list_add_tail(&timestamp.list, &addtllist);
  1243. }
  1244. if (addtl && 0 < addtl->len)
  1245. list_add_tail(&addtl->list, &addtllist);
  1246. /* 9.3.1 step 8 and 10 */
  1247. len = shadow->d_ops->generate(shadow, buf, buflen, &addtllist);
  1248. /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
  1249. shadow->reseed_ctr++;
  1250. if (0 >= len)
  1251. goto err;
  1252. /*
  1253. * Section 11.3.3 requires to re-perform self tests after some
  1254. * generated random numbers. The chosen value after which self
  1255. * test is performed is arbitrary, but it should be reasonable.
  1256. * However, we do not perform the self tests because of the following
  1257. * reasons: it is mathematically impossible that the initial self tests
  1258. * were successfully and the following are not. If the initial would
  1259. * pass and the following would not, the kernel integrity is violated.
  1260. * In this case, the entire kernel operation is questionable and it
  1261. * is unlikely that the integrity violation only affects the
  1262. * correct operation of the DRBG.
  1263. *
  1264. * Albeit the following code is commented out, it is provided in
  1265. * case somebody has a need to implement the test of 11.3.3.
  1266. */
  1267. #if 0
  1268. if (shadow->reseed_ctr && !(shadow->reseed_ctr % 4096)) {
  1269. int err = 0;
  1270. pr_devel("DRBG: start to perform self test\n");
  1271. if (drbg->core->flags & DRBG_HMAC)
  1272. err = alg_test("drbg_pr_hmac_sha256",
  1273. "drbg_pr_hmac_sha256", 0, 0);
  1274. else if (drbg->core->flags & DRBG_CTR)
  1275. err = alg_test("drbg_pr_ctr_aes128",
  1276. "drbg_pr_ctr_aes128", 0, 0);
  1277. else
  1278. err = alg_test("drbg_pr_sha256",
  1279. "drbg_pr_sha256", 0, 0);
  1280. if (err) {
  1281. pr_err("DRBG: periodical self test failed\n");
  1282. /*
  1283. * uninstantiate implies that from now on, only errors
  1284. * are returned when reusing this DRBG cipher handle
  1285. */
  1286. drbg_uninstantiate(drbg);
  1287. drbg_dealloc_state(shadow);
  1288. kzfree(shadow);
  1289. return 0;
  1290. } else {
  1291. pr_devel("DRBG: self test successful\n");
  1292. }
  1293. }
  1294. #endif
  1295. err:
  1296. shadow->d_ops->crypto_fini(shadow);
  1297. drbg_restore_shadow(drbg, &shadow);
  1298. return len;
  1299. }
  1300. /*
  1301. * Wrapper around drbg_generate which can pull arbitrary long strings
  1302. * from the DRBG without hitting the maximum request limitation.
  1303. *
  1304. * Parameters: see drbg_generate
  1305. * Return codes: see drbg_generate -- if one drbg_generate request fails,
  1306. * the entire drbg_generate_long request fails
  1307. */
  1308. static int drbg_generate_long(struct drbg_state *drbg,
  1309. unsigned char *buf, unsigned int buflen,
  1310. struct drbg_string *addtl)
  1311. {
  1312. int len = 0;
  1313. unsigned int slice = 0;
  1314. do {
  1315. int tmplen = 0;
  1316. unsigned int chunk = 0;
  1317. slice = ((buflen - len) / drbg_max_request_bytes(drbg));
  1318. chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
  1319. tmplen = drbg_generate(drbg, buf + len, chunk, addtl);
  1320. if (0 >= tmplen)
  1321. return tmplen;
  1322. len += tmplen;
  1323. } while (slice > 0 && (len < buflen));
  1324. return len;
  1325. }
  1326. /*
  1327. * DRBG instantiation function as required by SP800-90A - this function
  1328. * sets up the DRBG handle, performs the initial seeding and all sanity
  1329. * checks required by SP800-90A
  1330. *
  1331. * @drbg memory of state -- if NULL, new memory is allocated
  1332. * @pers Personalization string that is mixed into state, may be NULL -- note
  1333. * the entropy is pulled by the DRBG internally unconditionally
  1334. * as defined in SP800-90A. The additional input is mixed into
  1335. * the state in addition to the pulled entropy.
  1336. * @coreref reference to core
  1337. * @pr prediction resistance enabled
  1338. *
  1339. * return
  1340. * 0 on success
  1341. * error value otherwise
  1342. */
  1343. static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
  1344. int coreref, bool pr)
  1345. {
  1346. int ret = -ENOMEM;
  1347. pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
  1348. "%s\n", coreref, pr ? "enabled" : "disabled");
  1349. drbg->core = &drbg_cores[coreref];
  1350. drbg->pr = pr;
  1351. drbg->seeded = false;
  1352. switch (drbg->core->flags & DRBG_TYPE_MASK) {
  1353. #ifdef CONFIG_CRYPTO_DRBG_HMAC
  1354. case DRBG_HMAC:
  1355. drbg->d_ops = &drbg_hmac_ops;
  1356. break;
  1357. #endif /* CONFIG_CRYPTO_DRBG_HMAC */
  1358. #ifdef CONFIG_CRYPTO_DRBG_HASH
  1359. case DRBG_HASH:
  1360. drbg->d_ops = &drbg_hash_ops;
  1361. break;
  1362. #endif /* CONFIG_CRYPTO_DRBG_HASH */
  1363. #ifdef CONFIG_CRYPTO_DRBG_CTR
  1364. case DRBG_CTR:
  1365. drbg->d_ops = &drbg_ctr_ops;
  1366. break;
  1367. #endif /* CONFIG_CRYPTO_DRBG_CTR */
  1368. default:
  1369. return -EOPNOTSUPP;
  1370. }
  1371. /* 9.1 step 1 is implicit with the selected DRBG type */
  1372. /*
  1373. * 9.1 step 2 is implicit as caller can select prediction resistance
  1374. * and the flag is copied into drbg->flags --
  1375. * all DRBG types support prediction resistance
  1376. */
  1377. /* 9.1 step 4 is implicit in drbg_sec_strength */
  1378. ret = drbg_alloc_state(drbg);
  1379. if (ret)
  1380. return ret;
  1381. ret = -EFAULT;
  1382. if (drbg->d_ops->crypto_init(drbg))
  1383. goto err;
  1384. ret = drbg_seed(drbg, pers, false);
  1385. drbg->d_ops->crypto_fini(drbg);
  1386. if (ret)
  1387. goto err;
  1388. return 0;
  1389. err:
  1390. drbg_dealloc_state(drbg);
  1391. return ret;
  1392. }
  1393. /*
  1394. * DRBG uninstantiate function as required by SP800-90A - this function
  1395. * frees all buffers and the DRBG handle
  1396. *
  1397. * @drbg DRBG state handle
  1398. *
  1399. * return
  1400. * 0 on success
  1401. */
  1402. static int drbg_uninstantiate(struct drbg_state *drbg)
  1403. {
  1404. spin_lock_bh(&drbg->drbg_lock);
  1405. drbg_dealloc_state(drbg);
  1406. /* no scrubbing of test_data -- this shall survive an uninstantiate */
  1407. spin_unlock_bh(&drbg->drbg_lock);
  1408. return 0;
  1409. }
  1410. /*
  1411. * Helper function for setting the test data in the DRBG
  1412. *
  1413. * @drbg DRBG state handle
  1414. * @test_data test data to sets
  1415. */
  1416. static inline void drbg_set_testdata(struct drbg_state *drbg,
  1417. struct drbg_test_data *test_data)
  1418. {
  1419. if (!test_data || !test_data->testentropy)
  1420. return;
  1421. spin_lock_bh(&drbg->drbg_lock);
  1422. drbg->test_data = test_data;
  1423. spin_unlock_bh(&drbg->drbg_lock);
  1424. }
  1425. /***************************************************************
  1426. * Kernel crypto API cipher invocations requested by DRBG
  1427. ***************************************************************/
  1428. #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
  1429. struct sdesc {
  1430. struct shash_desc shash;
  1431. char ctx[];
  1432. };
  1433. static int drbg_init_hash_kernel(struct drbg_state *drbg)
  1434. {
  1435. struct sdesc *sdesc;
  1436. struct crypto_shash *tfm;
  1437. tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
  1438. if (IS_ERR(tfm)) {
  1439. pr_info("DRBG: could not allocate digest TFM handle\n");
  1440. return PTR_ERR(tfm);
  1441. }
  1442. BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
  1443. sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
  1444. GFP_KERNEL);
  1445. if (!sdesc) {
  1446. crypto_free_shash(tfm);
  1447. return -ENOMEM;
  1448. }
  1449. sdesc->shash.tfm = tfm;
  1450. sdesc->shash.flags = 0;
  1451. drbg->priv_data = sdesc;
  1452. return 0;
  1453. }
  1454. static int drbg_fini_hash_kernel(struct drbg_state *drbg)
  1455. {
  1456. struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
  1457. if (sdesc) {
  1458. crypto_free_shash(sdesc->shash.tfm);
  1459. kzfree(sdesc);
  1460. }
  1461. drbg->priv_data = NULL;
  1462. return 0;
  1463. }
  1464. static int drbg_kcapi_hash(struct drbg_state *drbg, const unsigned char *key,
  1465. unsigned char *outval, const struct list_head *in)
  1466. {
  1467. struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
  1468. struct drbg_string *input = NULL;
  1469. if (key)
  1470. crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
  1471. crypto_shash_init(&sdesc->shash);
  1472. list_for_each_entry(input, in, list)
  1473. crypto_shash_update(&sdesc->shash, input->buf, input->len);
  1474. return crypto_shash_final(&sdesc->shash, outval);
  1475. }
  1476. #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
  1477. #ifdef CONFIG_CRYPTO_DRBG_CTR
  1478. static int drbg_init_sym_kernel(struct drbg_state *drbg)
  1479. {
  1480. int ret = 0;
  1481. struct crypto_blkcipher *tfm;
  1482. tfm = crypto_alloc_blkcipher(drbg->core->backend_cra_name, 0, 0);
  1483. if (IS_ERR(tfm)) {
  1484. pr_info("DRBG: could not allocate cipher TFM handle\n");
  1485. return PTR_ERR(tfm);
  1486. }
  1487. BUG_ON(drbg_blocklen(drbg) != crypto_blkcipher_blocksize(tfm));
  1488. drbg->priv_data = tfm;
  1489. return ret;
  1490. }
  1491. static int drbg_fini_sym_kernel(struct drbg_state *drbg)
  1492. {
  1493. struct crypto_blkcipher *tfm =
  1494. (struct crypto_blkcipher *)drbg->priv_data;
  1495. if (tfm)
  1496. crypto_free_blkcipher(tfm);
  1497. drbg->priv_data = NULL;
  1498. return 0;
  1499. }
  1500. static int drbg_kcapi_sym(struct drbg_state *drbg, const unsigned char *key,
  1501. unsigned char *outval, const struct drbg_string *in)
  1502. {
  1503. int ret = 0;
  1504. struct scatterlist sg_in, sg_out;
  1505. struct blkcipher_desc desc;
  1506. struct crypto_blkcipher *tfm =
  1507. (struct crypto_blkcipher *)drbg->priv_data;
  1508. desc.tfm = tfm;
  1509. desc.flags = 0;
  1510. crypto_blkcipher_setkey(tfm, key, (drbg_keylen(drbg)));
  1511. /* there is only component in *in */
  1512. sg_init_one(&sg_in, in->buf, in->len);
  1513. sg_init_one(&sg_out, outval, drbg_blocklen(drbg));
  1514. ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, in->len);
  1515. return ret;
  1516. }
  1517. #endif /* CONFIG_CRYPTO_DRBG_CTR */
  1518. /***************************************************************
  1519. * Kernel crypto API interface to register DRBG
  1520. ***************************************************************/
  1521. /*
  1522. * Look up the DRBG flags by given kernel crypto API cra_name
  1523. * The code uses the drbg_cores definition to do this
  1524. *
  1525. * @cra_name kernel crypto API cra_name
  1526. * @coreref reference to integer which is filled with the pointer to
  1527. * the applicable core
  1528. * @pr reference for setting prediction resistance
  1529. *
  1530. * return: flags
  1531. */
  1532. static inline void drbg_convert_tfm_core(const char *cra_driver_name,
  1533. int *coreref, bool *pr)
  1534. {
  1535. int i = 0;
  1536. size_t start = 0;
  1537. int len = 0;
  1538. *pr = true;
  1539. /* disassemble the names */
  1540. if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
  1541. start = 10;
  1542. *pr = false;
  1543. } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
  1544. start = 8;
  1545. } else {
  1546. return;
  1547. }
  1548. /* remove the first part */
  1549. len = strlen(cra_driver_name) - start;
  1550. for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
  1551. if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
  1552. len)) {
  1553. *coreref = i;
  1554. return;
  1555. }
  1556. }
  1557. }
  1558. static int drbg_kcapi_init(struct crypto_tfm *tfm)
  1559. {
  1560. struct drbg_state *drbg = crypto_tfm_ctx(tfm);
  1561. bool pr = false;
  1562. int coreref = 0;
  1563. drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm), &coreref, &pr);
  1564. /*
  1565. * when personalization string is needed, the caller must call reset
  1566. * and provide the personalization string as seed information
  1567. */
  1568. return drbg_instantiate(drbg, NULL, coreref, pr);
  1569. }
  1570. static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
  1571. {
  1572. drbg_uninstantiate(crypto_tfm_ctx(tfm));
  1573. }
  1574. /*
  1575. * Generate random numbers invoked by the kernel crypto API:
  1576. * The API of the kernel crypto API is extended as follows:
  1577. *
  1578. * If dlen is larger than zero, rdata is interpreted as the output buffer
  1579. * where random data is to be stored.
  1580. *
  1581. * If dlen is zero, rdata is interpreted as a pointer to a struct drbg_gen
  1582. * which holds the additional information string that is used for the
  1583. * DRBG generation process. The output buffer that is to be used to store
  1584. * data is also pointed to by struct drbg_gen.
  1585. */
  1586. static int drbg_kcapi_random(struct crypto_rng *tfm, u8 *rdata,
  1587. unsigned int dlen)
  1588. {
  1589. struct drbg_state *drbg = crypto_rng_ctx(tfm);
  1590. if (0 < dlen) {
  1591. return drbg_generate_long(drbg, rdata, dlen, NULL);
  1592. } else {
  1593. struct drbg_gen *data = (struct drbg_gen *)rdata;
  1594. struct drbg_string addtl;
  1595. /* catch NULL pointer */
  1596. if (!data)
  1597. return 0;
  1598. drbg_set_testdata(drbg, data->test_data);
  1599. /* linked list variable is now local to allow modification */
  1600. drbg_string_fill(&addtl, data->addtl->buf, data->addtl->len);
  1601. return drbg_generate_long(drbg, data->outbuf, data->outlen,
  1602. &addtl);
  1603. }
  1604. }
  1605. /*
  1606. * Reset the DRBG invoked by the kernel crypto API
  1607. * The reset implies a full re-initialization of the DRBG. Similar to the
  1608. * generate function of drbg_kcapi_random, this function extends the
  1609. * kernel crypto API interface with struct drbg_gen
  1610. */
  1611. static int drbg_kcapi_reset(struct crypto_rng *tfm, u8 *seed, unsigned int slen)
  1612. {
  1613. struct drbg_state *drbg = crypto_rng_ctx(tfm);
  1614. struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
  1615. bool pr = false;
  1616. struct drbg_string seed_string;
  1617. int coreref = 0;
  1618. drbg_uninstantiate(drbg);
  1619. drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
  1620. &pr);
  1621. if (0 < slen) {
  1622. drbg_string_fill(&seed_string, seed, slen);
  1623. return drbg_instantiate(drbg, &seed_string, coreref, pr);
  1624. } else {
  1625. struct drbg_gen *data = (struct drbg_gen *)seed;
  1626. /* allow invocation of API call with NULL, 0 */
  1627. if (!data)
  1628. return drbg_instantiate(drbg, NULL, coreref, pr);
  1629. drbg_set_testdata(drbg, data->test_data);
  1630. /* linked list variable is now local to allow modification */
  1631. drbg_string_fill(&seed_string, data->addtl->buf,
  1632. data->addtl->len);
  1633. return drbg_instantiate(drbg, &seed_string, coreref, pr);
  1634. }
  1635. }
  1636. /***************************************************************
  1637. * Kernel module: code to load the module
  1638. ***************************************************************/
  1639. /*
  1640. * Tests as defined in 11.3.2 in addition to the cipher tests: testing
  1641. * of the error handling.
  1642. *
  1643. * Note: testing of failing seed source as defined in 11.3.2 is not applicable
  1644. * as seed source of get_random_bytes does not fail.
  1645. *
  1646. * Note 2: There is no sensible way of testing the reseed counter
  1647. * enforcement, so skip it.
  1648. */
  1649. static inline int __init drbg_healthcheck_sanity(void)
  1650. {
  1651. #ifdef CONFIG_CRYPTO_FIPS
  1652. int len = 0;
  1653. #define OUTBUFLEN 16
  1654. unsigned char buf[OUTBUFLEN];
  1655. struct drbg_state *drbg = NULL;
  1656. int ret = -EFAULT;
  1657. int rc = -EFAULT;
  1658. bool pr = false;
  1659. int coreref = 0;
  1660. struct drbg_string addtl;
  1661. size_t max_addtllen, max_request_bytes;
  1662. /* only perform test in FIPS mode */
  1663. if (!fips_enabled)
  1664. return 0;
  1665. #ifdef CONFIG_CRYPTO_DRBG_CTR
  1666. drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
  1667. #elif defined CONFIG_CRYPTO_DRBG_HASH
  1668. drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
  1669. #else
  1670. drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
  1671. #endif
  1672. drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
  1673. if (!drbg)
  1674. return -ENOMEM;
  1675. /*
  1676. * if the following tests fail, it is likely that there is a buffer
  1677. * overflow as buf is much smaller than the requested or provided
  1678. * string lengths -- in case the error handling does not succeed
  1679. * we may get an OOPS. And we want to get an OOPS as this is a
  1680. * grave bug.
  1681. */
  1682. /* get a valid instance of DRBG for following tests */
  1683. ret = drbg_instantiate(drbg, NULL, coreref, pr);
  1684. if (ret) {
  1685. rc = ret;
  1686. goto outbuf;
  1687. }
  1688. max_addtllen = drbg_max_addtl(drbg);
  1689. max_request_bytes = drbg_max_request_bytes(drbg);
  1690. drbg_string_fill(&addtl, buf, max_addtllen + 1);
  1691. /* overflow addtllen with additonal info string */
  1692. len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
  1693. BUG_ON(0 < len);
  1694. /* overflow max_bits */
  1695. len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
  1696. BUG_ON(0 < len);
  1697. drbg_uninstantiate(drbg);
  1698. /* overflow max addtllen with personalization string */
  1699. ret = drbg_instantiate(drbg, &addtl, coreref, pr);
  1700. BUG_ON(0 == ret);
  1701. /* all tests passed */
  1702. rc = 0;
  1703. pr_devel("DRBG: Sanity tests for failure code paths successfully "
  1704. "completed\n");
  1705. drbg_uninstantiate(drbg);
  1706. outbuf:
  1707. kzfree(drbg);
  1708. return rc;
  1709. #else /* CONFIG_CRYPTO_FIPS */
  1710. return 0;
  1711. #endif /* CONFIG_CRYPTO_FIPS */
  1712. }
  1713. static struct crypto_alg drbg_algs[22];
  1714. /*
  1715. * Fill the array drbg_algs used to register the different DRBGs
  1716. * with the kernel crypto API. To fill the array, the information
  1717. * from drbg_cores[] is used.
  1718. */
  1719. static inline void __init drbg_fill_array(struct crypto_alg *alg,
  1720. const struct drbg_core *core, int pr)
  1721. {
  1722. int pos = 0;
  1723. static int priority = 100;
  1724. memset(alg, 0, sizeof(struct crypto_alg));
  1725. memcpy(alg->cra_name, "stdrng", 6);
  1726. if (pr) {
  1727. memcpy(alg->cra_driver_name, "drbg_pr_", 8);
  1728. pos = 8;
  1729. } else {
  1730. memcpy(alg->cra_driver_name, "drbg_nopr_", 10);
  1731. pos = 10;
  1732. }
  1733. memcpy(alg->cra_driver_name + pos, core->cra_name,
  1734. strlen(core->cra_name));
  1735. alg->cra_priority = priority;
  1736. priority++;
  1737. /*
  1738. * If FIPS mode enabled, the selected DRBG shall have the
  1739. * highest cra_priority over other stdrng instances to ensure
  1740. * it is selected.
  1741. */
  1742. if (fips_enabled)
  1743. alg->cra_priority += 200;
  1744. alg->cra_flags = CRYPTO_ALG_TYPE_RNG;
  1745. alg->cra_ctxsize = sizeof(struct drbg_state);
  1746. alg->cra_type = &crypto_rng_type;
  1747. alg->cra_module = THIS_MODULE;
  1748. alg->cra_init = drbg_kcapi_init;
  1749. alg->cra_exit = drbg_kcapi_cleanup;
  1750. alg->cra_u.rng.rng_make_random = drbg_kcapi_random;
  1751. alg->cra_u.rng.rng_reset = drbg_kcapi_reset;
  1752. alg->cra_u.rng.seedsize = 0;
  1753. }
  1754. static int __init drbg_init(void)
  1755. {
  1756. unsigned int i = 0; /* pointer to drbg_algs */
  1757. unsigned int j = 0; /* pointer to drbg_cores */
  1758. int ret = -EFAULT;
  1759. ret = drbg_healthcheck_sanity();
  1760. if (ret)
  1761. return ret;
  1762. if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
  1763. pr_info("DRBG: Cannot register all DRBG types"
  1764. "(slots needed: %zu, slots available: %zu)\n",
  1765. ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
  1766. return ret;
  1767. }
  1768. /*
  1769. * each DRBG definition can be used with PR and without PR, thus
  1770. * we instantiate each DRBG in drbg_cores[] twice.
  1771. *
  1772. * As the order of placing them into the drbg_algs array matters
  1773. * (the later DRBGs receive a higher cra_priority) we register the
  1774. * prediction resistance DRBGs first as the should not be too
  1775. * interesting.
  1776. */
  1777. for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
  1778. drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
  1779. for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
  1780. drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
  1781. return crypto_register_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
  1782. }
  1783. static void __exit drbg_exit(void)
  1784. {
  1785. crypto_unregister_algs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
  1786. }
  1787. module_init(drbg_init);
  1788. module_exit(drbg_exit);
  1789. #ifndef CRYPTO_DRBG_HASH_STRING
  1790. #define CRYPTO_DRBG_HASH_STRING ""
  1791. #endif
  1792. #ifndef CRYPTO_DRBG_HMAC_STRING
  1793. #define CRYPTO_DRBG_HMAC_STRING ""
  1794. #endif
  1795. #ifndef CRYPTO_DRBG_CTR_STRING
  1796. #define CRYPTO_DRBG_CTR_STRING ""
  1797. #endif
  1798. MODULE_LICENSE("GPL");
  1799. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  1800. MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
  1801. "using following cores: "
  1802. CRYPTO_DRBG_HASH_STRING
  1803. CRYPTO_DRBG_HMAC_STRING
  1804. CRYPTO_DRBG_CTR_STRING);