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 "internal.h"
25
26
27int EVP_MD_type(const EVP_MD *md) { return md->type; }
28
29int EVP_MD_nid(const EVP_MD *md) { return EVP_MD_type(md); }
30
31uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; }
32
33size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; }
34
35size_t EVP_MD_block_size(const EVP_MD *md) { return md->block_size; }
36
37
38void EVP_MD_CTX_init(EVP_MD_CTX *ctx) {
39  ctx->digest = nullptr;
40  ctx->pctx = nullptr;
41  ctx->pctx_ops = nullptr;
42}
43
44EVP_MD_CTX *EVP_MD_CTX_new(void) {
45  EVP_MD_CTX *ctx =
46      reinterpret_cast<EVP_MD_CTX *>(OPENSSL_malloc(sizeof(EVP_MD_CTX)));
47
48  if (ctx) {
49    EVP_MD_CTX_init(ctx);
50  }
51
52  return ctx;
53}
54
55EVP_MD_CTX *EVP_MD_CTX_create(void) { return EVP_MD_CTX_new(); }
56
57int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) {
58  assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
59  if (ctx->pctx_ops) {
60    ctx->pctx_ops->free(ctx->pctx);
61  }
62
63  EVP_MD_CTX_init(ctx);
64
65  return 1;
66}
67
68void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx) {
69  OPENSSL_cleanse(ctx->md_data, sizeof(ctx->md_data));
70  EVP_MD_CTX_cleanup(ctx);
71}
72
73void EVP_MD_CTX_free(EVP_MD_CTX *ctx) {
74  if (!ctx) {
75    return;
76  }
77
78  EVP_MD_CTX_cleanup(ctx);
79  OPENSSL_free(ctx);
80}
81
82void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) { EVP_MD_CTX_free(ctx); }
83
84int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, size_t len) {
85  OPENSSL_PUT_ERROR(DIGEST, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
86  return 0;
87}
88
89uint32_t EVP_MD_meth_get_flags(const EVP_MD *md) { return EVP_MD_flags(md); }
90
91void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) {}
92
93int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
94  // |in->digest| may be NULL if this is a signing |EVP_MD_CTX| for, e.g.,
95  // Ed25519 which does not hash with |EVP_MD_CTX|.
96  if (in == NULL || (in->pctx == NULL && in->digest == NULL)) {
97    OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_INPUT_NOT_INITIALIZED);
98    return 0;
99  }
100  if (out == in) {
101    OPENSSL_PUT_ERROR(DIGEST, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
102    return 0;
103  }
104
105  EVP_PKEY_CTX *pctx = NULL;
106  assert(in->pctx == NULL || in->pctx_ops != NULL);
107  if (in->pctx) {
108    pctx = in->pctx_ops->dup(in->pctx);
109    if (!pctx) {
110      return 0;
111    }
112  }
113
114  EVP_MD_CTX_cleanup(out);
115
116  out->digest = in->digest;
117  if (in->digest != NULL) {
118    OPENSSL_memcpy(out->md_data, in->md_data, in->digest->ctx_size);
119  }
120  out->pctx = pctx;
121  out->pctx_ops = in->pctx_ops;
122  assert(out->pctx == NULL || out->pctx_ops != NULL);
123
124  return 1;
125}
126
127void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in) {
128  EVP_MD_CTX_cleanup(out);
129  // While not guaranteed, |EVP_MD_CTX| is currently safe to move with |memcpy|.
130  // bssl-crypto currently relies on this, however, so if we change this, we
131  // need to box the |HMAC_CTX|. (Relying on this is only fine because we assume
132  // BoringSSL and bssl-crypto will always be updated atomically. We do not
133  // allow any version skew between the two.)
134  OPENSSL_memcpy(out, in, sizeof(EVP_MD_CTX));
135  EVP_MD_CTX_init(in);
136}
137
138int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
139  EVP_MD_CTX_init(out);
140  return EVP_MD_CTX_copy_ex(out, in);
141}
142
143int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) {
144  EVP_MD_CTX_cleanup(ctx);
145  EVP_MD_CTX_init(ctx);
146  return 1;
147}
148
149int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) {
150  if (ctx->digest != type) {
151    assert(type->ctx_size != 0);
152    assert(type->ctx_size <= sizeof(ctx->md_data));
153    ctx->digest = type;
154  }
155
156  assert(ctx->pctx == NULL || ctx->pctx_ops != NULL);
157
158  ctx->digest->init(ctx);
159  return 1;
160}
161
162int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) {
163  EVP_MD_CTX_init(ctx);
164  return EVP_DigestInit_ex(ctx, type, NULL);
165}
166
167int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) {
168  ctx->digest->update(ctx, data, len);
169  return 1;
170}
171
172int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) {
173  assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
174  ctx->digest->final(ctx, md_out);
175  if (size != NULL) {
176    *size = ctx->digest->md_size;
177  }
178  OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
179  return 1;
180}
181
182int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) {
183  (void)EVP_DigestFinal_ex(ctx, md, size);
184  EVP_MD_CTX_cleanup(ctx);
185  return 1;
186}
187
188int EVP_Digest(const void *data, size_t count, uint8_t *out_md,
189               unsigned int *out_size, const EVP_MD *type, ENGINE *impl) {
190  bssl::ScopedEVP_MD_CTX ctx;
191  return EVP_DigestInit_ex(ctx.get(), type, impl) &&
192         EVP_DigestUpdate(ctx.get(), data, count) &&
193         EVP_DigestFinal_ex(ctx.get(), out_md, out_size);
194}
195
196const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx) {
197  if (ctx == NULL) {
198    return NULL;
199  }
200  return ctx->digest;
201}
202
203const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) {
204  return EVP_MD_CTX_get0_md(ctx);
205}
206
207size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx) {
208  return EVP_MD_size(EVP_MD_CTX_get0_md(ctx));
209}
210
211size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx) {
212  return EVP_MD_block_size(EVP_MD_CTX_get0_md(ctx));
213}
214
215int EVP_MD_CTX_type(const EVP_MD_CTX *ctx) {
216  return EVP_MD_type(EVP_MD_CTX_get0_md(ctx));
217}
218
219int EVP_add_digest(const EVP_MD *digest) { return 1; }
220