diff options
author | Neale Ranns <nranns@cisco.com> | 2019-04-11 15:14:07 +0000 |
---|---|---|
committer | Damjan Marion <dmarion@me.com> | 2019-04-16 15:54:31 +0000 |
commit | 47feb1146ec3b0e1cf2ebd83cd5211e1df261194 (patch) | |
tree | 602218094ef655d3c430ba29762ebf64d59f15d7 | |
parent | 3cf215aab7237d44cf153b43d80a971be9300626 (diff) |
IPSEC: support GCM in ESP
Change-Id: Id2ddb77b4ec3dd543d6e638bc882923f2bac011d
Signed-off-by: Neale Ranns <nranns@cisco.com>
-rw-r--r-- | src/plugins/crypto_openssl/main.c | 8 | ||||
-rw-r--r-- | src/vnet/crypto/crypto.h | 2 | ||||
-rw-r--r-- | src/vnet/ipsec/esp.h | 34 | ||||
-rw-r--r-- | src/vnet/ipsec/esp_decrypt.c | 35 | ||||
-rw-r--r-- | src/vnet/ipsec/esp_encrypt.c | 15 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec.c | 25 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec.h | 1 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_api.c | 2 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_cli.c | 4 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_format.c | 1 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_if.c | 2 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_if.h | 1 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_sa.c | 11 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_sa.h | 7 | ||||
-rw-r--r-- | test/patches/scapy-2.4/ipsec.patch | 52 | ||||
-rw-r--r-- | test/template_ipsec.py | 10 | ||||
-rw-r--r-- | test/test_ipsec_esp.py | 60 | ||||
-rw-r--r-- | test/test_ipsec_tun_if_esp.py | 127 |
18 files changed, 349 insertions, 48 deletions
diff --git a/src/plugins/crypto_openssl/main.c b/src/plugins/crypto_openssl/main.c index 5b661504b7d..ac586c2af77 100644 --- a/src/plugins/crypto_openssl/main.c +++ b/src/plugins/crypto_openssl/main.c @@ -115,14 +115,18 @@ openssl_ops_enc_gcm (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops, for (i = 0; i < n_ops; i++) { vnet_crypto_op_t *op = ops[i]; + u32 nonce[3]; int len; if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV) RAND_bytes (op->iv, 8); + nonce[0] = op->salt; + clib_memcpy_fast (nonce + 1, op->iv, 8); + EVP_EncryptInit_ex (ctx, cipher, 0, 0, 0); - EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 8, NULL); - EVP_EncryptInit_ex (ctx, 0, 0, op->key, op->iv); + EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL); + EVP_EncryptInit_ex (ctx, 0, 0, op->key, (u8 *) nonce); if (op->aad_len) EVP_EncryptUpdate (ctx, NULL, &len, op->aad, op->aad_len); EVP_EncryptUpdate (ctx, op->dst, &len, op->src, op->len); diff --git a/src/vnet/crypto/crypto.h b/src/vnet/crypto/crypto.h index 06f7e534e11..901c363342b 100644 --- a/src/vnet/crypto/crypto.h +++ b/src/vnet/crypto/crypto.h @@ -115,7 +115,7 @@ typedef struct u8 flags; #define VNET_CRYPTO_OP_FLAG_INIT_IV (1 << 0) #define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 1) - u32 len; + u32 len, salt; u16 aad_len; u8 key_len, iv_len, digest_len, tag_len; u8 *key; diff --git a/src/vnet/ipsec/esp.h b/src/vnet/ipsec/esp.h index 1e7f08277ca..052bbfc1d88 100644 --- a/src/vnet/ipsec/esp.h +++ b/src/vnet/ipsec/esp.h @@ -54,6 +54,20 @@ typedef CLIB_PACKED (struct { }) ip6_and_esp_header_t; /* *INDENT-ON* */ +/** + * AES GCM Additional Authentication data + */ +typedef struct esp_aead_t_ +{ + /** + * for GCM: when using ESN it's: + * SPI, seq-hi, seg-low + * else + * SPI, seq-low + */ + u32 data[3]; +} __clib_packed esp_aead_t; + #define ESP_SEQ_MAX (4294967295UL) #define ESP_MAX_BLOCK_SIZE (16) #define ESP_MAX_IV_SIZE (16) @@ -117,6 +131,26 @@ hmac_calc (vlib_main_t * vm, ipsec_sa_t * sa, u8 * data, int data_len, return sa->integ_icv_size; } +always_inline void +esp_aad_fill (vnet_crypto_op_t * op, + const esp_header_t * esp, const ipsec_sa_t * sa) +{ + esp_aead_t *aad; + + aad = (esp_aead_t *) op->aad; + clib_memcpy_fast (aad, esp, 8); + + if (ipsec_sa_is_set_USE_ESN (sa)) + { + /* SPI, seq-hi, seq-low */ + aad->data[2] = aad->data[1]; + aad->data[1] = clib_host_to_net_u32 (sa->seq_hi); + op->aad_len = 12; + } + else + /* SPI, seq-low */ + op->aad_len = 8; +} #endif /* __ESP_H__ */ /* diff --git a/src/vnet/ipsec/esp_decrypt.c b/src/vnet/ipsec/esp_decrypt.c index c94577a5d5a..d2365fce2d8 100644 --- a/src/vnet/ipsec/esp_decrypt.c +++ b/src/vnet/ipsec/esp_decrypt.c @@ -197,7 +197,7 @@ esp_decrypt_inline (vlib_main_t * vm, current_sa_pkts += 1; current_sa_bytes += pd->current_length; - if (PREDICT_TRUE (cpd.icv_sz > 0)) + if (PREDICT_TRUE (sa0->integ_op_id != VNET_CRYPTO_OP_NONE)) { vnet_crypto_op_t *op; vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES); @@ -233,6 +233,39 @@ esp_decrypt_inline (vlib_main_t * vm, vnet_crypto_op_init (op, sa0->crypto_dec_op_id); op->key = sa0->crypto_key.data; op->iv = payload; + + if (ipsec_sa_is_set_IS_AEAD (sa0)) + { + esp_header_t *esp0; + esp_aead_t *aad; + u8 *scratch; + u32 salt; + + /* + * construct the AAD and the nonce (Salt || IV) in a scratch + * space in front of the IP header. + */ + scratch = payload - esp_sz; + esp0 = (esp_header_t *) (scratch); + + scratch -= (sizeof (*aad) + pd->hdr_sz); + op->aad = scratch; + + esp_aad_fill (op, esp0, sa0); + + /* + * we don't need to refer to the ESP header anymore so we + * can overwrite it with the salt and use the IV where it is + * to form the nonce = (Salt + IV) + */ + salt = clib_host_to_net_u32 (sa0->salt); + op->iv -= sizeof (sa0->salt); + clib_memcpy_fast (op->iv, &salt, sizeof (sa0->salt)); + op->iv_len = cpd.iv_sz + sizeof (sa0->salt); + + op->tag = payload + len; + op->tag_len = 16; + } op->src = op->dst = payload += cpd.iv_sz; op->len = len - cpd.iv_sz; op->user_data = b - bufs; diff --git a/src/vnet/ipsec/esp_encrypt.c b/src/vnet/ipsec/esp_encrypt.c index fbc5166b946..e319a9628f4 100644 --- a/src/vnet/ipsec/esp_encrypt.c +++ b/src/vnet/ipsec/esp_encrypt.c @@ -436,6 +436,21 @@ esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node, op->len = payload_len - icv_sz; op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV; op->user_data = b - bufs; + op->salt = sa0->salt; + + if (ipsec_sa_is_set_IS_AEAD (sa0)) + { + /* + * construct the AAD in a scratch space in front + * of the IP header. + */ + op->aad = payload - hdr_len - sizeof (esp_aead_t); + + esp_aad_fill (op, esp, sa0); + + op->tag = payload + op->len; + op->tag_len = 16; + } } if (sa0->integ_op_id) diff --git a/src/vnet/ipsec/ipsec.c b/src/vnet/ipsec/ipsec.c index dc2f4cdbb60..73c5cf4d7ab 100644 --- a/src/vnet/ipsec/ipsec.c +++ b/src/vnet/ipsec/ipsec.c @@ -38,13 +38,6 @@ ipsec_check_ah_support (ipsec_sa_t * sa) static clib_error_t * ipsec_check_esp_support (ipsec_sa_t * sa) { - if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) - return clib_error_return (0, "unsupported aes-gcm-128 crypto-alg"); - if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_192) - return clib_error_return (0, "unsupported aes-gcm-192 crypto-alg"); - if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_256) - return clib_error_return (0, "unsupported aes-gcm-256 crypto-alg"); - return 0; } @@ -293,6 +286,24 @@ ipsec_init (vlib_main_t * vm) a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC; a->iv_size = a->block_size = 16; + a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128; + a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC; + a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC; + a->iv_size = a->block_size = 8; + a->icv_size = 16; + + a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192; + a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC; + a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC; + a->iv_size = a->block_size = 8; + a->icv_size = 16; + + a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256; + a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC; + a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC; + a->iv_size = a->block_size = 8; + a->icv_size = 16; + vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1); ipsec_main_integ_alg_t *i; diff --git a/src/vnet/ipsec/ipsec.h b/src/vnet/ipsec/ipsec.h index b6332d672fb..110bbcb3e5a 100644 --- a/src/vnet/ipsec/ipsec.h +++ b/src/vnet/ipsec/ipsec.h @@ -70,6 +70,7 @@ typedef struct vnet_crypto_op_id_t dec_op_id; u8 iv_size; u8 block_size; + u8 icv_size; } ipsec_main_crypto_alg_t; typedef struct diff --git a/src/vnet/ipsec/ipsec_api.c b/src/vnet/ipsec/ipsec_api.c index 753d7530de4..767cd2fb076 100644 --- a/src/vnet/ipsec/ipsec_api.c +++ b/src/vnet/ipsec/ipsec_api.c @@ -390,7 +390,7 @@ static void vl_api_ipsec_sad_entry_add_del_t_handler rv = ipsec_sa_add (id, spi, proto, crypto_alg, &crypto_key, integ_alg, &integ_key, flags, - 0, &tun_src, &tun_dst, &sa_index); + 0, 0, &tun_src, &tun_dst, &sa_index); else rv = ipsec_sa_del (id); diff --git a/src/vnet/ipsec/ipsec_cli.c b/src/vnet/ipsec/ipsec_cli.c index b247d5d87c6..096060865e9 100644 --- a/src/vnet/ipsec/ipsec_cli.c +++ b/src/vnet/ipsec/ipsec_cli.c @@ -141,7 +141,7 @@ ipsec_sa_add_del_command_fn (vlib_main_t * vm, if (is_add) rv = ipsec_sa_add (id, spi, proto, crypto_alg, &ck, integ_alg, &ik, flags, - 0, &tun_src, &tun_dst, NULL); + 0, 0, &tun_src, &tun_dst, NULL); else rv = ipsec_sa_del (id); @@ -790,6 +790,8 @@ create_ipsec_tunnel_command_fn (vlib_main_t * vm, num_m_args++; else if (unformat (line_input, "instance %u", &a.show_instance)) a.renumber = 1; + else if (unformat (line_input, "salt 0x%x", &a.salt)) + ; else if (unformat (line_input, "udp-encap")) a.udp_encap = 1; else if (unformat (line_input, "use-esn")) diff --git a/src/vnet/ipsec/ipsec_format.c b/src/vnet/ipsec/ipsec_format.c index 6e6007eed2a..80691f2836c 100644 --- a/src/vnet/ipsec/ipsec_format.c +++ b/src/vnet/ipsec/ipsec_format.c @@ -290,6 +290,7 @@ format_ipsec_sa (u8 * s, va_list * args) if (!(flags & IPSEC_FORMAT_DETAIL)) goto done; + s = format (s, "\n salt 0x%x", sa->salt); s = format (s, "\n seq %u seq-hi %u", sa->seq, sa->seq_hi); s = format (s, "\n last-seq %u last-seq-hi %u window %U", sa->last_seq, sa->last_seq_hi, diff --git a/src/vnet/ipsec/ipsec_if.c b/src/vnet/ipsec/ipsec_if.c index 3c1f84576d4..f7cd95873db 100644 --- a/src/vnet/ipsec/ipsec_if.c +++ b/src/vnet/ipsec/ipsec_if.c @@ -308,6 +308,7 @@ ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm, &integ_key, (flags | IPSEC_SA_FLAG_IS_INBOUND), args->tx_table_id, + args->salt, &args->remote_ip, &args->local_ip, &t->input_sa_index); @@ -328,6 +329,7 @@ ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm, &integ_key, flags, args->tx_table_id, + args->salt, &args->local_ip, &args->remote_ip, &t->output_sa_index); diff --git a/src/vnet/ipsec/ipsec_if.h b/src/vnet/ipsec/ipsec_if.h index 1e7f9df7c93..d1fa9bd4a91 100644 --- a/src/vnet/ipsec/ipsec_if.h +++ b/src/vnet/ipsec/ipsec_if.h @@ -61,6 +61,7 @@ typedef struct u32 show_instance; u8 udp_encap; u32 tx_table_id; + u32 salt; } ipsec_add_del_tunnel_args_t; /* *INDENT-OFF* */ diff --git a/src/vnet/ipsec/ipsec_sa.c b/src/vnet/ipsec/ipsec_sa.c index da12560bb7d..8f41c95159b 100644 --- a/src/vnet/ipsec/ipsec_sa.c +++ b/src/vnet/ipsec/ipsec_sa.c @@ -102,6 +102,11 @@ ipsec_sa_set_crypto_alg (ipsec_sa_t * sa, ipsec_crypto_alg_t crypto_alg) sa->crypto_dec_op_id = im->crypto_algs[crypto_alg].dec_op_id; ASSERT (sa->crypto_iv_size <= ESP_MAX_IV_SIZE); ASSERT (sa->crypto_block_size <= ESP_MAX_BLOCK_SIZE); + if (IPSEC_CRYPTO_ALG_IS_GCM (crypto_alg)) + { + sa->integ_icv_size = im->crypto_algs[crypto_alg].icv_size; + ipsec_sa_set_IS_AEAD (sa); + } } void @@ -124,6 +129,7 @@ ipsec_sa_add (u32 id, const ipsec_key_t * ik, ipsec_sa_flags_t flags, u32 tx_table_id, + u32 salt, const ip46_address_t * tun_src, const ip46_address_t * tun_dst, u32 * sa_out_index) { @@ -150,10 +156,11 @@ ipsec_sa_add (u32 id, sa->stat_index = sa_index; sa->protocol = proto; sa->flags = flags; - ipsec_sa_set_crypto_alg (sa, crypto_alg); - clib_memcpy (&sa->crypto_key, ck, sizeof (sa->crypto_key)); + sa->salt = salt; ipsec_sa_set_integ_alg (sa, integ_alg); clib_memcpy (&sa->integ_key, ik, sizeof (sa->integ_key)); + ipsec_sa_set_crypto_alg (sa, crypto_alg); + clib_memcpy (&sa->crypto_key, ck, sizeof (sa->crypto_key)); ip46_address_copy (&sa->tunnel_src_addr, tun_src); ip46_address_copy (&sa->tunnel_dst_addr, tun_dst); diff --git a/src/vnet/ipsec/ipsec_sa.h b/src/vnet/ipsec/ipsec_sa.h index 72a592984f6..f87e12e0204 100644 --- a/src/vnet/ipsec/ipsec_sa.h +++ b/src/vnet/ipsec/ipsec_sa.h @@ -43,6 +43,11 @@ typedef enum IPSEC_CRYPTO_N_ALG, } ipsec_crypto_alg_t; +#define IPSEC_CRYPTO_ALG_IS_GCM(_alg) \ + (((_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) || \ + (_alg == IPSEC_CRYPTO_ALG_AES_GCM_192) || \ + (_alg == IPSEC_CRYPTO_ALG_AES_GCM_256))) + #define foreach_ipsec_integ_alg \ _ (0, NONE, "none") \ _ (1, MD5_96, "md5-96") /* RFC2403 */ \ @@ -92,6 +97,7 @@ typedef struct ipsec_key_t_ _ (16, UDP_ENCAP, "udp-encap") \ _ (32, IS_GRE, "GRE") \ _ (64, IS_INBOUND, "inboud") \ + _ (128, IS_AEAD, "aead") \ typedef enum ipsec_sad_flags_t_ { @@ -192,6 +198,7 @@ extern int ipsec_sa_add (u32 id, const ipsec_key_t * ik, ipsec_sa_flags_t flags, u32 tx_table_id, + u32 salt, const ip46_address_t * tunnel_src_addr, const ip46_address_t * tunnel_dst_addr, u32 * sa_index); diff --git a/test/patches/scapy-2.4/ipsec.patch b/test/patches/scapy-2.4/ipsec.patch index b19112db2b7..731fe3ccc25 100644 --- a/test/patches/scapy-2.4/ipsec.patch +++ b/test/patches/scapy-2.4/ipsec.patch @@ -1,8 +1,30 @@ diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py -index 69e7ae3b..b35da2b1 100644 +index 69e7ae3b..0c69ba1a 100644 --- a/scapy/layers/ipsec.py +++ b/scapy/layers/ipsec.py -@@ -518,12 +518,16 @@ class AuthAlgo(object): +@@ -344,7 +344,8 @@ class CryptAlgo(object): + encryptor = cipher.encryptor() + + if self.is_aead: +- aad = struct.pack('!LL', esp.spi, esp.seq) ++ aad = struct.pack('!LQ' if sa.use_esn else '!LL', ++ esp.spi, esp.seq) + encryptor.authenticate_additional_data(aad) + data = encryptor.update(data) + encryptor.finalize() + data += encryptor.tag[:self.icv_size] +@@ -381,9 +382,9 @@ class CryptAlgo(object): + if self.is_aead: + # Tag value check is done during the finalize method + decryptor.authenticate_additional_data( +- struct.pack('!LL', esp.spi, esp.seq) ++ struct.pack('!LQ' if sa.use_esn else '!LL', ++ esp.spi, esp.seq) + ) +- + try: + data = decryptor.update(data) + decryptor.finalize() + except InvalidTag as err: +@@ -518,12 +519,16 @@ class AuthAlgo(object): else: return self.mac(key, self.digestmod(), default_backend()) @@ -20,7 +42,7 @@ index 69e7ae3b..b35da2b1 100644 @return: the signed packet """ -@@ -534,16 +538,20 @@ class AuthAlgo(object): +@@ -534,16 +539,20 @@ class AuthAlgo(object): if pkt.haslayer(ESP): mac.update(raw(pkt[ESP])) @@ -42,7 +64,7 @@ index 69e7ae3b..b35da2b1 100644 """ Check that the integrity check value (icv) of a packet is valid. -@@ -574,6 +582,8 @@ class AuthAlgo(object): +@@ -574,6 +583,8 @@ class AuthAlgo(object): clone = zero_mutable_fields(pkt.copy(), sending=False) mac.update(raw(clone)) @@ -51,7 +73,7 @@ index 69e7ae3b..b35da2b1 100644 computed_icv = mac.finalize()[:self.icv_size] # XXX: Cannot use mac.verify because the ICV can be truncated -@@ -757,7 +767,8 @@ class SecurityAssociation(object): +@@ -757,7 +768,8 @@ class SecurityAssociation(object): SUPPORTED_PROTOS = (IP, IPv6) def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None, @@ -61,7 +83,7 @@ index 69e7ae3b..b35da2b1 100644 """ @param proto: the IPsec proto to use (ESP or AH) @param spi: the Security Parameters Index of this SA -@@ -771,6 +782,7 @@ class SecurityAssociation(object): +@@ -771,6 +783,7 @@ class SecurityAssociation(object): to encapsulate the encrypted packets. @param nat_t_header: an instance of a UDP header that will be used for NAT-Traversal. @@ -69,7 +91,7 @@ index 69e7ae3b..b35da2b1 100644 """ if proto not in (ESP, AH, ESP.name, AH.name): -@@ -782,6 +794,7 @@ class SecurityAssociation(object): +@@ -782,6 +795,7 @@ class SecurityAssociation(object): self.spi = spi self.seq_num = seq_num @@ -77,7 +99,7 @@ index 69e7ae3b..b35da2b1 100644 if crypt_algo: if crypt_algo not in CRYPT_ALGOS: -@@ -827,6 +840,17 @@ class SecurityAssociation(object): +@@ -827,6 +841,17 @@ class SecurityAssociation(object): raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' % (pkt.spi, self.spi)) @@ -88,14 +110,14 @@ index 69e7ae3b..b35da2b1 100644 + upper = num >> 32 + + if self.use_esn: -+ return lower, struct.pack(">I", upper) ++ return lower, struct.pack("!I", upper) + else: + return lower, None + def _encrypt_esp(self, pkt, seq_num=None, iv=None): if iv is None: -@@ -835,7 +859,8 @@ class SecurityAssociation(object): +@@ -835,7 +860,8 @@ class SecurityAssociation(object): if len(iv) != self.crypt_algo.iv_size: raise TypeError('iv length must be %s' % self.crypt_algo.iv_size) @@ -105,7 +127,7 @@ index 69e7ae3b..b35da2b1 100644 if self.tunnel_header: tunnel = self.tunnel_header.copy() -@@ -857,7 +882,7 @@ class SecurityAssociation(object): +@@ -857,7 +883,7 @@ class SecurityAssociation(object): esp = self.crypt_algo.pad(esp) esp = self.crypt_algo.encrypt(self, esp, self.crypt_key) @@ -114,7 +136,7 @@ index 69e7ae3b..b35da2b1 100644 if self.nat_t_header: nat_t_header = self.nat_t_header.copy() -@@ -884,7 +909,8 @@ class SecurityAssociation(object): +@@ -884,7 +910,8 @@ class SecurityAssociation(object): def _encrypt_ah(self, pkt, seq_num=None): @@ -124,7 +146,7 @@ index 69e7ae3b..b35da2b1 100644 icv = b"\x00" * self.auth_algo.icv_size) if self.tunnel_header: -@@ -924,7 +950,8 @@ class SecurityAssociation(object): +@@ -924,7 +951,8 @@ class SecurityAssociation(object): else: ip_header.plen = len(ip_header.payload) + len(ah) + len(payload) @@ -134,7 +156,7 @@ index 69e7ae3b..b35da2b1 100644 # sequence number must always change, unless specified by the user if seq_num is None: -@@ -955,11 +982,12 @@ class SecurityAssociation(object): +@@ -955,11 +983,12 @@ class SecurityAssociation(object): def _decrypt_esp(self, pkt, verify=True): @@ -148,7 +170,7 @@ index 69e7ae3b..b35da2b1 100644 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key, self.crypt_algo.icv_size or -@@ -998,9 +1026,11 @@ class SecurityAssociation(object): +@@ -998,9 +1027,11 @@ class SecurityAssociation(object): def _decrypt_ah(self, pkt, verify=True): diff --git a/test/template_ipsec.py b/test/template_ipsec.py index 6854a352108..6e42ac7f9f4 100644 --- a/test/template_ipsec.py +++ b/test/template_ipsec.py @@ -42,6 +42,7 @@ class IPsecIPv4Params(object): IPSEC_API_CRYPTO_ALG_AES_CBC_128) self.crypt_algo = 'AES-CBC' # scapy name self.crypt_key = 'JPjyOWBeVEQiMe7h' + self.crypt_salt = '' self.flags = 0 self.nat_header = None @@ -77,6 +78,7 @@ class IPsecIPv6Params(object): IPSEC_API_CRYPTO_ALG_AES_CBC_128) self.crypt_algo = 'AES-CBC' # scapy name self.crypt_key = 'JPjyOWBeVEQiMe7h' + self.crypt_salt = '' self.flags = 0 self.nat_header = None @@ -87,7 +89,7 @@ def config_tun_params(p, encryption_type, tun_if): IPSEC_API_SAD_FLAG_USE_ESN)) p.scapy_tun_sa = SecurityAssociation( encryption_type, spi=p.vpp_tun_spi, - crypt_algo=p.crypt_algo, crypt_key=p.crypt_key, + crypt_algo=p.crypt_algo, crypt_key=p.crypt_key + p.crypt_salt, auth_algo=p.auth_algo, auth_key=p.auth_key, tunnel_header=ip_class_by_addr_type[p.addr_type]( src=tun_if.remote_addr[p.addr_type], @@ -96,7 +98,7 @@ def config_tun_params(p, encryption_type, tun_if): use_esn=use_esn) p.vpp_tun_sa = SecurityAssociation( encryption_type, spi=p.scapy_tun_spi, - crypt_algo=p.crypt_algo, crypt_key=p.crypt_key, + crypt_algo=p.crypt_algo, crypt_key=p.crypt_key + p.crypt_salt, auth_algo=p.auth_algo, auth_key=p.auth_key, tunnel_header=ip_class_by_addr_type[p.addr_type]( dst=tun_if.remote_addr[p.addr_type], @@ -112,7 +114,7 @@ def config_tra_params(p, encryption_type): encryption_type, spi=p.vpp_tra_spi, crypt_algo=p.crypt_algo, - crypt_key=p.crypt_key, + crypt_key=p.crypt_key + p.crypt_salt, auth_algo=p.auth_algo, auth_key=p.auth_key, nat_t_header=p.nat_header, @@ -121,7 +123,7 @@ def config_tra_params(p, encryption_type): encryption_type, spi=p.scapy_tra_spi, crypt_algo=p.crypt_algo, - crypt_key=p.crypt_key, + crypt_key=p.crypt_key + p.crypt_salt, auth_algo=p.auth_algo, auth_key=p.auth_key, nat_t_header=p.nat_header, diff --git a/test/test_ipsec_esp.py b/test/test_ipsec_esp.py index 403f0bb0b61..8d9b3ecb9e2 100644 --- a/test/test_ipsec_esp.py +++ b/test/test_ipsec_esp.py @@ -1,5 +1,6 @@ import socket import unittest +import struct from scapy.layers.ipsec import ESP from scapy.layers.inet import UDP @@ -357,23 +358,51 @@ class TestIpsecEspAll(ConfigIpsecESP, super(TestIpsecEspAll, self).tearDown() def test_crypto_algs(self): - """All engines AES-CBC-[128, 192, 256] w/ & w/o ESN""" + """All engines AES-[CBC, GCM]-[128, 192, 256] w/ & w/o ESN""" # foreach VPP crypto engine engines = ["ia32", "ipsecmb", "openssl"] # foreach crypto algorithm - algos = [{'vpp': VppEnum.vl_api_ipsec_crypto_alg_t. - IPSEC_API_CRYPTO_ALG_AES_CBC_128, - 'scapy': "AES-CBC", + algos = [{'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_GCM_128), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_NONE), + 'scapy-crypto': "AES-GCM", + 'scapy-integ': "NULL", + 'key': "JPjyOWBeVEQiMe7h", + 'salt': struct.pack("!L", 0)}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_GCM_256), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_NONE), + 'scapy-crypto': "AES-GCM", + 'scapy-integ': "NULL", + 'key': "JPjyOWBeVEQiMe7h0123456787654321", + 'salt': struct.pack("!L", 0)}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_128), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', 'key': "JPjyOWBeVEQiMe7h"}, - {'vpp': VppEnum.vl_api_ipsec_crypto_alg_t. - IPSEC_API_CRYPTO_ALG_AES_CBC_192, - 'scapy': "AES-CBC", + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_192), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', 'key': "JPjyOWBeVEQiMe7hJPjyOWBe"}, - {'vpp': VppEnum.vl_api_ipsec_crypto_alg_t. - IPSEC_API_CRYPTO_ALG_AES_CBC_256, - 'scapy': "AES-CBC", + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_256), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', 'key': "JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h"}] # with and without ESN @@ -404,9 +433,12 @@ class TestIpsecEspAll(ConfigIpsecESP, self.ipv6_params} for _, p in self.params.items(): - p.crypt_algo_vpp_id = algo['vpp'] - p.crypt_algo = algo['scapy'] + p.auth_algo_vpp_id = algo['vpp-integ'] + p.crypt_algo_vpp_id = algo['vpp-crypto'] + p.crypt_algo = algo['scapy-crypto'] + p.auth_algo = algo['scapy-integ'] p.crypt_key = algo['key'] + p.crypt_salt = algo['salt'] p.flags = p.flags | flag # @@ -421,8 +453,8 @@ class TestIpsecEspAll(ConfigIpsecESP, # self.verify_tra_basic6(count=17) self.verify_tra_basic4(count=17) - self.verify_tun_66(self.params[socket.AF_INET6], 1) - self.verify_tun_44(self.params[socket.AF_INET], 1) + self.verify_tun_66(self.params[socket.AF_INET6], 17) + self.verify_tun_44(self.params[socket.AF_INET], 17) # # remove the SPDs, SAs, etc diff --git a/test/test_ipsec_tun_if_esp.py b/test/test_ipsec_tun_if_esp.py index 9c5a15377e6..833bbd47bb3 100644 --- a/test/test_ipsec_tun_if_esp.py +++ b/test/test_ipsec_tun_if_esp.py @@ -1,6 +1,8 @@ import unittest import socket import copy +import struct + from scapy.layers.ipsec import ESP from scapy.layers.l2 import Ether, Raw, GRE from scapy.layers.inet import IP, UDP @@ -13,6 +15,7 @@ from vpp_ip_route import VppIpRoute, VppRoutePath, DpoProto from vpp_ipsec import VppIpsecSA from vpp_l2 import VppBridgeDomain, VppBridgeDomainPort from util import ppp +from vpp_papi import VppEnum class TemplateIpsec4TunIfEsp(TemplateIpsec): @@ -200,6 +203,130 @@ class TestIpsec4MultiTunIfEsp(TemplateIpsec, IpsecTun4): self.assertEqual(c['packets'], 127) +class TestIpsec4TunIfEspAll(TemplateIpsec, IpsecTun4): + """ IPsec IPv4 Tunnel interface all Algos """ + + encryption_type = ESP + tun4_encrypt_node_name = "esp4-encrypt" + tun4_decrypt_node_name = "esp4-decrypt" + + def config_network(self, p): + config_tun_params(p, self.encryption_type, self.tun_if) + + p.tun_if = VppIpsecTunInterface(self, self.pg0, p.vpp_tun_spi, + p.scapy_tun_spi, + p.crypt_algo_vpp_id, + p.crypt_key, p.crypt_key, + p.auth_algo_vpp_id, p.auth_key, + p.auth_key) + p.tun_if.add_vpp_config() + p.tun_if.admin_up() + p.tun_if.config_ip4() + self.logger.info(self.vapi.cli("sh ipsec sa 0")) + self.logger.info(self.vapi.cli("sh ipsec sa 1")) + + p.route = VppIpRoute(self, p.remote_tun_if_host, 32, + [VppRoutePath(p.tun_if.remote_ip4, + 0xffffffff)]) + p.route.add_vpp_config() + + def unconfig_network(self, p): + p.tun_if.unconfig_ip4() + p.tun_if.remove_vpp_config() + p.route.remove_vpp_config() + + def setUp(self): + super(TestIpsec4TunIfEspAll, self).setUp() + + self.tun_if = self.pg0 + + def tearDown(self): + super(TestIpsec4TunIfEspAll, self).tearDown() + + def test_tun_44(self): + """IPSEC tunnel all algos """ + + # foreach VPP crypto engine + engines = ["ia32", "ipsecmb", "openssl"] + + # foreach crypto algorithm + algos = [{'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_GCM_128), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_NONE), + 'scapy-crypto': "AES-GCM", + 'scapy-integ': "NULL", + 'key': "JPjyOWBeVEQiMe7h", + 'salt': struct.pack("!L", 0)}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_GCM_192), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_NONE), + 'scapy-crypto': "AES-GCM", + 'scapy-integ': "NULL", + 'key': "JPjyOWBeVEQiMe7hJPjyOWBe", + 'salt': struct.pack("!L", 0)}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_GCM_256), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_NONE), + 'scapy-crypto': "AES-GCM", + 'scapy-integ': "NULL", + 'key': "JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h", + 'salt': struct.pack("!L", 0)}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_128), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', + 'key': "JPjyOWBeVEQiMe7h"}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_192), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', + 'key': "JPjyOWBeVEQiMe7hJPjyOWBe"}, + {'vpp-crypto': (VppEnum.vl_api_ipsec_crypto_alg_t. + IPSEC_API_CRYPTO_ALG_AES_CBC_256), + 'vpp-integ': (VppEnum.vl_api_ipsec_integ_alg_t. + IPSEC_API_INTEG_ALG_SHA1_96), + 'scapy-crypto': "AES-CBC", + 'scapy-integ': "HMAC-SHA1-96", + 'salt': '', + 'key': "JPjyOWBeVEQiMe7hJPjyOWBeVEQiMe7h"}] + + for engine in engines: + self.vapi.cli("set crypto handler all %s" % engine) + + # + # loop through each of the algorithms + # + for algo in algos: + # with self.subTest(algo=algo['scapy']): + + p = copy.copy(self.ipv4_params) + p.auth_algo_vpp_id = algo['vpp-integ'] + p.crypt_algo_vpp_id = algo['vpp-crypto'] + p.crypt_algo = algo['scapy-crypto'] + p.auth_algo = algo['scapy-integ'] + p.crypt_key = algo['key'] + p.crypt_salt = algo['salt'] + + self.config_network(p) + + self.verify_tun_44(p, count=127) + c = p.tun_if.get_rx_stats() + self.assertEqual(c['packets'], 127) + c = p.tun_if.get_tx_stats() + self.assertEqual(c['packets'], 127) + + self.unconfig_network(p) + + class TestIpsec6MultiTunIfEsp(TemplateIpsec, IpsecTun6): """ IPsec IPv6 Multi Tunnel interface """ |