Exemple #1
0
  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);
  }
Exemple #2
0
CompositeIntegerGroup::CompositeIntegerGroup(Integer n) :
    _n(n)
{
    // We pick the generator deterministically n
    Hash *hash = CryptoFactory::GetInstance().GetLibrary()->GetHashAlgorithm();
    QByteArray seed = hash->ComputeHash(n.GetByteArray());

    for(_s=3; ; _s = _s+1) {
        if(!_s.IsPrime()) continue;
        _p = (2 * _s * _n) + 1;

        if(_p.IsPrime()) break;
    }

    // Set g to some random element

    Integer g, h;
    const Integer e_test = 2*_s;
    for(Integer i=0; ; i = i+1) {
        h = (Integer(seed) + i) % _p;

        // Make sure that g generates a subgroup that is bigger
        // than order 2 and s and smaller than order P.
        // Since we do not know the factorization of n=qr,
        // we might be generating a subgroup of order q or r.
        if((g = h.Pow(e_test, _p)) != 1) break;
    }

    _g = Element(new IntegerElementData(g));

    /*
    qDebug() << "_p" << _p.GetByteArray().toHex();
    qDebug() << "_s" << _s.GetByteArray().toHex();
    */
};
Exemple #3
0
  QByteArray BulkRound::ProcessMessage(int des_idx, int msg_index)
  {
    int count = _messages.size();
    const Descriptor &des = GetDescriptors()[des_idx];
    int length = des.Length();
    QByteArray msg(length, 0);

    Hash hashalgo;
    bool good = true;

    for(int idx = 0; idx < count; idx++) {
      const char *tmsg = _messages[idx].constData() + msg_index;
      QByteArray xor_msg(QByteArray::fromRawData(tmsg, length));

      if(des.XorMessageHashes()[idx] != hashalgo.ComputeHash(xor_msg)) {
        qWarning() << "Xor message does not hash properly";
        _bad_message_hash.append(BadHash(des_idx, idx));
        good = false;
      }

      if(good) {
        Xor(msg, msg, xor_msg);
      }
    }

    if(good) {
      return msg;
    } else {
      return QByteArray();
    }
  }
Exemple #4
0
QByteArray IBEPrivateKey::Decrypt(const QByteArray &data) const
{
    QDataStream stream(data);

    //Receive the data and split it into three pieces
    QByteArray Ureceive;
    QByteArray Vreceive;
    QByteArray Wreceive;

    stream>>Ureceive>>Vreceive>>Wreceive;
    QByteArray Sigma;
    QByteArray M;

    Element U=this->_sysparam.GetGroup1()->ElementFromByteArray(Ureceive);
    //If U is not a element of Gourp1 then reject the ciphertext
    if(!this->_sysparam.GetGroup1()->IsElement(U)){
        return QByteArray();
    }

    Element PairDidU=this->_sysparam.GetGroupT()->ApplyPairing(this->_privatekey,U);
    QByteArray GTDidU=this->_sysparam.GetGroupT()->ElementToByteArray(PairDidU);
    Hash *hash = CryptoFactory::GetInstance().GetLibrary()->GetHashAlgorithm();
    //Get H2(e(dID,U))
    QByteArray HashGTDidU = hash->ComputeHash(GTDidU);

    Sigma = Utils::IBEUtils::calculateXor(Vreceive,HashGTDidU);
    //H4(sigma)
    QByteArray HashSigma = hash->ComputeHash(Sigma);
     //Get Message M
    M = Utils::IBEUtils::calculateXor(Wreceive,HashSigma);
    QByteArray hashSigmaM = Sigma;
    hashSigmaM.append(M);

    Integer r = Utils::IBEUtils::HashToZr(this->_sysparam.GetGroup1()->GetOrder(),hashSigmaM);
    Element generator = this->_sysparam.GetGroup1()->GetGenerator();

    if(U!= this->_sysparam.GetGroup1()->Exponentiate(generator,r)){
       return QByteArray();
     }
    free(hash);
     return M;
}
Exemple #5
0
  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();
  }
Exemple #6
0
  void BulkRound::PrepareBlameShuffle()
  {
    QSharedPointer<Network> net(GetNetwork()->Clone());
    QVariantHash headers = net->GetHeaders();
    headers["bulk"] = false;
    net->SetHeaders(headers);

    Hash hashalgo;
    QByteArray roundid = GetRoundId().GetByteArray();
    roundid = hashalgo.ComputeHash(roundid);
    roundid = hashalgo.ComputeHash(roundid);
    Id sr_id(roundid);

    _shuffle_round = _create_shuffle(GetGroup(), GetPrivateIdentity(), sr_id, net,
        _get_blame_data);

    _shuffle_round->SetSink(&_shuffle_sink);

    QObject::connect(_shuffle_round.data(), SIGNAL(Finished()),
        this, SLOT(BlameShuffleFinished()));
  }
Exemple #7
0
  void BulkRound::ProcessBlame(const QVector<BlameEntry> blame_vector)
  {
    Hash hashalgo;

    foreach(const BlameEntry &be, blame_vector) {
      if(!_bad_message_hash.contains(BadHash(be.first, be.second))) {
        qDebug() << "No knowledge of blame:" << be.first << be.second;
        continue;
      }

      const Descriptor &des = _descriptors[be.first];
      QByteArray msg(des.Length(), 0);
      CryptoRandom(be.third).GenerateBlock(msg);

      QByteArray hash = hashalgo.ComputeHash(msg);
      if(hash == des.XorMessageHashes()[be.second] && !_bad_members.contains(be.second)) {
        qDebug() << "Blame verified for" << be.first << be.second;
        _bad_members.append(be.second);
      } else {
        qDebug() << "Blame could not be verified for" << be.first << be.second;
      }
    }
  }
Exemple #8
0
int main(int argc, char **argv)
{
  QCoreApplication qca(argc, argv);
  QStringList args = QCoreApplication::arguments();

  Settings settings = Settings::CommandLineParse(args);
  if(settings.Help || !settings.IsValid()) {
    QTextStream qtout(stdout, QIODevice::WriteOnly);
    qtout << "usage: " << args[0] << " [options] [settings.conf]\n\n";
    qtout << "options:\n";
    qtout << Settings::GetUsage() << "\n";
    if(!settings.Help) {
      qtout << "error: " << settings.GetError() << "\n\n";
    }
    return -1;
  }
  Settings::ApplicationSettings = settings;

  QList<QSharedPointer<Node> > nodes;

  QSharedPointer<ISink> default_sink(new DummySink());
  QSharedPointer<SinkMultiplexer> app_sink(new SinkMultiplexer());

  QSharedPointer<CommandLine> commandline;
  QSharedPointer<SignalSink> signal_sink(new SignalSink());
  app_sink->AddSink(signal_sink.data());

  QSharedPointer<KeyShare> keys(new KeyShare(settings.PublicKeys));
  Hash hashalgo;
  foreach(const Id &server, settings.ServerIds) {
    QString serv = server.ToString();
    if(!keys->Contains(serv)) {
      qFatal("Missing key for %s", serv.toLatin1().data());
    }
    Q_ASSERT(Id(hashalgo.ComputeHash(keys->GetKey(serv)->GetByteArray())) == server);
  }

  QList<Address> local_end_points = settings.LocalEndPoints;

  for(int idx = 0; idx < settings.LocalNodeCount; idx++) {
    Id local_id = idx < settings.LocalId.count() ? settings.LocalId[idx] : Id();
    QSharedPointer<AsymmetricKey> key;

    QString key_path = settings.PrivateKeys + "/" + local_id.ToString();
    QFile key_file(key_path);
    if(key_file.exists()) {
      key = QSharedPointer<AsymmetricKey>(new DsaPrivateKey(key_path));
    } else {
      QByteArray id = local_id.GetByteArray();
      key = QSharedPointer<AsymmetricKey>(new DsaPrivateKey(id, true));
    }

    QSharedPointer<ISink> nsink = (idx == 0) ? app_sink.dynamicCast<ISink>() : default_sink;
    QSharedPointer<Overlay> overlay(new Overlay(local_id, local_end_points,
          settings.RemoteEndPoints, settings.ServerIds));
    overlay->SetSharedPointer(overlay);

    CreateRound create_round = RoundFactory::GetCreateRound(settings.RoundType);
    QSharedPointer<Session> session;
    if(settings.ServerIds.contains(local_id)) {
      session = MakeSession<ServerSession>(overlay, key, keys, create_round);
    } else {
      session = MakeSession<ClientSession>(overlay, key, keys, create_round);
    }
    session->SetSink(nsink.data());
    QSharedPointer<Node> node(new Node(key, keys, overlay, nsink, session));
    nodes.append(node);

    for(int idx = 0; idx < local_end_points.count(); idx++) {
      local_end_points[idx] = AddressFactory::GetInstance().
        CreateAny(local_end_points[idx].GetType());
    }
  }

  QScopedPointer<WebServer> ws;
//  QScopedPointer<SessionEntryTunnel> tun_entry;
//  QScopedPointer<SessionExitTunnel> tun_exit;

  if(settings.Console) {
    commandline = QSharedPointer<CommandLine>(new CommandLine(nodes));
    QObject::connect(&qca, SIGNAL(aboutToQuit()), commandline.data(), SLOT(Stop()));
    commandline->Start();
    app_sink->AddSink(commandline.data());
  }

  if(settings.WebServer) {
    ws.reset(new WebServer(settings.WebServerUrl));

    /* Stop Web server when application is about to quit */
    QObject::connect(&qca, SIGNAL(aboutToQuit()), ws.data(), SLOT(Stop()));

    /* When the web server stops, quit the application */
    QObject::connect(ws.data(), SIGNAL(Stopped()), &qca, SLOT(quit()));

    QSharedPointer<GetMessagesService> get_messages(new GetMessagesService());
    QObject::connect(signal_sink.data(), SIGNAL(IncomingData(const QByteArray&)),
        get_messages.data(), SLOT(HandleIncomingMessage(const QByteArray&)));
    ws->AddRoute(QHttpRequest::HTTP_GET, "/session/messages", get_messages);

    QSharedPointer<GetFileService> get_webpage(new GetFileService("index.html"));
    ws->AddRoute(QHttpRequest::HTTP_GET, "/web", get_webpage);

    QSharedPointer<GetDirectoryService> get_dir(new GetDirectoryService("webpath"));
    ws->AddRoute(QHttpRequest::HTTP_GET, "/dir", get_dir);

    QSharedPointer<SessionService> session_serv(new SessionService(nodes[0]->GetSession()));
    ws->AddRoute(QHttpRequest::HTTP_GET, "/session", session_serv);

    QSharedPointer<SendMessageService> send_message(new SendMessageService(nodes[0]->GetSession()));
    ws->AddRoute(QHttpRequest::HTTP_POST, "/session/send", send_message);

//    QSharedPointer<BuddiesService> bs(new BuddiesService(nodes[0]->GetSessionManager()));
//    ws->AddRoute(QHttpRequest::HTTP_GET, "/session/buddies", bs);

    ws->Start();
  }