char signedness
[exim.git] / src / src / pdkim / hash.c
CommitLineData
2592e6c0
JH
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
41void
42exim_sha_init(hctx * h, BOOL sha1)
43{
44h->sha1 = sha1;
45h->hashlen = sha1 ? 20 : 32;
46if (h->sha1)
47 SHA1_Init (&h->u.sha1);
48else
49 SHA256_Init(&h->u.sha2);
50}
51
52
53void
e2e3255a 54exim_sha_update(hctx * h, const uschar * data, int len)
2592e6c0
JH
55{
56if (h->sha1)
57 SHA1_Update (&h->u.sha1, data, len);
58else
59 SHA256_Update(&h->u.sha2, data, len);
60}
61
62
63void
64exim_sha_finish(hctx * h, blob * b)
65{
66b->data = store_get(b->len = h->hashlen);
67
68if (h->sha1)
69 SHA1_Final (b->data, &h->u.sha1);
70else
71 SHA256_Final(b->data, &h->u.sha2);
72}
73
74
75
76#elif defined(SHA_GNUTLS)
77/******************************************************************************/
78
79void
80exim_sha_init(hctx * h, BOOL sha1)
81{
82h->sha1 = sha1;
83h->hashlen = sha1 ? 20 : 32;
84gnutls_hash_init(&h->sha, sha1 ? GNUTLS_DIG_SHA1 : GNUTLS_DIG_SHA256);
85}
86
87
88void
e2e3255a 89exim_sha_update(hctx * h, const uschar * data, int len)
2592e6c0
JH
90{
91gnutls_hash(h->sha, data, len);
92}
93
94
95void
96exim_sha_finish(hctx * h, blob * b)
97{
98b->data = store_get(b->len = h->hashlen);
99gnutls_hash_output(h->sha, b->data);
100}
101
102
103
104#elif defined(SHA_GCRYPT)
105/******************************************************************************/
106
107void
108exim_sha_init(hctx * h, BOOL sha1)
109{
110h->sha1 = sha1;
111h->hashlen = sha1 ? 20 : 32;
112gcry_md_open(&h->sha, sha1 ? GCRY_MD_SHA1 : GCRY_MD_SHA256, 0);
113}
114
115
116void
e2e3255a 117exim_sha_update(hctx * h, const uschar * data, int len)
2592e6c0
JH
118{
119gcry_md_write(h->sha, data, len);
120}
121
122
123void
124exim_sha_finish(hctx * h, blob * b)
125{
126b->data = store_get(b->len = h->hashlen);
127memcpy(b->data, gcry_md_read(h->sha, 0), h->hashlen);
128}
129
130
131
132
133#elif defined(SHA_POLARSSL)
134/******************************************************************************/
135
136void
137exim_sha_init(hctx * h, BOOL sha1)
138{
139h->sha1 = sha1;
140h->hashlen = sha1 ? 20 : 32;
141if (h->sha1)
142 sha1_starts(&h->u.sha1);
143else
144 sha2_starts(&h->u.sha2, 0);
145}
146
147
148void
e2e3255a 149exim_sha_update(hctx * h, const uschar * data, int len)
2592e6c0
JH
150{
151if (h->sha1)
152 sha1_update(h->u.sha1, US data, len);
153else
154 sha2_update(h->u.sha2, US data, len);
155}
156
157
158void
159exim_sha_finish(hctx * h, blob * b)
160{
161b->data = store_get(b->len = h->hashlen);
162
163if (h->sha1)
164 sha1_finish(h->u.sha1, b->data);
165else
166 sha2_finish(h->u.sha2, b->data);
167}
168
169#endif
170/******************************************************************************/
171
172/* Common to all library versions */
173int
174exim_sha_hashlen(hctx * h)
175{
176return h->sha1 ? 20 : 32;
177}
178
179
180#endif /*DISABLE_DKIM*/
181/* End of File */