FD.io VPP  v19.08-27-gf4dcae4
Vector Packet Processing
quic_crypto.c
Go to the documentation of this file.
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 
28 {
29  ptls_cipher_context_t super;
32 };
33 
35 {
36  ptls_aead_context_t super;
39 };
40 
42 
43 static void
44 vpp_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 
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 vpp_crypto_cipher_dispose (ptls_cipher_context_t * _ctx)
71 {
72  /* Do nothing */
73 }
74 
75 static void
76 vpp_crypto_cipher_encrypt (ptls_cipher_context_t * _ctx, void *output,
77  const void *input, size_t _len)
78 {
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 vpp_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 = vpp_crypto_cipher_dispose;
97  ctx->super.do_init = vpp_crypto_cipher_do_init;
98  ctx->super.do_transform = do_transform;
99 
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 vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
129 }
130 
131 static int
132 aes256ctr_setup_crypto (ptls_cipher_context_t * ctx, int is_enc,
133  const void *key)
134 {
135  return vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
137 }
138 
139 size_t
140 vpp_crypto_aead_encrypt (ptls_aead_context_t * _ctx, void *output,
141  const void *input, size_t inlen, uint64_t seq,
142  const void *iv, const void *aad, size_t aadlen)
143 {
144  QUIC_DBG (1, "[quic] %s", __FUNCTION__);
145 
147  struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
148 
150  if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
151  {
152  id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
153  }
154  else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
155  {
156  id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
157  }
158  else
159  {
160  assert (0);
161  }
162 
163  vnet_crypto_op_init (&ctx->op, id);
164  ctx->op.aad = (u8 *) aad;
165  ctx->op.aad_len = aadlen;
166  ctx->op.iv = (u8 *) iv;
167 
168  ctx->op.src = (u8 *) input;
169  ctx->op.dst = output;
170  ctx->op.key_index = ctx->key_index;
171  ctx->op.len = inlen;
172 
173  ctx->op.tag_len = ctx->super.algo->tag_size;
174  ctx->op.tag = ctx->op.src + inlen;
175 
176  vnet_crypto_process_ops (vm, &ctx->op, 1);
177 
178  return ctx->op.len + ctx->op.tag_len;
179 }
180 
181 size_t
182 vpp_crypto_aead_decrypt (ptls_aead_context_t * _ctx, void *_output,
183  const void *input, size_t inlen, const void *iv,
184  const void *aad, size_t aadlen)
185 {
186  QUIC_DBG (1, "[quic] %s", __FUNCTION__);
187 
189  struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
190 
192  if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
193  {
194  id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
195  }
196  else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
197  {
198  id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
199  }
200  else
201  {
202  assert (0);
203  }
204 
205  vnet_crypto_op_init (&ctx->op, id);
206  ctx->op.aad = (u8 *) aad;
207  ctx->op.aad_len = aadlen;
208  ctx->op.iv = (u8 *) iv;
209 
210  ctx->op.src = (u8 *) input;
211  ctx->op.dst = _output;
212  ctx->op.key_index = ctx->key_index;
213  ctx->op.len = inlen - ctx->super.algo->tag_size;
214 
215  ctx->op.tag_len = ctx->super.algo->tag_size;
216  ctx->op.tag = ctx->op.src + ctx->op.len;
217 
218  vnet_crypto_process_ops (vm, &ctx->op, 1);
219 
220  return ctx->op.len;
221 }
222 
223 static void
224 vpp_crypto_aead_dispose_crypto (ptls_aead_context_t * _ctx)
225 {
226  QUIC_DBG (1, "[quic] %s", __FUNCTION__);
227 }
228 
229 static int
230 vpp_crypto_aead_setup_crypto (ptls_aead_context_t * _ctx, int is_enc,
231  const void *key, const EVP_CIPHER * cipher)
232 {
233  QUIC_DBG (1, "%s, algo : ", __FUNCTION__, _ctx->algo->name);
234 
236  struct aead_crypto_context_t *ctx = (struct aead_crypto_context_t *) _ctx;
237 
238  vnet_crypto_alg_t algo;
239  if (!strcmp (ctx->super.algo->name, "AES128-GCM"))
240  {
241  algo = VNET_CRYPTO_ALG_AES_128_GCM;
242  }
243  else if (!strcmp (ctx->super.algo->name, "AES256-GCM"))
244  {
245  algo = VNET_CRYPTO_ALG_AES_256_GCM;
246  }
247  else
248  {
249  QUIC_DBG (1, "%s, algo : ", __FUNCTION__, _ctx->algo->name);
250  assert (0);
251  }
252 
253  ctx->super.do_decrypt = vpp_crypto_aead_decrypt;
254  ctx->super.do_encrypt = vpp_crypto_aead_encrypt;
255  ctx->super.dispose_crypto = vpp_crypto_aead_dispose_crypto;
256 
257  ctx->key_index = vnet_crypto_key_add (vm, algo,
258  (u8 *) key, _ctx->algo->key_size);
259 
260  return 0;
261 }
262 
263 static int
264 vpp_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t * ctx, int is_enc,
265  const void *key)
266 {
267  return vpp_crypto_aead_setup_crypto (ctx, is_enc, key, EVP_aes_128_gcm ());
268 }
269 
270 static int
271 vpp_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t * ctx, int is_enc,
272  const void *key)
273 {
274  return vpp_crypto_aead_setup_crypto (ctx, is_enc, key, EVP_aes_256_gcm ());
275 }
276 
277 ptls_cipher_algorithm_t vpp_crypto_aes128ctr = { "AES128-CTR",
278  PTLS_AES128_KEY_SIZE,
279  1, PTLS_AES_IV_SIZE,
280  sizeof (struct cipher_context_t),
281  aes128ctr_setup_crypto
282 };
283 
284 ptls_cipher_algorithm_t vpp_crypto_aes256ctr = { "AES256-CTR",
285  PTLS_AES256_KEY_SIZE,
286  1 /* block size */ ,
287  PTLS_AES_IV_SIZE,
288  sizeof (struct cipher_context_t),
289  aes256ctr_setup_crypto
290 };
291 
292 ptls_aead_algorithm_t vpp_crypto_aes128gcm = { "AES128-GCM",
294  NULL,
295  PTLS_AES128_KEY_SIZE,
296  PTLS_AESGCM_IV_SIZE,
297  PTLS_AESGCM_TAG_SIZE,
298  sizeof (struct aead_crypto_context_t),
299  vpp_crypto_aead_aes128gcm_setup_crypto
300 };
301 
302 ptls_aead_algorithm_t vpp_crypto_aes256gcm = { "AES256-GCM",
304  NULL,
305  PTLS_AES256_KEY_SIZE,
306  PTLS_AESGCM_IV_SIZE,
307  PTLS_AESGCM_TAG_SIZE,
308  sizeof (struct aead_crypto_context_t),
309  vpp_crypto_aead_aes256gcm_setup_crypto
310 };
311 
312 ptls_cipher_suite_t vpp_crypto_aes128gcmsha256 =
313  { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
315  &ptls_openssl_sha256
316 };
317 
318 ptls_cipher_suite_t vpp_crypto_aes256gcmsha384 =
319  { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
321  &ptls_openssl_sha384
322 };
323 
324 ptls_cipher_suite_t *vpp_crypto_cipher_suites[] =
327  NULL
328 };
329 
330 /*
331  * fd.io coding-style-patch-verification: ON
332  *
333  * Local Variables:
334  * eval: (c-set-style "gnu")
335  * End:
336  */
u32 vnet_crypto_process_ops(vlib_main_t *vm, vnet_crypto_op_t ops[], u32 n_ops)
Definition: crypto.c:46
static int vpp_crypto_aead_aes256gcm_setup_crypto(ptls_aead_context_t *ctx, int is_enc, const void *key)
Definition: quic_crypto.c:271
ptls_cipher_context_t super
Definition: quic_crypto.c:29
static int aes256ctr_setup_crypto(ptls_cipher_context_t *ctx, int is_enc, const void *key)
Definition: quic_crypto.c:132
static int vpp_crypto_aead_aes128gcm_setup_crypto(ptls_aead_context_t *ctx, int is_enc, const void *key)
Definition: quic_crypto.c:264
#define NULL
Definition: clib.h:58
static int vpp_crypto_aead_setup_crypto(ptls_aead_context_t *_ctx, int is_enc, const void *key, const EVP_CIPHER *cipher)
Definition: quic_crypto.c:230
size_t vpp_crypto_aead_encrypt(ptls_aead_context_t *_ctx, void *output, const void *input, size_t inlen, uint64_t seq, const void *iv, const void *aad, size_t aadlen)
Definition: quic_crypto.c:140
static void vpp_crypto_cipher_encrypt(ptls_cipher_context_t *_ctx, void *output, const void *input, size_t _len)
Definition: quic_crypto.c:76
unsigned char u8
Definition: types.h:56
ptls_cipher_algorithm_t vpp_crypto_aes128ctr
Definition: quic_crypto.c:277
#define QUIC_DBG(_lvl, _fmt, _args...)
Definition: quic.h:65
static void vpp_crypto_aead_dispose_crypto(ptls_aead_context_t *_ctx)
Definition: quic_crypto.c:224
#define assert(x)
Definition: dlmalloc.c:30
static_always_inline void vnet_crypto_op_init(vnet_crypto_op_t *op, vnet_crypto_op_id_t type)
Definition: crypto.h:221
unsigned int u32
Definition: types.h:88
vnet_crypto_op_t op
Definition: quic_crypto.c:30
u32 vnet_crypto_key_add(vlib_main_t *vm, vnet_crypto_alg_t alg, u8 *data, u16 length)
Definition: crypto.c:207
vnet_crypto_alg_t
Definition: crypto.h:86
static void vpp_crypto_cipher_dispose(ptls_cipher_context_t *_ctx)
Definition: quic_crypto.c:70
static u8 iv[]
Definition: aes_cbc.c:24
long ctx[MAX_CONNS]
Definition: main.c:144
static void vpp_crypto_cipher_do_init(ptls_cipher_context_t *_ctx, const void *iv)
Definition: quic_crypto.c:44
ptls_cipher_algorithm_t vpp_crypto_aes256ctr
Definition: quic_crypto.c:284
vlib_main_t * vm
Definition: buffer.c:312
static int vpp_crypto_cipher_setup_crypto(ptls_cipher_context_t *_ctx, int is_enc, const void *key, const EVP_CIPHER *cipher, quicly_do_transform_fn do_transform)
Definition: quic_crypto.c:90
void(* quicly_do_transform_fn)(ptls_cipher_context_t *, void *, const void *, size_t)
Definition: quic_crypto.c:24
ptls_cipher_suite_t vpp_crypto_aes256gcmsha384
Definition: quic_crypto.c:318
ptls_cipher_suite_t vpp_crypto_aes128gcmsha256
Definition: quic_crypto.c:312
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
ptls_aead_algorithm_t vpp_crypto_aes256gcm
Definition: quic_crypto.c:302
size_t vpp_crypto_aead_decrypt(ptls_aead_context_t *_ctx, void *_output, const void *input, size_t inlen, const void *iv, const void *aad, size_t aadlen)
Definition: quic_crypto.c:182
vnet_crypto_op_t op
Definition: quic_crypto.c:37
typedef key
Definition: ipsec.api:245
ptls_aead_context_t super
Definition: quic_crypto.c:36
vnet_crypto_op_id_t
Definition: crypto.h:105
u32 id
Definition: udp.api:45
vnet_crypto_main_t crypto_main
Definition: crypto.c:20
static int aes128ctr_setup_crypto(ptls_cipher_context_t *ctx, int is_enc, const void *key)
Definition: quic_crypto.c:124
ptls_aead_algorithm_t vpp_crypto_aes128gcm
Definition: quic_crypto.c:292