def from_byte_buffer(buffer): offset = 0 if buffer[offset] == 0x0: exponent_length = (buffer[offset + 1] << 8) | buffer[offset + 2] offset = 0x3 else: exponent_length = buffer[offset] offset = 0x1 exponent = buffer[offset:offset + exponent_length] offset += exponent_length modulus = buffer[offset:] return RSAHostID(Math.bytes_to_int(exponent), Math.bytes_to_int(modulus))
def __init__(self, curve_id, x, y): self.x = Math.int_to_bytes(x) self.y = Math.int_to_bytes(y) if curve_id == ECDSALowHostID.SECP160R1_CURVE_ID: #if self.SECP160R1_LENGTH - len(self.x) > 0: self.x = bytearray( ([0] * (ECDSALowHostID.NIST_P_384_CURVE_ID - len(self.x)))) + self.x self.y = bytearray( ([0] * (ECDSALowHostID.NIST_P_384_CURVE_ID - len(self.y)))) + self.y else: raise Exception("Unsupported curve") self.curve_id = bytearray([(curve_id >> 8) & 0xFF, curve_id & 0xFF]) self.buffer = self.curve_id + self.x + self.y
def from_byte_buffer(buffer): curve_id = (buffer[0] << 8) | buffer[1] if curve_id == ECDSALowHostID.NIST_P_256_CURVE_ID: x = buffer[ECDSALowHostID. CURVE_ID_LENGTH:ECDSALowHostID.CURVE_ID_LENGTH + ECDSALowHostID.NIST_P_256_LENGTH] y = buffer[ECDSALowHostID.CURVE_ID_LENGTH + ECDSALowHostID.NIST_P_256_LENGTH:] elif curve_id == ECDSALowHostID.NIST_P_384_CURVE_ID: x = buffer[ECDSALowHostID. CURVE_ID_LENGTH:ECDSALowHostID.CURVE_ID_LENGTH + ECDSALowHostID.NIST_P_384_CURVE_ID] y = buffer[ECDSALowHostID.CURVE_ID_LENGTH + ECDSALowHostID.NIST_P_384_CURVE_ID:] else: raise Exception("Unsupported curve") return ECDSALowHostID(curve_id, Math.bytes_to_int(x), Math.bytes_to_int(y))
def get_modulus(self): offset = 0x1 if self.buffer[0] == 0x0: exponent_length = (self.buffer[1] << 8) exponent_length |= (self.buffer[2] & 0xFF) offset = 0x3 else: exponent_length = self.buffer[0] return Math.bytes_to_int(self.buffer[offset + exponent_length:])
def __init__(self, exponent=None, modulus=None): exponent_bytes = Math.int_to_bytes(exponent) modulus_bytes = Math.int_to_bytes(modulus) exponent_length = len(exponent_bytes) self.exponent_length_field_length = 0x1 if len(exponent_bytes) > 255: self.exponent_length_field_length = 0x3 self.buffer = bytearray([0] * (self.exponent_length_field_length + \ len(exponent_bytes) + len(modulus_bytes))) offset = 0x1 if exponent > 255: self.buffer[1] = (exponent_length >> 8) & 0xFF self.buffer[2] = (exponent_length) & 0xFF offset = 0x3 else: self.buffer[0] = len(exponent_bytes) self.buffer[offset:offset + len(exponent_bytes)] = exponent_bytes offset += len(exponent_bytes) self.buffer[offset:offset + len(modulus_bytes)] = modulus_bytes
spi = urandom(4) seq = 1 for i in range(0, 100): start = time() cipher = AES256CBCCipher() hmac = SHA256HMAC(hmac_key) iv = urandom(AES256CBCCipher.BLOCK_SIZE) data = urandom(1400) padded_data = IPSec.IPSecUtils.pad(cipher.BLOCK_SIZE, list(data), 58) encrypted_data = cipher.encrypt(cipher_key, iv, bytearray(padded_data)) ip_sec_packet = IPSec.IPSecPacket() ip_sec_packet.set_spi(Math.bytes_to_int(spi)) ip_sec_packet.set_sequence(seq) ip_sec_packet.add_payload(list(iv) + list(encrypted_data)) icv = hmac.digest(bytearray(ip_sec_packet.get_byte_buffer())) ip_sec_packet.add_payload(list(icv)) # Send ESP packet to destination ipv4_packet = IPv4.IPv4Packet() ipv4_packet.set_version(IPv4.IPV4_VERSION) ipv4_packet.set_destination_address([192, 168, 0, 121]) ipv4_packet.set_source_address([192, 168, 0, 101]) ipv4_packet.set_ttl(IPv4.IPV4_DEFAULT_TTL) ipv4_packet.set_protocol(IPSec.IPSEC_PROTOCOL) ipv4_packet.set_ihl(IPv4.IPV4_IHL_NO_OPTIONS) ipv4_packet.set_payload(ip_sec_packet.get_byte_buffer())
def get_y(self): #return self.y; return Math.bytes_to_int(self.y)
def get_x(self): #return self.x; return Math.bytes_to_int(self.x)