drbg.c 57 KB

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