e_afalg.c 24 KB

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