md5_one.c revision 280304
1239674Srwatson/* crypto/md5/md5_one.c */
2331722Seadler/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3239674Srwatson * All rights reserved.
4239674Srwatson *
5239674Srwatson * This package is an SSL implementation written
6239674Srwatson * by Eric Young (eay@cryptsoft.com).
7239674Srwatson * The implementation was written so as to conform with Netscapes SSL.
8239674Srwatson *
9239674Srwatson * This library is free for commercial and non-commercial use as long as
10239674Srwatson * the following conditions are aheared to.  The following conditions
11239674Srwatson * apply to all code found in this distribution, be it the RC4, RSA,
12239674Srwatson * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13239674Srwatson * included with this distribution is covered by the same copyright terms
14239674Srwatson * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15239674Srwatson *
16239674Srwatson * Copyright remains Eric Young's, and as such any Copyright notices in
17239674Srwatson * the code are not to be removed.
18239674Srwatson * If this package is used in a product, Eric Young should be given attribution
19239674Srwatson * as the author of the parts of the library used.
20239674Srwatson * This can be in the form of a textual message at program startup or
21239674Srwatson * in documentation (online or textual) provided with the package.
22239674Srwatson *
23239674Srwatson * Redistribution and use in source and binary forms, with or without
24239674Srwatson * modification, are permitted provided that the following conditions
25239674Srwatson * are met:
26239674Srwatson * 1. Redistributions of source code must retain the copyright
27239674Srwatson *    notice, this list of conditions and the following disclaimer.
28239674Srwatson * 2. Redistributions in binary form must reproduce the above copyright
29239674Srwatson *    notice, this list of conditions and the following disclaimer in the
30239674Srwatson *    documentation and/or other materials provided with the distribution.
31239674Srwatson * 3. All advertising materials mentioning features or use of this software
32239674Srwatson *    must display the following acknowledgement:
33239674Srwatson *    "This product includes cryptographic software written by
34239674Srwatson *     Eric Young (eay@cryptsoft.com)"
35239674Srwatson *    The word 'cryptographic' can be left out if the rouines from the library
36239674Srwatson *    being used are not cryptographic related :-).
37239674Srwatson * 4. If you include any Windows specific code (or a derivative thereof) from
38239674Srwatson *    the apps directory (application code) you must include an acknowledgement:
39239674Srwatson *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40239674Srwatson *
41239674Srwatson * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42239674Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43239674Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44239674Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45239674Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46239674Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47239674Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48239674Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49239674Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50239674Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51239674Srwatson * SUCH DAMAGE.
52239674Srwatson *
53239674Srwatson * The licence and distribution terms for any publically available version or
54239674Srwatson * derivative of this code cannot be changed.  i.e. this code cannot simply be
55239674Srwatson * copied and put under another distribution licence
56239674Srwatson * [including the GNU Public Licence.]
57239674Srwatson */
58239674Srwatson
59239674Srwatson#include <stdio.h>
60239674Srwatson#include <string.h>
61239674Srwatson#include <openssl/md5.h>
62245380Srwatson#include <openssl/crypto.h>
63245380Srwatson
64239674Srwatson#ifdef CHARSET_EBCDIC
65239674Srwatson# include <openssl/ebcdic.h>
66239674Srwatson#endif
67239674Srwatson
68239674Srwatsonunsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md)
69239674Srwatson{
70239674Srwatson    MD5_CTX c;
71239674Srwatson    static unsigned char m[MD5_DIGEST_LENGTH];
72239674Srwatson
73331722Seadler    if (md == NULL)
74239674Srwatson        md = m;
75239674Srwatson    if (!MD5_Init(&c))
76239674Srwatson        return NULL;
77239674Srwatson#ifndef CHARSET_EBCDIC
78239674Srwatson    MD5_Update(&c, d, n);
79239674Srwatson#else
80239674Srwatson    {
81239674Srwatson        char temp[1024];
82239674Srwatson        unsigned long chunk;
83239674Srwatson
84239674Srwatson        while (n > 0) {
85239674Srwatson            chunk = (n > sizeof(temp)) ? sizeof(temp) : n;
86239674Srwatson            ebcdic2ascii(temp, d, chunk);
87239674Srwatson            MD5_Update(&c, temp, chunk);
88239674Srwatson            n -= chunk;
89239674Srwatson            d += chunk;
90239674Srwatson        }
91239674Srwatson    }
92239674Srwatson#endif
93239674Srwatson    MD5_Final(md, &c);
94239674Srwatson    OPENSSL_cleanse(&c, sizeof(c)); /* security consideration */
95239674Srwatson    return (md);
96239674Srwatson}
97239674Srwatson