Exemplo n.º 1
0
def generate_private_key(security_level=SECURITY_LEVEL, s_size=S_SIZE, a=A, modulus=MODULUS):    
    r = random_integer(64)
    target = random_integer(31)
    t = modular_subtraction(target, r, modulus)
    assert (t + r) % modulus == target
    s = random_integer(s_size) >> 4   
    r += t % s    
    ae = t / s
    e = modular_subtraction(ae, a, modulus)
    print log(a, 2), log(s, 2), log(e, 2), log(r, 2)
    assert e > 0
    assert ((s * (a + e)) + r) % modulus == target, (((s * (a + e)) + r) % modulus, target)
    #if e < 0 or modulus - (s * (a + e)
    return s, e, r
Exemplo n.º 2
0
def generate_public_key(private_key, modulus=P):    
    x_i, z_i, xz_i = private_key
    x = modular_inverse(x_i, modulus)
    z = modular_inverse(z_i, modulus)
    a = modular_subtraction(z, x, modulus) # z - x    
    public_key = ((a * x) + (x * 2)) % modulus
    return public_key
Exemplo n.º 3
0
def generate_public_key(private_key, modulus=P):
    x_i, z_i, xz_i = private_key
    x = modular_inverse(x_i, modulus)
    z = modular_inverse(z_i, modulus)
    a = modular_subtraction(z, x, modulus)  # z - x
    public_key = ((a * x) + (x * 2)) % modulus
    return public_key
Exemplo n.º 4
0
def decrypt(ciphertext, private_key, p=N, _hash=hash_function):    
    private_key, pubk_i = private_key
    e = recover_key(ciphertext, private_key, p)    
    pubk_ciphertext = modular_subtraction(ciphertext, e, p)
    _ciphertext = (pubk_ciphertext * pubk_i) % p    
    key = _hash(e)
    return _ciphertext ^ key
Exemplo n.º 5
0
def decrypt(ciphertext, private_key, p=N, _hash=hash_function):
    private_key, pubk_i = private_key
    e = recover_key(ciphertext, private_key, p)
    pubk_ciphertext = modular_subtraction(ciphertext, e, p)
    _ciphertext = (pubk_ciphertext * pubk_i) % p
    key = _hash(e)
    return _ciphertext ^ key
Exemplo n.º 6
0
def generate_parameters(a_size=A_SIZE, b_size=B_SIZE,
                        x_size=X_SIZE, p_size=P_SIZE):
    a = random_integer(a_size)
    b = random_integer(b_size)
    x = random_integer(x_size)
    p = big_prime(p_size) 
    z = modular_inverse(modular_subtraction(1, a, p), p)
    return a, b, x, p, z
Exemplo n.º 7
0
def generate_parameters(a_size=A_SIZE, b_size=B_SIZE,
                        x_size=X_SIZE, p_size=P_SIZE):
    a = random_integer(a_size)
    b = random_integer(b_size)
    x = random_integer(x_size)
    p = big_prime(p_size) 
    z = modular_inverse(modular_subtraction(1, a, p), p)
    return a, b, x, p, z
Exemplo n.º 8
0
def generate_private_key(security_level=SECURITY_LEVEL,
                         s_size=S_SIZE,
                         a=A,
                         modulus=MODULUS):
    r = random_integer(64)
    target = random_integer(31)
    t = modular_subtraction(target, r, modulus)
    assert (t + r) % modulus == target
    s = random_integer(s_size) >> 4
    r += t % s
    ae = t / s
    e = modular_subtraction(ae, a, modulus)
    print log(a, 2), log(s, 2), log(e, 2), log(r, 2)
    assert e > 0
    assert ((s * (a + e)) + r) % modulus == target, (((s * (a + e)) + r) %
                                                     modulus, target)
    #if e < 0 or modulus - (s * (a + e)
    return s, e, r
Exemplo n.º 9
0
def convergent_decrypt(ciphertext, key, public_key, n=N,
                       _hash=hash_function, decrypt=symmetric_decrypt):   
    """ usage: convergent_decrypt(ciphertext, key, public_key, n=N,
                                  _hash=hash_function, decrypt=symmetric_decrypt) => message or None
                                  
        Decrypts ciphertext using key; If the integrity is validated, then return the message; If not, then return None. """
        
    public_key_ciphertext = modular_subtraction(ciphertext, key, n)
    ciphertext = (modular_inverse(public_key, n) * public_key_ciphertext) % n    
    return _decrypt(key, ciphertext, decrypt, _hash)
Exemplo n.º 10
0
def encrypt(m, public_key, security_level=SECURITY_LEVEL, _modulus=(2 ** (SECURITY_LEVEL * 8))):
    ciphertext = 0
    value = 0
    for element in public_key[:-1]:
        r = random_integer(security_level)
        ciphertext += element * r
        value += r
    final_r = modular_subtraction(m, value, _modulus)
    ciphertext += public_key[-1] * final_r    
    return ciphertext
Exemplo n.º 11
0
def generate_private_key(security_level=SECURITY_LEVEL, a=A, modulus=MODULUS):
    e = random_integer(security_level - 1)
    public_key = random_integer(security_level)    
    while e < public_key:
        e = random_integer(security_level)
        public_key = random_integer(security_level)    
    _as = modular_subtraction(public_key, e, modulus)
    s = _as / a
    e += _as % a
    assert ((a * s) + e) % modulus == public_key
    return s, e
Exemplo n.º 12
0
def generate_private_key(security_level=SECURITY_LEVEL, a=A, modulus=MODULUS):
    e = random_integer(security_level - 1)
    public_key = random_integer(security_level)
    while e < public_key:
        e = random_integer(security_level)
        public_key = random_integer(security_level)
    _as = modular_subtraction(public_key, e, modulus)
    s = _as / a
    e += _as % a
    assert ((a * s) + e) % modulus == public_key
    return s, e
Exemplo n.º 13
0
def encrypt(m, secret_key, security_level=SECURITY_LEVEL):
    modulus, encryption_key, decryption_key = secret_key
    ciphertext = 0
    value = 0
    for element in encryption_key[:-1]:
        r = random_integer(security_level)
        ciphertext += element * r
        value += r

    final_r = modular_subtraction(m, value, modulus)
    ciphertext += encryption_key[-1] * final_r
    return ciphertext % (modulus * random_integer(security_level))
Exemplo n.º 14
0
def encrypt(m,
            public_key,
            security_level=SECURITY_LEVEL,
            _modulus=(2**(SECURITY_LEVEL * 8))):
    ciphertext = 0
    value = 0
    for element in public_key[:-1]:
        r = random_integer(security_level)
        ciphertext += element * r
        value += r
    final_r = modular_subtraction(m, value, _modulus)
    ciphertext += public_key[-1] * final_r
    return ciphertext
Exemplo n.º 15
0
def convergent_decrypt(ciphertext,
                       key,
                       public_key,
                       n=N,
                       _hash=hash_function,
                       decrypt=symmetric_decrypt):
    """ usage: convergent_decrypt(ciphertext, key, public_key, n=N,
                                  _hash=hash_function, decrypt=symmetric_decrypt) => message or None
                                  
        Decrypts ciphertext using key; If the integrity is validated, then return the message; If not, then return None. """

    public_key_ciphertext = modular_subtraction(ciphertext, key, n)
    ciphertext = (modular_inverse(public_key, n) * public_key_ciphertext) % n
    return _decrypt(key, ciphertext, decrypt, _hash)
Exemplo n.º 16
0
def backdoor_decrypt(ciphertext, private_key, n=N,
                     _hash=hash_function, decrypt=symmetric_decrypt):  
    """ usage: backdoor_decrypt(ciphertext, private_key, n=N,
                                _hash=hash_function, decrypt=symmetric_decrypt) => message
                                
        Decrypts a ciphertext using the private key; If the integrity is validated, then return the message; If not, then return None.
        This method can be used on any ciphertext that was created with the corresponding public key. """
    p, pi, ri = private_key       
    pikey_cr = (pi * ciphertext) % n    
    cr = pikey_cr % pi    
    pikey = modular_subtraction(pikey_cr, cr, n)
    ciphertext = (cr * ri) % n
    key = (pikey * p) % n
    return _decrypt(key, ciphertext, decrypt, _hash)
Exemplo n.º 17
0
def encrypt(m, public_key, r_size=R_SIZE, q=Q, s_mask=secretkey.S_MASK + 1):
    ciphertext = 0
    running_total = 0
    for encryption_of_1 in public_key[:-1]:
        r = random_integer(r_size)
        ciphertext += encryption_of_1 * r
        running_total += r
        
    encryption_of_1 = public_key[-1]
    correction_factor = modular_subtraction(m, running_total, s_mask) # ensures r + r + r + ... == m # + 1 because it's a mask and not the modulus
    ciphertext += encryption_of_1 * correction_factor
    ciphertext += random_integer(160)
    ciphertext %= q
    #running_total += correction_factor
    #running_total &= s_mask
    #assert running_total == m, (running_total, m)
    return ciphertext
Exemplo n.º 18
0
def backdoor_decrypt(ciphertext,
                     private_key,
                     n=N,
                     _hash=hash_function,
                     decrypt=symmetric_decrypt):
    """ usage: backdoor_decrypt(ciphertext, private_key, n=N,
                                _hash=hash_function, decrypt=symmetric_decrypt) => message
                                
        Decrypts a ciphertext using the private key; If the integrity is validated, then return the message; If not, then return None.
        This method can be used on any ciphertext that was created with the corresponding public key. """
    p, pi, ri = private_key
    pikey_cr = (pi * ciphertext) % n
    cr = pikey_cr % pi
    pikey = modular_subtraction(pikey_cr, cr, n)
    ciphertext = (cr * ri) % n
    key = (pikey * p) % n
    return _decrypt(key, ciphertext, decrypt, _hash)
Exemplo n.º 19
0
def encrypt(m, public_key, r_size=R_SIZE, q=Q, s_mask=secretkey.S_MASK + 1):
    ciphertext = 0
    running_total = 0
    for encryption_of_1 in public_key[:-1]:
        r = random_integer(r_size)
        ciphertext += encryption_of_1 * r
        running_total += r

    encryption_of_1 = public_key[-1]
    correction_factor = modular_subtraction(
        m, running_total, s_mask
    )  # ensures r + r + r + ... == m # + 1 because it's a mask and not the modulus
    ciphertext += encryption_of_1 * correction_factor
    ciphertext += random_integer(160)
    ciphertext %= q
    #running_total += correction_factor
    #running_total &= s_mask
    #assert running_total == m, (running_total, m)
    return ciphertext
Exemplo n.º 20
0
def _sum_geometric_series(a, p=P, z=Z):
    return modular_subtraction(z, z * a, p)
Exemplo n.º 21
0
def invert_arithmetic_choice(d, b, c, modulus=256):
    return modular_subtraction(d, c, modulus) * (modular_inverse(a, modulus) * modular_subtraction(b, c, modulus)) % modulus, modulus)
Exemplo n.º 22
0
def generate_key(size=SIZE, p=P):
    a = random_integer(size)
    b = random_integer(size)
    d = modular_inverse(modular_subtraction(b ** 2, a ** 2, p), p)
    return a, b, d
Exemplo n.º 23
0
def invert_mix_columns(a, b, c, d, modulus=MODULUS):
    d ^= a
    b ^= c
    c = modular_subtraction(c, d, modulus)
    a = modular_subtraction(a, b, modulus)
    return a, b, c, d
Exemplo n.º 24
0
def sign(m, x, p=P):
    r, priv_info = generate_keypair()    
    e = hash(r + m)
    k = priv_info[0]    
    s = modular_subtraction(k, (x[0] + e), p)
    return s, e
Exemplo n.º 25
0
def _sum_geometric_series(a, p=P, z=Z):
    return modular_subtraction(z, z * a, p)    
Exemplo n.º 26
0
def _sum_geometric_series(point_count, a=A, p=P, z=Z):
    t = modular_subtraction(1, a, p)
    return (t * z) % p  
Exemplo n.º 27
0
def decrypt(c, secret_key, modulus=DEFAULT_MODULUS):
    k1, k2, k3 = secret_key
    _e = c #% k1    
    return (modular_subtraction(_e, k3, modulus) * modular_inverse(k2, modulus)) % modulus