2 * Copyright 2006-2021 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
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/asn1t.h>
19 #include <openssl/x509.h>
20 #include <openssl/evp.h>
21 #include <openssl/bn.h>
22 #include "crypto/evp.h"
23 #include "dsa_local.h"
25 /* DSA pkey context structure */
28 /* Parameter gen parameters */
29 int nbits; /* size of p in bits (default: 2048) */
30 int qbits; /* size of q in bits (default: 224) */
31 const EVP_MD *pmd; /* MD for parameter generation */
32 /* Keygen callback info */
35 const EVP_MD *md; /* MD for the signature */
38 static int pkey_dsa_init(EVP_PKEY_CTX *ctx)
40 DSA_PKEY_CTX *dctx = OPENSSL_malloc(sizeof(*dctx));
50 ctx->keygen_info = dctx->gentmp;
51 ctx->keygen_info_count = 2;
56 static int pkey_dsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
58 DSA_PKEY_CTX *dctx, *sctx;
60 if (!pkey_dsa_init(dst))
64 dctx->nbits = sctx->nbits;
65 dctx->qbits = sctx->qbits;
66 dctx->pmd = sctx->pmd;
71 static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
73 DSA_PKEY_CTX *dctx = ctx->data;
77 static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
78 size_t *siglen, const unsigned char *tbs,
83 DSA_PKEY_CTX *dctx = ctx->data;
85 * Discard const. Its marked as const because this may be a cached copy of
86 * the "real" key. These calls don't make any modifications that need to
87 * be reflected back in the "original" key.
89 DSA *dsa = (DSA *)EVP_PKEY_get0_DSA(ctx->pkey);
91 if (dctx->md != NULL && tbslen != (size_t)EVP_MD_get_size(dctx->md))
94 ret = DSA_sign(0, tbs, tbslen, sig, &sltmp, dsa);
102 static int pkey_dsa_verify(EVP_PKEY_CTX *ctx,
103 const unsigned char *sig, size_t siglen,
104 const unsigned char *tbs, size_t tbslen)
107 DSA_PKEY_CTX *dctx = ctx->data;
109 * Discard const. Its marked as const because this may be a cached copy of
110 * the "real" key. These calls don't make any modifications that need to
111 * be reflected back in the "original" key.
113 DSA *dsa = (DSA *)EVP_PKEY_get0_DSA(ctx->pkey);
115 if (dctx->md != NULL && tbslen != (size_t)EVP_MD_get_size(dctx->md))
118 ret = DSA_verify(0, tbs, tbslen, sig, siglen, dsa);
123 static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
125 DSA_PKEY_CTX *dctx = ctx->data;
128 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
134 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
135 if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
140 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
141 if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
142 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
143 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256) {
144 ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
150 case EVP_PKEY_CTRL_MD:
151 if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
152 EVP_MD_get_type((const EVP_MD *)p2) != NID_dsa &&
153 EVP_MD_get_type((const EVP_MD *)p2) != NID_dsaWithSHA &&
154 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
155 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256 &&
156 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha384 &&
157 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha512 &&
158 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_224 &&
159 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_256 &&
160 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_384 &&
161 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_512) {
162 ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
168 case EVP_PKEY_CTRL_GET_MD:
169 *(const EVP_MD **)p2 = dctx->md;
172 case EVP_PKEY_CTRL_DIGESTINIT:
173 case EVP_PKEY_CTRL_PKCS7_SIGN:
174 case EVP_PKEY_CTRL_CMS_SIGN:
177 case EVP_PKEY_CTRL_PEER_KEY:
178 ERR_raise(ERR_LIB_DSA, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
186 static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
187 const char *type, const char *value)
189 if (strcmp(type, "dsa_paramgen_bits") == 0) {
192 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
194 if (strcmp(type, "dsa_paramgen_q_bits") == 0) {
195 int qbits = atoi(value);
196 return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, qbits);
198 if (strcmp(type, "dsa_paramgen_md") == 0) {
199 const EVP_MD *md = EVP_get_digestbyname(value);
202 ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
205 return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, md);
210 static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
213 DSA_PKEY_CTX *dctx = ctx->data;
217 if (ctx->pkey_gencb) {
218 pcb = BN_GENCB_new();
221 evp_pkey_set_cb_translate(pcb, ctx);
229 if (dctx->md != NULL)
230 ossl_ffc_set_digest(&dsa->params, EVP_MD_get0_name(dctx->md), NULL);
232 ret = ossl_ffc_params_FIPS186_4_generate(NULL, &dsa->params,
233 FFC_PARAM_TYPE_DSA, dctx->nbits,
234 dctx->qbits, &res, pcb);
237 EVP_PKEY_assign_DSA(pkey, dsa);
243 static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
247 if (ctx->pkey == NULL) {
248 ERR_raise(ERR_LIB_DSA, DSA_R_NO_PARAMETERS_SET);
254 EVP_PKEY_assign_DSA(pkey, dsa);
255 /* Note: if error return, pkey is freed by parent routine */
256 if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
258 return DSA_generate_key((DSA *)EVP_PKEY_get0_DSA(pkey));
261 static const EVP_PKEY_METHOD dsa_pkey_meth = {
263 EVP_PKEY_FLAG_AUTOARGLEN,
294 const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void)
296 return &dsa_pkey_meth;