def createDataMessage(self, message, flags=0, tlvs=None): # check MSGSTATE if self.theirKeyid == 0: raise InvalidParameterError if tlvs is None: tlvs = [] sess = self.sessionkeys[1][0] sess.sendctr.inc() logger.debug('create: enc={0!r} mac={1!r} ctr={2!r}' \ .format(sess.sendenc, sess.sendmac, sess.sendctr)) # plaintext + TLVS plainBuf = message + b'\0' + b''.join([ bytes(t) for t in tlvs]) encmsg = AESCTR(sess.sendenc, sess.sendctr).encrypt(plainBuf) msg = proto.DataMessage(flags, self.ourKeyid-1, self.theirKeyid, long_to_bytes(self.ourDHKey.pub), sess.sendctr.byteprefix(), encmsg, b'', b''.join(self.savedMacKeys)) self.savedMacKeys = [] msg.mac = SHA1HMAC(sess.sendmac, msg.getMacedData()) return msg
def testGenericMsg(self): msg = base64.b64encode(proto.pack_data(b'foo')) self.assertEqual(b'foo', proto.DHKey.parsePayload(msg).gy) self.assertEqual(b'?OTR:AAIK' + msg + b'.', bytes(proto.DHKey(b'foo'))) msg = base64.b64encode(b'\x42\1\3\3\1\x08\6\4\2' + proto.pack_data(b'foo') + b'\0\0\0\0\xde\xad\xbe\xef' + proto.pack_data(b'encoded_dummy') + b'this is a dummy mac\0' + b'\0\0\0\0') pMsg = proto.DataMessage.parsePayload(msg) self.assertEqual(0x42, pMsg.flags) self.assertEqual(0x01030301, pMsg.skeyid) self.assertEqual(0x08060402, pMsg.rkeyid) self.assertEqual(b'foo', pMsg.dhy) self.assertEqual(b'\0\0\0\0\xde\xad\xbe\xef', pMsg.ctr) self.assertEqual(b'encoded_dummy', pMsg.encmsg) self.assertEqual(b'this is a dummy mac\0', pMsg.mac) self.assertEqual(b'', pMsg.oldmacs) self.assertEqual(b'?OTR:AAID' + msg + b'.', bytes(proto.DataMessage(0x42, 0x01030301, 0x08060402, b'foo', b'\0\0\0\0\xde\xad\xbe\xef', b'encoded_dummy', b'this is a dummy mac\0', b'')))