2 * @file sipe-digest-openssl.c
6 * Copyright (C) 2013 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
26 #include <openssl/evp.h>
27 #include <openssl/hmac.h>
28 #include <openssl/md5.h>
29 #include <openssl/sha.h>
33 #include "sipe-digest.h"
35 /* One-shot MD5/SHA-1 digests */
36 void sipe_digest_md5(const guchar
*data
, gsize length
, guchar
*digest
)
38 MD5(data
, length
, digest
);
41 void sipe_digest_sha1(const guchar
*data
, gsize length
, guchar
*digest
)
43 SHA1(data
, length
, digest
);
46 /* One-shot HMAC(MD5/SHA-1) digests */
47 void sipe_digest_hmac_md5(const guchar
*key
, gsize key_length
,
48 const guchar
*data
, gsize data_length
,
51 HMAC(EVP_md5(), key
, key_length
, data
, data_length
, digest
, NULL
);
54 void sipe_digest_hmac_sha1(const guchar
*key
, gsize key_length
,
55 const guchar
*data
, gsize data_length
,
58 HMAC(EVP_sha1(), key
, key_length
, data
, data_length
, digest
, NULL
);
61 /* Stream HMAC(SHA1) digest for file transfer */
62 gpointer
sipe_digest_ft_start(const guchar
*sha1_digest
)
64 HMAC_CTX
*ctx
= g_malloc(sizeof(HMAC_CTX
));
66 /* used are only the first 16 bytes of the 20 byte SHA1 digest */
67 HMAC_Init(ctx
, sha1_digest
, 16, EVP_sha1());
71 void sipe_digest_ft_update(gpointer context
, const guchar
*data
, gsize length
)
73 HMAC_Update(context
, data
, length
);
76 void sipe_digest_ft_end(gpointer context
, guchar
*digest
)
78 HMAC_Final(context
, digest
, NULL
);
81 void sipe_digest_ft_destroy(gpointer context
)
83 HMAC_CTX_cleanup(context
);
87 /* Stream digests, e.g. for TLS */
88 gpointer
sipe_digest_md5_start(void)
90 MD5_CTX
*ctx
= g_malloc(sizeof(MD5_CTX
));
95 void sipe_digest_md5_update(gpointer context
, const guchar
*data
, gsize length
)
97 MD5_Update(context
, data
, length
);
100 void sipe_digest_md5_end(gpointer context
, guchar
*digest
)
102 /* save context to ensure this function can be called multiple times */
103 MD5_CTX
*orig_ctx
= context
;
104 MD5_CTX saved_ctx
= *orig_ctx
;
105 MD5_Final(digest
, orig_ctx
);
106 *orig_ctx
= saved_ctx
;
109 void sipe_digest_md5_destroy(gpointer context
)
114 gpointer
sipe_digest_sha1_start(void)
116 SHA_CTX
*ctx
= g_malloc(sizeof(SHA_CTX
));
121 void sipe_digest_sha1_update(gpointer context
, const guchar
*data
, gsize length
)
123 SHA1_Update(context
, data
, length
);
126 void sipe_digest_sha1_end(gpointer context
, guchar
*digest
)
128 /* save context to ensure this function can be called multiple times */
129 SHA_CTX
*orig_ctx
= context
;
130 SHA_CTX saved_ctx
= *orig_ctx
;
131 SHA1_Final(digest
, orig_ctx
);
132 *orig_ctx
= saved_ctx
;
135 void sipe_digest_sha1_destroy(gpointer context
)