430-e_devcrypto-make-the-dev-crypto-engine-dynamic.patch 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693
  1. From 37a5c14aad5051201e4bd18faf1a4b25a824cc30 Mon Sep 17 00:00:00 2001
  2. From: Eneas U de Queiroz <[email protected]>
  3. Date: Tue, 6 Nov 2018 10:57:03 -0200
  4. Subject: [PATCH 4/4] e_devcrypto: make the /dev/crypto engine dynamic
  5. Engine has been moved from crypto/engine/eng_devcrypto.c to
  6. engines/e_devcrypto.c.
  7. Signed-off-by: Eneas U de Queiroz <[email protected]>
  8. --- a/crypto/engine/build.info
  9. +++ b/crypto/engine/build.info
  10. @@ -6,6 +6,3 @@ SOURCE[../../libcrypto]=\
  11. tb_cipher.c tb_digest.c tb_pkmeth.c tb_asnmth.c tb_eckey.c \
  12. eng_openssl.c eng_cnf.c eng_dyn.c \
  13. eng_rdrand.c
  14. -IF[{- !$disabled{devcryptoeng} -}]
  15. - SOURCE[../../libcrypto]=eng_devcrypto.c
  16. -ENDIF
  17. --- a/crypto/init.c
  18. +++ b/crypto/init.c
  19. @@ -330,18 +330,6 @@ DEFINE_RUN_ONCE_STATIC(ossl_init_engine_
  20. engine_load_openssl_int();
  21. return 1;
  22. }
  23. -# ifndef OPENSSL_NO_DEVCRYPTOENG
  24. -static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
  25. -DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
  26. -{
  27. -# ifdef OPENSSL_INIT_DEBUG
  28. - fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
  29. - "engine_load_devcrypto_int()\n");
  30. -# endif
  31. - engine_load_devcrypto_int();
  32. - return 1;
  33. -}
  34. -# endif
  35. # ifndef OPENSSL_NO_RDRAND
  36. static CRYPTO_ONCE engine_rdrand = CRYPTO_ONCE_STATIC_INIT;
  37. @@ -366,6 +354,18 @@ DEFINE_RUN_ONCE_STATIC(ossl_init_engine_
  38. return 1;
  39. }
  40. # ifndef OPENSSL_NO_STATIC_ENGINE
  41. +# ifndef OPENSSL_NO_DEVCRYPTOENG
  42. +static CRYPTO_ONCE engine_devcrypto = CRYPTO_ONCE_STATIC_INIT;
  43. +DEFINE_RUN_ONCE_STATIC(ossl_init_engine_devcrypto)
  44. +{
  45. +# ifdef OPENSSL_INIT_DEBUG
  46. + fprintf(stderr, "OPENSSL_INIT: ossl_init_engine_devcrypto: "
  47. + "engine_load_devcrypto_int()\n");
  48. +# endif
  49. + engine_load_devcrypto_int();
  50. + return 1;
  51. +}
  52. +# endif
  53. # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
  54. static CRYPTO_ONCE engine_padlock = CRYPTO_ONCE_STATIC_INIT;
  55. DEFINE_RUN_ONCE_STATIC(ossl_init_engine_padlock)
  56. @@ -714,11 +714,6 @@ int OPENSSL_init_crypto(uint64_t opts, c
  57. if ((opts & OPENSSL_INIT_ENGINE_OPENSSL)
  58. && !RUN_ONCE(&engine_openssl, ossl_init_engine_openssl))
  59. return 0;
  60. -# if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_DEVCRYPTOENG)
  61. - if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
  62. - && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
  63. - return 0;
  64. -# endif
  65. # ifndef OPENSSL_NO_RDRAND
  66. if ((opts & OPENSSL_INIT_ENGINE_RDRAND)
  67. && !RUN_ONCE(&engine_rdrand, ossl_init_engine_rdrand))
  68. @@ -728,6 +723,11 @@ int OPENSSL_init_crypto(uint64_t opts, c
  69. && !RUN_ONCE(&engine_dynamic, ossl_init_engine_dynamic))
  70. return 0;
  71. # ifndef OPENSSL_NO_STATIC_ENGINE
  72. +# ifndef OPENSSL_NO_DEVCRYPTOENG
  73. + if ((opts & OPENSSL_INIT_ENGINE_CRYPTODEV)
  74. + && !RUN_ONCE(&engine_devcrypto, ossl_init_engine_devcrypto))
  75. + return 0;
  76. +# endif
  77. # if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_PADLOCK)
  78. if ((opts & OPENSSL_INIT_ENGINE_PADLOCK)
  79. && !RUN_ONCE(&engine_padlock, ossl_init_engine_padlock))
  80. --- a/engines/build.info
  81. +++ b/engines/build.info
  82. @@ -10,6 +10,9 @@ IF[{- !$disabled{"engine"} -}]
  83. IF[{- !$disabled{afalgeng} -}]
  84. SOURCE[../libcrypto]=e_afalg.c
  85. ENDIF
  86. + IF[{- !$disabled{"devcryptoeng"} -}]
  87. + SOURCE[../libcrypto]=e_devcrypto.c
  88. + ENDIF
  89. ELSE
  90. ENGINES=padlock
  91. SOURCE[padlock]=e_padlock.c {- $target{padlock_asm_src} -}
  92. @@ -27,6 +30,12 @@ IF[{- !$disabled{"engine"} -}]
  93. DEPEND[afalg]=../libcrypto
  94. INCLUDE[afalg]= ../include
  95. ENDIF
  96. + IF[{- !$disabled{"devcryptoeng"} -}]
  97. + ENGINES=devcrypto
  98. + SOURCE[devcrypto]=e_devcrypto.c
  99. + DEPEND[devcrypto]=../libcrypto
  100. + INCLUDE[devcrypto]=../include
  101. + ENDIF
  102. ENGINES_NO_INST=ossltest dasync
  103. SOURCE[dasync]=e_dasync.c
  104. --- a/crypto/engine/eng_devcrypto.c
  105. +++ /dev/null
  106. @@ -1,1264 +0,0 @@
  107. -/*
  108. - * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
  109. - *
  110. - * Licensed under the OpenSSL license (the "License"). You may not use
  111. - * this file except in compliance with the License. You can obtain a copy
  112. - * in the file LICENSE in the source distribution or at
  113. - * https://www.openssl.org/source/license.html
  114. - */
  115. -
  116. -#include "e_os.h"
  117. -#include <string.h>
  118. -#include <sys/types.h>
  119. -#include <sys/stat.h>
  120. -#include <fcntl.h>
  121. -#include <sys/ioctl.h>
  122. -#include <unistd.h>
  123. -#include <assert.h>
  124. -
  125. -#include <openssl/conf.h>
  126. -#include <openssl/evp.h>
  127. -#include <openssl/err.h>
  128. -#include <openssl/engine.h>
  129. -#include <openssl/objects.h>
  130. -#include <crypto/cryptodev.h>
  131. -
  132. -#include "internal/engine.h"
  133. -
  134. -/* #define ENGINE_DEVCRYPTO_DEBUG */
  135. -
  136. -#ifdef CRYPTO_ALGORITHM_MIN
  137. -# define CHECK_BSD_STYLE_MACROS
  138. -#endif
  139. -
  140. -/*
  141. - * ONE global file descriptor for all sessions. This allows operations
  142. - * such as digest session data copying (see digest_copy()), but is also
  143. - * saner... why re-open /dev/crypto for every session?
  144. - */
  145. -static int cfd;
  146. -#define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
  147. -#define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */
  148. -#define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
  149. -
  150. -#define DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS DEVCRYPTO_REJECT_SOFTWARE
  151. -static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS;
  152. -
  153. -/*
  154. - * cipher/digest status & acceleration definitions
  155. - * Make sure the defaults are set to 0
  156. - */
  157. -struct driver_info_st {
  158. - enum devcrypto_status_t {
  159. - DEVCRYPTO_STATUS_FAILURE = -3, /* unusable for other reason */
  160. - DEVCRYPTO_STATUS_NO_CIOCCPHASH = -2, /* hash state copy not supported */
  161. - DEVCRYPTO_STATUS_NO_CIOCGSESSION = -1, /* session open failed */
  162. - DEVCRYPTO_STATUS_UNKNOWN = 0, /* not tested yet */
  163. - DEVCRYPTO_STATUS_USABLE = 1 /* algo can be used */
  164. - } status;
  165. -
  166. - enum devcrypto_accelerated_t {
  167. - DEVCRYPTO_NOT_ACCELERATED = -1, /* software implemented */
  168. - DEVCRYPTO_ACCELERATION_UNKNOWN = 0, /* acceleration support unkown */
  169. - DEVCRYPTO_ACCELERATED = 1 /* hardware accelerated */
  170. - } accelerated;
  171. -
  172. - char *driver_name;
  173. -};
  174. -
  175. -static int clean_devcrypto_session(struct session_op *sess) {
  176. - if (ioctl(cfd, CIOCFSESSION, &sess->ses) < 0) {
  177. - SYSerr(SYS_F_IOCTL, errno);
  178. - return 0;
  179. - }
  180. - memset(sess, 0, sizeof(struct session_op));
  181. - return 1;
  182. -}
  183. -
  184. -/******************************************************************************
  185. - *
  186. - * Ciphers
  187. - *
  188. - * Because they all do the same basic operation, we have only one set of
  189. - * method functions for them all to share, and a mapping table between
  190. - * NIDs and cryptodev IDs, with all the necessary size data.
  191. - *
  192. - *****/
  193. -
  194. -struct cipher_ctx {
  195. - struct session_op sess;
  196. - int op; /* COP_ENCRYPT or COP_DECRYPT */
  197. - unsigned long mode; /* EVP_CIPH_*_MODE */
  198. -
  199. - /* to handle ctr mode being a stream cipher */
  200. - unsigned char partial[EVP_MAX_BLOCK_LENGTH];
  201. - unsigned int blocksize, num;
  202. -};
  203. -
  204. -static const struct cipher_data_st {
  205. - int nid;
  206. - int blocksize;
  207. - int keylen;
  208. - int ivlen;
  209. - int flags;
  210. - int devcryptoid;
  211. -} cipher_data[] = {
  212. -#ifndef OPENSSL_NO_DES
  213. - { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
  214. - { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
  215. -#endif
  216. -#ifndef OPENSSL_NO_BF
  217. - { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
  218. -#endif
  219. -#ifndef OPENSSL_NO_CAST
  220. - { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
  221. -#endif
  222. - { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  223. - { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  224. - { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  225. -#ifndef OPENSSL_NO_RC4
  226. - { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
  227. -#endif
  228. -#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
  229. - { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  230. - { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  231. - { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  232. -#endif
  233. -#if 0 /* Not yet supported */
  234. - { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
  235. - { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
  236. -#endif
  237. -#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
  238. - { NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  239. - { NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  240. - { NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  241. -#endif
  242. -#if 0 /* Not yet supported */
  243. - { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  244. - { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  245. - { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  246. -#endif
  247. -#ifndef OPENSSL_NO_CAMELLIA
  248. - { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
  249. - CRYPTO_CAMELLIA_CBC },
  250. - { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
  251. - CRYPTO_CAMELLIA_CBC },
  252. - { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
  253. - CRYPTO_CAMELLIA_CBC },
  254. -#endif
  255. -};
  256. -
  257. -static size_t find_cipher_data_index(int nid)
  258. -{
  259. - size_t i;
  260. -
  261. - for (i = 0; i < OSSL_NELEM(cipher_data); i++)
  262. - if (nid == cipher_data[i].nid)
  263. - return i;
  264. - return (size_t)-1;
  265. -}
  266. -
  267. -static size_t get_cipher_data_index(int nid)
  268. -{
  269. - size_t i = find_cipher_data_index(nid);
  270. -
  271. - if (i != (size_t)-1)
  272. - return i;
  273. -
  274. - /*
  275. - * Code further down must make sure that only NIDs in the table above
  276. - * are used. If any other NID reaches this function, there's a grave
  277. - * coding error further down.
  278. - */
  279. - assert("Code that never should be reached" == NULL);
  280. - return -1;
  281. -}
  282. -
  283. -static const struct cipher_data_st *get_cipher_data(int nid)
  284. -{
  285. - return &cipher_data[get_cipher_data_index(nid)];
  286. -}
  287. -
  288. -/*
  289. - * Following are the three necessary functions to map OpenSSL functionality
  290. - * with cryptodev.
  291. - */
  292. -
  293. -static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  294. - const unsigned char *iv, int enc)
  295. -{
  296. - struct cipher_ctx *cipher_ctx =
  297. - (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  298. - const struct cipher_data_st *cipher_d =
  299. - get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
  300. -
  301. - /* cleanup a previous session */
  302. - if (cipher_ctx->sess.ses != 0 &&
  303. - clean_devcrypto_session(&cipher_ctx->sess) == 0)
  304. - return 0;
  305. -
  306. - cipher_ctx->sess.cipher = cipher_d->devcryptoid;
  307. - cipher_ctx->sess.keylen = cipher_d->keylen;
  308. - cipher_ctx->sess.key = (void *)key;
  309. - cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
  310. - cipher_ctx->mode = cipher_d->flags & EVP_CIPH_MODE;
  311. - cipher_ctx->blocksize = cipher_d->blocksize;
  312. - if (ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
  313. - SYSerr(SYS_F_IOCTL, errno);
  314. - return 0;
  315. - }
  316. -
  317. - return 1;
  318. -}
  319. -
  320. -static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  321. - const unsigned char *in, size_t inl)
  322. -{
  323. - struct cipher_ctx *cipher_ctx =
  324. - (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  325. - struct crypt_op cryp;
  326. - unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
  327. -#if !defined(COP_FLAG_WRITE_IV)
  328. - unsigned char saved_iv[EVP_MAX_IV_LENGTH];
  329. - const unsigned char *ivptr;
  330. - size_t nblocks, ivlen;
  331. -#endif
  332. -
  333. - memset(&cryp, 0, sizeof(cryp));
  334. - cryp.ses = cipher_ctx->sess.ses;
  335. - cryp.len = inl;
  336. - cryp.src = (void *)in;
  337. - cryp.dst = (void *)out;
  338. - cryp.iv = (void *)iv;
  339. - cryp.op = cipher_ctx->op;
  340. -#if !defined(COP_FLAG_WRITE_IV)
  341. - cryp.flags = 0;
  342. -
  343. - ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  344. - if (ivlen > 0)
  345. - switch (cipher_ctx->mode) {
  346. - case EVP_CIPH_CBC_MODE:
  347. - assert(inl >= ivlen);
  348. - if (!EVP_CIPHER_CTX_encrypting(ctx)) {
  349. - ivptr = in + inl - ivlen;
  350. - memcpy(saved_iv, ivptr, ivlen);
  351. - }
  352. - break;
  353. -
  354. - case EVP_CIPH_CTR_MODE:
  355. - break;
  356. -
  357. - default: /* should not happen */
  358. - return 0;
  359. - }
  360. -#else
  361. - cryp.flags = COP_FLAG_WRITE_IV;
  362. -#endif
  363. -
  364. - if (ioctl(cfd, CIOCCRYPT, &cryp) < 0) {
  365. - SYSerr(SYS_F_IOCTL, errno);
  366. - return 0;
  367. - }
  368. -
  369. -#if !defined(COP_FLAG_WRITE_IV)
  370. - if (ivlen > 0)
  371. - switch (cipher_ctx->mode) {
  372. - case EVP_CIPH_CBC_MODE:
  373. - assert(inl >= ivlen);
  374. - if (EVP_CIPHER_CTX_encrypting(ctx))
  375. - ivptr = out + inl - ivlen;
  376. - else
  377. - ivptr = saved_iv;
  378. -
  379. - memcpy(iv, ivptr, ivlen);
  380. - break;
  381. -
  382. - case EVP_CIPH_CTR_MODE:
  383. - nblocks = (inl + cipher_ctx->blocksize - 1)
  384. - / cipher_ctx->blocksize;
  385. - do {
  386. - ivlen--;
  387. - nblocks += iv[ivlen];
  388. - iv[ivlen] = (uint8_t) nblocks;
  389. - nblocks >>= 8;
  390. - } while (ivlen);
  391. - break;
  392. -
  393. - default: /* should not happen */
  394. - return 0;
  395. - }
  396. -#endif
  397. -
  398. - return 1;
  399. -}
  400. -
  401. -static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  402. - const unsigned char *in, size_t inl)
  403. -{
  404. - struct cipher_ctx *cipher_ctx =
  405. - (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  406. - size_t nblocks, len;
  407. -
  408. - /* initial partial block */
  409. - while (cipher_ctx->num && inl) {
  410. - (*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num];
  411. - --inl;
  412. - cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize;
  413. - }
  414. -
  415. - /* full blocks */
  416. - if (inl > (unsigned int) cipher_ctx->blocksize) {
  417. - nblocks = inl/cipher_ctx->blocksize;
  418. - len = nblocks * cipher_ctx->blocksize;
  419. - if (cipher_do_cipher(ctx, out, in, len) < 1)
  420. - return 0;
  421. - inl -= len;
  422. - out += len;
  423. - in += len;
  424. - }
  425. -
  426. - /* final partial block */
  427. - if (inl) {
  428. - memset(cipher_ctx->partial, 0, cipher_ctx->blocksize);
  429. - if (cipher_do_cipher(ctx, cipher_ctx->partial, cipher_ctx->partial,
  430. - cipher_ctx->blocksize) < 1)
  431. - return 0;
  432. - while (inl--) {
  433. - out[cipher_ctx->num] = in[cipher_ctx->num]
  434. - ^ cipher_ctx->partial[cipher_ctx->num];
  435. - cipher_ctx->num++;
  436. - }
  437. - }
  438. -
  439. - return 1;
  440. -}
  441. -
  442. -static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
  443. -{
  444. - struct cipher_ctx *cipher_ctx =
  445. - (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  446. - EVP_CIPHER_CTX *to_ctx = (EVP_CIPHER_CTX *)p2;
  447. - struct cipher_ctx *to_cipher_ctx;
  448. -
  449. - switch (type) {
  450. - case EVP_CTRL_COPY:
  451. - if (cipher_ctx == NULL)
  452. - return 1;
  453. - /* when copying the context, a new session needs to be initialized */
  454. - to_cipher_ctx =
  455. - (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(to_ctx);
  456. - memset(&to_cipher_ctx->sess, 0, sizeof(to_cipher_ctx->sess));
  457. - return cipher_init(to_ctx, cipher_ctx->sess.key, EVP_CIPHER_CTX_iv(ctx),
  458. - (cipher_ctx->op == COP_ENCRYPT));
  459. -
  460. - case EVP_CTRL_INIT:
  461. - memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
  462. - return 1;
  463. -
  464. - default:
  465. - break;
  466. - }
  467. -
  468. - return -1;
  469. -}
  470. -
  471. -static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
  472. -{
  473. - struct cipher_ctx *cipher_ctx =
  474. - (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  475. -
  476. - return clean_devcrypto_session(&cipher_ctx->sess);
  477. -}
  478. -
  479. -/*
  480. - * Keep tables of known nids, associated methods, selected ciphers, and driver
  481. - * info.
  482. - * Note that known_cipher_nids[] isn't necessarily indexed the same way as
  483. - * cipher_data[] above, which the other tables are.
  484. - */
  485. -static int known_cipher_nids[OSSL_NELEM(cipher_data)];
  486. -static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
  487. -static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
  488. -static int selected_ciphers[OSSL_NELEM(cipher_data)];
  489. -static struct driver_info_st cipher_driver_info[OSSL_NELEM(cipher_data)];
  490. -
  491. -
  492. -static int devcrypto_test_cipher(size_t cipher_data_index)
  493. -{
  494. - return (cipher_driver_info[cipher_data_index].status == DEVCRYPTO_STATUS_USABLE
  495. - && selected_ciphers[cipher_data_index] == 1
  496. - && (cipher_driver_info[cipher_data_index].accelerated
  497. - == DEVCRYPTO_ACCELERATED
  498. - || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
  499. - || (cipher_driver_info[cipher_data_index].accelerated
  500. - != DEVCRYPTO_NOT_ACCELERATED
  501. - && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
  502. -}
  503. -
  504. -static void prepare_cipher_methods(void)
  505. -{
  506. - size_t i;
  507. - struct session_op sess;
  508. - unsigned long cipher_mode;
  509. -#ifdef CIOCGSESSINFO
  510. - struct session_info_op siop;
  511. -#endif
  512. -
  513. - memset(&cipher_driver_info, 0, sizeof(cipher_driver_info));
  514. -
  515. - memset(&sess, 0, sizeof(sess));
  516. - sess.key = (void *)"01234567890123456789012345678901234567890123456789";
  517. -
  518. - for (i = 0, known_cipher_nids_amount = 0;
  519. - i < OSSL_NELEM(cipher_data); i++) {
  520. -
  521. - selected_ciphers[i] = 1;
  522. - /*
  523. - * Check that the cipher is usable
  524. - */
  525. - sess.cipher = cipher_data[i].devcryptoid;
  526. - sess.keylen = cipher_data[i].keylen;
  527. - if (ioctl(cfd, CIOCGSESSION, &sess) < 0) {
  528. - cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
  529. - continue;
  530. - }
  531. -
  532. - cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE;
  533. -
  534. - if ((known_cipher_methods[i] =
  535. - EVP_CIPHER_meth_new(cipher_data[i].nid,
  536. - cipher_mode == EVP_CIPH_CTR_MODE ? 1 :
  537. - cipher_data[i].blocksize,
  538. - cipher_data[i].keylen)) == NULL
  539. - || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
  540. - cipher_data[i].ivlen)
  541. - || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
  542. - cipher_data[i].flags
  543. - | EVP_CIPH_CUSTOM_COPY
  544. - | EVP_CIPH_CTRL_INIT
  545. - | EVP_CIPH_FLAG_DEFAULT_ASN1)
  546. - || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
  547. - || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
  548. - cipher_mode == EVP_CIPH_CTR_MODE ?
  549. - ctr_do_cipher :
  550. - cipher_do_cipher)
  551. - || !EVP_CIPHER_meth_set_ctrl(known_cipher_methods[i], cipher_ctrl)
  552. - || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
  553. - cipher_cleanup)
  554. - || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
  555. - sizeof(struct cipher_ctx))) {
  556. - cipher_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
  557. - EVP_CIPHER_meth_free(known_cipher_methods[i]);
  558. - known_cipher_methods[i] = NULL;
  559. - } else {
  560. - cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
  561. -#ifdef CIOCGSESSINFO
  562. - siop.ses = sess.ses;
  563. - if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
  564. - cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
  565. - } else {
  566. - cipher_driver_info[i].driver_name =
  567. - OPENSSL_strndup(siop.cipher_info.cra_driver_name,
  568. - CRYPTODEV_MAX_ALG_NAME);
  569. - if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
  570. - cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
  571. - else
  572. - cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
  573. - }
  574. -#endif /* CIOCGSESSINFO */
  575. - }
  576. - ioctl(cfd, CIOCFSESSION, &sess.ses);
  577. - if (devcrypto_test_cipher(i)) {
  578. - known_cipher_nids[known_cipher_nids_amount++] =
  579. - cipher_data[i].nid;
  580. - }
  581. - }
  582. -}
  583. -
  584. -static void rebuild_known_cipher_nids(ENGINE *e)
  585. -{
  586. - size_t i;
  587. -
  588. - for (i = 0, known_cipher_nids_amount = 0; i < OSSL_NELEM(cipher_data); i++) {
  589. - if (devcrypto_test_cipher(i))
  590. - known_cipher_nids[known_cipher_nids_amount++] = cipher_data[i].nid;
  591. - }
  592. - ENGINE_unregister_ciphers(e);
  593. - ENGINE_register_ciphers(e);
  594. -}
  595. -
  596. -static const EVP_CIPHER *get_cipher_method(int nid)
  597. -{
  598. - size_t i = get_cipher_data_index(nid);
  599. -
  600. - if (i == (size_t)-1)
  601. - return NULL;
  602. - return known_cipher_methods[i];
  603. -}
  604. -
  605. -static int get_cipher_nids(const int **nids)
  606. -{
  607. - *nids = known_cipher_nids;
  608. - return known_cipher_nids_amount;
  609. -}
  610. -
  611. -static void destroy_cipher_method(int nid)
  612. -{
  613. - size_t i = get_cipher_data_index(nid);
  614. -
  615. - EVP_CIPHER_meth_free(known_cipher_methods[i]);
  616. - known_cipher_methods[i] = NULL;
  617. -}
  618. -
  619. -static void destroy_all_cipher_methods(void)
  620. -{
  621. - size_t i;
  622. -
  623. - for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
  624. - destroy_cipher_method(cipher_data[i].nid);
  625. - OPENSSL_free(cipher_driver_info[i].driver_name);
  626. - cipher_driver_info[i].driver_name = NULL;
  627. - }
  628. -}
  629. -
  630. -static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  631. - const int **nids, int nid)
  632. -{
  633. - if (cipher == NULL)
  634. - return get_cipher_nids(nids);
  635. -
  636. - *cipher = get_cipher_method(nid);
  637. -
  638. - return *cipher != NULL;
  639. -}
  640. -
  641. -static void devcrypto_select_all_ciphers(int *cipher_list)
  642. -{
  643. - size_t i;
  644. -
  645. - for (i = 0; i < OSSL_NELEM(cipher_data); i++)
  646. - cipher_list[i] = 1;
  647. -}
  648. -
  649. -static int cryptodev_select_cipher_cb(const char *str, int len, void *usr)
  650. -{
  651. - int *cipher_list = (int *)usr;
  652. - char *name;
  653. - const EVP_CIPHER *EVP;
  654. - size_t i;
  655. -
  656. - if (len == 0)
  657. - return 1;
  658. - if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
  659. - return 0;
  660. - EVP = EVP_get_cipherbyname(name);
  661. - if (EVP == NULL)
  662. - fprintf(stderr, "devcrypto: unknown cipher %s\n", name);
  663. - else if ((i = find_cipher_data_index(EVP_CIPHER_nid(EVP))) != (size_t)-1)
  664. - cipher_list[i] = 1;
  665. - else
  666. - fprintf(stderr, "devcrypto: cipher %s not available\n", name);
  667. - OPENSSL_free(name);
  668. - return 1;
  669. -}
  670. -
  671. -static void dump_cipher_info(void)
  672. -{
  673. - size_t i;
  674. - const char *name;
  675. -
  676. - fprintf (stderr, "Information about ciphers supported by the /dev/crypto"
  677. - " engine:\n");
  678. -#ifndef CIOCGSESSINFO
  679. - fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
  680. -#endif
  681. - for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
  682. - name = OBJ_nid2sn(cipher_data[i].nid);
  683. - fprintf (stderr, "Cipher %s, NID=%d, /dev/crypto info: id=%d, ",
  684. - name ? name : "unknown", cipher_data[i].nid,
  685. - cipher_data[i].devcryptoid);
  686. - if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION ) {
  687. - fprintf (stderr, "CIOCGSESSION (session open call) failed\n");
  688. - continue;
  689. - }
  690. - fprintf (stderr, "driver=%s ", cipher_driver_info[i].driver_name ?
  691. - cipher_driver_info[i].driver_name : "unknown");
  692. - if (cipher_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
  693. - fprintf(stderr, "(hw accelerated)");
  694. - else if (cipher_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
  695. - fprintf(stderr, "(software)");
  696. - else
  697. - fprintf(stderr, "(acceleration status unknown)");
  698. - if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
  699. - fprintf (stderr, ". Cipher setup failed");
  700. - fprintf(stderr, "\n");
  701. - }
  702. - fprintf(stderr, "\n");
  703. -}
  704. -
  705. -/*
  706. - * We only support digests if the cryptodev implementation supports multiple
  707. - * data updates and session copying. Otherwise, we would be forced to maintain
  708. - * a cache, which is perilous if there's a lot of data coming in (if someone
  709. - * wants to checksum an OpenSSL tarball, for example).
  710. - */
  711. -#if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
  712. -#define IMPLEMENT_DIGEST
  713. -
  714. -/******************************************************************************
  715. - *
  716. - * Digests
  717. - *
  718. - * Because they all do the same basic operation, we have only one set of
  719. - * method functions for them all to share, and a mapping table between
  720. - * NIDs and cryptodev IDs, with all the necessary size data.
  721. - *
  722. - *****/
  723. -
  724. -struct digest_ctx {
  725. - struct session_op sess;
  726. - /* This signals that the init function was called, not that it succeeded. */
  727. - int init_called;
  728. - unsigned char digest_res[HASH_MAX_LEN];
  729. -};
  730. -
  731. -static const struct digest_data_st {
  732. - int nid;
  733. - int blocksize;
  734. - int digestlen;
  735. - int devcryptoid;
  736. -} digest_data[] = {
  737. -#ifndef OPENSSL_NO_MD5
  738. - { NID_md5, /* MD5_CBLOCK */ 64, 16, CRYPTO_MD5 },
  739. -#endif
  740. - { NID_sha1, SHA_CBLOCK, 20, CRYPTO_SHA1 },
  741. -#ifndef OPENSSL_NO_RMD160
  742. -# if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
  743. - { NID_ripemd160, /* RIPEMD160_CBLOCK */ 64, 20, CRYPTO_RIPEMD160 },
  744. -# endif
  745. -#endif
  746. -#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
  747. - { NID_sha224, SHA256_CBLOCK, 224 / 8, CRYPTO_SHA2_224 },
  748. -#endif
  749. -#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
  750. - { NID_sha256, SHA256_CBLOCK, 256 / 8, CRYPTO_SHA2_256 },
  751. -#endif
  752. -#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
  753. - { NID_sha384, SHA512_CBLOCK, 384 / 8, CRYPTO_SHA2_384 },
  754. -#endif
  755. -#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
  756. - { NID_sha512, SHA512_CBLOCK, 512 / 8, CRYPTO_SHA2_512 },
  757. -#endif
  758. -};
  759. -
  760. -static size_t find_digest_data_index(int nid)
  761. -{
  762. - size_t i;
  763. -
  764. - for (i = 0; i < OSSL_NELEM(digest_data); i++)
  765. - if (nid == digest_data[i].nid)
  766. - return i;
  767. - return (size_t)-1;
  768. -}
  769. -
  770. -static size_t get_digest_data_index(int nid)
  771. -{
  772. - size_t i = find_digest_data_index(nid);
  773. -
  774. - if (i != (size_t)-1)
  775. - return i;
  776. -
  777. - /*
  778. - * Code further down must make sure that only NIDs in the table above
  779. - * are used. If any other NID reaches this function, there's a grave
  780. - * coding error further down.
  781. - */
  782. - assert("Code that never should be reached" == NULL);
  783. - return -1;
  784. -}
  785. -
  786. -static const struct digest_data_st *get_digest_data(int nid)
  787. -{
  788. - return &digest_data[get_digest_data_index(nid)];
  789. -}
  790. -
  791. -/*
  792. - * Following are the five necessary functions to map OpenSSL functionality
  793. - * with cryptodev: init, update, final, cleanup, and copy.
  794. - */
  795. -
  796. -static int digest_init(EVP_MD_CTX *ctx)
  797. -{
  798. - struct digest_ctx *digest_ctx =
  799. - (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  800. - const struct digest_data_st *digest_d =
  801. - get_digest_data(EVP_MD_CTX_type(ctx));
  802. -
  803. - digest_ctx->init_called = 1;
  804. -
  805. - memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
  806. - digest_ctx->sess.mac = digest_d->devcryptoid;
  807. - if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
  808. - SYSerr(SYS_F_IOCTL, errno);
  809. - return 0;
  810. - }
  811. -
  812. - return 1;
  813. -}
  814. -
  815. -static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
  816. - void *res, unsigned int flags)
  817. -{
  818. - struct crypt_op cryp;
  819. -
  820. - memset(&cryp, 0, sizeof(cryp));
  821. - cryp.ses = ctx->sess.ses;
  822. - cryp.len = srclen;
  823. - cryp.src = (void *)src;
  824. - cryp.dst = NULL;
  825. - cryp.mac = res;
  826. - cryp.flags = flags;
  827. - return ioctl(cfd, CIOCCRYPT, &cryp);
  828. -}
  829. -
  830. -static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  831. -{
  832. - struct digest_ctx *digest_ctx =
  833. - (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  834. -
  835. - if (count == 0)
  836. - return 1;
  837. -
  838. - if (digest_ctx == NULL)
  839. - return 0;
  840. -
  841. - if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
  842. - if (digest_op(digest_ctx, data, count, digest_ctx->digest_res, 0) >= 0)
  843. - return 1;
  844. - } else if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) >= 0) {
  845. - return 1;
  846. - }
  847. -
  848. - SYSerr(SYS_F_IOCTL, errno);
  849. - return 0;
  850. -}
  851. -
  852. -static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
  853. -{
  854. - struct digest_ctx *digest_ctx =
  855. - (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  856. -
  857. - if (md == NULL || digest_ctx == NULL)
  858. - return 0;
  859. -
  860. - if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
  861. - memcpy(md, digest_ctx->digest_res, EVP_MD_CTX_size(ctx));
  862. - } else if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
  863. - SYSerr(SYS_F_IOCTL, errno);
  864. - return 0;
  865. - }
  866. -
  867. - return 1;
  868. -}
  869. -
  870. -static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
  871. -{
  872. - struct digest_ctx *digest_from =
  873. - (struct digest_ctx *)EVP_MD_CTX_md_data(from);
  874. - struct digest_ctx *digest_to =
  875. - (struct digest_ctx *)EVP_MD_CTX_md_data(to);
  876. - struct cphash_op cphash;
  877. -
  878. - if (digest_from == NULL || digest_from->init_called != 1)
  879. - return 1;
  880. -
  881. - if (!digest_init(to)) {
  882. - SYSerr(SYS_F_IOCTL, errno);
  883. - return 0;
  884. - }
  885. -
  886. - cphash.src_ses = digest_from->sess.ses;
  887. - cphash.dst_ses = digest_to->sess.ses;
  888. - if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
  889. - SYSerr(SYS_F_IOCTL, errno);
  890. - return 0;
  891. - }
  892. - return 1;
  893. -}
  894. -
  895. -static int digest_cleanup(EVP_MD_CTX *ctx)
  896. -{
  897. - struct digest_ctx *digest_ctx =
  898. - (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  899. -
  900. - if (digest_ctx == NULL)
  901. - return 1;
  902. -
  903. - return clean_devcrypto_session(&digest_ctx->sess);
  904. -}
  905. -
  906. -/*
  907. - * Keep tables of known nids, associated methods, selected digests, and
  908. - * driver info.
  909. - * Note that known_digest_nids[] isn't necessarily indexed the same way as
  910. - * digest_data[] above, which the other tables are.
  911. - */
  912. -static int known_digest_nids[OSSL_NELEM(digest_data)];
  913. -static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
  914. -static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
  915. -static int selected_digests[OSSL_NELEM(digest_data)];
  916. -static struct driver_info_st digest_driver_info[OSSL_NELEM(digest_data)];
  917. -
  918. -static int devcrypto_test_digest(size_t digest_data_index)
  919. -{
  920. - return (digest_driver_info[digest_data_index].status == DEVCRYPTO_STATUS_USABLE
  921. - && selected_digests[digest_data_index] == 1
  922. - && (digest_driver_info[digest_data_index].accelerated
  923. - == DEVCRYPTO_ACCELERATED
  924. - || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
  925. - || (digest_driver_info[digest_data_index].accelerated
  926. - != DEVCRYPTO_NOT_ACCELERATED
  927. - && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
  928. -}
  929. -
  930. -static void rebuild_known_digest_nids(ENGINE *e)
  931. -{
  932. - size_t i;
  933. -
  934. - for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); i++) {
  935. - if (devcrypto_test_digest(i))
  936. - known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
  937. - }
  938. - ENGINE_unregister_digests(e);
  939. - ENGINE_register_digests(e);
  940. -}
  941. -
  942. -static void prepare_digest_methods(void)
  943. -{
  944. - size_t i;
  945. - struct session_op sess1, sess2;
  946. -#ifdef CIOCGSESSINFO
  947. - struct session_info_op siop;
  948. -#endif
  949. - struct cphash_op cphash;
  950. -
  951. - memset(&digest_driver_info, 0, sizeof(digest_driver_info));
  952. -
  953. - memset(&sess1, 0, sizeof(sess1));
  954. - memset(&sess2, 0, sizeof(sess2));
  955. -
  956. - for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
  957. - i++) {
  958. -
  959. - selected_digests[i] = 1;
  960. -
  961. - /*
  962. - * Check that the digest is usable
  963. - */
  964. - sess1.mac = digest_data[i].devcryptoid;
  965. - sess2.ses = 0;
  966. - if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) {
  967. - digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
  968. - goto finish;
  969. - }
  970. -
  971. -#ifdef CIOCGSESSINFO
  972. - /* gather hardware acceleration info from the driver */
  973. - siop.ses = sess1.ses;
  974. - if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
  975. - digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
  976. - } else {
  977. - digest_driver_info[i].driver_name =
  978. - OPENSSL_strndup(siop.hash_info.cra_driver_name,
  979. - CRYPTODEV_MAX_ALG_NAME);
  980. - if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
  981. - digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
  982. - else
  983. - digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
  984. - }
  985. -#endif
  986. -
  987. - /* digest must be capable of hash state copy */
  988. - sess2.mac = sess1.mac;
  989. - if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) {
  990. - digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
  991. - goto finish;
  992. - }
  993. - cphash.src_ses = sess1.ses;
  994. - cphash.dst_ses = sess2.ses;
  995. - if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
  996. - digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCCPHASH;
  997. - goto finish;
  998. - }
  999. - if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
  1000. - NID_undef)) == NULL
  1001. - || !EVP_MD_meth_set_input_blocksize(known_digest_methods[i],
  1002. - digest_data[i].blocksize)
  1003. - || !EVP_MD_meth_set_result_size(known_digest_methods[i],
  1004. - digest_data[i].digestlen)
  1005. - || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
  1006. - || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
  1007. - || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
  1008. - || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
  1009. - || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
  1010. - || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
  1011. - sizeof(struct digest_ctx))) {
  1012. - digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
  1013. - EVP_MD_meth_free(known_digest_methods[i]);
  1014. - known_digest_methods[i] = NULL;
  1015. - goto finish;
  1016. - }
  1017. - digest_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
  1018. -finish:
  1019. - ioctl(cfd, CIOCFSESSION, &sess1.ses);
  1020. - if (sess2.ses != 0)
  1021. - ioctl(cfd, CIOCFSESSION, &sess2.ses);
  1022. - if (devcrypto_test_digest(i))
  1023. - known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
  1024. - }
  1025. -}
  1026. -
  1027. -static const EVP_MD *get_digest_method(int nid)
  1028. -{
  1029. - size_t i = get_digest_data_index(nid);
  1030. -
  1031. - if (i == (size_t)-1)
  1032. - return NULL;
  1033. - return known_digest_methods[i];
  1034. -}
  1035. -
  1036. -static int get_digest_nids(const int **nids)
  1037. -{
  1038. - *nids = known_digest_nids;
  1039. - return known_digest_nids_amount;
  1040. -}
  1041. -
  1042. -static void destroy_digest_method(int nid)
  1043. -{
  1044. - size_t i = get_digest_data_index(nid);
  1045. -
  1046. - EVP_MD_meth_free(known_digest_methods[i]);
  1047. - known_digest_methods[i] = NULL;
  1048. -}
  1049. -
  1050. -static void destroy_all_digest_methods(void)
  1051. -{
  1052. - size_t i;
  1053. -
  1054. - for (i = 0; i < OSSL_NELEM(digest_data); i++) {
  1055. - destroy_digest_method(digest_data[i].nid);
  1056. - OPENSSL_free(digest_driver_info[i].driver_name);
  1057. - digest_driver_info[i].driver_name = NULL;
  1058. - }
  1059. -}
  1060. -
  1061. -static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
  1062. - const int **nids, int nid)
  1063. -{
  1064. - if (digest == NULL)
  1065. - return get_digest_nids(nids);
  1066. -
  1067. - *digest = get_digest_method(nid);
  1068. -
  1069. - return *digest != NULL;
  1070. -}
  1071. -
  1072. -static void devcrypto_select_all_digests(int *digest_list)
  1073. -{
  1074. - size_t i;
  1075. -
  1076. - for (i = 0; i < OSSL_NELEM(digest_data); i++)
  1077. - digest_list[i] = 1;
  1078. -}
  1079. -
  1080. -static int cryptodev_select_digest_cb(const char *str, int len, void *usr)
  1081. -{
  1082. - int *digest_list = (int *)usr;
  1083. - char *name;
  1084. - const EVP_MD *EVP;
  1085. - size_t i;
  1086. -
  1087. - if (len == 0)
  1088. - return 1;
  1089. - if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
  1090. - return 0;
  1091. - EVP = EVP_get_digestbyname(name);
  1092. - if (EVP == NULL)
  1093. - fprintf(stderr, "devcrypto: unknown digest %s\n", name);
  1094. - else if ((i = find_digest_data_index(EVP_MD_type(EVP))) != (size_t)-1)
  1095. - digest_list[i] = 1;
  1096. - else
  1097. - fprintf(stderr, "devcrypto: digest %s not available\n", name);
  1098. - OPENSSL_free(name);
  1099. - return 1;
  1100. -}
  1101. -
  1102. -static void dump_digest_info(void)
  1103. -{
  1104. - size_t i;
  1105. - const char *name;
  1106. -
  1107. - fprintf (stderr, "Information about digests supported by the /dev/crypto"
  1108. - " engine:\n");
  1109. -#ifndef CIOCGSESSINFO
  1110. - fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
  1111. -#endif
  1112. -
  1113. - for (i = 0; i < OSSL_NELEM(digest_data); i++) {
  1114. - name = OBJ_nid2sn(digest_data[i].nid);
  1115. - fprintf (stderr, "Digest %s, NID=%d, /dev/crypto info: id=%d, driver=%s",
  1116. - name ? name : "unknown", digest_data[i].nid,
  1117. - digest_data[i].devcryptoid,
  1118. - digest_driver_info[i].driver_name ? digest_driver_info[i].driver_name : "unknown");
  1119. - if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) {
  1120. - fprintf (stderr, ". CIOCGSESSION (session open) failed\n");
  1121. - continue;
  1122. - }
  1123. - if (digest_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
  1124. - fprintf(stderr, " (hw accelerated)");
  1125. - else if (digest_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
  1126. - fprintf(stderr, " (software)");
  1127. - else
  1128. - fprintf(stderr, " (acceleration status unknown)");
  1129. - if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
  1130. - fprintf (stderr, ". Cipher setup failed\n");
  1131. - else if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCCPHASH)
  1132. - fprintf(stderr, ", CIOCCPHASH failed\n");
  1133. - else
  1134. - fprintf(stderr, ", CIOCCPHASH capable\n");
  1135. - }
  1136. - fprintf(stderr, "\n");
  1137. -}
  1138. -
  1139. -#endif
  1140. -
  1141. -/******************************************************************************
  1142. - *
  1143. - * CONTROL COMMANDS
  1144. - *
  1145. - *****/
  1146. -
  1147. -#define DEVCRYPTO_CMD_USE_SOFTDRIVERS ENGINE_CMD_BASE
  1148. -#define DEVCRYPTO_CMD_CIPHERS (ENGINE_CMD_BASE + 1)
  1149. -#define DEVCRYPTO_CMD_DIGESTS (ENGINE_CMD_BASE + 2)
  1150. -#define DEVCRYPTO_CMD_DUMP_INFO (ENGINE_CMD_BASE + 3)
  1151. -
  1152. -/* Helper macros for CPP string composition */
  1153. -#ifndef OPENSSL_MSTR
  1154. -# define OPENSSL_MSTR_HELPER(x) #x
  1155. -# define OPENSSL_MSTR(x) OPENSSL_MSTR_HELPER(x)
  1156. -#endif
  1157. -
  1158. -static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
  1159. -#ifdef CIOCGSESSINFO
  1160. - {DEVCRYPTO_CMD_USE_SOFTDRIVERS,
  1161. - "USE_SOFTDRIVERS",
  1162. - "specifies whether to use software (not accelerated) drivers ("
  1163. - OPENSSL_MSTR(DEVCRYPTO_REQUIRE_ACCELERATED) "=use only accelerated drivers, "
  1164. - OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
  1165. - OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
  1166. - "=use if acceleration can't be determined) [default="
  1167. - OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFDTRIVERS) "]",
  1168. - ENGINE_CMD_FLAG_NUMERIC},
  1169. -#endif
  1170. -
  1171. - {DEVCRYPTO_CMD_CIPHERS,
  1172. - "CIPHERS",
  1173. - "either ALL, NONE, or a comma-separated list of ciphers to enable [default=ALL]",
  1174. - ENGINE_CMD_FLAG_STRING},
  1175. -
  1176. -#ifdef IMPLEMENT_DIGEST
  1177. - {DEVCRYPTO_CMD_DIGESTS,
  1178. - "DIGESTS",
  1179. - "either ALL, NONE, or a comma-separated list of digests to enable [default=ALL]",
  1180. - ENGINE_CMD_FLAG_STRING},
  1181. -#endif
  1182. -
  1183. - {DEVCRYPTO_CMD_DUMP_INFO,
  1184. - "DUMP_INFO",
  1185. - "dump info about each algorithm to stderr; use 'openssl engine -pre DUMP_INFO devcrypto'",
  1186. - ENGINE_CMD_FLAG_NO_INPUT},
  1187. -
  1188. - {0, NULL, NULL, 0}
  1189. -};
  1190. -
  1191. -static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  1192. -{
  1193. - int *new_list;
  1194. - switch (cmd) {
  1195. -#ifdef CIOCGSESSINFO
  1196. - case DEVCRYPTO_CMD_USE_SOFTDRIVERS:
  1197. - switch (i) {
  1198. - case DEVCRYPTO_REQUIRE_ACCELERATED:
  1199. - case DEVCRYPTO_USE_SOFTWARE:
  1200. - case DEVCRYPTO_REJECT_SOFTWARE:
  1201. - break;
  1202. - default:
  1203. - fprintf(stderr, "devcrypto: invalid value (%ld) for USE_SOFTDRIVERS\n", i);
  1204. - return 0;
  1205. - }
  1206. - if (use_softdrivers == i)
  1207. - return 1;
  1208. - use_softdrivers = i;
  1209. -#ifdef IMPLEMENT_DIGEST
  1210. - rebuild_known_digest_nids(e);
  1211. -#endif
  1212. - rebuild_known_cipher_nids(e);
  1213. - return 1;
  1214. -#endif /* CIOCGSESSINFO */
  1215. -
  1216. - case DEVCRYPTO_CMD_CIPHERS:
  1217. - if (p == NULL)
  1218. - return 1;
  1219. - if (strcasecmp((const char *)p, "ALL") == 0) {
  1220. - devcrypto_select_all_ciphers(selected_ciphers);
  1221. - } else if (strcasecmp((const char*)p, "NONE") == 0) {
  1222. - memset(selected_ciphers, 0, sizeof(selected_ciphers));
  1223. - } else {
  1224. - new_list=OPENSSL_zalloc(sizeof(selected_ciphers));
  1225. - if (!CONF_parse_list(p, ',', 1, cryptodev_select_cipher_cb, new_list)) {
  1226. - OPENSSL_free(new_list);
  1227. - return 0;
  1228. - }
  1229. - memcpy(selected_ciphers, new_list, sizeof(selected_ciphers));
  1230. - OPENSSL_free(new_list);
  1231. - }
  1232. - rebuild_known_cipher_nids(e);
  1233. - return 1;
  1234. -
  1235. -#ifdef IMPLEMENT_DIGEST
  1236. - case DEVCRYPTO_CMD_DIGESTS:
  1237. - if (p == NULL)
  1238. - return 1;
  1239. - if (strcasecmp((const char *)p, "ALL") == 0) {
  1240. - devcrypto_select_all_digests(selected_digests);
  1241. - } else if (strcasecmp((const char*)p, "NONE") == 0) {
  1242. - memset(selected_digests, 0, sizeof(selected_digests));
  1243. - } else {
  1244. - new_list=OPENSSL_zalloc(sizeof(selected_digests));
  1245. - if (!CONF_parse_list(p, ',', 1, cryptodev_select_digest_cb, new_list)) {
  1246. - OPENSSL_free(new_list);
  1247. - return 0;
  1248. - }
  1249. - memcpy(selected_digests, new_list, sizeof(selected_digests));
  1250. - OPENSSL_free(new_list);
  1251. - }
  1252. - rebuild_known_digest_nids(e);
  1253. - return 1;
  1254. -#endif /* IMPLEMENT_DIGEST */
  1255. -
  1256. - case DEVCRYPTO_CMD_DUMP_INFO:
  1257. - dump_cipher_info();
  1258. -#ifdef IMPLEMENT_DIGEST
  1259. - dump_digest_info();
  1260. -#endif
  1261. - return 1;
  1262. -
  1263. - default:
  1264. - break;
  1265. - }
  1266. - return 0;
  1267. -}
  1268. -
  1269. -/******************************************************************************
  1270. - *
  1271. - * LOAD / UNLOAD
  1272. - *
  1273. - *****/
  1274. -
  1275. -static int devcrypto_unload(ENGINE *e)
  1276. -{
  1277. - destroy_all_cipher_methods();
  1278. -#ifdef IMPLEMENT_DIGEST
  1279. - destroy_all_digest_methods();
  1280. -#endif
  1281. -
  1282. - close(cfd);
  1283. -
  1284. - return 1;
  1285. -}
  1286. -/*
  1287. - * This engine is always built into libcrypto, so it doesn't offer any
  1288. - * ability to be dynamically loadable.
  1289. - */
  1290. -void engine_load_devcrypto_int()
  1291. -{
  1292. - ENGINE *e = NULL;
  1293. -
  1294. - if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
  1295. -#ifndef ENGINE_DEVCRYPTO_DEBUG
  1296. - if (errno != ENOENT)
  1297. -#endif
  1298. - fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
  1299. - return;
  1300. - }
  1301. -
  1302. - if ((e = ENGINE_new()) == NULL
  1303. - || !ENGINE_set_destroy_function(e, devcrypto_unload)) {
  1304. - ENGINE_free(e);
  1305. - /*
  1306. - * We know that devcrypto_unload() won't be called when one of the
  1307. - * above two calls have failed, so we close cfd explicitly here to
  1308. - * avoid leaking resources.
  1309. - */
  1310. - close(cfd);
  1311. - return;
  1312. - }
  1313. -
  1314. - prepare_cipher_methods();
  1315. -#ifdef IMPLEMENT_DIGEST
  1316. - prepare_digest_methods();
  1317. -#endif
  1318. -
  1319. - if (!ENGINE_set_id(e, "devcrypto")
  1320. - || !ENGINE_set_name(e, "/dev/crypto engine")
  1321. - || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
  1322. - || !ENGINE_set_ctrl_function(e, devcrypto_ctrl)
  1323. -
  1324. -/*
  1325. - * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
  1326. - * implementations, it seems to only exist in FreeBSD, and regarding the
  1327. - * parameters in its crypt_kop, the manual crypto(4) has this to say:
  1328. - *
  1329. - * The semantics of these arguments are currently undocumented.
  1330. - *
  1331. - * Reading through the FreeBSD source code doesn't give much more than
  1332. - * their CRK_MOD_EXP implementation for ubsec.
  1333. - *
  1334. - * It doesn't look much better with cryptodev-linux. They have the crypt_kop
  1335. - * structure as well as the command (CRK_*) in cryptodev.h, but no support
  1336. - * seems to be implemented at all for the moment.
  1337. - *
  1338. - * At the time of writing, it seems impossible to write proper support for
  1339. - * FreeBSD's asym features without some very deep knowledge and access to
  1340. - * specific kernel modules.
  1341. - *
  1342. - * /Richard Levitte, 2017-05-11
  1343. - */
  1344. -#if 0
  1345. -# ifndef OPENSSL_NO_RSA
  1346. - || !ENGINE_set_RSA(e, devcrypto_rsa)
  1347. -# endif
  1348. -# ifndef OPENSSL_NO_DSA
  1349. - || !ENGINE_set_DSA(e, devcrypto_dsa)
  1350. -# endif
  1351. -# ifndef OPENSSL_NO_DH
  1352. - || !ENGINE_set_DH(e, devcrypto_dh)
  1353. -# endif
  1354. -# ifndef OPENSSL_NO_EC
  1355. - || !ENGINE_set_EC(e, devcrypto_ec)
  1356. -# endif
  1357. -#endif
  1358. - || !ENGINE_set_ciphers(e, devcrypto_ciphers)
  1359. -#ifdef IMPLEMENT_DIGEST
  1360. - || !ENGINE_set_digests(e, devcrypto_digests)
  1361. -#endif
  1362. - ) {
  1363. - ENGINE_free(e);
  1364. - return;
  1365. - }
  1366. -
  1367. - ENGINE_add(e);
  1368. - ENGINE_free(e); /* Loose our local reference */
  1369. - ERR_clear_error();
  1370. -}
  1371. --- /dev/null
  1372. +++ b/engines/e_devcrypto.c
  1373. @@ -0,0 +1,1315 @@
  1374. +/*
  1375. + * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
  1376. + *
  1377. + * Licensed under the OpenSSL license (the "License"). You may not use
  1378. + * this file except in compliance with the License. You can obtain a copy
  1379. + * in the file LICENSE in the source distribution or at
  1380. + * https://www.openssl.org/source/license.html
  1381. + */
  1382. +
  1383. +#include "../e_os.h"
  1384. +#include <string.h>
  1385. +#include <sys/types.h>
  1386. +#include <sys/stat.h>
  1387. +#include <fcntl.h>
  1388. +#include <sys/ioctl.h>
  1389. +#include <unistd.h>
  1390. +#include <assert.h>
  1391. +
  1392. +#include <openssl/conf.h>
  1393. +#include <openssl/evp.h>
  1394. +#include <openssl/err.h>
  1395. +#include <openssl/engine.h>
  1396. +#include <openssl/objects.h>
  1397. +#include <crypto/cryptodev.h>
  1398. +
  1399. +/* #define ENGINE_DEVCRYPTO_DEBUG */
  1400. +
  1401. +#ifdef CRYPTO_ALGORITHM_MIN
  1402. +# define CHECK_BSD_STYLE_MACROS
  1403. +#endif
  1404. +
  1405. +#define engine_devcrypto_id "devcrypto"
  1406. +
  1407. +/*
  1408. + * ONE global file descriptor for all sessions. This allows operations
  1409. + * such as digest session data copying (see digest_copy()), but is also
  1410. + * saner... why re-open /dev/crypto for every session?
  1411. + */
  1412. +static int cfd = -1;
  1413. +#define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */
  1414. +#define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */
  1415. +#define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */
  1416. +
  1417. +#define DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS DEVCRYPTO_REJECT_SOFTWARE
  1418. +static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS;
  1419. +
  1420. +/*
  1421. + * cipher/digest status & acceleration definitions
  1422. + * Make sure the defaults are set to 0
  1423. + */
  1424. +struct driver_info_st {
  1425. + enum devcrypto_status_t {
  1426. + DEVCRYPTO_STATUS_FAILURE = -3, /* unusable for other reason */
  1427. + DEVCRYPTO_STATUS_NO_CIOCCPHASH = -2, /* hash state copy not supported */
  1428. + DEVCRYPTO_STATUS_NO_CIOCGSESSION = -1, /* session open failed */
  1429. + DEVCRYPTO_STATUS_UNKNOWN = 0, /* not tested yet */
  1430. + DEVCRYPTO_STATUS_USABLE = 1 /* algo can be used */
  1431. + } status;
  1432. +
  1433. + enum devcrypto_accelerated_t {
  1434. + DEVCRYPTO_NOT_ACCELERATED = -1, /* software implemented */
  1435. + DEVCRYPTO_ACCELERATION_UNKNOWN = 0, /* acceleration support unkown */
  1436. + DEVCRYPTO_ACCELERATED = 1 /* hardware accelerated */
  1437. + } accelerated;
  1438. +
  1439. + char *driver_name;
  1440. +};
  1441. +
  1442. +#ifdef OPENSSL_NO_DYNAMIC_ENGINE
  1443. +void engine_load_devcrypto_int(void);
  1444. +#endif
  1445. +
  1446. +static int clean_devcrypto_session(struct session_op *sess) {
  1447. + if (ioctl(cfd, CIOCFSESSION, &sess->ses) < 0) {
  1448. + SYSerr(SYS_F_IOCTL, errno);
  1449. + return 0;
  1450. + }
  1451. + memset(sess, 0, sizeof(struct session_op));
  1452. + return 1;
  1453. +}
  1454. +
  1455. +/******************************************************************************
  1456. + *
  1457. + * Ciphers
  1458. + *
  1459. + * Because they all do the same basic operation, we have only one set of
  1460. + * method functions for them all to share, and a mapping table between
  1461. + * NIDs and cryptodev IDs, with all the necessary size data.
  1462. + *
  1463. + *****/
  1464. +
  1465. +struct cipher_ctx {
  1466. + struct session_op sess;
  1467. + int op; /* COP_ENCRYPT or COP_DECRYPT */
  1468. + unsigned long mode; /* EVP_CIPH_*_MODE */
  1469. +
  1470. + /* to handle ctr mode being a stream cipher */
  1471. + unsigned char partial[EVP_MAX_BLOCK_LENGTH];
  1472. + unsigned int blocksize, num;
  1473. +};
  1474. +
  1475. +static const struct cipher_data_st {
  1476. + int nid;
  1477. + int blocksize;
  1478. + int keylen;
  1479. + int ivlen;
  1480. + int flags;
  1481. + int devcryptoid;
  1482. +} cipher_data[] = {
  1483. +#ifndef OPENSSL_NO_DES
  1484. + { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC },
  1485. + { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC },
  1486. +#endif
  1487. +#ifndef OPENSSL_NO_BF
  1488. + { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC },
  1489. +#endif
  1490. +#ifndef OPENSSL_NO_CAST
  1491. + { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC },
  1492. +#endif
  1493. + { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  1494. + { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  1495. + { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC },
  1496. +#ifndef OPENSSL_NO_RC4
  1497. + { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 },
  1498. +#endif
  1499. +#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR)
  1500. + { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  1501. + { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  1502. + { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR },
  1503. +#endif
  1504. +#if 0 /* Not yet supported */
  1505. + { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
  1506. + { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS },
  1507. +#endif
  1508. +#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB)
  1509. + { NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  1510. + { NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  1511. + { NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB },
  1512. +#endif
  1513. +#if 0 /* Not yet supported */
  1514. + { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  1515. + { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  1516. + { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM },
  1517. +#endif
  1518. +#ifndef OPENSSL_NO_CAMELLIA
  1519. + { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE,
  1520. + CRYPTO_CAMELLIA_CBC },
  1521. + { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE,
  1522. + CRYPTO_CAMELLIA_CBC },
  1523. + { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE,
  1524. + CRYPTO_CAMELLIA_CBC },
  1525. +#endif
  1526. +};
  1527. +
  1528. +static size_t find_cipher_data_index(int nid)
  1529. +{
  1530. + size_t i;
  1531. +
  1532. + for (i = 0; i < OSSL_NELEM(cipher_data); i++)
  1533. + if (nid == cipher_data[i].nid)
  1534. + return i;
  1535. + return (size_t)-1;
  1536. +}
  1537. +
  1538. +static size_t get_cipher_data_index(int nid)
  1539. +{
  1540. + size_t i = find_cipher_data_index(nid);
  1541. +
  1542. + if (i != (size_t)-1)
  1543. + return i;
  1544. +
  1545. + /*
  1546. + * Code further down must make sure that only NIDs in the table above
  1547. + * are used. If any other NID reaches this function, there's a grave
  1548. + * coding error further down.
  1549. + */
  1550. + assert("Code that never should be reached" == NULL);
  1551. + return -1;
  1552. +}
  1553. +
  1554. +static const struct cipher_data_st *get_cipher_data(int nid)
  1555. +{
  1556. + return &cipher_data[get_cipher_data_index(nid)];
  1557. +}
  1558. +
  1559. +/*
  1560. + * Following are the three necessary functions to map OpenSSL functionality
  1561. + * with cryptodev.
  1562. + */
  1563. +
  1564. +static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  1565. + const unsigned char *iv, int enc)
  1566. +{
  1567. + struct cipher_ctx *cipher_ctx =
  1568. + (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  1569. + const struct cipher_data_st *cipher_d =
  1570. + get_cipher_data(EVP_CIPHER_CTX_nid(ctx));
  1571. +
  1572. + /* cleanup a previous session */
  1573. + if (cipher_ctx->sess.ses != 0 &&
  1574. + clean_devcrypto_session(&cipher_ctx->sess) == 0)
  1575. + return 0;
  1576. +
  1577. + cipher_ctx->sess.cipher = cipher_d->devcryptoid;
  1578. + cipher_ctx->sess.keylen = cipher_d->keylen;
  1579. + cipher_ctx->sess.key = (void *)key;
  1580. + cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT;
  1581. + cipher_ctx->mode = cipher_d->flags & EVP_CIPH_MODE;
  1582. + cipher_ctx->blocksize = cipher_d->blocksize;
  1583. + if (ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess) < 0) {
  1584. + SYSerr(SYS_F_IOCTL, errno);
  1585. + return 0;
  1586. + }
  1587. +
  1588. + return 1;
  1589. +}
  1590. +
  1591. +static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1592. + const unsigned char *in, size_t inl)
  1593. +{
  1594. + struct cipher_ctx *cipher_ctx =
  1595. + (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  1596. + struct crypt_op cryp;
  1597. + unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
  1598. +#if !defined(COP_FLAG_WRITE_IV)
  1599. + unsigned char saved_iv[EVP_MAX_IV_LENGTH];
  1600. + const unsigned char *ivptr;
  1601. + size_t nblocks, ivlen;
  1602. +#endif
  1603. +
  1604. + memset(&cryp, 0, sizeof(cryp));
  1605. + cryp.ses = cipher_ctx->sess.ses;
  1606. + cryp.len = inl;
  1607. + cryp.src = (void *)in;
  1608. + cryp.dst = (void *)out;
  1609. + cryp.iv = (void *)iv;
  1610. + cryp.op = cipher_ctx->op;
  1611. +#if !defined(COP_FLAG_WRITE_IV)
  1612. + cryp.flags = 0;
  1613. +
  1614. + ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  1615. + if (ivlen > 0)
  1616. + switch (cipher_ctx->mode) {
  1617. + case EVP_CIPH_CBC_MODE:
  1618. + assert(inl >= ivlen);
  1619. + if (!EVP_CIPHER_CTX_encrypting(ctx)) {
  1620. + ivptr = in + inl - ivlen;
  1621. + memcpy(saved_iv, ivptr, ivlen);
  1622. + }
  1623. + break;
  1624. +
  1625. + case EVP_CIPH_CTR_MODE:
  1626. + break;
  1627. +
  1628. + default: /* should not happen */
  1629. + return 0;
  1630. + }
  1631. +#else
  1632. + cryp.flags = COP_FLAG_WRITE_IV;
  1633. +#endif
  1634. +
  1635. + if (ioctl(cfd, CIOCCRYPT, &cryp) < 0) {
  1636. + SYSerr(SYS_F_IOCTL, errno);
  1637. + return 0;
  1638. + }
  1639. +
  1640. +#if !defined(COP_FLAG_WRITE_IV)
  1641. + if (ivlen > 0)
  1642. + switch (cipher_ctx->mode) {
  1643. + case EVP_CIPH_CBC_MODE:
  1644. + assert(inl >= ivlen);
  1645. + if (EVP_CIPHER_CTX_encrypting(ctx))
  1646. + ivptr = out + inl - ivlen;
  1647. + else
  1648. + ivptr = saved_iv;
  1649. +
  1650. + memcpy(iv, ivptr, ivlen);
  1651. + break;
  1652. +
  1653. + case EVP_CIPH_CTR_MODE:
  1654. + nblocks = (inl + cipher_ctx->blocksize - 1)
  1655. + / cipher_ctx->blocksize;
  1656. + do {
  1657. + ivlen--;
  1658. + nblocks += iv[ivlen];
  1659. + iv[ivlen] = (uint8_t) nblocks;
  1660. + nblocks >>= 8;
  1661. + } while (ivlen);
  1662. + break;
  1663. +
  1664. + default: /* should not happen */
  1665. + return 0;
  1666. + }
  1667. +#endif
  1668. +
  1669. + return 1;
  1670. +}
  1671. +
  1672. +static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1673. + const unsigned char *in, size_t inl)
  1674. +{
  1675. + struct cipher_ctx *cipher_ctx =
  1676. + (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  1677. + size_t nblocks, len;
  1678. +
  1679. + /* initial partial block */
  1680. + while (cipher_ctx->num && inl) {
  1681. + (*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num];
  1682. + --inl;
  1683. + cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize;
  1684. + }
  1685. +
  1686. + /* full blocks */
  1687. + if (inl > (unsigned int) cipher_ctx->blocksize) {
  1688. + nblocks = inl/cipher_ctx->blocksize;
  1689. + len = nblocks * cipher_ctx->blocksize;
  1690. + if (cipher_do_cipher(ctx, out, in, len) < 1)
  1691. + return 0;
  1692. + inl -= len;
  1693. + out += len;
  1694. + in += len;
  1695. + }
  1696. +
  1697. + /* final partial block */
  1698. + if (inl) {
  1699. + memset(cipher_ctx->partial, 0, cipher_ctx->blocksize);
  1700. + if (cipher_do_cipher(ctx, cipher_ctx->partial, cipher_ctx->partial,
  1701. + cipher_ctx->blocksize) < 1)
  1702. + return 0;
  1703. + while (inl--) {
  1704. + out[cipher_ctx->num] = in[cipher_ctx->num]
  1705. + ^ cipher_ctx->partial[cipher_ctx->num];
  1706. + cipher_ctx->num++;
  1707. + }
  1708. + }
  1709. +
  1710. + return 1;
  1711. +}
  1712. +
  1713. +static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2)
  1714. +{
  1715. + struct cipher_ctx *cipher_ctx =
  1716. + (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  1717. + EVP_CIPHER_CTX *to_ctx = (EVP_CIPHER_CTX *)p2;
  1718. + struct cipher_ctx *to_cipher_ctx;
  1719. +
  1720. + switch (type) {
  1721. +
  1722. + case EVP_CTRL_COPY:
  1723. + if (cipher_ctx == NULL)
  1724. + return 1;
  1725. + /* when copying the context, a new session needs to be initialized */
  1726. + to_cipher_ctx =
  1727. + (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(to_ctx);
  1728. + memset(&to_cipher_ctx->sess, 0, sizeof(to_cipher_ctx->sess));
  1729. + return cipher_init(to_ctx, cipher_ctx->sess.key, EVP_CIPHER_CTX_iv(ctx),
  1730. + (cipher_ctx->op == COP_ENCRYPT));
  1731. +
  1732. + case EVP_CTRL_INIT:
  1733. + memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess));
  1734. + return 1;
  1735. +
  1736. + default:
  1737. + break;
  1738. + }
  1739. +
  1740. + return -1;
  1741. +}
  1742. +
  1743. +static int cipher_cleanup(EVP_CIPHER_CTX *ctx)
  1744. +{
  1745. + struct cipher_ctx *cipher_ctx =
  1746. + (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  1747. +
  1748. + return clean_devcrypto_session(&cipher_ctx->sess);
  1749. +}
  1750. +
  1751. +/*
  1752. + * Keep tables of known nids, associated methods, selected ciphers, and driver
  1753. + * info.
  1754. + * Note that known_cipher_nids[] isn't necessarily indexed the same way as
  1755. + * cipher_data[] above, which the other tables are.
  1756. + */
  1757. +static int known_cipher_nids[OSSL_NELEM(cipher_data)];
  1758. +static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */
  1759. +static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, };
  1760. +static int selected_ciphers[OSSL_NELEM(cipher_data)];
  1761. +static struct driver_info_st cipher_driver_info[OSSL_NELEM(cipher_data)];
  1762. +
  1763. +
  1764. +static int devcrypto_test_cipher(size_t cipher_data_index)
  1765. +{
  1766. + return (cipher_driver_info[cipher_data_index].status == DEVCRYPTO_STATUS_USABLE
  1767. + && selected_ciphers[cipher_data_index] == 1
  1768. + && (cipher_driver_info[cipher_data_index].accelerated
  1769. + == DEVCRYPTO_ACCELERATED
  1770. + || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
  1771. + || (cipher_driver_info[cipher_data_index].accelerated
  1772. + != DEVCRYPTO_NOT_ACCELERATED
  1773. + && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
  1774. +}
  1775. +
  1776. +static void prepare_cipher_methods(void)
  1777. +{
  1778. + size_t i;
  1779. + struct session_op sess;
  1780. + unsigned long cipher_mode;
  1781. +#ifdef CIOCGSESSINFO
  1782. + struct session_info_op siop;
  1783. +#endif
  1784. +
  1785. + memset(&cipher_driver_info, 0, sizeof(cipher_driver_info));
  1786. +
  1787. + memset(&sess, 0, sizeof(sess));
  1788. + sess.key = (void *)"01234567890123456789012345678901234567890123456789";
  1789. +
  1790. + for (i = 0, known_cipher_nids_amount = 0;
  1791. + i < OSSL_NELEM(cipher_data); i++) {
  1792. +
  1793. + selected_ciphers[i] = 1;
  1794. + /*
  1795. + * Check that the cipher is usable
  1796. + */
  1797. + sess.cipher = cipher_data[i].devcryptoid;
  1798. + sess.keylen = cipher_data[i].keylen;
  1799. + if (ioctl(cfd, CIOCGSESSION, &sess) < 0) {
  1800. + cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
  1801. + continue;
  1802. + }
  1803. +
  1804. + cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE;
  1805. +
  1806. + if ((known_cipher_methods[i] =
  1807. + EVP_CIPHER_meth_new(cipher_data[i].nid,
  1808. + cipher_mode == EVP_CIPH_CTR_MODE ? 1 :
  1809. + cipher_data[i].blocksize,
  1810. + cipher_data[i].keylen)) == NULL
  1811. + || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i],
  1812. + cipher_data[i].ivlen)
  1813. + || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i],
  1814. + cipher_data[i].flags
  1815. + | EVP_CIPH_CUSTOM_COPY
  1816. + | EVP_CIPH_CTRL_INIT
  1817. + | EVP_CIPH_FLAG_DEFAULT_ASN1)
  1818. + || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init)
  1819. + || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i],
  1820. + cipher_mode == EVP_CIPH_CTR_MODE ?
  1821. + ctr_do_cipher :
  1822. + cipher_do_cipher)
  1823. + || !EVP_CIPHER_meth_set_ctrl(known_cipher_methods[i], cipher_ctrl)
  1824. + || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i],
  1825. + cipher_cleanup)
  1826. + || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i],
  1827. + sizeof(struct cipher_ctx))) {
  1828. + cipher_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
  1829. + EVP_CIPHER_meth_free(known_cipher_methods[i]);
  1830. + known_cipher_methods[i] = NULL;
  1831. + } else {
  1832. + cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
  1833. +#ifdef CIOCGSESSINFO
  1834. + siop.ses = sess.ses;
  1835. + if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
  1836. + cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
  1837. + } else {
  1838. + cipher_driver_info[i].driver_name =
  1839. + OPENSSL_strndup(siop.cipher_info.cra_driver_name,
  1840. + CRYPTODEV_MAX_ALG_NAME);
  1841. + if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY))
  1842. + cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
  1843. + else
  1844. + cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
  1845. + }
  1846. +#endif /* CIOCGSESSINFO */
  1847. + }
  1848. + ioctl(cfd, CIOCFSESSION, &sess.ses);
  1849. + if (devcrypto_test_cipher(i)) {
  1850. + known_cipher_nids[known_cipher_nids_amount++] =
  1851. + cipher_data[i].nid;
  1852. + }
  1853. + }
  1854. +}
  1855. +
  1856. +static void rebuild_known_cipher_nids(ENGINE *e)
  1857. +{
  1858. + size_t i;
  1859. +
  1860. + for (i = 0, known_cipher_nids_amount = 0; i < OSSL_NELEM(cipher_data); i++) {
  1861. + if (devcrypto_test_cipher(i))
  1862. + known_cipher_nids[known_cipher_nids_amount++] = cipher_data[i].nid;
  1863. + }
  1864. + ENGINE_unregister_ciphers(e);
  1865. + ENGINE_register_ciphers(e);
  1866. +}
  1867. +
  1868. +static const EVP_CIPHER *get_cipher_method(int nid)
  1869. +{
  1870. + size_t i = get_cipher_data_index(nid);
  1871. +
  1872. + if (i == (size_t)-1)
  1873. + return NULL;
  1874. + return known_cipher_methods[i];
  1875. +}
  1876. +
  1877. +static int get_cipher_nids(const int **nids)
  1878. +{
  1879. + *nids = known_cipher_nids;
  1880. + return known_cipher_nids_amount;
  1881. +}
  1882. +
  1883. +static void destroy_cipher_method(int nid)
  1884. +{
  1885. + size_t i = get_cipher_data_index(nid);
  1886. +
  1887. + EVP_CIPHER_meth_free(known_cipher_methods[i]);
  1888. + known_cipher_methods[i] = NULL;
  1889. +}
  1890. +
  1891. +static void destroy_all_cipher_methods(void)
  1892. +{
  1893. + size_t i;
  1894. +
  1895. + for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
  1896. + destroy_cipher_method(cipher_data[i].nid);
  1897. + OPENSSL_free(cipher_driver_info[i].driver_name);
  1898. + cipher_driver_info[i].driver_name = NULL;
  1899. + }
  1900. +}
  1901. +
  1902. +static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  1903. + const int **nids, int nid)
  1904. +{
  1905. + if (cipher == NULL)
  1906. + return get_cipher_nids(nids);
  1907. +
  1908. + *cipher = get_cipher_method(nid);
  1909. +
  1910. + return *cipher != NULL;
  1911. +}
  1912. +
  1913. +static void devcrypto_select_all_ciphers(int *cipher_list)
  1914. +{
  1915. + size_t i;
  1916. +
  1917. + for (i = 0; i < OSSL_NELEM(cipher_data); i++)
  1918. + cipher_list[i] = 1;
  1919. +}
  1920. +
  1921. +static int cryptodev_select_cipher_cb(const char *str, int len, void *usr)
  1922. +{
  1923. + int *cipher_list = (int *)usr;
  1924. + char *name;
  1925. + const EVP_CIPHER *EVP;
  1926. + size_t i;
  1927. +
  1928. + if (len == 0)
  1929. + return 1;
  1930. + if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
  1931. + return 0;
  1932. + EVP = EVP_get_cipherbyname(name);
  1933. + if (EVP == NULL)
  1934. + fprintf(stderr, "devcrypto: unknown cipher %s\n", name);
  1935. + else if ((i = find_cipher_data_index(EVP_CIPHER_nid(EVP))) != (size_t)-1)
  1936. + cipher_list[i] = 1;
  1937. + else
  1938. + fprintf(stderr, "devcrypto: cipher %s not available\n", name);
  1939. + OPENSSL_free(name);
  1940. + return 1;
  1941. +}
  1942. +
  1943. +static void dump_cipher_info(void)
  1944. +{
  1945. + size_t i;
  1946. + const char *name;
  1947. +
  1948. + fprintf (stderr, "Information about ciphers supported by the /dev/crypto"
  1949. + " engine:\n");
  1950. +#ifndef CIOCGSESSINFO
  1951. + fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
  1952. +#endif
  1953. + for (i = 0; i < OSSL_NELEM(cipher_data); i++) {
  1954. + name = OBJ_nid2sn(cipher_data[i].nid);
  1955. + fprintf (stderr, "Cipher %s, NID=%d, /dev/crypto info: id=%d, ",
  1956. + name ? name : "unknown", cipher_data[i].nid,
  1957. + cipher_data[i].devcryptoid);
  1958. + if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION ) {
  1959. + fprintf (stderr, "CIOCGSESSION (session open call) failed\n");
  1960. + continue;
  1961. + }
  1962. + fprintf (stderr, "driver=%s ", cipher_driver_info[i].driver_name ?
  1963. + cipher_driver_info[i].driver_name : "unknown");
  1964. + if (cipher_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
  1965. + fprintf(stderr, "(hw accelerated)");
  1966. + else if (cipher_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
  1967. + fprintf(stderr, "(software)");
  1968. + else
  1969. + fprintf(stderr, "(acceleration status unknown)");
  1970. + if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
  1971. + fprintf (stderr, ". Cipher setup failed");
  1972. + fprintf(stderr, "\n");
  1973. + }
  1974. + fprintf(stderr, "\n");
  1975. +}
  1976. +
  1977. +/*
  1978. + * We only support digests if the cryptodev implementation supports multiple
  1979. + * data updates and session copying. Otherwise, we would be forced to maintain
  1980. + * a cache, which is perilous if there's a lot of data coming in (if someone
  1981. + * wants to checksum an OpenSSL tarball, for example).
  1982. + */
  1983. +#if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL)
  1984. +#define IMPLEMENT_DIGEST
  1985. +
  1986. +/******************************************************************************
  1987. + *
  1988. + * Digests
  1989. + *
  1990. + * Because they all do the same basic operation, we have only one set of
  1991. + * method functions for them all to share, and a mapping table between
  1992. + * NIDs and cryptodev IDs, with all the necessary size data.
  1993. + *
  1994. + *****/
  1995. +
  1996. +struct digest_ctx {
  1997. + struct session_op sess;
  1998. + /* This signals that the init function was called, not that it succeeded. */
  1999. + int init_called;
  2000. + unsigned char digest_res[HASH_MAX_LEN];
  2001. +};
  2002. +
  2003. +static const struct digest_data_st {
  2004. + int nid;
  2005. + int blocksize;
  2006. + int digestlen;
  2007. + int devcryptoid;
  2008. +} digest_data[] = {
  2009. +#ifndef OPENSSL_NO_MD5
  2010. + { NID_md5, /* MD5_CBLOCK */ 64, 16, CRYPTO_MD5 },
  2011. +#endif
  2012. + { NID_sha1, SHA_CBLOCK, 20, CRYPTO_SHA1 },
  2013. +#ifndef OPENSSL_NO_RMD160
  2014. +# if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160)
  2015. + { NID_ripemd160, /* RIPEMD160_CBLOCK */ 64, 20, CRYPTO_RIPEMD160 },
  2016. +# endif
  2017. +#endif
  2018. +#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224)
  2019. + { NID_sha224, SHA256_CBLOCK, 224 / 8, CRYPTO_SHA2_224 },
  2020. +#endif
  2021. +#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256)
  2022. + { NID_sha256, SHA256_CBLOCK, 256 / 8, CRYPTO_SHA2_256 },
  2023. +#endif
  2024. +#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384)
  2025. + { NID_sha384, SHA512_CBLOCK, 384 / 8, CRYPTO_SHA2_384 },
  2026. +#endif
  2027. +#if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512)
  2028. + { NID_sha512, SHA512_CBLOCK, 512 / 8, CRYPTO_SHA2_512 },
  2029. +#endif
  2030. +};
  2031. +
  2032. +static size_t find_digest_data_index(int nid)
  2033. +{
  2034. + size_t i;
  2035. +
  2036. + for (i = 0; i < OSSL_NELEM(digest_data); i++)
  2037. + if (nid == digest_data[i].nid)
  2038. + return i;
  2039. + return (size_t)-1;
  2040. +}
  2041. +
  2042. +static size_t get_digest_data_index(int nid)
  2043. +{
  2044. + size_t i = find_digest_data_index(nid);
  2045. +
  2046. + if (i != (size_t)-1)
  2047. + return i;
  2048. +
  2049. + /*
  2050. + * Code further down must make sure that only NIDs in the table above
  2051. + * are used. If any other NID reaches this function, there's a grave
  2052. + * coding error further down.
  2053. + */
  2054. + assert("Code that never should be reached" == NULL);
  2055. + return -1;
  2056. +}
  2057. +
  2058. +static const struct digest_data_st *get_digest_data(int nid)
  2059. +{
  2060. + return &digest_data[get_digest_data_index(nid)];
  2061. +}
  2062. +
  2063. +/*
  2064. + * Following are the five necessary functions to map OpenSSL functionality
  2065. + * with cryptodev: init, update, final, cleanup, and copy.
  2066. + */
  2067. +
  2068. +static int digest_init(EVP_MD_CTX *ctx)
  2069. +{
  2070. + struct digest_ctx *digest_ctx =
  2071. + (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  2072. + const struct digest_data_st *digest_d =
  2073. + get_digest_data(EVP_MD_CTX_type(ctx));
  2074. +
  2075. + digest_ctx->init_called = 1;
  2076. +
  2077. + memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess));
  2078. + digest_ctx->sess.mac = digest_d->devcryptoid;
  2079. + if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) {
  2080. + SYSerr(SYS_F_IOCTL, errno);
  2081. + return 0;
  2082. + }
  2083. + return 1;
  2084. +}
  2085. +
  2086. +static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen,
  2087. + void *res, unsigned int flags)
  2088. +{
  2089. + struct crypt_op cryp;
  2090. +
  2091. + memset(&cryp, 0, sizeof(cryp));
  2092. + cryp.ses = ctx->sess.ses;
  2093. + cryp.len = srclen;
  2094. + cryp.src = (void *)src;
  2095. + cryp.dst = NULL;
  2096. + cryp.mac = res;
  2097. + cryp.flags = flags;
  2098. + return ioctl(cfd, CIOCCRYPT, &cryp);
  2099. +}
  2100. +
  2101. +static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  2102. +{
  2103. + struct digest_ctx *digest_ctx =
  2104. + (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  2105. +
  2106. + if (count == 0)
  2107. + return 1;
  2108. +
  2109. + if (digest_ctx == NULL)
  2110. + return 0;
  2111. +
  2112. + if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
  2113. + if (digest_op(digest_ctx, data, count, digest_ctx->digest_res, 0) >= 0)
  2114. + return 1;
  2115. + } else if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) >= 0) {
  2116. + return 1;
  2117. + }
  2118. +
  2119. + SYSerr(SYS_F_IOCTL, errno);
  2120. + return 0;
  2121. +}
  2122. +
  2123. +static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
  2124. +{
  2125. + struct digest_ctx *digest_ctx =
  2126. + (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  2127. +
  2128. + if (md == NULL || digest_ctx == NULL)
  2129. + return 0;
  2130. +
  2131. + if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
  2132. + memcpy(md, digest_ctx->digest_res, EVP_MD_CTX_size(ctx));
  2133. + } else if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
  2134. + SYSerr(SYS_F_IOCTL, errno);
  2135. + return 0;
  2136. + }
  2137. +
  2138. + return 1;
  2139. +}
  2140. +
  2141. +static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
  2142. +{
  2143. + struct digest_ctx *digest_from =
  2144. + (struct digest_ctx *)EVP_MD_CTX_md_data(from);
  2145. + struct digest_ctx *digest_to =
  2146. + (struct digest_ctx *)EVP_MD_CTX_md_data(to);
  2147. + struct cphash_op cphash;
  2148. +
  2149. + if (digest_from == NULL || digest_from->init_called != 1)
  2150. + return 1;
  2151. +
  2152. + if (!digest_init(to)) {
  2153. + SYSerr(SYS_F_IOCTL, errno);
  2154. + return 0;
  2155. + }
  2156. +
  2157. + cphash.src_ses = digest_from->sess.ses;
  2158. + cphash.dst_ses = digest_to->sess.ses;
  2159. + if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
  2160. + SYSerr(SYS_F_IOCTL, errno);
  2161. + return 0;
  2162. + }
  2163. + return 1;
  2164. +}
  2165. +
  2166. +static int digest_cleanup(EVP_MD_CTX *ctx)
  2167. +{
  2168. + struct digest_ctx *digest_ctx =
  2169. + (struct digest_ctx *)EVP_MD_CTX_md_data(ctx);
  2170. +
  2171. + if (digest_ctx == NULL)
  2172. + return 1;
  2173. +
  2174. + return clean_devcrypto_session(&digest_ctx->sess);
  2175. +}
  2176. +
  2177. +/*
  2178. + * Keep tables of known nids, associated methods, selected digests, and
  2179. + * driver info.
  2180. + * Note that known_digest_nids[] isn't necessarily indexed the same way as
  2181. + * digest_data[] above, which the other tables are.
  2182. + */
  2183. +static int known_digest_nids[OSSL_NELEM(digest_data)];
  2184. +static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */
  2185. +static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, };
  2186. +static int selected_digests[OSSL_NELEM(digest_data)];
  2187. +static struct driver_info_st digest_driver_info[OSSL_NELEM(digest_data)];
  2188. +
  2189. +static int devcrypto_test_digest(size_t digest_data_index)
  2190. +{
  2191. + return (digest_driver_info[digest_data_index].status == DEVCRYPTO_STATUS_USABLE
  2192. + && selected_digests[digest_data_index] == 1
  2193. + && (digest_driver_info[digest_data_index].accelerated
  2194. + == DEVCRYPTO_ACCELERATED
  2195. + || use_softdrivers == DEVCRYPTO_USE_SOFTWARE
  2196. + || (digest_driver_info[digest_data_index].accelerated
  2197. + != DEVCRYPTO_NOT_ACCELERATED
  2198. + && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE)));
  2199. +}
  2200. +
  2201. +static void rebuild_known_digest_nids(ENGINE *e)
  2202. +{
  2203. + size_t i;
  2204. +
  2205. + for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); i++) {
  2206. + if (devcrypto_test_digest(i))
  2207. + known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
  2208. + }
  2209. + ENGINE_unregister_digests(e);
  2210. + ENGINE_register_digests(e);
  2211. +}
  2212. +
  2213. +static void prepare_digest_methods(void)
  2214. +{
  2215. + size_t i;
  2216. + struct session_op sess1, sess2;
  2217. +#ifdef CIOCGSESSINFO
  2218. + struct session_info_op siop;
  2219. +#endif
  2220. + struct cphash_op cphash;
  2221. +
  2222. + memset(&digest_driver_info, 0, sizeof(digest_driver_info));
  2223. +
  2224. + memset(&sess1, 0, sizeof(sess1));
  2225. + memset(&sess2, 0, sizeof(sess2));
  2226. +
  2227. + for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data);
  2228. + i++) {
  2229. +
  2230. + selected_digests[i] = 1;
  2231. +
  2232. + /*
  2233. + * Check that the digest is usable
  2234. + */
  2235. + sess1.mac = digest_data[i].devcryptoid;
  2236. + sess2.ses = 0;
  2237. + if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) {
  2238. + digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION;
  2239. + goto finish;
  2240. + }
  2241. +
  2242. +#ifdef CIOCGSESSINFO
  2243. + /* gather hardware acceleration info from the driver */
  2244. + siop.ses = sess1.ses;
  2245. + if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) {
  2246. + digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN;
  2247. + } else {
  2248. + digest_driver_info[i].driver_name =
  2249. + OPENSSL_strndup(siop.hash_info.cra_driver_name,
  2250. + CRYPTODEV_MAX_ALG_NAME);
  2251. + if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)
  2252. + digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED;
  2253. + else
  2254. + digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED;
  2255. + }
  2256. +#endif
  2257. +
  2258. + /* digest must be capable of hash state copy */
  2259. + sess2.mac = sess1.mac;
  2260. + if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) {
  2261. + digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
  2262. + goto finish;
  2263. + }
  2264. + cphash.src_ses = sess1.ses;
  2265. + cphash.dst_ses = sess2.ses;
  2266. + if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) {
  2267. + digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCCPHASH;
  2268. + goto finish;
  2269. + }
  2270. + if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid,
  2271. + NID_undef)) == NULL
  2272. + || !EVP_MD_meth_set_input_blocksize(known_digest_methods[i],
  2273. + digest_data[i].blocksize)
  2274. + || !EVP_MD_meth_set_result_size(known_digest_methods[i],
  2275. + digest_data[i].digestlen)
  2276. + || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init)
  2277. + || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update)
  2278. + || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final)
  2279. + || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy)
  2280. + || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup)
  2281. + || !EVP_MD_meth_set_app_datasize(known_digest_methods[i],
  2282. + sizeof(struct digest_ctx))) {
  2283. + digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE;
  2284. + EVP_MD_meth_free(known_digest_methods[i]);
  2285. + known_digest_methods[i] = NULL;
  2286. + goto finish;
  2287. + }
  2288. + digest_driver_info[i].status = DEVCRYPTO_STATUS_USABLE;
  2289. +finish:
  2290. + ioctl(cfd, CIOCFSESSION, &sess1.ses);
  2291. + if (sess2.ses != 0)
  2292. + ioctl(cfd, CIOCFSESSION, &sess2.ses);
  2293. + if (devcrypto_test_digest(i))
  2294. + known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid;
  2295. + }
  2296. +}
  2297. +
  2298. +static const EVP_MD *get_digest_method(int nid)
  2299. +{
  2300. + size_t i = get_digest_data_index(nid);
  2301. +
  2302. + if (i == (size_t)-1)
  2303. + return NULL;
  2304. + return known_digest_methods[i];
  2305. +}
  2306. +
  2307. +static int get_digest_nids(const int **nids)
  2308. +{
  2309. + *nids = known_digest_nids;
  2310. + return known_digest_nids_amount;
  2311. +}
  2312. +
  2313. +static void destroy_digest_method(int nid)
  2314. +{
  2315. + size_t i = get_digest_data_index(nid);
  2316. +
  2317. + EVP_MD_meth_free(known_digest_methods[i]);
  2318. + known_digest_methods[i] = NULL;
  2319. +}
  2320. +
  2321. +static void destroy_all_digest_methods(void)
  2322. +{
  2323. + size_t i;
  2324. +
  2325. + for (i = 0; i < OSSL_NELEM(digest_data); i++) {
  2326. + destroy_digest_method(digest_data[i].nid);
  2327. + OPENSSL_free(digest_driver_info[i].driver_name);
  2328. + digest_driver_info[i].driver_name = NULL;
  2329. + }
  2330. +}
  2331. +
  2332. +static int devcrypto_digests(ENGINE *e, const EVP_MD **digest,
  2333. + const int **nids, int nid)
  2334. +{
  2335. + if (digest == NULL)
  2336. + return get_digest_nids(nids);
  2337. +
  2338. + *digest = get_digest_method(nid);
  2339. +
  2340. + return *digest != NULL;
  2341. +}
  2342. +
  2343. +static void devcrypto_select_all_digests(int *digest_list)
  2344. +{
  2345. + size_t i;
  2346. +
  2347. + for (i = 0; i < OSSL_NELEM(digest_data); i++)
  2348. + digest_list[i] = 1;
  2349. +}
  2350. +
  2351. +static int cryptodev_select_digest_cb(const char *str, int len, void *usr)
  2352. +{
  2353. + int *digest_list = (int *)usr;
  2354. + char *name;
  2355. + const EVP_MD *EVP;
  2356. + size_t i;
  2357. +
  2358. + if (len == 0)
  2359. + return 1;
  2360. + if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL)
  2361. + return 0;
  2362. + EVP = EVP_get_digestbyname(name);
  2363. + if (EVP == NULL)
  2364. + fprintf(stderr, "devcrypto: unknown digest %s\n", name);
  2365. + else if ((i = find_digest_data_index(EVP_MD_type(EVP))) != (size_t)-1)
  2366. + digest_list[i] = 1;
  2367. + else
  2368. + fprintf(stderr, "devcrypto: digest %s not available\n", name);
  2369. + OPENSSL_free(name);
  2370. + return 1;
  2371. +}
  2372. +
  2373. +static void dump_digest_info(void)
  2374. +{
  2375. + size_t i;
  2376. + const char *name;
  2377. +
  2378. + fprintf (stderr, "Information about digests supported by the /dev/crypto"
  2379. + " engine:\n");
  2380. +#ifndef CIOCGSESSINFO
  2381. + fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n");
  2382. +#endif
  2383. +
  2384. + for (i = 0; i < OSSL_NELEM(digest_data); i++) {
  2385. + name = OBJ_nid2sn(digest_data[i].nid);
  2386. + fprintf (stderr, "Digest %s, NID=%d, /dev/crypto info: id=%d, driver=%s",
  2387. + name ? name : "unknown", digest_data[i].nid,
  2388. + digest_data[i].devcryptoid,
  2389. + digest_driver_info[i].driver_name ? digest_driver_info[i].driver_name : "unknown");
  2390. + if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) {
  2391. + fprintf (stderr, ". CIOCGSESSION (session open) failed\n");
  2392. + continue;
  2393. + }
  2394. + if (digest_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED)
  2395. + fprintf(stderr, " (hw accelerated)");
  2396. + else if (digest_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED)
  2397. + fprintf(stderr, " (software)");
  2398. + else
  2399. + fprintf(stderr, " (acceleration status unknown)");
  2400. + if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE)
  2401. + fprintf (stderr, ". Cipher setup failed\n");
  2402. + else if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCCPHASH)
  2403. + fprintf(stderr, ", CIOCCPHASH failed\n");
  2404. + else
  2405. + fprintf(stderr, ", CIOCCPHASH capable\n");
  2406. + }
  2407. + fprintf(stderr, "\n");
  2408. +}
  2409. +
  2410. +#endif
  2411. +
  2412. +/******************************************************************************
  2413. + *
  2414. + * CONTROL COMMANDS
  2415. + *
  2416. + *****/
  2417. +
  2418. +#define DEVCRYPTO_CMD_USE_SOFTDRIVERS ENGINE_CMD_BASE
  2419. +#define DEVCRYPTO_CMD_CIPHERS (ENGINE_CMD_BASE + 1)
  2420. +#define DEVCRYPTO_CMD_DIGESTS (ENGINE_CMD_BASE + 2)
  2421. +#define DEVCRYPTO_CMD_DUMP_INFO (ENGINE_CMD_BASE + 3)
  2422. +
  2423. +/* Helper macros for CPP string composition */
  2424. +#ifndef OPENSSL_MSTR
  2425. +# define OPENSSL_MSTR_HELPER(x) #x
  2426. +# define OPENSSL_MSTR(x) OPENSSL_MSTR_HELPER(x)
  2427. +#endif
  2428. +
  2429. +static const ENGINE_CMD_DEFN devcrypto_cmds[] = {
  2430. +#ifdef CIOCGSESSINFO
  2431. + {DEVCRYPTO_CMD_USE_SOFTDRIVERS,
  2432. + "USE_SOFTDRIVERS",
  2433. + "specifies whether to use software (not accelerated) drivers ("
  2434. + OPENSSL_MSTR(DEVCRYPTO_REQUIRE_ACCELERATED) "=use only accelerated drivers, "
  2435. + OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, "
  2436. + OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE)
  2437. + "=use if acceleration can't be determined) [default="
  2438. + OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS) "]",
  2439. + ENGINE_CMD_FLAG_NUMERIC},
  2440. +#endif
  2441. +
  2442. + {DEVCRYPTO_CMD_CIPHERS,
  2443. + "CIPHERS",
  2444. + "either ALL, NONE, or a comma-separated list of ciphers to enable [default=ALL]",
  2445. + ENGINE_CMD_FLAG_STRING},
  2446. +
  2447. +#ifdef IMPLEMENT_DIGEST
  2448. + {DEVCRYPTO_CMD_DIGESTS,
  2449. + "DIGESTS",
  2450. + "either ALL, NONE, or a comma-separated list of digests to enable [default=ALL]",
  2451. + ENGINE_CMD_FLAG_STRING},
  2452. +#endif
  2453. +
  2454. + {DEVCRYPTO_CMD_DUMP_INFO,
  2455. + "DUMP_INFO",
  2456. + "dump info about each algorithm to stderr; use 'openssl engine -pre DUMP_INFO devcrypto'",
  2457. + ENGINE_CMD_FLAG_NO_INPUT},
  2458. +
  2459. + {0, NULL, NULL, 0}
  2460. +};
  2461. +
  2462. +static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  2463. +{
  2464. + int *new_list;
  2465. + switch (cmd) {
  2466. +#ifdef CIOCGSESSINFO
  2467. + case DEVCRYPTO_CMD_USE_SOFTDRIVERS:
  2468. + switch (i) {
  2469. + case DEVCRYPTO_REQUIRE_ACCELERATED:
  2470. + case DEVCRYPTO_USE_SOFTWARE:
  2471. + case DEVCRYPTO_REJECT_SOFTWARE:
  2472. + break;
  2473. + default:
  2474. + fprintf(stderr, "devcrypto: invalid value (%ld) for USE_SOFTDRIVERS\n", i);
  2475. + return 0;
  2476. + }
  2477. + if (use_softdrivers == i)
  2478. + return 1;
  2479. + use_softdrivers = i;
  2480. +#ifdef IMPLEMENT_DIGEST
  2481. + rebuild_known_digest_nids(e);
  2482. +#endif
  2483. + rebuild_known_cipher_nids(e);
  2484. + return 1;
  2485. +#endif /* CIOCGSESSINFO */
  2486. +
  2487. + case DEVCRYPTO_CMD_CIPHERS:
  2488. + if (p == NULL)
  2489. + return 1;
  2490. + if (strcasecmp((const char *)p, "ALL") == 0) {
  2491. + devcrypto_select_all_ciphers(selected_ciphers);
  2492. + } else if (strcasecmp((const char*)p, "NONE") == 0) {
  2493. + memset(selected_ciphers, 0, sizeof(selected_ciphers));
  2494. + } else {
  2495. + new_list=OPENSSL_zalloc(sizeof(selected_ciphers));
  2496. + if (!CONF_parse_list(p, ',', 1, cryptodev_select_cipher_cb, new_list)) {
  2497. + OPENSSL_free(new_list);
  2498. + return 0;
  2499. + }
  2500. + memcpy(selected_ciphers, new_list, sizeof(selected_ciphers));
  2501. + OPENSSL_free(new_list);
  2502. + }
  2503. + rebuild_known_cipher_nids(e);
  2504. + return 1;
  2505. +
  2506. +#ifdef IMPLEMENT_DIGEST
  2507. + case DEVCRYPTO_CMD_DIGESTS:
  2508. + if (p == NULL)
  2509. + return 1;
  2510. + if (strcasecmp((const char *)p, "ALL") == 0) {
  2511. + devcrypto_select_all_digests(selected_digests);
  2512. + } else if (strcasecmp((const char*)p, "NONE") == 0) {
  2513. + memset(selected_digests, 0, sizeof(selected_digests));
  2514. + } else {
  2515. + new_list=OPENSSL_zalloc(sizeof(selected_digests));
  2516. + if (!CONF_parse_list(p, ',', 1, cryptodev_select_digest_cb, new_list)) {
  2517. + OPENSSL_free(new_list);
  2518. + return 0;
  2519. + }
  2520. + memcpy(selected_digests, new_list, sizeof(selected_digests));
  2521. + OPENSSL_free(new_list);
  2522. + }
  2523. + rebuild_known_digest_nids(e);
  2524. + return 1;
  2525. +#endif /* IMPLEMENT_DIGEST */
  2526. +
  2527. + case DEVCRYPTO_CMD_DUMP_INFO:
  2528. + dump_cipher_info();
  2529. +#ifdef IMPLEMENT_DIGEST
  2530. + dump_digest_info();
  2531. +#endif
  2532. + return 1;
  2533. +
  2534. + default:
  2535. + break;
  2536. + }
  2537. + return 0;
  2538. +}
  2539. +
  2540. +/******************************************************************************
  2541. + *
  2542. + * LOAD / UNLOAD
  2543. + *
  2544. + *****/
  2545. +
  2546. +/*
  2547. + * Opens /dev/crypto
  2548. + */
  2549. +static int open_devcrypto(void)
  2550. +{
  2551. + if (cfd >= 0)
  2552. + return 1;
  2553. +
  2554. + if ((cfd = open("/dev/crypto", O_RDWR, 0)) < 0) {
  2555. +#ifndef ENGINE_DEVCRYPTO_DEBUG
  2556. + if (errno != ENOENT)
  2557. +#endif
  2558. + fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno));
  2559. + return 0;
  2560. + }
  2561. +
  2562. + return 1;
  2563. +}
  2564. +
  2565. +static int close_devcrypto(void)
  2566. +{
  2567. + int ret;
  2568. +
  2569. + if (cfd < 0)
  2570. + return 1;
  2571. + ret = close(cfd);
  2572. + cfd = -1;
  2573. + if (ret != 0) {
  2574. + fprintf(stderr, "Error closing /dev/crypto: %s\n", strerror(errno));
  2575. + return 0;
  2576. + }
  2577. + return 1;
  2578. +}
  2579. +
  2580. +static int devcrypto_unload(ENGINE *e)
  2581. +{
  2582. + destroy_all_cipher_methods();
  2583. +#ifdef IMPLEMENT_DIGEST
  2584. + destroy_all_digest_methods();
  2585. +#endif
  2586. +
  2587. + close_devcrypto();
  2588. +
  2589. + return 1;
  2590. +}
  2591. +
  2592. +static int bind_devcrypto(ENGINE *e) {
  2593. +
  2594. + if (!ENGINE_set_id(e, engine_devcrypto_id)
  2595. + || !ENGINE_set_name(e, "/dev/crypto engine")
  2596. + || !ENGINE_set_destroy_function(e, devcrypto_unload)
  2597. + || !ENGINE_set_cmd_defns(e, devcrypto_cmds)
  2598. + || !ENGINE_set_ctrl_function(e, devcrypto_ctrl))
  2599. + return 0;
  2600. +
  2601. + prepare_cipher_methods();
  2602. +#ifdef IMPLEMENT_DIGEST
  2603. + prepare_digest_methods();
  2604. +#endif
  2605. +
  2606. + return (ENGINE_set_ciphers(e, devcrypto_ciphers)
  2607. +#ifdef IMPLEMENT_DIGEST
  2608. + && ENGINE_set_digests(e, devcrypto_digests)
  2609. +#endif
  2610. +/*
  2611. + * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD
  2612. + * implementations, it seems to only exist in FreeBSD, and regarding the
  2613. + * parameters in its crypt_kop, the manual crypto(4) has this to say:
  2614. + *
  2615. + * The semantics of these arguments are currently undocumented.
  2616. + *
  2617. + * Reading through the FreeBSD source code doesn't give much more than
  2618. + * their CRK_MOD_EXP implementation for ubsec.
  2619. + *
  2620. + * It doesn't look much better with cryptodev-linux. They have the crypt_kop
  2621. + * structure as well as the command (CRK_*) in cryptodev.h, but no support
  2622. + * seems to be implemented at all for the moment.
  2623. + *
  2624. + * At the time of writing, it seems impossible to write proper support for
  2625. + * FreeBSD's asym features without some very deep knowledge and access to
  2626. + * specific kernel modules.
  2627. + *
  2628. + * /Richard Levitte, 2017-05-11
  2629. + */
  2630. +#if 0
  2631. +# ifndef OPENSSL_NO_RSA
  2632. + && ENGINE_set_RSA(e, devcrypto_rsa)
  2633. +# endif
  2634. +# ifndef OPENSSL_NO_DSA
  2635. + && ENGINE_set_DSA(e, devcrypto_dsa)
  2636. +# endif
  2637. +# ifndef OPENSSL_NO_DH
  2638. + && ENGINE_set_DH(e, devcrypto_dh)
  2639. +# endif
  2640. +# ifndef OPENSSL_NO_EC
  2641. + && ENGINE_set_EC(e, devcrypto_ec)
  2642. +# endif
  2643. +#endif
  2644. + );
  2645. +}
  2646. +
  2647. +#ifdef OPENSSL_NO_DYNAMIC_ENGINE
  2648. +/*
  2649. + * In case this engine is built into libcrypto, then it doesn't offer any
  2650. + * ability to be dynamically loadable.
  2651. + */
  2652. +void engine_load_devcrypto_int(void)
  2653. +{
  2654. + ENGINE *e = NULL;
  2655. +
  2656. + if (!open_devcrypto())
  2657. + return;
  2658. +
  2659. + if ((e = ENGINE_new()) == NULL
  2660. + || !bind_devcrypto(e)) {
  2661. + close_devcrypto();
  2662. + ENGINE_free(e);
  2663. + return;
  2664. + }
  2665. +
  2666. + ENGINE_add(e);
  2667. + ENGINE_free(e); /* Loose our local reference */
  2668. + ERR_clear_error();
  2669. +}
  2670. +
  2671. +#else
  2672. +
  2673. +static int bind_helper(ENGINE *e, const char *id)
  2674. +{
  2675. + if ((id && (strcmp(id, engine_devcrypto_id) != 0))
  2676. + || !open_devcrypto())
  2677. + return 0;
  2678. + if (!bind_devcrypto(e)) {
  2679. + close_devcrypto();
  2680. + return 0;
  2681. + }
  2682. + return 1;
  2683. +}
  2684. +
  2685. +IMPLEMENT_DYNAMIC_CHECK_FN()
  2686. +IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  2687. +
  2688. +#endif