2 * PDKIM - a RFC4871 (DKIM) implementation
4 * Copyright (C) 2016 Exim maintainers
6 * Hash interface functions
11 #ifndef DISABLE_DKIM /* entire file */
14 # error Need SUPPORT_TLS for DKIM
17 #include "crypt_ver.h"
20 # include <openssl/rsa.h>
21 # include <openssl/ssl.h>
22 # include <openssl/err.h>
23 #elif defined(RSA_GNUTLS)
24 # include <gnutls/gnutls.h>
25 # include <gnutls/x509.h>
26 # ifdef RSA_VERIFY_GNUTLS
27 # include <gnutls/abstract.h>
32 # include <gnutls/crypto.h>
38 /******************************************************************************/
42 exim_sha_init(hctx * h, BOOL sha1)
45 h->hashlen = sha1 ? 20 : 32;
47 SHA1_Init (&h->u.sha1);
49 SHA256_Init(&h->u.sha2);
54 exim_sha_update(hctx * h, const char * data, int len)
57 SHA1_Update (&h->u.sha1, data, len);
59 SHA256_Update(&h->u.sha2, data, len);
64 exim_sha_finish(hctx * h, blob * b)
66 b->data = store_get(b->len = h->hashlen);
69 SHA1_Final (b->data, &h->u.sha1);
71 SHA256_Final(b->data, &h->u.sha2);
76 #elif defined(SHA_GNUTLS)
77 /******************************************************************************/
80 exim_sha_init(hctx * h, BOOL sha1)
83 h->hashlen = sha1 ? 20 : 32;
84 gnutls_hash_init(&h->sha, sha1 ? GNUTLS_DIG_SHA1 : GNUTLS_DIG_SHA256);
89 exim_sha_update(hctx * h, const char * data, int len)
91 gnutls_hash(h->sha, data, len);
96 exim_sha_finish(hctx * h, blob * b)
98 b->data = store_get(b->len = h->hashlen);
99 gnutls_hash_output(h->sha, b->data);
104 #elif defined(SHA_GCRYPT)
105 /******************************************************************************/
108 exim_sha_init(hctx * h, BOOL sha1)
111 h->hashlen = sha1 ? 20 : 32;
112 gcry_md_open(&h->sha, sha1 ? GCRY_MD_SHA1 : GCRY_MD_SHA256, 0);
117 exim_sha_update(hctx * h, const char * data, int len)
119 gcry_md_write(h->sha, data, len);
124 exim_sha_finish(hctx * h, blob * b)
126 b->data = store_get(b->len = h->hashlen);
127 memcpy(b->data, gcry_md_read(h->sha, 0), h->hashlen);
133 #elif defined(SHA_POLARSSL)
134 /******************************************************************************/
137 exim_sha_init(hctx * h, BOOL sha1)
140 h->hashlen = sha1 ? 20 : 32;
142 sha1_starts(&h->u.sha1);
144 sha2_starts(&h->u.sha2, 0);
149 exim_sha_update(hctx * h, const char * data, int len)
152 sha1_update(h->u.sha1, US data, len);
154 sha2_update(h->u.sha2, US data, len);
159 exim_sha_finish(hctx * h, blob * b)
161 b->data = store_get(b->len = h->hashlen);
164 sha1_finish(h->u.sha1, b->data);
166 sha2_finish(h->u.sha2, b->data);
170 /******************************************************************************/
172 /* Common to all library versions */
174 exim_sha_hashlen(hctx * h)
176 return h->sha1 ? 20 : 32;
180 #endif /*DISABLE_DKIM*/