| 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-2024 Free Software |
| 4 |
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 |
/* This file uses HAVE_OPENSSL_MD5. */ |
| 24 |
#if !_GL_CONFIG_H_INCLUDED |
| 25 |
#error "Please include config.h first." |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdint.h> |
| 30 |
|
| 31 |
# if HAVE_OPENSSL_MD5 |
| 32 |
# ifndef OPENSSL_API_COMPAT |
| 33 |
# define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API. */ |
| 34 |
# endif |
| 35 |
/* If <openssl/macros.h> would give a compile-time error, don't use OpenSSL. */ |
| 36 |
# include <openssl/opensslv.h> |
| 37 |
# if OPENSSL_VERSION_MAJOR >= 3 |
| 38 |
# include <openssl/configuration.h> |
| 39 |
# if (OPENSSL_CONFIGURED_API \ |
| 40 |
< (OPENSSL_API_COMPAT < 0x900000L ? OPENSSL_API_COMPAT : \ |
| 41 |
((OPENSSL_API_COMPAT >> 28) & 0xF) * 10000 \ |
| 42 |
+ ((OPENSSL_API_COMPAT >> 20) & 0xFF) * 100 \ |
| 43 |
+ ((OPENSSL_API_COMPAT >> 12) & 0xFF))) |
| 44 |
# undef HAVE_OPENSSL_MD5 |
| 45 |
# endif |
| 46 |
# endif |
| 47 |
# if HAVE_OPENSSL_MD5 |
| 48 |
# include <openssl/md5.h> |
| 49 |
# endif |
| 50 |
# endif |
| 51 |
|
| 52 |
#define MD5_DIGEST_SIZE 16 |
| 53 |
#define MD5_BLOCK_SIZE 64 |
| 54 |
|
| 55 |
#if defined __clang__ |
| 56 |
/* clang really only groks GNU C 4.2, regardless of its value of __GNUC__. */ |
| 57 |
# undef __GNUC_PREREQ |
| 58 |
# define __GNUC_PREREQ(maj, min) ((maj) < 4 + ((min) <= 2)) |
| 59 |
#endif |
| 60 |
#ifndef __GNUC_PREREQ |
| 61 |
# if defined __GNUC__ && defined __GNUC_MINOR__ |
| 62 |
# define __GNUC_PREREQ(maj, min) ((maj) < __GNUC__ + ((min) <= __GNUC_MINOR__)) |
| 63 |
# else |
| 64 |
# define __GNUC_PREREQ(maj, min) 0 |
| 65 |
# endif |
| 66 |
#endif |
| 67 |
|
| 68 |
#ifndef __THROW |
| 69 |
# if defined __cplusplus && (__GNUC_PREREQ (2,8) || __clang_major__ >= 4) |
| 70 |
# if __cplusplus >= 201103L |
| 71 |
# define __THROW noexcept (true) |
| 72 |
# else |
| 73 |
# define __THROW throw () |
| 74 |
# endif |
| 75 |
# else |
| 76 |
# define __THROW |
| 77 |
# endif |
| 78 |
#endif |
| 79 |
|
| 80 |
#ifndef _LIBC |
| 81 |
# define __md5_buffer md5_buffer |
| 82 |
# define __md5_finish_ctx md5_finish_ctx |
| 83 |
# define __md5_init_ctx md5_init_ctx |
| 84 |
# define __md5_process_block md5_process_block |
| 85 |
# define __md5_process_bytes md5_process_bytes |
| 86 |
# define __md5_read_ctx md5_read_ctx |
| 87 |
# define __md5_stream md5_stream |
| 88 |
#endif |
| 89 |
|
| 90 |
# ifdef __cplusplus |
| 91 |
extern "C" { |
| 92 |
# endif |
| 93 |
|
| 94 |
# if HAVE_OPENSSL_MD5 |
| 95 |
# define GL_OPENSSL_NAME 5 |
| 96 |
# include "gl_openssl.h" |
| 97 |
# else |
| 98 |
/* Structure to save state of computation between the single steps. */ |
| 99 |
struct md5_ctx |
| 100 |
{ |
| 101 |
uint32_t A; |
| 102 |
uint32_t B; |
| 103 |
uint32_t C; |
| 104 |
uint32_t D; |
| 105 |
|
| 106 |
uint32_t total[2]; |
| 107 |
uint32_t buflen; /* ≥ 0, ≤ 128 */ |
| 108 |
uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */ |
| 109 |
}; |
| 110 |
|
| 111 |
/* |
| 112 |
* The following three functions are build up the low level used in |
| 113 |
* the functions 'md5_stream' and 'md5_buffer'. |
| 114 |
*/ |
| 115 |
|
| 116 |
/* Initialize structure containing state of computation. |
| 117 |
(RFC 1321, 3.3: Step 3) */ |
| 118 |
extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW; |
| 119 |
|
| 120 |
/* Starting with the result of former calls of this function (or the |
| 121 |
initialization function update the context for the next LEN bytes |
| 122 |
starting at BUFFER. |
| 123 |
It is necessary that LEN is a multiple of 64!!! */ |
| 124 |
extern void __md5_process_block (const void *buffer, size_t len, |
| 125 |
struct md5_ctx *ctx) __THROW; |
| 126 |
|
| 127 |
/* Starting with the result of former calls of this function (or the |
| 128 |
initialization function update the context for the next LEN bytes |
| 129 |
starting at BUFFER. |
| 130 |
It is NOT required that LEN is a multiple of 64. */ |
| 131 |
extern void __md5_process_bytes (const void *buffer, size_t len, |
| 132 |
struct md5_ctx *ctx) __THROW; |
| 133 |
|
| 134 |
/* Process the remaining bytes in the buffer and put result from CTX |
| 135 |
in first 16 bytes following RESBUF. The result is always in little |
| 136 |
endian byte order, so that a byte-wise output yields to the wanted |
| 137 |
ASCII representation of the message digest. */ |
| 138 |
extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *restrict resbuf) |
| 139 |
__THROW; |
| 140 |
|
| 141 |
|
| 142 |
/* Put result from CTX in first 16 bytes following RESBUF. The result is |
| 143 |
always in little endian byte order, so that a byte-wise output yields |
| 144 |
to the wanted ASCII representation of the message digest. */ |
| 145 |
extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *restrict resbuf) |
| 146 |
__THROW; |
| 147 |
|
| 148 |
|
| 149 |
/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The |
| 150 |
result is always in little endian byte order, so that a byte-wise |
| 151 |
output yields to the wanted ASCII representation of the message |
| 152 |
digest. */ |
| 153 |
extern void *__md5_buffer (const char *buffer, size_t len, |
| 154 |
void *restrict resblock) __THROW; |
| 155 |
|
| 156 |
# endif |
| 157 |
|
| 158 |
/* Compute MD5 message digest for bytes read from STREAM. |
| 159 |
STREAM is an open file stream. Regular files are handled more efficiently. |
| 160 |
The contents of STREAM from its current position to its end will be read. |
| 161 |
The case that the last operation on STREAM was an 'ungetc' is not supported. |
| 162 |
The resulting message digest number will be written into the 16 bytes |
| 163 |
beginning at RESBLOCK. */ |
| 164 |
extern int __md5_stream (FILE *stream, void *resblock) __THROW; |
| 165 |
|
| 166 |
|
| 167 |
# ifdef __cplusplus |
| 168 |
} |
| 169 |
# endif |
| 170 |
|
| 171 |
#endif /* md5.h */ |
| 172 |
|
| 173 |
/* |
| 174 |
* Hey Emacs! |
| 175 |
* Local Variables: |
| 176 |
* coding: utf-8 |
| 177 |
* End: |
| 178 |
*/ |