e_afalg.c 23 KB

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