Skip to content

Fix: SHA-512 64-bit arithmetic overflow in Oracle 10g - #4

Open
LuizFelipeBarretoSimoes wants to merge 3 commits into
antonscheffer:masterfrom
LuizFelipeBarretoSimoes:master
Open

Fix: SHA-512 64-bit arithmetic overflow in Oracle 10g#4
LuizFelipeBarretoSimoes wants to merge 3 commits into
antonscheffer:masterfrom
LuizFelipeBarretoSimoes:master

Conversation

@LuizFelipeBarretoSimoes

Copy link
Copy Markdown

Context & Bug
When computing a SHA-512 hash using the as_crypto package in Oracle 10g, the function was returning an incorrect, repeating sequence (7fffffffffffffff...).
Root Cause
This issue occurs due to a known limitation in the Oracle 10g mathematical engine. The native bitwise functions (BITAND, BITXOR) and hexadecimal conversion functions (TO_NUMBER, TO_CHAR) handle numbers as signed 63-bit integers ($2^{63}-1$). Since the SHA-512 algorithm relies heavily on 64-bit unsigned integer blocks, the 10g engine was truncating values that exceeded the 63-bit boundary, defaulting to the maximum allowed value (7fffffffffffffff), effectively corrupting the hash.
Changes Implemented
To bypass the 63-bit limitation in Oracle 10g without losing performance, the sha512 function was refactored with custom mathematical helpers:
Safe Hex Conversions: Added hex_to_num and num_to_hex helpers to safely convert 64-bit numbers by splitting them into two 32-bit halves.
32-bit Chunked Bitwise Operations: Introduced bxor64 and band64 to replace native XOR/AND calls. These functions break 64-bit numbers into High/Low 32-bit chunks, process them safely, and reassemble them.
Mathematical Bit Shifting: Replaced the original ror64 and right-shifts with rot_r and shr_safe, which achieve accurate 64-bit shifting and rotating using pure MOD and TRUNC arithmetic instead of native bitwise wrappers.
Padding Fix: Ensured the mathematical padding generation properly constructs the exact 128-byte block size required by the SHA-512 specification.
Testing & Verification
Environment: Oracle 10g
Input string: 1234
Expected Output: d404559f602eab6fd602ac7680dacbfaadd13630335e951f097af3900e9de176b6db28512f2e000b9d04fba5133e8b1c6e8df59dd3a8ab9d15e3401116318c52
Result: Hash computed successfully with accurate values.

Context & BugWhen computing a SHA-512 hash using the as_crypto package in Oracle 10g, the function was returning an incorrect, repeating sequence (7fffffffffffffff...).Root CauseThis issue occurs due to a known limitation in the Oracle 10g mathematical engine. The native bitwise functions (BITAND, BITXOR) and hexadecimal conversion functions (TO_NUMBER, TO_CHAR) handle numbers as signed 63-bit integers ($2^{63}-1$). Since the SHA-512 algorithm relies heavily on 64-bit unsigned integer blocks, the 10g engine was truncating values that exceeded the 63-bit boundary, defaulting to the maximum allowed value (7fffffffffffffff), effectively corrupting the hash.Changes ImplementedTo bypass the 63-bit limitation in Oracle 10g without losing performance, the sha512 function was refactored with custom mathematical helpers:Safe Hex Conversions: Added hex_to_num and num_to_hex helpers to safely convert 64-bit numbers by splitting them into two 32-bit halves.32-bit Chunked Bitwise Operations: Introduced bxor64 and band64 to replace native XOR/AND calls. These functions break 64-bit numbers into High/Low 32-bit chunks, process them safely, and reassemble them.Mathematical Bit Shifting: Replaced the original ror64 and right-shifts with rot_r and shr_safe, which achieve accurate 64-bit shifting and rotating using pure MOD and TRUNC arithmetic instead of native bitwise wrappers.Padding Fix: Ensured the mathematical padding generation properly constructs the exact 128-byte block size required by the SHA-512 specification.Testing & VerificationEnvironment: Oracle 10gInput string: 1234Expected Output: d404559f602eab6fd602ac7680dacbfaadd13630335e951f097af3900e9de176b6db28512f2e000b9d04fba5133e8b1c6e8df59dd3a8ab9d15e3401116318c52Result: Hash computed successfully with accurate values.
Context & Bug
When computing a SHA-512 hash using the as_crypto package in Oracle 10g, the function was returning an incorrect, repeating sequence (7fffffffffffffff...).
Root Cause
This issue occurs due to a known limitation in the Oracle 10g mathematical engine. The native bitwise functions (BITAND, BITXOR) and hexadecimal conversion functions (TO_NUMBER, TO_CHAR) handle numbers as signed 63-bit integers ($2^{63}-1$). Since the SHA-512 algorithm relies heavily on 64-bit unsigned integer blocks, the 10g engine was truncating values that exceeded the 63-bit boundary, defaulting to the maximum allowed value (7fffffffffffffff), effectively corrupting the hash.
Changes Implemented
To bypass the 63-bit limitation in Oracle 10g without losing performance, the sha512 function was refactored with custom mathematical helpers:
Safe Hex Conversions: Added hex_to_num and num_to_hex helpers to safely convert 64-bit numbers by splitting them into two 32-bit halves.
32-bit Chunked Bitwise Operations: Introduced bxor64 and band64 to replace native XOR/AND calls. These functions break 64-bit numbers into High/Low 32-bit chunks, process them safely, and reassemble them.
Mathematical Bit Shifting: Replaced the original ror64 and right-shifts with rot_r and shr_safe, which achieve accurate 64-bit shifting and rotating using pure MOD and TRUNC arithmetic instead of native bitwise wrappers.
Padding Fix: Ensured the mathematical padding generation properly constructs the exact 128-byte block size required by the SHA-512 specification.
Testing & Verification
Environment: Oracle 10g
Input string: 1234
Expected Output: d404559f602eab6fd602ac7680dacbfaadd13630335e951f097af3900e9de176b6db28512f2e000b9d04fba5133e8b1c6e8df59dd3a8ab9d15e3401116318c52
Result: Hash computed successfully with accurate values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant