1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /*
11  * MD2 low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/md2.h>
19 
20 /*
21  * This is a separate file so that #defines in cryptlib.h can map my MD
22  * functions to different names
23  */
24 
MD2(const unsigned char * d,size_t n,unsigned char * md)25 unsigned char *MD2(const unsigned char *d, size_t n, unsigned char *md)
26 {
27     MD2_CTX c;
28     static unsigned char m[MD2_DIGEST_LENGTH];
29 
30     if (md == NULL)
31         md = m;
32     if (!MD2_Init(&c))
33         return NULL;
34 #ifndef CHARSET_EBCDIC
35     MD2_Update(&c, d, n);
36 #else
37     {
38         char temp[1024];
39         unsigned long chunk;
40 
41         while (n > 0) {
42             chunk = (n > sizeof(temp)) ? sizeof(temp) : n;
43             ebcdic2ascii(temp, d, chunk);
44             MD2_Update(&c, temp, chunk);
45             n -= chunk;
46             d += chunk;
47         }
48     }
49 #endif
50     MD2_Final(md, &c);
51     OPENSSL_cleanse(&c, sizeof(c)); /* Security consideration */
52     return md;
53 }
54