2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 #include <openssl/crypto.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/core_names.h>
13 #include <openssl/dsa.h>
14 #include <openssl/params.h>
15 #include <openssl/evp.h>
16 #include "prov/implementations.h"
17 #include "prov/provider_ctx.h"
18 #include "crypto/dsa.h"
20 static OSSL_OP_signature_newctx_fn dsa_newctx;
21 static OSSL_OP_signature_sign_init_fn dsa_signature_init;
22 static OSSL_OP_signature_verify_init_fn dsa_signature_init;
23 static OSSL_OP_signature_sign_fn dsa_sign;
24 static OSSL_OP_signature_verify_fn dsa_verify;
25 static OSSL_OP_signature_digest_sign_init_fn dsa_digest_signverify_init;
26 static OSSL_OP_signature_digest_sign_update_fn dsa_digest_signverify_update;
27 static OSSL_OP_signature_digest_sign_final_fn dsa_digest_sign_final;
28 static OSSL_OP_signature_digest_verify_init_fn dsa_digest_signverify_init;
29 static OSSL_OP_signature_digest_verify_update_fn dsa_digest_signverify_update;
30 static OSSL_OP_signature_digest_verify_final_fn dsa_digest_verify_final;
31 static OSSL_OP_signature_freectx_fn dsa_freectx;
32 static OSSL_OP_signature_dupctx_fn dsa_dupctx;
33 static OSSL_OP_signature_get_ctx_params_fn dsa_get_ctx_params;
34 static OSSL_OP_signature_gettable_ctx_params_fn dsa_gettable_ctx_params;
35 static OSSL_OP_signature_set_ctx_params_fn dsa_set_ctx_params;
36 static OSSL_OP_signature_settable_ctx_params_fn dsa_settable_ctx_params;
37 static OSSL_OP_signature_get_ctx_md_params_fn dsa_get_ctx_md_params;
38 static OSSL_OP_signature_gettable_ctx_md_params_fn dsa_gettable_ctx_md_params;
39 static OSSL_OP_signature_set_ctx_md_params_fn dsa_set_ctx_md_params;
40 static OSSL_OP_signature_settable_ctx_md_params_fn dsa_settable_ctx_md_params;
43 * What's passed as an actual key is defined by the KEYMGMT interface.
44 * We happen to know that our KEYMGMT simply passes DSA structures, so
45 * we use that here too.
52 /* Should be big enough */
58 static void *dsa_newctx(void *provctx)
60 PROV_DSA_CTX *pdsactx = OPENSSL_zalloc(sizeof(PROV_DSA_CTX));
65 pdsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
69 static int dsa_signature_init(void *vpdsactx, void *vdsa)
71 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
73 if (pdsactx == NULL || vdsa == NULL || !DSA_up_ref(vdsa))
75 DSA_free(pdsactx->dsa);
80 static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen,
81 size_t sigsize, const unsigned char *tbs, size_t tbslen)
83 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
86 size_t dsasize = DSA_size(pdsactx->dsa);
93 if (sigsize < (size_t)dsasize)
96 if (pdsactx->mdsize != 0 && tbslen != pdsactx->mdsize)
99 ret = dsa_sign_int(pdsactx->libctx, 0, tbs, tbslen, sig, &sltmp,
108 static int dsa_verify(void *vpdsactx, const unsigned char *sig, size_t siglen,
109 const unsigned char *tbs, size_t tbslen)
111 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
113 if (pdsactx->mdsize != 0 && tbslen != pdsactx->mdsize)
116 return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa);
119 static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname,
120 const char *props, void *vdsa)
122 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
125 if (!dsa_signature_init(vpdsactx, vdsa))
128 md = EVP_MD_fetch(pdsactx->libctx, mdname, props);
132 pdsactx->mdsize = EVP_MD_size(md);
133 pdsactx->mdctx = EVP_MD_CTX_new();
134 if (pdsactx->mdctx == NULL)
137 if (!EVP_DigestInit_ex(pdsactx->mdctx, md, NULL))
143 int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data,
146 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
148 if (pdsactx == NULL || pdsactx->mdctx == NULL)
151 return EVP_DigestUpdate(pdsactx->mdctx, data, datalen);
154 int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig, size_t *siglen,
157 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
158 unsigned char digest[EVP_MAX_MD_SIZE];
159 unsigned int dlen = 0;
161 if (pdsactx == NULL || pdsactx->mdctx == NULL)
165 * If sig is NULL then we're just finding out the sig size. Other fields
166 * are ignored. Defer to dsa_sign.
170 * TODO(3.0): There is the possibility that some externally provided
171 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
172 * but that problem is much larger than just in DSA.
174 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
178 return dsa_sign(vpdsactx, sig, siglen, sigsize, digest, (size_t)dlen);
182 int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig,
185 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
186 unsigned char digest[EVP_MAX_MD_SIZE];
187 unsigned int dlen = 0;
189 if (pdsactx == NULL || pdsactx->mdctx == NULL)
193 * TODO(3.0): There is the possibility that some externally provided
194 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
195 * but that problem is much larger than just in DSA.
197 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
200 return dsa_verify(vpdsactx, sig, siglen, digest, (size_t)dlen);
203 static void dsa_freectx(void *vpdsactx)
205 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
207 DSA_free(pdsactx->dsa);
208 EVP_MD_CTX_free(pdsactx->mdctx);
209 EVP_MD_free(pdsactx->md);
211 OPENSSL_free(pdsactx);
214 static void *dsa_dupctx(void *vpdsactx)
216 PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx;
217 PROV_DSA_CTX *dstctx;
219 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
226 dstctx->mdctx = NULL;
228 if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa))
230 dstctx->dsa = srcctx->dsa;
232 if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
234 dstctx->md = srcctx->md;
236 if (srcctx->mdctx != NULL) {
237 dstctx->mdctx = EVP_MD_CTX_new();
238 if (dstctx->mdctx == NULL
239 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
249 static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params)
251 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
254 if (pdsactx == NULL || params == NULL)
257 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
258 if (p != NULL && !OSSL_PARAM_set_size_t(p, pdsactx->mdsize))
261 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
262 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->md == NULL
264 : EVP_MD_name(pdsactx->md)))
270 static const OSSL_PARAM known_gettable_ctx_params[] = {
271 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
272 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
276 static const OSSL_PARAM *dsa_gettable_ctx_params(void)
278 return known_gettable_ctx_params;
281 static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])
283 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
287 if (pdsactx == NULL || params == NULL)
290 if (pdsactx->md != NULL) {
292 * You cannot set the digest name/size when doing a DigestSign or
298 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
299 if (p != NULL && !OSSL_PARAM_get_size_t(p, &pdsactx->mdsize))
303 * We never actually use the mdname, but we do support getting it later.
304 * This can be useful for applications that want to know the MD that they
307 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
308 mdname = pdsactx->mdname;
310 && !OSSL_PARAM_get_utf8_string(p, &mdname, sizeof(pdsactx->mdname)))
316 static const OSSL_PARAM known_settable_ctx_params[] = {
317 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
318 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
322 static const OSSL_PARAM *dsa_settable_ctx_params(void)
325 * TODO(3.0): Should this function return a different set of settable ctx
326 * params if the ctx is being used for a DigestSign/DigestVerify? In that
327 * case it is not allowed to set the digest size/digest name because the
328 * digest is explicitly set as part of the init.
330 return known_settable_ctx_params;
333 static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params)
335 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
337 if (pdsactx->mdctx == NULL)
340 return EVP_MD_CTX_get_params(pdsactx->mdctx, params);
343 static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx)
345 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
347 if (pdsactx->md == NULL)
350 return EVP_MD_gettable_ctx_params(pdsactx->md);
353 static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[])
355 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
357 if (pdsactx->mdctx == NULL)
360 return EVP_MD_CTX_set_params(pdsactx->mdctx, params);
363 static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx)
365 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
367 if (pdsactx->md == NULL)
370 return EVP_MD_settable_ctx_params(pdsactx->md);
373 const OSSL_DISPATCH dsa_signature_functions[] = {
374 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx },
375 { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_signature_init },
376 { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign },
377 { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_signature_init },
378 { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify },
379 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
380 (void (*)(void))dsa_digest_signverify_init },
381 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
382 (void (*)(void))dsa_digest_signverify_update },
383 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
384 (void (*)(void))dsa_digest_sign_final },
385 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
386 (void (*)(void))dsa_digest_signverify_init },
387 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
388 (void (*)(void))dsa_digest_signverify_update },
389 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
390 (void (*)(void))dsa_digest_verify_final },
391 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx },
392 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx },
393 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params },
394 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
395 (void (*)(void))dsa_gettable_ctx_params },
396 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params },
397 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
398 (void (*)(void))dsa_settable_ctx_params },
399 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
400 (void (*)(void))dsa_get_ctx_md_params },
401 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
402 (void (*)(void))dsa_gettable_ctx_md_params },
403 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
404 (void (*)(void))dsa_set_ctx_md_params },
405 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
406 (void (*)(void))dsa_settable_ctx_md_params },