Sync from gnulib.
[platform/upstream/coreutils.git] / lib / sha512.h
1 /* Declarations of functions and data types used for SHA512 and SHA384 sum
2    library functions.
3    Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by the
7    Free Software Foundation; either version 2, or (at your option) any
8    later version.
9
10    This program 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 General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #ifndef SHA512_H
20 # define SHA512_H 1
21
22 # include <stdio.h>
23
24 # if HAVE_INTTYPES_H
25 #  include <inttypes.h>
26 # endif
27 # if HAVE_STDINT_H || _LIBC
28 #  include <stdint.h>
29 # endif
30
31 /* Structure to save state of computation between the single steps.  */
32 struct sha512_ctx
33 {
34   uint64_t state[8];
35
36   uint64_t total[2];
37   uint64_t buflen;
38   char buffer[256];
39 };
40
41
42 /* Initialize structure containing state of computation. */
43 extern void sha512_init_ctx (struct sha512_ctx *ctx);
44 extern void sha384_init_ctx (struct sha512_ctx *ctx);
45
46 /* Starting with the result of former calls of this function (or the
47    initialization function update the context for the next LEN bytes
48    starting at BUFFER.
49    It is necessary that LEN is a multiple of 128!!! */
50 extern void sha512_process_block (const void *buffer, size_t len,
51                                   struct sha512_ctx *ctx);
52
53 /* Starting with the result of former calls of this function (or the
54    initialization function update the context for the next LEN bytes
55    starting at BUFFER.
56    It is NOT required that LEN is a multiple of 128.  */
57 extern void sha512_process_bytes (const void *buffer, size_t len,
58                                   struct sha512_ctx *ctx);
59
60 /* Process the remaining bytes in the buffer and put result from CTX
61    in first 64 (48) bytes following RESBUF.  The result is always in little
62    endian byte order, so that a byte-wise output yields to the wanted
63    ASCII representation of the message digest.
64
65    IMPORTANT: On some systems it is required that RESBUF be correctly
66    aligned for a 64 bits value.  */
67 extern void *sha512_finish_ctx (struct sha512_ctx *ctx, void *resbuf);
68 extern void *sha384_finish_ctx (struct sha512_ctx *ctx, void *resbuf);
69
70
71 /* Put result from CTX in first 64 (48) bytes following RESBUF.  The result is
72    always in little endian byte order, so that a byte-wise output yields
73    to the wanted ASCII representation of the message digest.
74
75    IMPORTANT: On some systems it is required that RESBUF is correctly
76    aligned for a 32 bits value.  */
77 extern void *sha512_read_ctx (const struct sha512_ctx *ctx, void *resbuf);
78 extern void *sha384_read_ctx (const struct sha512_ctx *ctx, void *resbuf);
79
80
81 /* Compute SHA512 (SHA384) message digest for bytes read from STREAM.  The
82    resulting message digest number will be written into the 64 (48) bytes
83    beginning at RESBLOCK.  */
84 extern int sha512_stream (FILE *stream, void *resblock);
85 extern int sha384_stream (FILE *stream, void *resblock);
86
87 /* Compute SHA512 (SHA384) message digest for LEN bytes beginning at BUFFER.  The
88    result is always in little endian byte order, so that a byte-wise
89    output yields to the wanted ASCII representation of the message
90    digest.  */
91 extern void *sha512_buffer (const char *buffer, size_t len, void *resblock);
92 extern void *sha384_buffer (const char *buffer, size_t len, void *resblock);
93
94 # define rol64(x,n) ( ((x) << (n)) | ((x) >> (64-(n))) )
95
96 #endif