Ejemplo n.º 1
0
def on_msg_server(client, userdata, msg):
    print('on_msg_server')
    if userdata['established'] == True:
        ptxt = pyhy.hydro_secretbox_decrypt(msg.payload, 0, CTX,
                                            userdata['session_kp'].rx)
        if ptxt is not None:
            print('Rx > %s' % ptxt.decode())
            ctxt = pyhy.hydro_secretbox_encrypt(
                'You sent: "%s"' % ptxt.decode(), 0, CTX,
                userdata['session_kp'].tx)
            client.publish(SERVER_PUB_TOPIC, ctxt)
        else:
            userdata['established'] = False
            userdata['state'] = 0
    else:
        ################################## N ###################################
        if userdata['type'] == 'n':
            pkt1 = msg.payload
            server_kp = get_current_kp(userdata)
            session_kp_server = pyhy.hydro_kx_n_2(server_kp, pkt1)
            # pyhy.dump_session_keypair_hex(session_kp_server)
            userdata['session_kp'] = session_kp_server
            userdata['established'] = True
        ################################## KK ##################################
        elif userdata['type'] == 'kk':
            pkt1 = msg.payload
            server_kp = get_current_kp(userdata)
            session_kp_server, pkt2 = pyhy.hydro_kx_kk_2(
                pkt1, pyhy.unhexify(userdata['kp']['client-pk']), server_kp)
            userdata['session_kp'] = session_kp_server
            userdata['established'] = True
            publish.single(SERVER_PUB_TOPIC, pkt2, hostname=MQTT_HOST)
        ################################## XX ##################################
        elif userdata['type'] == 'xx':
            if userdata['state'] == 0:
                pkt1 = msg.payload
                xx_server = userdata['kx']  # kx client created on init
                server_kp = get_current_kp(userdata)
                pkt2 = xx_server.xx_2(pkt1, server_kp)
                userdata['state'] = 2
                publish.single(SERVER_PUB_TOPIC, pkt2, hostname=MQTT_HOST)
                print('Server (xx) pkt2 sent')
            elif userdata['state'] == 2:
                pkt3 = msg.payload
                xx_server = userdata['kx']  # kx client created on init
                (session_kp_server, peer_pk_client) = xx_server.xx_4(pkt3)
                assert session_kp_server is not None
                userdata['session_kp'] = session_kp_server
                userdata['state'] = 0
                userdata['established'] = True
                if peer_pk_client is not None:
                    print('Discovered a (client) peer: %s' %
                          peer_pk_client.hex())
                print('Server (xx) - Established')
        else:
            print('missing/invalid type in userdata')
            sys.exit(1)
Ejemplo n.º 2
0
def poll_client(client):
    global prev_ms
    now = int(round(time.time() * 1000))
    if ( (now - prev_ms) > TX_DELAY_MS ):
        prev_ms = now
        print('Client Tx')
        if client._userdata['established']:
            ctxt = pyhy.hydro_secretbox_encrypt(str('Testing %d' % now), 0, CTX, client._userdata['session_kp'].tx)
            client.publish(CLIENT_PUB_TOPIC, ctxt)
    return
Ejemplo n.º 3
0
def encrypt(keys, data):
    return pyhy.hydro_secretbox_encrypt(data, 0, MBOOT_PACK_HYDRO_CONTEXT,
                                        keys.secretbox)
Ejemplo n.º 4
0
 def encrypt_adv_msg(self, meta):
     raw_msg = xdrm.dumps(meta)
     signed_msg = self.handler.keypair.sign(raw_msg)
     return pyhy.hydro_secretbox_encrypt(signed_msg, 0, CTX,
                                         self.handler.netkey)
Ejemplo n.º 5
0
 def encrypt(self, plain_frame):
     return pyhy.hydro_secretbox_encrypt(plain_frame, 0, CTX,
                                         self.session_kp.tx)