2 * @file sipe-digest-macosx.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 Mac OS X Security Framework
26 * According to the documentation CDSA is a deprecated API since
27 * Mac OS X 10.7. But unfortunately its replacement SecTransform
28 * only supports one-shot hashing.
30 * See also: rdar://<TBD>
32 #include <CommonCrypto/CommonDigest.h>
33 #include <CommonCrypto/CommonHMAC.h>
37 #include "sipe-digest.h"
39 /* One-shot MD5/SHA-1 digests */
40 void sipe_digest_md5(const guchar
*data
, gsize length
, guchar
*digest
)
42 CC_MD5(data
, length
, digest
);
45 void sipe_digest_sha1(const guchar
*data
, gsize length
, guchar
*digest
)
47 CC_SHA1(data
, length
, digest
);
50 /* One-shot HMAC(MD5/SHA-1) digests */
51 void sipe_digest_hmac_md5(const guchar
*key
, gsize key_length
,
52 const guchar
*data
, gsize data_length
,
55 CCHmac(kCCHmacAlgMD5
, key
, key_length
, data
, data_length
, digest
);
58 void sipe_digest_hmac_sha1(const guchar
*key
, gsize key_length
,
59 const guchar
*data
, gsize data_length
,
62 CCHmac(kCCHmacAlgSHA1
, key
, key_length
, data
, data_length
, digest
);
65 /* Stream HMAC(SHA1) digest for file transfer */
66 gpointer
sipe_digest_ft_start(const guchar
*sha1_digest
)
68 CCHmacContext
*ctx
= g_malloc(sizeof(CCHmacContext
));
69 /* used are only the first 16 bytes of the 20 byte SHA1 digest */
70 CCHmacInit(ctx
, kCCHmacAlgSHA1
, sha1_digest
, 16);
74 void sipe_digest_ft_update(gpointer context
, const guchar
*data
, gsize length
)
76 CCHmacUpdate(context
, data
, length
);
79 void sipe_digest_ft_end(gpointer context
, guchar
*digest
)
81 CCHmacFinal(context
, digest
);
84 void sipe_digest_ft_destroy(gpointer context
)
89 /* Stream digests, e.g. for TLS */
90 gpointer
sipe_digest_md5_start(void)
92 CC_MD5_CTX
*ctx
= g_malloc(sizeof(CC_MD5_CTX
));
97 void sipe_digest_md5_update(gpointer context
, const guchar
*data
, gsize length
)
99 CC_MD5_Update(context
, data
, length
);
102 void sipe_digest_md5_end(gpointer context
, guchar
*digest
)
104 /* save context to ensure this function can be called multiple times */
105 CC_MD5_CTX
*orig_ctx
= context
;
106 CC_MD5_CTX saved_ctx
= *orig_ctx
;
107 CC_MD5_Final(digest
, orig_ctx
);
108 *orig_ctx
= saved_ctx
;
111 void sipe_digest_md5_destroy(gpointer context
)
116 gpointer
sipe_digest_sha1_start(void)
118 CC_SHA1_CTX
*ctx
= g_malloc(sizeof(CC_SHA1_CTX
));
123 void sipe_digest_sha1_update(gpointer context
, const guchar
*data
, gsize length
)
125 CC_SHA1_Update(context
, data
, length
);
128 void sipe_digest_sha1_end(gpointer context
, guchar
*digest
)
130 /* save context to ensure this function can be called multiple times */
131 CC_SHA1_CTX
*orig_ctx
= context
;
132 CC_SHA1_CTX saved_ctx
= *orig_ctx
;
133 CC_SHA1_Final(digest
, orig_ctx
);
134 *orig_ctx
= saved_ctx
;
137 void sipe_digest_sha1_destroy(gpointer context
)