示例#1
0
    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
示例#2
0
    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
示例#3
0
    def main(self):
        blocksize = 8
        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 "decryptor shutdown"
                    break

            if self.dataReady("keyevent"):
                self.key = self.recv("keyevent")
                #print "key recieved at the decryptor",self.key

            if self.dataReady("inbox") and self.key != "\0":
                data = self.recv("inbox")
                dec = ''
                pad = True
                datalen = len(data)
                #Unpad last byte with 0x80 followed by zero (null) bytes
                if datalen > blocksize:
                    k = 0
                    if datalen > 2 * blocksize:
                        for i in range(0, datalen - 2 * blocksize, blocksize):
                            block = data[i:i + blocksize]
                            dec = dec + xtea.xtea_decrypt(self.key, block)
                        k = i + blocksize
                    block1 = xtea.xtea_decrypt(self.key, data[k:k + blocksize])
                    block2 = xtea.xtea_decrypt(self.key,
                                               data[k + blocksize:datalen])
                    dec = dec + block1
                    if block2 == MAGIC_STRING:
                        pad = False
                    else:
                        block = block2
                else:
                    block = xtea.xtea_decrypt(self.key, data)

                if pad == True:
                    rindex = block.rfind(chr(0x80))
                    if rindex != -1:
                        tmp = block[rindex:len(block)]
                        pad = chr(0x80) + (len(block) - rindex - 1) * chr(0x00)
                        if (pad == tmp):
                            print "remove padding", pad, "padlen", len(pad)
                            block = block[:rindex]
                    dec = dec + block

                #print "decrypted data ",dec
                self.send(dec, "outbox")
示例#4
0
   def main(self):
      blocksize = 8
      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 "decryptor shutdown"
                 break
            
	 if self.dataReady("keyevent"):
             self.key = self.recv("keyevent")
             #print "key recieved at the decryptor",self.key

         if self.dataReady("inbox") and self.key != "\0":
             data = self.recv("inbox")
             dec = ''
             pad = True
             datalen = len(data)
             #Unpad last byte with 0x80 followed by zero (null) bytes
             if datalen > blocksize:
                 k = 0
                 if datalen > 2*blocksize:
                     for i in range(0, datalen - 2*blocksize, blocksize):
                         block = data[i:i+blocksize]
                         dec = dec + xtea.xtea_decrypt(self.key,block)
                     k = i + blocksize
                 block1 = xtea.xtea_decrypt(self.key,data[k:k+blocksize])
                 block2 = xtea.xtea_decrypt(self.key,data[k+blocksize:datalen])
                 dec = dec + block1
                 if block2 == MAGIC_STRING:
                     pad = False
                 else:
                     block = block2
             else:
                 block = xtea.xtea_decrypt(self.key,data)
            
             if pad == True:
                 rindex = block.rfind(chr(0x80))
                 if rindex != -1:
                     tmp = block[rindex:len(block)]
                     pad = chr(0x80) + (len(block)-rindex-1)*chr(0x00)
                     if(pad == tmp):
                         print "remove padding", pad, "padlen", len(pad)
                         block = block[:rindex]
                 dec = dec + block
             
             #print "decrypted data ",dec
             self.send(dec, "outbox")
示例#5
0
    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