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; }
/* 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; }
uint Sha::Finish(void *digest){ if(_bits==sha_160){ sha1_finish(tvcast<sha1_context>(_sha_ctx),(byte*)digest); }else if(_bits==sha_224||_bits==sha_256){ sha2_finish(tvcast<sha2_context>(_sha_ctx),(byte*)digest); }else if(_bits==sha_384||_bits==sha_512){ sha4_finish(tvcast<sha4_context>(_sha_ctx),(byte*)digest); }else{ _ASSERT(0); return 0; } return _bits; }
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); } }
// 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); }
// Retrieve the tag size_t mintls_hash_finish( mintls_hash_context * ctx, // (I/O) Context uint8_t * tag // (O) Tag ) { switch (ctx->type) { case MinTLS_SHA_160: return sha1_finish((struct sha1_ctx *)ctx,tag); case MinTLS_SHA_256: case MinTLS_SHA_224: return sha2_finish((struct sha2_ctx *)ctx,tag); case MinTLS_SHA_384: case MinTLS_SHA_512: return sha4_finish((struct sha4_ctx *)ctx,tag); default: return 0; } }
void sha256_finish_wrap( void *ctx, unsigned char *output ) { sha2_finish( (sha2_context *) ctx, output ); }
void ctr_sha_256_finish( ctr_sha256_context* ctx, u8 hash[0x20] ) { sha2_finish(&ctx->sha, hash); }