Exemple #1
0
    def point_addition(self, point1, point2):
        # If we try to add the same point
        if point1.x == point2.x and point1.y == point2.y:
            return self.__point_doubling(point1)
        # if we are adding different points
        else:

            if point1.x == point2.x and point1.y == (self.p -
                                                     point2.y) % self.p:
                return Point(0, 0)

            if point1.x == 0 and point1.y == 0:
                return Point(point2.x, point2.y)

            if point2.x == 0 and point2.y == 0:
                return Point(point1.x, point1.y)

            neg_flag = 1
            if point2.x - point1.x < 0:
                neg_flag = -1
                inv = euclideans_algorithm.eea(abs(point2.x - point1.x),
                                               self.p)
            else:
                inv = euclideans_algorithm.eea(point2.x - point1.x, self.p)

            s = ((point2.y - point1.y) * (inv * neg_flag)) % self.p
            x3 = (s * s - point1.x - point2.x) % self.p
            y3 = (s * (point1.x - x3) - point1.y) % self.p
            result_point = Point(x3, y3)
            if self.testPoint(result_point):
                return result_point
            else:
                raise Exception(
                    "Point addition does not result in a valid curve point. Aborting."
                )
Exemple #2
0
 def verify_sig(self, r, s, pk, M, hash_value):
     if 1 < r < self.n - 1 and 1 < s < self.n - 1:
         #calculate hash of message
         if hash_value == "sha256":
             sha256 = hashlib.sha256()
             sha256.update(M)
             H = sha256.digest()
         elif hash_value == "sha384":
             sha384 = hashlib.sha384()
             sha384.update(M)
             H = sha384.digest()
         else:
             sha512 = hashlib.sha512()
             sha512.update(M)
             H = sha512.digest()
         e = int(H.encode('hex'), 16)
         w = euclideans_algorithm.eea(s, self.n)
         u1 = (e * w) % self.n
         u2 = (r * w) % self.n
         R = self.point_addition(self.multiply(u1, self.getGPoint()),
                                 self.multiply(u2, pk))
         v = R.x % self.n
         if v == r:
             return "Signature Validates!"
         else:
             return "Signature FAIL!"
Exemple #3
0
    def __point_doubling(self, point1):

        s = ((3 * (point1.x * point1.x) + self.a) *
             (euclideans_algorithm.eea(2 * point1.y, self.p)) % self.p)
        x3 = (s * s - point1.x - point1.x) % self.p
        y3 = (s * (point1.x - x3) - point1.y) % self.p
        result_point = Point(x3, y3)

        if self.testPoint(result_point):
            return result_point
        else:
            print "Result Point: x = %d, y = %d" % (result_point.x,
                                                    result_point.y)
            raise Exception(
                "Point doubling does not result in a valid curve point. Aborting."
            )
Exemple #4
0
    def sign(self, data, skey, hash_value):
        system = SystemRandom()
        ephemeral = system.randrange(self.n) + 1
        R = self.multiply(ephemeral, self.getGPoint())
        r = R.x % self.n
        inv_ephemeral = euclideans_algorithm.eea(ephemeral, self.n)
        if hash_value == "sha256":
            sha256 = hashlib.sha256()
            sha256.update(data)
            H = sha256.digest()
        elif hash_value == "sha384":
            sha384 = hashlib.sha384()
            sha384.update(data)
            H = sha384.digest()
        else:
            sha512 = hashlib.sha512()
            sha512.update(data)
            H = sha512.digest()

        e = int(H.encode('hex'), 16)
        return r, (inv_ephemeral * (e + (skey * r))) % self.n
Exemple #5
0
test_curve = EllipticCurve(secp256_dparameters[0], secp256_dparameters[1], secp256_dparameters[2],
                           secp256_dparameters[3], secp256_dparameters[4], secp256_dparameters[5],
                           secp256_dparameters[6])
sk = 0x70a12c2db16845ed56ff68cfc21a472b3f04d7d6851bf6349f2d7d5b3452b38a
pk = test_curve.multiply(sk, test_curve.getGPoint())
print "pk x -> ", hex(pk.x)
print "pk y -> ", hex(pk.y)

if pk.x == 0x8101ece47464a6ead70cf69a6e2bd3d88691a3262d22cba4f7635eaff26680a8 and \
                pk.y == 0xd8a12ba61d599235f67d9cb4d58f1783d3ca43e78f0a5abaa624079936c0c3a9:
    print "PK TEST SUCCESSFUL"
else:
    print "PK TEST FAIL"

k = 0x580ec00d856434334cef3f71ecaed4965b12ae37fa47055b1965c7b134ee45d0
inv_k = euclideans_algorithm.eea(k, secp256_dparameters[5])
if inv_k == 0x6a664fa115356d33f16331b54c4e7ce967965386c7dcbf2904604d0c132b4a74:
    print "TEST INVERSE SUCCESSFUL"
else:
    print "TEST INVERSE FAIL"

R = test_curve.gen_pkey(k)
xr = 0x7214bc9647160bbd39ff2f80533f5dc6ddd70ddf86bb815661e805d5d4e6f27c
xy = 0x8b81e3e977597110c7cf2633435b2294b72642987defd3d4007e1cfc5df84541

if R.x == xr and R.y == xy:
    print "Ephemeral key gen successful"
else:
    print "Ephemeral key gen fail"

r = xr % secp256_dparameters[5]