Exemplo n.º 1
0
	def __init__(self, headsetId=0, research_headset = False):
		
		if research_headset:
			self.rijn = rijndael(research_key, 16)
		else:
			self.rijn = rijndael(consumer_key, 16)
		
		self._goOn = True
		self.packets = []
		
		if self.setupWin(headsetId) if windows else self.setupPosix(headsetId):
			logger.info("Fine, connected to the Emotiv EPOC receiver")
		else:
			logger.error("Unable to connect to the Emotiv EPOC receiver :-(")
			sys.exit(1)
Exemplo n.º 2
0
	def __init__(self, headsetId=0, research_headset = True):
		
		if research_headset:
			self.rijn = rijndael(research_key, 16)
		else:
			self.rijn = rijndael(consumer_key, 16)
		
		self._goOn = True
		self.packets = []
		
		if self.setupWin(headsetId) if windows else self.setupPosix(headsetId):
			logger.info("Fine, connected to the Emotiv EPOC receiver")
		else:
			logger.error("Unable to connect to the Emotiv EPOC receiver :-(")
			sys.exit(1)
Exemplo n.º 3
0
	def setupCrypto(self, sn, feature):
		type = feature[5]
		type &= 0xF
		type = type == 0

		key = ['\0'] * 16
		key[0] = sn[-1]
		key[1] = '\0'
		key[2] = sn[-2]
		if type:
			key[3] = 'H'
			key[4] = sn[-1]
			key[5] = '\0'
			key[6] = sn[-2]
			key[7] = 'T'
			key[8] = sn[-3]
			key[9] = '\x10'
			key[10] = sn[-4]
			key[11] = 'B'
		else:
			key[3] = 'T'
			key[4] = sn[-3]
			key[5] = '\x10'
			key[6] = sn[-4]
			key[7] = 'B'
			key[8] = sn[-1]
			key[9] = '\0'
			key[10] = sn[-2]
			key[11] = 'H'
		
		key[12] = sn[-3]
		key[13] = '\0'
		key[14] = sn[-4]
		key[15] = 'P'
		self.rijn = rijndael(''.join(key), 16)
Exemplo n.º 4
0
    def _detect_key_posix(self):
        for key_name, key in KEYS.items():
            self._rijn = rijndael(key, 16)
            successive = []

            # Read a couple of times because first readings are misleading
            for i in range(10):
                self._read_posix()

            # Sample the raw data 20 times
            for i in range(20):
                fst = self._read_posix()
                snd = self._read_posix()

                # Account for the counter resetting
                if fst.counter != 127 and fst.counter != 230:
                    successive.append((fst, snd))

            # If the counter increments correctly, we got the right key!
            if all(
                    map(lambda pair: pair[0].counter + 1 == pair[1].counter,
                        successive)):
                self._key = key
                return

        raise UnknownKey, "Cannot decrypt data: Unknown key."
Exemplo n.º 5
0
def UnwrapKey(key, kek):
    if len(key) > 32:
        # assume hex
        key = key.decode('hex')
    if len(kek) > 16:
        # assume hex
        kek = kek.decode('hex')

    if (len(key) % 8) or (len(kek) % 8):
        raise Exception('key and kek must be a multiple of 64 bits')

    # create a de-cipher with kek
    decipher = aes.rijndael(kek)

    # Inputs:  Ciphertext, (n+1) 64-bit values {C0, C1, ..., Cn}, and
    # Key, K (the KEK).
    # Outputs: Plaintext, n 64-bit values {P0, P1, K, Pn}.
    n = len(key) // 8 - 1
    if n < 1:
        raise Exception('wrapped key too short')

    # 1) Initialize variables.
    #
    #    Set A = C[0]
    #    For i = 1 to n
    #      R[i] = C[i]
    A = key[0:8]
    R = [key[(i + 1) * 8:(i + 2) * 8] for i in range(n)]

    # 2) Compute intermediate values.
    #
    #    For j = 5 to 0
    #     For i = n to 1
    #       B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i
    #       A = MSB(64, B)
    #       R[i] = LSB(64, B)
    for j in range(5, -1, -1):
        for i in range(n - 1, -1, -1):
            B = decipher.decrypt(A[0:7] + chr(ord(A[7]) ^ ((n * j) + i + 1)) +
                                 R[i])
            A = B[0:8]
            R[i] = B[8:16]

    # 3) Output results.
    #
    #    If A is an appropriate initial value (see 2.2.3),
    #    Then
    #      For i = 1 to n
    #        P[i] = R[i]
    #    Else
    #      Return an error
    for i in range(8):
        if ord(A[i]) != 0xA6:
            raise Exception('invalid/corrupted wrapped key or wrong kek')
    return ''.join(R)
Exemplo n.º 6
0
def UnwrapKey(key, kek):
    if len(key) > 32:
        # assume hex
        key = key.decode('hex')
    if len(kek) > 16:
        # assume hex
        kek = kek.decode('hex')

    if (len(key)% 8) or (len(kek) % 8):
        raise Exception('key and kek must be a multiple of 64 bits')

    # create a de-cipher with kek
    decipher = aes.rijndael(kek)

    # Inputs:  Ciphertext, (n+1) 64-bit values {C0, C1, ..., Cn}, and
    # Key, K (the KEK).
    # Outputs: Plaintext, n 64-bit values {P0, P1, K, Pn}.
    n = len(key)/8 - 1;
    if n < 1:
        raise Exception('wrapped key too short');

    # 1) Initialize variables.
    #
    #    Set A = C[0]
    #    For i = 1 to n
    #      R[i] = C[i]
    A = key[0:8]
    R = [key[(i+1)*8:(i+2)*8] for i in range(n)]

    # 2) Compute intermediate values.
    #
    #    For j = 5 to 0
    #     For i = n to 1
    #       B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i
    #       A = MSB(64, B)
    #       R[i] = LSB(64, B)
    for j in range(5, -1, -1):
        for i in range(n-1, -1, -1):
            B = decipher.decrypt(A[0:7] + chr(ord(A[7])^((n*j)+i+1))+R[i])
            A = B[0:8]
            R[i] = B[8:16]

    # 3) Output results.
    #
    #    If A is an appropriate initial value (see 2.2.3),
    #    Then
    #      For i = 1 to n
    #        P[i] = R[i]
    #    Else
    #      Return an error
    for i in range(8):
        if ord(A[i]) != 0xA6:
            raise Exception('invalid/corrupted wrapped key or wrong kek')
    return ''.join(R)
Exemplo n.º 7
0
    def __init__(self, headsetId=0, headset_type="research", vendor_id=0x21A1):
        headset_keys = {"research" : research_key,
                        "consumer" : consumer_key,
                        "special"  : special_key}
        
        self.rijn = rijndael(headset_keys[headset_type], 16)
        self._go_on = True
        self.packets = []

        if self.setup_win(headsetId, vendor_id) if windows else self.setup_posix():
            logger.info("Fine, connected to the Emotiv EPOC receiver")
        else:
            logger.error("Unable to connect to the Emotiv EPOC receiver :-(")
            raise UnableToConnect()
Exemplo n.º 8
0
def WrapKey(key, kek):
    if len(key) > 16:
        # assume hex
        key = bytes.fromhex(key)
    if len(kek) > 16:
        # assume hex
        kek = bytes.fromhex(kek)

    if (len(key)% 8) or (len(kek) % 8):
        raise Exception('key and kek must be a multiple of 64 bits')

    # create a cipher with kek
    cipher = aes.rijndael(kek)

    # Inputs:      Plaintext, n 64-bit values {P1, P2, ..., Pn}, and
    # Key, K (the KEK).
    # Outputs:     Ciphertext, (n+1) 64-bit values {C0, C1, ..., Cn}.
    n = len(key) // 8
    if n < 1:
        raise Exception('key too short')

    # 1) Initialize variables.
    #
    #    Set A = IV, an initial value (see 2.2.3)
    #      For i = 1 to n
    #      R[i] = P[i]
    A = bytes.fromhex('A6A6A6A6A6A6A6A6')
    R = [key[i*8:(i+1)*8] for i in range(n)]

    # 2) Calculate intermediate values.
    #
    #    For j = 0 to 5
    #      For i=1 to n
    #        B = AES(K, A | R[i])
    #        A = MSB(64, B) ^ t where t = (n*j)+i
    #        R[i] = LSB(64, B)
    for j in range(6):
        for i in range(n):
            B = cipher.encrypt(A+R[i])
            A = B[0:7] + bytes([B[7] ^ ((n*j)+i+1)])
            R[i] = B[8:16]

    # 3) Output the results.
    #
    #    Set C[0] = A
    #    For i = 1 to n
    #      C[i] = R[i]
    return b''.join([A]+R)
Exemplo n.º 9
0
def WrapKey(key, kek):
    if len(key) > 16:
        # assume hex
        key = key.decode('hex')
    if len(kek) > 16:
        # assume hex
        kek = kek.decode('hex')

    if (len(key)% 8) or (len(kek) % 8):
        raise Exception('key and kek must be a multiple of 64 bits')

    # create a cipher with kek
    cipher = aes.rijndael(kek)

    # Inputs:      Plaintext, n 64-bit values {P1, P2, ..., Pn}, and
    # Key, K (the KEK).
    # Outputs:     Ciphertext, (n+1) 64-bit values {C0, C1, ..., Cn}.
    n = len(key)/8;
    if n < 1: 
        raise Exception('key too short')

    # 1) Initialize variables.
    #
    #    Set A = IV, an initial value (see 2.2.3)
    #      For i = 1 to n
    #      R[i] = P[i]
    A = 'A6A6A6A6A6A6A6A6'.decode('hex');
    R = [key[i*8:(i+1)*8] for i in range(n)]

    # 2) Calculate intermediate values.
    #
    #    For j = 0 to 5
    #      For i=1 to n
    #        B = AES(K, A | R[i])
    #        A = MSB(64, B) ^ t where t = (n*j)+i
    #        R[i] = LSB(64, B)
    for j in range(6):
        for i in range(n):
            B = cipher.encrypt(A+R[i])
            A = B[0:7]+chr(ord(B[7]) ^ ((n*j)+i+1))
            R[i] = B[8:16]

    # 3) Output the results.
    #
    #    Set C[0] = A
    #    For i = 1 to n
    #      C[i] = R[i]
    return ''.join([A]+R)
Exemplo n.º 10
0
    def __init__(self, simulation='', key=consumer_key):
        self._simulation = True if simulation else False
        if self._simulation:
            with open(simulation) as f:
                lines = f.readlines()

            def line_has_reading(x):
                return len(x) > 0 and x[0] != '#' and len(x.split(' ')) == 18

            relevant_lines = filter(line_has_reading, lines)

            labels = ['counter', 'gyroX', 'gyroY'] + sensorlist

            def to_packet(line):
                packet_dict = dict(zip(labels, map(int, line.split(' ')[:-1])))
                return EmotivPacket(as_dict=packet_dict)

            self._generator = itertools.cycle(map(to_packet, relevant_lines))

            self._collect = self._generator.next

        else:
            self._setup_win() if windows else self._setup_posix()

            # haven't implemented key detection for windows yet, using default key
            if windows:
                self._rijn = rijndael(key, 16)
            else:
                self._detect_key_posix()

            self._collect = self._read_posix

        self._conn, self._reader_end = multiprocessing.Pipe()

        def reader():
            while True:
                packet = self._collect()
                if self._reader_end.poll():
                    self._reader_end.recv()
                    self._reader_end.send(packet)

        self._reader = multiprocessing.Process(target=reader)
        self._reader.start()
Exemplo n.º 11
0
        def __init__(self, simulation = '', key = consumer_key):
                self._simulation = True if simulation else False
                if self._simulation:
                        with open(simulation) as f:
                                lines = f.readlines()
                                
                        def line_has_reading(x):
                                return len(x) > 0 and x[0] != '#' and len(x.split(' ')) == 18

                        relevant_lines = filter(line_has_reading, lines)

                        labels = ['counter', 'gyroX', 'gyroY'] + sensorlist

                        def to_packet(line):
                                packet_dict = dict(zip(labels, map(int, line.split(' ')[:-1])))
                                return EmotivPacket(as_dict = packet_dict)

                        self._generator = itertools.cycle(map(to_packet, relevant_lines))
                        
                        self._collect = self._generator.next

                else:
                        self._setup_win() if windows else self._setup_posix()

                        # haven't implemented key detection for windows yet, using default key
                        if windows:
                                self._rijn = rijndael(key, 16)
                        else:
                                self._detect_key_posix()
                                        
                        self._collect = self._read_posix

                self._conn, self._reader_end = multiprocessing.Pipe()

                def reader():
                        while True:
                                packet = self._collect()
                                if self._reader_end.poll():
                                        self._reader_end.recv()
                                        self._reader_end.send(packet)

                self._reader = multiprocessing.Process(target = reader)
                self._reader.start()
Exemplo n.º 12
0
        def _detect_key_posix(self):
                for key_name, key in KEYS.items():
                        self._rijn = rijndael(key, 16)
                        successive = []
                        
                        # Read a couple of times because first readings are misleading
                        for i in range(10): self._read_posix()
                        
                        # Sample the raw data 20 times
                        for i in range(20):
                                fst = self._read_posix()
                                snd = self._read_posix()

                                # Account for the counter resetting
                                if fst.counter != 127 and fst.counter != 230:
                                        successive.append((fst, snd))

                        # If the counter increments correctly, we got the right key!
                        if all(map(lambda pair: pair[0].counter+1 == pair[1].counter, successive)):
                                self._key = key
                                return

                raise UnknownKey, "Cannot decrypt data: Unknown key."
Exemplo n.º 13
0
    def setupCrypto(self, sn, feature):
        type = 0  #feature[5]
        type &= 0xF
        type = type == 0

        k = ['\0'] * 16
        k[0] = sn[-1]
        k[1] = '\0'
        k[2] = sn[-2]
        if type:
            k[3] = 'H'
            k[4] = sn[-1]
            k[5] = '\0'
            k[6] = sn[-2]
            k[7] = 'T'
            k[8] = sn[-3]
            k[9] = '\x10'
            k[10] = sn[-4]
            k[11] = 'B'
        else:
            k[3] = 'T'
            k[4] = sn[-3]
            k[5] = '\x10'
            k[6] = sn[-4]
            k[7] = 'B'
            k[8] = sn[-1]
            k[9] = '\0'
            k[10] = sn[-2]
            k[11] = 'H'

        k[12] = sn[-3]
        k[13] = '\0'
        k[14] = sn[-4]
        k[15] = 'P'
        self.rijn = rijndael(''.join(k), 16)
        for i in k:
            print "0x%.02x " % (ord(i))
Exemplo n.º 14
0
  def setupCrypto(self, sn, feature):
    type = 0 #feature[5]
    type &= 0xF
    type = type == 0

    k = ['\0'] * 16
    k[0] = sn[-1]
    k[1] = '\0'
    k[2] = sn[-2]
    if type:
      k[3] = 'H'
      k[4] = sn[-1]
      k[5] = '\0'
      k[6] = sn[-2]
      k[7] = 'T'
      k[8] = sn[-3]
      k[9] = '\x10'
      k[10] = sn[-4]
      k[11] = 'B'
    else:
      k[3] = 'T'
      k[4] = sn[-3]
      k[5] = '\x10'
      k[6] = sn[-4]
      k[7] = 'B'
      k[8] = sn[-1]
      k[9] = '\0'
      k[10] = sn[-2]
      k[11] = 'H'
    
    k[12] = sn[-3]
    k[13] = '\0'
    k[14] = sn[-4]
    k[15] = 'P'
    self.rijn = rijndael(''.join(k), 16)
    for i in k: print "0x%.02x " % (ord(i))
Exemplo n.º 15
0
def ComputePlayReadyChecksum(kid, key):
    import aes
    return aes.rijndael(key).encrypt(kid)[:8]
Exemplo n.º 16
0
def test_ecb_encrypt():
    msg = bytes.fromhex("00112233445566778899aabbccddeeff")
    key = bytes.fromhex("000102030405060708090a0b0c0d0e0f")
    encrypted = aes.rijndael(key).encrypt(msg).hex()
    assert encrypted == "69c4e0d86a7b0430d8cdb78070b4c55a"
Exemplo n.º 17
0
try:
	import pywinusb.hid as hid
	windows = True
except:
	import hid
	windows = False
from aes import rijndael
import struct

key = '\x31\x00\x35\x54\x38\x10\x37\x42\x31\x00\x35\x48\x38\x00\x37\x50'
rijn = rijndael(key, 16)

channels = dict(
	L1=(9, 20), 
	L2=(5, 18), 
	L3=(31, 7), 
	L4=(2, -1), 
	L5=(2, -1), 
	L6=(28, -1), 
	L7=(23, -1), 
	
	R1=(0, -1), 
	R2=(0, -1), 
	R3=(0, -1), 
	R4=(28, -1), 
	R5=(17, -1), 
	R6=(0, -1), 
	R7=(0, -1), 
)

valid = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 31]
Exemplo n.º 18
0
def ComputePlayReadyChecksum(kid, key):
    import aes
    return aes.rijndael(key).encrypt(kid)[:8]
Exemplo n.º 19
0
import time
import pywinusb.hid as hid

from aes import rijndael

key = '\x31\x00\x35\x54\x38\x10\x37\x42\x31\x00\x35\x48\x38\x00\x37\x50'
rijn = rijndael(key, 16)

iv = [0] * 16


def decrypt(data):
    global iv
    dec = list(map(ord, rijn.decrypt(data[:16])))
    dec2 = list(map(ord, rijn.decrypt(data[16:])))
    data = list(map(ord, data))
    #dec2 = [data[i] ^ dec2[i] for i in range(16)]
    #dec = (dec[i] ^ iv[i] for i in range(16))
    #iv = map(ord, data[16:])
    return ''.join(map(chr, dec + dec2))


count = 0
last = 0


def sample_handler(data):
    global count, last
    assert data[0] == 0
    data = ''.join(map(chr, data[1:]))
    data = decrypt(data)