예제 #1
0
파일: main.c 프로젝트: Azii/Uni
/*
Calculates Blockhashes in a simple loop.
*/
int bitcoin_simple() {
	printf("Starting bitcoin_simple\n");
	// Start, end time
	unsigned long start,end;
	// Set start time
	start = current_time_millis();


	// Creates and retrieves the Block-Header information
	Blockheader * blockheader = malloc(sizeof(Blockheader));
	// The getWork method fills an empty Blockheader struct with all the neccesary information needed to calcualte the Hash of a Block.
	getWork(blockheader);
	// The nonce is the value that is incremented in each run to get a different hash value
	char * n = malloc(sizeof(char)*4);
	memcpy(n,&(blockheader->nonce),sizeof(char)*4);
	// The values in the Blockheader are actually in reverse byte order and need to be reversed in order to increment the nonce value. 
	byte_reversal(n,sizeof(char)*4);
	// Convert the 4 byte long raw data into an unsinged long 
	unsigned long starting_nonce = n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
	// The nonce value we received in the getWork method is the actual starting nonce value. We start to calculate hashes with this initial nonce and increase it by one in each run. 
	unsigned long nonce = starting_nonce;
	char * hash;
	// In practice it is very hard to find a valid hash, so in this exercise we will limit the amount of hashes we calculate.
	for(;nonce<=(starting_nonce+MAX_HASHES);nonce++) {
		// put current nonce in blockheader object
		// first, shift long back to char[4]
		n[0] = nonce >> 24;
		n[1] = nonce >> 16;
		n[2] = nonce >> 8;
		n[3] = nonce;
		// reverse byte order
		byte_reversal(n,sizeof(char)*4);
		// put n into blockheader
		blockheader->nonce[0] = n[0];
		blockheader->nonce[1] = n[1];
		blockheader->nonce[2] = n[2];
		blockheader->nonce[3] = n[3];
		// calculate the hash using the sha-256 hashing algorithm
		size_t size = getData(blockheader,&hash);
		size = sha256_digest(hash,size,&hash);
		// To calculate a valid hash, we need to do two hashing passes
		size = sha256_digest(hash,size,&hash);
		if(check_hash(hash,(int)size))
		{
			printf("%ld : ", nonce);
			print_hash(hash,size);
		}
	}

	end = current_time_millis();
	printf("Calculation finished after %.3fs\n", (double) (end - start) / 1000);

	free(blockheader);
	return EXIT_SUCCESS;

}
예제 #2
0
static void
yarrow_iterate(uint8_t *digest)
{
  uint8_t v0[SHA256_DIGEST_SIZE];
  unsigned i;
  
  memcpy(v0, digest, SHA256_DIGEST_SIZE);
  
  /* When hashed inside the loop, i should run from 1 to
   * YARROW_RESEED_ITERATIONS */
  for (i = 0; ++i < YARROW_RESEED_ITERATIONS; )
    {
      uint8_t count[4];
      struct sha256_ctx hash;
  
      sha256_init(&hash);

      /* Hash v_i | v_0 | i */
      WRITE_UINT32(count, i);
      sha256_update(&hash, SHA256_DIGEST_SIZE, digest);
      sha256_update(&hash, sizeof(v0), v0);
      sha256_update(&hash, sizeof(count), count);

      sha256_digest(&hash, SHA256_DIGEST_SIZE, digest);
    }
}
예제 #3
0
int
pkcs1_rsa_sha256_encode(mpz_t m, size_t key_size, struct sha256_ctx *hash)
{
  uint8_t *p;
  TMP_GMP_DECL(em, uint8_t);

  TMP_GMP_ALLOC(em, key_size);

  p = _pkcs1_signature_prefix(key_size, em,
			      sizeof(sha256_prefix),
			      sha256_prefix,
			      SHA256_DIGEST_SIZE);
  if (p)
    {
      sha256_digest(hash, SHA256_DIGEST_SIZE, p);
      nettle_mpz_set_str_256_u(m, key_size, em);
      TMP_GMP_FREE(em);
      return 1;
    }
  else
    {
      TMP_GMP_FREE(em);
      return 0;
    }
}
int
dsa_sha256_verify(const struct dsa_public_key *key,
		  struct sha256_ctx *hash,
		  const struct dsa_signature *signature)
{
  uint8_t digest[SHA256_DIGEST_SIZE];
  sha256_digest(hash, sizeof(digest), digest);

  return _dsa_verify(key, sizeof(digest), digest, signature);
}
int
dsa_sha256_sign(const struct dsa_public_key *pub,
		const struct dsa_private_key *key,
		void *random_ctx, nettle_random_func random,
		struct sha256_ctx *hash,
		struct dsa_signature *signature)
{
  uint8_t digest[SHA256_DIGEST_SIZE];
  sha256_digest(hash, sizeof(digest), digest);

  return _dsa_sign(pub, key, random_ctx, random,
		   sizeof(digest), digest, signature);
}
예제 #6
0
파일: gtls.c 프로젝트: CopySpotter/curl
void Curl_gtls_sha256sum(const unsigned char *tmp, /* input */
                      size_t tmplen,
                      unsigned char *sha256sum, /* output */
                      size_t sha256len)
{
#if defined(USE_GNUTLS_NETTLE)
  struct sha256_ctx SHA256pw;
  sha256_init(&SHA256pw);
  sha256_update(&SHA256pw, (unsigned int)tmplen, tmp);
  sha256_digest(&SHA256pw, (unsigned int)sha256len, sha256sum);
#elif defined(USE_GNUTLS)
  gcry_md_hd_t SHA256pw;
  gcry_md_open(&SHA256pw, GCRY_MD_SHA256, 0);
  gcry_md_write(SHA256pw, tmp, tmplen);
  memcpy(sha256sum, gcry_md_read (SHA256pw, 0), sha256len);
  gcry_md_close(SHA256pw);
#endif
}
int
pkcs1_rsa_sha256_encode(mpz_t m, unsigned size, struct sha256_ctx *hash)
{
  TMP_DECL(em, uint8_t, NETTLE_MAX_BIGNUM_BITS / 8);
  TMP_ALLOC(em, size);

  if (pkcs1_signature_prefix(size, em,
			     sizeof(sha256_prefix),
			     sha256_prefix,
			     SHA256_DIGEST_SIZE))
    {
      sha256_digest(hash, SHA256_DIGEST_SIZE, em + size - SHA256_DIGEST_SIZE);
      nettle_mpz_set_str_256_u(m, size, em);
      return 1;
    }
  else
    return 0;	
}
예제 #8
0
파일: val_secalgo.c 프로젝트: k0nsl/unbound
static int
_digest_nettle(int algo, uint8_t* buf, size_t len,
	unsigned char* res)
{
	switch(algo) {
		case SHA1_DIGEST_SIZE:
		{
			struct sha1_ctx ctx;
			sha1_init(&ctx);
			sha1_update(&ctx, len, buf);
			sha1_digest(&ctx, SHA1_DIGEST_SIZE, res);
			return 1;
		}
		case SHA256_DIGEST_SIZE:
		{
			struct sha256_ctx ctx;
			sha256_init(&ctx);
			sha256_update(&ctx, len, buf);
			sha256_digest(&ctx, SHA256_DIGEST_SIZE, res);
			return 1;
		}
		case SHA384_DIGEST_SIZE:
		{
			struct sha384_ctx ctx;
			sha384_init(&ctx);
			sha384_update(&ctx, len, buf);
			sha384_digest(&ctx, SHA384_DIGEST_SIZE, res);
			return 1;
		}
		case SHA512_DIGEST_SIZE:
		{
			struct sha512_ctx ctx;
			sha512_init(&ctx);
			sha512_update(&ctx, len, buf);
			sha512_digest(&ctx, SHA512_DIGEST_SIZE, res);
			return 1;
		}
		default:
			break;
	}
	return 0;
}
예제 #9
0
void
yarrow256_slow_reseed(struct yarrow256_ctx *ctx)
{
  uint8_t digest[SHA256_DIGEST_SIZE];
  unsigned i;

#if YARROW_DEBUG
  fprintf(stderr, "yarrow256_slow_reseed\n");
#endif

  /* Get digest of the slow pool*/
  sha256_digest(&ctx->pools[YARROW_SLOW], sizeof(digest), digest);

  /* Feed it into the fast pool */
  sha256_update(&ctx->pools[YARROW_FAST], sizeof(digest), digest);

  yarrow256_fast_reseed(ctx);
  
  /* Reset estimates. */
  for (i = 0; i<ctx->nsources; i++)
    ctx->sources[i].estimate[YARROW_SLOW] = 0;
}
예제 #10
0
void
yarrow256_fast_reseed(struct yarrow256_ctx *ctx)
{
  uint8_t digest[SHA256_DIGEST_SIZE];
  unsigned i;
  
#if YARROW_DEBUG
  fprintf(stderr, "yarrow256_fast_reseed\n");
#endif
  
  /* We feed two block of output using the current key into the pool
   * before emptying it. */
  if (ctx->seeded)
    {
      uint8_t blocks[AES_BLOCK_SIZE * 2];
      
      yarrow_generate_block(ctx, blocks);
      yarrow_generate_block(ctx, blocks + AES_BLOCK_SIZE);
      sha256_update(&ctx->pools[YARROW_FAST], sizeof(blocks), blocks);
    }
  
  sha256_digest(&ctx->pools[YARROW_FAST], sizeof(digest), digest);

  /* Iterate */
  yarrow_iterate(digest);

  aes256_set_encrypt_key(&ctx->key, digest);
  ctx->seeded = 1;

  /* Derive new counter value */
  memset(ctx->counter, 0, sizeof(ctx->counter));
  aes256_encrypt(&ctx->key, sizeof(ctx->counter), ctx->counter, ctx->counter);
  
  /* Reset estimates. */
  for (i = 0; i<ctx->nsources; i++)
    ctx->sources[i].estimate[YARROW_FAST] = 0;
}
예제 #11
0
파일: main.c 프로젝트: Azii/Uni
int calculate_hash(Blockheader * blockheader, int num_hashes)
{
	unsigned char * n = malloc(sizeof(char) * 4);
	memcpy(n, blockheader->nonce, sizeof(char) * 4);
	byte_reversal(n, sizeof(char) * 4);
	
	unsigned long starting_nonce = n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
	unsigned long end_nonce = starting_nonce + num_hashes;
	unsigned long nonce = starting_nonce;
	//printf("starting_nonce: %ld\n", starting_nonce);
	
	// copy paste
	for( ; nonce <= end_nonce; nonce++) {
		// put current nonce in blockheader object
		// first, shift long back to char[4]
		n[0] = nonce >> 24;
		n[1] = nonce >> 16;
		n[2] = nonce >> 8;
		n[3] = nonce;
		// reverse byte order
		byte_reversal(n,sizeof(char)*4);
		// put n into blockheader
		blockheader->nonce[0] = n[0];
		blockheader->nonce[1] = n[1];
		blockheader->nonce[2] = n[2];
		blockheader->nonce[3] = n[3];
		//printf("blockheader->nonce val after assignment in for loop: %ld\n", toulong(blockheader->nonce));
		// calculate the hash using the sha-256 hashing algorithm
		char * hash;
		size_t size = getData(blockheader,&hash);
		char * r1;
		size = sha256_digest(hash,size,&r1);
		free(hash);
		// To calculate a valid hash, we need to do two hashing passes
		char * r2;
		size = sha256_digest(r1,size,&r2);
		free(r1);
		if(check_hash(r2,(int)size))
		{
			printf("%ld : ", nonce);
			print_hash(r2,size);
		}
		free(r2);
		
	}
	
	//printf("blockheader->nonce val after for loop: %ld", toulong(blockheader->nonce));
	
	unsigned char * end_nonce_char = to_reversed_char_arr(end_nonce - 1);
	//memcpy(&(blockheader->nonce), end_nonce_char, sizeof(char) * 4);
	blockheader->nonce[0] = end_nonce_char[0];
	blockheader->nonce[1] = end_nonce_char[1];
	blockheader->nonce[2] = end_nonce_char[2];
	blockheader->nonce[3] = end_nonce_char[3];
	
	//printf("blockheader nonce end of calc hash function: %ld\n", toulong(blockheader->nonce));
	
	free(n);
	
	return EXIT_SUCCESS;
}
예제 #12
0
파일: SHA2_256.cpp 프로젝트: gt1/libmaus2
void libmaus2::digest::SHA2_256::digest(uint8_t * digest) { sha256_digest(reinterpret_cast<sha256_ctx *>(ctx),digestlength,&digest[0]); }
예제 #13
0
extern "C" DLL_EXPORT int getFileVerify(std::vector<std::string> &infile, std::vector<Result> &outre)
{
	__int64 finishedSize = 0;
	std::vector<__int64> fSizes(infile.size());
	std::string strFileSize;
	std::string strFileMD5;
	std::string strFileSHA1;
	std::string strFileSHA256;
	std::string strFileCRC32;

	//开始计算,循环处理每个文件
	for (int i = 0; i < infile.size(); i++)
	{
		// Declaration for calculator
		char* path = const_cast<char*>(infile[i].c_str());
		__int64 fsize = 0;
		finishedSize = 0;
		DataBuffer databuf;

		MD5_CTX mdContext; // MD5 context

		CSHA1 sha1; // SHA1 object
		char strSHA1[256];

		SHA256_CTX sha256Ctx; // SHA256 context
		std::string strSHA256 ="";

		unsigned long ulCRC32; // CRC32 context

		ResultData result;
		result.strPath = infile[i];
		//打开文件
		FILE *fp = fopen(infile[i].c_str(), "rb+");
		if (fp != NULL)
		{
			//成功打开文件
			MD5Init(&mdContext, 0); // MD5开始
			sha1.Reset(); // SHA1开始
			sha256_init(&sha256Ctx); // SHA256开始
			crc32Init(&ulCRC32); // CRC32开始

			//获取文件信息
			std::string lastModifiedTime;
			struct _stat64 info;
			_wstat64(AnsiToUnicode(infile[i].c_str()), &info);
			fsize = info.st_size;
			API_TimeToStringEX(lastModifiedTime, info.st_mtime);
			do
			{
				//读取文件
				databuf.datalen = fread(databuf.data, 1, DataBuffer::preflen, fp);

				MD5Update(&mdContext, databuf.data, databuf.datalen); // MD5更新
				sha1.Update(databuf.data, databuf.datalen); // SHA1更新
				sha256_update(&sha256Ctx, databuf.data, databuf.datalen); // SHA256更新
				crc32Update(&ulCRC32, databuf.data, databuf.datalen); // CRC32更新

				finishedSize += databuf.datalen;

				//设置偏移
				if (databuf.datalen >= DataBuffer::preflen)
					_fseeki64(fp, finishedSize, SEEK_SET);
			} while (databuf.datalen >= DataBuffer::preflen);
			fclose(fp);//关闭文件
			MD5Final(&mdContext); // MD5完成
			sha1.Final(); // SHA1完成
			sha256_final(&sha256Ctx); // SHA256完成
			crc32Finish(&ulCRC32); //CRC32完成

			//格式化校验码
			char _tempmd5[256] = { '\0' };
			sprintf(_tempmd5, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
				mdContext.digest[0],
				mdContext.digest[1],
				mdContext.digest[2],
				mdContext.digest[3],
				mdContext.digest[4],
				mdContext.digest[5],
				mdContext.digest[6],
				mdContext.digest[7],
				mdContext.digest[8],
				mdContext.digest[9],
				mdContext.digest[10],
				mdContext.digest[11],
				mdContext.digest[12],
				mdContext.digest[13],
				mdContext.digest[14],
				mdContext.digest[15]);
			strFileMD5 = _tempmd5;
			sha1.ReportHash(strSHA1, CSHA1::REPORT_HEX);
			strFileSHA1 = (strSHA1);
			sha256_digest(&sha256Ctx, &strSHA256);
			strFileSHA256 = strSHA256;
			//strFileCRC32.Format(_T("%08X"), ulCRC32);
			char _tempcrc[128] = { '\0' };
			sprintf(_tempcrc, "%08X", ulCRC32);
			strFileCRC32 = _tempcrc;

			result.bDone = TRUE;
			result.strPath = infile[i];
			result.ulSize = fsize;
			result.strMDate = lastModifiedTime;
			// 在没做转换前,结果都是大写的
			result.strMD5 = strFileMD5;
			result.strSHA1 = strFileSHA1;
			result.strSHA256 = strFileSHA256;
			result.strCRC32 = strFileCRC32;
			//转换大小写
		}
		else
		{
			result.bDone = FALSE;
			result.strError = "Open file error";			
		}

		outre.push_back(result);
	}
	return 0;
}
예제 #14
0
static void SHA256_Final(unsigned char digest[32], SHA256_CTX *ctx)
{
  sha256_digest(ctx, 32, digest);
}
예제 #15
0
ReturnStateContext EngimaState::onRun(QKeyboard &kb) {
	StateBase* nextState = this;
	static uint32_t LastScrollTime = HAL_GetTick();
	switch (InternalState) {
	case SET_WHEEL:
		gui_lable_multiline("Enter password (for rotors)", 0, 10, 128, 64, 0, 0);
		kb.updateContext(getKeyboardContext());
		gui_lable_multiline(&Wheels[0], 0, 30, 128, 64, 0, 0);
		if (kb.getLastKeyReleased() == 11) {
			InternalState = PLUG_BOARD;
			getKeyboardContext().finalize();
			getKeyboardContext().init(&PlugBoard[0], sizeof(PlugBoard));
		} else if (kb.getLastKeyReleased() == 9) {
			nextState = StateFactory::getMenuState();
		}
		break;
	case PLUG_BOARD:
		gui_lable_multiline("Enter plug board pairs:", 0, 10, 128, 64, 0, 0);
		kb.updateContext(getKeyboardContext());
		gui_lable_multiline(&PlugBoard[0], 0, 30, 128, 64, 0, 0);
		if (kb.getLastKeyReleased() == 11) {
			InternalState = ENTER_MESSAGE;
			getKeyboardContext().finalize();
			getKeyboardContext().init(&EntryBuffer[0], sizeof(EntryBuffer));
		} else if (kb.getLastKeyReleased() == 9) {
			nextState = StateFactory::getMenuState();
		}
		break;
	case ENTER_MESSAGE: {
		gui_lable_multiline("Enter cipher text: ", 0, 10, 128, 64, 0, 0);
		kb.updateContext(getKeyboardContext());
		uint16_t offset =
				getKeyboardContext().getCursorPosition() > 37 ? getKeyboardContext().getCursorPosition() - 32 : 0;
		gui_lable_multiline(&EntryBuffer[offset], 0, 30, 128, 64, 0, 0);
		if (kb.getLastKeyReleased() == 11) {
			InternalState = DECRYPT;
			getKeyboardContext().finalize();
			crypt(&Wheels[0], &PlugBoard[0], strlen(&PlugBoard[0]), &EntryBuffer[0]);
			DisplayOffset = 0;
			LastScrollTime = HAL_GetTick();
		} else if (kb.getLastKeyReleased() == 9) {
			nextState = StateFactory::getMenuState();
		}
	}
		break;
	case DECRYPT: {
		gui_lable_multiline("Decodes to:", 0, 10, 128, 64, 0, 0);
		uint32_t decryptedLen = strlen(&EncryptResult[0]);
		if (decryptedLen > 48 && ((HAL_GetTick()-LastScrollTime)>500)) {
			LastScrollTime = HAL_GetTick();
			DisplayOffset = (DisplayOffset + 1) % decryptedLen;
		}
		gui_lable_multiline(&EncryptResult[DisplayOffset], 0, 20, 128, 64, 0, 0);
		if (kb.getLastKeyReleased() == 11) {
			InternalState = QUEST_COMPLETION;
			ShaOBJ sha;
			sha256_init(&sha);
			sha256_add(&sha, (const uint8_t*) getContactStore().getMyInfo().getPrivateKey(), ContactStore::PRIVATE_KEY_LENGTH);
			sha256_add(&sha, (const uint8_t*) &EncryptResult[0], strlen(&EncryptResult[0]));
			sha256_digest(&sha, &ResultHash[0]);
			memset(&EntryBuffer[0], 0, sizeof(EntryBuffer));
			sprintf(&EntryBuffer[0], "%02x%02x%02x%02x%02x%02x%02x%02x", ResultHash[0], ResultHash[1], ResultHash[2], ResultHash[3],
					ResultHash[4], ResultHash[5], ResultHash[6], ResultHash[7]);
		} else if (kb.getLastKeyReleased() == 9) {
			nextState = StateFactory::getMenuState();
		}
	}
		break;
	case QUEST_COMPLETION:
		gui_lable_multiline("Daemon code: ", 0, 10, 128, 64, 0, 0);
		gui_lable_multiline(&EntryBuffer[0], 0, 30, 128, 64, 0, 0);
		if (kb.getLastKeyReleased()==11) {
			nextState = StateFactory::getMenuState();
		}
		break;
	default:
		break;
	}
	return ReturnStateContext(nextState);
}
예제 #16
0
static int
__archive_nettle_sha256final(archive_sha256_ctx *ctx, void *md)
{
  sha256_digest(ctx, SHA256_DIGEST_SIZE, md);
  return (ARCHIVE_OK);
}
예제 #17
0
파일: main.c 프로젝트: shinroo/TUB_Programs
	// put current nonce in blockheader object
	// first, shift long back to char[4]
	n[0] = nonce >> 24;
	n[1] = nonce >> 16;
	n[2] = nonce >> 8;
	n[3] = nonce;
	// reverse byte order
	byte_reversal(n,sizeof(char)*4);
	// put n into blockheader
	blockheader->nonce[0] = n[0];
	blockheader->nonce[1] = n[1];
	blockheader->nonce[2] = n[2];
	blockheader->nonce[3] = n[3];
	// calculate the hash using the sha-256 hashing algorithm
	size_t size = getData(blockheader,&hash);
	size = sha256_digest(hash,size,&hash);
	// To calculate a valid hash, we need to do two hashing passes
	size = sha256_digest(hash,size,&hash);
	if(check_hash(hash,(int)size))
	{
		printf("%ld : ", nonce);
		print_hash(hash,size);
	}
}

int bitcoin_loop(const unsigned int processcount) {
	printf("\n\nStarting bitcoin_loop\n");

	// Check, if input data are valid
	if ((int) processcount > (int) MAX_HASHES) {
		perror("processcount must be <= MAX_HASHES!!!\n");