summaryrefslogtreecommitdiffstats
path: root/lib/isc/md.c
blob: a8c63129e2f92458d4987b9e55ca005790596cc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

#include <stdio.h>

#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/opensslv.h>

#include <isc/md.h>
#include <isc/util.h>

#include "openssl_shim.h"

isc_md_t *
isc_md_new(void) {
	isc_md_t *md = EVP_MD_CTX_new();
	RUNTIME_CHECK(md != NULL);
	return (md);
}

void
isc_md_free(isc_md_t *md) {
	if (md == NULL) {
		return;
	}

	EVP_MD_CTX_free(md);
}

isc_result_t
isc_md_init(isc_md_t *md, const isc_md_type_t *md_type) {
	REQUIRE(md != NULL);

	if (md_type == NULL) {
		return (ISC_R_NOTIMPLEMENTED);
	}

	if (EVP_DigestInit_ex(md, md_type, NULL) != 1) {
		ERR_clear_error();
		return (ISC_R_CRYPTOFAILURE);
	}

	return (ISC_R_SUCCESS);
}

isc_result_t
isc_md_reset(isc_md_t *md) {
	REQUIRE(md != NULL);

	if (EVP_MD_CTX_reset(md) != 1) {
		ERR_clear_error();
		return (ISC_R_CRYPTOFAILURE);
	}

	return (ISC_R_SUCCESS);
}

isc_result_t
isc_md_update(isc_md_t *md, const unsigned char *buf, const size_t len) {
	REQUIRE(md != NULL);

	if (buf == NULL || len == 0) {
		return (ISC_R_SUCCESS);
	}

	if (EVP_DigestUpdate(md, buf, len) != 1) {
		ERR_clear_error();
		return (ISC_R_CRYPTOFAILURE);
	}

	return (ISC_R_SUCCESS);
}

isc_result_t
isc_md_final(isc_md_t *md, unsigned char *digest, unsigned int *digestlen) {
	REQUIRE(md != NULL);
	REQUIRE(digest != NULL);

	if (EVP_DigestFinal_ex(md, digest, digestlen) != 1) {
		ERR_clear_error();
		return (ISC_R_CRYPTOFAILURE);
	}

	return (ISC_R_SUCCESS);
}

const isc_md_type_t *
isc_md_get_md_type(isc_md_t *md) {
	REQUIRE(md != NULL);

	return (EVP_MD_CTX_get0_md(md));
}

size_t
isc_md_get_size(isc_md_t *md) {
	REQUIRE(md != NULL);

	return (EVP_MD_CTX_size(md));
}

size_t
isc_md_get_block_size(isc_md_t *md) {
	REQUIRE(md != NULL);

	return (EVP_MD_CTX_block_size(md));
}

size_t
isc_md_type_get_size(const isc_md_type_t *md_type) {
	STATIC_ASSERT(ISC_MAX_MD_SIZE >= EVP_MAX_MD_SIZE,
		      "Change ISC_MAX_MD_SIZE to be greater than or equal to "
		      "EVP_MAX_MD_SIZE");
	if (md_type != NULL) {
		return ((size_t)EVP_MD_size(md_type));
	}

	return (ISC_MAX_MD_SIZE);
}

size_t
isc_md_type_get_block_size(const isc_md_type_t *md_type) {
	STATIC_ASSERT(ISC_MAX_MD_SIZE >= EVP_MAX_MD_SIZE,
		      "Change ISC_MAX_MD_SIZE to be greater than or equal to "
		      "EVP_MAX_MD_SIZE");
	if (md_type != NULL) {
		return ((size_t)EVP_MD_block_size(md_type));
	}

	return (ISC_MAX_MD_SIZE);
}

isc_result_t
isc_md(const isc_md_type_t *md_type, const unsigned char *buf, const size_t len,
       unsigned char *digest, unsigned int *digestlen) {
	isc_md_t *md;
	isc_result_t res;

	md = isc_md_new();

	res = isc_md_init(md, md_type);
	if (res != ISC_R_SUCCESS) {
		goto end;
	}

	res = isc_md_update(md, buf, len);
	if (res != ISC_R_SUCCESS) {
		goto end;
	}

	res = isc_md_final(md, digest, digestlen);
	if (res != ISC_R_SUCCESS) {
		goto end;
	}
end:
	isc_md_free(md);

	return (res);
}

#define md_register_algorithm(alg)                        \
	const isc_md_type_t *isc__md_##alg(void) {        \
		const isc_md_type_t *value = EVP_##alg(); \
		if (value == NULL) {                      \
			ERR_clear_error();                \
		}                                         \
		return (value);                           \
	}

md_register_algorithm(md5);
md_register_algorithm(sha1);
md_register_algorithm(sha224);
md_register_algorithm(sha256);
md_register_algorithm(sha384);
md_register_algorithm(sha512);