Esempio n. 1
0
void aes_key_setup(UCHAR * digest, UCHAR * iv, char *key, int keylen)
{
	sha2_context sha_ctx;
	int i = 0;

	memset(digest, '\0', AES_KEY_SIZE);
	memcpy(digest, iv, AES_IV_SIZE);
	for (i = 0; i < AES_KEY_ROUNDS; i++) {
		sha2_starts(&sha_ctx, 0);
		sha2_update(&sha_ctx, digest, AES_KEY_SIZE);
		sha2_update(&sha_ctx, (UCHAR *) key, keylen);
		sha2_finish(&sha_ctx, digest);
	}
}
Esempio n. 2
0
/* returns NULL on error, pointer to hash on success */
int ewf_crypto_sha256_hash(char *msg, char **result)
{
	sha2_context sha256;
	unsigned char digest[32];
	int i;

	if (msg == NULL)
	{
		return EWF_ERROR;
	}

	*result = calloc(1, 32 * 2 + 1);

	if (*result == NULL)
	{
		return EWF_ERROR;
	}

	sha2_starts(&sha256, 0);
	sha2_update(&sha256, (unsigned char*) msg, strlen(msg));
	sha2_finish(&sha256, digest);
	
	for (i = 0; i < 32; i++)
	{
		nbu_string_printf((*result) + 2 * i, 3, "%02x", (unsigned char) digest[i]);
	}

	return EWF_SUCCESS;
}
// verify function is called after all the data has been received and stored
// in the NV and after the signature has been downloaded.  The function reads back
// data from the NV and calculates the hash.
// Function returns TRUE if signature verifications passes
int ws_upgrade_verify(void)
{
    unsigned int i;
    sha2_context ctx;
    int          error;
    int          bytesToRead;

    sha2_starts(&ctx, FALSE);

    // first few bytes in the stream was version information
    sha2_update(&ctx, (UINT8 *)&ws_upgrade_info, sizeof (ws_upgrade_info));

    // read patch data and update hash
    for (i = 0; i < ws_upgrade_patch_len; i += WS_UPGRADE_READ_CHUNK)
    {
        char memory_chunk[WS_UPGRADE_READ_CHUNK];

        bytesToRead = i + WS_UPGRADE_READ_CHUNK < ws_upgrade_patch_len ? WS_UPGRADE_READ_CHUNK : ws_upgrade_patch_len - i;

        ws_upgrade_retrieve_from_nv(i, memory_chunk, bytesToRead);

        sha2_update(&ctx, memory_chunk, bytesToRead);
    }

    sha2_finish(&ctx, ws_upgrade_sha256sum);

    ble_trace0("hash:\n");
    ble_tracen(ws_upgrade_sha256sum, sizeof (ws_upgrade_sha256sum));

    ble_trace0("signature:\n");
    ble_tracen(ws_upgrade_sig, sizeof (ws_upgrade_sig));

    memset( &ctx, 0, sizeof(sha2_context));

    rsaCtx.len = WS_UPGRADE_RSA_SIGNATURE_LEN;
    error = rsa_rsassa_pss_verify_(&rsaCtx, RSA_PUBLIC, SIG_RSA_SHA256, sizeof(ws_upgrade_sha256sum), ws_upgrade_sha256sum, ws_upgrade_sig);

    // just for testing verify that stack was not corrupted
    ble_trace1("StackOverflow3:%d\n", blecm_DidStackOverflow());

    ble_trace1("ws_verify err:%x\n", error);

    return (error == 0);
}
Esempio n. 4
0
	void Sha::Update(const void* data,uint len){
		if(_bits==sha_160){
			sha1_update(tvcast<sha1_context>(_sha_ctx),(byte*)data,len);
		}else if(_bits==sha_224||_bits==sha_256){
			sha2_update(tvcast<sha2_context>(_sha_ctx),(byte*)data,len);
		}else if(_bits==sha_384||_bits==sha_512){
			sha4_update(tvcast<sha4_context>(_sha_ctx),(byte*)data,len);
		}else{
			_ASSERT(0);
		} 
	}
Esempio n. 5
0
static void BcmCalcMidState(uint8_t *pMidState, uint8_t const *pData8)
{
	unsigned char data[64];
	sha2_context ctx;

	BcmEndianFlip(data, pData8, 64);
	sha2_starts(&ctx);
	sha2_update(&ctx, data, 64);
	// Quick fix... endianness of midState should not be changed
	//BcmEndianFlip(pMidState, (uint8_t *)ctx.state, 32);
	memcpy(pMidState, ctx.state, 32);
}
Esempio n. 6
0
int deriv_passwd(unsigned char *key,
		char *password,
		unsigned char *salt, int salt_len,
		unsigned int iterations)
{
	int ret;
	unsigned int i;
	unsigned char hash[32];
	sha2_context ctx;

	/* *** Init *** */
	ret = 1; // error
	i = 0;

	/* *** Check args *** */
	if((key == NULL) || (password == NULL) || (salt == NULL) 
			|| (salt_len <= 0) || (iterations == 0))
		goto cleanup;


	/* *** Get H0 *** */
	sha2_starts(&ctx, 0);

	sha2_update(&ctx, (unsigned char *)password, strlen(password));
	sha2_update(&ctx, salt, salt_len);
	sha2_update(&ctx, (unsigned char *)&i, sizeof(int));
	
	sha2_finish(&ctx, hash);

	/* *** Hi *** */
	for(i = 1; i < iterations; i++)
	{
		sha2_starts(&ctx, 0);

		sha2_update(&ctx, hash, 32);
		sha2_update(&ctx, (unsigned char *)password, strlen(password));
		sha2_update(&ctx, salt, salt_len);
		sha2_update(&ctx, (unsigned char *)&i, sizeof(int));

		sha2_finish(&ctx, hash);
	}
	memcpy(key, hash, 32);

	ret = 0; // success

cleanup:

	memset(&ctx, 0x00, sizeof(sha2_context));
	memset(hash, 0x00, 32);
	return ret;
}
Esempio n. 7
0
File: sha.c Progetto: drufino/minTLS
// Process some data
void mintls_hash_update(
    mintls_hash_context *   ctx,            // (I/O) Context
    uint8_t const*          input,          // (I) Data
    size_t                  ilen            // (I) Length
)
{
    switch (ctx->type)
    {
    case MinTLS_SHA_160:
        sha1_update((struct sha1_ctx *)ctx,input,ilen);
        break;
    case MinTLS_SHA_256:
    case MinTLS_SHA_224:
        sha2_update((struct sha2_ctx *)ctx,input,ilen);
        break;
    case MinTLS_SHA_384:
    case MinTLS_SHA_512:
        sha4_update((struct sha4_ctx *)ctx,input,ilen);
        break;
    }
    return;
}
Esempio n. 8
0
void sha256_update_wrap( void *ctx, const unsigned char *input, size_t ilen )
{
    sha2_update( (sha2_context *) ctx, input, ilen );
}
Esempio n. 9
0
void ctr_sha_256_update( ctr_sha256_context* ctx, 
							    const u8* data,
								u32 size )
{
	sha2_update(&ctx->sha, data, size);
}