summaryrefslogtreecommitdiffstats
path: root/src/plugins/gtpu/gtpu_msg_enum.h
AgeCommit message (Expand)AuthorFilesLines
2017-06-02Add l3 routing for GTP-U decapHongjun Ni1-1/+1
2017-05-15Add GTP-U plugin. VPP-694Hongjun Ni1-0/+31
a id='n66' href='#n66'>66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
/*
 * Copyright (c) 2021 Intel and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/crypto/crypto.h>
#include <vnet/tls/tls.h>
#include <picotls/openssl.h>
#include <picotls.h>

#include <tlspicotls/pico_vpp_crypto.h>
#include <tlspicotls/tls_picotls.h>

typedef void (*ptls_vpp_do_transform_fn) (ptls_cipher_context_t *, void *,
					  const void *, size_t);

vnet_crypto_main_t *cm = &crypto_main;
extern picotls_main_t picotls_main;

struct cipher_context_t
{
  ptls_cipher_context_t super;
  vnet_crypto_op_t op;
  vnet_crypto_op_id_t id;
  u32 key_index;
};

struct vpp_aead_context_t
{
  ptls_aead_context_t super;
  EVP_CIPHER_CTX *evp_ctx;
  uint8_t static_iv[PTLS_MAX_IV_SIZE];
  vnet_crypto_op_t op;
  u32 key_index;
  vnet_crypto_op_id_t id;
  vnet_crypto_op_chunk_t chunks[2];
  vnet_crypto_alg_t alg;
  u32 chunk_index;
  uint8_t iv[PTLS_MAX_IV_SIZE];
};

static void
ptls_vpp_crypto_cipher_do_init (ptls_cipher_context_t * _ctx, const void *iv)
{
  struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;

  vnet_crypto_op_init (&ctx->op, ctx->id);
  ctx->op.iv = (u8 *) iv;
  ctx->op.key_index = ctx->key_index;
}

static void
ptls_vpp_crypto_cipher_dispose (ptls_cipher_context_t * _ctx)
{
  /* Do nothing */
}

static void
ptls_vpp_crypto_cipher_encrypt (ptls_cipher_context_t * _ctx, void *output,
				const void *input, size_t _len)
{
  vlib_main_t *vm = vlib_get_main ();
  struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;

  ctx->op.src = (u8 *) input;
  ctx->op.dst = output;
  ctx->op.len = _len;

  vnet_crypto_process_ops (vm, &ctx->op, 1);
}

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)
{
  struct cipher_context_t *ctx = (struct cipher_context_t *) _ctx;

  ctx->super.do_dispose = ptls_vpp_crypto_cipher_dispose;
  ctx->super.do_init = ptls_vpp_crypto_cipher_do_init;
  ctx->super.do_transform = do_transform;

  vlib_main_t *vm = vlib_get_main ();
  vnet_crypto_alg_t algo;
  if (!strcmp (ctx->super.algo->name, "AES128-CTR"))
    {
      algo = VNET_CRYPTO_ALG_AES_128_CTR;
      ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_CTR_ENC :
			 VNET_CRYPTO_OP_AES_128_CTR_DEC;
    }
  else if (!strcmp (ctx->super.algo->name, "AES256-CTR"))
    {
      algo = VNET_CRYPTO_ALG_AES_256_CTR;
      ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_CTR_ENC :
			 VNET_CRYPTO_OP_AES_256_CTR_DEC;
    }
  else
    {
      TLS_DBG (1, "%s, Invalid crypto cipher : ", __FUNCTION__,
	       _ctx->algo->name);
      assert (0);
    }

  clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
  ctx->key_index = vnet_crypto_key_add (vm, algo,
					(u8 *) key, _ctx->algo->key_size);
  clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);

  return 0;
}

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)
{
  vlib_main_t *vm = vlib_get_main ();
  struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
  int tag_size = ctx->super.algo->tag_size;

  vnet_crypto_op_init (&ctx->op, ctx->id);
  ctx->op.aad = (u8 *) aad;
  ctx->op.aad_len = aadlen;
  ctx->op.iv = ctx->iv;
  ptls_aead__build_iv (ctx->super.algo, ctx->op.iv, ctx->static_iv, seq);
  ctx->op.src = (u8 *) input;
  ctx->op.dst = _output;
  ctx->op.key_index = ctx->key_index;
  ctx->op.len = inlen - tag_size;
  ctx->op.tag_len = tag_size;
  ctx->op.tag = ctx->op.src + ctx->op.len;

  vnet_crypto_process_ops (vm, &(ctx->op), 1);
  assert (ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);

  return ctx->op.len;
}

static void
ptls_vpp_crypto_aead_encrypt_init (ptls_aead_context_t *_ctx, uint64_t seq,
				   const void *aad, size_t aadlen)
{
  struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;

  vnet_crypto_op_init (&ctx->op, ctx->id);
  ctx->op.aad = (void *) aad;
  ctx->op.aad_len = aadlen;
  ctx->op.iv = ctx->iv;
  ptls_aead__build_iv (ctx->super.algo, ctx->op.iv, ctx->static_iv, seq);
  ctx->op.key_index = ctx->key_index;
  ctx->op.n_chunks = 2;
  ctx->op.chunk_index = 0;

  ctx->op.flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
}

static size_t
ptls_vpp_crypto_aead_encrypt_update (ptls_aead_context_t * _ctx, void *output,
				     const void *input, size_t inlen)
{
  struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
  ctx->chunks[ctx->chunk_index].dst = output;
  ctx->chunks[ctx->chunk_index].src = (void *) input;
  ctx->chunks[ctx->chunk_index].len = inlen;

  ctx->chunk_index = ctx->chunk_index == 0 ? 1 : 0;

  return inlen;
}

static size_t
ptls_vpp_crypto_aead_encrypt_final (ptls_aead_context_t * _ctx, void *_output)
{
  struct vlib_main_t *vm = vlib_get_main ();
  struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;

  ctx->op.tag = _output;
  ctx->op.tag_len = ctx->super.algo->tag_size;

  vnet_crypto_process_chained_ops (vm, &(ctx->op), ctx->chunks, 1);
  assert (ctx->op.status == VNET_CRYPTO_OP_STATUS_COMPLETED);

  return ctx->super.algo->tag_size;
}

static void
ptls_vpp_crypto_aead_dispose_crypto (ptls_aead_context_t * _ctx)
{
  vlib_main_t *vm = vlib_get_main ();
  struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;

  clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
  vnet_crypto_key_del (vm, ctx->key_index);
  clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);
}

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)
{
  struct vlib_main_t *vm = vlib_get_main ();
  struct vpp_aead_context_t *ctx = (struct vpp_aead_context_t *) _ctx;
  u16 key_len = ctx->super.algo->key_size;

  if (alg == VNET_CRYPTO_ALG_AES_128_GCM)
    {
      ctx->id = is_enc ? VNET_CRYPTO_OP_AES_128_GCM_ENC :
			 VNET_CRYPTO_OP_AES_128_GCM_DEC;
    }
  else if (alg == VNET_CRYPTO_ALG_AES_256_GCM)
    {
      ctx->id = is_enc ? VNET_CRYPTO_OP_AES_256_GCM_ENC :
			 VNET_CRYPTO_OP_AES_256_GCM_DEC;
    }
  else
    {
      TLS_DBG (1, "%s, invalied aead cipher %s", __FUNCTION__,
	       _ctx->algo->name);
      return -1;
    }

  ctx->alg = alg;
  ctx->chunk_index = 0;
  clib_memcpy (ctx->static_iv, iv, ctx->super.algo->iv_size);

  clib_rwlock_writer_lock (&picotls_main.crypto_keys_rw_lock);
  ctx->key_index = vnet_crypto_key_add (vm, alg, (void *) key, key_len);
  clib_rwlock_writer_unlock (&picotls_main.crypto_keys_rw_lock);

  if (is_enc)
    {
      ctx->super.do_encrypt_init = ptls_vpp_crypto_aead_encrypt_init;
      ctx->super.do_encrypt_update = ptls_vpp_crypto_aead_encrypt_update;
      ctx->super.do_encrypt_final = ptls_vpp_crypto_aead_encrypt_final;
    }
  else
    {
      ctx->super.do_decrypt = ptls_vpp_crypto_aead_decrypt;
    }
  ctx->super.dispose_crypto = ptls_vpp_crypto_aead_dispose_crypto;

  return 0;
}

static int
ptls_vpp_crypto_aes128ctr_setup_crypto (ptls_cipher_context_t * ctx,
					int is_enc, const void *key)
{
  return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_128_ctr (),
					      ptls_vpp_crypto_cipher_encrypt);
}

static int
ptls_vpp_crypto_aes256ctr_setup_crypto (ptls_cipher_context_t * ctx,
					int is_enc, const void *key)
{
  return ptls_vpp_crypto_cipher_setup_crypto (ctx, 1, key, EVP_aes_256_ctr (),
					      ptls_vpp_crypto_cipher_encrypt);
}

static int
ptls_vpp_crypto_aead_aes128gcm_setup_crypto (ptls_aead_context_t *ctx,
					     int is_enc, const void *key,
					     const void *iv)
{
  return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
					    VNET_CRYPTO_ALG_AES_128_GCM);
}

static int
ptls_vpp_crypto_aead_aes256gcm_setup_crypto (ptls_aead_context_t *ctx,
					     int is_enc, const void *key,
					     const void *iv)
{
  return ptls_vpp_crypto_aead_setup_crypto (ctx, is_enc, key, iv,
					    VNET_CRYPTO_ALG_AES_256_GCM);
}

ptls_cipher_algorithm_t ptls_vpp_crypto_aes128ctr = {
  "AES128-CTR",
  PTLS_AES128_KEY_SIZE,
  1,
  PTLS_AES_IV_SIZE,
  sizeof (struct vpp_aead_context_t),
  ptls_vpp_crypto_aes128ctr_setup_crypto
};

ptls_cipher_algorithm_t ptls_vpp_crypto_aes256ctr = {
  "AES256-CTR",
  PTLS_AES256_KEY_SIZE,
  1 /* block size */,
  PTLS_AES_IV_SIZE,
  sizeof (struct vpp_aead_context_t),
  ptls_vpp_crypto_aes256ctr_setup_crypto
};

#define PTLS_X86_CACHE_LINE_ALIGN_BITS 6
ptls_aead_algorithm_t ptls_vpp_crypto_aes128gcm = {
  "AES128-GCM",
  PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
  PTLS_AESGCM_INTEGRITY_LIMIT,
  &ptls_vpp_crypto_aes128ctr,
  NULL,
  PTLS_AES128_KEY_SIZE,
  PTLS_AESGCM_IV_SIZE,
  PTLS_AESGCM_TAG_SIZE,
  { PTLS_TLS12_AESGCM_FIXED_IV_SIZE, PTLS_TLS12_AESGCM_RECORD_IV_SIZE },
  1,
  PTLS_X86_CACHE_LINE_ALIGN_BITS,
  sizeof (struct vpp_aead_context_t),
  ptls_vpp_crypto_aead_aes128gcm_setup_crypto
};

ptls_aead_algorithm_t ptls_vpp_crypto_aes256gcm = {
  "AES256-GCM",
  PTLS_AESGCM_CONFIDENTIALITY_LIMIT,
  PTLS_AESGCM_INTEGRITY_LIMIT,
  &ptls_vpp_crypto_aes256ctr,
  NULL,
  PTLS_AES256_KEY_SIZE,
  PTLS_AESGCM_IV_SIZE,
  PTLS_AESGCM_TAG_SIZE,
  { PTLS_TLS12_AESGCM_FIXED_IV_SIZE, PTLS_TLS12_AESGCM_RECORD_IV_SIZE },
  1,
  PTLS_X86_CACHE_LINE_ALIGN_BITS,
  sizeof (struct vpp_aead_context_t),
  ptls_vpp_crypto_aead_aes256gcm_setup_crypto
};

ptls_cipher_suite_t ptls_vpp_crypto_aes128gcmsha256 =
  { PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,
  &ptls_vpp_crypto_aes128gcm,
  &ptls_openssl_sha256
};

ptls_cipher_suite_t ptls_vpp_crypto_aes256gcmsha384 =
  { PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,
  &ptls_vpp_crypto_aes256gcm,
  &ptls_openssl_sha384
};

ptls_cipher_suite_t *ptls_vpp_crypto_cipher_suites[] =
  { &ptls_vpp_crypto_aes256gcmsha384,
  &ptls_vpp_crypto_aes128gcmsha256,
  NULL
};

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */