MCPcopy Create free account
hub / github.com/secdev/scapy / encrypt

Method encrypt

scapy/layers/ipsec.py:395–443  ·  view source on GitHub ↗

Encrypt an ESP packet :param sa: the SecurityAssociation associated with the ESP packet. :param esp: an unencrypted _ESPPlain packet with valid padding :param key: the secret key used for encryption :param icv_size: the length of the icv used for integri

(self, sa, esp, key, icv_size=None, esn_en=False, esn=0)

Source from the content-addressed store, hash-verified

393 return esp
394
395 def encrypt(self, sa, esp, key, icv_size=None, esn_en=False, esn=0):
396 """
397 Encrypt an ESP packet
398
399 :param sa: the SecurityAssociation associated with the ESP packet.
400 :param esp: an unencrypted _ESPPlain packet with valid padding
401 :param key: the secret key used for encryption
402 :param icv_size: the length of the icv used for integrity check
403 :esn_en: extended sequence number enable which allows to use 64-bit
404 sequence number instead of 32-bit when using an AEAD
405 algorithm
406 :esn: extended sequence number (32 MSB)
407 :return: a valid ESP packet encrypted with this algorithm
408 """
409 if icv_size is None:
410 icv_size = self.icv_size if self.is_aead else 0
411 data = esp.data_for_encryption()
412
413 if self.cipher:
414 mode_iv = self._format_mode_iv(algo=self, sa=sa, iv=esp.iv)
415 aad = None
416 if self.is_aead:
417 if esn_en:
418 aad = struct.pack('!LLL', esp.spi, esn, esp.seq)
419 else:
420 aad = struct.pack('!LL', esp.spi, esp.seq)
421 if self.ciphers_aead_api:
422 # New API
423 if self.cipher == aead.AESCCM:
424 cipher = self.cipher(key, tag_length=icv_size)
425 else:
426 cipher = self.cipher(key)
427 if self.name == 'AES-NULL-GMAC':
428 # Special case for GMAC (rfc 4543 sect 3)
429 data = data + cipher.encrypt(mode_iv, b"", aad + esp.iv + data)
430 else:
431 data = cipher.encrypt(mode_iv, data, aad)
432 else:
433 cipher = self.new_cipher(key, mode_iv)
434 encryptor = cipher.encryptor()
435
436 if self.is_aead:
437 encryptor.authenticate_additional_data(aad)
438 data = encryptor.update(data) + encryptor.finalize()
439 data += encryptor.tag[:icv_size]
440 else:
441 data = encryptor.update(data) + encryptor.finalize()
442
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 """

Callers 1

_encrypt_espMethod · 0.45

Calls 4

new_cipherMethod · 0.95
ESPClass · 0.85
data_for_encryptionMethod · 0.80
updateMethod · 0.45

Tested by

no test coverage detected