IPSEC tests fnd fix or Extended Sequence Numbers
[vpp.git] / test / patches / scapy-2.4 / ipsec.patch
1 diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
2 index 69e7ae3b..99373466 100644
3 --- a/scapy/layers/ipsec.py
4 +++ b/scapy/layers/ipsec.py
5 @@ -518,12 +517,16 @@ class AuthAlgo(object):
6          else:
7              return self.mac(key, self.digestmod(), default_backend())
8  
9 -    def sign(self, pkt, key):
10 +    def sign(self, pkt, key, trailer=None):
11          """
12          Sign an IPsec (ESP or AH) packet with this algo.
13  
14          @param pkt:    a packet that contains a valid encrypted ESP or AH layer
15          @param key:    the authentication key, a byte string
16 +        @param trailer: additional data appended to the packet for ICV
17 +                        calculation, but not trnasmitted with the packet.
18 +                        For example, the high order bits of the exteneded
19 +                        sequence number.
20  
21          @return: the signed packet
22          """
23 @@ -539,11 +542,13 @@ class AuthAlgo(object):
24          elif pkt.haslayer(AH):
25              clone = zero_mutable_fields(pkt.copy(), sending=True)
26              mac.update(raw(clone))
27 +            if trailer:
28 +                mac.update(trailer)
29              pkt[AH].icv = mac.finalize()[:self.icv_size]
30  
31          return pkt
32  
33 -    def verify(self, pkt, key):
34 +    def verify(self, pkt, key, trailer):
35          """
36          Check that the integrity check value (icv) of a packet is valid.
37  
38 @@ -574,6 +579,8 @@ class AuthAlgo(object):
39              clone = zero_mutable_fields(pkt.copy(), sending=False)
40  
41          mac.update(raw(clone))
42 +        if trailer:
43 +            mac.update(trailer) # bytearray(4)) #raw(trailer))
44          computed_icv = mac.finalize()[:self.icv_size]
45  
46          # XXX: Cannot use mac.verify because the ICV can be truncated
47 @@ -757,7 +764,8 @@ class SecurityAssociation(object):
48      SUPPORTED_PROTOS = (IP, IPv6)
49  
50      def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
51 -                 auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None):
52 +                 auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None,
53 +                 use_esn=False):
54          """
55          @param proto: the IPsec proto to use (ESP or AH)
56          @param spi: the Security Parameters Index of this SA
57 @@ -771,6 +779,7 @@ class SecurityAssociation(object):
58                                to encapsulate the encrypted packets.
59          @param nat_t_header: an instance of a UDP header that will be used
60                               for NAT-Traversal.
61 +        @param use_esn: Use Extended Sequence Numbers
62          """
63  
64          if proto not in (ESP, AH, ESP.name, AH.name):
65 @@ -782,6 +791,7 @@ class SecurityAssociation(object):
66  
67          self.spi = spi
68          self.seq_num = seq_num
69 +        self.use_esn = use_esn
70  
71          if crypt_algo:
72              if crypt_algo not in CRYPT_ALGOS:
73 @@ -827,6 +837,17 @@ class SecurityAssociation(object):
74              raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
75                              (pkt.spi, self.spi))
76  
77 +    def build_seq_num(self, num):
78 +        # only lower order bits are  transmitted
79 +        # higher order bits are used in the ICV
80 +        lower = num & 0xffffffff
81 +        upper = num >> 32
82 +
83 +        if self.use_esn:
84 +            return lower, struct.pack(">I", upper)
85 +        else:
86 +            return lower, None
87 +
88      def _encrypt_esp(self, pkt, seq_num=None, iv=None):
89  
90          if iv is None:
91 @@ -835,7 +856,8 @@ class SecurityAssociation(object):
92              if len(iv) != self.crypt_algo.iv_size:
93                  raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
94  
95 -        esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
96 +        low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
97 +        esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
98  
99          if self.tunnel_header:
100              tunnel = self.tunnel_header.copy()
101 @@ -857,7 +879,7 @@ class SecurityAssociation(object):
102          esp = self.crypt_algo.pad(esp)
103          esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
104  
105 -        self.auth_algo.sign(esp, self.auth_key)
106 +        self.auth_algo.sign(esp, self.auth_key, high_seq_num)
107  
108          if self.nat_t_header:
109              nat_t_header = self.nat_t_header.copy()
110 @@ -884,7 +906,8 @@ class SecurityAssociation(object):
111  
112      def _encrypt_ah(self, pkt, seq_num=None):
113  
114 -        ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
115 +        low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
116 +        ah = AH(spi=self.spi, seq=low_seq_num,
117                  icv = b"\x00" * self.auth_algo.icv_size)
118  
119          if self.tunnel_header:
120 @@ -924,7 +947,8 @@ class SecurityAssociation(object):
121          else:
122              ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
123  
124 -        signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key)
125 +        signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
126 +                                         self.auth_key, high_seq_num)
127  
128          # sequence number must always change, unless specified by the user
129          if seq_num is None:
130 @@ -955,11 +979,12 @@ class SecurityAssociation(object):
131  
132      def _decrypt_esp(self, pkt, verify=True):
133  
134 +        low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
135          encrypted = pkt[ESP]
136  
137          if verify:
138              self.check_spi(pkt)
139 -            self.auth_algo.verify(encrypted, self.auth_key)
140 +            self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
141  
142          esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
143                                        self.crypt_algo.icv_size or
144 @@ -998,9 +1023,11 @@ class SecurityAssociation(object):
145  
146      def _decrypt_ah(self, pkt, verify=True):
147  
148 +        low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
149 +
150          if verify:
151              self.check_spi(pkt)
152 -            self.auth_algo.verify(pkt, self.auth_key)
153 +            self.auth_algo.verify(pkt, self.auth_key, high_seq_num)
154  
155          ah = pkt[AH]
156          payload = ah.payload