speed.c 165 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811
  1. /*
  2. * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #undef SECONDS
  11. #define SECONDS 3
  12. #define PKEY_SECONDS 10
  13. #define RSA_SECONDS PKEY_SECONDS
  14. #define DSA_SECONDS PKEY_SECONDS
  15. #define ECDSA_SECONDS PKEY_SECONDS
  16. #define ECDH_SECONDS PKEY_SECONDS
  17. #define EdDSA_SECONDS PKEY_SECONDS
  18. #define SM2_SECONDS PKEY_SECONDS
  19. #define FFDH_SECONDS PKEY_SECONDS
  20. #define KEM_SECONDS PKEY_SECONDS
  21. #define SIG_SECONDS PKEY_SECONDS
  22. #define MAX_ALGNAME_SUFFIX 100
  23. /* We need to use some deprecated APIs */
  24. #define OPENSSL_SUPPRESS_DEPRECATED
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <math.h>
  29. #include "apps.h"
  30. #include "progs.h"
  31. #include "internal/nelem.h"
  32. #include "internal/numbers.h"
  33. #include <openssl/crypto.h>
  34. #include <openssl/rand.h>
  35. #include <openssl/err.h>
  36. #include <openssl/evp.h>
  37. #include <openssl/objects.h>
  38. #include <openssl/core_names.h>
  39. #include <openssl/async.h>
  40. #include <openssl/provider.h>
  41. #if !defined(OPENSSL_SYS_MSDOS)
  42. # include <unistd.h>
  43. #endif
  44. #if defined(__TANDEM)
  45. # if defined(OPENSSL_TANDEM_FLOSS)
  46. # include <floss.h(floss_fork)>
  47. # endif
  48. #endif
  49. #if defined(_WIN32)
  50. # include <windows.h>
  51. /*
  52. * While VirtualLock is available under the app partition (e.g. UWP),
  53. * the headers do not define the API. Define it ourselves instead.
  54. */
  55. WINBASEAPI
  56. BOOL
  57. WINAPI
  58. VirtualLock(
  59. _In_ LPVOID lpAddress,
  60. _In_ SIZE_T dwSize
  61. );
  62. #endif
  63. #if defined(OPENSSL_SYS_LINUX)
  64. # include <sys/mman.h>
  65. #endif
  66. #include <openssl/bn.h>
  67. #include <openssl/rsa.h>
  68. #include "./testrsa.h"
  69. #ifndef OPENSSL_NO_DH
  70. # include <openssl/dh.h>
  71. #endif
  72. #include <openssl/x509.h>
  73. #include <openssl/dsa.h>
  74. #include "./testdsa.h"
  75. #include <openssl/modes.h>
  76. #ifndef HAVE_FORK
  77. # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VXWORKS)
  78. # define HAVE_FORK 0
  79. # else
  80. # define HAVE_FORK 1
  81. # include <sys/wait.h>
  82. # endif
  83. #endif
  84. #if HAVE_FORK
  85. # undef NO_FORK
  86. #else
  87. # define NO_FORK
  88. #endif
  89. #define MAX_MISALIGNMENT 63
  90. #define MAX_ECDH_SIZE 256
  91. #define MISALIGN 64
  92. #define MAX_FFDH_SIZE 1024
  93. #ifndef RSA_DEFAULT_PRIME_NUM
  94. # define RSA_DEFAULT_PRIME_NUM 2
  95. #endif
  96. typedef struct openssl_speed_sec_st {
  97. int sym;
  98. int rsa;
  99. int dsa;
  100. int ecdsa;
  101. int ecdh;
  102. int eddsa;
  103. int sm2;
  104. int ffdh;
  105. int kem;
  106. int sig;
  107. } openssl_speed_sec_t;
  108. static volatile int run = 0;
  109. static int mr = 0; /* machine-readeable output format to merge fork results */
  110. static int usertime = 1;
  111. static double Time_F(int s);
  112. static void print_message(const char *s, int length, int tm);
  113. static void pkey_print_message(const char *str, const char *str2,
  114. unsigned int bits, int sec);
  115. static void kskey_print_message(const char *str, const char *str2, int tm);
  116. static void print_result(int alg, int run_no, int count, double time_used);
  117. #ifndef NO_FORK
  118. static int do_multi(int multi, int size_num);
  119. #endif
  120. static int domlock = 0;
  121. static const int lengths_list[] = {
  122. 16, 64, 256, 1024, 8 * 1024, 16 * 1024
  123. };
  124. #define SIZE_NUM OSSL_NELEM(lengths_list)
  125. static const int *lengths = lengths_list;
  126. static const int aead_lengths_list[] = {
  127. 2, 31, 136, 1024, 8 * 1024, 16 * 1024
  128. };
  129. #define START 0
  130. #define STOP 1
  131. #ifdef SIGALRM
  132. static void alarmed(ossl_unused int sig)
  133. {
  134. signal(SIGALRM, alarmed);
  135. run = 0;
  136. }
  137. static double Time_F(int s)
  138. {
  139. double ret = app_tminterval(s, usertime);
  140. if (s == STOP)
  141. alarm(0);
  142. return ret;
  143. }
  144. #elif defined(_WIN32)
  145. # define SIGALRM -1
  146. static unsigned int lapse;
  147. static volatile unsigned int schlock;
  148. static void alarm_win32(unsigned int secs)
  149. {
  150. lapse = secs * 1000;
  151. }
  152. # define alarm alarm_win32
  153. static DWORD WINAPI sleepy(VOID * arg)
  154. {
  155. schlock = 1;
  156. Sleep(lapse);
  157. run = 0;
  158. return 0;
  159. }
  160. static double Time_F(int s)
  161. {
  162. double ret;
  163. static HANDLE thr;
  164. if (s == START) {
  165. schlock = 0;
  166. thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
  167. if (thr == NULL) {
  168. DWORD err = GetLastError();
  169. BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
  170. ExitProcess(err);
  171. }
  172. while (!schlock)
  173. Sleep(0); /* scheduler spinlock */
  174. ret = app_tminterval(s, usertime);
  175. } else {
  176. ret = app_tminterval(s, usertime);
  177. if (run)
  178. TerminateThread(thr, 0);
  179. CloseHandle(thr);
  180. }
  181. return ret;
  182. }
  183. #else
  184. # error "SIGALRM not defined and the platform is not Windows"
  185. #endif
  186. static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
  187. const openssl_speed_sec_t *seconds);
  188. static int opt_found(const char *name, unsigned int *result,
  189. const OPT_PAIR pairs[], unsigned int nbelem)
  190. {
  191. unsigned int idx;
  192. for (idx = 0; idx < nbelem; ++idx, pairs++)
  193. if (strcmp(name, pairs->name) == 0) {
  194. *result = pairs->retval;
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. #define opt_found(value, pairs, result)\
  200. opt_found(value, result, pairs, OSSL_NELEM(pairs))
  201. typedef enum OPTION_choice {
  202. OPT_COMMON,
  203. OPT_ELAPSED, OPT_EVP, OPT_HMAC, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
  204. OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM, OPT_PROV_ENUM, OPT_CONFIG,
  205. OPT_PRIMES, OPT_SECONDS, OPT_BYTES, OPT_AEAD, OPT_CMAC, OPT_MLOCK, OPT_KEM, OPT_SIG
  206. } OPTION_CHOICE;
  207. const OPTIONS speed_options[] = {
  208. {OPT_HELP_STR, 1, '-',
  209. "Usage: %s [options] [algorithm...]\n"
  210. "All +int options consider prefix '0' as base-8 input, "
  211. "prefix '0x'/'0X' as base-16 input.\n"
  212. },
  213. OPT_SECTION("General"),
  214. {"help", OPT_HELP, '-', "Display this summary"},
  215. {"mb", OPT_MB, '-',
  216. "Enable (tls1>=1) multi-block mode on EVP-named cipher"},
  217. {"mr", OPT_MR, '-', "Produce machine readable output"},
  218. #ifndef NO_FORK
  219. {"multi", OPT_MULTI, 'p', "Run benchmarks in parallel"},
  220. #endif
  221. #ifndef OPENSSL_NO_ASYNC
  222. {"async_jobs", OPT_ASYNCJOBS, 'p',
  223. "Enable async mode and start specified number of jobs"},
  224. #endif
  225. #ifndef OPENSSL_NO_ENGINE
  226. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  227. #endif
  228. {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
  229. {"mlock", OPT_MLOCK, '-', "Lock memory for better result determinism"},
  230. OPT_CONFIG_OPTION,
  231. OPT_SECTION("Selection"),
  232. {"evp", OPT_EVP, 's', "Use EVP-named cipher or digest"},
  233. {"hmac", OPT_HMAC, 's', "HMAC using EVP-named digest"},
  234. {"cmac", OPT_CMAC, 's', "CMAC using EVP-named cipher"},
  235. {"decrypt", OPT_DECRYPT, '-',
  236. "Time decryption instead of encryption (only EVP)"},
  237. {"aead", OPT_AEAD, '-',
  238. "Benchmark EVP-named AEAD cipher in TLS-like sequence"},
  239. {"kem-algorithms", OPT_KEM, '-',
  240. "Benchmark KEM algorithms"},
  241. {"signature-algorithms", OPT_SIG, '-',
  242. "Benchmark signature algorithms"},
  243. OPT_SECTION("Timing"),
  244. {"elapsed", OPT_ELAPSED, '-',
  245. "Use wall-clock time instead of CPU user time as divisor"},
  246. {"seconds", OPT_SECONDS, 'p',
  247. "Run benchmarks for specified amount of seconds"},
  248. {"bytes", OPT_BYTES, 'p',
  249. "Run [non-PKI] benchmarks on custom-sized buffer"},
  250. {"misalign", OPT_MISALIGN, 'p',
  251. "Use specified offset to mis-align buffers"},
  252. OPT_R_OPTIONS,
  253. OPT_PROV_OPTIONS,
  254. OPT_PARAMETERS(),
  255. {"algorithm", 0, 0, "Algorithm(s) to test (optional; otherwise tests all)"},
  256. {NULL}
  257. };
  258. enum {
  259. D_MD2, D_MDC2, D_MD4, D_MD5, D_SHA1, D_RMD160,
  260. D_SHA256, D_SHA512, D_WHIRLPOOL, D_HMAC,
  261. D_CBC_DES, D_EDE3_DES, D_RC4, D_CBC_IDEA, D_CBC_SEED,
  262. D_CBC_RC2, D_CBC_RC5, D_CBC_BF, D_CBC_CAST,
  263. D_CBC_128_AES, D_CBC_192_AES, D_CBC_256_AES,
  264. D_CBC_128_CML, D_CBC_192_CML, D_CBC_256_CML,
  265. D_EVP, D_GHASH, D_RAND, D_EVP_CMAC, ALGOR_NUM
  266. };
  267. /* name of algorithms to test. MUST BE KEEP IN SYNC with above enum ! */
  268. static const char *names[ALGOR_NUM] = {
  269. "md2", "mdc2", "md4", "md5", "sha1", "rmd160",
  270. "sha256", "sha512", "whirlpool", "hmac(md5)",
  271. "des-cbc", "des-ede3", "rc4", "idea-cbc", "seed-cbc",
  272. "rc2-cbc", "rc5-cbc", "blowfish", "cast-cbc",
  273. "aes-128-cbc", "aes-192-cbc", "aes-256-cbc",
  274. "camellia-128-cbc", "camellia-192-cbc", "camellia-256-cbc",
  275. "evp", "ghash", "rand", "cmac"
  276. };
  277. /* list of configured algorithm (remaining), with some few alias */
  278. static const OPT_PAIR doit_choices[] = {
  279. {"md2", D_MD2},
  280. {"mdc2", D_MDC2},
  281. {"md4", D_MD4},
  282. {"md5", D_MD5},
  283. {"hmac", D_HMAC},
  284. {"sha1", D_SHA1},
  285. {"sha256", D_SHA256},
  286. {"sha512", D_SHA512},
  287. {"whirlpool", D_WHIRLPOOL},
  288. {"ripemd", D_RMD160},
  289. {"rmd160", D_RMD160},
  290. {"ripemd160", D_RMD160},
  291. {"rc4", D_RC4},
  292. {"des-cbc", D_CBC_DES},
  293. {"des-ede3", D_EDE3_DES},
  294. {"aes-128-cbc", D_CBC_128_AES},
  295. {"aes-192-cbc", D_CBC_192_AES},
  296. {"aes-256-cbc", D_CBC_256_AES},
  297. {"camellia-128-cbc", D_CBC_128_CML},
  298. {"camellia-192-cbc", D_CBC_192_CML},
  299. {"camellia-256-cbc", D_CBC_256_CML},
  300. {"rc2-cbc", D_CBC_RC2},
  301. {"rc2", D_CBC_RC2},
  302. {"rc5-cbc", D_CBC_RC5},
  303. {"rc5", D_CBC_RC5},
  304. {"idea-cbc", D_CBC_IDEA},
  305. {"idea", D_CBC_IDEA},
  306. {"seed-cbc", D_CBC_SEED},
  307. {"seed", D_CBC_SEED},
  308. {"bf-cbc", D_CBC_BF},
  309. {"blowfish", D_CBC_BF},
  310. {"bf", D_CBC_BF},
  311. {"cast-cbc", D_CBC_CAST},
  312. {"cast", D_CBC_CAST},
  313. {"cast5", D_CBC_CAST},
  314. {"ghash", D_GHASH},
  315. {"rand", D_RAND}
  316. };
  317. static double results[ALGOR_NUM][SIZE_NUM];
  318. enum { R_DSA_1024, R_DSA_2048, DSA_NUM };
  319. static const OPT_PAIR dsa_choices[DSA_NUM] = {
  320. {"dsa1024", R_DSA_1024},
  321. {"dsa2048", R_DSA_2048}
  322. };
  323. static double dsa_results[DSA_NUM][2]; /* 2 ops: sign then verify */
  324. enum {
  325. R_RSA_512, R_RSA_1024, R_RSA_2048, R_RSA_3072, R_RSA_4096, R_RSA_7680,
  326. R_RSA_15360, RSA_NUM
  327. };
  328. static const OPT_PAIR rsa_choices[RSA_NUM] = {
  329. {"rsa512", R_RSA_512},
  330. {"rsa1024", R_RSA_1024},
  331. {"rsa2048", R_RSA_2048},
  332. {"rsa3072", R_RSA_3072},
  333. {"rsa4096", R_RSA_4096},
  334. {"rsa7680", R_RSA_7680},
  335. {"rsa15360", R_RSA_15360}
  336. };
  337. static double rsa_results[RSA_NUM][4]; /* 4 ops: sign, verify, encrypt, decrypt */
  338. #ifndef OPENSSL_NO_DH
  339. enum ff_params_t {
  340. R_FFDH_2048, R_FFDH_3072, R_FFDH_4096, R_FFDH_6144, R_FFDH_8192, FFDH_NUM
  341. };
  342. static const OPT_PAIR ffdh_choices[FFDH_NUM] = {
  343. {"ffdh2048", R_FFDH_2048},
  344. {"ffdh3072", R_FFDH_3072},
  345. {"ffdh4096", R_FFDH_4096},
  346. {"ffdh6144", R_FFDH_6144},
  347. {"ffdh8192", R_FFDH_8192},
  348. };
  349. static double ffdh_results[FFDH_NUM][1]; /* 1 op: derivation */
  350. #endif /* OPENSSL_NO_DH */
  351. enum ec_curves_t {
  352. R_EC_P160, R_EC_P192, R_EC_P224, R_EC_P256, R_EC_P384, R_EC_P521,
  353. #ifndef OPENSSL_NO_EC2M
  354. R_EC_K163, R_EC_K233, R_EC_K283, R_EC_K409, R_EC_K571,
  355. R_EC_B163, R_EC_B233, R_EC_B283, R_EC_B409, R_EC_B571,
  356. #endif
  357. R_EC_BRP256R1, R_EC_BRP256T1, R_EC_BRP384R1, R_EC_BRP384T1,
  358. R_EC_BRP512R1, R_EC_BRP512T1, ECDSA_NUM
  359. };
  360. /* list of ecdsa curves */
  361. static const OPT_PAIR ecdsa_choices[ECDSA_NUM] = {
  362. {"ecdsap160", R_EC_P160},
  363. {"ecdsap192", R_EC_P192},
  364. {"ecdsap224", R_EC_P224},
  365. {"ecdsap256", R_EC_P256},
  366. {"ecdsap384", R_EC_P384},
  367. {"ecdsap521", R_EC_P521},
  368. #ifndef OPENSSL_NO_EC2M
  369. {"ecdsak163", R_EC_K163},
  370. {"ecdsak233", R_EC_K233},
  371. {"ecdsak283", R_EC_K283},
  372. {"ecdsak409", R_EC_K409},
  373. {"ecdsak571", R_EC_K571},
  374. {"ecdsab163", R_EC_B163},
  375. {"ecdsab233", R_EC_B233},
  376. {"ecdsab283", R_EC_B283},
  377. {"ecdsab409", R_EC_B409},
  378. {"ecdsab571", R_EC_B571},
  379. #endif
  380. {"ecdsabrp256r1", R_EC_BRP256R1},
  381. {"ecdsabrp256t1", R_EC_BRP256T1},
  382. {"ecdsabrp384r1", R_EC_BRP384R1},
  383. {"ecdsabrp384t1", R_EC_BRP384T1},
  384. {"ecdsabrp512r1", R_EC_BRP512R1},
  385. {"ecdsabrp512t1", R_EC_BRP512T1}
  386. };
  387. enum {
  388. #ifndef OPENSSL_NO_ECX
  389. R_EC_X25519 = ECDSA_NUM, R_EC_X448, EC_NUM
  390. #else
  391. EC_NUM = ECDSA_NUM
  392. #endif
  393. };
  394. /* list of ecdh curves, extension of |ecdsa_choices| list above */
  395. static const OPT_PAIR ecdh_choices[EC_NUM] = {
  396. {"ecdhp160", R_EC_P160},
  397. {"ecdhp192", R_EC_P192},
  398. {"ecdhp224", R_EC_P224},
  399. {"ecdhp256", R_EC_P256},
  400. {"ecdhp384", R_EC_P384},
  401. {"ecdhp521", R_EC_P521},
  402. #ifndef OPENSSL_NO_EC2M
  403. {"ecdhk163", R_EC_K163},
  404. {"ecdhk233", R_EC_K233},
  405. {"ecdhk283", R_EC_K283},
  406. {"ecdhk409", R_EC_K409},
  407. {"ecdhk571", R_EC_K571},
  408. {"ecdhb163", R_EC_B163},
  409. {"ecdhb233", R_EC_B233},
  410. {"ecdhb283", R_EC_B283},
  411. {"ecdhb409", R_EC_B409},
  412. {"ecdhb571", R_EC_B571},
  413. #endif
  414. {"ecdhbrp256r1", R_EC_BRP256R1},
  415. {"ecdhbrp256t1", R_EC_BRP256T1},
  416. {"ecdhbrp384r1", R_EC_BRP384R1},
  417. {"ecdhbrp384t1", R_EC_BRP384T1},
  418. {"ecdhbrp512r1", R_EC_BRP512R1},
  419. {"ecdhbrp512t1", R_EC_BRP512T1},
  420. #ifndef OPENSSL_NO_ECX
  421. {"ecdhx25519", R_EC_X25519},
  422. {"ecdhx448", R_EC_X448}
  423. #endif
  424. };
  425. static double ecdh_results[EC_NUM][1]; /* 1 op: derivation */
  426. static double ecdsa_results[ECDSA_NUM][2]; /* 2 ops: sign then verify */
  427. #ifndef OPENSSL_NO_ECX
  428. enum { R_EC_Ed25519, R_EC_Ed448, EdDSA_NUM };
  429. static const OPT_PAIR eddsa_choices[EdDSA_NUM] = {
  430. {"ed25519", R_EC_Ed25519},
  431. {"ed448", R_EC_Ed448}
  432. };
  433. static double eddsa_results[EdDSA_NUM][2]; /* 2 ops: sign then verify */
  434. #endif /* OPENSSL_NO_ECX */
  435. #ifndef OPENSSL_NO_SM2
  436. enum { R_EC_CURVESM2, SM2_NUM };
  437. static const OPT_PAIR sm2_choices[SM2_NUM] = {
  438. {"curveSM2", R_EC_CURVESM2}
  439. };
  440. # define SM2_ID "TLSv1.3+GM+Cipher+Suite"
  441. # define SM2_ID_LEN sizeof("TLSv1.3+GM+Cipher+Suite") - 1
  442. static double sm2_results[SM2_NUM][2]; /* 2 ops: sign then verify */
  443. #endif /* OPENSSL_NO_SM2 */
  444. #define MAX_KEM_NUM 111
  445. static size_t kems_algs_len = 0;
  446. static char *kems_algname[MAX_KEM_NUM] = { NULL };
  447. static double kems_results[MAX_KEM_NUM][3]; /* keygen, encaps, decaps */
  448. #define MAX_SIG_NUM 111
  449. static size_t sigs_algs_len = 0;
  450. static char *sigs_algname[MAX_SIG_NUM] = { NULL };
  451. static double sigs_results[MAX_SIG_NUM][3]; /* keygen, sign, verify */
  452. #define COND(unused_cond) (run && count < INT_MAX)
  453. #define COUNT(d) (count)
  454. typedef struct loopargs_st {
  455. ASYNC_JOB *inprogress_job;
  456. ASYNC_WAIT_CTX *wait_ctx;
  457. unsigned char *buf;
  458. unsigned char *buf2;
  459. unsigned char *buf_malloc;
  460. unsigned char *buf2_malloc;
  461. unsigned char *key;
  462. size_t buflen;
  463. size_t sigsize;
  464. size_t encsize;
  465. EVP_PKEY_CTX *rsa_sign_ctx[RSA_NUM];
  466. EVP_PKEY_CTX *rsa_verify_ctx[RSA_NUM];
  467. EVP_PKEY_CTX *rsa_encrypt_ctx[RSA_NUM];
  468. EVP_PKEY_CTX *rsa_decrypt_ctx[RSA_NUM];
  469. EVP_PKEY_CTX *dsa_sign_ctx[DSA_NUM];
  470. EVP_PKEY_CTX *dsa_verify_ctx[DSA_NUM];
  471. EVP_PKEY_CTX *ecdsa_sign_ctx[ECDSA_NUM];
  472. EVP_PKEY_CTX *ecdsa_verify_ctx[ECDSA_NUM];
  473. EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
  474. #ifndef OPENSSL_NO_ECX
  475. EVP_MD_CTX *eddsa_ctx[EdDSA_NUM];
  476. EVP_MD_CTX *eddsa_ctx2[EdDSA_NUM];
  477. #endif /* OPENSSL_NO_ECX */
  478. #ifndef OPENSSL_NO_SM2
  479. EVP_MD_CTX *sm2_ctx[SM2_NUM];
  480. EVP_MD_CTX *sm2_vfy_ctx[SM2_NUM];
  481. EVP_PKEY *sm2_pkey[SM2_NUM];
  482. #endif
  483. unsigned char *secret_a;
  484. unsigned char *secret_b;
  485. size_t outlen[EC_NUM];
  486. #ifndef OPENSSL_NO_DH
  487. EVP_PKEY_CTX *ffdh_ctx[FFDH_NUM];
  488. unsigned char *secret_ff_a;
  489. unsigned char *secret_ff_b;
  490. #endif
  491. EVP_CIPHER_CTX *ctx;
  492. EVP_MAC_CTX *mctx;
  493. EVP_PKEY_CTX *kem_gen_ctx[MAX_KEM_NUM];
  494. EVP_PKEY_CTX *kem_encaps_ctx[MAX_KEM_NUM];
  495. EVP_PKEY_CTX *kem_decaps_ctx[MAX_KEM_NUM];
  496. size_t kem_out_len[MAX_KEM_NUM];
  497. size_t kem_secret_len[MAX_KEM_NUM];
  498. unsigned char *kem_out[MAX_KEM_NUM];
  499. unsigned char *kem_send_secret[MAX_KEM_NUM];
  500. unsigned char *kem_rcv_secret[MAX_KEM_NUM];
  501. EVP_PKEY_CTX *sig_gen_ctx[MAX_KEM_NUM];
  502. EVP_PKEY_CTX *sig_sign_ctx[MAX_KEM_NUM];
  503. EVP_PKEY_CTX *sig_verify_ctx[MAX_KEM_NUM];
  504. size_t sig_max_sig_len[MAX_KEM_NUM];
  505. size_t sig_act_sig_len[MAX_KEM_NUM];
  506. unsigned char *sig_sig[MAX_KEM_NUM];
  507. } loopargs_t;
  508. static int run_benchmark(int async_jobs, int (*loop_function) (void *),
  509. loopargs_t *loopargs);
  510. static unsigned int testnum;
  511. static char *evp_mac_mdname = "md5";
  512. static char *evp_hmac_name = NULL;
  513. static const char *evp_md_name = NULL;
  514. static char *evp_mac_ciphername = "aes-128-cbc";
  515. static char *evp_cmac_name = NULL;
  516. static int have_md(const char *name)
  517. {
  518. int ret = 0;
  519. EVP_MD *md = NULL;
  520. if (opt_md_silent(name, &md)) {
  521. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  522. if (ctx != NULL && EVP_DigestInit(ctx, md) > 0)
  523. ret = 1;
  524. EVP_MD_CTX_free(ctx);
  525. EVP_MD_free(md);
  526. }
  527. return ret;
  528. }
  529. static int have_cipher(const char *name)
  530. {
  531. int ret = 0;
  532. EVP_CIPHER *cipher = NULL;
  533. if (opt_cipher_silent(name, &cipher)) {
  534. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  535. if (ctx != NULL
  536. && EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) > 0)
  537. ret = 1;
  538. EVP_CIPHER_CTX_free(ctx);
  539. EVP_CIPHER_free(cipher);
  540. }
  541. return ret;
  542. }
  543. static int EVP_Digest_loop(const char *mdname, ossl_unused int algindex, void *args)
  544. {
  545. loopargs_t *tempargs = *(loopargs_t **) args;
  546. unsigned char *buf = tempargs->buf;
  547. unsigned char digest[EVP_MAX_MD_SIZE];
  548. int count;
  549. EVP_MD *md = NULL;
  550. if (!opt_md_silent(mdname, &md))
  551. return -1;
  552. for (count = 0; COND(c[algindex][testnum]); count++) {
  553. if (!EVP_Digest(buf, (size_t)lengths[testnum], digest, NULL, md,
  554. NULL)) {
  555. count = -1;
  556. break;
  557. }
  558. }
  559. EVP_MD_free(md);
  560. return count;
  561. }
  562. static int EVP_Digest_md_loop(void *args)
  563. {
  564. return EVP_Digest_loop(evp_md_name, D_EVP, args);
  565. }
  566. static int EVP_Digest_MD2_loop(void *args)
  567. {
  568. return EVP_Digest_loop("md2", D_MD2, args);
  569. }
  570. static int EVP_Digest_MDC2_loop(void *args)
  571. {
  572. return EVP_Digest_loop("mdc2", D_MDC2, args);
  573. }
  574. static int EVP_Digest_MD4_loop(void *args)
  575. {
  576. return EVP_Digest_loop("md4", D_MD4, args);
  577. }
  578. static int MD5_loop(void *args)
  579. {
  580. return EVP_Digest_loop("md5", D_MD5, args);
  581. }
  582. static int EVP_MAC_loop(ossl_unused int algindex, void *args)
  583. {
  584. loopargs_t *tempargs = *(loopargs_t **) args;
  585. unsigned char *buf = tempargs->buf;
  586. EVP_MAC_CTX *mctx = tempargs->mctx;
  587. unsigned char mac[EVP_MAX_MD_SIZE];
  588. int count;
  589. for (count = 0; COND(c[algindex][testnum]); count++) {
  590. size_t outl;
  591. if (!EVP_MAC_init(mctx, NULL, 0, NULL)
  592. || !EVP_MAC_update(mctx, buf, lengths[testnum])
  593. || !EVP_MAC_final(mctx, mac, &outl, sizeof(mac)))
  594. return -1;
  595. }
  596. return count;
  597. }
  598. static int HMAC_loop(void *args)
  599. {
  600. return EVP_MAC_loop(D_HMAC, args);
  601. }
  602. static int CMAC_loop(void *args)
  603. {
  604. return EVP_MAC_loop(D_EVP_CMAC, args);
  605. }
  606. static int SHA1_loop(void *args)
  607. {
  608. return EVP_Digest_loop("sha1", D_SHA1, args);
  609. }
  610. static int SHA256_loop(void *args)
  611. {
  612. return EVP_Digest_loop("sha256", D_SHA256, args);
  613. }
  614. static int SHA512_loop(void *args)
  615. {
  616. return EVP_Digest_loop("sha512", D_SHA512, args);
  617. }
  618. static int WHIRLPOOL_loop(void *args)
  619. {
  620. return EVP_Digest_loop("whirlpool", D_WHIRLPOOL, args);
  621. }
  622. static int EVP_Digest_RMD160_loop(void *args)
  623. {
  624. return EVP_Digest_loop("ripemd160", D_RMD160, args);
  625. }
  626. static int algindex;
  627. static int EVP_Cipher_loop(void *args)
  628. {
  629. loopargs_t *tempargs = *(loopargs_t **) args;
  630. unsigned char *buf = tempargs->buf;
  631. int count;
  632. if (tempargs->ctx == NULL)
  633. return -1;
  634. for (count = 0; COND(c[algindex][testnum]); count++)
  635. if (EVP_Cipher(tempargs->ctx, buf, buf, (size_t)lengths[testnum]) <= 0)
  636. return -1;
  637. return count;
  638. }
  639. static int GHASH_loop(void *args)
  640. {
  641. loopargs_t *tempargs = *(loopargs_t **) args;
  642. unsigned char *buf = tempargs->buf;
  643. EVP_MAC_CTX *mctx = tempargs->mctx;
  644. int count;
  645. /* just do the update in the loop to be comparable with 1.1.1 */
  646. for (count = 0; COND(c[D_GHASH][testnum]); count++) {
  647. if (!EVP_MAC_update(mctx, buf, lengths[testnum]))
  648. return -1;
  649. }
  650. return count;
  651. }
  652. #define MAX_BLOCK_SIZE 128
  653. static unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
  654. static EVP_CIPHER_CTX *init_evp_cipher_ctx(const char *ciphername,
  655. const unsigned char *key,
  656. int keylen)
  657. {
  658. EVP_CIPHER_CTX *ctx = NULL;
  659. EVP_CIPHER *cipher = NULL;
  660. if (!opt_cipher_silent(ciphername, &cipher))
  661. return NULL;
  662. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  663. goto end;
  664. if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1)) {
  665. EVP_CIPHER_CTX_free(ctx);
  666. ctx = NULL;
  667. goto end;
  668. }
  669. if (EVP_CIPHER_CTX_set_key_length(ctx, keylen) <= 0) {
  670. EVP_CIPHER_CTX_free(ctx);
  671. ctx = NULL;
  672. goto end;
  673. }
  674. if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1)) {
  675. EVP_CIPHER_CTX_free(ctx);
  676. ctx = NULL;
  677. goto end;
  678. }
  679. end:
  680. EVP_CIPHER_free(cipher);
  681. return ctx;
  682. }
  683. static int RAND_bytes_loop(void *args)
  684. {
  685. loopargs_t *tempargs = *(loopargs_t **) args;
  686. unsigned char *buf = tempargs->buf;
  687. int count;
  688. for (count = 0; COND(c[D_RAND][testnum]); count++)
  689. RAND_bytes(buf, lengths[testnum]);
  690. return count;
  691. }
  692. static int decrypt = 0;
  693. static int EVP_Update_loop(void *args)
  694. {
  695. loopargs_t *tempargs = *(loopargs_t **) args;
  696. unsigned char *buf = tempargs->buf;
  697. EVP_CIPHER_CTX *ctx = tempargs->ctx;
  698. int outl, count, rc;
  699. unsigned char faketag[16] = { 0xcc };
  700. if (decrypt) {
  701. if (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER) {
  702. (void)EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(faketag), faketag);
  703. }
  704. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  705. rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
  706. if (rc != 1) {
  707. /* reset iv in case of counter overflow */
  708. rc = EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
  709. }
  710. }
  711. } else {
  712. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  713. rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
  714. if (rc != 1) {
  715. /* reset iv in case of counter overflow */
  716. rc = EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
  717. }
  718. }
  719. }
  720. if (decrypt)
  721. rc = EVP_DecryptFinal_ex(ctx, buf, &outl);
  722. else
  723. rc = EVP_EncryptFinal_ex(ctx, buf, &outl);
  724. if (rc == 0)
  725. BIO_printf(bio_err, "Error finalizing cipher loop\n");
  726. return count;
  727. }
  728. /*
  729. * CCM does not support streaming. For the purpose of performance measurement,
  730. * each message is encrypted using the same (key,iv)-pair. Do not use this
  731. * code in your application.
  732. */
  733. static int EVP_Update_loop_ccm(void *args)
  734. {
  735. loopargs_t *tempargs = *(loopargs_t **) args;
  736. unsigned char *buf = tempargs->buf;
  737. EVP_CIPHER_CTX *ctx = tempargs->ctx;
  738. int outl, count, realcount = 0, final;
  739. unsigned char tag[12];
  740. if (decrypt) {
  741. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  742. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag),
  743. tag) > 0
  744. /* reset iv */
  745. && EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) > 0
  746. /* counter is reset on every update */
  747. && EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]) > 0)
  748. realcount++;
  749. }
  750. } else {
  751. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  752. /* restore iv length field */
  753. if (EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]) > 0
  754. /* counter is reset on every update */
  755. && EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]) > 0)
  756. realcount++;
  757. }
  758. }
  759. if (decrypt)
  760. final = EVP_DecryptFinal_ex(ctx, buf, &outl);
  761. else
  762. final = EVP_EncryptFinal_ex(ctx, buf, &outl);
  763. if (final == 0)
  764. BIO_printf(bio_err, "Error finalizing ccm loop\n");
  765. return realcount;
  766. }
  767. /*
  768. * To make AEAD benchmarking more relevant perform TLS-like operations,
  769. * 13-byte AAD followed by payload. But don't use TLS-formatted AAD, as
  770. * payload length is not actually limited by 16KB...
  771. */
  772. static int EVP_Update_loop_aead(void *args)
  773. {
  774. loopargs_t *tempargs = *(loopargs_t **) args;
  775. unsigned char *buf = tempargs->buf;
  776. EVP_CIPHER_CTX *ctx = tempargs->ctx;
  777. int outl, count, realcount = 0;
  778. unsigned char aad[13] = { 0xcc };
  779. unsigned char faketag[16] = { 0xcc };
  780. if (decrypt) {
  781. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  782. if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) > 0
  783. && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  784. sizeof(faketag), faketag) > 0
  785. && EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad)) > 0
  786. && EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]) > 0
  787. && EVP_DecryptFinal_ex(ctx, buf + outl, &outl) > 0)
  788. realcount++;
  789. }
  790. } else {
  791. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  792. if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) > 0
  793. && EVP_EncryptUpdate(ctx, NULL, &outl, aad, sizeof(aad)) > 0
  794. && EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]) > 0
  795. && EVP_EncryptFinal_ex(ctx, buf + outl, &outl) > 0)
  796. realcount++;
  797. }
  798. }
  799. return realcount;
  800. }
  801. static int RSA_sign_loop(void *args)
  802. {
  803. loopargs_t *tempargs = *(loopargs_t **) args;
  804. unsigned char *buf = tempargs->buf;
  805. unsigned char *buf2 = tempargs->buf2;
  806. size_t *rsa_num = &tempargs->sigsize;
  807. EVP_PKEY_CTX **rsa_sign_ctx = tempargs->rsa_sign_ctx;
  808. int ret, count;
  809. for (count = 0; COND(rsa_c[testnum][0]); count++) {
  810. *rsa_num = tempargs->buflen;
  811. ret = EVP_PKEY_sign(rsa_sign_ctx[testnum], buf2, rsa_num, buf, 36);
  812. if (ret <= 0) {
  813. BIO_printf(bio_err, "RSA sign failure\n");
  814. ERR_print_errors(bio_err);
  815. count = -1;
  816. break;
  817. }
  818. }
  819. return count;
  820. }
  821. static int RSA_verify_loop(void *args)
  822. {
  823. loopargs_t *tempargs = *(loopargs_t **) args;
  824. unsigned char *buf = tempargs->buf;
  825. unsigned char *buf2 = tempargs->buf2;
  826. size_t rsa_num = tempargs->sigsize;
  827. EVP_PKEY_CTX **rsa_verify_ctx = tempargs->rsa_verify_ctx;
  828. int ret, count;
  829. for (count = 0; COND(rsa_c[testnum][1]); count++) {
  830. ret = EVP_PKEY_verify(rsa_verify_ctx[testnum], buf2, rsa_num, buf, 36);
  831. if (ret <= 0) {
  832. BIO_printf(bio_err, "RSA verify failure\n");
  833. ERR_print_errors(bio_err);
  834. count = -1;
  835. break;
  836. }
  837. }
  838. return count;
  839. }
  840. static int RSA_encrypt_loop(void *args)
  841. {
  842. loopargs_t *tempargs = *(loopargs_t **) args;
  843. unsigned char *buf = tempargs->buf;
  844. unsigned char *buf2 = tempargs->buf2;
  845. size_t *rsa_num = &tempargs->encsize;
  846. EVP_PKEY_CTX **rsa_encrypt_ctx = tempargs->rsa_encrypt_ctx;
  847. int ret, count;
  848. for (count = 0; COND(rsa_c[testnum][2]); count++) {
  849. *rsa_num = tempargs->buflen;
  850. ret = EVP_PKEY_encrypt(rsa_encrypt_ctx[testnum], buf2, rsa_num, buf, 36);
  851. if (ret <= 0) {
  852. BIO_printf(bio_err, "RSA encrypt failure\n");
  853. ERR_print_errors(bio_err);
  854. count = -1;
  855. break;
  856. }
  857. }
  858. return count;
  859. }
  860. static int RSA_decrypt_loop(void *args)
  861. {
  862. loopargs_t *tempargs = *(loopargs_t **) args;
  863. unsigned char *buf = tempargs->buf;
  864. unsigned char *buf2 = tempargs->buf2;
  865. size_t rsa_num;
  866. EVP_PKEY_CTX **rsa_decrypt_ctx = tempargs->rsa_decrypt_ctx;
  867. int ret, count;
  868. for (count = 0; COND(rsa_c[testnum][3]); count++) {
  869. rsa_num = tempargs->buflen;
  870. ret = EVP_PKEY_decrypt(rsa_decrypt_ctx[testnum], buf, &rsa_num, buf2, tempargs->encsize);
  871. if (ret <= 0) {
  872. BIO_printf(bio_err, "RSA decrypt failure\n");
  873. ERR_print_errors(bio_err);
  874. count = -1;
  875. break;
  876. }
  877. }
  878. return count;
  879. }
  880. #ifndef OPENSSL_NO_DH
  881. static int FFDH_derive_key_loop(void *args)
  882. {
  883. loopargs_t *tempargs = *(loopargs_t **) args;
  884. EVP_PKEY_CTX *ffdh_ctx = tempargs->ffdh_ctx[testnum];
  885. unsigned char *derived_secret = tempargs->secret_ff_a;
  886. int count;
  887. for (count = 0; COND(ffdh_c[testnum][0]); count++) {
  888. /* outlen can be overwritten with a too small value (no padding used) */
  889. size_t outlen = MAX_FFDH_SIZE;
  890. EVP_PKEY_derive(ffdh_ctx, derived_secret, &outlen);
  891. }
  892. return count;
  893. }
  894. #endif /* OPENSSL_NO_DH */
  895. static int DSA_sign_loop(void *args)
  896. {
  897. loopargs_t *tempargs = *(loopargs_t **) args;
  898. unsigned char *buf = tempargs->buf;
  899. unsigned char *buf2 = tempargs->buf2;
  900. size_t *dsa_num = &tempargs->sigsize;
  901. EVP_PKEY_CTX **dsa_sign_ctx = tempargs->dsa_sign_ctx;
  902. int ret, count;
  903. for (count = 0; COND(dsa_c[testnum][0]); count++) {
  904. *dsa_num = tempargs->buflen;
  905. ret = EVP_PKEY_sign(dsa_sign_ctx[testnum], buf2, dsa_num, buf, 20);
  906. if (ret <= 0) {
  907. BIO_printf(bio_err, "DSA sign failure\n");
  908. ERR_print_errors(bio_err);
  909. count = -1;
  910. break;
  911. }
  912. }
  913. return count;
  914. }
  915. static int DSA_verify_loop(void *args)
  916. {
  917. loopargs_t *tempargs = *(loopargs_t **) args;
  918. unsigned char *buf = tempargs->buf;
  919. unsigned char *buf2 = tempargs->buf2;
  920. size_t dsa_num = tempargs->sigsize;
  921. EVP_PKEY_CTX **dsa_verify_ctx = tempargs->dsa_verify_ctx;
  922. int ret, count;
  923. for (count = 0; COND(dsa_c[testnum][1]); count++) {
  924. ret = EVP_PKEY_verify(dsa_verify_ctx[testnum], buf2, dsa_num, buf, 20);
  925. if (ret <= 0) {
  926. BIO_printf(bio_err, "DSA verify failure\n");
  927. ERR_print_errors(bio_err);
  928. count = -1;
  929. break;
  930. }
  931. }
  932. return count;
  933. }
  934. static int ECDSA_sign_loop(void *args)
  935. {
  936. loopargs_t *tempargs = *(loopargs_t **) args;
  937. unsigned char *buf = tempargs->buf;
  938. unsigned char *buf2 = tempargs->buf2;
  939. size_t *ecdsa_num = &tempargs->sigsize;
  940. EVP_PKEY_CTX **ecdsa_sign_ctx = tempargs->ecdsa_sign_ctx;
  941. int ret, count;
  942. for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
  943. *ecdsa_num = tempargs->buflen;
  944. ret = EVP_PKEY_sign(ecdsa_sign_ctx[testnum], buf2, ecdsa_num, buf, 20);
  945. if (ret <= 0) {
  946. BIO_printf(bio_err, "ECDSA sign failure\n");
  947. ERR_print_errors(bio_err);
  948. count = -1;
  949. break;
  950. }
  951. }
  952. return count;
  953. }
  954. static int ECDSA_verify_loop(void *args)
  955. {
  956. loopargs_t *tempargs = *(loopargs_t **) args;
  957. unsigned char *buf = tempargs->buf;
  958. unsigned char *buf2 = tempargs->buf2;
  959. size_t ecdsa_num = tempargs->sigsize;
  960. EVP_PKEY_CTX **ecdsa_verify_ctx = tempargs->ecdsa_verify_ctx;
  961. int ret, count;
  962. for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
  963. ret = EVP_PKEY_verify(ecdsa_verify_ctx[testnum], buf2, ecdsa_num,
  964. buf, 20);
  965. if (ret <= 0) {
  966. BIO_printf(bio_err, "ECDSA verify failure\n");
  967. ERR_print_errors(bio_err);
  968. count = -1;
  969. break;
  970. }
  971. }
  972. return count;
  973. }
  974. /* ******************************************************************** */
  975. static int ECDH_EVP_derive_key_loop(void *args)
  976. {
  977. loopargs_t *tempargs = *(loopargs_t **) args;
  978. EVP_PKEY_CTX *ctx = tempargs->ecdh_ctx[testnum];
  979. unsigned char *derived_secret = tempargs->secret_a;
  980. int count;
  981. size_t *outlen = &(tempargs->outlen[testnum]);
  982. for (count = 0; COND(ecdh_c[testnum][0]); count++)
  983. EVP_PKEY_derive(ctx, derived_secret, outlen);
  984. return count;
  985. }
  986. #ifndef OPENSSL_NO_ECX
  987. static int EdDSA_sign_loop(void *args)
  988. {
  989. loopargs_t *tempargs = *(loopargs_t **) args;
  990. unsigned char *buf = tempargs->buf;
  991. EVP_MD_CTX **edctx = tempargs->eddsa_ctx;
  992. unsigned char *eddsasig = tempargs->buf2;
  993. size_t *eddsasigsize = &tempargs->sigsize;
  994. int ret, count;
  995. for (count = 0; COND(eddsa_c[testnum][0]); count++) {
  996. ret = EVP_DigestSignInit(edctx[testnum], NULL, NULL, NULL, NULL);
  997. if (ret == 0) {
  998. BIO_printf(bio_err, "EdDSA sign init failure\n");
  999. ERR_print_errors(bio_err);
  1000. count = -1;
  1001. break;
  1002. }
  1003. ret = EVP_DigestSign(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
  1004. if (ret == 0) {
  1005. BIO_printf(bio_err, "EdDSA sign failure\n");
  1006. ERR_print_errors(bio_err);
  1007. count = -1;
  1008. break;
  1009. }
  1010. }
  1011. return count;
  1012. }
  1013. static int EdDSA_verify_loop(void *args)
  1014. {
  1015. loopargs_t *tempargs = *(loopargs_t **) args;
  1016. unsigned char *buf = tempargs->buf;
  1017. EVP_MD_CTX **edctx = tempargs->eddsa_ctx2;
  1018. unsigned char *eddsasig = tempargs->buf2;
  1019. size_t eddsasigsize = tempargs->sigsize;
  1020. int ret, count;
  1021. for (count = 0; COND(eddsa_c[testnum][1]); count++) {
  1022. ret = EVP_DigestVerifyInit(edctx[testnum], NULL, NULL, NULL, NULL);
  1023. if (ret == 0) {
  1024. BIO_printf(bio_err, "EdDSA verify init failure\n");
  1025. ERR_print_errors(bio_err);
  1026. count = -1;
  1027. break;
  1028. }
  1029. ret = EVP_DigestVerify(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
  1030. if (ret != 1) {
  1031. BIO_printf(bio_err, "EdDSA verify failure\n");
  1032. ERR_print_errors(bio_err);
  1033. count = -1;
  1034. break;
  1035. }
  1036. }
  1037. return count;
  1038. }
  1039. #endif /* OPENSSL_NO_ECX */
  1040. #ifndef OPENSSL_NO_SM2
  1041. static int SM2_sign_loop(void *args)
  1042. {
  1043. loopargs_t *tempargs = *(loopargs_t **) args;
  1044. unsigned char *buf = tempargs->buf;
  1045. EVP_MD_CTX **sm2ctx = tempargs->sm2_ctx;
  1046. unsigned char *sm2sig = tempargs->buf2;
  1047. size_t sm2sigsize;
  1048. int ret, count;
  1049. EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
  1050. const size_t max_size = EVP_PKEY_get_size(sm2_pkey[testnum]);
  1051. for (count = 0; COND(sm2_c[testnum][0]); count++) {
  1052. sm2sigsize = max_size;
  1053. if (!EVP_DigestSignInit(sm2ctx[testnum], NULL, EVP_sm3(),
  1054. NULL, sm2_pkey[testnum])) {
  1055. BIO_printf(bio_err, "SM2 init sign failure\n");
  1056. ERR_print_errors(bio_err);
  1057. count = -1;
  1058. break;
  1059. }
  1060. ret = EVP_DigestSign(sm2ctx[testnum], sm2sig, &sm2sigsize,
  1061. buf, 20);
  1062. if (ret == 0) {
  1063. BIO_printf(bio_err, "SM2 sign failure\n");
  1064. ERR_print_errors(bio_err);
  1065. count = -1;
  1066. break;
  1067. }
  1068. /* update the latest returned size and always use the fixed buffer size */
  1069. tempargs->sigsize = sm2sigsize;
  1070. }
  1071. return count;
  1072. }
  1073. static int SM2_verify_loop(void *args)
  1074. {
  1075. loopargs_t *tempargs = *(loopargs_t **) args;
  1076. unsigned char *buf = tempargs->buf;
  1077. EVP_MD_CTX **sm2ctx = tempargs->sm2_vfy_ctx;
  1078. unsigned char *sm2sig = tempargs->buf2;
  1079. size_t sm2sigsize = tempargs->sigsize;
  1080. int ret, count;
  1081. EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
  1082. for (count = 0; COND(sm2_c[testnum][1]); count++) {
  1083. if (!EVP_DigestVerifyInit(sm2ctx[testnum], NULL, EVP_sm3(),
  1084. NULL, sm2_pkey[testnum])) {
  1085. BIO_printf(bio_err, "SM2 verify init failure\n");
  1086. ERR_print_errors(bio_err);
  1087. count = -1;
  1088. break;
  1089. }
  1090. ret = EVP_DigestVerify(sm2ctx[testnum], sm2sig, sm2sigsize,
  1091. buf, 20);
  1092. if (ret != 1) {
  1093. BIO_printf(bio_err, "SM2 verify failure\n");
  1094. ERR_print_errors(bio_err);
  1095. count = -1;
  1096. break;
  1097. }
  1098. }
  1099. return count;
  1100. }
  1101. #endif /* OPENSSL_NO_SM2 */
  1102. static int KEM_keygen_loop(void *args)
  1103. {
  1104. loopargs_t *tempargs = *(loopargs_t **) args;
  1105. EVP_PKEY_CTX *ctx = tempargs->kem_gen_ctx[testnum];
  1106. EVP_PKEY *pkey = NULL;
  1107. int count;
  1108. for (count = 0; COND(kems_c[testnum][0]); count++) {
  1109. if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
  1110. return -1;
  1111. /*
  1112. * runtime defined to quite some degree by randomness,
  1113. * so performance overhead of _free doesn't impact
  1114. * results significantly. In any case this test is
  1115. * meant to permit relative algorithm performance
  1116. * comparison.
  1117. */
  1118. EVP_PKEY_free(pkey);
  1119. pkey = NULL;
  1120. }
  1121. return count;
  1122. }
  1123. static int KEM_encaps_loop(void *args)
  1124. {
  1125. loopargs_t *tempargs = *(loopargs_t **) args;
  1126. EVP_PKEY_CTX *ctx = tempargs->kem_encaps_ctx[testnum];
  1127. size_t out_len = tempargs->kem_out_len[testnum];
  1128. size_t secret_len = tempargs->kem_secret_len[testnum];
  1129. unsigned char *out = tempargs->kem_out[testnum];
  1130. unsigned char *secret = tempargs->kem_send_secret[testnum];
  1131. int count;
  1132. for (count = 0; COND(kems_c[testnum][1]); count++) {
  1133. if (EVP_PKEY_encapsulate(ctx, out, &out_len, secret, &secret_len) <= 0)
  1134. return -1;
  1135. }
  1136. return count;
  1137. }
  1138. static int KEM_decaps_loop(void *args)
  1139. {
  1140. loopargs_t *tempargs = *(loopargs_t **) args;
  1141. EVP_PKEY_CTX *ctx = tempargs->kem_decaps_ctx[testnum];
  1142. size_t out_len = tempargs->kem_out_len[testnum];
  1143. size_t secret_len = tempargs->kem_secret_len[testnum];
  1144. unsigned char *out = tempargs->kem_out[testnum];
  1145. unsigned char *secret = tempargs->kem_send_secret[testnum];
  1146. int count;
  1147. for (count = 0; COND(kems_c[testnum][2]); count++) {
  1148. if (EVP_PKEY_decapsulate(ctx, secret, &secret_len, out, out_len) <= 0)
  1149. return -1;
  1150. }
  1151. return count;
  1152. }
  1153. static int SIG_keygen_loop(void *args)
  1154. {
  1155. loopargs_t *tempargs = *(loopargs_t **) args;
  1156. EVP_PKEY_CTX *ctx = tempargs->sig_gen_ctx[testnum];
  1157. EVP_PKEY *pkey = NULL;
  1158. int count;
  1159. for (count = 0; COND(kems_c[testnum][0]); count++) {
  1160. EVP_PKEY_keygen(ctx, &pkey);
  1161. /* TBD: How much does free influence runtime? */
  1162. EVP_PKEY_free(pkey);
  1163. pkey = NULL;
  1164. }
  1165. return count;
  1166. }
  1167. static int SIG_sign_loop(void *args)
  1168. {
  1169. loopargs_t *tempargs = *(loopargs_t **) args;
  1170. EVP_PKEY_CTX *ctx = tempargs->sig_sign_ctx[testnum];
  1171. /* be sure to not change stored sig: */
  1172. unsigned char *sig = app_malloc(tempargs->sig_max_sig_len[testnum],
  1173. "sig sign loop");
  1174. unsigned char md[SHA256_DIGEST_LENGTH] = { 0 };
  1175. size_t md_len = SHA256_DIGEST_LENGTH;
  1176. int count;
  1177. for (count = 0; COND(kems_c[testnum][1]); count++) {
  1178. size_t sig_len = tempargs->sig_max_sig_len[testnum];
  1179. int ret = EVP_PKEY_sign(ctx, sig, &sig_len, md, md_len);
  1180. if (ret <= 0) {
  1181. BIO_printf(bio_err, "SIG sign failure at count %d\n", count);
  1182. ERR_print_errors(bio_err);
  1183. count = -1;
  1184. break;
  1185. }
  1186. }
  1187. OPENSSL_free(sig);
  1188. return count;
  1189. }
  1190. static int SIG_verify_loop(void *args)
  1191. {
  1192. loopargs_t *tempargs = *(loopargs_t **) args;
  1193. EVP_PKEY_CTX *ctx = tempargs->sig_verify_ctx[testnum];
  1194. size_t sig_len = tempargs->sig_act_sig_len[testnum];
  1195. unsigned char *sig = tempargs->sig_sig[testnum];
  1196. unsigned char md[SHA256_DIGEST_LENGTH] = { 0 };
  1197. size_t md_len = SHA256_DIGEST_LENGTH;
  1198. int count;
  1199. for (count = 0; COND(kems_c[testnum][2]); count++) {
  1200. int ret = EVP_PKEY_verify(ctx, sig, sig_len, md, md_len);
  1201. if (ret <= 0) {
  1202. BIO_printf(bio_err, "SIG verify failure at count %d\n", count);
  1203. ERR_print_errors(bio_err);
  1204. count = -1;
  1205. break;
  1206. }
  1207. }
  1208. return count;
  1209. }
  1210. static int run_benchmark(int async_jobs,
  1211. int (*loop_function) (void *), loopargs_t *loopargs)
  1212. {
  1213. int job_op_count = 0;
  1214. int total_op_count = 0;
  1215. int num_inprogress = 0;
  1216. int error = 0, i = 0, ret = 0;
  1217. OSSL_ASYNC_FD job_fd = 0;
  1218. size_t num_job_fds = 0;
  1219. if (async_jobs == 0) {
  1220. return loop_function((void *)&loopargs);
  1221. }
  1222. for (i = 0; i < async_jobs && !error; i++) {
  1223. loopargs_t *looparg_item = loopargs + i;
  1224. /* Copy pointer content (looparg_t item address) into async context */
  1225. ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
  1226. &job_op_count, loop_function,
  1227. (void *)&looparg_item, sizeof(looparg_item));
  1228. switch (ret) {
  1229. case ASYNC_PAUSE:
  1230. ++num_inprogress;
  1231. break;
  1232. case ASYNC_FINISH:
  1233. if (job_op_count == -1) {
  1234. error = 1;
  1235. } else {
  1236. total_op_count += job_op_count;
  1237. }
  1238. break;
  1239. case ASYNC_NO_JOBS:
  1240. case ASYNC_ERR:
  1241. BIO_printf(bio_err, "Failure in the job\n");
  1242. ERR_print_errors(bio_err);
  1243. error = 1;
  1244. break;
  1245. }
  1246. }
  1247. while (num_inprogress > 0) {
  1248. #if defined(OPENSSL_SYS_WINDOWS)
  1249. DWORD avail = 0;
  1250. #elif defined(OPENSSL_SYS_UNIX)
  1251. int select_result = 0;
  1252. OSSL_ASYNC_FD max_fd = 0;
  1253. fd_set waitfdset;
  1254. FD_ZERO(&waitfdset);
  1255. for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
  1256. if (loopargs[i].inprogress_job == NULL)
  1257. continue;
  1258. if (!ASYNC_WAIT_CTX_get_all_fds
  1259. (loopargs[i].wait_ctx, NULL, &num_job_fds)
  1260. || num_job_fds > 1) {
  1261. BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
  1262. ERR_print_errors(bio_err);
  1263. error = 1;
  1264. break;
  1265. }
  1266. ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
  1267. &num_job_fds);
  1268. FD_SET(job_fd, &waitfdset);
  1269. if (job_fd > max_fd)
  1270. max_fd = job_fd;
  1271. }
  1272. if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
  1273. BIO_printf(bio_err,
  1274. "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
  1275. "Decrease the value of async_jobs\n",
  1276. max_fd, FD_SETSIZE);
  1277. ERR_print_errors(bio_err);
  1278. error = 1;
  1279. break;
  1280. }
  1281. select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
  1282. if (select_result == -1 && errno == EINTR)
  1283. continue;
  1284. if (select_result == -1) {
  1285. BIO_printf(bio_err, "Failure in the select\n");
  1286. ERR_print_errors(bio_err);
  1287. error = 1;
  1288. break;
  1289. }
  1290. if (select_result == 0)
  1291. continue;
  1292. #endif
  1293. for (i = 0; i < async_jobs; i++) {
  1294. if (loopargs[i].inprogress_job == NULL)
  1295. continue;
  1296. if (!ASYNC_WAIT_CTX_get_all_fds
  1297. (loopargs[i].wait_ctx, NULL, &num_job_fds)
  1298. || num_job_fds > 1) {
  1299. BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
  1300. ERR_print_errors(bio_err);
  1301. error = 1;
  1302. break;
  1303. }
  1304. ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
  1305. &num_job_fds);
  1306. #if defined(OPENSSL_SYS_UNIX)
  1307. if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
  1308. continue;
  1309. #elif defined(OPENSSL_SYS_WINDOWS)
  1310. if (num_job_fds == 1
  1311. && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
  1312. && avail > 0)
  1313. continue;
  1314. #endif
  1315. ret = ASYNC_start_job(&loopargs[i].inprogress_job,
  1316. loopargs[i].wait_ctx, &job_op_count,
  1317. loop_function, (void *)(loopargs + i),
  1318. sizeof(loopargs_t));
  1319. switch (ret) {
  1320. case ASYNC_PAUSE:
  1321. break;
  1322. case ASYNC_FINISH:
  1323. if (job_op_count == -1) {
  1324. error = 1;
  1325. } else {
  1326. total_op_count += job_op_count;
  1327. }
  1328. --num_inprogress;
  1329. loopargs[i].inprogress_job = NULL;
  1330. break;
  1331. case ASYNC_NO_JOBS:
  1332. case ASYNC_ERR:
  1333. --num_inprogress;
  1334. loopargs[i].inprogress_job = NULL;
  1335. BIO_printf(bio_err, "Failure in the job\n");
  1336. ERR_print_errors(bio_err);
  1337. error = 1;
  1338. break;
  1339. }
  1340. }
  1341. }
  1342. return error ? -1 : total_op_count;
  1343. }
  1344. typedef struct ec_curve_st {
  1345. const char *name;
  1346. unsigned int nid;
  1347. unsigned int bits;
  1348. size_t sigsize; /* only used for EdDSA curves */
  1349. } EC_CURVE;
  1350. static EVP_PKEY *get_ecdsa(const EC_CURVE *curve)
  1351. {
  1352. EVP_PKEY_CTX *kctx = NULL;
  1353. EVP_PKEY *key = NULL;
  1354. /* Ensure that the error queue is empty */
  1355. if (ERR_peek_error()) {
  1356. BIO_printf(bio_err,
  1357. "WARNING: the error queue contains previous unhandled errors.\n");
  1358. ERR_print_errors(bio_err);
  1359. }
  1360. /*
  1361. * Let's try to create a ctx directly from the NID: this works for
  1362. * curves like Curve25519 that are not implemented through the low
  1363. * level EC interface.
  1364. * If this fails we try creating a EVP_PKEY_EC generic param ctx,
  1365. * then we set the curve by NID before deriving the actual keygen
  1366. * ctx for that specific curve.
  1367. */
  1368. kctx = EVP_PKEY_CTX_new_id(curve->nid, NULL);
  1369. if (kctx == NULL) {
  1370. EVP_PKEY_CTX *pctx = NULL;
  1371. EVP_PKEY *params = NULL;
  1372. /*
  1373. * If we reach this code EVP_PKEY_CTX_new_id() failed and a
  1374. * "int_ctx_new:unsupported algorithm" error was added to the
  1375. * error queue.
  1376. * We remove it from the error queue as we are handling it.
  1377. */
  1378. unsigned long error = ERR_peek_error();
  1379. if (error == ERR_peek_last_error() /* oldest and latest errors match */
  1380. /* check that the error origin matches */
  1381. && ERR_GET_LIB(error) == ERR_LIB_EVP
  1382. && (ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM
  1383. || ERR_GET_REASON(error) == ERR_R_UNSUPPORTED))
  1384. ERR_get_error(); /* pop error from queue */
  1385. if (ERR_peek_error()) {
  1386. BIO_printf(bio_err,
  1387. "Unhandled error in the error queue during EC key setup.\n");
  1388. ERR_print_errors(bio_err);
  1389. return NULL;
  1390. }
  1391. /* Create the context for parameter generation */
  1392. if ((pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) == NULL
  1393. || EVP_PKEY_paramgen_init(pctx) <= 0
  1394. || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
  1395. curve->nid) <= 0
  1396. || EVP_PKEY_paramgen(pctx, &params) <= 0) {
  1397. BIO_printf(bio_err, "EC params init failure.\n");
  1398. ERR_print_errors(bio_err);
  1399. EVP_PKEY_CTX_free(pctx);
  1400. return NULL;
  1401. }
  1402. EVP_PKEY_CTX_free(pctx);
  1403. /* Create the context for the key generation */
  1404. kctx = EVP_PKEY_CTX_new(params, NULL);
  1405. EVP_PKEY_free(params);
  1406. }
  1407. if (kctx == NULL
  1408. || EVP_PKEY_keygen_init(kctx) <= 0
  1409. || EVP_PKEY_keygen(kctx, &key) <= 0) {
  1410. BIO_printf(bio_err, "EC key generation failure.\n");
  1411. ERR_print_errors(bio_err);
  1412. key = NULL;
  1413. }
  1414. EVP_PKEY_CTX_free(kctx);
  1415. return key;
  1416. }
  1417. #define stop_it(do_it, test_num)\
  1418. memset(do_it + test_num, 0, OSSL_NELEM(do_it) - test_num);
  1419. /* Checks to see if algorithms are fetchable */
  1420. #define IS_FETCHABLE(type, TYPE) \
  1421. static int is_ ## type ## _fetchable(const TYPE *alg) \
  1422. { \
  1423. TYPE *impl; \
  1424. const char *propq = app_get0_propq(); \
  1425. OSSL_LIB_CTX *libctx = app_get0_libctx(); \
  1426. const char *name = TYPE ## _get0_name(alg); \
  1427. \
  1428. ERR_set_mark(); \
  1429. impl = TYPE ## _fetch(libctx, name, propq); \
  1430. ERR_pop_to_mark(); \
  1431. if (impl == NULL) \
  1432. return 0; \
  1433. TYPE ## _free(impl); \
  1434. return 1; \
  1435. }
  1436. IS_FETCHABLE(signature, EVP_SIGNATURE)
  1437. IS_FETCHABLE(kem, EVP_KEM)
  1438. DEFINE_STACK_OF(EVP_KEM)
  1439. static int kems_cmp(const EVP_KEM * const *a,
  1440. const EVP_KEM * const *b)
  1441. {
  1442. return strcmp(OSSL_PROVIDER_get0_name(EVP_KEM_get0_provider(*a)),
  1443. OSSL_PROVIDER_get0_name(EVP_KEM_get0_provider(*b)));
  1444. }
  1445. static void collect_kem(EVP_KEM *kem, void *stack)
  1446. {
  1447. STACK_OF(EVP_KEM) *kem_stack = stack;
  1448. if (is_kem_fetchable(kem)
  1449. && sk_EVP_KEM_push(kem_stack, kem) > 0) {
  1450. EVP_KEM_up_ref(kem);
  1451. }
  1452. }
  1453. static int kem_locate(const char *algo, unsigned int *idx)
  1454. {
  1455. unsigned int i;
  1456. for (i = 0; i < kems_algs_len; i++) {
  1457. if (strcmp(kems_algname[i], algo) == 0) {
  1458. *idx = i;
  1459. return 1;
  1460. }
  1461. }
  1462. return 0;
  1463. }
  1464. DEFINE_STACK_OF(EVP_SIGNATURE)
  1465. static int signatures_cmp(const EVP_SIGNATURE * const *a,
  1466. const EVP_SIGNATURE * const *b)
  1467. {
  1468. return strcmp(OSSL_PROVIDER_get0_name(EVP_SIGNATURE_get0_provider(*a)),
  1469. OSSL_PROVIDER_get0_name(EVP_SIGNATURE_get0_provider(*b)));
  1470. }
  1471. static void collect_signatures(EVP_SIGNATURE *sig, void *stack)
  1472. {
  1473. STACK_OF(EVP_SIGNATURE) *sig_stack = stack;
  1474. if (is_signature_fetchable(sig)
  1475. && sk_EVP_SIGNATURE_push(sig_stack, sig) > 0)
  1476. EVP_SIGNATURE_up_ref(sig);
  1477. }
  1478. static int sig_locate(const char *algo, unsigned int *idx)
  1479. {
  1480. unsigned int i;
  1481. for (i = 0; i < sigs_algs_len; i++) {
  1482. if (strcmp(sigs_algname[i], algo) == 0) {
  1483. *idx = i;
  1484. return 1;
  1485. }
  1486. }
  1487. return 0;
  1488. }
  1489. static int get_max(const uint8_t doit[], size_t algs_len) {
  1490. size_t i = 0;
  1491. int maxcnt = 0;
  1492. for (i = 0; i < algs_len; i++)
  1493. if (maxcnt < doit[i]) maxcnt = doit[i];
  1494. return maxcnt;
  1495. }
  1496. int speed_main(int argc, char **argv)
  1497. {
  1498. CONF *conf = NULL;
  1499. ENGINE *e = NULL;
  1500. loopargs_t *loopargs = NULL;
  1501. const char *prog;
  1502. const char *engine_id = NULL;
  1503. EVP_CIPHER *evp_cipher = NULL;
  1504. EVP_MAC *mac = NULL;
  1505. double d = 0.0;
  1506. OPTION_CHOICE o;
  1507. int async_init = 0, multiblock = 0, pr_header = 0;
  1508. uint8_t doit[ALGOR_NUM] = { 0 };
  1509. int ret = 1, misalign = 0, lengths_single = 0, aead = 0;
  1510. STACK_OF(EVP_KEM) *kem_stack = NULL;
  1511. STACK_OF(EVP_SIGNATURE) *sig_stack = NULL;
  1512. long count = 0;
  1513. unsigned int size_num = SIZE_NUM;
  1514. unsigned int i, k, loopargs_len = 0, async_jobs = 0;
  1515. unsigned int idx;
  1516. int keylen;
  1517. int buflen;
  1518. size_t declen;
  1519. BIGNUM *bn = NULL;
  1520. EVP_PKEY_CTX *genctx = NULL;
  1521. #ifndef NO_FORK
  1522. int multi = 0;
  1523. #endif
  1524. long op_count = 1;
  1525. openssl_speed_sec_t seconds = { SECONDS, RSA_SECONDS, DSA_SECONDS,
  1526. ECDSA_SECONDS, ECDH_SECONDS,
  1527. EdDSA_SECONDS, SM2_SECONDS,
  1528. FFDH_SECONDS, KEM_SECONDS,
  1529. SIG_SECONDS };
  1530. static const unsigned char key32[32] = {
  1531. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
  1532. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
  1533. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
  1534. 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
  1535. };
  1536. static const unsigned char deskey[] = {
  1537. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, /* key1 */
  1538. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, /* key2 */
  1539. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 /* key3 */
  1540. };
  1541. static const struct {
  1542. const unsigned char *data;
  1543. unsigned int length;
  1544. unsigned int bits;
  1545. } rsa_keys[] = {
  1546. { test512, sizeof(test512), 512 },
  1547. { test1024, sizeof(test1024), 1024 },
  1548. { test2048, sizeof(test2048), 2048 },
  1549. { test3072, sizeof(test3072), 3072 },
  1550. { test4096, sizeof(test4096), 4096 },
  1551. { test7680, sizeof(test7680), 7680 },
  1552. { test15360, sizeof(test15360), 15360 }
  1553. };
  1554. uint8_t rsa_doit[RSA_NUM] = { 0 };
  1555. int primes = RSA_DEFAULT_PRIME_NUM;
  1556. #ifndef OPENSSL_NO_DH
  1557. typedef struct ffdh_params_st {
  1558. const char *name;
  1559. unsigned int nid;
  1560. unsigned int bits;
  1561. } FFDH_PARAMS;
  1562. static const FFDH_PARAMS ffdh_params[FFDH_NUM] = {
  1563. {"ffdh2048", NID_ffdhe2048, 2048},
  1564. {"ffdh3072", NID_ffdhe3072, 3072},
  1565. {"ffdh4096", NID_ffdhe4096, 4096},
  1566. {"ffdh6144", NID_ffdhe6144, 6144},
  1567. {"ffdh8192", NID_ffdhe8192, 8192}
  1568. };
  1569. uint8_t ffdh_doit[FFDH_NUM] = { 0 };
  1570. #endif /* OPENSSL_NO_DH */
  1571. static const unsigned int dsa_bits[DSA_NUM] = { 1024, 2048 };
  1572. uint8_t dsa_doit[DSA_NUM] = { 0 };
  1573. /*
  1574. * We only test over the following curves as they are representative, To
  1575. * add tests over more curves, simply add the curve NID and curve name to
  1576. * the following arrays and increase the |ecdh_choices| and |ecdsa_choices|
  1577. * lists accordingly.
  1578. */
  1579. static const EC_CURVE ec_curves[EC_NUM] = {
  1580. /* Prime Curves */
  1581. {"secp160r1", NID_secp160r1, 160},
  1582. {"nistp192", NID_X9_62_prime192v1, 192},
  1583. {"nistp224", NID_secp224r1, 224},
  1584. {"nistp256", NID_X9_62_prime256v1, 256},
  1585. {"nistp384", NID_secp384r1, 384},
  1586. {"nistp521", NID_secp521r1, 521},
  1587. #ifndef OPENSSL_NO_EC2M
  1588. /* Binary Curves */
  1589. {"nistk163", NID_sect163k1, 163},
  1590. {"nistk233", NID_sect233k1, 233},
  1591. {"nistk283", NID_sect283k1, 283},
  1592. {"nistk409", NID_sect409k1, 409},
  1593. {"nistk571", NID_sect571k1, 571},
  1594. {"nistb163", NID_sect163r2, 163},
  1595. {"nistb233", NID_sect233r1, 233},
  1596. {"nistb283", NID_sect283r1, 283},
  1597. {"nistb409", NID_sect409r1, 409},
  1598. {"nistb571", NID_sect571r1, 571},
  1599. #endif
  1600. {"brainpoolP256r1", NID_brainpoolP256r1, 256},
  1601. {"brainpoolP256t1", NID_brainpoolP256t1, 256},
  1602. {"brainpoolP384r1", NID_brainpoolP384r1, 384},
  1603. {"brainpoolP384t1", NID_brainpoolP384t1, 384},
  1604. {"brainpoolP512r1", NID_brainpoolP512r1, 512},
  1605. {"brainpoolP512t1", NID_brainpoolP512t1, 512},
  1606. #ifndef OPENSSL_NO_ECX
  1607. /* Other and ECDH only ones */
  1608. {"X25519", NID_X25519, 253},
  1609. {"X448", NID_X448, 448}
  1610. #endif
  1611. };
  1612. #ifndef OPENSSL_NO_ECX
  1613. static const EC_CURVE ed_curves[EdDSA_NUM] = {
  1614. /* EdDSA */
  1615. {"Ed25519", NID_ED25519, 253, 64},
  1616. {"Ed448", NID_ED448, 456, 114}
  1617. };
  1618. #endif /* OPENSSL_NO_ECX */
  1619. #ifndef OPENSSL_NO_SM2
  1620. static const EC_CURVE sm2_curves[SM2_NUM] = {
  1621. /* SM2 */
  1622. {"CurveSM2", NID_sm2, 256}
  1623. };
  1624. uint8_t sm2_doit[SM2_NUM] = { 0 };
  1625. #endif
  1626. uint8_t ecdsa_doit[ECDSA_NUM] = { 0 };
  1627. uint8_t ecdh_doit[EC_NUM] = { 0 };
  1628. #ifndef OPENSSL_NO_ECX
  1629. uint8_t eddsa_doit[EdDSA_NUM] = { 0 };
  1630. #endif /* OPENSSL_NO_ECX */
  1631. uint8_t kems_doit[MAX_KEM_NUM] = { 0 };
  1632. uint8_t sigs_doit[MAX_SIG_NUM] = { 0 };
  1633. uint8_t do_kems = 0;
  1634. uint8_t do_sigs = 0;
  1635. /* checks declared curves against choices list. */
  1636. #ifndef OPENSSL_NO_ECX
  1637. OPENSSL_assert(ed_curves[EdDSA_NUM - 1].nid == NID_ED448);
  1638. OPENSSL_assert(strcmp(eddsa_choices[EdDSA_NUM - 1].name, "ed448") == 0);
  1639. OPENSSL_assert(ec_curves[EC_NUM - 1].nid == NID_X448);
  1640. OPENSSL_assert(strcmp(ecdh_choices[EC_NUM - 1].name, "ecdhx448") == 0);
  1641. OPENSSL_assert(ec_curves[ECDSA_NUM - 1].nid == NID_brainpoolP512t1);
  1642. OPENSSL_assert(strcmp(ecdsa_choices[ECDSA_NUM - 1].name, "ecdsabrp512t1") == 0);
  1643. #endif /* OPENSSL_NO_ECX */
  1644. #ifndef OPENSSL_NO_SM2
  1645. OPENSSL_assert(sm2_curves[SM2_NUM - 1].nid == NID_sm2);
  1646. OPENSSL_assert(strcmp(sm2_choices[SM2_NUM - 1].name, "curveSM2") == 0);
  1647. #endif
  1648. prog = opt_init(argc, argv, speed_options);
  1649. while ((o = opt_next()) != OPT_EOF) {
  1650. switch (o) {
  1651. case OPT_EOF:
  1652. case OPT_ERR:
  1653. opterr:
  1654. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  1655. goto end;
  1656. case OPT_HELP:
  1657. opt_help(speed_options);
  1658. ret = 0;
  1659. goto end;
  1660. case OPT_ELAPSED:
  1661. usertime = 0;
  1662. break;
  1663. case OPT_EVP:
  1664. if (doit[D_EVP]) {
  1665. BIO_printf(bio_err, "%s: -evp option cannot be used more than once\n", prog);
  1666. goto opterr;
  1667. }
  1668. ERR_set_mark();
  1669. if (!opt_cipher_silent(opt_arg(), &evp_cipher)) {
  1670. if (have_md(opt_arg()))
  1671. evp_md_name = opt_arg();
  1672. }
  1673. if (evp_cipher == NULL && evp_md_name == NULL) {
  1674. ERR_clear_last_mark();
  1675. BIO_printf(bio_err,
  1676. "%s: %s is an unknown cipher or digest\n",
  1677. prog, opt_arg());
  1678. goto end;
  1679. }
  1680. ERR_pop_to_mark();
  1681. doit[D_EVP] = 1;
  1682. break;
  1683. case OPT_HMAC:
  1684. if (!have_md(opt_arg())) {
  1685. BIO_printf(bio_err, "%s: %s is an unknown digest\n",
  1686. prog, opt_arg());
  1687. goto end;
  1688. }
  1689. evp_mac_mdname = opt_arg();
  1690. doit[D_HMAC] = 1;
  1691. break;
  1692. case OPT_CMAC:
  1693. if (!have_cipher(opt_arg())) {
  1694. BIO_printf(bio_err, "%s: %s is an unknown cipher\n",
  1695. prog, opt_arg());
  1696. goto end;
  1697. }
  1698. evp_mac_ciphername = opt_arg();
  1699. doit[D_EVP_CMAC] = 1;
  1700. break;
  1701. case OPT_DECRYPT:
  1702. decrypt = 1;
  1703. break;
  1704. case OPT_ENGINE:
  1705. /*
  1706. * In a forked execution, an engine might need to be
  1707. * initialised by each child process, not by the parent.
  1708. * So store the name here and run setup_engine() later on.
  1709. */
  1710. engine_id = opt_arg();
  1711. break;
  1712. case OPT_MULTI:
  1713. #ifndef NO_FORK
  1714. multi = opt_int_arg();
  1715. if ((size_t)multi >= SIZE_MAX / sizeof(int)) {
  1716. BIO_printf(bio_err, "%s: multi argument too large\n", prog);
  1717. return 0;
  1718. }
  1719. #endif
  1720. break;
  1721. case OPT_ASYNCJOBS:
  1722. #ifndef OPENSSL_NO_ASYNC
  1723. async_jobs = opt_int_arg();
  1724. if (!ASYNC_is_capable()) {
  1725. BIO_printf(bio_err,
  1726. "%s: async_jobs specified but async not supported\n",
  1727. prog);
  1728. goto opterr;
  1729. }
  1730. if (async_jobs > 99999) {
  1731. BIO_printf(bio_err, "%s: too many async_jobs\n", prog);
  1732. goto opterr;
  1733. }
  1734. #endif
  1735. break;
  1736. case OPT_MISALIGN:
  1737. misalign = opt_int_arg();
  1738. if (misalign > MISALIGN) {
  1739. BIO_printf(bio_err,
  1740. "%s: Maximum offset is %d\n", prog, MISALIGN);
  1741. goto opterr;
  1742. }
  1743. break;
  1744. case OPT_MR:
  1745. mr = 1;
  1746. break;
  1747. case OPT_MB:
  1748. multiblock = 1;
  1749. #ifdef OPENSSL_NO_MULTIBLOCK
  1750. BIO_printf(bio_err,
  1751. "%s: -mb specified but multi-block support is disabled\n",
  1752. prog);
  1753. goto end;
  1754. #endif
  1755. break;
  1756. case OPT_R_CASES:
  1757. if (!opt_rand(o))
  1758. goto end;
  1759. break;
  1760. case OPT_PROV_CASES:
  1761. if (!opt_provider(o))
  1762. goto end;
  1763. break;
  1764. case OPT_CONFIG:
  1765. conf = app_load_config_modules(opt_arg());
  1766. if (conf == NULL)
  1767. goto end;
  1768. break;
  1769. case OPT_PRIMES:
  1770. primes = opt_int_arg();
  1771. break;
  1772. case OPT_SECONDS:
  1773. seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
  1774. = seconds.ecdh = seconds.eddsa
  1775. = seconds.sm2 = seconds.ffdh
  1776. = seconds.kem = seconds.sig = opt_int_arg();
  1777. break;
  1778. case OPT_BYTES:
  1779. lengths_single = opt_int_arg();
  1780. lengths = &lengths_single;
  1781. size_num = 1;
  1782. break;
  1783. case OPT_AEAD:
  1784. aead = 1;
  1785. break;
  1786. case OPT_KEM:
  1787. do_kems = 1;
  1788. break;
  1789. case OPT_SIG:
  1790. do_sigs = 1;
  1791. break;
  1792. case OPT_MLOCK:
  1793. domlock = 1;
  1794. #if !defined(_WIN32) && !defined(OPENSSL_SYS_LINUX)
  1795. BIO_printf(bio_err,
  1796. "%s: -mlock not supported on this platform\n",
  1797. prog);
  1798. goto end;
  1799. #endif
  1800. break;
  1801. }
  1802. }
  1803. /* find all KEMs currently available */
  1804. kem_stack = sk_EVP_KEM_new(kems_cmp);
  1805. EVP_KEM_do_all_provided(app_get0_libctx(), collect_kem, kem_stack);
  1806. kems_algs_len = 0;
  1807. for (idx = 0; idx < (unsigned int)sk_EVP_KEM_num(kem_stack); idx++) {
  1808. EVP_KEM *kem = sk_EVP_KEM_value(kem_stack, idx);
  1809. if (strcmp(EVP_KEM_get0_name(kem), "RSA") == 0) {
  1810. if (kems_algs_len + OSSL_NELEM(rsa_choices) >= MAX_KEM_NUM) {
  1811. BIO_printf(bio_err,
  1812. "Too many KEMs registered. Change MAX_KEM_NUM.\n");
  1813. goto end;
  1814. }
  1815. for (i = 0; i < OSSL_NELEM(rsa_choices); i++) {
  1816. kems_doit[kems_algs_len] = 1;
  1817. kems_algname[kems_algs_len++] = OPENSSL_strdup(rsa_choices[i].name);
  1818. }
  1819. } else if (strcmp(EVP_KEM_get0_name(kem), "EC") == 0) {
  1820. if (kems_algs_len + 3 >= MAX_KEM_NUM) {
  1821. BIO_printf(bio_err,
  1822. "Too many KEMs registered. Change MAX_KEM_NUM.\n");
  1823. goto end;
  1824. }
  1825. kems_doit[kems_algs_len] = 1;
  1826. kems_algname[kems_algs_len++] = OPENSSL_strdup("ECP-256");
  1827. kems_doit[kems_algs_len] = 1;
  1828. kems_algname[kems_algs_len++] = OPENSSL_strdup("ECP-384");
  1829. kems_doit[kems_algs_len] = 1;
  1830. kems_algname[kems_algs_len++] = OPENSSL_strdup("ECP-521");
  1831. } else {
  1832. if (kems_algs_len + 1 >= MAX_KEM_NUM) {
  1833. BIO_printf(bio_err,
  1834. "Too many KEMs registered. Change MAX_KEM_NUM.\n");
  1835. goto end;
  1836. }
  1837. kems_doit[kems_algs_len] = 1;
  1838. kems_algname[kems_algs_len++] = OPENSSL_strdup(EVP_KEM_get0_name(kem));
  1839. }
  1840. }
  1841. sk_EVP_KEM_pop_free(kem_stack, EVP_KEM_free);
  1842. kem_stack = NULL;
  1843. /* find all SIGNATUREs currently available */
  1844. sig_stack = sk_EVP_SIGNATURE_new(signatures_cmp);
  1845. EVP_SIGNATURE_do_all_provided(app_get0_libctx(), collect_signatures, sig_stack);
  1846. sigs_algs_len = 0;
  1847. for (idx = 0; idx < (unsigned int)sk_EVP_SIGNATURE_num(sig_stack); idx++) {
  1848. EVP_SIGNATURE *s = sk_EVP_SIGNATURE_value(sig_stack, idx);
  1849. const char *sig_name = EVP_SIGNATURE_get0_name(s);
  1850. if (strcmp(sig_name, "RSA") == 0) {
  1851. if (sigs_algs_len + OSSL_NELEM(rsa_choices) >= MAX_SIG_NUM) {
  1852. BIO_printf(bio_err,
  1853. "Too many signatures registered. Change MAX_SIG_NUM.\n");
  1854. goto end;
  1855. }
  1856. for (i = 0; i < OSSL_NELEM(rsa_choices); i++) {
  1857. sigs_doit[sigs_algs_len] = 1;
  1858. sigs_algname[sigs_algs_len++] = OPENSSL_strdup(rsa_choices[i].name);
  1859. }
  1860. }
  1861. else if (strcmp(sig_name, "DSA") == 0) {
  1862. if (sigs_algs_len + DSA_NUM >= MAX_SIG_NUM) {
  1863. BIO_printf(bio_err,
  1864. "Too many signatures registered. Change MAX_SIG_NUM.\n");
  1865. goto end;
  1866. }
  1867. for (i = 0; i < DSA_NUM; i++) {
  1868. sigs_doit[sigs_algs_len] = 1;
  1869. sigs_algname[sigs_algs_len++] = OPENSSL_strdup(dsa_choices[i].name);
  1870. }
  1871. }
  1872. /* skipping these algs as tested elsewhere - and b/o setup is a pain */
  1873. else if (strcmp(sig_name, "ED25519") &&
  1874. strcmp(sig_name, "ED448") &&
  1875. strcmp(sig_name, "ECDSA") &&
  1876. strcmp(sig_name, "HMAC") &&
  1877. strcmp(sig_name, "SIPHASH") &&
  1878. strcmp(sig_name, "POLY1305") &&
  1879. strcmp(sig_name, "CMAC") &&
  1880. strcmp(sig_name, "SM2")) { /* skip alg */
  1881. if (sigs_algs_len + 1 >= MAX_SIG_NUM) {
  1882. BIO_printf(bio_err,
  1883. "Too many signatures registered. Change MAX_SIG_NUM.\n");
  1884. goto end;
  1885. }
  1886. /* activate this provider algorithm */
  1887. sigs_doit[sigs_algs_len] = 1;
  1888. sigs_algname[sigs_algs_len++] = OPENSSL_strdup(sig_name);
  1889. }
  1890. }
  1891. sk_EVP_SIGNATURE_pop_free(sig_stack, EVP_SIGNATURE_free);
  1892. sig_stack = NULL;
  1893. /* Remaining arguments are algorithms. */
  1894. argc = opt_num_rest();
  1895. argv = opt_rest();
  1896. if (!app_RAND_load())
  1897. goto end;
  1898. for (; *argv; argv++) {
  1899. const char *algo = *argv;
  1900. int algo_found = 0;
  1901. if (opt_found(algo, doit_choices, &i)) {
  1902. doit[i] = 1;
  1903. algo_found = 1;
  1904. }
  1905. if (strcmp(algo, "des") == 0) {
  1906. doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
  1907. algo_found = 1;
  1908. }
  1909. if (strcmp(algo, "sha") == 0) {
  1910. doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
  1911. algo_found = 1;
  1912. }
  1913. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1914. if (strcmp(algo, "openssl") == 0) /* just for compatibility */
  1915. algo_found = 1;
  1916. #endif
  1917. if (HAS_PREFIX(algo, "rsa")) {
  1918. if (algo[sizeof("rsa") - 1] == '\0') {
  1919. memset(rsa_doit, 1, sizeof(rsa_doit));
  1920. algo_found = 1;
  1921. }
  1922. if (opt_found(algo, rsa_choices, &i)) {
  1923. rsa_doit[i] = 1;
  1924. algo_found = 1;
  1925. }
  1926. }
  1927. #ifndef OPENSSL_NO_DH
  1928. if (HAS_PREFIX(algo, "ffdh")) {
  1929. if (algo[sizeof("ffdh") - 1] == '\0') {
  1930. memset(ffdh_doit, 1, sizeof(ffdh_doit));
  1931. algo_found = 1;
  1932. }
  1933. if (opt_found(algo, ffdh_choices, &i)) {
  1934. ffdh_doit[i] = 2;
  1935. algo_found = 1;
  1936. }
  1937. }
  1938. #endif
  1939. if (HAS_PREFIX(algo, "dsa")) {
  1940. if (algo[sizeof("dsa") - 1] == '\0') {
  1941. memset(dsa_doit, 1, sizeof(dsa_doit));
  1942. algo_found = 1;
  1943. }
  1944. if (opt_found(algo, dsa_choices, &i)) {
  1945. dsa_doit[i] = 2;
  1946. algo_found = 1;
  1947. }
  1948. }
  1949. if (strcmp(algo, "aes") == 0) {
  1950. doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
  1951. algo_found = 1;
  1952. }
  1953. if (strcmp(algo, "camellia") == 0) {
  1954. doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
  1955. algo_found = 1;
  1956. }
  1957. if (HAS_PREFIX(algo, "ecdsa")) {
  1958. if (algo[sizeof("ecdsa") - 1] == '\0') {
  1959. memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
  1960. algo_found = 1;
  1961. }
  1962. if (opt_found(algo, ecdsa_choices, &i)) {
  1963. ecdsa_doit[i] = 2;
  1964. algo_found = 1;
  1965. }
  1966. }
  1967. if (HAS_PREFIX(algo, "ecdh")) {
  1968. if (algo[sizeof("ecdh") - 1] == '\0') {
  1969. memset(ecdh_doit, 1, sizeof(ecdh_doit));
  1970. algo_found = 1;
  1971. }
  1972. if (opt_found(algo, ecdh_choices, &i)) {
  1973. ecdh_doit[i] = 2;
  1974. algo_found = 1;
  1975. }
  1976. }
  1977. #ifndef OPENSSL_NO_ECX
  1978. if (strcmp(algo, "eddsa") == 0) {
  1979. memset(eddsa_doit, 1, sizeof(eddsa_doit));
  1980. algo_found = 1;
  1981. }
  1982. if (opt_found(algo, eddsa_choices, &i)) {
  1983. eddsa_doit[i] = 2;
  1984. algo_found = 1;
  1985. }
  1986. #endif /* OPENSSL_NO_ECX */
  1987. #ifndef OPENSSL_NO_SM2
  1988. if (strcmp(algo, "sm2") == 0) {
  1989. memset(sm2_doit, 1, sizeof(sm2_doit));
  1990. algo_found = 1;
  1991. }
  1992. if (opt_found(algo, sm2_choices, &i)) {
  1993. sm2_doit[i] = 2;
  1994. algo_found = 1;
  1995. }
  1996. #endif
  1997. if (kem_locate(algo, &idx)) {
  1998. kems_doit[idx]++;
  1999. do_kems = 1;
  2000. algo_found = 1;
  2001. }
  2002. if (sig_locate(algo, &idx)) {
  2003. sigs_doit[idx]++;
  2004. do_sigs = 1;
  2005. algo_found = 1;
  2006. }
  2007. if (!algo_found) {
  2008. BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, algo);
  2009. goto end;
  2010. }
  2011. }
  2012. /* Sanity checks */
  2013. if (aead) {
  2014. if (evp_cipher == NULL) {
  2015. BIO_printf(bio_err, "-aead can be used only with an AEAD cipher\n");
  2016. goto end;
  2017. } else if (!(EVP_CIPHER_get_flags(evp_cipher) &
  2018. EVP_CIPH_FLAG_AEAD_CIPHER)) {
  2019. BIO_printf(bio_err, "%s is not an AEAD cipher\n",
  2020. EVP_CIPHER_get0_name(evp_cipher));
  2021. goto end;
  2022. }
  2023. }
  2024. if (kems_algs_len > 0) {
  2025. int maxcnt = get_max(kems_doit, kems_algs_len);
  2026. if (maxcnt > 1) {
  2027. /* some algs explicitly selected */
  2028. for (i = 0; i < kems_algs_len; i++) {
  2029. /* disable the rest */
  2030. kems_doit[i]--;
  2031. }
  2032. }
  2033. }
  2034. if (sigs_algs_len > 0) {
  2035. int maxcnt = get_max(sigs_doit, sigs_algs_len);
  2036. if (maxcnt > 1) {
  2037. /* some algs explicitly selected */
  2038. for (i = 0; i < sigs_algs_len; i++) {
  2039. /* disable the rest */
  2040. sigs_doit[i]--;
  2041. }
  2042. }
  2043. }
  2044. if (multiblock) {
  2045. if (evp_cipher == NULL) {
  2046. BIO_printf(bio_err, "-mb can be used only with a multi-block"
  2047. " capable cipher\n");
  2048. goto end;
  2049. } else if (!(EVP_CIPHER_get_flags(evp_cipher) &
  2050. EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
  2051. BIO_printf(bio_err, "%s is not a multi-block capable\n",
  2052. EVP_CIPHER_get0_name(evp_cipher));
  2053. goto end;
  2054. } else if (async_jobs > 0) {
  2055. BIO_printf(bio_err, "Async mode is not supported with -mb");
  2056. goto end;
  2057. }
  2058. }
  2059. /* Initialize the job pool if async mode is enabled */
  2060. if (async_jobs > 0) {
  2061. async_init = ASYNC_init_thread(async_jobs, async_jobs);
  2062. if (!async_init) {
  2063. BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
  2064. goto end;
  2065. }
  2066. }
  2067. loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
  2068. loopargs =
  2069. app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
  2070. memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
  2071. buflen = lengths[size_num - 1];
  2072. if (buflen < 36) /* size of random vector in RSA benchmark */
  2073. buflen = 36;
  2074. if (INT_MAX - (MAX_MISALIGNMENT + 1) < buflen) {
  2075. BIO_printf(bio_err, "Error: buffer size too large\n");
  2076. goto end;
  2077. }
  2078. buflen += MAX_MISALIGNMENT + 1;
  2079. for (i = 0; i < loopargs_len; i++) {
  2080. if (async_jobs > 0) {
  2081. loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
  2082. if (loopargs[i].wait_ctx == NULL) {
  2083. BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
  2084. goto end;
  2085. }
  2086. }
  2087. loopargs[i].buf_malloc = app_malloc(buflen, "input buffer");
  2088. loopargs[i].buf2_malloc = app_malloc(buflen, "input buffer");
  2089. /* Align the start of buffers on a 64 byte boundary */
  2090. loopargs[i].buf = loopargs[i].buf_malloc + misalign;
  2091. loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
  2092. loopargs[i].buflen = buflen - misalign;
  2093. loopargs[i].sigsize = buflen - misalign;
  2094. loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
  2095. loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
  2096. #ifndef OPENSSL_NO_DH
  2097. loopargs[i].secret_ff_a = app_malloc(MAX_FFDH_SIZE, "FFDH secret a");
  2098. loopargs[i].secret_ff_b = app_malloc(MAX_FFDH_SIZE, "FFDH secret b");
  2099. #endif
  2100. }
  2101. #ifndef NO_FORK
  2102. if (multi && do_multi(multi, size_num))
  2103. goto show_res;
  2104. #endif
  2105. for (i = 0; i < loopargs_len; ++i) {
  2106. if (domlock) {
  2107. #if defined(_WIN32)
  2108. (void)VirtualLock(loopargs[i].buf_malloc, buflen);
  2109. (void)VirtualLock(loopargs[i].buf2_malloc, buflen);
  2110. #elif defined(OPENSSL_SYS_LINUX)
  2111. (void)mlock(loopargs[i].buf_malloc, buflen);
  2112. (void)mlock(loopargs[i].buf_malloc, buflen);
  2113. #endif
  2114. }
  2115. memset(loopargs[i].buf_malloc, 0, buflen);
  2116. memset(loopargs[i].buf2_malloc, 0, buflen);
  2117. }
  2118. /* Initialize the engine after the fork */
  2119. e = setup_engine(engine_id, 0);
  2120. /* No parameters; turn on everything. */
  2121. if (argc == 0 && !doit[D_EVP] && !doit[D_HMAC]
  2122. && !doit[D_EVP_CMAC] && !do_kems && !do_sigs) {
  2123. memset(doit, 1, sizeof(doit));
  2124. doit[D_EVP] = doit[D_EVP_CMAC] = 0;
  2125. ERR_set_mark();
  2126. for (i = D_MD2; i <= D_WHIRLPOOL; i++) {
  2127. if (!have_md(names[i]))
  2128. doit[i] = 0;
  2129. }
  2130. for (i = D_CBC_DES; i <= D_CBC_256_CML; i++) {
  2131. if (!have_cipher(names[i]))
  2132. doit[i] = 0;
  2133. }
  2134. if ((mac = EVP_MAC_fetch(app_get0_libctx(), "GMAC",
  2135. app_get0_propq())) != NULL) {
  2136. EVP_MAC_free(mac);
  2137. mac = NULL;
  2138. } else {
  2139. doit[D_GHASH] = 0;
  2140. }
  2141. if ((mac = EVP_MAC_fetch(app_get0_libctx(), "HMAC",
  2142. app_get0_propq())) != NULL) {
  2143. EVP_MAC_free(mac);
  2144. mac = NULL;
  2145. } else {
  2146. doit[D_HMAC] = 0;
  2147. }
  2148. ERR_pop_to_mark();
  2149. memset(rsa_doit, 1, sizeof(rsa_doit));
  2150. #ifndef OPENSSL_NO_DH
  2151. memset(ffdh_doit, 1, sizeof(ffdh_doit));
  2152. #endif
  2153. memset(dsa_doit, 1, sizeof(dsa_doit));
  2154. #ifndef OPENSSL_NO_ECX
  2155. memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
  2156. memset(ecdh_doit, 1, sizeof(ecdh_doit));
  2157. memset(eddsa_doit, 1, sizeof(eddsa_doit));
  2158. #endif /* OPENSSL_NO_ECX */
  2159. #ifndef OPENSSL_NO_SM2
  2160. memset(sm2_doit, 1, sizeof(sm2_doit));
  2161. #endif
  2162. memset(kems_doit, 1, sizeof(kems_doit));
  2163. do_kems = 1;
  2164. memset(sigs_doit, 1, sizeof(sigs_doit));
  2165. do_sigs = 1;
  2166. }
  2167. for (i = 0; i < ALGOR_NUM; i++)
  2168. if (doit[i])
  2169. pr_header++;
  2170. if (usertime == 0 && !mr)
  2171. BIO_printf(bio_err,
  2172. "You have chosen to measure elapsed time "
  2173. "instead of user CPU time.\n");
  2174. #if SIGALRM > 0
  2175. signal(SIGALRM, alarmed);
  2176. #endif
  2177. if (doit[D_MD2]) {
  2178. for (testnum = 0; testnum < size_num; testnum++) {
  2179. print_message(names[D_MD2], lengths[testnum], seconds.sym);
  2180. Time_F(START);
  2181. count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
  2182. d = Time_F(STOP);
  2183. print_result(D_MD2, testnum, count, d);
  2184. if (count < 0)
  2185. break;
  2186. }
  2187. }
  2188. if (doit[D_MDC2]) {
  2189. for (testnum = 0; testnum < size_num; testnum++) {
  2190. print_message(names[D_MDC2], lengths[testnum], seconds.sym);
  2191. Time_F(START);
  2192. count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
  2193. d = Time_F(STOP);
  2194. print_result(D_MDC2, testnum, count, d);
  2195. if (count < 0)
  2196. break;
  2197. }
  2198. }
  2199. if (doit[D_MD4]) {
  2200. for (testnum = 0; testnum < size_num; testnum++) {
  2201. print_message(names[D_MD4], lengths[testnum], seconds.sym);
  2202. Time_F(START);
  2203. count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
  2204. d = Time_F(STOP);
  2205. print_result(D_MD4, testnum, count, d);
  2206. if (count < 0)
  2207. break;
  2208. }
  2209. }
  2210. if (doit[D_MD5]) {
  2211. for (testnum = 0; testnum < size_num; testnum++) {
  2212. print_message(names[D_MD5], lengths[testnum], seconds.sym);
  2213. Time_F(START);
  2214. count = run_benchmark(async_jobs, MD5_loop, loopargs);
  2215. d = Time_F(STOP);
  2216. print_result(D_MD5, testnum, count, d);
  2217. if (count < 0)
  2218. break;
  2219. }
  2220. }
  2221. if (doit[D_SHA1]) {
  2222. for (testnum = 0; testnum < size_num; testnum++) {
  2223. print_message(names[D_SHA1], lengths[testnum], seconds.sym);
  2224. Time_F(START);
  2225. count = run_benchmark(async_jobs, SHA1_loop, loopargs);
  2226. d = Time_F(STOP);
  2227. print_result(D_SHA1, testnum, count, d);
  2228. if (count < 0)
  2229. break;
  2230. }
  2231. }
  2232. if (doit[D_SHA256]) {
  2233. for (testnum = 0; testnum < size_num; testnum++) {
  2234. print_message(names[D_SHA256], lengths[testnum], seconds.sym);
  2235. Time_F(START);
  2236. count = run_benchmark(async_jobs, SHA256_loop, loopargs);
  2237. d = Time_F(STOP);
  2238. print_result(D_SHA256, testnum, count, d);
  2239. if (count < 0)
  2240. break;
  2241. }
  2242. }
  2243. if (doit[D_SHA512]) {
  2244. for (testnum = 0; testnum < size_num; testnum++) {
  2245. print_message(names[D_SHA512], lengths[testnum], seconds.sym);
  2246. Time_F(START);
  2247. count = run_benchmark(async_jobs, SHA512_loop, loopargs);
  2248. d = Time_F(STOP);
  2249. print_result(D_SHA512, testnum, count, d);
  2250. if (count < 0)
  2251. break;
  2252. }
  2253. }
  2254. if (doit[D_WHIRLPOOL]) {
  2255. for (testnum = 0; testnum < size_num; testnum++) {
  2256. print_message(names[D_WHIRLPOOL], lengths[testnum], seconds.sym);
  2257. Time_F(START);
  2258. count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
  2259. d = Time_F(STOP);
  2260. print_result(D_WHIRLPOOL, testnum, count, d);
  2261. if (count < 0)
  2262. break;
  2263. }
  2264. }
  2265. if (doit[D_RMD160]) {
  2266. for (testnum = 0; testnum < size_num; testnum++) {
  2267. print_message(names[D_RMD160], lengths[testnum], seconds.sym);
  2268. Time_F(START);
  2269. count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
  2270. d = Time_F(STOP);
  2271. print_result(D_RMD160, testnum, count, d);
  2272. if (count < 0)
  2273. break;
  2274. }
  2275. }
  2276. if (doit[D_HMAC]) {
  2277. static const char hmac_key[] = "This is a key...";
  2278. int len = strlen(hmac_key);
  2279. OSSL_PARAM params[3];
  2280. mac = EVP_MAC_fetch(app_get0_libctx(), "HMAC", app_get0_propq());
  2281. if (mac == NULL || evp_mac_mdname == NULL)
  2282. goto end;
  2283. evp_hmac_name = app_malloc(sizeof("hmac()") + strlen(evp_mac_mdname),
  2284. "HMAC name");
  2285. sprintf(evp_hmac_name, "hmac(%s)", evp_mac_mdname);
  2286. names[D_HMAC] = evp_hmac_name;
  2287. params[0] =
  2288. OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  2289. evp_mac_mdname, 0);
  2290. params[1] =
  2291. OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  2292. (char *)hmac_key, len);
  2293. params[2] = OSSL_PARAM_construct_end();
  2294. for (i = 0; i < loopargs_len; i++) {
  2295. loopargs[i].mctx = EVP_MAC_CTX_new(mac);
  2296. if (loopargs[i].mctx == NULL)
  2297. goto end;
  2298. if (!EVP_MAC_CTX_set_params(loopargs[i].mctx, params))
  2299. goto skip_hmac; /* Digest not found */
  2300. }
  2301. for (testnum = 0; testnum < size_num; testnum++) {
  2302. print_message(names[D_HMAC], lengths[testnum], seconds.sym);
  2303. Time_F(START);
  2304. count = run_benchmark(async_jobs, HMAC_loop, loopargs);
  2305. d = Time_F(STOP);
  2306. print_result(D_HMAC, testnum, count, d);
  2307. if (count < 0)
  2308. break;
  2309. }
  2310. for (i = 0; i < loopargs_len; i++)
  2311. EVP_MAC_CTX_free(loopargs[i].mctx);
  2312. EVP_MAC_free(mac);
  2313. mac = NULL;
  2314. }
  2315. skip_hmac:
  2316. if (doit[D_CBC_DES]) {
  2317. int st = 1;
  2318. for (i = 0; st && i < loopargs_len; i++) {
  2319. loopargs[i].ctx = init_evp_cipher_ctx("des-cbc", deskey,
  2320. sizeof(deskey) / 3);
  2321. st = loopargs[i].ctx != NULL;
  2322. }
  2323. algindex = D_CBC_DES;
  2324. for (testnum = 0; st && testnum < size_num; testnum++) {
  2325. print_message(names[D_CBC_DES], lengths[testnum], seconds.sym);
  2326. Time_F(START);
  2327. count = run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
  2328. d = Time_F(STOP);
  2329. print_result(D_CBC_DES, testnum, count, d);
  2330. }
  2331. for (i = 0; i < loopargs_len; i++)
  2332. EVP_CIPHER_CTX_free(loopargs[i].ctx);
  2333. }
  2334. if (doit[D_EDE3_DES]) {
  2335. int st = 1;
  2336. for (i = 0; st && i < loopargs_len; i++) {
  2337. loopargs[i].ctx = init_evp_cipher_ctx("des-ede3-cbc", deskey,
  2338. sizeof(deskey));
  2339. st = loopargs[i].ctx != NULL;
  2340. }
  2341. algindex = D_EDE3_DES;
  2342. for (testnum = 0; st && testnum < size_num; testnum++) {
  2343. print_message(names[D_EDE3_DES], lengths[testnum], seconds.sym);
  2344. Time_F(START);
  2345. count =
  2346. run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
  2347. d = Time_F(STOP);
  2348. print_result(D_EDE3_DES, testnum, count, d);
  2349. }
  2350. for (i = 0; i < loopargs_len; i++)
  2351. EVP_CIPHER_CTX_free(loopargs[i].ctx);
  2352. }
  2353. for (k = 0; k < 3; k++) {
  2354. algindex = D_CBC_128_AES + k;
  2355. if (doit[algindex]) {
  2356. int st = 1;
  2357. keylen = 16 + k * 8;
  2358. for (i = 0; st && i < loopargs_len; i++) {
  2359. loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
  2360. key32, keylen);
  2361. st = loopargs[i].ctx != NULL;
  2362. }
  2363. for (testnum = 0; st && testnum < size_num; testnum++) {
  2364. print_message(names[algindex], lengths[testnum], seconds.sym);
  2365. Time_F(START);
  2366. count =
  2367. run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
  2368. d = Time_F(STOP);
  2369. print_result(algindex, testnum, count, d);
  2370. }
  2371. for (i = 0; i < loopargs_len; i++)
  2372. EVP_CIPHER_CTX_free(loopargs[i].ctx);
  2373. }
  2374. }
  2375. for (k = 0; k < 3; k++) {
  2376. algindex = D_CBC_128_CML + k;
  2377. if (doit[algindex]) {
  2378. int st = 1;
  2379. keylen = 16 + k * 8;
  2380. for (i = 0; st && i < loopargs_len; i++) {
  2381. loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
  2382. key32, keylen);
  2383. st = loopargs[i].ctx != NULL;
  2384. }
  2385. for (testnum = 0; st && testnum < size_num; testnum++) {
  2386. print_message(names[algindex], lengths[testnum], seconds.sym);
  2387. Time_F(START);
  2388. count =
  2389. run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
  2390. d = Time_F(STOP);
  2391. print_result(algindex, testnum, count, d);
  2392. }
  2393. for (i = 0; i < loopargs_len; i++)
  2394. EVP_CIPHER_CTX_free(loopargs[i].ctx);
  2395. }
  2396. }
  2397. for (algindex = D_RC4; algindex <= D_CBC_CAST; algindex++) {
  2398. if (doit[algindex]) {
  2399. int st = 1;
  2400. keylen = 16;
  2401. for (i = 0; st && i < loopargs_len; i++) {
  2402. loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
  2403. key32, keylen);
  2404. st = loopargs[i].ctx != NULL;
  2405. }
  2406. for (testnum = 0; st && testnum < size_num; testnum++) {
  2407. print_message(names[algindex], lengths[testnum], seconds.sym);
  2408. Time_F(START);
  2409. count =
  2410. run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
  2411. d = Time_F(STOP);
  2412. print_result(algindex, testnum, count, d);
  2413. }
  2414. for (i = 0; i < loopargs_len; i++)
  2415. EVP_CIPHER_CTX_free(loopargs[i].ctx);
  2416. }
  2417. }
  2418. if (doit[D_GHASH]) {
  2419. static const char gmac_iv[] = "0123456789ab";
  2420. OSSL_PARAM params[3];
  2421. mac = EVP_MAC_fetch(app_get0_libctx(), "GMAC", app_get0_propq());
  2422. if (mac == NULL)
  2423. goto end;
  2424. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER,
  2425. "aes-128-gcm", 0);
  2426. params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
  2427. (char *)gmac_iv,
  2428. sizeof(gmac_iv) - 1);
  2429. params[2] = OSSL_PARAM_construct_end();
  2430. for (i = 0; i < loopargs_len; i++) {
  2431. loopargs[i].mctx = EVP_MAC_CTX_new(mac);
  2432. if (loopargs[i].mctx == NULL)
  2433. goto end;
  2434. if (!EVP_MAC_init(loopargs[i].mctx, key32, 16, params))
  2435. goto end;
  2436. }
  2437. for (testnum = 0; testnum < size_num; testnum++) {
  2438. print_message(names[D_GHASH], lengths[testnum], seconds.sym);
  2439. Time_F(START);
  2440. count = run_benchmark(async_jobs, GHASH_loop, loopargs);
  2441. d = Time_F(STOP);
  2442. print_result(D_GHASH, testnum, count, d);
  2443. if (count < 0)
  2444. break;
  2445. }
  2446. for (i = 0; i < loopargs_len; i++)
  2447. EVP_MAC_CTX_free(loopargs[i].mctx);
  2448. EVP_MAC_free(mac);
  2449. mac = NULL;
  2450. }
  2451. if (doit[D_RAND]) {
  2452. for (testnum = 0; testnum < size_num; testnum++) {
  2453. print_message(names[D_RAND], lengths[testnum], seconds.sym);
  2454. Time_F(START);
  2455. count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
  2456. d = Time_F(STOP);
  2457. print_result(D_RAND, testnum, count, d);
  2458. }
  2459. }
  2460. if (doit[D_EVP]) {
  2461. if (evp_cipher != NULL) {
  2462. int (*loopfunc) (void *) = EVP_Update_loop;
  2463. if (multiblock && (EVP_CIPHER_get_flags(evp_cipher) &
  2464. EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
  2465. multiblock_speed(evp_cipher, lengths_single, &seconds);
  2466. ret = 0;
  2467. goto end;
  2468. }
  2469. names[D_EVP] = EVP_CIPHER_get0_name(evp_cipher);
  2470. if (EVP_CIPHER_get_mode(evp_cipher) == EVP_CIPH_CCM_MODE) {
  2471. loopfunc = EVP_Update_loop_ccm;
  2472. } else if (aead && (EVP_CIPHER_get_flags(evp_cipher) &
  2473. EVP_CIPH_FLAG_AEAD_CIPHER)) {
  2474. loopfunc = EVP_Update_loop_aead;
  2475. if (lengths == lengths_list) {
  2476. lengths = aead_lengths_list;
  2477. size_num = OSSL_NELEM(aead_lengths_list);
  2478. }
  2479. }
  2480. for (testnum = 0; testnum < size_num; testnum++) {
  2481. print_message(names[D_EVP], lengths[testnum], seconds.sym);
  2482. for (k = 0; k < loopargs_len; k++) {
  2483. loopargs[k].ctx = EVP_CIPHER_CTX_new();
  2484. if (loopargs[k].ctx == NULL) {
  2485. BIO_printf(bio_err, "\nEVP_CIPHER_CTX_new failure\n");
  2486. exit(1);
  2487. }
  2488. if (!EVP_CipherInit_ex(loopargs[k].ctx, evp_cipher, NULL,
  2489. NULL, iv, decrypt ? 0 : 1)) {
  2490. BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
  2491. ERR_print_errors(bio_err);
  2492. exit(1);
  2493. }
  2494. EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
  2495. keylen = EVP_CIPHER_CTX_get_key_length(loopargs[k].ctx);
  2496. loopargs[k].key = app_malloc(keylen, "evp_cipher key");
  2497. EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
  2498. if (!EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
  2499. loopargs[k].key, NULL, -1)) {
  2500. BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
  2501. ERR_print_errors(bio_err);
  2502. exit(1);
  2503. }
  2504. OPENSSL_clear_free(loopargs[k].key, keylen);
  2505. /* GCM-SIV/SIV mode only allows for a single Update operation */
  2506. if (EVP_CIPHER_get_mode(evp_cipher) == EVP_CIPH_SIV_MODE
  2507. || EVP_CIPHER_get_mode(evp_cipher) == EVP_CIPH_GCM_SIV_MODE)
  2508. (void)EVP_CIPHER_CTX_ctrl(loopargs[k].ctx,
  2509. EVP_CTRL_SET_SPEED, 1, NULL);
  2510. }
  2511. Time_F(START);
  2512. count = run_benchmark(async_jobs, loopfunc, loopargs);
  2513. d = Time_F(STOP);
  2514. for (k = 0; k < loopargs_len; k++)
  2515. EVP_CIPHER_CTX_free(loopargs[k].ctx);
  2516. print_result(D_EVP, testnum, count, d);
  2517. }
  2518. } else if (evp_md_name != NULL) {
  2519. names[D_EVP] = evp_md_name;
  2520. for (testnum = 0; testnum < size_num; testnum++) {
  2521. print_message(names[D_EVP], lengths[testnum], seconds.sym);
  2522. Time_F(START);
  2523. count = run_benchmark(async_jobs, EVP_Digest_md_loop, loopargs);
  2524. d = Time_F(STOP);
  2525. print_result(D_EVP, testnum, count, d);
  2526. if (count < 0)
  2527. break;
  2528. }
  2529. }
  2530. }
  2531. if (doit[D_EVP_CMAC]) {
  2532. OSSL_PARAM params[3];
  2533. EVP_CIPHER *cipher = NULL;
  2534. mac = EVP_MAC_fetch(app_get0_libctx(), "CMAC", app_get0_propq());
  2535. if (mac == NULL || evp_mac_ciphername == NULL)
  2536. goto end;
  2537. if (!opt_cipher(evp_mac_ciphername, &cipher))
  2538. goto end;
  2539. keylen = EVP_CIPHER_get_key_length(cipher);
  2540. EVP_CIPHER_free(cipher);
  2541. if (keylen <= 0 || keylen > (int)sizeof(key32)) {
  2542. BIO_printf(bio_err, "\nRequested CMAC cipher with unsupported key length.\n");
  2543. goto end;
  2544. }
  2545. evp_cmac_name = app_malloc(sizeof("cmac()")
  2546. + strlen(evp_mac_ciphername), "CMAC name");
  2547. sprintf(evp_cmac_name, "cmac(%s)", evp_mac_ciphername);
  2548. names[D_EVP_CMAC] = evp_cmac_name;
  2549. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER,
  2550. evp_mac_ciphername, 0);
  2551. params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  2552. (char *)key32, keylen);
  2553. params[2] = OSSL_PARAM_construct_end();
  2554. for (i = 0; i < loopargs_len; i++) {
  2555. loopargs[i].mctx = EVP_MAC_CTX_new(mac);
  2556. if (loopargs[i].mctx == NULL)
  2557. goto end;
  2558. if (!EVP_MAC_CTX_set_params(loopargs[i].mctx, params))
  2559. goto end;
  2560. }
  2561. for (testnum = 0; testnum < size_num; testnum++) {
  2562. print_message(names[D_EVP_CMAC], lengths[testnum], seconds.sym);
  2563. Time_F(START);
  2564. count = run_benchmark(async_jobs, CMAC_loop, loopargs);
  2565. d = Time_F(STOP);
  2566. print_result(D_EVP_CMAC, testnum, count, d);
  2567. if (count < 0)
  2568. break;
  2569. }
  2570. for (i = 0; i < loopargs_len; i++)
  2571. EVP_MAC_CTX_free(loopargs[i].mctx);
  2572. EVP_MAC_free(mac);
  2573. mac = NULL;
  2574. }
  2575. for (i = 0; i < loopargs_len; i++)
  2576. if (RAND_bytes(loopargs[i].buf, 36) <= 0)
  2577. goto end;
  2578. for (testnum = 0; testnum < RSA_NUM; testnum++) {
  2579. EVP_PKEY *rsa_key = NULL;
  2580. int st = 0;
  2581. if (!rsa_doit[testnum])
  2582. continue;
  2583. if (primes > RSA_DEFAULT_PRIME_NUM) {
  2584. /* we haven't set keys yet, generate multi-prime RSA keys */
  2585. bn = BN_new();
  2586. st = bn != NULL
  2587. && BN_set_word(bn, RSA_F4)
  2588. && init_gen_str(&genctx, "RSA", NULL, 0, NULL, NULL)
  2589. && EVP_PKEY_CTX_set_rsa_keygen_bits(genctx, rsa_keys[testnum].bits) > 0
  2590. && EVP_PKEY_CTX_set1_rsa_keygen_pubexp(genctx, bn) > 0
  2591. && EVP_PKEY_CTX_set_rsa_keygen_primes(genctx, primes) > 0
  2592. && EVP_PKEY_keygen(genctx, &rsa_key);
  2593. BN_free(bn);
  2594. bn = NULL;
  2595. EVP_PKEY_CTX_free(genctx);
  2596. genctx = NULL;
  2597. } else {
  2598. const unsigned char *p = rsa_keys[testnum].data;
  2599. st = (rsa_key = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p,
  2600. rsa_keys[testnum].length)) != NULL;
  2601. }
  2602. for (i = 0; st && i < loopargs_len; i++) {
  2603. loopargs[i].rsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key, NULL);
  2604. loopargs[i].sigsize = loopargs[i].buflen;
  2605. if (loopargs[i].rsa_sign_ctx[testnum] == NULL
  2606. || EVP_PKEY_sign_init(loopargs[i].rsa_sign_ctx[testnum]) <= 0
  2607. || EVP_PKEY_sign(loopargs[i].rsa_sign_ctx[testnum],
  2608. loopargs[i].buf2,
  2609. &loopargs[i].sigsize,
  2610. loopargs[i].buf, 36) <= 0)
  2611. st = 0;
  2612. }
  2613. if (!st) {
  2614. BIO_printf(bio_err,
  2615. "RSA sign setup failure. No RSA sign will be done.\n");
  2616. ERR_print_errors(bio_err);
  2617. op_count = 1;
  2618. } else {
  2619. pkey_print_message("private", "rsa sign",
  2620. rsa_keys[testnum].bits, seconds.rsa);
  2621. /* RSA_blinding_on(rsa_key[testnum],NULL); */
  2622. Time_F(START);
  2623. count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
  2624. d = Time_F(STOP);
  2625. BIO_printf(bio_err,
  2626. mr ? "+R1:%ld:%d:%.2f\n"
  2627. : "%ld %u bits private RSA sign ops in %.2fs\n",
  2628. count, rsa_keys[testnum].bits, d);
  2629. rsa_results[testnum][0] = (double)count / d;
  2630. op_count = count;
  2631. }
  2632. for (i = 0; st && i < loopargs_len; i++) {
  2633. loopargs[i].rsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key,
  2634. NULL);
  2635. if (loopargs[i].rsa_verify_ctx[testnum] == NULL
  2636. || EVP_PKEY_verify_init(loopargs[i].rsa_verify_ctx[testnum]) <= 0
  2637. || EVP_PKEY_verify(loopargs[i].rsa_verify_ctx[testnum],
  2638. loopargs[i].buf2,
  2639. loopargs[i].sigsize,
  2640. loopargs[i].buf, 36) <= 0)
  2641. st = 0;
  2642. }
  2643. if (!st) {
  2644. BIO_printf(bio_err,
  2645. "RSA verify setup failure. No RSA verify will be done.\n");
  2646. ERR_print_errors(bio_err);
  2647. rsa_doit[testnum] = 0;
  2648. } else {
  2649. pkey_print_message("public", "rsa verify",
  2650. rsa_keys[testnum].bits, seconds.rsa);
  2651. Time_F(START);
  2652. count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
  2653. d = Time_F(STOP);
  2654. BIO_printf(bio_err,
  2655. mr ? "+R2:%ld:%d:%.2f\n"
  2656. : "%ld %u bits public RSA verify ops in %.2fs\n",
  2657. count, rsa_keys[testnum].bits, d);
  2658. rsa_results[testnum][1] = (double)count / d;
  2659. }
  2660. for (i = 0; st && i < loopargs_len; i++) {
  2661. loopargs[i].rsa_encrypt_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key, NULL);
  2662. loopargs[i].encsize = loopargs[i].buflen;
  2663. if (loopargs[i].rsa_encrypt_ctx[testnum] == NULL
  2664. || EVP_PKEY_encrypt_init(loopargs[i].rsa_encrypt_ctx[testnum]) <= 0
  2665. || EVP_PKEY_encrypt(loopargs[i].rsa_encrypt_ctx[testnum],
  2666. loopargs[i].buf2,
  2667. &loopargs[i].encsize,
  2668. loopargs[i].buf, 36) <= 0)
  2669. st = 0;
  2670. }
  2671. if (!st) {
  2672. BIO_printf(bio_err,
  2673. "RSA encrypt setup failure. No RSA encrypt will be done.\n");
  2674. ERR_print_errors(bio_err);
  2675. op_count = 1;
  2676. } else {
  2677. pkey_print_message("private", "rsa encrypt",
  2678. rsa_keys[testnum].bits, seconds.rsa);
  2679. /* RSA_blinding_on(rsa_key[testnum],NULL); */
  2680. Time_F(START);
  2681. count = run_benchmark(async_jobs, RSA_encrypt_loop, loopargs);
  2682. d = Time_F(STOP);
  2683. BIO_printf(bio_err,
  2684. mr ? "+R3:%ld:%d:%.2f\n"
  2685. : "%ld %u bits public RSA encrypt ops in %.2fs\n",
  2686. count, rsa_keys[testnum].bits, d);
  2687. rsa_results[testnum][2] = (double)count / d;
  2688. op_count = count;
  2689. }
  2690. for (i = 0; st && i < loopargs_len; i++) {
  2691. loopargs[i].rsa_decrypt_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key, NULL);
  2692. declen = loopargs[i].buflen;
  2693. if (loopargs[i].rsa_decrypt_ctx[testnum] == NULL
  2694. || EVP_PKEY_decrypt_init(loopargs[i].rsa_decrypt_ctx[testnum]) <= 0
  2695. || EVP_PKEY_decrypt(loopargs[i].rsa_decrypt_ctx[testnum],
  2696. loopargs[i].buf,
  2697. &declen,
  2698. loopargs[i].buf2,
  2699. loopargs[i].encsize) <= 0)
  2700. st = 0;
  2701. }
  2702. if (!st) {
  2703. BIO_printf(bio_err,
  2704. "RSA decrypt setup failure. No RSA decrypt will be done.\n");
  2705. ERR_print_errors(bio_err);
  2706. op_count = 1;
  2707. } else {
  2708. pkey_print_message("private", "rsa decrypt",
  2709. rsa_keys[testnum].bits, seconds.rsa);
  2710. /* RSA_blinding_on(rsa_key[testnum],NULL); */
  2711. Time_F(START);
  2712. count = run_benchmark(async_jobs, RSA_decrypt_loop, loopargs);
  2713. d = Time_F(STOP);
  2714. BIO_printf(bio_err,
  2715. mr ? "+R4:%ld:%d:%.2f\n"
  2716. : "%ld %u bits private RSA decrypt ops in %.2fs\n",
  2717. count, rsa_keys[testnum].bits, d);
  2718. rsa_results[testnum][3] = (double)count / d;
  2719. op_count = count;
  2720. }
  2721. if (op_count <= 1) {
  2722. /* if longer than 10s, don't do any more */
  2723. stop_it(rsa_doit, testnum);
  2724. }
  2725. EVP_PKEY_free(rsa_key);
  2726. }
  2727. for (testnum = 0; testnum < DSA_NUM; testnum++) {
  2728. EVP_PKEY *dsa_key = NULL;
  2729. int st;
  2730. if (!dsa_doit[testnum])
  2731. continue;
  2732. st = (dsa_key = get_dsa(dsa_bits[testnum])) != NULL;
  2733. for (i = 0; st && i < loopargs_len; i++) {
  2734. loopargs[i].dsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(dsa_key,
  2735. NULL);
  2736. loopargs[i].sigsize = loopargs[i].buflen;
  2737. if (loopargs[i].dsa_sign_ctx[testnum] == NULL
  2738. || EVP_PKEY_sign_init(loopargs[i].dsa_sign_ctx[testnum]) <= 0
  2739. || EVP_PKEY_sign(loopargs[i].dsa_sign_ctx[testnum],
  2740. loopargs[i].buf2,
  2741. &loopargs[i].sigsize,
  2742. loopargs[i].buf, 20) <= 0)
  2743. st = 0;
  2744. }
  2745. if (!st) {
  2746. BIO_printf(bio_err,
  2747. "DSA sign setup failure. No DSA sign will be done.\n");
  2748. ERR_print_errors(bio_err);
  2749. op_count = 1;
  2750. } else {
  2751. pkey_print_message("sign", "dsa",
  2752. dsa_bits[testnum], seconds.dsa);
  2753. Time_F(START);
  2754. count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
  2755. d = Time_F(STOP);
  2756. BIO_printf(bio_err,
  2757. mr ? "+R5:%ld:%u:%.2f\n"
  2758. : "%ld %u bits DSA sign ops in %.2fs\n",
  2759. count, dsa_bits[testnum], d);
  2760. dsa_results[testnum][0] = (double)count / d;
  2761. op_count = count;
  2762. }
  2763. for (i = 0; st && i < loopargs_len; i++) {
  2764. loopargs[i].dsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(dsa_key,
  2765. NULL);
  2766. if (loopargs[i].dsa_verify_ctx[testnum] == NULL
  2767. || EVP_PKEY_verify_init(loopargs[i].dsa_verify_ctx[testnum]) <= 0
  2768. || EVP_PKEY_verify(loopargs[i].dsa_verify_ctx[testnum],
  2769. loopargs[i].buf2,
  2770. loopargs[i].sigsize,
  2771. loopargs[i].buf, 36) <= 0)
  2772. st = 0;
  2773. }
  2774. if (!st) {
  2775. BIO_printf(bio_err,
  2776. "DSA verify setup failure. No DSA verify will be done.\n");
  2777. ERR_print_errors(bio_err);
  2778. dsa_doit[testnum] = 0;
  2779. } else {
  2780. pkey_print_message("verify", "dsa",
  2781. dsa_bits[testnum], seconds.dsa);
  2782. Time_F(START);
  2783. count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
  2784. d = Time_F(STOP);
  2785. BIO_printf(bio_err,
  2786. mr ? "+R6:%ld:%u:%.2f\n"
  2787. : "%ld %u bits DSA verify ops in %.2fs\n",
  2788. count, dsa_bits[testnum], d);
  2789. dsa_results[testnum][1] = (double)count / d;
  2790. }
  2791. if (op_count <= 1) {
  2792. /* if longer than 10s, don't do any more */
  2793. stop_it(dsa_doit, testnum);
  2794. }
  2795. EVP_PKEY_free(dsa_key);
  2796. }
  2797. for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
  2798. EVP_PKEY *ecdsa_key = NULL;
  2799. int st;
  2800. if (!ecdsa_doit[testnum])
  2801. continue;
  2802. st = (ecdsa_key = get_ecdsa(&ec_curves[testnum])) != NULL;
  2803. for (i = 0; st && i < loopargs_len; i++) {
  2804. loopargs[i].ecdsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(ecdsa_key,
  2805. NULL);
  2806. loopargs[i].sigsize = loopargs[i].buflen;
  2807. if (loopargs[i].ecdsa_sign_ctx[testnum] == NULL
  2808. || EVP_PKEY_sign_init(loopargs[i].ecdsa_sign_ctx[testnum]) <= 0
  2809. || EVP_PKEY_sign(loopargs[i].ecdsa_sign_ctx[testnum],
  2810. loopargs[i].buf2,
  2811. &loopargs[i].sigsize,
  2812. loopargs[i].buf, 20) <= 0)
  2813. st = 0;
  2814. }
  2815. if (!st) {
  2816. BIO_printf(bio_err,
  2817. "ECDSA sign setup failure. No ECDSA sign will be done.\n");
  2818. ERR_print_errors(bio_err);
  2819. op_count = 1;
  2820. } else {
  2821. pkey_print_message("sign", "ecdsa",
  2822. ec_curves[testnum].bits, seconds.ecdsa);
  2823. Time_F(START);
  2824. count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
  2825. d = Time_F(STOP);
  2826. BIO_printf(bio_err,
  2827. mr ? "+R7:%ld:%u:%.2f\n"
  2828. : "%ld %u bits ECDSA sign ops in %.2fs\n",
  2829. count, ec_curves[testnum].bits, d);
  2830. ecdsa_results[testnum][0] = (double)count / d;
  2831. op_count = count;
  2832. }
  2833. for (i = 0; st && i < loopargs_len; i++) {
  2834. loopargs[i].ecdsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(ecdsa_key,
  2835. NULL);
  2836. if (loopargs[i].ecdsa_verify_ctx[testnum] == NULL
  2837. || EVP_PKEY_verify_init(loopargs[i].ecdsa_verify_ctx[testnum]) <= 0
  2838. || EVP_PKEY_verify(loopargs[i].ecdsa_verify_ctx[testnum],
  2839. loopargs[i].buf2,
  2840. loopargs[i].sigsize,
  2841. loopargs[i].buf, 20) <= 0)
  2842. st = 0;
  2843. }
  2844. if (!st) {
  2845. BIO_printf(bio_err,
  2846. "ECDSA verify setup failure. No ECDSA verify will be done.\n");
  2847. ERR_print_errors(bio_err);
  2848. ecdsa_doit[testnum] = 0;
  2849. } else {
  2850. pkey_print_message("verify", "ecdsa",
  2851. ec_curves[testnum].bits, seconds.ecdsa);
  2852. Time_F(START);
  2853. count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
  2854. d = Time_F(STOP);
  2855. BIO_printf(bio_err,
  2856. mr ? "+R8:%ld:%u:%.2f\n"
  2857. : "%ld %u bits ECDSA verify ops in %.2fs\n",
  2858. count, ec_curves[testnum].bits, d);
  2859. ecdsa_results[testnum][1] = (double)count / d;
  2860. }
  2861. if (op_count <= 1) {
  2862. /* if longer than 10s, don't do any more */
  2863. stop_it(ecdsa_doit, testnum);
  2864. }
  2865. }
  2866. for (testnum = 0; testnum < EC_NUM; testnum++) {
  2867. int ecdh_checks = 1;
  2868. if (!ecdh_doit[testnum])
  2869. continue;
  2870. for (i = 0; i < loopargs_len; i++) {
  2871. EVP_PKEY_CTX *test_ctx = NULL;
  2872. EVP_PKEY_CTX *ctx = NULL;
  2873. EVP_PKEY *key_A = NULL;
  2874. EVP_PKEY *key_B = NULL;
  2875. size_t outlen;
  2876. size_t test_outlen;
  2877. if ((key_A = get_ecdsa(&ec_curves[testnum])) == NULL /* generate secret key A */
  2878. || (key_B = get_ecdsa(&ec_curves[testnum])) == NULL /* generate secret key B */
  2879. || (ctx = EVP_PKEY_CTX_new(key_A, NULL)) == NULL /* derivation ctx from skeyA */
  2880. || EVP_PKEY_derive_init(ctx) <= 0 /* init derivation ctx */
  2881. || EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 /* set peer pubkey in ctx */
  2882. || EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 /* determine max length */
  2883. || outlen == 0 /* ensure outlen is a valid size */
  2884. || outlen > MAX_ECDH_SIZE /* avoid buffer overflow */) {
  2885. ecdh_checks = 0;
  2886. BIO_printf(bio_err, "ECDH key generation failure.\n");
  2887. ERR_print_errors(bio_err);
  2888. op_count = 1;
  2889. break;
  2890. }
  2891. /*
  2892. * Here we perform a test run, comparing the output of a*B and b*A;
  2893. * we try this here and assume that further EVP_PKEY_derive calls
  2894. * never fail, so we can skip checks in the actually benchmarked
  2895. * code, for maximum performance.
  2896. */
  2897. if ((test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) == NULL /* test ctx from skeyB */
  2898. || EVP_PKEY_derive_init(test_ctx) <= 0 /* init derivation test_ctx */
  2899. || EVP_PKEY_derive_set_peer(test_ctx, key_A) <= 0 /* set peer pubkey in test_ctx */
  2900. || EVP_PKEY_derive(test_ctx, NULL, &test_outlen) <= 0 /* determine max length */
  2901. || EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) <= 0 /* compute a*B */
  2902. || EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) <= 0 /* compute b*A */
  2903. || test_outlen != outlen /* compare output length */) {
  2904. ecdh_checks = 0;
  2905. BIO_printf(bio_err, "ECDH computation failure.\n");
  2906. ERR_print_errors(bio_err);
  2907. op_count = 1;
  2908. break;
  2909. }
  2910. /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
  2911. if (CRYPTO_memcmp(loopargs[i].secret_a,
  2912. loopargs[i].secret_b, outlen)) {
  2913. ecdh_checks = 0;
  2914. BIO_printf(bio_err, "ECDH computations don't match.\n");
  2915. ERR_print_errors(bio_err);
  2916. op_count = 1;
  2917. break;
  2918. }
  2919. loopargs[i].ecdh_ctx[testnum] = ctx;
  2920. loopargs[i].outlen[testnum] = outlen;
  2921. EVP_PKEY_free(key_A);
  2922. EVP_PKEY_free(key_B);
  2923. EVP_PKEY_CTX_free(test_ctx);
  2924. test_ctx = NULL;
  2925. }
  2926. if (ecdh_checks != 0) {
  2927. pkey_print_message("", "ecdh",
  2928. ec_curves[testnum].bits, seconds.ecdh);
  2929. Time_F(START);
  2930. count =
  2931. run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
  2932. d = Time_F(STOP);
  2933. BIO_printf(bio_err,
  2934. mr ? "+R9:%ld:%d:%.2f\n" :
  2935. "%ld %u-bits ECDH ops in %.2fs\n", count,
  2936. ec_curves[testnum].bits, d);
  2937. ecdh_results[testnum][0] = (double)count / d;
  2938. op_count = count;
  2939. }
  2940. if (op_count <= 1) {
  2941. /* if longer than 10s, don't do any more */
  2942. stop_it(ecdh_doit, testnum);
  2943. }
  2944. }
  2945. #ifndef OPENSSL_NO_ECX
  2946. for (testnum = 0; testnum < EdDSA_NUM; testnum++) {
  2947. int st = 1;
  2948. EVP_PKEY *ed_pkey = NULL;
  2949. EVP_PKEY_CTX *ed_pctx = NULL;
  2950. if (!eddsa_doit[testnum])
  2951. continue; /* Ignore Curve */
  2952. for (i = 0; i < loopargs_len; i++) {
  2953. loopargs[i].eddsa_ctx[testnum] = EVP_MD_CTX_new();
  2954. if (loopargs[i].eddsa_ctx[testnum] == NULL) {
  2955. st = 0;
  2956. break;
  2957. }
  2958. loopargs[i].eddsa_ctx2[testnum] = EVP_MD_CTX_new();
  2959. if (loopargs[i].eddsa_ctx2[testnum] == NULL) {
  2960. st = 0;
  2961. break;
  2962. }
  2963. if ((ed_pctx = EVP_PKEY_CTX_new_id(ed_curves[testnum].nid,
  2964. NULL)) == NULL
  2965. || EVP_PKEY_keygen_init(ed_pctx) <= 0
  2966. || EVP_PKEY_keygen(ed_pctx, &ed_pkey) <= 0) {
  2967. st = 0;
  2968. EVP_PKEY_CTX_free(ed_pctx);
  2969. break;
  2970. }
  2971. EVP_PKEY_CTX_free(ed_pctx);
  2972. if (!EVP_DigestSignInit(loopargs[i].eddsa_ctx[testnum], NULL, NULL,
  2973. NULL, ed_pkey)) {
  2974. st = 0;
  2975. EVP_PKEY_free(ed_pkey);
  2976. break;
  2977. }
  2978. if (!EVP_DigestVerifyInit(loopargs[i].eddsa_ctx2[testnum], NULL,
  2979. NULL, NULL, ed_pkey)) {
  2980. st = 0;
  2981. EVP_PKEY_free(ed_pkey);
  2982. break;
  2983. }
  2984. EVP_PKEY_free(ed_pkey);
  2985. ed_pkey = NULL;
  2986. }
  2987. if (st == 0) {
  2988. BIO_printf(bio_err, "EdDSA failure.\n");
  2989. ERR_print_errors(bio_err);
  2990. op_count = 1;
  2991. } else {
  2992. for (i = 0; i < loopargs_len; i++) {
  2993. /* Perform EdDSA signature test */
  2994. loopargs[i].sigsize = ed_curves[testnum].sigsize;
  2995. st = EVP_DigestSign(loopargs[i].eddsa_ctx[testnum],
  2996. loopargs[i].buf2, &loopargs[i].sigsize,
  2997. loopargs[i].buf, 20);
  2998. if (st == 0)
  2999. break;
  3000. }
  3001. if (st == 0) {
  3002. BIO_printf(bio_err,
  3003. "EdDSA sign failure. No EdDSA sign will be done.\n");
  3004. ERR_print_errors(bio_err);
  3005. op_count = 1;
  3006. } else {
  3007. pkey_print_message("sign", ed_curves[testnum].name,
  3008. ed_curves[testnum].bits, seconds.eddsa);
  3009. Time_F(START);
  3010. count = run_benchmark(async_jobs, EdDSA_sign_loop, loopargs);
  3011. d = Time_F(STOP);
  3012. BIO_printf(bio_err,
  3013. mr ? "+R10:%ld:%u:%s:%.2f\n" :
  3014. "%ld %u bits %s sign ops in %.2fs \n",
  3015. count, ed_curves[testnum].bits,
  3016. ed_curves[testnum].name, d);
  3017. eddsa_results[testnum][0] = (double)count / d;
  3018. op_count = count;
  3019. }
  3020. /* Perform EdDSA verification test */
  3021. for (i = 0; i < loopargs_len; i++) {
  3022. st = EVP_DigestVerify(loopargs[i].eddsa_ctx2[testnum],
  3023. loopargs[i].buf2, loopargs[i].sigsize,
  3024. loopargs[i].buf, 20);
  3025. if (st != 1)
  3026. break;
  3027. }
  3028. if (st != 1) {
  3029. BIO_printf(bio_err,
  3030. "EdDSA verify failure. No EdDSA verify will be done.\n");
  3031. ERR_print_errors(bio_err);
  3032. eddsa_doit[testnum] = 0;
  3033. } else {
  3034. pkey_print_message("verify", ed_curves[testnum].name,
  3035. ed_curves[testnum].bits, seconds.eddsa);
  3036. Time_F(START);
  3037. count = run_benchmark(async_jobs, EdDSA_verify_loop, loopargs);
  3038. d = Time_F(STOP);
  3039. BIO_printf(bio_err,
  3040. mr ? "+R11:%ld:%u:%s:%.2f\n"
  3041. : "%ld %u bits %s verify ops in %.2fs\n",
  3042. count, ed_curves[testnum].bits,
  3043. ed_curves[testnum].name, d);
  3044. eddsa_results[testnum][1] = (double)count / d;
  3045. }
  3046. if (op_count <= 1) {
  3047. /* if longer than 10s, don't do any more */
  3048. stop_it(eddsa_doit, testnum);
  3049. }
  3050. }
  3051. }
  3052. #endif /* OPENSSL_NO_ECX */
  3053. #ifndef OPENSSL_NO_SM2
  3054. for (testnum = 0; testnum < SM2_NUM; testnum++) {
  3055. int st = 1;
  3056. EVP_PKEY *sm2_pkey = NULL;
  3057. if (!sm2_doit[testnum])
  3058. continue; /* Ignore Curve */
  3059. /* Init signing and verification */
  3060. for (i = 0; i < loopargs_len; i++) {
  3061. EVP_PKEY_CTX *sm2_pctx = NULL;
  3062. EVP_PKEY_CTX *sm2_vfy_pctx = NULL;
  3063. EVP_PKEY_CTX *pctx = NULL;
  3064. st = 0;
  3065. loopargs[i].sm2_ctx[testnum] = EVP_MD_CTX_new();
  3066. loopargs[i].sm2_vfy_ctx[testnum] = EVP_MD_CTX_new();
  3067. if (loopargs[i].sm2_ctx[testnum] == NULL
  3068. || loopargs[i].sm2_vfy_ctx[testnum] == NULL)
  3069. break;
  3070. sm2_pkey = NULL;
  3071. st = !((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL)) == NULL
  3072. || EVP_PKEY_keygen_init(pctx) <= 0
  3073. || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
  3074. sm2_curves[testnum].nid) <= 0
  3075. || EVP_PKEY_keygen(pctx, &sm2_pkey) <= 0);
  3076. EVP_PKEY_CTX_free(pctx);
  3077. if (st == 0)
  3078. break;
  3079. st = 0; /* set back to zero */
  3080. /* attach it sooner to rely on main final cleanup */
  3081. loopargs[i].sm2_pkey[testnum] = sm2_pkey;
  3082. loopargs[i].sigsize = EVP_PKEY_get_size(sm2_pkey);
  3083. sm2_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
  3084. sm2_vfy_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
  3085. if (sm2_pctx == NULL || sm2_vfy_pctx == NULL) {
  3086. EVP_PKEY_CTX_free(sm2_vfy_pctx);
  3087. break;
  3088. }
  3089. /* attach them directly to respective ctx */
  3090. EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_ctx[testnum], sm2_pctx);
  3091. EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_vfy_ctx[testnum], sm2_vfy_pctx);
  3092. /*
  3093. * No need to allow user to set an explicit ID here, just use
  3094. * the one defined in the 'draft-yang-tls-tl13-sm-suites' I-D.
  3095. */
  3096. if (EVP_PKEY_CTX_set1_id(sm2_pctx, SM2_ID, SM2_ID_LEN) != 1
  3097. || EVP_PKEY_CTX_set1_id(sm2_vfy_pctx, SM2_ID, SM2_ID_LEN) != 1)
  3098. break;
  3099. if (!EVP_DigestSignInit(loopargs[i].sm2_ctx[testnum], NULL,
  3100. EVP_sm3(), NULL, sm2_pkey))
  3101. break;
  3102. if (!EVP_DigestVerifyInit(loopargs[i].sm2_vfy_ctx[testnum], NULL,
  3103. EVP_sm3(), NULL, sm2_pkey))
  3104. break;
  3105. st = 1; /* mark loop as succeeded */
  3106. }
  3107. if (st == 0) {
  3108. BIO_printf(bio_err, "SM2 init failure.\n");
  3109. ERR_print_errors(bio_err);
  3110. op_count = 1;
  3111. } else {
  3112. for (i = 0; i < loopargs_len; i++) {
  3113. /* Perform SM2 signature test */
  3114. st = EVP_DigestSign(loopargs[i].sm2_ctx[testnum],
  3115. loopargs[i].buf2, &loopargs[i].sigsize,
  3116. loopargs[i].buf, 20);
  3117. if (st == 0)
  3118. break;
  3119. }
  3120. if (st == 0) {
  3121. BIO_printf(bio_err,
  3122. "SM2 sign failure. No SM2 sign will be done.\n");
  3123. ERR_print_errors(bio_err);
  3124. op_count = 1;
  3125. } else {
  3126. pkey_print_message("sign", sm2_curves[testnum].name,
  3127. sm2_curves[testnum].bits, seconds.sm2);
  3128. Time_F(START);
  3129. count = run_benchmark(async_jobs, SM2_sign_loop, loopargs);
  3130. d = Time_F(STOP);
  3131. BIO_printf(bio_err,
  3132. mr ? "+R12:%ld:%u:%s:%.2f\n" :
  3133. "%ld %u bits %s sign ops in %.2fs \n",
  3134. count, sm2_curves[testnum].bits,
  3135. sm2_curves[testnum].name, d);
  3136. sm2_results[testnum][0] = (double)count / d;
  3137. op_count = count;
  3138. }
  3139. /* Perform SM2 verification test */
  3140. for (i = 0; i < loopargs_len; i++) {
  3141. st = EVP_DigestVerify(loopargs[i].sm2_vfy_ctx[testnum],
  3142. loopargs[i].buf2, loopargs[i].sigsize,
  3143. loopargs[i].buf, 20);
  3144. if (st != 1)
  3145. break;
  3146. }
  3147. if (st != 1) {
  3148. BIO_printf(bio_err,
  3149. "SM2 verify failure. No SM2 verify will be done.\n");
  3150. ERR_print_errors(bio_err);
  3151. sm2_doit[testnum] = 0;
  3152. } else {
  3153. pkey_print_message("verify", sm2_curves[testnum].name,
  3154. sm2_curves[testnum].bits, seconds.sm2);
  3155. Time_F(START);
  3156. count = run_benchmark(async_jobs, SM2_verify_loop, loopargs);
  3157. d = Time_F(STOP);
  3158. BIO_printf(bio_err,
  3159. mr ? "+R13:%ld:%u:%s:%.2f\n"
  3160. : "%ld %u bits %s verify ops in %.2fs\n",
  3161. count, sm2_curves[testnum].bits,
  3162. sm2_curves[testnum].name, d);
  3163. sm2_results[testnum][1] = (double)count / d;
  3164. }
  3165. if (op_count <= 1) {
  3166. /* if longer than 10s, don't do any more */
  3167. for (testnum++; testnum < SM2_NUM; testnum++)
  3168. sm2_doit[testnum] = 0;
  3169. }
  3170. }
  3171. }
  3172. #endif /* OPENSSL_NO_SM2 */
  3173. #ifndef OPENSSL_NO_DH
  3174. for (testnum = 0; testnum < FFDH_NUM; testnum++) {
  3175. int ffdh_checks = 1;
  3176. if (!ffdh_doit[testnum])
  3177. continue;
  3178. for (i = 0; i < loopargs_len; i++) {
  3179. EVP_PKEY *pkey_A = NULL;
  3180. EVP_PKEY *pkey_B = NULL;
  3181. EVP_PKEY_CTX *ffdh_ctx = NULL;
  3182. EVP_PKEY_CTX *test_ctx = NULL;
  3183. size_t secret_size;
  3184. size_t test_out;
  3185. /* Ensure that the error queue is empty */
  3186. if (ERR_peek_error()) {
  3187. BIO_printf(bio_err,
  3188. "WARNING: the error queue contains previous unhandled errors.\n");
  3189. ERR_print_errors(bio_err);
  3190. }
  3191. pkey_A = EVP_PKEY_new();
  3192. if (!pkey_A) {
  3193. BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
  3194. ERR_print_errors(bio_err);
  3195. op_count = 1;
  3196. ffdh_checks = 0;
  3197. break;
  3198. }
  3199. pkey_B = EVP_PKEY_new();
  3200. if (!pkey_B) {
  3201. BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
  3202. ERR_print_errors(bio_err);
  3203. op_count = 1;
  3204. ffdh_checks = 0;
  3205. break;
  3206. }
  3207. ffdh_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
  3208. if (!ffdh_ctx) {
  3209. BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
  3210. ERR_print_errors(bio_err);
  3211. op_count = 1;
  3212. ffdh_checks = 0;
  3213. break;
  3214. }
  3215. if (EVP_PKEY_keygen_init(ffdh_ctx) <= 0) {
  3216. BIO_printf(bio_err, "Error while initialising EVP_PKEY_CTX.\n");
  3217. ERR_print_errors(bio_err);
  3218. op_count = 1;
  3219. ffdh_checks = 0;
  3220. break;
  3221. }
  3222. if (EVP_PKEY_CTX_set_dh_nid(ffdh_ctx, ffdh_params[testnum].nid) <= 0) {
  3223. BIO_printf(bio_err, "Error setting DH key size for keygen.\n");
  3224. ERR_print_errors(bio_err);
  3225. op_count = 1;
  3226. ffdh_checks = 0;
  3227. break;
  3228. }
  3229. if (EVP_PKEY_keygen(ffdh_ctx, &pkey_A) <= 0 ||
  3230. EVP_PKEY_keygen(ffdh_ctx, &pkey_B) <= 0) {
  3231. BIO_printf(bio_err, "FFDH key generation failure.\n");
  3232. ERR_print_errors(bio_err);
  3233. op_count = 1;
  3234. ffdh_checks = 0;
  3235. break;
  3236. }
  3237. EVP_PKEY_CTX_free(ffdh_ctx);
  3238. /*
  3239. * check if the derivation works correctly both ways so that
  3240. * we know if future derive calls will fail, and we can skip
  3241. * error checking in benchmarked code
  3242. */
  3243. ffdh_ctx = EVP_PKEY_CTX_new(pkey_A, NULL);
  3244. if (ffdh_ctx == NULL) {
  3245. BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
  3246. ERR_print_errors(bio_err);
  3247. op_count = 1;
  3248. ffdh_checks = 0;
  3249. break;
  3250. }
  3251. if (EVP_PKEY_derive_init(ffdh_ctx) <= 0) {
  3252. BIO_printf(bio_err, "FFDH derivation context init failure.\n");
  3253. ERR_print_errors(bio_err);
  3254. op_count = 1;
  3255. ffdh_checks = 0;
  3256. break;
  3257. }
  3258. if (EVP_PKEY_derive_set_peer(ffdh_ctx, pkey_B) <= 0) {
  3259. BIO_printf(bio_err, "Assigning peer key for derivation failed.\n");
  3260. ERR_print_errors(bio_err);
  3261. op_count = 1;
  3262. ffdh_checks = 0;
  3263. break;
  3264. }
  3265. if (EVP_PKEY_derive(ffdh_ctx, NULL, &secret_size) <= 0) {
  3266. BIO_printf(bio_err, "Checking size of shared secret failed.\n");
  3267. ERR_print_errors(bio_err);
  3268. op_count = 1;
  3269. ffdh_checks = 0;
  3270. break;
  3271. }
  3272. if (secret_size > MAX_FFDH_SIZE) {
  3273. BIO_printf(bio_err, "Assertion failure: shared secret too large.\n");
  3274. op_count = 1;
  3275. ffdh_checks = 0;
  3276. break;
  3277. }
  3278. if (EVP_PKEY_derive(ffdh_ctx,
  3279. loopargs[i].secret_ff_a,
  3280. &secret_size) <= 0) {
  3281. BIO_printf(bio_err, "Shared secret derive failure.\n");
  3282. ERR_print_errors(bio_err);
  3283. op_count = 1;
  3284. ffdh_checks = 0;
  3285. break;
  3286. }
  3287. /* Now check from side B */
  3288. test_ctx = EVP_PKEY_CTX_new(pkey_B, NULL);
  3289. if (!test_ctx) {
  3290. BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
  3291. ERR_print_errors(bio_err);
  3292. op_count = 1;
  3293. ffdh_checks = 0;
  3294. break;
  3295. }
  3296. if (EVP_PKEY_derive_init(test_ctx) <= 0 ||
  3297. EVP_PKEY_derive_set_peer(test_ctx, pkey_A) <= 0 ||
  3298. EVP_PKEY_derive(test_ctx, NULL, &test_out) <= 0 ||
  3299. EVP_PKEY_derive(test_ctx, loopargs[i].secret_ff_b, &test_out) <= 0 ||
  3300. test_out != secret_size) {
  3301. BIO_printf(bio_err, "FFDH computation failure.\n");
  3302. op_count = 1;
  3303. ffdh_checks = 0;
  3304. break;
  3305. }
  3306. /* compare the computed secrets */
  3307. if (CRYPTO_memcmp(loopargs[i].secret_ff_a,
  3308. loopargs[i].secret_ff_b, secret_size)) {
  3309. BIO_printf(bio_err, "FFDH computations don't match.\n");
  3310. ERR_print_errors(bio_err);
  3311. op_count = 1;
  3312. ffdh_checks = 0;
  3313. break;
  3314. }
  3315. loopargs[i].ffdh_ctx[testnum] = ffdh_ctx;
  3316. EVP_PKEY_free(pkey_A);
  3317. pkey_A = NULL;
  3318. EVP_PKEY_free(pkey_B);
  3319. pkey_B = NULL;
  3320. EVP_PKEY_CTX_free(test_ctx);
  3321. test_ctx = NULL;
  3322. }
  3323. if (ffdh_checks != 0) {
  3324. pkey_print_message("", "ffdh",
  3325. ffdh_params[testnum].bits, seconds.ffdh);
  3326. Time_F(START);
  3327. count =
  3328. run_benchmark(async_jobs, FFDH_derive_key_loop, loopargs);
  3329. d = Time_F(STOP);
  3330. BIO_printf(bio_err,
  3331. mr ? "+R14:%ld:%d:%.2f\n" :
  3332. "%ld %u-bits FFDH ops in %.2fs\n", count,
  3333. ffdh_params[testnum].bits, d);
  3334. ffdh_results[testnum][0] = (double)count / d;
  3335. op_count = count;
  3336. }
  3337. if (op_count <= 1) {
  3338. /* if longer than 10s, don't do any more */
  3339. stop_it(ffdh_doit, testnum);
  3340. }
  3341. }
  3342. #endif /* OPENSSL_NO_DH */
  3343. for (testnum = 0; testnum < kems_algs_len; testnum++) {
  3344. int kem_checks = 1;
  3345. const char *kem_name = kems_algname[testnum];
  3346. if (!kems_doit[testnum] || !do_kems)
  3347. continue;
  3348. for (i = 0; i < loopargs_len; i++) {
  3349. EVP_PKEY *pkey = NULL;
  3350. EVP_PKEY_CTX *kem_gen_ctx = NULL;
  3351. EVP_PKEY_CTX *kem_encaps_ctx = NULL;
  3352. EVP_PKEY_CTX *kem_decaps_ctx = NULL;
  3353. size_t send_secret_len, out_len;
  3354. size_t rcv_secret_len;
  3355. unsigned char *out = NULL, *send_secret = NULL, *rcv_secret;
  3356. unsigned int bits;
  3357. char *name;
  3358. char sfx[MAX_ALGNAME_SUFFIX];
  3359. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  3360. int use_params = 0;
  3361. enum kem_type_t { KEM_RSA = 1, KEM_EC, KEM_X25519, KEM_X448 } kem_type;
  3362. /* no string after rsa<bitcnt> permitted: */
  3363. if (strlen(kem_name) < MAX_ALGNAME_SUFFIX + 4 /* rsa+digit */
  3364. && sscanf(kem_name, "rsa%u%s", &bits, sfx) == 1)
  3365. kem_type = KEM_RSA;
  3366. else if (strncmp(kem_name, "EC", 2) == 0)
  3367. kem_type = KEM_EC;
  3368. else if (strcmp(kem_name, "X25519") == 0)
  3369. kem_type = KEM_X25519;
  3370. else if (strcmp(kem_name, "X448") == 0)
  3371. kem_type = KEM_X448;
  3372. else kem_type = 0;
  3373. if (ERR_peek_error()) {
  3374. BIO_printf(bio_err,
  3375. "WARNING: the error queue contains previous unhandled errors.\n");
  3376. ERR_print_errors(bio_err);
  3377. }
  3378. if (kem_type == KEM_RSA) {
  3379. params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_RSA_BITS,
  3380. &bits);
  3381. use_params = 1;
  3382. } else if (kem_type == KEM_EC) {
  3383. name = (char *)(kem_name + 2);
  3384. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  3385. name, 0);
  3386. use_params = 1;
  3387. }
  3388. kem_gen_ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(),
  3389. (kem_type == KEM_RSA) ? "RSA":
  3390. (kem_type == KEM_EC) ? "EC":
  3391. kem_name,
  3392. app_get0_propq());
  3393. if ((!kem_gen_ctx || EVP_PKEY_keygen_init(kem_gen_ctx) <= 0)
  3394. || (use_params
  3395. && EVP_PKEY_CTX_set_params(kem_gen_ctx, params) <= 0)) {
  3396. BIO_printf(bio_err, "Error initializing keygen ctx for %s.\n",
  3397. kem_name);
  3398. goto kem_err_break;
  3399. }
  3400. if (EVP_PKEY_keygen(kem_gen_ctx, &pkey) <= 0) {
  3401. BIO_printf(bio_err, "Error while generating KEM EVP_PKEY.\n");
  3402. goto kem_err_break;
  3403. }
  3404. /* Now prepare encaps data structs */
  3405. kem_encaps_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
  3406. pkey,
  3407. app_get0_propq());
  3408. if (kem_encaps_ctx == NULL
  3409. || EVP_PKEY_encapsulate_init(kem_encaps_ctx, NULL) <= 0
  3410. || (kem_type == KEM_RSA
  3411. && EVP_PKEY_CTX_set_kem_op(kem_encaps_ctx, "RSASVE") <= 0)
  3412. || ((kem_type == KEM_EC
  3413. || kem_type == KEM_X25519
  3414. || kem_type == KEM_X448)
  3415. && EVP_PKEY_CTX_set_kem_op(kem_encaps_ctx, "DHKEM") <= 0)
  3416. || EVP_PKEY_encapsulate(kem_encaps_ctx, NULL, &out_len,
  3417. NULL, &send_secret_len) <= 0) {
  3418. BIO_printf(bio_err,
  3419. "Error while initializing encaps data structs for %s.\n",
  3420. kem_name);
  3421. goto kem_err_break;
  3422. }
  3423. out = app_malloc(out_len, "encaps result");
  3424. send_secret = app_malloc(send_secret_len, "encaps secret");
  3425. if (out == NULL || send_secret == NULL) {
  3426. BIO_printf(bio_err, "MemAlloc error in encaps for %s.\n", kem_name);
  3427. goto kem_err_break;
  3428. }
  3429. if (EVP_PKEY_encapsulate(kem_encaps_ctx, out, &out_len,
  3430. send_secret, &send_secret_len) <= 0) {
  3431. BIO_printf(bio_err, "Encaps error for %s.\n", kem_name);
  3432. goto kem_err_break;
  3433. }
  3434. /* Now prepare decaps data structs */
  3435. kem_decaps_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
  3436. pkey,
  3437. app_get0_propq());
  3438. if (kem_decaps_ctx == NULL
  3439. || EVP_PKEY_decapsulate_init(kem_decaps_ctx, NULL) <= 0
  3440. || (kem_type == KEM_RSA
  3441. && EVP_PKEY_CTX_set_kem_op(kem_decaps_ctx, "RSASVE") <= 0)
  3442. || ((kem_type == KEM_EC
  3443. || kem_type == KEM_X25519
  3444. || kem_type == KEM_X448)
  3445. && EVP_PKEY_CTX_set_kem_op(kem_decaps_ctx, "DHKEM") <= 0)
  3446. || EVP_PKEY_decapsulate(kem_decaps_ctx, NULL, &rcv_secret_len,
  3447. out, out_len) <= 0) {
  3448. BIO_printf(bio_err,
  3449. "Error while initializing decaps data structs for %s.\n",
  3450. kem_name);
  3451. goto kem_err_break;
  3452. }
  3453. rcv_secret = app_malloc(rcv_secret_len, "KEM decaps secret");
  3454. if (rcv_secret == NULL) {
  3455. BIO_printf(bio_err, "MemAlloc failure in decaps for %s.\n",
  3456. kem_name);
  3457. goto kem_err_break;
  3458. }
  3459. if (EVP_PKEY_decapsulate(kem_decaps_ctx, rcv_secret,
  3460. &rcv_secret_len, out, out_len) <= 0
  3461. || rcv_secret_len != send_secret_len
  3462. || memcmp(send_secret, rcv_secret, send_secret_len)) {
  3463. BIO_printf(bio_err, "Decaps error for %s.\n", kem_name);
  3464. goto kem_err_break;
  3465. }
  3466. loopargs[i].kem_gen_ctx[testnum] = kem_gen_ctx;
  3467. loopargs[i].kem_encaps_ctx[testnum] = kem_encaps_ctx;
  3468. loopargs[i].kem_decaps_ctx[testnum] = kem_decaps_ctx;
  3469. loopargs[i].kem_out_len[testnum] = out_len;
  3470. loopargs[i].kem_secret_len[testnum] = send_secret_len;
  3471. loopargs[i].kem_out[testnum] = out;
  3472. loopargs[i].kem_send_secret[testnum] = send_secret;
  3473. loopargs[i].kem_rcv_secret[testnum] = rcv_secret;
  3474. EVP_PKEY_free(pkey);
  3475. pkey = NULL;
  3476. continue;
  3477. kem_err_break:
  3478. ERR_print_errors(bio_err);
  3479. EVP_PKEY_free(pkey);
  3480. op_count = 1;
  3481. kem_checks = 0;
  3482. break;
  3483. }
  3484. if (kem_checks != 0) {
  3485. kskey_print_message(kem_name, "keygen", seconds.kem);
  3486. Time_F(START);
  3487. count =
  3488. run_benchmark(async_jobs, KEM_keygen_loop, loopargs);
  3489. d = Time_F(STOP);
  3490. BIO_printf(bio_err,
  3491. mr ? "+R15:%ld:%s:%.2f\n" :
  3492. "%ld %s KEM keygen ops in %.2fs\n", count,
  3493. kem_name, d);
  3494. kems_results[testnum][0] = (double)count / d;
  3495. op_count = count;
  3496. kskey_print_message(kem_name, "encaps", seconds.kem);
  3497. Time_F(START);
  3498. count =
  3499. run_benchmark(async_jobs, KEM_encaps_loop, loopargs);
  3500. d = Time_F(STOP);
  3501. BIO_printf(bio_err,
  3502. mr ? "+R16:%ld:%s:%.2f\n" :
  3503. "%ld %s KEM encaps ops in %.2fs\n", count,
  3504. kem_name, d);
  3505. kems_results[testnum][1] = (double)count / d;
  3506. op_count = count;
  3507. kskey_print_message(kem_name, "decaps", seconds.kem);
  3508. Time_F(START);
  3509. count =
  3510. run_benchmark(async_jobs, KEM_decaps_loop, loopargs);
  3511. d = Time_F(STOP);
  3512. BIO_printf(bio_err,
  3513. mr ? "+R17:%ld:%s:%.2f\n" :
  3514. "%ld %s KEM decaps ops in %.2fs\n", count,
  3515. kem_name, d);
  3516. kems_results[testnum][2] = (double)count / d;
  3517. op_count = count;
  3518. }
  3519. if (op_count <= 1) {
  3520. /* if longer than 10s, don't do any more */
  3521. stop_it(kems_doit, testnum);
  3522. }
  3523. }
  3524. for (testnum = 0; testnum < sigs_algs_len; testnum++) {
  3525. int sig_checks = 1;
  3526. const char *sig_name = sigs_algname[testnum];
  3527. if (!sigs_doit[testnum] || !do_sigs)
  3528. continue;
  3529. for (i = 0; i < loopargs_len; i++) {
  3530. EVP_PKEY *pkey = NULL;
  3531. EVP_PKEY_CTX *ctx_params = NULL;
  3532. EVP_PKEY* pkey_params = NULL;
  3533. EVP_PKEY_CTX *sig_gen_ctx = NULL;
  3534. EVP_PKEY_CTX *sig_sign_ctx = NULL;
  3535. EVP_PKEY_CTX *sig_verify_ctx = NULL;
  3536. unsigned char md[SHA256_DIGEST_LENGTH];
  3537. unsigned char *sig;
  3538. char sfx[MAX_ALGNAME_SUFFIX];
  3539. size_t md_len = SHA256_DIGEST_LENGTH;
  3540. size_t max_sig_len, sig_len;
  3541. unsigned int bits;
  3542. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  3543. int use_params = 0;
  3544. /* only sign little data to avoid measuring digest performance */
  3545. memset(md, 0, SHA256_DIGEST_LENGTH);
  3546. if (ERR_peek_error()) {
  3547. BIO_printf(bio_err,
  3548. "WARNING: the error queue contains previous unhandled errors.\n");
  3549. ERR_print_errors(bio_err);
  3550. }
  3551. /* no string after rsa<bitcnt> permitted: */
  3552. if (strlen(sig_name) < MAX_ALGNAME_SUFFIX + 4 /* rsa+digit */
  3553. && sscanf(sig_name, "rsa%u%s", &bits, sfx) == 1) {
  3554. params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_RSA_BITS,
  3555. &bits);
  3556. use_params = 1;
  3557. }
  3558. if (strncmp(sig_name, "dsa", 3) == 0) {
  3559. ctx_params = EVP_PKEY_CTX_new_id(EVP_PKEY_DSA, NULL);
  3560. if (ctx_params == NULL
  3561. || EVP_PKEY_paramgen_init(ctx_params) <= 0
  3562. || EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx_params,
  3563. atoi(sig_name + 3)) <= 0
  3564. || EVP_PKEY_paramgen(ctx_params, &pkey_params) <= 0
  3565. || (sig_gen_ctx = EVP_PKEY_CTX_new(pkey_params, NULL)) == NULL
  3566. || EVP_PKEY_keygen_init(sig_gen_ctx) <= 0) {
  3567. BIO_printf(bio_err,
  3568. "Error initializing classic keygen ctx for %s.\n",
  3569. sig_name);
  3570. goto sig_err_break;
  3571. }
  3572. }
  3573. if (sig_gen_ctx == NULL)
  3574. sig_gen_ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(),
  3575. use_params == 1 ? "RSA" : sig_name,
  3576. app_get0_propq());
  3577. if (!sig_gen_ctx || EVP_PKEY_keygen_init(sig_gen_ctx) <= 0
  3578. || (use_params &&
  3579. EVP_PKEY_CTX_set_params(sig_gen_ctx, params) <= 0)) {
  3580. BIO_printf(bio_err, "Error initializing keygen ctx for %s.\n",
  3581. sig_name);
  3582. goto sig_err_break;
  3583. }
  3584. if (EVP_PKEY_keygen(sig_gen_ctx, &pkey) <= 0) {
  3585. BIO_printf(bio_err,
  3586. "Error while generating signature EVP_PKEY for %s.\n",
  3587. sig_name);
  3588. goto sig_err_break;
  3589. }
  3590. /* Now prepare signature data structs */
  3591. sig_sign_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
  3592. pkey,
  3593. app_get0_propq());
  3594. if (sig_sign_ctx == NULL
  3595. || EVP_PKEY_sign_init(sig_sign_ctx) <= 0
  3596. || (use_params == 1
  3597. && (EVP_PKEY_CTX_set_rsa_padding(sig_sign_ctx,
  3598. RSA_PKCS1_PADDING) <= 0))
  3599. || EVP_PKEY_sign(sig_sign_ctx, NULL, &max_sig_len,
  3600. md, md_len) <= 0) {
  3601. BIO_printf(bio_err,
  3602. "Error while initializing signing data structs for %s.\n",
  3603. sig_name);
  3604. goto sig_err_break;
  3605. }
  3606. sig = app_malloc(sig_len = max_sig_len, "signature buffer");
  3607. if (sig == NULL) {
  3608. BIO_printf(bio_err, "MemAlloc error in sign for %s.\n", sig_name);
  3609. goto sig_err_break;
  3610. }
  3611. if (EVP_PKEY_sign(sig_sign_ctx, sig, &sig_len, md, md_len) <= 0) {
  3612. BIO_printf(bio_err, "Signing error for %s.\n", sig_name);
  3613. goto sig_err_break;
  3614. }
  3615. /* Now prepare verify data structs */
  3616. memset(md, 0, SHA256_DIGEST_LENGTH);
  3617. sig_verify_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
  3618. pkey,
  3619. app_get0_propq());
  3620. if (sig_verify_ctx == NULL
  3621. || EVP_PKEY_verify_init(sig_verify_ctx) <= 0
  3622. || (use_params == 1
  3623. && (EVP_PKEY_CTX_set_rsa_padding(sig_verify_ctx,
  3624. RSA_PKCS1_PADDING) <= 0))) {
  3625. BIO_printf(bio_err,
  3626. "Error while initializing verify data structs for %s.\n",
  3627. sig_name);
  3628. goto sig_err_break;
  3629. }
  3630. if (EVP_PKEY_verify(sig_verify_ctx, sig, sig_len, md, md_len) <= 0) {
  3631. BIO_printf(bio_err, "Verify error for %s.\n", sig_name);
  3632. goto sig_err_break;
  3633. }
  3634. if (EVP_PKEY_verify(sig_verify_ctx, sig, sig_len, md, md_len) <= 0) {
  3635. BIO_printf(bio_err, "Verify 2 error for %s.\n", sig_name);
  3636. goto sig_err_break;
  3637. }
  3638. loopargs[i].sig_gen_ctx[testnum] = sig_gen_ctx;
  3639. loopargs[i].sig_sign_ctx[testnum] = sig_sign_ctx;
  3640. loopargs[i].sig_verify_ctx[testnum] = sig_verify_ctx;
  3641. loopargs[i].sig_max_sig_len[testnum] = max_sig_len;
  3642. loopargs[i].sig_act_sig_len[testnum] = sig_len;
  3643. loopargs[i].sig_sig[testnum] = sig;
  3644. EVP_PKEY_free(pkey);
  3645. pkey = NULL;
  3646. continue;
  3647. sig_err_break:
  3648. ERR_print_errors(bio_err);
  3649. EVP_PKEY_free(pkey);
  3650. op_count = 1;
  3651. sig_checks = 0;
  3652. break;
  3653. }
  3654. if (sig_checks != 0) {
  3655. kskey_print_message(sig_name, "keygen", seconds.sig);
  3656. Time_F(START);
  3657. count = run_benchmark(async_jobs, SIG_keygen_loop, loopargs);
  3658. d = Time_F(STOP);
  3659. BIO_printf(bio_err,
  3660. mr ? "+R18:%ld:%s:%.2f\n" :
  3661. "%ld %s signature keygen ops in %.2fs\n", count,
  3662. sig_name, d);
  3663. sigs_results[testnum][0] = (double)count / d;
  3664. op_count = count;
  3665. kskey_print_message(sig_name, "signs", seconds.sig);
  3666. Time_F(START);
  3667. count =
  3668. run_benchmark(async_jobs, SIG_sign_loop, loopargs);
  3669. d = Time_F(STOP);
  3670. BIO_printf(bio_err,
  3671. mr ? "+R19:%ld:%s:%.2f\n" :
  3672. "%ld %s signature sign ops in %.2fs\n", count,
  3673. sig_name, d);
  3674. sigs_results[testnum][1] = (double)count / d;
  3675. op_count = count;
  3676. kskey_print_message(sig_name, "verify", seconds.sig);
  3677. Time_F(START);
  3678. count =
  3679. run_benchmark(async_jobs, SIG_verify_loop, loopargs);
  3680. d = Time_F(STOP);
  3681. BIO_printf(bio_err,
  3682. mr ? "+R20:%ld:%s:%.2f\n" :
  3683. "%ld %s signature verify ops in %.2fs\n", count,
  3684. sig_name, d);
  3685. sigs_results[testnum][2] = (double)count / d;
  3686. op_count = count;
  3687. }
  3688. if (op_count <= 1)
  3689. stop_it(sigs_doit, testnum);
  3690. }
  3691. #ifndef NO_FORK
  3692. show_res:
  3693. #endif
  3694. if (!mr) {
  3695. printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING));
  3696. printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
  3697. printf("options: %s\n", BN_options());
  3698. printf("%s\n", OpenSSL_version(OPENSSL_CFLAGS));
  3699. printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO));
  3700. }
  3701. if (pr_header) {
  3702. if (mr) {
  3703. printf("+H");
  3704. } else {
  3705. printf("The 'numbers' are in 1000s of bytes per second processed.\n");
  3706. printf("type ");
  3707. }
  3708. for (testnum = 0; testnum < size_num; testnum++)
  3709. printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
  3710. printf("\n");
  3711. }
  3712. for (k = 0; k < ALGOR_NUM; k++) {
  3713. const char *alg_name = names[k];
  3714. if (!doit[k])
  3715. continue;
  3716. if (k == D_EVP) {
  3717. if (evp_cipher == NULL)
  3718. alg_name = evp_md_name;
  3719. else if ((alg_name = EVP_CIPHER_get0_name(evp_cipher)) == NULL)
  3720. app_bail_out("failed to get name of cipher '%s'\n", evp_cipher);
  3721. }
  3722. if (mr)
  3723. printf("+F:%u:%s", k, alg_name);
  3724. else
  3725. printf("%-13s", alg_name);
  3726. for (testnum = 0; testnum < size_num; testnum++) {
  3727. if (results[k][testnum] > 10000 && !mr)
  3728. printf(" %11.2fk", results[k][testnum] / 1e3);
  3729. else
  3730. printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
  3731. }
  3732. printf("\n");
  3733. }
  3734. testnum = 1;
  3735. for (k = 0; k < RSA_NUM; k++) {
  3736. if (!rsa_doit[k])
  3737. continue;
  3738. if (testnum && !mr) {
  3739. printf("%19ssign verify encrypt decrypt sign/s verify/s encr./s decr./s\n", " ");
  3740. testnum = 0;
  3741. }
  3742. if (mr)
  3743. printf("+F2:%u:%u:%f:%f:%f:%f\n",
  3744. k, rsa_keys[k].bits, rsa_results[k][0], rsa_results[k][1],
  3745. rsa_results[k][2], rsa_results[k][3]);
  3746. else
  3747. printf("rsa %5u bits %8.6fs %8.6fs %8.6fs %8.6fs %8.1f %8.1f %8.1f %8.1f\n",
  3748. rsa_keys[k].bits, 1.0 / rsa_results[k][0],
  3749. 1.0 / rsa_results[k][1], 1.0 / rsa_results[k][2],
  3750. 1.0 / rsa_results[k][3],
  3751. rsa_results[k][0], rsa_results[k][1],
  3752. rsa_results[k][2], rsa_results[k][3]);
  3753. }
  3754. testnum = 1;
  3755. for (k = 0; k < DSA_NUM; k++) {
  3756. if (!dsa_doit[k])
  3757. continue;
  3758. if (testnum && !mr) {
  3759. printf("%18ssign verify sign/s verify/s\n", " ");
  3760. testnum = 0;
  3761. }
  3762. if (mr)
  3763. printf("+F3:%u:%u:%f:%f\n",
  3764. k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
  3765. else
  3766. printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
  3767. dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
  3768. dsa_results[k][0], dsa_results[k][1]);
  3769. }
  3770. testnum = 1;
  3771. for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
  3772. if (!ecdsa_doit[k])
  3773. continue;
  3774. if (testnum && !mr) {
  3775. printf("%30ssign verify sign/s verify/s\n", " ");
  3776. testnum = 0;
  3777. }
  3778. if (mr)
  3779. printf("+F4:%u:%u:%f:%f\n",
  3780. k, ec_curves[k].bits,
  3781. ecdsa_results[k][0], ecdsa_results[k][1]);
  3782. else
  3783. printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
  3784. ec_curves[k].bits, ec_curves[k].name,
  3785. 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
  3786. ecdsa_results[k][0], ecdsa_results[k][1]);
  3787. }
  3788. testnum = 1;
  3789. for (k = 0; k < EC_NUM; k++) {
  3790. if (!ecdh_doit[k])
  3791. continue;
  3792. if (testnum && !mr) {
  3793. printf("%30sop op/s\n", " ");
  3794. testnum = 0;
  3795. }
  3796. if (mr)
  3797. printf("+F5:%u:%u:%f:%f\n",
  3798. k, ec_curves[k].bits,
  3799. ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
  3800. else
  3801. printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
  3802. ec_curves[k].bits, ec_curves[k].name,
  3803. 1.0 / ecdh_results[k][0], ecdh_results[k][0]);
  3804. }
  3805. #ifndef OPENSSL_NO_ECX
  3806. testnum = 1;
  3807. for (k = 0; k < OSSL_NELEM(eddsa_doit); k++) {
  3808. if (!eddsa_doit[k])
  3809. continue;
  3810. if (testnum && !mr) {
  3811. printf("%30ssign verify sign/s verify/s\n", " ");
  3812. testnum = 0;
  3813. }
  3814. if (mr)
  3815. printf("+F6:%u:%u:%s:%f:%f\n",
  3816. k, ed_curves[k].bits, ed_curves[k].name,
  3817. eddsa_results[k][0], eddsa_results[k][1]);
  3818. else
  3819. printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
  3820. ed_curves[k].bits, ed_curves[k].name,
  3821. 1.0 / eddsa_results[k][0], 1.0 / eddsa_results[k][1],
  3822. eddsa_results[k][0], eddsa_results[k][1]);
  3823. }
  3824. #endif /* OPENSSL_NO_ECX */
  3825. #ifndef OPENSSL_NO_SM2
  3826. testnum = 1;
  3827. for (k = 0; k < OSSL_NELEM(sm2_doit); k++) {
  3828. if (!sm2_doit[k])
  3829. continue;
  3830. if (testnum && !mr) {
  3831. printf("%30ssign verify sign/s verify/s\n", " ");
  3832. testnum = 0;
  3833. }
  3834. if (mr)
  3835. printf("+F7:%u:%u:%s:%f:%f\n",
  3836. k, sm2_curves[k].bits, sm2_curves[k].name,
  3837. sm2_results[k][0], sm2_results[k][1]);
  3838. else
  3839. printf("%4u bits SM2 (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
  3840. sm2_curves[k].bits, sm2_curves[k].name,
  3841. 1.0 / sm2_results[k][0], 1.0 / sm2_results[k][1],
  3842. sm2_results[k][0], sm2_results[k][1]);
  3843. }
  3844. #endif
  3845. #ifndef OPENSSL_NO_DH
  3846. testnum = 1;
  3847. for (k = 0; k < FFDH_NUM; k++) {
  3848. if (!ffdh_doit[k])
  3849. continue;
  3850. if (testnum && !mr) {
  3851. printf("%23sop op/s\n", " ");
  3852. testnum = 0;
  3853. }
  3854. if (mr)
  3855. printf("+F8:%u:%u:%f:%f\n",
  3856. k, ffdh_params[k].bits,
  3857. ffdh_results[k][0], 1.0 / ffdh_results[k][0]);
  3858. else
  3859. printf("%4u bits ffdh %8.4fs %8.1f\n",
  3860. ffdh_params[k].bits,
  3861. 1.0 / ffdh_results[k][0], ffdh_results[k][0]);
  3862. }
  3863. #endif /* OPENSSL_NO_DH */
  3864. testnum = 1;
  3865. for (k = 0; k < kems_algs_len; k++) {
  3866. const char *kem_name = kems_algname[k];
  3867. if (!kems_doit[k] || !do_kems)
  3868. continue;
  3869. if (testnum && !mr) {
  3870. printf("%31skeygen encaps decaps keygens/s encaps/s decaps/s\n", " ");
  3871. testnum = 0;
  3872. }
  3873. if (mr)
  3874. printf("+F9:%u:%f:%f:%f\n",
  3875. k, kems_results[k][0], kems_results[k][1],
  3876. kems_results[k][2]);
  3877. else
  3878. printf("%27s %8.6fs %8.6fs %8.6fs %9.1f %9.1f %9.1f\n", kem_name,
  3879. 1.0 / kems_results[k][0],
  3880. 1.0 / kems_results[k][1], 1.0 / kems_results[k][2],
  3881. kems_results[k][0], kems_results[k][1], kems_results[k][2]);
  3882. }
  3883. ret = 0;
  3884. testnum = 1;
  3885. for (k = 0; k < sigs_algs_len; k++) {
  3886. const char *sig_name = sigs_algname[k];
  3887. if (!sigs_doit[k] || !do_sigs)
  3888. continue;
  3889. if (testnum && !mr) {
  3890. printf("%31skeygen signs verify keygens/s sign/s verify/s\n", " ");
  3891. testnum = 0;
  3892. }
  3893. if (mr)
  3894. printf("+F10:%u:%f:%f:%f\n",
  3895. k, sigs_results[k][0], sigs_results[k][1],
  3896. sigs_results[k][2]);
  3897. else
  3898. printf("%27s %8.6fs %8.6fs %8.6fs %9.1f %9.1f %9.1f\n", sig_name,
  3899. 1.0 / sigs_results[k][0], 1.0 / sigs_results[k][1],
  3900. 1.0 / sigs_results[k][2], sigs_results[k][0],
  3901. sigs_results[k][1], sigs_results[k][2]);
  3902. }
  3903. ret = 0;
  3904. end:
  3905. ERR_print_errors(bio_err);
  3906. for (i = 0; i < loopargs_len; i++) {
  3907. OPENSSL_free(loopargs[i].buf_malloc);
  3908. OPENSSL_free(loopargs[i].buf2_malloc);
  3909. BN_free(bn);
  3910. EVP_PKEY_CTX_free(genctx);
  3911. for (k = 0; k < RSA_NUM; k++) {
  3912. EVP_PKEY_CTX_free(loopargs[i].rsa_sign_ctx[k]);
  3913. EVP_PKEY_CTX_free(loopargs[i].rsa_verify_ctx[k]);
  3914. EVP_PKEY_CTX_free(loopargs[i].rsa_encrypt_ctx[k]);
  3915. EVP_PKEY_CTX_free(loopargs[i].rsa_decrypt_ctx[k]);
  3916. }
  3917. #ifndef OPENSSL_NO_DH
  3918. OPENSSL_free(loopargs[i].secret_ff_a);
  3919. OPENSSL_free(loopargs[i].secret_ff_b);
  3920. for (k = 0; k < FFDH_NUM; k++)
  3921. EVP_PKEY_CTX_free(loopargs[i].ffdh_ctx[k]);
  3922. #endif
  3923. for (k = 0; k < DSA_NUM; k++) {
  3924. EVP_PKEY_CTX_free(loopargs[i].dsa_sign_ctx[k]);
  3925. EVP_PKEY_CTX_free(loopargs[i].dsa_verify_ctx[k]);
  3926. }
  3927. for (k = 0; k < ECDSA_NUM; k++) {
  3928. EVP_PKEY_CTX_free(loopargs[i].ecdsa_sign_ctx[k]);
  3929. EVP_PKEY_CTX_free(loopargs[i].ecdsa_verify_ctx[k]);
  3930. }
  3931. for (k = 0; k < EC_NUM; k++)
  3932. EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
  3933. #ifndef OPENSSL_NO_ECX
  3934. for (k = 0; k < EdDSA_NUM; k++) {
  3935. EVP_MD_CTX_free(loopargs[i].eddsa_ctx[k]);
  3936. EVP_MD_CTX_free(loopargs[i].eddsa_ctx2[k]);
  3937. }
  3938. #endif /* OPENSSL_NO_ECX */
  3939. #ifndef OPENSSL_NO_SM2
  3940. for (k = 0; k < SM2_NUM; k++) {
  3941. EVP_PKEY_CTX *pctx = NULL;
  3942. /* free signing ctx */
  3943. if (loopargs[i].sm2_ctx[k] != NULL
  3944. && (pctx = EVP_MD_CTX_get_pkey_ctx(loopargs[i].sm2_ctx[k])) != NULL)
  3945. EVP_PKEY_CTX_free(pctx);
  3946. EVP_MD_CTX_free(loopargs[i].sm2_ctx[k]);
  3947. /* free verification ctx */
  3948. if (loopargs[i].sm2_vfy_ctx[k] != NULL
  3949. && (pctx = EVP_MD_CTX_get_pkey_ctx(loopargs[i].sm2_vfy_ctx[k])) != NULL)
  3950. EVP_PKEY_CTX_free(pctx);
  3951. EVP_MD_CTX_free(loopargs[i].sm2_vfy_ctx[k]);
  3952. /* free pkey */
  3953. EVP_PKEY_free(loopargs[i].sm2_pkey[k]);
  3954. }
  3955. #endif
  3956. for (k = 0; k < kems_algs_len; k++) {
  3957. EVP_PKEY_CTX_free(loopargs[i].kem_gen_ctx[k]);
  3958. EVP_PKEY_CTX_free(loopargs[i].kem_encaps_ctx[k]);
  3959. EVP_PKEY_CTX_free(loopargs[i].kem_decaps_ctx[k]);
  3960. OPENSSL_free(loopargs[i].kem_out[k]);
  3961. OPENSSL_free(loopargs[i].kem_send_secret[k]);
  3962. OPENSSL_free(loopargs[i].kem_rcv_secret[k]);
  3963. }
  3964. for (k = 0; k < sigs_algs_len; k++) {
  3965. EVP_PKEY_CTX_free(loopargs[i].sig_gen_ctx[k]);
  3966. EVP_PKEY_CTX_free(loopargs[i].sig_sign_ctx[k]);
  3967. EVP_PKEY_CTX_free(loopargs[i].sig_verify_ctx[k]);
  3968. OPENSSL_free(loopargs[i].sig_sig[k]);
  3969. }
  3970. OPENSSL_free(loopargs[i].secret_a);
  3971. OPENSSL_free(loopargs[i].secret_b);
  3972. }
  3973. OPENSSL_free(evp_hmac_name);
  3974. OPENSSL_free(evp_cmac_name);
  3975. for (k = 0; k < kems_algs_len; k++)
  3976. OPENSSL_free(kems_algname[k]);
  3977. if (kem_stack != NULL)
  3978. sk_EVP_KEM_pop_free(kem_stack, EVP_KEM_free);
  3979. for (k = 0; k < sigs_algs_len; k++)
  3980. OPENSSL_free(sigs_algname[k]);
  3981. if (sig_stack != NULL)
  3982. sk_EVP_SIGNATURE_pop_free(sig_stack, EVP_SIGNATURE_free);
  3983. if (async_jobs > 0) {
  3984. for (i = 0; i < loopargs_len; i++)
  3985. ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
  3986. }
  3987. if (async_init) {
  3988. ASYNC_cleanup_thread();
  3989. }
  3990. OPENSSL_free(loopargs);
  3991. release_engine(e);
  3992. EVP_CIPHER_free(evp_cipher);
  3993. EVP_MAC_free(mac);
  3994. NCONF_free(conf);
  3995. return ret;
  3996. }
  3997. static void print_message(const char *s, int length, int tm)
  3998. {
  3999. BIO_printf(bio_err,
  4000. mr ? "+DT:%s:%d:%d\n"
  4001. : "Doing %s ops for %ds on %d size blocks: ", s, tm, length);
  4002. (void)BIO_flush(bio_err);
  4003. run = 1;
  4004. alarm(tm);
  4005. }
  4006. static void pkey_print_message(const char *str, const char *str2, unsigned int bits,
  4007. int tm)
  4008. {
  4009. BIO_printf(bio_err,
  4010. mr ? "+DTP:%d:%s:%s:%d\n"
  4011. : "Doing %u bits %s %s ops for %ds: ", bits, str, str2, tm);
  4012. (void)BIO_flush(bio_err);
  4013. run = 1;
  4014. alarm(tm);
  4015. }
  4016. static void kskey_print_message(const char *str, const char *str2, int tm)
  4017. {
  4018. BIO_printf(bio_err,
  4019. mr ? "+DTP:%s:%s:%d\n"
  4020. : "Doing %s %s ops for %ds: ", str, str2, tm);
  4021. (void)BIO_flush(bio_err);
  4022. run = 1;
  4023. alarm(tm);
  4024. }
  4025. static void print_result(int alg, int run_no, int count, double time_used)
  4026. {
  4027. if (count == -1) {
  4028. BIO_printf(bio_err, "%s error!\n", names[alg]);
  4029. ERR_print_errors(bio_err);
  4030. return;
  4031. }
  4032. BIO_printf(bio_err,
  4033. mr ? "+R:%d:%s:%f\n"
  4034. : "%d %s ops in %.2fs\n", count, names[alg], time_used);
  4035. results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
  4036. }
  4037. #ifndef NO_FORK
  4038. static char *sstrsep(char **string, const char *delim)
  4039. {
  4040. char isdelim[256];
  4041. char *token = *string;
  4042. memset(isdelim, 0, sizeof(isdelim));
  4043. isdelim[0] = 1;
  4044. while (*delim) {
  4045. isdelim[(unsigned char)(*delim)] = 1;
  4046. delim++;
  4047. }
  4048. while (!isdelim[(unsigned char)(**string)])
  4049. (*string)++;
  4050. if (**string) {
  4051. **string = 0;
  4052. (*string)++;
  4053. }
  4054. return token;
  4055. }
  4056. static int strtoint(const char *str, const int min_val, const int upper_val,
  4057. int *res)
  4058. {
  4059. char *end = NULL;
  4060. long int val = 0;
  4061. errno = 0;
  4062. val = strtol(str, &end, 10);
  4063. if (errno == 0 && end != str && *end == 0
  4064. && min_val <= val && val < upper_val) {
  4065. *res = (int)val;
  4066. return 1;
  4067. } else {
  4068. return 0;
  4069. }
  4070. }
  4071. static int do_multi(int multi, int size_num)
  4072. {
  4073. int n;
  4074. int fd[2];
  4075. int *fds;
  4076. int status;
  4077. static char sep[] = ":";
  4078. fds = app_malloc(sizeof(*fds) * multi, "fd buffer for do_multi");
  4079. for (n = 0; n < multi; ++n) {
  4080. if (pipe(fd) == -1) {
  4081. BIO_printf(bio_err, "pipe failure\n");
  4082. exit(1);
  4083. }
  4084. fflush(stdout);
  4085. (void)BIO_flush(bio_err);
  4086. if (fork()) {
  4087. close(fd[1]);
  4088. fds[n] = fd[0];
  4089. } else {
  4090. close(fd[0]);
  4091. close(1);
  4092. if (dup(fd[1]) == -1) {
  4093. BIO_printf(bio_err, "dup failed\n");
  4094. exit(1);
  4095. }
  4096. close(fd[1]);
  4097. mr = 1;
  4098. usertime = 0;
  4099. OPENSSL_free(fds);
  4100. return 0;
  4101. }
  4102. printf("Forked child %d\n", n);
  4103. }
  4104. /* for now, assume the pipe is long enough to take all the output */
  4105. for (n = 0; n < multi; ++n) {
  4106. FILE *f;
  4107. char buf[1024];
  4108. char *p;
  4109. char *tk;
  4110. int k;
  4111. double d;
  4112. if ((f = fdopen(fds[n], "r")) == NULL) {
  4113. BIO_printf(bio_err, "fdopen failure with 0x%x\n",
  4114. errno);
  4115. OPENSSL_free(fds);
  4116. return 1;
  4117. }
  4118. while (fgets(buf, sizeof(buf), f)) {
  4119. p = strchr(buf, '\n');
  4120. if (p)
  4121. *p = '\0';
  4122. if (buf[0] != '+') {
  4123. BIO_printf(bio_err,
  4124. "Don't understand line '%s' from child %d\n", buf,
  4125. n);
  4126. continue;
  4127. }
  4128. printf("Got: %s from %d\n", buf, n);
  4129. p = buf;
  4130. if (CHECK_AND_SKIP_PREFIX(p, "+F:")) {
  4131. int alg;
  4132. int j;
  4133. if (strtoint(sstrsep(&p, sep), 0, ALGOR_NUM, &alg)) {
  4134. sstrsep(&p, sep);
  4135. for (j = 0; j < size_num; ++j)
  4136. results[alg][j] += atof(sstrsep(&p, sep));
  4137. }
  4138. } else if (CHECK_AND_SKIP_PREFIX(p, "+F2:")) {
  4139. tk = sstrsep(&p, sep);
  4140. if (strtoint(tk, 0, OSSL_NELEM(rsa_results), &k)) {
  4141. sstrsep(&p, sep);
  4142. d = atof(sstrsep(&p, sep));
  4143. rsa_results[k][0] += d;
  4144. d = atof(sstrsep(&p, sep));
  4145. rsa_results[k][1] += d;
  4146. d = atof(sstrsep(&p, sep));
  4147. rsa_results[k][2] += d;
  4148. d = atof(sstrsep(&p, sep));
  4149. rsa_results[k][3] += d;
  4150. }
  4151. } else if (CHECK_AND_SKIP_PREFIX(p, "+F3:")) {
  4152. tk = sstrsep(&p, sep);
  4153. if (strtoint(tk, 0, OSSL_NELEM(dsa_results), &k)) {
  4154. sstrsep(&p, sep);
  4155. d = atof(sstrsep(&p, sep));
  4156. dsa_results[k][0] += d;
  4157. d = atof(sstrsep(&p, sep));
  4158. dsa_results[k][1] += d;
  4159. }
  4160. } else if (CHECK_AND_SKIP_PREFIX(p, "+F4:")) {
  4161. tk = sstrsep(&p, sep);
  4162. if (strtoint(tk, 0, OSSL_NELEM(ecdsa_results), &k)) {
  4163. sstrsep(&p, sep);
  4164. d = atof(sstrsep(&p, sep));
  4165. ecdsa_results[k][0] += d;
  4166. d = atof(sstrsep(&p, sep));
  4167. ecdsa_results[k][1] += d;
  4168. }
  4169. } else if (CHECK_AND_SKIP_PREFIX(p, "+F5:")) {
  4170. tk = sstrsep(&p, sep);
  4171. if (strtoint(tk, 0, OSSL_NELEM(ecdh_results), &k)) {
  4172. sstrsep(&p, sep);
  4173. d = atof(sstrsep(&p, sep));
  4174. ecdh_results[k][0] += d;
  4175. }
  4176. # ifndef OPENSSL_NO_ECX
  4177. } else if (CHECK_AND_SKIP_PREFIX(p, "+F6:")) {
  4178. tk = sstrsep(&p, sep);
  4179. if (strtoint(tk, 0, OSSL_NELEM(eddsa_results), &k)) {
  4180. sstrsep(&p, sep);
  4181. sstrsep(&p, sep);
  4182. d = atof(sstrsep(&p, sep));
  4183. eddsa_results[k][0] += d;
  4184. d = atof(sstrsep(&p, sep));
  4185. eddsa_results[k][1] += d;
  4186. }
  4187. # endif /* OPENSSL_NO_ECX */
  4188. # ifndef OPENSSL_NO_SM2
  4189. } else if (CHECK_AND_SKIP_PREFIX(p, "+F7:")) {
  4190. tk = sstrsep(&p, sep);
  4191. if (strtoint(tk, 0, OSSL_NELEM(sm2_results), &k)) {
  4192. sstrsep(&p, sep);
  4193. sstrsep(&p, sep);
  4194. d = atof(sstrsep(&p, sep));
  4195. sm2_results[k][0] += d;
  4196. d = atof(sstrsep(&p, sep));
  4197. sm2_results[k][1] += d;
  4198. }
  4199. # endif /* OPENSSL_NO_SM2 */
  4200. # ifndef OPENSSL_NO_DH
  4201. } else if (CHECK_AND_SKIP_PREFIX(p, "+F8:")) {
  4202. tk = sstrsep(&p, sep);
  4203. if (strtoint(tk, 0, OSSL_NELEM(ffdh_results), &k)) {
  4204. sstrsep(&p, sep);
  4205. d = atof(sstrsep(&p, sep));
  4206. ffdh_results[k][0] += d;
  4207. }
  4208. # endif /* OPENSSL_NO_DH */
  4209. } else if (CHECK_AND_SKIP_PREFIX(p, "+F9:")) {
  4210. tk = sstrsep(&p, sep);
  4211. if (strtoint(tk, 0, OSSL_NELEM(kems_results), &k)) {
  4212. d = atof(sstrsep(&p, sep));
  4213. kems_results[k][0] += d;
  4214. d = atof(sstrsep(&p, sep));
  4215. kems_results[k][1] += d;
  4216. d = atof(sstrsep(&p, sep));
  4217. kems_results[k][2] += d;
  4218. }
  4219. } else if (CHECK_AND_SKIP_PREFIX(p, "+F10:")) {
  4220. tk = sstrsep(&p, sep);
  4221. if (strtoint(tk, 0, OSSL_NELEM(sigs_results), &k)) {
  4222. d = atof(sstrsep(&p, sep));
  4223. sigs_results[k][0] += d;
  4224. d = atof(sstrsep(&p, sep));
  4225. sigs_results[k][1] += d;
  4226. d = atof(sstrsep(&p, sep));
  4227. sigs_results[k][2] += d;
  4228. }
  4229. } else if (!HAS_PREFIX(buf, "+H:")) {
  4230. BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
  4231. n);
  4232. }
  4233. }
  4234. fclose(f);
  4235. }
  4236. OPENSSL_free(fds);
  4237. for (n = 0; n < multi; ++n) {
  4238. while (wait(&status) == -1)
  4239. if (errno != EINTR) {
  4240. BIO_printf(bio_err, "Waitng for child failed with 0x%x\n",
  4241. errno);
  4242. return 1;
  4243. }
  4244. if (WIFEXITED(status) && WEXITSTATUS(status)) {
  4245. BIO_printf(bio_err, "Child exited with %d\n", WEXITSTATUS(status));
  4246. } else if (WIFSIGNALED(status)) {
  4247. BIO_printf(bio_err, "Child terminated by signal %d\n",
  4248. WTERMSIG(status));
  4249. }
  4250. }
  4251. return 1;
  4252. }
  4253. #endif
  4254. static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
  4255. const openssl_speed_sec_t *seconds)
  4256. {
  4257. static const int mblengths_list[] =
  4258. { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
  4259. const int *mblengths = mblengths_list;
  4260. int j, count, keylen, num = OSSL_NELEM(mblengths_list), ciph_success = 1;
  4261. const char *alg_name;
  4262. unsigned char *inp = NULL, *out = NULL, *key, no_key[32], no_iv[16];
  4263. EVP_CIPHER_CTX *ctx = NULL;
  4264. double d = 0.0;
  4265. if (lengths_single) {
  4266. mblengths = &lengths_single;
  4267. num = 1;
  4268. }
  4269. inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
  4270. out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
  4271. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  4272. app_bail_out("failed to allocate cipher context\n");
  4273. if (!EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv))
  4274. app_bail_out("failed to initialise cipher context\n");
  4275. if ((keylen = EVP_CIPHER_CTX_get_key_length(ctx)) < 0) {
  4276. BIO_printf(bio_err, "Impossible negative key length: %d\n", keylen);
  4277. goto err;
  4278. }
  4279. key = app_malloc(keylen, "evp_cipher key");
  4280. if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
  4281. app_bail_out("failed to generate random cipher key\n");
  4282. if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL))
  4283. app_bail_out("failed to set cipher key\n");
  4284. OPENSSL_clear_free(key, keylen);
  4285. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
  4286. sizeof(no_key), no_key) <= 0)
  4287. app_bail_out("failed to set AEAD key\n");
  4288. if ((alg_name = EVP_CIPHER_get0_name(evp_cipher)) == NULL)
  4289. app_bail_out("failed to get cipher name\n");
  4290. for (j = 0; j < num; j++) {
  4291. print_message(alg_name, mblengths[j], seconds->sym);
  4292. Time_F(START);
  4293. for (count = 0; run && count < INT_MAX; count++) {
  4294. unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
  4295. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
  4296. size_t len = mblengths[j];
  4297. int packlen;
  4298. memset(aad, 0, 8); /* avoid uninitialized values */
  4299. aad[8] = 23; /* SSL3_RT_APPLICATION_DATA */
  4300. aad[9] = 3; /* version */
  4301. aad[10] = 2;
  4302. aad[11] = 0; /* length */
  4303. aad[12] = 0;
  4304. mb_param.out = NULL;
  4305. mb_param.inp = aad;
  4306. mb_param.len = len;
  4307. mb_param.interleave = 8;
  4308. packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
  4309. sizeof(mb_param), &mb_param);
  4310. if (packlen > 0) {
  4311. mb_param.out = out;
  4312. mb_param.inp = inp;
  4313. mb_param.len = len;
  4314. (void)EVP_CIPHER_CTX_ctrl(ctx,
  4315. EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
  4316. sizeof(mb_param), &mb_param);
  4317. } else {
  4318. int pad;
  4319. if (RAND_bytes(inp, 16) <= 0)
  4320. app_bail_out("error setting random bytes\n");
  4321. len += 16;
  4322. aad[11] = (unsigned char)(len >> 8);
  4323. aad[12] = (unsigned char)(len);
  4324. pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
  4325. EVP_AEAD_TLS1_AAD_LEN, aad);
  4326. ciph_success = EVP_Cipher(ctx, out, inp, len + pad);
  4327. }
  4328. }
  4329. d = Time_F(STOP);
  4330. BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
  4331. : "%d %s ops in %.2fs\n", count, "evp", d);
  4332. if ((ciph_success <= 0) && (mr == 0))
  4333. BIO_printf(bio_err, "Error performing cipher op\n");
  4334. results[D_EVP][j] = ((double)count) / d * mblengths[j];
  4335. }
  4336. if (mr) {
  4337. fprintf(stdout, "+H");
  4338. for (j = 0; j < num; j++)
  4339. fprintf(stdout, ":%d", mblengths[j]);
  4340. fprintf(stdout, "\n");
  4341. fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
  4342. for (j = 0; j < num; j++)
  4343. fprintf(stdout, ":%.2f", results[D_EVP][j]);
  4344. fprintf(stdout, "\n");
  4345. } else {
  4346. fprintf(stdout,
  4347. "The 'numbers' are in 1000s of bytes per second processed.\n");
  4348. fprintf(stdout, "type ");
  4349. for (j = 0; j < num; j++)
  4350. fprintf(stdout, "%7d bytes", mblengths[j]);
  4351. fprintf(stdout, "\n");
  4352. fprintf(stdout, "%-24s", alg_name);
  4353. for (j = 0; j < num; j++) {
  4354. if (results[D_EVP][j] > 10000)
  4355. fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
  4356. else
  4357. fprintf(stdout, " %11.2f ", results[D_EVP][j]);
  4358. }
  4359. fprintf(stdout, "\n");
  4360. }
  4361. err:
  4362. OPENSSL_free(inp);
  4363. OPENSSL_free(out);
  4364. EVP_CIPHER_CTX_free(ctx);
  4365. }