testmgr.c 49 KB

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