Secure 256-bit hashing (ver. 4) for PowerBASIC
Code for the following two files appears below:
All code compiles with either PBDLL 8.0/9.0+ or PBCC 4.0/5.0+. Conditional compiling makes use of features of the newer compilers if available. This code should be treated as a replacement of my 2001 posting of SHA256 code.
MAJOR CHANGES
Many of the following revisions are contained in code updates which I distributed privately.
SHA256c.INC
SHA256c.BAS
from the original 2001 code:
----------------------------------------------------------------------
A hash is considered secure when it possesses the following qualities.
An input string can be of any length and thus far longer than its resulting hash. This is accomplished through use of a compression function which treats the input as a combination of any previously hashed input and the current input. Designing this feature is one of the most important challenges when creating a secure hash algorithm.
Secure hashes have many uses, among them validation of passphrases. In this arrangement, a user enters a passphrase when setting up an account. The input string is hashed, and the hash is stored (not the passphrase). When the user seeks readmission, the passphrase entered is hashed, and the hash is compared with the one on record. If the hashes match, the strings which generated them also must match. If the collection of stored hashes is compromised, the passphrases remain unknown.
Secure hashes are not designed for speed. The implementation below relies on assembly language to improve speed in its most heavily traveled section of code, but unless security is required, a secure hash is a poor choice when compared with the many simpler, far more efficient hash algorithms in widespread use.
----------------------------------------------------------------------
Available at the following URL is NIST information about the 256-bit, 384-bit, and 512-bit extensions to the SHA standard: http://csrc.nist.gov/groups/ST/toolk...e_hashing.html
This PB implementation of SHA256 is hereby placed in the public domain. Use it as you wish.
Greg Turgeon
gturgeon at ssge dot net
2/2009
Code for the following two files appears below:
- SHA256c.INC
Hash routines for returning 32-byte SHA256 hashes of buffers and files - SHA256c.BAS
Test bed EXE illustrating buffer and file hashing.
All code compiles with either PBDLL 8.0/9.0+ or PBCC 4.0/5.0+. Conditional compiling makes use of features of the newer compilers if available. This code should be treated as a replacement of my 2001 posting of SHA256 code.
MAJOR CHANGES
Many of the following revisions are contained in code updates which I distributed privately.
SHA256c.INC
- Added conditional compile %RETURN_LITTLE_ENDIAN setting for return of hash in little-endian format, with %RETURN_LITTLE_ENDIAN = %TRUE as default.
- Added MMX and SSE2 code support (with the original 32-bit code remaining intact). Run-time checking of CPU capabilities causes branching to the most advanced instruction set available.
- Aligned arrays and loops.
- Renamed hash context UDT. (SHA256_CONTEXT replaces original tSHA_STATE).
- Simplified parameter passing of two routines, with parameter Length& of FUNCTION SHA256_Buffer() changed to dword.
- Added %HASHLEN equate for consistency with other hash code.
SHA256c.BAS
- Replaced ShowHash&() function with revision which treats returned hash as series of bytes rather than dwords.
- Get_FileSize() utility function returns quad instead of dword.
from the original 2001 code:
----------------------------------------------------------------------
A hash is considered secure when it possesses the following qualities.
- Determining the input string from the hash (i.e., working backward from the hash alone to determine the string which generated it) is not considered feasible.
- Given an input string, it is not considered feasible to find another string which hashes to the same value.
- It is not considered feasible to find two random strings which hash to the same value.
An input string can be of any length and thus far longer than its resulting hash. This is accomplished through use of a compression function which treats the input as a combination of any previously hashed input and the current input. Designing this feature is one of the most important challenges when creating a secure hash algorithm.
Secure hashes have many uses, among them validation of passphrases. In this arrangement, a user enters a passphrase when setting up an account. The input string is hashed, and the hash is stored (not the passphrase). When the user seeks readmission, the passphrase entered is hashed, and the hash is compared with the one on record. If the hashes match, the strings which generated them also must match. If the collection of stored hashes is compromised, the passphrases remain unknown.
Secure hashes are not designed for speed. The implementation below relies on assembly language to improve speed in its most heavily traveled section of code, but unless security is required, a secure hash is a poor choice when compared with the many simpler, far more efficient hash algorithms in widespread use.
----------------------------------------------------------------------
Available at the following URL is NIST information about the 256-bit, 384-bit, and 512-bit extensions to the SHA standard: http://csrc.nist.gov/groups/ST/toolk...e_hashing.html
This PB implementation of SHA256 is hereby placed in the public domain. Use it as you wish.
Greg Turgeon
gturgeon at ssge dot net
2/2009
Comment