2 * Copyright 1999-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"
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/rand.h>
22 ASN1_SEQUENCE(DSA_SIG) = {
23 ASN1_SIMPLE(DSA_SIG, r, CBIGNUM),
24 ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
25 } static_ASN1_SEQUENCE_END(DSA_SIG)
27 IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG)
29 void DSA_SIG_get0(BIGNUM **pr, BIGNUM **ps, const DSA_SIG *sig)
35 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
37 BN_clear_free(sig->r);
38 BN_clear_free(sig->s);
44 /* Override the default free and new methods */
45 static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
48 if (operation == ASN1_OP_NEW_PRE) {
49 *pval = (ASN1_VALUE *)DSA_new();
53 } else if (operation == ASN1_OP_FREE_PRE) {
54 DSA_free((DSA *)*pval);
61 ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
62 ASN1_SIMPLE(DSA, version, LONG),
63 ASN1_SIMPLE(DSA, p, BIGNUM),
64 ASN1_SIMPLE(DSA, q, BIGNUM),
65 ASN1_SIMPLE(DSA, g, BIGNUM),
66 ASN1_SIMPLE(DSA, pub_key, BIGNUM),
67 ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
68 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
70 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey)
72 ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
73 ASN1_SIMPLE(DSA, p, BIGNUM),
74 ASN1_SIMPLE(DSA, q, BIGNUM),
75 ASN1_SIMPLE(DSA, g, BIGNUM),
76 } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
78 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams)
80 ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
81 ASN1_SIMPLE(DSA, pub_key, BIGNUM),
82 ASN1_SIMPLE(DSA, p, BIGNUM),
83 ASN1_SIMPLE(DSA, q, BIGNUM),
84 ASN1_SIMPLE(DSA, g, BIGNUM)
85 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
87 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey)
89 DSA *DSAparams_dup(DSA *dsa)
91 return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
94 int DSA_sign(int type, const unsigned char *dgst, int dlen,
95 unsigned char *sig, unsigned int *siglen, DSA *dsa)
98 RAND_seed(dgst, dlen);
99 s = DSA_do_sign(dgst, dlen, dsa);
104 *siglen = i2d_DSA_SIG(s, &sig);
109 /* data has already been hashed (probably with SHA or SHA-1). */
112 * 1: correct signature
113 * 0: incorrect signature
116 int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
117 const unsigned char *sigbuf, int siglen, DSA *dsa)
120 const unsigned char *p = sigbuf;
121 unsigned char *der = NULL;
128 if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
130 /* Ensure signature uses DER and doesn't have trailing garbage */
131 derlen = i2d_DSA_SIG(s, &der);
132 if (derlen != siglen || memcmp(sigbuf, der, derlen))
134 ret = DSA_do_verify(dgst, dgst_len, s, dsa);
136 OPENSSL_clear_free(der, derlen);