]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - crypto/openssl/fips/dsa/fips_dsa_sign.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / crypto / openssl / fips / dsa / fips_dsa_sign.c
1 /* fips_dsa_sign.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2007.
4  */
5 /* ====================================================================
6  * Copyright (c) 2007 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <string.h>
60 #include <openssl/evp.h>
61 #include <openssl/dsa.h>
62 #include <openssl/err.h>
63 #include <openssl/sha.h>
64 #include <openssl/bn.h>
65
66 #ifdef OPENSSL_FIPS
67
68 /* FIPS versions of DSA_sign() and DSA_verify().
69  * These include a tiny ASN1 encoder/decoder to handle the specific
70  * case of a DSA signature.
71  */
72
73 int FIPS_dsa_size(DSA *r)
74         {
75         int ilen;
76         ilen = BN_num_bytes(r->q);
77         if (ilen > 20)
78                 return -1;
79         /* If MSB set need padding byte */
80         ilen ++;
81         /* Also need 2 bytes INTEGER header for r and s plus
82          * 2 bytes SEQUENCE header making 6 in total.
83          */
84         return ilen * 2 + 6;
85         }
86
87 /* Tiny ASN1 encoder for DSA_SIG structure. We can assume r, s smaller than
88  * 0x80 octets as by the DSA standards they will be less than 2^160
89  */
90
91 int FIPS_dsa_sig_encode(unsigned char *out, DSA_SIG *sig)
92         {
93         int rlen, slen, rpad, spad, seqlen;
94         rlen = BN_num_bytes(sig->r);
95         if (rlen > 20)
96                 return -1;
97         if (BN_num_bits(sig->r) & 0x7)
98                 rpad = 0;
99         else
100                 rpad = 1;
101         slen = BN_num_bytes(sig->s);
102         if (slen > 20)
103                 return -1;
104         if (BN_num_bits(sig->s) & 0x7)
105                 spad = 0;
106         else
107                 spad = 1;
108         /* Length of SEQUENCE, (1 tag + 1 len octet) * 2 + content octets */
109         seqlen = rlen + rpad + slen + spad + 4;
110         /* Actual encoded length: include SEQUENCE header */
111         if (!out)
112                 return seqlen + 2;
113
114         /* Output SEQUENCE header */
115         *out++ = V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED;
116         *out++ = (unsigned char)seqlen;
117
118         /* Output r */
119         *out++ = V_ASN1_INTEGER;
120         *out++ = (unsigned char)(rlen + rpad);
121         if (rpad)
122                 *out++ = 0;
123         BN_bn2bin(sig->r, out);
124         out += rlen;
125
126         /* Output s */
127         *out++ = V_ASN1_INTEGER;
128         *out++ = (unsigned char)(slen + spad);
129         if (spad)
130                 *out++ = 0;
131         BN_bn2bin(sig->s, out);
132         return seqlen + 2;
133         }
134
135 /* Companion DSA_SIG decoder */
136
137 int FIPS_dsa_sig_decode(DSA_SIG *sig, const unsigned char *in, int inlen)
138         {
139         int seqlen, rlen, slen;
140         const unsigned char *rbin;
141         /* Sanity check */
142
143         /* Need SEQUENCE tag */
144         if (*in++ != (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED))
145                 return 0;
146         /* Get length octet */
147         seqlen = *in++;
148         /* Check sensible length value */
149         if (seqlen < 4 || seqlen > 0x7F)
150                 return 0;
151         /* Check INTEGER tag */
152         if (*in++ != V_ASN1_INTEGER)
153                 return 0;
154         rlen = *in++;
155         seqlen -= 2 + rlen;
156         /* Check sensible seqlen value */
157         if (seqlen < 2)
158                 return 0;
159         rbin = in;
160         in += rlen;
161         /* Check INTEGER tag */
162         if (*in++ != V_ASN1_INTEGER)
163                 return 0;
164         slen = *in++;
165         /* Remaining bytes of SEQUENCE should exactly match
166          * encoding of s
167          */
168         if (seqlen != (slen + 2))
169                 return 0;
170         if (!sig->r && !(sig->r = BN_new()))
171                 return 0;
172         if (!sig->s && !(sig->s = BN_new()))
173                 return 0;
174         if (!BN_bin2bn(rbin, rlen, sig->r))
175                 return 0;
176         if (!BN_bin2bn(in, slen, sig->s))
177                 return 0;
178         return 1;
179         }
180
181 static int fips_dsa_sign(int type, const unsigned char *x, int y,
182              unsigned char *sig, unsigned int *siglen, EVP_MD_SVCTX *sv)
183         {
184         DSA *dsa = sv->key;
185         unsigned char dig[EVP_MAX_MD_SIZE];
186         unsigned int dlen;
187         DSA_SIG *s;
188         EVP_DigestFinal_ex(sv->mctx, dig, &dlen);
189         s=dsa->meth->dsa_do_sign(dig,dlen,dsa);
190         OPENSSL_cleanse(dig, dlen);
191         if (s == NULL)
192                 {
193                 *siglen=0;
194                 return 0;
195                 }
196         *siglen= FIPS_dsa_sig_encode(sig, s);
197         DSA_SIG_free(s);
198         if (*siglen < 0)
199                 return 0;
200         return 1;
201         }
202
203 static int fips_dsa_verify(int type, const unsigned char *x, int y,
204              const unsigned char *sigbuf, unsigned int siglen, EVP_MD_SVCTX *sv)
205         {
206         DSA *dsa = sv->key;
207         DSA_SIG *s;
208         int ret=-1;
209         unsigned char dig[EVP_MAX_MD_SIZE];
210         unsigned int dlen;
211
212         s = DSA_SIG_new();
213         if (s == NULL)
214                 return ret;
215         if (!FIPS_dsa_sig_decode(s,sigbuf,siglen))
216                 goto err;
217         EVP_DigestFinal_ex(sv->mctx, dig, &dlen);
218         ret=dsa->meth->dsa_do_verify(dig,dlen,s,dsa);
219         OPENSSL_cleanse(dig, dlen);
220 err:
221         DSA_SIG_free(s);
222         return ret;
223         }
224
225 static int init(EVP_MD_CTX *ctx)
226         { return SHA1_Init(ctx->md_data); }
227
228 static int update(EVP_MD_CTX *ctx,const void *data,size_t count)
229         { return SHA1_Update(ctx->md_data,data,count); }
230
231 static int final(EVP_MD_CTX *ctx,unsigned char *md)
232         { return SHA1_Final(md,ctx->md_data); }
233
234 static const EVP_MD dss1_md=
235         {
236         NID_dsa,
237         NID_dsaWithSHA1,
238         SHA_DIGEST_LENGTH,
239         EVP_MD_FLAG_FIPS|EVP_MD_FLAG_SVCTX,
240         init,
241         update,
242         final,
243         NULL,
244         NULL,
245         (evp_sign_method *)fips_dsa_sign,
246         (evp_verify_method *)fips_dsa_verify,
247         {EVP_PKEY_DSA,EVP_PKEY_DSA2,EVP_PKEY_DSA3, EVP_PKEY_DSA4,0},
248         SHA_CBLOCK,
249         sizeof(EVP_MD *)+sizeof(SHA_CTX),
250         };
251
252 const EVP_MD *EVP_dss1(void)
253         {
254         return(&dss1_md);
255         }
256 #endif