1 /* Copyright (c) 2012, Oracle and/or its affiliates.
2 Copyright (c) 2017, MariaDB Corporation
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
16
17
18 /**
19 @file
20
21 @brief
22 Wrapper functions for OpenSSL and YaSSL. Also provides a Compatibility layer
23 to make available YaSSL's MD5 implementation.
24 */
25
26 #include <my_global.h>
27 #include <my_md5.h>
28 #include <stdarg.h>
29
30 #if defined(HAVE_WOLFSSL)
31 #include <wolfssl/wolfcrypt/md5.h>
32 #include <ssl_compat.h>
33 typedef wc_Md5 EVP_MD_CTX;
md5_init(EVP_MD_CTX * context)34 static void md5_init(EVP_MD_CTX *context)
35 {
36 wc_InitMd5(context);;
37 }
38
md5_input(EVP_MD_CTX * context,const uchar * buf,unsigned len)39 static void md5_input(EVP_MD_CTX *context, const uchar *buf, unsigned len)
40 {
41 wc_Md5Update(context, buf, len);
42 }
43
md5_result(EVP_MD_CTX * context,uchar digest[MD5_HASH_SIZE])44 static void md5_result(EVP_MD_CTX *context, uchar digest[MD5_HASH_SIZE])
45 {
46 wc_Md5Final(context,digest);
47 }
48
49 #elif defined(HAVE_OPENSSL)
50 #include <openssl/evp.h>
51 #include <ssl_compat.h>
52
md5_init(EVP_MD_CTX * context)53 static void md5_init(EVP_MD_CTX *context)
54 {
55 EVP_MD_CTX_init(context);
56 #ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
57 /* Ok to ignore FIPS: MD5 is not used for crypto here */
58 EVP_MD_CTX_set_flags(context, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
59 #endif
60 EVP_DigestInit_ex(context, EVP_md5(), NULL);
61 }
62
md5_input(EVP_MD_CTX * context,const uchar * buf,unsigned len)63 static void md5_input(EVP_MD_CTX *context, const uchar *buf, unsigned len)
64 {
65 EVP_DigestUpdate(context, buf, len);
66 }
67
md5_result(EVP_MD_CTX * context,uchar digest[MD5_HASH_SIZE])68 static void md5_result(EVP_MD_CTX *context, uchar digest[MD5_HASH_SIZE])
69 {
70 EVP_DigestFinal_ex(context, digest, NULL);
71 EVP_MD_CTX_reset(context);
72 }
73
74 #endif /* HAVE_WOLFSSL */
75
76 /**
77 Wrapper function to compute MD5 message digest.
78
79 @param digest [out] Computed MD5 digest
80 @param buf [in] Message to be computed
81 @param len [in] Length of the message
82
83 @return void
84 */
my_md5(uchar * digest,const char * buf,size_t len)85 void my_md5(uchar *digest, const char *buf, size_t len)
86 {
87 char ctx_buf[EVP_MD_CTX_SIZE];
88 EVP_MD_CTX * const ctx= (EVP_MD_CTX*)ctx_buf;
89 md5_init(ctx);
90 md5_input(ctx, (const uchar *)buf, (uint) len);
91 md5_result(ctx, digest);
92 }
93
94
95 /**
96 Wrapper function to compute MD5 message digest for
97 many messages, concatenated.
98
99 @param digest [out] Computed MD5 digest
100 @param buf1 [in] First message
101 @param len1 [in] Length of first message
102 ...
103 @param bufN [in] NULL terminates the list of buf,len pairs.
104
105 @return void
106 */
my_md5_multi(uchar * digest,...)107 void my_md5_multi(uchar *digest, ...)
108 {
109 va_list args;
110 const uchar *str;
111 char ctx_buf[EVP_MD_CTX_SIZE];
112 EVP_MD_CTX * const ctx= (EVP_MD_CTX*)ctx_buf;
113 va_start(args, digest);
114
115 md5_init(ctx);
116 for (str= va_arg(args, const uchar*); str; str= va_arg(args, const uchar*))
117 md5_input(ctx, str, (uint) va_arg(args, size_t));
118
119 md5_result(ctx, digest);
120 va_end(args);
121 }
122
my_md5_context_size()123 size_t my_md5_context_size()
124 {
125 return EVP_MD_CTX_SIZE;
126 }
127
my_md5_init(void * context)128 void my_md5_init(void *context)
129 {
130 md5_init((EVP_MD_CTX *)context);
131 }
132
my_md5_input(void * context,const uchar * buf,size_t len)133 void my_md5_input(void *context, const uchar *buf, size_t len)
134 {
135 md5_input((EVP_MD_CTX *)context, buf, (uint) len);
136 }
137
my_md5_result(void * context,uchar * digest)138 void my_md5_result(void *context, uchar *digest)
139 {
140 md5_result((EVP_MD_CTX *)context, digest);
141 }
142