speed.c 165 KB

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