Beispiel #1
0
def startsearch(tool_parameters):
    """
    Starts the search tool for the given parameters
    """

    cipher_suite = {
        "craft": craft.CraftCipher(),
        "craftrtk": craftrtk.CraftCipherRTK(),
        "craftrtk1": craftrtk1.CraftCipherRTK1(),
        "craftrtk2": craftrtk2.CraftCipherRTK2(),
        "craftrtk3": craftrtk3.CraftCipherRTK3(),
        "craftlinear": craftlinear.CraftCipherLinear(),
        "ein_even": ein_even.EinEven(),
        "em_even": em_even.EmEven(),
        "eout_even": eout_even.EoutEven(),
        "ein_odd": ein_odd.EinOdd(),
        "em_odd": em_odd.EmOdd(),
        "eout_odd": eout_odd.EoutOdd(),
        "ein_even_6r": ein_even_6r.EinEven6r()
    }

    cipher = None

    if tool_parameters["cipher"] in cipher_suite:
        cipher = cipher_suite[tool_parameters["cipher"]]
    else:
        print("Cipher not supported!")
        return

    # Handle program flow
    if tool_parameters["mode"] == 0:
        search.findMinWeightCharacteristic(cipher, tool_parameters)
    elif tool_parameters["mode"] == 1:
        search.searchCharacteristics(cipher, tool_parameters)
    elif tool_parameters["mode"] == 2:
        search.findAllCharacteristics(cipher, tool_parameters)
    elif tool_parameters["mode"] == 3:
        search.findBestConstants(cipher, tool_parameters)
    elif tool_parameters["mode"] == 4:
        search.computeProbabilityOfDifferentials(cipher, tool_parameters)

    return
def startsearch(tool_parameters):
    """
    Starts the search tool for the given parameters
    """

    cipher_suite = {
        "craft": craft.CraftCipher(),
        "craftrtk": craftrtk.CraftCipherRTK(),
        "craftrtk1": craftrtk1.CraftCipherRTK1(),
        "craftrtk2": craftrtk2.CraftCipherRTK2(),
        "craftlinear": craftlinear.CraftCipherLinear(),
        "ein_tk2_7r": ein_tk2_7r.CraftCipherRTK2()
    }

    cipher = None

    if tool_parameters["cipher"] in cipher_suite:
        cipher = cipher_suite[tool_parameters["cipher"]]
    else:
        print("Cipher not supported!")
        return

    # Handle program flow
    if tool_parameters["mode"] == 0:
        search1.findMinWeightCharacteristic(cipher, tool_parameters)
    elif tool_parameters["mode"] == 1:
        search1.searchCharacteristics(cipher, tool_parameters)
    elif tool_parameters["mode"] == 2:
        search1.findAllCharacteristics(cipher, tool_parameters)
    elif tool_parameters["mode"] == 3:
        search1.findBestConstants(cipher, tool_parameters)
    elif tool_parameters["mode"] == 4:
        fileobj = open("tk1_possible_outputs_17r.txt", 'r')
        possible_inputs = fileobj.readlines()
        fileobj.close()
        fileobj = open("result_eout_tk1_9r_outof_17r.txt", "w")
        distribution = [0] * len(possible_inputs)
        tool_parameters_backup = tool_parameters.copy()

        stweight = int(tool_parameters["sweight"])
        endweight = int(tool_parameters["endweight"])
        ntrails = [[0] for i in range(len(possible_inputs))]

        for i in range(len(possible_inputs)):
            tool_parameters = tool_parameters_backup.copy()
            tool_parameters["fixedVariables"]["x0"] = possible_inputs[i][0:-1]
            print("output[%d]: %s" % (i, possible_inputs[i][0:-1]))
            fileobj.write("output[%d]: %s\n" % (i, possible_inputs[i][0:-1]))
            pr, ntrails[i] = search1.computeProbabilityOfDifferentials(
                cipher, tool_parameters)
            distribution[i] = pr
            if pr > 0.0:
                prob_disp = "Probability: 2^(" + str(math.log(pr, 2)) + ")"
                print(prob_disp)
                fileobj.write(prob_disp)
                fileobj.write("\n")
            else:
                print("Probability: 0")
                fileobj.write("Probability: 0\n")
            trails = "#Trails: %s" % ntrails[i]
            print(trails)
            fileobj.write(trails)
            fileobj.write("\n\n")
        fileobj.write("Distribution : %s\n" % distribution)
        for w in range(stweight, endweight):
            fileobj.write("#Trails of weigth %d: [" % w)
            temp = [0] * (len(possible_inputs))
            for t in range(len(possible_inputs)):
                temp[t] = str(ntrails[t][w])
            fileobj.write(", ".join(temp))
            fileobj.write("]\n")
        fileobj.close()
    return
Beispiel #3
0
def startsearch(tool_parameters):
    """
    Starts the search tool for the given parameters
    """

    cipher_suite = {
        "craft": craft.CraftCipher(),
        "craftrtk": craftrtk.CraftCipherRTK(),
        "craftrtk1": craftrtk1.CraftCipherRTK1(),
        "craftrtk2": craftrtk2.CraftCipherRTK2(),
        "craftrtk3": craftrtk3.CraftCipherRTK3(),
        "craftlinear": craftlinear.CraftCipherLinear(),
        "ein_even": ein_even.EinEven(),
        "em_even": em_even.EmEven(),
        "eout_even": eout_even.EoutEven(),
        "ein_odd": ein_odd.EinOdd(),
        "em_odd": em_odd.EmOdd(),
        "eout_odd": eout_odd.EoutOdd(),
        "ein_even_6r": ein_even_6r.EinEven6r()
    }

    cipher = None

    if tool_parameters["cipher"] in cipher_suite:
        cipher = cipher_suite[tool_parameters["cipher"]]
    else:
        print("Cipher not supported!")
        return

    # Handle program flow
    if tool_parameters["mode"] == 0:
        search1.findMinWeightCharacteristic(cipher, tool_parameters)
    elif tool_parameters["mode"] == 1:
        search1.searchCharacteristics(cipher, tool_parameters)
    elif tool_parameters["mode"] == 2:
        search1.findAllCharacteristics(cipher, tool_parameters)
    elif tool_parameters["mode"] == 3:
        search1.findBestConstants(cipher, tool_parameters)
    elif tool_parameters["mode"] == 4:
        fileobj = open("even_possible_ios.txt", 'r')
        possible_inputs = fileobj.readlines()
        fileobj.close()
        fileobj = open("result_em_even_2r.txt", "w")
        distribution = [[0 for k1 in range(len(possible_inputs))]
                        for k2 in range(len(possible_inputs))]
        tool_parameters_backup = tool_parameters.copy()
        number_of_possible_trails = 0

        stweight = int(tool_parameters["sweight"])
        endweight = int(tool_parameters["endweight"])
        ntrails = [[0 for k1 in range(len(possible_inputs))]
                   for k2 in range(len(possible_inputs))]
        len_of_possible_ios = len(possible_inputs)

        for i in range(len_of_possible_ios):
            for j in range(len_of_possible_ios):
                tool_parameters = tool_parameters_backup.copy()
                tool_parameters["fixedVariables"]["z0"] = possible_inputs[i][
                    0:-1]
                input_disp = "Input[%d]: %s" % (i, possible_inputs[i][0:-1])
                tool_parameters["fixedVariables"]["z2"] = possible_inputs[j][
                    0:-1]
                output_disp = "Output[%d]: %s" % (j, possible_inputs[j][0:-1])
                inout_disp = "%s \t %s" % (input_disp, output_disp)
                print(inout_disp)
                fileobj.write(inout_disp)
                fileobj.write("\n")
                pr, ntrails[i][j] = search1.computeProbabilityOfDifferentials(
                    cipher, tool_parameters)
                distribution[i][j] = pr
                if pr > 0.0:
                    prob_disp = "Probability: 2^(" + str(math.log(pr, 2)) + ")"
                    print(prob_disp)
                    fileobj.write(prob_disp)
                    fileobj.write("\n")
                    number_of_possible_trails += 1
                else:
                    print("Probability: 0")
                    fileobj.write("Probability: 0\n")
                trails = "#Trails: %s\n" % ntrails[i][j]
                print(trails)
                fileobj.write(trails)
                fileobj.write("\n\n")
    fileobj.write("Distribution : %s\n\n" % distribution)

    for w in range(stweight, endweight):
        row = [0 for k1 in range(len_of_possible_ios)]
        matrix_of_trails = [0 for k1 in range(len_of_possible_ios)]
        for k1 in range(len_of_possible_ios):
            for k2 in range(len_of_possible_ios):
                row[k2] = str(ntrails[k1][k2][w])
            matrix_of_trails[k1] = "[" + ", ".join(row) + "]"
        fileobj.write("#Trails of weigth %d:\n" % w)
        fileobj.write("[" + ", ".join(matrix_of_trails) + "]")
        fileobj.write("\n\n")

    fileobj.write("\nNumber of possible trails : %d out of %d" %
                  (number_of_possible_trails, 10000))
    fileobj.close()
    return