static bool wallet_verify_hmac(const struct wallet *wallet, const char *privStr, uint8 **encPrivKey, size_t *encLen) { uint8 *hash; size_t hash_len; uint8 *key; size_t key_len; uint256 mac; size_t len; char *macStr; char *keyStr; bool s; len = strlen(privStr); if (len < 65) { return 0; } macStr = safe_strdup(privStr); keyStr = safe_strdup(privStr); // copy the hmac that was appended. memcpy(macStr, macStr + len - 64, 64); macStr[64] = '\0'; // stop right before hmac ASSERT(keyStr[len - 65] == '-'); keyStr[len - 65] = '\0'; str_to_bytes(macStr, &hash, &hash_len); str_to_bytes(keyStr, &key, &key_len); ASSERT(hash_len == sizeof mac); free(macStr); free(keyStr); crypt_hmac_sha256(key, key_len, wallet->pass->buf, wallet->pass->len, &mac); s = memcmp(mac.data, hash, sizeof mac) == 0; free(hash); if (s) { *encPrivKey = key; *encLen = key_len; } else { Log(LGPFX" %s failed.\n", __FUNCTION__); free(key); } return s; }
static void wallet_crypt_init(struct wallet *wallet, const char *saltStr, int64 count) { int64 count0 = count; uint8 *salt; size_t len; bool s; if (saltStr == NULL) { return; } if (wallet->pass == NULL) { Log(LGPFX" wallet is encrypted. no passphrase given.\n"); return; } ASSERT(saltStr); str_to_bytes(saltStr, &salt, &len); ASSERT(len == sizeof wallet->ckey->salt); memcpy(wallet->ckey->salt, salt, len); free(salt); s = crypt_set_key_from_passphrase(wallet->pass, wallet->ckey, &count0); ASSERT(s); ASSERT(count == count0); }
int main() { char input[2048], c; unsigned char *bytes, key, *k; int i; i = 0; while ((c = getchar()) != EOF) { input[i++] = c; } input[i--] = 0; i = (i + 1) / 2; bytes = malloc(sizeof(unsigned char) * i); str_to_bytes(input, bytes, i); key = break_singlechar_xor(bytes, i); k = malloc(sizeof(char)+1); k[0] = key; k[1] = 0; decrypt_repeat_xor(bytes, k, i); printf("\nPrinting bytes:\n"); print_bytes(bytes); printf("\nPrinting clear text\n"); printf("%s\n", bytes); return 0; }
static bool wallet_alloc_key(struct wallet *wallet, const char *priv, const char *pub, const char *desc, time_t birth, bool spendable) { struct wallet_key *wkey; struct key *key; uint160 pub_key; size_t len; uint8 *buf; bool s; ASSERT(priv); key = NULL; buf = NULL; len = 0; memset(&pub_key, 0, sizeof pub_key); if (btc->wallet_state == WALLET_ENCRYPTED_LOCKED) { if (wallet->pass) { struct secure_area *sec_b58; uint8 *encPrivKey; size_t encLen; if (!wallet_verify_hmac(wallet, priv, &encPrivKey, &encLen)) { return 0; } s = crypt_decrypt(wallet->ckey, encPrivKey, encLen, &sec_b58); free(encPrivKey); ASSERT(s); /* * 'buf' is a sensitive buffer here. It should be backed by * a struct secure_area. */ s = b58_privkey_to_bytes((char *)sec_b58->buf, &buf, &len); secure_free(sec_b58); ASSERT(s); } else { uint8 *pkey; size_t plen; str_to_bytes(pub, &pkey, &plen); hash160_calc(pkey, plen, &pub_key); free(pkey); } } else { s = b58_privkey_to_bytes(priv, &buf, &len); ASSERT(s); } if (buf) { key = key_alloc(); key_set_privkey(key, buf, len); memset(buf, 0, len); free(buf); key_get_pubkey_hash160(key, &pub_key); } ASSERT(!uint160_iszero(&pub_key)); wkey = safe_calloc(1, sizeof *wkey); wkey->cfg_idx = hashtable_getnumentries(wallet->hash_keys); wkey->btc_addr = b58_pubkey_from_uint160(&pub_key); wkey->desc = desc ? safe_strdup(desc) : NULL; wkey->pub_key = pub_key; wkey->birth = birth; wkey->key = key; wkey->spendable = spendable; if (spendable == 0) { Log(LGPFX" funds on %s are not spendable.\n", wkey->btc_addr); } s = hashtable_insert(wallet->hash_keys, &pub_key, sizeof pub_key, wkey); ASSERT(s); return 1; }
uint64_t savepathstr_with_sig_to_uint64(const char *savepathstr) { uint8_t b[sizeof(uint64_t)]; str_to_bytes(savepathstr, b, sizeof(b)); return htobe64(*(uint64_t *)&b); }
void md5str_to_bytes(const char *md5str, uint8_t *bytes) { str_to_bytes(md5str, bytes, MD5_DIGEST_LENGTH); }
void savepathstr_to_bytes(const char *savepathstr, uint8_t *bytes) { str_to_bytes(savepathstr, bytes, SAVE_PATH_LEN); }