test.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. /* This is the worst code you have ever seen written on purpose.... this code is just a big hack to test
  2. out the functionality of the library */
  3. #ifdef SONY_PS2
  4. #include <eetypes.h>
  5. #include <eeregs.h>
  6. #include "timer.h"
  7. #endif
  8. #include <mycrypt.h>
  9. int errnum;
  10. int
  11. null_setup (const unsigned char *key, int keylen, int num_rounds,
  12. symmetric_key * skey)
  13. {
  14. return CRYPT_OK;
  15. }
  16. void
  17. null_ecb_encrypt (const unsigned char *pt, unsigned char *ct,
  18. symmetric_key * key)
  19. {
  20. memcpy (ct, pt, 8);
  21. }
  22. void
  23. null_ecb_decrypt (const unsigned char *ct, unsigned char *pt,
  24. symmetric_key * key)
  25. {
  26. memcpy (pt, ct, 8);
  27. }
  28. int
  29. null_test (void)
  30. {
  31. return CRYPT_OK;
  32. }
  33. int
  34. null_keysize (int *desired_keysize)
  35. {
  36. return CRYPT_OK;
  37. }
  38. const struct _cipher_descriptor null_desc = {
  39. "memcpy()",
  40. 255,
  41. 8, 8, 8, 1,
  42. &null_setup,
  43. &null_ecb_encrypt,
  44. &null_ecb_decrypt,
  45. &null_test,
  46. &null_keysize
  47. };
  48. prng_state prng;
  49. void
  50. store_tests (void)
  51. {
  52. unsigned char buf[8];
  53. unsigned long L;
  54. ulong64 LL;
  55. printf ("LOAD32/STORE32 tests\n");
  56. L = 0x12345678UL;
  57. STORE32L (L, &buf[0]);
  58. L = 0;
  59. LOAD32L (L, &buf[0]);
  60. if (L != 0x12345678UL) {
  61. printf ("LOAD/STORE32 Little don't work\n");
  62. exit (-1);
  63. }
  64. LL = CONST64 (0x01020304050607);
  65. STORE64L (LL, &buf[0]);
  66. LL = 0;
  67. LOAD64L (LL, &buf[0])
  68. if (LL != CONST64 (0x01020304050607)) {
  69. printf ("LOAD/STORE64 Little don't work\n");
  70. exit (-1);
  71. }
  72. L = 0x12345678UL;
  73. STORE32H (L, &buf[0]);
  74. L = 0;
  75. LOAD32H (L, &buf[0]);
  76. if (L != 0x12345678UL) {
  77. printf ("LOAD/STORE32 High don't work\n");
  78. exit (-1);
  79. }
  80. LL = CONST64 (0x01020304050607);
  81. STORE64H (LL, &buf[0]);
  82. LL = 0;
  83. LOAD64H (LL, &buf[0])
  84. if (LL != CONST64 (0x01020304050607)) {
  85. printf ("LOAD/STORE64 High don't work\n");
  86. exit (-1);
  87. }
  88. }
  89. void
  90. cipher_tests (void)
  91. {
  92. int x;
  93. printf ("Ciphers compiled in\n");
  94. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  95. printf
  96. (" %12s (%2d) Key Size: %4d to %4d, Block Size: %3d, Default # of rounds: %2d\n",
  97. cipher_descriptor[x].name, cipher_descriptor[x].ID,
  98. cipher_descriptor[x].min_key_length * 8,
  99. cipher_descriptor[x].max_key_length * 8,
  100. cipher_descriptor[x].block_length * 8,
  101. cipher_descriptor[x].default_rounds);
  102. }
  103. }
  104. void
  105. ecb_tests (void)
  106. {
  107. int x;
  108. printf ("ECB tests\n");
  109. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  110. printf (" %12s: ", cipher_descriptor[x].name);
  111. if ((errnum = cipher_descriptor[x].test ()) != CRYPT_OK) {
  112. printf (" **failed** Reason: %s\n", error_to_string (errnum));
  113. exit (-1);
  114. } else {
  115. printf ("passed\n");
  116. }
  117. }
  118. }
  119. #ifdef CBC
  120. void
  121. cbc_tests (void)
  122. {
  123. symmetric_CBC cbc;
  124. int x, y;
  125. unsigned char blk[32], ct[32], key[32], IV[32];
  126. const unsigned char test[] =
  127. { 0XFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  128. printf ("CBC tests\n");
  129. /* ---- CBC ENCODING ---- */
  130. /* make up a block and IV */
  131. for (x = 0; x < 32; x++)
  132. blk[x] = IV[x] = x;
  133. /* now lets start a cbc session */
  134. if ((errnum =
  135. cbc_start (find_cipher ("blowfish"), IV, key, 16, 0,
  136. &cbc)) != CRYPT_OK) {
  137. printf ("CBC Setup: %s\n", error_to_string (errnum));
  138. exit (-1);
  139. }
  140. /* now lets encode 32 bytes */
  141. for (x = 0; x < 4; x++) {
  142. if ((errnum = cbc_encrypt (blk + 8 * x, ct + 8 * x, &cbc)) != CRYPT_OK) {
  143. printf ("CBC encrypt: %s\n", error_to_string (errnum));
  144. exit (-1);
  145. }
  146. }
  147. zeromem (blk, sizeof (blk));
  148. /* ---- CBC DECODING ---- */
  149. /* make up a IV */
  150. for (x = 0; x < 32; x++)
  151. IV[x] = x;
  152. /* now lets start a cbc session */
  153. if ((errnum =
  154. cbc_start (find_cipher ("blowfish"), IV, key, 16, 0,
  155. &cbc)) != CRYPT_OK) {
  156. printf ("CBC Setup: %s\n", error_to_string (errnum));
  157. exit (-1);
  158. }
  159. /* now lets decode 32 bytes */
  160. for (x = 0; x < 4; x++) {
  161. if ((errnum = cbc_decrypt (ct + 8 * x, blk + 8 * x, &cbc)) != CRYPT_OK) {
  162. printf ("CBC decrypt: %s\n", error_to_string (errnum));
  163. exit (-1);
  164. }
  165. }
  166. /* print output */
  167. for (x = y = 0; x < 32; x++)
  168. if (blk[x] != x)
  169. y = 1;
  170. printf (" %s\n", y ? "failed" : "passed");
  171. /* lets actually check the bytes */
  172. memset (IV, 0, 8);
  173. IV[0] = 0xFF; /* IV = FF 00 00 00 00 00 00 00 */
  174. memset (blk, 0, 32);
  175. blk[8] = 0xFF; /* BLK = 00 00 00 00 00 00 00 00 FF 00 00 00 00 00 00 00 */
  176. cbc_start (find_cipher ("memcpy()"), IV, key, 8, 0, &cbc);
  177. cbc_encrypt (blk, ct, &cbc); /* expect: FF 00 00 00 00 00 00 00 */
  178. cbc_encrypt (blk + 8, ct + 8, &cbc); /* expect: 00 00 00 00 00 00 00 00 */
  179. if (memcmp (ct, test, 16)) {
  180. printf ("CBC failed logical testing.\n");
  181. for (x = 0; x < 16; x++)
  182. printf ("%02x ", ct[x]);
  183. printf ("\n");
  184. exit (-1);
  185. } else {
  186. printf ("CBC passed logical testing.\n");
  187. }
  188. }
  189. #else
  190. void
  191. cbc_tests (void)
  192. {
  193. printf ("CBC not compiled in\n");
  194. }
  195. #endif
  196. #ifdef OFB
  197. void
  198. ofb_tests (void)
  199. {
  200. symmetric_OFB ofb;
  201. int x, y;
  202. unsigned char blk[32], ct[32], key[32], IV[32];
  203. printf ("OFB tests\n");
  204. /* ---- ofb ENCODING ---- */
  205. /* make up a block and IV */
  206. for (x = 0; x < 32; x++)
  207. blk[x] = IV[x] = x;
  208. /* now lets start a ofb session */
  209. if ((errnum =
  210. ofb_start (find_cipher ("cast5"), IV, key, 16, 0, &ofb)) != CRYPT_OK) {
  211. printf ("OFB Setup: %s\n", error_to_string (errnum));
  212. exit (-1);
  213. }
  214. /* now lets encode 32 bytes */
  215. for (x = 0; x < 4; x++) {
  216. if ((errnum = ofb_encrypt (blk + 8 * x, ct + 8 * x, 8, &ofb)) != CRYPT_OK) {
  217. printf ("OFB encrypt: %s\n", error_to_string (errnum));
  218. exit (-1);
  219. }
  220. }
  221. zeromem (blk, sizeof (blk));
  222. /* ---- ofb DECODING ---- */
  223. /* make up a IV */
  224. for (x = 0; x < 32; x++)
  225. IV[x] = x;
  226. /* now lets start a ofb session */
  227. if ((errnum =
  228. ofb_start (find_cipher ("cast5"), IV, key, 16, 0, &ofb)) != CRYPT_OK) {
  229. printf ("OFB setup: %s\n", error_to_string (errnum));
  230. exit (-1);
  231. }
  232. /* now lets decode 32 bytes */
  233. for (x = 0; x < 4; x++) {
  234. if ((errnum = ofb_decrypt (ct + 8 * x, blk + 8 * x, 8, &ofb)) != CRYPT_OK) {
  235. printf ("OFB decrypt: %s\n", error_to_string (errnum));
  236. exit (-1);
  237. }
  238. }
  239. /* print output */
  240. for (x = y = 0; x < 32; x++)
  241. if (blk[x] != x)
  242. y = 1;
  243. printf (" %s\n", y ? "failed" : "passed");
  244. if (y)
  245. exit (-1);
  246. }
  247. #else
  248. void
  249. ofb_tests (void)
  250. {
  251. printf ("OFB not compiled in\n");
  252. }
  253. #endif
  254. #ifdef CFB
  255. void
  256. cfb_tests (void)
  257. {
  258. symmetric_CFB cfb;
  259. int x, y;
  260. unsigned char blk[32], ct[32], key[32], IV[32];
  261. printf ("CFB tests\n");
  262. /* ---- cfb ENCODING ---- */
  263. /* make up a block and IV */
  264. for (x = 0; x < 32; x++)
  265. blk[x] = IV[x] = x;
  266. /* now lets start a cfb session */
  267. if ((errnum =
  268. cfb_start (find_cipher ("blowfish"), IV, key, 16, 0,
  269. &cfb)) != CRYPT_OK) {
  270. printf ("CFB setup: %s\n", error_to_string (errnum));
  271. exit (-1);
  272. }
  273. /* now lets encode 32 bytes */
  274. for (x = 0; x < 4; x++) {
  275. if ((errnum = cfb_encrypt (blk + 8 * x, ct + 8 * x, 8, &cfb)) != CRYPT_OK) {
  276. printf ("CFB encrypt: %s\n", error_to_string (errnum));
  277. exit (-1);
  278. }
  279. }
  280. zeromem (blk, sizeof (blk));
  281. /* ---- cfb DECODING ---- */
  282. /* make up ahash_descriptor[prng->yarrow.hash].hashsize IV */
  283. for (x = 0; x < 32; x++)
  284. IV[x] = x;
  285. /* now lets start a cfb session */
  286. if ((errnum =
  287. cfb_start (find_cipher ("blowfish"), IV, key, 16, 0,
  288. &cfb)) != CRYPT_OK) {
  289. printf ("CFB Setup: %s\n", error_to_string (errnum));
  290. exit (-1);
  291. }
  292. /* now lets decode 32 bytes */
  293. for (x = 0; x < 4; x++) {
  294. if ((errnum = cfb_decrypt (ct + 8 * x, blk + 8 * x, 8, &cfb)) != CRYPT_OK) {
  295. printf ("CFB decrypt: %s\n", error_to_string (errnum));
  296. exit (-1);
  297. }
  298. }
  299. /* print output */
  300. for (x = y = 0; x < 32; x++)
  301. if (blk[x] != x)
  302. y = 1;
  303. printf (" %s\n", y ? "failed" : "passed");
  304. if (y)
  305. exit (-1);
  306. }
  307. #else
  308. void
  309. cfb_tests (void)
  310. {
  311. printf ("CFB not compiled in\n");
  312. }
  313. #endif
  314. #ifdef CTR
  315. void
  316. ctr_tests (void)
  317. {
  318. symmetric_CTR ctr;
  319. int x, y;
  320. unsigned char blk[32], ct[32], key[32], count[32];
  321. const unsigned char test[] =
  322. { 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0 };
  323. printf ("CTR tests\n");
  324. /* ---- CTR ENCODING ---- */
  325. /* make up a block and IV */
  326. for (x = 0; x < 32; x++)
  327. blk[x] = count[x] = x;
  328. /* now lets start a ctr session */
  329. if ((errnum =
  330. ctr_start (find_cipher ("xtea"), count, key, 16, 0,
  331. &ctr)) != CRYPT_OK) {
  332. printf ("CTR Setup: %s\n", error_to_string (errnum));
  333. exit (-1);
  334. }
  335. /* now lets encode 32 bytes */
  336. for (x = 0; x < 4; x++) {
  337. if ((errnum = ctr_encrypt (blk + 8 * x, ct + 8 * x, 8, &ctr)) != CRYPT_OK) {
  338. printf ("CTR encrypt: %s\n", error_to_string (errnum));
  339. exit (-1);
  340. }
  341. }
  342. zeromem (blk, sizeof (blk));
  343. /* ---- CTR DECODING ---- */
  344. /* make up a IV */
  345. for (x = 0; x < 32; x++)
  346. count[x] = x;
  347. /* now lets start a cbc session */
  348. if ((errnum =
  349. ctr_start (find_cipher ("xtea"), count, key, 16, 0,
  350. &ctr)) != CRYPT_OK) {
  351. printf ("CTR Setup: %s\n", error_to_string (errnum));
  352. exit (-1);
  353. }
  354. /* now lets decode 32 bytes */
  355. for (x = 0; x < 4; x++) {
  356. if ((errnum = ctr_decrypt (ct + 8 * x, blk + 8 * x, 8, &ctr)) != CRYPT_OK) {
  357. printf ("CTR decrypt: %s\n", error_to_string (errnum));
  358. exit (-1);
  359. }
  360. }
  361. /* print output */
  362. for (x = y = 0; x < 32; x++)
  363. if (blk[x] != x)
  364. y = 1;
  365. printf (" %s\n", y ? "failed" : "passed");
  366. if (y)
  367. exit (-1);
  368. /* lets actually check the bytes */
  369. memset (count, 0, 8);
  370. count[0] = 0xFF; /* IV = FF 00 00 00 00 00 00 00 */
  371. memset (blk, 0, 32);
  372. blk[9] = 2; /* BLK = 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 */
  373. ctr_start (find_cipher ("memcpy()"), count, key, 8, 0, &ctr);
  374. ctr_encrypt (blk, ct, 8, &ctr); /* expect: FF 00 00 00 00 00 00 00 */
  375. ctr_encrypt (blk + 8, ct + 8, 8, &ctr); /* expect: 00 03 00 00 00 00 00 00 */
  376. if (memcmp (ct, test, 16)) {
  377. printf ("CTR failed logical testing.\n");
  378. for (x = 0; x < 16; x++)
  379. printf ("%02x ", ct[x]);
  380. printf ("\n");
  381. } else {
  382. printf ("CTR passed logical testing.\n");
  383. }
  384. }
  385. #else
  386. void
  387. ctr_tests (void)
  388. {
  389. printf ("CTR not compiled in\n");
  390. }
  391. #endif
  392. void
  393. hash_tests (void)
  394. {
  395. int x;
  396. printf ("Hash tests\n");
  397. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  398. printf (" %10s (%2d) ", hash_descriptor[x].name, hash_descriptor[x].ID);
  399. if ((errnum = hash_descriptor[x].test ()) != CRYPT_OK) {
  400. printf ("**failed** Reason: %s\n", error_to_string (errnum));
  401. exit(-1);
  402. } else {
  403. printf ("passed\n");
  404. }
  405. }
  406. }
  407. #ifdef MRSA
  408. void
  409. pad_test (void)
  410. {
  411. unsigned char in[100], out[100];
  412. unsigned long x, y;
  413. /* make a dummy message */
  414. for (x = 0; x < 16; x++)
  415. in[x] = (unsigned char) x;
  416. /* pad the message so that random filler is placed before and after it */
  417. y = 100;
  418. if ((errnum =
  419. rsa_pad (in, 16, out, &y, find_prng ("yarrow"), &prng)) != CRYPT_OK) {
  420. printf ("Error: %s\n", error_to_string (errnum));
  421. exit (-1);
  422. }
  423. /* depad the message to get the original content */
  424. memset (in, 0, sizeof (in));
  425. x = 100;
  426. if ((errnum = rsa_depad (out, y, in, &x)) != CRYPT_OK) {
  427. printf ("Error: %s\n", error_to_string (errnum));
  428. exit (-1);
  429. }
  430. /* check outcome */
  431. printf ("rsa_pad: ");
  432. if (x != 16) {
  433. printf ("Failed. Wrong size.\n");
  434. exit (-1);
  435. }
  436. for (x = 0; x < 16; x++)
  437. if (in[x] != x) {
  438. printf ("Failed. Expected %02lx and got %02x.\n", x, in[x]);
  439. exit (-1);
  440. }
  441. printf ("passed.\n");
  442. }
  443. void
  444. rsa_test (void)
  445. {
  446. unsigned char in[4096], out[4096];
  447. unsigned long x, y, z, limit;
  448. int stat;
  449. rsa_key key;
  450. clock_t t;
  451. /* ---- SINGLE ENCRYPT ---- */
  452. /* encrypt a short 8 byte string */
  453. if ((errnum =
  454. rsa_make_key (&prng, find_prng ("yarrow"), 1024 / 8, 65537,
  455. &key)) != CRYPT_OK) {
  456. printf ("Error: %s\n", error_to_string (errnum));
  457. exit (-1);
  458. }
  459. for (x = 0; x < 8; x++)
  460. in[x] = (unsigned char) (x + 1);
  461. y = sizeof (in);
  462. if ((errnum = rsa_exptmod (in, 8, out, &y, PK_PUBLIC, &key)) != CRYPT_OK) {
  463. printf ("Error: %s\n", error_to_string (errnum));
  464. exit (-1);
  465. }
  466. /* decrypt it */
  467. zeromem (in, sizeof (in));
  468. x = sizeof (out);
  469. if ((errnum = rsa_exptmod (out, y, in, &x, PK_PRIVATE, &key)) != CRYPT_OK) {
  470. printf ("Error: %s\n", error_to_string (errnum));
  471. exit (-1);
  472. }
  473. /* compare */
  474. printf ("RSA : ");
  475. for (x = 0; x < 8; x++)
  476. if (in[x] != (x + 1)) {
  477. printf ("Failed. x==%02lx, in[%ld]==%02x\n", x, x, in[x]);
  478. exit (-1);
  479. }
  480. printf ("passed.\n");
  481. /* test the rsa_encrypt_key functions */
  482. for (x = 0; x < 16; x++)
  483. in[x] = x;
  484. y = sizeof (out);
  485. if ((errnum =
  486. rsa_encrypt_key (in, 16, out, &y, &prng, find_prng ("yarrow"),
  487. &key)) != CRYPT_OK) {
  488. printf ("Error: %s\n", error_to_string (errnum));
  489. exit (-1);
  490. }
  491. zeromem (in, sizeof (in));
  492. x = sizeof (in);
  493. if ((errnum = rsa_decrypt_key (out, y, in, &x, &key)) != CRYPT_OK) {
  494. printf ("Error: %s\n", error_to_string (errnum));
  495. exit (-1);
  496. }
  497. printf ("RSA en/de crypt key routines: ");
  498. if (x != 16) {
  499. printf ("Failed (length)\n");
  500. exit (-1);
  501. }
  502. for (x = 0; x < 16; x++)
  503. if (in[x] != x) {
  504. printf ("Failed (contents)\n");
  505. exit (-1);
  506. }
  507. printf ("Passed\n");
  508. /* test sign_hash functions */
  509. for (x = 0; x < 16; x++)
  510. in[x] = x;
  511. x = sizeof (in);
  512. if ((errnum = rsa_sign_hash (in, 16, out, &x, &key)) != CRYPT_OK) {
  513. printf ("Error: %s\n", error_to_string (errnum));
  514. exit (-1);
  515. }
  516. printf ("RSA signed hash: %lu bytes\n", x);
  517. if ((errnum = rsa_verify_hash (out, x, in, &stat, &key)) != CRYPT_OK) {
  518. printf ("Error: %s\n", error_to_string (errnum));
  519. exit (-1);
  520. }
  521. printf ("Verify hash: %s, ", stat ? "passed" : "failed");
  522. in[0] ^= 1;
  523. if ((errnum = rsa_verify_hash (out, x, in, &stat, &key)) != CRYPT_OK) {
  524. printf ("Error: %s\n", error_to_string (errnum));
  525. exit (-1);
  526. }
  527. printf ("%s\n", (!stat) ? "passed" : "failed");
  528. if (stat)
  529. exit (-1);
  530. rsa_free (&key);
  531. /* make a RSA key */
  532. #ifdef SONY_PS2_NOPE
  533. limit = 1024;
  534. #else
  535. limit = 2048;
  536. #endif
  537. {
  538. int tt;
  539. for (z = 1024; z <= limit; z += 512) {
  540. t = XCLOCK ();
  541. for (tt = 0; tt < 3; tt++) {
  542. if ((errnum =
  543. rsa_make_key (&prng, find_prng ("yarrow"), z / 8, 65537,
  544. &key)) != CRYPT_OK) {
  545. printf ("Error: %s\n", error_to_string (errnum));
  546. exit (-1);
  547. }
  548. if (tt < 2)
  549. rsa_free (&key);
  550. }
  551. t = XCLOCK () - t;
  552. printf ("Took %.0f ms to make a %ld-bit RSA key.\n",
  553. 1000.0 * (((double) t / 3.0) / (double) XCLOCKS_PER_SEC), z);
  554. /* time encryption */
  555. t = XCLOCK ();
  556. for (tt = 0; tt < 20; tt++) {
  557. y = sizeof (in);
  558. if ((errnum =
  559. rsa_exptmod (in, 8, out, &y, PK_PUBLIC, &key)) != CRYPT_OK) {
  560. printf ("Error: %s\n", error_to_string (errnum));
  561. exit (-1);
  562. }
  563. }
  564. t = XCLOCK () - t;
  565. printf ("Took %.0f ms to encrypt with a %ld-bit RSA key.\n",
  566. 1000.0 * (((double) t / 20.0) / (double) XCLOCKS_PER_SEC), z);
  567. /* time decryption */
  568. t = XCLOCK ();
  569. for (tt = 0; tt < 20; tt++) {
  570. x = sizeof (out);
  571. if ((errnum =
  572. rsa_exptmod (out, y, in, &x, PK_PRIVATE, &key)) != CRYPT_OK) {
  573. printf ("Error: %s\n", error_to_string (errnum));
  574. exit (-1);
  575. }
  576. }
  577. t = XCLOCK () - t;
  578. printf ("Took %.0f ms to decrypt with a %ld-bit RSA key.\n",
  579. 1000.0 * (((double) t / 20.0) / (double) XCLOCKS_PER_SEC), z);
  580. rsa_free (&key);
  581. }
  582. }
  583. }
  584. #else
  585. void
  586. pad_test (void)
  587. {
  588. printf ("MRSA not compiled in\n");
  589. }
  590. void
  591. rsa_test (void)
  592. {
  593. printf ("MRSA not compiled in\n");
  594. }
  595. #endif
  596. #ifdef BASE64
  597. void
  598. base64_test (void)
  599. {
  600. unsigned char buf[2][100];
  601. unsigned long x, y;
  602. printf ("Base64 tests\n");
  603. zeromem (buf, sizeof (buf));
  604. for (x = 0; x < 16; x++)
  605. buf[0][x] = (unsigned char) x;
  606. x = 100;
  607. if (base64_encode (buf[0], 16, buf[1], &x) != CRYPT_OK) {
  608. printf (" error: %s\n", error_to_string (errnum));
  609. exit (-1);
  610. }
  611. printf (" encoded 16 bytes to %ld bytes...[%s]\n", x, buf[1]);
  612. memset (buf[0], 0, 100);
  613. y = 100;
  614. if (base64_decode (buf[1], x, buf[0], &y) != CRYPT_OK) {
  615. printf (" error: %s\n", error_to_string (errnum));
  616. exit (-1);
  617. }
  618. printf (" decoded %ld bytes to %ld bytes\n", x, y);
  619. for (x = 0; x < 16; x++)
  620. if (buf[0][x] != x) {
  621. printf (" **failed**\n");
  622. exit (-1);
  623. }
  624. printf (" passed\n");
  625. }
  626. #else
  627. void
  628. base64_test (void)
  629. {
  630. printf ("Base64 not compiled in\n");
  631. }
  632. #endif
  633. void
  634. time_hash (void)
  635. {
  636. clock_t t1;
  637. int x, y;
  638. unsigned long z;
  639. unsigned char input[4096], out[MAXBLOCKSIZE];
  640. printf ("Hash Time Trials (4KB blocks):\n");
  641. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  642. t1 = XCLOCK ();
  643. z = sizeof (out);
  644. y = 0;
  645. while (XCLOCK () - t1 < (5 * XCLOCKS_PER_SEC)) {
  646. hash_memory (x, input, 4096, out, &z);
  647. hash_memory (x, input, 4096, out, &z);
  648. hash_memory (x, input, 4096, out, &z);
  649. hash_memory (x, input, 4096, out, &z);
  650. hash_memory (x, input, 4096, out, &z);
  651. hash_memory (x, input, 4096, out, &z);
  652. hash_memory (x, input, 4096, out, &z);
  653. hash_memory (x, input, 4096, out, &z);
  654. hash_memory (x, input, 4096, out, &z);
  655. hash_memory (x, input, 4096, out, &z);
  656. hash_memory (x, input, 4096, out, &z);
  657. hash_memory (x, input, 4096, out, &z);
  658. hash_memory (x, input, 4096, out, &z);
  659. hash_memory (x, input, 4096, out, &z);
  660. hash_memory (x, input, 4096, out, &z);
  661. hash_memory (x, input, 4096, out, &z);
  662. hash_memory (x, input, 4096, out, &z);
  663. hash_memory (x, input, 4096, out, &z);
  664. hash_memory (x, input, 4096, out, &z);
  665. hash_memory (x, input, 4096, out, &z);
  666. hash_memory (x, input, 4096, out, &z);
  667. hash_memory (x, input, 4096, out, &z);
  668. hash_memory (x, input, 4096, out, &z);
  669. hash_memory (x, input, 4096, out, &z);
  670. hash_memory (x, input, 4096, out, &z);
  671. hash_memory (x, input, 4096, out, &z);
  672. hash_memory (x, input, 4096, out, &z);
  673. hash_memory (x, input, 4096, out, &z);
  674. hash_memory (x, input, 4096, out, &z);
  675. hash_memory (x, input, 4096, out, &z);
  676. hash_memory (x, input, 4096, out, &z);
  677. hash_memory (x, input, 4096, out, &z);
  678. y += 32;
  679. }
  680. t1 = XCLOCK () - t1;
  681. printf ("%-20s: Hash at %5.2f Mbit/sec\n", hash_descriptor[x].name,
  682. ((8.0 * 4096.0) *
  683. ((double) y / ((double) t1 / (double) XCLOCKS_PER_SEC))) /
  684. 1000000.0);
  685. }
  686. }
  687. void
  688. time_ecb (void)
  689. {
  690. clock_t t1, t2;
  691. long x, y1, y2;
  692. unsigned char pt[32], key[32];
  693. symmetric_key skey;
  694. void (*func) (const unsigned char *, unsigned char *, symmetric_key *);
  695. printf ("ECB Time Trials for the Symmetric Ciphers:\n");
  696. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  697. cipher_descriptor[x].setup (key, cipher_descriptor[x].min_key_length, 0,
  698. &skey);
  699. #define DO1 func(pt,pt,&skey);
  700. #define DO2 DO1 DO1
  701. #define DO4 DO2 DO2
  702. #define DO8 DO4 DO4
  703. #define DO16 DO8 DO8
  704. #define DO32 DO16 DO16
  705. #define DO64 DO32 DO32
  706. #define DO128 DO64 DO64
  707. #define DO256 DO128 DO128
  708. func = cipher_descriptor[x].ecb_encrypt;
  709. y1 = 0;
  710. t1 = XCLOCK ();
  711. while (XCLOCK () - t1 < 3 * XCLOCKS_PER_SEC) {
  712. DO256;
  713. y1 += 256;
  714. }
  715. t1 = XCLOCK () - t1;
  716. func = cipher_descriptor[x].ecb_decrypt;
  717. y2 = 0;
  718. t2 = XCLOCK ();
  719. while (XCLOCK () - t2 < 3 * XCLOCKS_PER_SEC) {
  720. DO256;
  721. y2 += 256;
  722. }
  723. t2 = XCLOCK () - t2;
  724. printf
  725. ("%-20s: Encrypt at %5.2f Mbit/sec and Decrypt at %5.2f Mbit/sec\n",
  726. cipher_descriptor[x].name,
  727. ((8.0 * (double) cipher_descriptor[x].block_length) *
  728. ((double) y1 / ((double) t1 / (double) XCLOCKS_PER_SEC))) / 1000000.0,
  729. ((8.0 * (double) cipher_descriptor[x].block_length) *
  730. ((double) y2 / ((double) t2 / (double) XCLOCKS_PER_SEC))) /
  731. 1000000.0);
  732. #undef DO256
  733. #undef DO128
  734. #undef DO64
  735. #undef DO32
  736. #undef DO16
  737. #undef DO8
  738. #undef DO4
  739. #undef DO2
  740. #undef DO1
  741. }
  742. }
  743. #ifdef MDH
  744. void
  745. dh_tests (void)
  746. {
  747. unsigned char buf[3][4096];
  748. unsigned long x, y, z;
  749. int low, high, stat, stat2;
  750. dh_key usera, userb;
  751. clock_t t1;
  752. /* if ((errnum = dh_test()) != CRYPT_OK) printf("DH Error: %s\n", error_to_string(errnum)); */
  753. dh_sizes (&low, &high);
  754. printf ("DH Keys from %d to %d supported.\n", low * 8, high * 8);
  755. /* make up two keys */
  756. if ((errnum =
  757. dh_make_key (&prng, find_prng ("yarrow"), 96, &usera)) != CRYPT_OK) {
  758. printf ("Error: %s\n", error_to_string (errnum));
  759. exit (-1);
  760. }
  761. if ((errnum =
  762. dh_make_key (&prng, find_prng ("yarrow"), 96, &userb)) != CRYPT_OK) {
  763. printf ("Error: %s\n", error_to_string (errnum));
  764. exit (-1);
  765. }
  766. /* make the shared secret */
  767. x = 4096;
  768. if ((errnum = dh_shared_secret (&usera, &userb, buf[0], &x)) != CRYPT_OK) {
  769. printf ("Error: %s\n", error_to_string (errnum));
  770. exit (-1);
  771. }
  772. y = 4096;
  773. if ((errnum = dh_shared_secret (&userb, &usera, buf[1], &y)) != CRYPT_OK) {
  774. printf ("Error: %s\n", error_to_string (errnum));
  775. exit (-1);
  776. }
  777. if (y != x) {
  778. printf ("DH Shared keys are not same size.\n");
  779. exit (-1);
  780. }
  781. if (memcmp (buf[0], buf[1], x)) {
  782. printf ("DH Shared keys not same contents.\n");
  783. exit (-1);
  784. }
  785. /* now export userb */
  786. y = 4096;
  787. if ((errnum = dh_export (buf[1], &y, PK_PUBLIC, &userb)) != CRYPT_OK) {
  788. printf ("Error: %s\n", error_to_string (errnum));
  789. exit (-1);
  790. }
  791. dh_free (&userb);
  792. /* import and make the shared secret again */
  793. if ((errnum = dh_import (buf[1], y, &userb)) != CRYPT_OK) {
  794. printf ("Error: %s\n", error_to_string (errnum));
  795. exit (-1);
  796. }
  797. z = 4096;
  798. if ((errnum = dh_shared_secret (&usera, &userb, buf[2], &z)) != CRYPT_OK) {
  799. printf ("Error: %s\n", error_to_string (errnum));
  800. exit (-1);
  801. }
  802. printf ("DH routines: ");
  803. if (z != x) {
  804. printf ("failed. Size don't match?\n");
  805. exit (-1);
  806. }
  807. if (memcmp (buf[0], buf[2], x)) {
  808. printf ("Failed. Content didn't match.\n");
  809. exit (-1);
  810. }
  811. printf ("Passed\n");
  812. dh_free (&usera);
  813. dh_free (&userb);
  814. /* time stuff */
  815. {
  816. static int sizes[] = { 96, 128, 160, 192, 224, 256, 320, 384, 512 };
  817. int ii, tt;
  818. for (ii = 0; ii < (int) (sizeof (sizes) / sizeof (sizes[0])); ii++) {
  819. t1 = XCLOCK ();
  820. for (tt = 0; tt < 5; tt++) {
  821. dh_make_key (&prng, find_prng ("yarrow"), sizes[ii], &usera);
  822. dh_free (&usera);
  823. }
  824. t1 = XCLOCK () - t1;
  825. printf ("Make dh-%d key took %f msec\n", sizes[ii] * 8,
  826. 1000.0 * (((double) t1 / 5.0) / (double) XCLOCKS_PER_SEC));
  827. }
  828. }
  829. /* test encrypt_key */
  830. dh_make_key (&prng, find_prng ("yarrow"), 96, &usera);
  831. for (x = 0; x < 16; x++)
  832. buf[0][x] = x;
  833. y = sizeof (buf[1]);
  834. if ((errnum =
  835. dh_encrypt_key (buf[0], 16, buf[1], &y, &prng, find_prng ("yarrow"),
  836. find_hash ("md5"), &usera)) != CRYPT_OK) {
  837. printf ("Error: %s\n", error_to_string (errnum));
  838. exit (-1);
  839. }
  840. zeromem (buf[0], sizeof (buf[0]));
  841. x = sizeof (buf[0]);
  842. if ((errnum = dh_decrypt_key (buf[1], y, buf[0], &x, &usera)) != CRYPT_OK) {
  843. printf ("Error: %s\n", error_to_string (errnum));
  844. exit (-1);
  845. }
  846. printf ("DH en/de crypt key routines: ");
  847. if (x != 16) {
  848. printf ("Failed (length)\n");
  849. exit (-1);
  850. }
  851. for (x = 0; x < 16; x++)
  852. if (buf[0][x] != x) {
  853. printf ("Failed (contents)\n");
  854. exit (-1);
  855. }
  856. printf ("Passed (size %lu)\n", y);
  857. /* test sign_hash */
  858. for (x = 0; x < 16; x++)
  859. buf[0][x] = x;
  860. x = sizeof (buf[1]);
  861. if ((errnum =
  862. dh_sign_hash (buf[0], 16, buf[1], &x, &prng, find_prng ("yarrow"),
  863. &usera)) != CRYPT_OK) {
  864. printf ("Error: %s\n", error_to_string (errnum));
  865. exit (-1);
  866. }
  867. if (dh_verify_hash (buf[1], x, buf[0], 16, &stat, &usera)) {
  868. printf ("Error: %s\n", error_to_string (errnum));
  869. exit (-1);
  870. }
  871. buf[0][0] ^= 1;
  872. if (dh_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera)) {
  873. printf ("Error: %s\n", error_to_string (errnum));
  874. exit (-1);
  875. }
  876. printf ("dh_sign/verify_hash: %s (%d,%d), %lu\n",
  877. ((stat == 1)
  878. && (stat2 == 0)) ? "passed" : "failed", stat, stat2, x);
  879. dh_free (&usera);
  880. }
  881. #else
  882. void
  883. dh_tests (void)
  884. {
  885. printf ("MDH not compiled in\n");
  886. }
  887. #endif
  888. int callback_x = 0;
  889. void
  890. callback (void)
  891. {
  892. printf ("%c\x08", "-\\|/"[++callback_x & 3]);
  893. #ifndef SONY_PS2
  894. fflush (stdout);
  895. #endif
  896. }
  897. void
  898. rng_tests (void)
  899. {
  900. unsigned char buf[16];
  901. clock_t t1;
  902. int x, y;
  903. printf ("RNG tests\n");
  904. t1 = XCLOCK ();
  905. x = rng_get_bytes (buf, sizeof (buf), &callback);
  906. t1 = XCLOCK () - t1;
  907. printf (" %f bytes per second...",
  908. (double) x / ((double) t1 / (double) XCLOCKS_PER_SEC));
  909. printf ("read %d bytes.\n ", x);
  910. for (y = 0; y < x; y++)
  911. printf ("%02x ", buf[y]);
  912. printf ("\n");
  913. #ifdef YARROW
  914. if ((errnum =
  915. rng_make_prng (128, find_prng ("yarrow"), &prng,
  916. &callback)) != CRYPT_OK) {
  917. printf (" starting yarrow error: %s\n", error_to_string (errnum));
  918. exit (-1);
  919. }
  920. #endif
  921. }
  922. #ifdef MECC
  923. void
  924. ecc_tests (void)
  925. {
  926. unsigned char buf[4][4096];
  927. unsigned long x, y, z;
  928. int stat, stat2, low, high;
  929. ecc_key usera, userb;
  930. clock_t t1;
  931. if ((errnum = ecc_test ()) != CRYPT_OK) {
  932. printf ("ecc Error: %s\n", error_to_string (errnum));
  933. exit (-1);
  934. }
  935. ecc_sizes (&low, &high);
  936. printf ("ecc Keys from %d to %d supported.\n", low * 8, high * 8);
  937. /* make up two keys */
  938. if ((errnum =
  939. ecc_make_key (&prng, find_prng ("yarrow"), 24, &usera)) != CRYPT_OK) {
  940. printf ("Error: %s\n", error_to_string (errnum));
  941. exit (-1);
  942. }
  943. if ((errnum =
  944. ecc_make_key (&prng, find_prng ("yarrow"), 24, &userb)) != CRYPT_OK) {
  945. printf ("Error: %s\n", error_to_string (errnum));
  946. exit (-1);
  947. }
  948. /* make the shared secret */
  949. x = 4096;
  950. if ((errnum = ecc_shared_secret (&usera, &userb, buf[0], &x)) != CRYPT_OK) {
  951. printf ("Error: %s\n", error_to_string (errnum));
  952. exit (-1);
  953. }
  954. y = 4096;
  955. if ((errnum = ecc_shared_secret (&userb, &usera, buf[1], &y)) != CRYPT_OK) {
  956. printf ("Error: %s\n", error_to_string (errnum));
  957. exit (-1);
  958. }
  959. if (y != x) {
  960. printf ("ecc Shared keys are not same size.\n");
  961. exit (-1);
  962. }
  963. if (memcmp (buf[0], buf[1], x)) {
  964. printf ("ecc Shared keys not same contents.\n");
  965. exit (-1);
  966. }
  967. /* now export userb */
  968. y = 4096;
  969. if ((errnum = ecc_export (buf[1], &y, PK_PUBLIC, &userb)) != CRYPT_OK) {
  970. printf ("Error: %s\n", error_to_string (errnum));
  971. exit (-1);
  972. }
  973. ecc_free (&userb);
  974. printf ("ECC-192 export took %ld bytes\n", y);
  975. /* import and make the shared secret again */
  976. if ((errnum = ecc_import (buf[1], y, &userb)) != CRYPT_OK) {
  977. printf ("Error: %s\n", error_to_string (errnum));
  978. exit (-1);
  979. }
  980. z = 4096;
  981. if ((errnum = ecc_shared_secret (&usera, &userb, buf[2], &z)) != CRYPT_OK) {
  982. printf ("Error: %s\n", error_to_string (errnum));
  983. exit (-1);
  984. }
  985. printf ("ecc routines: ");
  986. if (z != x) {
  987. printf ("failed. Size don't match?\n");
  988. exit (-1);
  989. }
  990. if (memcmp (buf[0], buf[2], x)) {
  991. printf ("Failed. Content didn't match.\n");
  992. exit (-1);
  993. }
  994. printf ("Passed\n");
  995. ecc_free (&usera);
  996. ecc_free (&userb);
  997. /* time stuff */
  998. {
  999. static int sizes[] = { 20, 24, 28, 32, 48, 65 };
  1000. int ii, tt;
  1001. for (ii = 0; ii < (int) (sizeof (sizes) / sizeof (sizes[0])); ii++) {
  1002. t1 = XCLOCK ();
  1003. for (tt = 0; tt < 10; tt++) {
  1004. if ((errnum =
  1005. ecc_make_key (&prng, find_prng ("yarrow"), sizes[ii],
  1006. &usera)) != CRYPT_OK) {
  1007. printf ("Error: %s\n", error_to_string (errnum));
  1008. exit (-1);
  1009. }
  1010. ecc_free (&usera);
  1011. }
  1012. t1 = XCLOCK () - t1;
  1013. printf ("Make ECC-%d key took %f msec\n", sizes[ii] * 8,
  1014. 1000.0 * (((double) t1 / 10.0) / (double) XCLOCKS_PER_SEC));
  1015. }
  1016. }
  1017. /* test encrypt_key */
  1018. ecc_make_key (&prng, find_prng ("yarrow"), 20, &usera);
  1019. for (x = 0; x < 32; x++)
  1020. buf[0][x] = x;
  1021. y = sizeof (buf[1]);
  1022. if ((errnum =
  1023. ecc_encrypt_key (buf[0], 32, buf[1], &y, &prng, find_prng ("yarrow"),
  1024. find_hash ("sha256"), &usera)) != CRYPT_OK) {
  1025. printf ("Error: %s\n", error_to_string (errnum));
  1026. exit (-1);
  1027. }
  1028. zeromem (buf[0], sizeof (buf[0]));
  1029. x = sizeof (buf[0]);
  1030. if ((errnum = ecc_decrypt_key (buf[1], y, buf[0], &x, &usera)) != CRYPT_OK) {
  1031. printf ("Error: %s\n", error_to_string (errnum));
  1032. exit (-1);
  1033. }
  1034. printf ("ECC en/de crypt key routines: ");
  1035. if (x != 32) {
  1036. printf ("Failed (length)\n");
  1037. exit (-1);
  1038. }
  1039. for (x = 0; x < 32; x++)
  1040. if (buf[0][x] != x) {
  1041. printf ("Failed (contents)\n");
  1042. exit (-1);
  1043. }
  1044. printf ("Passed (size: %lu)\n", y);
  1045. /* test sign_hash */
  1046. for (x = 0; x < 16; x++)
  1047. buf[0][x] = x;
  1048. x = sizeof (buf[1]);
  1049. if ((errnum =
  1050. ecc_sign_hash (buf[0], 16, buf[1], &x, &prng, find_prng ("yarrow"),
  1051. &usera)) != CRYPT_OK) {
  1052. printf ("Error: %s\n", error_to_string (errnum));
  1053. exit (-1);
  1054. }
  1055. printf("Signature size: %lu\n", x);
  1056. if (ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &usera)) {
  1057. printf ("Error: %s\n", error_to_string (errnum));
  1058. exit (-1);
  1059. }
  1060. buf[0][0] ^= 1;
  1061. if (ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera)) {
  1062. printf ("Error: %s\n", error_to_string (errnum));
  1063. exit (-1);
  1064. }
  1065. printf ("ecc_sign/verify_hash: %s (%d,%d)\n",
  1066. ((stat == 1) && (stat2 == 0)) ? "passed" : "failed", stat, stat2);
  1067. ecc_free (&usera);
  1068. }
  1069. #else
  1070. void
  1071. ecc_tests (void)
  1072. {
  1073. printf ("MECC not compiled in\n");
  1074. }
  1075. #endif
  1076. #ifdef GF
  1077. void
  1078. gf_tests (void)
  1079. {
  1080. gf_int a, b, c, d;
  1081. int n;
  1082. unsigned char buf[1024];
  1083. printf ("GF tests\n");
  1084. gf_zero (a);
  1085. gf_zero (b);
  1086. gf_zero (c);
  1087. gf_zero (d);
  1088. /* a == 0x18000000b */
  1089. a[1] = 1;
  1090. a[0] = 0x8000000bUL;
  1091. /* b == 0x012345678 */
  1092. b[0] = 0x012345678UL;
  1093. /* find 1/b mod a */
  1094. gf_invmod (b, a, c);
  1095. /* find 1/1/b mod a */
  1096. gf_invmod (c, a, d);
  1097. /* display them */
  1098. printf (" %08lx %08lx\n", c[0], d[0]);
  1099. /* store as binary string */
  1100. n = gf_size (a);
  1101. printf (" a takes %d bytes\n", n);
  1102. gf_toraw (a, buf);
  1103. gf_readraw (a, buf, n);
  1104. printf (" a == %08lx%08lx\n", a[1], a[0]);
  1105. /* primality testing */
  1106. gf_zero (a);
  1107. a[0] = 0x169;
  1108. printf (" GF prime: %s, ", gf_is_prime (a) ? "passed" : "failed");
  1109. a[0] = 0x168;
  1110. printf (" %s\n", gf_is_prime (a) ? "failed" : "passed");
  1111. /* test sqrt code */
  1112. gf_zero (a);
  1113. a[1] = 0x00000001;
  1114. a[0] = 0x8000000bUL;
  1115. gf_zero (b);
  1116. b[0] = 0x12345678UL;
  1117. gf_sqrt (b, a, c);
  1118. gf_mulmod (c, c, a, b);
  1119. printf (" (%08lx)^2 = %08lx (mod %08lx%08lx) \n", c[0], b[0], a[1], a[0]);
  1120. }
  1121. #else
  1122. void
  1123. gf_tests (void)
  1124. {
  1125. printf ("GF not compiled in\n");
  1126. }
  1127. #endif
  1128. #ifdef MPI
  1129. void
  1130. test_prime (void)
  1131. {
  1132. char buf[1024];
  1133. mp_int a;
  1134. int x;
  1135. /* make a 1024 bit prime */
  1136. mp_init (&a);
  1137. rand_prime (&a, 128, &prng, find_prng ("yarrow"));
  1138. /* dump it */
  1139. mp_todecimal (&a, buf);
  1140. printf ("1024-bit prime:\n");
  1141. for (x = 0; x < (int) strlen (buf);) {
  1142. printf ("%c", buf[x]);
  1143. if (!(++x % 60))
  1144. printf ("\\ \n");
  1145. }
  1146. printf ("\n\n");
  1147. mp_clear (&a);
  1148. }
  1149. #else
  1150. void
  1151. test_prime (void)
  1152. {
  1153. printf ("MPI not compiled in\n");
  1154. }
  1155. #endif
  1156. void
  1157. register_all_algs (void)
  1158. {
  1159. #ifdef RIJNDAEL
  1160. register_cipher (&aes_desc);
  1161. #endif
  1162. #ifdef BLOWFISH
  1163. register_cipher (&blowfish_desc);
  1164. #endif
  1165. #ifdef XTEA
  1166. register_cipher (&xtea_desc);
  1167. #endif
  1168. #ifdef RC5
  1169. register_cipher (&rc5_desc);
  1170. #endif
  1171. #ifdef RC6
  1172. register_cipher (&rc6_desc);
  1173. #endif
  1174. #ifdef SAFERP
  1175. register_cipher (&saferp_desc);
  1176. #endif
  1177. #ifdef TWOFISH
  1178. register_cipher (&twofish_desc);
  1179. #endif
  1180. #ifdef SAFER
  1181. register_cipher (&safer_k64_desc);
  1182. register_cipher (&safer_sk64_desc);
  1183. register_cipher (&safer_k128_desc);
  1184. register_cipher (&safer_sk128_desc);
  1185. #endif
  1186. #ifdef RC2
  1187. register_cipher (&rc2_desc);
  1188. #endif
  1189. #ifdef DES
  1190. register_cipher (&des_desc);
  1191. register_cipher (&des3_desc);
  1192. #endif
  1193. #ifdef CAST5
  1194. register_cipher (&cast5_desc);
  1195. #endif
  1196. #ifdef NOEKEON
  1197. register_cipher (&noekeon_desc);
  1198. #endif
  1199. register_cipher (&null_desc);
  1200. #ifdef TIGER
  1201. register_hash (&tiger_desc);
  1202. #endif
  1203. #ifdef MD2
  1204. register_hash (&md2_desc);
  1205. #endif
  1206. #ifdef MD4
  1207. register_hash (&md4_desc);
  1208. #endif
  1209. #ifdef MD5
  1210. register_hash (&md5_desc);
  1211. #endif
  1212. #ifdef SHA1
  1213. register_hash (&sha1_desc);
  1214. #endif
  1215. #ifdef SHA256
  1216. register_hash (&sha256_desc);
  1217. #endif
  1218. #ifdef SHA384
  1219. register_hash (&sha384_desc);
  1220. #endif
  1221. #ifdef SHA512
  1222. register_hash (&sha512_desc);
  1223. #endif
  1224. #ifdef YARROW
  1225. register_prng (&yarrow_desc);
  1226. #endif
  1227. #ifdef SPRNG
  1228. register_prng (&sprng_desc);
  1229. #endif
  1230. }
  1231. #ifdef KR
  1232. void
  1233. kr_display (pk_key * kr)
  1234. {
  1235. static const char *sys[] = { "NON-KEY", "RSA", "DH", "ECC" };
  1236. static const char *type[] = { "PRIVATE", "PUBLIC", "PRIVATE_OPTIMIZED" };
  1237. while (kr->system != NON_KEY) {
  1238. printf ("CRC [%08lx], System [%10s], Type [%20s], %s, %s, %s\n", kr->ID,
  1239. sys[kr->system], type[kr->key_type], kr->name, kr->email,
  1240. kr->description);
  1241. kr = kr->next;
  1242. }
  1243. printf ("\n");
  1244. }
  1245. void
  1246. kr_test_makekeys (pk_key ** kr)
  1247. {
  1248. if ((errnum = kr_init (kr)) != CRYPT_OK) {
  1249. printf ("KR init error %s\n", error_to_string (errnum));
  1250. exit (-1);
  1251. }
  1252. /* make a DH key */
  1253. printf ("KR: Making DH key...\n");
  1254. if ((errnum =
  1255. kr_make_key (*kr, &prng, find_prng ("yarrow"), DH_KEY, 128, "dhkey",
  1256. "[email protected]", "dhkey one")) != CRYPT_OK) {
  1257. printf ("Make key error: %s\n", error_to_string (errnum));
  1258. exit (-1);
  1259. }
  1260. /* make a ECC key */
  1261. printf ("KR: Making ECC key...\n");
  1262. if ((errnum =
  1263. kr_make_key (*kr, &prng, find_prng ("yarrow"), ECC_KEY, 20, "ecckey",
  1264. "[email protected]", "ecckey one")) != CRYPT_OK) {
  1265. printf ("Make key error: %s\n", error_to_string (errnum));
  1266. exit (-1);
  1267. }
  1268. /* make a RSA key */
  1269. printf ("KR: Making RSA key...\n");
  1270. if ((errnum =
  1271. kr_make_key (*kr, &prng, find_prng ("yarrow"), RSA_KEY, 128, "rsakey",
  1272. "[email protected]", "rsakey one")) != CRYPT_OK) {
  1273. printf ("Make key error: %s\n", error_to_string (errnum));
  1274. exit (-1);
  1275. }
  1276. }
  1277. void
  1278. kr_test (void)
  1279. {
  1280. pk_key *kr, *_kr;
  1281. unsigned char buf[8192], buf2[8192], buf3[8192];
  1282. unsigned long len;
  1283. int i, j, stat;
  1284. #ifndef NO_FILE
  1285. FILE *f;
  1286. #endif
  1287. kr_test_makekeys (&kr);
  1288. printf ("The original list:\n");
  1289. kr_display (kr);
  1290. for (i = 0; i < 3; i++) {
  1291. len = sizeof (buf);
  1292. if ((errnum = kr_export (kr, kr->ID, kr->key_type, buf, &len)) != CRYPT_OK) {
  1293. printf ("Error exporting key %d, %s\n", i, error_to_string (errnum));
  1294. exit (-1);
  1295. }
  1296. printf ("Exported key was: %lu bytes\n", len);
  1297. if ((errnum = kr_del (&kr, kr->ID)) != CRYPT_OK) {
  1298. printf ("Error deleting key %d, %s\n", i, error_to_string (errnum));
  1299. exit (-1);
  1300. }
  1301. kr_display (kr);
  1302. if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
  1303. printf ("Error importing key %d, %s\n", i, error_to_string (errnum));
  1304. exit (-1);
  1305. }
  1306. kr_display (kr);
  1307. }
  1308. for (i = 0; i < 3; i++) {
  1309. len = sizeof (buf);
  1310. if ((errnum = kr_export (kr, kr->ID, PK_PUBLIC, buf, &len)) != CRYPT_OK) {
  1311. printf ("Error exporting key %d, %s\n", i, error_to_string (errnum));
  1312. exit (-1);
  1313. }
  1314. printf ("Exported key was: %lu bytes\n", len);
  1315. if ((errnum = kr_del (&kr, kr->ID)) != CRYPT_OK) {
  1316. printf ("Error deleting key %d, %s\n", i, error_to_string (errnum));
  1317. exit (-1);
  1318. }
  1319. kr_display (kr);
  1320. if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
  1321. printf ("Error importing key %d, %s\n", i, error_to_string (errnum));
  1322. exit (-1);
  1323. }
  1324. kr_display (kr);
  1325. }
  1326. if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
  1327. printf ("Error clearing ring: %s\n", error_to_string (errnum));
  1328. exit (-1);
  1329. }
  1330. /* TEST output to file */
  1331. #ifndef NO_FILE
  1332. if ((errnum = kr_init (&kr)) != CRYPT_OK) {
  1333. printf ("KR init error %s\n", error_to_string (errnum));
  1334. exit (-1);
  1335. }
  1336. kr_test_makekeys (&kr);
  1337. /* save to file */
  1338. f = fopen ("ring.dat", "wb");
  1339. if ((errnum = kr_save (kr, f, NULL)) != CRYPT_OK) {
  1340. printf ("kr_save error %s\n", error_to_string (errnum));
  1341. exit (-1);
  1342. }
  1343. fclose (f);
  1344. /* delete and load */
  1345. if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
  1346. printf ("clear error: %s\n", error_to_string (errnum));
  1347. exit (-1);
  1348. }
  1349. f = fopen ("ring.dat", "rb");
  1350. if ((errnum = kr_load (&kr, f, NULL)) != CRYPT_OK) {
  1351. printf ("kr_load error %s\n", error_to_string (errnum));
  1352. exit (-1);
  1353. }
  1354. fclose (f);
  1355. remove ("ring.dat");
  1356. printf ("After load and save...\n");
  1357. kr_display (kr);
  1358. if ((errnum = kr_clear (&kr)) != CRYPT_OK) {
  1359. printf ("clear error: %s\n", error_to_string (errnum));
  1360. exit (-1);
  1361. }
  1362. #endif
  1363. /* test the packet encryption/sign stuff */
  1364. for (i = 0; i < 32; i++)
  1365. buf[i] = i;
  1366. kr_test_makekeys (&kr);
  1367. _kr = kr;
  1368. for (i = 0; i < 3; i++) {
  1369. printf ("Testing a key with system %d, type %d:\t", _kr->system,
  1370. _kr->key_type);
  1371. len = sizeof (buf2);
  1372. if ((errnum =
  1373. kr_encrypt_key (kr, _kr->ID, buf, 16, buf2, &len, &prng,
  1374. find_prng ("yarrow"),
  1375. find_hash ("md5"))) != CRYPT_OK) {
  1376. printf ("Encrypt error, %d, %s\n", i, error_to_string (errnum));
  1377. exit (-1);
  1378. }
  1379. len = sizeof (buf3);
  1380. if ((errnum = kr_decrypt_key (kr, buf2, buf3, &len)) != CRYPT_OK) {
  1381. printf ("decrypt error, %d, %s\n", i, error_to_string (errnum));
  1382. exit (-1);
  1383. }
  1384. if (len != 16 || memcmp (buf3, buf, 16)) {
  1385. printf ("kr_decrypt_key failed, %i, %lu\n", i, len);
  1386. exit (-1);
  1387. }
  1388. printf ("kr_encrypt_key passed, ");
  1389. len = sizeof (buf2);
  1390. if ((errnum =
  1391. kr_sign_hash (kr, _kr->ID, buf, 32, buf2, &len, &prng,
  1392. find_prng ("yarrow"))) != CRYPT_OK) {
  1393. printf ("kr_sign_hash failed, %i, %s\n", i, error_to_string (errnum));
  1394. exit (-1);
  1395. }
  1396. printf ("kr_sign_hash: ");
  1397. if ((errnum = kr_verify_hash (kr, buf2, buf, 32, &stat)) != CRYPT_OK) {
  1398. printf ("kr_sign_hash failed, %i, %s\n", i, error_to_string (errnum));
  1399. exit (-1);
  1400. }
  1401. printf ("%s, ", stat ? "passed" : "failed");
  1402. buf[15] ^= 1;
  1403. if ((errnum = kr_verify_hash (kr, buf2, buf, 32, &stat)) != CRYPT_OK) {
  1404. printf ("kr_sign_hash failed, %i, %s\n", i, error_to_string (errnum));
  1405. exit (-1);
  1406. }
  1407. printf ("%s\n", (!stat) ? "passed" : "failed");
  1408. buf[15] ^= 1;
  1409. len = sizeof (buf);
  1410. if ((errnum =
  1411. kr_fingerprint (kr, _kr->ID, find_hash ("sha1"), buf,
  1412. &len)) != CRYPT_OK) {
  1413. printf ("kr_fingerprint failed, %i, %lu\n", i, len);
  1414. exit (-1);
  1415. }
  1416. printf ("Fingerprint: ");
  1417. for (j = 0; j < 20; j++) {
  1418. printf ("%02x", buf[j]);
  1419. if (j < 19)
  1420. printf (":");
  1421. }
  1422. printf ("\n\n");
  1423. _kr = _kr->next;
  1424. }
  1425. /* Test encrypting/decrypting to a public key */
  1426. /* first dump the other two keys */
  1427. kr_del (&kr, kr->ID);
  1428. kr_del (&kr, kr->ID);
  1429. kr_display (kr);
  1430. /* now export it as public and private */
  1431. len = sizeof (buf);
  1432. if ((errnum = kr_export (kr, kr->ID, PK_PUBLIC, buf, &len)) != CRYPT_OK) {
  1433. printf ("Error exporting key %d, %s\n", i, error_to_string (errnum));
  1434. exit (-1);
  1435. }
  1436. /* check boundaries */
  1437. memset (buf + len, 0, sizeof (buf) - len);
  1438. len = sizeof (buf2);
  1439. if ((errnum = kr_export (kr, kr->ID, PK_PRIVATE, buf2, &len)) != CRYPT_OK) {
  1440. printf ("Error exporting key %s\n", error_to_string (errnum));
  1441. exit (-1);
  1442. }
  1443. /* check boundaries */
  1444. memset (buf2 + len, 0, sizeof (buf2) - len);
  1445. /* delete the key and import the public */
  1446. kr_clear (&kr);
  1447. kr_init (&kr);
  1448. kr_display (kr);
  1449. if ((errnum = kr_import (kr, buf, len)) != CRYPT_OK) {
  1450. printf ("Error importing key %s\n", error_to_string (errnum));
  1451. exit (-1);
  1452. }
  1453. kr_display (kr);
  1454. /* now encrypt a buffer */
  1455. for (i = 0; i < 16; i++)
  1456. buf[i] = i;
  1457. len = sizeof (buf3);
  1458. if ((errnum =
  1459. kr_encrypt_key (kr, kr->ID, buf, 16, buf3, &len, &prng,
  1460. find_prng ("yarrow"),
  1461. find_hash ("md5"))) != CRYPT_OK) {
  1462. printf ("Encrypt error, %d, %s\n", i, error_to_string (errnum));
  1463. exit (-1);
  1464. }
  1465. /* now delete the key and import the private one */
  1466. kr_clear (&kr);
  1467. kr_init (&kr);
  1468. kr_display (kr);
  1469. if ((errnum = kr_import (kr, buf2, len)) != CRYPT_OK) {
  1470. printf ("Error importing key %s\n", error_to_string (errnum));
  1471. exit (-1);
  1472. }
  1473. kr_display (kr);
  1474. /* now decrypt */
  1475. len = sizeof (buf2);
  1476. if ((errnum = kr_decrypt_key (kr, buf3, buf2, &len)) != CRYPT_OK) {
  1477. printf ("decrypt error, %s\n", error_to_string (errnum));
  1478. exit (-1);
  1479. }
  1480. printf ("KR encrypt to public, decrypt with private: ");
  1481. if (len == 16 && !memcmp (buf2, buf, 16)) {
  1482. printf ("passed\n");
  1483. } else {
  1484. printf ("failed\n");
  1485. }
  1486. kr_clear (&kr);
  1487. }
  1488. #endif
  1489. void
  1490. test_errs (void)
  1491. {
  1492. #define ERR(x) printf("%25s => %s\n", #x, error_to_string(x));
  1493. ERR (CRYPT_OK);
  1494. ERR (CRYPT_ERROR);
  1495. ERR (CRYPT_INVALID_KEYSIZE);
  1496. ERR (CRYPT_INVALID_ROUNDS);
  1497. ERR (CRYPT_FAIL_TESTVECTOR);
  1498. ERR (CRYPT_BUFFER_OVERFLOW);
  1499. ERR (CRYPT_INVALID_PACKET);
  1500. ERR (CRYPT_INVALID_PRNGSIZE);
  1501. ERR (CRYPT_ERROR_READPRNG);
  1502. ERR (CRYPT_INVALID_CIPHER);
  1503. ERR (CRYPT_INVALID_HASH);
  1504. ERR (CRYPT_INVALID_PRNG);
  1505. ERR (CRYPT_MEM);
  1506. ERR (CRYPT_PK_TYPE_MISMATCH);
  1507. ERR (CRYPT_PK_NOT_PRIVATE);
  1508. ERR (CRYPT_INVALID_ARG);
  1509. ERR (CRYPT_PK_INVALID_TYPE);
  1510. ERR (CRYPT_PK_INVALID_SYSTEM);
  1511. ERR (CRYPT_PK_DUP);
  1512. ERR (CRYPT_PK_NOT_FOUND);
  1513. ERR (CRYPT_PK_INVALID_SIZE);
  1514. ERR (CRYPT_INVALID_PRIME_SIZE);
  1515. }
  1516. int
  1517. main (void)
  1518. {
  1519. #ifdef SONY_PS2
  1520. TIMER_Init ();
  1521. #endif
  1522. register_all_algs ();
  1523. if ((errnum = yarrow_start (&prng)) != CRYPT_OK) {
  1524. printf ("yarrow_start: %s\n", error_to_string (errnum));
  1525. }
  1526. if ((errnum = yarrow_add_entropy ("hello", 5, &prng)) != CRYPT_OK) {
  1527. printf ("yarrow_add_entropy: %s\n", error_to_string (errnum));
  1528. }
  1529. if ((errnum = yarrow_ready (&prng)) != CRYPT_OK) {
  1530. printf ("yarrow_ready: %s\n", error_to_string (errnum));
  1531. }
  1532. printf (crypt_build_settings);
  1533. test_errs ();
  1534. #ifdef HMAC
  1535. printf ("HMAC: %s\n", hmac_test () == CRYPT_OK ? "passed" : "failed");
  1536. #endif
  1537. store_tests ();
  1538. cipher_tests ();
  1539. hash_tests ();
  1540. ecb_tests ();
  1541. cbc_tests ();
  1542. ctr_tests ();
  1543. ofb_tests ();
  1544. cfb_tests ();
  1545. rng_tests ();
  1546. test_prime();
  1547. #ifdef KR
  1548. kr_test ();
  1549. #endif
  1550. rsa_test ();
  1551. pad_test ();
  1552. ecc_tests ();
  1553. dh_tests ();
  1554. gf_tests ();
  1555. base64_test ();
  1556. time_ecb ();
  1557. time_hash ();
  1558. #ifdef SONY_PS2
  1559. TIMER_Shutdown ();
  1560. #endif
  1561. return 0;
  1562. }