speed.c 176 KB

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