crypto: pass multiple ops to handler
[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   _(DES_CBC, EVP_des_cbc) \
41   _(3DES_CBC, EVP_des_ede3_cbc) \
42   _(AES_128_CBC, EVP_aes_128_cbc) \
43   _(AES_192_CBC, EVP_aes_192_cbc) \
44   _(AES_256_CBC, EVP_aes_256_cbc)
45
46 #define foreach_openssl_hmac_op \
47   _(MD5, EVP_md5) \
48   _(SHA1, EVP_sha1) \
49   _(SHA224, EVP_sha224) \
50   _(SHA256, EVP_sha256) \
51   _(SHA384, EVP_sha384) \
52   _(SHA512, EVP_sha512)
53
54 static_always_inline u32
55 openssl_ops_enc_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
56                      const EVP_CIPHER * cipher)
57 {
58   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
59                                                      vm->thread_index);
60   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
61   u32 i;
62   for (i = 0; i < n_ops; i++)
63     {
64       vnet_crypto_op_t *op = ops[i];
65       int out_len;
66
67       if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
68         RAND_bytes (op->iv, 16);
69
70       EVP_EncryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
71       EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
72       if (out_len < op->len)
73         EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
74       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
75     }
76   return n_ops;
77 }
78
79 static_always_inline u32
80 openssl_ops_dec_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
81                      const EVP_CIPHER * cipher)
82 {
83   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
84                                                      vm->thread_index);
85   EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
86   u32 i;
87   for (i = 0; i < n_ops; i++)
88     {
89       vnet_crypto_op_t *op = ops[i];
90       int out_len;
91
92       EVP_DecryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
93       EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
94       if (out_len < op->len)
95         EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
96       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
97     }
98   return n_ops;
99 }
100
101 static_always_inline u32
102 openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
103                   const EVP_MD * md)
104 {
105   u8 buffer[64];
106   openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
107                                                      vm->thread_index);
108   HMAC_CTX *ctx = ptd->hmac_ctx;
109   u32 i, n_fail = 0;
110   for (i = 0; i < n_ops; i++)
111     {
112       vnet_crypto_op_t *op = ops[i];
113       unsigned int out_len;
114       size_t sz = op->hmac_trunc_len ? op->hmac_trunc_len : EVP_MD_size (md);
115
116       HMAC_Init_ex (ctx, op->key, op->key_len, md, NULL);
117       HMAC_Update (ctx, op->src, op->len);
118       HMAC_Final (ctx, buffer, &out_len);
119
120       if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
121         {
122           if ((memcmp (op->dst, buffer, sz)))
123             {
124               n_fail++;
125               op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
126               continue;
127             }
128         }
129       else
130         clib_memcpy_fast (op->dst, buffer, sz);
131       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
132     }
133   return n_ops - n_fail;
134 }
135
136 #define _(a, b) \
137 static u32 \
138 openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
139 { return openssl_ops_enc_cbc (vm, ops, n_ops, b ()); } \
140 \
141 u32 \
142 openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
143 { return openssl_ops_dec_cbc (vm, ops, n_ops, b ()); }
144
145 foreach_openssl_evp_op;
146 #undef _
147
148 #define _(a, b) \
149 static u32 \
150 openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
151 { return openssl_ops_hmac (vm, ops, n_ops, b ()); } \
152
153 foreach_openssl_hmac_op;
154 #undef _
155
156
157 clib_error_t *
158 crypto_openssl_init (vlib_main_t * vm)
159 {
160   vlib_thread_main_t *tm = vlib_get_thread_main ();
161   openssl_per_thread_data_t *ptd;
162   u8 *seed_data = 0;
163   time_t t;
164   pid_t pid;
165
166   u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
167   clib_error_t *error;
168
169   if ((error = vlib_call_init_function (vm, vnet_crypto_init)))
170     return error;
171
172 #define _(a, b) \
173   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
174                                     openssl_ops_enc_##a); \
175   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
176                                     openssl_ops_dec_##a);
177
178   foreach_openssl_evp_op;
179 #undef _
180
181 #define _(a, b) \
182   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
183                                     openssl_ops_hmac_##a); \
184
185   foreach_openssl_hmac_op;
186 #undef _
187
188   vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
189                         CLIB_CACHE_LINE_BYTES);
190
191   vec_foreach (ptd, per_thread_data)
192   {
193     ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
194 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
195     ptd->hmac_ctx = HMAC_CTX_new ();
196 #else
197     HMAC_CTX_init (&(ptd->_hmac_ctx));
198     ptd->hmac_ctx = &ptd->_hmac_ctx;
199 #endif
200   }
201
202   t = time (NULL);
203   pid = getpid ();
204   vec_add (seed_data, &t, sizeof (t));
205   vec_add (seed_data, &pid, sizeof (pid));
206   vec_add (seed_data, seed_data, sizeof (seed_data));
207
208   RAND_seed ((const void *) seed_data, vec_len (seed_data));
209
210   vec_free (seed_data);
211
212   return 0;
213 }
214
215 VLIB_INIT_FUNCTION (crypto_openssl_init);
216
217 /* *INDENT-OFF* */
218 VLIB_PLUGIN_REGISTER () = {
219   .version = VPP_BUILD_VER,
220   .description = "OpenSSL Crypto Engine Plugin",
221 };
222 /* *INDENT-ON* */
223
224 /*
225  * fd.io coding-style-patch-verification: ON
226  *
227  * Local Variables:
228  * eval: (c-set-style "gnu")
229  * End:
230  */