def processKeyExchangeMessage(self, message): # When sending a message to someone we may end up in this function ourBaseKey = ratcheting_session.Curve().generateKeyPair(True) ourEphemeralKey = ratcheting_session.Curve().generateKeyPair(True) theirBaseKey = message.publicKey theirEphemeralKey = theirBaseKey theirIdentityKey = message.identityKey ourIdentityKey = IdentityKeyUtil().getIdentityKeyPair() #if self.sessionRecord.sessionState.needsRefresh: self.sessionRecord.sessionState.archiveCurrentState() #else: self.sessionRecord.sessionState.clear() ratcheting_session.initializeSession(self.sessionRecord.sessionState, ourBaseKey, theirBaseKey, ourEphemeralKey, theirEphemeralKey, ourIdentityKey, theirIdentityKey) self.sessionRecord.sessionState.setPendingPreKey( message.keyId, ourBaseKey.publicKey) self.sessionRecord.sessionState.setLocalRegistrationId(123) self.sessionRecord.sessionState.setRemoteRegistrationId( message.registrationId) self.sessionRecord.save()
def loadData(self): publicKey = ratcheting_session.Curve().decodePrivatePoint( PreKeyUtil().getPreKey(self.id)) privateKey = ratcheting_session.Curve().decodePrivatePoint( PreKeyUtil().getPreKeyPrivate(self.id)) self.publicKey = publicKey self.privateKey = privateKey self.keyPair = ECKeyPair(self.publicKey, self.privateKey)
def initFromSerialized(self, serialized): version = serialized[ 0] #is this int 34? because initparams is int 34... message = serialized[1:][:len(serialized) - 1 - self.MAC_LENGTH] mac = serialized[len(serialized) - 1 - self.MAC_LENGTH:][:self.MAC_LENGTH] if (version & 0xFF) >> 4 <= 1: raise "Legacy message: " + str((version & 0xFF) >> 4) if (version & 0xFF) >> 4 != 2: raise "Unknown version: " + str((version & 0xFF) >> 4) #if highBitsToInt(version) <= self.UNSUPPORTED_VERSION: # raise "Legacy Message: " + str(version) #if highBitsToInt(version) != self.CURRENT_VERSION: # raise "Unknown version: " + str(version) whisperMessage = WhisperTextProtocol_pb2.WhisperMessage() whisperMessage.ParseFromString(message) self.serialized = serialized self.senderEphemeral = ratcheting_session.Curve().decodePoint( whisperMessage.ephemeralKey) self.counter = whisperMessage.counter self.previousCounter = whisperMessage.previousCounter self.ciphertext = whisperMessage.ciphertext
def createPreKeyListFromJson(serialized): data = json.loads(serialized.decode('utf-8')) keys = list() for key in data['keys']: publicKey = ratcheting_session.Curve().decodePoint( base64.b64decode(key['publicKey'])) identityKey = ratcheting_session.Curve().decodePoint( base64.b64decode(key['identityKey'])) keys.append( PreKeyEntity(key['deviceId'], key['keyId'], publicKey, identityKey, key['registrationId'])) preKeyList = PreKeyList('', keys) return preKeyList
def initFromSerialized(self, serialized): self.version = (serialized[0] & 0xFF) >> 4 if self.version > self.CURRENT_VERSION: raise "Unknown version: " + str(self.version) preKeyWhisperMessage = WhisperTextProtocol_pb2.PreKeyWhisperMessage() preKeyWhisperMessage.ParseFromString(serialized[1:]) self.serialized = serialized self.registrationId = preKeyWhisperMessage.registrationId self.preKeyId = preKeyWhisperMessage.preKeyId self.baseKey = ratcheting_session.Curve().decodePoint( preKeyWhisperMessage.baseKey) #decode point...? self.identityKey = ratcheting_session.Curve().decodePoint( preKeyWhisperMessage.identityKey) self.message = WhisperMessage(serialized=preKeyWhisperMessage.message)
def getReceiverChain(self, senderEphemeral): receiverChains = self.sessionStructure.receiverChains index = 0 for receiverChain in receiverChains: chainSenderEphemeral = ratcheting_session.Curve().decodePoint( receiverChain.senderEphemeral, 0) if chainSenderEphemeral.publicKey == senderEphemeral.publicKey: return (receiverChain, index) index += 1 return None
def getOrCreateChainKey(self, sessionState, theirEphemeral): if sessionState.hasReceiverChain(theirEphemeral): return sessionState.getReceiverChainKey(theirEphemeral) rootKey = sessionState.getRootKey() ourEphemeral = sessionState.getSenderEphemeralPair() receiverChain = rootKey.createChain(theirEphemeral.publicKey, ourEphemeral) ourNewEphemeral = ratcheting_session.Curve().generateKeyPair(True) senderChain = receiverChain[0].createChain(theirEphemeral.publicKey, ourNewEphemeral) sessionState.setRootKey(senderChain[0]) sessionState.addReceiverChain(theirEphemeral, receiverChain[1]) if sessionState.getSenderChainKey().getIndex() - 1 != -1: sessionState.setPreviousCounter( sessionState.getSenderChainKey().getIndex() - 1) sessionState.setSenderChain(ourNewEphemeral, senderChain[1]) return receiverChain[1]
def getSenderEphemeralPair(self): import receive_textsecure publicKey = self.getSenderEphemeral() privateKey = ratcheting_session.Curve().decodePrivatePoint( self.sessionStructure.senderChain.senderEphemeralPrivate) return receive_textsecure.ECKeyPair(publicKey, privateKey)
def getSenderEphemeral(self): return ratcheting_session.Curve().decodePoint( self.sessionStructure.senderChain.senderEphemeral, 0)
def getPendingPreKey(self): return (self.sessionStructure.pendingPreKey.preKeyId, ratcheting_session.Curve().decodePoint( self.sessionStructure.pendingPreKey.baseKey))