blob: 349f061b3bd522d3c015d29c38dcebf84929aa54 [file] [log] [blame]
Jarno Lamsa18987a42019-04-24 15:40:43 +03001/* ecc_dh.h - TinyCrypt interface to EC-DSA implementation */
2
3/*
4 * Copyright (c) 2014, Kenneth MacKay
5 * All rights reserved.
6 *
Simon Butchercffedb52019-09-09 16:28:54 +01007 * SPDX-License-Identifier: BSD-3-Clause
8 *
Jarno Lamsa18987a42019-04-24 15:40:43 +03009 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions are met:
37 *
38 * - Redistributions of source code must retain the above copyright notice,
39 * this list of conditions and the following disclaimer.
40 *
41 * - Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 *
45 * - Neither the name of Intel Corporation nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
50 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
53 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
60 */
61
62/**
63 * @file
64 * @brief -- Interface to EC-DSA implementation.
65 *
66 * Overview: This software is an implementation of EC-DSA. This implementation
67 * uses curve NIST p-256.
68 *
69 * Security: The curve NIST p-256 provides approximately 128 bits of security.
70 *
71 * Usage: - To sign: Compute a hash of the data you wish to sign (SHA-2 is
72 * recommended) and pass it in to ecdsa_sign function along with your
73 * private key and a random number. You must use a new non-predictable
74 * random number to generate each new signature.
75 * - To verify a signature: Compute the hash of the signed data using
76 * the same hash as the signer and pass it to this function along with
77 * the signer's public key and the signature values (r and s).
78 */
79
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +020080#if defined(MBEDTLS_USE_TINYCRYPT)
Jarno Lamsa18987a42019-04-24 15:40:43 +030081#ifndef __TC_ECC_DSA_H__
82#define __TC_ECC_DSA_H__
83
84#include <tinycrypt/ecc.h>
85
86#ifdef __cplusplus
87extern "C" {
88#endif
89
90/**
91 * @brief Generate an ECDSA signature for a given hash value.
92 * @return returns TC_CRYPTO_SUCCESS (1) if the signature generated successfully
93 * returns TC_CRYPTO_FAIL (0) if an error occurred.
94 *
95 * @param p_private_key IN -- Your private key.
96 * @param p_message_hash IN -- The hash of the message to sign.
97 * @param p_hash_size IN -- The size of p_message_hash in bytes.
98 * @param p_signature OUT -- Will be filled in with the signature value. Must be
99 * at least 2 * curve size long (for secp256r1, signature must be 64 bytes long).
100 *
101 * @warning A cryptographically-secure PRNG function must be set (using
102 * uECC_set_rng()) before calling uECC_sign().
103 * @note Usage: Compute a hash of the data you wish to sign (SHA-2 is
104 * recommended) and pass it in to this function along with your private key.
105 * @note side-channel countermeasure: algorithm strengthened against timing
106 * attack.
107 */
108int uECC_sign(const uint8_t *p_private_key, const uint8_t *p_message_hash,
109 unsigned p_hash_size, uint8_t *p_signature, uECC_Curve curve);
110
111#ifdef ENABLE_TESTS
112/*
113 * THIS FUNCTION SHOULD BE CALLED FOR TEST PURPOSES ONLY.
114 * Refer to uECC_sign() function for real applications.
115 */
116int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
117 unsigned int hash_size, uECC_word_t *k, uint8_t *signature,
118 uECC_Curve curve);
119#endif
120
121/**
122 * @brief Verify an ECDSA signature.
123 * @return returns TC_SUCCESS (1) if the signature is valid
124 * returns TC_FAIL (0) if the signature is invalid.
125 *
126 * @param p_public_key IN -- The signer's public key.
127 * @param p_message_hash IN -- The hash of the signed data.
128 * @param p_hash_size IN -- The size of p_message_hash in bytes.
129 * @param p_signature IN -- The signature values.
130 *
131 * @note Usage: Compute the hash of the signed data using the same hash as the
132 * signer and pass it to this function along with the signer's public key and
133 * the signature values (hash_size and signature).
134 */
135int uECC_verify(const uint8_t *p_public_key, const uint8_t *p_message_hash,
136 unsigned int p_hash_size, const uint8_t *p_signature, uECC_Curve curve);
137
138#ifdef __cplusplus
139}
140#endif
141
142#endif /* __TC_ECC_DSA_H__ */
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +0200143#endif /* MBEDTLS_USE_TINYCRYPT */