testmgr.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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_ablkcipher *tfm, int enc,
  465. struct cipher_testvec *template, unsigned int tcount)
  466. {
  467. const char *algo =
  468. crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
  469. unsigned int i, j, k, n, temp;
  470. int ret;
  471. char *q;
  472. struct ablkcipher_request *req;
  473. struct scatterlist sg[8];
  474. const char *e;
  475. struct tcrypt_result result;
  476. void *data;
  477. char iv[MAX_IVLEN];
  478. if (enc == ENCRYPT)
  479. e = "encryption";
  480. else
  481. e = "decryption";
  482. init_completion(&result.completion);
  483. req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
  484. if (!req) {
  485. printk(KERN_ERR "alg: cipher: Failed to allocate request for "
  486. "%s\n", algo);
  487. ret = -ENOMEM;
  488. goto out;
  489. }
  490. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  491. tcrypt_complete, &result);
  492. j = 0;
  493. for (i = 0; i < tcount; i++) {
  494. if (template[i].iv)
  495. memcpy(iv, template[i].iv, MAX_IVLEN);
  496. else
  497. memset(iv, 0, MAX_IVLEN);
  498. if (!(template[i].np)) {
  499. j++;
  500. data = xbuf[0];
  501. memcpy(data, template[i].input, template[i].ilen);
  502. crypto_ablkcipher_clear_flags(tfm, ~0);
  503. if (template[i].wk)
  504. crypto_ablkcipher_set_flags(
  505. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  506. ret = crypto_ablkcipher_setkey(tfm, template[i].key,
  507. template[i].klen);
  508. if (!ret == template[i].fail) {
  509. printk(KERN_ERR "alg: cipher: setkey failed "
  510. "on test %d for %s: flags=%x\n", j,
  511. algo, crypto_ablkcipher_get_flags(tfm));
  512. goto out;
  513. } else if (ret)
  514. continue;
  515. sg_init_one(&sg[0], data, template[i].ilen);
  516. ablkcipher_request_set_crypt(req, sg, sg,
  517. template[i].ilen, iv);
  518. ret = enc ?
  519. crypto_ablkcipher_encrypt(req) :
  520. crypto_ablkcipher_decrypt(req);
  521. switch (ret) {
  522. case 0:
  523. break;
  524. case -EINPROGRESS:
  525. case -EBUSY:
  526. ret = wait_for_completion_interruptible(
  527. &result.completion);
  528. if (!ret && !((ret = result.err))) {
  529. INIT_COMPLETION(result.completion);
  530. break;
  531. }
  532. /* fall through */
  533. default:
  534. printk(KERN_ERR "alg: cipher: %s failed on "
  535. "test %d for %s: ret=%d\n", e, j, algo,
  536. -ret);
  537. goto out;
  538. }
  539. q = data;
  540. if (memcmp(q, template[i].result, template[i].rlen)) {
  541. printk(KERN_ERR "alg: cipher: Test %d failed "
  542. "on %s for %s\n", j, e, algo);
  543. hexdump(q, template[i].rlen);
  544. ret = -EINVAL;
  545. goto out;
  546. }
  547. }
  548. }
  549. j = 0;
  550. for (i = 0; i < tcount; i++) {
  551. if (template[i].iv)
  552. memcpy(iv, template[i].iv, MAX_IVLEN);
  553. else
  554. memset(iv, 0, MAX_IVLEN);
  555. if (template[i].np) {
  556. j++;
  557. crypto_ablkcipher_clear_flags(tfm, ~0);
  558. if (template[i].wk)
  559. crypto_ablkcipher_set_flags(
  560. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  561. ret = crypto_ablkcipher_setkey(tfm, template[i].key,
  562. template[i].klen);
  563. if (!ret == template[i].fail) {
  564. printk(KERN_ERR "alg: cipher: setkey failed "
  565. "on chunk test %d for %s: flags=%x\n",
  566. j, algo,
  567. crypto_ablkcipher_get_flags(tfm));
  568. goto out;
  569. } else if (ret)
  570. continue;
  571. temp = 0;
  572. ret = -EINVAL;
  573. sg_init_table(sg, template[i].np);
  574. for (k = 0; k < template[i].np; k++) {
  575. if (WARN_ON(offset_in_page(IDX[k]) +
  576. template[i].tap[k] > PAGE_SIZE))
  577. goto out;
  578. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  579. offset_in_page(IDX[k]);
  580. memcpy(q, template[i].input + temp,
  581. template[i].tap[k]);
  582. if (offset_in_page(q) + template[i].tap[k] <
  583. PAGE_SIZE)
  584. q[template[i].tap[k]] = 0;
  585. sg_set_buf(&sg[k], q, template[i].tap[k]);
  586. temp += template[i].tap[k];
  587. }
  588. ablkcipher_request_set_crypt(req, sg, sg,
  589. template[i].ilen, iv);
  590. ret = enc ?
  591. crypto_ablkcipher_encrypt(req) :
  592. crypto_ablkcipher_decrypt(req);
  593. switch (ret) {
  594. case 0:
  595. break;
  596. case -EINPROGRESS:
  597. case -EBUSY:
  598. ret = wait_for_completion_interruptible(
  599. &result.completion);
  600. if (!ret && !((ret = result.err))) {
  601. INIT_COMPLETION(result.completion);
  602. break;
  603. }
  604. /* fall through */
  605. default:
  606. printk(KERN_ERR "alg: cipher: %s failed on "
  607. "chunk test %d for %s: ret=%d\n", e, j,
  608. algo, -ret);
  609. goto out;
  610. }
  611. temp = 0;
  612. ret = -EINVAL;
  613. for (k = 0; k < template[i].np; k++) {
  614. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  615. offset_in_page(IDX[k]);
  616. if (memcmp(q, template[i].result + temp,
  617. template[i].tap[k])) {
  618. printk(KERN_ERR "alg: cipher: Chunk "
  619. "test %d failed on %s at page "
  620. "%u for %s\n", j, e, k, algo);
  621. hexdump(q, template[i].tap[k]);
  622. goto out;
  623. }
  624. q += template[i].tap[k];
  625. for (n = 0; offset_in_page(q + n) && q[n]; n++)
  626. ;
  627. if (n) {
  628. printk(KERN_ERR "alg: cipher: "
  629. "Result buffer corruption in "
  630. "chunk test %d on %s at page "
  631. "%u for %s: %u bytes:\n", j, e,
  632. k, algo, n);
  633. hexdump(q, n);
  634. goto out;
  635. }
  636. temp += template[i].tap[k];
  637. }
  638. }
  639. }
  640. ret = 0;
  641. out:
  642. ablkcipher_request_free(req);
  643. return ret;
  644. }
  645. static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
  646. struct comp_testvec *dtemplate, int ctcount, int dtcount)
  647. {
  648. const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
  649. unsigned int i;
  650. char result[COMP_BUF_SIZE];
  651. int ret;
  652. for (i = 0; i < ctcount; i++) {
  653. int ilen, dlen = COMP_BUF_SIZE;
  654. memset(result, 0, sizeof (result));
  655. ilen = ctemplate[i].inlen;
  656. ret = crypto_comp_compress(tfm, ctemplate[i].input,
  657. ilen, result, &dlen);
  658. if (ret) {
  659. printk(KERN_ERR "alg: comp: compression failed "
  660. "on test %d for %s: ret=%d\n", i + 1, algo,
  661. -ret);
  662. goto out;
  663. }
  664. if (memcmp(result, ctemplate[i].output, dlen)) {
  665. printk(KERN_ERR "alg: comp: Compression test %d "
  666. "failed for %s\n", i + 1, algo);
  667. hexdump(result, dlen);
  668. ret = -EINVAL;
  669. goto out;
  670. }
  671. }
  672. for (i = 0; i < dtcount; i++) {
  673. int ilen, ret, dlen = COMP_BUF_SIZE;
  674. memset(result, 0, sizeof (result));
  675. ilen = dtemplate[i].inlen;
  676. ret = crypto_comp_decompress(tfm, dtemplate[i].input,
  677. ilen, result, &dlen);
  678. if (ret) {
  679. printk(KERN_ERR "alg: comp: decompression failed "
  680. "on test %d for %s: ret=%d\n", i + 1, algo,
  681. -ret);
  682. goto out;
  683. }
  684. if (memcmp(result, dtemplate[i].output, dlen)) {
  685. printk(KERN_ERR "alg: comp: Decompression test %d "
  686. "failed for %s\n", i + 1, algo);
  687. hexdump(result, dlen);
  688. ret = -EINVAL;
  689. goto out;
  690. }
  691. }
  692. ret = 0;
  693. out:
  694. return ret;
  695. }
  696. static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
  697. u32 type, u32 mask)
  698. {
  699. struct crypto_aead *tfm;
  700. int err = 0;
  701. tfm = crypto_alloc_aead(driver, type, mask);
  702. if (IS_ERR(tfm)) {
  703. printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
  704. "%ld\n", driver, PTR_ERR(tfm));
  705. return PTR_ERR(tfm);
  706. }
  707. if (desc->suite.aead.enc.vecs) {
  708. err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
  709. desc->suite.aead.enc.count);
  710. if (err)
  711. goto out;
  712. }
  713. if (!err && desc->suite.aead.dec.vecs)
  714. err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
  715. desc->suite.aead.dec.count);
  716. out:
  717. crypto_free_aead(tfm);
  718. return err;
  719. }
  720. static int alg_test_cipher(const struct alg_test_desc *desc,
  721. const char *driver, u32 type, u32 mask)
  722. {
  723. struct crypto_ablkcipher *tfm;
  724. int err = 0;
  725. tfm = crypto_alloc_ablkcipher(driver, type, mask);
  726. if (IS_ERR(tfm)) {
  727. printk(KERN_ERR "alg: cipher: Failed to load transform for "
  728. "%s: %ld\n", driver, PTR_ERR(tfm));
  729. return PTR_ERR(tfm);
  730. }
  731. if (desc->suite.cipher.enc.vecs) {
  732. err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
  733. desc->suite.cipher.enc.count);
  734. if (err)
  735. goto out;
  736. }
  737. if (desc->suite.cipher.dec.vecs)
  738. err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
  739. desc->suite.cipher.dec.count);
  740. out:
  741. crypto_free_ablkcipher(tfm);
  742. return err;
  743. }
  744. static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
  745. u32 type, u32 mask)
  746. {
  747. struct crypto_comp *tfm;
  748. int err;
  749. tfm = crypto_alloc_comp(driver, type, mask);
  750. if (IS_ERR(tfm)) {
  751. printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
  752. "%ld\n", driver, PTR_ERR(tfm));
  753. return PTR_ERR(tfm);
  754. }
  755. err = test_comp(tfm, desc->suite.comp.comp.vecs,
  756. desc->suite.comp.decomp.vecs,
  757. desc->suite.comp.comp.count,
  758. desc->suite.comp.decomp.count);
  759. crypto_free_comp(tfm);
  760. return err;
  761. }
  762. static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
  763. u32 type, u32 mask)
  764. {
  765. struct crypto_ahash *tfm;
  766. int err;
  767. tfm = crypto_alloc_ahash(driver, type, mask);
  768. if (IS_ERR(tfm)) {
  769. printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
  770. "%ld\n", driver, PTR_ERR(tfm));
  771. return PTR_ERR(tfm);
  772. }
  773. err = test_hash(tfm, desc->suite.hash.vecs, desc->suite.hash.count);
  774. crypto_free_ahash(tfm);
  775. return err;
  776. }
  777. /* Please keep this list sorted by algorithm name. */
  778. static const struct alg_test_desc alg_test_descs[] = {
  779. {
  780. .alg = "cbc(aes)",
  781. .test = alg_test_cipher,
  782. .suite = {
  783. .cipher = {
  784. .enc = {
  785. .vecs = aes_cbc_enc_tv_template,
  786. .count = AES_CBC_ENC_TEST_VECTORS
  787. },
  788. .dec = {
  789. .vecs = aes_cbc_dec_tv_template,
  790. .count = AES_CBC_DEC_TEST_VECTORS
  791. }
  792. }
  793. }
  794. }, {
  795. .alg = "cbc(anubis)",
  796. .test = alg_test_cipher,
  797. .suite = {
  798. .cipher = {
  799. .enc = {
  800. .vecs = anubis_cbc_enc_tv_template,
  801. .count = ANUBIS_CBC_ENC_TEST_VECTORS
  802. },
  803. .dec = {
  804. .vecs = anubis_cbc_dec_tv_template,
  805. .count = ANUBIS_CBC_DEC_TEST_VECTORS
  806. }
  807. }
  808. }
  809. }, {
  810. .alg = "cbc(blowfish)",
  811. .test = alg_test_cipher,
  812. .suite = {
  813. .cipher = {
  814. .enc = {
  815. .vecs = bf_cbc_enc_tv_template,
  816. .count = BF_CBC_ENC_TEST_VECTORS
  817. },
  818. .dec = {
  819. .vecs = bf_cbc_dec_tv_template,
  820. .count = BF_CBC_DEC_TEST_VECTORS
  821. }
  822. }
  823. }
  824. }, {
  825. .alg = "cbc(camellia)",
  826. .test = alg_test_cipher,
  827. .suite = {
  828. .cipher = {
  829. .enc = {
  830. .vecs = camellia_cbc_enc_tv_template,
  831. .count = CAMELLIA_CBC_ENC_TEST_VECTORS
  832. },
  833. .dec = {
  834. .vecs = camellia_cbc_dec_tv_template,
  835. .count = CAMELLIA_CBC_DEC_TEST_VECTORS
  836. }
  837. }
  838. }
  839. }, {
  840. .alg = "cbc(des)",
  841. .test = alg_test_cipher,
  842. .suite = {
  843. .cipher = {
  844. .enc = {
  845. .vecs = des_cbc_enc_tv_template,
  846. .count = DES_CBC_ENC_TEST_VECTORS
  847. },
  848. .dec = {
  849. .vecs = des_cbc_dec_tv_template,
  850. .count = DES_CBC_DEC_TEST_VECTORS
  851. }
  852. }
  853. }
  854. }, {
  855. .alg = "cbc(des3_ede)",
  856. .test = alg_test_cipher,
  857. .suite = {
  858. .cipher = {
  859. .enc = {
  860. .vecs = des3_ede_cbc_enc_tv_template,
  861. .count = DES3_EDE_CBC_ENC_TEST_VECTORS
  862. },
  863. .dec = {
  864. .vecs = des3_ede_cbc_dec_tv_template,
  865. .count = DES3_EDE_CBC_DEC_TEST_VECTORS
  866. }
  867. }
  868. }
  869. }, {
  870. .alg = "cbc(twofish)",
  871. .test = alg_test_cipher,
  872. .suite = {
  873. .cipher = {
  874. .enc = {
  875. .vecs = tf_cbc_enc_tv_template,
  876. .count = TF_CBC_ENC_TEST_VECTORS
  877. },
  878. .dec = {
  879. .vecs = tf_cbc_dec_tv_template,
  880. .count = TF_CBC_DEC_TEST_VECTORS
  881. }
  882. }
  883. }
  884. }, {
  885. .alg = "ccm(aes)",
  886. .test = alg_test_aead,
  887. .suite = {
  888. .aead = {
  889. .enc = {
  890. .vecs = aes_ccm_enc_tv_template,
  891. .count = AES_CCM_ENC_TEST_VECTORS
  892. },
  893. .dec = {
  894. .vecs = aes_ccm_dec_tv_template,
  895. .count = AES_CCM_DEC_TEST_VECTORS
  896. }
  897. }
  898. }
  899. }, {
  900. .alg = "crc32c",
  901. .test = alg_test_hash,
  902. .suite = {
  903. .hash = {
  904. .vecs = crc32c_tv_template,
  905. .count = CRC32C_TEST_VECTORS
  906. }
  907. }
  908. }, {
  909. .alg = "cts(cbc(aes))",
  910. .test = alg_test_cipher,
  911. .suite = {
  912. .cipher = {
  913. .enc = {
  914. .vecs = cts_mode_enc_tv_template,
  915. .count = CTS_MODE_ENC_TEST_VECTORS
  916. },
  917. .dec = {
  918. .vecs = cts_mode_dec_tv_template,
  919. .count = CTS_MODE_DEC_TEST_VECTORS
  920. }
  921. }
  922. }
  923. }, {
  924. .alg = "deflate",
  925. .test = alg_test_comp,
  926. .suite = {
  927. .comp = {
  928. .comp = {
  929. .vecs = deflate_comp_tv_template,
  930. .count = DEFLATE_COMP_TEST_VECTORS
  931. },
  932. .decomp = {
  933. .vecs = deflate_decomp_tv_template,
  934. .count = DEFLATE_DECOMP_TEST_VECTORS
  935. }
  936. }
  937. }
  938. }, {
  939. .alg = "ecb(aes)",
  940. .test = alg_test_cipher,
  941. .suite = {
  942. .cipher = {
  943. .enc = {
  944. .vecs = aes_enc_tv_template,
  945. .count = AES_ENC_TEST_VECTORS
  946. },
  947. .dec = {
  948. .vecs = aes_dec_tv_template,
  949. .count = AES_DEC_TEST_VECTORS
  950. }
  951. }
  952. }
  953. }, {
  954. .alg = "ecb(anubis)",
  955. .test = alg_test_cipher,
  956. .suite = {
  957. .cipher = {
  958. .enc = {
  959. .vecs = anubis_enc_tv_template,
  960. .count = ANUBIS_ENC_TEST_VECTORS
  961. },
  962. .dec = {
  963. .vecs = anubis_dec_tv_template,
  964. .count = ANUBIS_DEC_TEST_VECTORS
  965. }
  966. }
  967. }
  968. }, {
  969. .alg = "ecb(arc4)",
  970. .test = alg_test_cipher,
  971. .suite = {
  972. .cipher = {
  973. .enc = {
  974. .vecs = arc4_enc_tv_template,
  975. .count = ARC4_ENC_TEST_VECTORS
  976. },
  977. .dec = {
  978. .vecs = arc4_dec_tv_template,
  979. .count = ARC4_DEC_TEST_VECTORS
  980. }
  981. }
  982. }
  983. }, {
  984. .alg = "ecb(blowfish)",
  985. .test = alg_test_cipher,
  986. .suite = {
  987. .cipher = {
  988. .enc = {
  989. .vecs = bf_enc_tv_template,
  990. .count = BF_ENC_TEST_VECTORS
  991. },
  992. .dec = {
  993. .vecs = bf_dec_tv_template,
  994. .count = BF_DEC_TEST_VECTORS
  995. }
  996. }
  997. }
  998. }, {
  999. .alg = "ecb(camellia)",
  1000. .test = alg_test_cipher,
  1001. .suite = {
  1002. .cipher = {
  1003. .enc = {
  1004. .vecs = camellia_enc_tv_template,
  1005. .count = CAMELLIA_ENC_TEST_VECTORS
  1006. },
  1007. .dec = {
  1008. .vecs = camellia_dec_tv_template,
  1009. .count = CAMELLIA_DEC_TEST_VECTORS
  1010. }
  1011. }
  1012. }
  1013. }, {
  1014. .alg = "ecb(cast5)",
  1015. .test = alg_test_cipher,
  1016. .suite = {
  1017. .cipher = {
  1018. .enc = {
  1019. .vecs = cast5_enc_tv_template,
  1020. .count = CAST5_ENC_TEST_VECTORS
  1021. },
  1022. .dec = {
  1023. .vecs = cast5_dec_tv_template,
  1024. .count = CAST5_DEC_TEST_VECTORS
  1025. }
  1026. }
  1027. }
  1028. }, {
  1029. .alg = "ecb(cast6)",
  1030. .test = alg_test_cipher,
  1031. .suite = {
  1032. .cipher = {
  1033. .enc = {
  1034. .vecs = cast6_enc_tv_template,
  1035. .count = CAST6_ENC_TEST_VECTORS
  1036. },
  1037. .dec = {
  1038. .vecs = cast6_dec_tv_template,
  1039. .count = CAST6_DEC_TEST_VECTORS
  1040. }
  1041. }
  1042. }
  1043. }, {
  1044. .alg = "ecb(des)",
  1045. .test = alg_test_cipher,
  1046. .suite = {
  1047. .cipher = {
  1048. .enc = {
  1049. .vecs = des_enc_tv_template,
  1050. .count = DES_ENC_TEST_VECTORS
  1051. },
  1052. .dec = {
  1053. .vecs = des_dec_tv_template,
  1054. .count = DES_DEC_TEST_VECTORS
  1055. }
  1056. }
  1057. }
  1058. }, {
  1059. .alg = "ecb(des3_ede)",
  1060. .test = alg_test_cipher,
  1061. .suite = {
  1062. .cipher = {
  1063. .enc = {
  1064. .vecs = des3_ede_enc_tv_template,
  1065. .count = DES3_EDE_ENC_TEST_VECTORS
  1066. },
  1067. .dec = {
  1068. .vecs = des3_ede_dec_tv_template,
  1069. .count = DES3_EDE_DEC_TEST_VECTORS
  1070. }
  1071. }
  1072. }
  1073. }, {
  1074. .alg = "ecb(khazad)",
  1075. .test = alg_test_cipher,
  1076. .suite = {
  1077. .cipher = {
  1078. .enc = {
  1079. .vecs = khazad_enc_tv_template,
  1080. .count = KHAZAD_ENC_TEST_VECTORS
  1081. },
  1082. .dec = {
  1083. .vecs = khazad_dec_tv_template,
  1084. .count = KHAZAD_DEC_TEST_VECTORS
  1085. }
  1086. }
  1087. }
  1088. }, {
  1089. .alg = "ecb(seed)",
  1090. .test = alg_test_cipher,
  1091. .suite = {
  1092. .cipher = {
  1093. .enc = {
  1094. .vecs = seed_enc_tv_template,
  1095. .count = SEED_ENC_TEST_VECTORS
  1096. },
  1097. .dec = {
  1098. .vecs = seed_dec_tv_template,
  1099. .count = SEED_DEC_TEST_VECTORS
  1100. }
  1101. }
  1102. }
  1103. }, {
  1104. .alg = "ecb(serpent)",
  1105. .test = alg_test_cipher,
  1106. .suite = {
  1107. .cipher = {
  1108. .enc = {
  1109. .vecs = serpent_enc_tv_template,
  1110. .count = SERPENT_ENC_TEST_VECTORS
  1111. },
  1112. .dec = {
  1113. .vecs = serpent_dec_tv_template,
  1114. .count = SERPENT_DEC_TEST_VECTORS
  1115. }
  1116. }
  1117. }
  1118. }, {
  1119. .alg = "ecb(tea)",
  1120. .test = alg_test_cipher,
  1121. .suite = {
  1122. .cipher = {
  1123. .enc = {
  1124. .vecs = tea_enc_tv_template,
  1125. .count = TEA_ENC_TEST_VECTORS
  1126. },
  1127. .dec = {
  1128. .vecs = tea_dec_tv_template,
  1129. .count = TEA_DEC_TEST_VECTORS
  1130. }
  1131. }
  1132. }
  1133. }, {
  1134. .alg = "ecb(tnepres)",
  1135. .test = alg_test_cipher,
  1136. .suite = {
  1137. .cipher = {
  1138. .enc = {
  1139. .vecs = tnepres_enc_tv_template,
  1140. .count = TNEPRES_ENC_TEST_VECTORS
  1141. },
  1142. .dec = {
  1143. .vecs = tnepres_dec_tv_template,
  1144. .count = TNEPRES_DEC_TEST_VECTORS
  1145. }
  1146. }
  1147. }
  1148. }, {
  1149. .alg = "ecb(twofish)",
  1150. .test = alg_test_cipher,
  1151. .suite = {
  1152. .cipher = {
  1153. .enc = {
  1154. .vecs = tf_enc_tv_template,
  1155. .count = TF_ENC_TEST_VECTORS
  1156. },
  1157. .dec = {
  1158. .vecs = tf_dec_tv_template,
  1159. .count = TF_DEC_TEST_VECTORS
  1160. }
  1161. }
  1162. }
  1163. }, {
  1164. .alg = "ecb(xeta)",
  1165. .test = alg_test_cipher,
  1166. .suite = {
  1167. .cipher = {
  1168. .enc = {
  1169. .vecs = xeta_enc_tv_template,
  1170. .count = XETA_ENC_TEST_VECTORS
  1171. },
  1172. .dec = {
  1173. .vecs = xeta_dec_tv_template,
  1174. .count = XETA_DEC_TEST_VECTORS
  1175. }
  1176. }
  1177. }
  1178. }, {
  1179. .alg = "ecb(xtea)",
  1180. .test = alg_test_cipher,
  1181. .suite = {
  1182. .cipher = {
  1183. .enc = {
  1184. .vecs = xtea_enc_tv_template,
  1185. .count = XTEA_ENC_TEST_VECTORS
  1186. },
  1187. .dec = {
  1188. .vecs = xtea_dec_tv_template,
  1189. .count = XTEA_DEC_TEST_VECTORS
  1190. }
  1191. }
  1192. }
  1193. }, {
  1194. .alg = "gcm(aes)",
  1195. .test = alg_test_aead,
  1196. .suite = {
  1197. .aead = {
  1198. .enc = {
  1199. .vecs = aes_gcm_enc_tv_template,
  1200. .count = AES_GCM_ENC_TEST_VECTORS
  1201. },
  1202. .dec = {
  1203. .vecs = aes_gcm_dec_tv_template,
  1204. .count = AES_GCM_DEC_TEST_VECTORS
  1205. }
  1206. }
  1207. }
  1208. }, {
  1209. .alg = "hmac(md5)",
  1210. .test = alg_test_hash,
  1211. .suite = {
  1212. .hash = {
  1213. .vecs = hmac_md5_tv_template,
  1214. .count = HMAC_MD5_TEST_VECTORS
  1215. }
  1216. }
  1217. }, {
  1218. .alg = "hmac(rmd128)",
  1219. .test = alg_test_hash,
  1220. .suite = {
  1221. .hash = {
  1222. .vecs = hmac_rmd128_tv_template,
  1223. .count = HMAC_RMD128_TEST_VECTORS
  1224. }
  1225. }
  1226. }, {
  1227. .alg = "hmac(rmd160)",
  1228. .test = alg_test_hash,
  1229. .suite = {
  1230. .hash = {
  1231. .vecs = hmac_rmd160_tv_template,
  1232. .count = HMAC_RMD160_TEST_VECTORS
  1233. }
  1234. }
  1235. }, {
  1236. .alg = "hmac(sha1)",
  1237. .test = alg_test_hash,
  1238. .suite = {
  1239. .hash = {
  1240. .vecs = hmac_sha1_tv_template,
  1241. .count = HMAC_SHA1_TEST_VECTORS
  1242. }
  1243. }
  1244. }, {
  1245. .alg = "hmac(sha224)",
  1246. .test = alg_test_hash,
  1247. .suite = {
  1248. .hash = {
  1249. .vecs = hmac_sha224_tv_template,
  1250. .count = HMAC_SHA224_TEST_VECTORS
  1251. }
  1252. }
  1253. }, {
  1254. .alg = "hmac(sha256)",
  1255. .test = alg_test_hash,
  1256. .suite = {
  1257. .hash = {
  1258. .vecs = hmac_sha256_tv_template,
  1259. .count = HMAC_SHA256_TEST_VECTORS
  1260. }
  1261. }
  1262. }, {
  1263. .alg = "hmac(sha384)",
  1264. .test = alg_test_hash,
  1265. .suite = {
  1266. .hash = {
  1267. .vecs = hmac_sha384_tv_template,
  1268. .count = HMAC_SHA384_TEST_VECTORS
  1269. }
  1270. }
  1271. }, {
  1272. .alg = "hmac(sha512)",
  1273. .test = alg_test_hash,
  1274. .suite = {
  1275. .hash = {
  1276. .vecs = hmac_sha512_tv_template,
  1277. .count = HMAC_SHA512_TEST_VECTORS
  1278. }
  1279. }
  1280. }, {
  1281. .alg = "lrw(aes)",
  1282. .test = alg_test_cipher,
  1283. .suite = {
  1284. .cipher = {
  1285. .enc = {
  1286. .vecs = aes_lrw_enc_tv_template,
  1287. .count = AES_LRW_ENC_TEST_VECTORS
  1288. },
  1289. .dec = {
  1290. .vecs = aes_lrw_dec_tv_template,
  1291. .count = AES_LRW_DEC_TEST_VECTORS
  1292. }
  1293. }
  1294. }
  1295. }, {
  1296. .alg = "lzo",
  1297. .test = alg_test_comp,
  1298. .suite = {
  1299. .comp = {
  1300. .comp = {
  1301. .vecs = lzo_comp_tv_template,
  1302. .count = LZO_COMP_TEST_VECTORS
  1303. },
  1304. .decomp = {
  1305. .vecs = lzo_decomp_tv_template,
  1306. .count = LZO_DECOMP_TEST_VECTORS
  1307. }
  1308. }
  1309. }
  1310. }, {
  1311. .alg = "md4",
  1312. .test = alg_test_hash,
  1313. .suite = {
  1314. .hash = {
  1315. .vecs = md4_tv_template,
  1316. .count = MD4_TEST_VECTORS
  1317. }
  1318. }
  1319. }, {
  1320. .alg = "md5",
  1321. .test = alg_test_hash,
  1322. .suite = {
  1323. .hash = {
  1324. .vecs = md5_tv_template,
  1325. .count = MD5_TEST_VECTORS
  1326. }
  1327. }
  1328. }, {
  1329. .alg = "michael_mic",
  1330. .test = alg_test_hash,
  1331. .suite = {
  1332. .hash = {
  1333. .vecs = michael_mic_tv_template,
  1334. .count = MICHAEL_MIC_TEST_VECTORS
  1335. }
  1336. }
  1337. }, {
  1338. .alg = "pcbc(fcrypt)",
  1339. .test = alg_test_cipher,
  1340. .suite = {
  1341. .cipher = {
  1342. .enc = {
  1343. .vecs = fcrypt_pcbc_enc_tv_template,
  1344. .count = FCRYPT_ENC_TEST_VECTORS
  1345. },
  1346. .dec = {
  1347. .vecs = fcrypt_pcbc_dec_tv_template,
  1348. .count = FCRYPT_DEC_TEST_VECTORS
  1349. }
  1350. }
  1351. }
  1352. }, {
  1353. .alg = "rfc3686(ctr(aes))",
  1354. .test = alg_test_cipher,
  1355. .suite = {
  1356. .cipher = {
  1357. .enc = {
  1358. .vecs = aes_ctr_enc_tv_template,
  1359. .count = AES_CTR_ENC_TEST_VECTORS
  1360. },
  1361. .dec = {
  1362. .vecs = aes_ctr_dec_tv_template,
  1363. .count = AES_CTR_DEC_TEST_VECTORS
  1364. }
  1365. }
  1366. }
  1367. }, {
  1368. .alg = "rmd128",
  1369. .test = alg_test_hash,
  1370. .suite = {
  1371. .hash = {
  1372. .vecs = rmd128_tv_template,
  1373. .count = RMD128_TEST_VECTORS
  1374. }
  1375. }
  1376. }, {
  1377. .alg = "rmd160",
  1378. .test = alg_test_hash,
  1379. .suite = {
  1380. .hash = {
  1381. .vecs = rmd160_tv_template,
  1382. .count = RMD160_TEST_VECTORS
  1383. }
  1384. }
  1385. }, {
  1386. .alg = "rmd256",
  1387. .test = alg_test_hash,
  1388. .suite = {
  1389. .hash = {
  1390. .vecs = rmd256_tv_template,
  1391. .count = RMD256_TEST_VECTORS
  1392. }
  1393. }
  1394. }, {
  1395. .alg = "rmd320",
  1396. .test = alg_test_hash,
  1397. .suite = {
  1398. .hash = {
  1399. .vecs = rmd320_tv_template,
  1400. .count = RMD320_TEST_VECTORS
  1401. }
  1402. }
  1403. }, {
  1404. .alg = "salsa20",
  1405. .test = alg_test_cipher,
  1406. .suite = {
  1407. .cipher = {
  1408. .enc = {
  1409. .vecs = salsa20_stream_enc_tv_template,
  1410. .count = SALSA20_STREAM_ENC_TEST_VECTORS
  1411. }
  1412. }
  1413. }
  1414. }, {
  1415. .alg = "sha1",
  1416. .test = alg_test_hash,
  1417. .suite = {
  1418. .hash = {
  1419. .vecs = sha1_tv_template,
  1420. .count = SHA1_TEST_VECTORS
  1421. }
  1422. }
  1423. }, {
  1424. .alg = "sha224",
  1425. .test = alg_test_hash,
  1426. .suite = {
  1427. .hash = {
  1428. .vecs = sha224_tv_template,
  1429. .count = SHA224_TEST_VECTORS
  1430. }
  1431. }
  1432. }, {
  1433. .alg = "sha256",
  1434. .test = alg_test_hash,
  1435. .suite = {
  1436. .hash = {
  1437. .vecs = sha256_tv_template,
  1438. .count = SHA256_TEST_VECTORS
  1439. }
  1440. }
  1441. }, {
  1442. .alg = "sha384",
  1443. .test = alg_test_hash,
  1444. .suite = {
  1445. .hash = {
  1446. .vecs = sha384_tv_template,
  1447. .count = SHA384_TEST_VECTORS
  1448. }
  1449. }
  1450. }, {
  1451. .alg = "sha512",
  1452. .test = alg_test_hash,
  1453. .suite = {
  1454. .hash = {
  1455. .vecs = sha512_tv_template,
  1456. .count = SHA512_TEST_VECTORS
  1457. }
  1458. }
  1459. }, {
  1460. .alg = "tgr128",
  1461. .test = alg_test_hash,
  1462. .suite = {
  1463. .hash = {
  1464. .vecs = tgr128_tv_template,
  1465. .count = TGR128_TEST_VECTORS
  1466. }
  1467. }
  1468. }, {
  1469. .alg = "tgr160",
  1470. .test = alg_test_hash,
  1471. .suite = {
  1472. .hash = {
  1473. .vecs = tgr160_tv_template,
  1474. .count = TGR160_TEST_VECTORS
  1475. }
  1476. }
  1477. }, {
  1478. .alg = "tgr192",
  1479. .test = alg_test_hash,
  1480. .suite = {
  1481. .hash = {
  1482. .vecs = tgr192_tv_template,
  1483. .count = TGR192_TEST_VECTORS
  1484. }
  1485. }
  1486. }, {
  1487. .alg = "wp256",
  1488. .test = alg_test_hash,
  1489. .suite = {
  1490. .hash = {
  1491. .vecs = wp256_tv_template,
  1492. .count = WP256_TEST_VECTORS
  1493. }
  1494. }
  1495. }, {
  1496. .alg = "wp384",
  1497. .test = alg_test_hash,
  1498. .suite = {
  1499. .hash = {
  1500. .vecs = wp384_tv_template,
  1501. .count = WP384_TEST_VECTORS
  1502. }
  1503. }
  1504. }, {
  1505. .alg = "wp512",
  1506. .test = alg_test_hash,
  1507. .suite = {
  1508. .hash = {
  1509. .vecs = wp512_tv_template,
  1510. .count = WP512_TEST_VECTORS
  1511. }
  1512. }
  1513. }, {
  1514. .alg = "xcbc(aes)",
  1515. .test = alg_test_hash,
  1516. .suite = {
  1517. .hash = {
  1518. .vecs = aes_xcbc128_tv_template,
  1519. .count = XCBC_AES_TEST_VECTORS
  1520. }
  1521. }
  1522. }, {
  1523. .alg = "xts(aes)",
  1524. .test = alg_test_cipher,
  1525. .suite = {
  1526. .cipher = {
  1527. .enc = {
  1528. .vecs = aes_xts_enc_tv_template,
  1529. .count = AES_XTS_ENC_TEST_VECTORS
  1530. },
  1531. .dec = {
  1532. .vecs = aes_xts_dec_tv_template,
  1533. .count = AES_XTS_DEC_TEST_VECTORS
  1534. }
  1535. }
  1536. }
  1537. }
  1538. };
  1539. int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
  1540. {
  1541. int start = 0;
  1542. int end = ARRAY_SIZE(alg_test_descs);
  1543. while (start < end) {
  1544. int i = (start + end) / 2;
  1545. int diff = strcmp(alg_test_descs[i].alg, alg);
  1546. if (diff > 0) {
  1547. end = i;
  1548. continue;
  1549. }
  1550. if (diff < 0) {
  1551. start = i + 1;
  1552. continue;
  1553. }
  1554. return alg_test_descs[i].test(alg_test_descs + i, driver,
  1555. type, mask);
  1556. }
  1557. printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
  1558. return 0;
  1559. }
  1560. EXPORT_SYMBOL_GPL(alg_test);
  1561. int __init testmgr_init(void)
  1562. {
  1563. int i;
  1564. for (i = 0; i < XBUFSIZE; i++) {
  1565. xbuf[i] = (void *)__get_free_page(GFP_KERNEL);
  1566. if (!xbuf[i])
  1567. goto err_free_xbuf;
  1568. }
  1569. for (i = 0; i < XBUFSIZE; i++) {
  1570. axbuf[i] = (void *)__get_free_page(GFP_KERNEL);
  1571. if (!axbuf[i])
  1572. goto err_free_axbuf;
  1573. }
  1574. return 0;
  1575. err_free_axbuf:
  1576. for (i = 0; i < XBUFSIZE && axbuf[i]; i++)
  1577. free_page((unsigned long)axbuf[i]);
  1578. err_free_xbuf:
  1579. for (i = 0; i < XBUFSIZE && xbuf[i]; i++)
  1580. free_page((unsigned long)xbuf[i]);
  1581. return -ENOMEM;
  1582. }
  1583. void testmgr_exit(void)
  1584. {
  1585. int i;
  1586. for (i = 0; i < XBUFSIZE; i++)
  1587. free_page((unsigned long)axbuf[i]);
  1588. for (i = 0; i < XBUFSIZE; i++)
  1589. free_page((unsigned long)xbuf[i]);
  1590. }