| 1 |
/* |
| 2 |
This file is part of libmicrohttpd |
| 3 |
Copyright (C) 2019-2021 Karlson2k (Evgeny Grin) |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Lesser General Public |
| 7 |
License as published by the Free Software Foundation; either |
| 8 |
version 2.1 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public |
| 16 |
License along with this library. |
| 17 |
If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
/** |
| 21 |
* @file microhttpd/sha1.h |
| 22 |
* @brief Calculation of SHA-1 digest |
| 23 |
* @author Karlson2k (Evgeny Grin) |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef MHD_SHA1_H |
| 27 |
#define MHD_SHA1_H 1 |
| 28 |
|
| 29 |
#include "mhd_options.h" |
| 30 |
#include <stdint.h> |
| 31 |
#ifdef HAVE_STDDEF_H |
| 32 |
#include <stddef.h> /* for size_t */ |
| 33 |
#endif /* HAVE_STDDEF_H */ |
| 34 |
|
| 35 |
/** |
| 36 |
* SHA-1 digest is kept internally as 5 32-bit words. |
| 37 |
*/ |
| 38 |
#define _SHA1_DIGEST_LENGTH 5 |
| 39 |
|
| 40 |
/** |
| 41 |
* Number of bits in single SHA-1 word |
| 42 |
*/ |
| 43 |
#define SHA1_WORD_SIZE_BITS 32 |
| 44 |
|
| 45 |
/** |
| 46 |
* Number of bytes in single SHA-1 word |
| 47 |
*/ |
| 48 |
#define SHA1_BYTES_IN_WORD (SHA1_WORD_SIZE_BITS / 8) |
| 49 |
|
| 50 |
/** |
| 51 |
* Size of SHA-1 digest in bytes |
| 52 |
*/ |
| 53 |
#define SHA1_DIGEST_SIZE (_SHA1_DIGEST_LENGTH * SHA1_BYTES_IN_WORD) |
| 54 |
|
| 55 |
/** |
| 56 |
* Size of SHA-1 digest string in chars including termination NUL |
| 57 |
*/ |
| 58 |
#define SHA1_DIGEST_STRING_SIZE ((SHA1_DIGEST_SIZE) * 2 + 1) |
| 59 |
|
| 60 |
/** |
| 61 |
* Size of single processing block in bits |
| 62 |
*/ |
| 63 |
#define SHA1_BLOCK_SIZE_BITS 512 |
| 64 |
|
| 65 |
/** |
| 66 |
* Size of single processing block in bytes |
| 67 |
*/ |
| 68 |
#define SHA1_BLOCK_SIZE (SHA1_BLOCK_SIZE_BITS / 8) |
| 69 |
|
| 70 |
|
| 71 |
struct sha1_ctx |
| 72 |
{ |
| 73 |
uint32_t H[_SHA1_DIGEST_LENGTH]; /**< Intermediate hash value / digest at end of calculation */ |
| 74 |
uint8_t buffer[SHA1_BLOCK_SIZE]; /**< SHA256 input data buffer */ |
| 75 |
uint64_t count; /**< number of bytes, mod 2^64 */ |
| 76 |
}; |
| 77 |
|
| 78 |
/** |
| 79 |
* Initialise structure for SHA-1 calculation. |
| 80 |
* |
| 81 |
* @param ctx_ must be a `struct sha1_ctx *` |
| 82 |
*/ |
| 83 |
void |
| 84 |
MHD_SHA1_init (void *ctx_); |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Process portion of bytes. |
| 89 |
* |
| 90 |
* @param ctx_ must be a `struct sha1_ctx *` |
| 91 |
* @param data bytes to add to hash |
| 92 |
* @param length number of bytes in @a data |
| 93 |
*/ |
| 94 |
void |
| 95 |
MHD_SHA1_update (void *ctx_, |
| 96 |
const uint8_t *data, |
| 97 |
size_t length); |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Finalise SHA-1 calculation, return digest. |
| 102 |
* |
| 103 |
* @param ctx_ must be a `struct sha1_ctx *` |
| 104 |
* @param[out] digest set to the hash, must be #SHA1_DIGEST_SIZE bytes |
| 105 |
*/ |
| 106 |
void |
| 107 |
MHD_SHA1_finish (void *ctx_, |
| 108 |
uint8_t digest[SHA1_DIGEST_SIZE]); |
| 109 |
|
| 110 |
#endif /* MHD_SHA1_H */ |