Exemple #1
0
    def test_hex(self):
        c = sources.digital_source_int_circuit(0xDEAD, 16)
        self.assertEqual([1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1], c.evaluate())

        c = sources.digital_source_int_circuit(0xBEEF, 32)
        self.assertEqual([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                          1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1], c.evaluate())
Exemple #2
0
    def test_no_padding(self):
        c = sources.digital_source_int_circuit(31, 5)
        self.assertEqual([1, 1, 1, 1, 1], c.evaluate())

        c = sources.digital_source_int_circuit(63, 6)
        self.assertEqual([1, 1, 1, 1, 1, 1], c.evaluate())

        c = sources.digital_source_int_circuit(53, 6)
        self.assertEqual([1, 1, 0, 1, 0, 1], c.evaluate())

        c = sources.digital_source_int_circuit(1, 1)
        self.assertEqual([1], c.evaluate())
Exemple #3
0
def sha1(message, rounds=80):
    """
    Runs the sha-1 block operation on a 512-bit message/chunk.
    """
    # Initial constants
    a = digital_source_int_circuit(0x67452301, 32)
    b = digital_source_int_circuit(0xEFCDAB89, 32)
    c = digital_source_int_circuit(0x98BADCFE, 32)
    d = digital_source_int_circuit(0x10325476, 32)
    e = digital_source_int_circuit(0xC3D2E1F0, 32)

    h0, h1, h2, h3, h4 = block_operation(message, a, b, c, d, e, rounds)

    # Concatenate results
    h01 = stack_circuits('h01', h0, h1)
    h012 = stack_circuits('h012', h01, h2)
    h0123 = stack_circuits('h0123', h012, h3)
    h = stack_circuits('H', h0123, h4)

    result = int(''.join(map(str, h.evaluate())), 2)
    return result, h
Exemple #4
0
def main(rounds=80):
    sys.setrecursionlimit(100000)

    a = sources.digital_source_int_circuit(0x67452301, 32)
    b = sources.digital_source_int_circuit(0xEFCDAB89, 32)
    c = sources.digital_source_int_circuit(0x98BADCFE, 32)
    d = sources.digital_source_int_circuit(0x10325476, 32)
    e = sources.digital_source_int_circuit(0xC3D2E1F0, 32)

    message_circuit = sources.digital_source_int_circuit(random.getrandbits(512), 512)

    h0, h1, h2, h3, h4 = builder.block_operation(message_circuit, a, b, c, d, e, rounds)

    # Concatenate results
    h01 = circuit.stack_circuits('h01', h0, h1)
    h012 = circuit.stack_circuits('h012', h01, h2)
    h0123 = circuit.stack_circuits('h0123', h012, h3)

    h = circuit.stack_circuits('H', h0123, h4)

    g = to_graph(message_circuit._outputs)

    # All gates/nodes that input hooks into
    # a = set()
    # for gate in message_circuit._outputs:
    #     a |= set(g.neighbors(gate))
    #     g.remove_node(gate)
    #
    # g.add_node('source')
    # for gate in a:
    #     g.add_edge('source', gate)
    #
    # g.add_node('sink')
    # for gate in h._outputs:
    #     g.add_edge(gate, 'sink')

    g.add_node('source')
    for gate in message_circuit._outputs:
        g.add_edge('source', gate, capacity=1)

    g.add_node('sink')
    for gate in h._outputs:
        g.add_edge(gate, 'sink', capacity=1)

    print '\n'
    print '---- Min-Cut on Reduced Rounds %d Rounds ----' % rounds
    print 'Number of nodes in circuit graph: %d' % len(g.nodes())
    print 'Number of edges in circuit graph: %d' % len(g.edges())
    print 'Total number of instantiated components: %d' % ComponentBase.count

    mc = nx.max_flow(g, 'source', 'sink')

    print 'Min-cut size: %d' % mc
Exemple #5
0
def block_operation(chunk, h0, h1, h2, h3, h4, rounds=80):
    """
    Returns (h0, h1, h2, h3, h4), the h-constants that result from running
    the SHA-1 algorithm on one block.
    """

    a, b, c, d, e = h0, h1, h2, h3, h4

    w = create_words(chunk, rounds)

    # Main loop here
    for i in xrange(0, rounds):
        if 0 <= i <= 19:
            # f = (b and c) or ((not b) and d)
            b_and_c = bitwise_and_circuit(32)
            connect_circuits(b, b_and_c, {x: x for x in xrange(0, 32)})
            connect_circuits(c, b_and_c, {x: x + 32 for x in xrange(0, 32)})

            not_b = bitwise_not_circuit(32)
            connect_circuits(b, not_b, {x: x for x in xrange(0, 32)})

            not_b_and_d = bitwise_and_circuit(32)
            connect_circuits(not_b, not_b_and_d, {x: x for x in xrange(0, 32)})
            connect_circuits(d, not_b_and_d, {x: x + 32 for x in xrange(0, 32)})

            f = bitwise_or_circuit(32)
            connect_circuits(b_and_c, f, {x: x for x in xrange(0, 32)})
            connect_circuits(not_b_and_d, f, {x: x + 32 for x in xrange(0, 32)})

            k = digital_source_int_circuit(0x5A827999, 32)
        elif 20 <= i <= 39:
            # f = b xor c xor d
            b_xor_c = bitwise_xor_circuit(32)
            connect_circuits(b, b_xor_c, {x: x for x in xrange(0, 32)})
            connect_circuits(c, b_xor_c, {x: x + 32 for x in xrange(0, 32)})

            f = bitwise_xor_circuit(32)
            connect_circuits(b_xor_c, f, {x: x for x in xrange(0, 32)})
            connect_circuits(d, f, {x: x + 32 for x in xrange(0, 32)})

            k = digital_source_int_circuit(0x6ED9EBA1, 32)
        elif 40 <= i <= 59:
            # f = (b and c) or (b and d) or (c and d)
            b_and_c = bitwise_and_circuit(32)
            connect_circuits(b, b_and_c, {x: x for x in xrange(0, 32)})
            connect_circuits(c, b_and_c, {x: x + 32 for x in xrange(0, 32)})

            b_and_d = bitwise_and_circuit(32)
            connect_circuits(b, b_and_d, {x: x for x in xrange(0, 32)})
            connect_circuits(d, b_and_d, {x: x + 32 for x in xrange(0, 32)})

            c_and_d = bitwise_and_circuit(32)
            connect_circuits(c, c_and_d, {x: x for x in xrange(0, 32)})
            connect_circuits(d, c_and_d, {x: x + 32 for x in xrange(0, 32)})

            bnc_or_bnd = bitwise_or_circuit(32)
            connect_circuits(b_and_c, bnc_or_bnd, {x: x for x in xrange(0, 32)})
            connect_circuits(b_and_d, bnc_or_bnd, {x: x + 32 for x in xrange(0, 32)})

            f = bitwise_or_circuit(32)
            connect_circuits(bnc_or_bnd, f, {x: x for x in xrange(0, 32)})
            connect_circuits(c_and_d, f, {x: x + 32 for x in xrange(0, 32)})

            k = digital_source_int_circuit(0x8F1BBCDC, 32)
        elif 60 <= i <= 79:
            # f = b xor c xor d
            b_xor_c = bitwise_xor_circuit(32)
            connect_circuits(b, b_xor_c, {x: x for x in xrange(0, 32)})
            connect_circuits(c, b_xor_c, {x: x + 32 for x in xrange(0, 32)})

            f = bitwise_xor_circuit(32)
            connect_circuits(b_xor_c, f, {x: x for x in xrange(0, 32)})
            connect_circuits(d, f, {x: x + 32 for x in xrange(0, 32)})

            k = digital_source_int_circuit(0xCA62C1D6, 32)
        else:
            raise Exception("Invalid word index in main loop!")

        # (a leftrotate 5) + f
        temp = ripple_adder_no_carry(32)
        connect_circuits(a, temp, {x: x - 5 for x in xrange(5, 32)})
        connect_circuits(a, temp, {x: x + 27 for x in xrange(0, 5)})
        connect_circuits(f, temp, {x: x + 32 for x in xrange(0, 32)})

        # result + e
        temp2 = ripple_adder_no_carry(32)
        connect_circuits(temp, temp2, {x: x for x in xrange(0, 32)})
        connect_circuits(e, temp2, {x: x + 32 for x in xrange(0, 32)})

        # result + k
        temp = ripple_adder_no_carry(32)
        connect_circuits(temp2, temp, {x: x for x in xrange(0, 32)})
        connect_circuits(k, temp, {x: x + 32 for x in xrange(0, 32)})

        # result + w[i]
        temp2 = ripple_adder_no_carry(32)
        connect_circuits(temp, temp2, {x: x for x in xrange(0, 32)})
        connect_circuits(w[i][0], temp2, {x: y for (x, y) in izip(w[i][1], xrange(32, 64))})

        # temp = (a leftrotate 5) + f + e + k + w[i]
        temp = temp2

        e = d
        d = c
        c = left_rotate(b, 30)
        b = a
        a = temp

    h0_add = ripple_adder_no_carry(32)
    connect_circuits(h0, h0_add, {i: i for i in xrange(0, 32)})
    connect_circuits(a, h0_add, {i: i + 32 for i in xrange(0, 32)})

    h1_add = ripple_adder_no_carry(32)
    connect_circuits(h1, h1_add, {i: i for i in xrange(0, 32)})
    connect_circuits(b, h1_add, {i: i + 32 for i in xrange(0, 32)})

    h2_add = ripple_adder_no_carry(32)
    connect_circuits(h2, h2_add, {i: i for i in xrange(0, 32)})
    connect_circuits(c, h2_add, {i: i + 32 for i in xrange(0, 32)})

    h3_add = ripple_adder_no_carry(32)
    connect_circuits(h3, h3_add, {i: i for i in xrange(0, 32)})
    connect_circuits(d, h3_add, {i: i + 32 for i in xrange(0, 32)})

    h4_add = ripple_adder_no_carry(32)
    connect_circuits(h4, h4_add, {i: i for i in xrange(0, 32)})
    connect_circuits(e, h4_add, {i: i + 32 for i in xrange(0, 32)})

    return h0_add, h1_add, h2_add, h3_add, h4_add
Exemple #6
0
    def test_with_padding(self):
        c = sources.digital_source_int_circuit(128, 16)
        self.assertEqual([0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], c.evaluate())

        c = sources.digital_source_int_circuit(77, 8)
        self.assertEqual([0, 1, 0, 0, 1, 1, 0, 1], c.evaluate())
Exemple #7
0
    def test_function(self):
        c = sources.digital_source_int_circuit(0, 1)

        self.assertEqual('dSrc', c.name)
        self.assertEqual(0, len(c._inputs))