예제 #1
0
bool ClientPuzzleManager::checkOneSolution(U32 solution, Nonce &clientNonce, Nonce &serverNonce, U32 puzzleDifficulty, U32 clientIdentity)
{
   U8 buffer[8];
   writeU32ToBuffer(solution, buffer);
   writeU32ToBuffer(clientIdentity, buffer + 4);

   hash_state hashState;
   U8 hash[32];

   sha256_init(&hashState);
   sha256_process(&hashState, buffer, sizeof(buffer));
   sha256_process(&hashState, clientNonce.data, Nonce::NonceSize);
   sha256_process(&hashState, serverNonce.data, Nonce::NonceSize);
   sha256_done(&hashState, hash);

   U32 index = 0;
   while(puzzleDifficulty > 8)
   {
      if(hash[index])
         return false;
      index++;
      puzzleDifficulty -= 8;
   }
   U8 mask = 0xFF << (8 - puzzleDifficulty);
   return (mask & hash[index]) == 0;
}
예제 #2
0
static void TNLToSocketAddress(const Address &address, SOCKADDR *sockAddr, socklen_t *addressSize)
{
   if(address.transport == IPProtocol || address.transport == TCPProtocol)
   {
      memset(sockAddr, 0, sizeof(SOCKADDR_IN));
      ((SOCKADDR_IN *) sockAddr)->sin_family = AF_INET;
      ((SOCKADDR_IN *) sockAddr)->sin_port = htons(address.port);
      ((SOCKADDR_IN *) sockAddr)->sin_addr.s_addr = htonl(address.netNum[0]);
      *addressSize = sizeof(SOCKADDR_IN);
   }
#ifndef NO_IPX_SUPPORT
   else if(address.transport == IPXProtocol)
   {
      memset(sockAddr, 0, sizeof(SOCKADDR_IPX));
      ((SOCKADDR_IPX *) sockAddr)->sa_family = AF_IPX;
      ((SOCKADDR_IPX *) sockAddr)->sa_socket = htons(address.port);
      writeU32ToBuffer(address.netNum[0], (U8 *) ((SOCKADDR_IPX *) sockAddr)->sa_netnum);
      writeU32ToBuffer(address.netNum[1], (U8 *) ((SOCKADDR_IPX *) sockAddr)->sa_nodenum);
      writeU16ToBuffer(address.netNum[2], (U8 *) ((SOCKADDR_IPX *) sockAddr)->sa_nodenum+4);
      *addressSize = sizeof(SOCKADDR_IPX);
   }
#endif
}
void AsymmetricKey::load(const ByteBuffer &theBuffer)
{
   mIsValid = false;

   crypto_key *theKey = (crypto_key *) malloc(sizeof(crypto_key));
   const U8 *bufferPtr = theBuffer.getBuffer();

   mHasPrivateKey = bufferPtr[0] == KeyTypePrivate;

   U32 bufferSize = theBuffer.getBufferSize();
   if(bufferSize < sizeof(U32) + 1)
      return;

   mKeySize = readU32FromBuffer(bufferPtr + 1);

   if( crypto_import(bufferPtr + sizeof(U32) + 1, bufferSize - sizeof(U32) - 1, theKey)
         != CRYPT_OK)
      return;

   mKeyData = theKey;

   if(mHasPrivateKey)
   {
      unsigned long bufferLen = sizeof(staticCryptoBuffer) - sizeof(U32) - 1;
      staticCryptoBuffer[0] = KeyTypePublic;

      writeU32ToBuffer(mKeySize, staticCryptoBuffer);

      if( crypto_export(staticCryptoBuffer + sizeof(U32) + 1, &bufferLen, PK_PUBLIC, theKey)
            != CRYPT_OK )
         return;

      bufferLen += sizeof(U32) + 1;

      mPublicKey = new ByteBuffer(staticCryptoBuffer, bufferLen);
      mPublicKey->takeOwnership();
      mPrivateKey = new ByteBuffer(theBuffer);
      mPrivateKey->takeOwnership();
   }
   else
   {
      mPublicKey = new ByteBuffer(theBuffer);
      mPublicKey->takeOwnership();
   }
   mIsValid = true;
}