Exemplo n.º 1
0
def generate(pwd, stat):
    config.generate_r()
    pwd = mpz(crypt.get_bit_str_from_byte(pwd), base=2)
    global table
    table = []  # clear the table
    for i in xrange(config.max_features):
        x_0 = crypt.p(mpz((i + 1) << 1), config.r)  # calculate x0, x1 and y0, y1
        x_1 = crypt.p(mpz(((i + 1) << 1) + 1), config.r)
        y_0 = gmpy2.add(poly.calculate(x_0), crypt.g(mpz((i + 1) << 1), config.r ^ pwd))  # use r xor pwd as the key
        y_1 = gmpy2.add(poly.calculate(x_1), crypt.g(mpz(((i + 1) << 1) + 1), config.r ^ pwd))
        if reader.if_init():
            table.append((y_0, y_1))  # if in initialization phase, add correct value
        else:
            if stat[i][0] is None:
                table.append((y_0, y_1))  # if no statistic information is derived
            elif (stat[i][1] + stat[i][0] * config.k) < config.ti:  # if fast
                rand_value = gmpy2.t_mod(config.generate_rand(), config.q)
                table.append((y_0, rand_value))
            elif (stat[i][1] - stat[i][0] * config.k) > config.ti:  # if slow
                rand_value = gmpy2.t_mod(config.generate_rand(), config.q)
                table.append((rand_value, y_1))
            else:
                table.append((y_0, y_1))
Exemplo n.º 2
0
def generate_poly():
    global c
    c = [config.h_pwd]   # first coefficient is the hardened password
    for i in xrange(config.max_features - 1):
        tmp = config.generate_rand()
        c.append(gmpy2.t_mod(tmp, config.q))
Exemplo n.º 3
0
    message = message[AES.block_size:]
    cipher = AES.new(key=get_byte_str_from_mpz(key), mode=AES.MODE_CFB, IV=iv)
    return cipher.decrypt(message)


# a keyed pseudorandom function family
def g(x, key):
    if not config.simple:
        mac = HMAC.new(key=get_byte_str_from_mpz(key), msg=get_byte_str_from_mpz(x)).digest()
        return gmpy2.t_mod(mpz(get_bit_str_from_byte(mac), base=2), config.q)
    return mpz()  # if in simple mode, return 0


# a keyed pseudorandom permutation function family
def p(x, key):
    if not config.simple:
        mac = HMAC.new(key=get_byte_str_from_mpz(key), msg=get_byte_str_from_mpz(x), digestmod=SHA).digest()
        return gmpy2.t_mod(mpz(get_bit_str_from_byte(mac), base=2), config.q)
    return x  # if in simple mode, return x


if __name__ == "__main__":
    # test for encrypt and decrypt
    mock_history = "history1;history2;etc."
    config.init_random()
    for i in xrange(100):
        mock_key = mpz(config.generate_rand())
        history_encrypt = encrypt(mock_history, mock_key)
        history_decrypt = decrypt(history_encrypt, mock_key)
        assert mock_history == history_decrypt, "de/encryption failed!"