root/lib/sha512.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


     1 /* Declarations of functions and data types used for SHA512 and SHA384 sum
     2    library functions.
     3    Copyright (C) 2005-2006, 2008-2023 Free Software Foundation, Inc.
     4 
     5    This file is free software: you can redistribute it and/or modify
     6    it under the terms of the GNU Lesser General Public License as
     7    published by the Free Software Foundation; either version 2.1 of the
     8    License, or (at your option) any later version.
     9 
    10    This file 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
    13    GNU Lesser General Public License for more details.
    14 
    15    You should have received a copy of the GNU Lesser General Public License
    16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
    17 
    18 #ifndef SHA512_H
    19 # define SHA512_H 1
    20 
    21 # include <stdio.h>
    22 # include "u64.h"
    23 
    24 # if HAVE_OPENSSL_SHA512
    25 #  ifndef OPENSSL_API_COMPAT
    26 #   define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API.  */
    27 #  endif
    28 #  include <openssl/sha.h>
    29 # endif
    30 
    31 # ifdef __cplusplus
    32 extern "C" {
    33 # endif
    34 
    35 enum { SHA384_DIGEST_SIZE = 384 / 8 };
    36 enum { SHA512_DIGEST_SIZE = 512 / 8 };
    37 
    38 # if HAVE_OPENSSL_SHA512
    39 #  define GL_OPENSSL_NAME 384
    40 #  include "gl_openssl.h"
    41 #  define GL_OPENSSL_NAME 512
    42 #  include "gl_openssl.h"
    43 # else
    44 /* Structure to save state of computation between the single steps.  */
    45 struct sha512_ctx
    46 {
    47   u64 state[8];
    48 
    49   u64 total[2];
    50   size_t buflen;  /* ≥ 0, ≤ 256 */
    51   u64 buffer[32]; /* 256 bytes; the first buflen bytes are in use */
    52 };
    53 
    54 /* Initialize structure containing state of computation. */
    55 extern void sha512_init_ctx (struct sha512_ctx *ctx);
    56 extern void sha384_init_ctx (struct sha512_ctx *ctx);
    57 
    58 /* Starting with the result of former calls of this function (or the
    59    initialization function update the context for the next LEN bytes
    60    starting at BUFFER.
    61    It is necessary that LEN is a multiple of 128!!! */
    62 extern void sha512_process_block (const void *buffer, size_t len,
    63                                   struct sha512_ctx *ctx);
    64 
    65 /* Starting with the result of former calls of this function (or the
    66    initialization function update the context for the next LEN bytes
    67    starting at BUFFER.
    68    It is NOT required that LEN is a multiple of 128.  */
    69 extern void sha512_process_bytes (const void *buffer, size_t len,
    70                                   struct sha512_ctx *ctx);
    71 
    72 /* Process the remaining bytes in the buffer and put result from CTX
    73    in first 64 (48) bytes following RESBUF.  The result is always in little
    74    endian byte order, so that a byte-wise output yields to the wanted
    75    ASCII representation of the message digest.  */
    76 extern void *sha512_finish_ctx (struct sha512_ctx *ctx, void *restrict resbuf);
    77 extern void *sha384_finish_ctx (struct sha512_ctx *ctx, void *restrict resbuf);
    78 
    79 
    80 /* Put result from CTX in first 64 (48) bytes following RESBUF.  The result is
    81    always in little endian byte order, so that a byte-wise output yields
    82    to the wanted ASCII representation of the message digest.
    83 
    84    IMPORTANT: On some systems it is required that RESBUF is correctly
    85    aligned for a 32 bits value.  */
    86 extern void *sha512_read_ctx (const struct sha512_ctx *ctx,
    87                               void *restrict resbuf);
    88 extern void *sha384_read_ctx (const struct sha512_ctx *ctx,
    89                               void *restrict resbuf);
    90 
    91 
    92 /* Compute SHA512 (SHA384) message digest for LEN bytes beginning at BUFFER.
    93    The result is always in little endian byte order, so that a byte-wise
    94    output yields to the wanted ASCII representation of the message
    95    digest.  */
    96 extern void *sha512_buffer (const char *buffer, size_t len,
    97                             void *restrict resblock);
    98 extern void *sha384_buffer (const char *buffer, size_t len,
    99                             void *restrict resblock);
   100 
   101 # endif
   102 
   103 /* Compute SHA512 (SHA384) message digest for bytes read from STREAM.
   104    STREAM is an open file stream.  Regular files are handled more efficiently.
   105    The contents of STREAM from its current position to its end will be read.
   106    The case that the last operation on STREAM was an 'ungetc' is not supported.
   107    The resulting message digest number will be written into the 64 (48) bytes
   108    beginning at RESBLOCK.  */
   109 extern int sha512_stream (FILE *stream, void *resblock);
   110 extern int sha384_stream (FILE *stream, void *resblock);
   111 
   112 
   113 # ifdef __cplusplus
   114 }
   115 # endif
   116 
   117 #endif
   118 
   119 /*
   120  * Hey Emacs!
   121  * Local Variables:
   122  * coding: utf-8
   123  * End:
   124  */

/* [<][>][^][v][top][bottom][index][help] */