FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
pico_vpp_crypto.c
Go to the documentation of this file.
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 
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 
29 
30 struct cipher_context_t
31 {
32  ptls_cipher_context_t super;
36 };
37 
39 {
40  ptls_aead_context_t super;
41  EVP_CIPHER_CTX *evp_ctx;
42  uint8_t static_iv[PTLS_MAX_IV_SIZE];
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 {
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 
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 
116  ctx->key_index = vnet_crypto_key_add (vm, algo,
117  (u8 *) key, _ctx->algo->key_size);
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 {
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 
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 static int
204 ptls_vpp_crypto_aead_setup_crypto (ptls_aead_context_t *_ctx, int is_enc,
205  const void *key, const void *iv,
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  if (alg == VNET_CRYPTO_ALG_AES_128_GCM)
213  {
214  ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_GCM_ENC :
215  VNET_CRYPTO_OP_AES_128_GCM_DEC;
216  }
217  else if (alg == VNET_CRYPTO_ALG_AES_256_GCM)
218  {
219  ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_GCM_ENC :
220  VNET_CRYPTO_OP_AES_256_GCM_DEC;
221  }
222  else
223  {
224  TLS_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
225  _ctx->algo->name);
226  return -1;
227  }
228 
229  ctx->alg = alg;
230  ctx->chunk_index = 0;
231  clib_memcpy (ctx->static_iv, iv, ctx->super.algo->iv_size);
232 
234  ctx->key_index = vnet_crypto_key_add (vm, alg, (void *) key, key_len);
236 
237  if (is_enc)
238  {
239  ctx->super.do_encrypt_init = ptls_vpp_crypto_aead_encrypt_init;
240  ctx->super.do_encrypt_update = ptls_vpp_crypto_aead_encrypt_update;
241  ctx->super.do_encrypt_final = ptls_vpp_crypto_aead_encrypt_final;
242  }
243  else
244  {
245  ctx->super.do_decrypt = ptls_vpp_crypto_aead_decrypt;
246  }
247  ctx->super.dispose_crypto = ptls_vpp_crypto_aead_dispose_crypto;
248 
249  return 0;
250 }
251 
252 static int
254  int is_enc, const void *key)
255 {
256  return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
258 }
259 
260 static int
262  int is_enc, const void *key)
263 {
264  return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
266 }
267 
268 static int
270  int is_enc, const void *key,
271  const void *iv)
272 {
273  return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
274  VNET_CRYPTO_ALG_AES_128_GCM);
275 }
276 
277 static int
279  int is_enc, const void *key,
280  const void *iv)
281 {
282  return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
283  VNET_CRYPTO_ALG_AES_256_GCM);
284 }
285 
286 ptls_cipher_algorithm_t ptls_vpp_crypto_aes128ctr = {
287  "AES128-CTR",
288  PTLS_AES128_KEY_SIZE,
289  1,
290  PTLS_AES_IV_SIZE,
291  sizeof (struct vpp_aead_context_t),
293 };
294 
295 ptls_cipher_algorithm_t ptls_vpp_crypto_aes256ctr = {
296  "AES256-CTR",
297  PTLS_AES256_KEY_SIZE,
298  1 /* block size */,
299  PTLS_AES_IV_SIZE,
300  sizeof (struct vpp_aead_context_t),
302 };
303 
304 ptls_aead_algorithm_t ptls_vpp_crypto_aes128gcm = {
305  "AES128-GCM",
306  PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
307  PTLS_AESGCM_INTEGRITY_LIMIT,
309  NULL,
310  PTLS_AES128_KEY_SIZE,
311  PTLS_AESGCM_IV_SIZE,
312  PTLS_AESGCM_TAG_SIZE,
313  sizeof (struct vpp_aead_context_t),
315 };
316 
317 ptls_aead_algorithm_t ptls_vpp_crypto_aes256gcm = {
318  "AES256-GCM",
319  PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
320  PTLS_AESGCM_INTEGRITY_LIMIT,
322  NULL,
323  PTLS_AES256_KEY_SIZE,
324  PTLS_AESGCM_IV_SIZE,
325  PTLS_AESGCM_TAG_SIZE,
326  sizeof (struct vpp_aead_context_t),
328 };
329 
330 ptls_cipher_suite_t ptls_vpp_crypto_aes128gcmsha256 =
331  { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
333  &ptls_openssl_sha256
334 };
335 
336 ptls_cipher_suite_t ptls_vpp_crypto_aes256gcmsha384 =
337  { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
339  &ptls_openssl_sha384
340 };
341 
342 ptls_cipher_suite_t *ptls_vpp_crypto_cipher_suites[] =
345  NULL
346 };
347 
348 /*
349  * fd.io coding-style-patch-verification: ON
350  *
351  * Local Variables:
352  * eval: (c-set-style "gnu")
353  * End:
354  */
vnet_crypto_op_init
static_always_inline void vnet_crypto_op_init(vnet_crypto_op_t *op, vnet_crypto_op_id_t type)
Definition: crypto.h:528
assert
#define assert(x)
Definition: dlmalloc.c:31
crypto.h
cipher_context_t::key_index
u32 key_index
Definition: pico_vpp_crypto.c:35
ptls_vpp_crypto_aead_aes256gcm_setup_crypto
static int ptls_vpp_crypto_aead_aes256gcm_setup_crypto(ptls_aead_context_t *ctx, int is_enc, const void *key, const void *iv)
Definition: pico_vpp_crypto.c:278
ptls_vpp_crypto_aes256gcm
ptls_aead_algorithm_t ptls_vpp_crypto_aes256gcm
Definition: pico_vpp_crypto.c:317
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
tls_picotls.h
vnet_crypto_main_t
Definition: crypto.h:451
vpp_aead_context_t::op
vnet_crypto_op_t op
Definition: pico_vpp_crypto.c:43
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
picotls_main
picotls_main_t picotls_main
Definition: tls_picotls.c:7
ptls_vpp_crypto_aead_encrypt_final
static size_t ptls_vpp_crypto_aead_encrypt_final(ptls_aead_context_t *_ctx, void *_output)
Definition: pico_vpp_crypto.c:183
clib_rwlock_writer_unlock
static void clib_rwlock_writer_unlock(clib_rwlock_t *p)
Definition: lock.h:206
ptls_vpp_crypto_aes128gcm
ptls_aead_algorithm_t ptls_vpp_crypto_aes128gcm
Definition: pico_vpp_crypto.c:304
key
typedef key
Definition: ipsec_types.api:91
ptls_vpp_crypto_aead_encrypt_init
static void ptls_vpp_crypto_aead_encrypt_init(ptls_aead_context_t *_ctx, uint64_t seq, const void *aad, size_t aadlen)
Definition: pico_vpp_crypto.c:151
ptls_vpp_crypto_cipher_suites
ptls_cipher_suite_t * ptls_vpp_crypto_cipher_suites[]
Definition: pico_vpp_crypto.c:342
vpp_aead_context_t::chunks
vnet_crypto_op_chunk_t chunks[2]
Definition: pico_vpp_crypto.c:46
vnet_crypto_op_t
Definition: crypto.h:255
ptls_vpp_crypto_aead_setup_crypto
static int ptls_vpp_crypto_aead_setup_crypto(ptls_aead_context_t *_ctx, int is_enc, const void *key, const void *iv, vnet_crypto_alg_t alg)
Definition: pico_vpp_crypto.c:204
ptls_vpp_crypto_aes128gcmsha256
ptls_cipher_suite_t ptls_vpp_crypto_aes128gcmsha256
Definition: pico_vpp_crypto.c:330
vpp_aead_context_t::super
ptls_aead_context_t super
Definition: pico_vpp_crypto.c:40
ptls_vpp_crypto_cipher_encrypt
static void ptls_vpp_crypto_cipher_encrypt(ptls_cipher_context_t *_ctx, void *output, const void *input, size_t _len)
Definition: pico_vpp_crypto.c:69
vnet_crypto_process_ops
u32 vnet_crypto_process_ops(vlib_main_t *vm, vnet_crypto_op_t ops[], u32 n_ops)
Definition: crypto.c:99
TLS_DBG
#define TLS_DBG(_lvl, _fmt, _args...)
Definition: tls.h:36
cipher_context_t::op
vnet_crypto_op_t op
Definition: quic_crypto.c:40
vpp_aead_context_t
Definition: pico_vpp_crypto.c:38
picotls_main_::crypto_keys_rw_lock
clib_rwlock_t crypto_keys_rw_lock
Definition: tls_picotls.h:46
vnet_crypto_process_chained_ops
u32 vnet_crypto_process_chained_ops(vlib_main_t *vm, vnet_crypto_op_t ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops)
Definition: crypto.c:105
ptls_vpp_crypto_aead_encrypt_update
static size_t ptls_vpp_crypto_aead_encrypt_update(ptls_aead_context_t *_ctx, void *output, const void *input, size_t inlen)
Definition: pico_vpp_crypto.c:169
cm
vnet_crypto_main_t * cm
Definition: pico_vpp_crypto.c:27
VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS
#define VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS
Definition: crypto.h:264
ptls_vpp_crypto_aes128ctr_setup_crypto
static int ptls_vpp_crypto_aes128ctr_setup_crypto(ptls_cipher_context_t *ctx, int is_enc, const void *key)
Definition: pico_vpp_crypto.c:253
clib_rwlock_writer_lock
static void clib_rwlock_writer_lock(clib_rwlock_t *p)
Definition: lock.h:192
ptls_vpp_crypto_aead_aes128gcm_setup_crypto
static int ptls_vpp_crypto_aead_aes128gcm_setup_crypto(ptls_aead_context_t *ctx, int is_enc, const void *key, const void *iv)
Definition: pico_vpp_crypto.c:269
vpp_aead_context_t::static_iv
uint8_t static_iv[PTLS_MAX_IV_SIZE]
Definition: pico_vpp_crypto.c:42
vnet_crypto_op_id_t
vnet_crypto_op_id_t
Definition: crypto.h:219
ptls_vpp_crypto_cipher_dispose
static void ptls_vpp_crypto_cipher_dispose(ptls_cipher_context_t *_ctx)
Definition: pico_vpp_crypto.c:63
vnet_crypto_op_chunk_t
Definition: crypto.h:248
cipher_context_t::super
ptls_cipher_context_t super
Definition: quic_crypto.c:39
crypto_main
vnet_crypto_main_t crypto_main
Definition: crypto.c:20
vnet_crypto_key_add
u32 vnet_crypto_key_add(vlib_main_t *vm, vnet_crypto_alg_t alg, u8 *data, u16 length)
Definition: crypto.c:360
ptls_vpp_crypto_aead_decrypt
size_t ptls_vpp_crypto_aead_decrypt(ptls_aead_context_t *_ctx, void *_output, const void *input, size_t inlen, uint64_t seq, const void *aad, size_t aadlen)
Definition: pico_vpp_crypto.c:124
ptls_vpp_crypto_cipher_setup_crypto
static int ptls_vpp_crypto_cipher_setup_crypto(ptls_cipher_context_t *_ctx, int is_enc, const void *key, const EVP_CIPHER *cipher, ptls_vpp_do_transform_fn do_transform)
Definition: pico_vpp_crypto.c:83
ptls_vpp_crypto_aes256ctr
ptls_cipher_algorithm_t ptls_vpp_crypto_aes256ctr
Definition: pico_vpp_crypto.c:295
cipher_context_t
Definition: quic_crypto.c:37
vpp_aead_context_t::alg
vnet_crypto_alg_t alg
Definition: pico_vpp_crypto.c:47
u32
unsigned int u32
Definition: types.h:88
ctx
long ctx[MAX_CONNS]
Definition: main.c:144
vpp_aead_context_t::evp_ctx
EVP_CIPHER_CTX * evp_ctx
Definition: pico_vpp_crypto.c:41
ptls_vpp_crypto_aes256gcmsha384
ptls_cipher_suite_t ptls_vpp_crypto_aes256gcmsha384
Definition: pico_vpp_crypto.c:336
cipher_context_t::id
vnet_crypto_op_id_t id
Definition: quic_crypto.c:41
iv
static u8 iv[]
Definition: aes_cbc.c:24
vpp_aead_context_t::id
vnet_crypto_op_id_t id
Definition: pico_vpp_crypto.c:45
vlib_main_t
Definition: main.h:102
tls.h
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
u8
unsigned char u8
Definition: types.h:56
ptls_vpp_do_transform_fn
void(* ptls_vpp_do_transform_fn)(ptls_cipher_context_t *, void *, const void *, size_t)
Definition: pico_vpp_crypto.c:24
vpp_aead_context_t::key_index
u32 key_index
Definition: pico_vpp_crypto.c:44
pico_vpp_crypto.h
key_len
u16 key_len
Definition: ikev2_types.api:95
ptls_vpp_crypto_aes256ctr_setup_crypto
static int ptls_vpp_crypto_aes256ctr_setup_crypto(ptls_cipher_context_t *ctx, int is_enc, const void *key)
Definition: pico_vpp_crypto.c:261
vpp_aead_context_t::iv
uint8_t iv[PTLS_MAX_IV_SIZE]
Definition: pico_vpp_crypto.c:49
vnet_crypto_alg_t
vnet_crypto_alg_t
Definition: crypto.h:145
ptls_vpp_crypto_aes128ctr
ptls_cipher_algorithm_t ptls_vpp_crypto_aes128ctr
Definition: pico_vpp_crypto.c:286
picotls_main_
Definition: tls_picotls.h:39
ptls_vpp_crypto_cipher_do_init
static void ptls_vpp_crypto_cipher_do_init(ptls_cipher_context_t *_ctx, const void *iv)
Definition: pico_vpp_crypto.c:53
vpp_aead_context_t::chunk_index
u32 chunk_index
Definition: pico_vpp_crypto.c:48
ptls_vpp_crypto_aead_dispose_crypto
static void ptls_vpp_crypto_aead_dispose_crypto(ptls_aead_context_t *_ctx)
Definition: pico_vpp_crypto.c:198