FD.io VPP  v19.04-6-g6f05f72
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 
49 #define foreach_openssl_hmac_op \
50  _(MD5, EVP_md5) \
51  _(SHA1, EVP_sha1) \
52  _(SHA224, EVP_sha224) \
53  _(SHA256, EVP_sha256) \
54  _(SHA384, EVP_sha384) \
55  _(SHA512, EVP_sha512)
56 
59  const EVP_CIPHER * cipher)
60 {
61  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
62  vm->thread_index);
63  EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
64  u32 i;
65  for (i = 0; i < n_ops; i++)
66  {
67  vnet_crypto_op_t *op = ops[i];
68  int out_len;
69 
71  RAND_bytes (op->iv, 16);
72 
73  EVP_EncryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
74  EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
75  if (out_len < op->len)
76  EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
77  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
78  }
79  return n_ops;
80 }
81 
84  const EVP_CIPHER * cipher)
85 {
86  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
87  vm->thread_index);
88  EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
89  u32 i;
90  for (i = 0; i < n_ops; i++)
91  {
92  vnet_crypto_op_t *op = ops[i];
93  int out_len;
94 
95  EVP_DecryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
96  EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
97  if (out_len < op->len)
98  EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
99  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
100  }
101  return n_ops;
102 }
103 
106  const EVP_CIPHER * cipher)
107 {
108  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
109  vm->thread_index);
110  EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
111  u32 i;
112  for (i = 0; i < n_ops; i++)
113  {
114  vnet_crypto_op_t *op = ops[i];
115  u32 nonce[3];
116  int len;
117 
119  RAND_bytes (op->iv, 8);
120 
121  nonce[0] = op->salt;
122  clib_memcpy_fast (nonce + 1, op->iv, 8);
123 
124  EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0);
125  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL);
126  EVP_EncryptInit_ex (ctx, 0, 0, op->key, (u8 *) nonce);
127  if (op->aad_len)
128  EVP_EncryptUpdate (ctx, NULL, &len, op->aad, op->aad_len);
129  EVP_EncryptUpdate (ctx, op->dst, &len, op->src, op->len);
130  EVP_EncryptFinal_ex (ctx, op->dst + len, &len);
131  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_GET_TAG, op->tag_len, op->tag);
132  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
133  }
134  return n_ops;
135 }
136 
139  const EVP_CIPHER * cipher)
140 {
141  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
142  vm->thread_index);
143  EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
144  u32 i, n_fail = 0;
145  for (i = 0; i < n_ops; i++)
146  {
147  vnet_crypto_op_t *op = ops[i];
148  int len;
149 
150  EVP_DecryptInit_ex (ctx, cipher, 0, 0, 0);
151  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, op->iv_len, 0);
152  EVP_DecryptInit_ex (ctx, 0, 0, op->key, op->iv);
153  if (op->aad_len)
154  EVP_DecryptUpdate (ctx, 0, &len, op->aad, op->aad_len);
155  EVP_DecryptUpdate (ctx, op->dst, &len, op->src, op->len);
156  EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_TAG, op->tag_len, op->tag);
157 
158  if (EVP_DecryptFinal_ex (ctx, op->dst + len, &len) > 0)
159  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
160  else
161  {
162  n_fail++;
163  op->status = VNET_CRYPTO_OP_STATUS_FAIL_DECRYPT;
164  }
165  }
166  return n_ops - n_fail;
167 }
168 
171  const EVP_MD * md)
172 {
173  u8 buffer[64];
174  openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
175  vm->thread_index);
176  HMAC_CTX *ctx = ptd->hmac_ctx;
177  u32 i, n_fail = 0;
178  for (i = 0; i < n_ops; i++)
179  {
180  vnet_crypto_op_t *op = ops[i];
181  unsigned int out_len;
182  size_t sz = op->digest_len ? op->digest_len : EVP_MD_size (md);
183 
184  HMAC_Init_ex (ctx, op->key, op->key_len, md, NULL);
185  HMAC_Update (ctx, op->src, op->len);
186  HMAC_Final (ctx, buffer, &out_len);
187 
189  {
190  if ((memcmp (op->digest, buffer, sz)))
191  {
192  n_fail++;
193  op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
194  continue;
195  }
196  }
197  else
198  clib_memcpy_fast (op->digest, buffer, sz);
199  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
200  }
201  return n_ops - n_fail;
202 }
203 
204 #define _(m, a, b) \
205 static u32 \
206 openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
207 { return openssl_ops_enc_##m (vm, ops, n_ops, b ()); } \
208 \
209 u32 \
210 openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
211 { return openssl_ops_dec_##m (vm, ops, n_ops, b ()); }
212 
214 #undef _
215 
216 #define _(a, b) \
217 static u32 \
218 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
219 { return openssl_ops_hmac (vm, ops, n_ops, b ()); } \
220 
222 #undef _
223 
224 
225 clib_error_t *
227 {
230  u8 *seed_data = 0;
231  clib_error_t *error;
232  time_t t;
233  pid_t pid;
234 
235  if ((error = vlib_call_init_function (vm, vnet_crypto_init)))
236  return error;
237 
238  u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
239 
240 #define _(m, a, b) \
241  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
242  openssl_ops_enc_##a); \
243  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
244  openssl_ops_dec_##a);
245 
247 #undef _
248 
249 #define _(a, b) \
250  vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
251  openssl_ops_hmac_##a); \
252 
254 #undef _
255 
256  vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
258 
259  vec_foreach (ptd, per_thread_data)
260  {
261  ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
262 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
263  ptd->hmac_ctx = HMAC_CTX_new ();
264 #else
265  HMAC_CTX_init (&(ptd->_hmac_ctx));
266  ptd->hmac_ctx = &ptd->_hmac_ctx;
267 #endif
268  }
269 
270  t = time (NULL);
271  pid = getpid ();
272  vec_add (seed_data, &t, sizeof (t));
273  vec_add (seed_data, &pid, sizeof (pid));
274  vec_add (seed_data, seed_data, sizeof (seed_data));
275 
276  RAND_seed ((const void *) seed_data, vec_len (seed_data));
277 
278  vec_free (seed_data);
279 
280  return 0;
281 }
282 
284 
285 /* *INDENT-OFF* */
287  .version = VPP_BUILD_VER,
288  .description = "OpenSSL Crypto Engine Plugin",
289 };
290 /* *INDENT-ON* */
291 
292 /*
293  * fd.io coding-style-patch-verification: ON
294  *
295  * Local Variables:
296  * eval: (c-set-style "gnu")
297  * End:
298  */
#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:58
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
#define NULL
Definition: clib.h:58
u32 thread_index
Definition: main.h:197
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:83
#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:163
#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
#define vlib_call_init_function(vm, x)
Definition: init.h:260
long ctx[MAX_CONNS]
Definition: main.c:144
clib_error_t * crypto_openssl_init(vlib_main_t *vm)
Definition: main.c:226
#define VNET_CRYPTO_OP_FLAG_HMAC_CHECK
Definition: crypto.h:114
EVP_CIPHER_CTX * evp_cipher_ctx
Definition: main.c:30
u8 len
Definition: ip_types.api:49
#define foreach_openssl_hmac_op
Definition: main.c:49
#define VNET_CRYPTO_OP_FLAG_INIT_IV
Definition: crypto.h:113
vlib_main_t * vm
Definition: buffer.c:312
#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:138
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:170
#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:105
vnet_crypto_op_status_t status
Definition: crypto.h:111
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
clib_error_t * vnet_crypto_init(vlib_main_t *vm)
Definition: crypto.c:201