Promote the pdkim variant-implementation sha routines to toplevel
[exim.git] / src / src / hash.c
CommitLineData
2592e6c0 1/*
63af6f3a 2 * Exim - an Internet mail transport agent
2592e6c0
JH
3 *
4 * Copyright (C) 2016 Exim maintainers
5 *
6 * Hash interface functions
7 */
8
63af6f3a 9#include "exim.h"
2592e6c0
JH
10
11#ifndef SUPPORT_TLS
12# error Need SUPPORT_TLS for DKIM
13#endif
14
63af6f3a
JH
15#include "sha_ver.h"
16#include "hash.h"
17
2592e6c0 18
63af6f3a 19#ifdef notdef
2592e6c0
JH
20#ifdef RSA_OPENSSL
21# include <openssl/rsa.h>
22# include <openssl/ssl.h>
23# include <openssl/err.h>
24#elif defined(RSA_GNUTLS)
25# include <gnutls/gnutls.h>
26# include <gnutls/x509.h>
27# ifdef RSA_VERIFY_GNUTLS
28# include <gnutls/abstract.h>
29# endif
30#endif
2592e6c0
JH
31#endif
32
2592e6c0
JH
33
34/******************************************************************************/
35#ifdef SHA_OPENSSL
36
37void
38exim_sha_init(hctx * h, BOOL sha1)
39{
40h->sha1 = sha1;
41h->hashlen = sha1 ? 20 : 32;
42if (h->sha1)
43 SHA1_Init (&h->u.sha1);
44else
45 SHA256_Init(&h->u.sha2);
46}
47
48
49void
e2e3255a 50exim_sha_update(hctx * h, const uschar * data, int len)
2592e6c0
JH
51{
52if (h->sha1)
53 SHA1_Update (&h->u.sha1, data, len);
54else
55 SHA256_Update(&h->u.sha2, data, len);
56}
57
58
59void
60exim_sha_finish(hctx * h, blob * b)
61{
62b->data = store_get(b->len = h->hashlen);
63
64if (h->sha1)
65 SHA1_Final (b->data, &h->u.sha1);
66else
67 SHA256_Final(b->data, &h->u.sha2);
68}
69
70
71
72#elif defined(SHA_GNUTLS)
73/******************************************************************************/
74
75void
76exim_sha_init(hctx * h, BOOL sha1)
77{
78h->sha1 = sha1;
79h->hashlen = sha1 ? 20 : 32;
80gnutls_hash_init(&h->sha, sha1 ? GNUTLS_DIG_SHA1 : GNUTLS_DIG_SHA256);
81}
82
83
84void
e2e3255a 85exim_sha_update(hctx * h, const uschar * data, int len)
2592e6c0
JH
86{
87gnutls_hash(h->sha, data, len);
88}
89
90
91void
92exim_sha_finish(hctx * h, blob * b)
93{
94b->data = store_get(b->len = h->hashlen);
95gnutls_hash_output(h->sha, b->data);
96}
97
98
99
100#elif defined(SHA_GCRYPT)
101/******************************************************************************/
102
103void
104exim_sha_init(hctx * h, BOOL sha1)
105{
106h->sha1 = sha1;
107h->hashlen = sha1 ? 20 : 32;
108gcry_md_open(&h->sha, sha1 ? GCRY_MD_SHA1 : GCRY_MD_SHA256, 0);
109}
110
111
112void
e2e3255a 113exim_sha_update(hctx * h, const uschar * data, int len)
2592e6c0
JH
114{
115gcry_md_write(h->sha, data, len);
116}
117
118
119void
120exim_sha_finish(hctx * h, blob * b)
121{
122b->data = store_get(b->len = h->hashlen);
123memcpy(b->data, gcry_md_read(h->sha, 0), h->hashlen);
124}
125
126
127
128
129#elif defined(SHA_POLARSSL)
130/******************************************************************************/
131
132void
133exim_sha_init(hctx * h, BOOL sha1)
134{
135h->sha1 = sha1;
136h->hashlen = sha1 ? 20 : 32;
137if (h->sha1)
138 sha1_starts(&h->u.sha1);
139else
140 sha2_starts(&h->u.sha2, 0);
141}
142
143
144void
e2e3255a 145exim_sha_update(hctx * h, const uschar * data, int len)
2592e6c0
JH
146{
147if (h->sha1)
148 sha1_update(h->u.sha1, US data, len);
149else
150 sha2_update(h->u.sha2, US data, len);
151}
152
153
154void
155exim_sha_finish(hctx * h, blob * b)
156{
157b->data = store_get(b->len = h->hashlen);
158
159if (h->sha1)
160 sha1_finish(h->u.sha1, b->data);
161else
162 sha2_finish(h->u.sha2, b->data);
163}
164
165#endif
166/******************************************************************************/
167
168/* Common to all library versions */
169int
170exim_sha_hashlen(hctx * h)
171{
172return h->sha1 ? 20 : 32;
173}
174
175
2592e6c0 176/* End of File */