DESCRIPTION

The SHA2 functions implement the NIST Secure Hash Standard, FIPS PUB
180-2. The SHA2 functions are used to generate a condensed representation
of a message called a message digest, suitable for use as a digital sig-
nature. There are three families of functions, with names corresponding
to the number of bits in the resulting message digest. The SHA-256 func-
tions are limited to processing a message of less than 2^64 bits as in-
put. The SHA-384 and SHA-512 functions can process a message of at most
2^128 - 1 bits as input.
The SHA2 functions are considered to be more secure than the sha1(3)
functions with which they share a similar interface. The 256, 384, and
512-bit versions of SHA2 share the same interface. For brevity, only the
256-bit variants are described below.
The SHA256_Init() function initializes a SHA256_CTX context for use with
SHA256_Update(), and SHA256_Final(). The SHA256_Update() function adds
data of length len to the SHA256_CTX specified by context. SHA256_Final()
is called when all data has been added via SHA256_Update() and stores a
message digest in the digest parameter.
The SHA256_Pad() function can be used to apply padding to the message
digest as in SHA256_Final(), but the current context can still be used
with SHA256_Update().
The SHA256_Transform() function is used by SHA256_Update() to hash 512-
bit blocks and forms the core of the algorithm. Most programs should use
the interface provided by SHA256_Init(), SHA256_Update(), and
SHA256_Final() instead of calling SHA256_Transform() directly.
The SHA256_End() function is a front end for SHA256_Final() which con-
verts the digest into an ASCII representation of the digest in hexade-
cimal.
The SHA256_File() function calculates the digest for a file and returns
the result via SHA256_End(). If SHA256_File() is unable to open the file,
a NULL pointer is returned.
SHA256_FileChunk() behaves like SHA256_File() but calculates the digest
only for that portion of the file starting at offset and continuing for
length bytes or until end of file is reached, whichever comes first. A
zero length can be specified to read until end of file. A negative length
or offset will be ignored.
The SHA256_Data() function calculates the digest of an arbitrary string
and returns the result via SHA256_End().
For each of the SHA256_End(), SHA256_File(), SHA256_FileChunk(), and
SHA256_Data() functions the buf parameter should either be a string large
enough to hold the resulting digest (e.g. SHA256_DIGEST_STRING_LENGTH,
SHA384_DIGEST_STRING_LENGTH, or SHA512_DIGEST_STRING_LENGTH, depending on
the function being used) or a NULL pointer. In the latter case, space
will be dynamically allocated via malloc(3) and should be freed using
free(3) when it is no longer needed.

SEE ALSO

HISTORY

The SHA2 functions appeared in OpenBSD 3.4.

AUTHORS

This implementation of the SHA functions was written by Aaron D. Gifford.
The SHA256_End(), SHA256_File(), SHA256_FileChunk(), and SHA256_Data()
helper functions are derived from code written by Poul-Henning Kamp.

CAVEATS

This implementation of the Secure Hash Standard has not been validated by
NIST and as such is not in official compliance with the standard.
If a message digest is to be copied to a multi-byte type (i.e. an array
of five 32-bit integers) it will be necessary to perform byte swapping on
little endian machines such as the i386, alpha, and vax.
MirOS BSD #10-current July 18, 2015 3