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