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