Exemplo n.º 1
0
def encrypt(k, x):
    r = random_string(block_size)
    c = r
    for i in range(1, len(x) / block_size + 1, 1):
        w_i = add_int_to_string(r, i, block_size * 8)
        x_i = x[((i - 1) * block_size):(i * block_size)]
        c += AES(k, xor_strings(x_i, w_i))
    return c
Exemplo n.º 2
0
    def initialize(self):
        """
        Generates a new key to be used by the hash function. Called by simulator
        class.

        :return: key to be used by hash function.
        """
        self.key = random_string(self.key_len)
        return self.key
Exemplo n.º 3
0
 def initialize(self):
     """
     Initializes the game and resets the state. Called every time you would
     like to play the game again, usually by the simulator class. Resets
     key and internal storage.
     """
     self.key = random_string(self.key_len)
     self.messages = []
     self.win = False
Exemplo n.º 4
0
 def initialize(self):
     """
     This method initializes the game and generates a new key to be called
     by the corresponding simulator class.
     """
     self.key = random_string(self.key_len)
     self.count = 0
     self.messages = {}
     self.cyphers = {}
Exemplo n.º 5
0
 def initialize(self):
     """
     Initializes the game and resets the state. Called every time you would
     like to play the game again, usually by the simulator class. Resets
     key and internal storage.
     """
     self.key = random_string(self.key_len)
     self.messages = []
     self.win = False
Exemplo n.º 6
0
 def initialize(self):
     """
     This method initializes the game and generates a new key to be called
     by the corresponding simulator class.
     """
     self.key = random_string(self.key_len)
     self.count = 0
     self.messages = {}
     self.cyphers = {}
Exemplo n.º 7
0
 def initialize(self):
     """
     Initializes key to be used in encryption. Called by simulator to
     reset game state in between runs.
     """
     self.key = random_string(self.key_len)
     self.cyphers = []
     self.messages = []
     self.win = False
Exemplo n.º 8
0
def E(k, m):
    if len(m) != block_len * 4: return None

    m = split(m, block_len)

    c = [random_string(block_len)]
    t = ["\x00" * block_len]

    for i in range(4):
        c += [AES(k, xor_strings(m[i], c[i]))]
        t += [AES(k, xor_strings(m[i], t[i]))]

    return join(c), t[-1]
Exemplo n.º 9
0
def encrypt(k, m):
    if len(m) != block_size:
        return None

    m = [None] + split(m, block_size / 4)
    ce = [random_string(16)]
    cm = ["\x00" * 16]

    for i in range(1, 5):
        ce += [AES(k, xor_strings(ce[i - 1], m[i]))]
        cm += [AES(k, xor_strings(cm[i - 1], m[i]))]

    return join(ce), cm[4]
Exemplo n.º 10
0
def encrypt(k, x):
    """
    :param k: The key used to encrypt/decrypt the message
    :param x: The plaintext to be decrypted
    :return: return the encryption of plaintext x
    """
    r = random_string(block_len)
    c = r
    for i in range(1, len(x) / block_len + 1, 1):
        w_i = add_int_to_string(r, i, block_len * 8)
        x_i = x[((i - 1) * block_len): (i * block_len)]
        c += AES(k, xor_strings(x_i, w_i))
    return c
Exemplo n.º 11
0
def E(k, m):
    if len(m) != block_len * 4: return None

    m = split(m, block_len)

    c = [random_string(block_len)]
    t = ["\x00" * block_len]

    for i in range(4):
        c += [AES(k, xor_strings(m[i], c[i]))]
        t += [AES(k, xor_strings(m[i], t[i]))]

    return join(c), t[-1]
Exemplo n.º 12
0
    def initialize(self, b=None):
        """
        This method initializes the game, generates a new key, and selects a
        random world if needed.

        :param b: This is an optional parameter that allows the simulator
                  to control which world the game is in. This allows for
                  more exact simulation measurements.
        """
        self.key = random_string(self.key_len)
        if b is None:
            b = random.randrange(0, 2, 1)
        self.b = b
        self.message_pairs = []
Exemplo n.º 13
0
    def initialize(self, world=None):
        """
        This is the initialize method and is part of GamePRF as defined in the
        slides. It is called automatically by WorldSim when a game is run.

        :param world: This is an optional parameter that allows the simulator
                      to control which world the game is in. This allows for
                      more exact simulation measurements.
        :return:
        """
        self.messages = {}
        self.key = random_string(self.key_len)
        if world is None:
            world = random.randrange(0, 2, 1)
        self.world = world
    def initialize(self, world=None):
        """
        This is the initialize method and is part of GamePRF as defined in the
        slides. It is called automatically by WorldSim when a game is run.

        :param world: This is an optional parameter that allows the simulator
                      to control which world the game is in. This allows for
                      more exact simulation measurements.
        :return:
        """
        self.messages = {}
        self.key = random_string(self.key_len)
        if world is None:
            world = random.randrange(0, 2, 1)
        self.world = world
    def fn(self, m):
        """
        This is the fn oracle that is exposed to the adversary via the
        simulator. It takes in a message m of length self.block_len and
        returns either the encrypted result in the real world and the
        random result in the random world. 0 = random world, 1 = real world.

        :param m: Message adversary wants to encrypt.
        :return: Either the encrypted result in the real world or random result
                 in the random world. 
        """
        if self.world == 0:
            if m not in self.messages.keys():
                self.messages[m] = random_string(self.block_len)
            return self.messages[m]
        else:
            return self.prf(self.key, m)
Exemplo n.º 16
0
    def initialize(self, b=None):
        """
        This method initializes the game, generates a new key, and selects a
        random world if needed.

        :param b: This is an optional parameter that allows the simulator
                  to control which world the game is in. This allows for
                  more exact simulation measurements.
        """
        if self.	key_gen is None:
            self.key = random_string(self.key_len)
        else:
            self.key = self.key_gen()

        if b is None:
            b = random.randrange(0, 2, 1)
        self.b = b
        self.message_pairs = []
Exemplo n.º 17
0
    def fn(self, m):
        """
        This is the fn oracle that is exposed to the adversary via the
        simulator. It takes in a message m of length self.block_len and
        returns either the encrypted result in the real world and the
        random result in the random world. 0 = random world, 1 = real world.

        :param m: Message adversary wants to encrypt.
        :return: Either the encrypted result in the real world or random result
                 in the random world.
        """
        if len(m) is not self.block_len:
            raise ValueError("Message is of length " + str(len(m)) + \
                    " but should be " + str(self.block_len) + ".")
        if self.world == 0:
            if m not in self.messages.keys():
                self.messages[m] = random_string(self.block_len)
            return self.messages[m]
        else:
            return self.prf(self.key, m)
Exemplo n.º 18
0
State the probability achieved by your adversary and the number of oracle calls
it makes.

probability:

queries:
"""

from crypto.games.game_lr import GameLR
from crypto.simulator.lr_sim import LRSim

if __name__ == '__main__':
    g = GameLR(encrypt, 16, 16)
    s = LRSim(g, adversary)

    print "The advantage of your adversary is ~" + str(s.compute_advantage())

    key = random_string(block_size)

    for j in range(100):
        blocks = random.randrange(100)
        message = random_string(blocks * 16)
        cypher = encrypt(key, message)
        bad = False
        if message != decrypt(key, cypher):
            bad = True
    if bad:
        print "Your Decryption function is incorrect"
    else:
        print "Your Decryption function is correct"
Exemplo n.º 19
0
from crypto.games.game_lr import GameLR
from crypto.simulator.lr_sim import LRSim

from crypto.games.game_int_ctxt import GameINTCTXT
from crypto.simulator.ctxt_sim import CTXTSim

if __name__ == '__main__':
    g1 = GameLR(E, 16)
    s1 = LRSim(g1, A_1)

    g2 = GameINTCTXT(E, D, key_len)
    s2 = CTXTSim(g2, A_2)

    print "The advantage of A_1 adversary is ~" + str(s1.compute_advantage())
    print "The advantage of A_2 adversary is ~" + str(s2.compute_advantage())

    key = K()

    for j in range(100):
        blocks = random.randrange(100)
        m = random_string(blocks * block_len)
        c, t = E(key, m)
        bad = False
        if m != D(key, (c, t)):
            print[len(m), len(E(key, m)[0]), len(D(key, (c, t)))]
            bad = True
    if bad:
        print "Your Decryption function is incorrect"
    else:
        print "Your Decryption function is correct"
Exemplo n.º 20
0
def K():
    return random_string(key_len)
Exemplo n.º 21
0
from crypto.games.game_lr import GameLR
from crypto.simulator.lr_sim import LRSim

from crypto.games.game_int_ctxt import GameINTCTXT
from crypto.simulator.ctxt_sim import CTXTSim

if __name__ == "__main__":
    g1 = GameLR(E, 16)
    s1 = LRSim(g1, A_1)

    g2 = GameINTCTXT(E, D, key_len)
    s2 = CTXTSim(g2, A_2)

    print "The advantage of A_1 adversary is ~" + str(s1.compute_advantage())
    print "The advantage of A_2 adversary is ~" + str(s2.compute_advantage())

    key = K()

    for j in range(100):
        blocks = random.randrange(100)
        m = random_string(blocks * block_len)
        c, t = E(key, m)
        bad = False
        if m != D(key, (c, t)):
            print [len(m), len(E(key, m)[0]), len(D(key, (c, t)))]
            bad = True
    if bad:
        print "Your Decryption function is incorrect"
    else:
        print "Your Decryption function is correct"
Exemplo n.º 22
0
def K():
    return random_string(key_len)
Exemplo n.º 23
0
    world.
    """

    pass


"""
3. [10 points] Provide a succinct analysis justifying the claimed advantage:
--&--
[Answer here.]
"""

from crypto.games.game_lr import GameLR
from crypto.simulator.lr_sim import LRSim

if __name__ == '__main__':
    g = GameLR(encrypt, key_len)
    s = LRSim(g, A)

    print "The advantage of your adversary is ~" + str(s.compute_advantage())

    key = random_string(key_len)

    for j in range(100):
        blocks = random.randrange(100)
        message = random_string(blocks*block_len)
        cypher = encrypt(key, message)
        if message != decrypt(key, cypher):
            print "Your Decryption function is incorrect"
    print "Your Decryption function is correct"