// ClientCDKey::CreateSymmetricKey // Creates a symmetric key from product name used to save/load CD-Key to/from // the registry. Symmetric key is created via a series of CRCs on the product. void ClientCDKey::CreateSymmetricKey(BFSymmetricKey& theSymKeyR) const { WTRACE("ClientCDKey::CreateSymmetricKey"); WDBG_LL("ClientCDKey::CreateSymmetricKey from product=" << mProduct); CRC16 aCRC; RawBuffer aBuf; // CRC the product and use it as 1st 2 bytes of key aCRC.Put(mProduct); unsigned short aCheckSum = aCRC.GetCRC(); WDBG_LL("ClientCDKey::CreateSymmetricKey First CRC=" << aCheckSum); aBuf.assign(reinterpret_cast<unsigned char*>(&aCheckSum), sizeof(aCheckSum)); // CRC each of 1st 3 chars of product and add them to key. for (int i=0; (i < 3) && (i < mProduct.size()); i++) { aCRC.Put(static_cast<unsigned char>(mProduct[i])); aCheckSum = aCRC.GetCRC(); WDBG_LL("ClientCDKey::CreateSymmetricKey Add CRC=" << aCheckSum); aBuf.append(reinterpret_cast<unsigned char*>(&aCheckSum), sizeof(aCheckSum)); } // Create the key WDBG_LL("ClientCDKey::CreateSymmetricKey Buf=" << aBuf); theSymKeyR.Create(aBuf.size(), aBuf.data()); }
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()); }
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; }
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()); }
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(); }