Skip to content

Commit

Permalink
update tx ordering on confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
voisine committed Dec 27, 2017
1 parent 9813ffa commit b8ead01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
36 changes: 18 additions & 18 deletions BRBIP38Key.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ static const uint8_t sboxi[256] = {
static void _BRAES256ECBEncrypt(const void *key32, void *buf16)
{
size_t i, j;
uint32_t _k[32/4], _x[16/4];
uint8_t *x = (uint8_t *)_x, *k = (uint8_t *)_k, r = 1, a, b, c, d, e;
uint32_t key[32/4], buf[16/4];
uint8_t *x = (uint8_t *)buf, *k = (uint8_t *)key, r = 1, a, b, c, d, e;

memcpy(_k, key32, sizeof(_k));
memcpy(_x, buf16, sizeof(_x));
memcpy(key, key32, sizeof(key));
memcpy(buf, buf16, sizeof(buf));

for (i = 0; i < 14; i++) {
for (j = 0; j < 4; j++) _x[j] ^= _k[j+(i & 1)*4]; // add round key
for (j = 0; j < 4; j++) buf[j] ^= key[j+(i & 1)*4]; // add round key

for (j = 0; j < 16; j++) x[j] = sbox[x[j]]; // sub bytes

Expand All @@ -119,20 +119,20 @@ static void _BRAES256ECBEncrypt(const void *key32, void *buf16)
}

var_clean(&r, &a, &b, &c, &d, &e);
for (i = 0; i < 4; i++) _x[i] ^= _k[i]; // final add round key
mem_clean(_k, sizeof(_k));
memcpy(buf16, _x, sizeof(_x));
mem_clean(_x, sizeof(_x));
for (i = 0; i < 4; i++) buf[i] ^= key[i]; // final add round key
mem_clean(key, sizeof(key));
memcpy(buf16, buf, sizeof(buf));
mem_clean(buf, sizeof(buf));
}

static void _BRAES256ECBDecrypt(const void *key32, void *buf16)
{
size_t i, j;
uint32_t _k[32/4], _x[16/4];
uint8_t *x = (uint8_t *)_x, *k = (uint8_t *)_k, r = 1, a, b, c, d, e, f, g, h;
uint32_t key[32/4], buf[16/4];
uint8_t *x = (uint8_t *)buf, *k = (uint8_t *)key, r = 1, a, b, c, d, e, f, g, h;

memcpy(_k, key32, sizeof(_k));
memcpy(_x, buf16, sizeof(_x));
memcpy(key, key32, sizeof(key));
memcpy(buf, buf16, sizeof(buf));

for (i = 0; i < 7; i++) { // expand key
k[0] ^= sbox[k[29]] ^ r, k[1] ^= sbox[k[30]], k[2] ^= sbox[k[31]], k[3] ^= sbox[k[28]], r = xt(r);
Expand All @@ -142,7 +142,7 @@ static void _BRAES256ECBDecrypt(const void *key32, void *buf16)
}

for (i = 0; i < 14; i++) {
for (j = 0; j < 4; j++) _x[j] ^= _k[j+(i & 1)*4]; // add round key
for (j = 0; j < 4; j++) buf[j] ^= key[j+(i & 1)*4]; // add round key

for (j = 0; i > 0 && j < 16; j += 4) { // unmix columns
a = x[j], b = x[j+1], c = x[j+2], d = x[j+3], e = a ^ b ^ c ^ d;
Expand All @@ -166,10 +166,10 @@ static void _BRAES256ECBDecrypt(const void *key32, void *buf16)
}

var_clean(&r, &a, &b, &c, &d, &e, &f, &g, &h);
for (i = 0; i < 4; i++) _x[i] ^= _k[i]; // final add round key
mem_clean(_k, sizeof(_k));
memcpy(buf16, _x, sizeof(_x));
mem_clean(_x, sizeof(_x));
for (i = 0; i < 4; i++) buf[i] ^= key[i]; // final add round key
mem_clean(key, sizeof(key));
memcpy(buf16, buf, sizeof(buf));
mem_clean(buf, sizeof(buf));
}

static UInt256 _BRBIP38DerivePassfactor(uint8_t flag, const uint8_t *entropy, const char *passphrase)
Expand Down
9 changes: 8 additions & 1 deletion BRWallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ void BRWalletUpdateTransactions(BRWallet *wallet, const UInt256 txHashes[], size
BRTransaction *tx;
UInt256 hashes[txCount];
int needsUpdate = 0;
size_t i, j;
size_t i, j, k;

assert(wallet != NULL);
assert(txHashes != NULL || txCount == 0);
Expand All @@ -949,6 +949,13 @@ void BRWalletUpdateTransactions(BRWallet *wallet, const UInt256 txHashes[], size
tx->blockHeight = blockHeight;

if (_BRWalletContainsTx(wallet, tx)) {
for (k = array_count(wallet->transactions); k > 0; k--) { // remove and re-insert tx to keep wallet sorted
if (! BRTransactionEq(wallet->transactions[k - 1], tx)) continue;
array_rm(wallet->transactions, k - 1);
_BRWalletInsertTx(wallet, tx);
break;
}

hashes[j++] = txHashes[i];
if (BRSetContains(wallet->pendingTx, tx) || BRSetContains(wallet->invalidTx, tx)) needsUpdate = 1;
}
Expand Down

0 comments on commit b8ead01

Please sign in to comment.