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

Method pad

scapy/layers/ipsec.py:360–393  ·  view source on GitHub ↗

Add the correct amount of padding so that the data to encrypt is exactly a multiple of the algorithm's block size. Also, make sure that the total ESP packet length is a multiple of 4 bytes. :param esp: an unencrypted _ESPPlain packet :returns:

(self, esp)

Source from the content-addressed store, hash-verified

358 )
359
360 def pad(self, esp):
361 """
362 Add the correct amount of padding so that the data to encrypt is
363 exactly a multiple of the algorithm's block size.
364
365 Also, make sure that the total ESP packet length is a multiple of 4
366 bytes.
367
368 :param esp: an unencrypted _ESPPlain packet
369
370 :returns: an unencrypted _ESPPlain packet with valid padding
371 """
372 # 2 extra bytes for padlen and nh
373 data_len = len(esp.data) + 2
374
375 # according to the RFC4303, section 2.4. Padding (for Encryption)
376 # the size of the ESP payload must be a multiple of 32 bits
377 align = _lcm(self.block_size, 4)
378
379 # pad for block size
380 esp.padlen = -data_len % align
381
382 # Still according to the RFC, the default value for padding *MUST* be an # noqa: E501
383 # array of bytes starting from 1 to padlen
384 # TODO: Handle padding function according to the encryption algo
385 esp.padding = struct.pack("B" * esp.padlen, *range(1, esp.padlen + 1))
386
387 # If the following test fails, it means that this algo does not comply
388 # with the RFC
389 payload_len = len(esp.iv) + len(esp.data) + len(esp.padding) + 2
390 if payload_len % 4 != 0:
391 raise ValueError('The size of the ESP data is not aligned to 32 bits after padding.') # noqa: E501
392
393 return esp
394
395 def encrypt(self, sa, esp, key, icv_size=None, esn_en=False, esn=0):
396 """

Callers 1

_encrypt_espMethod · 0.80

Calls 1

_lcmFunction · 0.85

Tested by

no test coverage detected