diff options
author | Mattias Andrée <maandree@kth.se> | 2019-02-09 18:20:57 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2019-02-09 18:20:57 +0100 |
commit | 242430d5e52cf8ee49dcd1701cf134e7454910c6 (patch) | |
tree | e3238ce604ffb0d80a4e122cd40d5807372f187f /src | |
parent | Merge pull request #2 from maaku/no-trailing-comma (diff) | |
download | libsha2-242430d5e52cf8ee49dcd1701cf134e7454910c6.tar.gz libsha2-242430d5e52cf8ee49dcd1701cf134e7454910c6.tar.bz2 libsha2-242430d5e52cf8ee49dcd1701cf134e7454910c6.tar.xz |
Change license to ISC and reorganise
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src')
-rw-r--r-- | src/libsha2.h | 30 | ||||
-rw-r--r-- | src/libsha2/digest.c | 217 | ||||
-rw-r--r-- | src/libsha2/digest.h | 50 | ||||
-rw-r--r-- | src/libsha2/files.c | 72 | ||||
-rw-r--r-- | src/libsha2/files.h | 40 | ||||
-rw-r--r-- | src/libsha2/hex.c | 81 | ||||
-rw-r--r-- | src/libsha2/hex.h | 62 | ||||
-rw-r--r-- | src/libsha2/mac/hmac.c | 21 | ||||
-rw-r--r-- | src/libsha2/mac/hmac.h | 25 | ||||
-rw-r--r-- | src/libsha2/state.c | 151 | ||||
-rw-r--r-- | src/libsha2/state.h | 221 |
11 files changed, 0 insertions, 970 deletions
diff --git a/src/libsha2.h b/src/libsha2.h deleted file mode 100644 index d6ebd2b..0000000 --- a/src/libsha2.h +++ /dev/null @@ -1,30 +0,0 @@ -/** - * libsha2 – SHA-2-family hashing library - * - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef LIBSHA2_H -#define LIBSHA2_H 1 - - -#include "libsha2/digest.h" -#include "libsha2/state.h" -#include "libsha2/hex.h" -#include "libsha2/files.h" - - -#endif - diff --git a/src/libsha2/digest.c b/src/libsha2/digest.c deleted file mode 100644 index 6b4765e..0000000 --- a/src/libsha2/digest.c +++ /dev/null @@ -1,217 +0,0 @@ -/** - * libsha2 – SHA-2-family hashing library - * - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ -#include "digest.h" -#include <alloca.h> -#include <string.h> - - - -/** - * Unified implementation (what can unified without performance impact) - * of the chunk processing for all SHA-2 functions - * - * @param A Wordsize-dependent constant, take a look at the code - * @param B Wordsize-dependent constant, take a look at the code - * @param C Wordsize-dependent constant, take a look at the code - * @param D Wordsize-dependent constant, take a look at the code - * @param E Wordsize-dependent constant, take a look at the code - * @param F Wordsize-dependent constant, take a look at the code - * @param G Wordsize-dependent constant, take a look at the code - * @param H Wordsize-dependent constant, take a look at the code - * @param I Wordsize-dependent constant, take a look at the code - * @param J Wordsize-dependent constant, take a look at the code - * @param K Wordsize-dependent constant, take a look at the code - * @param L Wordsize-dependent constant, take a look at the code - * @param WORD_T `__typeof()` on any wordsize-dependent variable, with exact size - * @param k Round constants - * @param w Words - * @param h Hash values - * @param work_h Space for temporary hash values - */ -#define SHA2_IMPLEMENTATION(A, B, C, D, E, F, G, H, I, J, K, L, WORD_T, k, w, h, work_h) \ - memcpy(work_h, h, sizeof(work_h)); \ - \ - memset(w, 0, 16 * sizeof(*(w))); \ - for (i = 0; i < 16; i++) \ - for (j = 0; j < sizeof(WORD_T); j++) \ - w[i] |= ((WORD_T)(state->chunk[(i + 1) * sizeof(WORD_T) - j - 1])) << (j << 3); \ - \ - for (i = 16; i < sizeof(k) / sizeof(*(k)); i++) \ - { \ - w[i] = w[i - 16] + w[i - 7]; \ - w[i] += ROTR(w[i - 15], A) ^ ROTR(w[i - 15], B) ^ (w[i - 15] >> (C)); \ - w[i] += ROTR(w[i - 2], D) ^ ROTR(w[i - 2], E) ^ (w[i - 2] >> (F)); \ - } \ - \ - for (i = 0; i < sizeof(k) / sizeof(*(k)); i++) \ - { \ - s1 = (work_h[4] & work_h[5]) ^ (work_h[6] & ~(work_h[4])); \ - s1 += work_h[7] + k[i] + w[i]; \ - s0 = (work_h[0] & work_h[1]) ^ (work_h[0] & work_h[2]) ^ (work_h[1] & work_h[2]); \ - s1 += ROTR(work_h[4], G) ^ ROTR(work_h[4], H) ^ ROTR(work_h[4], I); \ - s0 += ROTR(work_h[0], J) ^ ROTR(work_h[0], K) ^ ROTR(work_h[0], L); \ - \ - memmove(work_h + 1, work_h, 7 * sizeof(*(work_h))); \ - work_h[4] += s1; \ - work_h[0] = s1 + s0; \ - } \ - \ - for (i = 0; i < 8; i++) \ - h[i] += work_h[i] - - - -/** - * Process a chunk using SHA-256 - * - * @param state The hashing state - */ -static __attribute__((nonnull, nothrow)) -void process256(libsha2_state_t* restrict state) -{ - uint32_t s0, s1; - size_t i, j; -#define ROTR(X, N) (((X) >> (N)) | ((X) << ((sizeof(uint32_t) * 8) - (N)))) - SHA2_IMPLEMENTATION(7, 18, 3, 17, 19, 10, 6, 11, 25, 2, 13, 22, uint32_t, - state->k.b32, state->w.b32, state->h.b32, state->work_h.b32); -#undef ROTR -} - - -/** - * Process a chunk using SHA-512 - * - * @param state The hashing state - */ -static __attribute__((nonnull, nothrow)) -void process512(libsha2_state_t* restrict state) -{ - uint64_t s0, s1; - size_t i, j; -#define ROTR(X, N) (((X) >> (N)) | ((X) << ((sizeof(uint64_t) * 8) - (N)))) - SHA2_IMPLEMENTATION(1, 8, 7, 19, 61, 6, 14, 18, 41, 28, 34, 39, uint64_t, - state->k.b64, state->w.b64, state->h.b64, state->work_h.b64); -#undef ROTR -} - - -/** - * Absorb more of the message - * - * @param state The hashing state - * @param message The message, in bits, must be equivalent to 0 modulus 8 - * @param msglen The length of the message - */ -void libsha2_update(libsha2_state_t* restrict state, const char* restrict message, size_t msglen) -{ - size_t n, off, mlen; - - msglen /= 8; - mlen = state->message_size / 8; - - while (msglen) - { - off = mlen % state->chunk_size; - n = state->chunk_size - off; - n = n < msglen ? n : msglen; - memcpy(state->chunk + off, message, n); - if (off + n == state->chunk_size) - switch (state->algorithm) - { - case LIBSHA2_224: - case LIBSHA2_256: - process256(state); - break; - - default: - process512(state); - break; - } - message += n, mlen += n, msglen -= n; - } - - state->message_size = mlen * 8; -} - - -/** - * Absorb the last part of the message and output a hash - * - * @param state The hashing state - * @param message The message, in bits - * @param msglen The length of the message, zero if there is nothing more to absorb - * @param output The output buffer for the hash - */ -void libsha2_digest(libsha2_state_t* restrict state, const char* restrict message, size_t msglen, char* output) -{ - char* appendix; - size_t i, j, k, n; - - if (msglen & ~7) - { - libsha2_update(state, message, msglen & ~7); - message += msglen & ~7; - msglen &= 7; - } - - k = 8 * state->chunk_size; - n = state->chunk_size + 8; - n = (k + (n % k)) % k; - n = n / 8 - 1; - - appendix = state->appendix; - if (msglen) - { - j = 7 - msglen; - *appendix = *message; - *appendix |= 1 << j; - *appendix &= ~((1 << j) - 1); - } - else - *appendix = (unsigned char)128; - - k = state->message_size + msglen; - i = state->chunk_size / 8; - appendix += n + i - 1; - for (i = i < sizeof(size_t) ? i : sizeof(size_t); i--;) - *(appendix - i) = (unsigned char)((k >> (i * 8)) & 255); - - n += state->chunk_size; - libsha2_update(state, state->appendix, n); - - n = libsha2_algorithm_output_size(state->algorithm); - switch (state->algorithm) - { - case LIBSHA2_224: - case LIBSHA2_256: - for (i = 0; i < 8; i++) - for (j = 0; j < (state->chunk_size / 16); j++) - if (k = (i + 1) * (state->chunk_size / 16) - j - 1, k < n) - output[k] = (char)((state->h.b32[i] >> (8 * j)) & 255); - break; - - default: - for (i = 0; i < 8; i++) - for (j = 0; j < (state->chunk_size / 16); j++) - if (k = (i + 1) * (state->chunk_size / 16) - j - 1, k < n) - output[k] = (char)((state->h.b64[i] >> (8 * j)) & 255); - break; - } -} - diff --git a/src/libsha2/digest.h b/src/libsha2/digest.h deleted file mode 100644 index be81f90..0000000 --- a/src/libsha2/digest.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * libsha2 – SHA-2-family hashing library - * - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef LIBSHA2_DIGEST_H -#define LIBSHA2_DIGEST_H 1 - - -#include "state.h" - - -/** - * Absorb more of the message - * - * @param state The hashing state - * @param message The message, in bits, must be equivalent to 0 modulus 8 - * @param msglen The length of the message - */ -__attribute__((nonnull, nothrow)) -void libsha2_update(libsha2_state_t* restrict state, const char* restrict message, size_t msglen); - -/** - * Absorb the last part of the message and output a hash - * - * @param state The hashing state - * @param message The message, in bits - * @param msglen The length of the message, zero if there is nothing more to absorb - * @param output The output buffer for the hash - */ -__attribute__((nonnull(1, 4), nothrow)) -void libsha2_digest(libsha2_state_t* restrict state, const char* restrict message, size_t msglen, char* output); - - - -#endif - diff --git a/src/libsha2/files.c b/src/libsha2/files.c deleted file mode 100644 index 903c592..0000000 --- a/src/libsha2/files.c +++ /dev/null @@ -1,72 +0,0 @@ -/** - * libsha2 – SHA-2-family hashing library - * - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ -#include "files.h" -#include "digest.h" -#include <stddef.h> -#include <unistd.h> -#include <sys/stat.h> -#include <alloca.h> -#include <errno.h> - - - -/** - * Calculate the checksum for a file, - * the content of the file is assumed non-sensitive - * - * @param fd The file descriptor of the file - * @param algorithm The hashing algorithm - * @param hashsum Output buffer for the hash - * @return Zero on success, -1 on error - */ -int libsha2_sum_fd(int fd, libsha2_algorithm_t algorithm, char* restrict hashsum) -{ - libsha2_state_t state; - ssize_t got; - struct stat attr; - size_t blksize = 4096; - char* restrict chunk; - - if (libsha2_state_initialise(&state, algorithm) < 0) - return -1; - - if (fstat(fd, &attr) == 0) - if (attr.st_blksize > 0) - blksize = (size_t)(attr.st_blksize); - - chunk = alloca(blksize); - - for (;;) - { - got = read(fd, chunk, blksize); - if (got < 0) - { - if (errno == EINTR) - continue; - return -1; - } - if (got == 0) - break; - libsha2_update(&state, chunk, (size_t)got); - } - - libsha2_digest(&state, NULL, 0, hashsum); - return 0; -} - diff --git a/src/libsha2/files.h b/src/libsha2/files.h deleted file mode 100644 index 2854cd7..0000000 --- a/src/libsha2/files.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * libsha2 – SHA-2-family hashing library - * - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef LIBSHA2_FILES_H -#define LIBSHA2_FILES_H 1 - - -#include "state.h" - - -/** - * Calculate the checksum for a file, - * the content of the file is assumed non-sensitive - * - * @param fd The file descriptor of the file - * @param algorithm The hashing algorithm - * @param hashsum Output buffer for the hash - * @return Zero on success, -1 on error - */ -__attribute__((nonnull, leaf)) -int libsha2_sum_fd(int fd, libsha2_algorithm_t algorithm, char* restrict hashsum); - - -#endif - diff --git a/src/libsha2/hex.c b/src/libsha2/hex.c deleted file mode 100644 index 1f5c372..0000000 --- a/src/libsha2/hex.c +++ /dev/null @@ -1,81 +0,0 @@ -/** - * libsha2 – SHA-2-family hashing library - * - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ -#include "hex.h" -#include <string.h> - - - -/** - * Convert a binary hashsum to lower case hexadecimal representation - * - * @param output Output array, should have an allocation size of at least `2 * n + 1` - * @param hashsum The hashsum to convert - * @param n The size of `hashsum` - */ -void libsha2_behex_lower(char* restrict output, const char* restrict hashsum, size_t n) -{ - output[2 * n] = '\0'; - while (n--) - { - output[2 * n + 0] = "0123456789abcdef"[(hashsum[n] >> 4) & 15]; - output[2 * n + 1] = "0123456789abcdef"[(hashsum[n] >> 0) & 15]; - } -} - - -/** - * Convert a binary hashsum to upper case hexadecimal representation - * - * @param output Output array, should have an allocation size of at least `2 * n + 1` - * @param hashsum The hashsum to convert - * @param n The size of `hashsum` - */ -void libsha2_behex_upper(char* restrict output, const char* restrict hashsum, size_t n) -{ - output[2 * n] = '\0'; - while (n--) - { - output[2 * n + 0] = "0123456789ABCDEF"[(hashsum[n] >> 4) & 15]; - output[2 * n + 1] = "0123456789ABCDEF"[(hashsum[n] >> 0) & 15]; - } -} - - -/** - * Convert a hexadecimal hashsum (both lower case, upper - * case and mixed is supported) to binary representation - * - * @param output Output array, should have an allocation size of at least `strlen(hashsum) / 2` - * @param hashsum The hashsum to convert - */ -void libsha2_unhex(char* restrict output, const char* restrict hashsum) -{ - size_t n = strlen(hashsum) / 2; - while (n--) - { - char a = hashsum[2 * n + 0]; - char b = hashsum[2 * n + 1]; - - a = (char)((a & 15) + (a > '9' ? 9 : 0)); - b = (char)((b & 15) + (b > '9' ? 9 : 0)); - - output[n] = (char)((a << 4) | b); - } -} - diff --git a/src/libsha2/hex.h b/src/libsha2/hex.h deleted file mode 100644 index d9581b0..0000000 --- a/src/libsha2/hex.h +++ /dev/null @@ -1,62 +0,0 @@ -/** - * libsha2 – SHA-2-family hashing library - * - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef LIBSHA2_HEX_H -#define LIBSHA2_HEX_H 1 - - -#include <stddef.h> - - - -/** - * Convert a binary hashsum to lower case hexadecimal representation - * - * @param output Output array, should have an allocation size of at least `2 * n + 1` - * @param hashsum The hashsum to convert - * @param n The size of `hashsum` - */ -__attribute__((leaf, nonnull, nothrow)) -void libsha2_behex_lower(char* restrict output, const char* restrict hashsum, size_t n); - - -/** - * Convert a binary hashsum to upper case hexadecimal representation - * - * @param output Output array, should have an allocation size of at least `2 * n + 1` - * @param hashsum The hashsum to convert - * @param n The size of `hashsum` - */ -__attribute__((leaf, nonnull, nothrow)) -void libsha2_behex_upper(char* restrict output, const char* restrict hashsum, size_t n); - - -/** - * Convert a hexadecimal hashsum (both lower case, upper - * case and mixed is supported) to binary representation - * - * @param output Output array, should have an allocation size of at least `strlen(hashsum) / 2` - * @param hashsum The hashsum to convert - */ -__attribute__((leaf, nonnull, nothrow)) -void libsha2_unhex(char* restrict output, const char* restrict hashsum); - - - -#endif - diff --git a/src/libsha2/mac/hmac.c b/src/libsha2/mac/hmac.c deleted file mode 100644 index 2ee883d..0000000 --- a/src/libsha2/mac/hmac.c +++ /dev/null @@ -1,21 +0,0 @@ -/** - * libsha2 – SHA-2-family hashing library - * - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ -#include "hmac.h" - - diff --git a/src/libsha2/mac/hmac.h b/src/libsha2/mac/hmac.h deleted file mode 100644 index 745d610..0000000 --- a/src/libsha2/mac/hmac.h +++ /dev/null @@ -1,25 +0,0 @@ -/** - * libsha2 – SHA-2-family hashing library - * - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef LIBSHA2_MAC_HMAC_H -#define LIBSHA2_MAC_HMAC_H 1 - - - -#endif - diff --git a/src/libsha2/state.c b/src/libsha2/state.c deleted file mode 100644 index fb6b4fc..0000000 --- a/src/libsha2/state.c +++ /dev/null @@ -1,151 +0,0 @@ -/** - * libsha2 – SHA-2-family hashing library - * - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ -#include "state.h" -#include <string.h> -#include <errno.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}; - - - -/** - * 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(libsha2_state_t* restrict state, libsha2_algorithm_t algorithm) -{ - static const uint32_t H_224[] = { - 0xC1059ED8UL, 0x367CD507UL, 0x3070DD17UL, 0xF70E5939UL, - 0xFFC00B31UL, 0x68581511UL, 0x64F98FA7UL, 0xBEFA4FA4UL}; - static const uint32_t H_256[] = { - 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL, - 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL}; - static const uint64_t H_384[] = { - 0xCBBB9D5DC1059ED8ULL, 0x629A292A367CD507ULL, 0x9159015A3070DD17ULL, 0x152FECD8F70E5939ULL, - 0x67332667FFC00B31ULL, 0x8EB44A8768581511ULL, 0xDB0C2E0D64F98FA7ULL, 0x47B5481DBEFA4FA4ULL}; - static const uint64_t H_512[] = { - 0x6A09E667F3BCC908ULL, 0xBB67AE8584CAA73BULL, 0x3C6EF372FE94F82BULL, 0xA54FF53A5F1D36F1ULL, - 0x510E527FADE682D1ULL, 0x9B05688C2B3E6C1FULL, 0x1F83D9ABFB41BD6BULL, 0x5BE0CD19137E2179ULL}; - static const uint64_t H_512_224[] = { - 0x8C3D37C819544DA2ULL, 0x73E1996689DCD4D6ULL, 0x1DFAB7AE32FF9C82ULL, 0x679DD514582F9FCFULL, - 0x0F6D2B697BD44DA8ULL, 0x77E36F7304C48942ULL, 0x3F9D85A86A1D36C8ULL, 0x1112E6AD91D692A1ULL}; - static const uint64_t H_512_256[] = { - 0x22312194FC2BF72CULL, 0x9F555FA3C84C64C2ULL, 0x2393B86B6F53B151ULL, 0x963877195940EABDULL, - 0x96283EE2A88EFFE3ULL, 0xBE5E1E2553863992ULL, 0x2B0199FC2C85B8AAULL, 0x0EB72DDC81C52CA2ULL}; - - 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; -} - - -/** - * Get the output size of the algorithm specified for a state - * - * @parma state The state - * @return The number of bytes in the output, zero on error - */ -size_t libsha2_state_output_size(const libsha2_state_t* restrict state) -{ - return libsha2_algorithm_output_size(state->algorithm); -} - - -/** - * Get the output size of an algorithm - * - * @parma algorithm The hashing algorithm - * @return The number of bytes in the output, zero on error - */ -size_t libsha2_algorithm_output_size(libsha2_algorithm_t algorithm) -{ - switch (algorithm) - { - case LIBSHA2_224: return 28; - case LIBSHA2_256: return 32; - case LIBSHA2_384: return 48; - case LIBSHA2_512: return 64; - case LIBSHA2_512_224: return 28; - case LIBSHA2_512_256: return 32; - default: - return errno = EINVAL, 0; - } -} - diff --git a/src/libsha2/state.h b/src/libsha2/state.h deleted file mode 100644 index f081a64..0000000 --- a/src/libsha2/state.h +++ /dev/null @@ -1,221 +0,0 @@ -/** - * libsha2 – SHA-2-family hashing library - * - * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this library. If not, see <http://www.gnu.org/licenses/>. - */ -#ifndef LIBSHA2_STATE_H -#define LIBSHA2_STATE_H 1 - - -#include <stdint.h> -#include <stddef.h> - - - -/** - * Algorithms supported by libsha2 - */ -typedef enum libsha2_algorithm - { - /** - * SHA-224, outputs 28 bytes - */ - LIBSHA2_224 = 1, - - /** - * SHA-256, outputs 32 bytes - */ - LIBSHA2_256 = 2, - - /** - * SHA-384, outputs 48 bytes - */ - LIBSHA2_384 = 3, - - /** - * SHA-512, outputs 64 bytes - */ - LIBSHA2_512 = 4, - - /** - * SHA-512/224, outputs 28 bytes - */ - LIBSHA2_512_224 = 5, - - /** - * SHA-512/256, outputs 32 bytes - */ - LIBSHA2_512_256 = 6 - - } libsha2_algorithm_t; - - -/** - * Datastructure that describes the state of a hashing process - * - * Data that could just as well be allocated (with `auto`) are - * allocated here so that is is easier to wipe the data without - * exposing two versions of each function: one to wipe data, - * and one not to wipe data to gain speed, now you can use use - * `explicit_bzero` (or `memset`) when you are done. - * - * This datastructure is flat (it contains dynamic pointers) - * and can be marshalled and unmarshalled naïvely, and does - * not need destroyed; however, if you when to marshall it - * using as little memory as possible, this are comments - * about data that does not need to be mashalled - */ -typedef struct libsha2_state -{ - /** - * The size of the message, as far as processed, in bits; - */ - size_t message_size; - - /** - * Round constants - */ - union - { - /** - * For 32-bit algorithms - */ - uint32_t b32[64]; - - /** - * For 64-bit algorithms - */ - uint64_t b64[80]; - - } k; - - /** - * Words - * - * Does not need to be marshalled - */ - union - { - /** - * For 32-bit algorithms - */ - uint32_t b32[64]; - - /** - * For 64-bit algorithms - */ - uint64_t b64[80]; - - } w; - - /** - * Hashing values - */ - union - { - /** - * For 32-bit algorithms - */ - uint32_t b32[8]; - - /** - * For 64-bit algorithms - */ - uint64_t b64[8]; - - } h; - - /** - * Temporary hashing values - * - * Does not need to be marshalled - */ - union - { - /** - * For 32-bit algorithms - */ - uint32_t b32[8]; - - /** - * For 64-bit algorithms - */ - uint64_t b64[8]; - - } work_h; - - /** - * Space for chunks to process, limited - * to 64 bytes on 32-bit algorithms - */ - unsigned char chunk[128]; - - /** - * Space for storing the last bits and - * the padding - * - * Does not need to be marshalled - */ - char appendix[256]; - - /** - * The size of the chunks, in bytes - */ - size_t chunk_size; - - /** - * The algorithm that is used - */ - libsha2_algorithm_t algorithm; - - int __padding1; - -} libsha2_state_t; - - - -/** - * Initialise a state - * - * @parma state The state that should be initialised - * @parma algorithm The hashing algorithm - * @return Zero on success, -1 on error - */ -__attribute__((leaf, nothrow, nonnull)) -int libsha2_state_initialise(libsha2_state_t* restrict state, libsha2_algorithm_t algorithm); - -/** - * Get the output size of the algorithm specified for a state - * - * @parma state The state - * @return The number of bytes in the output, zero on error - */ -__attribute__((nothrow, nonnull, pure)) -size_t libsha2_state_output_size(const libsha2_state_t* restrict state); - -/** - * Get the output size of an algorithm - * - * @parma algorithm The hashing algorithm - * @return The number of bytes in the output, zero on error - */ -__attribute__((leaf, nothrow, const)) -size_t libsha2_algorithm_output_size(libsha2_algorithm_t algorithm); - - - -#endif - |