def send(self): ''' packs the Header vars into a buffer ''' unp = Packer() unp.pack_uint(self.magic) unp.pack_uint(self.version) unp.pack_uint(self.msgid) unp.pack_uint(self.msglen) unp.pack_uint(0) # reply_addr y reply_port unp.pack_uint(0) # reply_addr y reply_port unp.pack_fstring(8, self.callsign.encode()) return unp.get_buffer()
def generate_message(self, recipient_id, message): connect_info = self._session_keeper.get_connect_info(recipient_id) symmetric_key = connect_info['symmetric_key'] p = Packer() if connect_info.has_key('session_id_out'): p.pack_fstring(4, '\000\000\000\001') p.pack_fstring(SIZE_OF_UNIQS, connect_info['session_id_out']) else: #debugprint('including full header on message to %s\n', args=(recipient_id,), vs='mesgen') # XXX verbose p.pack_fstring(4, '\000\000\000\000') p.pack_string(connect_info['header']) iv = randsource.get(8) p.pack_fstring(8, iv) pdec = Packer() pdec.pack_string(message) # debugprint("------ ------ ------ ------ hmachish(key=%s, message=%s)\n" % (`symmetric_key`, `message`)) mac = cryptutil.hmacish(key=symmetric_key, message=message) pdec.pack_fstring(SIZE_OF_UNIQS, mac) encrypted = tripledescbc.new(symmetric_key).encrypt(iv, pdec.get_buffer()) p.pack_string(encrypted) return p.get_buffer()
def send(self): try: unp = Packer() unp.pack_fstring(96, self.model.encode()) unp.pack_double(self.time) unp.pack_double(self.lag) unp.pack_farray(3, self.position, unp.pack_double) unp.pack_farray(3, self.orientation, unp.pack_float) unp.pack_farray(3, self.linear_vel, unp.pack_float) unp.pack_farray(3, self.angular_vel, unp.pack_float) unp.pack_farray(3, self.linear_accel, unp.pack_float) unp.pack_farray(3, self.angular_accel, unp.pack_float) unp.pack_uint(0) self.properties.send(unp) msgbuf = unp.get_buffer() headbuf = self.header.send() self.header.msglen = len(headbuf + msgbuf) #print("setting len=",self.header.msglen, len(headbuf + msgbuf)) headbuf = self.header.send() #print("len2=",self.header.msglen, len(headbuf+msgbuf)) return headbuf + msgbuf except: llogger.exception("ERROR creating msg") print(self.model)
def __store_key(self, full_key): self.extres.db_env.nosyncerror_txn_checkpoint(MINS_BETWEEN_DB_CHECKPOINTS) trans = self.extres.db_env.txn_begin() try: key_id = sha(full_key).digest() if self.extres.counterparty_map.get(key_id, txn=trans, flags=db.DB_RMW) is not None : return id_in_rep = randsource.get(SIZE_OF_UNIQS) id_in = _mix_counterparties(self.__my_public_key_id, key_id, id_in_rep) id_out_rep = randsource.get(SIZE_OF_UNIQS) id_out = _mix_counterparties(self.__my_public_key_id, key_id, id_out_rep) key_seed = randsource.get(SIZE_OF_UNIQS) sr = HashRandom.SHARandom(_mix_counterparties(self.__my_public_key_id, key_id, key_seed)) symmetric_key = sr.get(SIZE_OF_SYMMETRIC_KEYS) iv = randsource.get(8) p = Packer() p.pack_fstring(SIZE_OF_UNIQS, key_id) x = MojoKey.makeRSAPublicKeyMVFromSexpString(full_key) padded = '\000' + cryptutil.oaep(symmetric_key, len(self.__key.get_modulus()) - 1) # The prepended 0 byte is to make modval happy. assert len(padded) == len(self.__key.get_modulus()) x.set_value_string(padded) x.encrypt() p.pack_string(x.get_value()) p.pack_fstring(8, iv) penc = Packer() penc.pack_string(self.__key.get_modulus()) penc.pack_fstring(SIZE_OF_UNIQS, id_out_rep) penc.pack_fstring(SIZE_OF_UNIQS, id_in_rep) penc.pack_fstring(SIZE_OF_UNIQS, key_seed) # debugprint("------ ------ ------ ------ hmachish(key=%s, message=%s)\n" % (`symmetric_key`, `penc.get_buffer()`)) hashie = cryptutil.hmacish(key=symmetric_key, message=penc.get_buffer()) paddedhashie = '\000' + cryptutil.oaep(hashie, len(self.__key.get_modulus()) - 1) # The prepended 0 byte is to make modval happy. assert len(paddedhashie) == len(self.__key.get_modulus()) self.__key.set_value_string(paddedhashie) self.__key.sign() signature = self.__key.get_value() penc.pack_fstring(len(signature), signature) encrypted = tripledescbc.new(symmetric_key).encrypt(iv, penc.get_buffer()) p.pack_string(encrypted) header = p.get_buffer() self.extres.counterparty_map.put(key_id, dumps([id_in, id_out, symmetric_key, header, full_key], 1), txn=trans) self.extres.session_map.put(id_in, full_key, txn=trans) trans.commit() trans = None finally: if trans is not None: trans.abort()