drbg.c 58 KB

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