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
12 #include <openssl/crypto.h>
13 #include <openssl/core_numbers.h>
14 #include <openssl/core_names.h>
15 #include <openssl/err.h>
16 #include <openssl/dsa.h>
17 #include <openssl/params.h>
18 #include <openssl/evp.h>
19 #include "internal/nelem.h"
20 #include "internal/sizes.h"
21 #include "prov/providercommonerr.h"
22 #include "prov/implementations.h"
23 #include "prov/provider_ctx.h"
24 #include "crypto/dsa.h"
26 static OSSL_OP_signature_newctx_fn dsa_newctx;
27 static OSSL_OP_signature_sign_init_fn dsa_signature_init;
28 static OSSL_OP_signature_verify_init_fn dsa_signature_init;
29 static OSSL_OP_signature_sign_fn dsa_sign;
30 static OSSL_OP_signature_verify_fn dsa_verify;
31 static OSSL_OP_signature_digest_sign_init_fn dsa_digest_signverify_init;
32 static OSSL_OP_signature_digest_sign_update_fn dsa_digest_signverify_update;
33 static OSSL_OP_signature_digest_sign_final_fn dsa_digest_sign_final;
34 static OSSL_OP_signature_digest_verify_init_fn dsa_digest_signverify_init;
35 static OSSL_OP_signature_digest_verify_update_fn dsa_digest_signverify_update;
36 static OSSL_OP_signature_digest_verify_final_fn dsa_digest_verify_final;
37 static OSSL_OP_signature_freectx_fn dsa_freectx;
38 static OSSL_OP_signature_dupctx_fn dsa_dupctx;
39 static OSSL_OP_signature_get_ctx_params_fn dsa_get_ctx_params;
40 static OSSL_OP_signature_gettable_ctx_params_fn dsa_gettable_ctx_params;
41 static OSSL_OP_signature_set_ctx_params_fn dsa_set_ctx_params;
42 static OSSL_OP_signature_settable_ctx_params_fn dsa_settable_ctx_params;
43 static OSSL_OP_signature_get_ctx_md_params_fn dsa_get_ctx_md_params;
44 static OSSL_OP_signature_gettable_ctx_md_params_fn dsa_gettable_ctx_md_params;
45 static OSSL_OP_signature_set_ctx_md_params_fn dsa_set_ctx_md_params;
46 static OSSL_OP_signature_settable_ctx_md_params_fn dsa_settable_ctx_md_params;
49 * What's passed as an actual key is defined by the KEYMGMT interface.
50 * We happen to know that our KEYMGMT simply passes DSA structures, so
51 * we use that here too.
58 char mdname[OSSL_MAX_NAME_SIZE];
60 /* The Algorithm Identifier of the combined signature agorithm */
61 unsigned char aid[OSSL_MAX_ALGORITHM_ID_SIZE];
69 static void *dsa_newctx(void *provctx)
71 PROV_DSA_CTX *pdsactx = OPENSSL_zalloc(sizeof(PROV_DSA_CTX));
76 pdsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
80 static int dsa_signature_init(void *vpdsactx, void *vdsa)
82 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
84 if (pdsactx == NULL || vdsa == NULL || !DSA_up_ref(vdsa))
86 DSA_free(pdsactx->dsa);
91 static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen,
92 size_t sigsize, const unsigned char *tbs, size_t tbslen)
94 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
97 size_t dsasize = DSA_size(pdsactx->dsa);
104 if (sigsize < (size_t)dsasize)
107 if (pdsactx->mdsize != 0 && tbslen != pdsactx->mdsize)
110 ret = dsa_sign_int(pdsactx->libctx, 0, tbs, tbslen, sig, &sltmp,
119 static int dsa_verify(void *vpdsactx, const unsigned char *sig, size_t siglen,
120 const unsigned char *tbs, size_t tbslen)
122 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
124 if (pdsactx->mdsize != 0 && tbslen != pdsactx->mdsize)
127 return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa);
130 static int get_md_nid(const EVP_MD *md)
133 * Because the DSA library deals with NIDs, we need to translate.
134 * We do so using EVP_MD_is_a(), and therefore need a name to NID
137 static const OSSL_ITEM name_to_nid[] = {
138 { NID_sha1, OSSL_DIGEST_NAME_SHA1 },
139 { NID_sha224, OSSL_DIGEST_NAME_SHA2_224 },
140 { NID_sha256, OSSL_DIGEST_NAME_SHA2_256 },
141 { NID_sha384, OSSL_DIGEST_NAME_SHA2_384 },
142 { NID_sha512, OSSL_DIGEST_NAME_SHA2_512 },
143 { NID_sha3_224, OSSL_DIGEST_NAME_SHA3_224 },
144 { NID_sha3_256, OSSL_DIGEST_NAME_SHA3_256 },
145 { NID_sha3_384, OSSL_DIGEST_NAME_SHA3_384 },
146 { NID_sha3_512, OSSL_DIGEST_NAME_SHA3_512 },
149 int mdnid = NID_undef;
154 for (i = 0; i < OSSL_NELEM(name_to_nid); i++) {
155 if (EVP_MD_is_a(md, name_to_nid[i].ptr)) {
156 mdnid = (int)name_to_nid[i].id;
161 if (mdnid == NID_undef)
162 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
168 static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname,
169 const char *props, void *vdsa)
171 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
172 size_t algorithmidentifier_len = 0;
173 const unsigned char *algorithmidentifier;
175 EVP_MD_CTX_free(pdsactx->mdctx);
176 EVP_MD_free(pdsactx->md);
177 pdsactx->mdctx = NULL;
181 if (!dsa_signature_init(vpdsactx, vdsa))
184 pdsactx->md = EVP_MD_fetch(pdsactx->libctx, mdname, props);
185 algorithmidentifier =
186 dsa_algorithmidentifier_encoding(get_md_nid(pdsactx->md),
187 &algorithmidentifier_len);
189 if (algorithmidentifier == NULL)
192 pdsactx->mdsize = EVP_MD_size(pdsactx->md);
193 pdsactx->mdctx = EVP_MD_CTX_new();
194 if (pdsactx->mdctx == NULL)
197 memcpy(pdsactx->aid, algorithmidentifier, algorithmidentifier_len);
198 pdsactx->aid_len = algorithmidentifier_len;
200 if (!EVP_DigestInit_ex(pdsactx->mdctx, pdsactx->md, NULL))
206 EVP_MD_CTX_free(pdsactx->mdctx);
207 EVP_MD_free(pdsactx->md);
208 pdsactx->mdctx = NULL;
214 int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data,
217 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
219 if (pdsactx == NULL || pdsactx->mdctx == NULL)
222 return EVP_DigestUpdate(pdsactx->mdctx, data, datalen);
225 int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig, size_t *siglen,
228 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
229 unsigned char digest[EVP_MAX_MD_SIZE];
230 unsigned int dlen = 0;
232 if (pdsactx == NULL || pdsactx->mdctx == NULL)
236 * If sig is NULL then we're just finding out the sig size. Other fields
237 * are ignored. Defer to dsa_sign.
241 * TODO(3.0): There is the possibility that some externally provided
242 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
243 * but that problem is much larger than just in DSA.
245 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
249 return dsa_sign(vpdsactx, sig, siglen, sigsize, digest, (size_t)dlen);
253 int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig,
256 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
257 unsigned char digest[EVP_MAX_MD_SIZE];
258 unsigned int dlen = 0;
260 if (pdsactx == NULL || pdsactx->mdctx == NULL)
264 * TODO(3.0): There is the possibility that some externally provided
265 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
266 * but that problem is much larger than just in DSA.
268 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
271 return dsa_verify(vpdsactx, sig, siglen, digest, (size_t)dlen);
274 static void dsa_freectx(void *vpdsactx)
276 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
278 DSA_free(pdsactx->dsa);
279 EVP_MD_CTX_free(pdsactx->mdctx);
280 EVP_MD_free(pdsactx->md);
282 OPENSSL_free(pdsactx);
285 static void *dsa_dupctx(void *vpdsactx)
287 PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx;
288 PROV_DSA_CTX *dstctx;
290 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
297 dstctx->mdctx = NULL;
299 if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa))
301 dstctx->dsa = srcctx->dsa;
303 if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
305 dstctx->md = srcctx->md;
307 if (srcctx->mdctx != NULL) {
308 dstctx->mdctx = EVP_MD_CTX_new();
309 if (dstctx->mdctx == NULL
310 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
320 static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params)
322 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
325 if (pdsactx == NULL || params == NULL)
328 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
330 && !OSSL_PARAM_set_octet_string(p, pdsactx->aid, pdsactx->aid_len))
333 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
334 if (p != NULL && !OSSL_PARAM_set_size_t(p, pdsactx->mdsize))
337 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
338 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->md == NULL
340 : EVP_MD_name(pdsactx->md)))
346 static const OSSL_PARAM known_gettable_ctx_params[] = {
347 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
348 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
349 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
353 static const OSSL_PARAM *dsa_gettable_ctx_params(void)
355 return known_gettable_ctx_params;
358 static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])
360 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
364 if (pdsactx == NULL || params == NULL)
367 if (pdsactx->md != NULL) {
369 * You cannot set the digest name/size when doing a DigestSign or
375 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
376 if (p != NULL && !OSSL_PARAM_get_size_t(p, &pdsactx->mdsize))
380 * We never actually use the mdname, but we do support getting it later.
381 * This can be useful for applications that want to know the MD that they
384 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
385 mdname = pdsactx->mdname;
387 && !OSSL_PARAM_get_utf8_string(p, &mdname, sizeof(pdsactx->mdname)))
393 static const OSSL_PARAM known_settable_ctx_params[] = {
394 OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
395 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
399 static const OSSL_PARAM *dsa_settable_ctx_params(void)
402 * TODO(3.0): Should this function return a different set of settable ctx
403 * params if the ctx is being used for a DigestSign/DigestVerify? In that
404 * case it is not allowed to set the digest size/digest name because the
405 * digest is explicitly set as part of the init.
407 return known_settable_ctx_params;
410 static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params)
412 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
414 if (pdsactx->mdctx == NULL)
417 return EVP_MD_CTX_get_params(pdsactx->mdctx, params);
420 static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx)
422 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
424 if (pdsactx->md == NULL)
427 return EVP_MD_gettable_ctx_params(pdsactx->md);
430 static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[])
432 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
434 if (pdsactx->mdctx == NULL)
437 return EVP_MD_CTX_set_params(pdsactx->mdctx, params);
440 static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx)
442 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
444 if (pdsactx->md == NULL)
447 return EVP_MD_settable_ctx_params(pdsactx->md);
450 const OSSL_DISPATCH dsa_signature_functions[] = {
451 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx },
452 { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_signature_init },
453 { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign },
454 { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_signature_init },
455 { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify },
456 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
457 (void (*)(void))dsa_digest_signverify_init },
458 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
459 (void (*)(void))dsa_digest_signverify_update },
460 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
461 (void (*)(void))dsa_digest_sign_final },
462 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
463 (void (*)(void))dsa_digest_signverify_init },
464 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
465 (void (*)(void))dsa_digest_signverify_update },
466 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
467 (void (*)(void))dsa_digest_verify_final },
468 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx },
469 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx },
470 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params },
471 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
472 (void (*)(void))dsa_gettable_ctx_params },
473 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params },
474 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
475 (void (*)(void))dsa_settable_ctx_params },
476 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
477 (void (*)(void))dsa_get_ctx_md_params },
478 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
479 (void (*)(void))dsa_gettable_ctx_md_params },
480 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
481 (void (*)(void))dsa_set_ctx_md_params },
482 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
483 (void (*)(void))dsa_settable_ctx_md_params },