tls: fix picotls engine crypto multi-thread issue
[vpp.git] / src / plugins / tlspicotls / pico_vpp_crypto.c
1 /*
2  * Copyright (c) 2020 Intel and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/crypto/crypto.h>
17 #include <vnet/tls/tls.h>
18 #include <picotls/openssl.h>
19 #include <picotls.h>
20
21 #include <tlspicotls/pico_vpp_crypto.h>
22 #include <tlspicotls/tls_picotls.h>
23
24 typedef void (*ptls_vpp_do_transform_fn) (ptls_cipher_context_t *, void *,
25                                           const void *, size_t);
26
27 vnet_crypto_main_t *cm = &crypto_main;
28 extern picotls_main_t picotls_main;
29
30 struct cipher_context_t
31 {
32   ptls_cipher_context_t super;
33   vnet_crypto_op_t op;
34   u32 key_index;
35 };
36
37 struct vpp_aead_context_t
38 {
39   ptls_aead_context_t super;
40   vnet_crypto_op_t op;
41   vnet_crypto_op_chunk_t chunks[2];
42   vnet_crypto_alg_t alg;
43   u32 key_index;
44   u32 chunk_index;
45 };
46
47 static void
48 ptls_vpp_crypto_cipher_do_init (ptls_cipher_context_t * _ctx, const void *iv)
49 {
50   struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
51
52   vnet_crypto_op_id_t id;
53   if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
54     {
55       id = VNET_CRYPTO_OP_AES_128_CTR_ENC;
56     }
57   else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
58     {
59       id = VNET_CRYPTO_OP_AES_256_CTR_ENC;
60     }
61   else
62     {
63       TLS_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
64                _ctx->algo->name);
65       assert (0);
66     }
67
68   vnet_crypto_op_init (&ctx->op, id);
69   ctx->op.iv = (u8 *) iv;
70   ctx->op.key_index = ctx->key_index;
71 }
72
73 static void
74 ptls_vpp_crypto_cipher_dispose (ptls_cipher_context_t * _ctx)
75 {
76   /* Do nothing */
77 }
78
79 static void
80 ptls_vpp_crypto_cipher_encrypt (ptls_cipher_context_t * _ctx, void *output,
81                                 const void *input, size_t _len)
82 {
83   vlib_main_t *vm = vlib_get_main ();
84   struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
85
86   ctx->op.src = (u8 *) input;
87   ctx->op.dst = output;
88   ctx->op.len = _len;
89
90   vnet_crypto_process_ops (vm, &ctx->op, 1);
91 }
92
93 static int
94 ptls_vpp_crypto_cipher_setup_crypto (ptls_cipher_context_t * _ctx, int is_enc,
95                                      const void *key,
96                                      const EVP_CIPHER * cipher,
97                                      ptls_vpp_do_transform_fn do_transform)
98 {
99   struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
100
101   ctx->super.do_dispose = ptls_vpp_crypto_cipher_dispose;
102   ctx->super.do_init = ptls_vpp_crypto_cipher_do_init;
103   ctx->super.do_transform = do_transform;
104
105   vlib_main_t *vm = vlib_get_main ();
106   vnet_crypto_alg_t algo;
107   if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
108     {
109       algo = VNET_CRYPTO_ALG_AES_128_CTR;
110     }
111   else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
112     {
113       algo = VNET_CRYPTO_ALG_AES_256_CTR;
114     }
115   else
116     {
117       TLS_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
118                _ctx->algo->name);
119       assert (0);
120     }
121
122   clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
123   ctx->key_index = vnet_crypto_key_add (vm, algo,
124                                         (u8 *) key, _ctx->algo->key_size);
125   clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
126
127   return 0;
128 }
129
130 size_t
131 ptls_vpp_crypto_aead_decrypt (ptls_aead_context_t * _ctx, void *_output,
132                               const void *input, size_t inlen, const void *iv,
133                               const void *aad, size_t aadlen)
134 {
135   vlib_main_t *vm = vlib_get_main ();
136   struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
137   int tag_size = ctx->super.algo->tag_size;
138
139   ctx->op.dst = _output;
140   ctx->op.src = (void *) input;
141   ctx->op.len = inlen - tag_size;;
142   ctx->op.iv = (void *) iv;
143   ctx->op.aad = (void *) aad;
144   ctx->op.aad_len = aadlen;
145   ctx->op.tag = (void *) input + inlen - tag_size;
146   ctx->op.tag_len = tag_size;
147
148   vnet_crypto_process_ops (vm, &(ctx->op), 1);
149   assert (ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
150
151   return inlen - tag_size;
152 }
153
154 static void
155 ptls_vpp_crypto_aead_encrypt_init (ptls_aead_context_t * _ctx, const void *iv,
156                                    const void *aad, size_t aadlen)
157 {
158   struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
159   ctx->op.iv = (void *) iv;
160   ctx->op.aad = (void *) aad;
161   ctx->op.aad_len = aadlen;
162   ctx->op.n_chunks = 2;
163   ctx->op.chunk_index = 0;
164
165   ctx->op.flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
166 }
167
168 static size_t
169 ptls_vpp_crypto_aead_encrypt_update (ptls_aead_context_t * _ctx, void *output,
170                                      const void *input, size_t inlen)
171 {
172   struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
173   ctx->chunks[ctx->chunk_index].dst = output;
174   ctx->chunks[ctx->chunk_index].src = (void *) input;
175   ctx->chunks[ctx->chunk_index].len = inlen;
176
177   ctx->chunk_index = ctx->chunk_index == 0 ? 1 : 0;
178
179   return inlen;
180 }
181
182 static size_t
183 ptls_vpp_crypto_aead_encrypt_final (ptls_aead_context_t * _ctx, void *_output)
184 {
185   struct vlib_main_t *vm = vlib_get_main ();
186   struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
187
188   ctx->op.tag = _output;
189   ctx->op.tag_len = ctx->super.algo->tag_size;
190
191   vnet_crypto_process_chained_ops (vm, &(ctx->op), ctx->chunks, 1);
192   assert (ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
193
194   return ctx->super.algo->tag_size;
195 }
196
197 static void
198 ptls_vpp_crypto_aead_dispose_crypto (ptls_aead_context_t * _ctx)
199 {
200   /* Do nothing */
201 }
202
203
204 static int
205 ptls_vpp_crypto_aead_setup_crypto (ptls_aead_context_t * _ctx, int is_enc,
206                                    const void *key, vnet_crypto_alg_t alg)
207 {
208   struct vlib_main_t *vm = vlib_get_main ();
209   struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
210   u16 key_len = ctx->super.algo->key_size;
211
212   memset (&(ctx->op), 0, sizeof (vnet_crypto_op_t));
213
214   if (alg == VNET_CRYPTO_ALG_AES_128_GCM)
215     {
216       if (is_enc)
217         vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_128_GCM_ENC);
218       else
219         vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_128_GCM_DEC);
220     }
221   else if (alg == VNET_CRYPTO_ALG_AES_256_GCM)
222     {
223       if (is_enc)
224         {
225           vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_256_GCM_ENC);
226         }
227       else
228         vnet_crypto_op_init (&(ctx->op), VNET_CRYPTO_OP_AES_256_GCM_DEC);
229     }
230   else
231     {
232       TLS_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
233                _ctx->algo->name);
234       return -1;
235     }
236
237   ctx->alg = alg;
238
239   clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
240   ctx->op.key_index =
241     vnet_crypto_key_add (vm, ctx->alg, (void *) key, key_len);
242   clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
243   ctx->chunk_index = 0;
244
245   ctx->super.do_decrypt = ptls_vpp_crypto_aead_decrypt;
246   ctx->super.do_encrypt_init = ptls_vpp_crypto_aead_encrypt_init;
247   ctx->super.do_encrypt_update = ptls_vpp_crypto_aead_encrypt_update;
248   ctx->super.do_encrypt_final = ptls_vpp_crypto_aead_encrypt_final;
249   ctx->super.dispose_crypto = ptls_vpp_crypto_aead_dispose_crypto;
250
251   return 0;
252 }
253
254 static int
255 ptls_vpp_crypto_aes128ctr_setup_crypto (ptls_cipher_context_t * ctx,
256                                         int is_enc, const void *key)
257 {
258   return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
259                                               ptls_vpp_crypto_cipher_encrypt);
260 }
261
262 static int
263 ptls_vpp_crypto_aes256ctr_setup_crypto (ptls_cipher_context_t * ctx,
264                                         int is_enc, const void *key)
265 {
266   return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
267                                               ptls_vpp_crypto_cipher_encrypt);
268 }
269
270 static int
271 ptls_vpp_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t * ctx,
272                                              int is_enc, const void *key)
273 {
274   return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key,
275                                             VNET_CRYPTO_ALG_AES_128_GCM);
276 }
277
278 static int
279 ptls_vpp_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t * ctx,
280                                              int is_enc, const void *key)
281 {
282   return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key,
283                                             VNET_CRYPTO_ALG_AES_256_GCM);
284 }
285
286 ptls_cipher_algorithm_t ptls_vpp_crypto_aes128ctr = { "AES128-CTR",
287   PTLS_AES128_KEY_SIZE,
288   1, PTLS_AES_IV_SIZE,
289   sizeof (struct vpp_aead_context_t),
290   ptls_vpp_crypto_aes128ctr_setup_crypto
291 };
292
293 ptls_cipher_algorithm_t ptls_vpp_crypto_aes256ctr = { "AES256-CTR",
294   PTLS_AES256_KEY_SIZE,
295   1 /* block size */ ,
296   PTLS_AES_IV_SIZE,
297   sizeof (struct vpp_aead_context_t),
298   ptls_vpp_crypto_aes256ctr_setup_crypto
299 };
300
301 ptls_aead_algorithm_t ptls_vpp_crypto_aes128gcm = { "AES128-GCM",
302   &ptls_vpp_crypto_aes128ctr,
303   NULL,
304   PTLS_AES128_KEY_SIZE,
305   PTLS_AESGCM_IV_SIZE,
306   PTLS_AESGCM_TAG_SIZE,
307   sizeof (struct vpp_aead_context_t),
308   ptls_vpp_crypto_aead_aes128gcm_setup_crypto
309 };
310
311 ptls_aead_algorithm_t ptls_vpp_crypto_aes256gcm = { "AES256-GCM",
312   &ptls_vpp_crypto_aes256ctr,
313   NULL,
314   PTLS_AES256_KEY_SIZE,
315   PTLS_AESGCM_IV_SIZE,
316   PTLS_AESGCM_TAG_SIZE,
317   sizeof (struct vpp_aead_context_t),
318   ptls_vpp_crypto_aead_aes256gcm_setup_crypto
319 };
320
321 ptls_cipher_suite_t ptls_vpp_crypto_aes128gcmsha256 =
322   { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
323   &ptls_vpp_crypto_aes128gcm,
324   &ptls_openssl_sha256
325 };
326
327 ptls_cipher_suite_t ptls_vpp_crypto_aes256gcmsha384 =
328   { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
329   &ptls_vpp_crypto_aes256gcm,
330   &ptls_openssl_sha384
331 };
332
333 ptls_cipher_suite_t *ptls_vpp_crypto_cipher_suites[] =
334   { &ptls_vpp_crypto_aes256gcmsha384,
335   &ptls_vpp_crypto_aes128gcmsha256,
336   NULL
337 };
338
339 /*
340  * fd.io coding-style-patch-verification: ON
341  *
342  * Local Variables:
343  * eval: (c-set-style "gnu")
344  * End:
345  */