2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* We need to use some deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 /* Required for vmsplice */
21 #include <openssl/engine.h>
22 #include <openssl/async.h>
23 #include <openssl/err.h>
24 #include "internal/nelem.h"
26 #include <sys/socket.h>
27 #include <linux/version.h>
31 #if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2) || \
34 # warning "AFALG ENGINE requires Kernel Headers >= 4.1.0"
35 # warning "Skipping Compilation of AFALG engine"
37 void engine_load_afalg_int(void);
38 void engine_load_afalg_int(void)
43 # include <linux/if_alg.h>
45 # include <sys/utsname.h>
47 # include <linux/aio_abi.h>
48 # include <sys/syscall.h>
52 # include "e_afalg_err.c"
59 # ifndef SPLICE_F_GIFT
60 # define SPLICE_F_GIFT (0x08)
64 # define ALG_AES_IV_LEN 16
65 # define ALG_IV_LEN(len) (sizeof(struct af_alg_iv) + (len))
66 # define ALG_OP_TYPE unsigned int
67 # define ALG_OP_LEN (sizeof(ALG_OP_TYPE))
69 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
70 void engine_load_afalg_int(void);
73 /* Local Linkage Functions */
74 static int afalg_init_aio(afalg_aio
*aio
);
75 static int afalg_fin_cipher_aio(afalg_aio
*ptr
, int sfd
,
76 unsigned char *buf
, size_t len
);
77 static int afalg_create_sk(afalg_ctx
*actx
, const char *ciphertype
,
78 const char *ciphername
);
79 static int afalg_destroy(ENGINE
*e
);
80 static int afalg_init(ENGINE
*e
);
81 static int afalg_finish(ENGINE
*e
);
82 static const EVP_CIPHER
*afalg_aes_cbc(int nid
);
83 static cbc_handles
*get_cipher_handle(int nid
);
84 static int afalg_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
85 const int **nids
, int nid
);
86 static int afalg_cipher_init(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
87 const unsigned char *iv
, int enc
);
88 static int afalg_do_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
89 const unsigned char *in
, size_t inl
);
90 static int afalg_cipher_cleanup(EVP_CIPHER_CTX
*ctx
);
91 static int afalg_chk_platform(void);
93 /* Engine Id and Name */
94 static const char *engine_afalg_id
= "afalg";
95 static const char *engine_afalg_name
= "AFALG engine support";
97 static int afalg_cipher_nids
[] = {
103 static cbc_handles cbc_handle
[] = {{AES_KEY_SIZE_128
, NULL
},
104 {AES_KEY_SIZE_192
, NULL
},
105 {AES_KEY_SIZE_256
, NULL
}};
107 static ossl_inline
int io_setup(unsigned n
, aio_context_t
*ctx
)
109 return syscall(__NR_io_setup
, n
, ctx
);
112 static ossl_inline
int eventfd(int n
)
114 return syscall(__NR_eventfd2
, n
, 0);
117 static ossl_inline
int io_destroy(aio_context_t ctx
)
119 return syscall(__NR_io_destroy
, ctx
);
122 static ossl_inline
int io_read(aio_context_t ctx
, long n
, struct iocb
**iocb
)
124 return syscall(__NR_io_submit
, ctx
, n
, iocb
);
127 /* A version of 'struct timespec' with 32-bit time_t and nanoseconds. */
130 __kernel_long_t tv_sec
;
131 __kernel_long_t tv_nsec
;
134 static ossl_inline
int io_getevents(aio_context_t ctx
, long min
, long max
,
135 struct io_event
*events
,
136 struct timespec
*timeout
)
138 #if defined(__NR_io_pgetevents_time64)
139 /* Check if we are a 32-bit architecture with a 64-bit time_t */
140 if (sizeof(*timeout
) != sizeof(struct __timespec32
)) {
141 int ret
= syscall(__NR_io_pgetevents_time64
, ctx
, min
, max
, events
,
143 if (ret
== 0 || errno
!= ENOSYS
)
148 #if defined(__NR_io_getevents)
149 if (sizeof(*timeout
) == sizeof(struct __timespec32
))
151 * time_t matches our architecture length, we can just use
154 return syscall(__NR_io_getevents
, ctx
, min
, max
, events
, timeout
);
157 * We don't have __NR_io_pgetevents_time64, but we are using a
158 * 64-bit time_t on a 32-bit architecture. If we can fit the
159 * timeout value in a 32-bit time_t, then let's do that
160 * and then use the __NR_io_getevents syscall.
162 if (timeout
&& timeout
->tv_sec
== (long)timeout
->tv_sec
) {
163 struct __timespec32 ts32
;
165 ts32
.tv_sec
= (__kernel_long_t
) timeout
->tv_sec
;
166 ts32
.tv_nsec
= (__kernel_long_t
) timeout
->tv_nsec
;
168 return syscall(__NR_io_getevents
, ctx
, min
, max
, events
, ts32
);
170 return syscall(__NR_io_getevents
, ctx
, min
, max
, events
, NULL
);
179 static void afalg_waitfd_cleanup(ASYNC_WAIT_CTX
*ctx
, const void *key
,
180 OSSL_ASYNC_FD waitfd
, void *custom
)
185 static int afalg_setup_async_event_notification(afalg_aio
*aio
)
188 ASYNC_WAIT_CTX
*waitctx
;
192 if ((job
= ASYNC_get_current_job()) != NULL
) {
194 waitctx
= ASYNC_get_wait_ctx(job
);
195 if (waitctx
== NULL
) {
196 ALG_WARN("%s(%d): ASYNC_get_wait_ctx error", __FILE__
, __LINE__
);
199 /* Get waitfd from ASYNC_WAIT_CTX if it is already set */
200 ret
= ASYNC_WAIT_CTX_get_fd(waitctx
, engine_afalg_id
,
204 * waitfd is not set in ASYNC_WAIT_CTX, create a new one
205 * and set it. efd will be signaled when AIO operation completes
207 aio
->efd
= eventfd(0);
208 if (aio
->efd
== -1) {
209 ALG_PERR("%s(%d): Failed to get eventfd : ", __FILE__
,
211 AFALGerr(AFALG_F_AFALG_SETUP_ASYNC_EVENT_NOTIFICATION
,
212 AFALG_R_EVENTFD_FAILED
);
215 ret
= ASYNC_WAIT_CTX_set_wait_fd(waitctx
, engine_afalg_id
,
217 afalg_waitfd_cleanup
);
219 ALG_WARN("%s(%d): Failed to set wait fd", __FILE__
, __LINE__
);
223 /* make fd non-blocking in async mode */
224 if (fcntl(aio
->efd
, F_SETFL
, O_NONBLOCK
) != 0) {
225 ALG_WARN("%s(%d): Failed to set event fd as NONBLOCKING",
229 aio
->mode
= MODE_ASYNC
;
232 aio
->efd
= eventfd(0);
233 if (aio
->efd
== -1) {
234 ALG_PERR("%s(%d): Failed to get eventfd : ", __FILE__
, __LINE__
);
235 AFALGerr(AFALG_F_AFALG_SETUP_ASYNC_EVENT_NOTIFICATION
,
236 AFALG_R_EVENTFD_FAILED
);
239 aio
->mode
= MODE_SYNC
;
244 static int afalg_init_aio(afalg_aio
*aio
)
248 /* Initialise for AIO */
250 r
= io_setup(MAX_INFLIGHTS
, &aio
->aio_ctx
);
252 ALG_PERR("%s(%d): io_setup error : ", __FILE__
, __LINE__
);
253 AFALGerr(AFALG_F_AFALG_INIT_AIO
, AFALG_R_IO_SETUP_FAILED
);
257 memset(aio
->cbt
, 0, sizeof(aio
->cbt
));
259 aio
->mode
= MODE_UNINIT
;
264 static int afalg_fin_cipher_aio(afalg_aio
*aio
, int sfd
, unsigned char *buf
,
269 unsigned int done
= 0;
271 struct timespec timeout
;
272 struct io_event events
[MAX_INFLIGHTS
];
278 /* if efd has not been initialised yet do it here */
279 if (aio
->mode
== MODE_UNINIT
) {
280 r
= afalg_setup_async_event_notification(aio
);
285 cb
= &(aio
->cbt
[0 % MAX_INFLIGHTS
]);
286 memset(cb
, '\0', sizeof(*cb
));
287 cb
->aio_fildes
= sfd
;
288 cb
->aio_lio_opcode
= IOCB_CMD_PREAD
;
290 * The pointer has to be converted to unsigned value first to avoid
291 * sign extension on cast to 64 bit value in 32-bit builds
293 cb
->aio_buf
= (size_t)buf
;
296 cb
->aio_nbytes
= len
;
297 cb
->aio_flags
= IOCB_FLAG_RESFD
;
298 cb
->aio_resfd
= aio
->efd
;
301 * Perform AIO read on AFALG socket, this in turn performs an async
302 * crypto operation in kernel space
304 r
= io_read(aio
->aio_ctx
, 1, &cb
);
306 ALG_PWARN("%s(%d): io_read failed : ", __FILE__
, __LINE__
);
311 /* While AIO read is being performed pause job */
314 /* Check for completion of AIO read */
315 r
= read(aio
->efd
, &eval
, sizeof(eval
));
317 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)
319 ALG_PERR("%s(%d): read failed for event fd : ", __FILE__
, __LINE__
);
321 } else if (r
== 0 || eval
<= 0) {
322 ALG_WARN("%s(%d): eventfd read %d bytes, eval = %lu\n", __FILE__
,
327 #ifdef OSSL_SANITIZE_MEMORY
329 * In a memory sanitiser build, the changes to memory made by the
330 * system call aren't reliably detected. By initialising the
331 * memory here, the sanitiser is told that they are okay.
333 memset(events
, 0, sizeof(events
));
336 /* Get results of AIO read */
337 r
= io_getevents(aio
->aio_ctx
, 1, MAX_INFLIGHTS
,
341 * events.res indicates the actual status of the operation.
342 * Handle the error condition first.
344 if (events
[0].res
< 0) {
346 * Underlying operation cannot be completed at the time
347 * of previous submission. Resubmit for the operation.
349 if (events
[0].res
== -EBUSY
&& retry
++ < 3) {
350 r
= io_read(aio
->aio_ctx
, 1, &cb
);
352 ALG_PERR("%s(%d): retry %d for io_read failed : ",
353 __FILE__
, __LINE__
, retry
);
359 * Retries exceed for -EBUSY or unrecoverable error
360 * condition for this instance of operation.
363 ("%s(%d): Crypto Operation failed with code %lld\n",
364 __FILE__
, __LINE__
, events
[0].res
);
368 /* Operation successful. */
371 ALG_PERR("%s(%d): io_getevents failed : ", __FILE__
, __LINE__
);
374 ALG_WARN("%s(%d): io_geteventd read 0 bytes\n", __FILE__
,
383 static ossl_inline
void afalg_set_op_sk(struct cmsghdr
*cmsg
,
384 const ALG_OP_TYPE op
)
386 cmsg
->cmsg_level
= SOL_ALG
;
387 cmsg
->cmsg_type
= ALG_SET_OP
;
388 cmsg
->cmsg_len
= CMSG_LEN(ALG_OP_LEN
);
389 memcpy(CMSG_DATA(cmsg
), &op
, ALG_OP_LEN
);
392 static void afalg_set_iv_sk(struct cmsghdr
*cmsg
, const unsigned char *iv
,
393 const unsigned int len
)
395 struct af_alg_iv
*aiv
;
397 cmsg
->cmsg_level
= SOL_ALG
;
398 cmsg
->cmsg_type
= ALG_SET_IV
;
399 cmsg
->cmsg_len
= CMSG_LEN(ALG_IV_LEN(len
));
400 aiv
= (struct af_alg_iv
*)CMSG_DATA(cmsg
);
402 memcpy(aiv
->iv
, iv
, len
);
405 static ossl_inline
int afalg_set_key(afalg_ctx
*actx
, const unsigned char *key
,
409 ret
= setsockopt(actx
->bfd
, SOL_ALG
, ALG_SET_KEY
, key
, klen
);
411 ALG_PERR("%s(%d): Failed to set socket option : ", __FILE__
, __LINE__
);
412 AFALGerr(AFALG_F_AFALG_SET_KEY
, AFALG_R_SOCKET_SET_KEY_FAILED
);
418 static int afalg_create_sk(afalg_ctx
*actx
, const char *ciphertype
,
419 const char *ciphername
)
421 struct sockaddr_alg sa
;
424 actx
->bfd
= actx
->sfd
= -1;
426 memset(&sa
, 0, sizeof(sa
));
427 sa
.salg_family
= AF_ALG
;
428 OPENSSL_strlcpy((char *) sa
.salg_type
, ciphertype
, sizeof(sa
.salg_type
));
429 OPENSSL_strlcpy((char *) sa
.salg_name
, ciphername
, sizeof(sa
.salg_name
));
431 actx
->bfd
= socket(AF_ALG
, SOCK_SEQPACKET
, 0);
432 if (actx
->bfd
== -1) {
433 ALG_PERR("%s(%d): Failed to open socket : ", __FILE__
, __LINE__
);
434 AFALGerr(AFALG_F_AFALG_CREATE_SK
, AFALG_R_SOCKET_CREATE_FAILED
);
438 r
= bind(actx
->bfd
, (struct sockaddr
*)&sa
, sizeof(sa
));
440 ALG_PERR("%s(%d): Failed to bind socket : ", __FILE__
, __LINE__
);
441 AFALGerr(AFALG_F_AFALG_CREATE_SK
, AFALG_R_SOCKET_BIND_FAILED
);
445 actx
->sfd
= accept(actx
->bfd
, NULL
, 0);
447 ALG_PERR("%s(%d): Socket Accept Failed : ", __FILE__
, __LINE__
);
448 AFALGerr(AFALG_F_AFALG_CREATE_SK
, AFALG_R_SOCKET_ACCEPT_FAILED
);
459 actx
->bfd
= actx
->sfd
= -1;
463 static int afalg_start_cipher_sk(afalg_ctx
*actx
, const unsigned char *in
,
464 size_t inl
, const unsigned char *iv
,
468 struct cmsghdr
*cmsg
;
471 # ifdef ALG_ZERO_COPY
474 char cbuf
[CMSG_SPACE(ALG_IV_LEN(ALG_AES_IV_LEN
)) + CMSG_SPACE(ALG_OP_LEN
)];
476 memset(&msg
, 0, sizeof(msg
));
477 memset(cbuf
, 0, sizeof(cbuf
));
478 msg
.msg_control
= cbuf
;
479 msg
.msg_controllen
= sizeof(cbuf
);
482 * cipher direction (i.e. encrypt or decrypt) and iv are sent to the
483 * kernel as part of sendmsg()'s ancillary data
485 cmsg
= CMSG_FIRSTHDR(&msg
);
486 afalg_set_op_sk(cmsg
, enc
);
487 cmsg
= CMSG_NXTHDR(&msg
, cmsg
);
488 afalg_set_iv_sk(cmsg
, iv
, ALG_AES_IV_LEN
);
490 /* iov that describes input data */
491 iov
.iov_base
= (unsigned char *)in
;
494 msg
.msg_flags
= MSG_MORE
;
496 # ifdef ALG_ZERO_COPY
499 * Works best when buffer is 4k aligned
500 * OPENS: out of place processing (i.e. out != in)
503 /* Input data is not sent as part of call to sendmsg() */
507 /* Sendmsg() sends iv and cipher direction to the kernel */
508 sbytes
= sendmsg(actx
->sfd
, &msg
, 0);
510 ALG_PERR("%s(%d): sendmsg failed for zero copy cipher operation : ",
516 * vmsplice and splice are used to pin the user space input buffer for
517 * kernel space processing avoiding copies from user to kernel space
519 ret
= vmsplice(actx
->zc_pipe
[1], &iov
, 1, SPLICE_F_GIFT
);
521 ALG_PERR("%s(%d): vmsplice failed : ", __FILE__
, __LINE__
);
525 ret
= splice(actx
->zc_pipe
[0], NULL
, actx
->sfd
, NULL
, inl
, 0);
527 ALG_PERR("%s(%d): splice failed : ", __FILE__
, __LINE__
);
534 /* Sendmsg() sends iv, cipher direction and input data to the kernel */
535 sbytes
= sendmsg(actx
->sfd
, &msg
, 0);
537 ALG_PERR("%s(%d): sendmsg failed for cipher operation : ", __FILE__
,
542 if (sbytes
!= (ssize_t
) inl
) {
543 ALG_WARN("Cipher operation send bytes %zd != inlen %zd\n", sbytes
,
552 static int afalg_cipher_init(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
553 const unsigned char *iv
, int enc
)
558 const char *ciphername
;
560 if (ctx
== NULL
|| key
== NULL
) {
561 ALG_WARN("%s(%d): Null Parameter\n", __FILE__
, __LINE__
);
565 if (EVP_CIPHER_CTX_get0_cipher(ctx
) == NULL
) {
566 ALG_WARN("%s(%d): Cipher object NULL\n", __FILE__
, __LINE__
);
570 actx
= EVP_CIPHER_CTX_get_cipher_data(ctx
);
572 ALG_WARN("%s(%d): Cipher data NULL\n", __FILE__
, __LINE__
);
576 ciphertype
= EVP_CIPHER_CTX_get_nid(ctx
);
577 switch (ciphertype
) {
578 case NID_aes_128_cbc
:
579 case NID_aes_192_cbc
:
580 case NID_aes_256_cbc
:
581 ciphername
= "cbc(aes)";
584 ALG_WARN("%s(%d): Unsupported Cipher type %d\n", __FILE__
, __LINE__
,
589 if (ALG_AES_IV_LEN
!= EVP_CIPHER_CTX_get_iv_length(ctx
)) {
590 ALG_WARN("%s(%d): Unsupported IV length :%d\n", __FILE__
, __LINE__
,
591 EVP_CIPHER_CTX_get_iv_length(ctx
));
595 /* Setup AFALG socket for crypto processing */
596 ret
= afalg_create_sk(actx
, "skcipher", ciphername
);
600 if ((len
= EVP_CIPHER_CTX_get_key_length(ctx
)) <= 0)
602 ret
= afalg_set_key(actx
, key
, len
);
606 /* Setup AIO ctx to allow async AFALG crypto processing */
607 if (afalg_init_aio(&actx
->aio
) == 0)
610 # ifdef ALG_ZERO_COPY
614 actx
->init_done
= MAGIC_INIT_NUM
;
624 static int afalg_do_cipher(EVP_CIPHER_CTX
*ctx
, unsigned char *out
,
625 const unsigned char *in
, size_t inl
)
629 char nxtiv
[ALG_AES_IV_LEN
] = { 0 };
631 if (ctx
== NULL
|| out
== NULL
|| in
== NULL
) {
632 ALG_WARN("NULL parameter passed to function %s(%d)\n", __FILE__
,
637 actx
= (afalg_ctx
*) EVP_CIPHER_CTX_get_cipher_data(ctx
);
638 if (actx
== NULL
|| actx
->init_done
!= MAGIC_INIT_NUM
) {
639 ALG_WARN("%s afalg ctx passed\n",
640 ctx
== NULL
? "NULL" : "Uninitialised");
645 * set iv now for decrypt operation as the input buffer can be
646 * overwritten for inplace operation where in = out.
648 if (EVP_CIPHER_CTX_is_encrypting(ctx
) == 0) {
649 memcpy(nxtiv
, in
+ (inl
- ALG_AES_IV_LEN
), ALG_AES_IV_LEN
);
652 /* Send input data to kernel space */
653 ret
= afalg_start_cipher_sk(actx
, (unsigned char *)in
, inl
,
654 EVP_CIPHER_CTX_iv(ctx
),
655 EVP_CIPHER_CTX_is_encrypting(ctx
));
660 /* Perform async crypto operation in kernel space */
661 ret
= afalg_fin_cipher_aio(&actx
->aio
, actx
->sfd
, out
, inl
);
665 if (EVP_CIPHER_CTX_is_encrypting(ctx
)) {
666 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx
), out
+ (inl
- ALG_AES_IV_LEN
),
669 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx
), nxtiv
, ALG_AES_IV_LEN
);
675 static int afalg_cipher_cleanup(EVP_CIPHER_CTX
*ctx
)
680 ALG_WARN("NULL parameter passed to function %s(%d)\n", __FILE__
,
685 actx
= (afalg_ctx
*) EVP_CIPHER_CTX_get_cipher_data(ctx
);
686 if (actx
== NULL
|| actx
->init_done
!= MAGIC_INIT_NUM
)
691 # ifdef ALG_ZERO_COPY
692 close(actx
->zc_pipe
[0]);
693 close(actx
->zc_pipe
[1]);
695 /* close efd in sync mode, async mode is closed in afalg_waitfd_cleanup() */
696 if (actx
->aio
.mode
== MODE_SYNC
)
697 close(actx
->aio
.efd
);
698 io_destroy(actx
->aio
.aio_ctx
);
703 static cbc_handles
*get_cipher_handle(int nid
)
706 case NID_aes_128_cbc
:
707 return &cbc_handle
[AES_CBC_128
];
708 case NID_aes_192_cbc
:
709 return &cbc_handle
[AES_CBC_192
];
710 case NID_aes_256_cbc
:
711 return &cbc_handle
[AES_CBC_256
];
717 static const EVP_CIPHER
*afalg_aes_cbc(int nid
)
719 cbc_handles
*cipher_handle
= get_cipher_handle(nid
);
721 if (cipher_handle
== NULL
)
723 if (cipher_handle
->_hidden
== NULL
724 && ((cipher_handle
->_hidden
=
725 EVP_CIPHER_meth_new(nid
,
727 cipher_handle
->key_size
)) == NULL
728 || !EVP_CIPHER_meth_set_iv_length(cipher_handle
->_hidden
,
730 || !EVP_CIPHER_meth_set_flags(cipher_handle
->_hidden
,
732 EVP_CIPH_FLAG_DEFAULT_ASN1
)
733 || !EVP_CIPHER_meth_set_init(cipher_handle
->_hidden
,
735 || !EVP_CIPHER_meth_set_do_cipher(cipher_handle
->_hidden
,
737 || !EVP_CIPHER_meth_set_cleanup(cipher_handle
->_hidden
,
738 afalg_cipher_cleanup
)
739 || !EVP_CIPHER_meth_set_impl_ctx_size(cipher_handle
->_hidden
,
740 sizeof(afalg_ctx
)))) {
741 EVP_CIPHER_meth_free(cipher_handle
->_hidden
);
742 cipher_handle
->_hidden
= NULL
;
744 return cipher_handle
->_hidden
;
747 static int afalg_ciphers(ENGINE
*e
, const EVP_CIPHER
**cipher
,
748 const int **nids
, int nid
)
752 if (cipher
== NULL
) {
753 *nids
= afalg_cipher_nids
;
754 return (sizeof(afalg_cipher_nids
) / sizeof(afalg_cipher_nids
[0]));
758 case NID_aes_128_cbc
:
759 case NID_aes_192_cbc
:
760 case NID_aes_256_cbc
:
761 *cipher
= afalg_aes_cbc(nid
);
770 static int bind_afalg(ENGINE
*e
)
772 /* Ensure the afalg error handling is set up */
774 ERR_load_AFALG_strings();
776 if (!ENGINE_set_id(e
, engine_afalg_id
)
777 || !ENGINE_set_name(e
, engine_afalg_name
)
778 || !ENGINE_set_destroy_function(e
, afalg_destroy
)
779 || !ENGINE_set_init_function(e
, afalg_init
)
780 || !ENGINE_set_finish_function(e
, afalg_finish
)) {
781 AFALGerr(AFALG_F_BIND_AFALG
, AFALG_R_INIT_FAILED
);
786 * Create _hidden_aes_xxx_cbc by calling afalg_aes_xxx_cbc
787 * now, as bind_aflag can only be called by one thread at a
790 for (i
= 0; i
< OSSL_NELEM(afalg_cipher_nids
); i
++) {
791 if (afalg_aes_cbc(afalg_cipher_nids
[i
]) == NULL
) {
792 AFALGerr(AFALG_F_BIND_AFALG
, AFALG_R_INIT_FAILED
);
797 if (!ENGINE_set_ciphers(e
, afalg_ciphers
)) {
798 AFALGerr(AFALG_F_BIND_AFALG
, AFALG_R_INIT_FAILED
);
805 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
806 static int bind_helper(ENGINE
*e
, const char *id
)
808 if (id
&& (strcmp(id
, engine_afalg_id
) != 0))
811 if (!afalg_chk_platform())
819 IMPLEMENT_DYNAMIC_CHECK_FN()
820 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper
)
823 static int afalg_chk_platform(void)
827 int kver
[3] = { -1, -1, -1 };
834 AFALGerr(AFALG_F_AFALG_CHK_PLATFORM
,
835 AFALG_R_FAILED_TO_GET_PLATFORM_INFO
);
839 str
= strtok(ut
.release
, ".");
840 for (i
= 0; i
< 3 && str
!= NULL
; i
++) {
842 str
= strtok(NULL
, ".");
845 if (KERNEL_VERSION(kver
[0], kver
[1], kver
[2])
846 < KERNEL_VERSION(K_MAJ
, K_MIN1
, K_MIN2
)) {
847 ALG_ERR("ASYNC AFALG not supported this kernel(%d.%d.%d)\n",
848 kver
[0], kver
[1], kver
[2]);
849 ALG_ERR("ASYNC AFALG requires kernel version %d.%d.%d or later\n",
850 K_MAJ
, K_MIN1
, K_MIN2
);
851 AFALGerr(AFALG_F_AFALG_CHK_PLATFORM
,
852 AFALG_R_KERNEL_DOES_NOT_SUPPORT_ASYNC_AFALG
);
856 /* Test if we can actually create an AF_ALG socket */
857 sock
= socket(AF_ALG
, SOCK_SEQPACKET
, 0);
859 AFALGerr(AFALG_F_AFALG_CHK_PLATFORM
, AFALG_R_SOCKET_CREATE_FAILED
);
867 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
868 static ENGINE
*engine_afalg(void)
870 ENGINE
*ret
= ENGINE_new();
873 if (!bind_afalg(ret
)) {
880 void engine_load_afalg_int(void)
884 if (!afalg_chk_platform())
887 toadd
= engine_afalg();
893 * If the "add" worked, it gets a structural reference. So either way, we
894 * release our just-created reference.
898 * If the "add" didn't work, it was probably a conflict because it was
899 * already added (eg. someone calling ENGINE_load_blah then calling
900 * ENGINE_load_builtin_engines() perhaps).
906 static int afalg_init(ENGINE
*e
)
911 static int afalg_finish(ENGINE
*e
)
916 static int free_cbc(void)
918 short unsigned int i
;
919 for (i
= 0; i
< OSSL_NELEM(afalg_cipher_nids
); i
++) {
920 EVP_CIPHER_meth_free(cbc_handle
[i
]._hidden
);
921 cbc_handle
[i
]._hidden
= NULL
;
926 static int afalg_destroy(ENGINE
*e
)
928 ERR_unload_AFALG_strings();
933 #endif /* KERNEL VERSION */