blob: 7ac9f92dee06e73ca2d03745c474774513713d4d [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/**
2 * \file ecdsa.h
3 *
4 * \brief Elliptic curve DSA
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01009 *
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_ECDSA_H
25#define MBEDTLS_ECDSA_H
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010026
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020027#include "ecp.h"
Manuel Pégourié-Gonnard887aa5b2014-04-04 13:57:20 +020028#include "md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010029
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020030/*
31 * RFC 4492 page 20:
32 *
33 * Ecdsa-Sig-Value ::= SEQUENCE {
34 * r INTEGER,
35 * s INTEGER
36 * }
37 *
38 * Size is at most
39 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
40 * twice that + 1 (tag) + 2 (len) for the sequence
41 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
42 * and less than 124 (total len <= 255) for the sequence)
43 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if MBEDTLS_ECP_MAX_BYTES > 124
45#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020046#endif
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +020047/** Maximum size of an ECDSA signature in bytes */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020049
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020050/**
51 * \brief ECDSA context structure
52 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020054
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010055#ifdef __cplusplus
56extern "C" {
57#endif
58
59/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010060 * \brief Compute ECDSA signature of a previously hashed message
61 *
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020062 * \note The deterministic version is usually prefered.
63 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010064 * \param grp ECP group
65 * \param r First output integer
66 * \param s Second output integer
67 * \param d Private signing key
68 * \param buf Message hash
69 * \param blen Length of buf
70 * \param f_rng RNG function
71 * \param p_rng RNG parameter
72 *
73 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
77 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010078 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010081/**
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020082 * \brief Compute ECDSA signature of a previously hashed message,
83 * deterministic version (RFC 6979).
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010084 *
85 * \param grp ECP group
86 * \param r First output integer
87 * \param s Second output integer
88 * \param d Private signing key
89 * \param buf Message hash
90 * \param blen Length of buf
91 * \param md_alg MD algorithm used to hash the message
92 *
93 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
97 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
98 mbedtls_md_type_t md_alg );
99#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100100
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100101/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100102 * \brief Verify ECDSA signature of a previously hashed message
103 *
104 * \param grp ECP group
105 * \param buf Message hash
106 * \param blen Length of buf
107 * \param Q Public key to use for verification
108 * \param r First integer of the signature
109 * \param s Second integer of the signature
110 *
111 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid
113 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100116 const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100118
119/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200120 * \brief Compute ECDSA signature and write it to buffer,
121 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200122 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200123 *
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200124 * \note The deterministice version (RFC 6979) is used if
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 * MBEDTLS_ECDSA_DETERMINISTIC is defined.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200126 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200127 * \param ctx ECDSA context
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200128 * \param md_alg Algorithm that was used to hash the message
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200129 * \param hash Message hash
130 * \param hlen Length of hash
131 * \param sig Buffer that will hold the signature
132 * \param slen Length of the signature written
133 * \param f_rng RNG function
134 * \param p_rng RNG parameter
135 *
136 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200137 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200139 *
140 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
142 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200145 const unsigned char *hash, size_t hlen,
146 unsigned char *sig, size_t *slen,
147 int (*f_rng)(void *, unsigned char *, size_t),
148 void *p_rng );
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
151#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
152#if defined(MBEDTLS_DEPRECATED_WARNING)
153#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200154#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200156#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100157/**
158 * \brief Compute ECDSA signature and write it to buffer,
159 * serialized as defined in RFC 4492 page 20.
160 * Deterministic version, RFC 6979.
161 * (Not thread-safe to use same context in multiple threads)
162 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200164 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100165 * \param ctx ECDSA context
166 * \param hash Message hash
167 * \param hlen Length of hash
168 * \param sig Buffer that will hold the signature
169 * \param slen Length of the signature written
170 * \param md_alg MD algorithm used to hash the message
171 *
172 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200173 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100175 *
176 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
178 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100181 const unsigned char *hash, size_t hlen,
182 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
184#undef MBEDTLS_DEPRECATED
185#endif /* MBEDTLS_DEPRECATED_REMOVED */
186#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100187
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200188/**
189 * \brief Read and verify an ECDSA signature
190 *
191 * \param ctx ECDSA context
192 * \param hash Message hash
193 * \param hlen Size of hash
194 * \param sig Signature to read and verify
195 * \param slen Size of sig
196 *
197 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
199 * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200200 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200204 const unsigned char *hash, size_t hlen,
205 const unsigned char *sig, size_t slen );
206
207/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200208 * \brief Generate an ECDSA keypair on the given curve
209 *
210 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200211 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 * MBEDTLS_ECP_DP_XXX macros depending on configuration.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200213 * \param f_rng RNG function
214 * \param p_rng RNG parameter
215 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200219 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
220
221/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200222 * \brief Set an ECDSA context from an EC key pair
223 *
224 * \param ctx ECDSA context to set
225 * \param key EC key to use
226 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200230
231/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200232 * \brief Initialize context
233 *
234 * \param ctx Context to initialize
235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200237
238/**
239 * \brief Free context
240 *
241 * \param ctx Context to free
242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200244
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100245#ifdef __cplusplus
246}
247#endif
248
Paul Bakker9af723c2014-05-01 13:03:14 +0200249#endif /* ecdsa.h */