speed.c 168 KB

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