예제 #1
0
    def parse_raw(self, raw_data):
        """Parses the data from the Cyton board into an OpenBCISample object."""
        #if type(raw_data) == str:
        #    data = struct.unpack(str(len(packet)) + 'B', "".join(packet))
        #else:
        #    data = raw_data

        start_byte = raw_data[0]
        bit_array = BitArray()
        self.checked_dropped(start_byte)
        # print(start_byte, start_byte == 0)

        if start_byte == 0:
            # 24-bit encoded
            for byte in raw_data[1:13]:
                bit_array.append('0b{0:08b}'.format(byte))
            results = []
            # and split it into 24-bit chunks here
            for sub_array in bit_array.cut(24):
                # calling ".int" interprets the value as signed 2's complement
                results.append(sub_array.int)
                self.last_values = np.array(results)
                # print(self.last_values)
                self.push_sample([np.append(start_byte, self.last_values)])

        elif start_byte >= 1 and start_byte <= 100:
            # 18-bit encoded + accel
            for byte in raw_data[1:-1]:
                bit_array.append('0b{0:08b}'.format(byte))
            deltas = []
            for sub_array in bit_array.cut(18):
                deltas.append(self.decompress_signed(sub_array))

            delta1, delta2 = np.array(deltas[:4]), np.array(deltas[4:])

            self.last_values1 = self.last_values - delta1
            self.last_values = self.last_values1 - delta2

            self.push_sample([self.last_values1, self.last_values])

        elif start_byte >= 101 and start_byte <= 200:
            # 19-bit encoded
            for byte in raw_data[1:]:
                bit_array.append('0b{0:08b}'.format(byte))
            deltas = []
            for sub_array in bit_array.cut(19):
                deltas.append(self.decompress_signed(sub_array))

            delta1, delta2 = np.array(deltas[:4]), np.array(deltas[4:])
            self.last_values1 = self.last_values - delta1
            # print(self.last_values1)
            self.last_values = self.last_values1 - delta2
            # print(self.last_values1)
            self.last_values = self.last_values1 - delta2
            # print(self.last_values)

            self.push_sample([
                np.append(start_byte, self.last_values1),
                np.append(start_byte, self.last_values)
            ])
예제 #2
0
    def bytes2data(self, raw_data):
        start_byte = raw_data[0]
        bit_array = BitArray()
        if start_byte == 0:
            print("ZERO PACKET")
            # we can just append everything to the bitarray
            for byte in raw_data[1:13]:
                bit_array.append('0b{0:08b}'.format(byte))
            results = []
            # and split it into 24-bit chunks here
            for sub_array in bit_array.cut(24):
                # calling ".int" interprets the value as signed 2's complement
                results.append(sub_array.int)
            self.last_values = np.array(results)
            print(self.last_values)
            return [self.last_values]
        elif start_byte >=1 and start_byte <=100:
            for byte in raw_data[1:-1]:
                bit_array.append('0b{0:08b}'.format(byte))
            deltas = []
            for sub_array in bit_array.cut(18):
                deltas.append(self.decompress_signed(sub_array))

            delta1 , delta2 = np.array(deltas[:4]) , np.array(deltas[4:])
            self.last_values1 = self.last_values - delta1
            print(self.last_values1)
            self.last_values = self.last_values1 - delta2
            print(self.last_values)
            return [self.last_values1, self.last_values]

        elif start_byte >=101 and start_byte <=200:
            for byte in raw_data[1:]:
                bit_array.append('0b{0:08b}'.format(byte))
            deltas = []
            for sub_array in bit_array.cut(19):
                deltas.append(self.decompress_signed(sub_array))

            delta1 , delta2 = np.array(deltas[:4]) , np.array(deltas[4:])
            self.last_values1 = self.last_values - delta1
            print(self.last_values1)
            self.last_values = self.last_values1 - delta2
            print(self.last_values)
            return [self.last_values1, self.last_values]
예제 #3
0
    def load_image_data(self,file):
        if (self.imageOffset == 0) and (self.bitsPerPixel == 24):
            self.imageOffset = BITMAP_FILEHEADER_SIZE + self.dib.biSize
            
        rowSize = math.ceil(self.imageWidth * self.bitsPerPixel / 32) * 4 # data pixels + pending data
        self.pixelArraySize = rowSize * abs(self.imageHight)

        file.seek(self.imageOffset,0)
        self.fullImageData = file.read(self.pixelArraySize)

        if self.compression == 0:

            file.seek(self.imageOffset,0)
  
            if self.bitsPerPixel == 1:
                for i in range(self.imageHight):
                    self.imageData.append([]) #new row in image data
                    row = BitArray(file.read(rowSize))
                    for j in range(self.imageWidth):
                        self.imageData[i].append(self.colorTable[1 if row[j] == 1 else 0])

            elif self.bitsPerPixel == 4:
                for i in range(self.imageHight):
                    self.imageData.append([]) #new row in image data
                    row = BitArray(file.read(rowSize))
                    pixels = list(row.cut(4))
                    for j in range(self.imageWidth):
                        self.imageData[i].append(self.colorTable[pixels[j].int])

            elif self.bitsPerPixel == 8:
               for i in range(self.imageHight):
                    self.imageData.append([]) #new row in image data
                    row = list(file.read(rowSize))
                    for j in range(self.imageWidth):
                        self.imageData[i].append(self.colorTable[row[j]])

            elif self.bitsPerPixel == 16:
                pass

            elif self.bitsPerPixel == 24:
                for i in range(self.imageHight):
                    self.imageData.append([]) #new row in image data
                    row = list(file.read(rowSize))
                    for j in range(self.imageWidth):
                        blue = row[3 * j + 0]
                        green = row[3 * j + 1]
                        red = row[3 * j + 2]
                        self.imageData[i].append(RGBA(red,green,blue,0))
                    

            elif self.bitsPerPixel == 32:
                pass

        if self.imageHight > 0: self.imageData = list(reversed(self.imageData))
예제 #4
0
def decompress_signed(pkt_id: int, bit_array: BitArray) \
        -> 'Tuple[np.ndarray, np.ndarray]':
    channel_samples = bit_array.cut(18) if pkt_id <= 100 else bit_array.cut(19)

    def _process_channels(sample: Iterable[BitArray]) -> np.ndarray:
        sample_deltas = []
        for channel_data in sample:
            channel_delta = channel_data.uint
            if channel_data.endswith('0b1'):
                # ends with a 1 means that it's a negative number
                channel_delta -= 1
                channel_delta *= -1
            sample_deltas.append(channel_delta)

        return np.array(sample_deltas, dtype=np.int32)

    channel_samples = list(channel_samples)
    sample_1 = _process_channels(channel_samples[:4])
    sample_2 = _process_channels(channel_samples[4:])
    return sample_1, sample_2
예제 #5
0
 def command(self):
     """The bytes of the command that should update the data"""
     command = BitArray("0b100101")  # magic WRITE code
     # make sure the values are single bits
     command += [BitArray(bool=bit) for bit in (self.outtmg, self.extgck, self.tmgrst, self.dsprpt, self.blank)]
     for b in self.brightness:
         command += BitArray(uint=b, length=7)
     for rgb in self.pixels:
         for color in rgb:
             command += BitArray(uint=color, length=16)
     assert len(command) == 224
     return tuple([ba.uint for ba in command.cut(8)])
예제 #6
0
 def command(self):
     """The bytes of the command that should update the data"""
     command = BitArray('0b100101') # magic WRITE code
     # make sure the values are single bits
     command += [BitArray(bool=bit) for bit in (
             self.outtmg, self.extgck, self.tmgrst,
             self.dsprpt, self.blank)]
     for b in self.brightness:
         command += BitArray(uint=b, length=7) 
     for rgb in self.pixels:
         for color in rgb:
             command += BitArray(uint=color, length=16)
     assert len(command) == 224
     return tuple([ba.uint for ba in command.cut(8)])
예제 #7
0
def top_n_key_sizes(n: int, e: BA) -> [(int, int)]:
    distances = []
    for guess_key_size in range(1, min(50, len(e.bytes))):
        bs = list(e.cut(guess_key_size * 8))
        if not bs or len(bs) == 1:
            continue
        ds = []
        for i in range(len(bs) - 1):
            b0 = bs[i]
            b1 = bs[i + 1]
            ds.append(hamming_weight(b0, b1) / float(guess_key_size))

        distance = sum(ds) / float(len(ds))
        distances.append((guess_key_size, distance))

    return list(sorted(distances, key=lambda x: x[1]))[:n]
예제 #8
0
def double_DES(key, plain):
	keybits = BitArray(hex = key)
	plainbits= BitArray(hex = plain)


	if len(keybits) != 112:
		print "length of double DES key is not 112 bits!"
		return
	if len(plainbits) != 64:
		print "length of double DES plain text is not 64 bits!"
		return

	rev_keybytes = ''
	for k in keybits.cut(7):
		rev_keybytes += (odd_parity[k.bin])

	obj1 = DES.new(rev_keybytes[0:8], DES.MODE_ECB)
	midbytes = obj1.encrypt(plainbits.tobytes())
	obj2 = DES.new(rev_keybytes[8:16], DES.MODE_ECB)
	cipherbytes = obj2.encrypt(midbytes)
	return BitArray(bytes = cipherbytes).hex
예제 #9
0
def get_literals(bs: BitArray):
    '''
    Returns:
        a tuple ``(tail, literals)`` where ``tail`` is ``bs`` without the
        literals, and ``literals`` is the series of literal bytes at the
        beginning of ``bs``.
    '''

    literal_max = 0b1111
    literals = BitArray()
    count = 0

    for byte in bs.cut(bits_per_byte):
        if byte.count(1) == 0:
            break

        literals += byte
        count += 1

        if count == literal_max:
            break

    return bs[count * bits_per_byte:], literals
 def testCut(self):
     a = BitArray('0xff00ff1111ff2222')
     l = list(a.cut(16))
     self.assertEqual(l, ['0x2222', '0x11ff', '0xff11', '0xff00'])
예제 #11
0
    def load_image_data(self, file):
        if (self.imageOffset == 0) and (self.bitsPerPixel == 24):
            self.imageOffset = BITMAP_FILEHEADER_SIZE + self.dib.biSize

        rowSize = math.ceil(self.imageWidth * self.bitsPerPixel /
                            32) * 4  # data pixels + pending data
        self.pixelArraySize = rowSize * abs(self.imageHight)

        file.seek(self.imageOffset, 0)
        self.fullImageData = file.read(self.pixelArraySize)

        #--------------------- Progres bar window
        #----------------------------------
        # layout the window
        layout = [[
            sg.ProgressBar(self.imageHight,
                           orientation='h',
                           size=(20, 20),
                           key='progressbar',
                           bar_color=('blue', 'white'))
        ], [sg.Text('  0%', key='progress')]]
        # create the window
        window = sg.Window('', layout, keep_on_top=True, finalize=True)
        progress_bar = window['progressbar']
        progress_percent = window['progress']
        #--------------------- Progres bar window
        #----------------------------------

        if self.compression == 0:
            file.seek(self.imageOffset, 0)

            if self.bitsPerPixel == 1:
                for i in range(self.imageHight):
                    # updating progress bar ----------------------
                    prc = int(100 * (i + 1) / self.imageHight)
                    progress_percent.update(str(prc) + '%')
                    progress_bar.update_bar(i + 1)
                    #---------------------------------------------
                    self.imageData.append([])  #new row in image data
                    row = BitArray(file.read(rowSize))
                    for j in range(self.imageWidth):
                        self.imageData[i].append(
                            self.colorTable[1 if row[j] == 1 else 0])

            elif self.bitsPerPixel == 4:
                for i in range(self.imageHight):
                    # updating progress bar ----------------------
                    prc = int(100 * (i + 1) / self.imageHight)
                    progress_percent.update(str(prc) + '%')
                    progress_bar.update_bar(i + 1)
                    #---------------------------------------------
                    self.imageData.append([])  #new row in image data
                    row = BitArray(file.read(rowSize))
                    pixels = list(row.cut(4))
                    for j in range(self.imageWidth):
                        self.imageData[i].append(
                            self.colorTable[pixels[j].int])

            elif self.bitsPerPixel == 8:
                for i in range(self.imageHight):
                    # updating progress bar ----------------------
                    prc = int(100 * (i + 1) / self.imageHight)
                    progress_percent.update(str(prc) + '%')
                    progress_bar.update_bar(i + 1)
                    #---------------------------------------------
                    self.imageData.append([])  #new row in image data
                    row = list(file.read(rowSize))
                    for j in range(self.imageWidth):
                        self.imageData[i].append(self.colorTable[row[j]])

            elif self.bitsPerPixel == 16:
                pass

            elif self.bitsPerPixel == 24:
                for i in range(self.imageHight):
                    # updating progress bar ----------------------
                    prc = int(100 * (i + 1) / self.imageHight)
                    progress_percent.update(str(prc) + '%')
                    progress_bar.update_bar(i + 1)
                    #---------------------------------------------
                    self.imageData.append([])  #new row in image data
                    row = list(file.read(rowSize))
                    for j in range(self.imageWidth):
                        blue = row[3 * j + 0]
                        green = row[3 * j + 1]
                        red = row[3 * j + 2]
                        self.imageData[i].append(RGBA(red, green, blue, 0))

            elif self.bitsPerPixel == 32:
                pass

        if self.imageHight > 0: self.imageData = list(reversed(self.imageData))
        window.close()
예제 #12
0
    def handleNotification(self, cHandle, data):
        """Called when data is received. It parses the raw data from the
        Ganglion and returns an OpenBCISample object"""

        if len(data) < 1:
            self._logger.warning('A packet should at least hold one byte...')
            return

        bit_array = BitArray()
        start_byte = data[0]

        dropped, dummy_samples = self._upd_sample_count(start_byte)

        if start_byte == 0:
            # uncompressed sample
            if not self._timestamps:
                self._timestamps = _GanglionDelegate._timestamp_generator()

            self._wait_for_full_pkt = False

            for byte in data[1:13]:
                bit_array.append(f'0b{byte:08b}')

            results = []
            # and split it into 24-bit chunks here
            for sub_array in bit_array.cut(24):
                # calling '.int' interprets the value as signed 2's complement
                results.append(sub_array.int)

            self._last_values = np.array(results, dtype=np.int32)

            # store the sample
            self._result_callback(
                OpenBCISample(next(self._timestamps),
                              self._sample_cnt - 1,
                              start_byte,
                              self._last_values))

        elif 1 <= start_byte <= 200:
            if self._wait_for_full_pkt:
                self._logger.warning('Need to wait for next full packet...')
                for dummy in dummy_samples:
                    self._result_callback(dummy)
                return
            elif dropped > 0:
                self._logger.error(f'Dropped {dropped} packets! '
                                   'Need to wait for next full packet...')

                for dummy in dummy_samples:
                    self._result_callback(dummy)
                self._wait_for_full_pkt = True
                return
            else:
                for byte in data[1:]:
                    bit_array.append(f'0b{byte:08b}')

                delta_1, delta_2 = decompress_signed(start_byte, bit_array)

                tmp_value = self._last_values - delta_1
                self._last_values = tmp_value - delta_2

                self._result_callback(
                    OpenBCISample(next(self._timestamps),
                                  self._sample_cnt - 2,
                                  start_byte, tmp_value))
                self._result_callback(
                    OpenBCISample(next(self._timestamps),
                                  self._sample_cnt - 1,
                                  start_byte,
                                  self._last_values))
예제 #13
0
    def parse_raw(self, raw_data):
        """Parses the data from the Cyton board into an OpenBCISample object."""
        if type(raw_data) == str:
            data = struct.unpack(str(len(packet)) + 'B', "".join(packet))
        else:
            data = raw_data

        bit_array = BitArray()

        start_byte = raw_data[0]
        dropped, dummy_samples = self.check_dropped(start_byte)
        self.last_id = start_byte

        if self._wait_for_full_pkt:
            if start_byte != 0:
                self._logger.warning('Need to wait for next full packet...')
                if dropped > 0:
                    self.samples.extend(dummy_samples)
                else:
                    self.samples.extend([
                        OpenBCISample(start_byte, [np.NaN] * 4, [],
                                      self.start_time, self.__boardname),
                        OpenBCISample(start_byte, [np.NaN] * 4, [],
                                      self.start_time, self.__boardname)
                    ])
                return
            else:
                self._logger.warning('Got full packet, resuming.')
                self._wait_for_full_pkt = False

        if dropped > 0:
            self._logger.error('Dropped %d packets! '
                               'Need to wait for next full packet...' %
                               dropped)

            self.samples.extend(dummy_samples)
            self._wait_for_full_pkt = True
            return

        if start_byte == 0:
            # uncompressed sample
            for byte in raw_data[1:13]:
                bit_array.append('0b{0:08b}'.format(byte))

            results = []
            # and split it into 24-bit chunks here
            for sub_array in bit_array.cut(24):
                # calling ".int" interprets the value as signed 2's complement
                results.append(sub_array.int)

            self.last_values = np.array(results, dtype=np.int32)

            # store the sample
            self.samples.append(
                OpenBCISample(start_byte, self.last_values, [],
                              self.start_time, self.__boardname))

        elif 1 <= start_byte <= 200:
            for byte in raw_data[1:]:
                bit_array.append('0b{0:08b}'.format(byte))

            deltas = []
            if start_byte <= 100:
                # 18-bit compressed sample
                for sub_array in bit_array.cut(18):
                    deltas.append(self.decompress_signed(sub_array))
            else:
                # 19-bit compressed sample
                for sub_array in bit_array.cut(19):
                    deltas.append(self.decompress_signed(sub_array))

            delta1 = np.array(deltas[:4], dtype=np.int32)
            delta2 = np.array(deltas[4:], dtype=np.int32)

            self.last_values1 = self.last_values - delta1
            self.last_values = self.last_values1 - delta2

            # since compressed packets include two samples which have been
            # processed client-side, prefer to calculate timestamp as
            # expected timestamp with respect to the most-recent full-size
            # packet received

            # store both samples
            self.samples.append(
                OpenBCISample(start_byte, self.last_values1, [],
                              self.start_time, self.__boardname))

            self.samples.append(
                OpenBCISample(start_byte, self.last_values, [],
                              self.start_time, self.__boardname))
예제 #14
0
class DUKPT:
    """Base DUKPT class with common functions of both client and server"""
    _pin_mask      = BitArray(hex="0x00000000000000FF00000000000000FF")
    _mac_req_mask  = BitArray(hex="0x000000000000FF00000000000000FF00")
    _mac_resp_mask = BitArray(hex="0x00000000FF00000000000000FF000000")
    _mac_data_req  = BitArray(hex="0x0000000000FF00000000000000FF0000")
    _mac_data_resp = BitArray(hex="0x000000FF00000000000000FF00000000")
    _ipek          = None
    _tdes_key      = None
    _cur_key       = None
    _ksn           = None
    BDK_LEN        = 16
    KSN_LEN        = 10

    def __init__(self, bdk=None, ksn=None, ipek=None):
        """Initialization
        Keyword arguments:
        bdk (raw or BitArray)  -- Base Derivation Key (16 bytes)
        ksn (raw or BitArray)  -- Key Serial Number (10 bytes)
        ipek (raw or BitArray) -- Initial Pin Encryption Key (16 bytes)
        """
        if ipek:
            if isinstance(ipek, BitArray):
                self._ipek = ipek
            else:
                self._ipek = BitArray(bytes=ipek)
            if isinstance(ksn, BitArray):
                self._ksn = ksn
            else:
                self._ksn  = BitArray(bytes=ksn)
        else:
            if not bdk:
                raise InvalidDUKPTArguments("Must have either ipek or bdk")
            if len(bdk) != self.BDK_LEN:
                raise InvalidDUKPTArguments("BDK must have a length of %d" % self.BDK_LEN)
            self._bdk = BitArray(bytes=bdk)
        
    def derive_key(self, ipek, ksn):
        """Derive a unique key given the ipek and ksn

        Keyword arguments:
        ipek (BitArray) -- Initial Pin Encryption Key
        ksn (BitArray)  -- Key Serial Number
        """
        c_mask       = BitArray(hex='0xc0c0c0c000000000c0c0c0c000000000')
        ksn_offset   = 2
        ctr_offset   = -3
        right_offset = 8

        # Registers taken from documentation
        curkey = ipek
        ksnr   = BitArray(bytes=ksn.bytes[ksn_offset:])
        r3     = self.copy_counter(ksnr)
        r8     = self.reset_counter(ksnr.bytes)
        sr     = BitArray(hex='0x100000')
       
        while (sr.bytes[0] != '\x00') or (sr.bytes[1] != '\x00') or (sr.bytes[2] != '\x00'):
            tmp = self.copy_counter(sr)
            tmp = tmp & r3
            if (tmp.bytes[0] != '\x00') or (tmp.bytes[1] != '\x00') or (tmp.bytes[2] != '\x00'): 
                # Step 2
                n_ctr = BitArray(bytes=r8.bytes[ctr_offset:]) | sr
                r8    = BitArray(bytes=r8.bytes[:ctr_offset]+n_ctr.bytes)
                
                # Step 3
                r8a   = r8 ^ BitArray(bytes=curkey.bytes[right_offset:])
                
                # Step 4
                cipher = DES.new(curkey.bytes[:DES.key_size], DES.MODE_ECB)
                r8a    = BitArray(bytes=cipher.encrypt(r8a.bytes))
                
                # Step 5
                r8a = BitArray(bytes=curkey.bytes[right_offset:]) ^ r8a

                # Step 6
                curkey = curkey ^ c_mask
                
                # Step 7
                r8b = BitArray(bytes=curkey.bytes[right_offset:]) ^ r8
                
                # Step 8
                cipher = DES.new(curkey.bytes[:DES.key_size], DES.MODE_ECB)
                r8b    = BitArray(bytes=cipher.encrypt(r8b.bytes))
                
                # Step 9
                r8b = BitArray(bytes=curkey.bytes[right_offset:]) ^ r8b

                # Step 10 / 11
                curkey = BitArray(bytes=r8b.bytes+r8a.bytes)

            sr >>= 1
        self._cur_key = curkey
        return curkey

    def reset_counter(self, data):
        """Reset the counter to zero

        Keyword arguments:
        data (raw or BitArray) -- Must be at least 3 bytes
        
        Return:
        BitArray of the data passed in
        """
        if isinstance(data, BitArray):
            data = data.bytes
        if len(data) < 3:
            return None
        mask = BitArray(hex='0xe00000')
        ctr  = BitArray(bytes=data[len(data)-3:])
        return BitArray(bytes=data[:-3] + (mask & ctr).bytes)

    def copy_counter(self, data):
        """Copy only the counter bytes from a given string or BitArray

        Keyword arguments:
        data (raw or BitArray) -- Must be at least 3 bytes

        Return:
        BitArray of only the counter bytes
        """
        mask = BitArray(hex='0x1fffff')
        if len(data.bytes) > 3:
            ctr = BitArray(bytes=data.bytes[-3:])
        else:
            ctr = data

        return mask & ctr

    def increase_counter(self):
        """Increase the counter bytes of the stored ksn by one"""
        ctr = self._ksn.cut(21, start=59).next().int + 1
        self._ksn.overwrite('0b'+BitArray(int=ctr, length=21).bin, 59)