1
0

speed.c 178 KB

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