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