3 /* ========================================================================== **
8 * Copyright (C) 2003, 2004 by Christopher R. Hertel
10 * Email: crh@ubiqx.mn.org
12 * Modified for use with MPlayer, detailed CVS changelog at
13 * http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
16 * -------------------------------------------------------------------------- **
19 * Implements the MD5 hash algorithm, as described in RFC 1321.
21 * -------------------------------------------------------------------------- **
25 * This library is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU Lesser General Public
27 * License as published by the Free Software Foundation; either
28 * version 2.1 of the License, or (at your option) any later version.
30 * This library is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 * Lesser General Public License for more details.
35 * You should have received a copy of the GNU Lesser General Public
36 * License along with this library; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 * -------------------------------------------------------------------------- **
43 * None of this will make any sense unless you're studying RFC 1321 as you
46 * MD5 is described in RFC 1321.
47 * The MD*4* algorithm is described in RFC 1320 (that's 1321 - 1).
48 * MD5 is very similar to MD4, but not quite similar enough to justify
49 * putting the two into a single module. Besides, I wanted to add a few
50 * extra functions to this one to expand its usability.
52 * There are three primary motivations for this particular implementation.
53 * 1) Programmer's pride. I wanted to be able to say I'd done it, and I
54 * wanted to learn from the experience.
55 * 2) Portability. I wanted an implementation that I knew to be portable
56 * to a reasonable number platforms. In particular, the algorithm is
57 * designed with little-endian platforms in mind, but I wanted an
58 * endian-agnostic implementation.
59 * 3) Compactness. While not an overriding goal, I thought it worth-while
60 * to see if I could reduce the overall size of the result. This is in
61 * keeping with my hopes that this library will be suitable for use in
62 * some embedded environments.
63 * Beyond that, cleanliness and clarity are always worth pursuing.
65 * As mentioned above, the code really only makes sense if you are familiar
66 * with the MD5 algorithm or are using RFC 1321 as a guide. This code is
67 * quirky, however, so you'll want to be reading carefully.
69 * Yeah...most of the comments are cut-and-paste from my MD4 implementation.
71 * -------------------------------------------------------------------------- **
74 * IETF RFC 1321: The MD5 Message-Digest Algorithm
75 * Ron Rivest. IETF, April, 1992
77 * ========================================================================== **
80 /* #include "auth_common.h" This was in the original. */
82 #include <inttypes.h> /* This was not... */
84 /* -------------------------------------------------------------------------- **
88 typedef unsigned char uchar
; /* Added uchar typedef to keep as close to the
89 original as possible. */
100 /* -------------------------------------------------------------------------- **
104 auth_md5Ctx
*auth_md5InitCtx( auth_md5Ctx
*ctx
);
105 /* ------------------------------------------------------------------------ **
106 * Initialize an MD5 context.
108 * Input: ctx - A pointer to the MD5 context structure to be initialized.
109 * Contexts are typically created thusly:
110 * ctx = (auth_md5Ctx *)malloc( sizeof(auth_md5Ctx) );
112 * Output: A pointer to the initialized context (same as <ctx>).
114 * Notes: The purpose of the context is to make it possible to generate
115 * an MD5 Message Digest in stages, rather than having to pass a
116 * single large block to a single MD5 function. The context
117 * structure keeps track of various bits of state information.
119 * Once the context is initialized, the blocks of message data
120 * are passed to the <auth_md5SumCtx()> function. Once the
121 * final bit of data has been handed to <auth_md5SumCtx()> the
122 * context can be closed out by calling <auth_md5CloseCtx()>,
123 * which also calculates the final MD5 result.
125 * Don't forget to free an allocated context structure when
126 * you've finished using it.
128 * See Also: <auth_md5SumCtx()>, <auth_md5CloseCtx()>
130 * ------------------------------------------------------------------------ **
134 auth_md5Ctx
*auth_md5SumCtx( auth_md5Ctx
*ctx
,
137 /* ------------------------------------------------------------------------ **
138 * Build an MD5 Message Digest within the given context.
140 * Input: ctx - Pointer to the context in which the MD5 sum is being
142 * src - A chunk of source data. This will be used to drive
144 * len - The number of bytes in <src>.
146 * Output: A pointer to the updated context (same as <ctx>).
148 * See Also: <auth_md5InitCtx()>, <auth_md5CloseCtx()>, <auth_md5Sum()>
150 * ------------------------------------------------------------------------ **
154 auth_md5Ctx
*auth_md5CloseCtx( auth_md5Ctx
*ctx
, uchar
*dst
);
155 /* ------------------------------------------------------------------------ **
156 * Close an MD5 Message Digest context and generate the final MD5 sum.
158 * Input: ctx - Pointer to the context in which the MD5 sum is being
160 * dst - A pointer to at least 16 bytes of memory, which will
161 * receive the finished MD5 sum.
163 * Output: A pointer to the closed context (same as <ctx>).
164 * You might use this to free a malloc'd context structure. :)
166 * Notes: The context (<ctx>) is returned in an undefined state.
167 * It must be re-initialized before re-use.
169 * See Also: <auth_md5InitCtx()>, <auth_md5SumCtx()>
171 * ------------------------------------------------------------------------ **
175 uchar
*auth_md5Sum( uchar
*dst
, const uchar
*src
, const int len
);
176 /* ------------------------------------------------------------------------ **
177 * Compute an MD5 message digest.
179 * Input: dst - Destination buffer into which the result will be written.
180 * Must be 16 bytes, minimum.
181 * src - Source data block to be MD5'd.
182 * len - The length, in bytes, of the source block.
183 * (Note that the length is given in bytes, not bits.)
185 * Output: A pointer to <dst>, which will contain the calculated 16-byte
186 * MD5 message digest.
188 * Notes: This function is a shortcut. It takes a single input block.
189 * For more drawn-out operations, see <auth_md5InitCtx()>.
191 * This function is interface-compatible with the
192 * <auth_md4Sum()> function in the MD4 module.
194 * The MD5 algorithm is designed to work on data with an
195 * arbitrary *bit* length. Most implementations, this one
196 * included, handle the input data in byte-sized chunks.
198 * The MD5 algorithm does much of its work using four-byte
199 * words, and so can be tuned for speed based on the endian-ness
200 * of the host. This implementation is intended to be
201 * endian-neutral, which may make it a teeny bit slower than
204 * See Also: <auth_md5InitCtx()>
206 * ------------------------------------------------------------------------ **
210 /* ========================================================================== */
211 #endif /* AUTH_MD5_H */