test-crypto.cpp 108 KB

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