IPSEC: support GCM in ESP
[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..0c69ba1a 100644
3 --- a/scapy/layers/ipsec.py
4 +++ b/scapy/layers/ipsec.py
5 @@ -344,7 +344,8 @@ class CryptAlgo(object):
6              encryptor = cipher.encryptor()
7  
8              if self.is_aead:
9 -                aad = struct.pack('!LL', esp.spi, esp.seq)
10 +                aad = struct.pack('!LQ' if sa.use_esn else '!LL',
11 +                                  esp.spi, esp.seq)
12                  encryptor.authenticate_additional_data(aad)
13                  data = encryptor.update(data) + encryptor.finalize()
14                  data += encryptor.tag[:self.icv_size]
15 @@ -381,9 +382,9 @@ class CryptAlgo(object):
16              if self.is_aead:
17                  # Tag value check is done during the finalize method
18                  decryptor.authenticate_additional_data(
19 -                    struct.pack('!LL', esp.spi, esp.seq)
20 +                    struct.pack('!LQ' if sa.use_esn else '!LL',
21 +                                esp.spi, esp.seq)
22                  )
23 -
24              try:
25                  data = decryptor.update(data) + decryptor.finalize()
26              except InvalidTag as err:
27 @@ -518,12 +519,16 @@ class AuthAlgo(object):
28          else:
29              return self.mac(key, self.digestmod(), default_backend())
30  
31 -    def sign(self, pkt, key):
32 +    def sign(self, pkt, key, trailer=None):
33          """
34          Sign an IPsec (ESP or AH) packet with this algo.
35  
36          @param pkt:    a packet that contains a valid encrypted ESP or AH layer
37          @param key:    the authentication key, a byte string
38 +        @param trailer: additional data appended to the packet for ICV
39 +                        calculation, but not trnasmitted with the packet.
40 +                        For example, the high order bits of the exteneded
41 +                        sequence number.
42  
43          @return: the signed packet
44          """
45 @@ -534,16 +539,20 @@ class AuthAlgo(object):
46  
47          if pkt.haslayer(ESP):
48              mac.update(raw(pkt[ESP]))
49 +            if trailer:
50 +                mac.update(trailer)
51              pkt[ESP].data += mac.finalize()[:self.icv_size]
52  
53          elif pkt.haslayer(AH):
54              clone = zero_mutable_fields(pkt.copy(), sending=True)
55              mac.update(raw(clone))
56 +            if trailer:
57 +                mac.update(trailer)
58              pkt[AH].icv = mac.finalize()[:self.icv_size]
59  
60          return pkt
61  
62 -    def verify(self, pkt, key):
63 +    def verify(self, pkt, key, trailer):
64          """
65          Check that the integrity check value (icv) of a packet is valid.
66  
67 @@ -574,6 +583,8 @@ class AuthAlgo(object):
68              clone = zero_mutable_fields(pkt.copy(), sending=False)
69  
70          mac.update(raw(clone))
71 +        if trailer:
72 +            mac.update(trailer) # bytearray(4)) #raw(trailer))
73          computed_icv = mac.finalize()[:self.icv_size]
74  
75          # XXX: Cannot use mac.verify because the ICV can be truncated
76 @@ -757,7 +768,8 @@ class SecurityAssociation(object):
77      SUPPORTED_PROTOS = (IP, IPv6)
78  
79      def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
80 -                 auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None):
81 +                 auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None,
82 +                 use_esn=False):
83          """
84          @param proto: the IPsec proto to use (ESP or AH)
85          @param spi: the Security Parameters Index of this SA
86 @@ -771,6 +783,7 @@ class SecurityAssociation(object):
87                                to encapsulate the encrypted packets.
88          @param nat_t_header: an instance of a UDP header that will be used
89                               for NAT-Traversal.
90 +        @param use_esn: Use Extended Sequence Numbers
91          """
92  
93          if proto not in (ESP, AH, ESP.name, AH.name):
94 @@ -782,6 +795,7 @@ class SecurityAssociation(object):
95  
96          self.spi = spi
97          self.seq_num = seq_num
98 +        self.use_esn = use_esn
99  
100          if crypt_algo:
101              if crypt_algo not in CRYPT_ALGOS:
102 @@ -827,6 +841,17 @@ class SecurityAssociation(object):
103              raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
104                              (pkt.spi, self.spi))
105  
106 +    def build_seq_num(self, num):
107 +        # only lower order bits are  transmitted
108 +        # higher order bits are used in the ICV
109 +        lower = num & 0xffffffff
110 +        upper = num >> 32
111 +
112 +        if self.use_esn:
113 +            return lower, struct.pack("!I", upper)
114 +        else:
115 +            return lower, None
116 +
117      def _encrypt_esp(self, pkt, seq_num=None, iv=None):
118  
119          if iv is None:
120 @@ -835,7 +860,8 @@ class SecurityAssociation(object):
121              if len(iv) != self.crypt_algo.iv_size:
122                  raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
123  
124 -        esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
125 +        low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
126 +        esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
127  
128          if self.tunnel_header:
129              tunnel = self.tunnel_header.copy()
130 @@ -857,7 +883,7 @@ class SecurityAssociation(object):
131          esp = self.crypt_algo.pad(esp)
132          esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
133  
134 -        self.auth_algo.sign(esp, self.auth_key)
135 +        self.auth_algo.sign(esp, self.auth_key, high_seq_num)
136  
137          if self.nat_t_header:
138              nat_t_header = self.nat_t_header.copy()
139 @@ -884,7 +910,8 @@ class SecurityAssociation(object):
140  
141      def _encrypt_ah(self, pkt, seq_num=None):
142  
143 -        ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
144 +        low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
145 +        ah = AH(spi=self.spi, seq=low_seq_num,
146                  icv = b"\x00" * self.auth_algo.icv_size)
147  
148          if self.tunnel_header:
149 @@ -924,7 +951,8 @@ class SecurityAssociation(object):
150          else:
151              ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
152  
153 -        signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key)
154 +        signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
155 +                                         self.auth_key, high_seq_num)
156  
157          # sequence number must always change, unless specified by the user
158          if seq_num is None:
159 @@ -955,11 +983,12 @@ class SecurityAssociation(object):
160  
161      def _decrypt_esp(self, pkt, verify=True):
162  
163 +        low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
164          encrypted = pkt[ESP]
165  
166          if verify:
167              self.check_spi(pkt)
168 -            self.auth_algo.verify(encrypted, self.auth_key)
169 +            self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
170  
171          esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
172                                        self.crypt_algo.icv_size or
173 @@ -998,9 +1027,11 @@ class SecurityAssociation(object):
174  
175      def _decrypt_ah(self, pkt, verify=True):
176  
177 +        low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
178 +
179          if verify:
180              self.check_spi(pkt)
181 -            self.auth_algo.verify(pkt, self.auth_key)
182 +            self.auth_algo.verify(pkt, self.auth_key, high_seq_num)
183  
184          ah = pkt[AH]
185          payload = ah.payload