Example #1
0
def demo():
    import time
    d1 = dh.DiffieHellman(2)
    d2 = dh.DiffieHellman(2)
    start = time.time()
    print("start: ", start)
    d1_pubkey = d1.gen_public_key()
    g1 = time.time()
    print("g1: ", g1-start)
    d2_pubkey = d2.gen_public_key()
    g2 = time.time()
    print("g2: ", g2-g1)

    d1_sharedkey = d1.gen_shared_key(d2_pubkey)
    s1 = time.time()
    print("s1: ", s1-g2)

    d2_sharedkey = d2.gen_shared_key(d1_pubkey)
    s2 = time.time()
    print("s2: ", s2-s1)
    print(d1_sharedkey == d2_sharedkey)
    print(d1_pubkey)
    print(d2_pubkey)
    print(d1_sharedkey)
    print(len(d1_sharedkey))
Example #2
0
    def main(self, iCookie, rCookie, eType, hashType, authType, DHGroup,
             IDdata, flags, target, idType, sport, prevpayLen, keyLen):
        ikeneg = IKEv1Client(self.debug)
        #IKE initialization packet
        #Process transform before proposal to get payload length and then SA
        #Tranform
        if eType == "07" or int(eType) == 7 or eType == "AES":
            arrayTrans, paylenTrans = ikeneg.ikeTransform(
                eType, hashType, authType, DHGroup, "01", "00007080",
                prevpayLen, keyLen)  #static lifetime values for now
        else:
            arrayTrans, paylenTrans = ikeneg.ikeTransform(
                eType, hashType, authType, DHGroup, "01", "00007080",
                prevpayLen)
        lenTrans = len(arrayTrans)
        bytesTrans = struct.pack(("B" * lenTrans), *arrayTrans)

        #Proposal
        arrayProposal, paylenProp = ikeneg.ikeProposal(paylenTrans)
        lenProposal = len(arrayProposal)
        bytesProposal = struct.pack(("B" * lenProposal), *arrayProposal)

        #SA
        arraySA = ikeneg.ikeSA(
            lenProposal + lenTrans
        )  #SA payload length is the length of the transform and proposal payloads combined
        lenSA = len(arraySA)
        bytesSA = struct.pack(("B" * lenSA), *arraySA)

        arraySA_i = arraySA[4:] + arrayProposal + arrayTrans
        lenSA_i = len(arraySA_i)
        bytesSA_i = struct.pack(("B" * lenSA_i), *arraySA_i)
        SA_i = bytesSA_i.encode('hex')

        #Key Exchange
        ikeDH = dh.DiffieHellman(DHGroup)
        privKey = ikeDH.genPrivateKey(1024)
        pubKey = ikeDH.genPublicKey(privKey)
        hexPrivKey = '{0:x}'.format(
            privKey)  #using new formating to deal with long
        hexPubKey = '{0:x}'.format(pubKey)
        if len(hexPubKey) % 2 != 0:
            hexPubKey = "0" + hexPubKey

        if self.debug > 0:
            print "Initiator DH Private Key: %s" % privKey
            print "Initiator DH Private Key (hex): %s" % hexPrivKey
            print "Initiator DH Public Key: %s" % pubKey
            print "Initiator DH Public Key (hex): %s" % hexPubKey

        arrayKE = ikeneg.ikeKE(hexPubKey)
        lenKE = len(arrayKE)
        bytesKE = struct.pack(("B" * lenKE), *arrayKE)

        #Nonce payload
        arrayNonce, nonce = ikeneg.ikeNonce()
        lenNonce = len(arrayNonce)
        bytesNonce = struct.pack(("B" * lenNonce), *arrayNonce)

        #ID payload
        arrayID, ID_i = ikeneg.ikeID(IDdata, idType)
        lenID = len(arrayID)
        bytesID = struct.pack(("B" * lenID), *arrayID)

        #Header
        lenHDR = len(bytesSA + bytesProposal + bytesTrans + bytesKE +
                     bytesNonce + bytesID)
        arrayHDR = ikeneg.ikeHeader(
            "01", iCookie, rCookie, flags, "04", "00000000",
            lenHDR)  #exchange type statically 4 (aggressive) here
        lenHDR = len(arrayHDR)
        bytesHDR = struct.pack(("B" * lenHDR), *arrayHDR)

        #Process full packet to determine payload length for correctly formed header
        bytesIKE = bytesHDR + bytesSA + bytesProposal + bytesTrans + bytesKE + bytesNonce + bytesID
        payLen = (len(bytesIKE) + 8) / 2
        fullpayLen = hex(payLen)[2:].zfill(8)
        arraypayLen = array.array('B', fullpayLen.decode("hex"))

        lenpayLen = len(arraypayLen)
        bytespayLen = struct.pack(("B" * lenpayLen), *arraypayLen)
        bytesIKE = bytesHDR + bytesSA + bytesProposal + bytesTrans + bytesKE + bytesNonce + bytesID
        ikeneg.sendPacket(bytesIKE, target, sport, port)

        #Gather any details required for later processing of crypto etc
        dicCrypto = {
            "iCookie": iCookie,
            "rCookie": rCookie,
            "eType": eType,
            "hashType": hashType,
            "authType": authType,
            "DHGroup": DHGroup,
            "nonce_i": nonce,
            "SA_i": SA_i,
            "ID_i": ID_i,
            "privKey": privKey,
            "DHPubKey_i": hexPubKey
        }
        return dicCrypto
Example #3
0
    def main(self, iCookie, rCookie, eType, hashType, authType, DHGroup,
             IDdata, flags, target, idType, sport, keyLen):
        ikeneg = IKEv1Client(self.debug)
        #IKE initialization packet
        try:
            phase
        except:
            phase = 1
        try:
            version
        except:
            version = "10"

        #Process transform before proposal to get payload length and then SA
        #Tranform
        if eType == "07" or int(eType) == 7 or eType == "AES":
            arrayTrans = ikeneg.ikeTransform(
                eType, hashType, authType, DHGroup, "01", "00007080", "01",
                phase, "00", keyLen)  #static lifetime values for now
        else:
            arrayTrans = ikeneg.ikeTransform(eType, hashType, authType,
                                             DHGroup, "01", "00007080", "01",
                                             phase, "00")
        bytesTrans = ikeneg.packPacket(arrayTrans)

        #Proposal
        arrayProposal = ikeneg.ikeProposal(bytesTrans.encode('hex'), "00",
                                           phase)
        bytesProposal = ikeneg.packPacket(arrayProposal)

        #SA
        arraySA = ikeneg.ikeSA(
            bytesProposal.encode('hex'))  #+bytesTrans.encode('hex'))
        bytesSA = ikeneg.packPacket(arraySA)

        #Pull out the part included in crypto operations
        arraySA_i = arraySA[4:]
        SA_i = ikeneg.packPacket(arraySA_i).encode('hex')

        #Key Exchange
        ikeDH = dh.DiffieHellman(DHGroup)
        ###***need to update the below line when using alternative DH groups?
        privKey = ikeDH.genPrivateKey(1024)
        pubKey = ikeDH.genPublicKey(privKey)
        hexPrivKey = '{0:x}'.format(
            privKey)  #using new formating to deal with long
        hexPubKey = '{0:x}'.format(pubKey)
        if len(hexPubKey) % 2 != 0:
            hexPubKey = "0" + hexPubKey

        if self.debug > 0:
            print "Initiator DH Private Key: %s" % privKey
            print "Initiator DH Private Key (hex): %s" % hexPrivKey
            print "Initiator DH Public Key: %s" % pubKey
            print "Initiator DH Public Key (hex): %s" % hexPubKey

        arrayKE = ikeneg.ikeKE(hexPubKey)
        bytesKE = ikeneg.packPacket(arrayKE)

        #Nonce payload
        arrayNonce, nonce = ikeneg.ikeNonce()
        bytesNonce = ikeneg.packPacket(arrayNonce)

        #ID payload
        arrayID, ID_i = ikeneg.ikeID(
            IDdata, idType, "01f4", "11",
            "0d")  #next payload = none (0), 01f4 = port (500) 0d = vid
        bytesID = ikeneg.packPacket(arrayID)

        #VID payload (XAuth)
        arrayVID = ikeneg.ikeVID("00", "09002689dfd6b712")
        bytesVID = ikeneg.packPacket(arrayVID)

        #VID payload (DPD)
        arrayVID1 = ikeneg.ikeVID("0d", "afcad71368a1f1c96b8696fc77570100")
        bytesVID1 = ikeneg.packPacket(arrayVID1)

        #Header
        payloads = bytesSA + bytesKE + bytesNonce + bytesID + bytesVID1 + bytesVID
        arrayIKE = ikeneg.ikeHeader("01", iCookie, rCookie, version, flags,
                                    "04", "00000000", payloads.encode('hex'))
        bytesIKE = ikeneg.packPacket(arrayIKE)

        #Send packet
        ikeneg.sendPacket(bytesIKE, target, sport, port)

        #Gather any details required for later processing of crypto etc
        dicCrypto = {
            "iCookie": iCookie,
            "rCookie": rCookie,
            "eType": eType,
            "hashType": hashType,
            "authType": authType,
            "DHGroup": DHGroup,
            "nonce_i": nonce,
            "SA_i": SA_i,
            "ID_i": ID_i,
            "privKey": privKey,
            "DHPubKey_i": hexPubKey
        }
        return dicCrypto