blob: 18a4fd2695cffee76162714b87a075c16d97ae62 [file] [log] [blame]
Jarno Lamsa18987a42019-04-24 15:40:43 +03001/* ecc_dh.h - TinyCrypt interface to EC-DH 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/* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions are met:
34 *
35 * - Redistributions of source code must retain the above copyright notice,
36 * this list of conditions and the following disclaimer.
37 *
38 * - Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 *
42 * - Neither the name of Intel Corporation nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 * POSSIBILITY OF SUCH DAMAGE.
57 */
58
59/**
60 * @file
61 * @brief -- Interface to EC-DH implementation.
62 *
63 * Overview: This software is an implementation of EC-DH. This implementation
64 * uses curve NIST p-256.
65 *
66 * Security: The curve NIST p-256 provides approximately 128 bits of security.
67 */
68
Jarno Lamsa55427962019-04-29 10:25:23 +030069#if defined(MBEDTLS_USE_UECC)
Jarno Lamsa18987a42019-04-24 15:40:43 +030070#ifndef __TC_ECC_DH_H__
71#define __TC_ECC_DH_H__
72
73#include <tinycrypt/ecc.h>
74
75#ifdef __cplusplus
76extern "C" {
77#endif
78
79/**
80 * @brief Create a public/private key pair.
81 * @return returns TC_CRYPTO_SUCCESS (1) if the key pair was generated successfully
82 * returns TC_CRYPTO_FAIL (0) if error while generating key pair
83 *
84 * @param p_public_key OUT -- Will be filled in with the public key. Must be at
85 * least 2 * the curve size (in bytes) long. For curve secp256r1, p_public_key
86 * must be 64 bytes long.
87 * @param p_private_key OUT -- Will be filled in with the private key. Must be as
88 * long as the curve order (for secp256r1, p_private_key must be 32 bytes long).
89 *
90 * @note side-channel countermeasure: algorithm strengthened against timing
91 * attack.
92 * @warning A cryptographically-secure PRNG function must be set (using
93 * uECC_set_rng()) before calling uECC_make_key().
94 */
95int uECC_make_key(uint8_t *p_public_key, uint8_t *p_private_key, uECC_Curve curve);
96
97#ifdef ENABLE_TESTS
98
99/**
100 * @brief Create a public/private key pair given a specific d.
101 *
102 * @note THIS FUNCTION SHOULD BE CALLED ONLY FOR TEST PURPOSES. Refer to
103 * uECC_make_key() function for real applications.
104 */
105int uECC_make_key_with_d(uint8_t *p_public_key, uint8_t *p_private_key,
106 unsigned int *d, uECC_Curve curve);
107#endif
108
109/**
110 * @brief Compute a shared secret given your secret key and someone else's
111 * public key.
112 * @return returns TC_CRYPTO_SUCCESS (1) if the shared secret was computed successfully
113 * returns TC_CRYPTO_FAIL (0) otherwise
114 *
115 * @param p_secret OUT -- Will be filled in with the shared secret value. Must be
116 * the same size as the curve size (for curve secp256r1, secret must be 32 bytes
117 * long.
118 * @param p_public_key IN -- The public key of the remote party.
119 * @param p_private_key IN -- Your private key.
120 *
121 * @warning It is recommended to use the output of uECC_shared_secret() as the
122 * input of a recommended Key Derivation Function (see NIST SP 800-108) in
123 * order to produce a cryptographically secure symmetric key.
124 */
125int uECC_shared_secret(const uint8_t *p_public_key, const uint8_t *p_private_key,
126 uint8_t *p_secret, uECC_Curve curve);
127
128#ifdef __cplusplus
129}
130#endif
131
132#endif /* __TC_ECC_DH_H__ */
Jarno Lamsa55427962019-04-29 10:25:23 +0300133#endif /* MBEDTLS_USE_UECC */