| // |
| 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 |
| 27 | int EVP_MD_type(const EVP_MD *md) { return md->type; } |
| 28 |
| 29 | int EVP_MD_nid(const EVP_MD *md) { return EVP_MD_type(md); } |
| 30 |
| 31 | uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; } |
| 32 |
| 33 | size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; } |
| 34 |
| 35 | size_t EVP_MD_block_size(const EVP_MD *md) { return md->block_size; } |
| 36 |
| 37 |
| 38 | void EVP_MD_CTX_init(EVP_MD_CTX *ctx) { |
| 39 | OPENSSL_memset(ctx, 0, sizeof(EVP_MD_CTX)); |
| 40 | } |
| 41 |
| 42 | EVP_MD_CTX *EVP_MD_CTX_new(void) { |
| 43 | EVP_MD_CTX *ctx = |
| 44 | reinterpret_cast<EVP_MD_CTX *>(OPENSSL_malloc(sizeof(EVP_MD_CTX))); |
| 45 |
| 46 | if (ctx) { |
| 47 | EVP_MD_CTX_init(ctx); |
| 48 | } |
| 49 |
| 50 | return ctx; |
| 51 | } |
| 52 |
| 53 | EVP_MD_CTX *EVP_MD_CTX_create(void) { return EVP_MD_CTX_new(); } |
| 54 |
| 55 | int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) { |
| 56 | OPENSSL_free(ctx->md_data); |
| 57 |
| 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 |
| 68 | void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx) { |
| 69 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
| 70 | EVP_MD_CTX_cleanup(ctx); |
| 71 | } |
| 72 |
| 73 | void 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 |
| 82 | void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx) { EVP_MD_CTX_free(ctx); } |
| 83 |
| 84 | int 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 |
| 89 | uint32_t EVP_MD_meth_get_flags(const EVP_MD *md) { return EVP_MD_flags(md); } |
| 90 |
| 91 | void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) {} |
| 92 |
| 93 | int 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 |
| 101 | EVP_PKEY_CTX *pctx = NULL; |
| 102 | assert(in->pctx == NULL || in->pctx_ops != NULL); |
| 103 | if (in->pctx) { |
| 104 | pctx = in->pctx_ops->dup(in->pctx); |
| 105 | if (!pctx) { |
| 106 | return 0; |
| 107 | } |
| 108 | } |
| 109 |
| 110 | uint8_t *tmp_buf = NULL; |
| 111 | if (in->digest != NULL) { |
| 112 | if (out->digest != in->digest) { |
| 113 | assert(in->digest->ctx_size != 0); |
| 114 | tmp_buf = |
| 115 | reinterpret_cast<uint8_t *>(OPENSSL_malloc(in->digest->ctx_size)); |
| 116 | if (tmp_buf == NULL) { |
| 117 | if (pctx) { |
| 118 | in->pctx_ops->free(pctx); |
| 119 | } |
| 120 | return 0; |
| 121 | } |
| 122 | } else { |
| 123 | // |md_data| will be the correct size in this case. It's removed from |
| 124 | // |out| so that |EVP_MD_CTX_cleanup| doesn't free it, and then it's |
| 125 | // reused. |
| 126 | tmp_buf = reinterpret_cast<uint8_t *>(out->md_data); |
| 127 | out->md_data = NULL; |
| 128 | } |
| 129 | } |
| 130 |
| 131 | EVP_MD_CTX_cleanup(out); |
| 132 |
| 133 | out->digest = in->digest; |
| 134 | out->md_data = tmp_buf; |
| 135 | if (in->digest != NULL) { |
| 136 | OPENSSL_memcpy(out->md_data, in->md_data, in->digest->ctx_size); |
| 137 | } |
| 138 | out->pctx = pctx; |
| 139 | out->pctx_ops = in->pctx_ops; |
| 140 | assert(out->pctx == NULL || out->pctx_ops != NULL); |
| 141 |
| 142 | return 1; |
| 143 | } |
| 144 |
| 145 | void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in) { |
| 146 | EVP_MD_CTX_cleanup(out); |
| 147 | // While not guaranteed, |EVP_MD_CTX| is currently safe to move with |memcpy|. |
| 148 | // bssl-crypto currently relies on this, however, so if we change this, we |
| 149 | // need to box the |HMAC_CTX|. (Relying on this is only fine because we assume |
| 150 | // BoringSSL and bssl-crypto will always be updated atomically. We do not |
| 151 | // allow any version skew between the two.) |
| 152 | OPENSSL_memcpy(out, in, sizeof(EVP_MD_CTX)); |
| 153 | EVP_MD_CTX_init(in); |
| 154 | } |
| 155 |
| 156 | int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) { |
| 157 | EVP_MD_CTX_init(out); |
| 158 | return EVP_MD_CTX_copy_ex(out, in); |
| 159 | } |
| 160 |
| 161 | int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) { |
| 162 | EVP_MD_CTX_cleanup(ctx); |
| 163 | EVP_MD_CTX_init(ctx); |
| 164 | return 1; |
| 165 | } |
| 166 |
| 167 | int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) { |
| 168 | if (ctx->digest != type) { |
| 169 | assert(type->ctx_size != 0); |
| 170 | uint8_t *md_data = |
| 171 | reinterpret_cast<uint8_t *>(OPENSSL_malloc(type->ctx_size)); |
| 172 | if (md_data == NULL) { |
| 173 | return 0; |
| 174 | } |
| 175 |
| 176 | OPENSSL_free(ctx->md_data); |
| 177 | ctx->md_data = md_data; |
| 178 | ctx->digest = type; |
| 179 | } |
| 180 |
| 181 | assert(ctx->pctx == NULL || ctx->pctx_ops != NULL); |
| 182 |
| 183 | ctx->digest->init(ctx); |
| 184 | return 1; |
| 185 | } |
| 186 |
| 187 | int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) { |
| 188 | EVP_MD_CTX_init(ctx); |
| 189 | return EVP_DigestInit_ex(ctx, type, NULL); |
| 190 | } |
| 191 |
| 192 | int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
| 193 | ctx->digest->update(ctx, data, len); |
| 194 | return 1; |
| 195 | } |
| 196 |
| 197 | int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) { |
| 198 | assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE); |
| 199 | ctx->digest->final(ctx, md_out); |
| 200 | if (size != NULL) { |
| 201 | *size = ctx->digest->md_size; |
| 202 | } |
| 203 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
| 204 | return 1; |
| 205 | } |
| 206 |
| 207 | int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) { |
| 208 | (void)EVP_DigestFinal_ex(ctx, md, size); |
| 209 | EVP_MD_CTX_cleanup(ctx); |
| 210 | return 1; |
| 211 | } |
| 212 |
| 213 | int EVP_Digest(const void *data, size_t count, uint8_t *out_md, |
| 214 | unsigned int *out_size, const EVP_MD *type, ENGINE *impl) { |
| 215 | EVP_MD_CTX ctx; |
| 216 | int ret; |
| 217 |
| 218 | EVP_MD_CTX_init(&ctx); |
| 219 | ret = EVP_DigestInit_ex(&ctx, type, impl) && |
| 220 | EVP_DigestUpdate(&ctx, data, count) && |
| 221 | EVP_DigestFinal_ex(&ctx, out_md, out_size); |
| 222 | EVP_MD_CTX_cleanup(&ctx); |
| 223 |
| 224 | return ret; |
| 225 | } |
| 226 |
| 227 | const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx) { |
| 228 | if (ctx == NULL) { |
| 229 | return NULL; |
| 230 | } |
| 231 | return ctx->digest; |
| 232 | } |
| 233 |
| 234 | const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) { |
| 235 | return EVP_MD_CTX_get0_md(ctx); |
| 236 | } |
| 237 |
| 238 | size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx) { |
| 239 | return EVP_MD_size(EVP_MD_CTX_get0_md(ctx)); |
| 240 | } |
| 241 |
| 242 | size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx) { |
| 243 | return EVP_MD_block_size(EVP_MD_CTX_get0_md(ctx)); |
| 244 | } |
| 245 |
| 246 | int EVP_MD_CTX_type(const EVP_MD_CTX *ctx) { |
| 247 | return EVP_MD_type(EVP_MD_CTX_get0_md(ctx)); |
| 248 | } |
| 249 |
| 250 | int EVP_add_digest(const EVP_MD *digest) { return 1; } |
| 251 |