test-crypto.cpp 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Random.h>
  27. #include <LibCore/ArgsParser.h>
  28. #include <LibCore/EventLoop.h>
  29. #include <LibCore/File.h>
  30. #include <LibCrypto/Authentication/HMAC.h>
  31. #include <LibCrypto/BigInt/SignedBigInteger.h>
  32. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  33. #include <LibCrypto/Checksum/Adler32.h>
  34. #include <LibCrypto/Checksum/CRC32.h>
  35. #include <LibCrypto/Cipher/AES.h>
  36. #include <LibCrypto/Hash/MD5.h>
  37. #include <LibCrypto/Hash/SHA1.h>
  38. #include <LibCrypto/Hash/SHA2.h>
  39. #include <LibCrypto/PK/RSA.h>
  40. #include <LibLine/Editor.h>
  41. #include <LibTLS/TLSv12.h>
  42. #include <limits.h>
  43. #include <stdio.h>
  44. #include <time.h>
  45. static const char* secret_key = "WellHelloFreinds";
  46. static const char* suite = nullptr;
  47. static const char* filename = nullptr;
  48. static const char* server = nullptr;
  49. static int key_bits = 128;
  50. static bool binary = false;
  51. static bool interactive = false;
  52. static bool run_tests = false;
  53. static int port = 443;
  54. static bool in_ci = false;
  55. static struct timeval start_time {
  56. 0, 0
  57. };
  58. static bool g_some_test_failed = false;
  59. static bool encrypting = true;
  60. constexpr const char* DEFAULT_DIGEST_SUITE { "HMAC-SHA256" };
  61. constexpr const char* DEFAULT_CHECKSUM_SUITE { "CRC32" };
  62. constexpr const char* DEFAULT_HASH_SUITE { "SHA256" };
  63. constexpr const char* DEFAULT_CIPHER_SUITE { "AES_CBC" };
  64. constexpr const char* DEFAULT_SERVER { "www.google.com" };
  65. // listAllTests
  66. // Cipher
  67. static int aes_cbc_tests();
  68. static int aes_ctr_tests();
  69. // Hash
  70. static int md5_tests();
  71. static int sha1_tests();
  72. static int sha256_tests();
  73. static int sha512_tests();
  74. // Authentication
  75. static int hmac_md5_tests();
  76. static int hmac_sha256_tests();
  77. static int hmac_sha512_tests();
  78. static int hmac_sha1_tests();
  79. // Public-Key
  80. static int rsa_tests();
  81. // TLS
  82. static int tls_tests();
  83. // Big Integer
  84. static int bigint_tests();
  85. // Checksum
  86. static int adler32_tests();
  87. static int crc32_tests();
  88. // stop listing tests
  89. static void print_buffer(ReadonlyBytes buffer, int split)
  90. {
  91. for (size_t i = 0; i < buffer.size(); ++i) {
  92. if (split > 0) {
  93. if (i % split == 0 && i) {
  94. printf(" ");
  95. for (size_t j = i - split; j < i; ++j) {
  96. auto ch = buffer[j];
  97. printf("%c", ch >= 32 && ch <= 127 ? ch : '.'); // silly hack
  98. }
  99. puts("");
  100. }
  101. }
  102. printf("%02x ", buffer[i]);
  103. }
  104. puts("");
  105. }
  106. static Core::EventLoop g_loop;
  107. static int run(Function<void(const char*, size_t)> fn)
  108. {
  109. if (interactive) {
  110. auto editor = Line::Editor::construct();
  111. editor->initialize();
  112. for (;;) {
  113. auto line_result = editor->get_line("> ");
  114. if (line_result.is_error())
  115. break;
  116. auto& line = line_result.value();
  117. if (line == ".wait") {
  118. g_loop.exec();
  119. } else {
  120. fn(line.characters(), line.length());
  121. g_loop.pump();
  122. }
  123. }
  124. } else {
  125. if (filename == nullptr) {
  126. puts("must specify a file name");
  127. return 1;
  128. }
  129. if (!Core::File::exists(filename)) {
  130. puts("File does not exist");
  131. return 1;
  132. }
  133. auto file = Core::File::open(filename, Core::IODevice::OpenMode::ReadOnly);
  134. if (file.is_error()) {
  135. printf("That's a weird file man...\n");
  136. return 1;
  137. }
  138. auto buffer = file.value()->read_all();
  139. fn((const char*)buffer.data(), buffer.size());
  140. g_loop.exec();
  141. }
  142. return 0;
  143. }
  144. static void tls(const char* message, size_t len)
  145. {
  146. static RefPtr<TLS::TLSv12> tls;
  147. static ByteBuffer write {};
  148. if (!tls) {
  149. tls = TLS::TLSv12::construct(nullptr);
  150. tls->connect(server ?: DEFAULT_SERVER, port);
  151. tls->on_tls_ready_to_read = [](auto& tls) {
  152. auto buffer = tls.read();
  153. if (buffer.has_value())
  154. fprintf(stdout, "%.*s", (int)buffer.value().size(), buffer.value().data());
  155. };
  156. tls->on_tls_ready_to_write = [&](auto&) {
  157. if (write.size()) {
  158. tls->write(write);
  159. write.clear();
  160. }
  161. };
  162. tls->on_tls_error = [&](auto) {
  163. g_loop.quit(1);
  164. };
  165. tls->on_tls_finished = [&]() {
  166. g_loop.quit(0);
  167. };
  168. }
  169. write.append(message, len);
  170. write.append("\r\n", 2);
  171. }
  172. static void aes_cbc(const char* message, size_t len)
  173. {
  174. auto buffer = ByteBuffer::wrap(const_cast<char*>(message), len);
  175. // FIXME: Take iv as an optional parameter
  176. auto iv = ByteBuffer::create_zeroed(Crypto::Cipher::AESCipher::block_size());
  177. if (encrypting) {
  178. Crypto::Cipher::AESCipher::CBCMode cipher(
  179. ByteBuffer::wrap(const_cast<char*>(secret_key), strlen(secret_key)),
  180. key_bits,
  181. Crypto::Cipher::Intent::Encryption);
  182. auto enc = cipher.create_aligned_buffer(buffer.size());
  183. auto enc_span = enc.bytes();
  184. cipher.encrypt(buffer, enc_span, iv);
  185. if (binary)
  186. printf("%.*s", (int)enc_span.size(), enc_span.data());
  187. else
  188. print_buffer(enc_span, Crypto::Cipher::AESCipher::block_size());
  189. } else {
  190. Crypto::Cipher::AESCipher::CBCMode cipher(
  191. ByteBuffer::wrap(const_cast<char*>(secret_key), strlen(secret_key)),
  192. key_bits,
  193. Crypto::Cipher::Intent::Decryption);
  194. auto dec = cipher.create_aligned_buffer(buffer.size());
  195. auto dec_span = dec.bytes();
  196. cipher.decrypt(buffer, dec_span, iv);
  197. printf("%.*s\n", (int)dec_span.size(), dec_span.data());
  198. }
  199. }
  200. static void adler32(const char* message, size_t len)
  201. {
  202. auto checksum = Crypto::Checksum::Adler32({ (const u8*)message, len });
  203. printf("%#10X\n", checksum.digest());
  204. }
  205. static void crc32(const char* message, size_t len)
  206. {
  207. auto checksum = Crypto::Checksum::CRC32({ (const u8*)message, len });
  208. printf("%#10X\n", checksum.digest());
  209. }
  210. static void md5(const char* message, size_t len)
  211. {
  212. auto digest = Crypto::Hash::MD5::hash((const u8*)message, len);
  213. if (binary)
  214. printf("%.*s", (int)Crypto::Hash::MD5::digest_size(), digest.data);
  215. else
  216. print_buffer({ digest.data, Crypto::Hash::MD5::digest_size() }, -1);
  217. }
  218. static void hmac_md5(const char* message, size_t len)
  219. {
  220. Crypto::Authentication::HMAC<Crypto::Hash::MD5> hmac(secret_key);
  221. auto mac = hmac.process((const u8*)message, len);
  222. if (binary)
  223. printf("%.*s", (int)hmac.digest_size(), mac.data);
  224. else
  225. print_buffer({ mac.data, hmac.digest_size() }, -1);
  226. }
  227. static void sha1(const char* message, size_t len)
  228. {
  229. auto digest = Crypto::Hash::SHA1::hash((const u8*)message, len);
  230. if (binary)
  231. printf("%.*s", (int)Crypto::Hash::SHA1::digest_size(), digest.data);
  232. else
  233. print_buffer({ digest.data, Crypto::Hash::SHA1::digest_size() }, -1);
  234. }
  235. static void sha256(const char* message, size_t len)
  236. {
  237. auto digest = Crypto::Hash::SHA256::hash((const u8*)message, len);
  238. if (binary)
  239. printf("%.*s", (int)Crypto::Hash::SHA256::digest_size(), digest.data);
  240. else
  241. print_buffer({ digest.data, Crypto::Hash::SHA256::digest_size() }, -1);
  242. }
  243. static void hmac_sha256(const char* message, size_t len)
  244. {
  245. Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac(secret_key);
  246. auto mac = hmac.process((const u8*)message, len);
  247. if (binary)
  248. printf("%.*s", (int)hmac.digest_size(), mac.data);
  249. else
  250. print_buffer({ mac.data, hmac.digest_size() }, -1);
  251. }
  252. static void sha512(const char* message, size_t len)
  253. {
  254. auto digest = Crypto::Hash::SHA512::hash((const u8*)message, len);
  255. if (binary)
  256. printf("%.*s", (int)Crypto::Hash::SHA512::digest_size(), digest.data);
  257. else
  258. print_buffer({ digest.data, Crypto::Hash::SHA512::digest_size() }, -1);
  259. }
  260. static void hmac_sha512(const char* message, size_t len)
  261. {
  262. Crypto::Authentication::HMAC<Crypto::Hash::SHA512> hmac(secret_key);
  263. auto mac = hmac.process((const u8*)message, len);
  264. if (binary)
  265. printf("%.*s", (int)hmac.digest_size(), mac.data);
  266. else
  267. print_buffer({ mac.data, hmac.digest_size() }, -1);
  268. }
  269. auto main(int argc, char** argv) -> int
  270. {
  271. const char* mode = nullptr;
  272. Core::ArgsParser parser;
  273. parser.add_positional_argument(mode, "mode to operate in ('list' to see modes and descriptions)", "mode");
  274. parser.add_option(secret_key, "Set the secret key (default key is 'WellHelloFriends')", "secret-key", 'k', "secret key");
  275. parser.add_option(key_bits, "Size of the key", "key-bits", 'b', "key-bits");
  276. parser.add_option(filename, "Read from file", "file", 'f', "from file");
  277. parser.add_option(binary, "Force binary output", "force-binary", 0);
  278. parser.add_option(interactive, "REPL mode", "interactive", 'i');
  279. parser.add_option(run_tests, "Run tests for the specified suite", "tests", 't');
  280. parser.add_option(suite, "Set the suite used", "suite-name", 'n', "suite name");
  281. parser.add_option(server, "Set the server to talk to (only for `tls')", "server-address", 's', "server-address");
  282. parser.add_option(port, "Set the port to talk to (only for `tls')", "port", 'p', "port");
  283. parser.add_option(in_ci, "CI Test mode", "ci-mode", 'c');
  284. parser.parse(argc, argv);
  285. StringView mode_sv { mode };
  286. if (mode_sv == "list") {
  287. puts("test-crypto modes");
  288. puts("\tdigest - Access digest (authentication) functions");
  289. puts("\thash - Access hash functions");
  290. puts("\tchecksum - Access checksum functions");
  291. puts("\tencrypt -- Access encryption functions");
  292. puts("\tdecrypt -- Access decryption functions");
  293. puts("\ttls -- Connect to a peer over TLS 1.2");
  294. puts("\tlist -- List all known modes");
  295. puts("these modes only contain tests");
  296. puts("\ttest -- Run every test suite");
  297. puts("\tbigint -- Run big integer test suite");
  298. puts("\tpk -- Run Public-key system tests");
  299. return 0;
  300. }
  301. if (mode_sv == "hash") {
  302. if (suite == nullptr)
  303. suite = DEFAULT_HASH_SUITE;
  304. StringView suite_sv { suite };
  305. if (suite_sv == "MD5") {
  306. if (run_tests)
  307. return md5_tests();
  308. return run(md5);
  309. }
  310. if (suite_sv == "SHA1") {
  311. if (run_tests)
  312. return sha1_tests();
  313. return run(sha1);
  314. }
  315. if (suite_sv == "SHA256") {
  316. if (run_tests)
  317. return sha256_tests();
  318. return run(sha256);
  319. }
  320. if (suite_sv == "SHA512") {
  321. if (run_tests)
  322. return sha512_tests();
  323. return run(sha512);
  324. }
  325. printf("unknown hash function '%s'\n", suite);
  326. return 1;
  327. }
  328. if (mode_sv == "checksum") {
  329. if (suite == nullptr)
  330. suite = DEFAULT_CHECKSUM_SUITE;
  331. StringView suite_sv { suite };
  332. if (suite_sv == "CRC32") {
  333. if (run_tests)
  334. return crc32_tests();
  335. return run(crc32);
  336. }
  337. if (suite_sv == "Adler32") {
  338. if (run_tests)
  339. return adler32_tests();
  340. return run(adler32);
  341. }
  342. printf("unknown checksum function '%s'\n", suite);
  343. return 1;
  344. }
  345. if (mode_sv == "digest") {
  346. if (suite == nullptr)
  347. suite = DEFAULT_DIGEST_SUITE;
  348. StringView suite_sv { suite };
  349. if (suite_sv == "HMAC-MD5") {
  350. if (run_tests)
  351. return hmac_md5_tests();
  352. return run(hmac_md5);
  353. }
  354. if (suite_sv == "HMAC-SHA256") {
  355. if (run_tests)
  356. return hmac_sha256_tests();
  357. return run(hmac_sha256);
  358. }
  359. if (suite_sv == "HMAC-SHA512") {
  360. if (run_tests)
  361. return hmac_sha512_tests();
  362. return run(hmac_sha512);
  363. }
  364. if (suite_sv == "HMAC-SHA1") {
  365. if (run_tests)
  366. return hmac_sha1_tests();
  367. }
  368. printf("unknown hash function '%s'\n", suite);
  369. return 1;
  370. }
  371. if (mode_sv == "pk") {
  372. return rsa_tests();
  373. }
  374. if (mode_sv == "bigint") {
  375. return bigint_tests();
  376. }
  377. if (mode_sv == "tls") {
  378. if (run_tests)
  379. return tls_tests();
  380. return run(tls);
  381. }
  382. if (mode_sv == "test") {
  383. encrypting = true;
  384. aes_cbc_tests();
  385. aes_ctr_tests();
  386. encrypting = false;
  387. aes_cbc_tests();
  388. aes_ctr_tests();
  389. md5_tests();
  390. sha1_tests();
  391. sha256_tests();
  392. sha512_tests();
  393. hmac_md5_tests();
  394. hmac_sha256_tests();
  395. hmac_sha512_tests();
  396. hmac_sha1_tests();
  397. rsa_tests();
  398. if (!in_ci) {
  399. // Do not run these in CI to avoid tests with variables outside our control.
  400. tls_tests();
  401. }
  402. bigint_tests();
  403. return g_some_test_failed ? 1 : 0;
  404. }
  405. encrypting = mode_sv == "encrypt";
  406. if (encrypting || mode_sv == "decrypt") {
  407. if (suite == nullptr)
  408. suite = DEFAULT_CIPHER_SUITE;
  409. StringView suite_sv { suite };
  410. if (StringView(suite) == "AES_CBC") {
  411. if (run_tests)
  412. return aes_cbc_tests();
  413. if (!Crypto::Cipher::AESCipher::KeyType::is_valid_key_size(key_bits)) {
  414. printf("Invalid key size for AES: %d\n", key_bits);
  415. return 1;
  416. }
  417. if (strlen(secret_key) != (size_t)key_bits / 8) {
  418. printf("Key must be exactly %d bytes long\n", key_bits / 8);
  419. return 1;
  420. }
  421. return run(aes_cbc);
  422. } else {
  423. printf("Unknown cipher suite '%s'\n", suite);
  424. return 1;
  425. }
  426. }
  427. printf("Unknown mode '%s', check out the list of modes\n", mode);
  428. return 1;
  429. }
  430. #define I_TEST(thing) \
  431. { \
  432. printf("Testing " #thing "... "); \
  433. fflush(stdout); \
  434. gettimeofday(&start_time, nullptr); \
  435. }
  436. #define PASS \
  437. { \
  438. struct timeval end_time { \
  439. 0, 0 \
  440. }; \
  441. gettimeofday(&end_time, nullptr); \
  442. time_t interval_s = end_time.tv_sec - start_time.tv_sec; \
  443. suseconds_t interval_us = end_time.tv_usec; \
  444. if (interval_us < start_time.tv_usec) { \
  445. interval_s -= 1; \
  446. interval_us += 1000000; \
  447. } \
  448. interval_us -= start_time.tv_usec; \
  449. printf("PASS %llds %lldus\n", (long long)interval_s, (long long)interval_us); \
  450. }
  451. #define FAIL(reason) \
  452. do { \
  453. printf("FAIL: " #reason "\n"); \
  454. g_some_test_failed = true; \
  455. } while (0)
  456. static ByteBuffer operator""_b(const char* string, size_t length)
  457. {
  458. dbg() << "Create byte buffer of size " << length;
  459. return ByteBuffer::copy(string, length);
  460. }
  461. // tests go after here
  462. // please be reasonable with orders kthx
  463. static void aes_cbc_test_name();
  464. static void aes_cbc_test_encrypt();
  465. static void aes_cbc_test_decrypt();
  466. static void aes_ctr_test_name();
  467. static void aes_ctr_test_encrypt();
  468. static void aes_ctr_test_decrypt();
  469. static void md5_test_name();
  470. static void md5_test_hash();
  471. static void md5_test_consecutive_updates();
  472. static void sha1_test_name();
  473. static void sha1_test_hash();
  474. static void sha256_test_name();
  475. static void sha256_test_hash();
  476. static void sha512_test_name();
  477. static void sha512_test_hash();
  478. static void hmac_md5_test_name();
  479. static void hmac_md5_test_process();
  480. static void hmac_sha256_test_name();
  481. static void hmac_sha256_test_process();
  482. static void hmac_sha512_test_name();
  483. static void hmac_sha512_test_process();
  484. static void hmac_sha1_test_name();
  485. static void hmac_sha1_test_process();
  486. static void rsa_test_encrypt();
  487. static void rsa_test_der_parse();
  488. static void rsa_test_encrypt_decrypt();
  489. static void rsa_emsa_pss_test_create();
  490. static void bigint_test_number_theory(); // FIXME: we should really move these num theory stuff out
  491. static void tls_test_client_hello();
  492. static void bigint_test_fibo500();
  493. static void bigint_addition_edgecases();
  494. static void bigint_subtraction();
  495. static void bigint_multiplication();
  496. static void bigint_division();
  497. static void bigint_base10();
  498. static void bigint_import_export();
  499. static void bigint_bitwise();
  500. static void bigint_test_signed_fibo500();
  501. static void bigint_signed_addition_edgecases();
  502. static void bigint_signed_subtraction();
  503. static void bigint_signed_multiplication();
  504. static void bigint_signed_division();
  505. static void bigint_signed_base10();
  506. static void bigint_signed_import_export();
  507. static void bigint_signed_bitwise();
  508. static int aes_cbc_tests()
  509. {
  510. aes_cbc_test_name();
  511. if (encrypting) {
  512. aes_cbc_test_encrypt();
  513. } else {
  514. aes_cbc_test_decrypt();
  515. }
  516. return g_some_test_failed ? 1 : 0;
  517. }
  518. static void aes_cbc_test_name()
  519. {
  520. I_TEST((AES CBC class name));
  521. Crypto::Cipher::AESCipher::CBCMode cipher("WellHelloFriends"_b, 128, Crypto::Cipher::Intent::Encryption);
  522. if (cipher.class_name() != "AES_CBC")
  523. FAIL(Invalid class name);
  524. else
  525. PASS;
  526. }
  527. static void aes_cbc_test_encrypt()
  528. {
  529. auto test_it = [](auto& cipher, auto& result) {
  530. auto in = "This is a test! This is another test!"_b;
  531. auto out = cipher.create_aligned_buffer(in.size());
  532. auto iv = ByteBuffer::create_zeroed(Crypto::Cipher::AESCipher::block_size());
  533. auto out_span = out.bytes();
  534. cipher.encrypt(in, out_span, iv);
  535. if (out.size() != sizeof(result))
  536. FAIL(size mismatch);
  537. else if (memcmp(out_span.data(), result, out_span.size()) != 0) {
  538. FAIL(invalid data);
  539. print_buffer(out_span, Crypto::Cipher::AESCipher::block_size());
  540. } else
  541. PASS;
  542. };
  543. {
  544. I_TEST((AES CBC with 128 bit key | Encrypt))
  545. u8 result[] {
  546. 0xb8, 0x06, 0x7c, 0xf2, 0xa9, 0x56, 0x63, 0x58, 0x2d, 0x5c, 0xa1, 0x4b, 0xc5, 0xe3, 0x08,
  547. 0xcf, 0xb5, 0x93, 0xfb, 0x67, 0xb6, 0xf7, 0xaf, 0x45, 0x34, 0x64, 0x70, 0x9e, 0xc9, 0x1a,
  548. 0x8b, 0xd3, 0x70, 0x45, 0xf0, 0x79, 0x65, 0xca, 0xb9, 0x03, 0x88, 0x72, 0x1c, 0xdd, 0xab,
  549. 0x45, 0x6b, 0x1c
  550. };
  551. Crypto::Cipher::AESCipher::CBCMode cipher("WellHelloFriends"_b, 128, Crypto::Cipher::Intent::Encryption);
  552. test_it(cipher, result);
  553. }
  554. {
  555. I_TEST((AES CBC with 192 bit key | Encrypt))
  556. u8 result[] {
  557. 0xae, 0xd2, 0x70, 0xc4, 0x9c, 0xaa, 0x83, 0x33, 0xd3, 0xd3, 0xac, 0x11, 0x65, 0x35, 0xf7,
  558. 0x19, 0x48, 0x7c, 0x7a, 0x8a, 0x95, 0x64, 0xe7, 0xc6, 0x0a, 0xdf, 0x10, 0x06, 0xdc, 0x90,
  559. 0x68, 0x51, 0x09, 0xd7, 0x3b, 0x48, 0x1b, 0x8a, 0xd3, 0x50, 0x09, 0xba, 0xfc, 0xde, 0x11,
  560. 0xe0, 0x3f, 0xcb
  561. };
  562. Crypto::Cipher::AESCipher::CBCMode cipher("Well Hello Friends! whf!"_b, 192, Crypto::Cipher::Intent::Encryption);
  563. test_it(cipher, result);
  564. }
  565. {
  566. I_TEST((AES CBC with 256 bit key | Encrypt))
  567. u8 result[] {
  568. 0x0a, 0x44, 0x4d, 0x62, 0x9e, 0x8b, 0xd8, 0x11, 0x80, 0x48, 0x2a, 0x32, 0x53, 0x61, 0xe7,
  569. 0x59, 0x62, 0x55, 0x9e, 0xf4, 0xe6, 0xad, 0xea, 0xc5, 0x0b, 0xf6, 0xbc, 0x6a, 0xcb, 0x9c,
  570. 0x47, 0x9f, 0xc2, 0x21, 0xe6, 0x19, 0x62, 0xc3, 0x75, 0xca, 0xab, 0x2d, 0x18, 0xa1, 0x54,
  571. 0xd1, 0x41, 0xe6
  572. };
  573. Crypto::Cipher::AESCipher::CBCMode cipher("WellHelloFriendsWellHelloFriends"_b, 256, Crypto::Cipher::Intent::Encryption);
  574. test_it(cipher, result);
  575. }
  576. {
  577. I_TEST((AES CBC with 256 bit key | Encrypt with unsigned key))
  578. u8 result[] {
  579. 0x18, 0x71, 0x80, 0x4c, 0x28, 0x07, 0x55, 0x3c, 0x05, 0x33, 0x36, 0x3f, 0x19, 0x38, 0x5c,
  580. 0xbe, 0xf8, 0xb8, 0x0e, 0x0e, 0x66, 0x67, 0x63, 0x9c, 0xbf, 0x73, 0xcd, 0x82, 0xf9, 0xcb,
  581. 0x9d, 0x81, 0x56, 0xc6, 0x75, 0x14, 0x8b, 0x79, 0x60, 0xb0, 0xdf, 0xaa, 0x2c, 0x2b, 0xd4,
  582. 0xd6, 0xa0, 0x46
  583. };
  584. u8 key[] { 0x0a, 0x8c, 0x5b, 0x0d, 0x8a, 0x68, 0x43, 0xf7, 0xaf, 0xc0, 0xe3, 0x4e, 0x4b, 0x43, 0xaa, 0x28, 0x69, 0x9b, 0x6f, 0xe7, 0x24, 0x82, 0x1c, 0x71, 0x86, 0xf6, 0x2b, 0x87, 0xd6, 0x8b, 0x8f, 0xf1 };
  585. Crypto::Cipher::AESCipher::CBCMode cipher(ByteBuffer::wrap(key, 32), 256, Crypto::Cipher::Intent::Encryption);
  586. test_it(cipher, result);
  587. }
  588. // TODO: Test non-CMS padding options
  589. }
  590. static void aes_cbc_test_decrypt()
  591. {
  592. auto test_it = [](auto& cipher, auto& result, auto result_len) {
  593. auto true_value = "This is a test! This is another test!";
  594. auto in = ByteBuffer::copy(result, result_len);
  595. auto out = cipher.create_aligned_buffer(in.size());
  596. auto iv = ByteBuffer::create_zeroed(Crypto::Cipher::AESCipher::block_size());
  597. auto out_span = out.bytes();
  598. cipher.decrypt(in, out_span, iv);
  599. if (out_span.size() != strlen(true_value)) {
  600. FAIL(size mismatch);
  601. printf("Expected %zu bytes but got %zu\n", strlen(true_value), out_span.size());
  602. } else if (memcmp(out_span.data(), true_value, strlen(true_value)) != 0) {
  603. FAIL(invalid data);
  604. print_buffer(out_span, Crypto::Cipher::AESCipher::block_size());
  605. } else
  606. PASS;
  607. };
  608. {
  609. I_TEST((AES CBC with 128 bit key | Decrypt))
  610. u8 result[] {
  611. 0xb8, 0x06, 0x7c, 0xf2, 0xa9, 0x56, 0x63, 0x58, 0x2d, 0x5c, 0xa1, 0x4b, 0xc5, 0xe3, 0x08,
  612. 0xcf, 0xb5, 0x93, 0xfb, 0x67, 0xb6, 0xf7, 0xaf, 0x45, 0x34, 0x64, 0x70, 0x9e, 0xc9, 0x1a,
  613. 0x8b, 0xd3, 0x70, 0x45, 0xf0, 0x79, 0x65, 0xca, 0xb9, 0x03, 0x88, 0x72, 0x1c, 0xdd, 0xab,
  614. 0x45, 0x6b, 0x1c
  615. };
  616. Crypto::Cipher::AESCipher::CBCMode cipher("WellHelloFriends"_b, 128, Crypto::Cipher::Intent::Decryption);
  617. test_it(cipher, result, 48);
  618. }
  619. {
  620. I_TEST((AES CBC with 192 bit key | Decrypt))
  621. u8 result[] {
  622. 0xae, 0xd2, 0x70, 0xc4, 0x9c, 0xaa, 0x83, 0x33, 0xd3, 0xd3, 0xac, 0x11, 0x65, 0x35, 0xf7,
  623. 0x19, 0x48, 0x7c, 0x7a, 0x8a, 0x95, 0x64, 0xe7, 0xc6, 0x0a, 0xdf, 0x10, 0x06, 0xdc, 0x90,
  624. 0x68, 0x51, 0x09, 0xd7, 0x3b, 0x48, 0x1b, 0x8a, 0xd3, 0x50, 0x09, 0xba, 0xfc, 0xde, 0x11,
  625. 0xe0, 0x3f, 0xcb
  626. };
  627. Crypto::Cipher::AESCipher::CBCMode cipher("Well Hello Friends! whf!"_b, 192, Crypto::Cipher::Intent::Decryption);
  628. test_it(cipher, result, 48);
  629. }
  630. {
  631. I_TEST((AES CBC with 256 bit key | Decrypt))
  632. u8 result[] {
  633. 0x0a, 0x44, 0x4d, 0x62, 0x9e, 0x8b, 0xd8, 0x11, 0x80, 0x48, 0x2a, 0x32, 0x53, 0x61, 0xe7,
  634. 0x59, 0x62, 0x55, 0x9e, 0xf4, 0xe6, 0xad, 0xea, 0xc5, 0x0b, 0xf6, 0xbc, 0x6a, 0xcb, 0x9c,
  635. 0x47, 0x9f, 0xc2, 0x21, 0xe6, 0x19, 0x62, 0xc3, 0x75, 0xca, 0xab, 0x2d, 0x18, 0xa1, 0x54,
  636. 0xd1, 0x41, 0xe6
  637. };
  638. Crypto::Cipher::AESCipher::CBCMode cipher("WellHelloFriendsWellHelloFriends"_b, 256, Crypto::Cipher::Intent::Decryption);
  639. test_it(cipher, result, 48);
  640. }
  641. // TODO: Test non-CMS padding options
  642. }
  643. static int aes_ctr_tests()
  644. {
  645. aes_ctr_test_name();
  646. if (encrypting) {
  647. aes_ctr_test_encrypt();
  648. } else {
  649. aes_ctr_test_decrypt();
  650. }
  651. return g_some_test_failed ? 1 : 0;
  652. }
  653. static void aes_ctr_test_name()
  654. {
  655. I_TEST((AES CTR class name));
  656. Crypto::Cipher::AESCipher::CTRMode cipher("WellHelloFriends"_b, 128, Crypto::Cipher::Intent::Encryption);
  657. if (cipher.class_name() != "AES_CTR")
  658. FAIL(Invalid class name);
  659. else
  660. PASS;
  661. }
  662. #define AS_BB(x) (ByteBuffer::wrap((x), sizeof((x)) / sizeof((x)[0])))
  663. static void aes_ctr_test_encrypt()
  664. {
  665. auto test_it = [](auto key, auto ivec, auto in, auto out_expected) {
  666. // nonce is already included in ivec.
  667. Crypto::Cipher::AESCipher::CTRMode cipher(key, 8 * key.size(), Crypto::Cipher::Intent::Encryption);
  668. ByteBuffer out_actual = ByteBuffer::create_zeroed(in.size());
  669. Bytes out_span = out_actual.bytes();
  670. cipher.encrypt(in, out_span, ivec);
  671. if (out_expected.size() != out_actual.size()) {
  672. FAIL(size mismatch);
  673. printf("Expected %zu bytes but got %zu\n", out_expected.size(), out_span.size());
  674. print_buffer(out_span, Crypto::Cipher::AESCipher::block_size());
  675. } else if (memcmp(out_expected.data(), out_span.data(), out_expected.size()) != 0) {
  676. FAIL(invalid data);
  677. print_buffer(out_span, Crypto::Cipher::AESCipher::block_size());
  678. } else
  679. PASS;
  680. };
  681. // From RFC 3686, Section 6
  682. {
  683. // Test Vector #1
  684. I_TEST((AES CTR 16 octets with 128 bit key | Encrypt))
  685. u8 key[] {
  686. 0xae, 0x68, 0x52, 0xf8, 0x12, 0x10, 0x67, 0xcc, 0x4b, 0xf7, 0xa5, 0x76, 0x55, 0x77, 0xf3, 0x9e
  687. };
  688. u8 ivec[] {
  689. 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + 1 // See CTR.h
  690. };
  691. u8 in[] {
  692. 0x53, 0x69, 0x6e, 0x67, 0x6c, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x20, 0x6d, 0x73, 0x67
  693. };
  694. u8 out[] {
  695. 0xe4, 0x09, 0x5d, 0x4f, 0xb7, 0xa7, 0xb3, 0x79, 0x2d, 0x61, 0x75, 0xa3, 0x26, 0x13, 0x11, 0xb8
  696. };
  697. test_it(AS_BB(key), AS_BB(ivec), AS_BB(in), AS_BB(out));
  698. }
  699. {
  700. // Test Vector #2
  701. I_TEST((AES CTR 32 octets with 128 bit key | Encrypt))
  702. u8 key[] {
  703. 0x7e, 0x24, 0x06, 0x78, 0x17, 0xfa, 0xe0, 0xd7, 0x43, 0xd6, 0xce, 0x1f, 0x32, 0x53, 0x91, 0x63
  704. };
  705. u8 ivec[] {
  706. 0x00, 0x6c, 0xb6, 0xdb, 0xc0, 0x54, 0x3b, 0x59, 0xda, 0x48, 0xd9, 0x0b, 0x00, 0x00, 0x00, 0x00 + 1 // See CTR.h
  707. };
  708. u8 in[] {
  709. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  710. };
  711. u8 out[] {
  712. 0x51, 0x04, 0xa1, 0x06, 0x16, 0x8a, 0x72, 0xd9, 0x79, 0x0d, 0x41, 0xee, 0x8e, 0xda, 0xd3, 0x88,
  713. 0xeb, 0x2e, 0x1e, 0xfc, 0x46, 0xda, 0x57, 0xc8, 0xfc, 0xe6, 0x30, 0xdf, 0x91, 0x41, 0xbe, 0x28
  714. };
  715. test_it(AS_BB(key), AS_BB(ivec), AS_BB(in), AS_BB(out));
  716. }
  717. {
  718. // Test Vector #3
  719. I_TEST((AES CTR 36 octets with 128 bit key | Encrypt))
  720. u8 key[] {
  721. 0x76, 0x91, 0xbe, 0x03, 0x5e, 0x50, 0x20, 0xa8, 0xac, 0x6e, 0x61, 0x85, 0x29, 0xf9, 0xa0, 0xdc
  722. };
  723. u8 ivec[] {
  724. 0x00, 0xe0, 0x01, 0x7b, 0x27, 0x77, 0x7f, 0x3f, 0x4a, 0x17, 0x86, 0xf0, 0x00, 0x00, 0x00, 0x00 + 1 // See CTR.h
  725. };
  726. u8 in[] {
  727. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  728. };
  729. u8 out[] {
  730. 0xc1, 0xcf, 0x48, 0xa8, 0x9f, 0x2f, 0xfd, 0xd9, 0xcf, 0x46, 0x52, 0xe9, 0xef, 0xdb, 0x72, 0xd7, 0x45, 0x40, 0xa4, 0x2b, 0xde, 0x6d, 0x78, 0x36, 0xd5, 0x9a, 0x5c, 0xea, 0xae, 0xf3, 0x10, 0x53, 0x25, 0xb2, 0x07, 0x2f
  731. };
  732. test_it(AS_BB(key), AS_BB(ivec), AS_BB(in), AS_BB(out));
  733. }
  734. {
  735. // Test Vector #4
  736. I_TEST((AES CTR 16 octets with 192 bit key | Encrypt))
  737. u8 key[] {
  738. 0x16, 0xaf, 0x5b, 0x14, 0x5f, 0xc9, 0xf5, 0x79, 0xc1, 0x75, 0xf9, 0x3e, 0x3b, 0xfb, 0x0e, 0xed, 0x86, 0x3d, 0x06, 0xcc, 0xfd, 0xb7, 0x85, 0x15
  739. };
  740. u8 ivec[] {
  741. 0x00, 0x00, 0x00, 0x48, 0x36, 0x73, 0x3c, 0x14, 0x7d, 0x6d, 0x93, 0xcb, 0x00, 0x00, 0x00, 0x00 + 1 // See CTR.h
  742. };
  743. u8 in[] {
  744. 0x53, 0x69, 0x6e, 0x67, 0x6c, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x20, 0x6d, 0x73, 0x67
  745. };
  746. u8 out[] {
  747. 0x4b, 0x55, 0x38, 0x4f, 0xe2, 0x59, 0xc9, 0xc8, 0x4e, 0x79, 0x35, 0xa0, 0x03, 0xcb, 0xe9, 0x28
  748. };
  749. test_it(AS_BB(key), AS_BB(ivec), AS_BB(in), AS_BB(out));
  750. }
  751. {
  752. // Test Vector #5
  753. I_TEST((AES CTR 32 octets with 192 bit key | Encrypt))
  754. u8 key[] {
  755. 0x7c, 0x5c, 0xb2, 0x40, 0x1b, 0x3d, 0xc3, 0x3c, 0x19, 0xe7, 0x34, 0x08, 0x19, 0xe0, 0xf6, 0x9c, 0x67, 0x8c, 0x3d, 0xb8, 0xe6, 0xf6, 0xa9, 0x1a
  756. };
  757. u8 ivec[] {
  758. 0x00, 0x96, 0xb0, 0x3b, 0x02, 0x0c, 0x6e, 0xad, 0xc2, 0xcb, 0x50, 0x0d, 0x00, 0x00, 0x00, 0x00 + 1 // See CTR.h
  759. };
  760. u8 in[] {
  761. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  762. };
  763. u8 out[] {
  764. 0x45, 0x32, 0x43, 0xfc, 0x60, 0x9b, 0x23, 0x32, 0x7e, 0xdf, 0xaa, 0xfa, 0x71, 0x31, 0xcd, 0x9f, 0x84, 0x90, 0x70, 0x1c, 0x5a, 0xd4, 0xa7, 0x9c, 0xfc, 0x1f, 0xe0, 0xff, 0x42, 0xf4, 0xfb, 0x00
  765. };
  766. test_it(AS_BB(key), AS_BB(ivec), AS_BB(in), AS_BB(out));
  767. }
  768. {
  769. // Test Vector #6
  770. I_TEST((AES CTR 36 octets with 192 bit key | Encrypt))
  771. u8 key[] {
  772. 0x02, 0xbf, 0x39, 0x1e, 0xe8, 0xec, 0xb1, 0x59, 0xb9, 0x59, 0x61, 0x7b, 0x09, 0x65, 0x27, 0x9b, 0xf5, 0x9b, 0x60, 0xa7, 0x86, 0xd3, 0xe0, 0xfe
  773. };
  774. u8 ivec[] {
  775. 0x00, 0x07, 0xbd, 0xfd, 0x5c, 0xbd, 0x60, 0x27, 0x8d, 0xcc, 0x09, 0x12, 0x00, 0x00, 0x00, 0x00 + 1 // See CTR.h
  776. };
  777. u8 in[] {
  778. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  779. };
  780. u8 out[] {
  781. 0x96, 0x89, 0x3f, 0xc5, 0x5e, 0x5c, 0x72, 0x2f, 0x54, 0x0b, 0x7d, 0xd1, 0xdd, 0xf7, 0xe7, 0x58, 0xd2, 0x88, 0xbc, 0x95, 0xc6, 0x91, 0x65, 0x88, 0x45, 0x36, 0xc8, 0x11, 0x66, 0x2f, 0x21, 0x88, 0xab, 0xee, 0x09, 0x35
  782. };
  783. test_it(AS_BB(key), AS_BB(ivec), AS_BB(in), AS_BB(out));
  784. }
  785. {
  786. // Test Vector #7
  787. I_TEST((AES CTR 16 octets with 256 bit key | Encrypt))
  788. u8 key[] {
  789. 0x77, 0x6b, 0xef, 0xf2, 0x85, 0x1d, 0xb0, 0x6f, 0x4c, 0x8a, 0x05, 0x42, 0xc8, 0x69, 0x6f, 0x6c, 0x6a, 0x81, 0xaf, 0x1e, 0xec, 0x96, 0xb4, 0xd3, 0x7f, 0xc1, 0xd6, 0x89, 0xe6, 0xc1, 0xc1, 0x04
  790. };
  791. u8 ivec[] {
  792. 0x00, 0x00, 0x00, 0x60, 0xdb, 0x56, 0x72, 0xc9, 0x7a, 0xa8, 0xf0, 0xb2, 0x00, 0x00, 0x00, 0x00 + 1 // See CTR.h
  793. };
  794. u8 in[] {
  795. 0x53, 0x69, 0x6e, 0x67, 0x6c, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x20, 0x6d, 0x73, 0x67
  796. };
  797. u8 out[] {
  798. 0x14, 0x5a, 0xd0, 0x1d, 0xbf, 0x82, 0x4e, 0xc7, 0x56, 0x08, 0x63, 0xdc, 0x71, 0xe3, 0xe0, 0xc0
  799. };
  800. test_it(AS_BB(key), AS_BB(ivec), AS_BB(in), AS_BB(out));
  801. }
  802. {
  803. // Test Vector #8
  804. I_TEST((AES CTR 32 octets with 256 bit key | Encrypt))
  805. u8 key[] {
  806. 0xf6, 0xd6, 0x6d, 0x6b, 0xd5, 0x2d, 0x59, 0xbb, 0x07, 0x96, 0x36, 0x58, 0x79, 0xef, 0xf8, 0x86, 0xc6, 0x6d, 0xd5, 0x1a, 0x5b, 0x6a, 0x99, 0x74, 0x4b, 0x50, 0x59, 0x0c, 0x87, 0xa2, 0x38, 0x84
  807. };
  808. u8 ivec[] {
  809. 0x00, 0xfa, 0xac, 0x24, 0xc1, 0x58, 0x5e, 0xf1, 0x5a, 0x43, 0xd8, 0x75, 0x00, 0x00, 0x00, 0x00 + 1 // See CTR.h
  810. };
  811. u8 in[] {
  812. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  813. };
  814. u8 out[] {
  815. 0xf0, 0x5e, 0x23, 0x1b, 0x38, 0x94, 0x61, 0x2c, 0x49, 0xee, 0x00, 0x0b, 0x80, 0x4e, 0xb2, 0xa9, 0xb8, 0x30, 0x6b, 0x50, 0x8f, 0x83, 0x9d, 0x6a, 0x55, 0x30, 0x83, 0x1d, 0x93, 0x44, 0xaf, 0x1c
  816. };
  817. test_it(AS_BB(key), AS_BB(ivec), AS_BB(in), AS_BB(out));
  818. }
  819. {
  820. // Test Vector #9
  821. I_TEST((AES CTR 36 octets with 256 bit key | Encrypt))
  822. u8 key[] {
  823. 0xff, 0x7a, 0x61, 0x7c, 0xe6, 0x91, 0x48, 0xe4, 0xf1, 0x72, 0x6e, 0x2f, 0x43, 0x58, 0x1d, 0xe2, 0xaa, 0x62, 0xd9, 0xf8, 0x05, 0x53, 0x2e, 0xdf, 0xf1, 0xee, 0xd6, 0x87, 0xfb, 0x54, 0x15, 0x3d
  824. };
  825. u8 ivec[] {
  826. 0x00, 0x1c, 0xc5, 0xb7, 0x51, 0xa5, 0x1d, 0x70, 0xa1, 0xc1, 0x11, 0x48, 0x00, 0x00, 0x00, 0x00 + 1 // See CTR.h
  827. };
  828. u8 in[] {
  829. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  830. };
  831. u8 out[] {
  832. 0xeb, 0x6c, 0x52, 0x82, 0x1d, 0x0b, 0xbb, 0xf7, 0xce, 0x75, 0x94, 0x46, 0x2a, 0xca, 0x4f, 0xaa, 0xb4, 0x07, 0xdf, 0x86, 0x65, 0x69, 0xfd, 0x07, 0xf4, 0x8c, 0xc0, 0xb5, 0x83, 0xd6, 0x07, 0x1f, 0x1e, 0xc0, 0xe6, 0xb8
  833. };
  834. test_it(AS_BB(key), AS_BB(ivec), AS_BB(in), AS_BB(out));
  835. }
  836. // Manual test case
  837. {
  838. // This test checks whether counter overflow crashes.
  839. I_TEST((AES CTR 36 octets with 256 bit key, high counter | Encrypt))
  840. u8 key[] {
  841. 0xff, 0x7a, 0x61, 0x7c, 0xe6, 0x91, 0x48, 0xe4, 0xf1, 0x72, 0x6e, 0x2f, 0x43, 0x58, 0x1d, 0xe2, 0xaa, 0x62, 0xd9, 0xf8, 0x05, 0x53, 0x2e, 0xdf, 0xf1, 0xee, 0xd6, 0x87, 0xfb, 0x54, 0x15, 0x3d
  842. };
  843. u8 ivec[] {
  844. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  845. };
  846. u8 in[] {
  847. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  848. };
  849. u8 out[] {
  850. // Pasted from the output. The actual success condition is
  851. // not crashing when incrementing the counter.
  852. 0x6e, 0x8c, 0xfc, 0x59, 0x08, 0xa8, 0xc0, 0xf1, 0xe6, 0x85, 0x96, 0xe9, 0xc5, 0x40, 0xb6, 0x8b, 0xfe, 0x28, 0x72, 0xe2, 0x24, 0x11, 0x7e, 0x59, 0xef, 0xac, 0x5c, 0xe1, 0x06, 0x89, 0x09, 0xab, 0xf8, 0x90, 0x1c, 0x66
  853. };
  854. test_it(AS_BB(key), AS_BB(ivec), AS_BB(in), AS_BB(out));
  855. }
  856. }
  857. static void aes_ctr_test_decrypt()
  858. {
  859. auto test_it = [](auto key, auto ivec, auto in, auto out_expected) {
  860. // nonce is already included in ivec.
  861. Crypto::Cipher::AESCipher::CTRMode cipher(key, 8 * key.size(), Crypto::Cipher::Intent::Decryption);
  862. ByteBuffer out_actual = ByteBuffer::create_zeroed(in.size());
  863. auto out_span = out_actual.bytes();
  864. cipher.decrypt(in, out_span, ivec);
  865. if (out_expected.size() != out_span.size()) {
  866. FAIL(size mismatch);
  867. printf("Expected %zu bytes but got %zu\n", out_expected.size(), out_span.size());
  868. print_buffer(out_span, Crypto::Cipher::AESCipher::block_size());
  869. } else if (memcmp(out_expected.data(), out_span.data(), out_expected.size()) != 0) {
  870. FAIL(invalid data);
  871. print_buffer(out_span, Crypto::Cipher::AESCipher::block_size());
  872. } else
  873. PASS;
  874. };
  875. // From RFC 3686, Section 6
  876. {
  877. // Test Vector #1
  878. I_TEST((AES CTR 16 octets with 128 bit key | Decrypt))
  879. u8 key[] {
  880. 0xae, 0x68, 0x52, 0xf8, 0x12, 0x10, 0x67, 0xcc, 0x4b, 0xf7, 0xa5, 0x76, 0x55, 0x77, 0xf3, 0x9e
  881. };
  882. u8 ivec[] {
  883. 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + 1 // See CTR.h
  884. };
  885. u8 out[] {
  886. 0x53, 0x69, 0x6e, 0x67, 0x6c, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x20, 0x6d, 0x73, 0x67
  887. };
  888. u8 in[] {
  889. 0xe4, 0x09, 0x5d, 0x4f, 0xb7, 0xa7, 0xb3, 0x79, 0x2d, 0x61, 0x75, 0xa3, 0x26, 0x13, 0x11, 0xb8
  890. };
  891. test_it(AS_BB(key), AS_BB(ivec), AS_BB(in), AS_BB(out));
  892. }
  893. // If encryption works, then decryption works, too.
  894. }
  895. static int md5_tests()
  896. {
  897. md5_test_name();
  898. md5_test_hash();
  899. md5_test_consecutive_updates();
  900. return g_some_test_failed ? 1 : 0;
  901. }
  902. static void md5_test_name()
  903. {
  904. I_TEST((MD5 class name));
  905. Crypto::Hash::MD5 md5;
  906. if (md5.class_name() != "MD5")
  907. FAIL(Invalid class name);
  908. else
  909. PASS;
  910. }
  911. static void md5_test_hash()
  912. {
  913. {
  914. I_TEST((MD5 Hashing | "Well hello friends"));
  915. u8 result[] {
  916. 0xaf, 0x04, 0x3a, 0x08, 0x94, 0x38, 0x6e, 0x7f, 0xbf, 0x73, 0xe4, 0xaa, 0xf0, 0x8e, 0xee, 0x4c
  917. };
  918. auto digest = Crypto::Hash::MD5::hash("Well hello friends");
  919. if (memcmp(result, digest.data, Crypto::Hash::MD5::digest_size()) != 0) {
  920. FAIL(Invalid hash);
  921. print_buffer({ digest.data, Crypto::Hash::MD5::digest_size() }, -1);
  922. } else {
  923. PASS;
  924. }
  925. }
  926. // RFC tests
  927. {
  928. I_TEST((MD5 Hashing | ""));
  929. u8 result[] {
  930. 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e
  931. };
  932. auto digest = Crypto::Hash::MD5::hash("");
  933. if (memcmp(result, digest.data, Crypto::Hash::MD5::digest_size()) != 0) {
  934. FAIL(Invalid hash);
  935. print_buffer({ digest.data, Crypto::Hash::MD5::digest_size() }, -1);
  936. } else {
  937. PASS;
  938. }
  939. }
  940. {
  941. I_TEST((MD5 Hashing | "a"));
  942. u8 result[] {
  943. 0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61
  944. };
  945. auto digest = Crypto::Hash::MD5::hash("a");
  946. if (memcmp(result, digest.data, Crypto::Hash::MD5::digest_size()) != 0) {
  947. FAIL(Invalid hash);
  948. print_buffer({ digest.data, Crypto::Hash::MD5::digest_size() }, -1);
  949. } else {
  950. PASS;
  951. }
  952. }
  953. {
  954. I_TEST((MD5 Hashing | "abcdefghijklmnopqrstuvwxyz"));
  955. u8 result[] {
  956. 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b
  957. };
  958. auto digest = Crypto::Hash::MD5::hash("abcdefghijklmnopqrstuvwxyz");
  959. if (memcmp(result, digest.data, Crypto::Hash::MD5::digest_size()) != 0) {
  960. FAIL(Invalid hash);
  961. print_buffer({ digest.data, Crypto::Hash::MD5::digest_size() }, -1);
  962. } else {
  963. PASS;
  964. }
  965. }
  966. {
  967. I_TEST((MD5 Hashing | Long Sequence));
  968. u8 result[] {
  969. 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55, 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a
  970. };
  971. auto digest = Crypto::Hash::MD5::hash("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
  972. if (memcmp(result, digest.data, Crypto::Hash::MD5::digest_size()) != 0) {
  973. FAIL(Invalid hash);
  974. print_buffer({ digest.data, Crypto::Hash::MD5::digest_size() }, -1);
  975. } else {
  976. PASS;
  977. }
  978. }
  979. }
  980. static void md5_test_consecutive_updates()
  981. {
  982. {
  983. I_TEST((MD5 Hashing | Multiple Updates));
  984. u8 result[] {
  985. 0xaf, 0x04, 0x3a, 0x08, 0x94, 0x38, 0x6e, 0x7f, 0xbf, 0x73, 0xe4, 0xaa, 0xf0, 0x8e, 0xee, 0x4c
  986. };
  987. Crypto::Hash::MD5 md5;
  988. md5.update("Well");
  989. md5.update(" hello ");
  990. md5.update("friends");
  991. auto digest = md5.digest();
  992. if (memcmp(result, digest.data, Crypto::Hash::MD5::digest_size()) != 0)
  993. FAIL(Invalid hash);
  994. else
  995. PASS;
  996. }
  997. {
  998. I_TEST((MD5 Hashing | Reuse));
  999. Crypto::Hash::MD5 md5;
  1000. md5.update("Well");
  1001. md5.update(" hello ");
  1002. md5.update("friends");
  1003. auto digest0 = md5.digest();
  1004. md5.update("Well");
  1005. md5.update(" hello ");
  1006. md5.update("friends");
  1007. auto digest1 = md5.digest();
  1008. if (memcmp(digest0.data, digest1.data, Crypto::Hash::MD5::digest_size()) != 0)
  1009. FAIL(Cannot reuse);
  1010. else
  1011. PASS;
  1012. }
  1013. }
  1014. static int hmac_md5_tests()
  1015. {
  1016. hmac_md5_test_name();
  1017. hmac_md5_test_process();
  1018. return g_some_test_failed ? 1 : 0;
  1019. }
  1020. static int hmac_sha256_tests()
  1021. {
  1022. hmac_sha256_test_name();
  1023. hmac_sha256_test_process();
  1024. return g_some_test_failed ? 1 : 0;
  1025. }
  1026. static int hmac_sha512_tests()
  1027. {
  1028. hmac_sha512_test_name();
  1029. hmac_sha512_test_process();
  1030. return g_some_test_failed ? 1 : 0;
  1031. }
  1032. static int hmac_sha1_tests()
  1033. {
  1034. hmac_sha1_test_name();
  1035. hmac_sha1_test_process();
  1036. return g_some_test_failed ? 1 : 0;
  1037. }
  1038. static void hmac_md5_test_name()
  1039. {
  1040. I_TEST((HMAC - MD5 | Class name));
  1041. Crypto::Authentication::HMAC<Crypto::Hash::MD5> hmac("Well Hello Friends");
  1042. if (hmac.class_name() != "HMAC-MD5")
  1043. FAIL(Invalid class name);
  1044. else
  1045. PASS;
  1046. }
  1047. static void hmac_md5_test_process()
  1048. {
  1049. {
  1050. I_TEST((HMAC - MD5 | Basic));
  1051. Crypto::Authentication::HMAC<Crypto::Hash::MD5> hmac("Well Hello Friends");
  1052. u8 result[] {
  1053. 0x3b, 0x5b, 0xde, 0x30, 0x3a, 0x54, 0x7b, 0xbb, 0x09, 0xfe, 0x78, 0x89, 0xbc, 0x9f, 0x22, 0xa3
  1054. };
  1055. auto mac = hmac.process("Some bogus data");
  1056. if (memcmp(result, mac.data, hmac.digest_size()) != 0) {
  1057. FAIL(Invalid mac);
  1058. print_buffer({ mac.data, hmac.digest_size() }, -1);
  1059. } else
  1060. PASS;
  1061. }
  1062. {
  1063. I_TEST((HMAC - MD5 | Reuse));
  1064. Crypto::Authentication::HMAC<Crypto::Hash::MD5> hmac("Well Hello Friends");
  1065. auto mac_0 = hmac.process("Some bogus data");
  1066. auto mac_1 = hmac.process("Some bogus data");
  1067. if (memcmp(mac_0.data, mac_1.data, hmac.digest_size()) != 0) {
  1068. FAIL(Cannot reuse);
  1069. } else
  1070. PASS;
  1071. }
  1072. }
  1073. static void hmac_sha1_test_name()
  1074. {
  1075. I_TEST((HMAC - SHA1 | Class name));
  1076. Crypto::Authentication::HMAC<Crypto::Hash::SHA1> hmac("Well Hello Friends");
  1077. if (hmac.class_name() != "HMAC-SHA1")
  1078. FAIL(Invalid class name);
  1079. else
  1080. PASS;
  1081. }
  1082. static void hmac_sha1_test_process()
  1083. {
  1084. {
  1085. I_TEST((HMAC - SHA1 | Basic));
  1086. u8 key[] { 0xc8, 0x52, 0xe5, 0x4a, 0x2c, 0x03, 0x2b, 0xc9, 0x63, 0xd3, 0xc2, 0x79, 0x0f, 0x76, 0x43, 0xef, 0x36, 0xc3, 0x7a, 0xca };
  1087. Crypto::Authentication::HMAC<Crypto::Hash::SHA1> hmac(ByteBuffer::wrap(key, 20));
  1088. u8 result[] {
  1089. 0x2c, 0x57, 0x32, 0x61, 0x3b, 0xa7, 0x84, 0x87, 0x0e, 0x4f, 0x42, 0x07, 0x2f, 0xf0, 0xe7, 0x41, 0xd7, 0x15, 0xf4, 0x56
  1090. };
  1091. u8 value[] {
  1092. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x03, 0x03, 0x00, 0x10, 0x14, 0x00, 0x00, 0x0c, 0xa1, 0x91, 0x1a, 0x20, 0x59, 0xb5, 0x45, 0xa9, 0xb4, 0xad, 0x75, 0x3e
  1093. };
  1094. auto mac = hmac.process(value, 29);
  1095. if (memcmp(result, mac.data, hmac.digest_size()) != 0) {
  1096. FAIL(Invalid mac);
  1097. print_buffer({ mac.data, hmac.digest_size() }, -1);
  1098. } else
  1099. PASS;
  1100. }
  1101. {
  1102. I_TEST((HMAC - SHA1 | Reuse));
  1103. u8 key[] { 0xc8, 0x52, 0xe5, 0x4a, 0x2c, 0x03, 0x2b, 0xc9, 0x63, 0xd3, 0xc2, 0x79, 0x0f, 0x76, 0x43, 0xef, 0x36, 0xc3, 0x7a, 0xca };
  1104. Crypto::Authentication::HMAC<Crypto::Hash::SHA1> hmac(ByteBuffer::wrap(key, 20));
  1105. u8 result[] {
  1106. 0x2c, 0x57, 0x32, 0x61, 0x3b, 0xa7, 0x84, 0x87, 0x0e, 0x4f, 0x42, 0x07, 0x2f, 0xf0, 0xe7, 0x41, 0xd7, 0x15, 0xf4, 0x56
  1107. };
  1108. u8 value[] {
  1109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x03, 0x03, 0x00, 0x10, 0x14, 0x00, 0x00, 0x0c, 0xa1, 0x91, 0x1a, 0x20, 0x59, 0xb5, 0x45, 0xa9, 0xb4, 0xad, 0x75, 0x3e
  1110. };
  1111. hmac.update(value, 8);
  1112. hmac.update(value + 8, 5);
  1113. hmac.update(value + 13, 16);
  1114. auto mac = hmac.digest();
  1115. if (memcmp(result, mac.data, hmac.digest_size()) != 0) {
  1116. FAIL(Invalid mac);
  1117. print_buffer({ mac.data, hmac.digest_size() }, -1);
  1118. } else
  1119. PASS;
  1120. }
  1121. }
  1122. static int sha1_tests()
  1123. {
  1124. sha1_test_name();
  1125. sha1_test_hash();
  1126. return g_some_test_failed ? 1 : 0;
  1127. }
  1128. static void sha1_test_name()
  1129. {
  1130. I_TEST((SHA1 class name));
  1131. Crypto::Hash::SHA1 sha;
  1132. if (sha.class_name() != "SHA1") {
  1133. FAIL(Invalid class name);
  1134. printf("%s\n", sha.class_name().characters());
  1135. } else
  1136. PASS;
  1137. }
  1138. static void sha1_test_hash()
  1139. {
  1140. {
  1141. I_TEST((SHA256 Hashing | ""));
  1142. u8 result[] {
  1143. 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09
  1144. };
  1145. auto digest = Crypto::Hash::SHA1::hash("");
  1146. if (memcmp(result, digest.data, Crypto::Hash::SHA1::digest_size()) != 0) {
  1147. FAIL(Invalid hash);
  1148. print_buffer({ digest.data, Crypto::Hash::SHA1::digest_size() }, -1);
  1149. } else
  1150. PASS;
  1151. }
  1152. {
  1153. I_TEST((SHA256 Hashing | Long String));
  1154. u8 result[] {
  1155. 0x12, 0x15, 0x1f, 0xb1, 0x04, 0x44, 0x93, 0xcc, 0xed, 0x54, 0xa6, 0xb8, 0x7e, 0x93, 0x37, 0x7b, 0xb2, 0x13, 0x39, 0xdb
  1156. };
  1157. auto digest = Crypto::Hash::SHA1::hash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  1158. if (memcmp(result, digest.data, Crypto::Hash::SHA1::digest_size()) != 0) {
  1159. FAIL(Invalid hash);
  1160. print_buffer({ digest.data, Crypto::Hash::SHA1::digest_size() }, -1);
  1161. } else
  1162. PASS;
  1163. }
  1164. {
  1165. I_TEST((SHA256 Hashing | Successive Updates));
  1166. u8 result[] {
  1167. 0xd6, 0x6e, 0xce, 0xd1, 0xf4, 0x08, 0xc6, 0xd8, 0x35, 0xab, 0xf0, 0xc9, 0x05, 0x26, 0xa4, 0xb2, 0xb8, 0xa3, 0x7c, 0xd3
  1168. };
  1169. auto hasher = Crypto::Hash::SHA1 {};
  1170. hasher.update("aaaaaaaaaaaaaaa");
  1171. hasher.update("aaaaaaaaaaaaaaa");
  1172. hasher.update("aaaaaaaaaaaaaaa");
  1173. hasher.update("aaaaaaaaaaaaaaa");
  1174. hasher.update("aaaaaaaaaaaaaaa");
  1175. hasher.update("aaaaaaaaaaaaaaa");
  1176. hasher.update("aaaaaaaaaaaaaaa");
  1177. hasher.update("aaaaaaaaaaaaaaa");
  1178. hasher.update("aaaaaaaaaaaaaaa");
  1179. hasher.update("aaaaaaaaaaaaaaa");
  1180. hasher.update("aaaaaaaaaaaaaaa");
  1181. hasher.update("aaaaaaaaaaaaaaa");
  1182. hasher.update("aaaaaaaaa");
  1183. auto digest = hasher.digest();
  1184. if (memcmp(result, digest.data, Crypto::Hash::SHA1::digest_size()) != 0) {
  1185. FAIL(Invalid hash);
  1186. print_buffer({ digest.data, Crypto::Hash::SHA1::digest_size() }, -1);
  1187. } else
  1188. PASS;
  1189. }
  1190. }
  1191. static int sha256_tests()
  1192. {
  1193. sha256_test_name();
  1194. sha256_test_hash();
  1195. return g_some_test_failed ? 1 : 0;
  1196. }
  1197. static void sha256_test_name()
  1198. {
  1199. I_TEST((SHA256 class name));
  1200. Crypto::Hash::SHA256 sha;
  1201. if (sha.class_name() != "SHA256") {
  1202. FAIL(Invalid class name);
  1203. printf("%s\n", sha.class_name().characters());
  1204. } else
  1205. PASS;
  1206. }
  1207. static void sha256_test_hash()
  1208. {
  1209. {
  1210. I_TEST((SHA256 Hashing | "Well hello friends"));
  1211. u8 result[] {
  1212. 0x9a, 0xcd, 0x50, 0xf9, 0xa2, 0xaf, 0x37, 0xe4, 0x71, 0xf7, 0x61, 0xc3, 0xfe, 0x7b, 0x8d, 0xea, 0x56, 0x17, 0xe5, 0x1d, 0xac, 0x80, 0x2f, 0xe6, 0xc1, 0x77, 0xb7, 0x4a, 0xbf, 0x0a, 0xbb, 0x5a
  1213. };
  1214. auto digest = Crypto::Hash::SHA256::hash("Well hello friends");
  1215. if (memcmp(result, digest.data, Crypto::Hash::SHA256::digest_size()) != 0) {
  1216. FAIL(Invalid hash);
  1217. print_buffer({ digest.data, Crypto::Hash::SHA256::digest_size() }, -1);
  1218. } else
  1219. PASS;
  1220. }
  1221. {
  1222. I_TEST((SHA256 Hashing | ""));
  1223. u8 result[] {
  1224. 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
  1225. };
  1226. auto digest = Crypto::Hash::SHA256::hash("");
  1227. if (memcmp(result, digest.data, Crypto::Hash::SHA256::digest_size()) != 0) {
  1228. FAIL(Invalid hash);
  1229. print_buffer({ digest.data, Crypto::Hash::SHA256::digest_size() }, -1);
  1230. } else
  1231. PASS;
  1232. }
  1233. }
  1234. static void hmac_sha256_test_name()
  1235. {
  1236. I_TEST((HMAC - SHA256 | Class name));
  1237. Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac("Well Hello Friends");
  1238. if (hmac.class_name() != "HMAC-SHA256")
  1239. FAIL(Invalid class name);
  1240. else
  1241. PASS;
  1242. }
  1243. static void hmac_sha256_test_process()
  1244. {
  1245. {
  1246. I_TEST((HMAC - SHA256 | Basic));
  1247. Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac("Well Hello Friends");
  1248. u8 result[] {
  1249. 0x1a, 0xf2, 0x20, 0x62, 0xde, 0x3b, 0x84, 0x65, 0xc1, 0x25, 0x23, 0x99, 0x76, 0x15, 0x1b, 0xec, 0x15, 0x21, 0x82, 0x1f, 0x23, 0xca, 0x11, 0x66, 0xdd, 0x8c, 0x6e, 0xf1, 0x81, 0x3b, 0x7f, 0x1b
  1250. };
  1251. auto mac = hmac.process("Some bogus data");
  1252. if (memcmp(result, mac.data, hmac.digest_size()) != 0) {
  1253. FAIL(Invalid mac);
  1254. print_buffer({ mac.data, hmac.digest_size() }, -1);
  1255. } else
  1256. PASS;
  1257. }
  1258. {
  1259. I_TEST((HMAC - SHA256 | DataSize > FinalBlockDataSize));
  1260. Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac("Well Hello Friends");
  1261. u8 result[] = {
  1262. 0x9b, 0xa3, 0x9e, 0xf3, 0xb4, 0x30, 0x5f, 0x6f, 0x67, 0xd0, 0xa8, 0xb0, 0xf0, 0xcb, 0x12, 0xf5, 0x85, 0xe2, 0x19, 0xba, 0x0c, 0x8b, 0xe5, 0x43, 0xf0, 0x93, 0x39, 0xa8, 0xa3, 0x07, 0xf1, 0x95
  1263. };
  1264. auto mac = hmac.process("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  1265. if (memcmp(result, mac.data, hmac.digest_size()) != 0) {
  1266. FAIL(Invalid mac);
  1267. print_buffer({ mac.data, hmac.digest_size() }, -1);
  1268. } else
  1269. PASS;
  1270. }
  1271. {
  1272. I_TEST((HMAC - SHA256 | DataSize == BlockSize));
  1273. Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac("Well Hello Friends");
  1274. u8 result[] = {
  1275. 0x1d, 0x90, 0xce, 0x68, 0x45, 0x0b, 0xba, 0xd6, 0xbe, 0x1c, 0xb2, 0x3a, 0xea, 0x7f, 0xac, 0x4b, 0x68, 0x08, 0xa4, 0x77, 0x81, 0x2a, 0xad, 0x5d, 0x05, 0xe2, 0x15, 0xe8, 0xf4, 0xcb, 0x06, 0xaf
  1276. };
  1277. auto mac = hmac.process("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  1278. if (memcmp(result, mac.data, hmac.digest_size()) != 0) {
  1279. FAIL(Invalid mac);
  1280. print_buffer({ mac.data, hmac.digest_size() }, -1);
  1281. } else
  1282. PASS;
  1283. }
  1284. {
  1285. I_TEST((HMAC - SHA256 | Reuse));
  1286. Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac("Well Hello Friends");
  1287. auto mac_0 = hmac.process("Some bogus data");
  1288. auto mac_1 = hmac.process("Some bogus data");
  1289. if (memcmp(mac_0.data, mac_1.data, hmac.digest_size()) != 0) {
  1290. FAIL(Cannot reuse);
  1291. } else
  1292. PASS;
  1293. }
  1294. }
  1295. static int sha512_tests()
  1296. {
  1297. sha512_test_name();
  1298. sha512_test_hash();
  1299. return g_some_test_failed ? 1 : 0;
  1300. }
  1301. static void sha512_test_name()
  1302. {
  1303. I_TEST((SHA512 class name));
  1304. Crypto::Hash::SHA512 sha;
  1305. if (sha.class_name() != "SHA512") {
  1306. FAIL(Invalid class name);
  1307. printf("%s\n", sha.class_name().characters());
  1308. } else
  1309. PASS;
  1310. }
  1311. static void sha512_test_hash()
  1312. {
  1313. {
  1314. I_TEST((SHA512 Hashing | "Well hello friends"));
  1315. u8 result[] {
  1316. 0x00, 0xfe, 0x68, 0x09, 0x71, 0x0e, 0xcb, 0x2b, 0xe9, 0x58, 0x00, 0x13, 0x69, 0x6a, 0x9e, 0x9e, 0xbd, 0x09, 0x1b, 0xfe, 0x14, 0xc9, 0x13, 0x82, 0xc7, 0x40, 0x34, 0xfe, 0xca, 0xe6, 0x87, 0xcb, 0x26, 0x36, 0x92, 0xe6, 0x34, 0x94, 0x3a, 0x11, 0xe5, 0xbb, 0xb5, 0xeb, 0x8e, 0x70, 0xef, 0x64, 0xca, 0xf7, 0x21, 0xb1, 0xde, 0xf2, 0x34, 0x85, 0x6f, 0xa8, 0x56, 0xd8, 0x23, 0xa1, 0x3b, 0x29
  1317. };
  1318. auto digest = Crypto::Hash::SHA512::hash("Well hello friends");
  1319. if (memcmp(result, digest.data, Crypto::Hash::SHA512::digest_size()) != 0) {
  1320. FAIL(Invalid hash);
  1321. print_buffer({ digest.data, Crypto::Hash::SHA512::digest_size() }, -1);
  1322. } else
  1323. PASS;
  1324. }
  1325. {
  1326. I_TEST((SHA512 Hashing | ""));
  1327. u8 result[] {
  1328. 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07, 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e
  1329. };
  1330. auto digest = Crypto::Hash::SHA512::hash("");
  1331. if (memcmp(result, digest.data, Crypto::Hash::SHA512::digest_size()) != 0) {
  1332. FAIL(Invalid hash);
  1333. print_buffer({ digest.data, Crypto::Hash::SHA512::digest_size() }, -1);
  1334. } else
  1335. PASS;
  1336. }
  1337. }
  1338. static void hmac_sha512_test_name()
  1339. {
  1340. I_TEST((HMAC - SHA512 | Class name));
  1341. Crypto::Authentication::HMAC<Crypto::Hash::SHA512> hmac("Well Hello Friends");
  1342. if (hmac.class_name() != "HMAC-SHA512")
  1343. FAIL(Invalid class name);
  1344. else
  1345. PASS;
  1346. }
  1347. static void hmac_sha512_test_process()
  1348. {
  1349. {
  1350. I_TEST((HMAC - SHA512 | Basic));
  1351. Crypto::Authentication::HMAC<Crypto::Hash::SHA512> hmac("Well Hello Friends");
  1352. u8 result[] {
  1353. 0xeb, 0xa8, 0x34, 0x11, 0xfd, 0x5b, 0x46, 0x5b, 0xef, 0xbb, 0x67, 0x5e, 0x7d, 0xc2, 0x7c, 0x2c, 0x6b, 0xe1, 0xcf, 0xe6, 0xc7, 0xe4, 0x7d, 0xeb, 0xca, 0x97, 0xb7, 0x4c, 0xd3, 0x4d, 0x6f, 0x08, 0x9f, 0x0d, 0x3a, 0xf1, 0xcb, 0x00, 0x79, 0x78, 0x2f, 0x05, 0x8e, 0xeb, 0x94, 0x48, 0x0d, 0x50, 0x64, 0x3b, 0xca, 0x70, 0xe2, 0x69, 0x38, 0x4f, 0xe4, 0xb0, 0x49, 0x0f, 0xc5, 0x4c, 0x7a, 0xa7
  1354. };
  1355. auto mac = hmac.process("Some bogus data");
  1356. if (memcmp(result, mac.data, hmac.digest_size()) != 0) {
  1357. FAIL(Invalid mac);
  1358. print_buffer({ mac.data, hmac.digest_size() }, -1);
  1359. } else
  1360. PASS;
  1361. }
  1362. {
  1363. I_TEST((HMAC - SHA512 | Reuse));
  1364. Crypto::Authentication::HMAC<Crypto::Hash::SHA512> hmac("Well Hello Friends");
  1365. auto mac_0 = hmac.process("Some bogus data");
  1366. auto mac_1 = hmac.process("Some bogus data");
  1367. if (memcmp(mac_0.data, mac_1.data, hmac.digest_size()) != 0) {
  1368. FAIL(Cannot reuse);
  1369. } else
  1370. PASS;
  1371. }
  1372. }
  1373. static int rsa_tests()
  1374. {
  1375. rsa_test_encrypt();
  1376. rsa_test_der_parse();
  1377. bigint_test_number_theory();
  1378. rsa_test_encrypt_decrypt();
  1379. rsa_emsa_pss_test_create();
  1380. return g_some_test_failed ? 1 : 0;
  1381. }
  1382. static void rsa_test_encrypt()
  1383. {
  1384. {
  1385. I_TEST((RSA RAW | Encryption));
  1386. ByteBuffer data { "hellohellohellohellohellohellohellohellohellohellohellohello123-"_b };
  1387. u8 result[] { 0x6f, 0x7b, 0xe2, 0xd3, 0x95, 0xf8, 0x8d, 0x87, 0x6d, 0x10, 0x5e, 0xc3, 0xcd, 0xf7, 0xbb, 0xa6, 0x62, 0x8e, 0x45, 0xa0, 0xf1, 0xe5, 0x0f, 0xdf, 0x69, 0xcb, 0xb6, 0xd5, 0x42, 0x06, 0x7d, 0x72, 0xa9, 0x5e, 0xae, 0xbf, 0xbf, 0x0f, 0xe0, 0xeb, 0x31, 0x31, 0xca, 0x8a, 0x81, 0x1e, 0xb9, 0xec, 0x6d, 0xcc, 0xb8, 0xa4, 0xac, 0xa3, 0x31, 0x05, 0xa9, 0xac, 0xc9, 0xd3, 0xe6, 0x2a, 0x18, 0xfe };
  1388. Crypto::PK::RSA rsa(
  1389. "8126832723025844890518845777858816391166654950553329127845898924164623511718747856014227624997335860970996746552094406240834082304784428582653994490504519"_bigint,
  1390. "4234603516465654167360850580101327813936403862038934287300450163438938741499875303761385527882335478349599685406941909381269804396099893549838642251053393"_bigint,
  1391. "65537"_bigint);
  1392. u8 buffer[rsa.output_size()];
  1393. auto buf = ByteBuffer::wrap(buffer, sizeof(buffer));
  1394. rsa.encrypt(data, buf);
  1395. if (memcmp(result, buf.data(), buf.size())) {
  1396. FAIL(Invalid encryption result);
  1397. print_buffer(buf, 16);
  1398. } else {
  1399. PASS;
  1400. }
  1401. }
  1402. {
  1403. I_TEST((RSA PKCS #1 1.5 | Encryption));
  1404. ByteBuffer data { "hellohellohellohellohellohellohellohellohello123-"_b };
  1405. Crypto::PK::RSA_PKCS1_EME rsa(
  1406. "8126832723025844890518845777858816391166654950553329127845898924164623511718747856014227624997335860970996746552094406240834082304784428582653994490504519"_bigint,
  1407. "4234603516465654167360850580101327813936403862038934287300450163438938741499875303761385527882335478349599685406941909381269804396099893549838642251053393"_bigint,
  1408. "65537"_bigint);
  1409. u8 buffer[rsa.output_size()];
  1410. auto buf = ByteBuffer::wrap(buffer, sizeof(buffer));
  1411. rsa.encrypt(data, buf);
  1412. rsa.decrypt(buf, buf);
  1413. if (memcmp(buf.data(), "hellohellohellohellohellohellohellohellohello123-", 49))
  1414. FAIL(Invalid encryption);
  1415. else {
  1416. dbg() << "out size " << buf.size() << " values: " << StringView { (char*)buf.data(), buf.size() };
  1417. PASS;
  1418. }
  1419. }
  1420. }
  1421. static void bigint_test_number_theory()
  1422. {
  1423. {
  1424. I_TEST((Number Theory | Modular Inverse));
  1425. if (Crypto::NumberTheory::ModularInverse(7, 87) == 25) {
  1426. PASS;
  1427. } else {
  1428. FAIL(Invalid result);
  1429. }
  1430. }
  1431. {
  1432. struct {
  1433. Crypto::UnsignedBigInteger base;
  1434. Crypto::UnsignedBigInteger exp;
  1435. Crypto::UnsignedBigInteger mod;
  1436. Crypto::UnsignedBigInteger expected;
  1437. } mod_pow_tests[] = {
  1438. { "2988348162058574136915891421498819466320163312926952423791023078876139"_bigint, "2351399303373464486466122544523690094744975233415544072992656881240319"_bigint, "10000"_bigint, "3059"_bigint },
  1439. { "24231"_bigint, "12448"_bigint, "14679"_bigint, "4428"_bigint },
  1440. { "1005404"_bigint, "8352654"_bigint, "8161408"_bigint, "2605696"_bigint },
  1441. { "3665005778"_bigint, "3244425589"_bigint, "565668506"_bigint, "524766494"_bigint },
  1442. { "10662083169959689657"_bigint, "11605678468317533000"_bigint, "1896834583057209739"_bigint, "1292743154593945858"_bigint },
  1443. { "99667739213529524852296932424683448520"_bigint, "123394910770101395416306279070921784207"_bigint, "238026722756504133786938677233768788719"_bigint, "197165477545023317459748215952393063201"_bigint },
  1444. { "49368547511968178788919424448914214709244872098814465088945281575062739912239"_bigint, "25201856190991298572337188495596990852134236115562183449699512394891190792064"_bigint, "45950460777961491021589776911422805972195170308651734432277141467904883064645"_bigint, "39917885806532796066922509794537889114718612292469285403012781055544152450051"_bigint },
  1445. { "48399385336454791246880286907257136254351739111892925951016159217090949616810"_bigint, "5758661760571644379364752528081901787573279669668889744323710906207949658569"_bigint, "32812120644405991429173950312949738783216437173380339653152625840449006970808"_bigint, "7948464125034399875323770213514649646309423451213282653637296324080400293584"_bigint },
  1446. };
  1447. for (auto test_case : mod_pow_tests) {
  1448. I_TEST((Number Theory | Modular Power));
  1449. auto actual = Crypto::NumberTheory::ModularPower(
  1450. test_case.base, test_case.exp, test_case.mod);
  1451. if (actual == test_case.expected) {
  1452. PASS;
  1453. } else {
  1454. FAIL(Wrong result);
  1455. printf("b: %s\ne: %s\nm: %s\nexpect: %s\nactual: %s\n",
  1456. test_case.base.to_base10().characters(), test_case.exp.to_base10().characters(), test_case.mod.to_base10().characters(), test_case.expected.to_base10().characters(), actual.to_base10().characters());
  1457. }
  1458. }
  1459. }
  1460. {
  1461. struct {
  1462. Crypto::UnsignedBigInteger candidate;
  1463. bool expected_result;
  1464. } primality_tests[] = {
  1465. { "1180591620717411303424"_bigint, false }, // 2**70
  1466. { "620448401733239439360000"_bigint, false }, // 25!
  1467. { "953962166440690129601298432"_bigint, false }, // 12**25
  1468. { "620448401733239439360000"_bigint, false }, // 25!
  1469. { "147926426347074375"_bigint, false }, // 35! / 2**32
  1470. { "340282366920938429742726440690708343523"_bigint, false }, // 2 factors near 2^64
  1471. { "73"_bigint, true },
  1472. { "6967"_bigint, true },
  1473. { "787649"_bigint, true },
  1474. { "73513949"_bigint, true },
  1475. { "6691236901"_bigint, true },
  1476. { "741387182759"_bigint, true },
  1477. { "67466615915827"_bigint, true },
  1478. { "9554317039214687"_bigint, true },
  1479. { "533344522150170391"_bigint, true },
  1480. { "18446744073709551557"_bigint, true }, // just below 2**64
  1481. };
  1482. for (auto test_case : primality_tests) {
  1483. I_TEST((Number Theory | Primality));
  1484. bool actual_result = Crypto::NumberTheory::is_probably_prime(test_case.candidate);
  1485. if (test_case.expected_result == actual_result) {
  1486. PASS;
  1487. } else {
  1488. FAIL(Wrong primality guess);
  1489. printf("The number %s is %sa prime, but the test said it is %sa prime!\n",
  1490. test_case.candidate.to_base10().characters(), test_case.expected_result ? "" : "not ", actual_result ? "" : "not ");
  1491. }
  1492. }
  1493. }
  1494. {
  1495. struct {
  1496. Crypto::UnsignedBigInteger min;
  1497. Crypto::UnsignedBigInteger max;
  1498. } primality_tests[] = {
  1499. { "1"_bigint, "1000000"_bigint },
  1500. { "10000000000"_bigint, "20000000000"_bigint },
  1501. { "1000"_bigint, "200000000000000000"_bigint },
  1502. { "200000000000000000"_bigint, "200000000000010000"_bigint },
  1503. };
  1504. for (auto test_case : primality_tests) {
  1505. I_TEST((Number Theory | Random numbers));
  1506. auto actual_result = Crypto::NumberTheory::random_number(test_case.min, test_case.max);
  1507. if (actual_result < test_case.min) {
  1508. FAIL(Too small);
  1509. printf("The generated number %s is smaller than the requested minimum %s. (max = %s)\n", actual_result.to_base10().characters(), test_case.min.to_base10().characters(), test_case.max.to_base10().characters());
  1510. } else if (!(actual_result < test_case.max)) {
  1511. FAIL(Too large);
  1512. printf("The generated number %s is larger-or-equal to the requested maximum %s. (min = %s)\n", actual_result.to_base10().characters(), test_case.max.to_base10().characters(), test_case.min.to_base10().characters());
  1513. } else {
  1514. PASS;
  1515. }
  1516. }
  1517. }
  1518. {
  1519. I_TEST((Number Theory | Random distribution));
  1520. auto actual_result = Crypto::NumberTheory::random_number(
  1521. "1"_bigint,
  1522. "100000000000000000000000000000"_bigint); // 10**29
  1523. if (actual_result < "100000000000000000000"_bigint) { // 10**20
  1524. FAIL(Too small);
  1525. printf("The generated number %s is extremely small. This *can* happen by pure chance, but should happen only once in a billion times. So it's probably an error.\n", actual_result.to_base10().characters());
  1526. } else if ("99999999900000000000000000000"_bigint < actual_result) { // 10**29 - 10**20
  1527. FAIL(Too large);
  1528. printf("The generated number %s is extremely large. This *can* happen by pure chance, but should happen only once in a billion times. So it's probably an error.\n", actual_result.to_base10().characters());
  1529. } else {
  1530. PASS;
  1531. }
  1532. }
  1533. }
  1534. static void rsa_emsa_pss_test_create()
  1535. {
  1536. {
  1537. // This is a template validity test
  1538. I_TEST((RSA EMSA_PSS | Construction));
  1539. Crypto::PK::RSA rsa;
  1540. Crypto::PK::RSA_EMSA_PSS<Crypto::Hash::SHA256> rsa_esma_pss(rsa);
  1541. PASS;
  1542. }
  1543. }
  1544. static void rsa_test_der_parse()
  1545. {
  1546. I_TEST((RSA | ASN1 DER / PEM encoded Key import));
  1547. auto privkey = R"(-----BEGIN RSA PRIVATE KEY-----
  1548. MIIBOgIBAAJBAJsrIYHxs1YL9tpfodaWs1lJoMdF4kgFisUFSj6nvBhJUlmBh607AlgTaX0E
  1549. DGPYycXYGZ2n6rqmms5lpDXBpUcCAwEAAQJAUNpPkmtEHDENxsoQBUXvXDYeXdePSiIBJhpU
  1550. joNOYoR5R9z5oX2cpcyykQ58FC2vKKg+x8N6xczG7qO95tw5UQIhAN354CP/FA+uTeJ6KJ+i
  1551. zCBCl58CjNCzO0s5HTc56el5AiEAsvPKXo5/9gS/S4UzDRP6abq7GreixTfjR8LXidk3FL8C
  1552. IQCTjYI861Y+hjMnlORkGSdvWlTHUj6gjEOh4TlWeJzQoQIgAxMZOQKtxCZUuxFwzRq4xLRG
  1553. nrDlBQpuxz7bwSyQO7UCIHrYMnDohgNbwtA5ZpW3H1cKKQQvueWm6sxW9P5sUrZ3
  1554. -----END RSA PRIVATE KEY-----)";
  1555. Crypto::PK::RSA rsa(privkey);
  1556. if (rsa.public_key().public_exponent() == 65537) {
  1557. if (rsa.private_key().private_exponent() == "4234603516465654167360850580101327813936403862038934287300450163438938741499875303761385527882335478349599685406941909381269804396099893549838642251053393"_bigint) {
  1558. PASS;
  1559. } else
  1560. FAIL(Invalid private exponent);
  1561. } else {
  1562. FAIL(Invalid public exponent);
  1563. }
  1564. }
  1565. static void rsa_test_encrypt_decrypt()
  1566. {
  1567. I_TEST((RSA | Encrypt));
  1568. dbg() << " creating rsa object";
  1569. Crypto::PK::RSA rsa(
  1570. "9527497237087650398000977129550904920919162360737979403539302312977329868395261515707123424679295515888026193056908173564681660256268221509339074678416049"_bigint,
  1571. "39542231845947188736992321577701849924317746648774438832456325878966594812143638244746284968851807975097653255909707366086606867657273809465195392910913"_bigint,
  1572. "65537"_bigint);
  1573. dbg() << "Output size: " << rsa.output_size();
  1574. auto dec = ByteBuffer::create_zeroed(rsa.output_size());
  1575. auto enc = ByteBuffer::create_zeroed(rsa.output_size());
  1576. enc.overwrite(0, "WellHelloFriendsWellHelloFriendsWellHelloFriendsWellHelloFriends", 64);
  1577. rsa.encrypt(enc, dec);
  1578. rsa.decrypt(dec, enc);
  1579. dbg() << "enc size " << enc.size() << " dec size " << dec.size();
  1580. if (memcmp(enc.data(), "WellHelloFriendsWellHelloFriendsWellHelloFriendsWellHelloFriends", 64) != 0) {
  1581. FAIL(Could not encrypt then decrypt);
  1582. } else {
  1583. PASS;
  1584. }
  1585. }
  1586. static int tls_tests()
  1587. {
  1588. tls_test_client_hello();
  1589. return g_some_test_failed ? 1 : 0;
  1590. }
  1591. static void tls_test_client_hello()
  1592. {
  1593. I_TEST((TLS | Connect and Data Transfer));
  1594. Core::EventLoop loop;
  1595. RefPtr<TLS::TLSv12> tls = TLS::TLSv12::construct(nullptr);
  1596. bool sent_request = false;
  1597. ByteBuffer contents = ByteBuffer::create_uninitialized(0);
  1598. tls->on_tls_ready_to_write = [&](TLS::TLSv12& tls) {
  1599. if (sent_request)
  1600. return;
  1601. sent_request = true;
  1602. if (!tls.write("GET / HTTP/1.1\r\nHost: "_b)) {
  1603. FAIL(write(0) failed);
  1604. loop.quit(0);
  1605. }
  1606. auto* the_server = (const u8*)(server ?: DEFAULT_SERVER);
  1607. if (!tls.write(ByteBuffer::wrap(const_cast<u8*>(the_server), strlen((const char*)the_server)))) {
  1608. FAIL(write(1) failed);
  1609. loop.quit(0);
  1610. }
  1611. if (!tls.write("\r\nConnection : close\r\n\r\n"_b)) {
  1612. FAIL(write(2) failed);
  1613. loop.quit(0);
  1614. }
  1615. };
  1616. tls->on_tls_ready_to_read = [&](TLS::TLSv12& tls) {
  1617. auto data = tls.read();
  1618. if (!data.has_value()) {
  1619. FAIL(No data received);
  1620. loop.quit(1);
  1621. } else {
  1622. // print_buffer(data.value(), 16);
  1623. contents.append(data.value().data(), data.value().size());
  1624. }
  1625. };
  1626. tls->on_tls_finished = [&] {
  1627. PASS;
  1628. auto file = Core::File::open("foo.response", Core::IODevice::WriteOnly);
  1629. if (file.is_error()) {
  1630. printf("Can't write there, %s\n", file.error().characters());
  1631. loop.quit(2);
  1632. return;
  1633. }
  1634. file.value()->write(contents);
  1635. file.value()->close();
  1636. loop.quit(0);
  1637. };
  1638. tls->on_tls_error = [&](TLS::AlertDescription) {
  1639. FAIL(Connection failure);
  1640. loop.quit(1);
  1641. };
  1642. if (!tls->connect(server ?: DEFAULT_SERVER, port)) {
  1643. FAIL(connect() failed);
  1644. return;
  1645. }
  1646. loop.exec();
  1647. }
  1648. static int adler32_tests()
  1649. {
  1650. auto do_test = [](ReadonlyBytes input, u32 expected_result) {
  1651. I_TEST((CRC32));
  1652. auto pass = Crypto::Checksum::Adler32(input).digest() == expected_result;
  1653. if (pass) {
  1654. PASS;
  1655. } else {
  1656. FAIL(Incorrect Result);
  1657. }
  1658. };
  1659. do_test(String("").bytes(), 0x1);
  1660. do_test(String("a").bytes(), 0x00620062);
  1661. do_test(String("abc").bytes(), 0x024d0127);
  1662. do_test(String("message digest").bytes(), 0x29750586);
  1663. do_test(String("abcdefghijklmnopqrstuvwxyz").bytes(), 0x90860b20);
  1664. return g_some_test_failed ? 1 : 0;
  1665. }
  1666. static int crc32_tests()
  1667. {
  1668. auto do_test = [](ReadonlyBytes input, u32 expected_result) {
  1669. I_TEST((Adler32));
  1670. auto pass = Crypto::Checksum::CRC32(input).digest() == expected_result;
  1671. if (pass) {
  1672. PASS;
  1673. } else {
  1674. FAIL(Incorrect Result);
  1675. }
  1676. };
  1677. do_test(String("").bytes(), 0x0);
  1678. do_test(String("The quick brown fox jumps over the lazy dog").bytes(), 0x414FA339);
  1679. do_test(String("various CRC algorithms input data").bytes(), 0x9BD366AE);
  1680. return g_some_test_failed ? 1 : 0;
  1681. }
  1682. static int bigint_tests()
  1683. {
  1684. bigint_test_fibo500();
  1685. bigint_addition_edgecases();
  1686. bigint_subtraction();
  1687. bigint_multiplication();
  1688. bigint_division();
  1689. bigint_base10();
  1690. bigint_import_export();
  1691. bigint_bitwise();
  1692. bigint_test_signed_fibo500();
  1693. bigint_signed_addition_edgecases();
  1694. bigint_signed_subtraction();
  1695. bigint_signed_multiplication();
  1696. bigint_signed_division();
  1697. bigint_signed_base10();
  1698. bigint_signed_import_export();
  1699. bigint_signed_bitwise();
  1700. return g_some_test_failed ? 1 : 0;
  1701. }
  1702. static Crypto::UnsignedBigInteger bigint_fibonacci(size_t n)
  1703. {
  1704. Crypto::UnsignedBigInteger num1(0);
  1705. Crypto::UnsignedBigInteger num2(1);
  1706. for (size_t i = 0; i < n; ++i) {
  1707. Crypto::UnsignedBigInteger t = num1.plus(num2);
  1708. num2 = num1;
  1709. num1 = t;
  1710. }
  1711. return num1;
  1712. }
  1713. static Crypto::SignedBigInteger bigint_signed_fibonacci(size_t n)
  1714. {
  1715. Crypto::SignedBigInteger num1(0);
  1716. Crypto::SignedBigInteger num2(1);
  1717. for (size_t i = 0; i < n; ++i) {
  1718. Crypto::SignedBigInteger t = num1.plus(num2);
  1719. num2 = num1;
  1720. num1 = t;
  1721. }
  1722. return num1;
  1723. }
  1724. static void bigint_test_fibo500()
  1725. {
  1726. {
  1727. I_TEST((BigInteger | Fibonacci500));
  1728. bool pass = (bigint_fibonacci(500).words() == AK::Vector<u32> { 315178285, 505575602, 1883328078, 125027121, 3649625763, 347570207, 74535262, 3832543808, 2472133297, 1600064941, 65273441 });
  1729. if (pass) {
  1730. PASS;
  1731. } else {
  1732. FAIL(Incorrect Result);
  1733. }
  1734. }
  1735. }
  1736. static void bigint_addition_edgecases()
  1737. {
  1738. {
  1739. I_TEST((BigInteger | Edge Cases));
  1740. Crypto::UnsignedBigInteger num1;
  1741. Crypto::UnsignedBigInteger num2(70);
  1742. Crypto::UnsignedBigInteger num3 = num1.plus(num2);
  1743. bool pass = (num3 == num2);
  1744. pass &= (num1 == Crypto::UnsignedBigInteger(0));
  1745. if (pass) {
  1746. PASS;
  1747. } else {
  1748. FAIL(Incorrect Result);
  1749. }
  1750. }
  1751. {
  1752. I_TEST((BigInteger | Borrow with zero));
  1753. Crypto::UnsignedBigInteger num1({ UINT32_MAX - 3, UINT32_MAX });
  1754. Crypto::UnsignedBigInteger num2({ UINT32_MAX - 2, 0 });
  1755. if (num1.plus(num2).words() == Vector<u32> { 4294967289, 0, 1 }) {
  1756. PASS;
  1757. } else {
  1758. FAIL(Incorrect Result);
  1759. }
  1760. }
  1761. }
  1762. static void bigint_subtraction()
  1763. {
  1764. {
  1765. I_TEST((BigInteger | Simple Subtraction 1));
  1766. Crypto::UnsignedBigInteger num1(80);
  1767. Crypto::UnsignedBigInteger num2(70);
  1768. if (num1.minus(num2) == Crypto::UnsignedBigInteger(10)) {
  1769. PASS;
  1770. } else {
  1771. FAIL(Incorrect Result);
  1772. }
  1773. }
  1774. {
  1775. I_TEST((BigInteger | Simple Subtraction 2));
  1776. Crypto::UnsignedBigInteger num1(50);
  1777. Crypto::UnsignedBigInteger num2(70);
  1778. if (num1.minus(num2).is_invalid()) {
  1779. PASS;
  1780. } else {
  1781. FAIL(Incorrect Result);
  1782. }
  1783. }
  1784. {
  1785. I_TEST((BigInteger | Subtraction with borrow));
  1786. Crypto::UnsignedBigInteger num1(UINT32_MAX);
  1787. Crypto::UnsignedBigInteger num2(1);
  1788. Crypto::UnsignedBigInteger num3 = num1.plus(num2);
  1789. Crypto::UnsignedBigInteger result = num3.minus(num2);
  1790. if (result == num1) {
  1791. PASS;
  1792. } else {
  1793. FAIL(Incorrect Result);
  1794. }
  1795. }
  1796. {
  1797. I_TEST((BigInteger | Subtraction with large numbers));
  1798. Crypto::UnsignedBigInteger num1 = bigint_fibonacci(343);
  1799. Crypto::UnsignedBigInteger num2 = bigint_fibonacci(218);
  1800. Crypto::UnsignedBigInteger result = num1.minus(num2);
  1801. if ((result.plus(num2) == num1)
  1802. && (result.words() == Vector<u32> { 811430588, 2958904896, 1130908877, 2830569969, 3243275482, 3047460725, 774025231, 7990 })) {
  1803. PASS;
  1804. } else {
  1805. FAIL(Incorrect Result);
  1806. }
  1807. }
  1808. {
  1809. I_TEST((BigInteger | Subtraction with large numbers 2));
  1810. Crypto::UnsignedBigInteger num1(Vector<u32> { 1483061863, 446680044, 1123294122, 191895498, 3347106536, 16, 0, 0, 0 });
  1811. Crypto::UnsignedBigInteger num2(Vector<u32> { 4196414175, 1117247942, 1123294122, 191895498, 3347106536, 16 });
  1812. Crypto::UnsignedBigInteger result = num1.minus(num2);
  1813. // this test only verifies that we don't crash on an assertion
  1814. PASS;
  1815. }
  1816. {
  1817. I_TEST((BigInteger | Subtraction Regression 1));
  1818. auto num = Crypto::UnsignedBigInteger { 1 }.shift_left(256);
  1819. if (num.minus(1).words() == Vector<u32> { 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 0 }) {
  1820. PASS;
  1821. } else {
  1822. FAIL(Incorrect Result);
  1823. }
  1824. }
  1825. }
  1826. static void bigint_multiplication()
  1827. {
  1828. {
  1829. I_TEST((BigInteger | Simple Multiplication));
  1830. Crypto::UnsignedBigInteger num1(8);
  1831. Crypto::UnsignedBigInteger num2(251);
  1832. Crypto::UnsignedBigInteger result = num1.multiplied_by(num2);
  1833. if (result.words() == Vector<u32> { 2008 }) {
  1834. PASS;
  1835. } else {
  1836. FAIL(Incorrect Result);
  1837. }
  1838. }
  1839. {
  1840. I_TEST((BigInteger | Multiplications with big numbers 1));
  1841. Crypto::UnsignedBigInteger num1 = bigint_fibonacci(200);
  1842. Crypto::UnsignedBigInteger num2(12345678);
  1843. Crypto::UnsignedBigInteger result = num1.multiplied_by(num2);
  1844. if (result.words() == Vector<u32> { 669961318, 143970113, 4028714974, 3164551305, 1589380278, 2 }) {
  1845. PASS;
  1846. } else {
  1847. FAIL(Incorrect Result);
  1848. }
  1849. }
  1850. {
  1851. I_TEST((BigInteger | Multiplications with big numbers 2));
  1852. Crypto::UnsignedBigInteger num1 = bigint_fibonacci(200);
  1853. Crypto::UnsignedBigInteger num2 = bigint_fibonacci(341);
  1854. Crypto::UnsignedBigInteger result = num1.multiplied_by(num2);
  1855. if (result.words() == Vector<u32> { 3017415433, 2741793511, 1957755698, 3731653885, 3154681877, 785762127, 3200178098, 4260616581, 529754471, 3632684436, 1073347813, 2516430 }) {
  1856. PASS;
  1857. } else {
  1858. FAIL(Incorrect Result);
  1859. }
  1860. }
  1861. }
  1862. static void bigint_division()
  1863. {
  1864. {
  1865. I_TEST((BigInteger | Simple Division));
  1866. Crypto::UnsignedBigInteger num1(27194);
  1867. Crypto::UnsignedBigInteger num2(251);
  1868. auto result = num1.divided_by(num2);
  1869. Crypto::UnsignedDivisionResult expected = { Crypto::UnsignedBigInteger(108), Crypto::UnsignedBigInteger(86) };
  1870. if (result.quotient == expected.quotient && result.remainder == expected.remainder) {
  1871. PASS;
  1872. } else {
  1873. FAIL(Incorrect Result);
  1874. }
  1875. }
  1876. {
  1877. I_TEST((BigInteger | Division with big numbers));
  1878. Crypto::UnsignedBigInteger num1 = bigint_fibonacci(386);
  1879. Crypto::UnsignedBigInteger num2 = bigint_fibonacci(238);
  1880. auto result = num1.divided_by(num2);
  1881. Crypto::UnsignedDivisionResult expected = {
  1882. Crypto::UnsignedBigInteger(Vector<u32> { 2300984486, 2637503534, 2022805584, 107 }),
  1883. Crypto::UnsignedBigInteger(Vector<u32> { 1483061863, 446680044, 1123294122, 191895498, 3347106536, 16, 0, 0, 0 })
  1884. };
  1885. if (result.quotient == expected.quotient && result.remainder == expected.remainder) {
  1886. PASS;
  1887. } else {
  1888. FAIL(Incorrect Result);
  1889. }
  1890. }
  1891. {
  1892. I_TEST((BigInteger | Combined test));
  1893. auto num1 = bigint_fibonacci(497);
  1894. auto num2 = bigint_fibonacci(238);
  1895. auto div_result = num1.divided_by(num2);
  1896. if (div_result.quotient.multiplied_by(num2).plus(div_result.remainder) == num1) {
  1897. PASS;
  1898. } else {
  1899. FAIL(Incorrect Result);
  1900. }
  1901. }
  1902. }
  1903. static void bigint_base10()
  1904. {
  1905. {
  1906. I_TEST((BigInteger | From String));
  1907. auto result = Crypto::UnsignedBigInteger::from_base10("57195071295721390579057195715793");
  1908. if (result.words() == Vector<u32> { 3806301393, 954919431, 3879607298, 721 }) {
  1909. PASS;
  1910. } else {
  1911. FAIL(Incorrect Result);
  1912. }
  1913. }
  1914. {
  1915. I_TEST((BigInteger | To String));
  1916. auto result = Crypto::UnsignedBigInteger { Vector<u32> { 3806301393, 954919431, 3879607298, 721 } }.to_base10();
  1917. if (result == "57195071295721390579057195715793") {
  1918. PASS;
  1919. } else {
  1920. FAIL(Incorrect Result);
  1921. }
  1922. }
  1923. }
  1924. static void bigint_import_export()
  1925. {
  1926. {
  1927. I_TEST((BigInteger | BigEndian Decode / Encode roundtrip));
  1928. u8 random_bytes[128];
  1929. u8 target_buffer[128];
  1930. AK::fill_with_random(random_bytes, 128);
  1931. auto encoded = Crypto::UnsignedBigInteger::import_data(random_bytes, 128);
  1932. encoded.export_data({ target_buffer, 128 });
  1933. if (memcmp(target_buffer, random_bytes, 128) != 0)
  1934. FAIL(Could not roundtrip);
  1935. else
  1936. PASS;
  1937. }
  1938. {
  1939. I_TEST((BigInteger | BigEndian Encode / Decode roundtrip));
  1940. u8 target_buffer[128];
  1941. auto encoded = "12345678901234567890"_bigint;
  1942. auto size = encoded.export_data({ target_buffer, 128 });
  1943. auto decoded = Crypto::UnsignedBigInteger::import_data(target_buffer, size);
  1944. if (encoded != decoded)
  1945. FAIL(Could not roundtrip);
  1946. else
  1947. PASS;
  1948. }
  1949. {
  1950. I_TEST((BigInteger | BigEndian Import));
  1951. auto number = Crypto::UnsignedBigInteger::import_data("hello");
  1952. if (number == "448378203247"_bigint) {
  1953. PASS;
  1954. } else {
  1955. FAIL(Invalid value);
  1956. }
  1957. }
  1958. {
  1959. I_TEST((BigInteger | BigEndian Export));
  1960. auto number = "448378203247"_bigint;
  1961. char exported[8] { 0 };
  1962. auto exported_length = number.export_data({ exported, 8 }, true);
  1963. if (exported_length == 5 && memcmp(exported + 3, "hello", 5) == 0) {
  1964. PASS;
  1965. } else {
  1966. FAIL(Invalid value);
  1967. print_buffer({ exported - exported_length + 8, exported_length }, -1);
  1968. }
  1969. }
  1970. }
  1971. static void bigint_bitwise()
  1972. {
  1973. {
  1974. I_TEST((BigInteger | Basic bitwise or));
  1975. auto num1 = "1234567"_bigint;
  1976. auto num2 = "1234567"_bigint;
  1977. if (num1.bitwise_or(num2) == num1) {
  1978. PASS;
  1979. } else {
  1980. FAIL(Invalid value);
  1981. }
  1982. }
  1983. {
  1984. I_TEST((BigInteger | Bitwise or handles different lengths));
  1985. auto num1 = "1234567"_bigint;
  1986. auto num2 = "123456789012345678901234567890"_bigint;
  1987. auto expected = "123456789012345678901234622167"_bigint;
  1988. auto result = num1.bitwise_or(num2);
  1989. if (result == expected) {
  1990. PASS;
  1991. } else {
  1992. FAIL(Invalid value);
  1993. }
  1994. }
  1995. {
  1996. I_TEST((BigInteger | Basic bitwise and));
  1997. auto num1 = "1234567"_bigint;
  1998. auto num2 = "1234561"_bigint;
  1999. if (num1.bitwise_and(num2) == "1234561"_bigint) {
  2000. PASS;
  2001. } else {
  2002. FAIL(Invalid value);
  2003. }
  2004. }
  2005. {
  2006. I_TEST((BigInteger | Bitwise and handles different lengths));
  2007. auto num1 = "1234567"_bigint;
  2008. auto num2 = "123456789012345678901234567890"_bigint;
  2009. if (num1.bitwise_and(num2) == "1180290"_bigint) {
  2010. PASS;
  2011. } else {
  2012. FAIL(Invalid value);
  2013. }
  2014. }
  2015. {
  2016. I_TEST((BigInteger | Basic bitwise xor));
  2017. auto num1 = "1234567"_bigint;
  2018. auto num2 = "1234561"_bigint;
  2019. if (num1.bitwise_xor(num2) == 6) {
  2020. PASS;
  2021. } else {
  2022. FAIL(Invalid value);
  2023. }
  2024. }
  2025. {
  2026. I_TEST((BigInteger | Bitwise xor handles different lengths));
  2027. auto num1 = "1234567"_bigint;
  2028. auto num2 = "123456789012345678901234567890"_bigint;
  2029. if (num1.bitwise_xor(num2) == "123456789012345678901233441877"_bigint) {
  2030. PASS;
  2031. } else {
  2032. FAIL(Invalid value);
  2033. }
  2034. }
  2035. }
  2036. static void bigint_test_signed_fibo500()
  2037. {
  2038. {
  2039. I_TEST((Signed BigInteger | Fibonacci500));
  2040. bool pass = (bigint_signed_fibonacci(500).unsigned_value().words() == AK::Vector<u32> { 315178285, 505575602, 1883328078, 125027121, 3649625763, 347570207, 74535262, 3832543808, 2472133297, 1600064941, 65273441 });
  2041. if (pass) {
  2042. PASS;
  2043. } else {
  2044. FAIL(Incorrect Result);
  2045. }
  2046. }
  2047. }
  2048. static void bigint_signed_addition_edgecases()
  2049. {
  2050. {
  2051. I_TEST((Signed BigInteger | Borrow with zero));
  2052. Crypto::SignedBigInteger num1 { Crypto::UnsignedBigInteger { { UINT32_MAX - 3, UINT32_MAX } }, false };
  2053. Crypto::SignedBigInteger num2 { Crypto::UnsignedBigInteger { UINT32_MAX - 2 }, false };
  2054. if (num1.plus(num2).unsigned_value().words() == Vector<u32> { 4294967289, 0, 1 }) {
  2055. PASS;
  2056. } else {
  2057. FAIL(Incorrect Result);
  2058. }
  2059. }
  2060. {
  2061. I_TEST((Signed BigInteger | Addition to other sign));
  2062. Crypto::SignedBigInteger num1 = INT32_MAX;
  2063. Crypto::SignedBigInteger num2 = num1;
  2064. num2.negate();
  2065. if (num1.plus(num2) == Crypto::SignedBigInteger { 0 }) {
  2066. PASS;
  2067. } else {
  2068. FAIL(Incorrect Result);
  2069. }
  2070. }
  2071. }
  2072. static void bigint_signed_subtraction()
  2073. {
  2074. {
  2075. I_TEST((Signed BigInteger | Simple Subtraction 1));
  2076. Crypto::SignedBigInteger num1(80);
  2077. Crypto::SignedBigInteger num2(70);
  2078. if (num1.minus(num2) == Crypto::SignedBigInteger(10)) {
  2079. PASS;
  2080. } else {
  2081. FAIL(Incorrect Result);
  2082. }
  2083. }
  2084. {
  2085. I_TEST((Signed BigInteger | Simple Subtraction 2));
  2086. Crypto::SignedBigInteger num1(50);
  2087. Crypto::SignedBigInteger num2(70);
  2088. if (num1.minus(num2) == Crypto::SignedBigInteger { -20 }) {
  2089. PASS;
  2090. } else {
  2091. FAIL(Incorrect Result);
  2092. }
  2093. }
  2094. {
  2095. I_TEST((Signed BigInteger | Subtraction with borrow));
  2096. Crypto::SignedBigInteger num1(Crypto::UnsignedBigInteger { UINT32_MAX });
  2097. Crypto::SignedBigInteger num2(1);
  2098. Crypto::SignedBigInteger num3 = num1.plus(num2);
  2099. Crypto::SignedBigInteger result = num2.minus(num3);
  2100. num1.negate();
  2101. if (result == num1) {
  2102. PASS;
  2103. } else {
  2104. FAIL(Incorrect Result);
  2105. }
  2106. }
  2107. {
  2108. I_TEST((Signed BigInteger | Subtraction with large numbers));
  2109. Crypto::SignedBigInteger num1 = bigint_signed_fibonacci(343);
  2110. Crypto::SignedBigInteger num2 = bigint_signed_fibonacci(218);
  2111. Crypto::SignedBigInteger result = num2.minus(num1);
  2112. auto expected = Crypto::UnsignedBigInteger { Vector<u32> { 811430588, 2958904896, 1130908877, 2830569969, 3243275482, 3047460725, 774025231, 7990 } };
  2113. if ((result.plus(num1) == num2)
  2114. && (result.unsigned_value() == expected)) {
  2115. PASS;
  2116. } else {
  2117. FAIL(Incorrect Result);
  2118. }
  2119. }
  2120. {
  2121. I_TEST((Signed BigInteger | Subtraction with large numbers 2));
  2122. Crypto::SignedBigInteger num1(Crypto::UnsignedBigInteger { Vector<u32> { 1483061863, 446680044, 1123294122, 191895498, 3347106536, 16, 0, 0, 0 } });
  2123. Crypto::SignedBigInteger num2(Crypto::UnsignedBigInteger { Vector<u32> { 4196414175, 1117247942, 1123294122, 191895498, 3347106536, 16 } });
  2124. Crypto::SignedBigInteger result = num1.minus(num2);
  2125. // this test only verifies that we don't crash on an assertion
  2126. PASS;
  2127. }
  2128. }
  2129. static void bigint_signed_multiplication()
  2130. {
  2131. {
  2132. I_TEST((Signed BigInteger | Simple Multiplication));
  2133. Crypto::SignedBigInteger num1(8);
  2134. Crypto::SignedBigInteger num2(-251);
  2135. Crypto::SignedBigInteger result = num1.multiplied_by(num2);
  2136. if (result == Crypto::SignedBigInteger { -2008 }) {
  2137. PASS;
  2138. } else {
  2139. FAIL(Incorrect Result);
  2140. }
  2141. }
  2142. {
  2143. I_TEST((Signed BigInteger | Multiplications with big numbers 1));
  2144. Crypto::SignedBigInteger num1 = bigint_signed_fibonacci(200);
  2145. Crypto::SignedBigInteger num2(-12345678);
  2146. Crypto::SignedBigInteger result = num1.multiplied_by(num2);
  2147. if (result.unsigned_value().words() == Vector<u32> { 669961318, 143970113, 4028714974, 3164551305, 1589380278, 2 } && result.is_negative()) {
  2148. PASS;
  2149. } else {
  2150. FAIL(Incorrect Result);
  2151. }
  2152. }
  2153. {
  2154. I_TEST((Signed BigInteger | Multiplications with big numbers 2));
  2155. Crypto::SignedBigInteger num1 = bigint_signed_fibonacci(200);
  2156. Crypto::SignedBigInteger num2 = bigint_signed_fibonacci(341);
  2157. num1.negate();
  2158. Crypto::SignedBigInteger result = num1.multiplied_by(num2);
  2159. if (result.unsigned_value().words() == Vector<u32> { 3017415433, 2741793511, 1957755698, 3731653885, 3154681877, 785762127, 3200178098, 4260616581, 529754471, 3632684436, 1073347813, 2516430 } && result.is_negative()) {
  2160. PASS;
  2161. } else {
  2162. FAIL(Incorrect Result);
  2163. }
  2164. }
  2165. }
  2166. static void bigint_signed_division()
  2167. {
  2168. {
  2169. I_TEST((Signed BigInteger | Simple Division));
  2170. Crypto::SignedBigInteger num1(27194);
  2171. Crypto::SignedBigInteger num2(-251);
  2172. auto result = num1.divided_by(num2);
  2173. Crypto::SignedDivisionResult expected = { Crypto::SignedBigInteger(-108), Crypto::SignedBigInteger(86) };
  2174. if (result.quotient == expected.quotient && result.remainder == expected.remainder) {
  2175. PASS;
  2176. } else {
  2177. FAIL(Incorrect Result);
  2178. }
  2179. }
  2180. {
  2181. I_TEST((Signed BigInteger | Division with big numbers));
  2182. Crypto::SignedBigInteger num1 = bigint_signed_fibonacci(386);
  2183. Crypto::SignedBigInteger num2 = bigint_signed_fibonacci(238);
  2184. num1.negate();
  2185. auto result = num1.divided_by(num2);
  2186. Crypto::SignedDivisionResult expected = {
  2187. Crypto::SignedBigInteger(Crypto::UnsignedBigInteger { Vector<u32> { 2300984486, 2637503534, 2022805584, 107 } }, true),
  2188. Crypto::SignedBigInteger(Crypto::UnsignedBigInteger { Vector<u32> { 1483061863, 446680044, 1123294122, 191895498, 3347106536, 16, 0, 0, 0 } }, true)
  2189. };
  2190. if (result.quotient == expected.quotient && result.remainder == expected.remainder) {
  2191. PASS;
  2192. } else {
  2193. FAIL(Incorrect Result);
  2194. }
  2195. }
  2196. {
  2197. I_TEST((Signed BigInteger | Combined test));
  2198. auto num1 = bigint_signed_fibonacci(497);
  2199. auto num2 = bigint_signed_fibonacci(238);
  2200. num1.negate();
  2201. auto div_result = num1.divided_by(num2);
  2202. if (div_result.quotient.multiplied_by(num2).plus(div_result.remainder) == num1) {
  2203. PASS;
  2204. } else {
  2205. FAIL(Incorrect Result);
  2206. }
  2207. }
  2208. }
  2209. static void bigint_signed_base10()
  2210. {
  2211. {
  2212. I_TEST((Signed BigInteger | From String));
  2213. auto result = Crypto::SignedBigInteger::from_base10("-57195071295721390579057195715793");
  2214. if (result.unsigned_value().words() == Vector<u32> { 3806301393, 954919431, 3879607298, 721 } && result.is_negative()) {
  2215. PASS;
  2216. } else {
  2217. FAIL(Incorrect Result);
  2218. }
  2219. }
  2220. {
  2221. I_TEST((Signed BigInteger | To String));
  2222. auto result = Crypto::SignedBigInteger { Crypto::UnsignedBigInteger { Vector<u32> { 3806301393, 954919431, 3879607298, 721 } }, true }.to_base10();
  2223. if (result == "-57195071295721390579057195715793") {
  2224. PASS;
  2225. } else {
  2226. FAIL(Incorrect Result);
  2227. }
  2228. }
  2229. }
  2230. static void bigint_signed_import_export()
  2231. {
  2232. {
  2233. I_TEST((Signed BigInteger | BigEndian Decode / Encode roundtrip));
  2234. u8 random_bytes[129];
  2235. u8 target_buffer[129];
  2236. random_bytes[0] = 1;
  2237. AK::fill_with_random(random_bytes + 1, 128);
  2238. auto encoded = Crypto::SignedBigInteger::import_data(random_bytes, 129);
  2239. encoded.export_data({ target_buffer, 129 });
  2240. if (memcmp(target_buffer, random_bytes, 129) != 0)
  2241. FAIL(Could not roundtrip);
  2242. else
  2243. PASS;
  2244. }
  2245. {
  2246. I_TEST((Signed BigInteger | BigEndian Encode / Decode roundtrip));
  2247. u8 target_buffer[128];
  2248. auto encoded = "-12345678901234567890"_sbigint;
  2249. auto size = encoded.export_data({ target_buffer, 128 });
  2250. auto decoded = Crypto::SignedBigInteger::import_data(target_buffer, size);
  2251. if (encoded != decoded)
  2252. FAIL(Could not roundtrip);
  2253. else
  2254. PASS;
  2255. }
  2256. }
  2257. static void bigint_signed_bitwise()
  2258. {
  2259. {
  2260. I_TEST((Signed BigInteger | Bitwise or handles sign));
  2261. auto num1 = "-1234567"_sbigint;
  2262. auto num2 = "1234567"_sbigint;
  2263. if (num1.bitwise_or(num2) == num1) {
  2264. PASS;
  2265. } else {
  2266. FAIL(Invalid value);
  2267. }
  2268. }
  2269. }