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