Esempio n. 1
0
    def __init__(self):
        ### Gen p,q,b
        q = random.randint(2**16, 2**17 - 1)
        p = random.randint(2**31, 2**32 - 1)
        b = int((p - 1) / q)
        p = b * q + 1

        while not (is_simple(p) and is_simple(q)):
            q = random.randint(2**16, 2**17 - 1)
            p = random.randint(2**31, 2**32 - 1)
            b = int((p - 1) / q)
            p = b * q + 1

        ## Gen a
        a = random.randint(1, p - 1)
        temp = pow_mod(a, b, p)
        while temp != 1:
            a = random.randint(1, p - 1)
            temp = pow_mod(a, q, p)

        self.p = p
        self.q = q
        self.b = b
        self.a = a

        self.x = random.randint(1, self.q - 1)
        self.y = pow_mod(self.a, self.x, self.p)
Esempio n. 2
0
    def get_rs(self, h, file_sign, q=None, p=None, a=None, x=None):
        q = self.q if q is None else q
        p = self.p if p is None else p
        a = self.a if a is None else a
        x = self.x if x is None else x
        y = self.y

        rr = []
        ss = []

        with open(file_sign, "w") as fd_sign:
            for elem in h:
                if elem > q:
                    print(
                        "Error in gost signature! Some byte in hash more than q"
                    )
                    return None, None

                k = random.randint(0, q)
                r = pow_mod(a, k, p) % q
                s = (k * elem + x * r) % q

                while r == 0 or s == 0:
                    k = random.randint(0, q)
                    r = pow_mod(a, k, p) % q
                    s = (k * elem + x * r) % q

                # self.print_debug_each_iter(elem, p, q, r, s, k, x, y, a)

                rr.append(str(r) + "\n")
                ss.append(str(s) + "\n  ")
                fd_sign.write(str(r) + " " + str(s) + "\n")

        return rr, ss
Esempio n. 3
0
    def print_debug_each_iter(self, elem, r, s, u, k, k_inv):
        print(f"h_elem = {elem}")
        print(f"u = {elem} - {self.x} * {r} % {self.p} - 1 = {u}")
        print(
            f"Check k: k * k^-1 % p - 1 = {k} * {k_inv} % {self.p - 1} = {(k * k_inv) % (self.p - 1)}"
        )
        print(f"r = {r}, s = {s}")

        print("Check result:")
        res1 = (pow_mod(self.y, r, self.p) * pow_mod(r, s, self.p)) % self.p
        res2 = pow_mod(self.g, elem, self.p)
        print(f"{res1} = {res2}\n")
Esempio n. 4
0
    def print_debug_each_iter(self, elem, p, q, r, s, k, x, y, a):
        print(f"h_elem = {elem}")
        print(f"r: 0 < {r} < {q}, s: 0 < {s} < {q}")

        nod, x1, y1 = steroid_evklid(q, elem)
        h1 = y1  # (x % f + f) % f
        if h1 < 0:
            h1 = h1 + q
        print(
            f"Check h: h * h^-1 % p = {elem} * {h1} % {q} = {(elem * h1) % (q)}"
        )

        print("Check result:")
        u1 = (s * h1) % q
        u2 = ((-1 * r) * h1) % q
        r2 = ((pow_mod(a, u1, p) * pow_mod(y, u2, p)) % p) % q
        print(f"{r} = {r2}\n")
Esempio n. 5
0
    def compare_rs_hash(self, h, rr, ss, p=None, q=None, a=None, y=None):
        valid = True
        p = self.p if p is None else p
        q = self.q if q is None else q
        a = self.a if a is None else a
        y = self.y if y is None else y
        result_r = []

        print("Check result:")
        for elem, r, s in zip(h, rr, ss):
            if r < 0 or r > q:
                valid = False
                print(f"Error in gost sign! uncorrect r: 0 < {r} < {q}")
                return valid, None

            if s < 0 or s > q:
                valid = False
                print(f"Error in gost sign! uncorrect s: 0 < {s} < {q}")
                return valid, None

            nod, x1, y1 = steroid_evklid(q, elem)
            h1 = y1  # (x % f + f) % f
            if h1 < 0:
                h1 = h1 + q
            # print(f"Check h: h * h^-1 % p = {elem} * {h1} % {q} = {(elem * h1) % (q)}")

            u1 = (s * h1) % q
            u2 = ((-1 * r) * h1) % q
            r2 = ((pow_mod(a, u1, p) * pow_mod(y, u2, p)) % p) % q

            print(f"{r} = {r2}")
            valid = r == r2
            if not valid:
                print("Error! Gost signature is bullshit")
                return valid, None

            result_r.append(r2)

        if valid:
            print("\nGreat! Gost signature is authentic")

        return valid, result_r
Esempio n. 6
0
    def get_w(self, file_sign, d=None, N=None):
        d = self.alice.d if d is None else d
        N = self.alice.N if N is None else N
        self.w = []

        with open(file_sign, "r") as fd_sign:
            while True:
                s_num = fd_sign.readline()
                if not s_num:
                    break
                self.w.append(pow_mod(int(s_num), d, N))
        
        return self.w
Esempio n. 7
0
    def get_check_rs(self, file_sign, y=None, p=None):
        y = self.y if y is None else y
        p = self.p if p is None else p

        result = []
        with open(file_sign, "r") as fd_sign:
            while True:
                rs = fd_sign.readline()
                if not rs:
                    break

                split = rs.split(" ")
                r = int(split[0])
                split2 = split[1].split("\n")
                s = int(split2[0])

                # print(r, s)

                res = (pow_mod(y, r, p) * pow_mod(r, s, p)) % p
                # print(f"res {res}")
                result.append(res)

        return result
Esempio n. 8
0
    def get_rs(self, h, file_sign):
        rr = []
        ss = []

        with open(file_sign, "w") as fd_sign:
            for elem in h:
                k, k_inv = get_mut_prime(self.p - 1)
                r = pow_mod(self.g, k, self.p)
                u = (elem - (self.x * r)) % (self.p - 1)
                s = (k_inv * u) % (self.p - 1)

                rr.append(str(r) + "\n")
                ss.append(str(s) + "\n")
                fd_sign.write(str(r) + " " + str(s) + "\n")

                # self.print_debug_each_iter(elem, r, s, u, k, k_inv)

        return rr, ss
Esempio n. 9
0
    def compare_rs_hash(self, h, rs, p=None, g=None):
        valid = True

        p = self.p if p is None else p
        g = self.g if g is None else g
        result = []

        for elem_h, elem_rs in zip(h, rs):
            res = pow_mod(g, elem_h, p)
            print(f"{res} = {elem_rs}")

            valid = res == elem_rs
            if not valid:
                print("Error! ElGamal signature is bullshit")
                return valid

            result.append(res)

        if valid:
            print("Great! ElGamal signature is authentic")

        return valid, result
Esempio n. 10
0
 def decode(self, e):
     return pow_mod(e, self.c, self.N)
Esempio n. 11
0
 def encode(self, message, d, N):
     return pow_mod(message, d, N)
Esempio n. 12
0
 def get_hash(self, d):
     self.h = int(self.gen_sha256(str(self.n)), 16)
     self.h_serv = (self.h * pow_mod(self.r, d, self.N)) % self.N
     return self.h_serv
Esempio n. 13
0
 def encode(self, message, d) -> tuple:
     k = random.randint(1, self.p - 2)
     r = pow_mod(self.g, k, self.p)
     e = pow_mod_bs(message, d, k, self.p)
     return r, e
Esempio n. 14
0
 def __init__(self, p, g):
     self.p = p
     self.g = g
     self.c = random.randint(1, self.p - 1)
     self.d = pow_mod(self.g, self.c, self.p)
Esempio n. 15
0
 def decode(self, message):
     return pow_mod(message, self.d, self.p)
Esempio n. 16
0
 def encode(self, message):
     return pow_mod(message, self.c, self.p)