quic: update quicly to v0.0.7-vpp
[vpp.git] / src / plugins / quic / quic_crypto.c
1 /*
2  * Copyright (c) 2019 Cisco 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 <quic/quic_crypto.h>
17 #include <quic/quic.h>
18
19 #include <vnet/crypto/crypto.h>
20
21 #include <picotls/openssl.h>
22 #include <quicly.h>
23
24 typedef void (*quicly_do_transform_fn) (ptls_cipher_context_t *, void *,
25                                         const void *, size_t);
26
27 struct cipher_context_t
28 {
29   ptls_cipher_context_t super;
30   vnet_crypto_op_t op;
31   u32 key_index;
32 };
33
34 struct aead_crypto_context_t
35 {
36   ptls_aead_context_t super;
37   vnet_crypto_op_t op;
38   u32 key_index;
39 };
40
41 vnet_crypto_main_t *cm = &crypto_main;
42
43 static void
44 quic_crypto_cipher_do_init (ptls_cipher_context_t * _ctx, const void *iv)
45 {
46   struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
47
48   vnet_crypto_op_id_t id;
49   if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
50     {
51       id = VNET_CRYPTO_OP_AES_128_CTR_ENC;
52     }
53   else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
54     {
55       id = VNET_CRYPTO_OP_AES_256_CTR_ENC;
56     }
57   else
58     {
59       QUIC_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
60                 _ctx->algo->name);
61       assert (0);
62     }
63
64   vnet_crypto_op_init (&ctx->op, id);
65   ctx->op.iv = (u8 *) iv;
66   ctx->op.key_index = ctx->key_index;
67 }
68
69 static void
70 quic_crypto_cipher_dispose (ptls_cipher_context_t * _ctx)
71 {
72   /* Do nothing */
73 }
74
75 static void
76 quic_crypto_cipher_encrypt (ptls_cipher_context_t * _ctx, void *output,
77                             const void *input, size_t _len)
78 {
79   vlib_main_t *vm = vlib_get_main ();
80   struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
81
82   ctx->op.src = (u8 *) input;
83   ctx->op.dst = output;
84   ctx->op.len = _len;
85
86   vnet_crypto_process_ops (vm, &ctx->op, 1);
87 }
88
89 static int
90 quic_crypto_cipher_setup_crypto (ptls_cipher_context_t * _ctx, int is_enc,
91                                  const void *key, const EVP_CIPHER * cipher,
92                                  quicly_do_transform_fn do_transform)
93 {
94   struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
95
96   ctx->super.do_dispose = quic_crypto_cipher_dispose;
97   ctx->super.do_init = quic_crypto_cipher_do_init;
98   ctx->super.do_transform = do_transform;
99
100   vlib_main_t *vm = vlib_get_main ();
101   vnet_crypto_alg_t algo;
102   if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
103     {
104       algo = VNET_CRYPTO_ALG_AES_128_CTR;
105     }
106   else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
107     {
108       algo = VNET_CRYPTO_ALG_AES_256_CTR;
109     }
110   else
111     {
112       QUIC_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
113                 _ctx->algo->name);
114       assert (0);
115     }
116
117   ctx->key_index = vnet_crypto_key_add (vm, algo,
118                                         (u8 *) key, _ctx->algo->key_size);
119
120   return 0;
121 }
122
123 static int
124 aes128ctr_setup_crypto (ptls_cipher_context_t * ctx, int is_enc,
125                         const void *key)
126 {
127   return quic_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
128                                           quic_crypto_cipher_encrypt);
129 }
130
131 static int
132 aes256ctr_setup_crypto (ptls_cipher_context_t * ctx, int is_enc,
133                         const void *key)
134 {
135   return quic_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
136                                           quic_crypto_cipher_encrypt);
137 }
138
139 void
140 quic_crypto_aead_encrypt_init (ptls_aead_context_t * _ctx, const void *iv,
141                                const void *aad, size_t aadlen)
142 {
143   struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
144
145   vnet_crypto_op_id_t id;
146   if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
147     {
148       id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
149     }
150   else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
151     {
152       id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
153     }
154   else
155     {
156       assert (0);
157     }
158
159   vnet_crypto_op_init (&ctx->op, id);
160   ctx->op.aad = (u8 *) aad;
161   ctx->op.aad_len = aadlen;
162   ctx->op.iv = (u8 *) iv;
163   ctx->op.key_index = ctx->key_index;
164 }
165
166 size_t
167 quic_crypto_aead_encrypt_update (ptls_aead_context_t * _ctx, void *output,
168                                  const void *input, size_t inlen)
169 {
170   struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
171
172   ctx->op.src = (u8 *) input;
173   ctx->op.dst = output;
174   ctx->op.len = inlen;
175   ctx->op.tag_len = ctx->super.algo->tag_size;
176   ctx->op.tag = ctx->op.src + inlen;
177
178   return 0;
179 }
180
181 size_t
182 quic_crypto_aead_encrypt_final (ptls_aead_context_t * _ctx, void *output)
183 {
184   vlib_main_t *vm = vlib_get_main ();
185   struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
186
187   vnet_crypto_process_ops (vm, &ctx->op, 1);
188
189   return ctx->op.len + ctx->op.tag_len;
190 }
191
192 size_t
193 quic_crypto_aead_decrypt (ptls_aead_context_t * _ctx, void *_output,
194                           const void *input, size_t inlen, const void *iv,
195                           const void *aad, size_t aadlen)
196 {
197   vlib_main_t *vm = vlib_get_main ();
198   struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
199
200   vnet_crypto_op_id_t id;
201   if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
202     {
203       id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
204     }
205   else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
206     {
207       id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
208     }
209   else
210     {
211       assert (0);
212     }
213
214   vnet_crypto_op_init (&ctx->op, id);
215   ctx->op.aad = (u8 *) aad;
216   ctx->op.aad_len = aadlen;
217   ctx->op.iv = (u8 *) iv;
218
219   ctx->op.src = (u8 *) input;
220   ctx->op.dst = _output;
221   ctx->op.key_index = ctx->key_index;
222   ctx->op.len = inlen - ctx->super.algo->tag_size;
223
224   ctx->op.tag_len = ctx->super.algo->tag_size;
225   ctx->op.tag = ctx->op.src + ctx->op.len;
226
227   vnet_crypto_process_ops (vm, &ctx->op, 1);
228
229   return ctx->op.len;
230 }
231
232 static void
233 quic_crypto_aead_dispose_crypto (ptls_aead_context_t * _ctx)
234 {
235
236 }
237
238 static int
239 quic_crypto_aead_setup_crypto (ptls_aead_context_t * _ctx, int is_enc,
240                                const void *key, const EVP_CIPHER * cipher)
241 {
242   vlib_main_t *vm = vlib_get_main ();
243   struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
244
245   vnet_crypto_alg_t algo;
246   if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
247     {
248       algo = VNET_CRYPTO_ALG_AES_128_GCM;
249     }
250   else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
251     {
252       algo = VNET_CRYPTO_ALG_AES_256_GCM;
253     }
254   else
255     {
256       QUIC_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
257                 _ctx->algo->name);
258       assert (0);
259     }
260
261   ctx->super.do_decrypt = quic_crypto_aead_decrypt;
262   ctx->super.do_encrypt_init = quic_crypto_aead_encrypt_init;
263   ctx->super.do_encrypt_update = quic_crypto_aead_encrypt_update;
264   ctx->super.do_encrypt_final = quic_crypto_aead_encrypt_final;
265   ctx->super.dispose_crypto = quic_crypto_aead_dispose_crypto;
266
267   ctx->key_index = vnet_crypto_key_add (vm, algo,
268                                         (u8 *) key, _ctx->algo->key_size);
269
270   return 0;
271 }
272
273 static int
274 quic_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t * ctx,
275                                          int is_enc, const void *key)
276 {
277   return quic_crypto_aead_setup_crypto (ctx, is_enc, key, EVP_aes_128_gcm ());
278 }
279
280 static int
281 quic_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t * ctx,
282                                          int is_enc, const void *key)
283 {
284   return quic_crypto_aead_setup_crypto (ctx, is_enc, key, EVP_aes_256_gcm ());
285 }
286
287 ptls_cipher_algorithm_t quic_crypto_aes128ctr = { "AES128-CTR",
288   PTLS_AES128_KEY_SIZE,
289   1, PTLS_AES_IV_SIZE,
290   sizeof (struct cipher_context_t),
291   aes128ctr_setup_crypto
292 };
293
294 ptls_cipher_algorithm_t quic_crypto_aes256ctr = { "AES256-CTR",
295   PTLS_AES256_KEY_SIZE,
296   1 /* block size */ ,
297   PTLS_AES_IV_SIZE,
298   sizeof (struct cipher_context_t),
299   aes256ctr_setup_crypto
300 };
301
302 ptls_aead_algorithm_t quic_crypto_aes128gcm = { "AES128-GCM",
303   &quic_crypto_aes128ctr,
304   NULL,
305   PTLS_AES128_KEY_SIZE,
306   PTLS_AESGCM_IV_SIZE,
307   PTLS_AESGCM_TAG_SIZE,
308   sizeof (struct aead_crypto_context_t),
309   quic_crypto_aead_aes128gcm_setup_crypto
310 };
311
312 ptls_aead_algorithm_t quic_crypto_aes256gcm = { "AES256-GCM",
313   &quic_crypto_aes256ctr,
314   NULL,
315   PTLS_AES256_KEY_SIZE,
316   PTLS_AESGCM_IV_SIZE,
317   PTLS_AESGCM_TAG_SIZE,
318   sizeof (struct aead_crypto_context_t),
319   quic_crypto_aead_aes256gcm_setup_crypto
320 };
321
322 ptls_cipher_suite_t quic_crypto_aes128gcmsha256 =
323   { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
324   &quic_crypto_aes128gcm,
325   &ptls_openssl_sha256
326 };
327
328 ptls_cipher_suite_t quic_crypto_aes256gcmsha384 =
329   { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
330   &quic_crypto_aes256gcm,
331   &ptls_openssl_sha384
332 };
333
334 ptls_cipher_suite_t *quic_crypto_cipher_suites[] =
335   { &quic_crypto_aes256gcmsha384,
336   &quic_crypto_aes128gcmsha256,
337   NULL
338 };
339
340 int
341 quic_encrypt_ticket_cb (ptls_encrypt_ticket_t * _self, ptls_t * tls,
342                         int is_encrypt, ptls_buffer_t * dst, ptls_iovec_t src)
343 {
344   quic_session_cache_t *self = (void *) _self;
345   int ret;
346
347   if (is_encrypt)
348     {
349
350       /* replace the cached entry along with a newly generated session id */
351       clib_mem_free (self->data.base);
352       if ((self->data.base = clib_mem_alloc (src.len)) == NULL)
353         return PTLS_ERROR_NO_MEMORY;
354
355       ptls_get_context (tls)->random_bytes (self->id, sizeof (self->id));
356       clib_memcpy (self->data.base, src.base, src.len);
357       self->data.len = src.len;
358
359       /* store the session id in buffer */
360       if ((ret = ptls_buffer_reserve (dst, sizeof (self->id))) != 0)
361         return ret;
362       clib_memcpy (dst->base + dst->off, self->id, sizeof (self->id));
363       dst->off += sizeof (self->id);
364
365     }
366   else
367     {
368
369       /* check if session id is the one stored in cache */
370       if (src.len != sizeof (self->id))
371         return PTLS_ERROR_SESSION_NOT_FOUND;
372       if (clib_memcmp (self->id, src.base, sizeof (self->id)) != 0)
373         return PTLS_ERROR_SESSION_NOT_FOUND;
374
375       /* return the cached value */
376       if ((ret = ptls_buffer_reserve (dst, self->data.len)) != 0)
377         return ret;
378       clib_memcpy (dst->base + dst->off, self->data.base, self->data.len);
379       dst->off += self->data.len;
380     }
381
382   return 0;
383 }
384
385 /*
386  * fd.io coding-style-patch-verification: ON
387  *
388  * Local Variables:
389  * eval: (c-set-style "gnu")
390  * End:
391  */