patch-crypto_engine_eng_cryptodev_c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539
  1. --- openssl-1.0.2a.orig/crypto/engine/eng_cryptodev.c 2015-03-19 14:30:36.000000000 +0100
  2. +++ openssl-1.0.2a/crypto/engine/eng_cryptodev.c 2015-02-02 21:02:31.009892700 +0100
  3. @@ -2,6 +2,7 @@
  4. * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
  5. * Copyright (c) 2002 Theo de Raadt
  6. * Copyright (c) 2002 Markus Friedl
  7. + * Copyright (c) 2012 Nikos Mavrogiannopoulos
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. @@ -32,8 +33,8 @@
  12. #include <openssl/bn.h>
  13. #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
  14. - (defined(OpenBSD) || defined(__FreeBSD__))
  15. -# include <sys/param.h>
  16. + (defined(OpenBSD) || defined(__FreeBSD__))
  17. +#include <sys/param.h>
  18. # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
  19. # define HAVE_CRYPTODEV
  20. # endif
  21. @@ -44,39 +45,40 @@
  22. #ifndef HAVE_CRYPTODEV
  23. -void ENGINE_load_cryptodev(void)
  24. +void
  25. +ENGINE_load_cryptodev(void)
  26. {
  27. - /* This is a NOP on platforms without /dev/crypto */
  28. - return;
  29. + /* This is a NOP on platforms without /dev/crypto */
  30. + return;
  31. }
  32. -#else
  33. -
  34. -# include <sys/types.h>
  35. -# include <crypto/cryptodev.h>
  36. -# include <openssl/dh.h>
  37. -# include <openssl/dsa.h>
  38. -# include <openssl/err.h>
  39. -# include <openssl/rsa.h>
  40. -# include <sys/ioctl.h>
  41. -# include <errno.h>
  42. -# include <stdio.h>
  43. -# include <unistd.h>
  44. -# include <fcntl.h>
  45. -# include <stdarg.h>
  46. -# include <syslog.h>
  47. -# include <errno.h>
  48. -# include <string.h>
  49. +#else
  50. +
  51. +#include <sys/types.h>
  52. +#include <crypto/cryptodev.h>
  53. +#include <crypto/dh/dh.h>
  54. +#include <crypto/dsa/dsa.h>
  55. +#include <crypto/err/err.h>
  56. +#include <crypto/rsa/rsa.h>
  57. +#include <sys/ioctl.h>
  58. +#include <errno.h>
  59. +#include <stdio.h>
  60. +#include <unistd.h>
  61. +#include <fcntl.h>
  62. +#include <stdarg.h>
  63. +#include <syslog.h>
  64. +#include <errno.h>
  65. +#include <string.h>
  66. struct dev_crypto_state {
  67. - struct session_op d_sess;
  68. - int d_fd;
  69. -# ifdef USE_CRYPTODEV_DIGESTS
  70. - char dummy_mac_key[HASH_MAX_LEN];
  71. - unsigned char digest_res[HASH_MAX_LEN];
  72. - char *mac_data;
  73. - int mac_len;
  74. -# endif
  75. + struct session_op d_sess;
  76. + int d_fd;
  77. +
  78. +#ifdef USE_CRYPTODEV_DIGESTS
  79. + unsigned char digest_res[64];
  80. + char *mac_data;
  81. + int mac_len;
  82. +#endif
  83. };
  84. static u_int32_t cryptodev_asymfeat = 0;
  85. @@ -85,196 +87,153 @@ static int get_asym_dev_crypto(void);
  86. static int open_dev_crypto(void);
  87. static int get_dev_crypto(void);
  88. static int get_cryptodev_ciphers(const int **cnids);
  89. -# ifdef USE_CRYPTODEV_DIGESTS
  90. +#ifdef USE_CRYPTODEV_DIGESTS
  91. static int get_cryptodev_digests(const int **cnids);
  92. -# endif
  93. +#endif
  94. static int cryptodev_usable_ciphers(const int **nids);
  95. static int cryptodev_usable_digests(const int **nids);
  96. static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  97. - const unsigned char *in, size_t inl);
  98. + const unsigned char *in, size_t inl);
  99. static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  100. - const unsigned char *iv, int enc);
  101. + const unsigned char *iv, int enc);
  102. static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
  103. static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  104. - const int **nids, int nid);
  105. + const int **nids, int nid);
  106. static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
  107. - const int **nids, int nid);
  108. + const int **nids, int nid);
  109. static int bn2crparam(const BIGNUM *a, struct crparam *crp);
  110. static int crparam2bn(struct crparam *crp, BIGNUM *a);
  111. static void zapparams(struct crypt_kop *kop);
  112. static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
  113. - int slen, BIGNUM *s);
  114. + int slen, BIGNUM *s);
  115. static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
  116. - const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  117. - BN_MONT_CTX *m_ctx);
  118. -static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  119. - BN_CTX *ctx);
  120. -static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  121. - BN_CTX *ctx);
  122. + const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  123. +static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
  124. + RSA *rsa, BN_CTX *ctx);
  125. +static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
  126. static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
  127. - const BIGNUM *p, const BIGNUM *m,
  128. - BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  129. + const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  130. static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
  131. - BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2,
  132. - BIGNUM *p, BN_CTX *ctx,
  133. - BN_MONT_CTX *mont);
  134. -static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
  135. - DSA *dsa);
  136. + BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
  137. + BN_CTX *ctx, BN_MONT_CTX *mont);
  138. +static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
  139. + int dlen, DSA *dsa);
  140. static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
  141. - DSA_SIG *sig, DSA *dsa);
  142. + DSA_SIG *sig, DSA *dsa);
  143. static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  144. - const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  145. - BN_MONT_CTX *m_ctx);
  146. -static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key,
  147. - DH *dh);
  148. + const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  149. + BN_MONT_CTX *m_ctx);
  150. +static int cryptodev_dh_compute_key(unsigned char *key,
  151. + const BIGNUM *pub_key, DH *dh);
  152. static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
  153. - void (*f) (void));
  154. + void (*f)(void));
  155. void ENGINE_load_cryptodev(void);
  156. static const ENGINE_CMD_DEFN cryptodev_defns[] = {
  157. - {0, NULL, NULL, 0}
  158. + { 0, NULL, NULL, 0 }
  159. };
  160. static struct {
  161. - int id;
  162. - int nid;
  163. - int ivmax;
  164. - int keylen;
  165. + int id;
  166. + int nid;
  167. + int ivmax;
  168. + int keylen;
  169. } ciphers[] = {
  170. - {
  171. - CRYPTO_ARC4, NID_rc4, 0, 16,
  172. - },
  173. - {
  174. - CRYPTO_DES_CBC, NID_des_cbc, 8, 8,
  175. - },
  176. - {
  177. - CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24,
  178. - },
  179. - {
  180. - CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16,
  181. - },
  182. - {
  183. - CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24,
  184. - },
  185. - {
  186. - CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32,
  187. - },
  188. -# ifdef CRYPTO_AES_CTR
  189. - {
  190. - CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16,
  191. - },
  192. - {
  193. - CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24,
  194. - },
  195. - {
  196. - CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32,
  197. - },
  198. -# endif
  199. - {
  200. - CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16,
  201. - },
  202. - {
  203. - CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16,
  204. - },
  205. - {
  206. - CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0,
  207. - },
  208. - {
  209. - 0, NID_undef, 0, 0,
  210. - },
  211. + { CRYPTO_ARC4, NID_rc4, 0, 16, },
  212. + { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
  213. + { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
  214. + { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
  215. + { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, },
  216. + { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, },
  217. + { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
  218. + { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
  219. + { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
  220. + { 0, NID_undef, 0, 0, },
  221. };
  222. -# ifdef USE_CRYPTODEV_DIGESTS
  223. +#ifdef USE_CRYPTODEV_DIGESTS
  224. static struct {
  225. - int id;
  226. - int nid;
  227. - int keylen;
  228. + int id;
  229. + int nid;
  230. + int digestlen;
  231. } digests[] = {
  232. - {
  233. - CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16
  234. - },
  235. - {
  236. - CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20
  237. - },
  238. - {
  239. - CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16
  240. - /* ? */
  241. - },
  242. - {
  243. - CRYPTO_MD5_KPDK, NID_undef, 0
  244. - },
  245. - {
  246. - CRYPTO_SHA1_KPDK, NID_undef, 0
  247. - },
  248. - {
  249. - CRYPTO_MD5, NID_md5, 16
  250. - },
  251. - {
  252. - CRYPTO_SHA1, NID_sha1, 20
  253. - },
  254. - {
  255. - 0, NID_undef, 0
  256. - },
  257. +#if 0
  258. + /* HMAC is not supported */
  259. + { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16},
  260. + { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20},
  261. + { CRYPTO_SHA2_256_HMAC, NID_hmacWithSHA256, 32},
  262. + { CRYPTO_SHA2_384_HMAC, NID_hmacWithSHA384, 48},
  263. + { CRYPTO_SHA2_512_HMAC, NID_hmacWithSHA512, 64},
  264. +#endif
  265. + { CRYPTO_MD5, NID_md5, 16},
  266. + { CRYPTO_SHA1, NID_sha1, 20},
  267. + { CRYPTO_SHA2_256, NID_sha256, 32},
  268. + { CRYPTO_SHA2_384, NID_sha384, 48},
  269. + { CRYPTO_SHA2_512, NID_sha512, 64},
  270. + { 0, NID_undef, 0},
  271. };
  272. -# endif
  273. +#endif
  274. /*
  275. * Return a fd if /dev/crypto seems usable, 0 otherwise.
  276. */
  277. -static int open_dev_crypto(void)
  278. +static int
  279. +open_dev_crypto(void)
  280. {
  281. - static int fd = -1;
  282. + static int fd = -1;
  283. - if (fd == -1) {
  284. - if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
  285. - return (-1);
  286. - /* close on exec */
  287. - if (fcntl(fd, F_SETFD, 1) == -1) {
  288. - close(fd);
  289. - fd = -1;
  290. - return (-1);
  291. - }
  292. - }
  293. - return (fd);
  294. + if (fd == -1) {
  295. + if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
  296. + return (-1);
  297. + /* close on exec */
  298. + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
  299. + close(fd);
  300. + fd = -1;
  301. + return (-1);
  302. + }
  303. + }
  304. + return (fd);
  305. }
  306. -static int get_dev_crypto(void)
  307. +static int
  308. +get_dev_crypto(void)
  309. {
  310. - int fd, retfd;
  311. + int fd, retfd;
  312. - if ((fd = open_dev_crypto()) == -1)
  313. - return (-1);
  314. -# ifndef CRIOGET_NOT_NEEDED
  315. - if (ioctl(fd, CRIOGET, &retfd) == -1)
  316. - return (-1);
  317. + if ((fd = open_dev_crypto()) == -1)
  318. + return (-1);
  319. +#ifndef CRIOGET_NOT_NEEDED
  320. + if (ioctl(fd, CRIOGET, &retfd) == -1)
  321. + return (-1);
  322. - /* close on exec */
  323. - if (fcntl(retfd, F_SETFD, 1) == -1) {
  324. - close(retfd);
  325. - return (-1);
  326. - }
  327. -# else
  328. - retfd = fd;
  329. -# endif
  330. - return (retfd);
  331. + /* close on exec */
  332. + if (fcntl(retfd, F_SETFD, 1) == -1) {
  333. + close(retfd);
  334. + return (-1);
  335. + }
  336. +#else
  337. + retfd = fd;
  338. +#endif
  339. + return (retfd);
  340. }
  341. static void put_dev_crypto(int fd)
  342. {
  343. -# ifndef CRIOGET_NOT_NEEDED
  344. - close(fd);
  345. -# endif
  346. +#ifndef CRIOGET_NOT_NEEDED
  347. + close(fd);
  348. +#endif
  349. }
  350. /* Caching version for asym operations */
  351. -static int get_asym_dev_crypto(void)
  352. +static int
  353. +get_asym_dev_crypto(void)
  354. {
  355. - static int fd = -1;
  356. + static int fd = -1;
  357. - if (fd == -1)
  358. - fd = get_dev_crypto();
  359. - return fd;
  360. + if (fd == -1)
  361. + fd = get_dev_crypto();
  362. + return fd;
  363. }
  364. /*
  365. @@ -283,76 +242,80 @@ static int get_asym_dev_crypto(void)
  366. * returning them here is harmless, as long as we return NULL
  367. * when asked for a handler in the cryptodev_engine_ciphers routine
  368. */
  369. -static int get_cryptodev_ciphers(const int **cnids)
  370. +static int
  371. +get_cryptodev_ciphers(const int **cnids)
  372. {
  373. - static int nids[CRYPTO_ALGORITHM_MAX];
  374. - struct session_op sess;
  375. - int fd, i, count = 0;
  376. + static int nids[CRYPTO_ALGORITHM_MAX];
  377. + struct session_op sess;
  378. + int fd, i, count = 0;
  379. + unsigned char fake_key[EVP_MAX_KEY_LENGTH];
  380. - if ((fd = get_dev_crypto()) < 0) {
  381. - *cnids = NULL;
  382. - return (0);
  383. - }
  384. - memset(&sess, 0, sizeof(sess));
  385. - sess.key = (caddr_t) "123456789abcdefghijklmno";
  386. + if ((fd = get_dev_crypto()) < 0) {
  387. + *cnids = NULL;
  388. + return (0);
  389. + }
  390. + memset(&sess, 0, sizeof(sess));
  391. + sess.key = (void*)fake_key;
  392. - for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  393. - if (ciphers[i].nid == NID_undef)
  394. - continue;
  395. - sess.cipher = ciphers[i].id;
  396. - sess.keylen = ciphers[i].keylen;
  397. - sess.mac = 0;
  398. - if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
  399. - ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
  400. - nids[count++] = ciphers[i].nid;
  401. - }
  402. - put_dev_crypto(fd);
  403. + for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  404. + if (ciphers[i].nid == NID_undef)
  405. + continue;
  406. + sess.cipher = ciphers[i].id;
  407. + sess.keylen = ciphers[i].keylen;
  408. + sess.mac = 0;
  409. + if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
  410. + ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
  411. + nids[count++] = ciphers[i].nid;
  412. + }
  413. + put_dev_crypto(fd);
  414. - if (count > 0)
  415. - *cnids = nids;
  416. - else
  417. - *cnids = NULL;
  418. - return (count);
  419. + if (count > 0)
  420. + *cnids = nids;
  421. + else
  422. + *cnids = NULL;
  423. + return (count);
  424. }
  425. -# ifdef USE_CRYPTODEV_DIGESTS
  426. +#ifdef USE_CRYPTODEV_DIGESTS
  427. /*
  428. * Find out what digests /dev/crypto will let us have a session for.
  429. * XXX note, that some of these openssl doesn't deal with yet!
  430. * returning them here is harmless, as long as we return NULL
  431. * when asked for a handler in the cryptodev_engine_digests routine
  432. */
  433. -static int get_cryptodev_digests(const int **cnids)
  434. +static int
  435. +get_cryptodev_digests(const int **cnids)
  436. {
  437. - static int nids[CRYPTO_ALGORITHM_MAX];
  438. - struct session_op sess;
  439. - int fd, i, count = 0;
  440. + static int nids[CRYPTO_ALGORITHM_MAX];
  441. + unsigned char fake_key[EVP_MAX_KEY_LENGTH];
  442. + struct session_op sess;
  443. + int fd, i, count = 0;
  444. - if ((fd = get_dev_crypto()) < 0) {
  445. - *cnids = NULL;
  446. - return (0);
  447. - }
  448. - memset(&sess, 0, sizeof(sess));
  449. - sess.mackey = (caddr_t) "123456789abcdefghijklmno";
  450. - for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  451. - if (digests[i].nid == NID_undef)
  452. - continue;
  453. - sess.mac = digests[i].id;
  454. - sess.mackeylen = digests[i].keylen;
  455. - sess.cipher = 0;
  456. - if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
  457. - ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
  458. - nids[count++] = digests[i].nid;
  459. - }
  460. - put_dev_crypto(fd);
  461. + if ((fd = get_dev_crypto()) < 0) {
  462. + *cnids = NULL;
  463. + return (0);
  464. + }
  465. + memset(&sess, 0, sizeof(sess));
  466. + sess.mackey = fake_key;
  467. + for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
  468. + if (digests[i].nid == NID_undef)
  469. + continue;
  470. + sess.mac = digests[i].id;
  471. + sess.mackeylen = 8;
  472. + sess.cipher = 0;
  473. + if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
  474. + ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
  475. + nids[count++] = digests[i].nid;
  476. + }
  477. + put_dev_crypto(fd);
  478. - if (count > 0)
  479. - *cnids = nids;
  480. - else
  481. - *cnids = NULL;
  482. - return (count);
  483. + if (count > 0)
  484. + *cnids = nids;
  485. + else
  486. + *cnids = NULL;
  487. + return (count);
  488. }
  489. -# endif /* 0 */
  490. +#endif /* 0 */
  491. /*
  492. * Find the useable ciphers|digests from dev/crypto - this is the first
  493. @@ -375,158 +338,161 @@ static int get_cryptodev_digests(const i
  494. * want most of the decisions made about what we actually want
  495. * to use from /dev/crypto.
  496. */
  497. -static int cryptodev_usable_ciphers(const int **nids)
  498. +static int
  499. +cryptodev_usable_ciphers(const int **nids)
  500. {
  501. - return (get_cryptodev_ciphers(nids));
  502. + return (get_cryptodev_ciphers(nids));
  503. }
  504. -static int cryptodev_usable_digests(const int **nids)
  505. +static int
  506. +cryptodev_usable_digests(const int **nids)
  507. {
  508. -# ifdef USE_CRYPTODEV_DIGESTS
  509. - return (get_cryptodev_digests(nids));
  510. -# else
  511. - /*
  512. - * XXXX just disable all digests for now, because it sucks.
  513. - * we need a better way to decide this - i.e. I may not
  514. - * want digests on slow cards like hifn on fast machines,
  515. - * but might want them on slow or loaded machines, etc.
  516. - * will also want them when using crypto cards that don't
  517. - * suck moose gonads - would be nice to be able to decide something
  518. - * as reasonable default without having hackery that's card dependent.
  519. - * of course, the default should probably be just do everything,
  520. - * with perhaps a sysctl to turn algoritms off (or have them off
  521. - * by default) on cards that generally suck like the hifn.
  522. - */
  523. - *nids = NULL;
  524. - return (0);
  525. -# endif
  526. +#ifdef USE_CRYPTODEV_DIGESTS
  527. + return (get_cryptodev_digests(nids));
  528. +#else
  529. + /*
  530. + * XXXX just disable all digests for now, because it sucks.
  531. + * we need a better way to decide this - i.e. I may not
  532. + * want digests on slow cards like hifn on fast machines,
  533. + * but might want them on slow or loaded machines, etc.
  534. + * will also want them when using crypto cards that don't
  535. + * suck moose gonads - would be nice to be able to decide something
  536. + * as reasonable default without having hackery that's card dependent.
  537. + * of course, the default should probably be just do everything,
  538. + * with perhaps a sysctl to turn algoritms off (or have them off
  539. + * by default) on cards that generally suck like the hifn.
  540. + */
  541. + *nids = NULL;
  542. + return (0);
  543. +#endif
  544. }
  545. static int
  546. cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  547. - const unsigned char *in, size_t inl)
  548. + const unsigned char *in, size_t inl)
  549. {
  550. - struct crypt_op cryp;
  551. - struct dev_crypto_state *state = ctx->cipher_data;
  552. - struct session_op *sess = &state->d_sess;
  553. - const void *iiv;
  554. - unsigned char save_iv[EVP_MAX_IV_LENGTH];
  555. + struct crypt_op cryp;
  556. + struct dev_crypto_state *state = ctx->cipher_data;
  557. + struct session_op *sess = &state->d_sess;
  558. + const void *iiv;
  559. + unsigned char save_iv[EVP_MAX_IV_LENGTH];
  560. - if (state->d_fd < 0)
  561. - return (0);
  562. - if (!inl)
  563. - return (1);
  564. - if ((inl % ctx->cipher->block_size) != 0)
  565. - return (0);
  566. + if (state->d_fd < 0)
  567. + return (0);
  568. + if (!inl)
  569. + return (1);
  570. + if ((inl % ctx->cipher->block_size) != 0)
  571. + return (0);
  572. - memset(&cryp, 0, sizeof(cryp));
  573. + memset(&cryp, 0, sizeof(cryp));
  574. - cryp.ses = sess->ses;
  575. - cryp.flags = 0;
  576. - cryp.len = inl;
  577. - cryp.src = (caddr_t) in;
  578. - cryp.dst = (caddr_t) out;
  579. - cryp.mac = 0;
  580. + cryp.ses = sess->ses;
  581. + cryp.flags = 0;
  582. + cryp.len = inl;
  583. + cryp.src = (void*) in;
  584. + cryp.dst = (void*) out;
  585. + cryp.mac = 0;
  586. - cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
  587. + cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
  588. - if (ctx->cipher->iv_len) {
  589. - cryp.iv = (caddr_t) ctx->iv;
  590. - if (!ctx->encrypt) {
  591. - iiv = in + inl - ctx->cipher->iv_len;
  592. - memcpy(save_iv, iiv, ctx->cipher->iv_len);
  593. - }
  594. - } else
  595. - cryp.iv = NULL;
  596. + if (ctx->cipher->iv_len) {
  597. + cryp.iv = (void*) ctx->iv;
  598. + if (!ctx->encrypt) {
  599. + iiv = in + inl - ctx->cipher->iv_len;
  600. + memcpy(save_iv, iiv, ctx->cipher->iv_len);
  601. + }
  602. + } else
  603. + cryp.iv = NULL;
  604. - if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
  605. - /*
  606. - * XXX need better errror handling this can fail for a number of
  607. - * different reasons.
  608. - */
  609. - return (0);
  610. - }
  611. + if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
  612. + /* XXX need better errror handling
  613. + * this can fail for a number of different reasons.
  614. + */
  615. + return (0);
  616. + }
  617. - if (ctx->cipher->iv_len) {
  618. - if (ctx->encrypt)
  619. - iiv = out + inl - ctx->cipher->iv_len;
  620. - else
  621. - iiv = save_iv;
  622. - memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
  623. - }
  624. - return (1);
  625. + if (ctx->cipher->iv_len) {
  626. + if (ctx->encrypt)
  627. + iiv = out + inl - ctx->cipher->iv_len;
  628. + else
  629. + iiv = save_iv;
  630. + memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
  631. + }
  632. + return (1);
  633. }
  634. static int
  635. cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  636. - const unsigned char *iv, int enc)
  637. + const unsigned char *iv, int enc)
  638. {
  639. - struct dev_crypto_state *state = ctx->cipher_data;
  640. - struct session_op *sess = &state->d_sess;
  641. - int cipher = -1, i;
  642. + struct dev_crypto_state *state = ctx->cipher_data;
  643. + struct session_op *sess = &state->d_sess;
  644. + int cipher = -1, i;
  645. - for (i = 0; ciphers[i].id; i++)
  646. - if (ctx->cipher->nid == ciphers[i].nid &&
  647. - ctx->cipher->iv_len <= ciphers[i].ivmax &&
  648. - ctx->key_len == ciphers[i].keylen) {
  649. - cipher = ciphers[i].id;
  650. - break;
  651. - }
  652. + for (i = 0; ciphers[i].id; i++)
  653. + if (ctx->cipher->nid == ciphers[i].nid &&
  654. + ctx->cipher->iv_len <= ciphers[i].ivmax &&
  655. + ctx->key_len == ciphers[i].keylen) {
  656. + cipher = ciphers[i].id;
  657. + break;
  658. + }
  659. - if (!ciphers[i].id) {
  660. - state->d_fd = -1;
  661. - return (0);
  662. - }
  663. + if (!ciphers[i].id) {
  664. + state->d_fd = -1;
  665. + return (0);
  666. + }
  667. - memset(sess, 0, sizeof(struct session_op));
  668. + memset(sess, 0, sizeof(struct session_op));
  669. - if ((state->d_fd = get_dev_crypto()) < 0)
  670. - return (0);
  671. + if ((state->d_fd = get_dev_crypto()) < 0)
  672. + return (0);
  673. - sess->key = (caddr_t) key;
  674. - sess->keylen = ctx->key_len;
  675. - sess->cipher = cipher;
  676. + sess->key = (void*)key;
  677. + sess->keylen = ctx->key_len;
  678. + sess->cipher = cipher;
  679. - if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
  680. - put_dev_crypto(state->d_fd);
  681. - state->d_fd = -1;
  682. - return (0);
  683. - }
  684. - return (1);
  685. + if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
  686. + put_dev_crypto(state->d_fd);
  687. + state->d_fd = -1;
  688. + return (0);
  689. + }
  690. + return (1);
  691. }
  692. /*
  693. * free anything we allocated earlier when initting a
  694. * session, and close the session.
  695. */
  696. -static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
  697. +static int
  698. +cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
  699. {
  700. - int ret = 0;
  701. - struct dev_crypto_state *state = ctx->cipher_data;
  702. - struct session_op *sess = &state->d_sess;
  703. + int ret = 0;
  704. + struct dev_crypto_state *state = ctx->cipher_data;
  705. + struct session_op *sess = &state->d_sess;
  706. - if (state->d_fd < 0)
  707. - return (0);
  708. + if (state->d_fd < 0)
  709. + return (0);
  710. - /*
  711. - * XXX if this ioctl fails, someting's wrong. the invoker may have called
  712. - * us with a bogus ctx, or we could have a device that for whatever
  713. - * reason just doesn't want to play ball - it's not clear what's right
  714. - * here - should this be an error? should it just increase a counter,
  715. - * hmm. For right now, we return 0 - I don't believe that to be "right".
  716. - * we could call the gorpy openssl lib error handlers that print messages
  717. - * to users of the library. hmm..
  718. - */
  719. + /* XXX if this ioctl fails, someting's wrong. the invoker
  720. + * may have called us with a bogus ctx, or we could
  721. + * have a device that for whatever reason just doesn't
  722. + * want to play ball - it's not clear what's right
  723. + * here - should this be an error? should it just
  724. + * increase a counter, hmm. For right now, we return
  725. + * 0 - I don't believe that to be "right". we could
  726. + * call the gorpy openssl lib error handlers that
  727. + * print messages to users of the library. hmm..
  728. + */
  729. - if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
  730. - ret = 0;
  731. - } else {
  732. - ret = 1;
  733. - }
  734. - put_dev_crypto(state->d_fd);
  735. - state->d_fd = -1;
  736. + if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
  737. + ret = 0;
  738. + } else {
  739. + ret = 1;
  740. + }
  741. + put_dev_crypto(state->d_fd);
  742. + state->d_fd = -1;
  743. - return (ret);
  744. + return (ret);
  745. }
  746. /*
  747. @@ -536,151 +502,111 @@ static int cryptodev_cleanup(EVP_CIPHER_
  748. /* RC4 */
  749. const EVP_CIPHER cryptodev_rc4 = {
  750. - NID_rc4,
  751. - 1, 16, 0,
  752. - EVP_CIPH_VARIABLE_LENGTH,
  753. - cryptodev_init_key,
  754. - cryptodev_cipher,
  755. - cryptodev_cleanup,
  756. - sizeof(struct dev_crypto_state),
  757. - NULL,
  758. - NULL,
  759. - NULL
  760. + NID_rc4,
  761. + 1, 16, 0,
  762. + EVP_CIPH_VARIABLE_LENGTH,
  763. + cryptodev_init_key,
  764. + cryptodev_cipher,
  765. + cryptodev_cleanup,
  766. + sizeof(struct dev_crypto_state),
  767. + NULL,
  768. + NULL,
  769. + NULL
  770. };
  771. /* DES CBC EVP */
  772. const EVP_CIPHER cryptodev_des_cbc = {
  773. - NID_des_cbc,
  774. - 8, 8, 8,
  775. - EVP_CIPH_CBC_MODE,
  776. - cryptodev_init_key,
  777. - cryptodev_cipher,
  778. - cryptodev_cleanup,
  779. - sizeof(struct dev_crypto_state),
  780. - EVP_CIPHER_set_asn1_iv,
  781. - EVP_CIPHER_get_asn1_iv,
  782. - NULL
  783. + NID_des_cbc,
  784. + 8, 8, 8,
  785. + EVP_CIPH_CBC_MODE,
  786. + cryptodev_init_key,
  787. + cryptodev_cipher,
  788. + cryptodev_cleanup,
  789. + sizeof(struct dev_crypto_state),
  790. + EVP_CIPHER_set_asn1_iv,
  791. + EVP_CIPHER_get_asn1_iv,
  792. + NULL
  793. };
  794. /* 3DES CBC EVP */
  795. const EVP_CIPHER cryptodev_3des_cbc = {
  796. - NID_des_ede3_cbc,
  797. - 8, 24, 8,
  798. - EVP_CIPH_CBC_MODE,
  799. - cryptodev_init_key,
  800. - cryptodev_cipher,
  801. - cryptodev_cleanup,
  802. - sizeof(struct dev_crypto_state),
  803. - EVP_CIPHER_set_asn1_iv,
  804. - EVP_CIPHER_get_asn1_iv,
  805. - NULL
  806. + NID_des_ede3_cbc,
  807. + 8, 24, 8,
  808. + EVP_CIPH_CBC_MODE,
  809. + cryptodev_init_key,
  810. + cryptodev_cipher,
  811. + cryptodev_cleanup,
  812. + sizeof(struct dev_crypto_state),
  813. + EVP_CIPHER_set_asn1_iv,
  814. + EVP_CIPHER_get_asn1_iv,
  815. + NULL
  816. };
  817. const EVP_CIPHER cryptodev_bf_cbc = {
  818. - NID_bf_cbc,
  819. - 8, 16, 8,
  820. - EVP_CIPH_CBC_MODE,
  821. - cryptodev_init_key,
  822. - cryptodev_cipher,
  823. - cryptodev_cleanup,
  824. - sizeof(struct dev_crypto_state),
  825. - EVP_CIPHER_set_asn1_iv,
  826. - EVP_CIPHER_get_asn1_iv,
  827. - NULL
  828. + NID_bf_cbc,
  829. + 8, 16, 8,
  830. + EVP_CIPH_CBC_MODE,
  831. + cryptodev_init_key,
  832. + cryptodev_cipher,
  833. + cryptodev_cleanup,
  834. + sizeof(struct dev_crypto_state),
  835. + EVP_CIPHER_set_asn1_iv,
  836. + EVP_CIPHER_get_asn1_iv,
  837. + NULL
  838. };
  839. const EVP_CIPHER cryptodev_cast_cbc = {
  840. - NID_cast5_cbc,
  841. - 8, 16, 8,
  842. - EVP_CIPH_CBC_MODE,
  843. - cryptodev_init_key,
  844. - cryptodev_cipher,
  845. - cryptodev_cleanup,
  846. - sizeof(struct dev_crypto_state),
  847. - EVP_CIPHER_set_asn1_iv,
  848. - EVP_CIPHER_get_asn1_iv,
  849. - NULL
  850. + NID_cast5_cbc,
  851. + 8, 16, 8,
  852. + EVP_CIPH_CBC_MODE,
  853. + cryptodev_init_key,
  854. + cryptodev_cipher,
  855. + cryptodev_cleanup,
  856. + sizeof(struct dev_crypto_state),
  857. + EVP_CIPHER_set_asn1_iv,
  858. + EVP_CIPHER_get_asn1_iv,
  859. + NULL
  860. };
  861. const EVP_CIPHER cryptodev_aes_cbc = {
  862. - NID_aes_128_cbc,
  863. - 16, 16, 16,
  864. - EVP_CIPH_CBC_MODE,
  865. - cryptodev_init_key,
  866. - cryptodev_cipher,
  867. - cryptodev_cleanup,
  868. - sizeof(struct dev_crypto_state),
  869. - EVP_CIPHER_set_asn1_iv,
  870. - EVP_CIPHER_get_asn1_iv,
  871. - NULL
  872. + NID_aes_128_cbc,
  873. + 16, 16, 16,
  874. + EVP_CIPH_CBC_MODE,
  875. + cryptodev_init_key,
  876. + cryptodev_cipher,
  877. + cryptodev_cleanup,
  878. + sizeof(struct dev_crypto_state),
  879. + EVP_CIPHER_set_asn1_iv,
  880. + EVP_CIPHER_get_asn1_iv,
  881. + NULL
  882. };
  883. const EVP_CIPHER cryptodev_aes_192_cbc = {
  884. - NID_aes_192_cbc,
  885. - 16, 24, 16,
  886. - EVP_CIPH_CBC_MODE,
  887. - cryptodev_init_key,
  888. - cryptodev_cipher,
  889. - cryptodev_cleanup,
  890. - sizeof(struct dev_crypto_state),
  891. - EVP_CIPHER_set_asn1_iv,
  892. - EVP_CIPHER_get_asn1_iv,
  893. - NULL
  894. + NID_aes_192_cbc,
  895. + 16, 24, 16,
  896. + EVP_CIPH_CBC_MODE,
  897. + cryptodev_init_key,
  898. + cryptodev_cipher,
  899. + cryptodev_cleanup,
  900. + sizeof(struct dev_crypto_state),
  901. + EVP_CIPHER_set_asn1_iv,
  902. + EVP_CIPHER_get_asn1_iv,
  903. + NULL
  904. };
  905. const EVP_CIPHER cryptodev_aes_256_cbc = {
  906. - NID_aes_256_cbc,
  907. - 16, 32, 16,
  908. - EVP_CIPH_CBC_MODE,
  909. - cryptodev_init_key,
  910. - cryptodev_cipher,
  911. - cryptodev_cleanup,
  912. - sizeof(struct dev_crypto_state),
  913. - EVP_CIPHER_set_asn1_iv,
  914. - EVP_CIPHER_get_asn1_iv,
  915. - NULL
  916. -};
  917. -
  918. -# ifdef CRYPTO_AES_CTR
  919. -const EVP_CIPHER cryptodev_aes_ctr = {
  920. - NID_aes_128_ctr,
  921. - 16, 16, 14,
  922. - EVP_CIPH_CTR_MODE,
  923. - cryptodev_init_key,
  924. - cryptodev_cipher,
  925. - cryptodev_cleanup,
  926. - sizeof(struct dev_crypto_state),
  927. - EVP_CIPHER_set_asn1_iv,
  928. - EVP_CIPHER_get_asn1_iv,
  929. - NULL
  930. -};
  931. -
  932. -const EVP_CIPHER cryptodev_aes_ctr_192 = {
  933. - NID_aes_192_ctr,
  934. - 16, 24, 14,
  935. - EVP_CIPH_CTR_MODE,
  936. - cryptodev_init_key,
  937. - cryptodev_cipher,
  938. - cryptodev_cleanup,
  939. - sizeof(struct dev_crypto_state),
  940. - EVP_CIPHER_set_asn1_iv,
  941. - EVP_CIPHER_get_asn1_iv,
  942. - NULL
  943. + NID_aes_256_cbc,
  944. + 16, 32, 16,
  945. + EVP_CIPH_CBC_MODE,
  946. + cryptodev_init_key,
  947. + cryptodev_cipher,
  948. + cryptodev_cleanup,
  949. + sizeof(struct dev_crypto_state),
  950. + EVP_CIPHER_set_asn1_iv,
  951. + EVP_CIPHER_get_asn1_iv,
  952. + NULL
  953. };
  954. -const EVP_CIPHER cryptodev_aes_ctr_256 = {
  955. - NID_aes_256_ctr,
  956. - 16, 32, 14,
  957. - EVP_CIPH_CTR_MODE,
  958. - cryptodev_init_key,
  959. - cryptodev_cipher,
  960. - cryptodev_cleanup,
  961. - sizeof(struct dev_crypto_state),
  962. - EVP_CIPHER_set_asn1_iv,
  963. - EVP_CIPHER_get_asn1_iv,
  964. - NULL
  965. -};
  966. -# endif
  967. /*
  968. * Registered by the ENGINE when used to find out how to deal with
  969. * a particular NID in the ENGINE. this says what we'll do at the
  970. @@ -688,316 +614,353 @@ const EVP_CIPHER cryptodev_aes_ctr_256 =
  971. */
  972. static int
  973. cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  974. - const int **nids, int nid)
  975. + const int **nids, int nid)
  976. {
  977. - if (!cipher)
  978. - return (cryptodev_usable_ciphers(nids));
  979. + if (!cipher)
  980. + return (cryptodev_usable_ciphers(nids));
  981. - switch (nid) {
  982. - case NID_rc4:
  983. - *cipher = &cryptodev_rc4;
  984. - break;
  985. - case NID_des_ede3_cbc:
  986. - *cipher = &cryptodev_3des_cbc;
  987. - break;
  988. - case NID_des_cbc:
  989. - *cipher = &cryptodev_des_cbc;
  990. - break;
  991. - case NID_bf_cbc:
  992. - *cipher = &cryptodev_bf_cbc;
  993. - break;
  994. - case NID_cast5_cbc:
  995. - *cipher = &cryptodev_cast_cbc;
  996. - break;
  997. - case NID_aes_128_cbc:
  998. - *cipher = &cryptodev_aes_cbc;
  999. - break;
  1000. - case NID_aes_192_cbc:
  1001. - *cipher = &cryptodev_aes_192_cbc;
  1002. - break;
  1003. - case NID_aes_256_cbc:
  1004. - *cipher = &cryptodev_aes_256_cbc;
  1005. - break;
  1006. -# ifdef CRYPTO_AES_CTR
  1007. - case NID_aes_128_ctr:
  1008. - *cipher = &cryptodev_aes_ctr;
  1009. - break;
  1010. - case NID_aes_192_ctr:
  1011. - *cipher = &cryptodev_aes_ctr_192;
  1012. - break;
  1013. - case NID_aes_256_ctr:
  1014. - *cipher = &cryptodev_aes_ctr_256;
  1015. - break;
  1016. -# endif
  1017. - default:
  1018. - *cipher = NULL;
  1019. - break;
  1020. - }
  1021. - return (*cipher != NULL);
  1022. + switch (nid) {
  1023. + case NID_rc4:
  1024. + *cipher = &cryptodev_rc4;
  1025. + break;
  1026. + case NID_des_ede3_cbc:
  1027. + *cipher = &cryptodev_3des_cbc;
  1028. + break;
  1029. + case NID_des_cbc:
  1030. + *cipher = &cryptodev_des_cbc;
  1031. + break;
  1032. + case NID_bf_cbc:
  1033. + *cipher = &cryptodev_bf_cbc;
  1034. + break;
  1035. + case NID_cast5_cbc:
  1036. + *cipher = &cryptodev_cast_cbc;
  1037. + break;
  1038. + case NID_aes_128_cbc:
  1039. + *cipher = &cryptodev_aes_cbc;
  1040. + break;
  1041. + case NID_aes_192_cbc:
  1042. + *cipher = &cryptodev_aes_192_cbc;
  1043. + break;
  1044. + case NID_aes_256_cbc:
  1045. + *cipher = &cryptodev_aes_256_cbc;
  1046. + break;
  1047. + default:
  1048. + *cipher = NULL;
  1049. + break;
  1050. + }
  1051. + return (*cipher != NULL);
  1052. }
  1053. -# ifdef USE_CRYPTODEV_DIGESTS
  1054. +
  1055. +#ifdef USE_CRYPTODEV_DIGESTS
  1056. /* convert digest type to cryptodev */
  1057. -static int digest_nid_to_cryptodev(int nid)
  1058. +static int
  1059. +digest_nid_to_cryptodev(int nid)
  1060. {
  1061. - int i;
  1062. + int i;
  1063. - for (i = 0; digests[i].id; i++)
  1064. - if (digests[i].nid == nid)
  1065. - return (digests[i].id);
  1066. - return (0);
  1067. + for (i = 0; digests[i].id; i++)
  1068. + if (digests[i].nid == nid)
  1069. + return (digests[i].id);
  1070. + return (0);
  1071. }
  1072. -static int digest_key_length(int nid)
  1073. -{
  1074. - int i;
  1075. -
  1076. - for (i = 0; digests[i].id; i++)
  1077. - if (digests[i].nid == nid)
  1078. - return digests[i].keylen;
  1079. - return (0);
  1080. -}
  1081. static int cryptodev_digest_init(EVP_MD_CTX *ctx)
  1082. {
  1083. - struct dev_crypto_state *state = ctx->md_data;
  1084. - struct session_op *sess = &state->d_sess;
  1085. - int digest;
  1086. -
  1087. - if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef) {
  1088. - printf("cryptodev_digest_init: Can't get digest \n");
  1089. - return (0);
  1090. - }
  1091. + struct dev_crypto_state *state = ctx->md_data;
  1092. + struct session_op *sess = &state->d_sess;
  1093. + int digest;
  1094. - memset(state, 0, sizeof(struct dev_crypto_state));
  1095. + if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){
  1096. + printf("cryptodev_digest_init: Can't get digest \n");
  1097. + return (0);
  1098. + }
  1099. + memset(state, 0, sizeof(struct dev_crypto_state));
  1100. - if ((state->d_fd = get_dev_crypto()) < 0) {
  1101. - printf("cryptodev_digest_init: Can't get Dev \n");
  1102. - return (0);
  1103. - }
  1104. + if ((state->d_fd = get_dev_crypto()) < 0) {
  1105. + printf("cryptodev_digest_init: Can't get Dev \n");
  1106. + return (0);
  1107. + }
  1108. - sess->mackey = state->dummy_mac_key;
  1109. - sess->mackeylen = digest_key_length(ctx->digest->type);
  1110. - sess->mac = digest;
  1111. + sess->mackey = NULL;
  1112. + sess->mackeylen = 0;
  1113. + sess->mac = digest;
  1114. - if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
  1115. - put_dev_crypto(state->d_fd);
  1116. - state->d_fd = -1;
  1117. - printf("cryptodev_digest_init: Open session failed\n");
  1118. - return (0);
  1119. - }
  1120. + if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
  1121. + put_dev_crypto(state->d_fd);
  1122. + state->d_fd = -1;
  1123. + printf("cryptodev_digest_init: Open session failed\n");
  1124. + return (0);
  1125. + }
  1126. - return (1);
  1127. + return (1);
  1128. }
  1129. static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
  1130. - size_t count)
  1131. + size_t count)
  1132. {
  1133. - struct crypt_op cryp;
  1134. - struct dev_crypto_state *state = ctx->md_data;
  1135. - struct session_op *sess = &state->d_sess;
  1136. -
  1137. - if (!data || state->d_fd < 0) {
  1138. - printf("cryptodev_digest_update: illegal inputs \n");
  1139. - return (0);
  1140. - }
  1141. + struct dev_crypto_state *state = ctx->md_data;
  1142. + struct crypt_op cryp;
  1143. + struct session_op *sess = &state->d_sess;
  1144. - if (!count) {
  1145. - return (0);
  1146. - }
  1147. + if (!data || state->d_fd < 0) {
  1148. + printf("cryptodev_digest_update: illegal inputs \n");
  1149. + return (0);
  1150. + }
  1151. - if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
  1152. - /* if application doesn't support one buffer */
  1153. - state->mac_data =
  1154. - OPENSSL_realloc(state->mac_data, state->mac_len + count);
  1155. + if (!count) {
  1156. + return (1);
  1157. + }
  1158. - if (!state->mac_data) {
  1159. - printf("cryptodev_digest_update: realloc failed\n");
  1160. - return (0);
  1161. - }
  1162. + if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
  1163. + /* if application doesn't support one buffer */
  1164. + state->mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count);
  1165. - memcpy(state->mac_data + state->mac_len, data, count);
  1166. - state->mac_len += count;
  1167. + if (!state->mac_data) {
  1168. + printf("cryptodev_digest_update: realloc failed\n");
  1169. + return (0);
  1170. + }
  1171. - return (1);
  1172. - }
  1173. + memcpy(state->mac_data + state->mac_len, data, count);
  1174. + state->mac_len += count;
  1175. +
  1176. + return (1);
  1177. + }
  1178. - memset(&cryp, 0, sizeof(cryp));
  1179. + memset(&cryp, 0, sizeof(cryp));
  1180. - cryp.ses = sess->ses;
  1181. - cryp.flags = 0;
  1182. - cryp.len = count;
  1183. - cryp.src = (caddr_t) data;
  1184. - cryp.dst = NULL;
  1185. - cryp.mac = (caddr_t) state->digest_res;
  1186. - if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
  1187. - printf("cryptodev_digest_update: digest failed\n");
  1188. - return (0);
  1189. - }
  1190. - return (1);
  1191. + cryp.ses = sess->ses;
  1192. + cryp.flags = 0;
  1193. + cryp.len = count;
  1194. + cryp.src = (void*) data;
  1195. + cryp.dst = NULL;
  1196. + cryp.mac = (void*) state->digest_res;
  1197. + if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
  1198. + printf("cryptodev_digest_update: digest failed\n");
  1199. + return (0);
  1200. + }
  1201. + return (1);
  1202. }
  1203. +
  1204. static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md)
  1205. {
  1206. - struct crypt_op cryp;
  1207. - struct dev_crypto_state *state = ctx->md_data;
  1208. - struct session_op *sess = &state->d_sess;
  1209. -
  1210. - int ret = 1;
  1211. + struct crypt_op cryp;
  1212. + struct dev_crypto_state *state = ctx->md_data;
  1213. + struct session_op *sess = &state->d_sess;
  1214. - if (!md || state->d_fd < 0) {
  1215. - printf("cryptodev_digest_final: illegal input\n");
  1216. - return (0);
  1217. - }
  1218. + if (!md || state->d_fd < 0) {
  1219. + printf("cryptodev_digest_final: illegal input\n");
  1220. + return(0);
  1221. + }
  1222. - if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
  1223. - /* if application doesn't support one buffer */
  1224. - memset(&cryp, 0, sizeof(cryp));
  1225. - cryp.ses = sess->ses;
  1226. - cryp.flags = 0;
  1227. - cryp.len = state->mac_len;
  1228. - cryp.src = state->mac_data;
  1229. - cryp.dst = NULL;
  1230. - cryp.mac = (caddr_t) md;
  1231. - if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
  1232. - printf("cryptodev_digest_final: digest failed\n");
  1233. - return (0);
  1234. - }
  1235. + if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) {
  1236. + /* if application doesn't support one buffer */
  1237. + memset(&cryp, 0, sizeof(cryp));
  1238. + cryp.ses = sess->ses;
  1239. + cryp.flags = 0;
  1240. + cryp.len = state->mac_len;
  1241. + cryp.src = state->mac_data;
  1242. + cryp.dst = NULL;
  1243. + cryp.mac = (void*)md;
  1244. + if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
  1245. + printf("cryptodev_digest_final: digest failed\n");
  1246. + return (0);
  1247. + }
  1248. - return 1;
  1249. - }
  1250. + return 1;
  1251. + }
  1252. - memcpy(md, state->digest_res, ctx->digest->md_size);
  1253. + memcpy(md, state->digest_res, ctx->digest->md_size);
  1254. - return (ret);
  1255. + return 1;
  1256. }
  1257. +
  1258. static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx)
  1259. {
  1260. - int ret = 1;
  1261. - struct dev_crypto_state *state = ctx->md_data;
  1262. - struct session_op *sess = &state->d_sess;
  1263. + int ret = 1;
  1264. + struct dev_crypto_state *state = ctx->md_data;
  1265. + struct session_op *sess = &state->d_sess;
  1266. - if (state == NULL)
  1267. - return 0;
  1268. + if (state == NULL)
  1269. + return 0;
  1270. - if (state->d_fd < 0) {
  1271. - printf("cryptodev_digest_cleanup: illegal input\n");
  1272. - return (0);
  1273. - }
  1274. + if (state->d_fd < 0) {
  1275. + printf("cryptodev_digest_cleanup: illegal input\n");
  1276. + return (0);
  1277. + }
  1278. - if (state->mac_data) {
  1279. - OPENSSL_free(state->mac_data);
  1280. - state->mac_data = NULL;
  1281. - state->mac_len = 0;
  1282. - }
  1283. + if (state->mac_data) {
  1284. + OPENSSL_free(state->mac_data);
  1285. + state->mac_data = NULL;
  1286. + state->mac_len = 0;
  1287. + }
  1288. - if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
  1289. - printf("cryptodev_digest_cleanup: failed to close session\n");
  1290. - ret = 0;
  1291. - } else {
  1292. - ret = 1;
  1293. - }
  1294. - put_dev_crypto(state->d_fd);
  1295. - state->d_fd = -1;
  1296. + if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) {
  1297. + printf("cryptodev_digest_cleanup: failed to close session\n");
  1298. + ret = 0;
  1299. + } else {
  1300. + ret = 1;
  1301. + }
  1302. + put_dev_crypto(state->d_fd);
  1303. + state->d_fd = -1;
  1304. - return (ret);
  1305. + return (ret);
  1306. }
  1307. -static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
  1308. +static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
  1309. {
  1310. - struct dev_crypto_state *fstate = from->md_data;
  1311. - struct dev_crypto_state *dstate = to->md_data;
  1312. - struct session_op *sess;
  1313. - int digest;
  1314. + struct dev_crypto_state *fstate = from->md_data;
  1315. + struct dev_crypto_state *dstate = to->md_data;
  1316. + struct session_op *sess;
  1317. + int digest;
  1318. - if (dstate == NULL || fstate == NULL)
  1319. - return 1;
  1320. + if (dstate == NULL || fstate == NULL)
  1321. + return 1;
  1322. - memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
  1323. + memcpy(dstate, fstate, sizeof(struct dev_crypto_state));
  1324. - sess = &dstate->d_sess;
  1325. + sess = &dstate->d_sess;
  1326. - digest = digest_nid_to_cryptodev(to->digest->type);
  1327. + digest = digest_nid_to_cryptodev(to->digest->type);
  1328. - sess->mackey = dstate->dummy_mac_key;
  1329. - sess->mackeylen = digest_key_length(to->digest->type);
  1330. - sess->mac = digest;
  1331. + sess->mackey = NULL;
  1332. + sess->mackeylen = 0;
  1333. + sess->mac = digest;
  1334. - dstate->d_fd = get_dev_crypto();
  1335. + dstate->d_fd = get_dev_crypto();
  1336. - if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
  1337. - put_dev_crypto(dstate->d_fd);
  1338. - dstate->d_fd = -1;
  1339. - printf("cryptodev_digest_init: Open session failed\n");
  1340. - return (0);
  1341. - }
  1342. + if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) {
  1343. + put_dev_crypto(dstate->d_fd);
  1344. + dstate->d_fd = -1;
  1345. + printf("cryptodev_digest_init: Open session failed\n");
  1346. + return (0);
  1347. + }
  1348. - if (fstate->mac_len != 0) {
  1349. - if (fstate->mac_data != NULL) {
  1350. - dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
  1351. - memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
  1352. - dstate->mac_len = fstate->mac_len;
  1353. - }
  1354. - }
  1355. + if (fstate->mac_len != 0) {
  1356. + if (fstate->mac_data != NULL)
  1357. + {
  1358. + dstate->mac_data = OPENSSL_malloc(fstate->mac_len);
  1359. + memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len);
  1360. + dstate->mac_len = fstate->mac_len;
  1361. + }
  1362. + }
  1363. - return 1;
  1364. + return 1;
  1365. }
  1366. -const EVP_MD cryptodev_sha1 = {
  1367. - NID_sha1,
  1368. - NID_undef,
  1369. - SHA_DIGEST_LENGTH,
  1370. - EVP_MD_FLAG_ONESHOT,
  1371. - cryptodev_digest_init,
  1372. - cryptodev_digest_update,
  1373. - cryptodev_digest_final,
  1374. - cryptodev_digest_copy,
  1375. - cryptodev_digest_cleanup,
  1376. - EVP_PKEY_NULL_method,
  1377. - SHA_CBLOCK,
  1378. - sizeof(struct dev_crypto_state),
  1379. +
  1380. +static const EVP_MD cryptodev_sha1 = {
  1381. + NID_sha1,
  1382. + NID_sha1WithRSAEncryption,
  1383. + SHA_DIGEST_LENGTH,
  1384. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
  1385. + cryptodev_digest_init,
  1386. + cryptodev_digest_update,
  1387. + cryptodev_digest_final,
  1388. + cryptodev_digest_copy,
  1389. + cryptodev_digest_cleanup,
  1390. + EVP_PKEY_RSA_method,
  1391. + SHA_CBLOCK,
  1392. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  1393. };
  1394. -const EVP_MD cryptodev_md5 = {
  1395. - NID_md5,
  1396. - NID_undef,
  1397. - 16 /* MD5_DIGEST_LENGTH */ ,
  1398. - EVP_MD_FLAG_ONESHOT,
  1399. - cryptodev_digest_init,
  1400. - cryptodev_digest_update,
  1401. - cryptodev_digest_final,
  1402. - cryptodev_digest_copy,
  1403. - cryptodev_digest_cleanup,
  1404. - EVP_PKEY_NULL_method,
  1405. - 64 /* MD5_CBLOCK */ ,
  1406. - sizeof(struct dev_crypto_state),
  1407. +static const EVP_MD cryptodev_sha256 = {
  1408. + NID_sha256,
  1409. + NID_sha256WithRSAEncryption,
  1410. + SHA256_DIGEST_LENGTH,
  1411. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
  1412. + cryptodev_digest_init,
  1413. + cryptodev_digest_update,
  1414. + cryptodev_digest_final,
  1415. + cryptodev_digest_copy,
  1416. + cryptodev_digest_cleanup,
  1417. + EVP_PKEY_RSA_method,
  1418. + SHA256_CBLOCK,
  1419. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  1420. };
  1421. -# endif /* USE_CRYPTODEV_DIGESTS */
  1422. +static const EVP_MD cryptodev_sha384 = {
  1423. + NID_sha384,
  1424. + NID_sha384WithRSAEncryption,
  1425. + SHA384_DIGEST_LENGTH,
  1426. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
  1427. + cryptodev_digest_init,
  1428. + cryptodev_digest_update,
  1429. + cryptodev_digest_final,
  1430. + cryptodev_digest_copy,
  1431. + cryptodev_digest_cleanup,
  1432. + EVP_PKEY_RSA_method,
  1433. + SHA512_CBLOCK,
  1434. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  1435. +};
  1436. +
  1437. +static const EVP_MD cryptodev_sha512 = {
  1438. + NID_sha512,
  1439. + NID_sha512WithRSAEncryption,
  1440. + SHA512_DIGEST_LENGTH,
  1441. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
  1442. + cryptodev_digest_init,
  1443. + cryptodev_digest_update,
  1444. + cryptodev_digest_final,
  1445. + cryptodev_digest_copy,
  1446. + cryptodev_digest_cleanup,
  1447. + EVP_PKEY_RSA_method,
  1448. + SHA512_CBLOCK,
  1449. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  1450. +};
  1451. +
  1452. +static const EVP_MD cryptodev_md5 = {
  1453. + NID_md5,
  1454. + NID_md5WithRSAEncryption,
  1455. + 16 /* MD5_DIGEST_LENGTH */,
  1456. + EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
  1457. + cryptodev_digest_init,
  1458. + cryptodev_digest_update,
  1459. + cryptodev_digest_final,
  1460. + cryptodev_digest_copy,
  1461. + cryptodev_digest_cleanup,
  1462. + EVP_PKEY_RSA_method,
  1463. + 64 /* MD5_CBLOCK */,
  1464. + sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
  1465. +};
  1466. +
  1467. +#endif /* USE_CRYPTODEV_DIGESTS */
  1468. +
  1469. static int
  1470. cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
  1471. - const int **nids, int nid)
  1472. + const int **nids, int nid)
  1473. {
  1474. - if (!digest)
  1475. - return (cryptodev_usable_digests(nids));
  1476. + if (!digest)
  1477. + return (cryptodev_usable_digests(nids));
  1478. - switch (nid) {
  1479. -# ifdef USE_CRYPTODEV_DIGESTS
  1480. - case NID_md5:
  1481. - *digest = &cryptodev_md5;
  1482. - break;
  1483. - case NID_sha1:
  1484. - *digest = &cryptodev_sha1;
  1485. - break;
  1486. - default:
  1487. -# endif /* USE_CRYPTODEV_DIGESTS */
  1488. - *digest = NULL;
  1489. - break;
  1490. - }
  1491. - return (*digest != NULL);
  1492. + switch (nid) {
  1493. +#ifdef USE_CRYPTODEV_DIGESTS
  1494. + case NID_md5:
  1495. + *digest = &cryptodev_md5;
  1496. + break;
  1497. + case NID_sha1:
  1498. + *digest = &cryptodev_sha1;
  1499. + break;
  1500. + case NID_sha256:
  1501. + *digest = &cryptodev_sha256;
  1502. + break;
  1503. + case NID_sha384:
  1504. + *digest = &cryptodev_sha384;
  1505. + break;
  1506. + case NID_sha512:
  1507. + *digest = &cryptodev_sha512;
  1508. + break;
  1509. + default:
  1510. +#endif /* USE_CRYPTODEV_DIGESTS */
  1511. + *digest = NULL;
  1512. + break;
  1513. + }
  1514. + return (*digest != NULL);
  1515. }
  1516. /*
  1517. @@ -1005,423 +968,419 @@ cryptodev_engine_digests(ENGINE *e, cons
  1518. * Upon completion of use, the caller is responsible for freeing
  1519. * crp->crp_p.
  1520. */
  1521. -static int bn2crparam(const BIGNUM *a, struct crparam *crp)
  1522. +static int
  1523. +bn2crparam(const BIGNUM *a, struct crparam *crp)
  1524. {
  1525. - int i, j, k;
  1526. - ssize_t bytes, bits;
  1527. - u_char *b;
  1528. + int i, j, k;
  1529. + ssize_t bytes, bits;
  1530. + u_char *b;
  1531. - crp->crp_p = NULL;
  1532. - crp->crp_nbits = 0;
  1533. + crp->crp_p = NULL;
  1534. + crp->crp_nbits = 0;
  1535. - bits = BN_num_bits(a);
  1536. - bytes = (bits + 7) / 8;
  1537. + bits = BN_num_bits(a);
  1538. + bytes = (bits + 7) / 8;
  1539. - b = malloc(bytes);
  1540. - if (b == NULL)
  1541. - return (1);
  1542. - memset(b, 0, bytes);
  1543. + b = malloc(bytes);
  1544. + if (b == NULL)
  1545. + return (1);
  1546. + memset(b, 0, bytes);
  1547. - crp->crp_p = (caddr_t) b;
  1548. - crp->crp_nbits = bits;
  1549. + crp->crp_p = (void*) b;
  1550. + crp->crp_nbits = bits;
  1551. - for (i = 0, j = 0; i < a->top; i++) {
  1552. - for (k = 0; k < BN_BITS2 / 8; k++) {
  1553. - if ((j + k) >= bytes)
  1554. - return (0);
  1555. - b[j + k] = a->d[i] >> (k * 8);
  1556. - }
  1557. - j += BN_BITS2 / 8;
  1558. - }
  1559. - return (0);
  1560. + for (i = 0, j = 0; i < a->top; i++) {
  1561. + for (k = 0; k < BN_BITS2 / 8; k++) {
  1562. + if ((j + k) >= bytes)
  1563. + return (0);
  1564. + b[j + k] = a->d[i] >> (k * 8);
  1565. + }
  1566. + j += BN_BITS2 / 8;
  1567. + }
  1568. + return (0);
  1569. }
  1570. /* Convert a /dev/crypto parameter to a BIGNUM */
  1571. -static int crparam2bn(struct crparam *crp, BIGNUM *a)
  1572. +static int
  1573. +crparam2bn(struct crparam *crp, BIGNUM *a)
  1574. {
  1575. - u_int8_t *pd;
  1576. - int i, bytes;
  1577. + u_int8_t *pd;
  1578. + int i, bytes;
  1579. - bytes = (crp->crp_nbits + 7) / 8;
  1580. + bytes = (crp->crp_nbits + 7) / 8;
  1581. - if (bytes == 0)
  1582. - return (-1);
  1583. + if (bytes == 0)
  1584. + return (-1);
  1585. - if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
  1586. - return (-1);
  1587. + if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
  1588. + return (-1);
  1589. - for (i = 0; i < bytes; i++)
  1590. - pd[i] = crp->crp_p[bytes - i - 1];
  1591. + for (i = 0; i < bytes; i++)
  1592. + pd[i] = crp->crp_p[bytes - i - 1];
  1593. - BN_bin2bn(pd, bytes, a);
  1594. - free(pd);
  1595. + BN_bin2bn(pd, bytes, a);
  1596. + free(pd);
  1597. - return (0);
  1598. + return (0);
  1599. }
  1600. -static void zapparams(struct crypt_kop *kop)
  1601. +static void
  1602. +zapparams(struct crypt_kop *kop)
  1603. {
  1604. - int i;
  1605. + int i;
  1606. - for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
  1607. - if (kop->crk_param[i].crp_p)
  1608. - free(kop->crk_param[i].crp_p);
  1609. - kop->crk_param[i].crp_p = NULL;
  1610. - kop->crk_param[i].crp_nbits = 0;
  1611. - }
  1612. + for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) {
  1613. + if (kop->crk_param[i].crp_p)
  1614. + free(kop->crk_param[i].crp_p);
  1615. + kop->crk_param[i].crp_p = NULL;
  1616. + kop->crk_param[i].crp_nbits = 0;
  1617. + }
  1618. }
  1619. static int
  1620. -cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen,
  1621. - BIGNUM *s)
  1622. +cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
  1623. {
  1624. - int fd, ret = -1;
  1625. + int fd, ret = -1;
  1626. - if ((fd = get_asym_dev_crypto()) < 0)
  1627. - return (ret);
  1628. + if ((fd = get_asym_dev_crypto()) < 0)
  1629. + return (ret);
  1630. - if (r) {
  1631. - kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
  1632. - kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
  1633. - kop->crk_oparams++;
  1634. - }
  1635. - if (s) {
  1636. - kop->crk_param[kop->crk_iparams + 1].crp_p =
  1637. - calloc(slen, sizeof(char));
  1638. - kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8;
  1639. - kop->crk_oparams++;
  1640. - }
  1641. + if (r) {
  1642. + kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
  1643. + kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
  1644. + kop->crk_oparams++;
  1645. + }
  1646. + if (s) {
  1647. + kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
  1648. + kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
  1649. + kop->crk_oparams++;
  1650. + }
  1651. - if (ioctl(fd, CIOCKEY, kop) == 0) {
  1652. - if (r)
  1653. - crparam2bn(&kop->crk_param[kop->crk_iparams], r);
  1654. - if (s)
  1655. - crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s);
  1656. - ret = 0;
  1657. - }
  1658. + if (ioctl(fd, CIOCKEY, kop) == 0) {
  1659. + if (r)
  1660. + crparam2bn(&kop->crk_param[kop->crk_iparams], r);
  1661. + if (s)
  1662. + crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
  1663. + ret = 0;
  1664. + }
  1665. - return (ret);
  1666. + return (ret);
  1667. }
  1668. static int
  1669. cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  1670. - const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
  1671. + const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
  1672. {
  1673. - struct crypt_kop kop;
  1674. - int ret = 1;
  1675. + struct crypt_kop kop;
  1676. + int ret = 1;
  1677. - /*
  1678. - * Currently, we know we can do mod exp iff we can do any asymmetric
  1679. - * operations at all.
  1680. - */
  1681. - if (cryptodev_asymfeat == 0) {
  1682. - ret = BN_mod_exp(r, a, p, m, ctx);
  1683. - return (ret);
  1684. - }
  1685. + /* Currently, we know we can do mod exp iff we can do any
  1686. + * asymmetric operations at all.
  1687. + */
  1688. + if (cryptodev_asymfeat == 0) {
  1689. + ret = BN_mod_exp(r, a, p, m, ctx);
  1690. + return (ret);
  1691. + }
  1692. - memset(&kop, 0, sizeof kop);
  1693. - kop.crk_op = CRK_MOD_EXP;
  1694. + memset(&kop, 0, sizeof kop);
  1695. + kop.crk_op = CRK_MOD_EXP;
  1696. - /* inputs: a^p % m */
  1697. - if (bn2crparam(a, &kop.crk_param[0]))
  1698. - goto err;
  1699. - if (bn2crparam(p, &kop.crk_param[1]))
  1700. - goto err;
  1701. - if (bn2crparam(m, &kop.crk_param[2]))
  1702. - goto err;
  1703. - kop.crk_iparams = 3;
  1704. + /* inputs: a^p % m */
  1705. + if (bn2crparam(a, &kop.crk_param[0]))
  1706. + goto err;
  1707. + if (bn2crparam(p, &kop.crk_param[1]))
  1708. + goto err;
  1709. + if (bn2crparam(m, &kop.crk_param[2]))
  1710. + goto err;
  1711. + kop.crk_iparams = 3;
  1712. - if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
  1713. - const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1714. - printf("OCF asym process failed, Running in software\n");
  1715. - ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
  1716. + if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) {
  1717. + const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1718. + printf("OCF asym process failed, Running in software\n");
  1719. + ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
  1720. - } else if (ECANCELED == kop.crk_status) {
  1721. - const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1722. - printf("OCF hardware operation cancelled. Running in Software\n");
  1723. - ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
  1724. - }
  1725. - /* else cryptodev operation worked ok ==> ret = 1 */
  1726. + } else if (ECANCELED == kop.crk_status) {
  1727. + const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1728. + printf("OCF hardware operation cancelled. Running in Software\n");
  1729. + ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
  1730. + }
  1731. + /* else cryptodev operation worked ok ==> ret = 1*/
  1732. - err:
  1733. - zapparams(&kop);
  1734. - return (ret);
  1735. +err:
  1736. + zapparams(&kop);
  1737. + return (ret);
  1738. }
  1739. static int
  1740. -cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  1741. - BN_CTX *ctx)
  1742. +cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  1743. {
  1744. - int r;
  1745. - ctx = BN_CTX_new();
  1746. - r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
  1747. - BN_CTX_free(ctx);
  1748. - return (r);
  1749. + int r;
  1750. + ctx = BN_CTX_new();
  1751. + r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
  1752. + BN_CTX_free(ctx);
  1753. + return (r);
  1754. }
  1755. static int
  1756. cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  1757. {
  1758. - struct crypt_kop kop;
  1759. - int ret = 1;
  1760. + struct crypt_kop kop;
  1761. + int ret = 1;
  1762. - if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
  1763. - /* XXX 0 means failure?? */
  1764. - return (0);
  1765. - }
  1766. + if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
  1767. + /* XXX 0 means failure?? */
  1768. + return (0);
  1769. + }
  1770. - memset(&kop, 0, sizeof kop);
  1771. - kop.crk_op = CRK_MOD_EXP_CRT;
  1772. - /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
  1773. - if (bn2crparam(rsa->p, &kop.crk_param[0]))
  1774. - goto err;
  1775. - if (bn2crparam(rsa->q, &kop.crk_param[1]))
  1776. - goto err;
  1777. - if (bn2crparam(I, &kop.crk_param[2]))
  1778. - goto err;
  1779. - if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
  1780. - goto err;
  1781. - if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
  1782. - goto err;
  1783. - if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
  1784. - goto err;
  1785. - kop.crk_iparams = 6;
  1786. + memset(&kop, 0, sizeof kop);
  1787. + kop.crk_op = CRK_MOD_EXP_CRT;
  1788. + /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
  1789. + if (bn2crparam(rsa->p, &kop.crk_param[0]))
  1790. + goto err;
  1791. + if (bn2crparam(rsa->q, &kop.crk_param[1]))
  1792. + goto err;
  1793. + if (bn2crparam(I, &kop.crk_param[2]))
  1794. + goto err;
  1795. + if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
  1796. + goto err;
  1797. + if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
  1798. + goto err;
  1799. + if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
  1800. + goto err;
  1801. + kop.crk_iparams = 6;
  1802. - if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
  1803. - const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1804. - printf("OCF asym process failed, running in Software\n");
  1805. - ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
  1806. + if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) {
  1807. + const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1808. + printf("OCF asym process failed, running in Software\n");
  1809. + ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
  1810. - } else if (ECANCELED == kop.crk_status) {
  1811. - const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1812. - printf("OCF hardware operation cancelled. Running in Software\n");
  1813. - ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx);
  1814. - }
  1815. - /* else cryptodev operation worked ok ==> ret = 1 */
  1816. + } else if (ECANCELED == kop.crk_status) {
  1817. + const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  1818. + printf("OCF hardware operation cancelled. Running in Software\n");
  1819. + ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
  1820. + }
  1821. + /* else cryptodev operation worked ok ==> ret = 1*/
  1822. - err:
  1823. - zapparams(&kop);
  1824. - return (ret);
  1825. +err:
  1826. + zapparams(&kop);
  1827. + return (ret);
  1828. }
  1829. static RSA_METHOD cryptodev_rsa = {
  1830. - "cryptodev RSA method",
  1831. - NULL, /* rsa_pub_enc */
  1832. - NULL, /* rsa_pub_dec */
  1833. - NULL, /* rsa_priv_enc */
  1834. - NULL, /* rsa_priv_dec */
  1835. - NULL,
  1836. - NULL,
  1837. - NULL, /* init */
  1838. - NULL, /* finish */
  1839. - 0, /* flags */
  1840. - NULL, /* app_data */
  1841. - NULL, /* rsa_sign */
  1842. - NULL /* rsa_verify */
  1843. + "cryptodev RSA method",
  1844. + NULL, /* rsa_pub_enc */
  1845. + NULL, /* rsa_pub_dec */
  1846. + NULL, /* rsa_priv_enc */
  1847. + NULL, /* rsa_priv_dec */
  1848. + NULL,
  1849. + NULL,
  1850. + NULL, /* init */
  1851. + NULL, /* finish */
  1852. + 0, /* flags */
  1853. + NULL, /* app_data */
  1854. + NULL, /* rsa_sign */
  1855. + NULL /* rsa_verify */
  1856. };
  1857. static int
  1858. cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
  1859. - const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  1860. + const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  1861. {
  1862. - return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
  1863. + return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
  1864. }
  1865. static int
  1866. cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
  1867. - BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
  1868. - BN_CTX *ctx, BN_MONT_CTX *mont)
  1869. + BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
  1870. + BN_CTX *ctx, BN_MONT_CTX *mont)
  1871. {
  1872. - BIGNUM t2;
  1873. - int ret = 0;
  1874. + BIGNUM t2;
  1875. + int ret = 0;
  1876. - BN_init(&t2);
  1877. + BN_init(&t2);
  1878. - /* v = ( g^u1 * y^u2 mod p ) mod q */
  1879. - /* let t1 = g ^ u1 mod p */
  1880. - ret = 0;
  1881. + /* v = ( g^u1 * y^u2 mod p ) mod q */
  1882. + /* let t1 = g ^ u1 mod p */
  1883. + ret = 0;
  1884. - if (!dsa->meth->bn_mod_exp(dsa, t1, dsa->g, u1, dsa->p, ctx, mont))
  1885. - goto err;
  1886. + if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
  1887. + goto err;
  1888. - /* let t2 = y ^ u2 mod p */
  1889. - if (!dsa->meth->bn_mod_exp(dsa, &t2, dsa->pub_key, u2, dsa->p, ctx, mont))
  1890. - goto err;
  1891. - /* let u1 = t1 * t2 mod p */
  1892. - if (!BN_mod_mul(u1, t1, &t2, dsa->p, ctx))
  1893. - goto err;
  1894. + /* let t2 = y ^ u2 mod p */
  1895. + if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
  1896. + goto err;
  1897. + /* let u1 = t1 * t2 mod p */
  1898. + if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
  1899. + goto err;
  1900. - BN_copy(t1, u1);
  1901. + BN_copy(t1,u1);
  1902. - ret = 1;
  1903. - err:
  1904. - BN_free(&t2);
  1905. - return (ret);
  1906. + ret = 1;
  1907. +err:
  1908. + BN_free(&t2);
  1909. + return(ret);
  1910. }
  1911. -static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen,
  1912. - DSA *dsa)
  1913. +static DSA_SIG *
  1914. +cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
  1915. {
  1916. - struct crypt_kop kop;
  1917. - BIGNUM *r = NULL, *s = NULL;
  1918. - DSA_SIG *dsaret = NULL;
  1919. + struct crypt_kop kop;
  1920. + BIGNUM *r = NULL, *s = NULL;
  1921. + DSA_SIG *dsaret = NULL;
  1922. - if ((r = BN_new()) == NULL)
  1923. - goto err;
  1924. - if ((s = BN_new()) == NULL) {
  1925. - BN_free(r);
  1926. - goto err;
  1927. - }
  1928. + if ((r = BN_new()) == NULL)
  1929. + goto err;
  1930. + if ((s = BN_new()) == NULL) {
  1931. + BN_free(r);
  1932. + goto err;
  1933. + }
  1934. - memset(&kop, 0, sizeof kop);
  1935. - kop.crk_op = CRK_DSA_SIGN;
  1936. + memset(&kop, 0, sizeof kop);
  1937. + kop.crk_op = CRK_DSA_SIGN;
  1938. - /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
  1939. - kop.crk_param[0].crp_p = (caddr_t) dgst;
  1940. - kop.crk_param[0].crp_nbits = dlen * 8;
  1941. - if (bn2crparam(dsa->p, &kop.crk_param[1]))
  1942. - goto err;
  1943. - if (bn2crparam(dsa->q, &kop.crk_param[2]))
  1944. - goto err;
  1945. - if (bn2crparam(dsa->g, &kop.crk_param[3]))
  1946. - goto err;
  1947. - if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
  1948. - goto err;
  1949. - kop.crk_iparams = 5;
  1950. + /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
  1951. + kop.crk_param[0].crp_p = (void*)dgst;
  1952. + kop.crk_param[0].crp_nbits = dlen * 8;
  1953. + if (bn2crparam(dsa->p, &kop.crk_param[1]))
  1954. + goto err;
  1955. + if (bn2crparam(dsa->q, &kop.crk_param[2]))
  1956. + goto err;
  1957. + if (bn2crparam(dsa->g, &kop.crk_param[3]))
  1958. + goto err;
  1959. + if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
  1960. + goto err;
  1961. + kop.crk_iparams = 5;
  1962. - if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
  1963. - BN_num_bytes(dsa->q), s) == 0) {
  1964. - dsaret = DSA_SIG_new();
  1965. - dsaret->r = r;
  1966. - dsaret->s = s;
  1967. - } else {
  1968. - const DSA_METHOD *meth = DSA_OpenSSL();
  1969. - BN_free(r);
  1970. - BN_free(s);
  1971. - dsaret = (meth->dsa_do_sign) (dgst, dlen, dsa);
  1972. - }
  1973. - err:
  1974. - kop.crk_param[0].crp_p = NULL;
  1975. - zapparams(&kop);
  1976. - return (dsaret);
  1977. + if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
  1978. + BN_num_bytes(dsa->q), s) == 0) {
  1979. + dsaret = DSA_SIG_new();
  1980. + dsaret->r = r;
  1981. + dsaret->s = s;
  1982. + } else {
  1983. + const DSA_METHOD *meth = DSA_OpenSSL();
  1984. + BN_free(r);
  1985. + BN_free(s);
  1986. + dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
  1987. + }
  1988. +err:
  1989. + kop.crk_param[0].crp_p = NULL;
  1990. + zapparams(&kop);
  1991. + return (dsaret);
  1992. }
  1993. static int
  1994. cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
  1995. - DSA_SIG *sig, DSA *dsa)
  1996. + DSA_SIG *sig, DSA *dsa)
  1997. {
  1998. - struct crypt_kop kop;
  1999. - int dsaret = 1;
  2000. + struct crypt_kop kop;
  2001. + int dsaret = 1;
  2002. - memset(&kop, 0, sizeof kop);
  2003. - kop.crk_op = CRK_DSA_VERIFY;
  2004. + memset(&kop, 0, sizeof kop);
  2005. + kop.crk_op = CRK_DSA_VERIFY;
  2006. - /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
  2007. - kop.crk_param[0].crp_p = (caddr_t) dgst;
  2008. - kop.crk_param[0].crp_nbits = dlen * 8;
  2009. - if (bn2crparam(dsa->p, &kop.crk_param[1]))
  2010. - goto err;
  2011. - if (bn2crparam(dsa->q, &kop.crk_param[2]))
  2012. - goto err;
  2013. - if (bn2crparam(dsa->g, &kop.crk_param[3]))
  2014. - goto err;
  2015. - if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
  2016. - goto err;
  2017. - if (bn2crparam(sig->r, &kop.crk_param[5]))
  2018. - goto err;
  2019. - if (bn2crparam(sig->s, &kop.crk_param[6]))
  2020. - goto err;
  2021. - kop.crk_iparams = 7;
  2022. + /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
  2023. + kop.crk_param[0].crp_p = (void*)dgst;
  2024. + kop.crk_param[0].crp_nbits = dlen * 8;
  2025. + if (bn2crparam(dsa->p, &kop.crk_param[1]))
  2026. + goto err;
  2027. + if (bn2crparam(dsa->q, &kop.crk_param[2]))
  2028. + goto err;
  2029. + if (bn2crparam(dsa->g, &kop.crk_param[3]))
  2030. + goto err;
  2031. + if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
  2032. + goto err;
  2033. + if (bn2crparam(sig->r, &kop.crk_param[5]))
  2034. + goto err;
  2035. + if (bn2crparam(sig->s, &kop.crk_param[6]))
  2036. + goto err;
  2037. + kop.crk_iparams = 7;
  2038. - if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
  2039. - /*
  2040. - * OCF success value is 0, if not zero, change dsaret to fail
  2041. - */
  2042. - if (0 != kop.crk_status)
  2043. - dsaret = 0;
  2044. - } else {
  2045. - const DSA_METHOD *meth = DSA_OpenSSL();
  2046. + if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
  2047. +/*OCF success value is 0, if not zero, change dsaret to fail*/
  2048. + if(0 != kop.crk_status) dsaret = 0;
  2049. + } else {
  2050. + const DSA_METHOD *meth = DSA_OpenSSL();
  2051. - dsaret = (meth->dsa_do_verify) (dgst, dlen, sig, dsa);
  2052. - }
  2053. - err:
  2054. - kop.crk_param[0].crp_p = NULL;
  2055. - zapparams(&kop);
  2056. - return (dsaret);
  2057. + dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
  2058. + }
  2059. +err:
  2060. + kop.crk_param[0].crp_p = NULL;
  2061. + zapparams(&kop);
  2062. + return (dsaret);
  2063. }
  2064. static DSA_METHOD cryptodev_dsa = {
  2065. - "cryptodev DSA method",
  2066. - NULL,
  2067. - NULL, /* dsa_sign_setup */
  2068. - NULL,
  2069. - NULL, /* dsa_mod_exp */
  2070. - NULL,
  2071. - NULL, /* init */
  2072. - NULL, /* finish */
  2073. - 0, /* flags */
  2074. - NULL /* app_data */
  2075. + "cryptodev DSA method",
  2076. + NULL,
  2077. + NULL, /* dsa_sign_setup */
  2078. + NULL,
  2079. + NULL, /* dsa_mod_exp */
  2080. + NULL,
  2081. + NULL, /* init */
  2082. + NULL, /* finish */
  2083. + 0, /* flags */
  2084. + NULL /* app_data */
  2085. };
  2086. static int
  2087. cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  2088. - const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  2089. - BN_MONT_CTX *m_ctx)
  2090. + const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  2091. + BN_MONT_CTX *m_ctx)
  2092. {
  2093. - return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
  2094. + return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
  2095. }
  2096. static int
  2097. cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  2098. {
  2099. - struct crypt_kop kop;
  2100. - int dhret = 1;
  2101. - int fd, keylen;
  2102. + struct crypt_kop kop;
  2103. + int dhret = 1;
  2104. + int fd, keylen;
  2105. - if ((fd = get_asym_dev_crypto()) < 0) {
  2106. - const DH_METHOD *meth = DH_OpenSSL();
  2107. + if ((fd = get_asym_dev_crypto()) < 0) {
  2108. + const DH_METHOD *meth = DH_OpenSSL();
  2109. - return ((meth->compute_key) (key, pub_key, dh));
  2110. - }
  2111. + return ((meth->compute_key)(key, pub_key, dh));
  2112. + }
  2113. - keylen = BN_num_bits(dh->p);
  2114. + keylen = BN_num_bits(dh->p);
  2115. - memset(&kop, 0, sizeof kop);
  2116. - kop.crk_op = CRK_DH_COMPUTE_KEY;
  2117. + memset(&kop, 0, sizeof kop);
  2118. + kop.crk_op = CRK_DH_COMPUTE_KEY;
  2119. - /* inputs: dh->priv_key pub_key dh->p key */
  2120. - if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
  2121. - goto err;
  2122. - if (bn2crparam(pub_key, &kop.crk_param[1]))
  2123. - goto err;
  2124. - if (bn2crparam(dh->p, &kop.crk_param[2]))
  2125. - goto err;
  2126. - kop.crk_iparams = 3;
  2127. + /* inputs: dh->priv_key pub_key dh->p key */
  2128. + if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
  2129. + goto err;
  2130. + if (bn2crparam(pub_key, &kop.crk_param[1]))
  2131. + goto err;
  2132. + if (bn2crparam(dh->p, &kop.crk_param[2]))
  2133. + goto err;
  2134. + kop.crk_iparams = 3;
  2135. - kop.crk_param[3].crp_p = (caddr_t) key;
  2136. - kop.crk_param[3].crp_nbits = keylen * 8;
  2137. - kop.crk_oparams = 1;
  2138. + kop.crk_param[3].crp_p = (void*) key;
  2139. + kop.crk_param[3].crp_nbits = keylen * 8;
  2140. + kop.crk_oparams = 1;
  2141. - if (ioctl(fd, CIOCKEY, &kop) == -1) {
  2142. - const DH_METHOD *meth = DH_OpenSSL();
  2143. + if (ioctl(fd, CIOCKEY, &kop) == -1) {
  2144. + const DH_METHOD *meth = DH_OpenSSL();
  2145. - dhret = (meth->compute_key) (key, pub_key, dh);
  2146. - }
  2147. - err:
  2148. - kop.crk_param[3].crp_p = NULL;
  2149. - zapparams(&kop);
  2150. - return (dhret);
  2151. + dhret = (meth->compute_key)(key, pub_key, dh);
  2152. + }
  2153. +err:
  2154. + kop.crk_param[3].crp_p = NULL;
  2155. + zapparams(&kop);
  2156. + return (dhret);
  2157. }
  2158. static DH_METHOD cryptodev_dh = {
  2159. - "cryptodev DH method",
  2160. - NULL, /* cryptodev_dh_generate_key */
  2161. - NULL,
  2162. - NULL,
  2163. - NULL,
  2164. - NULL,
  2165. - 0, /* flags */
  2166. - NULL /* app_data */
  2167. + "cryptodev DH method",
  2168. + NULL, /* cryptodev_dh_generate_key */
  2169. + NULL,
  2170. + NULL,
  2171. + NULL,
  2172. + NULL,
  2173. + 0, /* flags */
  2174. + NULL /* app_data */
  2175. };
  2176. /*
  2177. @@ -1429,104 +1388,109 @@ static DH_METHOD cryptodev_dh = {
  2178. * but I expect we'll want some options soon.
  2179. */
  2180. static int
  2181. -cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  2182. +cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
  2183. {
  2184. -# ifdef HAVE_SYSLOG_R
  2185. - struct syslog_data sd = SYSLOG_DATA_INIT;
  2186. -# endif
  2187. +#ifdef HAVE_SYSLOG_R
  2188. + struct syslog_data sd = SYSLOG_DATA_INIT;
  2189. +#endif
  2190. - switch (cmd) {
  2191. - default:
  2192. -# ifdef HAVE_SYSLOG_R
  2193. - syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd);
  2194. -# else
  2195. - syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
  2196. -# endif
  2197. - break;
  2198. - }
  2199. - return (1);
  2200. + switch (cmd) {
  2201. + default:
  2202. +#ifdef HAVE_SYSLOG_R
  2203. + syslog_r(LOG_ERR, &sd,
  2204. + "cryptodev_ctrl: unknown command %d", cmd);
  2205. +#else
  2206. + syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
  2207. +#endif
  2208. + break;
  2209. + }
  2210. + return (1);
  2211. }
  2212. -void ENGINE_load_cryptodev(void)
  2213. +void
  2214. +ENGINE_load_cryptodev(void)
  2215. {
  2216. - ENGINE *engine = ENGINE_new();
  2217. - int fd;
  2218. + ENGINE *engine = ENGINE_new();
  2219. + int fd;
  2220. - if (engine == NULL)
  2221. - return;
  2222. - if ((fd = get_dev_crypto()) < 0) {
  2223. - ENGINE_free(engine);
  2224. - return;
  2225. - }
  2226. + if (engine == NULL)
  2227. + return;
  2228. + if ((fd = get_dev_crypto()) < 0) {
  2229. + ENGINE_free(engine);
  2230. + return;
  2231. + }
  2232. - /*
  2233. - * find out what asymmetric crypto algorithms we support
  2234. - */
  2235. - if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
  2236. - put_dev_crypto(fd);
  2237. - ENGINE_free(engine);
  2238. - return;
  2239. - }
  2240. - put_dev_crypto(fd);
  2241. + /*
  2242. + * find out what asymmetric crypto algorithms we support
  2243. + */
  2244. + if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
  2245. + put_dev_crypto(fd);
  2246. + ENGINE_free(engine);
  2247. + return;
  2248. + }
  2249. + put_dev_crypto(fd);
  2250. - if (!ENGINE_set_id(engine, "cryptodev") ||
  2251. - !ENGINE_set_name(engine, "BSD cryptodev engine") ||
  2252. - !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
  2253. - !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
  2254. - !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
  2255. - !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
  2256. - ENGINE_free(engine);
  2257. - return;
  2258. - }
  2259. + if (!ENGINE_set_id(engine, "cryptodev") ||
  2260. + !ENGINE_set_name(engine, "cryptodev engine") ||
  2261. + !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
  2262. + !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
  2263. + !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
  2264. + !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
  2265. + ENGINE_free(engine);
  2266. + return;
  2267. + }
  2268. - if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
  2269. - const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
  2270. + if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
  2271. + const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
  2272. - cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
  2273. - cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
  2274. - cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
  2275. - cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
  2276. - cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
  2277. - cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
  2278. - if (cryptodev_asymfeat & CRF_MOD_EXP) {
  2279. - cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
  2280. - if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
  2281. - cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_mod_exp;
  2282. - else
  2283. - cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_nocrt_mod_exp;
  2284. - }
  2285. - }
  2286. + cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
  2287. + cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
  2288. + cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
  2289. + cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
  2290. + cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
  2291. + cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
  2292. + if (cryptodev_asymfeat & CRF_MOD_EXP) {
  2293. + cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
  2294. + if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
  2295. + cryptodev_rsa.rsa_mod_exp =
  2296. + cryptodev_rsa_mod_exp;
  2297. + else
  2298. + cryptodev_rsa.rsa_mod_exp =
  2299. + cryptodev_rsa_nocrt_mod_exp;
  2300. + }
  2301. + }
  2302. - if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
  2303. - const DSA_METHOD *meth = DSA_OpenSSL();
  2304. + if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
  2305. + const DSA_METHOD *meth = DSA_OpenSSL();
  2306. - memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
  2307. - if (cryptodev_asymfeat & CRF_DSA_SIGN)
  2308. - cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
  2309. - if (cryptodev_asymfeat & CRF_MOD_EXP) {
  2310. - cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
  2311. - cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
  2312. - }
  2313. - if (cryptodev_asymfeat & CRF_DSA_VERIFY)
  2314. - cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
  2315. - }
  2316. + memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
  2317. + if (cryptodev_asymfeat & CRF_DSA_SIGN)
  2318. + cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
  2319. + if (cryptodev_asymfeat & CRF_MOD_EXP) {
  2320. + cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
  2321. + cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
  2322. + }
  2323. + if (cryptodev_asymfeat & CRF_DSA_VERIFY)
  2324. + cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
  2325. + }
  2326. - if (ENGINE_set_DH(engine, &cryptodev_dh)) {
  2327. - const DH_METHOD *dh_meth = DH_OpenSSL();
  2328. + if (ENGINE_set_DH(engine, &cryptodev_dh)){
  2329. + const DH_METHOD *dh_meth = DH_OpenSSL();
  2330. - cryptodev_dh.generate_key = dh_meth->generate_key;
  2331. - cryptodev_dh.compute_key = dh_meth->compute_key;
  2332. - cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
  2333. - if (cryptodev_asymfeat & CRF_MOD_EXP) {
  2334. - cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
  2335. - if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
  2336. - cryptodev_dh.compute_key = cryptodev_dh_compute_key;
  2337. - }
  2338. - }
  2339. + cryptodev_dh.generate_key = dh_meth->generate_key;
  2340. + cryptodev_dh.compute_key = dh_meth->compute_key;
  2341. + cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
  2342. + if (cryptodev_asymfeat & CRF_MOD_EXP) {
  2343. + cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
  2344. + if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
  2345. + cryptodev_dh.compute_key =
  2346. + cryptodev_dh_compute_key;
  2347. + }
  2348. + }
  2349. - ENGINE_add(engine);
  2350. - ENGINE_free(engine);
  2351. - ERR_clear_error();
  2352. + ENGINE_add(engine);
  2353. + ENGINE_free(engine);
  2354. + ERR_clear_error();
  2355. }
  2356. -#endif /* HAVE_CRYPTODEV */
  2357. +#endif /* HAVE_CRYPTODEV */