e_afalg.c 25 KB

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