aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/crypto_native/aes_gcm.c
blob: 6589d4119751b547d7020e3a249f8bc3287d72bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
/*
 *------------------------------------------------------------------
 * Copyright (c) 2019 Cisco 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 <vlib/vlib.h>
#include <vnet/plugin/plugin.h>
#include <vnet/crypto/crypto.h>
#include <crypto_native/crypto_native.h>
#include <vppinfra/crypto/aes_gcm.h>

#if __GNUC__ > 4 && !__clang__ && CLIB_DEBUG == 0
#pragma GCC optimize("O3")
#endif

static_always_inline u32
aes_ops_enc_aes_gcm (vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops,
		     aes_key_size_t ks)
{
  crypto_native_main_t *cm = &crypto_native_main;
  vnet_crypto_op_t *op = ops[0];
  aes_gcm_key_data_t *kd;
  u32 n_left = n_ops;

next:
  kd = (aes_gcm_key_data_t *) cm->key_data[op->key_index];
  aes_gcm (op->src, op->dst, op->aad, (u8 *) op->iv, op->tag, op->len,
	   op->aad_len, op->tag_len, kd, AES_KEY_ROUNDS (ks),
	   AES_GCM_OP_ENCRYPT);
  op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;

  if (--n_left)
    {
      op += 1;
      goto next;
    }

  return n_ops;
}

static_always_inline u32
aes_ops_dec_aes_gcm (vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops,
		     aes_key_size_t ks)
{
  crypto_native_main_t *cm = &crypto_native_main;
  vnet_crypto_op_t *op = ops[0];
  aes_gcm_key_data_t *kd;
  u32 n_left = n_ops;
  int rv;

next:
  kd = (aes_gcm_key_data_t *) cm->key_data[op->key_index];
  rv = aes_gcm (op->src, op->dst, op->aad, (u8 *) op->iv, op->tag, op->len,
		op->aad_len, op->tag_len, kd, AES_KEY_ROUNDS (ks),
		AES_GCM_OP_DECRYPT);

  if (rv)
    {
      op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
    }
  else
    {
      op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
      n_ops--;
    }

  if (--n_left)
    {
      op += 1;
      goto next;
    }

  return n_ops;
}

static_always_inline void *
aes_gcm_key_exp (vnet_crypto_key_t *key, aes_key_size_t ks)
{
  aes_gcm_key_data_t *kd;

  kd = clib_mem_alloc_aligned (sizeof (*kd), CLIB_CACHE_LINE_BYTES);

  clib_aes_gcm_key_expand (kd, key->data, ks);

  return kd;
}

#define foreach_aes_gcm_handler_type _ (128) _ (192) _ (256)

#define _(x)                                                                  \
  static u32 aes_ops_dec_aes_gcm_##x (vlib_main_t *vm,                        \
				      vnet_crypto_op_t *ops[], u32 n_ops)     \
  {                                                                           \
    return aes_ops_dec_aes_gcm (vm, ops, n_ops, AES_KEY_##x);                 \
  }                                                                           \
  static u32 aes_ops_enc_aes_gcm_##x (vlib_main_t *vm,                        \
				      vnet_crypto_op_t *ops[], u32 n_ops)     \
  {                                                                           \
    return aes_ops_enc_aes_gcm (vm, ops, n_ops, AES_KEY_##x);                 \
  }                                                                           \
  static void *aes_gcm_key_exp_##x (vnet_crypto_key_t *key)                   \
  {                                                                           \
    return aes_gcm_key_exp (key, AES_KEY_##x);                                \
  }

foreach_aes_gcm_handler_type;
#undef _

clib_error_t *
#if defined(__VAES__) && defined(__AVX512F__)
crypto_native_aes_gcm_init_icl (vlib_main_t *vm)
#elif defined(__VAES__)
crypto_native_aes_gcm_init_adl (vlib_main_t *vm)
#elif __AVX512F__
crypto_native_aes_gcm_init_skx (vlib_main_t *vm)
#elif __AVX2__
crypto_native_aes_gcm_init_hsw (vlib_main_t *vm)
#elif __aarch64__
crypto_native_aes_gcm_init_neon (vlib_main_t *vm)
#else
crypto_native_aes_gcm_init_slm (vlib_main_t *vm)
#endif
{
  crypto_native_main_t *cm = &crypto_native_main;

#define _(x)                                                                  \
  vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index,              \
				    VNET_CRYPTO_OP_AES_##x##_GCM_ENC,         \
				    aes_ops_enc_aes_gcm_##x);                 \
  vnet_crypto_register_ops_handler (vm, cm->crypto_engine_index,              \
				    VNET_CRYPTO_OP_AES_##x##_GCM_DEC,         \
				    aes_ops_dec_aes_gcm_##x);                 \
  cm->key_fn[VNET_CRYPTO_ALG_AES_##x##_GCM] = aes_gcm_key_exp_##x;
  foreach_aes_gcm_handler_type;
#undef _
  return 0;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
span class="p">): return ( self.glen == other.glen and self.saddr == other.gaddr and self.saddr == other.saddr ) elif ( hasattr(other, "grp_address_length") and hasattr(other, "grp_address") and hasattr(other, "src_address") ): # vl_api_mprefix_t if 4 == self.version: return ( self.glen == other.grp_address_length and self.gaddr == str(other.grp_address.ip4) and self.saddr == str(other.src_address.ip4) ) else: return ( self.glen == other.grp_address_length and self.gaddr == str(other.grp_address.ip6) and self.saddr == str(other.src_address.ip6) ) return NotImplemented class VppIpPuntPolicer(VppObject): def __init__(self, test, policer_index, is_ip6=False): self._test = test self._policer_index = policer_index self._is_ip6 = is_ip6 def add_vpp_config(self): self._test.vapi.ip_punt_police( policer_index=self._policer_index, is_ip6=self._is_ip6, is_add=True ) def remove_vpp_config(self): self._test.vapi.ip_punt_police( policer_index=self._policer_index, is_ip6=self._is_ip6, is_add=False ) def query_vpp_config(self): NotImplemented class VppIpPuntRedirect(VppObject): def __init__(self, test, rx_index, tx_index, nh_addr): self._test = test self._rx_index = rx_index self._tx_index = tx_index self._nh_addr = ip_address(nh_addr) def encode(self): return { "rx_sw_if_index": self._rx_index, "tx_sw_if_index": self._tx_index, "nh": self._nh_addr, } def add_vpp_config(self): self._test.vapi.ip_punt_redirect(punt=self.encode(), is_add=True) self._test.registry.register(self, self._test.logger) return self def remove_vpp_config(self): self._test.vapi.ip_punt_redirect(punt=self.encode(), is_add=False) def get_vpp_config(self): is_ipv6 = True if self._nh_addr.version == 6 else False return self._test.vapi.ip_punt_redirect_dump( sw_if_index=self._rx_index, is_ipv6=is_ipv6 ) def query_vpp_config(self): if self.get_vpp_config(): return True return False class VppIpPathMtu(VppObject): def __init__(self, test, nh, pmtu, table_id=0): self._test = test self.nh = nh self.pmtu = pmtu self.table_id = table_id def add_vpp_config(self): self._test.vapi.ip_path_mtu_update( pmtu={"nh": self.nh, "table_id": self.table_id, "path_mtu": self.pmtu} ) self._test.registry.register(self, self._test.logger) return self def modify(self, pmtu): self.pmtu = pmtu self._test.vapi.ip_path_mtu_update( pmtu={"nh": self.nh, "table_id": self.table_id, "path_mtu": self.pmtu} ) return self def remove_vpp_config(self): self._test.vapi.ip_path_mtu_update( pmtu={"nh": self.nh, "table_id": self.table_id, "path_mtu": 0} ) def query_vpp_config(self): ds = list(self._test.vapi.vpp.details_iter(self._test.vapi.ip_path_mtu_get)) for d in ds: if ( self.nh == str(d.pmtu.nh) and self.table_id == d.pmtu.table_id and self.pmtu == d.pmtu.path_mtu ): return True return False def object_id(self): return "ip-path-mtu-%d-%s-%d" % (self.table_id, self.nh, self.pmtu) def __str__(self): return self.object_id()