1 /* Declaration of functions and data types used for MD5 sum computing
2 library functions.
3 Copyright (C) 1995-1997, 1999-2001, 2004-2006, 2008-2023 Free
4 Software Foundation, Inc.
5 This file is part of the GNU C Library.
6
7 This file is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
11
12 This file is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19
20 #ifndef _MD5_H
21 #define _MD5_H 1
22
23 #include <stdio.h>
24 #include <stdint.h>
25
26 # if HAVE_OPENSSL_MD5
27 # ifndef OPENSSL_API_COMPAT
28 # define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API. */
29 # endif
30 # include <openssl/md5.h>
31 # endif
32
33 #define MD5_DIGEST_SIZE 16
34 #define MD5_BLOCK_SIZE 64
35
36 #ifndef __GNUC_PREREQ
37 # if defined __GNUC__ && defined __GNUC_MINOR__
38 # define __GNUC_PREREQ(maj, min) \
39 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
40 # else
41 # define __GNUC_PREREQ(maj, min) 0
42 # endif
43 #endif
44
45 #ifndef __THROW
46 # if defined __cplusplus && (__GNUC_PREREQ (2,8) || __clang_major__ >= 4)
47 # define __THROW throw ()
48 # else
49 # define __THROW
50 # endif
51 #endif
52
53 #ifndef _LIBC
54 # define __md5_buffer md5_buffer
55 # define __md5_finish_ctx md5_finish_ctx
56 # define __md5_init_ctx md5_init_ctx
57 # define __md5_process_block md5_process_block
58 # define __md5_process_bytes md5_process_bytes
59 # define __md5_read_ctx md5_read_ctx
60 # define __md5_stream md5_stream
61 #endif
62
63 # ifdef __cplusplus
64 extern "C" {
65 # endif
66
67 # if HAVE_OPENSSL_MD5
68 # define GL_OPENSSL_NAME 5
69 # include "gl_openssl.h"
70 # else
71 /* Structure to save state of computation between the single steps. */
72 struct md5_ctx
73 {
74 uint32_t A;
75 uint32_t B;
76 uint32_t C;
77 uint32_t D;
78
79 uint32_t total[2];
80 uint32_t buflen; /* ≥ 0, ≤ 128 */
81 uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
82 };
83
84 /*
85 * The following three functions are build up the low level used in
86 * the functions 'md5_stream' and 'md5_buffer'.
87 */
88
89 /* Initialize structure containing state of computation.
90 (RFC 1321, 3.3: Step 3) */
91 extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW;
92
93 /* Starting with the result of former calls of this function (or the
94 initialization function update the context for the next LEN bytes
95 starting at BUFFER.
96 It is necessary that LEN is a multiple of 64!!! */
97 extern void __md5_process_block (const void *buffer, size_t len,
98 struct md5_ctx *ctx) __THROW;
99
100 /* Starting with the result of former calls of this function (or the
101 initialization function update the context for the next LEN bytes
102 starting at BUFFER.
103 It is NOT required that LEN is a multiple of 64. */
104 extern void __md5_process_bytes (const void *buffer, size_t len,
105 struct md5_ctx *ctx) __THROW;
106
107 /* Process the remaining bytes in the buffer and put result from CTX
108 in first 16 bytes following RESBUF. The result is always in little
109 endian byte order, so that a byte-wise output yields to the wanted
110 ASCII representation of the message digest. */
111 extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *restrict resbuf)
112 __THROW;
113
114
115 /* Put result from CTX in first 16 bytes following RESBUF. The result is
116 always in little endian byte order, so that a byte-wise output yields
117 to the wanted ASCII representation of the message digest. */
118 extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *restrict resbuf)
119 __THROW;
120
121
122 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
123 result is always in little endian byte order, so that a byte-wise
124 output yields to the wanted ASCII representation of the message
125 digest. */
126 extern void *__md5_buffer (const char *buffer, size_t len,
127 void *restrict resblock) __THROW;
128
129 # endif
130
131 /* Compute MD5 message digest for bytes read from STREAM.
132 STREAM is an open file stream. Regular files are handled more efficiently.
133 The contents of STREAM from its current position to its end will be read.
134 The case that the last operation on STREAM was an 'ungetc' is not supported.
135 The resulting message digest number will be written into the 16 bytes
136 beginning at RESBLOCK. */
137 extern int __md5_stream (FILE *stream, void *resblock) __THROW;
138
139
140 # ifdef __cplusplus
141 }
142 # endif
143
144 #endif /* md5.h */
145
146 /*
147 * Hey Emacs!
148 * Local Variables:
149 * coding: utf-8
150 * End:
151 */