What changed, and why it matters
This commit fixes a programming oversight in the BIP38 encrypted-key code. Previously, the AES encryption/decryption helper functions ignored any error returned by the underlying AES routine. If AES failed for some reason, the code would continue and produce or accept potentially invalid encrypted private-key data. The change makes the helpers return the AES status and makes the callers abort the operation if AES fails, preventing use of corrupted or incorrect key material.
Review whether any other cryptographic helpers in the codebase ignore `wally_aes` or similar primitive return values. Consider adding regression tests that simulate AES failure to verify the new error paths are exercised. No immediate emergency response is indicated, but the fix should be included in the next release.
Security signals we found
Unchecked return value from cryptographic primitive (wally_aes) fixed
AES failure during BIP38 encryption/decryption could previously produce or accept malformed key material
Error propagation added to both encryption and decryption paths
Evidence from the diff
The patch changes aes_enc_impl and aes_dec_impl in src/bip38.c from void to int, capturing and returning the return value of wally_aes(). Callers in bip38_raw_from_private_key and to_private_key now check ret != WALLY_OK after each AES call and jump to finish on failure. This closes an error-handling gap where AES failures were silently ignored during BIP38 encryption and decryption.
Changed components
src/bip38.cBIP38 private-key encryption (`bip38_raw_from_private_key`)BIP38 private-key decryption (`to_private_key`)AES helper wrappers (`aes_enc_impl`, `aes_dec_impl`)Inspect captured patch +26 / −14
### src/bip38.c
@@ -124,19 +124,21 @@ static int address_from_private_key(const unsigned char *bytes,
return ret;
}
-static void aes_enc_impl(const unsigned char *src, const unsigned char *xor_bytes,
- const unsigned char *key, unsigned char *bytes_out)
+static int aes_enc_impl(const unsigned char *src, const unsigned char *xor_bytes,
+ const unsigned char *key, unsigned char *bytes_out)
{
unsigned char plaintext[AES_BLOCK_LEN];
size_t i;
+ int ret;
for (i = 0; i < sizeof(plaintext); ++i)
plaintext[i] = src[i] ^ xor_bytes[i];
- wally_aes(key, AES_KEY_LEN_256, plaintext, AES_BLOCK_LEN,
- AES_FLAG_ENCRYPT, bytes_out, AES_BLOCK_LEN);
+ ret = wally_aes(key, AES_KEY_LEN_256, plaintext, AES_BLOCK_LEN,
+ AES_FLAG_ENCRYPT, bytes_out, AES_BLOCK_LEN);
wally_clear(plaintext, sizeof(plaintext));
+ return ret;
}
int bip38_raw_from_private_key(const unsigned char *bytes, size_t bytes_len,
@@ -181,8 +183,12 @@ int bip38_raw_from_private_key(const unsigned char *bytes, size_t bytes_len,
buf.prefix = BIP38_PREFIX;
buf.ec_type = BIP38_NO_ECMUL; /* FIXME: EC-Multiply support */
buf.flags = BIP38_FLAG_DEFAULT | (compressed ? BIP38_FLAG_COMPRESSED : 0);
- aes_enc_impl(bytes + 0, derived.half1_lo, derived.half2, buf.u.normal.half1);
- aes_enc_impl(bytes + 16, derived.half1_hi, derived.half2, buf.u.normal.half2);
+ ret = aes_enc_impl(bytes + 0, derived.half1_lo, derived.half2, buf.u.normal.half1);
+ if (ret != WALLY_OK)
+ goto finish;
+ ret = aes_enc_impl(bytes + 16, derived.half1_hi, derived.half2, buf.u.normal.half2);
+ if (ret != WALLY_OK)
+ goto finish;
if (flags & BIP38_KEY_SWAP_ORDER) {
/* Shuffle hash from the beginning to the end (normal->swapped) */
@@ -227,18 +233,20 @@ int bip38_from_private_key(const unsigned char *bytes, size_t bytes_len,
}
-static void aes_dec_impl(const unsigned char *cyphertext, const unsigned char *xor_bytes,
- const unsigned char *key, unsigned char *bytes_out)
+static int aes_dec_impl(const unsigned char *cyphertext, const unsigned char *xor_bytes,
+ const unsigned char *key, unsigned char *bytes_out)
{
size_t i;
+ int ret;
- wally_aes(key, AES_KEY_LEN_256,
- (unsigned char *)cyphertext, AES_BLOCK_LEN,
- AES_FLAG_DECRYPT,
- bytes_out, AES_BLOCK_LEN);
+ ret = wally_aes(key, AES_KEY_LEN_256,
+ (unsigned char *)cyphertext, AES_BLOCK_LEN,
+ AES_FLAG_DECRYPT,
+ bytes_out, AES_BLOCK_LEN);
for (i = 0; i < AES_BLOCK_LEN; ++i)
bytes_out[i] ^= xor_bytes[i];
+ return ret;
}
static int to_private_key(const char *bip38,
@@ -305,8 +313,12 @@ static int to_private_key(const char *bip38,
(unsigned char *)&derived, sizeof(derived))))
goto finish;
- aes_dec_impl(buf.u.normal.half1, derived.half1_lo, derived.half2, bytes_out + 0);
- aes_dec_impl(buf.u.normal.half2, derived.half1_hi, derived.half2, bytes_out + 16);
+ ret = aes_dec_impl(buf.u.normal.half1, derived.half1_lo, derived.half2, bytes_out + 0);
+ if (ret != WALLY_OK)
+ goto finish;
+ ret = aes_dec_impl(buf.u.normal.half2, derived.half1_hi, derived.half2, bytes_out + 16);
+ if (ret != WALLY_OK)
+ goto finish;
if (flags & BIP38_KEY_RAW_MODE)
ret = base58_get_checksum(bytes_out, len, &checksum);Why this scored 44/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.