2
0

e_afalg.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Required for vmsplice */
  10. #ifndef _GNU_SOURCE
  11. # define _GNU_SOURCE
  12. #endif
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <openssl/engine.h>
  17. #include <openssl/async.h>
  18. #include <openssl/err.h>
  19. #include "internal/nelem.h"
  20. #include <sys/socket.h>
  21. #include <linux/version.h>
  22. #define K_MAJ 4
  23. #define K_MIN1 1
  24. #define K_MIN2 0
  25. #if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2) || \
  26. !defined(AF_ALG)
  27. # ifndef PEDANTIC
  28. # warning "AFALG ENGINE requires Kernel Headers >= 4.1.0"
  29. # warning "Skipping Compilation of AFALG engine"
  30. # endif
  31. void engine_load_afalg_int(void);
  32. void engine_load_afalg_int(void)
  33. {
  34. }
  35. #else
  36. # include <linux/if_alg.h>
  37. # include <fcntl.h>
  38. # include <sys/utsname.h>
  39. # include <linux/aio_abi.h>
  40. # include <sys/syscall.h>
  41. # include <errno.h>
  42. # include "e_afalg.h"
  43. # include "e_afalg_err.c"
  44. # ifndef SOL_ALG
  45. # define SOL_ALG 279
  46. # endif
  47. # ifdef ALG_ZERO_COPY
  48. # ifndef SPLICE_F_GIFT
  49. # define SPLICE_F_GIFT (0x08)
  50. # endif
  51. # endif
  52. # define ALG_AES_IV_LEN 16
  53. # define ALG_IV_LEN(len) (sizeof(struct af_alg_iv) + (len))
  54. # define ALG_OP_TYPE unsigned int
  55. # define ALG_OP_LEN (sizeof(ALG_OP_TYPE))
  56. #define ALG_MAX_SALG_NAME 64
  57. #define ALG_MAX_SALG_TYPE 14
  58. # ifdef OPENSSL_NO_DYNAMIC_ENGINE
  59. void engine_load_afalg_int(void);
  60. # endif
  61. /* Local Linkage Functions */
  62. static int afalg_init_aio(afalg_aio *aio);
  63. static int afalg_fin_cipher_aio(afalg_aio *ptr, int sfd,
  64. unsigned char *buf, size_t len);
  65. static int afalg_create_sk(afalg_ctx *actx, const char *ciphertype,
  66. const char *ciphername);
  67. static int afalg_destroy(ENGINE *e);
  68. static int afalg_init(ENGINE *e);
  69. static int afalg_finish(ENGINE *e);
  70. static const EVP_CIPHER *afalg_aes_cbc(int nid);
  71. static cbc_handles *get_cipher_handle(int nid);
  72. static int afalg_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  73. const int **nids, int nid);
  74. static int afalg_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  75. const unsigned char *iv, int enc);
  76. static int afalg_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  77. const unsigned char *in, size_t inl);
  78. static int afalg_cipher_cleanup(EVP_CIPHER_CTX *ctx);
  79. static int afalg_chk_platform(void);
  80. /* Engine Id and Name */
  81. static const char *engine_afalg_id = "afalg";
  82. static const char *engine_afalg_name = "AFALG engine support";
  83. static int afalg_cipher_nids[] = {
  84. NID_aes_128_cbc,
  85. NID_aes_192_cbc,
  86. NID_aes_256_cbc,
  87. };
  88. static cbc_handles cbc_handle[] = {{AES_KEY_SIZE_128, NULL},
  89. {AES_KEY_SIZE_192, NULL},
  90. {AES_KEY_SIZE_256, NULL}};
  91. static ossl_inline int io_setup(unsigned n, aio_context_t *ctx)
  92. {
  93. return syscall(__NR_io_setup, n, ctx);
  94. }
  95. static ossl_inline int eventfd(int n)
  96. {
  97. return syscall(__NR_eventfd2, n, 0);
  98. }
  99. static ossl_inline int io_destroy(aio_context_t ctx)
  100. {
  101. return syscall(__NR_io_destroy, ctx);
  102. }
  103. static ossl_inline int io_read(aio_context_t ctx, long n, struct iocb **iocb)
  104. {
  105. return syscall(__NR_io_submit, ctx, n, iocb);
  106. }
  107. static ossl_inline int io_getevents(aio_context_t ctx, long min, long max,
  108. struct io_event *events,
  109. struct timespec *timeout)
  110. {
  111. return syscall(__NR_io_getevents, ctx, min, max, events, timeout);
  112. }
  113. static void afalg_waitfd_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
  114. OSSL_ASYNC_FD waitfd, void *custom)
  115. {
  116. close(waitfd);
  117. }
  118. static int afalg_setup_async_event_notification(afalg_aio *aio)
  119. {
  120. ASYNC_JOB *job;
  121. ASYNC_WAIT_CTX *waitctx;
  122. void *custom = NULL;
  123. int ret;
  124. if ((job = ASYNC_get_current_job()) != NULL) {
  125. /* Async mode */
  126. waitctx = ASYNC_get_wait_ctx(job);
  127. if (waitctx == NULL) {
  128. ALG_WARN("%s(%d): ASYNC_get_wait_ctx error", __FILE__, __LINE__);
  129. return 0;
  130. }
  131. /* Get waitfd from ASYNC_WAIT_CTX if it is already set */
  132. ret = ASYNC_WAIT_CTX_get_fd(waitctx, engine_afalg_id,
  133. &aio->efd, &custom);
  134. if (ret == 0) {
  135. /*
  136. * waitfd is not set in ASYNC_WAIT_CTX, create a new one
  137. * and set it. efd will be signaled when AIO operation completes
  138. */
  139. aio->efd = eventfd(0);
  140. if (aio->efd == -1) {
  141. ALG_PERR("%s(%d): Failed to get eventfd : ", __FILE__,
  142. __LINE__);
  143. AFALGerr(AFALG_F_AFALG_SETUP_ASYNC_EVENT_NOTIFICATION,
  144. AFALG_R_EVENTFD_FAILED);
  145. return 0;
  146. }
  147. ret = ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_afalg_id,
  148. aio->efd, custom,
  149. afalg_waitfd_cleanup);
  150. if (ret == 0) {
  151. ALG_WARN("%s(%d): Failed to set wait fd", __FILE__, __LINE__);
  152. close(aio->efd);
  153. return 0;
  154. }
  155. /* make fd non-blocking in async mode */
  156. if (fcntl(aio->efd, F_SETFL, O_NONBLOCK) != 0) {
  157. ALG_WARN("%s(%d): Failed to set event fd as NONBLOCKING",
  158. __FILE__, __LINE__);
  159. }
  160. }
  161. aio->mode = MODE_ASYNC;
  162. } else {
  163. /* Sync mode */
  164. aio->efd = eventfd(0);
  165. if (aio->efd == -1) {
  166. ALG_PERR("%s(%d): Failed to get eventfd : ", __FILE__, __LINE__);
  167. AFALGerr(AFALG_F_AFALG_SETUP_ASYNC_EVENT_NOTIFICATION,
  168. AFALG_R_EVENTFD_FAILED);
  169. return 0;
  170. }
  171. aio->mode = MODE_SYNC;
  172. }
  173. return 1;
  174. }
  175. static int afalg_init_aio(afalg_aio *aio)
  176. {
  177. int r = -1;
  178. /* Initialise for AIO */
  179. aio->aio_ctx = 0;
  180. r = io_setup(MAX_INFLIGHTS, &aio->aio_ctx);
  181. if (r < 0) {
  182. ALG_PERR("%s(%d): io_setup error : ", __FILE__, __LINE__);
  183. AFALGerr(AFALG_F_AFALG_INIT_AIO, AFALG_R_IO_SETUP_FAILED);
  184. return 0;
  185. }
  186. memset(aio->cbt, 0, sizeof(aio->cbt));
  187. aio->efd = -1;
  188. aio->mode = MODE_UNINIT;
  189. return 1;
  190. }
  191. static int afalg_fin_cipher_aio(afalg_aio *aio, int sfd, unsigned char *buf,
  192. size_t len)
  193. {
  194. int r;
  195. int retry = 0;
  196. unsigned int done = 0;
  197. struct iocb *cb;
  198. struct timespec timeout;
  199. struct io_event events[MAX_INFLIGHTS];
  200. u_int64_t eval = 0;
  201. timeout.tv_sec = 0;
  202. timeout.tv_nsec = 0;
  203. /* if efd has not been initialised yet do it here */
  204. if (aio->mode == MODE_UNINIT) {
  205. r = afalg_setup_async_event_notification(aio);
  206. if (r == 0)
  207. return 0;
  208. }
  209. cb = &(aio->cbt[0 % MAX_INFLIGHTS]);
  210. memset(cb, '\0', sizeof(*cb));
  211. cb->aio_fildes = sfd;
  212. cb->aio_lio_opcode = IOCB_CMD_PREAD;
  213. /*
  214. * The pointer has to be converted to unsigned value first to avoid
  215. * sign extension on cast to 64 bit value in 32-bit builds
  216. */
  217. cb->aio_buf = (size_t)buf;
  218. cb->aio_offset = 0;
  219. cb->aio_data = 0;
  220. cb->aio_nbytes = len;
  221. cb->aio_flags = IOCB_FLAG_RESFD;
  222. cb->aio_resfd = aio->efd;
  223. /*
  224. * Perform AIO read on AFALG socket, this in turn performs an async
  225. * crypto operation in kernel space
  226. */
  227. r = io_read(aio->aio_ctx, 1, &cb);
  228. if (r < 0) {
  229. ALG_PWARN("%s(%d): io_read failed : ", __FILE__, __LINE__);
  230. return 0;
  231. }
  232. do {
  233. /* While AIO read is being performed pause job */
  234. ASYNC_pause_job();
  235. /* Check for completion of AIO read */
  236. r = read(aio->efd, &eval, sizeof(eval));
  237. if (r < 0) {
  238. if (errno == EAGAIN || errno == EWOULDBLOCK)
  239. continue;
  240. ALG_PERR("%s(%d): read failed for event fd : ", __FILE__, __LINE__);
  241. return 0;
  242. } else if (r == 0 || eval <= 0) {
  243. ALG_WARN("%s(%d): eventfd read %d bytes, eval = %lu\n", __FILE__,
  244. __LINE__, r, eval);
  245. }
  246. if (eval > 0) {
  247. /* Get results of AIO read */
  248. r = io_getevents(aio->aio_ctx, 1, MAX_INFLIGHTS,
  249. events, &timeout);
  250. if (r > 0) {
  251. /*
  252. * events.res indicates the actual status of the operation.
  253. * Handle the error condition first.
  254. */
  255. if (events[0].res < 0) {
  256. /*
  257. * Underlying operation cannot be completed at the time
  258. * of previous submission. Resubmit for the operation.
  259. */
  260. if (events[0].res == -EBUSY && retry++ < 3) {
  261. r = io_read(aio->aio_ctx, 1, &cb);
  262. if (r < 0) {
  263. ALG_PERR("%s(%d): retry %d for io_read failed : ",
  264. __FILE__, __LINE__, retry);
  265. return 0;
  266. }
  267. continue;
  268. } else {
  269. /*
  270. * Retries exceed for -EBUSY or unrecoverable error
  271. * condition for this instance of operation.
  272. */
  273. ALG_WARN
  274. ("%s(%d): Crypto Operation failed with code %lld\n",
  275. __FILE__, __LINE__, events[0].res);
  276. return 0;
  277. }
  278. }
  279. /* Operation successful. */
  280. done = 1;
  281. } else if (r < 0) {
  282. ALG_PERR("%s(%d): io_getevents failed : ", __FILE__, __LINE__);
  283. return 0;
  284. } else {
  285. ALG_WARN("%s(%d): io_geteventd read 0 bytes\n", __FILE__,
  286. __LINE__);
  287. }
  288. }
  289. } while (!done);
  290. return 1;
  291. }
  292. static ossl_inline void afalg_set_op_sk(struct cmsghdr *cmsg,
  293. const ALG_OP_TYPE op)
  294. {
  295. cmsg->cmsg_level = SOL_ALG;
  296. cmsg->cmsg_type = ALG_SET_OP;
  297. cmsg->cmsg_len = CMSG_LEN(ALG_OP_LEN);
  298. memcpy(CMSG_DATA(cmsg), &op, ALG_OP_LEN);
  299. }
  300. static void afalg_set_iv_sk(struct cmsghdr *cmsg, const unsigned char *iv,
  301. const unsigned int len)
  302. {
  303. struct af_alg_iv *aiv;
  304. cmsg->cmsg_level = SOL_ALG;
  305. cmsg->cmsg_type = ALG_SET_IV;
  306. cmsg->cmsg_len = CMSG_LEN(ALG_IV_LEN(len));
  307. aiv = (struct af_alg_iv *)CMSG_DATA(cmsg);
  308. aiv->ivlen = len;
  309. memcpy(aiv->iv, iv, len);
  310. }
  311. static ossl_inline int afalg_set_key(afalg_ctx *actx, const unsigned char *key,
  312. const int klen)
  313. {
  314. int ret;
  315. ret = setsockopt(actx->bfd, SOL_ALG, ALG_SET_KEY, key, klen);
  316. if (ret < 0) {
  317. ALG_PERR("%s(%d): Failed to set socket option : ", __FILE__, __LINE__);
  318. AFALGerr(AFALG_F_AFALG_SET_KEY, AFALG_R_SOCKET_SET_KEY_FAILED);
  319. return 0;
  320. }
  321. return 1;
  322. }
  323. static int afalg_create_sk(afalg_ctx *actx, const char *ciphertype,
  324. const char *ciphername)
  325. {
  326. struct sockaddr_alg sa;
  327. int r = -1;
  328. actx->bfd = actx->sfd = -1;
  329. memset(&sa, 0, sizeof(sa));
  330. sa.salg_family = AF_ALG;
  331. strncpy((char *) sa.salg_type, ciphertype, ALG_MAX_SALG_TYPE);
  332. sa.salg_type[ALG_MAX_SALG_TYPE-1] = '\0';
  333. strncpy((char *) sa.salg_name, ciphername, ALG_MAX_SALG_NAME);
  334. sa.salg_name[ALG_MAX_SALG_NAME-1] = '\0';
  335. actx->bfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
  336. if (actx->bfd == -1) {
  337. ALG_PERR("%s(%d): Failed to open socket : ", __FILE__, __LINE__);
  338. AFALGerr(AFALG_F_AFALG_CREATE_SK, AFALG_R_SOCKET_CREATE_FAILED);
  339. goto err;
  340. }
  341. r = bind(actx->bfd, (struct sockaddr *)&sa, sizeof(sa));
  342. if (r < 0) {
  343. ALG_PERR("%s(%d): Failed to bind socket : ", __FILE__, __LINE__);
  344. AFALGerr(AFALG_F_AFALG_CREATE_SK, AFALG_R_SOCKET_BIND_FAILED);
  345. goto err;
  346. }
  347. actx->sfd = accept(actx->bfd, NULL, 0);
  348. if (actx->sfd < 0) {
  349. ALG_PERR("%s(%d): Socket Accept Failed : ", __FILE__, __LINE__);
  350. AFALGerr(AFALG_F_AFALG_CREATE_SK, AFALG_R_SOCKET_ACCEPT_FAILED);
  351. goto err;
  352. }
  353. return 1;
  354. err:
  355. if (actx->bfd >= 0)
  356. close(actx->bfd);
  357. if (actx->sfd >= 0)
  358. close(actx->sfd);
  359. actx->bfd = actx->sfd = -1;
  360. return 0;
  361. }
  362. static int afalg_start_cipher_sk(afalg_ctx *actx, const unsigned char *in,
  363. size_t inl, const unsigned char *iv,
  364. unsigned int enc)
  365. {
  366. struct msghdr msg;
  367. struct cmsghdr *cmsg;
  368. struct iovec iov;
  369. ssize_t sbytes;
  370. # ifdef ALG_ZERO_COPY
  371. int ret;
  372. # endif
  373. char cbuf[CMSG_SPACE(ALG_IV_LEN(ALG_AES_IV_LEN)) + CMSG_SPACE(ALG_OP_LEN)];
  374. memset(&msg, 0, sizeof(msg));
  375. memset(cbuf, 0, sizeof(cbuf));
  376. msg.msg_control = cbuf;
  377. msg.msg_controllen = sizeof(cbuf);
  378. /*
  379. * cipher direction (i.e. encrypt or decrypt) and iv are sent to the
  380. * kernel as part of sendmsg()'s ancillary data
  381. */
  382. cmsg = CMSG_FIRSTHDR(&msg);
  383. afalg_set_op_sk(cmsg, enc);
  384. cmsg = CMSG_NXTHDR(&msg, cmsg);
  385. afalg_set_iv_sk(cmsg, iv, ALG_AES_IV_LEN);
  386. /* iov that describes input data */
  387. iov.iov_base = (unsigned char *)in;
  388. iov.iov_len = inl;
  389. msg.msg_flags = MSG_MORE;
  390. # ifdef ALG_ZERO_COPY
  391. /*
  392. * ZERO_COPY mode
  393. * Works best when buffer is 4k aligned
  394. * OPENS: out of place processing (i.e. out != in)
  395. */
  396. /* Input data is not sent as part of call to sendmsg() */
  397. msg.msg_iovlen = 0;
  398. msg.msg_iov = NULL;
  399. /* Sendmsg() sends iv and cipher direction to the kernel */
  400. sbytes = sendmsg(actx->sfd, &msg, 0);
  401. if (sbytes < 0) {
  402. ALG_PERR("%s(%d): sendmsg failed for zero copy cipher operation : ",
  403. __FILE__, __LINE__);
  404. return 0;
  405. }
  406. /*
  407. * vmsplice and splice are used to pin the user space input buffer for
  408. * kernel space processing avoiding copys from user to kernel space
  409. */
  410. ret = vmsplice(actx->zc_pipe[1], &iov, 1, SPLICE_F_GIFT);
  411. if (ret < 0) {
  412. ALG_PERR("%s(%d): vmsplice failed : ", __FILE__, __LINE__);
  413. return 0;
  414. }
  415. ret = splice(actx->zc_pipe[0], NULL, actx->sfd, NULL, inl, 0);
  416. if (ret < 0) {
  417. ALG_PERR("%s(%d): splice failed : ", __FILE__, __LINE__);
  418. return 0;
  419. }
  420. # else
  421. msg.msg_iovlen = 1;
  422. msg.msg_iov = &iov;
  423. /* Sendmsg() sends iv, cipher direction and input data to the kernel */
  424. sbytes = sendmsg(actx->sfd, &msg, 0);
  425. if (sbytes < 0) {
  426. ALG_PERR("%s(%d): sendmsg failed for cipher operation : ", __FILE__,
  427. __LINE__);
  428. return 0;
  429. }
  430. if (sbytes != (ssize_t) inl) {
  431. ALG_WARN("Cipher operation send bytes %zd != inlen %zd\n", sbytes,
  432. inl);
  433. return 0;
  434. }
  435. # endif
  436. return 1;
  437. }
  438. static int afalg_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  439. const unsigned char *iv, int enc)
  440. {
  441. int ciphertype;
  442. int ret;
  443. afalg_ctx *actx;
  444. char ciphername[ALG_MAX_SALG_NAME];
  445. if (ctx == NULL || key == NULL) {
  446. ALG_WARN("%s(%d): Null Parameter\n", __FILE__, __LINE__);
  447. return 0;
  448. }
  449. if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
  450. ALG_WARN("%s(%d): Cipher object NULL\n", __FILE__, __LINE__);
  451. return 0;
  452. }
  453. actx = EVP_CIPHER_CTX_get_cipher_data(ctx);
  454. if (actx == NULL) {
  455. ALG_WARN("%s(%d): Cipher data NULL\n", __FILE__, __LINE__);
  456. return 0;
  457. }
  458. ciphertype = EVP_CIPHER_CTX_nid(ctx);
  459. switch (ciphertype) {
  460. case NID_aes_128_cbc:
  461. case NID_aes_192_cbc:
  462. case NID_aes_256_cbc:
  463. strncpy(ciphername, "cbc(aes)", ALG_MAX_SALG_NAME);
  464. break;
  465. default:
  466. ALG_WARN("%s(%d): Unsupported Cipher type %d\n", __FILE__, __LINE__,
  467. ciphertype);
  468. return 0;
  469. }
  470. ciphername[ALG_MAX_SALG_NAME-1]='\0';
  471. if (ALG_AES_IV_LEN != EVP_CIPHER_CTX_iv_length(ctx)) {
  472. ALG_WARN("%s(%d): Unsupported IV length :%d\n", __FILE__, __LINE__,
  473. EVP_CIPHER_CTX_iv_length(ctx));
  474. return 0;
  475. }
  476. /* Setup AFALG socket for crypto processing */
  477. ret = afalg_create_sk(actx, "skcipher", ciphername);
  478. if (ret < 1)
  479. return 0;
  480. ret = afalg_set_key(actx, key, EVP_CIPHER_CTX_key_length(ctx));
  481. if (ret < 1)
  482. goto err;
  483. /* Setup AIO ctx to allow async AFALG crypto processing */
  484. if (afalg_init_aio(&actx->aio) == 0)
  485. goto err;
  486. # ifdef ALG_ZERO_COPY
  487. pipe(actx->zc_pipe);
  488. # endif
  489. actx->init_done = MAGIC_INIT_NUM;
  490. return 1;
  491. err:
  492. close(actx->sfd);
  493. close(actx->bfd);
  494. return 0;
  495. }
  496. static int afalg_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  497. const unsigned char *in, size_t inl)
  498. {
  499. afalg_ctx *actx;
  500. int ret;
  501. char nxtiv[ALG_AES_IV_LEN] = { 0 };
  502. if (ctx == NULL || out == NULL || in == NULL) {
  503. ALG_WARN("NULL parameter passed to function %s(%d)\n", __FILE__,
  504. __LINE__);
  505. return 0;
  506. }
  507. actx = (afalg_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
  508. if (actx == NULL || actx->init_done != MAGIC_INIT_NUM) {
  509. ALG_WARN("%s afalg ctx passed\n",
  510. ctx == NULL ? "NULL" : "Uninitialised");
  511. return 0;
  512. }
  513. /*
  514. * set iv now for decrypt operation as the input buffer can be
  515. * overwritten for inplace operation where in = out.
  516. */
  517. if (EVP_CIPHER_CTX_encrypting(ctx) == 0) {
  518. memcpy(nxtiv, in + (inl - ALG_AES_IV_LEN), ALG_AES_IV_LEN);
  519. }
  520. /* Send input data to kernel space */
  521. ret = afalg_start_cipher_sk(actx, (unsigned char *)in, inl,
  522. EVP_CIPHER_CTX_iv(ctx),
  523. EVP_CIPHER_CTX_encrypting(ctx));
  524. if (ret < 1) {
  525. return 0;
  526. }
  527. /* Perform async crypto operation in kernel space */
  528. ret = afalg_fin_cipher_aio(&actx->aio, actx->sfd, out, inl);
  529. if (ret < 1)
  530. return 0;
  531. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  532. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), out + (inl - ALG_AES_IV_LEN),
  533. ALG_AES_IV_LEN);
  534. } else {
  535. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), nxtiv, ALG_AES_IV_LEN);
  536. }
  537. return 1;
  538. }
  539. static int afalg_cipher_cleanup(EVP_CIPHER_CTX *ctx)
  540. {
  541. afalg_ctx *actx;
  542. if (ctx == NULL) {
  543. ALG_WARN("NULL parameter passed to function %s(%d)\n", __FILE__,
  544. __LINE__);
  545. return 0;
  546. }
  547. actx = (afalg_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
  548. if (actx == NULL || actx->init_done != MAGIC_INIT_NUM) {
  549. ALG_WARN("%s afalg ctx passed\n",
  550. ctx == NULL ? "NULL" : "Uninitialised");
  551. return 0;
  552. }
  553. close(actx->sfd);
  554. close(actx->bfd);
  555. # ifdef ALG_ZERO_COPY
  556. close(actx->zc_pipe[0]);
  557. close(actx->zc_pipe[1]);
  558. # endif
  559. /* close efd in sync mode, async mode is closed in afalg_waitfd_cleanup() */
  560. if (actx->aio.mode == MODE_SYNC)
  561. close(actx->aio.efd);
  562. io_destroy(actx->aio.aio_ctx);
  563. return 1;
  564. }
  565. static cbc_handles *get_cipher_handle(int nid)
  566. {
  567. switch (nid) {
  568. case NID_aes_128_cbc:
  569. return &cbc_handle[AES_CBC_128];
  570. case NID_aes_192_cbc:
  571. return &cbc_handle[AES_CBC_192];
  572. case NID_aes_256_cbc:
  573. return &cbc_handle[AES_CBC_256];
  574. default:
  575. return NULL;
  576. }
  577. }
  578. static const EVP_CIPHER *afalg_aes_cbc(int nid)
  579. {
  580. cbc_handles *cipher_handle = get_cipher_handle(nid);
  581. if (cipher_handle->_hidden == NULL
  582. && ((cipher_handle->_hidden =
  583. EVP_CIPHER_meth_new(nid,
  584. AES_BLOCK_SIZE,
  585. cipher_handle->key_size)) == NULL
  586. || !EVP_CIPHER_meth_set_iv_length(cipher_handle->_hidden,
  587. AES_IV_LEN)
  588. || !EVP_CIPHER_meth_set_flags(cipher_handle->_hidden,
  589. EVP_CIPH_CBC_MODE |
  590. EVP_CIPH_FLAG_DEFAULT_ASN1)
  591. || !EVP_CIPHER_meth_set_init(cipher_handle->_hidden,
  592. afalg_cipher_init)
  593. || !EVP_CIPHER_meth_set_do_cipher(cipher_handle->_hidden,
  594. afalg_do_cipher)
  595. || !EVP_CIPHER_meth_set_cleanup(cipher_handle->_hidden,
  596. afalg_cipher_cleanup)
  597. || !EVP_CIPHER_meth_set_impl_ctx_size(cipher_handle->_hidden,
  598. sizeof(afalg_ctx)))) {
  599. EVP_CIPHER_meth_free(cipher_handle->_hidden);
  600. cipher_handle->_hidden= NULL;
  601. }
  602. return cipher_handle->_hidden;
  603. }
  604. static int afalg_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  605. const int **nids, int nid)
  606. {
  607. int r = 1;
  608. if (cipher == NULL) {
  609. *nids = afalg_cipher_nids;
  610. return (sizeof(afalg_cipher_nids) / sizeof(afalg_cipher_nids[0]));
  611. }
  612. switch (nid) {
  613. case NID_aes_128_cbc:
  614. case NID_aes_192_cbc:
  615. case NID_aes_256_cbc:
  616. *cipher = afalg_aes_cbc(nid);
  617. break;
  618. default:
  619. *cipher = NULL;
  620. r = 0;
  621. }
  622. return r;
  623. }
  624. static int bind_afalg(ENGINE *e)
  625. {
  626. /* Ensure the afalg error handling is set up */
  627. unsigned short i;
  628. ERR_load_AFALG_strings();
  629. if (!ENGINE_set_id(e, engine_afalg_id)
  630. || !ENGINE_set_name(e, engine_afalg_name)
  631. || !ENGINE_set_destroy_function(e, afalg_destroy)
  632. || !ENGINE_set_init_function(e, afalg_init)
  633. || !ENGINE_set_finish_function(e, afalg_finish)) {
  634. AFALGerr(AFALG_F_BIND_AFALG, AFALG_R_INIT_FAILED);
  635. return 0;
  636. }
  637. /*
  638. * Create _hidden_aes_xxx_cbc by calling afalg_aes_xxx_cbc
  639. * now, as bind_aflag can only be called by one thread at a
  640. * time.
  641. */
  642. for(i = 0; i < OSSL_NELEM(afalg_cipher_nids); i++) {
  643. if (afalg_aes_cbc(afalg_cipher_nids[i]) == NULL) {
  644. AFALGerr(AFALG_F_BIND_AFALG, AFALG_R_INIT_FAILED);
  645. return 0;
  646. }
  647. }
  648. if (!ENGINE_set_ciphers(e, afalg_ciphers)) {
  649. AFALGerr(AFALG_F_BIND_AFALG, AFALG_R_INIT_FAILED);
  650. return 0;
  651. }
  652. return 1;
  653. }
  654. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  655. static int bind_helper(ENGINE *e, const char *id)
  656. {
  657. if (id && (strcmp(id, engine_afalg_id) != 0))
  658. return 0;
  659. if (!afalg_chk_platform())
  660. return 0;
  661. if (!bind_afalg(e))
  662. return 0;
  663. return 1;
  664. }
  665. IMPLEMENT_DYNAMIC_CHECK_FN()
  666. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  667. # endif
  668. static int afalg_chk_platform(void)
  669. {
  670. int ret;
  671. int i;
  672. int kver[3] = { -1, -1, -1 };
  673. int sock;
  674. char *str;
  675. struct utsname ut;
  676. ret = uname(&ut);
  677. if (ret != 0) {
  678. AFALGerr(AFALG_F_AFALG_CHK_PLATFORM,
  679. AFALG_R_FAILED_TO_GET_PLATFORM_INFO);
  680. return 0;
  681. }
  682. str = strtok(ut.release, ".");
  683. for (i = 0; i < 3 && str != NULL; i++) {
  684. kver[i] = atoi(str);
  685. str = strtok(NULL, ".");
  686. }
  687. if (KERNEL_VERSION(kver[0], kver[1], kver[2])
  688. < KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2)) {
  689. ALG_ERR("ASYNC AFALG not supported this kernel(%d.%d.%d)\n",
  690. kver[0], kver[1], kver[2]);
  691. ALG_ERR("ASYNC AFALG requires kernel version %d.%d.%d or later\n",
  692. K_MAJ, K_MIN1, K_MIN2);
  693. AFALGerr(AFALG_F_AFALG_CHK_PLATFORM,
  694. AFALG_R_KERNEL_DOES_NOT_SUPPORT_ASYNC_AFALG);
  695. return 0;
  696. }
  697. /* Test if we can actually create an AF_ALG socket */
  698. sock = socket(AF_ALG, SOCK_SEQPACKET, 0);
  699. if (sock == -1) {
  700. AFALGerr(AFALG_F_AFALG_CHK_PLATFORM, AFALG_R_SOCKET_CREATE_FAILED);
  701. return 0;
  702. }
  703. close(sock);
  704. return 1;
  705. }
  706. # ifdef OPENSSL_NO_DYNAMIC_ENGINE
  707. static ENGINE *engine_afalg(void)
  708. {
  709. ENGINE *ret = ENGINE_new();
  710. if (ret == NULL)
  711. return NULL;
  712. if (!bind_afalg(ret)) {
  713. ENGINE_free(ret);
  714. return NULL;
  715. }
  716. return ret;
  717. }
  718. void engine_load_afalg_int(void)
  719. {
  720. ENGINE *toadd;
  721. if (!afalg_chk_platform())
  722. return;
  723. toadd = engine_afalg();
  724. if (toadd == NULL)
  725. return;
  726. ENGINE_add(toadd);
  727. ENGINE_free(toadd);
  728. ERR_clear_error();
  729. }
  730. # endif
  731. static int afalg_init(ENGINE *e)
  732. {
  733. return 1;
  734. }
  735. static int afalg_finish(ENGINE *e)
  736. {
  737. return 1;
  738. }
  739. static int free_cbc(void)
  740. {
  741. short unsigned int i;
  742. for(i = 0; i < OSSL_NELEM(afalg_cipher_nids); i++) {
  743. EVP_CIPHER_meth_free(cbc_handle[i]._hidden);
  744. cbc_handle[i]._hidden = NULL;
  745. }
  746. return 1;
  747. }
  748. static int afalg_destroy(ENGINE *e)
  749. {
  750. ERR_unload_AFALG_strings();
  751. free_cbc();
  752. return 1;
  753. }
  754. #endif /* KERNEL VERSION */