예제 #1
0
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(('', MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

while True:
    try:
        data, addr = sock.recvfrom(1024)

        other_key = tuple([int.from_bytes(x, byteorder='big') 
            for x in [data[:32], data[32:]]])

        dh = ECDH()

        if dh.check_public_key(other_key):
            dh.gen_private_key()
            dh.gen_public_key()
            ser_pub_key = b''.join([x.to_bytes(length=32, byteorder='big') 
                for x in dh.public_key])

            sock.sendto(ser_pub_key, addr)

            dh.gen_secret(other_key)
            print(hexlify(dh.gen_key()))

        else:
            sock.sendto(bytes('key error', 'UTF-8'), addr)
            print('key error')

    except KeyboardInterrupt:
예제 #2
0
파일: CmdHandlers.py 프로젝트: herrfz/ecdh
    def run(self):
        data = self.data

        # TODO: this "if" hasn't been tested
        if len(data) == 0 or len(data) != data[0] + 1:
            msg = wdc_error
            msg[2] = WRONG_CMD
            send_udp(msg, *(self.srv_udp_sock))
            logging.error('bad command')
            return

        if data[1] == 0x10:  # syn TDMA
            logging.debug('sync-ing WDC')

        elif data[1] == 0x11:  # start TDMA
            logging.debug('starting TDMA')

            wdc_get_tdma_res = bytearray(24)
            wdc_get_tdma_res[0] = data[0] + 1
            wdc_get_tdma_res[1] = 0x16
            wdc_get_tdma_res[2] = 0x01  # running
            wdc_get_tdma_res[3:] = data[2:]

            write_serial(wdc_get_tdma_res, status_file)

            msg = ack
            msg[1] = 0x12  # START_TDMA_REQ_ACK
            send_udp(msg, *self.srv_udp_sock)

        elif data[1] == 0x13:  # stop TDMA
            logging.debug('stopping TDMA')

            msg = ack
            msg[1] = 0x14  # STOP_TDMA_REQ_ACK
            send_udp(msg, *self.srv_udp_sock)

        elif data[1] == 0x15:  # TDMA status
            logging.debug('sending TDMA status response')

            wdc_get_tdma_res = read_serial(status_file)

            msg = wdc_get_tdma_res
            send_udp(msg, *self.srv_udp_sock)

        elif data[1] == 0x17:  # data request
            logging.debug('data request {}'.format(data))

            if data[2] in range(1, 9):  # key management messages
                ## TEST TEST TEST TEST TODO
                #params = data[3:196]
                dap = data[3:]

                if len(dap) != 64:
                    logging.error('wrong public key length')
                    return

                other_key = tuple([int.from_bytes(x, byteorder='big')
                    for x in [dap[:32], dap[32:]]])

                ecdh = ECDH()

                if ecdh.check_public_key(other_key):
                    ecdh.gen_private_key()
                    ecdh.gen_public_key()
                    ser_pub_key = b''.join([x.to_bytes(length=32,
                        byteorder='big') for x in ecdh.public_key])
                    ecdh.gen_secret(other_key)

                    send_udp(ser_pub_key, *(self.srv_udp_sock))

                    logging.info('key: {}'.format(hexlify(ecdh.gen_key())))

            elif data[2] in [0x09, 0x0A]:  # application unicast, broadcast
                pass

            else:
                msg = wdc_error
                msg[2] = WRONG_CMD
                send_udp(msg, *(self.srv_udp_sock))
                logging.error('wrong MAC_DATA_REQUEST mID')

        else:
            msg = wdc_error
            msg[2] = WRONG_CMD