xref: /aosp_15_r20/external/boringssl/src/include/openssl/ecdh.h (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1*8fb009dcSAndroid Build Coastguard Worker /* ====================================================================
2*8fb009dcSAndroid Build Coastguard Worker  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
3*8fb009dcSAndroid Build Coastguard Worker  *
4*8fb009dcSAndroid Build Coastguard Worker  * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
5*8fb009dcSAndroid Build Coastguard Worker  * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
6*8fb009dcSAndroid Build Coastguard Worker  * to the OpenSSL project.
7*8fb009dcSAndroid Build Coastguard Worker  *
8*8fb009dcSAndroid Build Coastguard Worker  * The ECC Code is licensed pursuant to the OpenSSL open source
9*8fb009dcSAndroid Build Coastguard Worker  * license provided below.
10*8fb009dcSAndroid Build Coastguard Worker  *
11*8fb009dcSAndroid Build Coastguard Worker  * The ECDH software is originally written by Douglas Stebila of
12*8fb009dcSAndroid Build Coastguard Worker  * Sun Microsystems Laboratories.
13*8fb009dcSAndroid Build Coastguard Worker  *
14*8fb009dcSAndroid Build Coastguard Worker  */
15*8fb009dcSAndroid Build Coastguard Worker /* ====================================================================
16*8fb009dcSAndroid Build Coastguard Worker  * Copyright (c) 2000-2002 The OpenSSL Project.  All rights reserved.
17*8fb009dcSAndroid Build Coastguard Worker  *
18*8fb009dcSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
19*8fb009dcSAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
20*8fb009dcSAndroid Build Coastguard Worker  * are met:
21*8fb009dcSAndroid Build Coastguard Worker  *
22*8fb009dcSAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright
23*8fb009dcSAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
24*8fb009dcSAndroid Build Coastguard Worker  *
25*8fb009dcSAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright
26*8fb009dcSAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
27*8fb009dcSAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
28*8fb009dcSAndroid Build Coastguard Worker  *    distribution.
29*8fb009dcSAndroid Build Coastguard Worker  *
30*8fb009dcSAndroid Build Coastguard Worker  * 3. All advertising materials mentioning features or use of this
31*8fb009dcSAndroid Build Coastguard Worker  *    software must display the following acknowledgment:
32*8fb009dcSAndroid Build Coastguard Worker  *    "This product includes software developed by the OpenSSL Project
33*8fb009dcSAndroid Build Coastguard Worker  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
34*8fb009dcSAndroid Build Coastguard Worker  *
35*8fb009dcSAndroid Build Coastguard Worker  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
36*8fb009dcSAndroid Build Coastguard Worker  *    endorse or promote products derived from this software without
37*8fb009dcSAndroid Build Coastguard Worker  *    prior written permission. For written permission, please contact
38*8fb009dcSAndroid Build Coastguard Worker  *    [email protected].
39*8fb009dcSAndroid Build Coastguard Worker  *
40*8fb009dcSAndroid Build Coastguard Worker  * 5. Products derived from this software may not be called "OpenSSL"
41*8fb009dcSAndroid Build Coastguard Worker  *    nor may "OpenSSL" appear in their names without prior written
42*8fb009dcSAndroid Build Coastguard Worker  *    permission of the OpenSSL Project.
43*8fb009dcSAndroid Build Coastguard Worker  *
44*8fb009dcSAndroid Build Coastguard Worker  * 6. Redistributions of any form whatsoever must retain the following
45*8fb009dcSAndroid Build Coastguard Worker  *    acknowledgment:
46*8fb009dcSAndroid Build Coastguard Worker  *    "This product includes software developed by the OpenSSL Project
47*8fb009dcSAndroid Build Coastguard Worker  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
48*8fb009dcSAndroid Build Coastguard Worker  *
49*8fb009dcSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
50*8fb009dcSAndroid Build Coastguard Worker  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51*8fb009dcSAndroid Build Coastguard Worker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52*8fb009dcSAndroid Build Coastguard Worker  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
53*8fb009dcSAndroid Build Coastguard Worker  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54*8fb009dcSAndroid Build Coastguard Worker  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55*8fb009dcSAndroid Build Coastguard Worker  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56*8fb009dcSAndroid Build Coastguard Worker  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57*8fb009dcSAndroid Build Coastguard Worker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
58*8fb009dcSAndroid Build Coastguard Worker  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59*8fb009dcSAndroid Build Coastguard Worker  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
60*8fb009dcSAndroid Build Coastguard Worker  * OF THE POSSIBILITY OF SUCH DAMAGE.
61*8fb009dcSAndroid Build Coastguard Worker  * ====================================================================
62*8fb009dcSAndroid Build Coastguard Worker  *
63*8fb009dcSAndroid Build Coastguard Worker  * This product includes cryptographic software written by Eric Young
64*8fb009dcSAndroid Build Coastguard Worker  * ([email protected]).  This product includes software written by Tim
65*8fb009dcSAndroid Build Coastguard Worker  * Hudson ([email protected]). */
66*8fb009dcSAndroid Build Coastguard Worker 
67*8fb009dcSAndroid Build Coastguard Worker #ifndef OPENSSL_HEADER_ECDH_H
68*8fb009dcSAndroid Build Coastguard Worker #define OPENSSL_HEADER_ECDH_H
69*8fb009dcSAndroid Build Coastguard Worker 
70*8fb009dcSAndroid Build Coastguard Worker #include <openssl/base.h>
71*8fb009dcSAndroid Build Coastguard Worker 
72*8fb009dcSAndroid Build Coastguard Worker #include <openssl/ec_key.h>
73*8fb009dcSAndroid Build Coastguard Worker 
74*8fb009dcSAndroid Build Coastguard Worker #if defined(__cplusplus)
75*8fb009dcSAndroid Build Coastguard Worker extern "C" {
76*8fb009dcSAndroid Build Coastguard Worker #endif
77*8fb009dcSAndroid Build Coastguard Worker 
78*8fb009dcSAndroid Build Coastguard Worker 
79*8fb009dcSAndroid Build Coastguard Worker // Elliptic curve Diffie-Hellman.
80*8fb009dcSAndroid Build Coastguard Worker 
81*8fb009dcSAndroid Build Coastguard Worker 
82*8fb009dcSAndroid Build Coastguard Worker // ECDH_compute_key calculates the shared key between |pub_key| and |priv_key|.
83*8fb009dcSAndroid Build Coastguard Worker // If |kdf| is not NULL, then it is called with the bytes of the shared key and
84*8fb009dcSAndroid Build Coastguard Worker // the parameter |out|. When |kdf| returns, the value of |*outlen| becomes the
85*8fb009dcSAndroid Build Coastguard Worker // return value. Otherwise, as many bytes of the shared key as will fit are
86*8fb009dcSAndroid Build Coastguard Worker // copied directly to, at most, |outlen| bytes at |out|. It returns the number
87*8fb009dcSAndroid Build Coastguard Worker // of bytes written to |out|, or -1 on error.
88*8fb009dcSAndroid Build Coastguard Worker OPENSSL_EXPORT int ECDH_compute_key(
89*8fb009dcSAndroid Build Coastguard Worker     void *out, size_t outlen, const EC_POINT *pub_key, const EC_KEY *priv_key,
90*8fb009dcSAndroid Build Coastguard Worker     void *(*kdf)(const void *in, size_t inlen, void *out, size_t *outlen));
91*8fb009dcSAndroid Build Coastguard Worker 
92*8fb009dcSAndroid Build Coastguard Worker // ECDH_compute_key_fips calculates the shared key between |pub_key| and
93*8fb009dcSAndroid Build Coastguard Worker // |priv_key| and hashes it with the appropriate SHA function for |out_len|. The
94*8fb009dcSAndroid Build Coastguard Worker // only value values for |out_len| are thus 24 (SHA-224), 32 (SHA-256), 48
95*8fb009dcSAndroid Build Coastguard Worker // (SHA-384), and 64 (SHA-512). It returns one on success and zero on error.
96*8fb009dcSAndroid Build Coastguard Worker //
97*8fb009dcSAndroid Build Coastguard Worker // Note that the return value is different to |ECDH_compute_key|: it returns an
98*8fb009dcSAndroid Build Coastguard Worker // error flag (as is common for BoringSSL) rather than the number of bytes
99*8fb009dcSAndroid Build Coastguard Worker // written.
100*8fb009dcSAndroid Build Coastguard Worker //
101*8fb009dcSAndroid Build Coastguard Worker // This function allows the FIPS module to compute an ECDH and KDF within the
102*8fb009dcSAndroid Build Coastguard Worker // module boundary without taking an arbitrary function pointer for the KDF,
103*8fb009dcSAndroid Build Coastguard Worker // which isn't very FIPSy.
104*8fb009dcSAndroid Build Coastguard Worker OPENSSL_EXPORT int ECDH_compute_key_fips(uint8_t *out, size_t out_len,
105*8fb009dcSAndroid Build Coastguard Worker                                          const EC_POINT *pub_key,
106*8fb009dcSAndroid Build Coastguard Worker                                          const EC_KEY *priv_key);
107*8fb009dcSAndroid Build Coastguard Worker 
108*8fb009dcSAndroid Build Coastguard Worker 
109*8fb009dcSAndroid Build Coastguard Worker #if defined(__cplusplus)
110*8fb009dcSAndroid Build Coastguard Worker }  // extern C
111*8fb009dcSAndroid Build Coastguard Worker #endif
112*8fb009dcSAndroid Build Coastguard Worker 
113*8fb009dcSAndroid Build Coastguard Worker #define ECDH_R_KDF_FAILED 100
114*8fb009dcSAndroid Build Coastguard Worker #define ECDH_R_NO_PRIVATE_VALUE 101
115*8fb009dcSAndroid Build Coastguard Worker #define ECDH_R_POINT_ARITHMETIC_FAILURE 102
116*8fb009dcSAndroid Build Coastguard Worker #define ECDH_R_UNKNOWN_DIGEST_LENGTH 103
117*8fb009dcSAndroid Build Coastguard Worker 
118*8fb009dcSAndroid Build Coastguard Worker #endif  // OPENSSL_HEADER_ECDH_H
119