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