Coverage Report

Created: 2026-06-28 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/fipsmodule/digest/digest.cc.inc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/digest.h>
16
17
#include <assert.h>
18
#include <string.h>
19
20
#include <openssl/err.h>
21
#include <openssl/mem.h>
22
23
#include "../../internal.h"
24
#include "../../mem_internal.h"
25
#include "internal.h"
26
27
28
using namespace bssl;
29
30
53.4k
int EVP_MD_type(const EVP_MD *md) { return md->type; }
31
32
0
int EVP_MD_nid(const EVP_MD *md) { return EVP_MD_type(md); }
33
34
0
uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; }
35
36
1.17M
size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; }
37
38
467k
size_t EVP_MD_block_size(const EVP_MD *md) { return md->block_size; }
39
40
41
11.3M
void EVP_MD_CTX_init(EVP_MD_CTX *ctx) {
42
11.3M
  ctx->digest = nullptr;
43
11.3M
  ctx->pctx = nullptr;
44
11.3M
  ctx->pctx_ops = nullptr;
45
11.3M
}
46
47
0
EVP_MD_CTX *EVP_MD_CTX_new() {
48
0
  EVP_MD_CTX *ctx = New<EVP_MD_CTX>();
49
50
0
  if (ctx) {
51
0
    EVP_MD_CTX_init(ctx);
52
0
  }
53
54
0
  return ctx;
55
0
}
56
57
0
EVP_MD_CTX *EVP_MD_CTX_create() { return EVP_MD_CTX_new(); }
58
59
8.03M
int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) {
60
8.03M
  assert(ctx->pctx == nullptr || ctx->pctx_ops != nullptr);
61
8.03M
  if (ctx->pctx_ops) {
62
124k
    ctx->pctx_ops->free(ctx->pctx);
63
124k
  }
64
65
8.03M
  EVP_MD_CTX_init(ctx);
66
67
8.03M
  return 1;
68
8.03M
}
69
70
0
void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx) {
71
0
  OPENSSL_cleanse(ctx->md_data, sizeof(ctx->md_data));
72
0
  EVP_MD_CTX_cleanup(ctx);
73
0
}
74
75
0
void EVP_MD_CTX_free(EVP_MD_CTX *ctx) {
76
0
  if (!ctx) {
77
0
    return;
78
0
  }
79
80
0
  EVP_MD_CTX_cleanup(ctx);
81
0
  Delete(ctx);
82
0
}
83
84
0
void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) { EVP_MD_CTX_free(ctx); }
85
86
0
int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, size_t len) {
87
0
  OPENSSL_PUT_ERROR(DIGEST, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
88
0
  return 0;
89
0
}
90
91
0
uint32_t EVP_MD_meth_get_flags(const EVP_MD *md) { return EVP_MD_flags(md); }
92
93
0
void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) {}
94
95
3.42M
int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
96
  // `in->digest` may be NULL if this is a signing `EVP_MD_CTX` for, e.g.,
97
  // Ed25519 which does not hash with `EVP_MD_CTX`.
98
3.42M
  if (in == nullptr || (in->pctx == nullptr && in->digest == nullptr)) {
99
0
    OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_INPUT_NOT_INITIALIZED);
100
0
    return 0;
101
0
  }
102
3.42M
  if (out == in) {
103
0
    OPENSSL_PUT_ERROR(DIGEST, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
104
0
    return 0;
105
0
  }
106
107
3.42M
  EVP_PKEY_CTX *pctx = nullptr;
108
3.42M
  assert(in->pctx == nullptr || in->pctx_ops != nullptr);
109
3.42M
  if (in->pctx) {
110
62.2k
    pctx = in->pctx_ops->dup(in->pctx);
111
62.2k
    if (!pctx) {
112
0
      return 0;
113
0
    }
114
62.2k
  }
115
116
3.42M
  EVP_MD_CTX_cleanup(out);
117
118
3.42M
  out->digest = in->digest;
119
3.42M
  if (in->digest != nullptr) {
120
3.42M
    OPENSSL_memcpy(out->md_data, in->md_data, in->digest->ctx_size);
121
3.42M
  }
122
3.42M
  out->pctx = pctx;
123
3.42M
  out->pctx_ops = in->pctx_ops;
124
3.42M
  assert(out->pctx == nullptr || out->pctx_ops != nullptr);
125
126
3.42M
  return 1;
127
3.42M
}
128
129
0
void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in) {
130
0
  EVP_MD_CTX_cleanup(out);
131
  // While not guaranteed, `EVP_MD_CTX` is currently safe to move with `memcpy`.
132
  // bssl-crypto currently relies on this, however, so if we change this, we
133
  // need to box the `HMAC_CTX`. (Relying on this is only fine because we assume
134
  // BoringSSL and bssl-crypto will always be updated atomically. We do not
135
  // allow any version skew between the two.)
136
0
  OPENSSL_memcpy(out, in, sizeof(EVP_MD_CTX));
137
0
  EVP_MD_CTX_init(in);
138
0
}
139
140
0
int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
141
0
  EVP_MD_CTX_init(out);
142
0
  return EVP_MD_CTX_copy_ex(out, in);
143
0
}
144
145
0
int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) {
146
0
  EVP_MD_CTX_cleanup(ctx);
147
0
  EVP_MD_CTX_init(ctx);
148
0
  return 1;
149
0
}
150
151
1.33M
int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) {
152
1.33M
  EVP_MD_CTX_cleanup(ctx);
153
154
1.33M
  assert(ctx->pctx == nullptr);
155
1.33M
  assert(type->ctx_size != 0);
156
1.33M
  assert(type->ctx_size <= sizeof(ctx->md_data));
157
1.33M
  ctx->digest = type;
158
1.33M
  ctx->digest->init(ctx);
159
1.33M
  return 1;
160
1.33M
}
161
162
0
int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) {
163
0
  EVP_MD_CTX_init(ctx);
164
0
  return EVP_DigestInit_ex(ctx, type, nullptr);
165
0
}
166
167
4.94M
int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) {
168
4.94M
  ctx->digest->update(ctx, data, len);
169
4.94M
  return 1;
170
4.94M
}
171
172
2.23M
int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) {
173
2.23M
  assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
174
2.23M
  ctx->digest->final(ctx, md_out);
175
2.23M
  if (size != nullptr) {
176
1.92M
    *size = ctx->digest->md_size;
177
1.92M
  }
178
2.23M
  OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
179
2.23M
  return 1;
180
2.23M
}
181
182
0
int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) {
183
0
  (void)EVP_DigestFinal_ex(ctx, md, size);
184
0
  EVP_MD_CTX_cleanup(ctx);
185
0
  return 1;
186
0
}
187
188
int EVP_Digest(const void *data, size_t count, uint8_t *out_md,
189
14.0k
               unsigned int *out_size, const EVP_MD *type, ENGINE *impl) {
190
14.0k
  ScopedEVP_MD_CTX ctx;
191
14.0k
  return EVP_DigestInit_ex(ctx.get(), type, impl) &&
192
14.0k
         EVP_DigestUpdate(ctx.get(), data, count) &&
193
14.0k
         EVP_DigestFinal_ex(ctx.get(), out_md, out_size);
194
14.0k
}
195
196
872k
const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx) {
197
872k
  if (ctx == nullptr) {
198
0
    return nullptr;
199
0
  }
200
872k
  return ctx->digest;
201
872k
}
202
203
528k
const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) {
204
528k
  return EVP_MD_CTX_get0_md(ctx);
205
528k
}
206
207
0
size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx) {
208
0
  return EVP_MD_size(EVP_MD_CTX_get0_md(ctx));
209
0
}
210
211
0
size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx) {
212
0
  return EVP_MD_block_size(EVP_MD_CTX_get0_md(ctx));
213
0
}
214
215
0
int EVP_MD_CTX_type(const EVP_MD_CTX *ctx) {
216
0
  return EVP_MD_type(EVP_MD_CTX_get0_md(ctx));
217
0
}
218
219
0
EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx) { return ctx->pctx; }
220
221
0
int EVP_add_digest(const EVP_MD *digest) { return 1; }