Decrypt an ESP packet :param sa: the SecurityAssociation associated with the ESP packet. :param esp: an encrypted ESP packet :param key: the secret key used for encryption :param icv_size: the length of the icv used for integrity check :param esn_en:
(self, sa, esp, key, icv_size=None, esn_en=False, esn=0)
| 443 | return ESP(spi=esp.spi, seq=esp.seq, data=esp.iv + data) |
| 444 | |
| 445 | def decrypt(self, sa, esp, key, icv_size=None, esn_en=False, esn=0): |
| 446 | """ |
| 447 | Decrypt an ESP packet |
| 448 | |
| 449 | :param sa: the SecurityAssociation associated with the ESP packet. |
| 450 | :param esp: an encrypted ESP packet |
| 451 | :param key: the secret key used for encryption |
| 452 | :param icv_size: the length of the icv used for integrity check |
| 453 | :param esn_en: extended sequence number enable which allows to use |
| 454 | 64-bit sequence number instead of 32-bit when using an |
| 455 | AEAD algorithm |
| 456 | :param esn: extended sequence number (32 MSB) |
| 457 | :returns: a valid ESP packet encrypted with this algorithm |
| 458 | :raise scapy.layers.ipsec.IPSecIntegrityError: if the integrity check |
| 459 | fails with an AEAD algorithm |
| 460 | """ |
| 461 | if icv_size is None: |
| 462 | icv_size = self.icv_size if self.is_aead else 0 |
| 463 | |
| 464 | iv = esp.data[:self.iv_size] |
| 465 | data = esp.data[self.iv_size:len(esp.data) - icv_size] |
| 466 | icv = esp.data[len(esp.data) - icv_size:] |
| 467 | |
| 468 | if self.cipher: |
| 469 | mode_iv = self._format_mode_iv(sa=sa, iv=iv) |
| 470 | aad = None |
| 471 | if self.is_aead: |
| 472 | if esn_en: |
| 473 | aad = struct.pack('!LLL', esp.spi, esn, esp.seq) |
| 474 | else: |
| 475 | aad = struct.pack('!LL', esp.spi, esp.seq) |
| 476 | if self.ciphers_aead_api: |
| 477 | # New API |
| 478 | if self.cipher == aead.AESCCM: |
| 479 | cipher = self.cipher(key, tag_length=icv_size) |
| 480 | else: |
| 481 | cipher = self.cipher(key) |
| 482 | try: |
| 483 | if self.name == 'AES-NULL-GMAC': |
| 484 | # Special case for GMAC (rfc 4543 sect 3) |
| 485 | data = data + cipher.decrypt(mode_iv, icv, aad + iv + data) |
| 486 | else: |
| 487 | data = cipher.decrypt(mode_iv, data + icv, aad) |
| 488 | except InvalidTag as err: |
| 489 | raise IPSecIntegrityError(err) |
| 490 | else: |
| 491 | cipher = self.new_cipher(key, mode_iv, icv) |
| 492 | decryptor = cipher.decryptor() |
| 493 | |
| 494 | if self.is_aead: |
| 495 | # Tag value check is done during the finalize method |
| 496 | decryptor.authenticate_additional_data(aad) |
| 497 | try: |
| 498 | data = decryptor.update(data) + decryptor.finalize() |
| 499 | except InvalidTag as err: |
| 500 | raise IPSecIntegrityError(err) |
| 501 | |
| 502 | # extract padlen and nh |
no test coverage detected