ecs_sign.c revision 205128
1156230Smux/* crypto/ecdsa/ecdsa_sign.c */
2156230Smux/* ====================================================================
3156230Smux * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
4156230Smux *
5156230Smux * Redistribution and use in source and binary forms, with or without
6156230Smux * modification, are permitted provided that the following conditions
7156230Smux * are met:
8156230Smux *
9156230Smux * 1. Redistributions of source code must retain the above copyright
10156230Smux *    notice, this list of conditions and the following disclaimer.
11156230Smux *
12156230Smux * 2. Redistributions in binary form must reproduce the above copyright
13156230Smux *    notice, this list of conditions and the following disclaimer in
14156230Smux *    the documentation and/or other materials provided with the
15156230Smux *    distribution.
16156230Smux *
17156230Smux * 3. All advertising materials mentioning features or use of this
18156230Smux *    software must display the following acknowledgment:
19156230Smux *    "This product includes software developed by the OpenSSL Project
20156230Smux *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21156230Smux *
22156230Smux * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23156230Smux *    endorse or promote products derived from this software without
24156230Smux *    prior written permission. For written permission, please contact
25156230Smux *    openssl-core@OpenSSL.org.
26156230Smux *
27156230Smux * 5. Products derived from this software may not be called "OpenSSL"
28156230Smux *    nor may "OpenSSL" appear in their names without prior written
29156230Smux *    permission of the OpenSSL Project.
30156230Smux *
31156230Smux * 6. Redistributions of any form whatsoever must retain the following
32156230Smux *    acknowledgment:
33156230Smux *    "This product includes software developed by the OpenSSL Project
34156230Smux *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35156230Smux *
36156230Smux * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37156230Smux * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38156230Smux * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39156230Smux * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40156230Smux * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41156230Smux * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42156230Smux * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43156230Smux * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44156230Smux * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45156230Smux * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46156230Smux * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47156230Smux * OF THE POSSIBILITY OF SUCH DAMAGE.
48156230Smux * ====================================================================
49156230Smux *
50156230Smux * This product includes cryptographic software written by Eric Young
51156230Smux * (eay@cryptsoft.com).  This product includes software written by Tim
52156230Smux * Hudson (tjh@cryptsoft.com).
53156230Smux *
54156230Smux */
55156230Smux
56156230Smux#include "ecs_locl.h"
57156230Smux#ifndef OPENSSL_NO_ENGINE
58156230Smux#include <openssl/engine.h>
59156230Smux#endif
60156230Smux#include <openssl/rand.h>
61156230Smux
62156230SmuxECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
63156230Smux{
64156230Smux	return ECDSA_do_sign_ex(dgst, dlen, NULL, NULL, eckey);
65156230Smux}
66156230Smux
67156230SmuxECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dlen,
68156230Smux	const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey)
69156230Smux{
70156230Smux	ECDSA_DATA *ecdsa = ecdsa_check(eckey);
71156230Smux	if (ecdsa == NULL)
72156230Smux		return NULL;
73156230Smux	return ecdsa->meth->ecdsa_do_sign(dgst, dlen, kinv, rp, eckey);
74156230Smux}
75156230Smux
76156230Smuxint ECDSA_sign(int type, const unsigned char *dgst, int dlen, unsigned char
77156230Smux		*sig, unsigned int *siglen, EC_KEY *eckey)
78156230Smux{
79156230Smux	return ECDSA_sign_ex(type, dgst, dlen, sig, siglen, NULL, NULL, eckey);
80156230Smux}
81156230Smux
82156230Smuxint ECDSA_sign_ex(int type, const unsigned char *dgst, int dlen, unsigned char
83156230Smux	*sig, unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r,
84156230Smux	EC_KEY *eckey)
85156230Smux{
86156230Smux	ECDSA_SIG *s;
87156230Smux	RAND_seed(dgst, dlen);
88156230Smux	s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
89156230Smux	if (s == NULL)
90156230Smux	{
91156230Smux		*siglen=0;
92156230Smux		return 0;
93156230Smux	}
94156230Smux	*siglen = i2d_ECDSA_SIG(s, &sig);
95156230Smux	ECDSA_SIG_free(s);
96156230Smux	return 1;
97156230Smux}
98156230Smux
99156230Smuxint ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
100156230Smux		BIGNUM **rp)
101156230Smux{
102156230Smux	ECDSA_DATA *ecdsa = ecdsa_check(eckey);
103156230Smux	if (ecdsa == NULL)
104156230Smux		return 0;
105156230Smux	return ecdsa->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
106156230Smux}
107156230Smux