Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #ifndef included_vnet_crypto_crypto_h |
| 17 | #define included_vnet_crypto_crypto_h |
| 18 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 19 | #include <vlib/vlib.h> |
| 20 | |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 21 | #define VNET_CRYPTO_FRAME_SIZE 64 |
Fan Zhang | e4db945 | 2021-03-30 17:31:38 +0100 | [diff] [blame] | 22 | #define VNET_CRYPTO_FRAME_POOL_SIZE 1024 |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 23 | |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 24 | /* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES */ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 25 | #define foreach_crypto_cipher_alg \ |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 26 | _(DES_CBC, "des-cbc", 7) \ |
Neale Ranns | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 27 | _(3DES_CBC, "3des-cbc", 24) \ |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 28 | _(AES_128_CBC, "aes-128-cbc", 16) \ |
| 29 | _(AES_192_CBC, "aes-192-cbc", 24) \ |
| 30 | _(AES_256_CBC, "aes-256-cbc", 32) \ |
| 31 | _(AES_128_CTR, "aes-128-ctr", 16) \ |
| 32 | _(AES_192_CTR, "aes-192-ctr", 24) \ |
| 33 | _(AES_256_CTR, "aes-256-ctr", 32) |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 34 | |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 35 | /* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES */ |
Benoît Ganne | 84e6658 | 2023-03-10 17:33:03 +0100 | [diff] [blame] | 36 | #define foreach_crypto_aead_alg \ |
| 37 | _ (AES_128_GCM, "aes-128-gcm", 16) \ |
| 38 | _ (AES_192_GCM, "aes-192-gcm", 24) \ |
| 39 | _ (AES_256_GCM, "aes-256-gcm", 32) \ |
| 40 | _ (AES_128_NULL_GMAC, "aes-128-null-gmac", 16) \ |
| 41 | _ (AES_192_NULL_GMAC, "aes-192-null-gmac", 24) \ |
| 42 | _ (AES_256_NULL_GMAC, "aes-256-null-gmac", 32) \ |
| 43 | _ (CHACHA20_POLY1305, "chacha20-poly1305", 32) |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 44 | |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 45 | #define foreach_crypto_hash_alg \ |
| 46 | _ (SHA1, "sha-1") \ |
| 47 | _ (SHA224, "sha-224") \ |
| 48 | _ (SHA256, "sha-256") \ |
| 49 | _ (SHA384, "sha-384") \ |
| 50 | _ (SHA512, "sha-512") |
| 51 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 52 | #define foreach_crypto_hmac_alg \ |
Filip Tehlar | e225f71 | 2019-03-19 10:37:06 -0700 | [diff] [blame] | 53 | _(MD5, "md5") \ |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 54 | _(SHA1, "sha-1") \ |
| 55 | _(SHA224, "sha-224") \ |
| 56 | _(SHA256, "sha-256") \ |
| 57 | _(SHA384, "sha-384") \ |
| 58 | _(SHA512, "sha-512") |
| 59 | |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 60 | #define foreach_crypto_op_type \ |
| 61 | _ (ENCRYPT, "encrypt") \ |
| 62 | _ (DECRYPT, "decrypt") \ |
| 63 | _ (AEAD_ENCRYPT, "aead-encrypt") \ |
| 64 | _ (AEAD_DECRYPT, "aead-decrypt") \ |
| 65 | _ (HMAC, "hmac") \ |
| 66 | _ (HASH, "hash") |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 67 | |
| 68 | typedef enum |
| 69 | { |
| 70 | #define _(n, s) VNET_CRYPTO_OP_TYPE_##n, |
| 71 | foreach_crypto_op_type |
| 72 | #undef _ |
| 73 | VNET_CRYPTO_OP_N_TYPES, |
| 74 | } vnet_crypto_op_type_t; |
| 75 | |
| 76 | #define foreach_crypto_op_status \ |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 77 | _(IDLE, "idle") \ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 78 | _(PENDING, "pending") \ |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 79 | _(WORK_IN_PROGRESS, "work-in-progress") \ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 80 | _(COMPLETED, "completed") \ |
| 81 | _(FAIL_NO_HANDLER, "no-handler") \ |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 82 | _(FAIL_BAD_HMAC, "bad-hmac") \ |
| 83 | _(FAIL_ENGINE_ERR, "engine-error") |
| 84 | |
| 85 | /** async crypto **/ |
| 86 | |
| 87 | /* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, TAG_LEN, AAD_LEN */ |
Gabriel Oginski | 492d779 | 2021-11-10 07:59:56 +0000 | [diff] [blame] | 88 | #define foreach_crypto_aead_async_alg \ |
| 89 | _ (AES_128_GCM, "aes-128-gcm-aad8", 16, 16, 8) \ |
| 90 | _ (AES_128_GCM, "aes-128-gcm-aad12", 16, 16, 12) \ |
| 91 | _ (AES_192_GCM, "aes-192-gcm-aad8", 24, 16, 8) \ |
| 92 | _ (AES_192_GCM, "aes-192-gcm-aad12", 24, 16, 12) \ |
| 93 | _ (AES_256_GCM, "aes-256-gcm-aad8", 32, 16, 8) \ |
| 94 | _ (AES_256_GCM, "aes-256-gcm-aad12", 32, 16, 12) \ |
Benoît Ganne | 84e6658 | 2023-03-10 17:33:03 +0100 | [diff] [blame] | 95 | _ (AES_128_NULL_GMAC, "aes-128-null-gmac-aad8", 16, 16, 8) \ |
| 96 | _ (AES_128_NULL_GMAC, "aes-128-null-gmac-aad12", 16, 16, 12) \ |
| 97 | _ (AES_192_NULL_GMAC, "aes-192-null-gmac-aad8", 24, 16, 8) \ |
| 98 | _ (AES_192_NULL_GMAC, "aes-192-null-gmac-aad12", 24, 16, 12) \ |
| 99 | _ (AES_256_NULL_GMAC, "aes-256-null-gmac-aad8", 32, 16, 8) \ |
| 100 | _ (AES_256_NULL_GMAC, "aes-256-null-gmac-aad12", 32, 16, 12) \ |
Gabriel Oginski | 492d779 | 2021-11-10 07:59:56 +0000 | [diff] [blame] | 101 | _ (CHACHA20_POLY1305, "chacha20-poly1305-aad8", 32, 16, 8) \ |
| 102 | _ (CHACHA20_POLY1305, "chacha20-poly1305-aad12", 32, 16, 12) \ |
| 103 | _ (CHACHA20_POLY1305, "chacha20-poly1305", 32, 16, 0) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 104 | |
| 105 | /* CRYPTO_ID, INTEG_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, DIGEST_LEN */ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 106 | #define foreach_crypto_link_async_alg \ |
Alexander Chernavin | 005d1e4 | 2021-02-01 05:17:24 -0500 | [diff] [blame] | 107 | _ (3DES_CBC, MD5, "3des-cbc-hmac-md5", 24, 12) \ |
| 108 | _ (AES_128_CBC, MD5, "aes-128-cbc-hmac-md5", 16, 12) \ |
| 109 | _ (AES_192_CBC, MD5, "aes-192-cbc-hmac-md5", 24, 12) \ |
| 110 | _ (AES_256_CBC, MD5, "aes-256-cbc-hmac-md5", 32, 12) \ |
| 111 | _ (3DES_CBC, SHA1, "3des-cbc-hmac-sha-1", 24, 12) \ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 112 | _ (AES_128_CBC, SHA1, "aes-128-cbc-hmac-sha-1", 16, 12) \ |
| 113 | _ (AES_192_CBC, SHA1, "aes-192-cbc-hmac-sha-1", 24, 12) \ |
| 114 | _ (AES_256_CBC, SHA1, "aes-256-cbc-hmac-sha-1", 32, 12) \ |
Alexander Chernavin | 005d1e4 | 2021-02-01 05:17:24 -0500 | [diff] [blame] | 115 | _ (3DES_CBC, SHA224, "3des-cbc-hmac-sha-224", 24, 14) \ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 116 | _ (AES_128_CBC, SHA224, "aes-128-cbc-hmac-sha-224", 16, 14) \ |
| 117 | _ (AES_192_CBC, SHA224, "aes-192-cbc-hmac-sha-224", 24, 14) \ |
| 118 | _ (AES_256_CBC, SHA224, "aes-256-cbc-hmac-sha-224", 32, 14) \ |
Alexander Chernavin | 005d1e4 | 2021-02-01 05:17:24 -0500 | [diff] [blame] | 119 | _ (3DES_CBC, SHA256, "3des-cbc-hmac-sha-256", 24, 16) \ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 120 | _ (AES_128_CBC, SHA256, "aes-128-cbc-hmac-sha-256", 16, 16) \ |
| 121 | _ (AES_192_CBC, SHA256, "aes-192-cbc-hmac-sha-256", 24, 16) \ |
| 122 | _ (AES_256_CBC, SHA256, "aes-256-cbc-hmac-sha-256", 32, 16) \ |
Alexander Chernavin | 005d1e4 | 2021-02-01 05:17:24 -0500 | [diff] [blame] | 123 | _ (3DES_CBC, SHA384, "3des-cbc-hmac-sha-384", 24, 24) \ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 124 | _ (AES_128_CBC, SHA384, "aes-128-cbc-hmac-sha-384", 16, 24) \ |
| 125 | _ (AES_192_CBC, SHA384, "aes-192-cbc-hmac-sha-384", 24, 24) \ |
| 126 | _ (AES_256_CBC, SHA384, "aes-256-cbc-hmac-sha-384", 32, 24) \ |
Alexander Chernavin | 005d1e4 | 2021-02-01 05:17:24 -0500 | [diff] [blame] | 127 | _ (3DES_CBC, SHA512, "3des-cbc-hmac-sha-512", 24, 32) \ |
Benoît Ganne | 40ee200 | 2021-01-22 18:09:40 +0100 | [diff] [blame] | 128 | _ (AES_128_CBC, SHA512, "aes-128-cbc-hmac-sha-512", 16, 32) \ |
| 129 | _ (AES_192_CBC, SHA512, "aes-192-cbc-hmac-sha-512", 24, 32) \ |
| 130 | _ (AES_256_CBC, SHA512, "aes-256-cbc-hmac-sha-512", 32, 32) \ |
| 131 | _ (AES_128_CTR, SHA1, "aes-128-ctr-hmac-sha-1", 16, 12) \ |
| 132 | _ (AES_192_CTR, SHA1, "aes-192-ctr-hmac-sha-1", 24, 12) \ |
| 133 | _ (AES_256_CTR, SHA1, "aes-256-ctr-hmac-sha-1", 32, 12) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 134 | |
| 135 | #define foreach_crypto_async_op_type \ |
| 136 | _(ENCRYPT, "async-encrypt") \ |
| 137 | _(DECRYPT, "async-decrypt") |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 138 | |
| 139 | typedef enum |
| 140 | { |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 141 | VNET_CRYPTO_KEY_OP_ADD, |
| 142 | VNET_CRYPTO_KEY_OP_DEL, |
| 143 | VNET_CRYPTO_KEY_OP_MODIFY, |
| 144 | } vnet_crypto_key_op_t; |
| 145 | |
| 146 | typedef enum |
| 147 | { |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 148 | #define _(n, s) VNET_CRYPTO_OP_STATUS_##n, |
| 149 | foreach_crypto_op_status |
| 150 | #undef _ |
| 151 | VNET_CRYPTO_OP_N_STATUS, |
| 152 | } vnet_crypto_op_status_t; |
| 153 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 154 | typedef enum |
| 155 | { |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 156 | VNET_CRYPTO_ALG_NONE = 0, |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 157 | #define _(n, s, l) VNET_CRYPTO_ALG_##n, |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 158 | foreach_crypto_cipher_alg foreach_crypto_aead_alg |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 159 | #undef _ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 160 | #define _(n, s) VNET_CRYPTO_ALG_HMAC_##n, |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 161 | foreach_crypto_hmac_alg |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 162 | #undef _ |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 163 | #define _(n, s) VNET_CRYPTO_ALG_HASH_##n, |
| 164 | foreach_crypto_hash_alg |
| 165 | #undef _ |
| 166 | VNET_CRYPTO_N_ALGS, |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 167 | } vnet_crypto_alg_t; |
| 168 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 169 | typedef enum |
| 170 | { |
| 171 | #define _(n, s) VNET_CRYPTO_ASYNC_OP_TYPE_##n, |
| 172 | foreach_crypto_async_op_type |
| 173 | #undef _ |
| 174 | VNET_CRYPTO_ASYNC_OP_N_TYPES, |
| 175 | } vnet_crypto_async_op_type_t; |
| 176 | |
| 177 | typedef enum |
| 178 | { |
| 179 | VNET_CRYPTO_ASYNC_ALG_NONE = 0, |
| 180 | #define _(n, s, k, t, a) \ |
| 181 | VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a, |
| 182 | foreach_crypto_aead_async_alg |
| 183 | #undef _ |
| 184 | #define _(c, h, s, k ,d) \ |
| 185 | VNET_CRYPTO_ALG_##c##_##h##_TAG##d, |
| 186 | foreach_crypto_link_async_alg |
| 187 | #undef _ |
| 188 | VNET_CRYPTO_N_ASYNC_ALGS, |
| 189 | } vnet_crypto_async_alg_t; |
| 190 | |
| 191 | typedef enum |
| 192 | { |
| 193 | VNET_CRYPTO_ASYNC_OP_NONE = 0, |
| 194 | #define _(n, s, k, t, a) \ |
| 195 | VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \ |
| 196 | VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC, |
| 197 | foreach_crypto_aead_async_alg |
| 198 | #undef _ |
| 199 | #define _(c, h, s, k ,d) \ |
| 200 | VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \ |
| 201 | VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC, |
| 202 | foreach_crypto_link_async_alg |
| 203 | #undef _ |
| 204 | VNET_CRYPTO_ASYNC_OP_N_IDS, |
| 205 | } vnet_crypto_async_op_id_t; |
| 206 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 207 | typedef struct |
| 208 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 209 | union |
| 210 | { |
| 211 | struct |
| 212 | { |
| 213 | u8 *data; |
| 214 | vnet_crypto_alg_t alg:8; |
| 215 | }; |
| 216 | struct |
| 217 | { |
| 218 | u32 index_crypto; |
| 219 | u32 index_integ; |
| 220 | vnet_crypto_async_alg_t async_alg:8; |
| 221 | }; |
| 222 | }; |
| 223 | #define VNET_CRYPTO_KEY_TYPE_DATA 0 |
| 224 | #define VNET_CRYPTO_KEY_TYPE_LINK 1 |
| 225 | u8 type; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 226 | } vnet_crypto_key_t; |
| 227 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 228 | typedef enum |
| 229 | { |
| 230 | VNET_CRYPTO_OP_NONE = 0, |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 231 | #define _(n, s, l) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC, |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 232 | foreach_crypto_cipher_alg foreach_crypto_aead_alg |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 233 | #undef _ |
| 234 | #define _(n, s) VNET_CRYPTO_OP_##n##_HMAC, |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 235 | foreach_crypto_hmac_alg |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 236 | #undef _ |
Filip Tehlar | 06111a8 | 2021-05-03 15:29:56 +0000 | [diff] [blame] | 237 | #define _(n, s) VNET_CRYPTO_OP_##n##_HASH, |
| 238 | foreach_crypto_hash_alg |
| 239 | #undef _ |
| 240 | VNET_CRYPTO_N_OP_IDS, |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 241 | } vnet_crypto_op_id_t; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 242 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 243 | typedef enum |
| 244 | { |
| 245 | CRYPTO_OP_SIMPLE, |
| 246 | CRYPTO_OP_CHAINED, |
| 247 | CRYPTO_OP_BOTH, |
| 248 | } crypto_op_class_type_t; |
| 249 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 250 | typedef struct |
| 251 | { |
| 252 | char *name; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 253 | vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES]; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 254 | } vnet_crypto_alg_data_t; |
| 255 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 256 | typedef struct |
| 257 | { |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 258 | u8 *src; |
| 259 | u8 *dst; |
| 260 | u32 len; |
| 261 | } vnet_crypto_op_chunk_t; |
| 262 | |
| 263 | typedef struct |
| 264 | { |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 265 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 266 | uword user_data; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 267 | vnet_crypto_op_id_t op:16; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 268 | vnet_crypto_op_status_t status:8; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 269 | u8 flags; |
Benoît Ganne | 063549f | 2022-01-19 10:09:42 +0100 | [diff] [blame] | 270 | #define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 0) |
| 271 | #define VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS (1 << 1) |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 272 | |
| 273 | union |
| 274 | { |
| 275 | u8 digest_len; |
| 276 | u8 tag_len; |
| 277 | }; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 278 | u16 aad_len; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 279 | |
| 280 | union |
| 281 | { |
| 282 | struct |
| 283 | { |
| 284 | u8 *src; |
| 285 | u8 *dst; |
| 286 | }; |
| 287 | |
| 288 | /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */ |
| 289 | u16 n_chunks; |
| 290 | }; |
| 291 | |
| 292 | union |
| 293 | { |
| 294 | u32 len; |
| 295 | /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */ |
| 296 | u32 chunk_index; |
| 297 | }; |
| 298 | |
| 299 | u32 key_index; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 300 | u8 *iv; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 301 | u8 *aad; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 302 | |
| 303 | union |
| 304 | { |
| 305 | u8 *tag; |
| 306 | u8 *digest; |
| 307 | }; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 308 | } vnet_crypto_op_t; |
| 309 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 310 | STATIC_ASSERT_SIZEOF (vnet_crypto_op_t, CLIB_CACHE_LINE_BYTES); |
| 311 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 312 | typedef struct |
| 313 | { |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 314 | vnet_crypto_op_type_t type; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 315 | vnet_crypto_alg_t alg; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 316 | u32 active_engine_index_simple; |
| 317 | u32 active_engine_index_chained; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 318 | } vnet_crypto_op_data_t; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 319 | |
| 320 | typedef struct |
| 321 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 322 | vnet_crypto_async_op_type_t type; |
| 323 | vnet_crypto_async_alg_t alg; |
| 324 | u32 active_engine_index_async; |
| 325 | } vnet_crypto_async_op_data_t; |
| 326 | |
| 327 | typedef struct |
| 328 | { |
| 329 | char *name; |
| 330 | vnet_crypto_async_op_id_t op_by_type[VNET_CRYPTO_ASYNC_OP_N_TYPES]; |
| 331 | } vnet_crypto_async_alg_data_t; |
| 332 | |
| 333 | typedef struct |
| 334 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 335 | u8 *iv; |
| 336 | union |
| 337 | { |
| 338 | u8 *digest; |
| 339 | u8 *tag; |
| 340 | }; |
| 341 | u8 *aad; |
Neale Ranns | 63ab851 | 2021-02-24 09:18:53 +0000 | [diff] [blame] | 342 | u32 key_index; |
| 343 | u32 crypto_total_length; |
| 344 | i16 crypto_start_offset; /* first buffer offset */ |
| 345 | i16 integ_start_offset; |
| 346 | /* adj total_length for integ, e.g.4 bytes for IPSec ESN */ |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 347 | i16 integ_length_adj; |
Neale Ranns | 63ab851 | 2021-02-24 09:18:53 +0000 | [diff] [blame] | 348 | vnet_crypto_op_status_t status : 8; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 349 | u8 flags; /**< share same VNET_CRYPTO_OP_FLAG_* values */ |
| 350 | } vnet_crypto_async_frame_elt_t; |
| 351 | |
Neale Ranns | 63ab851 | 2021-02-24 09:18:53 +0000 | [diff] [blame] | 352 | /* Assert the size so the compiler will warn us when it changes */ |
| 353 | STATIC_ASSERT_SIZEOF (vnet_crypto_async_frame_elt_t, 5 * sizeof (u64)); |
| 354 | |
| 355 | typedef enum vnet_crypto_async_frame_state_t_ |
| 356 | { |
| 357 | VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED, |
| 358 | /* frame waiting to be processed */ |
| 359 | VNET_CRYPTO_FRAME_STATE_PENDING, |
| 360 | VNET_CRYPTO_FRAME_STATE_WORK_IN_PROGRESS, |
| 361 | VNET_CRYPTO_FRAME_STATE_SUCCESS, |
| 362 | VNET_CRYPTO_FRAME_STATE_ELT_ERROR |
| 363 | } __clib_packed vnet_crypto_async_frame_state_t; |
| 364 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 365 | typedef struct |
| 366 | { |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 367 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Neale Ranns | 63ab851 | 2021-02-24 09:18:53 +0000 | [diff] [blame] | 368 | vnet_crypto_async_frame_state_t state; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 369 | vnet_crypto_async_op_id_t op:8; |
| 370 | u16 n_elts; |
| 371 | vnet_crypto_async_frame_elt_t elts[VNET_CRYPTO_FRAME_SIZE]; |
| 372 | u32 buffer_indices[VNET_CRYPTO_FRAME_SIZE]; |
| 373 | u16 next_node_index[VNET_CRYPTO_FRAME_SIZE]; |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 374 | u32 enqueue_thread_index; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 375 | } vnet_crypto_async_frame_t; |
| 376 | |
| 377 | typedef struct |
| 378 | { |
| 379 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 380 | vnet_crypto_async_frame_t *frame_pool; |
Neale Ranns | 63ab851 | 2021-02-24 09:18:53 +0000 | [diff] [blame] | 381 | u32 *buffer_indices; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 382 | u16 *nexts; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 383 | } vnet_crypto_thread_t; |
| 384 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 385 | typedef u32 vnet_crypto_key_index_t; |
| 386 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 387 | typedef u32 (vnet_crypto_chained_ops_handler_t) (vlib_main_t * vm, |
| 388 | vnet_crypto_op_t * ops[], |
| 389 | vnet_crypto_op_chunk_t * |
| 390 | chunks, u32 n_ops); |
| 391 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 392 | typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm, |
| 393 | vnet_crypto_op_t * ops[], u32 n_ops); |
| 394 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 395 | typedef void (vnet_crypto_key_handler_t) (vlib_main_t * vm, |
| 396 | vnet_crypto_key_op_t kop, |
| 397 | vnet_crypto_key_index_t idx); |
| 398 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 399 | /** async crypto function handlers **/ |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 400 | typedef int |
| 401 | (vnet_crypto_frame_enqueue_t) (vlib_main_t * vm, |
| 402 | vnet_crypto_async_frame_t * frame); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 403 | typedef vnet_crypto_async_frame_t * |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 404 | (vnet_crypto_frame_dequeue_t) (vlib_main_t * vm, u32 * nb_elts_processed, |
| 405 | u32 * enqueue_thread_idx); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 406 | |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 407 | u32 |
| 408 | vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio, |
| 409 | char *desc); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 410 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 411 | void vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index, |
| 412 | vnet_crypto_op_id_t opt, |
| 413 | vnet_crypto_ops_handler_t * oph); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 414 | |
| 415 | void vnet_crypto_register_chained_ops_handler (vlib_main_t * vm, |
| 416 | u32 engine_index, |
| 417 | vnet_crypto_op_id_t opt, |
| 418 | vnet_crypto_chained_ops_handler_t |
| 419 | * oph); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 420 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 421 | void vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index, |
| 422 | vnet_crypto_op_id_t opt, |
| 423 | vnet_crypto_ops_handler_t * fn, |
| 424 | vnet_crypto_chained_ops_handler_t * |
| 425 | cfn); |
| 426 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 427 | void vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index, |
| 428 | vnet_crypto_key_handler_t * keyh); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 429 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 430 | /** async crypto register functions */ |
| 431 | u32 vnet_crypto_register_post_node (vlib_main_t * vm, char *post_node_name); |
Jakub Wysocki | 83b2bb8 | 2021-11-30 10:53:03 +0000 | [diff] [blame] | 432 | |
| 433 | void |
| 434 | vnet_crypto_register_enqueue_handler (vlib_main_t *vm, u32 engine_index, |
| 435 | vnet_crypto_async_op_id_t opt, |
| 436 | vnet_crypto_frame_enqueue_t *enq_fn); |
| 437 | |
| 438 | void |
| 439 | vnet_crypto_register_dequeue_handler (vlib_main_t *vm, u32 engine_index, |
| 440 | vnet_crypto_frame_dequeue_t *deq_fn); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 441 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 442 | typedef struct |
| 443 | { |
| 444 | char *name; |
| 445 | char *desc; |
| 446 | int priority; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 447 | vnet_crypto_key_handler_t *key_op_handler; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 448 | vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_IDS]; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 449 | vnet_crypto_chained_ops_handler_t |
| 450 | * chained_ops_handlers[VNET_CRYPTO_N_OP_IDS]; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 451 | vnet_crypto_frame_enqueue_t *enqueue_handlers[VNET_CRYPTO_ASYNC_OP_N_IDS]; |
Jakub Wysocki | 83b2bb8 | 2021-11-30 10:53:03 +0000 | [diff] [blame] | 452 | vnet_crypto_frame_dequeue_t *dequeue_handler; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 453 | } vnet_crypto_engine_t; |
| 454 | |
| 455 | typedef struct |
| 456 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 457 | u32 node_idx; |
| 458 | u32 next_idx; |
| 459 | } vnet_crypto_async_next_node_t; |
| 460 | |
| 461 | typedef struct |
| 462 | { |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 463 | vnet_crypto_alg_data_t *algs; |
| 464 | vnet_crypto_thread_t *threads; |
| 465 | vnet_crypto_ops_handler_t **ops_handlers; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 466 | vnet_crypto_chained_ops_handler_t **chained_ops_handlers; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 467 | vnet_crypto_frame_enqueue_t **enqueue_handlers; |
| 468 | vnet_crypto_frame_dequeue_t **dequeue_handlers; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 469 | vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS]; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 470 | vnet_crypto_async_op_data_t async_opt_data[VNET_CRYPTO_ASYNC_OP_N_IDS]; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 471 | vnet_crypto_engine_t *engines; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 472 | vnet_crypto_key_t *keys; |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 473 | uword *engine_index_by_name; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 474 | uword *alg_index_by_name; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 475 | uword *async_alg_index_by_name; |
| 476 | vnet_crypto_async_alg_data_t *async_algs; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 477 | vnet_crypto_async_next_node_t *next_nodes; |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 478 | u32 crypto_node_index; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 479 | } vnet_crypto_main_t; |
| 480 | |
| 481 | extern vnet_crypto_main_t crypto_main; |
| 482 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 483 | u32 vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], |
| 484 | vnet_crypto_op_chunk_t * chunks, |
| 485 | u32 n_ops); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 486 | u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], |
| 487 | u32 n_ops); |
| 488 | |
Vratko Polak | 139aba2 | 2023-08-17 16:22:48 +0200 | [diff] [blame] | 489 | void vnet_crypto_set_async_dispatch (u8 mode, u8 adaptive); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 490 | int vnet_crypto_set_handler2 (char *ops_handler_name, char *engine, |
| 491 | crypto_op_class_type_t oct); |
Neale Ranns | ece2ae0 | 2019-06-21 12:44:11 +0000 | [diff] [blame] | 492 | int vnet_crypto_is_set_handler (vnet_crypto_alg_t alg); |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 493 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 494 | u32 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg, |
| 495 | u8 * data, u16 length); |
| 496 | void vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index); |
Lijian Zhang | 6f8252e | 2023-11-08 09:09:10 +0000 | [diff] [blame] | 497 | void vnet_crypto_key_update (vlib_main_t *vm, vnet_crypto_key_index_t index); |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 498 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 499 | /** |
| 500 | * Use 2 created keys to generate new key for linked algs (cipher + integ) |
| 501 | * The returned key index is to be used for linked alg only. |
| 502 | **/ |
| 503 | u32 vnet_crypto_key_add_linked (vlib_main_t * vm, |
| 504 | vnet_crypto_key_index_t index_crypto, |
| 505 | vnet_crypto_key_index_t index_integ); |
| 506 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 507 | int vnet_crypto_set_async_handler2 (char *alg_name, char *engine); |
| 508 | |
| 509 | int vnet_crypto_is_set_async_handler (vnet_crypto_async_op_id_t opt); |
| 510 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 511 | vnet_crypto_async_alg_t vnet_crypto_link_algs (vnet_crypto_alg_t crypto_alg, |
| 512 | vnet_crypto_alg_t integ_alg); |
| 513 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 514 | format_function_t format_vnet_crypto_alg; |
| 515 | format_function_t format_vnet_crypto_engine; |
| 516 | format_function_t format_vnet_crypto_op; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 517 | format_function_t format_vnet_crypto_op_type; |
| 518 | format_function_t format_vnet_crypto_op_status; |
Damjan Marion | 61c0a3d | 2019-04-05 19:42:21 +0200 | [diff] [blame] | 519 | unformat_function_t unformat_vnet_crypto_alg; |
Damjan Marion | a03d182 | 2019-03-28 21:40:48 +0100 | [diff] [blame] | 520 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 521 | format_function_t format_vnet_crypto_async_op; |
| 522 | format_function_t format_vnet_crypto_async_alg; |
| 523 | format_function_t format_vnet_crypto_async_op_type; |
| 524 | |
Damjan Marion | a03d182 | 2019-03-28 21:40:48 +0100 | [diff] [blame] | 525 | static_always_inline void |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 526 | vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_id_t type) |
Damjan Marion | a03d182 | 2019-03-28 21:40:48 +0100 | [diff] [blame] | 527 | { |
| 528 | if (CLIB_DEBUG > 0) |
| 529 | clib_memset (op, 0xfe, sizeof (*op)); |
| 530 | op->op = type; |
| 531 | op->flags = 0; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 532 | op->key_index = ~0; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 533 | op->n_chunks = 0; |
Damjan Marion | a03d182 | 2019-03-28 21:40:48 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 536 | static_always_inline vnet_crypto_op_type_t |
| 537 | vnet_crypto_get_op_type (vnet_crypto_op_id_t id) |
| 538 | { |
| 539 | vnet_crypto_main_t *cm = &crypto_main; |
Lijian Zhang | b15d796 | 2019-09-27 16:25:35 +0800 | [diff] [blame] | 540 | ASSERT (id < VNET_CRYPTO_N_OP_IDS); |
| 541 | vnet_crypto_op_data_t *od = cm->opt_data + id; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 542 | return od->type; |
| 543 | } |
| 544 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 545 | static_always_inline vnet_crypto_key_t * |
| 546 | vnet_crypto_get_key (vnet_crypto_key_index_t index) |
| 547 | { |
| 548 | vnet_crypto_main_t *cm = &crypto_main; |
| 549 | return vec_elt_at_index (cm->keys, index); |
| 550 | } |
| 551 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 552 | static_always_inline int |
| 553 | vnet_crypto_set_handler (char *alg_name, char *engine) |
| 554 | { |
| 555 | return vnet_crypto_set_handler2 (alg_name, engine, CRYPTO_OP_BOTH); |
| 556 | } |
| 557 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 558 | /** async crypto inline functions **/ |
| 559 | |
| 560 | static_always_inline vnet_crypto_async_frame_t * |
| 561 | vnet_crypto_async_get_frame (vlib_main_t * vm, vnet_crypto_async_op_id_t opt) |
| 562 | { |
| 563 | vnet_crypto_main_t *cm = &crypto_main; |
| 564 | vnet_crypto_thread_t *ct = cm->threads + vm->thread_index; |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 565 | vnet_crypto_async_frame_t *f = NULL; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 566 | |
gaoginskx | f441b5d | 2021-06-07 12:07:01 +0100 | [diff] [blame] | 567 | if (PREDICT_TRUE (pool_free_elts (ct->frame_pool))) |
| 568 | { |
| 569 | pool_get_aligned (ct->frame_pool, f, CLIB_CACHE_LINE_BYTES); |
| 570 | #if CLIB_DEBUG > 0 |
| 571 | clib_memset (f, 0xfe, sizeof (*f)); |
| 572 | #endif |
| 573 | f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED; |
| 574 | f->op = opt; |
| 575 | f->n_elts = 0; |
| 576 | } |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 577 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 578 | return f; |
| 579 | } |
| 580 | |
| 581 | static_always_inline void |
| 582 | vnet_crypto_async_free_frame (vlib_main_t * vm, |
| 583 | vnet_crypto_async_frame_t * frame) |
| 584 | { |
| 585 | vnet_crypto_main_t *cm = &crypto_main; |
| 586 | vnet_crypto_thread_t *ct = cm->threads + vm->thread_index; |
| 587 | pool_put (ct->frame_pool, frame); |
| 588 | } |
| 589 | |
| 590 | static_always_inline int |
| 591 | vnet_crypto_async_submit_open_frame (vlib_main_t * vm, |
| 592 | vnet_crypto_async_frame_t * frame) |
| 593 | { |
| 594 | vnet_crypto_main_t *cm = &crypto_main; |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 595 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Xiaoming Jiang | 9a9604b | 2023-03-09 02:03:50 +0000 | [diff] [blame] | 596 | u32 i; |
| 597 | vlib_node_t *n; |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 598 | |
PiotrX Kleski | ef69b51 | 2020-11-24 08:26:26 +0000 | [diff] [blame] | 599 | frame->state = VNET_CRYPTO_FRAME_STATE_PENDING; |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 600 | frame->enqueue_thread_index = vm->thread_index; |
PiotrX Kleski | ef69b51 | 2020-11-24 08:26:26 +0000 | [diff] [blame] | 601 | |
Dastin Wilski | 8a4a7c2 | 2022-03-31 11:55:09 +0200 | [diff] [blame] | 602 | if (PREDICT_FALSE (cm->enqueue_handlers == NULL)) |
| 603 | { |
| 604 | frame->state = VNET_CRYPTO_FRAME_STATE_ELT_ERROR; |
| 605 | return -1; |
| 606 | } |
| 607 | |
PiotrX Kleski | ef69b51 | 2020-11-24 08:26:26 +0000 | [diff] [blame] | 608 | int ret = (cm->enqueue_handlers[frame->op]) (vm, frame); |
| 609 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 610 | if (PREDICT_TRUE (ret == 0)) |
| 611 | { |
Xiaoming Jiang | 9a9604b | 2023-03-09 02:03:50 +0000 | [diff] [blame] | 612 | n = vlib_get_node (vm, cm->crypto_node_index); |
| 613 | if (n->state == VLIB_NODE_STATE_INTERRUPT) |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 614 | { |
Xiaoming Jiang | 9a9604b | 2023-03-09 02:03:50 +0000 | [diff] [blame] | 615 | for (i = 0; i < tm->n_vlib_mains; i++) |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 616 | vlib_node_set_interrupt_pending (vlib_get_main_by_index (i), |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 617 | cm->crypto_node_index); |
| 618 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 619 | } |
PiotrX Kleski | ef69b51 | 2020-11-24 08:26:26 +0000 | [diff] [blame] | 620 | else |
| 621 | { |
| 622 | frame->state = VNET_CRYPTO_FRAME_STATE_ELT_ERROR; |
| 623 | } |
PiotrX Kleski | 2284817 | 2020-07-08 14:36:34 +0200 | [diff] [blame] | 624 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 625 | return ret; |
| 626 | } |
| 627 | |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 628 | static_always_inline void |
| 629 | vnet_crypto_async_add_to_frame (vlib_main_t *vm, vnet_crypto_async_frame_t *f, |
| 630 | u32 key_index, u32 crypto_len, |
| 631 | i16 integ_len_adj, i16 crypto_start_offset, |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 632 | i16 integ_start_offset, u32 buffer_index, |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 633 | u16 next_node, u8 *iv, u8 *tag, u8 *aad, |
| 634 | u8 flags) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 635 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 636 | vnet_crypto_async_frame_elt_t *fe; |
| 637 | u16 index; |
| 638 | |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 639 | ASSERT (f->n_elts < VNET_CRYPTO_FRAME_SIZE); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 640 | |
| 641 | index = f->n_elts; |
| 642 | fe = &f->elts[index]; |
| 643 | f->n_elts++; |
| 644 | fe->key_index = key_index; |
| 645 | fe->crypto_total_length = crypto_len; |
| 646 | fe->crypto_start_offset = crypto_start_offset; |
| 647 | fe->integ_start_offset = integ_start_offset; |
| 648 | fe->integ_length_adj = integ_len_adj; |
| 649 | fe->iv = iv; |
| 650 | fe->tag = tag; |
| 651 | fe->aad = aad; |
| 652 | fe->flags = flags; |
| 653 | f->buffer_indices[index] = buffer_index; |
| 654 | f->next_node_index[index] = next_node; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static_always_inline void |
| 658 | vnet_crypto_async_reset_frame (vnet_crypto_async_frame_t * f) |
| 659 | { |
| 660 | vnet_crypto_async_op_id_t opt; |
| 661 | ASSERT (f != 0); |
PiotrX Kleski | 8ed06c0 | 2020-11-24 07:34:09 +0000 | [diff] [blame] | 662 | ASSERT ((f->state == VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED |
| 663 | || f->state == VNET_CRYPTO_FRAME_STATE_ELT_ERROR)); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 664 | opt = f->op; |
| 665 | if (CLIB_DEBUG > 0) |
| 666 | clib_memset (f, 0xfe, sizeof (*f)); |
| 667 | f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED; |
| 668 | f->op = opt; |
| 669 | f->n_elts = 0; |
| 670 | } |
| 671 | |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 672 | static_always_inline u8 |
| 673 | vnet_crypto_async_frame_is_full (const vnet_crypto_async_frame_t *f) |
| 674 | { |
| 675 | return (f->n_elts == VNET_CRYPTO_FRAME_SIZE); |
| 676 | } |
| 677 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 678 | #endif /* included_vnet_crypto_crypto_h */ |
| 679 | |
| 680 | /* |
| 681 | * fd.io coding-style-patch-verification: ON |
| 682 | * |
| 683 | * Local Variables: |
| 684 | * eval: (c-set-style "gnu") |
| 685 | * End: |
| 686 | */ |