blob: cd1987e31945a471cfcb69fbdb553da6bbfe5bde [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/**
2 * \file ecdsa.h
3 *
4 * \brief Elliptic curve DSA
5 *
6 * Copyright (C) 2006-2013, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_ECDSA_H
28#define POLARSSL_ECDSA_H
29
30#include "polarssl/ecp.h"
31
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020032/**
33 * \brief ECDSA context structure
34 */
35typedef struct
36{
37 ecp_group grp; /*!< ellipitic curve used */
38 mpi d; /*!< secret signature key */
39 ecp_point Q; /*!< public signature key */
40 mpi r; /*!< first integer from signature */
41 mpi s; /*!< second integer from signature */
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020042}
43ecdsa_context;
44
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010045#ifdef __cplusplus
46extern "C" {
47#endif
48
49/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010050 * \brief Compute ECDSA signature of a previously hashed message
51 *
52 * \param grp ECP group
53 * \param r First output integer
54 * \param s Second output integer
55 * \param d Private signing key
56 * \param buf Message hash
57 * \param blen Length of buf
58 * \param f_rng RNG function
59 * \param p_rng RNG parameter
60 *
61 * \return 0 if successful,
62 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
63 */
64int ecdsa_sign( const ecp_group *grp, mpi *r, mpi *s,
65 const mpi *d, const unsigned char *buf, size_t blen,
66 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
67
68/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010069 * \brief Verify ECDSA signature of a previously hashed message
70 *
71 * \param grp ECP group
72 * \param buf Message hash
73 * \param blen Length of buf
74 * \param Q Public key to use for verification
75 * \param r First integer of the signature
76 * \param s Second integer of the signature
77 *
78 * \return 0 if successful,
79 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid
80 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
81 */
82int ecdsa_verify( const ecp_group *grp,
83 const unsigned char *buf, size_t blen,
84 const ecp_point *Q, const mpi *r, const mpi *s);
85
86/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +020087 * \brief Compute ECDSA signature and write it to buffer
88 *
89 * \param ctx ECDSA context
90 * \param hash Message hash
91 * \param hlen Length of hash
92 * \param sig Buffer that will hold the signature
93 * \param slen Length of the signature written
94 * \param f_rng RNG function
95 * \param p_rng RNG parameter
96 *
97 * \note The "sig" buffer must be at least as large as twice the
98 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
99 * curve is used).
100 *
101 * \return 0 if successful,
102 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
103 * POLARSSL_ERR_ASN1 error code
104 */
105int ecdsa_write_signature( ecdsa_context *ctx,
106 const unsigned char *hash, size_t hlen,
107 unsigned char *sig, size_t *slen,
108 int (*f_rng)(void *, unsigned char *, size_t),
109 void *p_rng );
110
111/**
112 * \brief Read and verify an ECDSA signature
113 *
114 * \param ctx ECDSA context
115 * \param hash Message hash
116 * \param hlen Size of hash
117 * \param sig Signature to read and verify
118 * \param slen Size of sig
119 *
120 * \return 0 if successful,
121 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid
122 * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code
123 */
124int ecdsa_read_signature( ecdsa_context *ctx,
125 const unsigned char *hash, size_t hlen,
126 const unsigned char *sig, size_t slen );
127
128/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200129 * \brief Generate an ECDSA keypair on the given curve
130 *
131 * \param ctx ECDSA context in which the keypair should be stored
132 * \param grp Group (elliptic curve) to use. One of the various
133 * POLARSSL_ECP_DP_XXX macros depending on configuration.
134 * \param f_rng RNG function
135 * \param p_rng RNG parameter
136 *
137 * \return 0 on success, or a POLARSSL_ERR_ECP code.
138 */
139int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
140 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
141
142/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200143 * \brief Initialize context
144 *
145 * \param ctx Context to initialize
146 */
147void ecdsa_init( ecdsa_context *ctx );
148
149/**
150 * \brief Free context
151 *
152 * \param ctx Context to free
153 */
154void ecdsa_free( ecdsa_context *ctx );
155
156/**
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100157 * \brief Checkup routine
158 *
159 * \return 0 if successful, or 1 if the test failed
160 */
161int ecdsa_self_test( int verbose );
162
163#ifdef __cplusplus
164}
165#endif
166
167#endif