2 * @file sipe-digest-nss.c
6 * Copyright (C) 2011 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010 pier11 <pier11@operamail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Digest routines implementation based on NSS.
26 * Includes: SHA1, MD5, HMAC_SHA_1, HMAC_MD5
33 * Work around a compiler error in NSS 3.13.x. Let's hope they fix it for
34 * 3.14.x. See also: https://bugzilla.mozilla.org/show_bug.cgi?id=702090
36 #if (NSS_VMAJOR == 3) && (NSS_VMINOR == 13)
37 #define __GNUC_MINOR __GNUC_MINOR__
41 #include "sipe-digest.h"
46 static PK11Context
*sipe_digest_ctx_create(const SECOidTag algorithm
)
48 PK11Context
*context
= PK11_CreateDigestContext(algorithm
);
49 PK11_DigestBegin(context
);
54 sipe_digest_hmac_ctx_create(CK_MECHANISM_TYPE hmacMech
, const guchar
*key
, gsize key_length
)
60 PK11Context
* DigestContext
;
63 slot
= PK11_GetBestSlot(hmacMech
, NULL
);
65 keyItem
.type
= siBuffer
;
66 keyItem
.data
= (unsigned char *)key
;
67 keyItem
.len
= key_length
;
69 SymKey
= PK11_ImportSymKey(slot
, hmacMech
, PK11_OriginUnwrap
, CKA_SIGN
, &keyItem
, NULL
);
71 /* Parameter for crypto context */
72 noParams
.type
= siBuffer
;
76 DigestContext
= PK11_CreateContextBySymKey(hmacMech
, CKA_SIGN
, SymKey
, &noParams
);
78 PK11_DigestBegin(DigestContext
);
80 PK11_FreeSymKey(SymKey
);
86 static void sipe_digest_ctx_append(PK11Context
* DigestContext
, const guchar
*data
, gsize data_length
)
88 PK11_DigestOp(DigestContext
, data
, data_length
);
91 static void sipe_digest_ctx_digest(PK11Context
* DigestContext
, guchar
*digest
, gsize digest_length
)
95 PK11_DigestFinal(DigestContext
, digest
, &len
, digest_length
);
98 static void sipe_digest_ctx_destroy(PK11Context
* DigestContext
)
100 PK11_DestroyContext(DigestContext
, PR_TRUE
);
103 static void sipe_digest(const SECOidTag algorithm
,
104 const guchar
*data
, gsize data_length
,
105 guchar
*digest
, gsize digest_length
)
109 DigestContext
= sipe_digest_ctx_create(algorithm
);
110 sipe_digest_ctx_append(DigestContext
, data
, data_length
);
111 sipe_digest_ctx_digest(DigestContext
, digest
, digest_length
);
112 sipe_digest_ctx_destroy(DigestContext
);
115 static void sipe_digest_hmac(CK_MECHANISM_TYPE hmacMech
,
116 const guchar
*key
, gsize key_length
,
117 const guchar
*data
, gsize data_length
,
118 guchar
*digest
, gsize digest_length
)
122 DigestContext
= sipe_digest_hmac_ctx_create(hmacMech
, key
, key_length
);
123 sipe_digest_ctx_append(DigestContext
, data
, data_length
);
124 sipe_digest_ctx_digest(DigestContext
, digest
, digest_length
);
125 sipe_digest_ctx_destroy(DigestContext
);
131 void sipe_digest_md5(const guchar
*data
, gsize length
, guchar
*digest
)
133 sipe_digest(SEC_OID_MD5
, data
, length
, digest
, SIPE_DIGEST_MD5_LENGTH
);
136 void sipe_digest_sha1(const guchar
*data
, gsize length
, guchar
*digest
)
138 sipe_digest(SEC_OID_SHA1
, data
, length
, digest
, SIPE_DIGEST_SHA1_LENGTH
);
141 void sipe_digest_hmac_md5(const guchar
*key
, gsize key_length
,
142 const guchar
*data
, gsize data_length
,
145 sipe_digest_hmac(CKM_MD5_HMAC
, key
, key_length
, data
, data_length
, digest
, SIPE_DIGEST_HMAC_MD5_LENGTH
);
148 void sipe_digest_hmac_sha1(const guchar
*key
, gsize key_length
,
149 const guchar
*data
, gsize data_length
,
152 sipe_digest_hmac(CKM_SHA_1_HMAC
, key
, key_length
, data
, data_length
, digest
, SIPE_DIGEST_HMAC_SHA1_LENGTH
);
155 /* Stream HMAC(SHA1) digest for file transfer */
156 gpointer
sipe_digest_ft_start(const guchar
*sha1_digest
)
158 /* used only the first 16 bytes of the 20 byte SHA1 digest */
159 return sipe_digest_hmac_ctx_create(CKM_SHA_1_HMAC
, sha1_digest
, 16);
162 void sipe_digest_ft_update(gpointer context
, const guchar
*data
, gsize length
)
164 sipe_digest_ctx_append(context
, data
, length
);
167 void sipe_digest_ft_end(gpointer context
, guchar
*digest
)
169 sipe_digest_ctx_digest(context
, digest
, SIPE_DIGEST_FILETRANSFER_LENGTH
);
172 void sipe_digest_ft_destroy(gpointer context
)
174 sipe_digest_ctx_destroy(context
);
177 /* Stream digests, e.g. for TLS */
178 gpointer
sipe_digest_md5_start(void)
180 return sipe_digest_ctx_create(SEC_OID_MD5
);
183 void sipe_digest_md5_update(gpointer context
, const guchar
*data
, gsize length
)
185 sipe_digest_ctx_append(context
, data
, length
);
188 void sipe_digest_md5_end(gpointer context
, guchar
*digest
)
190 unsigned int saved_length
;
191 /* save context to ensure this function can be called multiple times */
192 guchar
*saved
= PK11_SaveContextAlloc(context
,
196 sipe_digest_ctx_digest(context
, digest
, SIPE_DIGEST_MD5_LENGTH
);
197 PK11_RestoreContext(context
, saved
, saved_length
);
201 void sipe_digest_md5_destroy(gpointer context
)
203 sipe_digest_ctx_destroy(context
);
206 gpointer
sipe_digest_sha1_start(void)
208 return sipe_digest_ctx_create(SEC_OID_SHA1
);
211 void sipe_digest_sha1_update(gpointer context
, const guchar
*data
, gsize length
)
213 sipe_digest_ctx_append(context
, data
, length
);
216 void sipe_digest_sha1_end(gpointer context
, guchar
*digest
)
218 unsigned int saved_length
;
219 /* save context to ensure this function can be called multiple times */
220 guchar
*saved
= PK11_SaveContextAlloc(context
,
224 sipe_digest_ctx_digest(context
, digest
, SIPE_DIGEST_SHA1_LENGTH
);
225 PK11_RestoreContext(context
, saved
, saved_length
);
229 void sipe_digest_sha1_destroy(gpointer context
)
231 sipe_digest_ctx_destroy(context
);