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/asn1t.h>
13 #include <openssl/x509.h>
14 #include <openssl/evp.h>
15 #include <openssl/bn.h>
16 #include "internal/evp_int.h"
19 /* DSA pkey context structure */
22 /* Parameter gen parameters */
23 int nbits; /* size of p in bits (default: 1024) */
24 int qbits; /* size of q in bits (default: 160) */
25 const EVP_MD *pmd; /* MD for parameter generation */
26 /* Keygen callback info */
29 const EVP_MD *md; /* MD for the signature */
32 static int pkey_dsa_init(EVP_PKEY_CTX *ctx)
35 dctx = OPENSSL_malloc(sizeof(*dctx));
44 ctx->keygen_info = dctx->gentmp;
45 ctx->keygen_info_count = 2;
50 static int pkey_dsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
52 DSA_PKEY_CTX *dctx, *sctx;
53 if (!pkey_dsa_init(dst))
57 dctx->nbits = sctx->nbits;
58 dctx->qbits = sctx->qbits;
59 dctx->pmd = sctx->pmd;
64 static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
66 DSA_PKEY_CTX *dctx = ctx->data;
70 static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
71 size_t *siglen, const unsigned char *tbs,
76 DSA_PKEY_CTX *dctx = ctx->data;
77 DSA *dsa = ctx->pkey->pkey.dsa;
80 if (tbslen != (size_t)EVP_MD_size(dctx->md))
83 if (tbslen != SHA_DIGEST_LENGTH)
87 ret = DSA_sign(0, tbs, tbslen, sig, &sltmp, dsa);
95 static int pkey_dsa_verify(EVP_PKEY_CTX *ctx,
96 const unsigned char *sig, size_t siglen,
97 const unsigned char *tbs, size_t tbslen)
100 DSA_PKEY_CTX *dctx = ctx->data;
101 DSA *dsa = ctx->pkey->pkey.dsa;
104 if (tbslen != (size_t)EVP_MD_size(dctx->md))
107 if (tbslen != SHA_DIGEST_LENGTH)
111 ret = DSA_verify(0, tbs, tbslen, sig, siglen, dsa);
116 static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
118 DSA_PKEY_CTX *dctx = ctx->data;
120 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
126 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
127 if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
132 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
133 if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
134 EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
135 EVP_MD_type((const EVP_MD *)p2) != NID_sha256) {
136 DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE);
142 case EVP_PKEY_CTRL_MD:
143 if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
144 EVP_MD_type((const EVP_MD *)p2) != NID_dsa &&
145 EVP_MD_type((const EVP_MD *)p2) != NID_dsaWithSHA &&
146 EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
147 EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
148 EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
149 EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
150 DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE);
156 case EVP_PKEY_CTRL_GET_MD:
157 *(const EVP_MD **)p2 = dctx->md;
160 case EVP_PKEY_CTRL_DIGESTINIT:
161 case EVP_PKEY_CTRL_PKCS7_SIGN:
162 case EVP_PKEY_CTRL_CMS_SIGN:
165 case EVP_PKEY_CTRL_PEER_KEY:
166 DSAerr(DSA_F_PKEY_DSA_CTRL,
167 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
175 static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
176 const char *type, const char *value)
178 if (strcmp(type, "dsa_paramgen_bits") == 0) {
181 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
183 if (strcmp(type, "dsa_paramgen_q_bits") == 0) {
184 int qbits = atoi(value);
185 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
186 EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, qbits,
189 if (strcmp(type, "dsa_paramgen_md") == 0) {
190 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
191 EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0,
192 (void *)EVP_get_digestbyname(value));
197 static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
200 DSA_PKEY_CTX *dctx = ctx->data;
203 if (ctx->pkey_gencb) {
204 pcb = BN_GENCB_new();
207 evp_pkey_set_cb_translate(pcb, ctx);
215 ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd,
216 NULL, 0, NULL, NULL, NULL, pcb);
219 EVP_PKEY_assign_DSA(pkey, dsa);
225 static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
228 if (ctx->pkey == NULL) {
229 DSAerr(DSA_F_PKEY_DSA_KEYGEN, DSA_R_NO_PARAMETERS_SET);
235 EVP_PKEY_assign_DSA(pkey, dsa);
236 /* Note: if error return, pkey is freed by parent routine */
237 if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
239 return DSA_generate_key(pkey->pkey.dsa);
242 const EVP_PKEY_METHOD dsa_pkey_meth = {
244 EVP_PKEY_FLAG_AUTOARGLEN,