Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _3user():
eA = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
pA = nacl.crypto_scalarmult_curve25519_base(eA)
print "A public: \t%s\nA exp: \t%s" % (b85encode(pA), b85encode(eA))
eB = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
pB = nacl.crypto_scalarmult_curve25519_base(eB)
print "B public: \t%s\nB exp: \t%s" % (b85encode(pB), b85encode(eB))
eC = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
pC = nacl.crypto_scalarmult_curve25519_base(eC)
print "C public: \t%s\nC exp: \t%s" % (b85encode(pC), b85encode(eC))
print
pAB = nacl.crypto_scalarmult_curve25519(eB, pA)
print "public AB", b85encode(pAB)
pBA = nacl.crypto_scalarmult_curve25519(eA, pB)
print "public BA", b85encode(pBA)
pCA = nacl.crypto_scalarmult_curve25519(eA, pC)
print "public CA", b85encode(pCA)
print
key = nacl.crypto_scalarmult_curve25519(eB, pCA)
print "key: \t%s" % (b85encode(key))
key = nacl.crypto_scalarmult_curve25519(eC, pBA)
print "key: \t%s" % (b85encode(key))
def _3user():
eA = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
pA = nacl.crypto_scalarmult_curve25519_base(eA)
print "A public: \t%s\nA exp: \t%s" % (b85encode(pA), b85encode(eA))
eB = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
pB = nacl.crypto_scalarmult_curve25519_base(eB)
print "B public: \t%s\nB exp: \t%s" % (b85encode(pB), b85encode(eB))
eC = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
pC = nacl.crypto_scalarmult_curve25519_base(eC)
print "C public: \t%s\nC exp: \t%s" % (b85encode(pC), b85encode(eC))
print
pAB = nacl.crypto_scalarmult_curve25519(eB, pA)
print "public AB", b85encode(pAB)
pBA = nacl.crypto_scalarmult_curve25519(eA, pB)
print "public BA", b85encode(pBA)
pCA = nacl.crypto_scalarmult_curve25519(eA, pC)
print "public CA", b85encode(pCA)
print
def dh1_handler():
# provides a high level interface to start a DH key exchange
exp = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
public = nacl.crypto_scalarmult_curve25519_base(exp)
return (exp, public)
def __init__(self, me, peer, basedir):
self.me = me
self.peer = peer
self.basedir = basedir
self.e_in = ('\0' * nacl.crypto_scalarmult_curve25519_BYTES)
self.e_out = ('\0' * nacl.crypto_scalarmult_curve25519_BYTES)
self.out_k = ('\0' * nacl.crypto_secretbox_KEYBYTES)
self.in_k = ('\0' * nacl.crypto_secretbox_KEYBYTES)
self.in_prev = ('\0' * nacl.crypto_secretbox_KEYBYTES)
self.peer_pub = ('\0' * nacl.crypto_scalarmult_curve25519_BYTES)
self.me_id = publickey.Identity(self.me, basedir=self.basedir)
self.peer_id = publickey.Identity(self.peer, basedir=self.basedir)
def load_dh_keychain(infile):
if not infile or infile == '-':
fd = sys.stdin
else:
fd = open(infile,'r')
keychain = list(split_by_n(fd.read(), nacl.crypto_scalarmult_curve25519_BYTES))
if fd != sys.stdin: fd.close()
return keychain
def __init__(self, me, peer, basedir):
self.me = me
self.peer = peer
self.basedir = basedir
self.e_in = ('\0' * nacl.crypto_scalarmult_curve25519_BYTES)
self.e_out = ('\0' * nacl.crypto_scalarmult_curve25519_BYTES)
self.out_k = ('\0' * nacl.crypto_secretbox_KEYBYTES)
self.in_k = ('\0' * nacl.crypto_secretbox_KEYBYTES)
self.in_prev = ('\0' * nacl.crypto_secretbox_KEYBYTES)
self.peer_pub = ('\0' * nacl.crypto_scalarmult_curve25519_BYTES)
self.me_id = publickey.Identity(self.me, basedir=self.basedir)
self.peer_id = publickey.Identity(self.peer, basedir=self.basedir)
def load(self):
keyfdir="%s/sk/.%s" % (self.basedir, self.me)
if not os.path.exists(keyfdir):
os.mkdir(keyfdir)
return self
keyfname='%s/%s' % (keyfdir, self.peer)
if not os.path.exists(keyfname):
return self
if not self.me_id:
self.me_id = publickey.Identity(self.me, basedir=self.basedir)
with open(keyfname,'r') as fd:
nonce = fd.read(nacl.crypto_box_NONCEBYTES)
plain = nacl.crypto_box_open(fd.read(), nonce, self.me_id.cp, self.me_id.cs)
c=nacl.crypto_scalarmult_curve25519_BYTES
i=0
self.e_in = plain[:c]
i+=c
self.e_out = plain[i:i+c]
i+=c
self.peer_pub = plain[i:i+c]
i+=c
c=nacl.crypto_secretbox_KEYBYTES
self.out_k = plain[i:i+c]
i+=c
self.in_k = plain[i:i+c]
i+=c
self.in_prev = plain[i:i+c]
def receive(self, cipher, nonce):
# decrypt the packet
plain = self.decrypt(cipher, nonce)
# update context
self.peer_pub=plain[:nacl.crypto_scalarmult_curve25519_BYTES]
if self.e_out != ('\0' * nacl.crypto_scalarmult_curve25519_BYTES):
dh2=plain[nacl.crypto_scalarmult_curve25519_BYTES:nacl.crypto_scalarmult_curve25519_BYTES*2]
self.out_k = nacl.crypto_scalarmult_curve25519(self.e_out, dh2)
return plain[nacl.crypto_scalarmult_curve25519_BYTES*2:]
def send(self,plain):
# update context
if self.peer_pub != ('\0' * nacl.crypto_scalarmult_curve25519_BYTES):
# calculate a new incoming key, and finish that DH, start a new for
# outgoing keys.
# only do this directly after receiving a packet, not on later sends
# without receiving any acks before, we reset peer_pub to signal, that
# an incoming request has been already once processed like this.
self.e_in = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
self.in_prev = self.in_k
self.in_k = nacl.crypto_scalarmult_curve25519(self.e_in, self.peer_pub)
self.peer_pub = ('\0' * nacl.crypto_scalarmult_curve25519_BYTES)
# generate e_out
self.e_out = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
elif self.out_k == ('\0' * nacl.crypto_secretbox_KEYBYTES):
# only for the very first packet necessary
# we explicitly need to generate e_out
self.e_out = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
#else: # axolotlize
# print 'axolotl!'
# self.out_k = nacl.crypto_generichash(self.out_k,
# nacl.crypto_scalarmult_curve25519(self.me_id.cs, self.peer_id.cp),
# nacl.crypto_scalarmult_curve25519_BYTES)
def mpecdh1(self, keyring = []):
self.key = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES)
keyring = [nacl.crypto_scalarmult_curve25519(self.key, public)
for public in keyring]
keyring.append(nacl.crypto_scalarmult_curve25519_base(self.key))
if len(keyring) == int(self.peers): # we are last, remove our own secret
self.secret = keyring[0]
keyring = keyring[1:]
return keyring