1 /**
2  *  Copyright Notice:
3  *  Copyright 2021-2022 DMTF. All rights reserved.
4  *  License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5  **/
6 
7 #ifndef CRYPTLIB_DH_H
8 #define CRYPTLIB_DH_H
9 
10 /*=====================================================================================
11  *    Diffie-Hellman Key Exchange Primitives
12  *=====================================================================================
13  */
14 
15 #if LIBSPDM_FFDHE_SUPPORT
16 /**
17  * Allocates and initializes one Diffie-Hellman context for subsequent use with the NID.
18  *
19  * @param nid cipher NID
20  *
21  * @return  Pointer to the Diffie-Hellman context that has been initialized.
22  *          If the allocations fails, libspdm_dh_new_by_nid() returns NULL.
23  *          If the interface is not supported, libspdm_dh_new_by_nid() returns NULL.
24  **/
25 extern void *libspdm_dh_new_by_nid(size_t nid);
26 
27 /**
28  * Release the specified DH context.
29  *
30  * @param[in]  dh_context  Pointer to the DH context to be released.
31  **/
32 void libspdm_dh_free(void *dh_context);
33 
34 /**
35  * Generates DH public key.
36  *
37  * This function generates random secret exponent, and computes the public key, which is
38  * returned via parameter public_key and public_key_size. DH context is updated accordingly.
39  * If the public_key buffer is too small to hold the public key, false is returned and
40  * public_key_size is set to the required buffer size to obtain the public key.
41  *
42  * If dh_context is NULL, then return false.
43  * If public_key_size is NULL, then return false.
44  * If public_key_size is large enough but public_key is NULL, then return false.
45  * If this interface is not supported, then return false.
46  *
47  * For FFDHE2048, the public_size is 256.
48  * For FFDHE3072, the public_size is 384.
49  * For FFDHE4096, the public_size is 512.
50  *
51  * @param[in, out]  dh_context       Pointer to the DH context.
52  * @param[out]      public_key       Pointer to the buffer to receive generated public key.
53  * @param[in, out]  public_key_size  On input, the size of public_key buffer in bytes.
54  *                                   On output, the size of data returned in public_key buffer in
55  *                                   bytes.
56  *
57  * @retval true   DH public key generation succeeded.
58  * @retval false  DH public key generation failed.
59  * @retval false  public_key_size is not large enough.
60  * @retval false  This interface is not supported.
61  **/
62 extern bool libspdm_dh_generate_key(void *dh_context, uint8_t *public_key, size_t *public_key_size);
63 
64 /**
65  * Computes exchanged common key.
66  *
67  * Given peer's public key, this function computes the exchanged common key, based on its own
68  * context including value of prime modulus and random secret exponent.
69  *
70  * If dh_context is NULL, then return false.
71  * If peer_public_key is NULL, then return false.
72  * If key_size is NULL, then return false.
73  * If key is NULL, then return false.
74  * If key_size is not large enough, then return false.
75  * If this interface is not supported, then return false.
76  *
77  * For FFDHE2048, the peer_public_size and key_size is 256.
78  * For FFDHE3072, the peer_public_size and key_size is 384.
79  * For FFDHE4096, the peer_public_size and key_size is 512.
80  *
81  * @param[in, out]  dh_context            Pointer to the DH context.
82  * @param[in]       peer_public_key       Pointer to the peer's public key.
83  * @param[in]       peer_public_key_size  size of peer's public key in bytes.
84  * @param[out]      key                   Pointer to the buffer to receive generated key.
85  * @param[in, out]  key_size              On input, the size of key buffer in bytes.
86  *                                        On output, the size of data returned in key buffer in
87  *                                        bytes.
88  *
89  * @retval true   DH exchanged key generation succeeded.
90  * @retval false  DH exchanged key generation failed.
91  * @retval false  key_size is not large enough.
92  * @retval false  This interface is not supported.
93  **/
94 extern bool libspdm_dh_compute_key(void *dh_context, const uint8_t *peer_public_key,
95                                    size_t peer_public_key_size, uint8_t *key,
96                                    size_t *key_size);
97 #endif /* LIBSPDM_FFDHE_SUPPORT */
98 #endif /* CRYPTLIB_DH_H */
99