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"
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/rand.h>
16 #include "internal/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)
64 unsigned char *buf = NULL;
66 unsigned char **pp = NULL;
70 if (ppout != NULL && *ppout == NULL) {
71 if ((len = encode_der_dsa_sig(sig->r, sig->s, NULL, SIZE_MAX)) == 0)
73 buf = OPENSSL_malloc(len);
82 if ((encoded_len = encode_der_dsa_sig(sig->r, sig->s, pp, len)) == 0) {
88 return (int)encoded_len;
91 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
99 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
101 if (r == NULL || s == NULL)
103 BN_clear_free(sig->r);
104 BN_clear_free(sig->s);
110 /* Override the default free and new methods */
111 static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
114 if (operation == ASN1_OP_NEW_PRE) {
115 *pval = (ASN1_VALUE *)DSA_new();
119 } else if (operation == ASN1_OP_FREE_PRE) {
120 DSA_free((DSA *)*pval);
127 ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
128 ASN1_EMBED(DSA, version, INT32),
129 ASN1_SIMPLE(DSA, p, BIGNUM),
130 ASN1_SIMPLE(DSA, q, BIGNUM),
131 ASN1_SIMPLE(DSA, g, BIGNUM),
132 ASN1_SIMPLE(DSA, pub_key, BIGNUM),
133 ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
134 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
136 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPrivateKey, DSAPrivateKey)
138 ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
139 ASN1_SIMPLE(DSA, p, BIGNUM),
140 ASN1_SIMPLE(DSA, q, BIGNUM),
141 ASN1_SIMPLE(DSA, g, BIGNUM),
142 } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
144 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAparams, DSAparams)
146 ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
147 ASN1_SIMPLE(DSA, pub_key, BIGNUM),
148 ASN1_SIMPLE(DSA, p, BIGNUM),
149 ASN1_SIMPLE(DSA, q, BIGNUM),
150 ASN1_SIMPLE(DSA, g, BIGNUM)
151 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
153 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPublicKey, DSAPublicKey)
155 DSA *DSAparams_dup(const DSA *dsa)
157 return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
160 int DSA_sign(int type, const unsigned char *dgst, int dlen,
161 unsigned char *sig, unsigned int *siglen, DSA *dsa)
165 s = DSA_do_sign(dgst, dlen, dsa);
170 *siglen = i2d_DSA_SIG(s, &sig);
175 /* data has already been hashed (probably with SHA or SHA-1). */
178 * 1: correct signature
179 * 0: incorrect signature
182 int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
183 const unsigned char *sigbuf, int siglen, DSA *dsa)
186 const unsigned char *p = sigbuf;
187 unsigned char *der = NULL;
194 if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
196 /* Ensure signature uses DER and doesn't have trailing garbage */
197 derlen = i2d_DSA_SIG(s, &der);
198 if (derlen != siglen || memcmp(sigbuf, der, derlen))
200 ret = DSA_do_verify(dgst, dgst_len, s, dsa);
202 OPENSSL_clear_free(der, derlen);