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
24 * Digest routines implementation based on NSS.
25 * Includes: SHA1, MD5, MD4, HMAC_SHA_1, HMAC_MD5
34 #include "sipe-digest.h"
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;
49 context
= PK11_CreateDigestContext(algorithm
);
50 s
= PK11_DigestBegin(context
);
51 s
= PK11_DigestOp(context
, data
, data_length
);
52 s
= PK11_DigestFinal(context
, digest
, &len
, digest_length
);
53 PK11_DestroyContext(context
, PR_TRUE
);
57 sipe_digest_hmac_ctx_create(CK_MECHANISM_TYPE hmacMech
, const guchar
*key
, gsize key_length
)
63 PK11Context
* DigestContext
;
69 slot
= PK11_GetBestSlot(hmacMech
, NULL
);
71 keyItem
.type
= siBuffer
;
72 keyItem
.data
= (unsigned char *)key
;
73 keyItem
.len
= key_length
;
75 SymKey
= PK11_ImportSymKey(slot
, hmacMech
, PK11_OriginUnwrap
, CKA_SIGN
, &keyItem
, NULL
);
77 /* Parameter for crypto context */
78 noParams
.type
= siBuffer
;
82 DigestContext
= PK11_CreateContextBySymKey(hmacMech
, CKA_SIGN
, SymKey
, &noParams
);
84 s
= PK11_DigestBegin(DigestContext
);
86 PK11_FreeSymKey(SymKey
);
92 static void sipe_digest_hmac_ctx_append(PK11Context
* DigestContext
, const guchar
*data
, gsize data_length
)
94 PK11_DigestOp(DigestContext
, data
, data_length
);
97 static void sipe_digest_hmac_ctx_digest(PK11Context
* DigestContext
, guchar
*digest
, gsize digest_length
)
101 PK11_DigestFinal(DigestContext
, digest
, &len
, digest_length
);
104 static void sipe_digest_hmac_ctx_destroy(PK11Context
* DigestContext
)
106 PK11_DestroyContext(DigestContext
, PR_TRUE
);
113 void sipe_digest_md4(const guchar
*data
, gsize length
, guchar
*digest
)
115 /* From Firefox's complementing implementation for NSS.
116 * NSS doesn't include MD4 as weak algorithm
118 md4sum(data
, length
, digest
);
121 void sipe_digest_md5(const guchar
*data
, gsize length
, guchar
*digest
)
123 sipe_digest(SEC_OID_MD5
, data
, length
, digest
, SIPE_DIGEST_MD5_LENGTH
);
126 void sipe_digest_sha1(const guchar
*data
, gsize length
, guchar
*digest
)
128 sipe_digest(SEC_OID_SHA1
, data
, length
, digest
, SIPE_DIGEST_SHA1_LENGTH
);
131 void sipe_digest_hmac_md5(const guchar
*key
, gsize key_length
,
132 const guchar
*data
, gsize data_length
,
137 DigestContext
= sipe_digest_hmac_ctx_create(CKM_MD5_HMAC
, key
, key_length
);
138 sipe_digest_hmac_ctx_append(DigestContext
, data
, data_length
);
139 sipe_digest_hmac_ctx_digest(DigestContext
, digest
, SIPE_DIGEST_HMAC_MD5_LENGTH
);
140 sipe_digest_hmac_ctx_destroy(DigestContext
);
143 /* Stream HMAC(SHA1) digest for file transfer */
144 gpointer
sipe_digest_ft_start(const guchar
*sha1_digest
)
146 /* used only the first 16 bytes of the 20 byte SHA1 digest */
147 return sipe_digest_hmac_ctx_create(CKM_SHA_1_HMAC
, sha1_digest
, 16);
150 void sipe_digest_ft_update(gpointer context
, const guchar
*data
, gsize length
)
152 sipe_digest_hmac_ctx_append(context
, data
, length
);
155 void sipe_digest_ft_end(gpointer context
, guchar
*digest
)
157 sipe_digest_hmac_ctx_digest(context
, digest
, SIPE_DIGEST_FILETRANSFER_LENGTH
);
160 void sipe_digest_ft_destroy(gpointer context
)
162 sipe_digest_hmac_ctx_destroy(context
);