Imported Upstream version 0.155
[platform/upstream/elfutils.git] / lib / md5.h
1 /* Declaration of functions and data types used for MD5 sum computing
2    library functions.
3    Copyright (C) 1995,1996,1997,1999-2001,2004,2005,2008 Red Hat, Inc.
4    This file is part of elfutils.
5    Written by Ulrich Drepper <drepper@redhat.com>, 1995.
6
7    This file is free software; you can redistribute it and/or modify
8    it under the terms of either
9
10      * the GNU Lesser General Public License as published by the Free
11        Software Foundation; either version 3 of the License, or (at
12        your option) any later version
13
14    or
15
16      * the GNU General Public License as published by the Free
17        Software Foundation; either version 2 of the License, or (at
18        your option) any later version
19
20    or both in parallel, as here.
21
22    elfutils is distributed in the hope that it will be useful, but
23    WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25    General Public License for more details.
26
27    You should have received copies of the GNU General Public License and
28    the GNU Lesser General Public License along with this program.  If
29    not, see <http://www.gnu.org/licenses/>.  */
30
31 #ifndef _MD5_H
32 #define _MD5_H 1
33
34 #include <limits.h>
35 #include <stdint.h>
36 #include <stdio.h>
37
38 #define MD5_DIGEST_SIZE 16
39 #define MD5_BLOCK_SIZE 64
40
41 typedef uint32_t md5_uint32;
42 typedef uintptr_t md5_uintptr;
43
44 /* Structure to save state of computation between the single steps.  */
45 struct md5_ctx
46 {
47   md5_uint32 A;
48   md5_uint32 B;
49   md5_uint32 C;
50   md5_uint32 D;
51
52   md5_uint32 total[2];
53   md5_uint32 buflen;
54   char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
55 };
56
57 /*
58  * The following three functions are build up the low level used in
59  * the functions `md5_stream' and `md5_buffer'.
60  */
61
62 /* Initialize structure containing state of computation.
63    (RFC 1321, 3.3: Step 3)  */
64 extern void md5_init_ctx (struct md5_ctx *ctx);
65
66 /* Starting with the result of former calls of this function (or the
67    initialization function update the context for the next LEN bytes
68    starting at BUFFER.
69    It is necessary that LEN is a multiple of 64!!! */
70 extern void md5_process_block (const void *buffer, size_t len,
71                                struct md5_ctx *ctx);
72
73 /* Starting with the result of former calls of this function (or the
74    initialization function update the context for the next LEN bytes
75    starting at BUFFER.
76    It is NOT required that LEN is a multiple of 64.  */
77 extern void md5_process_bytes (const void *buffer, size_t len,
78                                struct md5_ctx *ctx);
79
80 /* Process the remaining bytes in the buffer and put result from CTX
81    in first 16 bytes following RESBUF.  The result is always in little
82    endian byte order, so that a byte-wise output yields to the wanted
83    ASCII representation of the message digest.
84
85    IMPORTANT: On some systems it is required that RESBUF is correctly
86    aligned for a 32 bits value.  */
87 extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
88
89
90 /* Put result from CTX in first 16 bytes following RESBUF.  The result is
91    always in little endian byte order, so that a byte-wise output yields
92    to the wanted ASCII representation of the message digest.
93
94    IMPORTANT: On some systems it is required that RESBUF is correctly
95    aligned for a 32 bits value.  */
96 extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
97
98
99 /* Compute MD5 message digest for bytes read from STREAM.  The
100    resulting message digest number will be written into the 16 bytes
101    beginning at RESBLOCK.  */
102 extern int md5_stream (FILE *stream, void *resblock);
103
104 /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
105    result is always in little endian byte order, so that a byte-wise
106    output yields to the wanted ASCII representation of the message
107    digest.  */
108 extern void *md5_buffer (const char *buffer, size_t len, void *resblock);
109
110 #endif /* md5.h */