2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/objects.h>
14 #include <openssl/x509.h>
15 #include "internal/evp_int.h"
18 static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
20 EVPerr(EVP_F_UPDATE, EVP_R_ONLY_ONESHOT_SUPPORTED);
24 static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
25 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey,
28 if (ctx->pctx == NULL)
29 ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
30 if (ctx->pctx == NULL)
33 if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
37 if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
38 type = EVP_get_digestbynid(def_nid);
42 EVPerr(EVP_F_DO_SIGVER_INIT, EVP_R_NO_DEFAULT_DIGEST);
48 if (ctx->pctx->pmeth->verifyctx_init) {
49 if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
51 ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
52 } else if (ctx->pctx->pmeth->digestverify != 0) {
53 ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
55 } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
59 if (ctx->pctx->pmeth->signctx_init) {
60 if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
62 ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
63 } else if (ctx->pctx->pmeth->digestsign != 0) {
64 ctx->pctx->operation = EVP_PKEY_OP_SIGN;
66 } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
70 if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
74 if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
76 if (!EVP_DigestInit_ex(ctx, type, e))
78 if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_DIGEST_CUSTOM) {
80 * This indicates the current algorithm requires
81 * special treatment before hashing the tbs-message.
83 if (ctx->pctx->pmeth->digest_custom)
84 return ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx);
89 int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
90 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
92 return do_sigver_init(ctx, pctx, type, e, pkey, 0);
95 int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
96 const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
98 return do_sigver_init(ctx, pctx, type, e, pkey, 1);
101 int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
105 EVP_PKEY_CTX *pctx = ctx->pctx;
106 if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
108 return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
109 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
110 r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
112 EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(ctx->pctx);
115 r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
116 EVP_PKEY_CTX_free(dctx);
120 if (pctx->pmeth->signctx)
125 unsigned char md[EVP_MAX_MD_SIZE];
126 unsigned int mdlen = 0;
127 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
129 r = ctx->pctx->pmeth->signctx(ctx->pctx, sigret, siglen, ctx);
131 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
133 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
136 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
137 EVP_MD_CTX_free(tmp_ctx);
141 r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
142 sigret, siglen, tmp_ctx);
144 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
145 EVP_MD_CTX_free(tmp_ctx);
149 if (EVP_PKEY_sign(ctx->pctx, sigret, siglen, md, mdlen) <= 0)
153 if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
156 int s = EVP_MD_size(ctx->digest);
157 if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
164 int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
165 const unsigned char *tbs, size_t tbslen)
167 if (ctx->pctx->pmeth->digestsign != NULL)
168 return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
169 if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
171 return EVP_DigestSignFinal(ctx, sigret, siglen);
174 int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
177 unsigned char md[EVP_MAX_MD_SIZE];
179 unsigned int mdlen = 0;
182 if (ctx->pctx->pmeth->verifyctx)
186 if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
188 r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx);
190 r = EVP_DigestFinal_ex(ctx, md, &mdlen);
192 EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
195 if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
196 EVP_MD_CTX_free(tmp_ctx);
200 r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
201 sig, siglen, tmp_ctx);
203 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
204 EVP_MD_CTX_free(tmp_ctx);
208 return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen);
211 int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
212 size_t siglen, const unsigned char *tbs, size_t tbslen)
214 if (ctx->pctx->pmeth->digestverify != NULL)
215 return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
216 if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
218 return EVP_DigestVerifyFinal(ctx, sigret, siglen);