testmgr.c 38 KB

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