ipsec: GCM, Anti-replay and ESN fixess

Type: fix

Several Fixes:
 1 - Anti-replay did not work with GCM becuase it overwrote the sequence
number in the ESP header. To fix i added the seq num to the per-packet
data so it is preserved
 2 - The high sequence number was not byte swapped during ESP encrypt.
 3 - openssl engine was the only one to return FAIL_DECRYPT for bad GCM
the others return BAD_HMAC. removed the former
 4 - improved tracing to show the low and high seq numbers
 5 - documented the anti-replay window checks
 6 - fixed scapy patch for ESN support for GCM
 7 - tests for anti-reply (w/ and w/o ESN) for each crypto algo

Change-Id: Id65d96b6d1d4dd821b2ab557e87468fff6d70e5b
Signed-off-by: Neale Ranns <nranns@cisco.com>
This commit is contained in:
Neale Ranns
2019-07-17 15:07:14 +00:00
committed by Damjan Marion
parent ae3eaacaf1
commit 6afaae156a
10 changed files with 347 additions and 127 deletions

View File

@ -169,7 +169,7 @@ openssl_ops_dec_gcm (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
else
{
n_fail++;
op->status = VNET_CRYPTO_OP_STATUS_FAIL_DECRYPT;
op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
}
}
return n_ops - n_fail;

View File

@ -235,7 +235,8 @@ dpdk_esp_decrypt_inline (vlib_main_t * vm,
}
/* anti-replay check */
if (ipsec_sa_anti_replay_check (sa0, &esp0->seq))
if (ipsec_sa_anti_replay_check
(sa0, clib_host_to_net_u32 (esp0->seq)))
{
clib_warning ("failed anti-replay check");
if (is_ip6)
@ -549,7 +550,8 @@ dpdk_esp_decrypt_post_inline (vlib_main_t * vm,
iv_size = cipher_alg->iv_len;
ipsec_sa_anti_replay_advance (sa0, esp0->seq);
ipsec_sa_anti_replay_advance (sa0,
clib_host_to_net_u32 (esp0->seq));
/* if UDP encapsulation is used adjust the address of the IP header */
if (ipsec_sa_is_set_UDP_ENCAP (sa0)

View File

@ -65,8 +65,7 @@ typedef enum
_(PENDING, "pending") \
_(COMPLETED, "completed") \
_(FAIL_NO_HANDLER, "no-handler") \
_(FAIL_BAD_HMAC, "bad-hmac") \
_(FAIL_DECRYPT, "decrypt-fail")
_(FAIL_BAD_HMAC, "bad-hmac")
typedef enum
{

View File

@ -203,7 +203,7 @@ ah_decrypt_inline (vlib_main_t * vm,
pd->seq = clib_host_to_net_u32 (ah0->seq_no);
/* anti-replay check */
if (ipsec_sa_anti_replay_check (sa0, &ah0->seq_no))
if (ipsec_sa_anti_replay_check (sa0, pd->seq))
{
b[0]->error = node->errors[AH_DECRYPT_ERROR_REPLAY];
next[0] = AH_DECRYPT_NEXT_DROP;
@ -303,7 +303,7 @@ ah_decrypt_inline (vlib_main_t * vm,
if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
{
ipsec_sa_anti_replay_advance (sa0, clib_host_to_net_u32 (pd->seq));
ipsec_sa_anti_replay_advance (sa0, pd->seq);
}
u16 ah_hdr_len = sizeof (ah_header_t) + pd->icv_size

View File

@ -67,6 +67,8 @@ static char *esp_decrypt_error_strings[] = {
typedef struct
{
u32 seq;
u32 sa_seq;
u32 sa_seq_hi;
ipsec_crypto_alg_t crypto_alg;
ipsec_integ_alg_t integ_alg;
} esp_decrypt_trace_t;
@ -79,9 +81,11 @@ format_esp_decrypt_trace (u8 * s, va_list * args)
CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
s = format (s, "esp: crypto %U integrity %U seq %u",
format_ipsec_crypto_alg, t->crypto_alg,
format_ipsec_integ_alg, t->integ_alg, t->seq);
s =
format (s,
"esp: crypto %U integrity %U pkt-seq %d sa-seq %u sa-seq-hi %u",
format_ipsec_crypto_alg, t->crypto_alg, format_ipsec_integ_alg,
t->integ_alg, t->seq, t->sa_seq, t->sa_seq_hi);
return s;
}
@ -99,12 +103,13 @@ typedef struct
u64 sa_data;
};
u32 seq;
i16 current_data;
i16 current_length;
u16 hdr_sz;
} esp_decrypt_packet_data_t;
STATIC_ASSERT_SIZEOF (esp_decrypt_packet_data_t, 2 * sizeof (u64));
STATIC_ASSERT_SIZEOF (esp_decrypt_packet_data_t, 3 * sizeof (u64));
#define ESP_ENCRYPT_PD_F_FD_TRANSPORT (1 << 2)
@ -177,6 +182,7 @@ esp_decrypt_inline (vlib_main_t * vm,
pd->current_length = b[0]->current_length;
pd->hdr_sz = pd->current_data - vnet_buffer (b[0])->l3_hdr_offset;
payload = b[0]->data + pd->current_data;
pd->seq = clib_host_to_net_u32 (((esp_header_t *) payload)->seq);
/* we need 4 extra bytes for HMAC calculation when ESN are used */
if (ipsec_sa_is_set_USE_ESN (sa0) && pd->icv_sz &&
@ -188,7 +194,7 @@ esp_decrypt_inline (vlib_main_t * vm,
}
/* anti-reply check */
if (ipsec_sa_anti_replay_check (sa0, &((esp_header_t *) payload)->seq))
if (ipsec_sa_anti_replay_check (sa0, pd->seq))
{
b[0]->error = node->errors[ESP_DECRYPT_ERROR_REPLAY];
next[0] = ESP_DECRYPT_NEXT_DROP;
@ -221,10 +227,11 @@ esp_decrypt_inline (vlib_main_t * vm,
op->len = len;
if (ipsec_sa_is_set_USE_ESN (sa0))
{
/* shift ICV for 4 bytes to insert ESN */
/* shift ICV by 4 bytes to insert ESN */
u32 seq_hi = clib_host_to_net_u32 (sa0->seq_hi);
u8 tmp[ESP_MAX_ICV_SIZE], sz = sizeof (sa0->seq_hi);
clib_memcpy_fast (tmp, payload + len, ESP_MAX_ICV_SIZE);
clib_memcpy_fast (payload + len, &sa0->seq_hi, sz);
clib_memcpy_fast (payload + len, &seq_hi, sz);
clib_memcpy_fast (payload + len + sz, tmp, ESP_MAX_ICV_SIZE);
op->len += sz;
op->digest += sz;
@ -368,9 +375,8 @@ esp_decrypt_inline (vlib_main_t * vm,
goto trace;
sa0 = vec_elt_at_index (im->sad, pd->sa_index);
u8 *payload = b[0]->data + pd->current_data;
ipsec_sa_anti_replay_advance (sa0, ((esp_header_t *) payload)->seq);
ipsec_sa_anti_replay_advance (sa0, pd->seq);
esp_footer_t *f = (esp_footer_t *) (b[0]->data + pd->current_data +
pd->current_length - sizeof (*f) -
@ -485,13 +491,14 @@ esp_decrypt_inline (vlib_main_t * vm,
if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
{
esp_decrypt_trace_t *tr;
u8 *payload = b[0]->data + pd->current_data;
tr = vlib_add_trace (vm, node, b[0], sizeof (*tr));
sa0 = pool_elt_at_index (im->sad,
vnet_buffer (b[0])->ipsec.sad_index);
tr->crypto_alg = sa0->crypto_alg;
tr->integ_alg = sa0->integ_alg;
tr->seq = clib_host_to_net_u32 (((esp_header_t *) payload)->seq);
tr->seq = pd->seq;
tr->sa_seq = sa0->last_seq;
tr->sa_seq_hi = sa0->seq_hi;
}
/* next */

View File

@ -65,6 +65,7 @@ typedef struct
u32 sa_index;
u32 spi;
u32 seq;
u32 sa_seq_hi;
u8 udp_encap;
ipsec_crypto_alg_t crypto_alg;
ipsec_integ_alg_t integ_alg;
@ -80,8 +81,9 @@ format_esp_encrypt_trace (u8 * s, va_list * args)
s =
format (s,
"esp: sa-index %d spi %u (0x%08x) seq %u crypto %U integrity %U%s",
t->sa_index, t->spi, t->spi, t->seq, format_ipsec_crypto_alg,
"esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s",
t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi,
format_ipsec_crypto_alg,
t->crypto_alg, format_ipsec_integ_alg, t->integ_alg,
t->udp_encap ? " udp-encap-enabled" : "");
return s;
@ -521,7 +523,8 @@ esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
sizeof (*tr));
tr->sa_index = sa_index0;
tr->spi = sa0->spi;
tr->seq = sa0->seq - 1;
tr->seq = sa0->seq;
tr->sa_seq_hi = sa0->seq_hi;
tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0);
tr->crypto_alg = sa0->crypto_alg;
tr->integ_alg = sa0->integ_alg;

View File

@ -19,8 +19,6 @@
#include <vnet/ip/ip.h>
#include <vnet/fib/fib_node.h>
#define IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE (64)
#define foreach_ipsec_crypto_alg \
_ (0, NONE, "none") \
_ (1, AES_CBC_128, "aes-cbc-128") \
@ -232,18 +230,34 @@ extern uword unformat_ipsec_integ_alg (unformat_input_t * input,
va_list * args);
extern uword unformat_ipsec_key (unformat_input_t * input, va_list * args);
/*
* Anti Replay definitions
*/
#define IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE (64)
#define IPSEC_SA_ANTI_REPLAY_WINDOW_MAX_INDEX (IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE-1)
/*
* sequence number less than the lower bound are outside of the window
* From RFC4303 Appendix A:
* Bl = Tl - W + 1
*/
#define IPSEC_SA_ANTI_REPLAY_WINDOW_LOWER_BOUND(_tl) (_tl - IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE + 1)
/*
* Anti replay check.
* inputs need to be in host byte order.
*/
always_inline int
ipsec_sa_anti_replay_check (ipsec_sa_t * sa, u32 * seqp)
ipsec_sa_anti_replay_check (ipsec_sa_t * sa, u32 seq)
{
u32 seq, diff, tl, th;
u32 diff, tl, th;
if ((sa->flags & IPSEC_SA_FLAG_USE_ANTI_REPLAY) == 0)
return 0;
seq = clib_net_to_host_u32 (*seqp);
if ((sa->flags & IPSEC_SA_FLAG_USE_ESN) == 0)
if (!ipsec_sa_is_set_USE_ESN (sa))
{
if (PREDICT_TRUE (seq > sa->last_seq))
return 0;
@ -261,50 +275,113 @@ ipsec_sa_anti_replay_check (ipsec_sa_t * sa, u32 * seqp)
th = sa->last_seq_hi;
diff = tl - seq;
if (PREDICT_TRUE (tl >= (IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE - 1)))
if (PREDICT_TRUE (tl >= (IPSEC_SA_ANTI_REPLAY_WINDOW_MAX_INDEX)))
{
if (seq >= (tl - IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE + 1))
/*
* the last sequence number VPP recieved is more than one
* window size greater than zero.
* Case A from RFC4303 Appendix A.
*/
if (seq < IPSEC_SA_ANTI_REPLAY_WINDOW_LOWER_BOUND (tl))
{
sa->seq_hi = th;
if (seq <= tl)
return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
else
return 0;
/*
* the received sequence number is lower than the lower bound
* of the window, this could mean either a replay packet or that
* the high sequence number has wrapped. if it decrypts corrently
* then it's the latter.
*/
sa->seq_hi = th + 1;
return 0;
}
else
{
sa->seq_hi = th + 1;
return 0;
/*
* the recieved sequence number greater than the low
* end of the window.
*/
sa->seq_hi = th;
if (seq <= tl)
/*
* The recieved seq number is within bounds of the window
* check if it's a duplicate
*/
return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
else
/*
* The received sequence number is greater than the window
* upper bound. this packet will move the window along, assuming
* it decrypts correctly.
*/
return 0;
}
}
else
{
if (seq >= (tl - IPSEC_SA_ANTI_REPLAY_WINDOW_SIZE + 1))
/*
* the last sequence number VPP recieved is within one window
* size of zero, i.e. 0 < TL < WINDOW_SIZE, the lower bound is thus a
* large sequence number.
* Note that the check below uses unsiged integer arthimetic, so the
* RHS will be a larger number.
* Case B from RFC4303 Appendix A.
*/
if (seq < IPSEC_SA_ANTI_REPLAY_WINDOW_LOWER_BOUND (tl))
{
sa->seq_hi = th - 1;
return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
/*
* the sequence number is less than the lower bound.
*/
if (seq <= tl)
{
/*
* the packet is within the window upper bound.
* check for duplicates.
*/
sa->seq_hi = th;
return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
}
else
{
/*
* the packet is less the window lower bound or greater than
* the higher bound, depending on how you look at it...
* We're assuming, given that the last sequence number received,
* TL < WINDOW_SIZE, that a largeer seq num is more likely to be
* a packet that moves the window forward, than a packet that has
* wrapped the high sequence again. If it were the latter then
* we've lost close to 2^32 packets.
*/
sa->seq_hi = th;
return 0;
}
}
else
{
sa->seq_hi = th;
if (seq <= tl)
return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
else
return 0;
/*
* the packet seq number is between the lower bound (a large nubmer)
* and MAX_SEQ_NUM. This is in the window since the window upper bound
* tl > 0.
* However, since TL is the other side of 0 to the received
* packet, the SA has moved on to a higher sequence number.
*/
sa->seq_hi = th - 1;
return (sa->replay_window & (1ULL << diff)) ? 1 : 0;
}
}
return 0;
}
/*
* Anti replay window advance
* inputs need to be in host byte order.
*/
always_inline void
ipsec_sa_anti_replay_advance (ipsec_sa_t * sa, u32 seqp)
ipsec_sa_anti_replay_advance (ipsec_sa_t * sa, u32 seq)
{
u32 pos, seq;
u32 pos;
if (PREDICT_TRUE (sa->flags & IPSEC_SA_FLAG_USE_ANTI_REPLAY) == 0)
return;
seq = clib_host_to_net_u32 (seqp);
if (PREDICT_TRUE (sa->flags & IPSEC_SA_FLAG_USE_ESN))
{
int wrap = sa->seq_hi - sa->last_seq_hi;

View File

@ -1,30 +1,30 @@
diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
index 69e7ae3b..0c69ba1a 100644
index 69e7ae3b..3a1724b2 100644
--- a/scapy/layers/ipsec.py
+++ b/scapy/layers/ipsec.py
@@ -344,7 +344,8 @@ class CryptAlgo(object):
@@ -344,8 +344,7 @@ 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)
- encryptor.authenticate_additional_data(aad)
+ encryptor.authenticate_additional_data(sa.build_aead(esp))
data = encryptor.update(data) + encryptor.finalize()
data += encryptor.tag[:self.icv_size]
@@ -381,9 +382,9 @@ class CryptAlgo(object):
else:
@@ -380,10 +379,7 @@ class CryptAlgo(object):
if self.is_aead:
# Tag value check is done during the finalize method
decryptor.authenticate_additional_data(
- decryptor.authenticate_additional_data(
- struct.pack('!LL', esp.spi, esp.seq)
+ struct.pack('!LQ' if sa.use_esn else '!LL',
+ esp.spi, esp.seq)
)
- )
-
+ decryptor.authenticate_additional_data(sa.build_aead(esp))
try:
data = decryptor.update(data) + decryptor.finalize()
except InvalidTag as err:
@@ -518,12 +519,16 @@ class AuthAlgo(object):
@@ -518,12 +514,16 @@ class AuthAlgo(object):
else:
return self.mac(key, self.digestmod(), default_backend())
@ -42,7 +42,7 @@ index 69e7ae3b..0c69ba1a 100644
@return: the signed packet
"""
@@ -534,16 +539,20 @@ class AuthAlgo(object):
@@ -534,16 +534,20 @@ class AuthAlgo(object):
if pkt.haslayer(ESP):
mac.update(raw(pkt[ESP]))
@ -64,7 +64,7 @@ index 69e7ae3b..0c69ba1a 100644
"""
Check that the integrity check value (icv) of a packet is valid.
@@ -574,6 +583,8 @@ class AuthAlgo(object):
@@ -574,6 +578,8 @@ class AuthAlgo(object):
clone = zero_mutable_fields(pkt.copy(), sending=False)
mac.update(raw(clone))
@ -73,7 +73,7 @@ index 69e7ae3b..0c69ba1a 100644
computed_icv = mac.finalize()[:self.icv_size]
# XXX: Cannot use mac.verify because the ICV can be truncated
@@ -757,7 +768,8 @@ class SecurityAssociation(object):
@@ -757,7 +763,8 @@ class SecurityAssociation(object):
SUPPORTED_PROTOS = (IP, IPv6)
def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
@ -83,7 +83,7 @@ index 69e7ae3b..0c69ba1a 100644
"""
@param proto: the IPsec proto to use (ESP or AH)
@param spi: the Security Parameters Index of this SA
@@ -771,6 +783,7 @@ class SecurityAssociation(object):
@@ -771,6 +778,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.
@ -91,7 +91,7 @@ index 69e7ae3b..0c69ba1a 100644
"""
if proto not in (ESP, AH, ESP.name, AH.name):
@@ -782,6 +795,7 @@ class SecurityAssociation(object):
@@ -782,6 +790,7 @@ class SecurityAssociation(object):
self.spi = spi
self.seq_num = seq_num
@ -99,10 +99,16 @@ index 69e7ae3b..0c69ba1a 100644
if crypt_algo:
if crypt_algo not in CRYPT_ALGOS:
@@ -827,6 +841,17 @@ class SecurityAssociation(object):
@@ -827,6 +836,23 @@ class SecurityAssociation(object):
raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
(pkt.spi, self.spi))
+ def build_aead(self, esp):
+ if self.use_esn:
+ return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq))
+ else:
+ return (struct.pack('!LL', esp.spi, esp.seq))
+
+ def build_seq_num(self, num):
+ # only lower order bits are transmitted
+ # higher order bits are used in the ICV
@ -117,7 +123,7 @@ index 69e7ae3b..0c69ba1a 100644
def _encrypt_esp(self, pkt, seq_num=None, iv=None):
if iv is None:
@@ -835,7 +860,8 @@ class SecurityAssociation(object):
@@ -835,7 +861,8 @@ class SecurityAssociation(object):
if len(iv) != self.crypt_algo.iv_size:
raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
@ -127,7 +133,7 @@ index 69e7ae3b..0c69ba1a 100644
if self.tunnel_header:
tunnel = self.tunnel_header.copy()
@@ -857,7 +883,7 @@ class SecurityAssociation(object):
@@ -857,7 +884,7 @@ class SecurityAssociation(object):
esp = self.crypt_algo.pad(esp)
esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
@ -136,7 +142,7 @@ index 69e7ae3b..0c69ba1a 100644
if self.nat_t_header:
nat_t_header = self.nat_t_header.copy()
@@ -884,7 +910,8 @@ class SecurityAssociation(object):
@@ -884,7 +911,8 @@ class SecurityAssociation(object):
def _encrypt_ah(self, pkt, seq_num=None):
@ -146,7 +152,7 @@ index 69e7ae3b..0c69ba1a 100644
icv = b"\x00" * self.auth_algo.icv_size)
if self.tunnel_header:
@@ -924,7 +951,8 @@ class SecurityAssociation(object):
@@ -924,7 +952,8 @@ class SecurityAssociation(object):
else:
ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
@ -156,7 +162,7 @@ index 69e7ae3b..0c69ba1a 100644
# sequence number must always change, unless specified by the user
if seq_num is None:
@@ -955,11 +983,12 @@ class SecurityAssociation(object):
@@ -955,11 +984,12 @@ class SecurityAssociation(object):
def _decrypt_esp(self, pkt, verify=True):
@ -170,7 +176,7 @@ index 69e7ae3b..0c69ba1a 100644
esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
self.crypt_algo.icv_size or
@@ -998,9 +1027,11 @@ class SecurityAssociation(object):
@@ -998,9 +1028,11 @@ class SecurityAssociation(object):
def _decrypt_ah(self, pkt, verify=True):

File diff suppressed because it is too large Load Diff

View File

@ -486,6 +486,14 @@ class TestIpsecEspAll(ConfigIpsecESP,
#
self.unconfig_network()
#
# reconfigure the network and SA to run the
# anti replay tests
#
self.config_network(self.params.values())
self.verify_tra_anti_replay()
self.unconfig_network()
if __name__ == '__main__':
unittest.main(testRunner=VppTestRunner)