Coverage Report

Created: 2024-11-21 07:03

/src/boringssl/crypto/fipsmodule/digest/digest.c.inc
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
 * All rights reserved.
3
 *
4
 * This package is an SSL implementation written
5
 * by Eric Young (eay@cryptsoft.com).
6
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8
 * This library is free for commercial and non-commercial use as long as
9
 * the following conditions are aheared to.  The following conditions
10
 * apply to all code found in this distribution, be it the RC4, RSA,
11
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12
 * included with this distribution is covered by the same copyright terms
13
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15
 * Copyright remains Eric Young's, and as such any Copyright notices in
16
 * the code are not to be removed.
17
 * If this package is used in a product, Eric Young should be given attribution
18
 * as the author of the parts of the library used.
19
 * This can be in the form of a textual message at program startup or
20
 * in documentation (online or textual) provided with the package.
21
 *
22
 * Redistribution and use in source and binary forms, with or without
23
 * modification, are permitted provided that the following conditions
24
 * are met:
25
 * 1. Redistributions of source code must retain the copyright
26
 *    notice, this list of conditions and the following disclaimer.
27
 * 2. Redistributions in binary form must reproduce the above copyright
28
 *    notice, this list of conditions and the following disclaimer in the
29
 *    documentation and/or other materials provided with the distribution.
30
 * 3. All advertising materials mentioning features or use of this software
31
 *    must display the following acknowledgement:
32
 *    "This product includes cryptographic software written by
33
 *     Eric Young (eay@cryptsoft.com)"
34
 *    The word 'cryptographic' can be left out if the rouines from the library
35
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37
 *    the apps directory (application code) you must include an acknowledgement:
38
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 * The licence and distribution terms for any publically available version or
53
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54
 * copied and put under another distribution licence
55
 * [including the GNU Public Licence.] */
56
57
#include <openssl/digest.h>
58
59
#include <assert.h>
60
#include <string.h>
61
62
#include <openssl/err.h>
63
#include <openssl/mem.h>
64
65
#include "internal.h"
66
#include "../../internal.h"
67
68
69
0
int EVP_MD_type(const EVP_MD *md) { return md->type; }
70
71
0
int EVP_MD_nid(const EVP_MD *md) { return EVP_MD_type(md); }
72
73
0
uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; }
74
75
1.42k
size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; }
76
77
1.00k
size_t EVP_MD_block_size(const EVP_MD *md) { return md->block_size; }
78
79
80
719k
void EVP_MD_CTX_init(EVP_MD_CTX *ctx) {
81
719k
  OPENSSL_memset(ctx, 0, sizeof(EVP_MD_CTX));
82
719k
}
83
84
48.2k
EVP_MD_CTX *EVP_MD_CTX_new(void) {
85
48.2k
  EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_MD_CTX));
86
87
48.2k
  if (ctx) {
88
48.2k
    EVP_MD_CTX_init(ctx);
89
48.2k
  }
90
91
48.2k
  return ctx;
92
48.2k
}
93
94
0
EVP_MD_CTX *EVP_MD_CTX_create(void) { return EVP_MD_CTX_new(); }
95
96
373k
int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) {
97
373k
  OPENSSL_free(ctx->md_data);
98
99
373k
  assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
100
373k
  if (ctx->pctx_ops) {
101
0
    ctx->pctx_ops->free(ctx->pctx);
102
0
  }
103
104
373k
  EVP_MD_CTX_init(ctx);
105
106
373k
  return 1;
107
373k
}
108
109
0
void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx) {
110
0
  OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
111
0
  EVP_MD_CTX_cleanup(ctx);
112
0
}
113
114
48.2k
void EVP_MD_CTX_free(EVP_MD_CTX *ctx) {
115
48.2k
  if (!ctx) {
116
0
    return;
117
0
  }
118
119
48.2k
  EVP_MD_CTX_cleanup(ctx);
120
48.2k
  OPENSSL_free(ctx);
121
48.2k
}
122
123
0
void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) { EVP_MD_CTX_free(ctx); }
124
125
0
int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, size_t len) {
126
0
  OPENSSL_PUT_ERROR(DIGEST, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
127
0
  return 0;
128
0
}
129
130
0
uint32_t EVP_MD_meth_get_flags(const EVP_MD *md) { return EVP_MD_flags(md); }
131
132
2.36M
void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) {}
133
134
189k
int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
135
  // |in->digest| may be NULL if this is a signing |EVP_MD_CTX| for, e.g.,
136
  // Ed25519 which does not hash with |EVP_MD_CTX|.
137
189k
  if (in == NULL || (in->pctx == NULL && in->digest == NULL)) {
138
1.89k
    OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_INPUT_NOT_INITIALIZED);
139
1.89k
    return 0;
140
1.89k
  }
141
142
188k
  EVP_PKEY_CTX *pctx = NULL;
143
188k
  assert(in->pctx == NULL || in->pctx_ops != NULL);
144
188k
  if (in->pctx) {
145
0
    pctx = in->pctx_ops->dup(in->pctx);
146
0
    if (!pctx) {
147
0
      return 0;
148
0
    }
149
0
  }
150
151
188k
  uint8_t *tmp_buf = NULL;
152
188k
  if (in->digest != NULL) {
153
188k
    if (out->digest != in->digest) {
154
155k
      assert(in->digest->ctx_size != 0);
155
155k
      tmp_buf = OPENSSL_malloc(in->digest->ctx_size);
156
155k
      if (tmp_buf == NULL) {
157
0
        if (pctx) {
158
0
          in->pctx_ops->free(pctx);
159
0
        }
160
0
        return 0;
161
0
      }
162
155k
    } else {
163
      // |md_data| will be the correct size in this case. It's removed from
164
      // |out| so that |EVP_MD_CTX_cleanup| doesn't free it, and then it's
165
      // reused.
166
32.5k
      tmp_buf = out->md_data;
167
32.5k
      out->md_data = NULL;
168
32.5k
    }
169
188k
  }
170
171
188k
  EVP_MD_CTX_cleanup(out);
172
173
188k
  out->digest = in->digest;
174
188k
  out->md_data = tmp_buf;
175
188k
  if (in->digest != NULL) {
176
188k
    OPENSSL_memcpy(out->md_data, in->md_data, in->digest->ctx_size);
177
188k
  }
178
188k
  out->pctx = pctx;
179
188k
  out->pctx_ops = in->pctx_ops;
180
188k
  assert(out->pctx == NULL || out->pctx_ops != NULL);
181
182
188k
  return 1;
183
188k
}
184
185
0
void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in) {
186
0
  EVP_MD_CTX_cleanup(out);
187
  // While not guaranteed, |EVP_MD_CTX| is currently safe to move with |memcpy|.
188
  // bssl-crypto currently relies on this, however, so if we change this, we
189
  // need to box the |HMAC_CTX|. (Relying on this is only fine because we assume
190
  // BoringSSL and bssl-crypto will always be updated atomically. We do not
191
  // allow any version skew between the two.)
192
0
  OPENSSL_memcpy(out, in, sizeof(EVP_MD_CTX));
193
0
  EVP_MD_CTX_init(in);
194
0
}
195
196
237k
int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
197
237k
  EVP_MD_CTX_init(out);
198
237k
  return EVP_MD_CTX_copy_ex(out, in);
199
237k
}
200
201
2.12M
int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) {
202
2.12M
  EVP_MD_CTX_cleanup(ctx);
203
2.12M
  EVP_MD_CTX_init(ctx);
204
2.12M
  return 1;
205
2.12M
}
206
207
19.5k
int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) {
208
19.5k
  if (ctx->digest != type) {
209
19.5k
    assert(type->ctx_size != 0);
210
19.5k
    uint8_t *md_data = OPENSSL_malloc(type->ctx_size);
211
19.5k
    if (md_data == NULL) {
212
0
      return 0;
213
0
    }
214
215
19.5k
    OPENSSL_free(ctx->md_data);
216
19.5k
    ctx->md_data = md_data;
217
19.5k
    ctx->digest = type;
218
19.5k
  }
219
220
19.5k
  assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
221
222
19.5k
  ctx->digest->init(ctx);
223
19.5k
  return 1;
224
19.5k
}
225
226
198
int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) {
227
198
  EVP_MD_CTX_init(ctx);
228
198
  return EVP_DigestInit_ex(ctx, type, NULL);
229
198
}
230
231
151k
int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) {
232
151k
  ctx->digest->update(ctx, data, len);
233
151k
  return 1;
234
151k
}
235
236
50.0k
int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) {
237
50.0k
  assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
238
50.0k
  ctx->digest->final(ctx, md_out);
239
50.0k
  if (size != NULL) {
240
18.0k
    *size = ctx->digest->md_size;
241
18.0k
  }
242
50.0k
  OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
243
50.0k
  return 1;
244
50.0k
}
245
246
0
int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) {
247
0
  (void)EVP_DigestFinal_ex(ctx, md, size);
248
0
  EVP_MD_CTX_cleanup(ctx);
249
0
  return 1;
250
0
}
251
252
int EVP_Digest(const void *data, size_t count, uint8_t *out_md,
253
16.4k
               unsigned int *out_size, const EVP_MD *type, ENGINE *impl) {
254
16.4k
  EVP_MD_CTX ctx;
255
16.4k
  int ret;
256
257
16.4k
  EVP_MD_CTX_init(&ctx);
258
16.4k
  ret = EVP_DigestInit_ex(&ctx, type, impl) &&
259
16.4k
        EVP_DigestUpdate(&ctx, data, count) &&
260
16.4k
        EVP_DigestFinal_ex(&ctx, out_md, out_size);
261
16.4k
  EVP_MD_CTX_cleanup(&ctx);
262
263
16.4k
  return ret;
264
16.4k
}
265
266
267
0
const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) {
268
0
  if (ctx == NULL) {
269
0
    return NULL;
270
0
  }
271
0
  return ctx->digest;
272
0
}
273
274
0
size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx) {
275
0
  return EVP_MD_size(EVP_MD_CTX_md(ctx));
276
0
}
277
278
0
size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx) {
279
0
  return EVP_MD_block_size(EVP_MD_CTX_md(ctx));
280
0
}
281
282
0
int EVP_MD_CTX_type(const EVP_MD_CTX *ctx) {
283
0
  return EVP_MD_type(EVP_MD_CTX_md(ctx));
284
0
}
285
286
0
int EVP_add_digest(const EVP_MD *digest) {
287
0
  return 1;
288
0
}