FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
main.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2019 Cisco and/or its affiliates.
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  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 
18 #include <openssl/evp.h>
19 #include <openssl/hmac.h>
20 #include <openssl/rand.h>
21 
22 #include <vlib/vlib.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vnet/crypto/crypto.h>
25 #include <vpp/app/version.h>
26 
27 typedef struct
28 {
29  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
30  EVP_CIPHER_CTX *evp_cipher_ctx;
31  HMAC_CTX *hmac_ctx;
32 #if OPENSSL_VERSION_NUMBER < 0x10100000L
33  HMAC_CTX _hmac_ctx;
34 #endif
36 
37 static openssl_per_thread_data_t *per_thread_data = 0;
38 
39 #define foreach_openssl_evp_op \
40  _(cbc, DES_CBC, EVP_des_cbc) \
41  _(cbc, 3DES_CBC, EVP_des_ede3_cbc) \
42  _(cbc, AES_128_CBC, EVP_aes_128_cbc) \
43  _(cbc, AES_192_CBC, EVP_aes_192_cbc) \
44  _(cbc, AES_256_CBC, EVP_aes_256_cbc) \
45  _(gcm, AES_128_GCM, EVP_aes_128_gcm) \
46  _(gcm, AES_192_GCM, EVP_aes_192_gcm) \
47  _(gcm, AES_256_GCM, EVP_aes_256_gcm) \
48  _(cbc, AES_128_CTR, EVP_aes_128_ctr) \
49  _(cbc, AES_192_CTR, EVP_aes_192_ctr) \
50  _(cbc, AES_256_CTR, EVP_aes_256_ctr) \
51 
52 #define foreach_openssl_hmac_op \
53  _(MD5, EVP_md5) \
54  _(SHA1, EVP_sha1) \
55  _(SHA224, EVP_sha224) \
56  _(SHA256, EVP_sha256) \
57  _(SHA384, EVP_sha384) \
58  _(SHA512, EVP_sha512)
59 
62  const EVP_CIPHER * cipher)
63 {
64  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
65  vm->thread_index);
66  EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
67  u32 i;
68  for (i = 0; i < n_ops; i++)
69  {
70  vnet_crypto_op_t *op = ops[i];
72  int out_len;
73  int iv_len;
74 
75  if (op->op == VNET_CRYPTO_OP_3DES_CBC_ENC
76  || op->op == VNET_CRYPTO_OP_DES_CBC_ENC)
77  iv_len = 8;
78  else
79  iv_len = 16;
80 
82  RAND_bytes (op->iv, iv_len);
83 
84  EVP_EncryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
85  EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
86  if (out_len < op->len)
87  EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
88  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
89  }
90  return n_ops;
91 }
92 
95  const EVP_CIPHER * cipher)
96 {
97  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
98  vm->thread_index);
99  EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
100  u32 i;
101  for (i = 0; i < n_ops; i++)
102  {
103  vnet_crypto_op_t *op = ops[i];
105  int out_len;
106 
107  EVP_DecryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
108  EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
109  if (out_len < op->len)
110  EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
111  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
112  }
113  return n_ops;
114 }
115 
118  const EVP_CIPHER * cipher)
119 {
120  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
121  vm->thread_index);
122  EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
123  u32 i;
124  for (i = 0; i < n_ops; i++)
125  {
126  vnet_crypto_op_t *op = ops[i];
128  int len;
129 
131  RAND_bytes (op->iv, 8);
132 
133  EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0);
134  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL);
135  EVP_EncryptInit_ex (ctx, 0, 0, key->data, op->iv);
136  if (op->aad_len)
137  EVP_EncryptUpdate (ctx, NULL, &len, op->aad, op->aad_len);
138  EVP_EncryptUpdate (ctx, op->dst, &len, op->src, op->len);
139  EVP_EncryptFinal_ex (ctx, op->dst + len, &len);
140  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_GET_TAG, op->tag_len, op->tag);
141  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
142  }
143  return n_ops;
144 }
145 
148  const EVP_CIPHER * cipher)
149 {
150  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
151  vm->thread_index);
152  EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
153  u32 i, n_fail = 0;
154  for (i = 0; i < n_ops; i++)
155  {
156  vnet_crypto_op_t *op = ops[i];
158  int len;
159 
160  EVP_DecryptInit_ex (ctx, cipher, 0, 0, 0);
161  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0);
162  EVP_DecryptInit_ex (ctx, 0, 0, key->data, op->iv);
163  if (op->aad_len)
164  EVP_DecryptUpdate (ctx, 0, &len, op->aad, op->aad_len);
165  EVP_DecryptUpdate (ctx, op->dst, &len, op->src, op->len);
166  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_TAG, op->tag_len, op->tag);
167 
168  if (EVP_DecryptFinal_ex (ctx, op->dst + len, &len) > 0)
169  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
170  else
171  {
172  n_fail++;
173  op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
174  }
175  }
176  return n_ops - n_fail;
177 }
178 
181  const EVP_MD * md)
182 {
183  u8 buffer[64];
184  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
185  vm->thread_index);
186  HMAC_CTX *ctx = ptd->hmac_ctx;
187  u32 i, n_fail = 0;
188  for (i = 0; i < n_ops; i++)
189  {
190  vnet_crypto_op_t *op = ops[i];
192  unsigned int out_len;
193  size_t sz = op->digest_len ? op->digest_len : EVP_MD_size (md);
194 
195  HMAC_Init_ex (ctx, key->data, vec_len (key->data), md, NULL);
196  HMAC_Update (ctx, op->src, op->len);
197  HMAC_Final (ctx, buffer, &out_len);
198 
200  {
201  if ((memcmp (op->digest, buffer, sz)))
202  {
203  n_fail++;
204  op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
205  continue;
206  }
207  }
208  else
209  clib_memcpy_fast (op->digest, buffer, sz);
210  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
211  }
212  return n_ops - n_fail;
213 }
214 
215 #define _(m, a, b) \
216 static u32 \
217 openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
218 { return openssl_ops_enc_##m (vm, ops, n_ops, b ()); } \
219 \
220 u32 \
221 openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
222 { return openssl_ops_dec_##m (vm, ops, n_ops, b ()); }
223 
225 #undef _
226 
227 #define _(a, b) \
228 static u32 \
229 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
230 { return openssl_ops_hmac (vm, ops, n_ops, b ()); } \
231 
233 #undef _
234 
235 
236 clib_error_t *
238 {
241  u8 *seed_data = 0;
242  time_t t;
243  pid_t pid;
244 
245  u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
246 
247 #define _(m, a, b) \
248  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
249  openssl_ops_enc_##a); \
250  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
251  openssl_ops_dec_##a);
252 
254 #undef _
255 
256 #define _(a, b) \
257  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
258  openssl_ops_hmac_##a); \
259 
261 #undef _
262 
263  vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
265 
266  vec_foreach (ptd, per_thread_data)
267  {
268  ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
269 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
270  ptd->hmac_ctx = HMAC_CTX_new ();
271 #else
272  HMAC_CTX_init (&(ptd->_hmac_ctx));
273  ptd->hmac_ctx = &ptd->_hmac_ctx;
274 #endif
275  }
276 
277  t = time (NULL);
278  pid = getpid ();
279  vec_add (seed_data, &t, sizeof (t));
280  vec_add (seed_data, &pid, sizeof (pid));
281  vec_add (seed_data, seed_data, sizeof (seed_data));
282 
283  RAND_seed ((const void *) seed_data, vec_len (seed_data));
284 
285  vec_free (seed_data);
286 
287  return 0;
288 }
289 
290 /* *INDENT-OFF* */
292 {
293  .runs_after = VLIB_INITS ("vnet_crypto_init"),
294 };
295 /* *INDENT-ON* */
296 
297 
298 /* *INDENT-OFF* */
300  .version = VPP_BUILD_VER,
301  .description = "OpenSSL Crypto Engine",
302 };
303 /* *INDENT-ON* */
304 
305 /*
306  * fd.io coding-style-patch-verification: ON
307  *
308  * Local Variables:
309  * eval: (c-set-style "gnu")
310  * End:
311  */
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
static_always_inline u32 openssl_ops_enc_cbc(vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops, const EVP_CIPHER *cipher)
Definition: main.c:61
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
#define NULL
Definition: clib.h:58
u32 thread_index
Definition: main.h:218
int i
VLIB_PLUGIN_REGISTER()
static_always_inline u32 openssl_ops_dec_cbc(vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops, const EVP_CIPHER *cipher)
Definition: main.c:94
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:451
HMAC_CTX * hmac_ctx
Definition: main.c:31
unsigned char u8
Definition: types.h:56
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:599
#define static_always_inline
Definition: clib.h:99
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
unsigned int u32
Definition: types.h:88
vnet_crypto_op_id_t op
Definition: crypto.h:128
long ctx[MAX_CONNS]
Definition: main.c:144
clib_error_t * crypto_openssl_init(vlib_main_t *vm)
Definition: main.c:237
vlib_main_t * vm
Definition: in2out_ed.c:1810
#define VNET_CRYPTO_OP_FLAG_HMAC_CHECK
Definition: crypto.h:132
EVP_CIPHER_CTX * evp_cipher_ctx
Definition: main.c:30
u8 len
Definition: ip_types.api:91
#define foreach_openssl_hmac_op
Definition: main.c:52
#define VNET_CRYPTO_OP_FLAG_INIT_IV
Definition: crypto.h:131
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
static_always_inline u32 openssl_ops_dec_gcm(vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops, const EVP_CIPHER *cipher)
Definition: main.c:147
static_always_inline u32 openssl_ops_hmac(vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops, const EVP_MD *md)
Definition: main.c:180
typedef key
Definition: ipsec_types.api:83
static_always_inline vnet_crypto_key_t * vnet_crypto_get_key(vnet_crypto_key_index_t index)
Definition: crypto.h:240
#define foreach_openssl_evp_op
Definition: main.c:39
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static_always_inline u32 openssl_ops_enc_gcm(vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops, const EVP_CIPHER *cipher)
Definition: main.c:117
u32 pid
Definition: dhcp.api:164
vnet_crypto_op_status_t status
Definition: crypto.h:129
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
#define vec_foreach(var, vec)
Vector iterator.
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
u32 vnet_crypto_register_engine(vlib_main_t *vm, char *name, int prio, char *desc)
Definition: crypto.c:78
#define VLIB_INITS(...)
Definition: init.h:344