quic: update quicly to v0.1.3
[vpp.git] / src / plugins / quic / quic_crypto.c
1 /*
2  * Copyright (c) 2021 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.h>
17 #include <quic/quic_crypto.h>
18
19 #include <quicly.h>
20 #include <picotls/openssl.h>
21
22 #define QUICLY_EPOCH_1RTT 3
23
24 extern quic_main_t quic_main;
25 extern quic_ctx_t *quic_get_conn_ctx (quicly_conn_t * conn);
26 vnet_crypto_main_t *cm = &crypto_main;
27
28 struct cipher_context_t
29 {
30   ptls_cipher_context_t super;
31   vnet_crypto_op_t op;
32   vnet_crypto_op_id_t id;
33   u32 key_index;
34 };
35
36 struct aead_crypto_context_t
37 {
38   ptls_aead_context_t super;
39   vnet_crypto_op_t op;
40   u32 key_index;
41   vnet_crypto_op_id_t id;
42   uint8_t iv[PTLS_MAX_IV_SIZE];
43   uint8_t static_iv[PTLS_MAX_IV_SIZE];
44 };
45
46 static int
47 quic_crypto_setup_cipher (quicly_crypto_engine_t *engine, quicly_conn_t *conn,
48                           size_t epoch, int is_enc,
49                           ptls_cipher_context_t **header_protect_ctx,
50                           ptls_aead_context_t **packet_protect_ctx,
51                           ptls_aead_algorithm_t *aead,
52                           ptls_hash_algorithm_t *hash, const void *secret)
53 {
54   uint8_t hpkey[PTLS_MAX_SECRET_SIZE];
55   int ret;
56
57   *packet_protect_ctx = NULL;
58   /* generate new header protection key */
59   if (header_protect_ctx != NULL)
60     {
61       *header_protect_ctx = NULL;
62       ret =
63         ptls_hkdf_expand_label (hash, hpkey, aead->ctr_cipher->key_size,
64                                 ptls_iovec_init (secret, hash->digest_size),
65                                 "quic hp", ptls_iovec_init (NULL, 0), NULL);
66       if (ret)
67         goto Exit;
68       *header_protect_ctx = ptls_cipher_new (aead->ctr_cipher, is_enc, hpkey);
69       if (NULL == *header_protect_ctx)
70         {
71           ret = PTLS_ERROR_NO_MEMORY;
72           goto Exit;
73         }
74     }
75
76   /* generate new AEAD context */
77   *packet_protect_ctx =
78     ptls_aead_new (aead, hash, is_enc, secret, QUICLY_AEAD_BASE_LABEL);
79   if (NULL == *packet_protect_ctx)
80     {
81       ret = PTLS_ERROR_NO_MEMORY;
82       goto Exit;
83     }
84
85   if (epoch == QUICLY_EPOCH_1RTT && !is_enc)
86     {
87       quic_ctx_t *qctx = quic_get_conn_ctx (conn);
88       if (qctx->ingress_keys.aead_ctx != NULL)
89         qctx->key_phase_ingress++;
90
91       qctx->ingress_keys.aead_ctx = *packet_protect_ctx;
92       if (header_protect_ctx != NULL)
93         qctx->ingress_keys.hp_ctx = *header_protect_ctx;
94     }
95
96   ret = 0;
97
98 Exit:
99   if (ret)
100     {
101       if (*packet_protect_ctx != NULL)
102         {
103           ptls_aead_free (*packet_protect_ctx);
104           *packet_protect_ctx = NULL;
105         }
106       if (header_protect_ctx && *header_protect_ctx != NULL)
107         {
108           ptls_cipher_free (*header_protect_ctx);
109           *header_protect_ctx = NULL;
110         }
111     }
112   ptls_clear_memory (hpkey, sizeof (hpkey));
113   return ret;
114 }
115
116 static size_t
117 quic_crypto_aead_decrypt (quic_ctx_t *qctx, ptls_aead_context_t *_ctx,
118                           void *_output, const void *input, size_t inlen,
119                           uint64_t decrypted_pn, const void *aad,
120                           size_t aadlen)
121 {
122   vlib_main_t *vm = vlib_get_main ();
123
124   struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
125
126   vnet_crypto_op_init (&ctx->op, ctx->id);
127   ctx->op.aad = (u8 *) aad;
128   ctx->op.aad_len = aadlen;
129   ctx->op.iv = ctx->iv;
130   ptls_aead__build_iv (ctx->super.algo, ctx->op.iv, ctx->static_iv,
131                        decrypted_pn);
132   ctx->op.src = (u8 *) input;
133   ctx->op.dst = _output;
134   ctx->op.key_index = ctx->key_index;
135   ctx->op.len = inlen - ctx->super.algo->tag_size;
136   ctx->op.tag_len = ctx->super.algo->tag_size;
137   ctx->op.tag = ctx->op.src + ctx->op.len;
138
139   vnet_crypto_process_ops (vm, &(ctx->op), 1);
140
141   return ctx->op.len;
142 }
143
144 void
145 quic_crypto_decrypt_packet (quic_ctx_t * qctx, quic_rx_packet_ctx_t * pctx)
146 {
147   ptls_cipher_context_t *header_protection = NULL;
148   ptls_aead_context_t *aead = NULL;
149   int pn;
150
151   /* Long Header packets are not decrypted by vpp */
152   if (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]))
153     return;
154
155   uint64_t next_expected_packet_number =
156     quicly_get_next_expected_packet_number (qctx->conn);
157   if (next_expected_packet_number == UINT64_MAX)
158     return;
159
160   aead = qctx->ingress_keys.aead_ctx;
161   header_protection = qctx->ingress_keys.hp_ctx;
162
163   if (!aead || !header_protection)
164     return;
165
166   size_t encrypted_len = pctx->packet.octets.len - pctx->packet.encrypted_off;
167   uint8_t hpmask[5] = { 0 };
168   uint32_t pnbits = 0;
169   size_t pnlen, ptlen, i;
170
171   /* decipher the header protection, as well as obtaining pnbits, pnlen */
172   if (encrypted_len < header_protection->algo->iv_size + QUICLY_MAX_PN_SIZE)
173     return;
174   ptls_cipher_init (header_protection,
175                     pctx->packet.octets.base + pctx->packet.encrypted_off +
176                     QUICLY_MAX_PN_SIZE);
177   ptls_cipher_encrypt (header_protection, hpmask, hpmask, sizeof (hpmask));
178   pctx->packet.octets.base[0] ^=
179     hpmask[0] & (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]) ?
180                  0xf : 0x1f);
181   pnlen = (pctx->packet.octets.base[0] & 0x3) + 1;
182   for (i = 0; i != pnlen; ++i)
183     {
184       pctx->packet.octets.base[pctx->packet.encrypted_off + i] ^=
185         hpmask[i + 1];
186       pnbits =
187         (pnbits << 8) | pctx->packet.octets.base[pctx->packet.encrypted_off +
188                                                  i];
189     }
190
191   size_t aead_off = pctx->packet.encrypted_off + pnlen;
192
193   pn =
194     quicly_determine_packet_number (pnbits, pnlen * 8,
195                                     next_expected_packet_number);
196
197   int key_phase_bit =
198     (pctx->packet.octets.base[0] & QUICLY_KEY_PHASE_BIT) != 0;
199
200   if (key_phase_bit != (qctx->key_phase_ingress & 1))
201     {
202       pctx->packet.octets.base[0] ^=
203         hpmask[0] &
204         (QUICLY_PACKET_IS_LONG_HEADER (pctx->packet.octets.base[0]) ? 0xf :
205          0x1f);
206       for (i = 0; i != pnlen; ++i)
207         {
208           pctx->packet.octets.base[pctx->packet.encrypted_off + i] ^=
209             hpmask[i + 1];
210         }
211       return;
212     }
213
214   if ((ptlen = quic_crypto_aead_decrypt (
215          qctx, aead, pctx->packet.octets.base + aead_off,
216          pctx->packet.octets.base + aead_off,
217          pctx->packet.octets.len - aead_off, pn, pctx->packet.octets.base,
218          aead_off)) == SIZE_MAX)
219     {
220       fprintf (stderr,
221                "%s: aead decryption failure (pn: %d)\n", __FUNCTION__, pn);
222       return;
223     }
224
225   pctx->packet.encrypted_off = aead_off;
226   pctx->packet.octets.len = ptlen + aead_off;
227
228   pctx->packet.decrypted.pn = pn;
229   pctx->packet.decrypted.key_phase = qctx->key_phase_ingress;
230 }
231
232 void
233 quic_crypto_encrypt_packet (struct st_quicly_crypto_engine_t *engine,
234                             quicly_conn_t *conn,
235                             ptls_cipher_context_t *header_protect_ctx,
236                             ptls_aead_context_t *packet_protect_ctx,
237                             ptls_iovec_t datagram, size_t first_byte_at,
238                             size_t payload_from, uint64_t packet_number,
239                             int coalesced)
240 {
241   vlib_main_t *vm = vlib_get_main ();
242
243   struct cipher_context_t *hp_ctx =
244     (struct cipher_context_t *) header_protect_ctx;
245   struct aead_crypto_context_t *aead_ctx =
246     (struct aead_crypto_context_t *) packet_protect_ctx;
247
248   void *input = datagram.base + payload_from;
249   void *output = input;
250   size_t inlen =
251     datagram.len - payload_from - packet_protect_ctx->algo->tag_size;
252   const void *aad = datagram.base + first_byte_at;
253   size_t aadlen = payload_from - first_byte_at;
254
255   /* Build AEAD encrypt crypto operation */
256   vnet_crypto_op_init (&aead_ctx->op, aead_ctx->id);
257   aead_ctx->op.aad = (u8 *) aad;
258   aead_ctx->op.aad_len = aadlen;
259   aead_ctx->op.iv = aead_ctx->iv;
260   ptls_aead__build_iv (aead_ctx->super.algo, aead_ctx->op.iv,
261                        aead_ctx->static_iv, packet_number);
262   aead_ctx->op.key_index = aead_ctx->key_index;
263   aead_ctx->op.src = (u8 *) input;
264   aead_ctx->op.dst = output;
265   aead_ctx->op.len = inlen;
266   aead_ctx->op.tag_len = aead_ctx->super.algo->tag_size;
267   aead_ctx->op.tag = aead_ctx->op.src + inlen;
268   vnet_crypto_process_ops (vm, &(aead_ctx->op), 1);
269   assert (aead_ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
270
271   /* Build Header protection crypto operation */
272   ptls_aead_supplementary_encryption_t supp = {
273     .ctx = header_protect_ctx,
274     .input =
275       datagram.base + payload_from - QUICLY_SEND_PN_SIZE + QUICLY_MAX_PN_SIZE
276   };
277
278   /* Build Header protection crypto operation */
279   vnet_crypto_op_init (&hp_ctx->op, hp_ctx->id);
280   memset (supp.output, 0, sizeof (supp.output));
281   hp_ctx->op.iv = (u8 *) supp.input;
282   hp_ctx->op.key_index = hp_ctx->key_index;
283   hp_ctx->op.src = (u8 *) supp.output;
284   hp_ctx->op.dst = (u8 *) supp.output;
285   hp_ctx->op.len = sizeof (supp.output);
286   vnet_crypto_process_ops (vm, &(hp_ctx->op), 1);
287   assert (hp_ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);
288
289   datagram.base[first_byte_at] ^=
290     supp.output[0] &
291     (QUICLY_PACKET_IS_LONG_HEADER (datagram.base[first_byte_at]) ? 0xf : 0x1f);
292   for (size_t i = 0; i != QUICLY_SEND_PN_SIZE; ++i)
293     datagram.base[payload_from + i - QUICLY_SEND_PN_SIZE] ^=
294       supp.output[i + 1];
295 }
296
297 static int
298 quic_crypto_cipher_setup_crypto (ptls_cipher_context_t *_ctx, int is_enc,
299                                  const void *key, const EVP_CIPHER *cipher)
300 {
301   struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;
302
303   vlib_main_t *vm = vlib_get_main ();
304   vnet_crypto_alg_t algo;
305   if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
306     {
307       algo = VNET_CRYPTO_ALG_AES_128_CTR;
308       ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_CTR_ENC :
309                          VNET_CRYPTO_OP_AES_128_CTR_DEC;
310       ptls_openssl_aes128ctr.setup_crypto (_ctx, is_enc, key);
311     }
312   else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
313     {
314       algo = VNET_CRYPTO_ALG_AES_256_CTR;
315       ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_CTR_ENC :
316                          VNET_CRYPTO_OP_AES_256_CTR_DEC;
317       ptls_openssl_aes256ctr.setup_crypto (_ctx, is_enc, key);
318     }
319   else
320     {
321       QUIC_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
322                 _ctx->algo->name);
323       assert (0);
324     }
325
326   if (quic_main.vnet_crypto_enabled)
327     {
328       clib_rwlock_writer_lock (&quic_main.crypto_keys_quic_rw_lock);
329       ctx->key_index =
330         vnet_crypto_key_add (vm, algo, (u8 *) key, _ctx->algo->key_size);
331       clib_rwlock_writer_unlock (&quic_main.crypto_keys_quic_rw_lock);
332     }
333
334   return 0;
335 }
336
337 static int
338 quic_crypto_aes128ctr_setup_crypto (ptls_cipher_context_t * ctx, int is_enc,
339                                     const void *key)
340 {
341   return quic_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr ());
342 }
343
344 static int
345 quic_crypto_aes256ctr_setup_crypto (ptls_cipher_context_t * ctx, int is_enc,
346                                     const void *key)
347 {
348   return quic_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr ());
349 }
350
351 static int
352 quic_crypto_aead_setup_crypto (ptls_aead_context_t *_ctx, int is_enc,
353                                const void *key, const void *iv,
354                                const EVP_CIPHER *cipher)
355 {
356   vlib_main_t *vm = vlib_get_main ();
357   struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
358
359   vnet_crypto_alg_t algo;
360   if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
361     {
362       algo = VNET_CRYPTO_ALG_AES_128_GCM;
363       ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_GCM_ENC :
364                          VNET_CRYPTO_OP_AES_128_GCM_DEC;
365       ptls_openssl_aes128gcm.setup_crypto (_ctx, is_enc, key, iv);
366     }
367   else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
368     {
369       algo = VNET_CRYPTO_ALG_AES_256_GCM;
370       ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_GCM_ENC :
371                          VNET_CRYPTO_OP_AES_256_GCM_DEC;
372       ptls_openssl_aes256gcm.setup_crypto (_ctx, is_enc, key, iv);
373     }
374   else
375     {
376       QUIC_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
377                 _ctx->algo->name);
378       assert (0);
379     }
380
381   if (quic_main.vnet_crypto_enabled)
382     {
383       clib_memcpy (ctx->static_iv, iv, ctx->super.algo->iv_size);
384
385       clib_rwlock_writer_lock (&quic_main.crypto_keys_quic_rw_lock);
386       ctx->key_index = vnet_crypto_key_add (vm, algo,
387                                             (u8 *) key, _ctx->algo->key_size);
388       clib_rwlock_writer_unlock (&quic_main.crypto_keys_quic_rw_lock);
389     }
390
391   return 0;
392 }
393
394 static int
395 quic_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t *ctx, int is_enc,
396                                          const void *key, const void *iv)
397 {
398   return quic_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
399                                         EVP_aes_128_gcm ());
400 }
401
402 static int
403 quic_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t *ctx, int is_enc,
404                                          const void *key, const void *iv)
405 {
406   return quic_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
407                                         EVP_aes_256_gcm ());
408 }
409
410 int
411 quic_encrypt_ticket_cb (ptls_encrypt_ticket_t *_self, ptls_t *tls,
412                         int is_encrypt, ptls_buffer_t *dst, ptls_iovec_t src)
413 {
414   quic_session_cache_t *self = (void *) _self;
415   int ret;
416
417   if (is_encrypt)
418     {
419
420       /* replace the cached entry along with a newly generated session id */
421       clib_mem_free (self->data.base);
422       if ((self->data.base = clib_mem_alloc (src.len)) == NULL)
423         return PTLS_ERROR_NO_MEMORY;
424
425       ptls_get_context (tls)->random_bytes (self->id, sizeof (self->id));
426       clib_memcpy (self->data.base, src.base, src.len);
427       self->data.len = src.len;
428
429       /* store the session id in buffer */
430       if ((ret = ptls_buffer_reserve (dst, sizeof (self->id))) != 0)
431         return ret;
432       clib_memcpy (dst->base + dst->off, self->id, sizeof (self->id));
433       dst->off += sizeof (self->id);
434     }
435   else
436     {
437       /* check if session id is the one stored in cache */
438       if (src.len != sizeof (self->id))
439         return PTLS_ERROR_SESSION_NOT_FOUND;
440       if (clib_memcmp (self->id, src.base, sizeof (self->id)) != 0)
441         return PTLS_ERROR_SESSION_NOT_FOUND;
442
443       /* return the cached value */
444       if ((ret = ptls_buffer_reserve (dst, self->data.len)) != 0)
445         return ret;
446       clib_memcpy (dst->base + dst->off, self->data.base, self->data.len);
447       dst->off += self->data.len;
448     }
449
450   return 0;
451 }
452
453 ptls_cipher_algorithm_t quic_crypto_aes128ctr = {
454   "AES128-CTR",
455   PTLS_AES128_KEY_SIZE,
456   1,
457   PTLS_AES_IV_SIZE,
458   sizeof (struct cipher_context_t),
459   quic_crypto_aes128ctr_setup_crypto
460 };
461
462 ptls_cipher_algorithm_t quic_crypto_aes256ctr = {
463   "AES256-CTR",
464   PTLS_AES256_KEY_SIZE,
465   1 /* block size */,
466   PTLS_AES_IV_SIZE,
467   sizeof (struct cipher_context_t),
468   quic_crypto_aes256ctr_setup_crypto
469 };
470
471 ptls_aead_algorithm_t quic_crypto_aes128gcm = {
472   "AES128-GCM",
473   PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
474   PTLS_AESGCM_INTEGRITY_LIMIT,
475   &quic_crypto_aes128ctr,
476   &ptls_openssl_aes128ecb,
477   PTLS_AES128_KEY_SIZE,
478   PTLS_AESGCM_IV_SIZE,
479   PTLS_AESGCM_TAG_SIZE,
480   sizeof (struct aead_crypto_context_t),
481   quic_crypto_aead_aes128gcm_setup_crypto
482 };
483
484 ptls_aead_algorithm_t quic_crypto_aes256gcm = {
485   "AES256-GCM",
486   PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
487   PTLS_AESGCM_INTEGRITY_LIMIT,
488   &quic_crypto_aes256ctr,
489   &ptls_openssl_aes256ecb,
490   PTLS_AES256_KEY_SIZE,
491   PTLS_AESGCM_IV_SIZE,
492   PTLS_AESGCM_TAG_SIZE,
493   sizeof (struct aead_crypto_context_t),
494   quic_crypto_aead_aes256gcm_setup_crypto
495 };
496
497 ptls_cipher_suite_t quic_crypto_aes128gcmsha256 = {
498   PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
499   &quic_crypto_aes128gcm, &ptls_openssl_sha256
500 };
501
502 ptls_cipher_suite_t quic_crypto_aes256gcmsha384 = {
503   PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
504   &quic_crypto_aes256gcm, &ptls_openssl_sha384
505 };
506
507 ptls_cipher_suite_t *quic_crypto_cipher_suites[] = {
508   &quic_crypto_aes256gcmsha384, &quic_crypto_aes128gcmsha256, NULL
509 };
510
511 quicly_crypto_engine_t quic_crypto_engine = { quic_crypto_setup_cipher,
512                                               quic_crypto_encrypt_packet };
513
514 /*
515  * fd.io coding-style-patch-verification: ON
516  *
517  * Local Variables:
518  * eval: (c-set-style "gnu")
519  * End:
520  */