drbg.c 58 KB

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