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