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