blob: 26e191a24bb2ac8d51a7637c23eaef2796117b5a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file md5.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief MD5 message digest algorithm (hash function)
Hanno Beckerbbca8c52017-09-25 14:53:51 +01005 *
6 * \warning MD5 is considered a weak message digest and its use constitutes a
7 * security risk. We recommend considering stronger message
8 * digests instead.
Darryl Greena40a1012018-01-05 15:33:17 +00009 */
10/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020011 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#ifndef MBEDTLS_MD5_H
27#define MBEDTLS_MD5_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020028#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Bence Szépkútic662b362021-05-27 11:25:03 +020030#include "mbedtls/build_info.h"
Paul Bakker90995b52013-06-24 19:20:35 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000034
Paul Bakker407a0da2013-06-27 14:29:21 +020035#ifdef __cplusplus
36extern "C" {
37#endif
38
Ron Eldorb2aacec2017-05-18 16:53:08 +030039#if !defined(MBEDTLS_MD5_ALT)
40// Regular implementation
41//
42
Paul Bakker5121ce52009-01-03 21:22:43 +000043/**
44 * \brief MD5 context structure
Hanno Beckerbbca8c52017-09-25 14:53:51 +010045 *
46 * \warning MD5 is considered a weak message digest and its use
47 * constitutes a security risk. We recommend considering
48 * stronger message digests instead.
49 *
Paul Bakker5121ce52009-01-03 21:22:43 +000050 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051typedef struct mbedtls_md5_context {
52 uint32_t MBEDTLS_PRIVATE(total)[2]; /*!< number of bytes processed */
53 uint32_t MBEDTLS_PRIVATE(state)[4]; /*!< intermediate digest state */
54 unsigned char MBEDTLS_PRIVATE(buffer)[64]; /*!< data block being processed
55 */
56} mbedtls_md5_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020058#else /* MBEDTLS_MD5_ALT */
59# include "md5_alt.h"
Ron Eldorb2aacec2017-05-18 16:53:08 +030060#endif /* MBEDTLS_MD5_ALT */
61
Paul Bakker5121ce52009-01-03 21:22:43 +000062/**
Paul Bakker5b4af392014-06-26 12:09:34 +020063 * \brief Initialize MD5 context
64 *
65 * \param ctx MD5 context to be initialized
Hanno Beckerbbca8c52017-09-25 14:53:51 +010066 *
67 * \warning MD5 is considered a weak message digest and its use
68 * constitutes a security risk. We recommend considering
69 * stronger message digests instead.
70 *
Paul Bakker5b4af392014-06-26 12:09:34 +020071 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072void mbedtls_md5_init(mbedtls_md5_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020073
74/**
75 * \brief Clear MD5 context
76 *
77 * \param ctx MD5 context to be cleared
Hanno Beckerbbca8c52017-09-25 14:53:51 +010078 *
79 * \warning MD5 is considered a weak message digest and its use
80 * constitutes a security risk. We recommend considering
81 * stronger message digests instead.
82 *
Paul Bakker5b4af392014-06-26 12:09:34 +020083 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020084void mbedtls_md5_free(mbedtls_md5_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020085
86/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020087 * \brief Clone (the state of) an MD5 context
88 *
89 * \param dst The destination context
90 * \param src The context to be cloned
Hanno Beckerbbca8c52017-09-25 14:53:51 +010091 *
92 * \warning MD5 is considered a weak message digest and its use
93 * constitutes a security risk. We recommend considering
94 * stronger message digests instead.
95 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020096 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020097void mbedtls_md5_clone(mbedtls_md5_context *dst,
98 const mbedtls_md5_context *src);
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020099
100/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 * \brief MD5 context setup
102 *
103 * \param ctx context to be initialized
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100104 *
105 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100106 *
107 * \warning MD5 is considered a weak message digest and its use
108 * constitutes a security risk. We recommend considering
109 * stronger message digests instead.
110 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200112int mbedtls_md5_starts(mbedtls_md5_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114/**
115 * \brief MD5 process buffer
116 *
117 * \param ctx MD5 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100118 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 * \param ilen length of the input data
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100120 *
121 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100122 *
123 * \warning MD5 is considered a weak message digest and its use
124 * constitutes a security risk. We recommend considering
125 * stronger message digests instead.
126 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200128int mbedtls_md5_update(mbedtls_md5_context *ctx,
129 const unsigned char *input,
130 size_t ilen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
132/**
133 * \brief MD5 final digest
134 *
135 * \param ctx MD5 context
136 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100137 *
138 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100139 *
140 * \warning MD5 is considered a weak message digest and its use
141 * constitutes a security risk. We recommend considering
142 * stronger message digests instead.
143 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200145int mbedtls_md5_finish(mbedtls_md5_context *ctx, unsigned char output[16]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100147/**
148 * \brief MD5 process data block (internal use only)
149 *
150 * \param ctx MD5 context
151 * \param data buffer holding one block of data
152 *
153 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100154 *
155 * \warning MD5 is considered a weak message digest and its use
156 * constitutes a security risk. We recommend considering
157 * stronger message digests instead.
158 *
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100159 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200160int mbedtls_internal_md5_process(mbedtls_md5_context *ctx,
161 const unsigned char data[64]);
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100162
Paul Bakker5121ce52009-01-03 21:22:43 +0000163/**
164 * \brief Output = MD5( input buffer )
165 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100166 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 * \param ilen length of the input data
168 * \param output MD5 checksum result
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100169 *
170 * \return 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100171 *
172 * \warning MD5 is considered a weak message digest and its use
173 * constitutes a security risk. We recommend considering
174 * stronger message digests instead.
175 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177int mbedtls_md5(const unsigned char *input,
178 size_t ilen,
179 unsigned char output[16]);
Andres Amaya Garcia2cfd7a92017-05-02 10:19:27 +0100180
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500181#if defined(MBEDTLS_SELF_TEST)
182
Paul Bakker5121ce52009-01-03 21:22:43 +0000183/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 * \brief Checkup routine
185 *
186 * \return 0 if successful, or 1 if the test failed
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100187 *
188 * \warning MD5 is considered a weak message digest and its use
189 * constitutes a security risk. We recommend considering
190 * stronger message digests instead.
191 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193int mbedtls_md5_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500195#endif /* MBEDTLS_SELF_TEST */
196
Paul Bakker5121ce52009-01-03 21:22:43 +0000197#ifdef __cplusplus
198}
199#endif
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#endif /* mbedtls_md5.h */