Exemple #1
0
 def process_command(self, cmd_tp, cmd_str):
     if cmd_tp in [CommandType.REQA, CommandType.WUPA, CommandType.HALT]:
         self._reset_tag()
     elif cmd_tp == CommandType.ATQAUL:
         self._tag_type = TagType.ULTRALIGHT
     elif cmd_tp == CommandType.ATQA1K:
         self._tag_type = TagType.CLASSIC1K
     elif cmd_tp == CommandType.ATQA4K:
         self._tag_type = TagType.CLASSIC4K
     elif cmd_tp == CommandType.ATQADS:
         self._tag_type = TagType.DESFIRE
     elif cmd_tp == CommandType.ANTI1U:
         uid = cmd_str.extra()[0:3]
         self._uid.extend(uid)
     elif cmd_tp == CommandType.ANTI1G:
         uid = cmd_str.extra()[0:4]
         self._uid.extend(uid)
     elif cmd_tp == CommandType.SEL1R:
         start = 1 if self._tag_type == TagType.ULTRALIGHT else 0
         uid = cmd_str.extra()[start:4]
         if uid != self._uid:
             print ("MISMATCH BETWEEN READER-TAG UID"), uid, self._uid
             self._uid = uid
     elif cmd_tp == CommandType.ANTI2T:
         uid = cmd_str.extra()[0:4]
         self._uid.extend(uid)
     elif cmd_tp == CommandType.SEL2R:
         uid = cmd_str.extra()[0:4]
         if uid != self._uid[-4:]:
             print ("MISMATCH BETWEEN READER_TAG UID#2"), uid, self._uid
             self._uid.extend(uid)
     elif cmd_tp == CommandType.AUTHA:
         self._cur_key = self._keya
     elif cmd_tp == CommandType.AUTHB:
         self._cur_key = self._keyb
     elif cmd_tp == CommandType.RANDTA:
         if not self._encryption:
             self._encryption = cipher(self._cur_key)
             uid_bits = Convert.to_bit_ar(self._uid)
             nonce_bits = Convert.to_bit_ar(cmd_str.extra())
             self._encryption.set_tag_bits(uid_bits, nonce_bits, 0)
     elif cmd_tp == CommandType.RANDRB:
         ar = cmd_str.extra()[4:]
         if ar != self._encryption.get_ar():
             print ("ERROR WITH AR")
         else:
             print ("AR OK")
     elif cmd_tp == CommandType.RANDTB:
         at = cmd_str.extra()
         if at != self._encryption.get_at():
             print ("ERROR WITH AT")
         else:
             print ("AT OK")
     elif cmd_tp == CommandType.READR:
         self._read = cmd_str.extra()
Exemple #2
0
 def process_command(self, cmd_tp, cmd_str):
     if cmd_tp in [CommandType.REQA, CommandType.WUPA, CommandType.HALT]:
         self._reset_tag()
     elif cmd_tp == CommandType.ATQAUL:
         self._tag_type = TagType.ULTRALIGHT
     elif cmd_tp == CommandType.ATQA1K:
         self._tag_type = TagType.CLASSIC1K
     elif cmd_tp == CommandType.ATQA4K:
         self._tag_type = TagType.CLASSIC4K
     elif cmd_tp == CommandType.ATQADS:
         self._tag_type = TagType.DESFIRE
     elif cmd_tp == CommandType.ANTI1U:
         uid = cmd_str.extra()[0:3]
         self._uid.extend(uid)
     elif cmd_tp == CommandType.ANTI1G:
         uid = cmd_str.extra()[0:4]
         self._uid.extend(uid)
     elif cmd_tp == CommandType.SEL1R:
         start = 1 if self._tag_type == TagType.ULTRALIGHT else 0
         uid = cmd_str.extra()[start:4]
         if uid != self._uid:
             print "MISMATCH BETWEEN READER-TAG UID", uid, self._uid
             self._uid = uid
     elif cmd_tp == CommandType.ANTI2T:
         uid = cmd_str.extra()[0:4]
         self._uid.extend(uid)
     elif cmd_tp == CommandType.SEL2R:
         uid = cmd_str.extra()[0:4]
         if uid != self._uid[-4:]:
             print "MISMATCH BETWEEN READER_TAG UID#2", uid, self._uid
             self._uid.extend(uid)
     elif cmd_tp == CommandType.AUTHA:
         self._cur_key = self._keya
     elif cmd_tp == CommandType.AUTHB:
         self._cur_key = self._keyb
     elif cmd_tp == CommandType.RANDTA:
         if not self._encryption:
             self._encryption = cipher(self._cur_key)
             uid_bits = Convert.to_bit_ar(self._uid)
             nonce_bits = Convert.to_bit_ar(cmd_str.extra())
             self._encryption.set_tag_bits(uid_bits, nonce_bits, 0)
     elif cmd_tp == CommandType.RANDRB:
         ar = cmd_str.extra()[4:]
         if ar != self._encryption.get_ar():
             print "ERROR WITH AR"
         else:
             print "AR OK"
     elif cmd_tp == CommandType.RANDTB:
         at = cmd_str.extra()
         if at != self._encryption.get_at():
             print "ERROR WITH AT"
         else:
             print "AT OK"
     elif cmd_tp == CommandType.READR:
         self._read = cmd_str.extra()
Exemple #3
0
 def _set_at(self, auth_key, prosp):
     self._reset()
     self._auth_key = auth_key
     self._auth_prosp = prosp
     extra_param = self._random.get_next()
     c = cipher(self._keya if self._auth_key == AuthKey.A else self._keyb)
     uid_bits = Convert.to_bit_ar(self._uid[0:4])
     nonce_bits = Convert.to_bit_ar(extra_param)
     c.set_tag_bits(uid_bits, nonce_bits, 0)
     self._at = c.get_at()
     return extra_param
Exemple #4
0
 def _set_at(self, auth_key, prosp):
     self._reset()
     self._auth_key = auth_key
     self._auth_prosp = prosp
     extra_param = self._random.get_next()
     c = cipher(self._keya if self._auth_key == AuthKey.A else self._keyb)
     uid_bits = Convert.to_bit_ar(self._uid[0:4])
     nonce_bits = Convert.to_bit_ar(extra_param)
     c.set_tag_bits(uid_bits, nonce_bits, 0)
     self._at = c.get_at()
     return extra_param
Exemple #5
0
    def _handle_next(self, cmd, extra):
        if not cmd:
            return None
        struct = CommandStructure.encode_command(cmd, extra)
        print "TAG OUTGOING"
        struct.display()

        all_bits = Convert.to_bit_ar(struct.all_bytes(), True)
        if self._encode:
            all_bits = self._encode(all_bits, cmd)
        #print all_bits
        self._callback(all_bits)
        return (cmd, struct)
Exemple #6
0
    def _handle_next(self, cmd, extra):
        if not cmd:
            return None
        struct = CommandStructure.encode_command(cmd, extra)
        print "TAG OUTGOING"
        struct.display()

        all_bits = Convert.to_bit_ar(struct.all_bytes(), True)
        if self._encode:
            all_bits = self._encode(all_bits, cmd)   
        #print all_bits
        self._callback(all_bits)
        return (cmd, struct)
Exemple #7
0
    def _handle_next(self, cmd, extra):
        struct = CommandStructure.encode_command(cmd, extra)
        print "READER OUTGOING"
        struct.display()
        if cmd == CommandType.HALT:
           self._reset_tag()

        all_bits = Convert.to_bit_ar(struct.all_bytes(), True)
        if self._encode:
            all_bits = self._encode(all_bits, cmd)   
        #print all_bits
        self._callback(all_bits, cmd == CommandType.HALT)
        return (cmd, struct) 
Exemple #8
0
    def _handle_next(self, cmd, extra):
        struct = CommandStructure.encode_command(cmd, extra)
        print "READER OUTGOING"
        struct.display()
        if cmd == CommandType.HALT:
            self._reset_tag()

        all_bits = Convert.to_bit_ar(struct.all_bytes(), True)
        if self._encode:
            all_bits = self._encode(all_bits, cmd)
        #print all_bits
        self._callback(all_bits, cmd == CommandType.HALT)
        return (cmd, struct)
Exemple #9
0
    def process_outgoing(self, bits, cmd):
        packet_type = cmd.packet_type()
        tag = self._tag_type
        if tag == TagType.ULTRALIGHT:
            self.process_bits(bits, packet_type) # update state
        elif tag == TagType.CLASSIC1K:
            self._cur_cmd = cmd
            c = self._encryption
            if c and cmd != CommandType.RANDTA:
                enc_bits = []
                if cmd == CommandType.RANDRB:                  
                    ll = len(bits)/2
                    enc_bits.extend(c.enc_bits(bits[0:ll], 1))
                    enc_bits.extend(c.enc_bits(bits[ll:]))
                else:
                    enc_bits.extend(c.enc_bits(bits))
                bits = enc_bits
            elif cmd == CommandType.RANDTA:
                old_enc = self._encryption
                
                c = cipher(self._cur_key)
                uid_bits = Convert.to_bit_ar(self._uid)
                nonce_bits = []
                ll = len(bits)
                for i in xrange(ll):
                    if i%9 != 8:
                        nonce_bits.append(bits[i])

                c.set_tag_bits(uid_bits, nonce_bits, 0)
                self._encryption = c
                if old_enc:
                    bits = old_enc.enc_bits(bits)
            
        elif cmd == CommandType.ATQAUL:
            self._tag_type = TagType.ULTRALIGHT
        elif cmd == CommandType.ATQA1K:
            self._tag_type = TagType.CLASSIC1K
        elif cmd == CommandType.ATQA4K:
            self._tag_type = TagType.CLASSIC4K
        elif cmd == CommandType.ATQADS:
            self._tag_type = TagType.DESFIRE
        else:     
            #print "TAG TYPE NOT CURRENTLY SUPPORTED", tag
            pass
        return bits
Exemple #10
0
    def process_outgoing(self, bits, cmd):
        packet_type = cmd.packet_type()
        tag = self._tag_type
        if tag == TagType.ULTRALIGHT:
            self.process_bits(bits, packet_type) # update state
        elif tag == TagType.CLASSIC1K:
            self._cur_cmd = cmd
            c = self._encryption
            if c and cmd != CommandType.RANDTA:
                enc_bits = []
                if cmd == CommandType.RANDRB:                  
                    ll = len(bits)/2
                    enc_bits.extend(c.enc_bits(bits[0:ll], 1))
                    enc_bits.extend(c.enc_bits(bits[ll:]))
                else:
                    enc_bits.extend(c.enc_bits(bits))
                bits = enc_bits
            elif cmd == CommandType.RANDTA:
                old_enc = self._encryption
                
                c = cipher(self._cur_key)
                uid_bits = Convert.to_bit_ar(self._uid)
                nonce_bits = []
                ll = len(bits)
                for i in xrange(ll):
                    if i%9 != 8:
                        nonce_bits.append(bits[i])

                c.set_tag_bits(uid_bits, nonce_bits, 0)
                self._encryption = c
                if old_enc:
                    bits = old_enc.enc_bits(bits)
            
        elif cmd == CommandType.ATQAUL:
            self._tag_type = TagType.ULTRALIGHT
        elif cmd == CommandType.ATQA1K:
            self._tag_type = TagType.CLASSIC1K
        elif cmd == CommandType.ATQA4K:
            self._tag_type = TagType.CLASSIC4K
        elif cmd == CommandType.ATQADS:
            self._tag_type = TagType.DESFIRE
        else:     
            #print "TAG TYPE NOT CURRENTLY SUPPORTED", tag
            pass
        return bits
Exemple #11
0
    def _decrypt_bits(self, bits):
        c = self._encryption        
        if c:
            self._print_enc(bits)

            start_bits = []
            rem_bits = bits

            ls = (len(bits)+1)/9

            if self._cur_cmd == CommandType.RANDTA and ls == CommandType.RANDRB.total_len():
                ll = len(rem_bits)/2           
                rdr_enc_nonce = bits[0:ll]
                start_bits = c.enc_bits(rdr_enc_nonce, 1, 1)                
                rem_bits = bits[ll:]
            elif self._cur_cmd in [CommandType.AUTHA, CommandType.AUTHB]:
                c = cipher(self._cur_key)
                self._encryption = c
                start_bits = c.set_tag_bits(Convert.to_bit_ar(self._uid), bits, 1)
                rem_bits = []
            bits = start_bits + c.enc_bits(rem_bits)
        return bits
Exemple #12
0
    def _decrypt_bits(self, bits):
        c = self._encryption        
        if c:
            self._print_enc(bits)

            start_bits = []
            rem_bits = bits

            ls = (len(bits)+1)/9

            if self._cur_cmd == CommandType.RANDTA and ls == CommandType.RANDRB.total_len():
                ll = len(rem_bits)/2           
                rdr_enc_nonce = bits[0:ll]
                start_bits = c.enc_bits(rdr_enc_nonce, 1, 1)                
                rem_bits = bits[ll:]
            elif self._cur_cmd in [CommandType.AUTHA, CommandType.AUTHB]:
                c = cipher(self._cur_key)
                self._encryption = c
                start_bits = c.set_tag_bits(Convert.to_bit_ar(self._uid), bits, 1)
                rem_bits = []
            bits = start_bits + c.enc_bits(rem_bits)
        return bits
Exemple #13
0
    def process_packet(self, cmd, struct):
        if cmd.packet_type() != PacketType.TAG_TO_READER:
           return None

        print "READER INCOMING"
        struct.display()

        next_cmd = CommandType.HALT
        extra_param = []

        if cmd == CommandType.ATQAUL:
            self._tag_type = TagType.ULTRALIGHT
            next_cmd = CommandType.ANTI1R
        elif cmd == CommandType.ATQA1K:
            self._tag_type = TagType.CLASSIC1K
            next_cmd = CommandType.ANTI1R
        elif cmd == CommandType.ATQA4K:
            self._tag_type = TagType.CLASSIC4K
            #next_cmd = CommandType.ANTI1R
            next_cmd = CommandType.HALT # don't have access to this, but similar to 1k
        elif cmd == CommandType.ATQADS:
            self._tag_type = TagType.DESFIRE
            next_cmd = CommandType.HALT # can't handle this
        elif cmd == CommandType.ANTI1U:
            extra_param = [0x88] + struct.extra()
            uid = extra_param[1:4] # should check tag type...
            self._uid.extend(uid)
            next_cmd = CommandType.SEL1R
        elif cmd == CommandType.ANTI1G:
            extra_param = struct.extra()
            uid = extra_param[0:4]
            self._uid.extend(uid)
            next_cmd = CommandType.SEL1R
        elif cmd == CommandType.SEL1U:
            next_cmd = CommandType.ANTI2R
        elif cmd == CommandType.SEL1K:
            next_cmd = CommandType.AUTHA
            self._auth_addr = 0x3C
            self._cur_addr = 0x3F
            extra_param = [self._auth_addr]
        elif cmd == CommandType.ANTI2T:
            extra_param = struct.extra()
            uid = extra_param[0:4]
            self._uid.extend(uid)
            next_cmd = CommandType.SEL2R
        elif cmd == CommandType.RANDTA:
            extra_param.extend(self._random.get_next())
            
            c = cipher(self._key)
            uid_bits = Convert.to_bit_ar(self._uid)
            nonce_bits = Convert.to_bit_ar(struct.extra())
            c.set_tag_bits(uid_bits, nonce_bits, 0)
            extra_param.extend(c.get_ar())
            next_cmd = CommandType.RANDRB
        elif cmd == CommandType.RANDTB:
            next_cmd = CommandType.READR
            extra_param = [self._cur_addr]
        elif cmd == CommandType.SEL2T:
            next_cmd = CommandType.READR
            self._cur_addr = 0x00            
            extra_param = [self._cur_addr]
        elif cmd == CommandType.READT:
            addr = self._cur_addr
            if self._tag_type == TagType.ULTRALIGHT:
                if addr > 0x08:
                    next_cmd = CommandType.HALT
                else:
                    addr += 0x04
                    next_cmd = CommandType.READR
                    extra_param = [addr]
            elif self._tag_type == TagType.CLASSIC1K:
                if addr % 4 == 0:
                    if addr == 0:
                        next_cmd == CommandType.HALT
                    else:
                        next_cmd = CommandType.AUTHA
                        addr -= 1
                        self._auth_addr -= 0x04
                        extra_param = [self._auth_addr]
                else:
                    addr -= 1
                    next_cmd = CommandType.READR
                    extra_param = [addr]

            self._cur_addr = addr
            
        else:
            print "ERROR, Unexpected command!!"

        ret = self._handle_next(next_cmd, extra_param)
        return ret
Exemple #14
0
 def _set_lfsr(self, bits):
     ls = lfsr.lfsr(bits, [16, 18, 19, 21])
     ls.advance(64)
     self._ar = Convert.to_byte_ar(ls.get_contents())
     ls.advance(32)
     self._at = Convert.to_byte_ar(ls.get_contents())
Exemple #15
0
 def __init__(self, key):
     self._bits = Convert.to_bit_ar(key)
Exemple #16
0
    def process_packet(self, cmd, struct):
        if cmd.packet_type() != PacketType.TAG_TO_READER:
            return None

        print "READER INCOMING"
        struct.display()

        next_cmd = CommandType.HALT
        extra_param = []

        if cmd == CommandType.ATQAUL:
            self._tag_type = TagType.ULTRALIGHT
            next_cmd = CommandType.ANTI1R
        elif cmd == CommandType.ATQA1K:
            self._tag_type = TagType.CLASSIC1K
            next_cmd = CommandType.ANTI1R
        elif cmd == CommandType.ATQA4K:
            self._tag_type = TagType.CLASSIC4K
            #next_cmd = CommandType.ANTI1R
            next_cmd = CommandType.HALT  # don't have access to this, but similar to 1k
        elif cmd == CommandType.ATQADS:
            self._tag_type = TagType.DESFIRE
            next_cmd = CommandType.HALT  # can't handle this
        elif cmd == CommandType.ANTI1U:
            extra_param = [0x88] + struct.extra()
            uid = extra_param[1:4]  # should check tag type...
            self._uid.extend(uid)
            next_cmd = CommandType.SEL1R
        elif cmd == CommandType.ANTI1G:
            extra_param = struct.extra()
            uid = extra_param[0:4]
            self._uid.extend(uid)
            next_cmd = CommandType.SEL1R
        elif cmd == CommandType.SEL1U:
            next_cmd = CommandType.ANTI2R
        elif cmd == CommandType.SEL1K:
            next_cmd = CommandType.AUTHA
            self._auth_addr = 0x3C
            self._cur_addr = 0x3F
            extra_param = [self._auth_addr]
        elif cmd == CommandType.ANTI2T:
            extra_param = struct.extra()
            uid = extra_param[0:4]
            self._uid.extend(uid)
            next_cmd = CommandType.SEL2R
        elif cmd == CommandType.RANDTA:
            extra_param.extend(self._random.get_next())

            c = cipher(self._key)
            uid_bits = Convert.to_bit_ar(self._uid)
            nonce_bits = Convert.to_bit_ar(struct.extra())
            c.set_tag_bits(uid_bits, nonce_bits, 0)
            extra_param.extend(c.get_ar())
            next_cmd = CommandType.RANDRB
        elif cmd == CommandType.RANDTB:
            next_cmd = CommandType.READR
            extra_param = [self._cur_addr]
        elif cmd == CommandType.SEL2T:
            next_cmd = CommandType.READR
            self._cur_addr = 0x00
            extra_param = [self._cur_addr]
        elif cmd == CommandType.READT:
            addr = self._cur_addr
            if self._tag_type == TagType.ULTRALIGHT:
                if addr > 0x08:
                    next_cmd = CommandType.HALT
                else:
                    addr += 0x04
                    next_cmd = CommandType.READR
                    extra_param = [addr]
            elif self._tag_type == TagType.CLASSIC1K:
                if addr % 4 == 0:
                    if addr == 0:
                        next_cmd == CommandType.HALT
                    else:
                        next_cmd = CommandType.AUTHA
                        addr -= 1
                        self._auth_addr -= 0x04
                        extra_param = [self._auth_addr]
                else:
                    addr -= 1
                    next_cmd = CommandType.READR
                    extra_param = [addr]

            self._cur_addr = addr

        else:
            print "ERROR, Unexpected command!!"

        ret = self._handle_next(next_cmd, extra_param)
        return ret