Fix #3198585: Extra line breaks
[siplcs.git] / src / core / sipe-digest.c
blobe06e38f681d34edbc38cd895681461bba3734440
1 /**
2 * @file sipe-digest.c
4 * pidgin-sipe
6 * Copyright (C) 2010 pier11 <pier11@operamail.com>
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 NSS.
25 * Includes: SHA1, MD5, MD4, HMAC_SHA_1, HMAC_MD5
28 #include "glib.h"
30 #include "nss.h"
31 #include "pk11pub.h"
32 #include "md4.h"
34 #include "sipe-digest.h"
37 /* PRIVATE methods */
39 static void sipe_digest(const SECOidTag algorithm,
40 const guchar *data, gsize data_length,
41 guchar *digest, gsize digest_length)
43 PK11Context *context = 0;
44 SECStatus s;
45 unsigned int len;
47 context = PK11_CreateDigestContext(algorithm);
48 s = PK11_DigestBegin(context);
49 s = PK11_DigestOp(context, data, data_length);
50 s = PK11_DigestFinal(context, digest, &len, digest_length);
51 PK11_DestroyContext(context, PR_TRUE);
54 static PK11Context*
55 sipe_digest_hmac_ctx_create(CK_MECHANISM_TYPE hmacMech, const guchar *key, gsize key_length)
57 PK11SlotInfo* slot;
58 SECItem keyItem;
59 SECItem noParams;
60 PK11SymKey* SymKey;
61 PK11Context* DigestContext;
62 SECStatus s;
64 /* For key */
65 slot = PK11_GetBestSlot(hmacMech, NULL);
67 keyItem.type = siBuffer;
68 keyItem.data = (unsigned char *)key;
69 keyItem.len = key_length;
71 SymKey = PK11_ImportSymKey(slot, hmacMech, PK11_OriginUnwrap, CKA_SIGN, &keyItem, NULL);
73 /* Parameter for crypto context */
74 noParams.type = siBuffer;
75 noParams.data = NULL;
76 noParams.len = 0;
78 DigestContext = PK11_CreateContextBySymKey(hmacMech, CKA_SIGN, SymKey, &noParams);
80 s = PK11_DigestBegin(DigestContext);
82 PK11_FreeSymKey(SymKey);
83 PK11_FreeSlot(slot);
85 return DigestContext;
88 static void sipe_digest_hmac_ctx_append(PK11Context* DigestContext, const guchar *data, gsize data_length)
90 PK11_DigestOp(DigestContext, data, data_length);
93 static void sipe_digest_hmac_ctx_digest(PK11Context* DigestContext, guchar *digest, gsize digest_length)
95 unsigned int len;
97 PK11_DigestFinal(DigestContext, digest, &len, digest_length);
100 static void sipe_digest_hmac_ctx_destroy(PK11Context* DigestContext)
102 PK11_DestroyContext(DigestContext, PR_TRUE);
107 /* PUBLIC methods */
109 void sipe_digest_md4(const guchar *data, gsize length, guchar *digest)
111 /* From Firefox's complementing implementation for NSS.
112 * NSS doesn't include MD4 as weak algorithm
114 md4sum(data, length, digest);
117 void sipe_digest_md5(const guchar *data, gsize length, guchar *digest)
119 sipe_digest(SEC_OID_MD5, data, length, digest, SIPE_DIGEST_MD5_LENGTH);
122 void sipe_digest_sha1(const guchar *data, gsize length, guchar *digest)
124 sipe_digest(SEC_OID_SHA1, data, length, digest, SIPE_DIGEST_SHA1_LENGTH);
127 void sipe_digest_hmac_md5(const guchar *key, gsize key_length,
128 const guchar *data, gsize data_length,
129 guchar *digest)
131 void *DigestContext;
133 DigestContext = sipe_digest_hmac_ctx_create(CKM_MD5_HMAC, key, key_length);
134 sipe_digest_hmac_ctx_append(DigestContext, data, data_length);
135 sipe_digest_hmac_ctx_digest(DigestContext, digest, SIPE_DIGEST_HMAC_MD5_LENGTH);
136 sipe_digest_hmac_ctx_destroy(DigestContext);
139 /* Stream HMAC(SHA1) digest for file transfer */
140 gpointer sipe_digest_ft_start(const guchar *sha1_digest)
142 /* used only the first 16 bytes of the 20 byte SHA1 digest */
143 return sipe_digest_hmac_ctx_create(CKM_SHA_1_HMAC, sha1_digest, 16);
146 void sipe_digest_ft_update(gpointer context, const guchar *data, gsize length)
148 sipe_digest_hmac_ctx_append(context, data, length);
151 void sipe_digest_ft_end(gpointer context, guchar *digest)
153 sipe_digest_hmac_ctx_digest(context, digest, SIPE_DIGEST_FILETRANSFER_LENGTH);
156 void sipe_digest_ft_destroy(gpointer context)
158 sipe_digest_hmac_ctx_destroy(context);
162 Local Variables:
163 mode: c
164 c-file-style: "bsd"
165 indent-tabs-mode: t
166 tab-width: 8
167 End: