SignedPreKeyRecord::SignedPreKeyRecord(uint64_t id, uint64_t timestamp, const ECKeyPair &keyPair, const ByteArray &signature)
{
    ByteArray bytePublic = keyPair.getPublicKey().serialize();
    ByteArray bytePrivate = keyPair.getPrivateKey().serialize();

    structure.set_id(id);
    structure.set_publickey(bytePublic.c_str(), bytePublic.size());
    structure.set_privatekey(bytePrivate.c_str(), bytePrivate.size());
    structure.set_signature(signature.c_str(), signature.size());
    structure.set_timestamp(timestamp);
}
ByteArray HKDF::expand(const ByteArray &prk, const ByteArray &info, int outputSize) const
{
    int iterations = std::ceil((float)outputSize / HKDF::HASH_OUTPUT_SIZE);
    ByteArray mixin;
    ByteArray results;
    int remainingBytes = outputSize;

    for (int i = iterationStartOffset; i < (iterations + iterationStartOffset); i++) {

        ByteArray message(mixin);
        if (!info.empty()) {
            message += info;
        }

        message += ByteArray(1, (char)(i % 256));

		unsigned char out[32];
		HMAC_SHA256((unsigned char*)message.c_str(), message.size(), (unsigned char*)prk.c_str(), prk.size(), out);
        ByteArray stepResult((const char*)out, 32);

        int stepSize = std::min((int)remainingBytes, (int)stepResult.size());
        results += stepResult.substr(0, stepSize);
        mixin = stepResult;
        remainingBytes -= stepSize;
    }
    return results;
}
Example #3
0
void File::Write( const ByteArray& buffer )
{
    if( m_fd < 0 )
    {
        THROW_ERROR( "Can't write to a closed file" );
    }

    while(true)
    {
        ssize_t n = write( m_fd, buffer.c_str(), buffer.size() );
        if( n < 0 )
        {
            if( errno == EINTR ) continue;

            THROW_ERROR( "Failed to write data to the file (Error=%s)", strerror(errno) );
        }

        if( n != (ssize_t)buffer.size() )
        {
            THROW_ERROR( "Failed to write all data to the file" );
        }

        break;
    }
}
SenderKeyState::SenderKeyState(int id, int iteration, const ByteArray &chainKey, const DjbECPublicKey &signatureKeyPublic, const DjbECPrivateKey &signatureKeyPrivate)
{
    senderKeyStateStructure = textsecure::SenderKeyStateStructure();
    senderKeyStateStructure.set_senderkeyid(id);
    senderKeyStateStructure.mutable_senderchainkey()->set_iteration(iteration);
    senderKeyStateStructure.mutable_senderchainkey()->set_seed(chainKey.c_str(),
                                                               chainKey.size());
    senderKeyStateStructure.mutable_sendersigningkey()->set_public_(signatureKeyPublic.serialize().c_str(),
                                                                    signatureKeyPublic.serialize().size());
    senderKeyStateStructure.mutable_sendersigningkey()->set_private_(signatureKeyPrivate.serialize().c_str(),
                                                                     signatureKeyPrivate.serialize().size());

    /*textsecure::SenderKeyStateStructure::SenderChainKey senderChainKeyStructure;
    senderChainKeyStructure.set_iteration(iteration);
    senderChainKeyStructure.set_seed(chainKey.c_str());

    textsecure::SenderKeyStateStructure::SenderSigningKey signingKeyStructure;
    signingKeyStructure.set_public_(signatureKeyPublic.serialize().c_str());

    signingKeyStructure.set_private_(signatureKeyPrivate.serialize().c_str());

    senderKeyStateStructure = textsecure::SenderKeyStateStructure();
    senderKeyStateStructure.set_senderkeyid(id);
    senderKeyStateStructure.mutable_senderchainkey()->CopyFrom(senderChainKeyStructure);
    senderKeyStateStructure.mutable_sendersigningkey()->CopyFrom(signingKeyStructure);*/
}
SignedPreKeyRecord::SignedPreKeyRecord(const ByteArray &serialized)
{
    structure.ParsePartialFromArray(serialized.c_str(), serialized.size());
}
ByteArray HKDF::extract(const ByteArray &salt, const ByteArray &inputKeyMaterial) const
{
    unsigned char out[32];
	HMAC_SHA256((unsigned char*)inputKeyMaterial.c_str(), inputKeyMaterial.size(), (unsigned char*)salt.c_str(), salt.size(), out);
    return ByteArray((const char*)out, 32);
}
ByteArray SenderChainKey::getDerivative(const ByteArray &seed, const ByteArray &key) const
{
    unsigned char out[32];
	HMAC_SHA256((unsigned char*)seed.c_str(), seed.size(), (unsigned char*)key.c_str(), key.size(), out);
    return ByteArray((const char*)out, 32);
}