blob: c443f8536a549a900a100b99a27881f0aef7e2fb [file] [log] [blame]
Jarno Lamsa18987a42019-04-24 15:40:43 +03001/* ecc_dh.h - TinyCrypt interface to EC-DH implementation */
2
3/*
Simon Butcher92c3d1f2019-09-09 17:25:08 +01004 * Copyright (c) 2019, Arm Limited (or its affiliates), All Rights Reserved.
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8/*
Jarno Lamsa18987a42019-04-24 15:40:43 +03009 * Copyright (c) 2014, Kenneth MacKay
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * * Redistributions of source code must retain the above copyright notice, this
16 * list of conditions and the following disclaimer.
17 *
18 * * Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions are met:
39 *
40 * - Redistributions of source code must retain the above copyright notice,
41 * this list of conditions and the following disclaimer.
42 *
43 * - Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 *
47 * - Neither the name of Intel Corporation nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
52 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
55 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
62 */
63
64/**
65 * @file
66 * @brief -- Interface to EC-DH implementation.
67 *
68 * Overview: This software is an implementation of EC-DH. This implementation
69 * uses curve NIST p-256.
70 *
71 * Security: The curve NIST p-256 provides approximately 128 bits of security.
72 */
73
74#ifndef __TC_ECC_DH_H__
75#define __TC_ECC_DH_H__
76
77#include <tinycrypt/ecc.h>
78
79#ifdef __cplusplus
80extern "C" {
81#endif
82
83/**
84 * @brief Create a public/private key pair.
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +010085 * @return UECC_SUCCESS or UECC_FAILURE or UECC_FAULT_DETECTED
Jarno Lamsa18987a42019-04-24 15:40:43 +030086 *
87 * @param p_public_key OUT -- Will be filled in with the public key. Must be at
88 * least 2 * the curve size (in bytes) long. For curve secp256r1, p_public_key
89 * must be 64 bytes long.
90 * @param p_private_key OUT -- Will be filled in with the private key. Must be as
91 * long as the curve order (for secp256r1, p_private_key must be 32 bytes long).
92 *
93 * @note side-channel countermeasure: algorithm strengthened against timing
94 * attack.
95 * @warning A cryptographically-secure PRNG function must be set (using
96 * uECC_set_rng()) before calling uECC_make_key().
97 */
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +010098int uECC_make_key(uint8_t *p_public_key, uint8_t *p_private_key);
Jarno Lamsa18987a42019-04-24 15:40:43 +030099
100#ifdef ENABLE_TESTS
101
102/**
103 * @brief Create a public/private key pair given a specific d.
104 *
105 * @note THIS FUNCTION SHOULD BE CALLED ONLY FOR TEST PURPOSES. Refer to
106 * uECC_make_key() function for real applications.
107 */
108int uECC_make_key_with_d(uint8_t *p_public_key, uint8_t *p_private_key,
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100109 unsigned int *d);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300110#endif
111
112/**
113 * @brief Compute a shared secret given your secret key and someone else's
114 * public key.
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100115 * @return UECC_SUCCESS or UECC_FAILURE or UECC_FAULT_DETECTED
Jarno Lamsa18987a42019-04-24 15:40:43 +0300116 *
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,
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100128 uint8_t *p_secret);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300129
130#ifdef __cplusplus
131}
132#endif
133
134#endif /* __TC_ECC_DH_H__ */