def main():
    alice = HonestCryptographer(p, g, q=q)
    bob = HonestCryptographer(p, g, q=q)

    alice.generate_keys(subgroup=True)
    bob.generate_keys(subgroup=True)

    secret_key = find_bob_secret_key(bob)

    if secret_key == bob.secret_key:
        print("challenge 7.57 completed.")
    else:
        print("challenge 7.57 failed.")
def main():
    alice = HonestCryptographer(p, g, q=q)
    bob = HonestCryptographer(p, g, q=q)

    alice.generate_keys(subgroup=True)
    bob.generate_keys(subgroup=True)

    #### subchallenge 1 ####
    y = 7760073848032689505395005705677365876654629189298052775754597607446617558600394076764814236081991643094239886772481052254010323780165093955236429914607119

    x = brute_force_dlp(y)
    print "Brute forced DLP: %s" % x

    a, b = 0, 2**20
    x = wild_kangaroo(a, b, y, g)
    print "Pollard kangaroo method DLP (subchallenge 1): %s" % x

    #### subchallenge 2 ####
    y = 9388897478013399550694114614498790691034187453089355259602614074132918843899833277397448144245883225611726912025846772975325932794909655215329941809013733

    a, b = 0, 2**40
    x = wild_kangaroo(a, b, y, g)
    print "Pollard kangaroo method DLP (subchallenge 2): %s" % x
    assert pow(g, 359579674340, p) == y

    #### subchallenge 3 - extra info ####

    r = random_integer(2, q - 1)
    n = bob.secret_key % r
    y_, g_ = transform(bob.public_key, r, n)

    m = wild_kangaroo(0, (q - 1) / r, y_, g_)
    x = n + r * m

    if x == bob.secret_key:
        print("challenge 7.58 completed.")
    else:
        print("challenge 7.58 failed.")
Ejemplo n.º 3
0
def honest_interaction():
    p = int(
        "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024"
        "e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd"
        "3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec"
        "6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f"
        "24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361"
        "c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552"
        "bb9ed529077096966d670c354e4abc9804f1746c08ca237327fff"
        "fffffffffffff", 16)
    g = 2

    # initial phase
    alice = HonestCryptographer(p, g)
    alice.generate_keys()
    bob = HonestCryptographer(1, 1)  # inactive party with dummy params

    # 1st message
    message_type = 'p, g'
    message = (alice.p, alice.g)
    for_bob = send_A_to_B(alice, bob, message_type, message, do_nothing)

    bob.p = for_bob[0]
    bob.g = for_bob[1]
    bob.generate_keys()

    # 2nd message
    message_type = 'ACK'
    message = 'ACK'
    _ = send_B_to_A(alice, bob, message_type, message, do_nothing)
    print("Bob params: p = %s, g = %s" % (bob.p, bob.g))

    # 3rd message
    message_type = 'A'
    message = alice.public_key
    for_bob = send_A_to_B(alice, bob, message_type, message, do_nothing)
    bob.compute_shared_secret(for_bob)

    # 4th message
    message_type = 'B'
    message = bob.public_key
    for_alice = send_A_to_B(alice, bob, message_type, message, do_nothing)
    alice.compute_shared_secret(for_alice)

    # 5th message
    message_type = 'A->B message'
    message = alice.send_encrypted_message(s2b('YELLOW SUBMARINE'))
    for_bob = send_A_to_B(alice, bob, message_type, message, do_nothing)
    print(bob.receive_encrypted_message(for_bob[0], for_bob[1]))

    # 6th message
    message_type = 'B->A message'
    message = bob.send_encrypted_message(s2b('SATOSHI NAKAMOTO'))
    for_alice = send_A_to_B(alice, bob, message_type, message, do_nothing)
    print(alice.receive_encrypted_message(for_alice[0], for_alice[1]))
Ejemplo n.º 4
0
def mitm_attack_3():
    p = int(
        "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024"
        "e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd"
        "3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec"
        "6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f"
        "24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361"
        "c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552"
        "bb9ed529077096966d670c354e4abc9804f1746c08ca237327fff"
        "fffffffffffff", 16)
    g = 2

    # initial phase
    alice = HonestCryptographer(p, g)
    alice.generate_keys()
    bob = HonestCryptographer(1, 1)  # inactive party with dummy params

    # 1st message
    message_type = 'p, g'
    message = (alice.p, alice.g)
    for_bob = send_A_to_B(alice, bob, message_type, message,
                          active_adversary_g_p_minus_1)

    bob.p = for_bob[0]
    bob.g = for_bob[1]
    bob.generate_keys()

    # 2nd mesage
    message_type = 'ACK'
    message = 'ACK'
    _ = send_B_to_A(alice, bob, message_type, message,
                    active_adversary_g_p_minus_1)
    print(f"Bob params: p = {bob.p}, g = {bob.g}")

    # 3rd message
    message_type = 'A'
    message = alice.public_key
    for_bob = send_A_to_B(alice, bob, message_type, message,
                          active_adversary_g_p_minus_1)
    bob.compute_shared_secret(for_bob)
    print(f'Bob shared secret: {bob.shared_secret}')

    # 4th message
    message_type = 'B'
    message = bob.public_key
    for_alice = send_A_to_B(alice, bob, message_type, message,
                            active_adversary_g_p_minus_1)
    alice.compute_shared_secret(for_alice)
    print(f'Alice shared secret: {alice.shared_secret}')

    # 5th message
    message_type = 'A->B message'
    message = alice.send_encrypted_message(s2b('YELLOW SUBMARINE'))
    for_bob = send_A_to_B(alice, bob, message_type, message,
                          active_adversary_g_p_minus_1)
    print(
        f'\nReceived by Bob: {bob.receive_encrypted_message(for_bob[0], for_bob[1])}'
    )

    key = key = hex_string_to_bytearray(sha1(s2b('1')))[:16]
    decrypted = decrypt_aes_cbc(for_bob[0], key, for_bob[1])
    print(f'Decrypted by Mallory: {decrypted}')
    win_condition_1 = decrypted == b'YELLOW SUBMARINE'

    key = hex_string_to_bytearray(sha1(s2b(str(ADVERSARY_STATE_3['p'] -
                                               1))))[:16]
    decrypted = decrypt_aes_cbc(for_bob[0], key, for_bob[1])
    print(f'Decrypted by Mallory: {decrypted}')
    win_condition_2 = decrypted == b'YELLOW SUBMARINE'

    # 6th message
    message_type = 'B->A message'
    message = bob.send_encrypted_message(s2b('SATOSHI NAKAMOTO'))
    for_alice = send_A_to_B(alice, bob, message_type, message,
                            active_adversary_g_p_minus_1)
    print(
        f'\nReceived by Alice: {alice.receive_encrypted_message(for_alice[0], for_alice[1])}'
    )

    return win_condition_1 or win_condition_2