Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 63 | #include <openssl/mem.h> |
| 64 | |
| 65 | #include "internal.h" |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 66 | #include "../../internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 67 | |
| 68 | |
| 69 | int EVP_MD_type(const EVP_MD *md) { return md->type; } |
| 70 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 71 | uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; } |
| 72 | |
| 73 | size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; } |
| 74 | |
| 75 | size_t EVP_MD_block_size(const EVP_MD *md) { return md->block_size; } |
| 76 | |
| 77 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 78 | void EVP_MD_CTX_init(EVP_MD_CTX *ctx) { |
| 79 | OPENSSL_memset(ctx, 0, sizeof(EVP_MD_CTX)); |
| 80 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 81 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 82 | EVP_MD_CTX *EVP_MD_CTX_new(void) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 83 | EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_MD_CTX)); |
| 84 | |
| 85 | if (ctx) { |
| 86 | EVP_MD_CTX_init(ctx); |
| 87 | } |
| 88 | |
| 89 | return ctx; |
| 90 | } |
| 91 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 92 | EVP_MD_CTX *EVP_MD_CTX_create(void) { return EVP_MD_CTX_new(); } |
| 93 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 94 | int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) { |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 95 | OPENSSL_free(ctx->md_data); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 96 | |
| 97 | assert(ctx->pctx == NULL || ctx->pctx_ops != NULL); |
| 98 | if (ctx->pctx_ops) { |
| 99 | ctx->pctx_ops->free(ctx->pctx); |
| 100 | } |
| 101 | |
| 102 | EVP_MD_CTX_init(ctx); |
| 103 | |
| 104 | return 1; |
| 105 | } |
| 106 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 107 | void EVP_MD_CTX_free(EVP_MD_CTX *ctx) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 108 | if (!ctx) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | EVP_MD_CTX_cleanup(ctx); |
| 113 | OPENSSL_free(ctx); |
| 114 | } |
| 115 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 116 | void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) { EVP_MD_CTX_free(ctx); } |
| 117 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 118 | int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) { |
Robert Sloan | f63bd1f | 2019-04-16 09:26:20 -0700 | [diff] [blame] | 119 | // |in->digest| may be NULL if this is a signing |EVP_MD_CTX| for, e.g., |
| 120 | // Ed25519 which does not hash with |EVP_MD_CTX|. |
| 121 | if (in == NULL || (in->pctx == NULL && in->digest == NULL)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 122 | OPENSSL_PUT_ERROR(DIGEST, DIGEST_R_INPUT_NOT_INITIALIZED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 126 | EVP_PKEY_CTX *pctx = NULL; |
| 127 | assert(in->pctx == NULL || in->pctx_ops != NULL); |
| 128 | if (in->pctx) { |
| 129 | pctx = in->pctx_ops->dup(in->pctx); |
| 130 | if (!pctx) { |
| 131 | OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE); |
| 132 | return 0; |
| 133 | } |
| 134 | } |
| 135 | |
Robert Sloan | f63bd1f | 2019-04-16 09:26:20 -0700 | [diff] [blame] | 136 | uint8_t *tmp_buf = NULL; |
| 137 | if (in->digest != NULL) { |
| 138 | if (out->digest != in->digest) { |
| 139 | assert(in->digest->ctx_size != 0); |
| 140 | tmp_buf = OPENSSL_malloc(in->digest->ctx_size); |
| 141 | if (tmp_buf == NULL) { |
| 142 | if (pctx) { |
| 143 | in->pctx_ops->free(pctx); |
| 144 | } |
| 145 | OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE); |
| 146 | return 0; |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 147 | } |
Robert Sloan | f63bd1f | 2019-04-16 09:26:20 -0700 | [diff] [blame] | 148 | } else { |
| 149 | // |md_data| will be the correct size in this case. It's removed from |
| 150 | // |out| so that |EVP_MD_CTX_cleanup| doesn't free it, and then it's |
| 151 | // reused. |
| 152 | tmp_buf = out->md_data; |
| 153 | out->md_data = NULL; |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 154 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | EVP_MD_CTX_cleanup(out); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 158 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 159 | out->digest = in->digest; |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 160 | out->md_data = tmp_buf; |
Robert Sloan | f63bd1f | 2019-04-16 09:26:20 -0700 | [diff] [blame] | 161 | if (in->digest != NULL) { |
| 162 | OPENSSL_memcpy(out->md_data, in->md_data, in->digest->ctx_size); |
| 163 | } |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 164 | out->pctx = pctx; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 165 | out->pctx_ops = in->pctx_ops; |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 166 | assert(out->pctx == NULL || out->pctx_ops != NULL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 167 | |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) { |
| 172 | EVP_MD_CTX_init(out); |
| 173 | return EVP_MD_CTX_copy_ex(out, in); |
| 174 | } |
| 175 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 176 | int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) { |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 177 | EVP_MD_CTX_cleanup(ctx); |
| 178 | EVP_MD_CTX_init(ctx); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 179 | return 1; |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 182 | int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) { |
| 183 | if (ctx->digest != type) { |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 184 | assert(type->ctx_size != 0); |
| 185 | uint8_t *md_data = OPENSSL_malloc(type->ctx_size); |
| 186 | if (md_data == NULL) { |
| 187 | OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE); |
| 188 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 189 | } |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 190 | |
| 191 | OPENSSL_free(ctx->md_data); |
| 192 | ctx->md_data = md_data; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 193 | ctx->digest = type; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | assert(ctx->pctx == NULL || ctx->pctx_ops != NULL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 197 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 198 | ctx->digest->init(ctx); |
| 199 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) { |
| 203 | EVP_MD_CTX_init(ctx); |
| 204 | return EVP_DigestInit_ex(ctx, type, NULL); |
| 205 | } |
| 206 | |
| 207 | int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 208 | ctx->digest->update(ctx, data, len); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 209 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 213 | assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 214 | ctx->digest->final(ctx, md_out); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 215 | if (size != NULL) { |
| 216 | *size = ctx->digest->md_size; |
| 217 | } |
| 218 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 219 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 223 | (void)EVP_DigestFinal_ex(ctx, md, size); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 224 | EVP_MD_CTX_cleanup(ctx); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 225 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | int EVP_Digest(const void *data, size_t count, uint8_t *out_md, |
| 229 | unsigned int *out_size, const EVP_MD *type, ENGINE *impl) { |
| 230 | EVP_MD_CTX ctx; |
| 231 | int ret; |
| 232 | |
| 233 | EVP_MD_CTX_init(&ctx); |
| 234 | ret = EVP_DigestInit_ex(&ctx, type, impl) && |
| 235 | EVP_DigestUpdate(&ctx, data, count) && |
| 236 | EVP_DigestFinal_ex(&ctx, out_md, out_size); |
| 237 | EVP_MD_CTX_cleanup(&ctx); |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) { |
| 244 | if (ctx == NULL) { |
| 245 | return NULL; |
| 246 | } |
| 247 | return ctx->digest; |
| 248 | } |
| 249 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 250 | size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 251 | return EVP_MD_size(EVP_MD_CTX_md(ctx)); |
| 252 | } |
| 253 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 254 | size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 255 | return EVP_MD_block_size(EVP_MD_CTX_md(ctx)); |
| 256 | } |
| 257 | |
| 258 | int EVP_MD_CTX_type(const EVP_MD_CTX *ctx) { |
| 259 | return EVP_MD_type(EVP_MD_CTX_md(ctx)); |
| 260 | } |
| 261 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 262 | int EVP_add_digest(const EVP_MD *digest) { |
| 263 | return 1; |
| 264 | } |