1*f5b1c8a1SJohn Marino /* $OpenBSD: ecdsa.h,v 1.3 2014/11/17 20:25:50 miod Exp $ */ 2*f5b1c8a1SJohn Marino /** 3*f5b1c8a1SJohn Marino * \file crypto/ecdsa/ecdsa.h Include file for the OpenSSL ECDSA functions 4*f5b1c8a1SJohn Marino * \author Written by Nils Larsch for the OpenSSL project 5*f5b1c8a1SJohn Marino */ 6*f5b1c8a1SJohn Marino /* ==================================================================== 7*f5b1c8a1SJohn Marino * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved. 8*f5b1c8a1SJohn Marino * 9*f5b1c8a1SJohn Marino * Redistribution and use in source and binary forms, with or without 10*f5b1c8a1SJohn Marino * modification, are permitted provided that the following conditions 11*f5b1c8a1SJohn Marino * are met: 12*f5b1c8a1SJohn Marino * 13*f5b1c8a1SJohn Marino * 1. Redistributions of source code must retain the above copyright 14*f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer. 15*f5b1c8a1SJohn Marino * 16*f5b1c8a1SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright 17*f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer in 18*f5b1c8a1SJohn Marino * the documentation and/or other materials provided with the 19*f5b1c8a1SJohn Marino * distribution. 20*f5b1c8a1SJohn Marino * 21*f5b1c8a1SJohn Marino * 3. All advertising materials mentioning features or use of this 22*f5b1c8a1SJohn Marino * software must display the following acknowledgment: 23*f5b1c8a1SJohn Marino * "This product includes software developed by the OpenSSL Project 24*f5b1c8a1SJohn Marino * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25*f5b1c8a1SJohn Marino * 26*f5b1c8a1SJohn Marino * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27*f5b1c8a1SJohn Marino * endorse or promote products derived from this software without 28*f5b1c8a1SJohn Marino * prior written permission. For written permission, please contact 29*f5b1c8a1SJohn Marino * licensing@OpenSSL.org. 30*f5b1c8a1SJohn Marino * 31*f5b1c8a1SJohn Marino * 5. Products derived from this software may not be called "OpenSSL" 32*f5b1c8a1SJohn Marino * nor may "OpenSSL" appear in their names without prior written 33*f5b1c8a1SJohn Marino * permission of the OpenSSL Project. 34*f5b1c8a1SJohn Marino * 35*f5b1c8a1SJohn Marino * 6. Redistributions of any form whatsoever must retain the following 36*f5b1c8a1SJohn Marino * acknowledgment: 37*f5b1c8a1SJohn Marino * "This product includes software developed by the OpenSSL Project 38*f5b1c8a1SJohn Marino * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39*f5b1c8a1SJohn Marino * 40*f5b1c8a1SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41*f5b1c8a1SJohn Marino * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42*f5b1c8a1SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43*f5b1c8a1SJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44*f5b1c8a1SJohn Marino * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45*f5b1c8a1SJohn Marino * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46*f5b1c8a1SJohn Marino * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47*f5b1c8a1SJohn Marino * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*f5b1c8a1SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49*f5b1c8a1SJohn Marino * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50*f5b1c8a1SJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51*f5b1c8a1SJohn Marino * OF THE POSSIBILITY OF SUCH DAMAGE. 52*f5b1c8a1SJohn Marino * ==================================================================== 53*f5b1c8a1SJohn Marino * 54*f5b1c8a1SJohn Marino * This product includes cryptographic software written by Eric Young 55*f5b1c8a1SJohn Marino * (eay@cryptsoft.com). This product includes software written by Tim 56*f5b1c8a1SJohn Marino * Hudson (tjh@cryptsoft.com). 57*f5b1c8a1SJohn Marino * 58*f5b1c8a1SJohn Marino */ 59*f5b1c8a1SJohn Marino #ifndef HEADER_ECDSA_H 60*f5b1c8a1SJohn Marino #define HEADER_ECDSA_H 61*f5b1c8a1SJohn Marino 62*f5b1c8a1SJohn Marino #include <openssl/opensslconf.h> 63*f5b1c8a1SJohn Marino 64*f5b1c8a1SJohn Marino #ifdef OPENSSL_NO_ECDSA 65*f5b1c8a1SJohn Marino #error ECDSA is disabled. 66*f5b1c8a1SJohn Marino #endif 67*f5b1c8a1SJohn Marino 68*f5b1c8a1SJohn Marino #include <openssl/ec.h> 69*f5b1c8a1SJohn Marino #include <openssl/ossl_typ.h> 70*f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_DEPRECATED 71*f5b1c8a1SJohn Marino #include <openssl/bn.h> 72*f5b1c8a1SJohn Marino #endif 73*f5b1c8a1SJohn Marino 74*f5b1c8a1SJohn Marino #ifdef __cplusplus 75*f5b1c8a1SJohn Marino extern "C" { 76*f5b1c8a1SJohn Marino #endif 77*f5b1c8a1SJohn Marino 78*f5b1c8a1SJohn Marino typedef struct ECDSA_SIG_st ECDSA_SIG; 79*f5b1c8a1SJohn Marino 80*f5b1c8a1SJohn Marino struct ecdsa_method { 81*f5b1c8a1SJohn Marino const char *name; 82*f5b1c8a1SJohn Marino ECDSA_SIG *(*ecdsa_do_sign)(const unsigned char *dgst, int dgst_len, 83*f5b1c8a1SJohn Marino const BIGNUM *inv, const BIGNUM *rp, EC_KEY *eckey); 84*f5b1c8a1SJohn Marino int (*ecdsa_sign_setup)(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, 85*f5b1c8a1SJohn Marino BIGNUM **r); 86*f5b1c8a1SJohn Marino int (*ecdsa_do_verify)(const unsigned char *dgst, int dgst_len, 87*f5b1c8a1SJohn Marino const ECDSA_SIG *sig, EC_KEY *eckey); 88*f5b1c8a1SJohn Marino #if 0 89*f5b1c8a1SJohn Marino int (*init)(EC_KEY *eckey); 90*f5b1c8a1SJohn Marino int (*finish)(EC_KEY *eckey); 91*f5b1c8a1SJohn Marino #endif 92*f5b1c8a1SJohn Marino int flags; 93*f5b1c8a1SJohn Marino char *app_data; 94*f5b1c8a1SJohn Marino }; 95*f5b1c8a1SJohn Marino 96*f5b1c8a1SJohn Marino /* If this flag is set the ECDSA method is FIPS compliant and can be used 97*f5b1c8a1SJohn Marino * in FIPS mode. This is set in the validated module method. If an 98*f5b1c8a1SJohn Marino * application sets this flag in its own methods it is its responsibility 99*f5b1c8a1SJohn Marino * to ensure the result is compliant. 100*f5b1c8a1SJohn Marino */ 101*f5b1c8a1SJohn Marino 102*f5b1c8a1SJohn Marino #define ECDSA_FLAG_FIPS_METHOD 0x1 103*f5b1c8a1SJohn Marino 104*f5b1c8a1SJohn Marino struct ECDSA_SIG_st { 105*f5b1c8a1SJohn Marino BIGNUM *r; 106*f5b1c8a1SJohn Marino BIGNUM *s; 107*f5b1c8a1SJohn Marino }; 108*f5b1c8a1SJohn Marino 109*f5b1c8a1SJohn Marino /** Allocates and initialize a ECDSA_SIG structure 110*f5b1c8a1SJohn Marino * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 111*f5b1c8a1SJohn Marino */ 112*f5b1c8a1SJohn Marino ECDSA_SIG *ECDSA_SIG_new(void); 113*f5b1c8a1SJohn Marino 114*f5b1c8a1SJohn Marino /** frees a ECDSA_SIG structure 115*f5b1c8a1SJohn Marino * \param sig pointer to the ECDSA_SIG structure 116*f5b1c8a1SJohn Marino */ 117*f5b1c8a1SJohn Marino void ECDSA_SIG_free(ECDSA_SIG *sig); 118*f5b1c8a1SJohn Marino 119*f5b1c8a1SJohn Marino /** DER encode content of ECDSA_SIG object (note: this function modifies *pp 120*f5b1c8a1SJohn Marino * (*pp += length of the DER encoded signature)). 121*f5b1c8a1SJohn Marino * \param sig pointer to the ECDSA_SIG object 122*f5b1c8a1SJohn Marino * \param pp pointer to a unsigned char pointer for the output or NULL 123*f5b1c8a1SJohn Marino * \return the length of the DER encoded ECDSA_SIG object or 0 124*f5b1c8a1SJohn Marino */ 125*f5b1c8a1SJohn Marino int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp); 126*f5b1c8a1SJohn Marino 127*f5b1c8a1SJohn Marino /** Decodes a DER encoded ECDSA signature (note: this function changes *pp 128*f5b1c8a1SJohn Marino * (*pp += len)). 129*f5b1c8a1SJohn Marino * \param sig pointer to ECDSA_SIG pointer (may be NULL) 130*f5b1c8a1SJohn Marino * \param pp memory buffer with the DER encoded signature 131*f5b1c8a1SJohn Marino * \param len length of the buffer 132*f5b1c8a1SJohn Marino * \return pointer to the decoded ECDSA_SIG structure (or NULL) 133*f5b1c8a1SJohn Marino */ 134*f5b1c8a1SJohn Marino ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len); 135*f5b1c8a1SJohn Marino 136*f5b1c8a1SJohn Marino /** Computes the ECDSA signature of the given hash value using 137*f5b1c8a1SJohn Marino * the supplied private key and returns the created signature. 138*f5b1c8a1SJohn Marino * \param dgst pointer to the hash value 139*f5b1c8a1SJohn Marino * \param dgst_len length of the hash value 140*f5b1c8a1SJohn Marino * \param eckey EC_KEY object containing a private EC key 141*f5b1c8a1SJohn Marino * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 142*f5b1c8a1SJohn Marino */ 143*f5b1c8a1SJohn Marino ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len, 144*f5b1c8a1SJohn Marino EC_KEY *eckey); 145*f5b1c8a1SJohn Marino 146*f5b1c8a1SJohn Marino /** Computes ECDSA signature of a given hash value using the supplied 147*f5b1c8a1SJohn Marino * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 148*f5b1c8a1SJohn Marino * \param dgst pointer to the hash value to sign 149*f5b1c8a1SJohn Marino * \param dgstlen length of the hash value 150*f5b1c8a1SJohn Marino * \param kinv BIGNUM with a pre-computed inverse k (optional) 151*f5b1c8a1SJohn Marino * \param rp BIGNUM with a pre-computed rp value (optioanl), 152*f5b1c8a1SJohn Marino * see ECDSA_sign_setup 153*f5b1c8a1SJohn Marino * \param eckey EC_KEY object containing a private EC key 154*f5b1c8a1SJohn Marino * \return pointer to a ECDSA_SIG structure or NULL if an error occurred 155*f5b1c8a1SJohn Marino */ 156*f5b1c8a1SJohn Marino ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen, 157*f5b1c8a1SJohn Marino const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey); 158*f5b1c8a1SJohn Marino 159*f5b1c8a1SJohn Marino /** Verifies that the supplied signature is a valid ECDSA 160*f5b1c8a1SJohn Marino * signature of the supplied hash value using the supplied public key. 161*f5b1c8a1SJohn Marino * \param dgst pointer to the hash value 162*f5b1c8a1SJohn Marino * \param dgst_len length of the hash value 163*f5b1c8a1SJohn Marino * \param sig ECDSA_SIG structure 164*f5b1c8a1SJohn Marino * \param eckey EC_KEY object containing a public EC key 165*f5b1c8a1SJohn Marino * \return 1 if the signature is valid, 0 if the signature is invalid 166*f5b1c8a1SJohn Marino * and -1 on error 167*f5b1c8a1SJohn Marino */ 168*f5b1c8a1SJohn Marino int ECDSA_do_verify(const unsigned char *dgst, int dgst_len, 169*f5b1c8a1SJohn Marino const ECDSA_SIG *sig, EC_KEY* eckey); 170*f5b1c8a1SJohn Marino 171*f5b1c8a1SJohn Marino const ECDSA_METHOD *ECDSA_OpenSSL(void); 172*f5b1c8a1SJohn Marino 173*f5b1c8a1SJohn Marino /** Sets the default ECDSA method 174*f5b1c8a1SJohn Marino * \param meth new default ECDSA_METHOD 175*f5b1c8a1SJohn Marino */ 176*f5b1c8a1SJohn Marino void ECDSA_set_default_method(const ECDSA_METHOD *meth); 177*f5b1c8a1SJohn Marino 178*f5b1c8a1SJohn Marino /** Returns the default ECDSA method 179*f5b1c8a1SJohn Marino * \return pointer to ECDSA_METHOD structure containing the default method 180*f5b1c8a1SJohn Marino */ 181*f5b1c8a1SJohn Marino const ECDSA_METHOD *ECDSA_get_default_method(void); 182*f5b1c8a1SJohn Marino 183*f5b1c8a1SJohn Marino /** Sets method to be used for the ECDSA operations 184*f5b1c8a1SJohn Marino * \param eckey EC_KEY object 185*f5b1c8a1SJohn Marino * \param meth new method 186*f5b1c8a1SJohn Marino * \return 1 on success and 0 otherwise 187*f5b1c8a1SJohn Marino */ 188*f5b1c8a1SJohn Marino int ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth); 189*f5b1c8a1SJohn Marino 190*f5b1c8a1SJohn Marino /** Returns the maximum length of the DER encoded signature 191*f5b1c8a1SJohn Marino * \param eckey EC_KEY object 192*f5b1c8a1SJohn Marino * \return numbers of bytes required for the DER encoded signature 193*f5b1c8a1SJohn Marino */ 194*f5b1c8a1SJohn Marino int ECDSA_size(const EC_KEY *eckey); 195*f5b1c8a1SJohn Marino 196*f5b1c8a1SJohn Marino /** Precompute parts of the signing operation 197*f5b1c8a1SJohn Marino * \param eckey EC_KEY object containing a private EC key 198*f5b1c8a1SJohn Marino * \param ctx BN_CTX object (optional) 199*f5b1c8a1SJohn Marino * \param kinv BIGNUM pointer for the inverse of k 200*f5b1c8a1SJohn Marino * \param rp BIGNUM pointer for x coordinate of k * generator 201*f5b1c8a1SJohn Marino * \return 1 on success and 0 otherwise 202*f5b1c8a1SJohn Marino */ 203*f5b1c8a1SJohn Marino int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, 204*f5b1c8a1SJohn Marino BIGNUM **rp); 205*f5b1c8a1SJohn Marino 206*f5b1c8a1SJohn Marino /** Computes ECDSA signature of a given hash value using the supplied 207*f5b1c8a1SJohn Marino * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 208*f5b1c8a1SJohn Marino * \param type this parameter is ignored 209*f5b1c8a1SJohn Marino * \param dgst pointer to the hash value to sign 210*f5b1c8a1SJohn Marino * \param dgstlen length of the hash value 211*f5b1c8a1SJohn Marino * \param sig memory for the DER encoded created signature 212*f5b1c8a1SJohn Marino * \param siglen pointer to the length of the returned signature 213*f5b1c8a1SJohn Marino * \param eckey EC_KEY object containing a private EC key 214*f5b1c8a1SJohn Marino * \return 1 on success and 0 otherwise 215*f5b1c8a1SJohn Marino */ 216*f5b1c8a1SJohn Marino int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen, 217*f5b1c8a1SJohn Marino unsigned char *sig, unsigned int *siglen, EC_KEY *eckey); 218*f5b1c8a1SJohn Marino 219*f5b1c8a1SJohn Marino 220*f5b1c8a1SJohn Marino /** Computes ECDSA signature of a given hash value using the supplied 221*f5b1c8a1SJohn Marino * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 222*f5b1c8a1SJohn Marino * \param type this parameter is ignored 223*f5b1c8a1SJohn Marino * \param dgst pointer to the hash value to sign 224*f5b1c8a1SJohn Marino * \param dgstlen length of the hash value 225*f5b1c8a1SJohn Marino * \param sig buffer to hold the DER encoded signature 226*f5b1c8a1SJohn Marino * \param siglen pointer to the length of the returned signature 227*f5b1c8a1SJohn Marino * \param kinv BIGNUM with a pre-computed inverse k (optional) 228*f5b1c8a1SJohn Marino * \param rp BIGNUM with a pre-computed rp value (optioanl), 229*f5b1c8a1SJohn Marino * see ECDSA_sign_setup 230*f5b1c8a1SJohn Marino * \param eckey EC_KEY object containing a private EC key 231*f5b1c8a1SJohn Marino * \return 1 on success and 0 otherwise 232*f5b1c8a1SJohn Marino */ 233*f5b1c8a1SJohn Marino int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen, 234*f5b1c8a1SJohn Marino unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv, 235*f5b1c8a1SJohn Marino const BIGNUM *rp, EC_KEY *eckey); 236*f5b1c8a1SJohn Marino 237*f5b1c8a1SJohn Marino /** Verifies that the given signature is valid ECDSA signature 238*f5b1c8a1SJohn Marino * of the supplied hash value using the specified public key. 239*f5b1c8a1SJohn Marino * \param type this parameter is ignored 240*f5b1c8a1SJohn Marino * \param dgst pointer to the hash value 241*f5b1c8a1SJohn Marino * \param dgstlen length of the hash value 242*f5b1c8a1SJohn Marino * \param sig pointer to the DER encoded signature 243*f5b1c8a1SJohn Marino * \param siglen length of the DER encoded signature 244*f5b1c8a1SJohn Marino * \param eckey EC_KEY object containing a public EC key 245*f5b1c8a1SJohn Marino * \return 1 if the signature is valid, 0 if the signature is invalid 246*f5b1c8a1SJohn Marino * and -1 on error 247*f5b1c8a1SJohn Marino */ 248*f5b1c8a1SJohn Marino int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen, 249*f5b1c8a1SJohn Marino const unsigned char *sig, int siglen, EC_KEY *eckey); 250*f5b1c8a1SJohn Marino 251*f5b1c8a1SJohn Marino /* the standard ex_data functions */ 252*f5b1c8a1SJohn Marino int ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 253*f5b1c8a1SJohn Marino CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); 254*f5b1c8a1SJohn Marino int ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg); 255*f5b1c8a1SJohn Marino void *ECDSA_get_ex_data(EC_KEY *d, int idx); 256*f5b1c8a1SJohn Marino 257*f5b1c8a1SJohn Marino 258*f5b1c8a1SJohn Marino /* BEGIN ERROR CODES */ 259*f5b1c8a1SJohn Marino /* The following lines are auto generated by the script mkerr.pl. Any changes 260*f5b1c8a1SJohn Marino * made after this point may be overwritten when the script is next run. 261*f5b1c8a1SJohn Marino */ 262*f5b1c8a1SJohn Marino void ERR_load_ECDSA_strings(void); 263*f5b1c8a1SJohn Marino 264*f5b1c8a1SJohn Marino /* Error codes for the ECDSA functions. */ 265*f5b1c8a1SJohn Marino 266*f5b1c8a1SJohn Marino /* Function codes. */ 267*f5b1c8a1SJohn Marino #define ECDSA_F_ECDSA_CHECK 104 268*f5b1c8a1SJohn Marino #define ECDSA_F_ECDSA_DATA_NEW_METHOD 100 269*f5b1c8a1SJohn Marino #define ECDSA_F_ECDSA_DO_SIGN 101 270*f5b1c8a1SJohn Marino #define ECDSA_F_ECDSA_DO_VERIFY 102 271*f5b1c8a1SJohn Marino #define ECDSA_F_ECDSA_SIGN_SETUP 103 272*f5b1c8a1SJohn Marino 273*f5b1c8a1SJohn Marino /* Reason codes. */ 274*f5b1c8a1SJohn Marino #define ECDSA_R_BAD_SIGNATURE 100 275*f5b1c8a1SJohn Marino #define ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 101 276*f5b1c8a1SJohn Marino #define ECDSA_R_ERR_EC_LIB 102 277*f5b1c8a1SJohn Marino #define ECDSA_R_MISSING_PARAMETERS 103 278*f5b1c8a1SJohn Marino #define ECDSA_R_NEED_NEW_SETUP_VALUES 106 279*f5b1c8a1SJohn Marino #define ECDSA_R_NON_FIPS_METHOD 107 280*f5b1c8a1SJohn Marino #define ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED 104 281*f5b1c8a1SJohn Marino #define ECDSA_R_SIGNATURE_MALLOC_FAILED 105 282*f5b1c8a1SJohn Marino 283*f5b1c8a1SJohn Marino #ifdef __cplusplus 284*f5b1c8a1SJohn Marino } 285*f5b1c8a1SJohn Marino #endif 286*f5b1c8a1SJohn Marino #endif 287