def main(self): kpikeys = self.kpikeys users = [] while 1: while not self.dataReady("userevent"): yield 1 print "SC sending a key" userid = self.recv("userevent") #to avoid duplicate entries try: users.index(userid) except ValueError: users.append(userid) users.sort() #todo to send in a format idkeymap = kpikeys.getCommonKeys(users) sessionKey = self.getSessionKey() print "idkeymap", idkeymap #encrypt the the session key with common keys for ID, key in idkeymap.iteritems(): idstr = struct.pack("!2L", 0, ID) print "id,key", ID,len(key) cipher = xtea.xtea_encrypt(key, sessionKey[:8]) cipher = cipher + xtea.xtea_encrypt(key, sessionKey[8:16]) data = idstr + cipher self.send(data, "outbox") self.send(sessionKey, "notifykey") yield 1
def main(self): kpikeys = self.kpikeys users = [] while 1: while not self.dataReady("userevent"): yield 1 print "SC sending a key" userid = self.recv("userevent") # to avoid duplicate entries try: users.index(userid) except ValueError: users.append(userid) users.sort() # todo to send in a format idkeymap = kpikeys.getCommonKeys(users) sessionKey = self.getSessionKey() print "idkeymap", idkeymap # encrypt the the session key with common keys for ID, key in idkeymap.iteritems(): idstr = struct.pack("!2L", 0, ID) print "id,key", ID, len(key) cipher = xtea.xtea_encrypt(key, sessionKey[:8]) cipher = cipher + xtea.xtea_encrypt(key, sessionKey[8:16]) data = idstr + cipher self.send(data, "outbox") self.send(sessionKey, "notifykey") yield 1
def main(self): userid = self.kpiuser.getID() data = xtea.xtea_encrypt(self.kpiuser.getRootKey(), struct.pack('!2L',0, userid)) print "encrypting user id with user key", self.kpiuser.getID(), self.kpiuser.getRootKey() self.send(data, "outbox") yield 1 while not self.dataReady("inbox"): yield 1 data = self.recv("inbox") temp = xtea.xtea_decrypt(self.kpiuser.getUserKey(), data) padding, challenge = struct.unpack('!2L',temp) response = challenge+1 print "received challenge",challenge print "sending response", response data = xtea.xtea_encrypt(self.kpiuser.getUserKey(), struct.pack('!2L',0, response)) self.send(data, "outbox") yield 1 while not self.dataReady("inbox"): yield 1 data = self.recv("inbox") if data == "SUCCESS": print "authentication success" else: print "authenication failure" return #decode data while 1: while self.dataReady("inbox"): data = self.recv("inbox") print "decoder", data if data.startswith("KEY"): index = len("KEY") #get the ID padding,ID = struct.unpack("!2L", data[index:index+8]) print "****ID****", ID key = "" try: key = self.kpiuser.getKey(ID) except KeyError: pass #the key is not for me if key != "": enckey = data[index+8:len(data)] part1 = xtea.xtea_decrypt(key, enckey[:8]) part2 = xtea.xtea_decrypt(key, enckey[8:16]) sessionkey = part1 + part2 print "decoded key", sessionkey self.send(sessionkey, "notifykey") else: data = data[len("DAT"):len(data)] print "decoded data", data self.send(data, "encout") yield 1
def main(self): userid = self.kpiuser.getID() data = xtea.xtea_encrypt(self.kpiuser.getRootKey(), struct.pack('!2L', 0, userid)) print "encrypting user id with user key", self.kpiuser.getID( ), self.kpiuser.getRootKey() self.send(data, "outbox") yield 1 while not self.dataReady("inbox"): yield 1 data = self.recv("inbox") temp = xtea.xtea_decrypt(self.kpiuser.getUserKey(), data) padding, challenge = struct.unpack('!2L', temp) response = challenge + 1 print "received challenge", challenge print "sending response", response data = xtea.xtea_encrypt(self.kpiuser.getUserKey(), struct.pack('!2L', 0, response)) self.send(data, "outbox") yield 1 while not self.dataReady("inbox"): yield 1 data = self.recv("inbox") if data == "SUCCESS": print "authentication success" else: print "authenication failure" return #decode data while 1: while self.dataReady("inbox"): data = self.recv("inbox") print "decoder", data if data.startswith("KEY"): index = len("KEY") #get the ID padding, ID = struct.unpack("!2L", data[index:index + 8]) print "****ID****", ID key = "" try: key = self.kpiuser.getKey(ID) except KeyError: pass #the key is not for me if key != "": enckey = data[index + 8:len(data)] part1 = xtea.xtea_decrypt(key, enckey[:8]) part2 = xtea.xtea_decrypt(key, enckey[8:16]) sessionkey = part1 + part2 print "decoded key", sessionkey self.send(sessionkey, "notifykey") else: data = data[len("DAT"):len(data)] print "decoded data", data self.send(data, "encout") yield 1
def main(self): blocksize = 8 # to do generalize padding and breaking in to blocks fmtstr = '!'+ str(blocksize) +'s' MAGIC_STRING = blocksize * chr(0x80) while 1: yield 1 if self.dataReady("control"): data = self.recv("control") if data == "SHUTDOWN": self.send(data, "signal") print "encryptor shutdown" break if self.dataReady("keyevent"): self.key = self.recv("keyevent") #print "key recieved at the encryptor",self.key if self.dataReady("inbox") and self.key != "\0": data = self.recv("inbox") enc = '' i = 0 #do padding if less than block size #Pad with 0x80 followed by zero (null) bytes datalen = len(data) if datalen > blocksize: for i in range(0, datalen-blocksize, blocksize): block = data[i:i+blocksize] enc = enc + xtea.xtea_encrypt(self.key,block) i = i + blocksize #get the last 8 bytes block = data[i:datalen] if len(block) == blocksize: enc = enc + xtea.xtea_encrypt(self.key,block) if block.find(chr(0x80)) != -1: enc = enc + xtea.xtea_encrypt(self.key,MAGIC_STRING) else: block = struct.pack(fmtstr, block + chr(0x80)) enc = enc + xtea.xtea_encrypt(self.key,block) self.send(enc, "outbox")
def main(self): blocksize = 8 # to do generalize padding and breaking in to blocks fmtstr = '!' + str(blocksize) + 's' MAGIC_STRING = blocksize * chr(0x80) while 1: yield 1 if self.dataReady("control"): data = self.recv("control") if data == "SHUTDOWN": self.send(data, "signal") print "encryptor shutdown" break if self.dataReady("keyevent"): self.key = self.recv("keyevent") #print "key recieved at the encryptor",self.key if self.dataReady("inbox") and self.key != "\0": data = self.recv("inbox") enc = '' i = 0 #do padding if less than block size #Pad with 0x80 followed by zero (null) bytes datalen = len(data) if datalen > blocksize: for i in range(0, datalen - blocksize, blocksize): block = data[i:i + blocksize] enc = enc + xtea.xtea_encrypt(self.key, block) i = i + blocksize #get the last 8 bytes block = data[i:datalen] if len(block) == blocksize: enc = enc + xtea.xtea_encrypt(self.key, block) if block.find(chr(0x80)) != -1: enc = enc + xtea.xtea_encrypt(self.key, MAGIC_STRING) else: block = struct.pack(fmtstr, block + chr(0x80)) enc = enc + xtea.xtea_encrypt(self.key, block) self.send(enc, "outbox")
def main(self): kpidb = self.kpidb while not self.dataReady("inbox"): yield 1 data = self.recv("inbox") padding,userid = struct.unpack('!2L', xtea.xtea_decrypt(kpidb.getRootKey(),data)) print "Authenticator received userid:", userid if kpidb.isValidUser(userid) == False: print "Invalid UserID" # todo shutdown return challenge = random.getrandbits(32) temp = struct.pack('!2L',0, challenge) userkey = kpidb.getKPIKeys().getKey(userid) data = xtea.xtea_encrypt(userkey, temp) print data, challenge, userkey self.send(data, "outbox") yield 1 while not self.dataReady("inbox"): yield 1 data = self.recv("inbox") temp = xtea.xtea_decrypt(userkey,data) padding, response = struct.unpack('!2L', temp) print data, response if response == challenge + 1: self.send("SUCCESS", "outbox") yield 1 else: print "authenication failure" return # shutdown #new user added self.send(userid, "notifyuser") #subscribe to data Management back plane subscriber = subscribeTo("DataManagement") self.link( (subscriber, "outbox"), (self, "outbox"), passthrough=2) subscriber.activate() yield 1 while 1: yield 1