blob: 6c6ae294f9438694a560880512739aa117d26bc4 [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/**
2 * \file ecdsa.h
3 *
4 * \brief Elliptic curve DSA
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010023 */
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 *
Janos Follath0a5154b2017-03-10 11:31:41 +000073 * \note If the bitlength of the message hash is larger than the
74 * bitlength of the group order, then the hash is truncated as
75 * prescribed by SEC1 4.1.3 step 5.
76 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010077 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
81 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010082 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010085/**
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020086 * \brief Compute ECDSA signature of a previously hashed message,
87 * deterministic version (RFC 6979).
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010088 *
89 * \param grp ECP group
90 * \param r First output integer
91 * \param s Second output integer
92 * \param d Private signing key
93 * \param buf Message hash
94 * \param blen Length of buf
95 * \param md_alg MD algorithm used to hash the message
96 *
Janos Follath0a5154b2017-03-10 11:31:41 +000097 * \note If the bitlength of the message hash is larger than the
98 * bitlength of the group order, then the hash is truncated as
99 * prescribed by SEC1 4.1.3 step 5.
100 *
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100101 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
105 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
106 mbedtls_md_type_t md_alg );
107#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100108
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100109/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100110 * \brief Verify ECDSA signature of a previously hashed message
111 *
112 * \param grp ECP group
113 * \param buf Message hash
114 * \param blen Length of buf
115 * \param Q Public key to use for verification
116 * \param r First integer of the signature
117 * \param s Second integer of the signature
118 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000119 * \note If the bitlength of the message hash is larger than the
120 * bitlength of the group order, then the hash is truncated as
121 * prescribed by SEC1 4.1.4 step 3.
122 *
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100123 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid
125 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100128 const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100130
131/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200132 * \brief Compute ECDSA signature and write it to buffer,
133 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200134 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200135 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000136 * \note The deterministic version (RFC 6979) is used if
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 * MBEDTLS_ECDSA_DETERMINISTIC is defined.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200138 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200139 * \param ctx ECDSA context
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200140 * \param md_alg Algorithm that was used to hash the message
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200141 * \param hash Message hash
142 * \param hlen Length of hash
143 * \param sig Buffer that will hold the signature
144 * \param slen Length of the signature written
145 * \param f_rng RNG function
146 * \param p_rng RNG parameter
147 *
148 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200149 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200151 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000152 * \note If the bitlength of the message hash is larger than the
153 * bitlength of the group order, then the hash is truncated as
154 * prescribed by SEC1 4.1.3 step 5.
155 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200156 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
158 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200161 const unsigned char *hash, size_t hlen,
162 unsigned char *sig, size_t *slen,
163 int (*f_rng)(void *, unsigned char *, size_t),
164 void *p_rng );
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
167#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
168#if defined(MBEDTLS_DEPRECATED_WARNING)
169#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200170#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200172#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100173/**
174 * \brief Compute ECDSA signature and write it to buffer,
175 * serialized as defined in RFC 4492 page 20.
176 * Deterministic version, RFC 6979.
177 * (Not thread-safe to use same context in multiple threads)
178 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200180 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100181 * \param ctx ECDSA context
182 * \param hash Message hash
183 * \param hlen Length of hash
184 * \param sig Buffer that will hold the signature
185 * \param slen Length of the signature written
186 * \param md_alg MD algorithm used to hash the message
187 *
188 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200189 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100191 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000192 * \note If the bitlength of the message hash is larger than the
193 * bitlength of the group order, then the hash is truncated as
194 * prescribed by SEC1 4.1.3 step 5.
195 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100196 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
198 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100201 const unsigned char *hash, size_t hlen,
202 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
204#undef MBEDTLS_DEPRECATED
205#endif /* MBEDTLS_DEPRECATED_REMOVED */
206#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100207
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200208/**
209 * \brief Read and verify an ECDSA signature
210 *
211 * \param ctx ECDSA context
212 * \param hash Message hash
213 * \param hlen Size of hash
214 * \param sig Signature to read and verify
215 * \param slen Size of sig
216 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000217 * \note If the bitlength of the message hash is larger than the
218 * bitlength of the group order, then the hash is truncated as
219 * prescribed by SEC1 4.1.4 step 3.
220 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200221 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
223 * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200224 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200228 const unsigned char *hash, size_t hlen,
229 const unsigned char *sig, size_t slen );
230
231/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200232 * \brief Generate an ECDSA keypair on the given curve
233 *
234 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200235 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 * MBEDTLS_ECP_DP_XXX macros depending on configuration.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200237 * \param f_rng RNG function
238 * \param p_rng RNG parameter
239 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200243 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
244
245/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200246 * \brief Set an ECDSA context from an EC key pair
247 *
248 * \param ctx ECDSA context to set
249 * \param key EC key to use
250 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200254
255/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200256 * \brief Initialize context
257 *
258 * \param ctx Context to initialize
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200261
262/**
263 * \brief Free context
264 *
265 * \param ctx Context to free
266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200268
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100269#ifdef __cplusplus
270}
271#endif
272
Paul Bakker9af723c2014-05-01 13:03:14 +0200273#endif /* ecdsa.h */