blob: 52827d8d1a59621ef66d3bd31dbc2a59ea165e44 [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é-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_ECDSA_H
24#define MBEDTLS_ECDSA_H
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010025
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020026#include "ecp.h"
Manuel Pégourié-Gonnard887aa5b2014-04-04 13:57:20 +020027#include "md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010028
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020029/*
30 * RFC 4492 page 20:
31 *
32 * Ecdsa-Sig-Value ::= SEQUENCE {
33 * r INTEGER,
34 * s INTEGER
35 * }
36 *
37 * Size is at most
38 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
39 * twice that + 1 (tag) + 2 (len) for the sequence
40 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
41 * and less than 124 (total len <= 255) for the sequence)
42 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if MBEDTLS_ECP_MAX_BYTES > 124
44#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020045#endif
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +020046/** Maximum size of an ECDSA signature in bytes */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020048
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020049/**
50 * \brief ECDSA context structure
51 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020053
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010054#ifdef __cplusplus
55extern "C" {
56#endif
57
58/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010059 * \brief Compute ECDSA signature of a previously hashed message
60 *
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020061 * \note The deterministic version is usually prefered.
62 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010063 * \param grp ECP group
64 * \param r First output integer
65 * \param s Second output integer
66 * \param d Private signing key
67 * \param buf Message hash
68 * \param blen Length of buf
69 * \param f_rng RNG function
70 * \param p_rng RNG parameter
71 *
72 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
76 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010077 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010080/**
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020081 * \brief Compute ECDSA signature of a previously hashed message,
82 * deterministic version (RFC 6979).
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010083 *
84 * \param grp ECP group
85 * \param r First output integer
86 * \param s Second output integer
87 * \param d Private signing key
88 * \param buf Message hash
89 * \param blen Length of buf
90 * \param md_alg MD algorithm used to hash the message
91 *
92 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
96 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
97 mbedtls_md_type_t md_alg );
98#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010099
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100100/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100101 * \brief Verify ECDSA signature of a previously hashed message
102 *
103 * \param grp ECP group
104 * \param buf Message hash
105 * \param blen Length of buf
106 * \param Q Public key to use for verification
107 * \param r First integer of the signature
108 * \param s Second integer of the signature
109 *
110 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid
112 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100115 const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100117
118/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200119 * \brief Compute ECDSA signature and write it to buffer,
120 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200121 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200122 *
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200123 * \note The deterministice version (RFC 6979) is used if
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 * MBEDTLS_ECDSA_DETERMINISTIC is defined.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200125 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200126 * \param ctx ECDSA context
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200127 * \param md_alg Algorithm that was used to hash the message
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200128 * \param hash Message hash
129 * \param hlen Length of hash
130 * \param sig Buffer that will hold the signature
131 * \param slen Length of the signature written
132 * \param f_rng RNG function
133 * \param p_rng RNG parameter
134 *
135 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200136 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200138 *
139 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
141 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200144 const unsigned char *hash, size_t hlen,
145 unsigned char *sig, size_t *slen,
146 int (*f_rng)(void *, unsigned char *, size_t),
147 void *p_rng );
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
150#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
151#if defined(MBEDTLS_DEPRECATED_WARNING)
152#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200153#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200155#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100156/**
157 * \brief Compute ECDSA signature and write it to buffer,
158 * serialized as defined in RFC 4492 page 20.
159 * Deterministic version, RFC 6979.
160 * (Not thread-safe to use same context in multiple threads)
161 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200163 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100164 * \param ctx ECDSA context
165 * \param hash Message hash
166 * \param hlen Length of hash
167 * \param sig Buffer that will hold the signature
168 * \param slen Length of the signature written
169 * \param md_alg MD algorithm used to hash the message
170 *
171 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200172 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100174 *
175 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
177 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100180 const unsigned char *hash, size_t hlen,
181 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
183#undef MBEDTLS_DEPRECATED
184#endif /* MBEDTLS_DEPRECATED_REMOVED */
185#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100186
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200187/**
188 * \brief Read and verify an ECDSA signature
189 *
190 * \param ctx ECDSA context
191 * \param hash Message hash
192 * \param hlen Size of hash
193 * \param sig Signature to read and verify
194 * \param slen Size of sig
195 *
196 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
198 * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200199 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200203 const unsigned char *hash, size_t hlen,
204 const unsigned char *sig, size_t slen );
205
206/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200207 * \brief Generate an ECDSA keypair on the given curve
208 *
209 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200210 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 * MBEDTLS_ECP_DP_XXX macros depending on configuration.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200212 * \param f_rng RNG function
213 * \param p_rng RNG parameter
214 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200218 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
219
220/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200221 * \brief Set an ECDSA context from an EC key pair
222 *
223 * \param ctx ECDSA context to set
224 * \param key EC key to use
225 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200229
230/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200231 * \brief Initialize context
232 *
233 * \param ctx Context to initialize
234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200236
237/**
238 * \brief Free context
239 *
240 * \param ctx Context to free
241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200243
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100244#ifdef __cplusplus
245}
246#endif
247
Paul Bakker9af723c2014-05-01 13:03:14 +0200248#endif /* ecdsa.h */