Coverage Report

Created: 2026-08-13 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/isc/md.c
Line
Count
Source
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
#include <stdio.h>
15
16
#include <openssl/err.h>
17
#include <openssl/evp.h>
18
#include <openssl/opensslv.h>
19
20
#include <isc/md.h>
21
#include <isc/ossl_wrap.h>
22
#include <isc/util.h>
23
24
#include "openssl_shim.h"
25
26
isc_md_t *
27
0
isc_md_new(void) {
28
0
  isc_md_t *md = EVP_MD_CTX_new();
29
0
  RUNTIME_CHECK(md != NULL);
30
0
  return md;
31
0
}
32
33
void
34
0
isc_md_free(isc_md_t *md) {
35
0
  if (md == NULL) {
36
0
    return;
37
0
  }
38
39
0
  EVP_MD_CTX_free(md);
40
0
}
41
42
isc_result_t
43
0
isc_md_init(isc_md_t *md, isc_md_type_t type) {
44
0
  EVP_MD *evp;
45
46
0
  REQUIRE(md != NULL);
47
0
  REQUIRE(type < ISC_MD_MAX);
48
49
0
  evp = isc__crypto_md[type];
50
0
  if (evp == NULL) {
51
0
    return ISC_R_NOTIMPLEMENTED;
52
0
  }
53
54
0
  if (EVP_DigestInit_ex(md, evp, NULL) != 1) {
55
0
    ERR_clear_error();
56
0
    return ISC_R_CRYPTOFAILURE;
57
0
  }
58
59
0
  return ISC_R_SUCCESS;
60
0
}
61
62
isc_result_t
63
0
isc_md_reset(isc_md_t *md) {
64
0
  REQUIRE(md != NULL);
65
66
0
  if (EVP_MD_CTX_reset(md) != 1) {
67
0
    ERR_clear_error();
68
0
    return ISC_R_CRYPTOFAILURE;
69
0
  }
70
71
0
  return ISC_R_SUCCESS;
72
0
}
73
74
isc_result_t
75
0
isc_md_update(isc_md_t *md, const unsigned char *buf, const size_t len) {
76
0
  REQUIRE(md != NULL);
77
78
0
  if (buf == NULL || len == 0) {
79
0
    return ISC_R_SUCCESS;
80
0
  }
81
82
0
  if (EVP_DigestUpdate(md, buf, len) != 1) {
83
0
    ERR_clear_error();
84
0
    return ISC_R_CRYPTOFAILURE;
85
0
  }
86
87
0
  return ISC_R_SUCCESS;
88
0
}
89
90
isc_result_t
91
0
isc_md_final(isc_md_t *md, unsigned char *digest, unsigned int *digestlen) {
92
0
  REQUIRE(md != NULL);
93
0
  REQUIRE(digest != NULL);
94
95
0
  if (EVP_DigestFinal_ex(md, digest, digestlen) != 1) {
96
0
    ERR_clear_error();
97
0
    return ISC_R_CRYPTOFAILURE;
98
0
  }
99
100
0
  return ISC_R_SUCCESS;
101
0
}
102
103
size_t
104
0
isc_md_get_size(isc_md_t *md) {
105
0
  REQUIRE(md != NULL);
106
107
0
  return EVP_MD_CTX_size(md);
108
0
}
109
110
size_t
111
0
isc_md_get_block_size(isc_md_t *md) {
112
0
  REQUIRE(md != NULL);
113
114
0
  return EVP_MD_CTX_block_size(md);
115
0
}
116
117
size_t
118
0
isc_md_type_get_block_size(isc_md_type_t type) {
119
0
  EVP_MD *evp;
120
121
0
  REQUIRE(type < ISC_MD_MAX);
122
0
  STATIC_ASSERT(ISC_MAX_MD_SIZE >= EVP_MAX_MD_SIZE,
123
0
          "Change ISC_MAX_MD_SIZE to be greater than or equal to "
124
0
          "EVP_MAX_MD_SIZE");
125
126
0
  evp = isc__crypto_md[type];
127
0
  if (evp != NULL) {
128
0
    return (size_t)EVP_MD_block_size(evp);
129
0
  }
130
131
0
  return ISC_MAX_MD_SIZE;
132
0
}
133
134
isc_result_t
135
isc_md(isc_md_type_t type, const unsigned char *buf, const size_t len,
136
0
       unsigned char *digest, unsigned int *digestlen) {
137
0
  isc_md_t *md;
138
0
  isc_result_t res;
139
140
0
  md = isc_md_new();
141
142
0
  res = isc_md_init(md, type);
143
0
  if (res != ISC_R_SUCCESS) {
144
0
    goto end;
145
0
  }
146
147
0
  res = isc_md_update(md, buf, len);
148
0
  if (res != ISC_R_SUCCESS) {
149
0
    goto end;
150
0
  }
151
152
0
  res = isc_md_final(md, digest, digestlen);
153
0
  if (res != ISC_R_SUCCESS) {
154
0
    goto end;
155
0
  }
156
0
end:
157
0
  isc_md_free(md);
158
159
0
  return res;
160
0
}