1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Crypto Component                                                 */
16 /**                                                                       */
17 /**   Diffie-Hellman (DH)                                                 */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /**************************************************************************/
24 /*                                                                        */
25 /*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
26 /*                                                                        */
27 /*    nx_crypto_dh.h                                      PORTABLE C      */
28 /*                                                           6.1          */
29 /*  AUTHOR                                                                */
30 /*                                                                        */
31 /*    Timothy Stapko, Microsoft Corporation                               */
32 /*                                                                        */
33 /*  DESCRIPTION                                                           */
34 /*                                                                        */
35 /*    This file defines the basic Application Interface (API) to the      */
36 /*    NetX Crypto DH module.                                              */
37 /*                                                                        */
38 /*  RELEASE HISTORY                                                       */
39 /*                                                                        */
40 /*    DATE              NAME                      DESCRIPTION             */
41 /*                                                                        */
42 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
43 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
44 /*                                            resulting in version 6.1    */
45 /*                                                                        */
46 /**************************************************************************/
47 
48 #ifndef NX_CRYPTO_DH_H
49 #define NX_CRYPTO_DH_H
50 
51 /* Determine if a C++ compiler is being used.  If so, ensure that standard
52    C is used to process the API information.  */
53 #ifdef __cplusplus
54 
55 /* Yes, C++ compiler is present.  Use standard C.  */
56 extern   "C" {
57 
58 #endif
59 
60 #include "nx_crypto_huge_number.h"
61 
62 
63 /* Diffie-Hellman key size. Buffer size for calculations is twice the key size */
64 #define NX_CRYPTO_DIFFIE_HELLMAN_GROUP_2_KEY_SIZE (128)         /* 1024 bits/8. */
65 #define NX_CRYPTO_DIFFIE_HELLMAN_MAX_KEY_SIZE     (NX_CRYPTO_DIFFIE_HELLMAN_GROUP_2_KEY_SIZE)
66 
67 /* Buffer and scratch buffer sizes are calculated from the maximum key size. */
68 #define NX_CRYPTO_DIFFIE_HELLMAN_BUFFER_SIZE      (NX_CRYPTO_DIFFIE_HELLMAN_MAX_KEY_SIZE * 4)
69 #define NX_CRYPTO_DIFFIE_HELLMAN_SCRATCH_SIZE     (NX_CRYPTO_DIFFIE_HELLMAN_BUFFER_SIZE * 8)
70 
71 /* Diffie-Hellman groups and key constants. */
72 #define NX_CRYPTO_DH_GROUP_2_GENERATOR            (0x2)  /* Generator constant for Diffie-Hellman group 2. */
73 
74 #define NX_CRYPTO_DH_GROUP_2                      (0x2)  /* Standard DH group 2 for IPSEC. */
75 #define NX_CRYPTO_DH_GROUP_TEST                   (0xFF) /* DH group used for testing. Note that this will only be valid in test builds. */
76 
77 /* Diffie-Hellman Key-exchange control structure. */
78 typedef struct NX_CRYPTO_DH_STRUCT
79 {
80     /* The size of the key being used. This is primarily for testing, but also allows for future expansion.
81        The value is assigned in _nx_crypto_dh_setup depending on the chosen group. */
82     UINT nx_crypto_dh_key_size;
83 
84     /* The private key is generated by nx_crypto_dh_setup and is a random number.
85        During the computation, the private key is used as exponent.  Therefore the buffer size is
86        the same as the key size.  This number does not expand during the power-modulus computation.
87        Make the array in units of UINT to make sure the starting address is 4-byte aligned. */
88     HN_UBASE nx_crypto_dh_private_key_buffer[NX_CRYPTO_DIFFIE_HELLMAN_MAX_KEY_SIZE >> HN_SIZE_SHIFT];
89 
90     /* The modulus, determined by the Diffie-Hellman group selected in the call to nx_crypto_dh_setup.
91        This number does not expand during the power-modulus computation.
92        Make the array in units of UINT to make sure the starting address is 4-byte aligned. */
93     HN_UBASE *nx_crypto_dh_modulus;
94 } NX_CRYPTO_DH;
95 
96 /* Function prototypes */
97 
98 
99 UINT _nx_crypto_dh_setup(NX_CRYPTO_DH  *dh_ptr,
100                          UCHAR  *local_public_key_ptr,
101                          UINT   *local_public_key_len_ptr,
102                          ULONG   dh_group_num,
103                          HN_UBASE *scratch_buf_ptr);
104 
105 UINT _nx_crypto_dh_compute_secret(NX_CRYPTO_DH  *dh_ptr,
106                                   UCHAR  *share_secret_key_ptr,
107                                   ULONG  *share_secret_key_len_ptr,
108                                   UCHAR  *remote_public_key,
109                                   ULONG   remote_public_key_len,
110                                   HN_UBASE *scratch_buf_ptr);
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif /* NX_CRYPTO_DH_H */
117 
118