Esempio n. 1
0
void BigNum::fromBinary(const RawBuffer &buffer)
{
    negative = false;    // Can't init negatives yet
    int alen = (buffer.length()*8 + BITS-1) / BITS;
    grow(*this, alen, false);
	len = alen;

    int pos = 0;
    n[pos] = 0;
    int bitpos = 0;
    // Index in reverse to get LSB first
    for (int i = buffer.length()-1; i >= 0; --i)
    {
        word b = buffer[i] & 0xFF;

        n[pos] |= (b << bitpos);
        bitpos += 8;
        if (bitpos >= BITS && i>0)
        {
            pos++;
            n[pos] = 0;
            bitpos -= BITS;
            if (bitpos > 0)
                n[pos] = b >> (8-bitpos);
        }
    }
Esempio n. 2
0
void DmaBufferedUart::packDmaArgs(DmaChannel::StreamDefinition&def, RawBuffer&rb, int subset ){
  def.device = &Uart::dcb->DR;
  def.devicesize = 2; //uart allows for 9 bit words, like 8051 multidrop protocol or forced parity
  def.buffer = rb.item(0);
  def.numItems = subset > 0 ? subset : rb.length();
  def.itemsize = rb.itemSize();
}
Esempio n. 3
0
void AuthContext::AppendHashes(WriteBuffer &theBuf, const RawBuffer &theChallengeSeed)
{
	AutoCrit aCrit(mDataCrit);

	int aNumHashes = 0;
	int aNumHashPos = theBuf.length();
	theBuf.SkipBytes(1); // put num hashes here

	AuthLoginCommunityMap::iterator anItr = mCommunityMap.begin();
	while(anItr!=mCommunityMap.end())
	{
		AuthLoginCommunityData &aData = anItr->second;
		if(!aData.mSimpleHash.empty())
		{
			MD5Digest aKeyedHash;
			aKeyedHash.update(theChallengeSeed);
			aKeyedHash.update(aData.mKeyedHashData);
			RawBuffer aKeyedHashBuf = aKeyedHash.digest();		
	

			theBuf.AppendByte(1); // hash tag
			theBuf.AppendWString(anItr->first); // community
			theBuf.AppendBytes(aData.mSimpleHash.data(),aData.mSimpleHash.length());
			theBuf.AppendBytes(aKeyedHashBuf.data(),aKeyedHashBuf.length());
			aNumHashes++;
		}

		++anItr;
	}

	theBuf.SetByte(aNumHashPos,aNumHashes);
}
EGPublicKey::CryptReturn EGPublicKey::Encrypt(const void* theMsgP, unsigned long theLen) const
{
	if ((! theMsgP) || (theLen == 0))
	{
		return CryptReturn(NULL,0);
	}


	RawBuffer anEncrypt;
	if(!mElGamal.encrypt(theMsgP,theLen,anEncrypt))
		return CryptReturn(NULL,0);

	unsigned char *anOutP = new unsigned char[anEncrypt.length()];
	memcpy(anOutP,anEncrypt.data(),anEncrypt.length());

	return CryptReturn(anOutP,anEncrypt.length());
}
Esempio n. 5
0
bool ElGamal::EncodeDigest(const RawBuffer& digest, BigInteger &h) const
{
    IntegerExtractor aDecoder(digest.data(),digest.length(),false);
    if(digest.length()*8 < q.bitLength())
    {
        if(!aDecoder.Decode(digest.length(),h))
            return false;
    }
    else
    {
        if(!aDecoder.Decode(q.byteLength(),h))
            return false;

        h = h.shiftRight(q.byteLength()*8 - q.bitLength() + 1);
    }

    return true;
}
Esempio n. 6
0
unsigned __int64 BigInteger::GetInt64() const
{
	BigInteger r;
	unsigned __int64 aVal = 0;

	RawBuffer aBuf = toByteArray();
	for(int i=0; i<aBuf.length(); i++)
	{
		aVal = (aVal<<8)|aBuf[i];
	}

	return aVal;


}
Esempio n. 7
0
bool AuthContext::LoadVerifierKey(const std::string &theFile)
{
	mCheckedVerifierFile = true;

	FILE *aFile = fopen(theFile.c_str(),"rb");
	if(aFile==NULL)
		return false;

	unsigned char aBuf[1024];
	RawBuffer aKeyBuf;
	while(!feof(aFile))
	{
		int aNumRead = fread(aBuf,1,1024,aFile);
		if(aNumRead>0)
			aKeyBuf.append(aBuf,aNumRead);
	
	}

	fclose(aFile);
	
	AutoCrit aCrit(GetVerifierCrit());
	return mVerifierKey.SetPublicKey(aKeyBuf.data(),aKeyBuf.length());
}
Esempio n. 8
0
ByteBufferPtr ElGamal::Decrypt(const void *theCipherText, int theCipherTextLen) const
{
    if(!IsPrivate())
        return NULL;

    const unsigned char *in = (const unsigned char*)theCipherText;
    int inOffset = 0;

    if(theCipherTextLen-inOffset<4) return NULL;
    int aNumBlocks = LongFromLittleEndian(*(int*)in);
    inOffset+=4;

    if(theCipherTextLen-inOffset < aNumBlocks*modulusLen*2-inOffset)
        return NULL;

    RawBuffer aBuf(modulusLen,(unsigned char)0);
    RawBuffer bBuf(modulusLen,(unsigned char)0);

    WriteBuffer aDecrypt;

    BigInteger a;;
    BigInteger b;

    BigInteger aPlainText;

    for(int i=0; i<aNumBlocks; i++)
    {
        aBuf.assign(in+inOffset,modulusLen);
        inOffset+=modulusLen;
        bBuf.assign(in+inOffset,modulusLen);
        inOffset+=modulusLen;

        a.fromBinary(aBuf);
        b.fromBinary(bBuf);

        if(!decrypt(a,b,aPlainText))
            return NULL;

        RawBuffer aBigIntArray;
        aPlainText.toBinary(aBigIntArray);


        if(aBigIntArray.length()==0) return NULL;
        int aPlainLen = aBigIntArray[aBigIntArray.length() - 1];

        if(aPlainLen>modulusLen - 3)
            return NULL;

        if(aBigIntArray.length() - 1 - aPlainLen < 0)
        {
            int extra = aPlainLen - (aBigIntArray.length() - 1);
            for(int j=0; j<extra; j++)
                aDecrypt.AppendByte(0);

            aDecrypt.AppendBytes(aBigIntArray.data(),aBigIntArray.length());
        }
        else
            aDecrypt.AppendBytes(aBigIntArray.data()+aBigIntArray.length()-1-aPlainLen,aPlainLen);
    }

    return aDecrypt.ToByteBuffer();
}