| 
									
										
										
										
											2016-05-18 02:20:24 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:52 +08:00
										 |  |  |  * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 21:07:27 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 02:20:24 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-22 08:38:10 +08:00
										 |  |  | /* We need to use some deprecated APIs */ | 
					
						
							| 
									
										
										
										
											2020-07-14 07:38:22 +08:00
										 |  |  | #define OPENSSL_SUPPRESS_DEPRECATED
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | /* Required for vmsplice */ | 
					
						
							| 
									
										
										
										
											2016-09-05 04:53:22 +08:00
										 |  |  | #ifndef _GNU_SOURCE
 | 
					
						
							|  |  |  | # define _GNU_SOURCE
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <openssl/engine.h>
 | 
					
						
							|  |  |  | #include <openssl/async.h>
 | 
					
						
							| 
									
										
										
										
											2016-03-18 22:07:31 +08:00
										 |  |  | #include <openssl/err.h>
 | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  | #include "internal/nelem.h"
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-05 04:53:22 +08:00
										 |  |  | #include <sys/socket.h>
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | #include <linux/version.h>
 | 
					
						
							|  |  |  | #define K_MAJ   4
 | 
					
						
							|  |  |  | #define K_MIN1  1
 | 
					
						
							|  |  |  | #define K_MIN2  0
 | 
					
						
							| 
									
										
										
										
											2017-10-30 18:38:09 +08:00
										 |  |  | #if LINUX_VERSION_CODE < KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2) || \
 | 
					
						
							| 
									
										
										
										
											2016-09-05 04:53:22 +08:00
										 |  |  |     !defined(AF_ALG) | 
					
						
							| 
									
										
										
										
											2016-12-16 16:24:00 +08:00
										 |  |  | # ifndef PEDANTIC
 | 
					
						
							|  |  |  | #  warning "AFALG ENGINE requires Kernel Headers >= 4.1.0"
 | 
					
						
							|  |  |  | #  warning "Skipping Compilation of AFALG engine"
 | 
					
						
							|  |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2016-11-24 06:55:13 +08:00
										 |  |  | void engine_load_afalg_int(void); | 
					
						
							| 
									
										
										
										
											2016-04-14 06:26:56 +08:00
										 |  |  | void engine_load_afalg_int(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # include <linux/if_alg.h>
 | 
					
						
							|  |  |  | # include <fcntl.h>
 | 
					
						
							|  |  |  | # include <sys/utsname.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # include <linux/aio_abi.h>
 | 
					
						
							|  |  |  | # include <sys/syscall.h>
 | 
					
						
							|  |  |  | # include <errno.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # include "e_afalg.h"
 | 
					
						
							| 
									
										
										
										
											2017-06-08 03:12:03 +08:00
										 |  |  | # include "e_afalg_err.c"
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # ifndef SOL_ALG
 | 
					
						
							|  |  |  | #  define SOL_ALG 279
 | 
					
						
							|  |  |  | # endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ifdef ALG_ZERO_COPY
 | 
					
						
							|  |  |  | #  ifndef SPLICE_F_GIFT
 | 
					
						
							|  |  |  | #   define SPLICE_F_GIFT    (0x08)
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							|  |  |  | # endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # define ALG_AES_IV_LEN 16
 | 
					
						
							|  |  |  | # define ALG_IV_LEN(len) (sizeof(struct af_alg_iv) + (len))
 | 
					
						
							|  |  |  | # define ALG_OP_TYPE     unsigned int
 | 
					
						
							|  |  |  | # define ALG_OP_LEN      (sizeof(ALG_OP_TYPE))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  | # ifdef OPENSSL_NO_DYNAMIC_ENGINE
 | 
					
						
							| 
									
										
										
										
											2016-04-12 19:20:16 +08:00
										 |  |  | void engine_load_afalg_int(void); | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  | # endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | /* Local Linkage Functions */ | 
					
						
							|  |  |  | static int afalg_init_aio(afalg_aio *aio); | 
					
						
							|  |  |  | static int afalg_fin_cipher_aio(afalg_aio *ptr, int sfd, | 
					
						
							|  |  |  |                                 unsigned char *buf, size_t len); | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  | static int afalg_create_sk(afalg_ctx *actx, const char *ciphertype, | 
					
						
							|  |  |  |                                 const char *ciphername); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | static int afalg_destroy(ENGINE *e); | 
					
						
							|  |  |  | static int afalg_init(ENGINE *e); | 
					
						
							|  |  |  | static int afalg_finish(ENGINE *e); | 
					
						
							| 
									
										
										
										
											2018-02-07 21:05:20 +08:00
										 |  |  | static const EVP_CIPHER *afalg_aes_cbc(int nid); | 
					
						
							| 
									
										
										
										
											2017-11-15 09:13:48 +08:00
										 |  |  | static cbc_handles *get_cipher_handle(int nid); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | static int afalg_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | 
					
						
							|  |  |  |                          const int **nids, int nid); | 
					
						
							|  |  |  | static int afalg_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 
					
						
							|  |  |  |                              const unsigned char *iv, int enc); | 
					
						
							|  |  |  | static int afalg_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 
					
						
							|  |  |  |                            const unsigned char *in, size_t inl); | 
					
						
							|  |  |  | static int afalg_cipher_cleanup(EVP_CIPHER_CTX *ctx); | 
					
						
							|  |  |  | static int afalg_chk_platform(void); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Engine Id and Name */ | 
					
						
							|  |  |  | static const char *engine_afalg_id = "afalg"; | 
					
						
							| 
									
										
										
										
											2016-05-12 02:14:54 +08:00
										 |  |  | static const char *engine_afalg_name = "AFALG engine support"; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 20:36:20 +08:00
										 |  |  | static int afalg_cipher_nids[] = { | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  |     NID_aes_128_cbc, | 
					
						
							|  |  |  |     NID_aes_192_cbc, | 
					
						
							|  |  |  |     NID_aes_256_cbc, | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  | static cbc_handles cbc_handle[] = {{AES_KEY_SIZE_128, NULL}, | 
					
						
							|  |  |  |                                     {AES_KEY_SIZE_192, NULL}, | 
					
						
							|  |  |  |                                     {AES_KEY_SIZE_256, NULL}}; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-09 03:10:50 +08:00
										 |  |  | static ossl_inline int io_setup(unsigned n, aio_context_t *ctx) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     return syscall(__NR_io_setup, n, ctx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-09 03:10:50 +08:00
										 |  |  | static ossl_inline int eventfd(int n) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-30 18:36:06 +08:00
										 |  |  |     return syscall(__NR_eventfd2, n, 0); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-09 03:10:50 +08:00
										 |  |  | static ossl_inline int io_destroy(aio_context_t ctx) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     return syscall(__NR_io_destroy, ctx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-09 03:10:50 +08:00
										 |  |  | static ossl_inline int io_read(aio_context_t ctx, long n, struct iocb **iocb) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     return syscall(__NR_io_submit, ctx, n, iocb); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-05 01:10:11 +08:00
										 |  |  | /* A version of 'struct timespec' with 32-bit time_t and nanoseconds.  */ | 
					
						
							|  |  |  | struct __timespec32 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   __kernel_long_t tv_sec; | 
					
						
							|  |  |  |   __kernel_long_t tv_nsec; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-09 03:10:50 +08:00
										 |  |  | static ossl_inline int io_getevents(aio_context_t ctx, long min, long max, | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |                                struct io_event *events, | 
					
						
							|  |  |  |                                struct timespec *timeout) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-05 01:10:11 +08:00
										 |  |  | #if defined(__NR_io_pgetevents_time64)
 | 
					
						
							|  |  |  |     /* Check if we are a 32-bit architecture with a 64-bit time_t */ | 
					
						
							|  |  |  |     if (sizeof(*timeout) != sizeof(struct __timespec32)) { | 
					
						
							|  |  |  |         int ret = syscall(__NR_io_pgetevents_time64, ctx, min, max, events, | 
					
						
							|  |  |  |                           timeout, NULL); | 
					
						
							|  |  |  |         if (ret == 0 || errno != ENOSYS) | 
					
						
							|  |  |  |             return ret; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-30 04:56:21 +08:00
										 |  |  | #if defined(__NR_io_getevents)
 | 
					
						
							| 
									
										
										
										
											2021-03-05 01:10:11 +08:00
										 |  |  |     if (sizeof(*timeout) == sizeof(struct __timespec32)) | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * time_t matches our architecture length, we can just use | 
					
						
							|  |  |  |          * __NR_io_getevents | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         return syscall(__NR_io_getevents, ctx, min, max, events, timeout); | 
					
						
							| 
									
										
										
										
											2019-08-30 04:56:21 +08:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2021-03-05 01:10:11 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * We don't have __NR_io_pgetevents_time64, but we are using a | 
					
						
							|  |  |  |          * 64-bit time_t on a 32-bit architecture. If we can fit the | 
					
						
							|  |  |  |          * timeout value in a 32-bit time_t, then let's do that | 
					
						
							|  |  |  |          * and then use the __NR_io_getevents syscall. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         if (timeout && timeout->tv_sec == (long)timeout->tv_sec) { | 
					
						
							|  |  |  |             struct __timespec32 ts32; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ts32.tv_sec = (__kernel_long_t) timeout->tv_sec; | 
					
						
							|  |  |  |             ts32.tv_nsec = (__kernel_long_t) timeout->tv_nsec; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-23 00:54:43 +08:00
										 |  |  |             return syscall(__NR_io_getevents, ctx, min, max, events, &ts32); | 
					
						
							| 
									
										
										
										
											2021-03-05 01:10:11 +08:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             return syscall(__NR_io_getevents, ctx, min, max, events, NULL); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-30 04:56:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-03-05 01:10:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     errno = ENOSYS; | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void afalg_waitfd_cleanup(ASYNC_WAIT_CTX *ctx, const void *key, | 
					
						
							|  |  |  |                                  OSSL_ASYNC_FD waitfd, void *custom) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     close(waitfd); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int afalg_setup_async_event_notification(afalg_aio *aio) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASYNC_JOB *job; | 
					
						
							|  |  |  |     ASYNC_WAIT_CTX *waitctx; | 
					
						
							|  |  |  |     void *custom = NULL; | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if ((job = ASYNC_get_current_job()) != NULL) { | 
					
						
							|  |  |  |         /* Async mode */ | 
					
						
							|  |  |  |         waitctx = ASYNC_get_wait_ctx(job); | 
					
						
							|  |  |  |         if (waitctx == NULL) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |             ALG_WARN("%s(%d): ASYNC_get_wait_ctx error", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-12 08:03:10 +08:00
										 |  |  |         /* Get waitfd from ASYNC_WAIT_CTX if it is already set */ | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         ret = ASYNC_WAIT_CTX_get_fd(waitctx, engine_afalg_id, | 
					
						
							|  |  |  |                                     &aio->efd, &custom); | 
					
						
							|  |  |  |         if (ret == 0) { | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |              * waitfd is not set in ASYNC_WAIT_CTX, create a new one | 
					
						
							|  |  |  |              * and set it. efd will be signaled when AIO operation completes | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  |             aio->efd = eventfd(0); | 
					
						
							|  |  |  |             if (aio->efd == -1) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |                 ALG_PERR("%s(%d): Failed to get eventfd : ", __FILE__, | 
					
						
							|  |  |  |                          __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |                 AFALGerr(AFALG_F_AFALG_SETUP_ASYNC_EVENT_NOTIFICATION, | 
					
						
							|  |  |  |                          AFALG_R_EVENTFD_FAILED); | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |             ret = ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_afalg_id, | 
					
						
							|  |  |  |                                              aio->efd, custom, | 
					
						
							|  |  |  |                                              afalg_waitfd_cleanup); | 
					
						
							|  |  |  |             if (ret == 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |                 ALG_WARN("%s(%d): Failed to set wait fd", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |                 close(aio->efd); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |                 return 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             /* make fd non-blocking in async mode */ | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |             if (fcntl(aio->efd, F_SETFL, O_NONBLOCK) != 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |                 ALG_WARN("%s(%d): Failed to set event fd as NONBLOCKING", | 
					
						
							|  |  |  |                          __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         aio->mode = MODE_ASYNC; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         /* Sync mode */ | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         aio->efd = eventfd(0); | 
					
						
							|  |  |  |         if (aio->efd == -1) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |             ALG_PERR("%s(%d): Failed to get eventfd : ", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |             AFALGerr(AFALG_F_AFALG_SETUP_ASYNC_EVENT_NOTIFICATION, | 
					
						
							|  |  |  |                      AFALG_R_EVENTFD_FAILED); | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         aio->mode = MODE_SYNC; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 21:05:20 +08:00
										 |  |  | static int afalg_init_aio(afalg_aio *aio) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int r = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Initialise for AIO */ | 
					
						
							|  |  |  |     aio->aio_ctx = 0; | 
					
						
							|  |  |  |     r = io_setup(MAX_INFLIGHTS, &aio->aio_ctx); | 
					
						
							|  |  |  |     if (r < 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_PERR("%s(%d): io_setup error : ", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         AFALGerr(AFALG_F_AFALG_INIT_AIO, AFALG_R_IO_SETUP_FAILED); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     memset(aio->cbt, 0, sizeof(aio->cbt)); | 
					
						
							|  |  |  |     aio->efd = -1; | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     aio->mode = MODE_UNINIT; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 21:05:20 +08:00
										 |  |  | static int afalg_fin_cipher_aio(afalg_aio *aio, int sfd, unsigned char *buf, | 
					
						
							|  |  |  |                                 size_t len) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int r; | 
					
						
							|  |  |  |     int retry = 0; | 
					
						
							|  |  |  |     unsigned int done = 0; | 
					
						
							|  |  |  |     struct iocb *cb; | 
					
						
							|  |  |  |     struct timespec timeout; | 
					
						
							|  |  |  |     struct io_event events[MAX_INFLIGHTS]; | 
					
						
							|  |  |  |     u_int64_t eval = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     timeout.tv_sec = 0; | 
					
						
							|  |  |  |     timeout.tv_nsec = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* if efd has not been initialised yet do it here */ | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     if (aio->mode == MODE_UNINIT) { | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         r = afalg_setup_async_event_notification(aio); | 
					
						
							|  |  |  |         if (r == 0) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cb = &(aio->cbt[0 % MAX_INFLIGHTS]); | 
					
						
							|  |  |  |     memset(cb, '\0', sizeof(*cb)); | 
					
						
							|  |  |  |     cb->aio_fildes = sfd; | 
					
						
							|  |  |  |     cb->aio_lio_opcode = IOCB_CMD_PREAD; | 
					
						
							| 
									
										
										
										
											2017-03-31 05:07:26 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * The pointer has to be converted to unsigned value first to avoid | 
					
						
							|  |  |  |      * sign extension on cast to 64 bit value in 32-bit builds | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     cb->aio_buf = (size_t)buf; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     cb->aio_offset = 0; | 
					
						
							|  |  |  |     cb->aio_data = 0; | 
					
						
							|  |  |  |     cb->aio_nbytes = len; | 
					
						
							|  |  |  |     cb->aio_flags = IOCB_FLAG_RESFD; | 
					
						
							|  |  |  |     cb->aio_resfd = aio->efd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * Perform AIO read on AFALG socket, this in turn performs an async | 
					
						
							|  |  |  |      * crypto operation in kernel space | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     r = io_read(aio->aio_ctx, 1, &cb); | 
					
						
							|  |  |  |     if (r < 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_PWARN("%s(%d): io_read failed : ", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |         /* While AIO read is being performed pause job */ | 
					
						
							|  |  |  |         ASYNC_pause_job(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* Check for completion of AIO read */ | 
					
						
							|  |  |  |         r = read(aio->efd, &eval, sizeof(eval)); | 
					
						
							|  |  |  |         if (r < 0) { | 
					
						
							|  |  |  |             if (errno == EAGAIN || errno == EWOULDBLOCK) | 
					
						
							|  |  |  |                 continue; | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |             ALG_PERR("%s(%d): read failed for event fd : ", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } else if (r == 0 || eval <= 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |             ALG_WARN("%s(%d): eventfd read %d bytes, eval = %lu\n", __FILE__, | 
					
						
							|  |  |  |                      __LINE__, r, eval); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (eval > 0) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-30 08:21:17 +08:00
										 |  |  | #ifdef OSSL_SANITIZE_MEMORY
 | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |              * In a memory sanitiser build, the changes to memory made by the | 
					
						
							|  |  |  |              * system call aren't reliably detected.  By initialising the | 
					
						
							|  |  |  |              * memory here, the sanitiser is told that they are okay. | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  |             memset(events, 0, sizeof(events)); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |             /* Get results of AIO read */ | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |             r = io_getevents(aio->aio_ctx, 1, MAX_INFLIGHTS, | 
					
						
							|  |  |  |                              events, &timeout); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |             if (r > 0) { | 
					
						
							|  |  |  |                 /*
 | 
					
						
							|  |  |  |                  * events.res indicates the actual status of the operation. | 
					
						
							|  |  |  |                  * Handle the error condition first. | 
					
						
							|  |  |  |                  */ | 
					
						
							|  |  |  |                 if (events[0].res < 0) { | 
					
						
							|  |  |  |                     /*
 | 
					
						
							|  |  |  |                      * Underlying operation cannot be completed at the time | 
					
						
							|  |  |  |                      * of previous submission. Resubmit for the operation. | 
					
						
							|  |  |  |                      */ | 
					
						
							|  |  |  |                     if (events[0].res == -EBUSY && retry++ < 3) { | 
					
						
							|  |  |  |                         r = io_read(aio->aio_ctx, 1, &cb); | 
					
						
							|  |  |  |                         if (r < 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |                             ALG_PERR("%s(%d): retry %d for io_read failed : ", | 
					
						
							|  |  |  |                                      __FILE__, __LINE__, retry); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |                             return 0; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         continue; | 
					
						
							|  |  |  |                     } else { | 
					
						
							| 
									
										
										
										
											2022-09-28 17:41:29 +08:00
										 |  |  |                         char strbuf[32]; | 
					
						
							|  |  |  |                         /*
 | 
					
						
							|  |  |  |                          * sometimes __s64 is defined as long long int | 
					
						
							|  |  |  |                          * but on some archs ( like mips64 or powerpc64 ) it's just long int | 
					
						
							|  |  |  |                          * | 
					
						
							|  |  |  |                          * to be able to use BIO_snprintf() with %lld without warnings | 
					
						
							|  |  |  |                          * copy events[0].res to an long long int variable | 
					
						
							|  |  |  |                          * | 
					
						
							|  |  |  |                          * because long long int should always be at least 64 bit this should work | 
					
						
							|  |  |  |                          */ | 
					
						
							|  |  |  |                         long long int op_ret = events[0].res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |                         /*
 | 
					
						
							|  |  |  |                          * Retries exceed for -EBUSY or unrecoverable error | 
					
						
							|  |  |  |                          * condition for this instance of operation. | 
					
						
							|  |  |  |                          */ | 
					
						
							|  |  |  |                         ALG_WARN | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |                             ("%s(%d): Crypto Operation failed with code %lld\n", | 
					
						
							|  |  |  |                              __FILE__, __LINE__, events[0].res); | 
					
						
							| 
									
										
										
										
											2022-09-28 17:41:29 +08:00
										 |  |  |                         BIO_snprintf(strbuf, sizeof(strbuf), "%lld", op_ret); | 
					
						
							|  |  |  |                         switch (events[0].res) { | 
					
						
							|  |  |  |                         case -ENOMEM: | 
					
						
							|  |  |  |                             AFALGerr(0, AFALG_R_KERNEL_OP_FAILED); | 
					
						
							|  |  |  |                             ERR_add_error_data(3, "-ENOMEM ( code ", strbuf, " )"); | 
					
						
							|  |  |  |                             break; | 
					
						
							|  |  |  |                         default: | 
					
						
							|  |  |  |                             AFALGerr(0, AFALG_R_KERNEL_OP_FAILED); | 
					
						
							|  |  |  |                             ERR_add_error_data(2, "code ", strbuf); | 
					
						
							|  |  |  |                             break; | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |                         return 0; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 /* Operation successful. */ | 
					
						
							|  |  |  |                 done = 1; | 
					
						
							|  |  |  |             } else if (r < 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |                 ALG_PERR("%s(%d): io_getevents failed : ", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |                 return 0; | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |                 ALG_WARN("%s(%d): io_geteventd read 0 bytes\n", __FILE__, | 
					
						
							|  |  |  |                          __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } while (!done); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-09 03:10:50 +08:00
										 |  |  | static ossl_inline void afalg_set_op_sk(struct cmsghdr *cmsg, | 
					
						
							| 
									
										
										
										
											2016-10-12 20:32:05 +08:00
										 |  |  |                                    const ALG_OP_TYPE op) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     cmsg->cmsg_level = SOL_ALG; | 
					
						
							|  |  |  |     cmsg->cmsg_type = ALG_SET_OP; | 
					
						
							|  |  |  |     cmsg->cmsg_len = CMSG_LEN(ALG_OP_LEN); | 
					
						
							| 
									
										
										
										
											2016-10-12 20:32:05 +08:00
										 |  |  |     memcpy(CMSG_DATA(cmsg), &op, ALG_OP_LEN); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void afalg_set_iv_sk(struct cmsghdr *cmsg, const unsigned char *iv, | 
					
						
							|  |  |  |                             const unsigned int len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct af_alg_iv *aiv; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cmsg->cmsg_level = SOL_ALG; | 
					
						
							|  |  |  |     cmsg->cmsg_type = ALG_SET_IV; | 
					
						
							|  |  |  |     cmsg->cmsg_len = CMSG_LEN(ALG_IV_LEN(len)); | 
					
						
							|  |  |  |     aiv = (struct af_alg_iv *)CMSG_DATA(cmsg); | 
					
						
							|  |  |  |     aiv->ivlen = len; | 
					
						
							|  |  |  |     memcpy(aiv->iv, iv, len); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-09 03:10:50 +08:00
										 |  |  | static ossl_inline int afalg_set_key(afalg_ctx *actx, const unsigned char *key, | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |                                 const int klen) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     ret = setsockopt(actx->bfd, SOL_ALG, ALG_SET_KEY, key, klen); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_PERR("%s(%d): Failed to set socket option : ", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         AFALGerr(AFALG_F_AFALG_SET_KEY, AFALG_R_SOCKET_SET_KEY_FAILED); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int afalg_create_sk(afalg_ctx *actx, const char *ciphertype, | 
					
						
							|  |  |  |                                 const char *ciphername) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct sockaddr_alg sa; | 
					
						
							| 
									
										
										
										
											2017-03-31 05:07:26 +08:00
										 |  |  |     int r = -1; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     actx->bfd = actx->sfd = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     memset(&sa, 0, sizeof(sa)); | 
					
						
							|  |  |  |     sa.salg_family = AF_ALG; | 
					
						
							| 
									
										
										
										
											2019-07-29 17:39:34 +08:00
										 |  |  |     OPENSSL_strlcpy((char *) sa.salg_type, ciphertype, sizeof(sa.salg_type)); | 
					
						
							|  |  |  |     OPENSSL_strlcpy((char *) sa.salg_name, ciphername, sizeof(sa.salg_name)); | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     actx->bfd = socket(AF_ALG, SOCK_SEQPACKET, 0); | 
					
						
							|  |  |  |     if (actx->bfd == -1) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_PERR("%s(%d): Failed to open socket : ", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         AFALGerr(AFALG_F_AFALG_CREATE_SK, AFALG_R_SOCKET_CREATE_FAILED); | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     r = bind(actx->bfd, (struct sockaddr *)&sa, sizeof(sa)); | 
					
						
							|  |  |  |     if (r < 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_PERR("%s(%d): Failed to bind socket : ", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         AFALGerr(AFALG_F_AFALG_CREATE_SK, AFALG_R_SOCKET_BIND_FAILED); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     actx->sfd = accept(actx->bfd, NULL, 0); | 
					
						
							|  |  |  |     if (actx->sfd < 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_PERR("%s(%d): Socket Accept Failed : ", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         AFALGerr(AFALG_F_AFALG_CREATE_SK, AFALG_R_SOCKET_ACCEPT_FAILED); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |  err: | 
					
						
							|  |  |  |     if (actx->bfd >= 0) | 
					
						
							|  |  |  |         close(actx->bfd); | 
					
						
							|  |  |  |     if (actx->sfd >= 0) | 
					
						
							|  |  |  |         close(actx->sfd); | 
					
						
							|  |  |  |     actx->bfd = actx->sfd = -1; | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int afalg_start_cipher_sk(afalg_ctx *actx, const unsigned char *in, | 
					
						
							|  |  |  |                                  size_t inl, const unsigned char *iv, | 
					
						
							|  |  |  |                                  unsigned int enc) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-29 16:17:38 +08:00
										 |  |  |     struct msghdr msg; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     struct cmsghdr *cmsg; | 
					
						
							|  |  |  |     struct iovec iov; | 
					
						
							|  |  |  |     ssize_t sbytes; | 
					
						
							|  |  |  | # ifdef ALG_ZERO_COPY
 | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2016-03-07 20:36:20 +08:00
										 |  |  |     char cbuf[CMSG_SPACE(ALG_IV_LEN(ALG_AES_IV_LEN)) + CMSG_SPACE(ALG_OP_LEN)]; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 16:17:38 +08:00
										 |  |  |     memset(&msg, 0, sizeof(msg)); | 
					
						
							| 
									
										
										
										
											2016-03-07 20:36:20 +08:00
										 |  |  |     memset(cbuf, 0, sizeof(cbuf)); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     msg.msg_control = cbuf; | 
					
						
							| 
									
										
										
										
											2016-03-07 20:36:20 +08:00
										 |  |  |     msg.msg_controllen = sizeof(cbuf); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * cipher direction (i.e. encrypt or decrypt) and iv are sent to the | 
					
						
							|  |  |  |      * kernel as part of sendmsg()'s ancillary data | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     cmsg = CMSG_FIRSTHDR(&msg); | 
					
						
							|  |  |  |     afalg_set_op_sk(cmsg, enc); | 
					
						
							|  |  |  |     cmsg = CMSG_NXTHDR(&msg, cmsg); | 
					
						
							|  |  |  |     afalg_set_iv_sk(cmsg, iv, ALG_AES_IV_LEN); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* iov that describes input data */ | 
					
						
							|  |  |  |     iov.iov_base = (unsigned char *)in; | 
					
						
							|  |  |  |     iov.iov_len = inl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     msg.msg_flags = MSG_MORE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ifdef ALG_ZERO_COPY
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * ZERO_COPY mode | 
					
						
							|  |  |  |      * Works best when buffer is 4k aligned | 
					
						
							|  |  |  |      * OPENS: out of place processing (i.e. out != in) | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Input data is not sent as part of call to sendmsg() */ | 
					
						
							|  |  |  |     msg.msg_iovlen = 0; | 
					
						
							|  |  |  |     msg.msg_iov = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Sendmsg() sends iv and cipher direction to the kernel */ | 
					
						
							|  |  |  |     sbytes = sendmsg(actx->sfd, &msg, 0); | 
					
						
							|  |  |  |     if (sbytes < 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_PERR("%s(%d): sendmsg failed for zero copy cipher operation : ", | 
					
						
							|  |  |  |                  __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * vmsplice and splice are used to pin the user space input buffer for | 
					
						
							| 
									
										
										
										
											2020-02-03 05:54:01 +08:00
										 |  |  |      * kernel space processing avoiding copies from user to kernel space | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     ret = vmsplice(actx->zc_pipe[1], &iov, 1, SPLICE_F_GIFT); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_PERR("%s(%d): vmsplice failed : ", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = splice(actx->zc_pipe[0], NULL, actx->sfd, NULL, inl, 0); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_PERR("%s(%d): splice failed : ", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | # else
 | 
					
						
							|  |  |  |     msg.msg_iovlen = 1; | 
					
						
							|  |  |  |     msg.msg_iov = &iov; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Sendmsg() sends iv, cipher direction and input data to the kernel */ | 
					
						
							|  |  |  |     sbytes = sendmsg(actx->sfd, &msg, 0); | 
					
						
							|  |  |  |     if (sbytes < 0) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_PERR("%s(%d): sendmsg failed for cipher operation : ", __FILE__, | 
					
						
							|  |  |  |                  __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (sbytes != (ssize_t) inl) { | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         ALG_WARN("Cipher operation send bytes %zd != inlen %zd\n", sbytes, | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |                 inl); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | # endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int afalg_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 
					
						
							|  |  |  |                              const unsigned char *iv, int enc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ciphertype; | 
					
						
							| 
									
										
										
										
											2021-06-07 07:26:42 +08:00
										 |  |  |     int ret, len; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     afalg_ctx *actx; | 
					
						
							| 
									
										
										
										
											2019-07-29 17:39:34 +08:00
										 |  |  |     const char *ciphername; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (ctx == NULL || key == NULL) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_WARN("%s(%d): Null Parameter\n", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Add "origin" field to EVP_CIPHER, EVP_MD
Add a "where did this EVP_{CIPHER,MD} come from" flag: global, via fetch,
or via EVP_{CIPHER,MD}_meth_new.  Update EVP_{CIPHER,MD}_free to handle all
three origins. The flag is deliberately right before some function pointers,
so that compile-time failures (int/pointer) will occur, as opposed to
taking a bit in the existing "flags" field.  The "global variable" flag
is non-zero, so the default case of using OPENSSL_zalloc (for provider
ciphers), will do the right thing. Ref-counting is a no-op for
Make up_ref no-op for global MD and CIPHER objects
Deprecate EVP_MD_CTX_md().  Added EVP_MD_CTX_get0_md() (same semantics as
the deprecated function) and EVP_MD_CTX_get1_md().  Likewise, deprecate
EVP_CIPHER_CTX_cipher() in favor of EVP_CIPHER_CTX_get0_cipher(), and add
EVP_CIPHER_CTX_get1_CIPHER().
Refactor EVP_MD_free() and EVP_MD_meth_free() to call new common
evp_md_free_int() function.
Refactor EVP_CIPHER_free() and EVP_CIPHER_meth_free() to call new common
evp_cipher_free_int() function.
Also change some flags tests to explicit test == or != zero. E.g.,
        if (flags & x) --> if ((flags & x) != 0)
        if (!(flags & x)) --> if ((flags & x) == 0)
Only done for those lines where "get0_cipher" calls were made.
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14193)
											
										 
											2021-02-17 06:51:56 +08:00
										 |  |  |     if (EVP_CIPHER_CTX_get0_cipher(ctx) == NULL) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_WARN("%s(%d): Cipher object NULL\n", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 18:17:27 +08:00
										 |  |  |     actx = EVP_CIPHER_CTX_get_cipher_data(ctx); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     if (actx == NULL) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_WARN("%s(%d): Cipher data NULL\n", __FILE__, __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |     ciphertype = EVP_CIPHER_CTX_get_nid(ctx); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     switch (ciphertype) { | 
					
						
							|  |  |  |     case NID_aes_128_cbc: | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  |     case NID_aes_192_cbc: | 
					
						
							|  |  |  |     case NID_aes_256_cbc: | 
					
						
							| 
									
										
										
										
											2019-07-29 17:39:34 +08:00
										 |  |  |         ciphername = "cbc(aes)"; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_WARN("%s(%d): Unsupported Cipher type %d\n", __FILE__, __LINE__, | 
					
						
							|  |  |  |                  ciphertype); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |     if (ALG_AES_IV_LEN != EVP_CIPHER_CTX_get_iv_length(ctx)) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_WARN("%s(%d): Unsupported IV length :%d\n", __FILE__, __LINE__, | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |                  EVP_CIPHER_CTX_get_iv_length(ctx)); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Setup AFALG socket for crypto processing */ | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     ret = afalg_create_sk(actx, "skcipher", ciphername); | 
					
						
							|  |  |  |     if (ret < 1) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 07:26:42 +08:00
										 |  |  |     if ((len = EVP_CIPHER_CTX_get_key_length(ctx)) <= 0) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     ret = afalg_set_key(actx, key, len); | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     if (ret < 1) | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Setup AIO ctx to allow async AFALG crypto processing */ | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     if (afalg_init_aio(&actx->aio) == 0) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | # ifdef ALG_ZERO_COPY
 | 
					
						
							|  |  |  |     pipe(actx->zc_pipe); | 
					
						
							|  |  |  | # endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     actx->init_done = MAGIC_INIT_NUM; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | err: | 
					
						
							|  |  |  |     close(actx->sfd); | 
					
						
							|  |  |  |     close(actx->bfd); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int afalg_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 
					
						
							|  |  |  |                            const unsigned char *in, size_t inl) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     afalg_ctx *actx; | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  |     char nxtiv[ALG_AES_IV_LEN] = { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ctx == NULL || out == NULL || in == NULL) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_WARN("NULL parameter passed to function %s(%d)\n", __FILE__, | 
					
						
							|  |  |  |                  __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 18:17:27 +08:00
										 |  |  |     actx = (afalg_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     if (actx == NULL || actx->init_done != MAGIC_INIT_NUM) { | 
					
						
							|  |  |  |         ALG_WARN("%s afalg ctx passed\n", | 
					
						
							|  |  |  |                  ctx == NULL ? "NULL" : "Uninitialised"); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * set iv now for decrypt operation as the input buffer can be | 
					
						
							|  |  |  |      * overwritten for inplace operation where in = out. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |     if (EVP_CIPHER_CTX_is_encrypting(ctx) == 0) { | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         memcpy(nxtiv, in + (inl - ALG_AES_IV_LEN), ALG_AES_IV_LEN); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Send input data to kernel space */ | 
					
						
							|  |  |  |     ret = afalg_start_cipher_sk(actx, (unsigned char *)in, inl, | 
					
						
							|  |  |  |                                 EVP_CIPHER_CTX_iv(ctx), | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |                                 EVP_CIPHER_CTX_is_encrypting(ctx)); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     if (ret < 1) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Perform async crypto operation in kernel space */ | 
					
						
							|  |  |  |     ret = afalg_fin_cipher_aio(&actx->aio, actx->sfd, out, inl); | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     if (ret < 1) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
											
										 
											2021-05-21 22:58:08 +08:00
										 |  |  |     if (EVP_CIPHER_CTX_is_encrypting(ctx)) { | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), out + (inl - ALG_AES_IV_LEN), | 
					
						
							|  |  |  |                ALG_AES_IV_LEN); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), nxtiv, ALG_AES_IV_LEN); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int afalg_cipher_cleanup(EVP_CIPHER_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     afalg_ctx *actx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ctx == NULL) { | 
					
						
							| 
									
										
										
										
											2018-06-22 15:41:29 +08:00
										 |  |  |         ALG_WARN("NULL parameter passed to function %s(%d)\n", __FILE__, | 
					
						
							|  |  |  |                  __LINE__); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 18:17:27 +08:00
										 |  |  |     actx = (afalg_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx); | 
					
						
							| 
									
										
										
										
											2021-10-05 01:45:19 +08:00
										 |  |  |     if (actx == NULL || actx->init_done != MAGIC_INIT_NUM) | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     close(actx->sfd); | 
					
						
							|  |  |  |     close(actx->bfd); | 
					
						
							|  |  |  | # ifdef ALG_ZERO_COPY
 | 
					
						
							|  |  |  |     close(actx->zc_pipe[0]); | 
					
						
							|  |  |  |     close(actx->zc_pipe[1]); | 
					
						
							|  |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     /* close efd in sync mode, async mode is closed in afalg_waitfd_cleanup() */ | 
					
						
							|  |  |  |     if (actx->aio.mode == MODE_SYNC) | 
					
						
							|  |  |  |         close(actx->aio.efd); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     io_destroy(actx->aio.aio_ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 18:44:36 +08:00
										 |  |  | static cbc_handles *get_cipher_handle(int nid) | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (nid) { | 
					
						
							|  |  |  |     case NID_aes_128_cbc: | 
					
						
							|  |  |  |         return &cbc_handle[AES_CBC_128]; | 
					
						
							|  |  |  |     case NID_aes_192_cbc: | 
					
						
							|  |  |  |         return &cbc_handle[AES_CBC_192]; | 
					
						
							|  |  |  |     case NID_aes_256_cbc: | 
					
						
							|  |  |  |         return &cbc_handle[AES_CBC_256]; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 21:05:20 +08:00
										 |  |  | static const EVP_CIPHER *afalg_aes_cbc(int nid) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  |     cbc_handles *cipher_handle = get_cipher_handle(nid); | 
					
						
							| 
									
										
										
										
											2020-10-28 17:35:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (cipher_handle == NULL) | 
					
						
							|  |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  |     if (cipher_handle->_hidden == NULL | 
					
						
							|  |  |  |         && ((cipher_handle->_hidden = | 
					
						
							|  |  |  |          EVP_CIPHER_meth_new(nid, | 
					
						
							|  |  |  |                              AES_BLOCK_SIZE, | 
					
						
							|  |  |  |                              cipher_handle->key_size)) == NULL | 
					
						
							|  |  |  |         || !EVP_CIPHER_meth_set_iv_length(cipher_handle->_hidden, | 
					
						
							|  |  |  |                                           AES_IV_LEN) | 
					
						
							|  |  |  |         || !EVP_CIPHER_meth_set_flags(cipher_handle->_hidden, | 
					
						
							|  |  |  |                                       EVP_CIPH_CBC_MODE | | 
					
						
							|  |  |  |                                       EVP_CIPH_FLAG_DEFAULT_ASN1) | 
					
						
							|  |  |  |         || !EVP_CIPHER_meth_set_init(cipher_handle->_hidden, | 
					
						
							|  |  |  |                                      afalg_cipher_init) | 
					
						
							|  |  |  |         || !EVP_CIPHER_meth_set_do_cipher(cipher_handle->_hidden, | 
					
						
							|  |  |  |                                           afalg_do_cipher) | 
					
						
							|  |  |  |         || !EVP_CIPHER_meth_set_cleanup(cipher_handle->_hidden, | 
					
						
							|  |  |  |                                         afalg_cipher_cleanup) | 
					
						
							|  |  |  |         || !EVP_CIPHER_meth_set_impl_ctx_size(cipher_handle->_hidden, | 
					
						
							|  |  |  |                                               sizeof(afalg_ctx)))) { | 
					
						
							|  |  |  |         EVP_CIPHER_meth_free(cipher_handle->_hidden); | 
					
						
							|  |  |  |         cipher_handle->_hidden= NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return cipher_handle->_hidden; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int afalg_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | 
					
						
							|  |  |  |                          const int **nids, int nid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int r = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (cipher == NULL) { | 
					
						
							|  |  |  |         *nids = afalg_cipher_nids; | 
					
						
							|  |  |  |         return (sizeof(afalg_cipher_nids) / sizeof(afalg_cipher_nids[0])); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (nid) { | 
					
						
							|  |  |  |     case NID_aes_128_cbc: | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  |     case NID_aes_192_cbc: | 
					
						
							|  |  |  |     case NID_aes_256_cbc: | 
					
						
							|  |  |  |         *cipher = afalg_aes_cbc(nid); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         *cipher = NULL; | 
					
						
							|  |  |  |         r = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return r; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int bind_afalg(ENGINE *e) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Ensure the afalg error handling is set up */ | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  |     unsigned short i; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     ERR_load_AFALG_strings(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!ENGINE_set_id(e, engine_afalg_id) | 
					
						
							|  |  |  |         || !ENGINE_set_name(e, engine_afalg_name) | 
					
						
							|  |  |  |         || !ENGINE_set_destroy_function(e, afalg_destroy) | 
					
						
							|  |  |  |         || !ENGINE_set_init_function(e, afalg_init) | 
					
						
							|  |  |  |         || !ENGINE_set_finish_function(e, afalg_finish)) { | 
					
						
							|  |  |  |         AFALGerr(AFALG_F_BIND_AFALG, AFALG_R_INIT_FAILED); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     /*
 | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  |      * Create _hidden_aes_xxx_cbc by calling afalg_aes_xxx_cbc | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |      * now, as bind_aflag can only be called by one thread at a | 
					
						
							|  |  |  |      * time. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-10-26 15:16:18 +08:00
										 |  |  |     for (i = 0; i < OSSL_NELEM(afalg_cipher_nids); i++) { | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  |         if (afalg_aes_cbc(afalg_cipher_nids[i]) == NULL) { | 
					
						
							|  |  |  |             AFALGerr(AFALG_F_BIND_AFALG, AFALG_R_INIT_FAILED); | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     if (!ENGINE_set_ciphers(e, afalg_ciphers)) { | 
					
						
							|  |  |  |         AFALGerr(AFALG_F_BIND_AFALG, AFALG_R_INIT_FAILED); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ifndef OPENSSL_NO_DYNAMIC_ENGINE
 | 
					
						
							|  |  |  | static int bind_helper(ENGINE *e, const char *id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (id && (strcmp(id, engine_afalg_id) != 0)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!afalg_chk_platform()) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 06:50:16 +08:00
										 |  |  |     if (!bind_afalg(e)) { | 
					
						
							|  |  |  |         afalg_destroy(e); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2024-01-29 06:50:16 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | IMPLEMENT_DYNAMIC_CHECK_FN() | 
					
						
							|  |  |  |     IMPLEMENT_DYNAMIC_BIND_FN(bind_helper) | 
					
						
							|  |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | static int afalg_chk_platform(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     int kver[3] = { -1, -1, -1 }; | 
					
						
							| 
									
										
										
										
											2016-06-02 06:15:12 +08:00
										 |  |  |     int sock; | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     char *str; | 
					
						
							|  |  |  |     struct utsname ut; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = uname(&ut); | 
					
						
							|  |  |  |     if (ret != 0) { | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         AFALGerr(AFALG_F_AFALG_CHK_PLATFORM, | 
					
						
							|  |  |  |                  AFALG_R_FAILED_TO_GET_PLATFORM_INFO); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     str = strtok(ut.release, "."); | 
					
						
							|  |  |  |     for (i = 0; i < 3 && str != NULL; i++) { | 
					
						
							|  |  |  |         kver[i] = atoi(str); | 
					
						
							|  |  |  |         str = strtok(NULL, "."); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (KERNEL_VERSION(kver[0], kver[1], kver[2]) | 
					
						
							|  |  |  |         < KERNEL_VERSION(K_MAJ, K_MIN1, K_MIN2)) { | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         ALG_ERR("ASYNC AFALG not supported this kernel(%d.%d.%d)\n", | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |                  kver[0], kver[1], kver[2]); | 
					
						
							| 
									
										
										
										
											2016-02-23 16:01:01 +08:00
										 |  |  |         ALG_ERR("ASYNC AFALG requires kernel version %d.%d.%d or later\n", | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |                  K_MAJ, K_MIN1, K_MIN2); | 
					
						
							|  |  |  |         AFALGerr(AFALG_F_AFALG_CHK_PLATFORM, | 
					
						
							|  |  |  |                  AFALG_R_KERNEL_DOES_NOT_SUPPORT_ASYNC_AFALG); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-02 06:15:12 +08:00
										 |  |  |     /* Test if we can actually create an AF_ALG socket */ | 
					
						
							|  |  |  |     sock = socket(AF_ALG, SOCK_SEQPACKET, 0); | 
					
						
							|  |  |  |     if (sock == -1) { | 
					
						
							|  |  |  |         AFALGerr(AFALG_F_AFALG_CHK_PLATFORM, AFALG_R_SOCKET_CREATE_FAILED); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     close(sock); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ifdef OPENSSL_NO_DYNAMIC_ENGINE
 | 
					
						
							|  |  |  | static ENGINE *engine_afalg(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ENGINE *ret = ENGINE_new(); | 
					
						
							|  |  |  |     if (ret == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     if (!bind_afalg(ret)) { | 
					
						
							|  |  |  |         ENGINE_free(ret); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 19:20:16 +08:00
										 |  |  | void engine_load_afalg_int(void) | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     ENGINE *toadd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!afalg_chk_platform()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     toadd = engine_afalg(); | 
					
						
							|  |  |  |     if (toadd == NULL) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-11-04 19:34:15 +08:00
										 |  |  |     ERR_set_mark(); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     ENGINE_add(toadd); | 
					
						
							| 
									
										
										
										
											2020-11-04 19:34:15 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * If the "add" worked, it gets a structural reference. So either way, we | 
					
						
							|  |  |  |      * release our just-created reference. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     ENGINE_free(toadd); | 
					
						
							| 
									
										
										
										
											2020-11-04 19:34:15 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * If the "add" didn't work, it was probably a conflict because it was | 
					
						
							|  |  |  |      * already added (eg. someone calling ENGINE_load_blah then calling | 
					
						
							|  |  |  |      * ENGINE_load_builtin_engines() perhaps). | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     ERR_pop_to_mark(); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | # endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int afalg_init(ENGINE *e) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int afalg_finish(ENGINE *e) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  | static int free_cbc(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-11-15 08:33:07 +08:00
										 |  |  |     short unsigned int i; | 
					
						
							| 
									
										
										
										
											2021-10-26 15:16:18 +08:00
										 |  |  |     for (i = 0; i < OSSL_NELEM(afalg_cipher_nids); i++) { | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  |         EVP_CIPHER_meth_free(cbc_handle[i]._hidden); | 
					
						
							|  |  |  |         cbc_handle[i]._hidden = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  | static int afalg_destroy(ENGINE *e) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ERR_unload_AFALG_strings(); | 
					
						
							| 
									
										
										
										
											2017-11-11 14:31:58 +08:00
										 |  |  |     free_cbc(); | 
					
						
							| 
									
										
										
										
											2016-02-17 21:38:36 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif                          /* KERNEL VERSION */
 |