1160814Ssimon/* crypto/ecdsa/ecdsa_vrf.c */
2160814Ssimon/*
3160814Ssimon * Written by Nils Larsch for the OpenSSL project
4160814Ssimon */
5160814Ssimon/* ====================================================================
6160814Ssimon * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
7160814Ssimon *
8160814Ssimon * Redistribution and use in source and binary forms, with or without
9160814Ssimon * modification, are permitted provided that the following conditions
10160814Ssimon * are met:
11160814Ssimon *
12160814Ssimon * 1. Redistributions of source code must retain the above copyright
13296465Sdelphij *    notice, this list of conditions and the following disclaimer.
14160814Ssimon *
15160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
16160814Ssimon *    notice, this list of conditions and the following disclaimer in
17160814Ssimon *    the documentation and/or other materials provided with the
18160814Ssimon *    distribution.
19160814Ssimon *
20160814Ssimon * 3. All advertising materials mentioning features or use of this
21160814Ssimon *    software must display the following acknowledgment:
22160814Ssimon *    "This product includes software developed by the OpenSSL Project
23160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24160814Ssimon *
25160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26160814Ssimon *    endorse or promote products derived from this software without
27160814Ssimon *    prior written permission. For written permission, please contact
28160814Ssimon *    openssl-core@OpenSSL.org.
29160814Ssimon *
30160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
31160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
32160814Ssimon *    permission of the OpenSSL Project.
33160814Ssimon *
34160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
35160814Ssimon *    acknowledgment:
36160814Ssimon *    "This product includes software developed by the OpenSSL Project
37160814Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38160814Ssimon *
39160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
51160814Ssimon * ====================================================================
52160814Ssimon *
53160814Ssimon * This product includes cryptographic software written by Eric Young
54160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
55160814Ssimon * Hudson (tjh@cryptsoft.com).
56160814Ssimon *
57160814Ssimon */
58160814Ssimon
59160814Ssimon#include "ecs_locl.h"
60296465Sdelphij#include <string.h>
61160814Ssimon#ifndef OPENSSL_NO_ENGINE
62296465Sdelphij# include <openssl/engine.h>
63160814Ssimon#endif
64160814Ssimon
65296465Sdelphij/*-
66296465Sdelphij * returns
67160814Ssimon *      1: correct signature
68160814Ssimon *      0: incorrect signature
69160814Ssimon *     -1: error
70160814Ssimon */
71296465Sdelphijint ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
72296465Sdelphij                    const ECDSA_SIG *sig, EC_KEY *eckey)
73296465Sdelphij{
74296465Sdelphij    ECDSA_DATA *ecdsa = ecdsa_check(eckey);
75296465Sdelphij    if (ecdsa == NULL)
76296465Sdelphij        return 0;
77296465Sdelphij    return ecdsa->meth->ecdsa_do_verify(dgst, dgst_len, sig, eckey);
78296465Sdelphij}
79160814Ssimon
80296465Sdelphij/*-
81296465Sdelphij * returns
82160814Ssimon *      1: correct signature
83160814Ssimon *      0: incorrect signature
84160814Ssimon *     -1: error
85160814Ssimon */
86160814Ssimonint ECDSA_verify(int type, const unsigned char *dgst, int dgst_len,
87296465Sdelphij                 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
88296465Sdelphij{
89296465Sdelphij    ECDSA_SIG *s;
90296465Sdelphij    const unsigned char *p = sigbuf;
91296465Sdelphij    unsigned char *der = NULL;
92296465Sdelphij    int derlen = -1;
93296465Sdelphij    int ret = -1;
94160814Ssimon
95296465Sdelphij    s = ECDSA_SIG_new();
96296465Sdelphij    if (s == NULL)
97296465Sdelphij        return (ret);
98296465Sdelphij    if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
99296465Sdelphij        goto err;
100296465Sdelphij    /* Ensure signature uses DER and doesn't have trailing garbage */
101296465Sdelphij    derlen = i2d_ECDSA_SIG(s, &der);
102296465Sdelphij    if (derlen != sig_len || memcmp(sigbuf, der, derlen))
103296465Sdelphij        goto err;
104296465Sdelphij    ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
105296465Sdelphij err:
106296465Sdelphij    if (derlen > 0) {
107296465Sdelphij        OPENSSL_cleanse(der, derlen);
108296465Sdelphij        OPENSSL_free(der);
109296465Sdelphij    }
110296465Sdelphij    ECDSA_SIG_free(s);
111296465Sdelphij    return (ret);
112296465Sdelphij}
113