testmgr.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  1. /*
  2. * Algorithm testing framework and tests.
  3. *
  4. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  5. * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
  6. * Copyright (c) 2007 Nokia Siemens Networks
  7. * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <crypto/hash.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. #include <crypto/rng.h>
  22. #include "internal.h"
  23. #include "testmgr.h"
  24. /*
  25. * Need slab memory for testing (size in number of pages).
  26. */
  27. #define XBUFSIZE 8
  28. /*
  29. * Indexes into the xbuf to simulate cross-page access.
  30. */
  31. #define IDX1 32
  32. #define IDX2 32400
  33. #define IDX3 1
  34. #define IDX4 8193
  35. #define IDX5 22222
  36. #define IDX6 17101
  37. #define IDX7 27333
  38. #define IDX8 3000
  39. /*
  40. * Used by test_cipher()
  41. */
  42. #define ENCRYPT 1
  43. #define DECRYPT 0
  44. struct tcrypt_result {
  45. struct completion completion;
  46. int err;
  47. };
  48. struct aead_test_suite {
  49. struct {
  50. struct aead_testvec *vecs;
  51. unsigned int count;
  52. } enc, dec;
  53. };
  54. struct cipher_test_suite {
  55. struct {
  56. struct cipher_testvec *vecs;
  57. unsigned int count;
  58. } enc, dec;
  59. };
  60. struct comp_test_suite {
  61. struct {
  62. struct comp_testvec *vecs;
  63. unsigned int count;
  64. } comp, decomp;
  65. };
  66. struct pcomp_test_suite {
  67. struct {
  68. struct pcomp_testvec *vecs;
  69. unsigned int count;
  70. } comp, decomp;
  71. };
  72. struct hash_test_suite {
  73. struct hash_testvec *vecs;
  74. unsigned int count;
  75. };
  76. struct cprng_test_suite {
  77. struct cprng_testvec *vecs;
  78. unsigned int count;
  79. };
  80. struct alg_test_desc {
  81. const char *alg;
  82. int (*test)(const struct alg_test_desc *desc, const char *driver,
  83. u32 type, u32 mask);
  84. union {
  85. struct aead_test_suite aead;
  86. struct cipher_test_suite cipher;
  87. struct comp_test_suite comp;
  88. struct pcomp_test_suite pcomp;
  89. struct hash_test_suite hash;
  90. struct cprng_test_suite cprng;
  91. } suite;
  92. };
  93. static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
  94. static char *xbuf[XBUFSIZE];
  95. static char *axbuf[XBUFSIZE];
  96. static void hexdump(unsigned char *buf, unsigned int len)
  97. {
  98. print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
  99. 16, 1,
  100. buf, len, false);
  101. }
  102. static void tcrypt_complete(struct crypto_async_request *req, int err)
  103. {
  104. struct tcrypt_result *res = req->data;
  105. if (err == -EINPROGRESS)
  106. return;
  107. res->err = err;
  108. complete(&res->completion);
  109. }
  110. static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
  111. unsigned int tcount)
  112. {
  113. const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
  114. unsigned int i, j, k, temp;
  115. struct scatterlist sg[8];
  116. char result[64];
  117. struct ahash_request *req;
  118. struct tcrypt_result tresult;
  119. int ret;
  120. void *hash_buff;
  121. init_completion(&tresult.completion);
  122. req = ahash_request_alloc(tfm, GFP_KERNEL);
  123. if (!req) {
  124. printk(KERN_ERR "alg: hash: Failed to allocate request for "
  125. "%s\n", algo);
  126. ret = -ENOMEM;
  127. goto out_noreq;
  128. }
  129. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  130. tcrypt_complete, &tresult);
  131. for (i = 0; i < tcount; i++) {
  132. memset(result, 0, 64);
  133. hash_buff = xbuf[0];
  134. memcpy(hash_buff, template[i].plaintext, template[i].psize);
  135. sg_init_one(&sg[0], hash_buff, template[i].psize);
  136. if (template[i].ksize) {
  137. crypto_ahash_clear_flags(tfm, ~0);
  138. ret = crypto_ahash_setkey(tfm, template[i].key,
  139. template[i].ksize);
  140. if (ret) {
  141. printk(KERN_ERR "alg: hash: setkey failed on "
  142. "test %d for %s: ret=%d\n", i + 1, algo,
  143. -ret);
  144. goto out;
  145. }
  146. }
  147. ahash_request_set_crypt(req, sg, result, template[i].psize);
  148. ret = crypto_ahash_digest(req);
  149. switch (ret) {
  150. case 0:
  151. break;
  152. case -EINPROGRESS:
  153. case -EBUSY:
  154. ret = wait_for_completion_interruptible(
  155. &tresult.completion);
  156. if (!ret && !(ret = tresult.err)) {
  157. INIT_COMPLETION(tresult.completion);
  158. break;
  159. }
  160. /* fall through */
  161. default:
  162. printk(KERN_ERR "alg: hash: digest failed on test %d "
  163. "for %s: ret=%d\n", i + 1, algo, -ret);
  164. goto out;
  165. }
  166. if (memcmp(result, template[i].digest,
  167. crypto_ahash_digestsize(tfm))) {
  168. printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
  169. i + 1, algo);
  170. hexdump(result, crypto_ahash_digestsize(tfm));
  171. ret = -EINVAL;
  172. goto out;
  173. }
  174. }
  175. j = 0;
  176. for (i = 0; i < tcount; i++) {
  177. if (template[i].np) {
  178. j++;
  179. memset(result, 0, 64);
  180. temp = 0;
  181. sg_init_table(sg, template[i].np);
  182. for (k = 0; k < template[i].np; k++) {
  183. sg_set_buf(&sg[k],
  184. memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
  185. offset_in_page(IDX[k]),
  186. template[i].plaintext + temp,
  187. template[i].tap[k]),
  188. template[i].tap[k]);
  189. temp += template[i].tap[k];
  190. }
  191. if (template[i].ksize) {
  192. crypto_ahash_clear_flags(tfm, ~0);
  193. ret = crypto_ahash_setkey(tfm, template[i].key,
  194. template[i].ksize);
  195. if (ret) {
  196. printk(KERN_ERR "alg: hash: setkey "
  197. "failed on chunking test %d "
  198. "for %s: ret=%d\n", j, algo,
  199. -ret);
  200. goto out;
  201. }
  202. }
  203. ahash_request_set_crypt(req, sg, result,
  204. template[i].psize);
  205. ret = crypto_ahash_digest(req);
  206. switch (ret) {
  207. case 0:
  208. break;
  209. case -EINPROGRESS:
  210. case -EBUSY:
  211. ret = wait_for_completion_interruptible(
  212. &tresult.completion);
  213. if (!ret && !(ret = tresult.err)) {
  214. INIT_COMPLETION(tresult.completion);
  215. break;
  216. }
  217. /* fall through */
  218. default:
  219. printk(KERN_ERR "alg: hash: digest failed "
  220. "on chunking test %d for %s: "
  221. "ret=%d\n", j, algo, -ret);
  222. goto out;
  223. }
  224. if (memcmp(result, template[i].digest,
  225. crypto_ahash_digestsize(tfm))) {
  226. printk(KERN_ERR "alg: hash: Chunking test %d "
  227. "failed for %s\n", j, algo);
  228. hexdump(result, crypto_ahash_digestsize(tfm));
  229. ret = -EINVAL;
  230. goto out;
  231. }
  232. }
  233. }
  234. ret = 0;
  235. out:
  236. ahash_request_free(req);
  237. out_noreq:
  238. return ret;
  239. }
  240. static int test_aead(struct crypto_aead *tfm, int enc,
  241. struct aead_testvec *template, unsigned int tcount)
  242. {
  243. const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
  244. unsigned int i, j, k, n, temp;
  245. int ret = 0;
  246. char *q;
  247. char *key;
  248. struct aead_request *req;
  249. struct scatterlist sg[8];
  250. struct scatterlist asg[8];
  251. const char *e;
  252. struct tcrypt_result result;
  253. unsigned int authsize;
  254. void *input;
  255. void *assoc;
  256. char iv[MAX_IVLEN];
  257. if (enc == ENCRYPT)
  258. e = "encryption";
  259. else
  260. e = "decryption";
  261. init_completion(&result.completion);
  262. req = aead_request_alloc(tfm, GFP_KERNEL);
  263. if (!req) {
  264. printk(KERN_ERR "alg: aead: Failed to allocate request for "
  265. "%s\n", algo);
  266. ret = -ENOMEM;
  267. goto out;
  268. }
  269. aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  270. tcrypt_complete, &result);
  271. for (i = 0, j = 0; i < tcount; i++) {
  272. if (!template[i].np) {
  273. j++;
  274. /* some tepmplates have no input data but they will
  275. * touch input
  276. */
  277. input = xbuf[0];
  278. assoc = axbuf[0];
  279. memcpy(input, template[i].input, template[i].ilen);
  280. memcpy(assoc, template[i].assoc, template[i].alen);
  281. if (template[i].iv)
  282. memcpy(iv, template[i].iv, MAX_IVLEN);
  283. else
  284. memset(iv, 0, MAX_IVLEN);
  285. crypto_aead_clear_flags(tfm, ~0);
  286. if (template[i].wk)
  287. crypto_aead_set_flags(
  288. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  289. key = template[i].key;
  290. ret = crypto_aead_setkey(tfm, key,
  291. template[i].klen);
  292. if (!ret == template[i].fail) {
  293. printk(KERN_ERR "alg: aead: setkey failed on "
  294. "test %d for %s: flags=%x\n", j, algo,
  295. crypto_aead_get_flags(tfm));
  296. goto out;
  297. } else if (ret)
  298. continue;
  299. authsize = abs(template[i].rlen - template[i].ilen);
  300. ret = crypto_aead_setauthsize(tfm, authsize);
  301. if (ret) {
  302. printk(KERN_ERR "alg: aead: Failed to set "
  303. "authsize to %u on test %d for %s\n",
  304. authsize, j, algo);
  305. goto out;
  306. }
  307. sg_init_one(&sg[0], input,
  308. template[i].ilen + (enc ? authsize : 0));
  309. sg_init_one(&asg[0], assoc, template[i].alen);
  310. aead_request_set_crypt(req, sg, sg,
  311. template[i].ilen, iv);
  312. aead_request_set_assoc(req, asg, template[i].alen);
  313. ret = enc ?
  314. crypto_aead_encrypt(req) :
  315. crypto_aead_decrypt(req);
  316. switch (ret) {
  317. case 0:
  318. if (template[i].novrfy) {
  319. /* verification was supposed to fail */
  320. printk(KERN_ERR "alg: aead: %s failed "
  321. "on test %d for %s: ret was 0, "
  322. "expected -EBADMSG\n",
  323. e, j, algo);
  324. /* so really, we got a bad message */
  325. ret = -EBADMSG;
  326. goto out;
  327. }
  328. break;
  329. case -EINPROGRESS:
  330. case -EBUSY:
  331. ret = wait_for_completion_interruptible(
  332. &result.completion);
  333. if (!ret && !(ret = result.err)) {
  334. INIT_COMPLETION(result.completion);
  335. break;
  336. }
  337. case -EBADMSG:
  338. if (template[i].novrfy)
  339. /* verification failure was expected */
  340. continue;
  341. /* fall through */
  342. default:
  343. printk(KERN_ERR "alg: aead: %s failed on test "
  344. "%d for %s: ret=%d\n", e, j, algo, -ret);
  345. goto out;
  346. }
  347. q = input;
  348. if (memcmp(q, template[i].result, template[i].rlen)) {
  349. printk(KERN_ERR "alg: aead: Test %d failed on "
  350. "%s for %s\n", j, e, algo);
  351. hexdump(q, template[i].rlen);
  352. ret = -EINVAL;
  353. goto out;
  354. }
  355. }
  356. }
  357. for (i = 0, j = 0; i < tcount; i++) {
  358. if (template[i].np) {
  359. j++;
  360. if (template[i].iv)
  361. memcpy(iv, template[i].iv, MAX_IVLEN);
  362. else
  363. memset(iv, 0, MAX_IVLEN);
  364. crypto_aead_clear_flags(tfm, ~0);
  365. if (template[i].wk)
  366. crypto_aead_set_flags(
  367. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  368. key = template[i].key;
  369. ret = crypto_aead_setkey(tfm, key, template[i].klen);
  370. if (!ret == template[i].fail) {
  371. printk(KERN_ERR "alg: aead: setkey failed on "
  372. "chunk test %d for %s: flags=%x\n", j,
  373. algo, crypto_aead_get_flags(tfm));
  374. goto out;
  375. } else if (ret)
  376. continue;
  377. authsize = abs(template[i].rlen - template[i].ilen);
  378. ret = -EINVAL;
  379. sg_init_table(sg, template[i].np);
  380. for (k = 0, temp = 0; k < template[i].np; k++) {
  381. if (WARN_ON(offset_in_page(IDX[k]) +
  382. template[i].tap[k] > PAGE_SIZE))
  383. goto out;
  384. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  385. offset_in_page(IDX[k]);
  386. memcpy(q, template[i].input + temp,
  387. template[i].tap[k]);
  388. n = template[i].tap[k];
  389. if (k == template[i].np - 1 && enc)
  390. n += authsize;
  391. if (offset_in_page(q) + n < PAGE_SIZE)
  392. q[n] = 0;
  393. sg_set_buf(&sg[k], q, template[i].tap[k]);
  394. temp += template[i].tap[k];
  395. }
  396. ret = crypto_aead_setauthsize(tfm, authsize);
  397. if (ret) {
  398. printk(KERN_ERR "alg: aead: Failed to set "
  399. "authsize to %u on chunk test %d for "
  400. "%s\n", authsize, j, algo);
  401. goto out;
  402. }
  403. if (enc) {
  404. if (WARN_ON(sg[k - 1].offset +
  405. sg[k - 1].length + authsize >
  406. PAGE_SIZE)) {
  407. ret = -EINVAL;
  408. goto out;
  409. }
  410. sg[k - 1].length += authsize;
  411. }
  412. sg_init_table(asg, template[i].anp);
  413. for (k = 0, temp = 0; k < template[i].anp; k++) {
  414. sg_set_buf(&asg[k],
  415. memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
  416. offset_in_page(IDX[k]),
  417. template[i].assoc + temp,
  418. template[i].atap[k]),
  419. template[i].atap[k]);
  420. temp += template[i].atap[k];
  421. }
  422. aead_request_set_crypt(req, sg, sg,
  423. template[i].ilen,
  424. iv);
  425. aead_request_set_assoc(req, asg, template[i].alen);
  426. ret = enc ?
  427. crypto_aead_encrypt(req) :
  428. crypto_aead_decrypt(req);
  429. switch (ret) {
  430. case 0:
  431. if (template[i].novrfy) {
  432. /* verification was supposed to fail */
  433. printk(KERN_ERR "alg: aead: %s failed "
  434. "on chunk test %d for %s: ret "
  435. "was 0, expected -EBADMSG\n",
  436. e, j, algo);
  437. /* so really, we got a bad message */
  438. ret = -EBADMSG;
  439. goto out;
  440. }
  441. break;
  442. case -EINPROGRESS:
  443. case -EBUSY:
  444. ret = wait_for_completion_interruptible(
  445. &result.completion);
  446. if (!ret && !(ret = result.err)) {
  447. INIT_COMPLETION(result.completion);
  448. break;
  449. }
  450. case -EBADMSG:
  451. if (template[i].novrfy)
  452. /* verification failure was expected */
  453. continue;
  454. /* fall through */
  455. default:
  456. printk(KERN_ERR "alg: aead: %s failed on "
  457. "chunk test %d for %s: ret=%d\n", e, j,
  458. algo, -ret);
  459. goto out;
  460. }
  461. ret = -EINVAL;
  462. for (k = 0, temp = 0; k < template[i].np; k++) {
  463. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  464. offset_in_page(IDX[k]);
  465. n = template[i].tap[k];
  466. if (k == template[i].np - 1)
  467. n += enc ? authsize : -authsize;
  468. if (memcmp(q, template[i].result + temp, n)) {
  469. printk(KERN_ERR "alg: aead: Chunk "
  470. "test %d failed on %s at page "
  471. "%u for %s\n", j, e, k, algo);
  472. hexdump(q, n);
  473. goto out;
  474. }
  475. q += n;
  476. if (k == template[i].np - 1 && !enc) {
  477. if (memcmp(q, template[i].input +
  478. temp + n, authsize))
  479. n = authsize;
  480. else
  481. n = 0;
  482. } else {
  483. for (n = 0; offset_in_page(q + n) &&
  484. q[n]; n++)
  485. ;
  486. }
  487. if (n) {
  488. printk(KERN_ERR "alg: aead: Result "
  489. "buffer corruption in chunk "
  490. "test %d on %s at page %u for "
  491. "%s: %u bytes:\n", j, e, k,
  492. algo, n);
  493. hexdump(q, n);
  494. goto out;
  495. }
  496. temp += template[i].tap[k];
  497. }
  498. }
  499. }
  500. ret = 0;
  501. out:
  502. aead_request_free(req);
  503. return ret;
  504. }
  505. static int test_cipher(struct crypto_cipher *tfm, int enc,
  506. struct cipher_testvec *template, unsigned int tcount)
  507. {
  508. const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
  509. unsigned int i, j, k;
  510. int ret;
  511. char *q;
  512. const char *e;
  513. void *data;
  514. if (enc == ENCRYPT)
  515. e = "encryption";
  516. else
  517. e = "decryption";
  518. j = 0;
  519. for (i = 0; i < tcount; i++) {
  520. if (template[i].np)
  521. continue;
  522. j++;
  523. data = xbuf[0];
  524. memcpy(data, template[i].input, template[i].ilen);
  525. crypto_cipher_clear_flags(tfm, ~0);
  526. if (template[i].wk)
  527. crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  528. ret = crypto_cipher_setkey(tfm, template[i].key,
  529. template[i].klen);
  530. if (!ret == template[i].fail) {
  531. printk(KERN_ERR "alg: cipher: setkey failed "
  532. "on test %d for %s: flags=%x\n", j,
  533. algo, crypto_cipher_get_flags(tfm));
  534. goto out;
  535. } else if (ret)
  536. continue;
  537. for (k = 0; k < template[i].ilen;
  538. k += crypto_cipher_blocksize(tfm)) {
  539. if (enc)
  540. crypto_cipher_encrypt_one(tfm, data + k,
  541. data + k);
  542. else
  543. crypto_cipher_decrypt_one(tfm, data + k,
  544. data + k);
  545. }
  546. q = data;
  547. if (memcmp(q, template[i].result, template[i].rlen)) {
  548. printk(KERN_ERR "alg: cipher: Test %d failed "
  549. "on %s for %s\n", j, e, algo);
  550. hexdump(q, template[i].rlen);
  551. ret = -EINVAL;
  552. goto out;
  553. }
  554. }
  555. ret = 0;
  556. out:
  557. return ret;
  558. }
  559. static int test_skcipher(struct crypto_ablkcipher *tfm, int enc,
  560. struct cipher_testvec *template, unsigned int tcount)
  561. {
  562. const char *algo =
  563. crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
  564. unsigned int i, j, k, n, temp;
  565. int ret;
  566. char *q;
  567. struct ablkcipher_request *req;
  568. struct scatterlist sg[8];
  569. const char *e;
  570. struct tcrypt_result result;
  571. void *data;
  572. char iv[MAX_IVLEN];
  573. if (enc == ENCRYPT)
  574. e = "encryption";
  575. else
  576. e = "decryption";
  577. init_completion(&result.completion);
  578. req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
  579. if (!req) {
  580. printk(KERN_ERR "alg: skcipher: Failed to allocate request "
  581. "for %s\n", algo);
  582. ret = -ENOMEM;
  583. goto out;
  584. }
  585. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  586. tcrypt_complete, &result);
  587. j = 0;
  588. for (i = 0; i < tcount; i++) {
  589. if (template[i].iv)
  590. memcpy(iv, template[i].iv, MAX_IVLEN);
  591. else
  592. memset(iv, 0, MAX_IVLEN);
  593. if (!(template[i].np)) {
  594. j++;
  595. data = xbuf[0];
  596. memcpy(data, template[i].input, template[i].ilen);
  597. crypto_ablkcipher_clear_flags(tfm, ~0);
  598. if (template[i].wk)
  599. crypto_ablkcipher_set_flags(
  600. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  601. ret = crypto_ablkcipher_setkey(tfm, template[i].key,
  602. template[i].klen);
  603. if (!ret == template[i].fail) {
  604. printk(KERN_ERR "alg: skcipher: setkey failed "
  605. "on test %d for %s: flags=%x\n", j,
  606. algo, crypto_ablkcipher_get_flags(tfm));
  607. goto out;
  608. } else if (ret)
  609. continue;
  610. sg_init_one(&sg[0], data, template[i].ilen);
  611. ablkcipher_request_set_crypt(req, sg, sg,
  612. template[i].ilen, iv);
  613. ret = enc ?
  614. crypto_ablkcipher_encrypt(req) :
  615. crypto_ablkcipher_decrypt(req);
  616. switch (ret) {
  617. case 0:
  618. break;
  619. case -EINPROGRESS:
  620. case -EBUSY:
  621. ret = wait_for_completion_interruptible(
  622. &result.completion);
  623. if (!ret && !((ret = result.err))) {
  624. INIT_COMPLETION(result.completion);
  625. break;
  626. }
  627. /* fall through */
  628. default:
  629. printk(KERN_ERR "alg: skcipher: %s failed on "
  630. "test %d for %s: ret=%d\n", e, j, algo,
  631. -ret);
  632. goto out;
  633. }
  634. q = data;
  635. if (memcmp(q, template[i].result, template[i].rlen)) {
  636. printk(KERN_ERR "alg: skcipher: Test %d "
  637. "failed on %s for %s\n", j, e, algo);
  638. hexdump(q, template[i].rlen);
  639. ret = -EINVAL;
  640. goto out;
  641. }
  642. }
  643. }
  644. j = 0;
  645. for (i = 0; i < tcount; i++) {
  646. if (template[i].iv)
  647. memcpy(iv, template[i].iv, MAX_IVLEN);
  648. else
  649. memset(iv, 0, MAX_IVLEN);
  650. if (template[i].np) {
  651. j++;
  652. crypto_ablkcipher_clear_flags(tfm, ~0);
  653. if (template[i].wk)
  654. crypto_ablkcipher_set_flags(
  655. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  656. ret = crypto_ablkcipher_setkey(tfm, template[i].key,
  657. template[i].klen);
  658. if (!ret == template[i].fail) {
  659. printk(KERN_ERR "alg: skcipher: setkey failed "
  660. "on chunk test %d for %s: flags=%x\n",
  661. j, algo,
  662. crypto_ablkcipher_get_flags(tfm));
  663. goto out;
  664. } else if (ret)
  665. continue;
  666. temp = 0;
  667. ret = -EINVAL;
  668. sg_init_table(sg, template[i].np);
  669. for (k = 0; k < template[i].np; k++) {
  670. if (WARN_ON(offset_in_page(IDX[k]) +
  671. template[i].tap[k] > PAGE_SIZE))
  672. goto out;
  673. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  674. offset_in_page(IDX[k]);
  675. memcpy(q, template[i].input + temp,
  676. template[i].tap[k]);
  677. if (offset_in_page(q) + template[i].tap[k] <
  678. PAGE_SIZE)
  679. q[template[i].tap[k]] = 0;
  680. sg_set_buf(&sg[k], q, template[i].tap[k]);
  681. temp += template[i].tap[k];
  682. }
  683. ablkcipher_request_set_crypt(req, sg, sg,
  684. template[i].ilen, iv);
  685. ret = enc ?
  686. crypto_ablkcipher_encrypt(req) :
  687. crypto_ablkcipher_decrypt(req);
  688. switch (ret) {
  689. case 0:
  690. break;
  691. case -EINPROGRESS:
  692. case -EBUSY:
  693. ret = wait_for_completion_interruptible(
  694. &result.completion);
  695. if (!ret && !((ret = result.err))) {
  696. INIT_COMPLETION(result.completion);
  697. break;
  698. }
  699. /* fall through */
  700. default:
  701. printk(KERN_ERR "alg: skcipher: %s failed on "
  702. "chunk test %d for %s: ret=%d\n", e, j,
  703. algo, -ret);
  704. goto out;
  705. }
  706. temp = 0;
  707. ret = -EINVAL;
  708. for (k = 0; k < template[i].np; k++) {
  709. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  710. offset_in_page(IDX[k]);
  711. if (memcmp(q, template[i].result + temp,
  712. template[i].tap[k])) {
  713. printk(KERN_ERR "alg: skcipher: Chunk "
  714. "test %d failed on %s at page "
  715. "%u for %s\n", j, e, k, algo);
  716. hexdump(q, template[i].tap[k]);
  717. goto out;
  718. }
  719. q += template[i].tap[k];
  720. for (n = 0; offset_in_page(q + n) && q[n]; n++)
  721. ;
  722. if (n) {
  723. printk(KERN_ERR "alg: skcipher: "
  724. "Result buffer corruption in "
  725. "chunk test %d on %s at page "
  726. "%u for %s: %u bytes:\n", j, e,
  727. k, algo, n);
  728. hexdump(q, n);
  729. goto out;
  730. }
  731. temp += template[i].tap[k];
  732. }
  733. }
  734. }
  735. ret = 0;
  736. out:
  737. ablkcipher_request_free(req);
  738. return ret;
  739. }
  740. static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
  741. struct comp_testvec *dtemplate, int ctcount, int dtcount)
  742. {
  743. const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
  744. unsigned int i;
  745. char result[COMP_BUF_SIZE];
  746. int ret;
  747. for (i = 0; i < ctcount; i++) {
  748. int ilen;
  749. unsigned int dlen = COMP_BUF_SIZE;
  750. memset(result, 0, sizeof (result));
  751. ilen = ctemplate[i].inlen;
  752. ret = crypto_comp_compress(tfm, ctemplate[i].input,
  753. ilen, result, &dlen);
  754. if (ret) {
  755. printk(KERN_ERR "alg: comp: compression failed "
  756. "on test %d for %s: ret=%d\n", i + 1, algo,
  757. -ret);
  758. goto out;
  759. }
  760. if (dlen != ctemplate[i].outlen) {
  761. printk(KERN_ERR "alg: comp: Compression test %d "
  762. "failed for %s: output len = %d\n", i + 1, algo,
  763. dlen);
  764. ret = -EINVAL;
  765. goto out;
  766. }
  767. if (memcmp(result, ctemplate[i].output, dlen)) {
  768. printk(KERN_ERR "alg: comp: Compression test %d "
  769. "failed for %s\n", i + 1, algo);
  770. hexdump(result, dlen);
  771. ret = -EINVAL;
  772. goto out;
  773. }
  774. }
  775. for (i = 0; i < dtcount; i++) {
  776. int ilen;
  777. unsigned int dlen = COMP_BUF_SIZE;
  778. memset(result, 0, sizeof (result));
  779. ilen = dtemplate[i].inlen;
  780. ret = crypto_comp_decompress(tfm, dtemplate[i].input,
  781. ilen, result, &dlen);
  782. if (ret) {
  783. printk(KERN_ERR "alg: comp: decompression failed "
  784. "on test %d for %s: ret=%d\n", i + 1, algo,
  785. -ret);
  786. goto out;
  787. }
  788. if (dlen != dtemplate[i].outlen) {
  789. printk(KERN_ERR "alg: comp: Decompression test %d "
  790. "failed for %s: output len = %d\n", i + 1, algo,
  791. dlen);
  792. ret = -EINVAL;
  793. goto out;
  794. }
  795. if (memcmp(result, dtemplate[i].output, dlen)) {
  796. printk(KERN_ERR "alg: comp: Decompression test %d "
  797. "failed for %s\n", i + 1, algo);
  798. hexdump(result, dlen);
  799. ret = -EINVAL;
  800. goto out;
  801. }
  802. }
  803. ret = 0;
  804. out:
  805. return ret;
  806. }
  807. static int test_pcomp(struct crypto_pcomp *tfm,
  808. struct pcomp_testvec *ctemplate,
  809. struct pcomp_testvec *dtemplate, int ctcount,
  810. int dtcount)
  811. {
  812. const char *algo = crypto_tfm_alg_driver_name(crypto_pcomp_tfm(tfm));
  813. unsigned int i;
  814. char result[COMP_BUF_SIZE];
  815. int error;
  816. for (i = 0; i < ctcount; i++) {
  817. struct comp_request req;
  818. error = crypto_compress_setup(tfm, ctemplate[i].params,
  819. ctemplate[i].paramsize);
  820. if (error) {
  821. pr_err("alg: pcomp: compression setup failed on test "
  822. "%d for %s: error=%d\n", i + 1, algo, error);
  823. return error;
  824. }
  825. error = crypto_compress_init(tfm);
  826. if (error) {
  827. pr_err("alg: pcomp: compression init failed on test "
  828. "%d for %s: error=%d\n", i + 1, algo, error);
  829. return error;
  830. }
  831. memset(result, 0, sizeof(result));
  832. req.next_in = ctemplate[i].input;
  833. req.avail_in = ctemplate[i].inlen / 2;
  834. req.next_out = result;
  835. req.avail_out = ctemplate[i].outlen / 2;
  836. error = crypto_compress_update(tfm, &req);
  837. if (error && (error != -EAGAIN || req.avail_in)) {
  838. pr_err("alg: pcomp: compression update failed on test "
  839. "%d for %s: error=%d\n", i + 1, algo, error);
  840. return error;
  841. }
  842. /* Add remaining input data */
  843. req.avail_in += (ctemplate[i].inlen + 1) / 2;
  844. error = crypto_compress_update(tfm, &req);
  845. if (error && (error != -EAGAIN || req.avail_in)) {
  846. pr_err("alg: pcomp: compression update failed on test "
  847. "%d for %s: error=%d\n", i + 1, algo, error);
  848. return error;
  849. }
  850. /* Provide remaining output space */
  851. req.avail_out += COMP_BUF_SIZE - ctemplate[i].outlen / 2;
  852. error = crypto_compress_final(tfm, &req);
  853. if (error) {
  854. pr_err("alg: pcomp: compression final failed on test "
  855. "%d for %s: error=%d\n", i + 1, algo, error);
  856. return error;
  857. }
  858. if (COMP_BUF_SIZE - req.avail_out != ctemplate[i].outlen) {
  859. pr_err("alg: comp: Compression test %d failed for %s: "
  860. "output len = %d (expected %d)\n", i + 1, algo,
  861. COMP_BUF_SIZE - req.avail_out,
  862. ctemplate[i].outlen);
  863. return -EINVAL;
  864. }
  865. if (memcmp(result, ctemplate[i].output, ctemplate[i].outlen)) {
  866. pr_err("alg: pcomp: Compression test %d failed for "
  867. "%s\n", i + 1, algo);
  868. hexdump(result, ctemplate[i].outlen);
  869. return -EINVAL;
  870. }
  871. }
  872. for (i = 0; i < dtcount; i++) {
  873. struct comp_request req;
  874. error = crypto_decompress_setup(tfm, dtemplate[i].params,
  875. dtemplate[i].paramsize);
  876. if (error) {
  877. pr_err("alg: pcomp: decompression setup failed on "
  878. "test %d for %s: error=%d\n", i + 1, algo,
  879. error);
  880. return error;
  881. }
  882. error = crypto_decompress_init(tfm);
  883. if (error) {
  884. pr_err("alg: pcomp: decompression init failed on test "
  885. "%d for %s: error=%d\n", i + 1, algo, error);
  886. return error;
  887. }
  888. memset(result, 0, sizeof(result));
  889. req.next_in = dtemplate[i].input;
  890. req.avail_in = dtemplate[i].inlen / 2;
  891. req.next_out = result;
  892. req.avail_out = dtemplate[i].outlen / 2;
  893. error = crypto_decompress_update(tfm, &req);
  894. if (error && (error != -EAGAIN || req.avail_in)) {
  895. pr_err("alg: pcomp: decompression update failed on "
  896. "test %d for %s: error=%d\n", i + 1, algo,
  897. error);
  898. return error;
  899. }
  900. /* Add remaining input data */
  901. req.avail_in += (dtemplate[i].inlen + 1) / 2;
  902. error = crypto_decompress_update(tfm, &req);
  903. if (error && (error != -EAGAIN || req.avail_in)) {
  904. pr_err("alg: pcomp: decompression update failed on "
  905. "test %d for %s: error=%d\n", i + 1, algo,
  906. error);
  907. return error;
  908. }
  909. /* Provide remaining output space */
  910. req.avail_out += COMP_BUF_SIZE - dtemplate[i].outlen / 2;
  911. error = crypto_decompress_final(tfm, &req);
  912. if (error && (error != -EAGAIN || req.avail_in)) {
  913. pr_err("alg: pcomp: decompression final failed on "
  914. "test %d for %s: error=%d\n", i + 1, algo,
  915. error);
  916. return error;
  917. }
  918. if (COMP_BUF_SIZE - req.avail_out != dtemplate[i].outlen) {
  919. pr_err("alg: comp: Decompression test %d failed for "
  920. "%s: output len = %d (expected %d)\n", i + 1,
  921. algo, COMP_BUF_SIZE - req.avail_out,
  922. dtemplate[i].outlen);
  923. return -EINVAL;
  924. }
  925. if (memcmp(result, dtemplate[i].output, dtemplate[i].outlen)) {
  926. pr_err("alg: pcomp: Decompression test %d failed for "
  927. "%s\n", i + 1, algo);
  928. hexdump(result, dtemplate[i].outlen);
  929. return -EINVAL;
  930. }
  931. }
  932. return 0;
  933. }
  934. static int test_cprng(struct crypto_rng *tfm, struct cprng_testvec *template,
  935. unsigned int tcount)
  936. {
  937. const char *algo = crypto_tfm_alg_driver_name(crypto_rng_tfm(tfm));
  938. int err, i, j, seedsize;
  939. u8 *seed;
  940. char result[32];
  941. seedsize = crypto_rng_seedsize(tfm);
  942. seed = kmalloc(seedsize, GFP_KERNEL);
  943. if (!seed) {
  944. printk(KERN_ERR "alg: cprng: Failed to allocate seed space "
  945. "for %s\n", algo);
  946. return -ENOMEM;
  947. }
  948. for (i = 0; i < tcount; i++) {
  949. memset(result, 0, 32);
  950. memcpy(seed, template[i].v, template[i].vlen);
  951. memcpy(seed + template[i].vlen, template[i].key,
  952. template[i].klen);
  953. memcpy(seed + template[i].vlen + template[i].klen,
  954. template[i].dt, template[i].dtlen);
  955. err = crypto_rng_reset(tfm, seed, seedsize);
  956. if (err) {
  957. printk(KERN_ERR "alg: cprng: Failed to reset rng "
  958. "for %s\n", algo);
  959. goto out;
  960. }
  961. for (j = 0; j < template[i].loops; j++) {
  962. err = crypto_rng_get_bytes(tfm, result,
  963. template[i].rlen);
  964. if (err != template[i].rlen) {
  965. printk(KERN_ERR "alg: cprng: Failed to obtain "
  966. "the correct amount of random data for "
  967. "%s (requested %d, got %d)\n", algo,
  968. template[i].rlen, err);
  969. goto out;
  970. }
  971. }
  972. err = memcmp(result, template[i].result,
  973. template[i].rlen);
  974. if (err) {
  975. printk(KERN_ERR "alg: cprng: Test %d failed for %s\n",
  976. i, algo);
  977. hexdump(result, template[i].rlen);
  978. err = -EINVAL;
  979. goto out;
  980. }
  981. }
  982. out:
  983. kfree(seed);
  984. return err;
  985. }
  986. static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
  987. u32 type, u32 mask)
  988. {
  989. struct crypto_aead *tfm;
  990. int err = 0;
  991. tfm = crypto_alloc_aead(driver, type, mask);
  992. if (IS_ERR(tfm)) {
  993. printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
  994. "%ld\n", driver, PTR_ERR(tfm));
  995. return PTR_ERR(tfm);
  996. }
  997. if (desc->suite.aead.enc.vecs) {
  998. err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
  999. desc->suite.aead.enc.count);
  1000. if (err)
  1001. goto out;
  1002. }
  1003. if (!err && desc->suite.aead.dec.vecs)
  1004. err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
  1005. desc->suite.aead.dec.count);
  1006. out:
  1007. crypto_free_aead(tfm);
  1008. return err;
  1009. }
  1010. static int alg_test_cipher(const struct alg_test_desc *desc,
  1011. const char *driver, u32 type, u32 mask)
  1012. {
  1013. struct crypto_cipher *tfm;
  1014. int err = 0;
  1015. tfm = crypto_alloc_cipher(driver, type, mask);
  1016. if (IS_ERR(tfm)) {
  1017. printk(KERN_ERR "alg: cipher: Failed to load transform for "
  1018. "%s: %ld\n", driver, PTR_ERR(tfm));
  1019. return PTR_ERR(tfm);
  1020. }
  1021. if (desc->suite.cipher.enc.vecs) {
  1022. err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
  1023. desc->suite.cipher.enc.count);
  1024. if (err)
  1025. goto out;
  1026. }
  1027. if (desc->suite.cipher.dec.vecs)
  1028. err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
  1029. desc->suite.cipher.dec.count);
  1030. out:
  1031. crypto_free_cipher(tfm);
  1032. return err;
  1033. }
  1034. static int alg_test_skcipher(const struct alg_test_desc *desc,
  1035. const char *driver, u32 type, u32 mask)
  1036. {
  1037. struct crypto_ablkcipher *tfm;
  1038. int err = 0;
  1039. tfm = crypto_alloc_ablkcipher(driver, type, mask);
  1040. if (IS_ERR(tfm)) {
  1041. printk(KERN_ERR "alg: skcipher: Failed to load transform for "
  1042. "%s: %ld\n", driver, PTR_ERR(tfm));
  1043. return PTR_ERR(tfm);
  1044. }
  1045. if (desc->suite.cipher.enc.vecs) {
  1046. err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
  1047. desc->suite.cipher.enc.count);
  1048. if (err)
  1049. goto out;
  1050. }
  1051. if (desc->suite.cipher.dec.vecs)
  1052. err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
  1053. desc->suite.cipher.dec.count);
  1054. out:
  1055. crypto_free_ablkcipher(tfm);
  1056. return err;
  1057. }
  1058. static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
  1059. u32 type, u32 mask)
  1060. {
  1061. struct crypto_comp *tfm;
  1062. int err;
  1063. tfm = crypto_alloc_comp(driver, type, mask);
  1064. if (IS_ERR(tfm)) {
  1065. printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
  1066. "%ld\n", driver, PTR_ERR(tfm));
  1067. return PTR_ERR(tfm);
  1068. }
  1069. err = test_comp(tfm, desc->suite.comp.comp.vecs,
  1070. desc->suite.comp.decomp.vecs,
  1071. desc->suite.comp.comp.count,
  1072. desc->suite.comp.decomp.count);
  1073. crypto_free_comp(tfm);
  1074. return err;
  1075. }
  1076. static int alg_test_pcomp(const struct alg_test_desc *desc, const char *driver,
  1077. u32 type, u32 mask)
  1078. {
  1079. struct crypto_pcomp *tfm;
  1080. int err;
  1081. tfm = crypto_alloc_pcomp(driver, type, mask);
  1082. if (IS_ERR(tfm)) {
  1083. pr_err("alg: pcomp: Failed to load transform for %s: %ld\n",
  1084. driver, PTR_ERR(tfm));
  1085. return PTR_ERR(tfm);
  1086. }
  1087. err = test_pcomp(tfm, desc->suite.pcomp.comp.vecs,
  1088. desc->suite.pcomp.decomp.vecs,
  1089. desc->suite.pcomp.comp.count,
  1090. desc->suite.pcomp.decomp.count);
  1091. crypto_free_pcomp(tfm);
  1092. return err;
  1093. }
  1094. static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
  1095. u32 type, u32 mask)
  1096. {
  1097. struct crypto_ahash *tfm;
  1098. int err;
  1099. tfm = crypto_alloc_ahash(driver, type, mask);
  1100. if (IS_ERR(tfm)) {
  1101. printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
  1102. "%ld\n", driver, PTR_ERR(tfm));
  1103. return PTR_ERR(tfm);
  1104. }
  1105. err = test_hash(tfm, desc->suite.hash.vecs, desc->suite.hash.count);
  1106. crypto_free_ahash(tfm);
  1107. return err;
  1108. }
  1109. static int alg_test_crc32c(const struct alg_test_desc *desc,
  1110. const char *driver, u32 type, u32 mask)
  1111. {
  1112. struct crypto_shash *tfm;
  1113. u32 val;
  1114. int err;
  1115. err = alg_test_hash(desc, driver, type, mask);
  1116. if (err)
  1117. goto out;
  1118. tfm = crypto_alloc_shash(driver, type, mask);
  1119. if (IS_ERR(tfm)) {
  1120. printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
  1121. "%ld\n", driver, PTR_ERR(tfm));
  1122. err = PTR_ERR(tfm);
  1123. goto out;
  1124. }
  1125. do {
  1126. struct {
  1127. struct shash_desc shash;
  1128. char ctx[crypto_shash_descsize(tfm)];
  1129. } sdesc;
  1130. sdesc.shash.tfm = tfm;
  1131. sdesc.shash.flags = 0;
  1132. *(u32 *)sdesc.ctx = le32_to_cpu(420553207);
  1133. err = crypto_shash_final(&sdesc.shash, (u8 *)&val);
  1134. if (err) {
  1135. printk(KERN_ERR "alg: crc32c: Operation failed for "
  1136. "%s: %d\n", driver, err);
  1137. break;
  1138. }
  1139. if (val != ~420553207) {
  1140. printk(KERN_ERR "alg: crc32c: Test failed for %s: "
  1141. "%d\n", driver, val);
  1142. err = -EINVAL;
  1143. }
  1144. } while (0);
  1145. crypto_free_shash(tfm);
  1146. out:
  1147. return err;
  1148. }
  1149. static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
  1150. u32 type, u32 mask)
  1151. {
  1152. struct crypto_rng *rng;
  1153. int err;
  1154. rng = crypto_alloc_rng(driver, type, mask);
  1155. if (IS_ERR(rng)) {
  1156. printk(KERN_ERR "alg: cprng: Failed to load transform for %s: "
  1157. "%ld\n", driver, PTR_ERR(rng));
  1158. return PTR_ERR(rng);
  1159. }
  1160. err = test_cprng(rng, desc->suite.cprng.vecs, desc->suite.cprng.count);
  1161. crypto_free_rng(rng);
  1162. return err;
  1163. }
  1164. /* Please keep this list sorted by algorithm name. */
  1165. static const struct alg_test_desc alg_test_descs[] = {
  1166. {
  1167. .alg = "ansi_cprng",
  1168. .test = alg_test_cprng,
  1169. .suite = {
  1170. .cprng = {
  1171. .vecs = ansi_cprng_aes_tv_template,
  1172. .count = ANSI_CPRNG_AES_TEST_VECTORS
  1173. }
  1174. }
  1175. }, {
  1176. .alg = "cbc(aes)",
  1177. .test = alg_test_skcipher,
  1178. .suite = {
  1179. .cipher = {
  1180. .enc = {
  1181. .vecs = aes_cbc_enc_tv_template,
  1182. .count = AES_CBC_ENC_TEST_VECTORS
  1183. },
  1184. .dec = {
  1185. .vecs = aes_cbc_dec_tv_template,
  1186. .count = AES_CBC_DEC_TEST_VECTORS
  1187. }
  1188. }
  1189. }
  1190. }, {
  1191. .alg = "cbc(anubis)",
  1192. .test = alg_test_skcipher,
  1193. .suite = {
  1194. .cipher = {
  1195. .enc = {
  1196. .vecs = anubis_cbc_enc_tv_template,
  1197. .count = ANUBIS_CBC_ENC_TEST_VECTORS
  1198. },
  1199. .dec = {
  1200. .vecs = anubis_cbc_dec_tv_template,
  1201. .count = ANUBIS_CBC_DEC_TEST_VECTORS
  1202. }
  1203. }
  1204. }
  1205. }, {
  1206. .alg = "cbc(blowfish)",
  1207. .test = alg_test_skcipher,
  1208. .suite = {
  1209. .cipher = {
  1210. .enc = {
  1211. .vecs = bf_cbc_enc_tv_template,
  1212. .count = BF_CBC_ENC_TEST_VECTORS
  1213. },
  1214. .dec = {
  1215. .vecs = bf_cbc_dec_tv_template,
  1216. .count = BF_CBC_DEC_TEST_VECTORS
  1217. }
  1218. }
  1219. }
  1220. }, {
  1221. .alg = "cbc(camellia)",
  1222. .test = alg_test_skcipher,
  1223. .suite = {
  1224. .cipher = {
  1225. .enc = {
  1226. .vecs = camellia_cbc_enc_tv_template,
  1227. .count = CAMELLIA_CBC_ENC_TEST_VECTORS
  1228. },
  1229. .dec = {
  1230. .vecs = camellia_cbc_dec_tv_template,
  1231. .count = CAMELLIA_CBC_DEC_TEST_VECTORS
  1232. }
  1233. }
  1234. }
  1235. }, {
  1236. .alg = "cbc(des)",
  1237. .test = alg_test_skcipher,
  1238. .suite = {
  1239. .cipher = {
  1240. .enc = {
  1241. .vecs = des_cbc_enc_tv_template,
  1242. .count = DES_CBC_ENC_TEST_VECTORS
  1243. },
  1244. .dec = {
  1245. .vecs = des_cbc_dec_tv_template,
  1246. .count = DES_CBC_DEC_TEST_VECTORS
  1247. }
  1248. }
  1249. }
  1250. }, {
  1251. .alg = "cbc(des3_ede)",
  1252. .test = alg_test_skcipher,
  1253. .suite = {
  1254. .cipher = {
  1255. .enc = {
  1256. .vecs = des3_ede_cbc_enc_tv_template,
  1257. .count = DES3_EDE_CBC_ENC_TEST_VECTORS
  1258. },
  1259. .dec = {
  1260. .vecs = des3_ede_cbc_dec_tv_template,
  1261. .count = DES3_EDE_CBC_DEC_TEST_VECTORS
  1262. }
  1263. }
  1264. }
  1265. }, {
  1266. .alg = "cbc(twofish)",
  1267. .test = alg_test_skcipher,
  1268. .suite = {
  1269. .cipher = {
  1270. .enc = {
  1271. .vecs = tf_cbc_enc_tv_template,
  1272. .count = TF_CBC_ENC_TEST_VECTORS
  1273. },
  1274. .dec = {
  1275. .vecs = tf_cbc_dec_tv_template,
  1276. .count = TF_CBC_DEC_TEST_VECTORS
  1277. }
  1278. }
  1279. }
  1280. }, {
  1281. .alg = "ccm(aes)",
  1282. .test = alg_test_aead,
  1283. .suite = {
  1284. .aead = {
  1285. .enc = {
  1286. .vecs = aes_ccm_enc_tv_template,
  1287. .count = AES_CCM_ENC_TEST_VECTORS
  1288. },
  1289. .dec = {
  1290. .vecs = aes_ccm_dec_tv_template,
  1291. .count = AES_CCM_DEC_TEST_VECTORS
  1292. }
  1293. }
  1294. }
  1295. }, {
  1296. .alg = "crc32c",
  1297. .test = alg_test_crc32c,
  1298. .suite = {
  1299. .hash = {
  1300. .vecs = crc32c_tv_template,
  1301. .count = CRC32C_TEST_VECTORS
  1302. }
  1303. }
  1304. }, {
  1305. .alg = "cts(cbc(aes))",
  1306. .test = alg_test_skcipher,
  1307. .suite = {
  1308. .cipher = {
  1309. .enc = {
  1310. .vecs = cts_mode_enc_tv_template,
  1311. .count = CTS_MODE_ENC_TEST_VECTORS
  1312. },
  1313. .dec = {
  1314. .vecs = cts_mode_dec_tv_template,
  1315. .count = CTS_MODE_DEC_TEST_VECTORS
  1316. }
  1317. }
  1318. }
  1319. }, {
  1320. .alg = "deflate",
  1321. .test = alg_test_comp,
  1322. .suite = {
  1323. .comp = {
  1324. .comp = {
  1325. .vecs = deflate_comp_tv_template,
  1326. .count = DEFLATE_COMP_TEST_VECTORS
  1327. },
  1328. .decomp = {
  1329. .vecs = deflate_decomp_tv_template,
  1330. .count = DEFLATE_DECOMP_TEST_VECTORS
  1331. }
  1332. }
  1333. }
  1334. }, {
  1335. .alg = "ecb(aes)",
  1336. .test = alg_test_skcipher,
  1337. .suite = {
  1338. .cipher = {
  1339. .enc = {
  1340. .vecs = aes_enc_tv_template,
  1341. .count = AES_ENC_TEST_VECTORS
  1342. },
  1343. .dec = {
  1344. .vecs = aes_dec_tv_template,
  1345. .count = AES_DEC_TEST_VECTORS
  1346. }
  1347. }
  1348. }
  1349. }, {
  1350. .alg = "ecb(anubis)",
  1351. .test = alg_test_skcipher,
  1352. .suite = {
  1353. .cipher = {
  1354. .enc = {
  1355. .vecs = anubis_enc_tv_template,
  1356. .count = ANUBIS_ENC_TEST_VECTORS
  1357. },
  1358. .dec = {
  1359. .vecs = anubis_dec_tv_template,
  1360. .count = ANUBIS_DEC_TEST_VECTORS
  1361. }
  1362. }
  1363. }
  1364. }, {
  1365. .alg = "ecb(arc4)",
  1366. .test = alg_test_skcipher,
  1367. .suite = {
  1368. .cipher = {
  1369. .enc = {
  1370. .vecs = arc4_enc_tv_template,
  1371. .count = ARC4_ENC_TEST_VECTORS
  1372. },
  1373. .dec = {
  1374. .vecs = arc4_dec_tv_template,
  1375. .count = ARC4_DEC_TEST_VECTORS
  1376. }
  1377. }
  1378. }
  1379. }, {
  1380. .alg = "ecb(blowfish)",
  1381. .test = alg_test_skcipher,
  1382. .suite = {
  1383. .cipher = {
  1384. .enc = {
  1385. .vecs = bf_enc_tv_template,
  1386. .count = BF_ENC_TEST_VECTORS
  1387. },
  1388. .dec = {
  1389. .vecs = bf_dec_tv_template,
  1390. .count = BF_DEC_TEST_VECTORS
  1391. }
  1392. }
  1393. }
  1394. }, {
  1395. .alg = "ecb(camellia)",
  1396. .test = alg_test_skcipher,
  1397. .suite = {
  1398. .cipher = {
  1399. .enc = {
  1400. .vecs = camellia_enc_tv_template,
  1401. .count = CAMELLIA_ENC_TEST_VECTORS
  1402. },
  1403. .dec = {
  1404. .vecs = camellia_dec_tv_template,
  1405. .count = CAMELLIA_DEC_TEST_VECTORS
  1406. }
  1407. }
  1408. }
  1409. }, {
  1410. .alg = "ecb(cast5)",
  1411. .test = alg_test_skcipher,
  1412. .suite = {
  1413. .cipher = {
  1414. .enc = {
  1415. .vecs = cast5_enc_tv_template,
  1416. .count = CAST5_ENC_TEST_VECTORS
  1417. },
  1418. .dec = {
  1419. .vecs = cast5_dec_tv_template,
  1420. .count = CAST5_DEC_TEST_VECTORS
  1421. }
  1422. }
  1423. }
  1424. }, {
  1425. .alg = "ecb(cast6)",
  1426. .test = alg_test_skcipher,
  1427. .suite = {
  1428. .cipher = {
  1429. .enc = {
  1430. .vecs = cast6_enc_tv_template,
  1431. .count = CAST6_ENC_TEST_VECTORS
  1432. },
  1433. .dec = {
  1434. .vecs = cast6_dec_tv_template,
  1435. .count = CAST6_DEC_TEST_VECTORS
  1436. }
  1437. }
  1438. }
  1439. }, {
  1440. .alg = "ecb(des)",
  1441. .test = alg_test_skcipher,
  1442. .suite = {
  1443. .cipher = {
  1444. .enc = {
  1445. .vecs = des_enc_tv_template,
  1446. .count = DES_ENC_TEST_VECTORS
  1447. },
  1448. .dec = {
  1449. .vecs = des_dec_tv_template,
  1450. .count = DES_DEC_TEST_VECTORS
  1451. }
  1452. }
  1453. }
  1454. }, {
  1455. .alg = "ecb(des3_ede)",
  1456. .test = alg_test_skcipher,
  1457. .suite = {
  1458. .cipher = {
  1459. .enc = {
  1460. .vecs = des3_ede_enc_tv_template,
  1461. .count = DES3_EDE_ENC_TEST_VECTORS
  1462. },
  1463. .dec = {
  1464. .vecs = des3_ede_dec_tv_template,
  1465. .count = DES3_EDE_DEC_TEST_VECTORS
  1466. }
  1467. }
  1468. }
  1469. }, {
  1470. .alg = "ecb(khazad)",
  1471. .test = alg_test_skcipher,
  1472. .suite = {
  1473. .cipher = {
  1474. .enc = {
  1475. .vecs = khazad_enc_tv_template,
  1476. .count = KHAZAD_ENC_TEST_VECTORS
  1477. },
  1478. .dec = {
  1479. .vecs = khazad_dec_tv_template,
  1480. .count = KHAZAD_DEC_TEST_VECTORS
  1481. }
  1482. }
  1483. }
  1484. }, {
  1485. .alg = "ecb(seed)",
  1486. .test = alg_test_skcipher,
  1487. .suite = {
  1488. .cipher = {
  1489. .enc = {
  1490. .vecs = seed_enc_tv_template,
  1491. .count = SEED_ENC_TEST_VECTORS
  1492. },
  1493. .dec = {
  1494. .vecs = seed_dec_tv_template,
  1495. .count = SEED_DEC_TEST_VECTORS
  1496. }
  1497. }
  1498. }
  1499. }, {
  1500. .alg = "ecb(serpent)",
  1501. .test = alg_test_skcipher,
  1502. .suite = {
  1503. .cipher = {
  1504. .enc = {
  1505. .vecs = serpent_enc_tv_template,
  1506. .count = SERPENT_ENC_TEST_VECTORS
  1507. },
  1508. .dec = {
  1509. .vecs = serpent_dec_tv_template,
  1510. .count = SERPENT_DEC_TEST_VECTORS
  1511. }
  1512. }
  1513. }
  1514. }, {
  1515. .alg = "ecb(tea)",
  1516. .test = alg_test_skcipher,
  1517. .suite = {
  1518. .cipher = {
  1519. .enc = {
  1520. .vecs = tea_enc_tv_template,
  1521. .count = TEA_ENC_TEST_VECTORS
  1522. },
  1523. .dec = {
  1524. .vecs = tea_dec_tv_template,
  1525. .count = TEA_DEC_TEST_VECTORS
  1526. }
  1527. }
  1528. }
  1529. }, {
  1530. .alg = "ecb(tnepres)",
  1531. .test = alg_test_skcipher,
  1532. .suite = {
  1533. .cipher = {
  1534. .enc = {
  1535. .vecs = tnepres_enc_tv_template,
  1536. .count = TNEPRES_ENC_TEST_VECTORS
  1537. },
  1538. .dec = {
  1539. .vecs = tnepres_dec_tv_template,
  1540. .count = TNEPRES_DEC_TEST_VECTORS
  1541. }
  1542. }
  1543. }
  1544. }, {
  1545. .alg = "ecb(twofish)",
  1546. .test = alg_test_skcipher,
  1547. .suite = {
  1548. .cipher = {
  1549. .enc = {
  1550. .vecs = tf_enc_tv_template,
  1551. .count = TF_ENC_TEST_VECTORS
  1552. },
  1553. .dec = {
  1554. .vecs = tf_dec_tv_template,
  1555. .count = TF_DEC_TEST_VECTORS
  1556. }
  1557. }
  1558. }
  1559. }, {
  1560. .alg = "ecb(xeta)",
  1561. .test = alg_test_skcipher,
  1562. .suite = {
  1563. .cipher = {
  1564. .enc = {
  1565. .vecs = xeta_enc_tv_template,
  1566. .count = XETA_ENC_TEST_VECTORS
  1567. },
  1568. .dec = {
  1569. .vecs = xeta_dec_tv_template,
  1570. .count = XETA_DEC_TEST_VECTORS
  1571. }
  1572. }
  1573. }
  1574. }, {
  1575. .alg = "ecb(xtea)",
  1576. .test = alg_test_skcipher,
  1577. .suite = {
  1578. .cipher = {
  1579. .enc = {
  1580. .vecs = xtea_enc_tv_template,
  1581. .count = XTEA_ENC_TEST_VECTORS
  1582. },
  1583. .dec = {
  1584. .vecs = xtea_dec_tv_template,
  1585. .count = XTEA_DEC_TEST_VECTORS
  1586. }
  1587. }
  1588. }
  1589. }, {
  1590. .alg = "gcm(aes)",
  1591. .test = alg_test_aead,
  1592. .suite = {
  1593. .aead = {
  1594. .enc = {
  1595. .vecs = aes_gcm_enc_tv_template,
  1596. .count = AES_GCM_ENC_TEST_VECTORS
  1597. },
  1598. .dec = {
  1599. .vecs = aes_gcm_dec_tv_template,
  1600. .count = AES_GCM_DEC_TEST_VECTORS
  1601. }
  1602. }
  1603. }
  1604. }, {
  1605. .alg = "hmac(md5)",
  1606. .test = alg_test_hash,
  1607. .suite = {
  1608. .hash = {
  1609. .vecs = hmac_md5_tv_template,
  1610. .count = HMAC_MD5_TEST_VECTORS
  1611. }
  1612. }
  1613. }, {
  1614. .alg = "hmac(rmd128)",
  1615. .test = alg_test_hash,
  1616. .suite = {
  1617. .hash = {
  1618. .vecs = hmac_rmd128_tv_template,
  1619. .count = HMAC_RMD128_TEST_VECTORS
  1620. }
  1621. }
  1622. }, {
  1623. .alg = "hmac(rmd160)",
  1624. .test = alg_test_hash,
  1625. .suite = {
  1626. .hash = {
  1627. .vecs = hmac_rmd160_tv_template,
  1628. .count = HMAC_RMD160_TEST_VECTORS
  1629. }
  1630. }
  1631. }, {
  1632. .alg = "hmac(sha1)",
  1633. .test = alg_test_hash,
  1634. .suite = {
  1635. .hash = {
  1636. .vecs = hmac_sha1_tv_template,
  1637. .count = HMAC_SHA1_TEST_VECTORS
  1638. }
  1639. }
  1640. }, {
  1641. .alg = "hmac(sha224)",
  1642. .test = alg_test_hash,
  1643. .suite = {
  1644. .hash = {
  1645. .vecs = hmac_sha224_tv_template,
  1646. .count = HMAC_SHA224_TEST_VECTORS
  1647. }
  1648. }
  1649. }, {
  1650. .alg = "hmac(sha256)",
  1651. .test = alg_test_hash,
  1652. .suite = {
  1653. .hash = {
  1654. .vecs = hmac_sha256_tv_template,
  1655. .count = HMAC_SHA256_TEST_VECTORS
  1656. }
  1657. }
  1658. }, {
  1659. .alg = "hmac(sha384)",
  1660. .test = alg_test_hash,
  1661. .suite = {
  1662. .hash = {
  1663. .vecs = hmac_sha384_tv_template,
  1664. .count = HMAC_SHA384_TEST_VECTORS
  1665. }
  1666. }
  1667. }, {
  1668. .alg = "hmac(sha512)",
  1669. .test = alg_test_hash,
  1670. .suite = {
  1671. .hash = {
  1672. .vecs = hmac_sha512_tv_template,
  1673. .count = HMAC_SHA512_TEST_VECTORS
  1674. }
  1675. }
  1676. }, {
  1677. .alg = "lrw(aes)",
  1678. .test = alg_test_skcipher,
  1679. .suite = {
  1680. .cipher = {
  1681. .enc = {
  1682. .vecs = aes_lrw_enc_tv_template,
  1683. .count = AES_LRW_ENC_TEST_VECTORS
  1684. },
  1685. .dec = {
  1686. .vecs = aes_lrw_dec_tv_template,
  1687. .count = AES_LRW_DEC_TEST_VECTORS
  1688. }
  1689. }
  1690. }
  1691. }, {
  1692. .alg = "lzo",
  1693. .test = alg_test_comp,
  1694. .suite = {
  1695. .comp = {
  1696. .comp = {
  1697. .vecs = lzo_comp_tv_template,
  1698. .count = LZO_COMP_TEST_VECTORS
  1699. },
  1700. .decomp = {
  1701. .vecs = lzo_decomp_tv_template,
  1702. .count = LZO_DECOMP_TEST_VECTORS
  1703. }
  1704. }
  1705. }
  1706. }, {
  1707. .alg = "md4",
  1708. .test = alg_test_hash,
  1709. .suite = {
  1710. .hash = {
  1711. .vecs = md4_tv_template,
  1712. .count = MD4_TEST_VECTORS
  1713. }
  1714. }
  1715. }, {
  1716. .alg = "md5",
  1717. .test = alg_test_hash,
  1718. .suite = {
  1719. .hash = {
  1720. .vecs = md5_tv_template,
  1721. .count = MD5_TEST_VECTORS
  1722. }
  1723. }
  1724. }, {
  1725. .alg = "michael_mic",
  1726. .test = alg_test_hash,
  1727. .suite = {
  1728. .hash = {
  1729. .vecs = michael_mic_tv_template,
  1730. .count = MICHAEL_MIC_TEST_VECTORS
  1731. }
  1732. }
  1733. }, {
  1734. .alg = "pcbc(fcrypt)",
  1735. .test = alg_test_skcipher,
  1736. .suite = {
  1737. .cipher = {
  1738. .enc = {
  1739. .vecs = fcrypt_pcbc_enc_tv_template,
  1740. .count = FCRYPT_ENC_TEST_VECTORS
  1741. },
  1742. .dec = {
  1743. .vecs = fcrypt_pcbc_dec_tv_template,
  1744. .count = FCRYPT_DEC_TEST_VECTORS
  1745. }
  1746. }
  1747. }
  1748. }, {
  1749. .alg = "rfc3686(ctr(aes))",
  1750. .test = alg_test_skcipher,
  1751. .suite = {
  1752. .cipher = {
  1753. .enc = {
  1754. .vecs = aes_ctr_enc_tv_template,
  1755. .count = AES_CTR_ENC_TEST_VECTORS
  1756. },
  1757. .dec = {
  1758. .vecs = aes_ctr_dec_tv_template,
  1759. .count = AES_CTR_DEC_TEST_VECTORS
  1760. }
  1761. }
  1762. }
  1763. }, {
  1764. .alg = "rfc4309(ccm(aes))",
  1765. .test = alg_test_aead,
  1766. .suite = {
  1767. .aead = {
  1768. .enc = {
  1769. .vecs = aes_ccm_rfc4309_enc_tv_template,
  1770. .count = AES_CCM_4309_ENC_TEST_VECTORS
  1771. },
  1772. .dec = {
  1773. .vecs = aes_ccm_rfc4309_dec_tv_template,
  1774. .count = AES_CCM_4309_DEC_TEST_VECTORS
  1775. }
  1776. }
  1777. }
  1778. }, {
  1779. .alg = "rmd128",
  1780. .test = alg_test_hash,
  1781. .suite = {
  1782. .hash = {
  1783. .vecs = rmd128_tv_template,
  1784. .count = RMD128_TEST_VECTORS
  1785. }
  1786. }
  1787. }, {
  1788. .alg = "rmd160",
  1789. .test = alg_test_hash,
  1790. .suite = {
  1791. .hash = {
  1792. .vecs = rmd160_tv_template,
  1793. .count = RMD160_TEST_VECTORS
  1794. }
  1795. }
  1796. }, {
  1797. .alg = "rmd256",
  1798. .test = alg_test_hash,
  1799. .suite = {
  1800. .hash = {
  1801. .vecs = rmd256_tv_template,
  1802. .count = RMD256_TEST_VECTORS
  1803. }
  1804. }
  1805. }, {
  1806. .alg = "rmd320",
  1807. .test = alg_test_hash,
  1808. .suite = {
  1809. .hash = {
  1810. .vecs = rmd320_tv_template,
  1811. .count = RMD320_TEST_VECTORS
  1812. }
  1813. }
  1814. }, {
  1815. .alg = "salsa20",
  1816. .test = alg_test_skcipher,
  1817. .suite = {
  1818. .cipher = {
  1819. .enc = {
  1820. .vecs = salsa20_stream_enc_tv_template,
  1821. .count = SALSA20_STREAM_ENC_TEST_VECTORS
  1822. }
  1823. }
  1824. }
  1825. }, {
  1826. .alg = "sha1",
  1827. .test = alg_test_hash,
  1828. .suite = {
  1829. .hash = {
  1830. .vecs = sha1_tv_template,
  1831. .count = SHA1_TEST_VECTORS
  1832. }
  1833. }
  1834. }, {
  1835. .alg = "sha224",
  1836. .test = alg_test_hash,
  1837. .suite = {
  1838. .hash = {
  1839. .vecs = sha224_tv_template,
  1840. .count = SHA224_TEST_VECTORS
  1841. }
  1842. }
  1843. }, {
  1844. .alg = "sha256",
  1845. .test = alg_test_hash,
  1846. .suite = {
  1847. .hash = {
  1848. .vecs = sha256_tv_template,
  1849. .count = SHA256_TEST_VECTORS
  1850. }
  1851. }
  1852. }, {
  1853. .alg = "sha384",
  1854. .test = alg_test_hash,
  1855. .suite = {
  1856. .hash = {
  1857. .vecs = sha384_tv_template,
  1858. .count = SHA384_TEST_VECTORS
  1859. }
  1860. }
  1861. }, {
  1862. .alg = "sha512",
  1863. .test = alg_test_hash,
  1864. .suite = {
  1865. .hash = {
  1866. .vecs = sha512_tv_template,
  1867. .count = SHA512_TEST_VECTORS
  1868. }
  1869. }
  1870. }, {
  1871. .alg = "tgr128",
  1872. .test = alg_test_hash,
  1873. .suite = {
  1874. .hash = {
  1875. .vecs = tgr128_tv_template,
  1876. .count = TGR128_TEST_VECTORS
  1877. }
  1878. }
  1879. }, {
  1880. .alg = "tgr160",
  1881. .test = alg_test_hash,
  1882. .suite = {
  1883. .hash = {
  1884. .vecs = tgr160_tv_template,
  1885. .count = TGR160_TEST_VECTORS
  1886. }
  1887. }
  1888. }, {
  1889. .alg = "tgr192",
  1890. .test = alg_test_hash,
  1891. .suite = {
  1892. .hash = {
  1893. .vecs = tgr192_tv_template,
  1894. .count = TGR192_TEST_VECTORS
  1895. }
  1896. }
  1897. }, {
  1898. .alg = "wp256",
  1899. .test = alg_test_hash,
  1900. .suite = {
  1901. .hash = {
  1902. .vecs = wp256_tv_template,
  1903. .count = WP256_TEST_VECTORS
  1904. }
  1905. }
  1906. }, {
  1907. .alg = "wp384",
  1908. .test = alg_test_hash,
  1909. .suite = {
  1910. .hash = {
  1911. .vecs = wp384_tv_template,
  1912. .count = WP384_TEST_VECTORS
  1913. }
  1914. }
  1915. }, {
  1916. .alg = "wp512",
  1917. .test = alg_test_hash,
  1918. .suite = {
  1919. .hash = {
  1920. .vecs = wp512_tv_template,
  1921. .count = WP512_TEST_VECTORS
  1922. }
  1923. }
  1924. }, {
  1925. .alg = "xcbc(aes)",
  1926. .test = alg_test_hash,
  1927. .suite = {
  1928. .hash = {
  1929. .vecs = aes_xcbc128_tv_template,
  1930. .count = XCBC_AES_TEST_VECTORS
  1931. }
  1932. }
  1933. }, {
  1934. .alg = "xts(aes)",
  1935. .test = alg_test_skcipher,
  1936. .suite = {
  1937. .cipher = {
  1938. .enc = {
  1939. .vecs = aes_xts_enc_tv_template,
  1940. .count = AES_XTS_ENC_TEST_VECTORS
  1941. },
  1942. .dec = {
  1943. .vecs = aes_xts_dec_tv_template,
  1944. .count = AES_XTS_DEC_TEST_VECTORS
  1945. }
  1946. }
  1947. }
  1948. }, {
  1949. .alg = "zlib",
  1950. .test = alg_test_pcomp,
  1951. .suite = {
  1952. .pcomp = {
  1953. .comp = {
  1954. .vecs = zlib_comp_tv_template,
  1955. .count = ZLIB_COMP_TEST_VECTORS
  1956. },
  1957. .decomp = {
  1958. .vecs = zlib_decomp_tv_template,
  1959. .count = ZLIB_DECOMP_TEST_VECTORS
  1960. }
  1961. }
  1962. }
  1963. }
  1964. };
  1965. static int alg_find_test(const char *alg)
  1966. {
  1967. int start = 0;
  1968. int end = ARRAY_SIZE(alg_test_descs);
  1969. while (start < end) {
  1970. int i = (start + end) / 2;
  1971. int diff = strcmp(alg_test_descs[i].alg, alg);
  1972. if (diff > 0) {
  1973. end = i;
  1974. continue;
  1975. }
  1976. if (diff < 0) {
  1977. start = i + 1;
  1978. continue;
  1979. }
  1980. return i;
  1981. }
  1982. return -1;
  1983. }
  1984. int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
  1985. {
  1986. int i;
  1987. int rc;
  1988. if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
  1989. char nalg[CRYPTO_MAX_ALG_NAME];
  1990. if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
  1991. sizeof(nalg))
  1992. return -ENAMETOOLONG;
  1993. i = alg_find_test(nalg);
  1994. if (i < 0)
  1995. goto notest;
  1996. rc = alg_test_cipher(alg_test_descs + i, driver, type, mask);
  1997. goto test_done;
  1998. }
  1999. i = alg_find_test(alg);
  2000. if (i < 0)
  2001. goto notest;
  2002. rc = alg_test_descs[i].test(alg_test_descs + i, driver,
  2003. type, mask);
  2004. test_done:
  2005. if (fips_enabled && rc)
  2006. panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
  2007. if (fips_enabled && !rc)
  2008. printk(KERN_INFO "alg: self-tests for %s (%s) passed\n",
  2009. driver, alg);
  2010. return rc;
  2011. notest:
  2012. printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
  2013. return 0;
  2014. }
  2015. EXPORT_SYMBOL_GPL(alg_test);
  2016. int __init testmgr_init(void)
  2017. {
  2018. int i;
  2019. for (i = 0; i < XBUFSIZE; i++) {
  2020. xbuf[i] = (void *)__get_free_page(GFP_KERNEL);
  2021. if (!xbuf[i])
  2022. goto err_free_xbuf;
  2023. }
  2024. for (i = 0; i < XBUFSIZE; i++) {
  2025. axbuf[i] = (void *)__get_free_page(GFP_KERNEL);
  2026. if (!axbuf[i])
  2027. goto err_free_axbuf;
  2028. }
  2029. return 0;
  2030. err_free_axbuf:
  2031. for (i = 0; i < XBUFSIZE && axbuf[i]; i++)
  2032. free_page((unsigned long)axbuf[i]);
  2033. err_free_xbuf:
  2034. for (i = 0; i < XBUFSIZE && xbuf[i]; i++)
  2035. free_page((unsigned long)xbuf[i]);
  2036. return -ENOMEM;
  2037. }
  2038. void testmgr_exit(void)
  2039. {
  2040. int i;
  2041. for (i = 0; i < XBUFSIZE; i++)
  2042. free_page((unsigned long)axbuf[i]);
  2043. for (i = 0; i < XBUFSIZE; i++)
  2044. free_page((unsigned long)xbuf[i]);
  2045. }