f3514d07b23aa6d57f5c525da08892e44af8df50
[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   /* 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,
206                                    vnet_crypto_alg_t alg)
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
233   clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
234   ctx->key_index = vnet_crypto_key_add (vm, alg, (void *) key, key_len);
235   clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
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
253 ptls_vpp_crypto_aes128ctr_setup_crypto (ptls_cipher_context_t * ctx,
254                                         int is_enc, const void *key)
255 {
256   return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
257                                               ptls_vpp_crypto_cipher_encrypt);
258 }
259
260 static int
261 ptls_vpp_crypto_aes256ctr_setup_crypto (ptls_cipher_context_t * ctx,
262                                         int is_enc, const void *key)
263 {
264   return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
265                                               ptls_vpp_crypto_cipher_encrypt);
266 }
267
268 static int
269 ptls_vpp_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t *ctx,
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
278 ptls_vpp_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t *ctx,
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),
292   ptls_vpp_crypto_aes128ctr_setup_crypto
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),
301   ptls_vpp_crypto_aes256ctr_setup_crypto
302 };
303
304 ptls_aead_algorithm_t ptls_vpp_crypto_aes128gcm = {
305   "AES128-GCM",
306   PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
307   PTLS_AESGCM_INTEGRITY_LIMIT,
308   &ptls_vpp_crypto_aes128ctr,
309   NULL,
310   PTLS_AES128_KEY_SIZE,
311   PTLS_AESGCM_IV_SIZE,
312   PTLS_AESGCM_TAG_SIZE,
313   sizeof (struct vpp_aead_context_t),
314   ptls_vpp_crypto_aead_aes128gcm_setup_crypto
315 };
316
317 ptls_aead_algorithm_t ptls_vpp_crypto_aes256gcm = {
318   "AES256-GCM",
319   PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
320   PTLS_AESGCM_INTEGRITY_LIMIT,
321   &ptls_vpp_crypto_aes256ctr,
322   NULL,
323   PTLS_AES256_KEY_SIZE,
324   PTLS_AESGCM_IV_SIZE,
325   PTLS_AESGCM_TAG_SIZE,
326   sizeof (struct vpp_aead_context_t),
327   ptls_vpp_crypto_aead_aes256gcm_setup_crypto
328 };
329
330 ptls_cipher_suite_t ptls_vpp_crypto_aes128gcmsha256 =
331   { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
332   &ptls_vpp_crypto_aes128gcm,
333   &ptls_openssl_sha256
334 };
335
336 ptls_cipher_suite_t ptls_vpp_crypto_aes256gcmsha384 =
337   { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
338   &ptls_vpp_crypto_aes256gcm,
339   &ptls_openssl_sha384
340 };
341
342 ptls_cipher_suite_t *ptls_vpp_crypto_cipher_suites[] =
343   { &ptls_vpp_crypto_aes256gcmsha384,
344   &ptls_vpp_crypto_aes128gcmsha256,
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  */