2 * @file sipe-digest-openssl.c
6 * Copyright (C) 2013-2016 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
24 * Digest routines implementation based on OpenSSL
30 #include <openssl/evp.h>
31 #include <openssl/hmac.h>
32 #if !(defined(HAVE_GSSAPI_ONLY) || defined(HAVE_SSPI))
33 #include <openssl/md4.h>
35 #include <openssl/md5.h>
36 #include <openssl/sha.h>
40 #include "sipe-digest.h"
42 #if !(defined(HAVE_GSSAPI_ONLY) || defined(HAVE_SSPI))
43 /* One-shot MD4 digest - only used by internal NTLMv2 implementation */
44 void sipe_digest_md4(const guchar
*data
, gsize length
, guchar
*digest
)
46 MD4(data
, length
, digest
);
50 /* One-shot MD5/SHA-1 digests */
51 void sipe_digest_md5(const guchar
*data
, gsize length
, guchar
*digest
)
53 MD5(data
, length
, digest
);
56 void sipe_digest_sha1(const guchar
*data
, gsize length
, guchar
*digest
)
58 SHA1(data
, length
, digest
);
61 /* One-shot HMAC(MD5/SHA-1) digests */
62 void sipe_digest_hmac_md5(const guchar
*key
, gsize key_length
,
63 const guchar
*data
, gsize data_length
,
66 HMAC(EVP_md5(), key
, key_length
, data
, data_length
, digest
, NULL
);
69 void sipe_digest_hmac_sha1(const guchar
*key
, gsize key_length
,
70 const guchar
*data
, gsize data_length
,
73 HMAC(EVP_sha1(), key
, key_length
, data
, data_length
, digest
, NULL
);
76 /* Stream HMAC(SHA1) digest for file transfer */
77 gpointer
sipe_digest_ft_start(const guchar
*sha1_digest
)
79 HMAC_CTX
*ctx
= g_malloc(sizeof(HMAC_CTX
));
81 /* used are only the first 16 bytes of the 20 byte SHA1 digest */
82 HMAC_Init(ctx
, sha1_digest
, 16, EVP_sha1());
86 void sipe_digest_ft_update(gpointer context
, const guchar
*data
, gsize length
)
88 HMAC_Update(context
, data
, length
);
91 void sipe_digest_ft_end(gpointer context
, guchar
*digest
)
93 HMAC_Final(context
, digest
, NULL
);
96 void sipe_digest_ft_destroy(gpointer context
)
98 HMAC_CTX_cleanup(context
);
102 /* Stream digests, e.g. for TLS */
103 gpointer
sipe_digest_md5_start(void)
105 MD5_CTX
*ctx
= g_malloc(sizeof(MD5_CTX
));
110 void sipe_digest_md5_update(gpointer context
, const guchar
*data
, gsize length
)
112 MD5_Update(context
, data
, length
);
115 void sipe_digest_md5_end(gpointer context
, guchar
*digest
)
117 /* save context to ensure this function can be called multiple times */
118 MD5_CTX
*orig_ctx
= context
;
119 MD5_CTX saved_ctx
= *orig_ctx
;
120 MD5_Final(digest
, orig_ctx
);
121 *orig_ctx
= saved_ctx
;
124 void sipe_digest_md5_destroy(gpointer context
)
129 gpointer
sipe_digest_sha1_start(void)
131 SHA_CTX
*ctx
= g_malloc(sizeof(SHA_CTX
));
136 void sipe_digest_sha1_update(gpointer context
, const guchar
*data
, gsize length
)
138 SHA1_Update(context
, data
, length
);
141 void sipe_digest_sha1_end(gpointer context
, guchar
*digest
)
143 /* save context to ensure this function can be called multiple times */
144 SHA_CTX
*orig_ctx
= context
;
145 SHA_CTX saved_ctx
= *orig_ctx
;
146 SHA1_Final(digest
, orig_ctx
);
147 *orig_ctx
= saved_ctx
;
150 void sipe_digest_sha1_destroy(gpointer context
)