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