Ejemplo n.º 1
0
 def test_genkey(self):
     for _ in range(1024):
         while True:
             private = urandom(32)
             (v, public, representative) = elligator.scalarbasemult(private)
             if v:
                 break
Ejemplo n.º 2
0
    def test_public(self):
        while True:
            private = urandom(32)
            (v, public, representative) = elligator.scalarbasemult(private)
            if v:
                break

        public2 = elligator.representativetopublic(representative)
        self.assertEqual(public, public2)
Ejemplo n.º 3
0
    def test_nacl_scalarmult_curve25519_base(self):
        try:
            import nacl

            while True:
                private = urandom(32)
                (v, public, representative) = elligator.scalarbasemult(private)
                if v:
                    break

            public2 = nacl.crypto_scalarmult_curve25519_base(private)
            self.assertEqual(public, public2)
        except ImportError:
            raise ImportError('Can\'t import TweetNaCl')
Ejemplo n.º 4
0
 def test_kat(self):
     kat_vectors = [
         (
             True,
             bytearray((0x4b, 0x97, 0x97, 0xd0, 0x98, 0x5a, 0xf7, 0x42, 0x9b, 0xc3, 0x55, 0xd9, 0x4, 0x81, 0xcf,
                        0xc7, 0xcc, 0x14, 0x54, 0x5c, 0xa5, 0xe6, 0x7c, 0x84, 0xcb, 0x1b, 0x4a, 0x4c, 0x4d, 0xa1, 0xda, 0x33,)),
             bytearray((0xbc, 0x13, 0x1a, 0x67, 0xb, 0x92, 0x2, 0x65, 0x8f, 0x2f, 0x79, 0xa, 0x7e, 0x4, 0x71,
                        0xd0, 0xe, 0x67, 0x90, 0xdb, 0x4d, 0x59, 0x8, 0xd2, 0x54, 0x4e, 0x5f, 0xbb, 0x8d, 0xa, 0x89, 0x78,)),
             bytearray((0x10, 0x3d, 0xba, 0xb8, 0x6f, 0x99, 0xee, 0xdb, 0xec, 0xa, 0xd6, 0x8f, 0xa9, 0x20, 0x3d,
                        0x5f, 0xd4, 0xf5, 0xe0, 0xdc, 0x48, 0xbc, 0xaf, 0x6c, 0x98, 0x50, 0xd0, 0x1a, 0x12, 0x9f, 0x28, 0x5c,))
         ),
         (
             True,
             bytearray((0xf1, 0x8, 0x6d, 0x75, 0xb3, 0x59, 0x5c, 0xe7, 0x3c, 0x41, 0xa8, 0x11, 0xbf, 0x1a, 0x10,
                        0xb1, 0xba, 0x1a, 0x75, 0xe4, 0xff, 0xd4, 0x98, 0x6c, 0x37, 0x98, 0xe3, 0x54, 0xf3, 0x24, 0x6b, 0x50,)),
             bytearray((0x6, 0x13, 0x6a, 0x4f, 0x20, 0x93, 0x37, 0x12, 0x4f, 0x4d, 0x92, 0x31, 0xc8, 0x34, 0xe9,
                        0xa0, 0x94, 0x8f, 0x89, 0x6d, 0xc9, 0x1c, 0x85, 0x5b, 0x32, 0x80, 0xd3, 0xd1, 0x4f, 0x42, 0xe8, 0x4e,)),
             bytearray((0x68, 0x4f, 0x96, 0xdf, 0xde, 0xa0, 0x57, 0xf5, 0xb2, 0x3f, 0xf6, 0x29, 0x52, 0xb3, 0x34,
                        0x95, 0xb0, 0x7b, 0xa3, 0xd5, 0x4, 0xac, 0x79, 0x1d, 0xf, 0x3c, 0x87, 0x52, 0x3a, 0xa7, 0x3f, 0x6d,))
         ),
         (
             True,
             bytearray((0x49, 0x76, 0xe, 0x1, 0x60, 0x24, 0x44, 0x48, 0x48, 0xc7, 0x9d, 0xc1, 0x81, 0x4, 0x6d, 0xc,
                        0x3a, 0x48, 0x8e, 0xf8, 0x67, 0xbd, 0xf9, 0xd1, 0x6f, 0x8c, 0x8f, 0xe4, 0x9b, 0x7b, 0x7f, 0x66,)),
             bytearray((0xb7, 0xdd, 0x0, 0x28, 0x3, 0xe0, 0x9f, 0x93, 0x52, 0x5d, 0xf6, 0x49, 0xa3, 0x9, 0xbf, 0x29,
                        0x16, 0x71, 0xfd, 0x82, 0x52, 0x23, 0xf2, 0x96, 0x2, 0xee, 0x97, 0x20, 0xc1, 0xd7, 0xa6, 0x3b,)),
             bytearray((0x8, 0x69, 0x14, 0xc7, 0xc7, 0xe8, 0x33, 0x79, 0x27, 0xf4, 0xa7, 0x1c, 0xda, 0x21, 0x25,
                        0x41, 0x9a, 0xe7, 0xe1, 0x83, 0x90, 0x52, 0x1f, 0xaf, 0x14, 0x3d, 0x5a, 0x2, 0xbc, 0x2e, 0x9c, 0x55,))
         ),
         (
             False,
             bytearray((0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
                        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,)),
             bytearray((0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
                        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,)),
             bytearray((0x78, 0x51, 0x2b, 0xcf, 0xe1, 0x62, 0x1d, 0x53, 0xed, 0xfc, 0xd5, 0xc0, 0x96, 0xc8, 0xb9,
                        0x96, 0x8e, 0x3, 0x7, 0x5e, 0xc3, 0x3a, 0x2e, 0xea, 0xaa, 0x44, 0x96, 0x64, 0x71, 0xfc, 0x98, 0x65,))
         ),
         (
             False,
             bytearray((0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
                        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,)),
             bytearray((0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
                        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,)),
             bytearray((0x38, 0xa9, 0xb4, 0x8a, 0xe8, 0x8b, 0xad, 0xbe, 0x15, 0xd9, 0xbe, 0x6b, 0x1d, 0xf8, 0x82,
                        0xa1, 0x89, 0x60, 0xd1, 0x14, 0xb6, 0x74, 0xba, 0xe8, 0x3f, 0xb6, 0xac, 0xd4, 0x5d, 0x94, 0x1, 0x68,))
         ),
         (
             True,
             bytearray((0x1e, 0x92, 0x62, 0xd, 0xc3, 0x9f, 0xf1, 0x28, 0x86, 0x40, 0x80, 0xb1, 0xfd, 0x37, 0xb9,
                        0x91, 0xac, 0xcc, 0xbf, 0x3d, 0x2e, 0x48, 0x67, 0xd2, 0xed, 0xf1, 0x75, 0xa6, 0x58, 0x10, 0x6d, 0x55,)),
             bytearray((0x5a, 0x25, 0x9d, 0x71, 0x1f, 0xec, 0xb2, 0x6d, 0xe7, 0x8, 0x4f, 0x8d, 0x80, 0x15, 0x4e,
                        0xec, 0x8c, 0xa3, 0xde, 0xd5, 0xde, 0x3f, 0xb6, 0x2f, 0x38, 0xc8, 0x6b, 0xf5, 0xf6, 0x84, 0x6e, 0x26,)),
             bytearray((0x28, 0x4f, 0x7, 0xcf, 0x45, 0xc0, 0x56, 0x74, 0xc6, 0xa7, 0xce, 0xa4, 0x8e, 0xf1, 0x83,
                        0xb7, 0xb5, 0x22, 0x3c, 0xff, 0xe9, 0x2e, 0xa7, 0xcb, 0x78, 0xa2, 0x3, 0x1a, 0x47, 0x54, 0xc, 0x6d,))
         ),
         (
             False,
             bytearray((0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
                        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,)),
             bytearray((0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
                        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,)),
             bytearray((0x10, 0x22, 0xe3, 0x43, 0x94, 0x95, 0xd7, 0xd9, 0x0, 0xf5, 0xf5, 0xac, 0xd, 0x39, 0x6,
                        0x48, 0x86, 0x54, 0x91, 0xe2, 0x88, 0xe5, 0xc2, 0xe6, 0x53, 0x5f, 0x10, 0xd8, 0x3e, 0xd0, 0xe, 0x7f,))
         ),
         (
             False,
             bytearray((0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
                        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,)),
             bytearray((0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
                        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,)),
             bytearray((0xa8, 0x30, 0x33, 0xe7, 0x55, 0x18, 0xae, 0x32, 0xb2, 0xd4, 0xdd, 0xb7, 0x76, 0x9a, 0x73,
                        0xd7, 0x72, 0x8d, 0x5f, 0xd8, 0xd8, 0xe9, 0x57, 0x8a, 0xa9, 0x8e, 0xb6, 0x12, 0x7b, 0x3e, 0x8d, 0x6e,))
         ),
         (
             True,
             bytearray((0x64, 0x27, 0xe0, 0x5, 0x13, 0xab, 0x7a, 0x81, 0x46, 0xd5, 0x8e, 0xbc, 0x28, 0x25, 0xf4,
                        0x66, 0xe3, 0x1c, 0x12, 0xbf, 0x97, 0x25, 0x99, 0x20, 0x37, 0x27, 0xd6, 0x1e, 0x9b, 0x6a, 0x6e, 0x7d,)),
             bytearray((0x5f, 0x9e, 0x2, 0x23, 0x7c, 0xa4, 0xfc, 0xc2, 0xc1, 0x8c, 0xc8, 0x91, 0x53, 0xdb, 0xa7,
                        0x5c, 0xca, 0x58, 0xea, 0x12, 0x60, 0x41, 0x3f, 0x36, 0xe, 0xe7, 0x7d, 0x78, 0x1d, 0x72, 0xa9, 0x33,)),
             bytearray((0x80, 0x5a, 0x8f, 0xba, 0x73, 0x45, 0x30, 0xc8, 0xf0, 0xb7, 0x64, 0x6a, 0xae, 0x73, 0x6f,
                        0x54, 0x65, 0x22, 0x5, 0xe8, 0x7, 0xd6, 0xab, 0x83, 0xc2, 0xe6, 0x79, 0x5e, 0x9a, 0x73, 0x22, 0x78,))
         ),
         (
             False,
             bytearray((0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
                        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,)),
             bytearray((0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
                        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,)),
             bytearray((0xc8, 0x25, 0x50, 0x29, 0xcd, 0x16, 0x69, 0xa9, 0x5c, 0x37, 0xab, 0xe7, 0xf5, 0x51, 0x9e,
                        0xaa, 0xbe, 0x7e, 0x8d, 0x51, 0x27, 0x4c, 0xd4, 0x46, 0x14, 0xb, 0xf9, 0x50, 0x80, 0x4, 0x14, 0x6e,))
         )
     ]
     for (v, pub, rv, pri) in kat_vectors:
         (test_v, test_pub, test_rv) = elligator.scalarbasemult(pri)
         self.assertEqual(v, test_v)
         if test_v:
             self.assertEqual(test_pub, pub)
             self.assertEqual(test_rv, rv)
             test_pub = elligator.representativetopublic(rv)
             self.assertEqual(test_pub, pub)
             test_rv = elligator.representativetopublic(test_rv)
             self.assertEqual(test_pub, pub)
Ejemplo n.º 5
0
    # use the next prime as the new q value
    q = pre_q.next_prime()
    n = p * q
    phi = (p - 1) * (q - 1)
    # compute new private exponent
    d = gmpy.invert(e, phi)
    # make sure that p is smaller than q
    if p > q:
        (p, q) = (q, p)
    return RSA.construct((long(n), long(e), long(d), long(p), long(q)))


if __name__ == "__main__":
    # deserialize master ECDH public key embedded in program
    MASTER_PUB_HEX = '525e422e42c9c662362a7326c3c5c785ac7ef52e86782c4ac3c06887583e7a6f'
    master_pub = unhexlify(MASTER_PUB_HEX)
    # generate a random (yes, actually random) Curve25519 key
    while True:
        private = urandom(32)
        (v, pub, rep) = elligator.scalarbasemult(private)
        if v:
            break
    # combine the ECDH keys to generate the seed
    seed = nacl.crypto_box_beforenm(master_pub, private)

    prng = AESPRNG(seed)
    # deterministic key generation from seed
    rsa = build_key(embed=rep, pos=80, randfunc=prng.randbytes)

    print rsa.exportKey()
Ejemplo n.º 6
0
    pre_q = n / p
    # use the next prime as the new q value
    q = pre_q.next_prime()
    n = p * q
    phi = (p-1) * (q-1)
    # compute new private exponent
    d = gmpy.invert(e, phi)
    # make sure that p is smaller than q
    if p > q:
        (p, q) = (q, p)
    return RSA.construct((long(n), long(e), long(d), long(p), long(q)))

if __name__ == "__main__":
    # deserialize master ECDH public key embedded in program
    MASTER_PUB_HEX = '525e422e42c9c662362a7326c3c5c785ac7ef52e86782c4ac3c06887583e7a6f'
    master_pub = unhexlify(MASTER_PUB_HEX)
    # generate a random (yes, actually random) Curve25519 key
    while True:
	private = urandom(32)
	(v, pub, rep) = elligator.scalarbasemult(private)
	if v:
		break
    # combine the ECDH keys to generate the seed
    seed = nacl.crypto_box_beforenm(master_pub, private)

    prng = AESPRNG(seed)
    # deterministic key generation from seed 
    rsa = build_key(embed=rep, pos=80, randfunc=prng.randbytes)

    print rsa.exportKey()