b19112db2b7afb4f4fb0feec3a43f50786f40ef8
[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..b35da2b1 100644
3 --- a/scapy/layers/ipsec.py
4 +++ b/scapy/layers/ipsec.py
5 @@ -518,12 +518,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 @@ -534,16 +538,20 @@ class AuthAlgo(object):
24  
25          if pkt.haslayer(ESP):
26              mac.update(raw(pkt[ESP]))
27 +            if trailer:
28 +                mac.update(trailer)
29              pkt[ESP].data += mac.finalize()[:self.icv_size]
30  
31          elif pkt.haslayer(AH):
32              clone = zero_mutable_fields(pkt.copy(), sending=True)
33              mac.update(raw(clone))
34 +            if trailer:
35 +                mac.update(trailer)
36              pkt[AH].icv = mac.finalize()[:self.icv_size]
37  
38          return pkt
39  
40 -    def verify(self, pkt, key):
41 +    def verify(self, pkt, key, trailer):
42          """
43          Check that the integrity check value (icv) of a packet is valid.
44  
45 @@ -574,6 +582,8 @@ class AuthAlgo(object):
46              clone = zero_mutable_fields(pkt.copy(), sending=False)
47  
48          mac.update(raw(clone))
49 +        if trailer:
50 +            mac.update(trailer) # bytearray(4)) #raw(trailer))
51          computed_icv = mac.finalize()[:self.icv_size]
52  
53          # XXX: Cannot use mac.verify because the ICV can be truncated
54 @@ -757,7 +767,8 @@ class SecurityAssociation(object):
55      SUPPORTED_PROTOS = (IP, IPv6)
56  
57      def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
58 -                 auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None):
59 +                 auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None,
60 +                 use_esn=False):
61          """
62          @param proto: the IPsec proto to use (ESP or AH)
63          @param spi: the Security Parameters Index of this SA
64 @@ -771,6 +782,7 @@ class SecurityAssociation(object):
65                                to encapsulate the encrypted packets.
66          @param nat_t_header: an instance of a UDP header that will be used
67                               for NAT-Traversal.
68 +        @param use_esn: Use Extended Sequence Numbers
69          """
70  
71          if proto not in (ESP, AH, ESP.name, AH.name):
72 @@ -782,6 +794,7 @@ class SecurityAssociation(object):
73  
74          self.spi = spi
75          self.seq_num = seq_num
76 +        self.use_esn = use_esn
77  
78          if crypt_algo:
79              if crypt_algo not in CRYPT_ALGOS:
80 @@ -827,6 +840,17 @@ class SecurityAssociation(object):
81              raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
82                              (pkt.spi, self.spi))
83  
84 +    def build_seq_num(self, num):
85 +        # only lower order bits are  transmitted
86 +        # higher order bits are used in the ICV
87 +        lower = num & 0xffffffff
88 +        upper = num >> 32
89 +
90 +        if self.use_esn:
91 +            return lower, struct.pack(">I", upper)
92 +        else:
93 +            return lower, None
94 +
95      def _encrypt_esp(self, pkt, seq_num=None, iv=None):
96  
97          if iv is None:
98 @@ -835,7 +859,8 @@ class SecurityAssociation(object):
99              if len(iv) != self.crypt_algo.iv_size:
100                  raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
101  
102 -        esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
103 +        low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
104 +        esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
105  
106          if self.tunnel_header:
107              tunnel = self.tunnel_header.copy()
108 @@ -857,7 +882,7 @@ class SecurityAssociation(object):
109          esp = self.crypt_algo.pad(esp)
110          esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
111  
112 -        self.auth_algo.sign(esp, self.auth_key)
113 +        self.auth_algo.sign(esp, self.auth_key, high_seq_num)
114  
115          if self.nat_t_header:
116              nat_t_header = self.nat_t_header.copy()
117 @@ -884,7 +909,8 @@ class SecurityAssociation(object):
118  
119      def _encrypt_ah(self, pkt, seq_num=None):
120  
121 -        ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
122 +        low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
123 +        ah = AH(spi=self.spi, seq=low_seq_num,
124                  icv = b"\x00" * self.auth_algo.icv_size)
125  
126          if self.tunnel_header:
127 @@ -924,7 +950,8 @@ class SecurityAssociation(object):
128          else:
129              ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
130  
131 -        signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key)
132 +        signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
133 +                                         self.auth_key, high_seq_num)
134  
135          # sequence number must always change, unless specified by the user
136          if seq_num is None:
137 @@ -955,11 +982,12 @@ class SecurityAssociation(object):
138  
139      def _decrypt_esp(self, pkt, verify=True):
140  
141 +        low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
142          encrypted = pkt[ESP]
143  
144          if verify:
145              self.check_spi(pkt)
146 -            self.auth_algo.verify(encrypted, self.auth_key)
147 +            self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
148  
149          esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
150                                        self.crypt_algo.icv_size or
151 @@ -998,9 +1026,11 @@ class SecurityAssociation(object):
152  
153      def _decrypt_ah(self, pkt, verify=True):
154  
155 +        low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
156 +
157          if verify:
158              self.check_spi(pkt)
159 -            self.auth_algo.verify(pkt, self.auth_key)
160 +            self.auth_algo.verify(pkt, self.auth_key, high_seq_num)
161  
162          ah = pkt[AH]
163          payload = ah.payload