| 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/evp.h> |
| 16 |
| 17 | #include <limits.h> |
| 18 |
| 19 | #include <openssl/digest.h> |
| 20 | #include <openssl/err.h> |
| 21 |
| 22 | #include "internal.h" |
| 23 |
| 24 |
| 25 | int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) { |
| 26 | return EVP_DigestInit_ex(ctx, type, impl); |
| 27 | } |
| 28 |
| 29 | int EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type) { |
| 30 | return EVP_DigestInit(ctx, type); |
| 31 | } |
| 32 |
| 33 | int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
| 34 | return EVP_DigestUpdate(ctx, data, len); |
| 35 | } |
| 36 |
| 37 | int EVP_SignFinal(const EVP_MD_CTX *ctx, uint8_t *sig, unsigned *out_sig_len, |
| 38 | EVP_PKEY *pkey) { |
| 39 | uint8_t m[EVP_MAX_MD_SIZE]; |
| 40 | unsigned m_len; |
| 41 | int ret = 0; |
| 42 | EVP_MD_CTX tmp_ctx; |
| 43 | EVP_PKEY_CTX *pkctx = NULL; |
| 44 | size_t sig_len = EVP_PKEY_size(pkey); |
| 45 |
| 46 | // Ensure the final result will fit in |unsigned|. |
| 47 | if (sig_len > UINT_MAX) { |
| 48 | sig_len = UINT_MAX; |
| 49 | } |
| 50 |
| 51 | *out_sig_len = 0; |
| 52 | EVP_MD_CTX_init(&tmp_ctx); |
| 53 | if (!EVP_MD_CTX_copy_ex(&tmp_ctx, ctx) || |
| 54 | !EVP_DigestFinal_ex(&tmp_ctx, m, &m_len)) { |
| 55 | goto out; |
| 56 | } |
| 57 | EVP_MD_CTX_cleanup(&tmp_ctx); |
| 58 |
| 59 | pkctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 60 | if (!pkctx || // |
| 61 | !EVP_PKEY_sign_init(pkctx) || |
| 62 | !EVP_PKEY_CTX_set_signature_md(pkctx, ctx->digest) || |
| 63 | !EVP_PKEY_sign(pkctx, sig, &sig_len, m, m_len)) { |
| 64 | goto out; |
| 65 | } |
| 66 | *out_sig_len = (unsigned)sig_len; |
| 67 | ret = 1; |
| 68 |
| 69 | out: |
| 70 | EVP_PKEY_CTX_free(pkctx); |
| 71 | return ret; |
| 72 | } |
| 73 |
| 74 | int EVP_VerifyInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) { |
| 75 | return EVP_DigestInit_ex(ctx, type, impl); |
| 76 | } |
| 77 |
| 78 | int EVP_VerifyInit(EVP_MD_CTX *ctx, const EVP_MD *type) { |
| 79 | return EVP_DigestInit(ctx, type); |
| 80 | } |
| 81 |
| 82 | int EVP_VerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
| 83 | return EVP_DigestUpdate(ctx, data, len); |
| 84 | } |
| 85 |
| 86 | int EVP_VerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len, |
| 87 | EVP_PKEY *pkey) { |
| 88 | uint8_t m[EVP_MAX_MD_SIZE]; |
| 89 | unsigned m_len; |
| 90 | int ret = 0; |
| 91 | EVP_MD_CTX tmp_ctx; |
| 92 | EVP_PKEY_CTX *pkctx = NULL; |
| 93 |
| 94 | EVP_MD_CTX_init(&tmp_ctx); |
| 95 | if (!EVP_MD_CTX_copy_ex(&tmp_ctx, ctx) || |
| 96 | !EVP_DigestFinal_ex(&tmp_ctx, m, &m_len)) { |
| 97 | EVP_MD_CTX_cleanup(&tmp_ctx); |
| 98 | goto out; |
| 99 | } |
| 100 | EVP_MD_CTX_cleanup(&tmp_ctx); |
| 101 |
| 102 | pkctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 103 | if (!pkctx || |
| 104 | !EVP_PKEY_verify_init(pkctx) || |
| 105 | !EVP_PKEY_CTX_set_signature_md(pkctx, ctx->digest)) { |
| 106 | goto out; |
| 107 | } |
| 108 | ret = EVP_PKEY_verify(pkctx, sig, sig_len, m, m_len); |
| 109 |
| 110 | out: |
| 111 | EVP_PKEY_CTX_free(pkctx); |
| 112 | return ret; |
| 113 | } |
| 114 |
| 115 |