xref: /freebsd/crypto/openssl/crypto/dsa/dsa_pmeth.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
16f9291ceSJung-uk Kim /*
2*b077aed3SPierre Pronchery  * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
31f13597dSJung-uk Kim  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
81f13597dSJung-uk Kim  */
91f13597dSJung-uk Kim 
10*b077aed3SPierre Pronchery /*
11*b077aed3SPierre Pronchery  * DSA low level APIs are deprecated for public use, but still ok for
12*b077aed3SPierre Pronchery  * internal use.
13*b077aed3SPierre Pronchery  */
14*b077aed3SPierre Pronchery #include "internal/deprecated.h"
15*b077aed3SPierre Pronchery 
161f13597dSJung-uk Kim #include <stdio.h>
17e71b7053SJung-uk Kim #include "internal/cryptlib.h"
181f13597dSJung-uk Kim #include <openssl/asn1t.h>
191f13597dSJung-uk Kim #include <openssl/x509.h>
201f13597dSJung-uk Kim #include <openssl/evp.h>
211f13597dSJung-uk Kim #include <openssl/bn.h>
2217f01e99SJung-uk Kim #include "crypto/evp.h"
2317f01e99SJung-uk Kim #include "dsa_local.h"
241f13597dSJung-uk Kim 
251f13597dSJung-uk Kim /* DSA pkey context structure */
261f13597dSJung-uk Kim 
276f9291ceSJung-uk Kim typedef struct {
281f13597dSJung-uk Kim     /* Parameter gen parameters */
29610a21fdSJung-uk Kim     int nbits;                  /* size of p in bits (default: 2048) */
30610a21fdSJung-uk Kim     int qbits;                  /* size of q in bits (default: 224) */
311f13597dSJung-uk Kim     const EVP_MD *pmd;          /* MD for parameter generation */
321f13597dSJung-uk Kim     /* Keygen callback info */
331f13597dSJung-uk Kim     int gentmp[2];
341f13597dSJung-uk Kim     /* message digest */
351f13597dSJung-uk Kim     const EVP_MD *md;           /* MD for the signature */
361f13597dSJung-uk Kim } DSA_PKEY_CTX;
371f13597dSJung-uk Kim 
pkey_dsa_init(EVP_PKEY_CTX * ctx)381f13597dSJung-uk Kim static int pkey_dsa_init(EVP_PKEY_CTX *ctx)
391f13597dSJung-uk Kim {
40e71b7053SJung-uk Kim     DSA_PKEY_CTX *dctx = OPENSSL_malloc(sizeof(*dctx));
41e71b7053SJung-uk Kim 
42e71b7053SJung-uk Kim     if (dctx == NULL)
431f13597dSJung-uk Kim         return 0;
44610a21fdSJung-uk Kim     dctx->nbits = 2048;
45610a21fdSJung-uk Kim     dctx->qbits = 224;
461f13597dSJung-uk Kim     dctx->pmd = NULL;
471f13597dSJung-uk Kim     dctx->md = NULL;
481f13597dSJung-uk Kim 
491f13597dSJung-uk Kim     ctx->data = dctx;
501f13597dSJung-uk Kim     ctx->keygen_info = dctx->gentmp;
511f13597dSJung-uk Kim     ctx->keygen_info_count = 2;
521f13597dSJung-uk Kim 
531f13597dSJung-uk Kim     return 1;
541f13597dSJung-uk Kim }
551f13597dSJung-uk Kim 
pkey_dsa_copy(EVP_PKEY_CTX * dst,const EVP_PKEY_CTX * src)56*b077aed3SPierre Pronchery static int pkey_dsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
571f13597dSJung-uk Kim {
581f13597dSJung-uk Kim     DSA_PKEY_CTX *dctx, *sctx;
59e71b7053SJung-uk Kim 
601f13597dSJung-uk Kim     if (!pkey_dsa_init(dst))
611f13597dSJung-uk Kim         return 0;
621f13597dSJung-uk Kim     sctx = src->data;
631f13597dSJung-uk Kim     dctx = dst->data;
641f13597dSJung-uk Kim     dctx->nbits = sctx->nbits;
651f13597dSJung-uk Kim     dctx->qbits = sctx->qbits;
661f13597dSJung-uk Kim     dctx->pmd = sctx->pmd;
671f13597dSJung-uk Kim     dctx->md = sctx->md;
681f13597dSJung-uk Kim     return 1;
691f13597dSJung-uk Kim }
701f13597dSJung-uk Kim 
pkey_dsa_cleanup(EVP_PKEY_CTX * ctx)711f13597dSJung-uk Kim static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
721f13597dSJung-uk Kim {
731f13597dSJung-uk Kim     DSA_PKEY_CTX *dctx = ctx->data;
741f13597dSJung-uk Kim     OPENSSL_free(dctx);
751f13597dSJung-uk Kim }
761f13597dSJung-uk Kim 
pkey_dsa_sign(EVP_PKEY_CTX * ctx,unsigned char * sig,size_t * siglen,const unsigned char * tbs,size_t tbslen)776f9291ceSJung-uk Kim static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
786f9291ceSJung-uk Kim                          size_t *siglen, const unsigned char *tbs,
796f9291ceSJung-uk Kim                          size_t tbslen)
801f13597dSJung-uk Kim {
81e71b7053SJung-uk Kim     int ret;
821f13597dSJung-uk Kim     unsigned int sltmp;
831f13597dSJung-uk Kim     DSA_PKEY_CTX *dctx = ctx->data;
84*b077aed3SPierre Pronchery     /*
85*b077aed3SPierre Pronchery      * Discard const. Its marked as const because this may be a cached copy of
86*b077aed3SPierre Pronchery      * the "real" key. These calls don't make any modifications that need to
87*b077aed3SPierre Pronchery      * be reflected back in the "original" key.
88*b077aed3SPierre Pronchery      */
89*b077aed3SPierre Pronchery     DSA *dsa = (DSA *)EVP_PKEY_get0_DSA(ctx->pkey);
901f13597dSJung-uk Kim 
91*b077aed3SPierre Pronchery     if (dctx->md != NULL && tbslen != (size_t)EVP_MD_get_size(dctx->md))
92e71b7053SJung-uk Kim         return 0;
931f13597dSJung-uk Kim 
94e71b7053SJung-uk Kim     ret = DSA_sign(0, tbs, tbslen, sig, &sltmp, dsa);
951f13597dSJung-uk Kim 
961f13597dSJung-uk Kim     if (ret <= 0)
971f13597dSJung-uk Kim         return ret;
981f13597dSJung-uk Kim     *siglen = sltmp;
991f13597dSJung-uk Kim     return 1;
1001f13597dSJung-uk Kim }
1011f13597dSJung-uk Kim 
pkey_dsa_verify(EVP_PKEY_CTX * ctx,const unsigned char * sig,size_t siglen,const unsigned char * tbs,size_t tbslen)1021f13597dSJung-uk Kim static int pkey_dsa_verify(EVP_PKEY_CTX *ctx,
1031f13597dSJung-uk Kim                            const unsigned char *sig, size_t siglen,
1041f13597dSJung-uk Kim                            const unsigned char *tbs, size_t tbslen)
1051f13597dSJung-uk Kim {
106e71b7053SJung-uk Kim     int ret;
1071f13597dSJung-uk Kim     DSA_PKEY_CTX *dctx = ctx->data;
108*b077aed3SPierre Pronchery     /*
109*b077aed3SPierre Pronchery      * Discard const. Its marked as const because this may be a cached copy of
110*b077aed3SPierre Pronchery      * the "real" key. These calls don't make any modifications that need to
111*b077aed3SPierre Pronchery      * be reflected back in the "original" key.
112*b077aed3SPierre Pronchery      */
113*b077aed3SPierre Pronchery     DSA *dsa = (DSA *)EVP_PKEY_get0_DSA(ctx->pkey);
1141f13597dSJung-uk Kim 
115*b077aed3SPierre Pronchery     if (dctx->md != NULL && tbslen != (size_t)EVP_MD_get_size(dctx->md))
116e71b7053SJung-uk Kim         return 0;
1171f13597dSJung-uk Kim 
118e71b7053SJung-uk Kim     ret = DSA_verify(0, tbs, tbslen, sig, siglen, dsa);
1191f13597dSJung-uk Kim 
1201f13597dSJung-uk Kim     return ret;
1211f13597dSJung-uk Kim }
1221f13597dSJung-uk Kim 
pkey_dsa_ctrl(EVP_PKEY_CTX * ctx,int type,int p1,void * p2)1231f13597dSJung-uk Kim static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
1241f13597dSJung-uk Kim {
1251f13597dSJung-uk Kim     DSA_PKEY_CTX *dctx = ctx->data;
126e71b7053SJung-uk Kim 
1276f9291ceSJung-uk Kim     switch (type) {
1281f13597dSJung-uk Kim     case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
1291f13597dSJung-uk Kim         if (p1 < 256)
1301f13597dSJung-uk Kim             return -2;
1311f13597dSJung-uk Kim         dctx->nbits = p1;
1321f13597dSJung-uk Kim         return 1;
1331f13597dSJung-uk Kim 
1341f13597dSJung-uk Kim     case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
1351f13597dSJung-uk Kim         if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
1361f13597dSJung-uk Kim             return -2;
1371f13597dSJung-uk Kim         dctx->qbits = p1;
1381f13597dSJung-uk Kim         return 1;
1391f13597dSJung-uk Kim 
1401f13597dSJung-uk Kim     case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
141*b077aed3SPierre Pronchery         if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
142*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
143*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256) {
144*b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
1451f13597dSJung-uk Kim             return 0;
1461f13597dSJung-uk Kim         }
1476cf8931aSJung-uk Kim         dctx->pmd = p2;
1481f13597dSJung-uk Kim         return 1;
1491f13597dSJung-uk Kim 
1501f13597dSJung-uk Kim     case EVP_PKEY_CTRL_MD:
151*b077aed3SPierre Pronchery         if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
152*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_dsa &&
153*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_dsaWithSHA &&
154*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
155*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256 &&
156*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha384 &&
157*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha512 &&
158*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_224 &&
159*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_256 &&
160*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_384 &&
161*b077aed3SPierre Pronchery             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_512) {
162*b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
1631f13597dSJung-uk Kim             return 0;
1641f13597dSJung-uk Kim         }
1651f13597dSJung-uk Kim         dctx->md = p2;
1661f13597dSJung-uk Kim         return 1;
1671f13597dSJung-uk Kim 
1687bded2dbSJung-uk Kim     case EVP_PKEY_CTRL_GET_MD:
1697bded2dbSJung-uk Kim         *(const EVP_MD **)p2 = dctx->md;
1707bded2dbSJung-uk Kim         return 1;
1717bded2dbSJung-uk Kim 
1721f13597dSJung-uk Kim     case EVP_PKEY_CTRL_DIGESTINIT:
1731f13597dSJung-uk Kim     case EVP_PKEY_CTRL_PKCS7_SIGN:
1741f13597dSJung-uk Kim     case EVP_PKEY_CTRL_CMS_SIGN:
1751f13597dSJung-uk Kim         return 1;
1761f13597dSJung-uk Kim 
1771f13597dSJung-uk Kim     case EVP_PKEY_CTRL_PEER_KEY:
178*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_DSA, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1791f13597dSJung-uk Kim         return -2;
1801f13597dSJung-uk Kim     default:
1811f13597dSJung-uk Kim         return -2;
1821f13597dSJung-uk Kim 
1831f13597dSJung-uk Kim     }
1841f13597dSJung-uk Kim }
1851f13597dSJung-uk Kim 
pkey_dsa_ctrl_str(EVP_PKEY_CTX * ctx,const char * type,const char * value)1861f13597dSJung-uk Kim static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
1871f13597dSJung-uk Kim                              const char *type, const char *value)
1881f13597dSJung-uk Kim {
189e71b7053SJung-uk Kim     if (strcmp(type, "dsa_paramgen_bits") == 0) {
1901f13597dSJung-uk Kim         int nbits;
1911f13597dSJung-uk Kim         nbits = atoi(value);
1921f13597dSJung-uk Kim         return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
1931f13597dSJung-uk Kim     }
194e71b7053SJung-uk Kim     if (strcmp(type, "dsa_paramgen_q_bits") == 0) {
1951f13597dSJung-uk Kim         int qbits = atoi(value);
19617f01e99SJung-uk Kim         return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, qbits);
1971f13597dSJung-uk Kim     }
198dea77ea6SJung-uk Kim     if (strcmp(type, "dsa_paramgen_md") == 0) {
199dea77ea6SJung-uk Kim         const EVP_MD *md = EVP_get_digestbyname(value);
200dea77ea6SJung-uk Kim 
201dea77ea6SJung-uk Kim         if (md == NULL) {
202*b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
203dea77ea6SJung-uk Kim             return 0;
204dea77ea6SJung-uk Kim         }
20517f01e99SJung-uk Kim         return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, md);
2061f13597dSJung-uk Kim     }
2071f13597dSJung-uk Kim     return -2;
2081f13597dSJung-uk Kim }
2091f13597dSJung-uk Kim 
pkey_dsa_paramgen(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey)2101f13597dSJung-uk Kim static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
2111f13597dSJung-uk Kim {
2121f13597dSJung-uk Kim     DSA *dsa = NULL;
2131f13597dSJung-uk Kim     DSA_PKEY_CTX *dctx = ctx->data;
214e71b7053SJung-uk Kim     BN_GENCB *pcb;
215*b077aed3SPierre Pronchery     int ret, res;
216e71b7053SJung-uk Kim 
2176f9291ceSJung-uk Kim     if (ctx->pkey_gencb) {
218e71b7053SJung-uk Kim         pcb = BN_GENCB_new();
219e71b7053SJung-uk Kim         if (pcb == NULL)
220e71b7053SJung-uk Kim             return 0;
2211f13597dSJung-uk Kim         evp_pkey_set_cb_translate(pcb, ctx);
2226f9291ceSJung-uk Kim     } else
2231f13597dSJung-uk Kim         pcb = NULL;
2241f13597dSJung-uk Kim     dsa = DSA_new();
225e71b7053SJung-uk Kim     if (dsa == NULL) {
226e71b7053SJung-uk Kim         BN_GENCB_free(pcb);
2271f13597dSJung-uk Kim         return 0;
228e71b7053SJung-uk Kim     }
229*b077aed3SPierre Pronchery     if (dctx->md != NULL)
230*b077aed3SPierre Pronchery         ossl_ffc_set_digest(&dsa->params, EVP_MD_get0_name(dctx->md), NULL);
231*b077aed3SPierre Pronchery 
232*b077aed3SPierre Pronchery     ret = ossl_ffc_params_FIPS186_4_generate(NULL, &dsa->params,
233*b077aed3SPierre Pronchery                                              FFC_PARAM_TYPE_DSA, dctx->nbits,
234*b077aed3SPierre Pronchery                                              dctx->qbits, &res, pcb);
235e71b7053SJung-uk Kim     BN_GENCB_free(pcb);
236*b077aed3SPierre Pronchery     if (ret > 0)
2371f13597dSJung-uk Kim         EVP_PKEY_assign_DSA(pkey, dsa);
2381f13597dSJung-uk Kim     else
2391f13597dSJung-uk Kim         DSA_free(dsa);
2401f13597dSJung-uk Kim     return ret;
2411f13597dSJung-uk Kim }
2421f13597dSJung-uk Kim 
pkey_dsa_keygen(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey)2431f13597dSJung-uk Kim static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
2441f13597dSJung-uk Kim {
2451f13597dSJung-uk Kim     DSA *dsa = NULL;
246e71b7053SJung-uk Kim 
2476f9291ceSJung-uk Kim     if (ctx->pkey == NULL) {
248*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_DSA, DSA_R_NO_PARAMETERS_SET);
2491f13597dSJung-uk Kim         return 0;
2501f13597dSJung-uk Kim     }
2511f13597dSJung-uk Kim     dsa = DSA_new();
252e71b7053SJung-uk Kim     if (dsa == NULL)
2531f13597dSJung-uk Kim         return 0;
2541f13597dSJung-uk Kim     EVP_PKEY_assign_DSA(pkey, dsa);
2551f13597dSJung-uk Kim     /* Note: if error return, pkey is freed by parent routine */
2561f13597dSJung-uk Kim     if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
2571f13597dSJung-uk Kim         return 0;
258*b077aed3SPierre Pronchery     return DSA_generate_key((DSA *)EVP_PKEY_get0_DSA(pkey));
2591f13597dSJung-uk Kim }
2601f13597dSJung-uk Kim 
261*b077aed3SPierre Pronchery static const EVP_PKEY_METHOD dsa_pkey_meth = {
2621f13597dSJung-uk Kim     EVP_PKEY_DSA,
2631f13597dSJung-uk Kim     EVP_PKEY_FLAG_AUTOARGLEN,
2641f13597dSJung-uk Kim     pkey_dsa_init,
2651f13597dSJung-uk Kim     pkey_dsa_copy,
2661f13597dSJung-uk Kim     pkey_dsa_cleanup,
2671f13597dSJung-uk Kim 
2681f13597dSJung-uk Kim     0,
2691f13597dSJung-uk Kim     pkey_dsa_paramgen,
2701f13597dSJung-uk Kim 
2711f13597dSJung-uk Kim     0,
2721f13597dSJung-uk Kim     pkey_dsa_keygen,
2731f13597dSJung-uk Kim 
2741f13597dSJung-uk Kim     0,
2751f13597dSJung-uk Kim     pkey_dsa_sign,
2761f13597dSJung-uk Kim 
2771f13597dSJung-uk Kim     0,
2781f13597dSJung-uk Kim     pkey_dsa_verify,
2791f13597dSJung-uk Kim 
2801f13597dSJung-uk Kim     0, 0,
2811f13597dSJung-uk Kim 
2821f13597dSJung-uk Kim     0, 0, 0, 0,
2831f13597dSJung-uk Kim 
2841f13597dSJung-uk Kim     0, 0,
2851f13597dSJung-uk Kim 
2861f13597dSJung-uk Kim     0, 0,
2871f13597dSJung-uk Kim 
2881f13597dSJung-uk Kim     0, 0,
2891f13597dSJung-uk Kim 
2901f13597dSJung-uk Kim     pkey_dsa_ctrl,
2911f13597dSJung-uk Kim     pkey_dsa_ctrl_str
2921f13597dSJung-uk Kim };
293*b077aed3SPierre Pronchery 
ossl_dsa_pkey_method(void)294*b077aed3SPierre Pronchery const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void)
295*b077aed3SPierre Pronchery {
296*b077aed3SPierre Pronchery     return &dsa_pkey_meth;
297*b077aed3SPierre Pronchery }
298