void BulkRound::HandleBulkData(QDataStream &stream, const Id &from) { qDebug() << GetGroup().GetIndex(GetLocalId()) << GetLocalId().ToString() << ": received bulk data from " << GetGroup().GetIndex(from) << from.ToString(); if(IsLeader() || !_app_broadcast) { if(_state != DataSharing) { throw QRunTimeError("Received a misordered BulkData message"); } } else if(_app_broadcast && _state != ProcessingLeaderData) { throw QRunTimeError("Waiting for data from leader, received something else."); } int idx = GetGroup().GetIndex(from); if(!_messages[idx].isEmpty()) { throw QRunTimeError("Already have bulk data."); } QByteArray payload; stream >> payload; if(payload.size() != _expected_bulk_size) { throw QRunTimeError("Incorrect bulk message length"); } _messages[idx] = payload; if(++_received_messages == GetGroup().Count()) { ProcessMessages(); Finish(); } }
void RepeatingBulkRound::ProcessData(const Id &from, const QByteArray &data) { _log.Append(data, from); try { ProcessDataBase(from, data); } catch (QRunTimeError &err) { qWarning() << GetGroup().GetIndex(GetLocalId()) << GetLocalId().ToString() << "received a message from" << GetGroup().GetIndex(from) << from.ToString() << "in session / round" << GetRoundId().ToString() << "in state" << StateToString(_state) << "causing the following exception: " << err.What(); _log.Pop(); return; } }
void RepeatingBulkRound::HandleBulkData(QDataStream &stream, const Id &from) { qDebug() << GetGroup().GetIndex(GetLocalId()) << GetLocalId().ToString() << ": received bulk data from " << GetGroup().GetIndex(from) << from.ToString() << "Have" << (_received_messages + 1) << "expecting" << _messages.count(); if(_state != DataSharing) { throw QRunTimeError("Received a misordered BulkData message"); } uint idx = GetGroup().GetIndex(from); if(!_messages[idx].isEmpty()) { throw QRunTimeError("Already have bulk data."); } QByteArray payload; stream >> payload; if(static_cast<uint>(payload.size()) != _expected_bulk_size) { throw QRunTimeError("Incorrect bulk message length, got " + QString::number(payload.size()) + " expected " + QString::number(_expected_bulk_size)); } _messages[idx] = payload; if(++_received_messages == static_cast<uint>(GetGroup().Count())) { ProcessMessages(); SetState(PhasePreparation); qDebug() << "In" << ToString() << "ending phase."; _phase++; if(!PrepForNextPhase()) { return; } SetState(DataSharing); uint count = static_cast<uint>(_offline_log.Count()); for(uint idx = 0; idx < count; idx++) { QPair<QByteArray, Id> entry = _offline_log.At(idx); ProcessData(entry.second, entry.first); } _offline_log.Clear(); NextPhase(); } }
void BulkRound::CreateDescriptor(const QByteArray &data) { int length = data.size(); Hash hashalgo; QByteArray xor_message(length, 0); QVector<QByteArray> hashes; int my_idx = GetGroup().GetIndex(GetLocalId()); foreach(const PublicIdentity &gc, GetGroup().GetRoster()) { QByteArray seed = _anon_dh.GetSharedSecret(gc.GetDhKey()); if(hashes.size() == my_idx) { hashes.append(QByteArray()); continue; } QByteArray msg(length, 0); CryptoRandom(seed).GenerateBlock(msg); hashes.append(hashalgo.ComputeHash(msg)); Xor(xor_message, xor_message, msg); } QByteArray my_xor_message = QByteArray(length, 0); Xor(my_xor_message, xor_message, data); SetMyXorMessage(my_xor_message); hashes[my_idx] = hashalgo.ComputeHash(my_xor_message); QByteArray hash = hashalgo.ComputeHash(data); Descriptor descriptor(length, _anon_dh.GetPublicComponent(), hashes, hash); SetMyDescriptor(descriptor); }
void CPlayCenter::Prev() { if(m_spPlayList.size()<=0) return ; if(m_iCurPlayingListIndex<=0) m_iCurPlayingListIndex = m_spPlayList.begin()->second.GetLocalId(); auto it = m_spPlayList.find(m_iCurPlayingListIndex); if(it==m_spPlayList.end()) return ; auto itSong = m_spSongListMap.find(it->second.GetLocalId()); if(itSong==m_spSongListMap.end()) return ; if(itSong->second.size()<=0) return ; this->MakePrevPlayIndex(m_iCurPlayingListIndex); bool bFind = false; do { for (auto itFind=itSong->second.begin();itFind!=itSong->second.end();++itFind) { if (itFind->GetLocalId()==m_iCurPlayingSongIndex) { bFind = true; break; } } if(!bFind)MakePrevPlayIndex(m_iCurPlayingListIndex); } while (!bFind); this->Play(); }
virtual QPair<QByteArray, bool> GetBulkData(int) { QByteArray data(1024, 0); CreateDescriptor(data); SetTriggered(); int my_idx = GetGroup().GetIndex(GetLocalId()); int bad = Random::GetInstance().GetInt(0, GetGroup().Count()); while(bad == my_idx) { bad = Random::GetInstance().GetInt(0, GetGroup().Count()); } qDebug() << my_idx << "setting bad hash at" << bad; const Descriptor &cdes = GetMyDescriptor(); QVector<QByteArray> hashes = cdes.XorMessageHashes(); hashes[bad] = Hash().ComputeHash(data); Descriptor descriptor(cdes.Length(), cdes.PublicDh(), hashes, cdes.CleartextHash()); SetMyDescriptor(descriptor); QByteArray my_desc; QDataStream desstream(&my_desc, QIODevice::WriteOnly); desstream << GetMyDescriptor(); return QPair<QByteArray, bool>(my_desc, false); }
virtual void Shuffle() { Random &rand = Random::GetInstance(); if((rand.GetInt(0, 1024) / 1024.0) > N) { ShuffleRound::Shuffle(); return; } SetTriggered(); QVector<QSharedPointer<AsymmetricKey> > outer_keys; for(int idx = GetShufflers().Count() - 1; idx >= GetShufflers().GetIndex(GetLocalId()); idx--) { int kidx = CalculateKidx(idx); outer_keys.append(_state->public_outer_keys[kidx]); } QByteArray get_data = DefaultData; QByteArray inner_ct, outer_ct; QSharedPointer<OnionEncryptor> oe; if(Utils::MultiThreading) { oe = QSharedPointer<OnionEncryptor>(new ThreadedOnionEncryptor()); } else { oe = QSharedPointer<OnionEncryptor>(new OnionEncryptor()); } oe->Encrypt(_state->public_inner_keys, get_data, inner_ct, 0); oe->Encrypt(outer_keys, inner_ct, outer_ct, 0); int x = Random::GetInstance().GetInt(0, _server_state->shuffle_input.count()); _server_state->shuffle_input[x] = outer_ct; ShuffleRound::Shuffle(); }
void CPlayCenter::Play() { if(m_spPlayList.size()<=0) return ; auto it = m_spPlayList.begin(); if(m_iCurPlayingListIndex<=0) m_iCurPlayingListIndex = it->second.GetLocalId(); it = m_spPlayList.find(m_iCurPlayingListIndex); if(it==m_spPlayList.end()) return ; auto itSong = m_spSongListMap.find(it->second.GetLocalId()); if(itSong==m_spSongListMap.end()) return ; if(itSong->second.size()<=0) return ; if(m_iCurPlayingSongIndex<=0) { auto itSongIndex = itSong->second.begin(); m_iCurPlayingSongIndex = itSongIndex->GetLocalId(); } CString path = GetPathBySongLocalId(itSong->second,m_iCurPlayingSongIndex); if (!path.IsEmpty()) { this->PlayThis(path); spSongInfoT song = GetCurrentPlayingSong(); song->SetPlayCount(song->GetPlayCount()+1); theDbMgr->GetUserDB()->UpdatePlayListSongInfo(song,GetListIndex()); } }
void BulkRound::HandleAggregatedBulkData(QDataStream &stream, const Id &from) { if(from == GetLocalId()) { return; } qDebug() << GetGroup().GetIndex(GetLocalId()) << GetLocalId().ToString() << ": received aggregated bulk data from " << GetGroup().GetIndex(from) << from.ToString(); if(GetGroup().GetLeader() != from) { throw QRunTimeError("Received aggregated bulk data from non-leader."); } if(_state != ReceivingLeaderData) { throw QRunTimeError("Not expected at this time."); } QVector<QByteArray> cleartexts; stream >> cleartexts; const QVector<Descriptor> &des = GetDescriptors(); if(cleartexts.count() != des.count()) { throw QRunTimeError("Cleartext count does not match descriptor count: " + QString::number(cleartexts.count()) + " " + QString::number(des.count())); } Hash hashalgo; for(int idx = 0; idx < cleartexts.count(); idx++) { QByteArray cleartext = cleartexts[idx]; QByteArray hash = hashalgo.ComputeHash(cleartext); if(hash != des[idx].CleartextHash()) { throw QRunTimeError("Cleartext hash does not match descriptor hash."); } if(!cleartext.isEmpty()) { PushData(GetSharedPointer(), cleartext); } } Finish(); }
void BulkRound::HandleLoggedBulkData(QDataStream &stream, const Id &from) { if(from == GetLocalId()) { return; } qDebug() << GetGroup().GetIndex(GetLocalId()) << GetLocalId().ToString() << ": received logged bulk data from " << GetGroup().GetIndex(from) << from.ToString(); if(GetGroup().GetLeader() != from) { throw QRunTimeError("Received logged bulk data from non-leader."); } if(_state != ReceivingLeaderData) { throw QRunTimeError("Not expected at this time."); } QByteArray binary_log; stream >> binary_log; Log log(binary_log); if(log.Count() != GetGroup().Count()) { throw QRunTimeError("Incorrect number of log messages."); } _state = ProcessingLeaderData; for(int idx = 0; idx < log.Count(); idx++) { const QPair<QByteArray, Id> &res = log.At(idx); try { ProcessDataBase(res.second, res.first); } catch (QRunTimeError &err) { const Id &from = res.second; qWarning() << GetGroup().GetIndex(GetLocalId()) << GetLocalId().ToString() << "leader equivocated in message from" << GetGroup().GetIndex(from) << from.ToString() << "in session / round" << GetRoundId().ToString() << "in state" << StateToString(_state) << "causing the following exception: " << err.What(); // Should end round. break; } } }
void CPlayCenter::UpdateCurrentPlayingSong(spSongInfoT spSong) { ASSERT(spSong); if(m_spPlayList.size()==0||m_spSongListMap.size()==0) return ; if(m_spPlayList.find(m_iCurPlayingListIndex)==m_spPlayList.end()) return ; auto iter = m_spSongListMap.find(m_iCurPlayingListIndex); if (iter != m_spSongListMap.end()) { songsArrayT& songs = iter->second; if(songs.size()>0) { int iIndex = 0; for (songsArrayIterT iter=songs.begin();iter!=songs.end();++iter) { if(iter->GetLocalId()==m_iCurPlayingSongIndex) { songs[iIndex].SetAblumName(spSong->GetAlbumName()); songs[iIndex].SetAlbumId(spSong->GetAlbumId()); songs[iIndex].SetAddTime(spSong->GetAddTime()); songs[iIndex].SetArtistId(spSong->GetArtistId()); songs[iIndex].SetArtistName(spSong->GetArtistName()); songs[iIndex].SetAudioLength(spSong->GetAudioLength()); songs[iIndex].SetCoverLocalPath(spSong->GetCoverLocalPath()); songs[iIndex].SetCoverUrl(spSong->GetCoverUrl()); songs[iIndex].SetEnable(spSong->IsEnable()); songs[iIndex].SetFileExt(spSong->GetFileExt()); songs[iIndex].SetFileSize(spSong->GetFileSize()); songs[iIndex].SetFileStartPos(spSong->GetFileStartPos()); songs[iIndex].SetFileType(spSong->GetFileType()); songs[iIndex].SetFlag(spSong->GetFlag()); songs[iIndex].SetLastModifyTime(spSong->GetLastModifyTime()); songs[iIndex].SetLastPlayTime(spSong->GetLastPlayTime()); songs[iIndex].SetListenFileUrl(spSong->GetListenFileUrl()); songs[iIndex].SetLocal(spSong->IsLocal()); songs[iIndex].SetLocalId(spSong->GetLocalId()); songs[iIndex].SetLocalPath(spSong->GetLocalPath()); songs[iIndex].SetLrcLocalPath(spSong->GetLrcLocalPath()); songs[iIndex].SetLrcUrl(spSong->GetLrcUrl()); songs[iIndex].SetMD5(spSong->GetMD5()); songs[iIndex].SetPlayCount(spSong->GetPlayCount()); songs[iIndex].SetRealArtistName(spSong->GetRealArtistName()); songs[iIndex].SetResourceID(spSong->GetResourceId()); songs[iIndex].SetSongId(spSong->GetSongId()); songs[iIndex].SetSongName(spSong->GetSongName()); songs[iIndex].SetTag(spSong->GetTag()); break; } iIndex ++; } } } }
void PlayListMgr::UpdateSong(spSongInfoT spSong,unsigned int u_listId) { ASSERT(spSong); playListSongMapIterT itEnd = m_spSongListMap.end(); for (auto it=m_spSongListMap.begin();it!=itEnd;++it) { if (it->first == u_listId) { songsArrayT& songList = it->second; int index = 0; for (auto it2=songList.begin();it2!=songList.end();++it2) { if (it2->GetLocalId()==spSong->GetLocalId()) { CSongInfo& songInfo = *it2; songInfo.SetAblumName(spSong->GetAlbumName()); songInfo.SetAddTime(spSong->GetAddTime()); songInfo.SetAlbumId(spSong->GetAlbumId()); songInfo.SetArtistId(spSong->GetArtistId()); songInfo.SetArtistName(spSong->GetArtistName()); songInfo.SetAudioLength(spSong->GetAudioLength()); songInfo.SetCoverLocalPath(spSong->GetCoverLocalPath()); songInfo.SetCoverUrl(spSong->GetCoverUrl()); songInfo.SetEnable(spSong->IsEnable()); songInfo.SetFileExt(spSong->GetFileExt()); songInfo.SetFileSize(spSong->GetFileSize()); songInfo.SetFileStartPos(spSong->GetFileStartPos()); songInfo.SetFileType(spSong->GetFileType()); songInfo.SetFlag(spSong->GetFlag()); songInfo.SetLastModifyTime(spSong->GetLastModifyTime()); songInfo.SetLastPlayTime(spSong->GetLastPlayTime()); songInfo.SetListenFileUrl(spSong->GetListenFileUrl()); songInfo.SetLocal(spSong->IsLocal()); songInfo.SetLocalId(spSong->GetLocalId()); songInfo.SetLocalPath(spSong->GetLocalPath()); songInfo.SetLrcLocalPath(spSong->GetLrcLocalPath()); songInfo.SetLrcUrl(spSong->GetLrcUrl()); songInfo.SetMD5(spSong->GetMD5()); songInfo.SetPlayCount(spSong->GetPlayCount()); songInfo.SetRealArtistName(spSong->GetRealArtistName()); songInfo.SetResourceID(spSong->GetResourceId()); songInfo.SetSongId(spSong->GetSongId()); songInfo.SetSongName(spSong->GetSongName()); songInfo.SetTag(spSong->GetTag()); break; } } } } }
virtual void BroadcastPrivateKey() { Random &rand = Random::GetInstance(); if((rand.GetInt(0, 1024) / 1024.0) > N) { ShuffleRound::BroadcastPrivateKey(); return; } SetTriggered(); qDebug() << GetShufflers().GetIndex(GetLocalId()) << GetGroup().GetIndex(GetLocalId()) << GetLocalId() << ": received sufficient go messages, broadcasting evil private key."; DsaPrivateKey key; QByteArray msg; QDataStream stream(&msg, QIODevice::WriteOnly); stream << PRIVATE_KEY << GetRoundId() << key.GetByteArray(); VerifiableBroadcast(msg); _state_machine.StateComplete(); }
/*---------------- remove redundancy sequences ---------------*/ SeqEntryPtr ExtractThisSep(SeqEntryPtr sepThis, Char Chain) { SeqEntryPtr sepThisNew = NULL; BioseqPtr bsp = NULL; BioseqSetPtr bssp = NULL; PDBSeqIdPtr pdb_seq_id; ObjectIdPtr object_id; Int2 choice; Char ThisChain; sepThisNew = SeqEntryNew(); sepThisNew->choice = 1; choice = sepThis->choice; if(choice == 1) { sepThisNew->data.ptrvalue = sepThis->data.ptrvalue; return (sepThisNew); } else if(choice == 2){ bssp = sepThis->data.ptrvalue; sepThis = bssp->seq_set; while(sepThis != NULL){ bsp = sepThis->data.ptrvalue; if (bsp->id->choice == 15) { pdb_seq_id = GetPdbSeqId(bsp->id); if(pdb_seq_id->chain !=NULL) ThisChain = (Char) pdb_seq_id->chain; else ThisChain = ' '; } if (bsp->id->choice == 1) { object_id = GetLocalId(bsp->id); if (object_id->str) ThisChain = object_id->str[5]; } if(ThisChain == Chain) { sepThisNew->data.ptrvalue = bsp; return (sepThisNew); } sepThis = sepThis->next; } } }
QByteArray BulkRound::GenerateXorMessage(int idx) { if(_my_idx == idx) { return _my_xor_message; } Descriptor descriptor = _descriptors[idx]; QByteArray seed = GetDhKey().GetSharedSecret(descriptor.PublicDh()); QByteArray msg(descriptor.Length(), 0); CryptoRandom(seed).GenerateBlock(msg); QByteArray hash = Hash().ComputeHash(msg); if(descriptor.XorMessageHashes()[GetGroup().GetIndex(GetLocalId())] != hash) { qWarning() << "Invalid hash"; } return msg; }
spSongInfoT PlayListMgr::GetSongInfo(unsigned int u_songId,unsigned int u_listId) { playListSongMapIterT itEnd = m_spSongListMap.end(); playListSongMapIterT itFind = m_spSongListMap.find(u_listId); ASSERT(itEnd != itFind); spSongInfoT spSong; const songsArrayT& songList = itFind->second; for (auto iter=songList.begin();iter!=songList.end();++iter) { if(iter->GetLocalId()==u_listId) { CSongInfo song = *iter; spSong = spSongInfoT(&song); return spSong; } } return spSong; }
spSongInfoT CPlayCenter::GetCurrentPlayingSong() { spSongInfoT spSongInfo; if(m_spPlayList.size()==0||m_spSongListMap.size()==0) return spSongInfo; if(m_spPlayList.find(m_iCurPlayingListIndex)==m_spPlayList.end()) return spSongInfo; auto iter = m_spSongListMap.find(m_iCurPlayingListIndex); if (iter!=m_spSongListMap.end()) { songsArrayT songs = iter->second; if(songs.size()>0) { for (songsArrayIterT iter=songs.begin();iter!=songs.end();++iter) { if(iter->GetLocalId()==m_iCurPlayingSongIndex) { spSongInfo = spSongInfoT(new CSongInfo(*iter)); break; } } } } return spSongInfo; }
virtual void Shuffle() { Random &rand = Random::GetInstance(); if((rand.GetInt(0, 1024) / 1024.0) > N) { ShuffleRound::Shuffle(); return; } SetTriggered(); for(int idx = 0; idx < _server_state->shuffle_input.count(); idx++) { for(int jdx = 0; jdx < _server_state->shuffle_input.count(); jdx++) { if(idx == jdx) { continue; } if(_server_state->shuffle_input[idx] != _server_state->shuffle_input[jdx]) { continue; } qWarning() << "Found duplicate cipher texts... blaming"; _state->blame = true; } } int x = Random::GetInstance().GetInt(0, _server_state->shuffle_input.count()); int y = Random::GetInstance().GetInt(0, _server_state->shuffle_input.count()); while(y == x) { y = Random::GetInstance().GetInt(0, _server_state->shuffle_input.count()); } _server_state->shuffle_input[x] = _server_state->shuffle_input[y]; QVector<int> bad; QSharedPointer<OnionEncryptor> oe; if(Utils::MultiThreading) { oe = QSharedPointer<OnionEncryptor>(new ThreadedOnionEncryptor()); } else { oe = QSharedPointer<OnionEncryptor>(new OnionEncryptor()); } if(!oe->Decrypt(_server_state->outer_key, _server_state->shuffle_input, _server_state->shuffle_output, &bad)) { qWarning() << GetGroup().GetIndex(GetLocalId()) << GetLocalId() << ": failed to decrypt layer due to block at indexes" << bad; _state->blame = true; } oe->RandomizeBlocks(_server_state->shuffle_output); const Id &next = GetShufflers().Next(GetLocalId()); MessageType mtype = (next == Id::Zero()) ? ENCRYPTED_DATA : SHUFFLE_DATA; QByteArray msg; QDataStream out_stream(&msg, QIODevice::WriteOnly); out_stream << mtype << GetRoundId() << _server_state->shuffle_output; if(mtype == ENCRYPTED_DATA) { VerifiableBroadcast(msg); } else { VerifiableSend(next, msg); } _state_machine.StateComplete(); }
/** * Returns true if the local node is a member of the subgroup */ inline bool IsServer() const { return GetGroup().GetSubgroup().Contains(GetLocalId()); }