From 9475c19921162986119ee69eba8bfa2a9832d551 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 9 Feb 2019 20:02:16 +0100 Subject: Minor changes, add man pages, rename libsha2_state_initialise to libsha2_init MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 16 ++++- TODO | 2 + init.c | 125 ++++++++++++++++++++++++++++++++++++++++ libsha2.h | 2 +- libsha2.h.0 | 110 +++++++++++++++++++++++++++++++++++ libsha2_algorithm_output_size.3 | 51 ++++++++++++++++ libsha2_behex_lower.3 | 49 ++++++++++++++++ libsha2_behex_upper.3 | 49 ++++++++++++++++ libsha2_digest.3 | 61 ++++++++++++++++++++ libsha2_init.3 | 64 ++++++++++++++++++++ libsha2_state_output_size.3 | 40 +++++++++++++ libsha2_sum_fd.3 | 77 +++++++++++++++++++++++++ libsha2_unhex.3 | 48 +++++++++++++++ libsha2_update.3 | 42 ++++++++++++++ state_initialise.c | 125 ---------------------------------------- sum_fd.c | 2 +- unhex.c | 5 +- 17 files changed, 738 insertions(+), 130 deletions(-) create mode 100644 init.c create mode 100644 libsha2.h.0 create mode 100644 libsha2_algorithm_output_size.3 create mode 100644 libsha2_behex_lower.3 create mode 100644 libsha2_behex_upper.3 create mode 100644 libsha2_digest.3 create mode 100644 libsha2_init.3 create mode 100644 libsha2_state_output_size.3 create mode 100644 libsha2_sum_fd.3 create mode 100644 libsha2_unhex.3 create mode 100644 libsha2_update.3 delete mode 100644 state_initialise.c diff --git a/Makefile b/Makefile index 566907e..893704a 100644 --- a/Makefile +++ b/Makefile @@ -21,12 +21,26 @@ OBJ =\ behex_lower.o\ behex_upper.o\ digest.o\ - state_initialise.o\ + init.o\ state_output_size.o\ sum_fd.o\ unhex.o\ update.o\ +MAN0 =\ + libsha2.h.0 + +MAN3 =\ + libsha2_algorithm_output_size.3\ + libsha2_behex_lower.3\ + libsha2_behex_upper.3\ + libsha2_digest.3\ + libsha2_init.3\ + libsha2_state_output_size.3\ + libsha2_sum_fd.3\ + libsha2_unhex.3\ + libsha2_update.3 + all: libsha2.a libsha2.$(LIBEXT) $(OBJ): $(@:.o=.c) $(HDR) diff --git a/TODO b/TODO index 0087301..7adeb9b 100644 --- a/TODO +++ b/TODO @@ -1 +1,3 @@ Add HMAC support +Add marshal support +Add bit support diff --git a/init.c b/init.c new file mode 100644 index 0000000..a7ffa73 --- /dev/null +++ b/init.c @@ -0,0 +1,125 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +/** + * Round constants, SHA-256, should keep the 32 most significant bits of 64 first constants + */ +static const uint64_t ROUND_CONSTANTS[] = { + 0x428A2F98D728AE22ULL, 0x7137449123EF65CDULL, 0xB5C0FBCFEC4D3B2FULL, 0xE9B5DBA58189DBBCULL, + 0x3956C25BF348B538ULL, 0x59F111F1B605D019ULL, 0x923F82A4AF194F9BULL, 0xAB1C5ED5DA6D8118ULL, + 0xD807AA98A3030242ULL, 0x12835B0145706FBEULL, 0x243185BE4EE4B28CULL, 0x550C7DC3D5FFB4E2ULL, + 0x72BE5D74F27B896FULL, 0x80DEB1FE3B1696B1ULL, 0x9BDC06A725C71235ULL, 0xC19BF174CF692694ULL, + 0xE49B69C19EF14AD2ULL, 0xEFBE4786384F25E3ULL, 0x0FC19DC68B8CD5B5ULL, 0x240CA1CC77AC9C65ULL, + 0x2DE92C6F592B0275ULL, 0x4A7484AA6EA6E483ULL, 0x5CB0A9DCBD41FBD4ULL, 0x76F988DA831153B5ULL, + 0x983E5152EE66DFABULL, 0xA831C66D2DB43210ULL, 0xB00327C898FB213FULL, 0xBF597FC7BEEF0EE4ULL, + 0xC6E00BF33DA88FC2ULL, 0xD5A79147930AA725ULL, 0x06CA6351E003826FULL, 0x142929670A0E6E70ULL, + 0x27B70A8546D22FFCULL, 0x2E1B21385C26C926ULL, 0x4D2C6DFC5AC42AEDULL, 0x53380D139D95B3DFULL, + 0x650A73548BAF63DEULL, 0x766A0ABB3C77B2A8ULL, 0x81C2C92E47EDAEE6ULL, 0x92722C851482353BULL, + 0xA2BFE8A14CF10364ULL, 0xA81A664BBC423001ULL, 0xC24B8B70D0F89791ULL, 0xC76C51A30654BE30ULL, + 0xD192E819D6EF5218ULL, 0xD69906245565A910ULL, 0xF40E35855771202AULL, 0x106AA07032BBD1B8ULL, + 0x19A4C116B8D2D0C8ULL, 0x1E376C085141AB53ULL, 0x2748774CDF8EEB99ULL, 0x34B0BCB5E19B48A8ULL, + 0x391C0CB3C5C95A63ULL, 0x4ED8AA4AE3418ACBULL, 0x5B9CCA4F7763E373ULL, 0x682E6FF3D6B2B8A3ULL, + 0x748F82EE5DEFB2FCULL, 0x78A5636F43172F60ULL, 0x84C87814A1F0AB72ULL, 0x8CC702081A6439ECULL, + 0x90BEFFFA23631E28ULL, 0xA4506CEBDE82BDE9ULL, 0xBEF9A3F7B2C67915ULL, 0xC67178F2E372532BULL, + 0xCA273ECEEA26619CULL, 0xD186B8C721C0C207ULL, 0xEADA7DD6CDE0EB1EULL, 0xF57D4F7FEE6ED178ULL, + 0x06F067AA72176FBAULL, 0x0A637DC5A2C898A6ULL, 0x113F9804BEF90DAEULL, 0x1B710B35131C471BULL, + 0x28DB77F523047D84ULL, 0x32CAAB7B40C72493ULL, 0x3C9EBE0A15C9BEBCULL, 0x431D67C49C100D4CULL, + 0x4CC5D4BECB3E42B6ULL, 0x597F299CFC657E2AULL, 0x5FCB6FAB3AD6FAECULL, 0x6C44198C4A475817ULL +}; + + +/** + * Initial state for SHA224 + */ +static const uint32_t H_224[] = { + 0xC1059ED8UL, 0x367CD507UL, 0x3070DD17UL, 0xF70E5939UL, + 0xFFC00B31UL, 0x68581511UL, 0x64F98FA7UL, 0xBEFA4FA4UL +}; + +/** + * Initial state for SHA256 + */ +static const uint32_t H_256[] = { + 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL, + 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL +}; + +/** + * Initial state for SHA384 + */ +static const uint64_t H_384[] = { + 0xCBBB9D5DC1059ED8ULL, 0x629A292A367CD507ULL, 0x9159015A3070DD17ULL, 0x152FECD8F70E5939ULL, + 0x67332667FFC00B31ULL, 0x8EB44A8768581511ULL, 0xDB0C2E0D64F98FA7ULL, 0x47B5481DBEFA4FA4ULL +}; + +/** + * Initial state for SHA512 + */ +static const uint64_t H_512[] = { + 0x6A09E667F3BCC908ULL, 0xBB67AE8584CAA73BULL, 0x3C6EF372FE94F82BULL, 0xA54FF53A5F1D36F1ULL, + 0x510E527FADE682D1ULL, 0x9B05688C2B3E6C1FULL, 0x1F83D9ABFB41BD6BULL, 0x5BE0CD19137E2179ULL +}; + +/** + * Initial state for SHA512/224 + */ +static const uint64_t H_512_224[] = { + 0x8C3D37C819544DA2ULL, 0x73E1996689DCD4D6ULL, 0x1DFAB7AE32FF9C82ULL, 0x679DD514582F9FCFULL, + 0x0F6D2B697BD44DA8ULL, 0x77E36F7304C48942ULL, 0x3F9D85A86A1D36C8ULL, 0x1112E6AD91D692A1ULL +}; + +/** + * Initial state for SHA512/256 + */ +static const uint64_t H_512_256[] = { + 0x22312194FC2BF72CULL, 0x9F555FA3C84C64C2ULL, 0x2393B86B6F53B151ULL, 0x963877195940EABDULL, + 0x96283EE2A88EFFE3ULL, 0xBE5E1E2553863992ULL, 0x2B0199FC2C85B8AAULL, 0x0EB72DDC81C52CA2ULL +}; + + +/** + * Initialise a state + * + * @parma state The state that should be initialised + * @parma algorithm The hashing algorithm + * @return Zero on success, -1 on error + */ +int +libsha2_init(struct libsha2_state *restrict state, enum libsha2_algorithm algorithm) +{ + size_t i; + + memset(state, 0, sizeof(*state)); + state->message_size = 0; + state->algorithm = algorithm; + + /* Set initial hash values. */ + switch (algorithm) { + case LIBSHA2_224: memcpy(state->h.b32, H_224, sizeof(H_224)); break; + case LIBSHA2_256: memcpy(state->h.b32, H_256, sizeof(H_256)); break; + case LIBSHA2_384: memcpy(state->h.b64, H_384, sizeof(H_384)); break; + case LIBSHA2_512: memcpy(state->h.b64, H_512, sizeof(H_512)); break; + case LIBSHA2_512_224: memcpy(state->h.b64, H_512_224, sizeof(H_512_224)); break; + case LIBSHA2_512_256: memcpy(state->h.b64, H_512_256, sizeof(H_512_256)); break; + default: + return errno = EINVAL, -1; + } + + /* Set round constants, and chunk size. */ + switch (algorithm) { + case LIBSHA2_224: + case LIBSHA2_256: + for (i = 0; i < 64; i++) + state->k.b32[i] = (uint32_t)(ROUND_CONSTANTS[i] >> 32); + state->chunk_size = 64; + break; + + default: + memcpy(state->k.b64, ROUND_CONSTANTS, sizeof(ROUND_CONSTANTS)); + state->chunk_size = 128; + break; + } + + return 0; +} diff --git a/libsha2.h b/libsha2.h index 5b37130..02a386b 100644 --- a/libsha2.h +++ b/libsha2.h @@ -164,7 +164,7 @@ struct libsha2_state { #if defined(__GNUC__) __attribute__((__leaf__, __nothrow__, __nonnull__)) #endif -int libsha2_state_initialise(struct libsha2_state *restrict, enum libsha2_algorithm); +int libsha2_init(struct libsha2_state *restrict, enum libsha2_algorithm); /** * Get the output size of the algorithm specified for a state diff --git a/libsha2.h.0 b/libsha2.h.0 new file mode 100644 index 0000000..5e79f73 --- /dev/null +++ b/libsha2.h.0 @@ -0,0 +1,110 @@ +.TH LIBSHA2.H 0 2019-02-09 libjson +.SH NAME +libsha2.h \- SHA 2 library header +.SH SYNOPSIS +.nf +#include + +enum libsha2_algorithm { + LIBSHA2_224, /* SHA-224 */ + LIBSHA2_256, /* SHA-256 */ + LIBSHA2_384, /* SHA-384 */ + LIBSHA2_512, /* SHA-512 */ + LIBSHA2_512_224, /* SHA-512/224 */ + LIBSHA2_512_256 /* SHA-512/256 */ +}; + +struct libsha2_state { + /* members omitted */ +}; + +int libsha2_init(struct libsha2_state *restrict \fIstate\fP, enum libsha2_algorithm \fIalgorithm\fP); +size_t libsha2_state_output_size(const struct libsha2_state *restrict \fIstate\fP); +size_t libsha2_algorithm_output_size(enum libsha2_algorithm \fIalgorithm\fP); +void libsha2_update(struct libsha2_state *restrict \fIstate\fP, const char *restrict \fImessage\fP, size_t \fImsglen\fP); +void libsha2_digest(struct libsha2_state *restrict \fIstate\fP, const char *restrict \fImessage\fP, size_t \fImsglen\fP, char *\fIoutput\fP); +int libsha2_sum_fd(int \fIfd\fP, enum libsha2_algorithm \fIalgorithm\fP, char *restrict \fIhashsum\fP); +void libsha2_behex_lower(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP, size_t \fIn\fP); +void libsha2_behex_upper(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP, size_t \fIn\fP); +void libsha2_unhex(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP); +.fi +.PP +Link with +.IR \-lsha2 . +.SH DESCRIPTION +The +.B libsha2.h +header, the header for the libsha2 C library defines +.B enum libsha2_algorithm +which has one value per supported algorithm: +.TP +.B LIBSHA2_224 +SHA-224 +.TP +.B LIBSHA2_256 +SHA-256 +.TP +.B LIBSHA2_384 +SHA-384 +.TP +.B LIBSHA2_512 +SHA-512 +.TP +.B LIBSHA2_512_224 +SHA-512/224 +.TP +.B LIBSHA2_512_256 +SHA-512/256 +.PP +Further, the +.B libsha2.h +header defines the opaque, but complete, +.B struct libsha2_state +which stores the selected algorithm and +the state of the hashing. The header also +defines the functions: +.TP +.BR libsha2_init (3) +Initialise hashing state. +.TP +.BR libsha2_state_output_size "(3), " libsha2_algorithm_output_size (3) +Get the output size for an algorithm. +.TP +.BR libsha2_update (3) +Feed data into the hashing state. +.TP +.BR libsha2_digest (3) +Get the result of a hashing. +.TP +.BR libsha2_sum_fd (3) +Hash an entire file. +.TP +.BR libsha2_behex_lower "(3), " libsha2_behex_upper (3) +Convert binary output from +.BR libsha2_digest (3) +to hexadecimal. +.TP +.BR libsha2_unhex (3) +Convert a hexadecimal hash to binary. +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsha2_algorithm_output_size (3), +.BR libsha2_behex_lower (3), +.BR libsha2_behex_upper (3), +.BR libsha2_digest (3), +.BR libsha2_init (3), +.BR libsha2_state_output_size (3), +.BR libsha2_sum_fd (3), +.BR libsha2_unhex (3), +.BR libsha2_update (3) diff --git a/libsha2_algorithm_output_size.3 b/libsha2_algorithm_output_size.3 new file mode 100644 index 0000000..49fd598 --- /dev/null +++ b/libsha2_algorithm_output_size.3 @@ -0,0 +1,51 @@ +.TH LIBSHA2_ALGORITHM_OUTPUT_SIZE 3 2019-02-09 libjson +.SH NAME +libsha2_algorithm_output_size \- Get the size of the output for a SHA 2 algorithm +.SH SYNOPSIS +.nf +#include + +size_t libsha2_algorithm_output_size(enum libsha2_algorithm \fIalgorithm\fP); +.fi +.PP +Link with +.IR \-lsha2 . +.SH DESCRIPTION +The +.BR libsha2_algorithm_output_size () +function get the output size for the +binary output of the hash algorithm +selected for the selected +.IR algorithm . +.SH RETURN VALUE +The +.BR libsha2_algorithm_output_size () +function returns the output size in bytes, +a positive number upon successful completion, +otherwise 0 is returned and +.I errno +is set appropriately. +.SH ERRORS +The +.BR libsha2_algorithm_output_size () +function will fail if: +.TP +.B EINVAL +.I algorithm +is not a valid +.B enum libsha2_algorithm +value. +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsha2_state_output_size (3) diff --git a/libsha2_behex_lower.3 b/libsha2_behex_lower.3 new file mode 100644 index 0000000..7882aa7 --- /dev/null +++ b/libsha2_behex_lower.3 @@ -0,0 +1,49 @@ +.TH LIBSHA2_BEHEX_LOWER 3 2019-02-09 libjson +.SH NAME +libsha2_behex_lower \- Convert binary to lower case hexadecimal +.SH SYNOPSIS +.nf +#include + +void libsha2_behex_lower(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP, size_t \fIn\fP); +.fi +.PP +Link with +.IR \-lsha2 . +.SH DESCRIPTION +The +.BR libsha2_behex_lower () +function converts the +.I n +first bytes of +.I hashsum +to lower case hexadecimal, as exactly +.I 2*n +characters, and stores the result, with +NUL byte termination, to +.IR output . +.PP +The user must make sure that +.I output +is at least +.I 2*n+1 +bytes large. +.SH RETURN VALUE +None. +.SH ERRORS +None. +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsha2_behex_upper (3), +.BR libsha2_unhex (3) diff --git a/libsha2_behex_upper.3 b/libsha2_behex_upper.3 new file mode 100644 index 0000000..6faaae2 --- /dev/null +++ b/libsha2_behex_upper.3 @@ -0,0 +1,49 @@ +.TH LIBSHA2_BEHEX_UPPER 3 2019-02-09 libjson +.SH NAME +libsha2_behex_upper \- Convert binary to upper case hexadecimal +.SH SYNOPSIS +.nf +#include + +void libsha2_behex_upper(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP, size_t \fIn\fP); +.fi +.PP +Link with +.IR \-lsha2 . +.SH DESCRIPTION +The +.BR libsha2_behex_upper () +function converts the +.I n +first bytes of +.I hashsum +to upper case hexadecimal, as exactly +.I 2*n +characters, and stores the result, with +NUL byte termination, to +.IR output . +.PP +The user must make sure that +.I output +is at least +.I 2*n+1 +bytes large. +.SH RETURN VALUE +None. +.SH ERRORS +None. +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsha2_behex_lower (3), +.BR libsha2_unhex (3) diff --git a/libsha2_digest.3 b/libsha2_digest.3 new file mode 100644 index 0000000..2e7d31e --- /dev/null +++ b/libsha2_digest.3 @@ -0,0 +1,61 @@ +.TH LIBSHA2_DIGEST 3 2019-02-09 libjson +.SH NAME +libsha2_digest \- Get the result of a SHA 2 hashing +.SH SYNOPSIS +.nf +#include + +void libsha2_digest(struct libsha2_state *restrict \fIstate\fP, const char *restrict \fImessage\fP, size_t \fImsglen\fP, char *\fIoutput\fP); +.fi +.PP +Link with +.IR \-lsha2 . +.SH DESCRIPTION +The +.BR libsha2_digest () +function feeds the first +.I msglen +bytes of +.I message +into the hashing state of the +.I state +parameter, and finalises the hashing. +The resulting hash is stored in binary +format in +.I output . +The user must make sure that +.I output +is sufficiently large, which means at +least the return value of the +.BR libsha2_state_output_size (3) +function. +.PP +The +.BR libsha2_behex_lower (3) +and +.BR libsha2_behex_upper (3) +functions can be used to convert the +result to hexadecimal format. +.SH RETURN VALUE +None. +.SH ERRORS +None. +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsha2_behex_lower (3), +.BR libsha2_behex_upper (3), +.BR libsha2_init (3), +.BR libsha2_state_output_size (3), +.BR libsha2_sum_fd (3), +.BR libsha2_update (3) diff --git a/libsha2_init.3 b/libsha2_init.3 new file mode 100644 index 0000000..2a11f89 --- /dev/null +++ b/libsha2_init.3 @@ -0,0 +1,64 @@ +.TH LIBSHA2_INIT 3 2019-02-09 libjson +.SH NAME +libsha2_init \- Initialises hashing with a SHA 2 algorithm +.SH SYNOPSIS +.nf +#include + +enum libsha2_algorithm { + LIBSHA2_224, /* SHA-224 */ + LIBSHA2_256, /* SHA-256 */ + LIBSHA2_384, /* SHA-384 */ + LIBSHA2_512, /* SHA-512 */ + LIBSHA2_512_224, /* SHA-512/224 */ + LIBSHA2_512_256 /* SHA-512/256 */ +}; + +int libsha2_init(struct libsha2_state *restrict \fIstate\fP, enum libsha2_algorithm \fIalgorithm\fP); +.fi +.PP +Link with +.IR \-lsha2 . +.SH DESCRIPTION +The +.BR libsha2_init () +function stores the selected +.I algorithm +in +.I state +and initialises +.I state +accordingly with the initial hashing state. +.SH RETURN VALUE +The +.BR libsha2_init () +function returns 0 upon successful completion, +otherwise -1 is returned and +.I errno +is set appropriately. +.SH ERRORS +The +.BR libsha2_init () +function will fail if: +.TP +.B EINVAL +.I algorithm +is not a valid +.B enum libsha2_algorithm +value. +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsha2_digest (3), +.BR libsha2_sum_fd (3), +.BR libsha2_update (3) diff --git a/libsha2_state_output_size.3 b/libsha2_state_output_size.3 new file mode 100644 index 0000000..34b4156 --- /dev/null +++ b/libsha2_state_output_size.3 @@ -0,0 +1,40 @@ +.TH LIBSHA2_STATE_OUTPUT_SIZE 3 2019-02-09 libjson +.SH NAME +libsha2_state_output_size \- Get the size of the output for a SHA 2 algorithm +.SH SYNOPSIS +.nf +#include + +size_t libsha2_state_output_size(const struct libsha2_state *restrict \fIstate\fP); +.fi +.PP +Link with +.IR \-lsha2 . +.SH DESCRIPTION +The +.BR libsha2_state_output_size () +function get the output size for the +binary output of the hash algorithm +selected for +.IR state . +.SH RETURN VALUE +The +.BR libsha2_state_output_size () +function returns the output size in bytes, +a positive number. +.SH ERRORS +None. +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsha2_algorithm_output_size (3) diff --git a/libsha2_sum_fd.3 b/libsha2_sum_fd.3 new file mode 100644 index 0000000..18e9291 --- /dev/null +++ b/libsha2_sum_fd.3 @@ -0,0 +1,77 @@ +.TH LIBSHA2_SUM_FD 3 2019-02-09 libjson +.SH NAME +libsha2_sum_fd \- Hash a file with a SHA 2 algorithm +.SH SYNOPSIS +.nf +#include + +enum libsha2_algorithm { + LIBSHA2_224, /* SHA-224 */ + LIBSHA2_256, /* SHA-256 */ + LIBSHA2_384, /* SHA-384 */ + LIBSHA2_512, /* SHA-512 */ + LIBSHA2_512_224, /* SHA-512/224 */ + LIBSHA2_512_256 /* SHA-512/256 */ +}; + +int libsha2_sum_fd(int \fIfd\fP, enum libsha2_algorithm \fIalgorithm\fP, char *restrict \fIhashsum\fP); +.fi +.PP +Link with +.IR \-lsha2 . +.SH DESCRIPTION +The +.BR libsha2_sum_fd () +function hashes the file with the +file descriptor +.I fd +with the selected +.IR algorithm . +The resulting hash is stored in binary +format in +.I hashsum . +The user must make sure that +.I hashsum +is sufficiently large, which means at +least the return value of the +.BR libsha2_algorithm_output_size (3) +function. +.PP +The +.BR libsha2_behex_lower (3) +and +.BR libsha2_behex_upper (3) +functions can be used to convert the +result to hexadecimal format. +.SH RETURN VALUE +The +.BR libsha2_sum_fd () +function returns 0 upon successful completion, +otherwise -1 is returned and +.I errno +is set appropriately. +.SH ERRORS +The +.BR libsha2_sum_fd () +function may fail for any reason specified for the +.BR read (3) +and +.BR libsha2_init (3) +functions. +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsha2_algorithm_output_size (3), +.BR libsha2_behex_lower (3), +.BR libsha2_behex_upper (3), +.BR libsha2_init (3) diff --git a/libsha2_unhex.3 b/libsha2_unhex.3 new file mode 100644 index 0000000..c1f7865 --- /dev/null +++ b/libsha2_unhex.3 @@ -0,0 +1,48 @@ +.TH LIBSHA2_UNHEX 3 2019-02-09 libjson +.SH NAME +libsha2_unhex \- Covert hexadecimal to binary +.SH SYNOPSIS +.nf +#include + +void libsha2_unhex(char *restrict \fIoutput\fP, const char *restrict \fIhashsum\fP); +.fi +.PP +Link with +.IR \-lsha2 . +.SH DESCRIPTION +The +.BR libsha2_unhex () +function converts the content of the string +.I hashsum +to binary and stores the result in +.IR output . +.PP +The user must make sure that +.I hashsum +is a NUL terminated string with an even number of characters +and each character is a hexadecimal digit (both lower case +and upper case, including mixing, is allowed). The output will +be half the length of this string, and the user must make sure +that +.I output +is at least this size. +.SH RETURN VALUE +None. +.SH ERRORS +None. +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsha2_behex_lower (3), +.BR libsha2_behex_upper (3) diff --git a/libsha2_update.3 b/libsha2_update.3 new file mode 100644 index 0000000..c504b76 --- /dev/null +++ b/libsha2_update.3 @@ -0,0 +1,42 @@ +.TH LIBSHA2_UPDATE 3 2019-02-09 libjson +.SH NAME +libsha2_update \- Feed data into a SHA 2 algorithm +.SH SYNOPSIS +.nf +#include + +void libsha2_update(struct libsha2_state *restrict \fIstate\fP, const char *restrict \fImessage\fP, size_t \fImsglen\fP); +.fi +.PP +Link with +.IR \-lsha2 . +.SH DESCRIPTION +The +.BR libsha2_update () +function feeds the first +.I msglen +bytes of +.I message +into the hashing state of the +.I state +parameter. +.SH RETURN VALUE +None. +.SH ERRORS +None. +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsha2_digest (3), +.BR libsha2_init (3), +.BR libsha2_sum_fd (3) diff --git a/state_initialise.c b/state_initialise.c deleted file mode 100644 index ba6fee0..0000000 --- a/state_initialise.c +++ /dev/null @@ -1,125 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include "common.h" - - -/** - * Round constants, SHA-256, should keep the 32 most significant bits of 64 first constants - */ -static const uint64_t ROUND_CONSTANTS[] = { - 0x428A2F98D728AE22ULL, 0x7137449123EF65CDULL, 0xB5C0FBCFEC4D3B2FULL, 0xE9B5DBA58189DBBCULL, - 0x3956C25BF348B538ULL, 0x59F111F1B605D019ULL, 0x923F82A4AF194F9BULL, 0xAB1C5ED5DA6D8118ULL, - 0xD807AA98A3030242ULL, 0x12835B0145706FBEULL, 0x243185BE4EE4B28CULL, 0x550C7DC3D5FFB4E2ULL, - 0x72BE5D74F27B896FULL, 0x80DEB1FE3B1696B1ULL, 0x9BDC06A725C71235ULL, 0xC19BF174CF692694ULL, - 0xE49B69C19EF14AD2ULL, 0xEFBE4786384F25E3ULL, 0x0FC19DC68B8CD5B5ULL, 0x240CA1CC77AC9C65ULL, - 0x2DE92C6F592B0275ULL, 0x4A7484AA6EA6E483ULL, 0x5CB0A9DCBD41FBD4ULL, 0x76F988DA831153B5ULL, - 0x983E5152EE66DFABULL, 0xA831C66D2DB43210ULL, 0xB00327C898FB213FULL, 0xBF597FC7BEEF0EE4ULL, - 0xC6E00BF33DA88FC2ULL, 0xD5A79147930AA725ULL, 0x06CA6351E003826FULL, 0x142929670A0E6E70ULL, - 0x27B70A8546D22FFCULL, 0x2E1B21385C26C926ULL, 0x4D2C6DFC5AC42AEDULL, 0x53380D139D95B3DFULL, - 0x650A73548BAF63DEULL, 0x766A0ABB3C77B2A8ULL, 0x81C2C92E47EDAEE6ULL, 0x92722C851482353BULL, - 0xA2BFE8A14CF10364ULL, 0xA81A664BBC423001ULL, 0xC24B8B70D0F89791ULL, 0xC76C51A30654BE30ULL, - 0xD192E819D6EF5218ULL, 0xD69906245565A910ULL, 0xF40E35855771202AULL, 0x106AA07032BBD1B8ULL, - 0x19A4C116B8D2D0C8ULL, 0x1E376C085141AB53ULL, 0x2748774CDF8EEB99ULL, 0x34B0BCB5E19B48A8ULL, - 0x391C0CB3C5C95A63ULL, 0x4ED8AA4AE3418ACBULL, 0x5B9CCA4F7763E373ULL, 0x682E6FF3D6B2B8A3ULL, - 0x748F82EE5DEFB2FCULL, 0x78A5636F43172F60ULL, 0x84C87814A1F0AB72ULL, 0x8CC702081A6439ECULL, - 0x90BEFFFA23631E28ULL, 0xA4506CEBDE82BDE9ULL, 0xBEF9A3F7B2C67915ULL, 0xC67178F2E372532BULL, - 0xCA273ECEEA26619CULL, 0xD186B8C721C0C207ULL, 0xEADA7DD6CDE0EB1EULL, 0xF57D4F7FEE6ED178ULL, - 0x06F067AA72176FBAULL, 0x0A637DC5A2C898A6ULL, 0x113F9804BEF90DAEULL, 0x1B710B35131C471BULL, - 0x28DB77F523047D84ULL, 0x32CAAB7B40C72493ULL, 0x3C9EBE0A15C9BEBCULL, 0x431D67C49C100D4CULL, - 0x4CC5D4BECB3E42B6ULL, 0x597F299CFC657E2AULL, 0x5FCB6FAB3AD6FAECULL, 0x6C44198C4A475817ULL -}; - - -/** - * Initial state for SHA224 - */ -static const uint32_t H_224[] = { - 0xC1059ED8UL, 0x367CD507UL, 0x3070DD17UL, 0xF70E5939UL, - 0xFFC00B31UL, 0x68581511UL, 0x64F98FA7UL, 0xBEFA4FA4UL -}; - -/** - * Initial state for SHA256 - */ -static const uint32_t H_256[] = { - 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL, - 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL -}; - -/** - * Initial state for SHA384 - */ -static const uint64_t H_384[] = { - 0xCBBB9D5DC1059ED8ULL, 0x629A292A367CD507ULL, 0x9159015A3070DD17ULL, 0x152FECD8F70E5939ULL, - 0x67332667FFC00B31ULL, 0x8EB44A8768581511ULL, 0xDB0C2E0D64F98FA7ULL, 0x47B5481DBEFA4FA4ULL -}; - -/** - * Initial state for SHA512 - */ -static const uint64_t H_512[] = { - 0x6A09E667F3BCC908ULL, 0xBB67AE8584CAA73BULL, 0x3C6EF372FE94F82BULL, 0xA54FF53A5F1D36F1ULL, - 0x510E527FADE682D1ULL, 0x9B05688C2B3E6C1FULL, 0x1F83D9ABFB41BD6BULL, 0x5BE0CD19137E2179ULL -}; - -/** - * Initial state for SHA512/224 - */ -static const uint64_t H_512_224[] = { - 0x8C3D37C819544DA2ULL, 0x73E1996689DCD4D6ULL, 0x1DFAB7AE32FF9C82ULL, 0x679DD514582F9FCFULL, - 0x0F6D2B697BD44DA8ULL, 0x77E36F7304C48942ULL, 0x3F9D85A86A1D36C8ULL, 0x1112E6AD91D692A1ULL -}; - -/** - * Initial state for SHA512/256 - */ -static const uint64_t H_512_256[] = { - 0x22312194FC2BF72CULL, 0x9F555FA3C84C64C2ULL, 0x2393B86B6F53B151ULL, 0x963877195940EABDULL, - 0x96283EE2A88EFFE3ULL, 0xBE5E1E2553863992ULL, 0x2B0199FC2C85B8AAULL, 0x0EB72DDC81C52CA2ULL -}; - - -/** - * Initialise a state - * - * @parma state The state that should be initialised - * @parma algorithm The hashing algorithm - * @return Zero on success, -1 on error - */ -int -libsha2_state_initialise(struct libsha2_state *restrict state, enum libsha2_algorithm algorithm) -{ - size_t i; - - memset(state, 0, sizeof(*state)); - state->message_size = 0; - state->algorithm = algorithm; - - /* Set initial hash values. */ - switch (algorithm) { - case LIBSHA2_224: memcpy(state->h.b32, H_224, sizeof(H_224)); break; - case LIBSHA2_256: memcpy(state->h.b32, H_256, sizeof(H_256)); break; - case LIBSHA2_384: memcpy(state->h.b64, H_384, sizeof(H_384)); break; - case LIBSHA2_512: memcpy(state->h.b64, H_512, sizeof(H_512)); break; - case LIBSHA2_512_224: memcpy(state->h.b64, H_512_224, sizeof(H_512_224)); break; - case LIBSHA2_512_256: memcpy(state->h.b64, H_512_256, sizeof(H_512_256)); break; - default: - return errno = EINVAL, -1; - } - - /* Set round constants, and chunk size. */ - switch (algorithm) { - case LIBSHA2_224: - case LIBSHA2_256: - for (i = 0; i < 64; i++) - state->k.b32[i] = (uint32_t)(ROUND_CONSTANTS[i] >> 32); - state->chunk_size = 64; - break; - - default: - memcpy(state->k.b64, ROUND_CONSTANTS, sizeof(ROUND_CONSTANTS)); - state->chunk_size = 128; - break; - } - - return 0; -} diff --git a/sum_fd.c b/sum_fd.c index 381a634..35d4121 100644 --- a/sum_fd.c +++ b/sum_fd.c @@ -20,7 +20,7 @@ libsha2_sum_fd(int fd, enum libsha2_algorithm algorithm, char *restrict hashsum) size_t blksize = 4096; char *restrict chunk; - if (libsha2_state_initialise(&state, algorithm) < 0) + if (libsha2_init(&state, algorithm) < 0) return -1; if (fstat(fd, &attr) == 0 && attr.st_blksize > 0) diff --git a/unhex.c b/unhex.c index 0dc231d..a84ac19 100644 --- a/unhex.c +++ b/unhex.c @@ -14,9 +14,10 @@ void libsha2_unhex(char *restrict output, const char *restrict hashsum) { size_t n = strlen(hashsum) / 2; + char a, b; while (n--) { - char a = hashsum[2 * n + 0]; - char b = hashsum[2 * n + 1]; + a = hashsum[2 * n + 0]; + b = hashsum[2 * n + 1]; a = (char)((a & 15) + (a > '9' ? 9 : 0)); b = (char)((b & 15) + (b > '9' ? 9 : 0)); -- cgit v1.2.3-70-g09d2