Пример #1
0
def expand_loose(algo, db, cols, tag="md4-wangs-differentials"):
    i = 0
    dbcols = attacks.collision.load_db_tag(algo, db, tag)
    while True:
        m = models()
        m.start(tag + "-expand-r2", False)
        models.vars.write_header()
        if i == 0:
            models.generate(algo, ["h1", "h2"])
        i += 1

        attacks.collision.loose.constraints(algo, cols)
        attacks.collision.write_constraints(algo)
        attacks.collision.differentials.negate(algo, cols + dbcols)
        models.vars.write_assign(["ccollision", "cblocks", "cdifferentials"])
        m.collapse()
        m.build()
        sat = m.run(count=1)
        if not sat:
            break
        rs = m.results(algo)
        attacks.collision.insert_db_multiple(algo, db, rs, tag)
        dbcols = attacks.collision.load_db_tag(algo, db, tag)
    db.commit()
    return dbcols
Пример #2
0
    def pre_run(self):
        cache_path = self.build_cache_path()
        cache_tag = self.build_cache_tag()

        if not os.path.exists(cache_path):
            count = 0
            m = models()
            m.model_dir = self.cache_dir()
            cache_dir_path = m.model_dir + "/" + cache_tag

            if self.create_cache_dir(cache_dir_path):
                m.start(cache_tag, False)
                models.vars.write_header()
                models.generate(self.algo, ["h1", "h2"],
                                rounds=self.rounds,
                                bypass=True)

                models.vars.write_assign(
                    ["cstart", "cinput", "cintermediate", "coutput"])
                m.collapse(bc="00-combined-model.bc")

        while not os.path.exists(cache_path) or not os.path.exists(
                cache_path + "/00-combined-model.bc"):
            time.sleep(0.1)

        m = models()
        tag = self.build_tag()
        m.start(tag, False)
        base_path = m.model_dir + "/" + tag
        os.system("ln -s " + cache_path + "/00-combined-model.bc " +
                  base_path + "/00-combined-model.txt")

        cstart = models.vars.differential(self.input_fill, "h1in",
                                          self.input_margin, "h2in",
                                          self.input_margin)
        models.vars.write_clause("cstart", cstart, "50-start.txt")

        tail = "*" * self.input_margin
        cinput = models.vars.differential(tail, "h1in", 0, "h2in", 0)
        models.vars.write_range_clause("cinput", self.input_error,
                                       self.input_error, cinput,
                                       "50-input.txt")

        tail = "*" * self.output_margin
        coutput = models.vars.differential(tail, "h1out", 0, "h2out", 0)
        models.vars.write_range_clause("coutput", self.output_error,
                                       self.output_error, coutput,
                                       "50-output.txt")

        cnf_file = self.cnf_path()

        model_files = "cat " + m.model_dir + "/" + tag + "/*.txt"
        compile_model = m.bc_bin + " " + " ".join(m.bc_args)
        cmd = model_files + " | " + compile_model

        of = open(cnf_file, "w")
        oerr = open(cnf_file + ".err", "w")

        ret = subprocess.call(cmd, shell=True, stdout=of, stderr=oerr)
        return ret
Пример #3
0
def find_simplest_differential(algo, db, rounds, tag):
    algo.rounds = rounds
    m = models()
    m.remote = False

    m.start(tag, False)
    models.vars.write_header()
    models.generate(algo, ["h1", "h2"], rounds=rounds, bypass=True)
    attacks.collision.write_same_state(algo)
    attacks.collision.write_constraints(algo)
    attacks.collision.write_optional_differential(algo)
    attacks.collision.reduced.only_one_difference(algo)
    found_collisions = []
    found_differences = []

    while True:
        print(len(found_collisions))
        if len(found_collisions) > 0:
            found_differences = attacks.collision.intermediate.analyze(
                algo, found_collisions)
            print(found_differences)
            attacks.collision.intermediate.write_negated(
                algo, found_differences)
        models.vars.write_assign(
            ["ccollision", "cblocks", "cstate", "cdifferentials", "cnegated"])
        m.collapse()
        m.build()
        sat = m.run(count=1)
        if not sat:
            break
        rs = m.results(algo)
        ncols = attacks.collision.build_col_rows(algo, db, rs, tag)
        found_collisions.append(ncols[0])
    print(found_differences)
    return found_differences
Пример #4
0
def find_neighbors(algo, db, start, rounds, tag):
    r = rounds
    algo.rounds = rounds
    cols = [start.copy()]
    attacks.collision.insert_db_multiple(algo, db, cols, tag + "-original")
    wq = []
    for i in range(0, rounds):
        wq.append((i))

    jq = []

    while len(wq) > 0:
        print(wq)
        i = wq.pop(0)

        m = models()
        m.start(tag + "-r" + str(i), False)
        models.vars.write_header()
        models.generate(algo, ["h1", "h2"], rounds=r, bypass=True)
        attacks.collision.connected.loose.constraints_new_neighbor(
            algo, cols, i)
        attacks.collision.write_same_state(algo)
        attacks.collision.write_constraints(algo)
        attacks.collision.write_optional_differential(algo)
        models.vars.write_assign(
            ["cstate", "ccollision", "cblocks", "cdifferentials"])
        m.collapse()
        m.build()
        jqj = compute.perform_sat("problem.cnf",
                                  "problem.out",
                                  count=1,
                                  no_wait=True)
        jq.append((i, jqj))

        while compute.assign_work() is None or (len(wq) == 0 and len(jq) > 0):
            print("Waiting for work...")
            fj = compute.wait_job_hosts(loop_until_found=True)
            fj_status = fj[0]
            fj_job = fj[1]
            for j in range(0, len(jq)):
                jqe = jq[j]
                jqr = jqe[0]
                jqj = jqe[1]
                if jqj[0] == fj_job[0]:
                    print("Found finished job:")
                    print((fj, jqe))
                    if fj_status:
                        m = models()
                        m.start(tag + "-r" + str(jqr), False)
                        rs = m.results(algo)
                        ncols = attacks.collision.build_col_rows(
                            algo, db, rs, tag)
                        cols.extend(ncols)
                        wq.append(jqr)
                    jq.remove(jq[j])
                    break

    attacks.collision.insert_db_multiple(algo, db, cols[1:], tag)
    return cols
Пример #5
0
    def pre_run(self):
        cache_path = self.build_cache_path()
        cache_tag = self.build_cache_tag()

        if not os.path.exists(cache_path):
            count = 0
            m = models()
            m.model_dir = self.cache_dir()
            cache_dir_path = m.model_dir + "/" + cache_tag

            if self.create_cache_dir(cache_dir_path):
                m.start(cache_tag, False)
                models.vars.write_header()
                models.generate(self.algo, ['h1', 'h2'],
                                rounds=self.rounds,
                                bypass=True)
                attacks.collision.write_constraints(self.algo)
                attacks.collision.write_optional_differential(self.algo)
                attacks.collision.write_same_state(self.algo)

                models.vars.write_assign([
                    'ccollision', 'cblocks', 'cstate', 'cdifferentials',
                    'czeroes'
                ])
                m.collapse(bc="00-combined-model.bc")

        while not os.path.exists(cache_path) or not os.path.exists(
                cache_path + "/00-combined-model.bc"):
            time.sleep(0.1)

        m = models()
        tag = self.build_tag()
        m.start(tag, False)
        base_path = m.model_dir + "/" + tag
        os.system("ln -s " + cache_path + "/00-combined-model.bc " +
                  base_path + "/00-combined-model.txt")

        attacks.collision.connected.loose.distributed_new_neighbor(
            self.algo, self.base, [], [], base_path + "/07-differential.txt")

        f = open(base_path + "/42-zeroes.txt", 'w')
        f.write("czeroes := [" + str(self.zeroes) + "," + str(self.zeroes) +
                "](" +
                ','.join(map(lambda x: 'NOT(h1b' + str(x) + ')', range(512))) +
                ");")
        f.flush()
        f.close()

        if self.h1_start_state != '':
            models.vars.write_values(self.h1_start_state, 'h1s',
                                     base_path + "/01-h1-state.txt")

        if self.h2_start_state != '':
            models.vars.write_values(self.h2_start_state, 'h2s',
                                     base_path + "/01-h2-state.txt")

        return 0
Пример #6
0
    def pre_run(self):
        cache_path = self.build_cache_path()
        cache_tag = self.build_cache_tag()

        if not os.path.exists(cache_path):
            count = 0
            m = models()
            m.model_dir = self.cache_dir()
            cache_dir_path = m.model_dir + "/" + cache_tag

            if self.create_cache_dir(cache_dir_path):
                m.start(cache_tag, False)
                models.vars.write_header()
                models.generate(self.algo, ['h1', 'h2', 'h3'],
                                rounds=self.rounds,
                                bypass=True)

                models.vars.write_assign(['cinput', 'coutput'])
                m.collapse(bc="00-combined-model.bc")

        while not os.path.exists(cache_path) or not os.path.exists(
                cache_path + "/00-combined-model.bc"):
            time.sleep(0.1)

        m = models()
        tag = self.build_tag()
        m.start(tag, False)
        base_path = m.model_dir + "/" + tag
        os.system("ln -s " + cache_path + "/00-combined-model.bc " +
                  base_path + "/00-combined-model.txt")

        cinput = ['and']
        for i in range(0, 25 * self.w):
            cinput.append(('equal', 'h1in' + str(i), ('xor', 'h2in' + str(i),
                                                      'h3in' + str(i))))
        cinput = tuple(cinput)
        models.vars.write_clause('cinput', cinput, '50-input.txt')

        coutput = ['and']
        for i in range(0, 25 * self.w):
            coutput.append(('equal', 'h1out' + str(i),
                            ('xor', 'h2out' + str(i), 'h3out' + str(i))))
        coutput = ('not', tuple(coutput))
        models.vars.write_clause('coutput', coutput, '50-output.txt')

        cnf_file = self.cnf_path()

        model_files = "cat " + m.model_dir + "/" + tag + "/*.txt"
        compile_model = m.bc_bin + " " + " ".join(m.bc_args)
        cmd = model_files + " | " + compile_model

        of = open(cnf_file, 'w')
        oerr = open(cnf_file + ".err", 'w')

        ret = subprocess.call(cmd, shell=True, stdout=of, stderr=oerr)
        return ret
Пример #7
0
    def pre_run(self):
        cache_path = self.build_cache_path()
        cache_tag = self.build_cache_tag()

        if not os.path.exists(cache_path):
            count = 0
            m = models()
            m.model_dir = self.cache_dir()
            cache_dir_path = m.model_dir + "/" + cache_tag

            if self.create_cache_dir(cache_dir_path):
                m.start(cache_tag, False)
                models.vars.write_header()
                models.generate(self.algo, ['h1', 'h2'],
                                rounds=self.rounds,
                                bypass=True)
                attacks.collision.write_constraints(self.algo)
                attacks.collision.write_optional_differential(self.algo)
                attacks.collision.write_same_state(self.algo)

                models.vars.write_assign([
                    'ccollision', 'cblocks', 'cstate', 'cdifferentials',
                    'cascii'
                ])
                m.collapse(bc="00-combined-model.bc")

        while not os.path.exists(cache_path) or not os.path.exists(
                cache_path + "/00-combined-model.bc"):
            time.sleep(0.1)

        m = models()
        tag = self.build_tag()
        m.start(tag, False)
        base_path = m.model_dir + "/" + tag
        os.system("ln -s " + cache_path + "/00-combined-model.bc " +
                  base_path + "/00-combined-model.txt")

        attacks.collision.connected.loose.distributed_new_neighbor(
            self.algo, self.base, [], [], base_path + "/07-differential.txt")

        prefixes = ['h1b']
        if self.both:
            prefixes = ['h1b', 'h2b']

        attacks.collision.write_ascii_constraints(prefixes,
                                                  base_path + "/44-ascii.txt")

        if self.h1_start_state != '':
            models.vars.write_values(self.h1_start_state, 'h1s',
                                     base_path + "/01-h1-state.txt")

        if self.h2_start_state != '':
            models.vars.write_values(self.h2_start_state, 'h2s',
                                     base_path + "/01-h2-state.txt")

        return 0
Пример #8
0
def find_arbitrary_differential(algo, db, rounds, tag):
    algo.rounds = rounds
    m = models()
    m.start(tag, False)
    models.vars.write_header()
    models.generate(algo, ["h1", "h2"], rounds=rounds, bypass=True)
    attacks.collision.write_same_state(algo)
    attacks.collision.write_constraints(algo)
    attacks.collision.write_optional_differential(algo)
    models.vars.write_assign(["ccollision", "cblocks", "cstate"])
    m.collapse()
    m.build()
    m.remote = False
    m.run(count=1)
    rs = m.results(algo)
    ncols = attacks.collision.build_col_rows(algo, db, rs, tag)
    print(ncols)
Пример #9
0
def complete(algo, db, cols, tag):
    differentials = attacks.collision.differentials.analyze(algo, cols)
    i = 0
    for differential in itertools.product(*differentials):
        m = models()
        m.start(tag, False)
        models.vars.write_header()
        models.generate(algo, ["h1", "h2"])
        attacks.collision.tight.constraints(algo, cols)
        attacks.collision.write_constraints(algo)
        attacks.collision.differentials.write(algo, differential)
        models.vars.write_assign(["ccollision", "cblocks", "cdifferentials"])
        m.collapse()
        m.build()
        m.run(count=1)
        rs = m.results(algo)
        attacks.collision.insert_db_multiple(algo, db, rs, tag)
        i += 1
Пример #10
0
    def pre_run(self):
        cache_path = self.build_cache_path()
        cache_tag = self.build_cache_tag()

        if not os.path.exists(cache_path):
            count = 0
            m = models()
            m.model_dir = self.cache_dir()
            cache_dir_path = m.model_dir + "/" + cache_tag

            if self.create_cache_dir(cache_dir_path):
                m.start(cache_tag, False)
                models.vars.write_header()
                models.generate(self.algo, ['h1', 'h2', 'h3', 'h4'], rounds=self.rounds, bypass=True)
                attacks.collision.write_constraints(self.algo, prefixes=['h1', 'h2'], name="98-h1-h2-constraints.txt", out_name="ccollisionh1h2")
                attacks.collision.write_constraints(self.algo, prefixes=['h3', 'h4'], name="98-h3-h4-constraints.txt", out_name="ccollisionh3h4")
                attacks.collision.write_optional_differential(self.algo)
                attacks.collision.write_same_state(self.algo, prefixes=['h1', 'h2'], name="01-h1-h2-state.txt", out_name="cstateh1h2")
                attacks.collision.write_same_state(self.algo, prefixes=['h3', 'h4'], name="01-h3-h4-state.txt", out_name="cstateh3h4")
                attacks.collision.write_same_blocks(self.algo, prefixes=['h1', 'h3'], name="08-h1-h3-block.txt", out_name="cblocksh1h3")
                attacks.collision.write_same_blocks(self.algo, prefixes=['h2', 'h4'], name="08-h2-h4-block.txt", out_name="cblocksh2h4")

                models.vars.write_assign(['ccollisionh1h2', 'ccollisionh3h4', 'cblocks', 'cstateh1h2', 'cstateh3h4', 'cblocksh1h3', 'cblocksh2h4', 'cdifferentialsh1h2', 'cdifferentialsh3h4'])
                m.collapse(bc="00-combined-model.bc")

        while not os.path.exists(cache_path) or not os.path.exists(cache_path + "/00-combined-model.bc"):
            time.sleep(0.1)

        m = models()
        tag = self.build_tag()
        m.start(tag, False)
        base_path  = m.model_dir + "/" + tag
        os.system("ln -s " + cache_path + "/00-combined-model.bc " + base_path + "/00-combined-model.txt")

        attacks.collision.connected.loose.distributed_new_neighbor(self.algo, self.base, [], [], base_path + "/07-h1-h2-differential.txt", ['h1', 'h2'], 'cdifferentialsh1h2')
        attacks.collision.connected.loose.distributed_new_neighbor(self.algo, self.base, [], [], base_path + "/07-h3-h4-differential.txt", ['h3', 'h4'], 'cdifferentialsh3h4')

        if self.h1_start_state != '':
            models.vars.write_values(self.h1_start_state, 'h1s', base_path + "/01-h1-state.txt")

        if self.h2_start_state != '':
            models.vars.write_values(self.h2_start_state, 'h2s', base_path + "/01-h2-state.txt")

        return 0
Пример #11
0
def expand(algo, db, stuck, intermediate):
    m = models()
    for i in range(0, algo.rounds):
        # Don't write constraints for the four previous rounds
        # and four following rounds

        cols = attacks.collision.load_db_tag(algo, db, "md4-wangs-original")
        tag = "md4-wangs-intermediate-r1-" + str(i) + "-" + str(
            len(intermediate[i]))
        m.start(tag, False)
        models.vars.write_header()
        models.generate(algo, ['h1', 'h2'])
        # Differential Path
        differential = ['and']
        for j in range(0, algo.rounds):
            dlist = []
            if i == j:
                for e in intermediate[j]:
                    dlist.append([
                        e, 'h1i', j * algo.int_size, "h2i", j * algo.int_size
                    ])
                differential.append(
                    ('not', models.vars.choice_differentials(dlist)))
                continue
            elif i - 4 <= j <= i + 4:
                continue
            for e in intermediate[j]:
                dlist.append(
                    [e, 'h1i', j * algo.int_size, "h2i", j * algo.int_size])
            differential.append(models.vars.choice_differentials(dlist))

        models.vars.write_clause('cdifferentials', tuple(differential),
                                 "07-differential.txt")
        attacks.collision.write_constraints(algo)
        attacks.collision.write_optional_differential(algo)
        attacks.collision.stuck.write(algo, stuck)
        models.vars.write_assign(
            ['ccollision', 'cblocks', 'cdifferentials', 'cstuck'])
        m.collapse()
        m.build()
        m.run(count=10)
        rs = m.results(algo)
        attacks.collision.insert_db_multiple(algo, db, rs, tag)
Пример #12
0
def model(algo, model, cols, generate=False):
    models.vars.write_header()

    if type(cols) == dict:
        cols = [cols]
    assert (type(cols) == list and len(cols) > 0 and type(cols[0]) == dict)
    prefixes = ['h1', 'h2']

    if generate:
        models.generate(algo, prefixes)

    for prefix in prefixes:
        s = attacks.collision.get_state(algo, cols[0], prefix)
        models.vars.write_values(s, prefix + 's',
                                 "01-" + prefix + "-state.txt")

    attacks.collision.loose.constraints(algo, cols)
    attacks.collision.write_constraints(algo)
    attacks.collision.write_optional_differential(algo)
    models.vars.write_assign(['ccollision', 'cblocks', 'cdifferentials'])
Пример #13
0
    def pre_run(self):
        cache_path = self.build_cache_path()
        cache_tag = self.build_cache_tag()

        if not os.path.exists(cache_path):
            count = 0
            m = models()
            m.model_dir = self.cache_dir()
            cache_dir_path = m.model_dir + "/" + cache_tag

            if self.create_cache_dir(cache_dir_path):
                m.start(cache_tag, False)
                models.vars.write_header()
                models.generate(self.algo, ['h1', 'h2'],
                                rounds=self.rounds,
                                bypass=True)
                attacks.collision.write_constraints(self.algo)
                attacks.collision.write_optional_differential(self.algo)
                attacks.collision.write_same_state(self.algo)

                models.vars.write_assign([
                    'ccollision', 'cblocks', 'cstate', 'cdifferentials',
                    'cascii'
                ])
                m.collapse(bc="00-combined-model.bc")

        while not os.path.exists(cache_path) or not os.path.exists(
                cache_path + "/00-combined-model.bc"):
            time.sleep(0.1)

        m = models()
        tag = self.build_tag()
        m.start(tag, False)
        base_path = m.model_dir + "/" + tag
        os.system("ln -s " + cache_path + "/00-combined-model.bc " +
                  base_path + "/00-combined-model.txt")

        attacks.collision.connected.loose.distributed_new_neighbor(
            self.algo, self.base, self.existing, self.poses,
            base_path + "/07-differential.txt")

        if self.ascii:
            prefixes = ['h1b']
            if self.both:
                prefixes = ['h1b', 'h2b']

            attacks.collision.write_ascii_constraints(
                prefixes, base_path + "/44-ascii.txt")

        if self.h1_start_state != '':
            models.vars.write_values(self.h1_start_state, 'h1s',
                                     base_path + "/01-h1-state.txt")

        if self.h1_start_block != '':
            models.vars.write_values(self.h1_start_block, 'h1b',
                                     base_path + "/15-h1-state.txt")

        if self.h2_start_state != '':
            models.vars.write_values(self.h2_start_state, 'h2s',
                                     base_path + "/01-h2-state.txt")

        if self.h2_start_block != '':
            models.vars.write_values(self.h2_start_block, 'h2b',
                                     base_path + "/15-h2-state.txt")

        cnf = self.cnf_path()
        o_cnf = open(cnf, 'w')
        o_err = open(cnf + ".err", 'w')
        model_files = "cat " + m.model_dir + "/" + tag + "/*.txt"
        compile_model = m.bc_bin + " " + " ".join(m.bc_args)
        cmd = model_files + " | " + compile_model
        ret = subprocess.call(cmd,
                              stdin=subprocess.DEVNULL,
                              stdout=o_cnf,
                              stderr=o_err,
                              shell=True)
Пример #14
0
def find_extended(algo, db, delta_set, rounds, tag_base):
    nd = []
    wq = []
    jq = []
    found_differences = {}

    r = rounds
    algo.rounds = r

    m = models()
    tag = tag_base + "-r" + str(r)
    m.start(tag, False)
    models.vars.write_header()
    models.generate(algo, ["h1", "h2"], rounds=r, bypass=True)
    attacks.collision.write_same_state(algo)
    attacks.collision.write_constraints(algo)
    attacks.collision.write_optional_differential(algo)
    models.vars.write_assign(
        ["ccollision", "cblocks", "cstate", "cdifferentials", "cnegated"])

    print("Generating work queue...")
    for deltas in delta_set:
        for n in range(0, 1):
            wd = attacks.collision.reduced.generate_test_delta(
                deltas, n, rounds)
            print(wd)
            print(type(wd[0]))
            for e in wd:
                m = models()
                s = len(e)
                tag = (tag_base + "-r" + str(r) + "-s" + str(s) + "-e" +
                       "-".join(list(map(str, e))))
                m.start(tag, False)
                os.system("cp -r " + m.model_dir + "/" + tag_base + "-r" +
                          str(r) + "/* " + m.model_dir + "/" + tag + "/")
                attacks.collision.reduced.specified_difference(algo, e)
                wq.append((r, s, e))
                found_differences[(r, s, e)] = []
                m.collapse()
                m.build()

    print("Finished generating work queue...")
    random.shuffle(wq)
    print("Running work...")
    while len(wq) > 0:
        w = wq.pop(0)
        print("wq: " + str(len(wq)))
        print("jq: " + str(len(jq)))
        print("Handling job: " + str(w))
        r = w[0]
        s = w[1]
        e = w[2]
        algo.rounds = r
        m = models()
        tag = (tag_base + "-r" + str(r) + "-s" + str(s) + "-e" +
               "-".join(list(map(str, e))))
        m.start(tag, False)
        j = compute.perform_sat("problem.cnf",
                                "problem.out",
                                count=1,
                                no_wait=True,
                                ident=(w))
        jq.append((w, j))
        print("Done handling job.")

        while compute.assign_work() is None or (len(wq) == 0 and len(jq) > 0):
            print("Waiting for work...")
            print(len(jq))
            fj = compute.wait_job_hosts(loop_until_found=True)
            fj_status = fj[0]
            fj_job = fj[1]
            fj_w = fj_job[6]
            found = False
            for j in range(0, len(jq)):
                jqe = jq[j]
                jqw = jqe[0]
                jqj = jqe[1]
                if fj_w == jqw:
                    print("Found finished job:")
                    print(fj_w)
                    print(fj_status)
                    found = True
                    if fj_status:
                        w = fj_w
                        # wq.append(w)
                        r = fj_w[0]
                        s = fj_w[1]
                        e = fj_w[2]
                        algo.rounds = r
                        m = models()
                        tag = (tag_base + "-r" + str(r) + "-s" + str(s) +
                               "-e" + "-".join(list(map(str, e))))
                        m.start(tag, False)
                        rs = m.results(algo)
                        ncols = attacks.collision.build_col_rows(
                            algo, db, rs, tag)
                        if len(ncols) < 1:
                            continue
                        attacks.collision.insert_db_multiple(
                            algo, db, ncols, tag)
                        db.commit()
                        nfd = attacks.collision.intermediate.analyze(
                            algo, ncols)
                        if len(found_differences[w]) == 0:
                            found_differences[w] = nfd
                        else:
                            for i in range(0, len(nfd)):
                                for ele in nfd[i]:
                                    found_differences[w][i].add(ele)

                    jq.remove(jq[j])
                    break

            if not found:
                print("Did not find job..." + str(jq) + " || " + str(fj))
            print("Done waiting for work.")

    print(found_differences)
Пример #15
0
def parallel_simple_differentials(algo, db, rounds, sizes, tag_base):
    wq = []
    jq = []
    found_collisions = {}
    found_differences = {}
    print("Starting to generate work queue")
    for r in rounds:
        m = models()
        tag = tag_base + "-r" + str(r)
        m.start(tag, False)
        models.vars.write_header()
        models.generate(algo, ['h1', 'h2'], rounds=r, bypass=True)
        attacks.collision.write_same_state(algo)
        attacks.collision.write_constraints(algo)
        attacks.collision.write_optional_differential(algo)
        models.vars.write_assign(
            ['ccollision', 'cblocks', 'cstate', 'cdifferentials', 'cnegated'])
        for s in sizes:
            for e in itertools.combinations(list(range(0, r)), s):
                algo.rounds = r
                m = models()
                tag = tag_base + "-r" + str(r) + "-s" + str(
                    s) + "-e" + '-'.join(list(map(str, e)))
                m.start(tag, False)
                os.system("cp -r " + m.model_dir + "/" + tag_base + "-r" +
                          str(r) + "/* " + m.model_dir + "/" + tag + "/")
                attacks.collision.reduced.specified_difference(algo, e)
                wq.append((r, s, e))
                found_differences[(r, s, e)] = []
    print("Done generating work...")
    print("Running work...")
    random.shuffle(wq)
    while len(wq) > 0:
        w = wq.pop(0)
        print("wq: " + str(len(wq)))
        print("jq: " + str(len(jq)))
        print("Handling job: " + str(w))
        r = w[0]
        s = w[1]
        e = w[2]
        algo.rounds = r
        m = models()
        tag = tag_base + "-r" + str(r) + "-s" + str(s) + "-e" + '-'.join(
            list(map(str, e)))
        m.start(tag, False)
        if len(found_differences[w]) > 0:
            print(found_differences[w])
            attacks.collision.intermediate.write_negated(
                algo, found_differences[w])
        m.collapse()
        m.build()
        j = compute.perform_sat("problem.cnf",
                                "problem.out",
                                count=1,
                                no_wait=True,
                                ident=(w))
        jq.append((w, j))
        print("Done handling job.")

        while compute.assign_work() is None or (len(wq) == 0 and len(jq) > 0):
            print("Waiting for work...")
            fj = compute.wait_job_hosts(loop_until_found=True)
            fj_status = fj[0]
            fj_job = fj[1]
            fj_w = fj_job[6]
            found = False
            for j in range(0, len(jq)):
                jqe = jq[j]
                jqw = jqe[0]
                jqj = jqe[1]
                if fj_w == jqw:
                    print("Found finished job:")
                    found = True
                    if fj_status:
                        w = fj_w
                        print(w)
                        #wq.append(w)
                        r = fj_w[0]
                        s = fj_w[1]
                        e = fj_w[2]
                        algo.rounds = r
                        m = models()
                        tag = tag_base + "-r" + str(r) + "-s" + str(
                            s) + "-e" + '-'.join(list(map(str, e)))
                        m.start(tag, False)
                        rs = m.results(algo)
                        ncols = attacks.collision.build_col_rows(
                            algo, db, rs, tag)
                        if len(ncols) < 1:
                            continue
                        attacks.collision.insert_db_multiple(
                            algo, db, ncols, tag)
                        db.commit()
                        nfd = attacks.collision.intermediate.analyze(
                            algo, ncols)
                        if len(found_differences[w]) == 0:
                            found_differences[w] = nfd
                        else:
                            for i in range(0, len(nfd)):
                                for ele in nfd[i]:
                                    found_differences[w][i].add(ele)

                    jq.remove(jq[j])
                    break

            if not found:
                print("Did not find job..." + str(jq) + " || " + str(fj))
            print("Done waiting for work.")

    print(found_differences)
Пример #16
0
    def pre_run(self):
        cache_path = self.build_cache_path()
        cache_tag = self.build_cache_tag()

        if not os.path.exists(cache_path):
            count = 0
            m = models()
            m.model_dir = self.cache_dir()
            cache_dir_path = m.model_dir + "/" + cache_tag

            if self.create_cache_dir(cache_dir_path):
                m.start(cache_tag, False)
                models.vars.write_header()
                models.generate(self.algo, ["h1", "h2"],
                                rounds=self.rounds,
                                bypass=True)

                models.vars.write_assign(
                    ["ccollision", "cinput", "cintermediate"])
                m.collapse(bc="00-combined-model.bc")

        while not os.path.exists(cache_path) or not os.path.exists(
                cache_path + "/00-combined-model.bc"):
            time.sleep(0.1)

        m = models()
        tag = self.build_tag()
        m.start(tag, False)
        base_path = m.model_dir + "/" + tag
        os.system("ln -s " + cache_path + "/00-combined-model.bc " +
                  base_path + "/00-combined-model.txt")

        cinput = ["and"]
        for i in range(0, 25 * self.w):
            cinput.append(("not", ("equal", "h1in" + str(i), "h2in" + str(i))))
        cinput = tuple(cinput)
        models.vars.write_range_clause("cinput", self.differences[0],
                                       self.differences[0], cinput,
                                       "15-input.txt")

        ccollision = ["and"]
        for i in range(0, self.margin):
            ccollision.append(("equal", "h1out" + str(i), "h2out" + str(i)))
        ccollision = tuple(ccollision)
        models.vars.write_clause("ccollision", ccollision, "20-collision.txt")

        if len(self.rounds) > 1:
            cintermediate = ["and", ("equal", "T", "T")]
            tir = 0
            rir = 0
            pir = 0
            cir = 0
            cic = 0
            for index in range(1, len(self.rounds)):
                r = self.rounds[index - 1]
                nerr = self.differences[index]
                if r[0] == "t":
                    cni = ["and"]
                    cvar = tir
                    for i in range(0, 25 * self.w):
                        cni.append((
                            "not",
                            (
                                "equal",
                                "h1r" + str(cvar) + r[0] + str(i),
                                "h2r" + str(cvar) + r[0] + str(i),
                            ),
                        ))
                    cni = tuple(cni)
                    models.vars.write_range_clause(
                        "cintermediate" + str(cic),
                        nerr,
                        nerr,
                        cni,
                        "39-intermediate-" + str(cic) + ".txt",
                    )
                    cintermediate.append("cintermediate" + str(cic))
                    cic += 1
                    tir += 1
                elif r[0] == "r":
                    cni = ["and"]
                    cvar = rir
                    for i in range(0, 25 * self.w):
                        cni.append((
                            "not",
                            (
                                "equal",
                                "h1r" + str(cvar) + r[0] + str(i),
                                "h2r" + str(cvar) + r[0] + str(i),
                            ),
                        ))
                    cni = tuple(cni)
                    models.vars.write_range_clause(
                        "cintermediate" + str(cic),
                        nerr,
                        nerr,
                        cni,
                        "39-intermediate-" + str(cic) + ".txt",
                    )
                    cintermediate.append("cintermediate" + str(cic))
                    cic += 1
                    rir += 1
                elif r[0] == "p":
                    cni = ["and"]
                    cvar = pir
                    for i in range(0, 25 * self.w):
                        cni.append((
                            "not",
                            (
                                "equal",
                                "h1r" + str(cvar) + r[0] + str(i),
                                "h2r" + str(cvar) + r[0] + str(i),
                            ),
                        ))
                    cni = tuple(cni)
                    models.vars.write_range_clause(
                        "cintermediate" + str(cic),
                        nerr,
                        nerr,
                        cni,
                        "39-intermediate-" + str(cic) + ".txt",
                    )
                    cintermediate.append("cintermediate" + str(cic))
                    cic += 1
                    pir += 1
                elif r[0] == "c":
                    cni = ["and"]
                    cvar = cir
                    for i in range(0, 25 * self.w):
                        cni.append((
                            "not",
                            (
                                "equal",
                                "h1r" + str(cvar) + r[0] + str(i),
                                "h2r" + str(cvar) + r[0] + str(i),
                            ),
                        ))
                    cni = tuple(cni)
                    models.vars.write_range_clause(
                        "cintermediate" + str(cic),
                        nerr,
                        nerr,
                        cni,
                        "39-intermediate-" + str(cic) + ".txt",
                    )
                    cintermediate.append("cintermediate" + str(cic))
                    cic += 1
                    cir += 1
                elif r[0] == "i":
                    iir = int(r[1:])
                    cni = ["and"]
                    cvar = iir
                    for i in range(0, 25 * self.w):
                        cni.append((
                            "not",
                            (
                                "equal",
                                "h1r" + str(cvar) + r[0] + str(i),
                                "h2r" + str(cvar) + r[0] + str(i),
                            ),
                        ))
                    cni = tuple(cni)
                    models.vars.write_range_clause(
                        "cintermediate" + str(cic),
                        nerr,
                        nerr,
                        cni,
                        "39-intermediate-" + str(cic) + ".txt",
                    )
                    cintermediate.append("cintermediate" + str(cic))
                    cic += 1
                else:
                    assert "Invalid round specification" == "Should not happen."

            cintermediate = tuple(cintermediate)
            models.vars.write_clause("cintermediate", cintermediate,
                                     "40-intermediate.txt")

        cnf_file = self.cnf_path()

        model_files = "cat " + m.model_dir + "/" + tag + "/*.txt"
        compile_model = m.bc_bin + " " + " ".join(m.bc_args)
        cmd = model_files + " | " + compile_model

        of = open(cnf_file, "w")
        oerr = open(cnf_file + ".err", "w")

        ret = subprocess.call(cmd, shell=True, stdout=of, stderr=oerr)
        return ret
Пример #17
0
    def pre_run(self):
        cache_path = self.build_cache_path()
        cache_tag = self.build_cache_tag()

        if not os.path.exists(cache_path):
            count = 0
            m = models()
            m.model_dir = self.cache_dir()
            cache_dir_path = m.model_dir + "/" + cache_tag

            if self.create_cache_dir(cache_dir_path):
                m.start(cache_tag, False)
                models.vars.write_header()
                models.generate(self.algo, ['h1', 'h2'],
                                rounds=self.rounds,
                                bypass=True)
                attacks.collision.write_constraints(self.algo)
                attacks.collision.write_optional_differential(self.algo)
                attacks.collision.write_same_state(self.algo)
                if self.invalid:
                    invalid_differentials = models.vars.differentials(
                        [['.' * 32, 'h1b', 96, 'h2b', 96],
                         ['.' * 32, 'h1b', 224, 'h2b', 224],
                         ['.' * 32, 'h1b', 352, 'h2b', 352],
                         ['.' * 32, 'h1b', 480, 'h2b', 480]])
                    models.vars.write_clause('cinvalid', invalid_differentials,
                                             '23-invalid.txt')

                models.vars.write_assign([
                    'ccollision', 'cblocks', 'cstate', 'cdifferentials',
                    'cinvalid', 'cnegated', 'cspecific', 'cascii'
                ])
                m.collapse(bc="00-combined-model.bc")
        while not os.path.exists(cache_path) or not os.path.exists(
                cache_path + "/00-combined-model.bc"):
            time.sleep(0.1)

        m = models()
        tag = self.build_tag()
        m.start(tag, False)
        base_path = m.model_dir + "/" + tag
        os.system("ln -s " + cache_path + "/00-combined-model.bc " +
                  base_path + "/00-combined-model.txt")

        if self.h1_start_state != '':
            models.vars.write_values(self.h1_start_state, 'h1s',
                                     base_path + "/01-h1-state.txt")
        if self.h1_start_block != '':
            models.vars.write_values(self.h1_start_block, 'h1b',
                                     base_path + "/15-h1-state.txt")
        if self.h2_start_state != '':
            models.vars.write_values(self.h2_start_state, 'h2s',
                                     base_path + "/01-h2-state.txt")
        if self.h2_start_block != '':
            models.vars.write_values(self.h2_start_block, 'h2b',
                                     base_path + "/15-h2-state.txt")

        if self.specific != None:
            specific_differentials = models.vars.differentials(self.specific)
            models.vars.write_clause('cspecific', specific_differentials,
                                     '28-specific.txt')

        attacks.collision.reduced.specified_difference(
            self.algo, self.places, base_path + "/07-differential.txt")

        attacks.collision.write_ascii_constraints(self.ascii, 64)

        cnf = self.cnf_path()
        o_cnf = open(cnf, 'w')
        o_err = open(cnf + ".err", 'w')
        model_files = "cat " + m.model_dir + "/" + tag + "/*.txt"
        compile_model = m.bc_bin + " " + " ".join(m.bc_args)
        cmd = model_files + " | " + compile_model
        ret = subprocess.call(cmd,
                              stdin=subprocess.DEVNULL,
                              stdout=o_cnf,
                              stderr=o_err,
                              shell=True)

        if not o_cnf.closed:
            o_cnf.flush()
            o_cnf.close()

        if not o_err.closed:
            o_err.flush()
            o_err.close()

        if ret != 0:
            print("ERROR COMPILING MODEL: " + tag)
            return ret

        return 0
Пример #18
0
    def pre_run(self):
        cache_path = self.build_cache_path()
        cache_tag = self.build_cache_tag()

        if not os.path.exists(cache_path):
            count = 0
            m = models()
            m.model_dir = self.cache_dir()
            cache_dir_path = m.model_dir + "/" + cache_tag

            if self.create_cache_dir(cache_dir_path):
                m.start(cache_tag, False)
                models.vars.write_header()
                models.generate(self.algo, ["h1", "h2"],
                                rounds=self.rounds,
                                bypass=True)

                models.vars.write_assign(["cbijection"])
                m.collapse(bc="00-combined-model.bc")

        while not os.path.exists(cache_path) or not os.path.exists(
                cache_path + "/00-combined-model.bc"):
            time.sleep(0.1)

        m = models()
        tag = self.build_tag()
        m.start(tag, False)
        base_path = m.model_dir + "/" + tag
        os.system("ln -s " + cache_path + "/00-combined-model.bc " +
                  base_path + "/00-combined-model.txt")

        cinin = ["and"]
        for i in range(0, 25 * self.w):
            cinin.append(("equal", "h1in" + str(i), "h2in" + str(i)))
        cinin = ("not", tuple(cinin))
        models.vars.write_clause("cinin", cinin, "02-inin.txt")
        cinout = ["and"]
        for i in range(0, 25 * self.w):
            cinout.append(("equal", "h1out" + str(i), "h2out" + str(i)))
        cinout = tuple(cinout)
        models.vars.write_clause("cinout", cinout, "02-inout.txt")

        csurin = ["and"]
        for i in range(0, 25 * self.w):
            csurin.append(("equal", "h1in" + str(i), "h2in" + str(i)))
        csurin = tuple(csurin)
        models.vars.write_clause("csurin", csurin, "02-surin.txt")
        csurout = ["and"]
        for i in range(0, 25 * self.w):
            csurout.append(("equal", "h1out" + str(i), "h2out" + str(i)))
        csurout = ("not", tuple(csurout))
        models.vars.write_clause("csurout", csurout, "02-surout.txt")

        cinjection = ("and", "cinin", "cinout")
        csurjection = ("and", "csurin", "csurout")
        cbijection = ("or", cinjection, csurjection)
        models.vars.write_clause("cbijection", cbijection, "98-problem.txt")

        cnf_file = self.cnf_path()

        model_files = "cat " + m.model_dir + "/" + tag + "/*.txt"
        compile_model = m.bc_bin + " " + " ".join(m.bc_args)
        cmd = model_files + " | " + compile_model

        of = open(cnf_file, "w")
        oerr = open(cnf_file + ".err", "w")

        ret = subprocess.call(cmd, shell=True, stdout=of, stderr=oerr)
        return ret
Пример #19
0
    def pre_run(self):
        cache_path = self.build_cache_path()
        cache_tag = self.build_cache_tag()

        if not os.path.exists(cache_path):
            count = 0
            m = models()
            m.model_dir = self.cache_dir()
            cache_dir_path = m.model_dir + "/" + cache_tag

            if self.create_cache_dir(cache_dir_path):
                m.start(cache_tag, False)
                models.vars.write_header()
                models.generate(self.algo, ['h1', 'h2'], rounds=self.rounds, bypass=True)

                models.vars.write_assign(['ccollision', 'cinput', 'cintermediate'])
                m.collapse(bc="00-combined-model.bc")

        while not os.path.exists(cache_path) or not os.path.exists(cache_path + "/00-combined-model.bc"):
            time.sleep(0.1)

        m = models()
        tag = self.build_tag()
        m.start(tag, False)
        base_path  = m.model_dir + "/" + tag
        os.system("ln -s " + cache_path + "/00-combined-model.bc " + base_path + "/00-combined-model.txt")

        cinput = ['and']
        for i in range(0, 25*self.w):
            cinput.append(('not', ('equal', 'h1in' + str(i), 'h2in' + str(i))))
        cinput = tuple(cinput)
        models.vars.write_range_clause('cinput', self.differences[0], self.differences[0], cinput, '15-input.txt')

        ccollision = ['and']
        for i in range(0, self.margin):
            ccollision.append(('equal', 'h1out' + str(i), 'h2out' + str(i)))
        ccollision = tuple(ccollision)
        models.vars.write_clause('ccollision', ccollision, '20-collision.txt')

        if len(self.rounds) > 1:
            cintermediate = ['and', ('equal', 'T', 'T')]
            tir = 0
            rir = 0
            pir = 0
            cir = 0
            cic = 0
            for index in range(1, len(self.rounds)):
                r = self.rounds[index-1]
                nerr = self.differences[index]
                if r[0] == 't':
                    cni = ['and']
                    cvar = tir
                    for i in range(0, 25*self.w):
                        cni.append(('not', ('equal', 'h1r' + str(cvar) + r[0] + str(i), 'h2r' + str(cvar) + r[0] + str(i))))
                    cni = tuple(cni)
                    models.vars.write_range_clause('cintermediate' + str(cic), nerr, nerr, cni, '39-intermediate-' + str(cic) + '.txt')
                    cintermediate.append('cintermediate' + str(cic))
                    cic += 1
                    tir += 1
                elif r[0] == 'r':
                    cni = ['and']
                    cvar = rir
                    for i in range(0, 25*self.w):
                        cni.append(('not', ('equal', 'h1r' + str(cvar) + r[0] + str(i), 'h2r' + str(cvar) + r[0] + str(i))))
                    cni = tuple(cni)
                    models.vars.write_range_clause('cintermediate' + str(cic), nerr, nerr, cni, '39-intermediate-' + str(cic) + '.txt')
                    cintermediate.append('cintermediate' + str(cic))
                    cic += 1
                    rir += 1
                elif r[0] == 'p':
                    cni = ['and']
                    cvar = pir
                    for i in range(0, 25*self.w):
                        cni.append(('not', ('equal', 'h1r' + str(cvar) + r[0] + str(i), 'h2r' + str(cvar) + r[0] + str(i))))
                    cni = tuple(cni)
                    models.vars.write_range_clause('cintermediate' + str(cic), nerr, nerr, cni, '39-intermediate-' + str(cic) + '.txt')
                    cintermediate.append('cintermediate' + str(cic))
                    cic += 1
                    pir += 1
                elif r[0] == 'c':
                    cni = ['and']
                    cvar = cir
                    for i in range(0, 25*self.w):
                        cni.append(('not', ('equal', 'h1r' + str(cvar) + r[0] + str(i), 'h2r' + str(cvar) + r[0] + str(i))))
                    cni = tuple(cni)
                    models.vars.write_range_clause('cintermediate' + str(cic), nerr, nerr, cni, '39-intermediate-' + str(cic) + '.txt')
                    cintermediate.append('cintermediate' + str(cic))
                    cic += 1
                    cir += 1
                elif r[0] == 'i':
                    iir = int(r[1:])
                    cni = ['and']
                    cvar = iir
                    for i in range(0, 25*self.w):
                        cni.append(('not', ('equal', 'h1r' + str(cvar) + r[0] + str(i), 'h2r' + str(cvar) + r[0] + str(i))))
                    cni = tuple(cni)
                    models.vars.write_range_clause('cintermediate' + str(cic), nerr, nerr, cni, '39-intermediate-' + str(cic) + '.txt')
                    cintermediate.append('cintermediate' + str(cic))
                    cic += 1
                else:
                    assert("Invalid round specification" == "Should not happen.")

            cintermediate = tuple(cintermediate)
            models.vars.write_clause('cintermediate', cintermediate, '40-intermediate.txt')

        cnf_file = self.cnf_path()

        model_files = "cat " + m.model_dir + "/" + tag + "/*.txt"
        compile_model = m.bc_bin + " " + " ".join(m.bc_args)
        cmd = model_files + " | " + compile_model

        of = open(cnf_file, 'w')
        oerr = open(cnf_file + ".err", 'w')

        ret = subprocess.call(cmd, shell=True, stdout=of, stderr=oerr)
        return ret