testmgr.c 51 KB

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