tls: memory leak due to missing call to vnet_crypto_key_del
[vpp.git] / src / plugins / tlspicotls / pico_vpp_crypto.c
1 /*
2  * Copyright (c) 2021 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   vnet_crypto_op_id_t id;
35   u32 key_index;
36 };
37
38 struct vpp_aead_context_t
39 {
40   ptls_aead_context_t super;
41   EVP_CIPHER_CTX *evp_ctx;
42   uint8_t static_iv[PTLS_MAX_IV_SIZE];
43   vnet_crypto_op_t op;
44   u32 key_index;
45   vnet_crypto_op_id_t id;
46   vnet_crypto_op_chunk_t chunks[2];
47   vnet_crypto_alg_t alg;
48   u32 chunk_index;
49   uint8_t iv[PTLS_MAX_IV_SIZE];
50 };
51
52 static void
53 ptls_vpp_crypto_cipher_do_init (ptls_cipher_context_t * _ctx, const void *iv)
54 {
55   struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
56
57   vnet_crypto_op_init (&ctx->op, ctx->id);
58   ctx->op.iv = (u8 *) iv;
59   ctx->op.key_index = ctx->key_index;
60 }
61
62 static void
63 ptls_vpp_crypto_cipher_dispose (ptls_cipher_context_t * _ctx)
64 {
65   /* Do nothing */
66 }
67
68 static void
69 ptls_vpp_crypto_cipher_encrypt (ptls_cipher_context_t * _ctx, void *output,
70                                 const void *input, size_t _len)
71 {
72   vlib_main_t *vm = vlib_get_main ();
73   struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
74
75   ctx->op.src = (u8 *) input;
76   ctx->op.dst = output;
77   ctx->op.len = _len;
78
79   vnet_crypto_process_ops (vm, &ctx->op, 1);
80 }
81
82 static int
83 ptls_vpp_crypto_cipher_setup_crypto (ptls_cipher_context_t * _ctx, int is_enc,
84                                      const void *key,
85                                      const EVP_CIPHER * cipher,
86                                      ptls_vpp_do_transform_fn do_transform)
87 {
88   struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
89
90   ctx->super.do_dispose = ptls_vpp_crypto_cipher_dispose;
91   ctx->super.do_init = ptls_vpp_crypto_cipher_do_init;
92   ctx->super.do_transform = do_transform;
93
94   vlib_main_t *vm = vlib_get_main ();
95   vnet_crypto_alg_t algo;
96   if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
97     {
98       algo = VNET_CRYPTO_ALG_AES_128_CTR;
99       ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_CTR_ENC :
100                          VNET_CRYPTO_OP_AES_128_CTR_DEC;
101     }
102   else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
103     {
104       algo = VNET_CRYPTO_ALG_AES_256_CTR;
105       ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_CTR_ENC :
106                          VNET_CRYPTO_OP_AES_256_CTR_DEC;
107     }
108   else
109     {
110       TLS_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
111                _ctx->algo->name);
112       assert (0);
113     }
114
115   clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
116   ctx->key_index = vnet_crypto_key_add (vm, algo,
117                                         (u8 *) key, _ctx->algo->key_size);
118   clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
119
120   return 0;
121 }
122
123 size_t
124 ptls_vpp_crypto_aead_decrypt (ptls_aead_context_t *_ctx, void *_output,
125                               const void *input, size_t inlen, uint64_t seq,
126                               const void *aad, size_t aadlen)
127 {
128   vlib_main_t *vm = vlib_get_main ();
129   struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
130   int tag_size = ctx->super.algo->tag_size;
131
132   vnet_crypto_op_init (&ctx->op, ctx->id);
133   ctx->op.aad = (u8 *) aad;
134   ctx->op.aad_len = aadlen;
135   ctx->op.iv = ctx->iv;
136   ptls_aead__build_iv (ctx->super.algo, ctx->op.iv, ctx->static_iv, seq);
137   ctx->op.src = (u8 *) input;
138   ctx->op.dst = _output;
139   ctx->op.key_index = ctx->key_index;
140   ctx->op.len = inlen - tag_size;
141   ctx->op.tag_len = tag_size;
142   ctx->op.tag = ctx->op.src + ctx->op.len;
143
144   vnet_crypto_process_ops (vm, &(ctx->op), 1);
145   assert (ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
146
147   return ctx->op.len;
148 }
149
150 static void
151 ptls_vpp_crypto_aead_encrypt_init (ptls_aead_context_t *_ctx, uint64_t seq,
152                                    const void *aad, size_t aadlen)
153 {
154   struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
155
156   vnet_crypto_op_init (&ctx->op, ctx->id);
157   ctx->op.aad = (void *) aad;
158   ctx->op.aad_len = aadlen;
159   ctx->op.iv = ctx->iv;
160   ptls_aead__build_iv (ctx->super.algo, ctx->op.iv, ctx->static_iv, seq);
161   ctx->op.key_index = ctx->key_index;
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   vlib_main_t *vm = vlib_get_main ();
201   struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
202
203   clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
204   vnet_crypto_key_del (vm, ctx->key_index);
205   clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
206 }
207
208 static int
209 ptls_vpp_crypto_aead_setup_crypto (ptls_aead_context_t *_ctx, int is_enc,
210                                    const void *key, const void *iv,
211                                    vnet_crypto_alg_t alg)
212 {
213   struct vlib_main_t *vm = vlib_get_main ();
214   struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
215   u16 key_len = ctx->super.algo->key_size;
216
217   if (alg == VNET_CRYPTO_ALG_AES_128_GCM)
218     {
219       ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_GCM_ENC :
220                          VNET_CRYPTO_OP_AES_128_GCM_DEC;
221     }
222   else if (alg == VNET_CRYPTO_ALG_AES_256_GCM)
223     {
224       ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_GCM_ENC :
225                          VNET_CRYPTO_OP_AES_256_GCM_DEC;
226     }
227   else
228     {
229       TLS_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
230                _ctx->algo->name);
231       return -1;
232     }
233
234   ctx->alg = alg;
235   ctx->chunk_index = 0;
236   clib_memcpy (ctx->static_iv, iv, ctx->super.algo->iv_size);
237
238   clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
239   ctx->key_index = vnet_crypto_key_add (vm, alg, (void *) key, key_len);
240   clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
241
242   if (is_enc)
243     {
244       ctx->super.do_encrypt_init = ptls_vpp_crypto_aead_encrypt_init;
245       ctx->super.do_encrypt_update = ptls_vpp_crypto_aead_encrypt_update;
246       ctx->super.do_encrypt_final = ptls_vpp_crypto_aead_encrypt_final;
247     }
248   else
249     {
250       ctx->super.do_decrypt = ptls_vpp_crypto_aead_decrypt;
251     }
252   ctx->super.dispose_crypto = ptls_vpp_crypto_aead_dispose_crypto;
253
254   return 0;
255 }
256
257 static int
258 ptls_vpp_crypto_aes128ctr_setup_crypto (ptls_cipher_context_t * ctx,
259                                         int is_enc, const void *key)
260 {
261   return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
262                                               ptls_vpp_crypto_cipher_encrypt);
263 }
264
265 static int
266 ptls_vpp_crypto_aes256ctr_setup_crypto (ptls_cipher_context_t * ctx,
267                                         int is_enc, const void *key)
268 {
269   return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
270                                               ptls_vpp_crypto_cipher_encrypt);
271 }
272
273 static int
274 ptls_vpp_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t *ctx,
275                                              int is_enc, const void *key,
276                                              const void *iv)
277 {
278   return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
279                                             VNET_CRYPTO_ALG_AES_128_GCM);
280 }
281
282 static int
283 ptls_vpp_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t *ctx,
284                                              int is_enc, const void *key,
285                                              const void *iv)
286 {
287   return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
288                                             VNET_CRYPTO_ALG_AES_256_GCM);
289 }
290
291 ptls_cipher_algorithm_t ptls_vpp_crypto_aes128ctr = {
292   "AES128-CTR",
293   PTLS_AES128_KEY_SIZE,
294   1,
295   PTLS_AES_IV_SIZE,
296   sizeof (struct vpp_aead_context_t),
297   ptls_vpp_crypto_aes128ctr_setup_crypto
298 };
299
300 ptls_cipher_algorithm_t ptls_vpp_crypto_aes256ctr = {
301   "AES256-CTR",
302   PTLS_AES256_KEY_SIZE,
303   1 /* block size */,
304   PTLS_AES_IV_SIZE,
305   sizeof (struct vpp_aead_context_t),
306   ptls_vpp_crypto_aes256ctr_setup_crypto
307 };
308
309 ptls_aead_algorithm_t ptls_vpp_crypto_aes128gcm = {
310   "AES128-GCM",
311   PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
312   PTLS_AESGCM_INTEGRITY_LIMIT,
313   &ptls_vpp_crypto_aes128ctr,
314   NULL,
315   PTLS_AES128_KEY_SIZE,
316   PTLS_AESGCM_IV_SIZE,
317   PTLS_AESGCM_TAG_SIZE,
318   sizeof (struct vpp_aead_context_t),
319   ptls_vpp_crypto_aead_aes128gcm_setup_crypto
320 };
321
322 ptls_aead_algorithm_t ptls_vpp_crypto_aes256gcm = {
323   "AES256-GCM",
324   PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
325   PTLS_AESGCM_INTEGRITY_LIMIT,
326   &ptls_vpp_crypto_aes256ctr,
327   NULL,
328   PTLS_AES256_KEY_SIZE,
329   PTLS_AESGCM_IV_SIZE,
330   PTLS_AESGCM_TAG_SIZE,
331   sizeof (struct vpp_aead_context_t),
332   ptls_vpp_crypto_aead_aes256gcm_setup_crypto
333 };
334
335 ptls_cipher_suite_t ptls_vpp_crypto_aes128gcmsha256 =
336   { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
337   &ptls_vpp_crypto_aes128gcm,
338   &ptls_openssl_sha256
339 };
340
341 ptls_cipher_suite_t ptls_vpp_crypto_aes256gcmsha384 =
342   { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
343   &ptls_vpp_crypto_aes256gcm,
344   &ptls_openssl_sha384
345 };
346
347 ptls_cipher_suite_t *ptls_vpp_crypto_cipher_suites[] =
348   { &ptls_vpp_crypto_aes256gcmsha384,
349   &ptls_vpp_crypto_aes128gcmsha256,
350   NULL
351 };
352
353 /*
354  * fd.io coding-style-patch-verification: ON
355  *
356  * Local Variables:
357  * eval: (c-set-style "gnu")
358  * End:
359  */