52
48
# define MD5_CTX gcry_md_hd_t
53
49
# define SHA_CTX gcry_md_hd_t
54
50
# define SHA256_CTX gcry_md_hd_t
55
#elif defined(USE_DARWINSSL)
56
/* For darwinssl: CommonCrypto has the functions we need. The library's
57
headers are even backward-compatible with OpenSSL's headers as long as
58
we define COMMON_DIGEST_FOR_OPENSSL first.
51
#elif defined(USE_NSS)
54
# define MD5_CTX void *
55
# define SHA_CTX void *
56
# define SHA256_CTX void *
57
# ifdef HAVE_NSS_INITCONTEXT
58
static NSSInitContext *nss_context;
60
#elif defined(__MAC_10_4) || defined(__IPHONE_5_0)
61
/* For Apple operating systems: CommonCrypto has the functions we need.
62
The library's headers are even backward-compatible with OpenSSL's
63
headers as long as we define COMMON_DIGEST_FOR_OPENSSL first.
60
65
These functions are available on Tiger and later, as well as iOS 5.0
61
66
and later. If you're building for an older cat, well, sorry. */
219
230
gcry_md_close(*ctx);
222
#elif defined(_WIN32)
233
#elif defined(USE_NSS)
235
static int nss_hash_init(void **pctx, SECOidTag hash_alg)
239
/* we have to initialize NSS if not initialized alraedy */
240
#ifdef HAVE_NSS_INITCONTEXT
241
if(!NSS_IsInitialized() && !nss_context) {
242
static NSSInitParameters params;
243
params.length = sizeof params;
244
nss_context = NSS_InitContext("", "", "", "", ¶ms, NSS_INIT_READONLY
245
| NSS_INIT_NOCERTDB | NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN
246
| NSS_INIT_NOROOTINIT | NSS_INIT_OPTIMIZESPACE | NSS_INIT_PK11RELOAD);
250
ctx = PK11_CreateDigestContext(hash_alg);
252
return /* failure */ 0;
254
if(PK11_DigestBegin(ctx) != SECSuccess) {
255
PK11_DestroyContext(ctx, PR_TRUE);
256
return /* failure */ 0;
260
return /* success */ 1;
263
static void nss_hash_final(void **pctx, unsigned char *out, unsigned int len)
265
PK11Context *ctx = *pctx;
267
PK11_DigestFinal(ctx, out, &outlen, len);
268
PK11_DestroyContext(ctx, PR_TRUE);
271
static int MD5_Init(MD5_CTX *pctx)
273
return nss_hash_init(pctx, SEC_OID_MD5);
276
static void MD5_Update(MD5_CTX *pctx,
277
const unsigned char *input,
278
unsigned int input_len)
280
PK11_DigestOp(*pctx, input, input_len);
283
static void MD5_Final(unsigned char digest[16], MD5_CTX *pctx)
285
nss_hash_final(pctx, digest, 16);
288
static int SHA1_Init(SHA_CTX *pctx)
290
return nss_hash_init(pctx, SEC_OID_SHA1);
293
static void SHA1_Update(SHA_CTX *pctx,
294
const unsigned char *input,
295
unsigned int input_len)
297
PK11_DigestOp(*pctx, input, input_len);
300
static void SHA1_Final(unsigned char digest[20], SHA_CTX *pctx)
302
nss_hash_final(pctx, digest, 20);
305
static int SHA256_Init(SHA256_CTX *pctx)
307
return nss_hash_init(pctx, SEC_OID_SHA256);
310
static void SHA256_Update(SHA256_CTX *pctx,
311
const unsigned char *input,
312
unsigned int input_len)
314
PK11_DigestOp(*pctx, input, input_len);
317
static void SHA256_Final(unsigned char digest[32], SHA256_CTX *pctx)
319
nss_hash_final(pctx, digest, 32);
322
#elif defined(_WIN32) && !defined(USE_SSLEAY)
224
324
static void win32_crypto_final(struct win32_crypto_hash *ctx,
225
325
unsigned char *digest,
235
335
CryptReleaseContext(ctx->hCryptProv, 0);
238
static void MD5_Init(MD5_CTX *ctx)
338
static int MD5_Init(MD5_CTX *ctx)
240
340
if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL,
241
341
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
242
342
CryptCreateHash(ctx->hCryptProv, CALG_MD5, 0, 0, &ctx->hHash);
246
347
static void MD5_Update(MD5_CTX *ctx,
255
356
win32_crypto_final(ctx, digest, 16);
258
static void SHA1_Init(SHA_CTX *ctx)
359
static int SHA1_Init(SHA_CTX *ctx)
260
361
if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL,
261
362
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
262
363
CryptCreateHash(ctx->hCryptProv, CALG_SHA1, 0, 0, &ctx->hHash);
266
368
static void SHA1_Update(SHA_CTX *ctx,
275
377
win32_crypto_final(ctx, digest, 20);
278
static void SHA256_Init(SHA256_CTX *ctx)
380
static int SHA256_Init(SHA256_CTX *ctx)
280
382
if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL,
281
383
PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
282
384
CryptCreateHash(ctx->hCryptProv, CALG_SHA_256, 0, 0, &ctx->hHash);
286
389
static void SHA256_Update(SHA256_CTX *ctx,