2 * Copyright 1995-2018 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/bn.h>
11 #include "internal/cryptlib.h"
12 #include "dsa_local.h"
13 #include "crypto/asn1_dsa.h"
14 #include "crypto/dsa.h"
16 DSA_SIG *DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
18 return dsa->meth->dsa_do_sign(dgst, dlen, dsa);
21 #ifndef OPENSSL_NO_DEPRECATED_3_0
22 int DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
24 return dsa->meth->dsa_sign_setup(dsa, ctx_in, kinvp, rp);
28 DSA_SIG *DSA_SIG_new(void)
30 DSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
32 DSAerr(DSA_F_DSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
36 void DSA_SIG_free(DSA_SIG *sig)
40 BN_clear_free(sig->r);
41 BN_clear_free(sig->s);
45 DSA_SIG *d2i_DSA_SIG(DSA_SIG **psig, const unsigned char **ppin, long len)
51 if (psig != NULL && *psig != NULL) {
62 if (decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
63 if (psig == NULL || *psig == NULL)
67 if (psig != NULL && *psig == NULL)
72 int i2d_DSA_SIG(const DSA_SIG *sig, unsigned char **ppout)
79 if (!WPACKET_init_null(&pkt, 0))
81 } else if (*ppout == NULL) {
82 if ((buf = BUF_MEM_new()) == NULL
83 || !WPACKET_init_len(&pkt, buf, 0)) {
88 if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0))
92 if (!encode_der_dsa_sig(&pkt, sig->r, sig->s)
93 || !WPACKET_get_total_written(&pkt, &encoded_len)
94 || !WPACKET_finish(&pkt)) {
96 WPACKET_cleanup(&pkt);
101 if (*ppout == NULL) {
102 *ppout = (unsigned char *)buf->data;
106 *ppout += encoded_len;
110 return (int)encoded_len;
113 int DSA_size(const DSA *dsa)
118 sig.r = sig.s = dsa->params.q;
119 ret = i2d_DSA_SIG(&sig, NULL);
126 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
134 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
136 if (r == NULL || s == NULL)
138 BN_clear_free(sig->r);
139 BN_clear_free(sig->s);
145 int dsa_sign_int(OPENSSL_CTX *libctx, int type, const unsigned char *dgst,
146 int dlen, unsigned char *sig, unsigned int *siglen, DSA *dsa)
150 /* legacy case uses the method table */
151 if (libctx == NULL || dsa->meth != DSA_get_default_method())
152 s = DSA_do_sign(dgst, dlen, dsa);
154 s = dsa_do_sign_int(libctx, dgst, dlen, dsa);
159 *siglen = i2d_DSA_SIG(s, &sig);
164 int DSA_sign(int type, const unsigned char *dgst, int dlen,
165 unsigned char *sig, unsigned int *siglen, DSA *dsa)
167 return dsa_sign_int(NULL, type, dgst, dlen, sig, siglen, dsa);
170 /* data has already been hashed (probably with SHA or SHA-1). */
173 * 1: correct signature
174 * 0: incorrect signature
177 int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
178 const unsigned char *sigbuf, int siglen, DSA *dsa)
181 const unsigned char *p = sigbuf;
182 unsigned char *der = NULL;
189 if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
191 /* Ensure signature uses DER and doesn't have trailing garbage */
192 derlen = i2d_DSA_SIG(s, &der);
193 if (derlen != siglen || memcmp(sigbuf, der, derlen))
195 ret = DSA_do_verify(dgst, dgst_len, s, dsa);
197 OPENSSL_clear_free(der, derlen);