2 * Copyright 1999-2016 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 #include "internal/cryptlib.h"
12 #include "dsa_local.h"
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/rand.h>
16 #include "crypto/asn1_dsa.h"
18 DSA_SIG *DSA_SIG_new(void)
20 DSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
22 DSAerr(DSA_F_DSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
26 void DSA_SIG_free(DSA_SIG *sig)
30 BN_clear_free(sig->r);
31 BN_clear_free(sig->s);
35 DSA_SIG *d2i_DSA_SIG(DSA_SIG **psig, const unsigned char **ppin, long len)
41 if (psig != NULL && *psig != NULL) {
52 if (decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
53 if (psig == NULL || *psig == NULL)
57 if (psig != NULL && *psig == NULL)
62 int i2d_DSA_SIG(const DSA_SIG *sig, unsigned char **ppout)
69 if (!WPACKET_init_null(&pkt, 0))
71 } else if (*ppout == NULL) {
72 if ((buf = BUF_MEM_new()) == NULL
73 || !WPACKET_init_len(&pkt, buf, 0)) {
78 if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0))
82 if (!encode_der_dsa_sig(&pkt, sig->r, sig->s)
83 || !WPACKET_get_total_written(&pkt, &encoded_len)
84 || !WPACKET_finish(&pkt)) {
86 WPACKET_cleanup(&pkt);
92 *ppout = (unsigned char *)buf->data;
96 *ppout += encoded_len;
100 return (int)encoded_len;
103 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
111 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
113 if (r == NULL || s == NULL)
115 BN_clear_free(sig->r);
116 BN_clear_free(sig->s);
122 /* Override the default free and new methods */
123 static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
126 if (operation == ASN1_OP_NEW_PRE) {
127 *pval = (ASN1_VALUE *)DSA_new();
131 } else if (operation == ASN1_OP_FREE_PRE) {
132 DSA_free((DSA *)*pval);
139 ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
140 ASN1_EMBED(DSA, version, INT32),
141 ASN1_SIMPLE(DSA, p, BIGNUM),
142 ASN1_SIMPLE(DSA, q, BIGNUM),
143 ASN1_SIMPLE(DSA, g, BIGNUM),
144 ASN1_SIMPLE(DSA, pub_key, BIGNUM),
145 ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
146 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
148 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPrivateKey, DSAPrivateKey)
150 ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
151 ASN1_SIMPLE(DSA, p, BIGNUM),
152 ASN1_SIMPLE(DSA, q, BIGNUM),
153 ASN1_SIMPLE(DSA, g, BIGNUM),
154 } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
156 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAparams, DSAparams)
158 ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
159 ASN1_SIMPLE(DSA, pub_key, BIGNUM),
160 ASN1_SIMPLE(DSA, p, BIGNUM),
161 ASN1_SIMPLE(DSA, q, BIGNUM),
162 ASN1_SIMPLE(DSA, g, BIGNUM)
163 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
165 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPublicKey, DSAPublicKey)
167 DSA *DSAparams_dup(const DSA *dsa)
169 return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
172 int DSA_sign(int type, const unsigned char *dgst, int dlen,
173 unsigned char *sig, unsigned int *siglen, DSA *dsa)
177 s = DSA_do_sign(dgst, dlen, dsa);
182 *siglen = i2d_DSA_SIG(s, &sig);
187 /* data has already been hashed (probably with SHA or SHA-1). */
190 * 1: correct signature
191 * 0: incorrect signature
194 int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
195 const unsigned char *sigbuf, int siglen, DSA *dsa)
198 const unsigned char *p = sigbuf;
199 unsigned char *der = NULL;
206 if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
208 /* Ensure signature uses DER and doesn't have trailing garbage */
209 derlen = i2d_DSA_SIG(s, &der);
210 if (derlen != siglen || memcmp(sigbuf, der, derlen))
212 ret = DSA_do_verify(dgst, dgst_len, s, dsa);
214 OPENSSL_clear_free(der, derlen);