示例#1
0
 def test_ecies(self):
     print("\nTEST: ECIES")
     alice = ECC()
     plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     ciphertext = alice.encrypt(plaintext, alice.get_pubkey())
     print(hexlify(ciphertext))
     self.assertEqual(plaintext, alice.decrypt(ciphertext))
示例#2
0
 def test_ecies(self):
     print("\nTEST: ECIES")
     alice = ECC()
     plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     ciphertext = alice.encrypt(plaintext, alice.get_pubkey())
     print(hexlify(ciphertext))
     self.assertEqual(plaintext, alice.decrypt(ciphertext))
示例#3
0
 def test_old_keys(self):
     alice = ECC()
     curve, px, py, i = ECC._old_decode_pubkey(alice._old_get_pubkey())
     curve2, pv, i = ECC._old_decode_privkey(alice._old_get_privkey())
     self.assertEqual(curve, curve2)
     alice2 = ECC(curve=curve, pubkey_x=px, pubkey_y=py, raw_privkey=pv)
     self.assertEqual(alice2.get_pubkey(), alice.get_pubkey())
     self.assertEqual(alice2.get_privkey(), alice.get_privkey())
示例#4
0
 def test_ecdsa(self):
     print("\nTEST: ECDSA")
     alice = ECC()
     plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     sig = alice.sign(plaintext)
     print(hexlify(sig))
     res = ECC(pubkey_x=alice.pubkey_x,
               pubkey_y=alice.pubkey_y).verify(sig, plaintext)
     self.assertTrue(res)
示例#5
0
 def test_ecdsa2(self):
     print("\nTEST: ECDSA 2")
     alice = ECC()
     plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     sig = b''.join((b'\x00', alice.sign(plaintext)))
     print(hexlify(sig))
     res = ECC(pubkey_x=alice.pubkey_x,
               pubkey_y=alice.pubkey_y).verify(sig, plaintext)
     self.assertFalse(res)
示例#6
0
 def test_ecdsa(self):
     print("\nTEST: ECDSA")
     alice = ECC()
     plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     sig = alice.sign(plaintext)
     print(hexlify(sig))
     res = ECC(pubkey_x=alice.pubkey_x,
               pubkey_y=alice.pubkey_y).verify(sig, plaintext)
     self.assertTrue(res)
示例#7
0
 def test_ecdsa2(self):
     print("\nTEST: ECDSA 2")
     alice = ECC()
     plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     sig = b''.join((b'\x00', alice.sign(plaintext)))
     print(hexlify(sig))
     res = ECC(pubkey_x=alice.pubkey_x,
               pubkey_y=alice.pubkey_y).verify(sig, plaintext)
     self.assertFalse(res)
示例#8
0
class PasswordCrypto:
    """
    Small wrapper around pyelliptic to perform assymetric encryption of
    passwords.
    """
    def __init__(self, pubkey):
        self.pubkey = b64decode(pubkey)
        self.actor = ECC(pubkey=self.pubkey)

    def encrypt(self, pwd):
        return b64encode(self.actor.encrypt(pwd, self.actor.get_pubkey()))
示例#9
0
 class FakeTransport():
     _myself = ECC(curve='secp256k1')
     def add_callback(self, section, cb):
         pass
     def send(self, msg):
         print 'sending', msg
     def log(self, msg):
         print msg
示例#10
0
 def test_compute_key_pair(self):
     print("\nTEST: Compute keypair")
     secret = b"This is a secret key for testing"
     goodx = "6e9e41bec214a5537cc4a9469485022e01f79952aa0c5dee3144e457123c05cd"
     goody = "a6c3f0695e9092c9b688733d3302545d887c3e08906127bf70cd9434a0d529b9"
     pubkeyx, pubkeyy, privkey = ECC.compute_keypair(secret, curve='secp256k1')
     print(hexlify(pubkeyx))
     print(hexlify(pubkeyy))
     self.assertEqual(goodx, hexlify(pubkeyx))
     self.assertEqual(goodx, hexlify(pubkeyy))
示例#11
0
 def test_compute_key_pair(self):
     print("\nTEST: Compute keypair")
     secret = b"This is a secret key for testing"
     goodx = "6e9e41bec214a5537cc4a9469485022e01f79952aa0c5dee3144e457123c05cd"
     goody = "a6c3f0695e9092c9b688733d3302545d887c3e08906127bf70cd9434a0d529b9"
     pubkeyx, pubkeyy, privkey = ECC.compute_keypair(secret,
                                                     curve='secp256k1')
     print(hexlify(pubkeyx))
     print(hexlify(pubkeyy))
     self.assertEqual(goodx, hexlify(pubkeyx))
     self.assertEqual(goodx, hexlify(pubkeyy))
    def parse_review(self, msg):
        pubkey = msg['pubkey'].decode('hex')
        subject = msg['subject'].decode('hex')
        signature = msg['sig'].decode('hex')
        text = msg['text']
        rating = msg['rating']

        # check the signature
        valid = ECC(pubkey=pubkey).verify(signature, self._build_review(subject, str(text), rating))
        
        if valid:
            newreview = review(pubkey, subject, signature, text, rating)
            self._reviews[subject].append(newreview)
        else:
            self._transport.log("[reputation] Invalid review!")
示例#13
0
                pass
                # ok
            elif myself == buyer:
                self.receive_order(msg)
            else:
                self._transport.log("Order not for us")
        if msg.get('id'):
            if msg['id'] in self._orders:
                self._orders[msg['id']]['state'] = msg['state']
            else:
                self._orders[msg['id']] = msg


# Test Code if run directly
if __name__ == '__main__':
    seller = ECC(curve='secp256k1')

    class FakeTransport():
        _myself = ECC(curve='secp256k1')

        def add_callback(self, section, cb):
            pass

        def send(self, msg, to=None):
            print 'sending', msg

        def log(self, msg):
            print msg

    transport = FakeTransport()
    rep = Orders(transport)
示例#14
0
            if myself == seller:
                pass
                # ok
            elif myself == buyer:
                self.receive_order(msg)
            else:
                self._transport.log("Order not for us")
        
        # Store order     
        if msg.get('id'):
            if self._orders.find( {id:msg['id']}):
                self._orders.update({'id':msg['id']}, { "$set": { 'state':msg['state'] } }, True)
            else:
                self._orders.update({'id':msg['id']}, { "$set": { msg } }, True)

if __name__ == '__main__':
    seller = ECC(curve='secp256k1')
    class FakeTransport():
        _myself = ECC(curve='secp256k1')
        def add_callback(self, section, cb):
            pass
        def send(self, msg, to=None):
            print 'sending', msg
        def log(self, msg):
            print msg
    transport = FakeTransport()
    rep = Orders(transport)
    rep.on_order(order(None, transport._myself.get_pubkey(), seller.get_pubkey(), 'new', 'One!', ["dsasd", "deadbeef"]))


示例#15
0
                self._transport.log("Order not for us")
        elif state == 'received':
            if myself == seller:
                pass
                # ok
            elif myself == buyer:
                self.receive_order(msg)
            else:
                self._transport.log("Order not for us")
        if msg.get('id'):
            if msg['id'] in self._orders:
                self._orders[msg['id']]['state'] = msg['state']
            else:
                self._orders[msg['id']] = msg

if __name__ == '__main__':
    seller = ECC(curve='secp256k1')
    class FakeTransport():
        _myself = ECC(curve='secp256k1')
        def add_callback(self, section, cb):
            pass
        def send(self, msg, to=None):
            print 'sending', msg
        def log(self, msg):
            print msg
    transport = FakeTransport()
    rep = Orders(transport)
    rep.on_order(order(None, transport._myself.get_pubkey(), seller.get_pubkey(), 'new', 'One!', ["dsasd", "deadbeef"]))


示例#16
0
if (len(argv) != 2):
    print('Error: No mixnet size given.\n\nUsage:\n{} mixnet_size'.format(argv[0]))
    exit(1)

CONFIG_FILENAME = 'mixnets.conf'

MIXNET_SIZE = int(argv[1])
BASE_PORT_WEB = 8000
BASE_PORT_P2P = 10000
MIXNET_NAME = 'sample_mixnet'

mixnet_config = {'global_config' : {'testnet' : 'True'}, 'mixing_peers' : {}, 'mixing_networks' : {MIXNET_NAME : {}}}

for mp in xrange(MIXNET_SIZE):
    mp_id = 'mp{0:02d}'.format(mp)
    mp_rank = mp
    mp_crypter = ECC(curve='secp256k1')
    mp_pubkey = mp_crypter.get_pubkey().encode('hex')
    mp_privkey = mp_crypter.get_privkey().encode('hex')

    mixnet_config['mixing_peers'][mp_id] = {'web_addr' : '{}.cp:{}'.format(mp_id, BASE_PORT_WEB + mp_rank), 'pubkey' : mp_pubkey, 'prvkey' : mp_privkey}
    mixnet_config['mixing_networks'][MIXNET_NAME][mp_id] = {'rank' : mp_rank, 'p2p_addr' : 'localhost:{}'.format(BASE_PORT_P2P + mp_rank)}

conf = ConfigObj()
for k in mixnet_config.keys():
    conf[k] = mixnet_config[k]

conf.filename = CONFIG_FILENAME
conf.write()
示例#17
0
 def test_old_keys(self):
     alice = ECC()
     curve, px, py, i = ECC._old_decode_pubkey(alice._old_get_pubkey())
     curve2, pv, i = ECC._old_decode_privkey(alice._old_get_privkey())
     self.assertEqual(curve, curve2)
     alice2 = ECC(curve=curve, pubkey_x=px, pubkey_y=py, raw_privkey=pv)
     self.assertEqual(alice2.get_pubkey(), alice.get_pubkey())
     self.assertEqual(alice2.get_privkey(), alice.get_privkey())
示例#18
0
print("\nTEST: AES-256-CBC")
ciphername = "aes-256-cbc"
iv_hex = b"000102030405060708090A0B0C0D0E0F"
iv = unhexlify(iv_hex)
key_hex = b"603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"
key = unhexlify(key_hex)
plain_hex = b"6bc1bee22e409f96e93d7e117393172a"
plaintext = unhexlify(plain_hex)

ctx = Cipher(key, iv, 1, ciphername=ciphername)
enc = ctx.ciphering(plaintext)
print(hexlify(enc))

ctx = Cipher(key, iv, 0, ciphername=ciphername)
assert ctx.ciphering(enc) == plaintext

print("\nTEST: ECIES")
alice = ECC()
plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ciphertext = ECC.encrypt(plaintext, alice.get_pubkey())
print(hexlify(ciphertext))
assert alice.decrypt(ciphertext) == plaintext

print("\nTEST: ECIES/RC4")
alice = ECC()
plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ciphertext = ECC.encrypt(plaintext, alice.get_pubkey(), ciphername="rc4")
print(hexlify(ciphertext))
assert alice.decrypt(ciphertext, ciphername="rc4") == plaintext
示例#19
0
ciphername = "aes-256-cbc"
iv_hex = b"000102030405060708090A0B0C0D0E0F"
iv = unhexlify(iv_hex)
key_hex = b"603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"
key = unhexlify(key_hex)
plain_hex = b"6bc1bee22e409f96e93d7e117393172a"
plaintext = unhexlify(plain_hex)

ctx = Cipher(key, iv, 1, ciphername=ciphername)
enc = ctx.ciphering(plaintext)
print(hexlify(enc))

ctx = Cipher(key, iv, 0, ciphername=ciphername)
assert ctx.ciphering(enc) == plaintext


print("\nTEST: ECIES")
alice = ECC()
plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ciphertext = ECC.encrypt(plaintext, alice.get_pubkey())
print(hexlify(ciphertext))
assert alice.decrypt(ciphertext) == plaintext


print("\nTEST: ECIES/RC4")
alice = ECC()
plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ciphertext = ECC.encrypt(plaintext, alice.get_pubkey(), ciphername="rc4")
print(hexlify(ciphertext))
assert alice.decrypt(ciphertext, ciphername="rc4") == plaintext
示例#20
0
 def __init__(self, pubkey):
     self.pubkey = b64decode(pubkey)
     self.actor = ECC(pubkey=self.pubkey)