digest: add support for OpenSSL 1.1.0
[siplcs.git] / src / core / sipe-digest-openssl.c
blob5f5d8a94577e252d7550264130760229e1d60f7b
1 /**
2 * @file sipe-digest-openssl.c
4 * pidgin-sipe
6 * Copyright (C) 2013-2017 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /**
24 * Digest routines implementation based on OpenSSL
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include <openssl/evp.h>
31 #include <openssl/hmac.h>
32 #if !(defined(HAVE_GSSAPI_ONLY) || defined(HAVE_SSPI))
33 #include <openssl/md4.h>
34 #endif
35 #include <openssl/md5.h>
36 #include <openssl/opensslv.h>
37 #include <openssl/sha.h>
39 #include "glib.h"
41 #include "sipe-digest.h"
43 #if !(defined(HAVE_GSSAPI_ONLY) || defined(HAVE_SSPI))
44 /* One-shot MD4 digest - only used by internal NTLMv2 implementation */
45 void sipe_digest_md4(const guchar *data, gsize length, guchar *digest)
47 MD4(data, length, digest);
49 #endif
51 /* One-shot MD5/SHA-1 digests */
52 void sipe_digest_md5(const guchar *data, gsize length, guchar *digest)
54 MD5(data, length, digest);
57 void sipe_digest_sha1(const guchar *data, gsize length, guchar *digest)
59 SHA1(data, length, digest);
62 /* One-shot HMAC(MD5/SHA-1) digests */
63 void sipe_digest_hmac_md5(const guchar *key, gsize key_length,
64 const guchar *data, gsize data_length,
65 guchar *digest)
67 HMAC(EVP_md5(), key, key_length, data, data_length, digest, NULL);
70 void sipe_digest_hmac_sha1(const guchar *key, gsize key_length,
71 const guchar *data, gsize data_length,
72 guchar *digest)
74 HMAC(EVP_sha1(), key, key_length, data, data_length, digest, NULL);
77 /* Stream HMAC(SHA1) digest for file transfer */
78 gpointer sipe_digest_ft_start(const guchar *sha1_digest)
80 #if OPENSSL_VERSION_NUMBER < 0x10100000L
81 HMAC_CTX *ctx = g_malloc(sizeof(HMAC_CTX));
82 HMAC_CTX_init(ctx);
83 #else
84 /* OpenSSL 1.1.0 or newer */
85 HMAC_CTX *ctx = HMAC_CTX_new();
86 #endif
87 /* used are only the first 16 bytes of the 20 byte SHA1 digest */
88 HMAC_Init_ex(ctx, sha1_digest, 16, EVP_sha1(), NULL);
89 return(ctx);
92 void sipe_digest_ft_update(gpointer context, const guchar *data, gsize length)
94 HMAC_Update(context, data, length);
97 void sipe_digest_ft_end(gpointer context, guchar *digest)
99 HMAC_Final(context, digest, NULL);
102 void sipe_digest_ft_destroy(gpointer context)
104 #if OPENSSL_VERSION_NUMBER < 0x10100000L
105 HMAC_CTX_cleanup(context);
106 g_free(context);
107 #else
108 /* OpenSSL 1.1.0 or newer */
109 HMAC_CTX_free(context);
110 #endif
113 /* Stream digests, e.g. for TLS */
114 gpointer sipe_digest_md5_start(void)
116 MD5_CTX *ctx = g_malloc(sizeof(MD5_CTX));
117 MD5_Init(ctx);
118 return(ctx);
121 void sipe_digest_md5_update(gpointer context, const guchar *data, gsize length)
123 MD5_Update(context, data, length);
126 void sipe_digest_md5_end(gpointer context, guchar *digest)
128 /* save context to ensure this function can be called multiple times */
129 MD5_CTX *orig_ctx = context;
130 MD5_CTX saved_ctx = *orig_ctx;
131 MD5_Final(digest, orig_ctx);
132 *orig_ctx = saved_ctx;
135 void sipe_digest_md5_destroy(gpointer context)
137 g_free(context);
140 gpointer sipe_digest_sha1_start(void)
142 SHA_CTX *ctx = g_malloc(sizeof(SHA_CTX));
143 SHA1_Init(ctx);
144 return(ctx);
147 void sipe_digest_sha1_update(gpointer context, const guchar *data, gsize length)
149 SHA1_Update(context, data, length);
152 void sipe_digest_sha1_end(gpointer context, guchar *digest)
154 /* save context to ensure this function can be called multiple times */
155 SHA_CTX *orig_ctx = context;
156 SHA_CTX saved_ctx = *orig_ctx;
157 SHA1_Final(digest, orig_ctx);
158 *orig_ctx = saved_ctx;
161 void sipe_digest_sha1_destroy(gpointer context)
163 g_free(context);
167 Local Variables:
168 mode: c
169 c-file-style: "bsd"
170 indent-tabs-mode: t
171 tab-width: 8
172 End: