drbg.c 60 KB

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