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