1 /* 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 
78 #ifndef __TC_ECC_DSA_H__
79 #define __TC_ECC_DSA_H__
80 
81 #include <tinycrypt/ecc.h>
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
87 /**
88  * @brief Generate an ECDSA signature for a given hash value.
89  * @return returns TC_CRYPTO_SUCCESS (1) if the signature generated successfully
90  *         returns TC_CRYPTO_FAIL (0) if an error occurred.
91  *
92  * @param p_private_key IN -- Your private key.
93  * @param p_message_hash IN -- The hash of the message to sign.
94  * @param p_hash_size IN -- The size of p_message_hash in bytes.
95  * @param p_signature OUT -- Will be filled in with the signature value. Must be
96  * at least 2 * curve size long (for secp256r1, signature must be 64 bytes long).
97  *
98  * @warning A cryptographically-secure PRNG function must be set (using
99  * uECC_set_rng()) before calling uECC_sign().
100  * @note Usage: Compute a hash of the data you wish to sign (SHA-2 is
101  * recommended) and pass it in to this function along with your private key.
102  * @note side-channel countermeasure: algorithm strengthened against timing
103  * attack.
104  */
105 int uECC_sign(const uint8_t *p_private_key, const uint8_t *p_message_hash,
106 	      unsigned p_hash_size, uint8_t *p_signature, uECC_Curve curve);
107 
108 #ifdef ENABLE_TESTS
109 /*
110  * THIS FUNCTION SHOULD BE CALLED FOR TEST PURPOSES ONLY.
111  * Refer to uECC_sign() function for real applications.
112  */
113 int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
114 		     unsigned int hash_size, uECC_word_t *k, uint8_t *signature,
115 		     uECC_Curve curve);
116 #endif
117 
118 /**
119  * @brief Verify an ECDSA signature.
120  * @return returns TC_SUCCESS (1) if the signature is valid
121  * 	   returns TC_FAIL (0) if the signature is invalid.
122  *
123  * @param p_public_key IN -- The signer's public key.
124  * @param p_message_hash IN -- The hash of the signed data.
125  * @param p_hash_size IN -- The size of p_message_hash in bytes.
126  * @param p_signature IN -- The signature values.
127  *
128  * @note Usage: Compute the hash of the signed data using the same hash as the
129  * signer and pass it to this function along with the signer's public key and
130  * the signature values (hash_size and signature).
131  */
132 int uECC_verify(const uint8_t *p_public_key, const uint8_t *p_message_hash,
133 		unsigned int p_hash_size, const uint8_t *p_signature, uECC_Curve curve);
134 
135 #ifdef __cplusplus
136 }
137 #endif
138 
139 #endif /* __TC_ECC_DSA_H__ */
140