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