drbg.c 57 KB

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