FD.io VPP  v19.08.1-401-g8e4ed521a
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  iv_len = 8;
77  else
78  iv_len = 16;
79 
81  RAND_bytes (op->iv, iv_len);
82 
83  EVP_EncryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
84  EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
85  if (out_len < op->len)
86  EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
87  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
88  }
89  return n_ops;
90 }
91 
94  const EVP_CIPHER * cipher)
95 {
96  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
97  vm->thread_index);
98  EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
99  u32 i;
100  for (i = 0; i < n_ops; i++)
101  {
102  vnet_crypto_op_t *op = ops[i];
104  int out_len;
105 
106  EVP_DecryptInit_ex (ctx, cipher, NULL, key->data, op->iv);
107  EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
108  if (out_len < op->len)
109  EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
110  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
111  }
112  return n_ops;
113 }
114 
117  const EVP_CIPHER * cipher)
118 {
119  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
120  vm->thread_index);
121  EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
122  u32 i;
123  for (i = 0; i < n_ops; i++)
124  {
125  vnet_crypto_op_t *op = ops[i];
127  int len;
128 
130  RAND_bytes (op->iv, 8);
131 
132  EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0);
133  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL);
134  EVP_EncryptInit_ex (ctx, 0, 0, key->data, op->iv);
135  if (op->aad_len)
136  EVP_EncryptUpdate (ctx, NULL, &len, op->aad, op->aad_len);
137  EVP_EncryptUpdate (ctx, op->dst, &len, op->src, op->len);
138  EVP_EncryptFinal_ex (ctx, op->dst + len, &len);
139  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_GET_TAG, op->tag_len, op->tag);
140  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
141  }
142  return n_ops;
143 }
144 
147  const EVP_CIPHER * cipher)
148 {
149  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
150  vm->thread_index);
151  EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
152  u32 i, n_fail = 0;
153  for (i = 0; i < n_ops; i++)
154  {
155  vnet_crypto_op_t *op = ops[i];
157  int len;
158 
159  EVP_DecryptInit_ex (ctx, cipher, 0, 0, 0);
160  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0);
161  EVP_DecryptInit_ex (ctx, 0, 0, key->data, op->iv);
162  if (op->aad_len)
163  EVP_DecryptUpdate (ctx, 0, &len, op->aad, op->aad_len);
164  EVP_DecryptUpdate (ctx, op->dst, &len, op->src, op->len);
165  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_TAG, op->tag_len, op->tag);
166 
167  if (EVP_DecryptFinal_ex (ctx, op->dst + len, &len) > 0)
168  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
169  else
170  {
171  n_fail++;
172  op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
173  }
174  }
175  return n_ops - n_fail;
176 }
177 
180  const EVP_MD * md)
181 {
182  u8 buffer[64];
183  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
184  vm->thread_index);
185  HMAC_CTX *ctx = ptd->hmac_ctx;
186  u32 i, n_fail = 0;
187  for (i = 0; i < n_ops; i++)
188  {
189  vnet_crypto_op_t *op = ops[i];
191  unsigned int out_len;
192  size_t sz = op->digest_len ? op->digest_len : EVP_MD_size (md);
193 
194  HMAC_Init_ex (ctx, key->data, vec_len (key->data), md, NULL);
195  HMAC_Update (ctx, op->src, op->len);
196  HMAC_Final (ctx, buffer, &out_len);
197 
199  {
200  if ((memcmp (op->digest, buffer, sz)))
201  {
202  n_fail++;
203  op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
204  continue;
205  }
206  }
207  else
208  clib_memcpy_fast (op->digest, buffer, sz);
209  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
210  }
211  return n_ops - n_fail;
212 }
213 
214 #define _(m, a, b) \
215 static u32 \
216 openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
217 { return openssl_ops_enc_##m (vm, ops, n_ops, b ()); } \
218 \
219 u32 \
220 openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
221 { return openssl_ops_dec_##m (vm, ops, n_ops, b ()); }
222 
224 #undef _
225 
226 #define _(a, b) \
227 static u32 \
228 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
229 { return openssl_ops_hmac (vm, ops, n_ops, b ()); } \
230 
232 #undef _
233 
234 
235 clib_error_t *
237 {
240  u8 *seed_data = 0;
241  time_t t;
242  pid_t pid;
243 
244  u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
245 
246 #define _(m, a, b) \
247  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
248  openssl_ops_enc_##a); \
249  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
250  openssl_ops_dec_##a);
251 
253 #undef _
254 
255 #define _(a, b) \
256  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
257  openssl_ops_hmac_##a); \
258 
260 #undef _
261 
262  vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
264 
265  vec_foreach (ptd, per_thread_data)
266  {
267  ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
268 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
269  ptd->hmac_ctx = HMAC_CTX_new ();
270 #else
271  HMAC_CTX_init (&(ptd->_hmac_ctx));
272  ptd->hmac_ctx = &ptd->_hmac_ctx;
273 #endif
274  }
275 
276  t = time (NULL);
277  pid = getpid ();
278  vec_add (seed_data, &t, sizeof (t));
279  vec_add (seed_data, &pid, sizeof (pid));
280  vec_add (seed_data, seed_data, sizeof (seed_data));
281 
282  RAND_seed ((const void *) seed_data, vec_len (seed_data));
283 
284  vec_free (seed_data);
285 
286  return 0;
287 }
288 
289 /* *INDENT-OFF* */
291 {
292  .runs_after = VLIB_INITS ("vnet_crypto_init"),
293 };
294 /* *INDENT-ON* */
295 
296 
297 /* *INDENT-OFF* */
299  .version = VPP_BUILD_VER,
300  .description = "OpenSSL Crypto Engine",
301 };
302 /* *INDENT-ON* */
303 
304 /*
305  * fd.io coding-style-patch-verification: ON
306  *
307  * Local Variables:
308  * eval: (c-set-style "gnu")
309  * End:
310  */
#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:93
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:450
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:598
#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:236
#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:90
#define foreach_openssl_hmac_op
Definition: main.c:52
#define VNET_CRYPTO_OP_FLAG_INIT_IV
Definition: crypto.h:131
vlib_main_t * vm
Definition: buffer.c:323
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
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:146
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:179
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:116
typedef key
Definition: ipsec.api:247
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