3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2007 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/evp.h>
62 #include <openssl/dsa.h>
63 #include <openssl/err.h>
64 #include <openssl/sha.h>
65 #include <openssl/bn.h>
70 * FIPS versions of DSA_sign() and DSA_verify(). These include a tiny ASN1
71 * encoder/decoder to handle the specific case of a DSA signature.
75 int FIPS_dsa_size(DSA *r)
78 ilen = BN_num_bytes(r->q);
81 /* If MSB set need padding byte */
84 * Also need 2 bytes INTEGER header for r and s plus 2 bytes SEQUENCE
85 * header making 6 in total.
92 * Tiny ASN1 encoder for DSA_SIG structure. We can assume r, s smaller than
93 * 0x80 octets as by the DSA standards they will be less than 2^160
96 int FIPS_dsa_sig_encode(unsigned char *out, DSA_SIG *sig)
98 int rlen, slen, rpad, spad, seqlen;
99 rlen = BN_num_bytes(sig->r);
102 if (BN_num_bits(sig->r) & 0x7)
106 slen = BN_num_bytes(sig->s);
109 if (BN_num_bits(sig->s) & 0x7)
113 /* Length of SEQUENCE, (1 tag + 1 len octet) * 2 + content octets */
114 seqlen = rlen + rpad + slen + spad + 4;
115 /* Actual encoded length: include SEQUENCE header */
119 /* Output SEQUENCE header */
120 *out++ = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
121 *out++ = (unsigned char)seqlen;
124 *out++ = V_ASN1_INTEGER;
125 *out++ = (unsigned char)(rlen + rpad);
128 BN_bn2bin(sig->r, out);
132 *out++ = V_ASN1_INTEGER;
133 *out++ = (unsigned char)(slen + spad);
136 BN_bn2bin(sig->s, out);
140 /* Companion DSA_SIG decoder */
142 int FIPS_dsa_sig_decode(DSA_SIG *sig, const unsigned char *in, int inlen)
144 int seqlen, rlen, slen;
145 const unsigned char *rbin;
148 /* Need SEQUENCE tag */
149 if (*in++ != (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED))
151 /* Get length octet */
153 /* Check sensible length value */
154 if (seqlen < 4 || seqlen > 0x7F)
156 /* Check INTEGER tag */
157 if (*in++ != V_ASN1_INTEGER)
161 /* Check sensible seqlen value */
166 /* Check INTEGER tag */
167 if (*in++ != V_ASN1_INTEGER)
171 * Remaining bytes of SEQUENCE should exactly match encoding of s
173 if (seqlen != (slen + 2))
175 if (!sig->r && !(sig->r = BN_new()))
177 if (!sig->s && !(sig->s = BN_new()))
179 if (!BN_bin2bn(rbin, rlen, sig->r))
181 if (!BN_bin2bn(in, slen, sig->s))
186 static int fips_dsa_sign(int type, const unsigned char *x, int y,
187 unsigned char *sig, unsigned int *siglen,
191 unsigned char dig[EVP_MAX_MD_SIZE];
194 EVP_DigestFinal_ex(sv->mctx, dig, &dlen);
195 s = dsa->meth->dsa_do_sign(dig, dlen, dsa);
196 OPENSSL_cleanse(dig, dlen);
201 *siglen = FIPS_dsa_sig_encode(sig, s);
208 static int fips_dsa_verify(int type, const unsigned char *x, int y,
209 const unsigned char *sigbuf, unsigned int siglen,
215 unsigned char dig[EVP_MAX_MD_SIZE];
221 if (!FIPS_dsa_sig_decode(s, sigbuf, siglen))
223 EVP_DigestFinal_ex(sv->mctx, dig, &dlen);
224 ret = dsa->meth->dsa_do_verify(dig, dlen, s, dsa);
225 OPENSSL_cleanse(dig, dlen);
231 static int init(EVP_MD_CTX *ctx)
233 return SHA1_Init(ctx->md_data);
236 static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
238 return SHA1_Update(ctx->md_data, data, count);
241 static int final(EVP_MD_CTX *ctx, unsigned char *md)
243 return SHA1_Final(md, ctx->md_data);
246 static const EVP_MD dss1_md = {
250 EVP_MD_FLAG_FIPS | EVP_MD_FLAG_SVCTX,
256 (evp_sign_method *) fips_dsa_sign,
257 (evp_verify_method *) fips_dsa_verify,
258 {EVP_PKEY_DSA, EVP_PKEY_DSA2, EVP_PKEY_DSA3, EVP_PKEY_DSA4, 0},
260 sizeof(EVP_MD *) + sizeof(SHA_CTX),
263 const EVP_MD *EVP_dss1(void)