libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

sha256.h (3140B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2019-2022 Evgeny Grin (Karlson2k)
      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/sha256.h
     22  * @brief  Calculation of SHA-256 digest
     23  * @author Karlson2k (Evgeny Grin)
     24  */
     25 
     26 #ifndef MHD_SHA256_H
     27 #define MHD_SHA256_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 /**
     37  *  Digest is kept internally as 8 32-bit words.
     38  */
     39 #define SHA256_DIGEST_SIZE_WORDS 8
     40 
     41 /**
     42  * Number of bits in single SHA-256 word
     43  */
     44 #define SHA256_WORD_SIZE_BITS 32
     45 
     46 /**
     47  * Number of bytes in single SHA-256 word
     48  * used to process data
     49  */
     50 #define SHA256_BYTES_IN_WORD (SHA256_WORD_SIZE_BITS / 8)
     51 
     52 /**
     53  * Size of SHA-256 digest in bytes
     54  */
     55 #define SHA256_DIGEST_SIZE (SHA256_DIGEST_SIZE_WORDS * SHA256_BYTES_IN_WORD)
     56 
     57 /**
     58  * Size of SHA-256 digest string in chars including termination NUL
     59  */
     60 #define SHA256_DIGEST_STRING_SIZE ((SHA256_DIGEST_SIZE) * 2 + 1)
     61 
     62 /**
     63  * Size of single processing block in bits
     64  */
     65 #define SHA256_BLOCK_SIZE_BITS 512
     66 
     67 /**
     68  * Size of single processing block in bytes
     69  */
     70 #define SHA256_BLOCK_SIZE (SHA256_BLOCK_SIZE_BITS / 8)
     71 
     72 /**
     73  * Size of single processing block in bytes
     74  */
     75 #define SHA256_BLOCK_SIZE_WORDS (SHA256_BLOCK_SIZE_BITS / SHA256_WORD_SIZE_BITS)
     76 
     77 
     78 struct Sha256Ctx
     79 {
     80   uint32_t H[SHA256_DIGEST_SIZE_WORDS];     /**< Intermediate hash value / digest at end of calculation */
     81   uint32_t buffer[SHA256_BLOCK_SIZE_WORDS]; /**< SHA256 input data buffer */
     82   uint64_t count;                           /**< number of bytes, mod 2^64 */
     83 };
     84 
     85 /**
     86  * Initialise structure for SHA256 calculation.
     87  *
     88  * @param ctx must be a `struct Sha256Ctx *`
     89  */
     90 void
     91 MHD_SHA256_init (struct Sha256Ctx *ctx);
     92 
     93 
     94 /**
     95  * Process portion of bytes.
     96  *
     97  * @param ctx must be a `struct Sha256Ctx *`
     98  * @param data bytes to add to hash
     99  * @param length number of bytes in @a data
    100  */
    101 void
    102 MHD_SHA256_update (struct Sha256Ctx *ctx,
    103                    const uint8_t *data,
    104                    size_t length);
    105 
    106 
    107 /**
    108  * Finalise SHA256 calculation, return digest.
    109  *
    110  * @param ctx must be a `struct Sha256Ctx *`
    111  * @param[out] digest set to the hash, must be #SHA256_DIGEST_SIZE bytes
    112  */
    113 void
    114 MHD_SHA256_finish (struct Sha256Ctx *ctx,
    115                    uint8_t digest[SHA256_DIGEST_SIZE]);
    116 
    117 /**
    118  * Indicates that function MHD_SHA256_finish() (without context reset) is available
    119  */
    120 #define MHD_SHA256_HAS_FINISH 1
    121 
    122 #endif /* MHD_SHA256_H */