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