| 1 | /* |
| 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | * You may not use this file except in compliance with the License. |
| 6 | * A copy of the License is located at |
| 7 | * |
| 8 | * http://aws.amazon.com/apache2.0 |
| 9 | * |
| 10 | * or in the "license" file accompanying this file. This file is distributed |
| 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | * express or implied. See the License for the specific language governing |
| 13 | * permissions and limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #pragma once |
| 17 | |
| 18 | #include <s2n.h> |
| 19 | |
| 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | /** |
| 25 | * @file async_offload.h |
| 26 | * |
| 27 | * The following APIs enable applications to offload expensive handshake operations that do not require user input. |
| 28 | * This model can be useful to move CPU-heavy operations (e.g. cryptographic calculations) out of the main event loop. |
| 29 | */ |
| 30 | |
| 31 | /** |
| 32 | * Opaque struct for the async offloading operation |
| 33 | */ |
| 34 | struct s2n_async_offload_op; |
| 35 | |
| 36 | /** |
| 37 | * The type of operations supported by the async offloading callback. Each type is represented by a different bit. |
| 38 | * |
| 39 | * S2N_ASYNC_OFFLOAD_ALLOW_ALL will automatically opt in to all the new types added in the future. |
| 40 | */ |
| 41 | typedef enum { |
| 42 | S2N_ASYNC_OFFLOAD_PKEY_VERIFY = 0x01, |
| 43 | /* Max value: ISO C restricts enumerator values to range of ‘int’ before C2X. */ |
| 44 | S2N_ASYNC_OFFLOAD_ALLOW_ALL = 0x7FFFFFFF, |
| 45 | } s2n_async_offload_op_type; |
| 46 | |
| 47 | /** |
| 48 | * The callback function invoked every time an allowed async operation is encountered during the handshake. |
| 49 | * |
| 50 | * To perform an operation asynchronously, the following condiditions must be satisfied: |
| 51 | * 1) This op type must be included in the allow list; |
| 52 | * 2) Async offloading callback returns success and s2n_async_offload_op_perform() is invoked outside the callback. |
| 53 | * |
| 54 | * If s2n_async_offload_op_perform() is invoked inside the callback, it is equivalent to the synchronous use case. |
| 55 | * |
| 56 | * `op` is owned by s2n-tls and will be freed along with s2n_connection eventually. |
| 57 | * |
| 58 | * @param conn Connection which triggered the async offloading callback |
| 59 | * @param op An opaque object representing the async operation |
| 60 | * @param ctx Application data provided to the callback via s2n_config_set_async_offload_callback() |
| 61 | */ |
| 62 | typedef int (*s2n_async_offload_cb)(struct s2n_connection *conn, struct s2n_async_offload_op *op, void *ctx); |
| 63 | |
| 64 | /** |
| 65 | * Sets up the custom async offloading callback and configures the offloaded handshake operations via allow_list. |
| 66 | * |
| 67 | * The value of allow_list should be the Bit-OR of all the allowed s2n_async_offload_op_type values. |
| 68 | * |
| 69 | * S2N_ASYNC_OFFLOAD_ALLOW_ALL provides the performance benefit of offloading all the supported operations; |
| 70 | * ensure your callback can support arbitrary operations. Otherwise, only allow operations that fit your use case. |
| 71 | * |
| 72 | * @param config Config to set the callback |
| 73 | * @param allow_list A bit representation of allowed operations |
| 74 | * @param fn The function that should be called for each allowed async operation |
| 75 | * @param ctx Optional application data passed to the callback |
| 76 | */ |
| 77 | S2N_API extern int s2n_config_set_async_offload_callback(struct s2n_config *config, uint32_t allow_list, |
| 78 | s2n_async_offload_cb fn, void *ctx); |
| 79 | |
| 80 | /** |
| 81 | * Performs the operation triggered by the async offloading callback. |
| 82 | * |
| 83 | * To execute operations asynchronously, users should spawn a separate thread to invoke s2n_async_offload_op_perform() |
| 84 | * and immediately return S2N_SUCCESS from the callback without waiting for that separate thread to complete. |
| 85 | * |
| 86 | * s2n_negotiate() will throw an `S2N_ERR_T_BLOCKED` error if the handshake is pending on the async offloading callback. |
| 87 | * Retrying s2n_negotiate() will produce the same result until s2n_async_offload_op_perform() is completed. |
| 88 | * |
| 89 | * s2n_async_offload_op_perform() can only be called once for each triggered operation. |
| 90 | * |
| 91 | * @param op An opaque object representing the async operation |
| 92 | */ |
| 93 | S2N_API extern int s2n_async_offload_op_perform(struct s2n_async_offload_op *op); |
| 94 | |
| 95 | #ifdef __cplusplus |
| 96 | } |
| 97 | #endif |