summaryrefslogtreecommitdiff
path: root/crypto_libraries/src/nx_crypto_dh.c
blob: 33ef1fd366a189cbdbd5e280e26945550166c0ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Crypto Component                                                 */
/**                                                                       */
/**   Diffie-Hellman (DH)                                                 */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_CRYPTO_SOURCE_CODE


/* Include necessary system files.  */

#include "nx_crypto_dh.h"
#ifndef NX_CRYPTO_SELF_TEST

/* The Diffie-Hellman group 2 modulus. */
/* Modulus, in byte stream, be */
/* ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece65381ffffffffffffffff */
static const HN_UBASE _nx_dh_group_2_modulus[] =
{
    HN_ULONG_TO_UBASE(0xFFFFFFFF), HN_ULONG_TO_UBASE(0xFFFFFFFF),
    HN_ULONG_TO_UBASE(0xECE65381), HN_ULONG_TO_UBASE(0x49286651),
    HN_ULONG_TO_UBASE(0x7C4B1FE6), HN_ULONG_TO_UBASE(0xAE9F2411),
    HN_ULONG_TO_UBASE(0x5A899FA5), HN_ULONG_TO_UBASE(0xEE386BFB),
    HN_ULONG_TO_UBASE(0xF406B7ED), HN_ULONG_TO_UBASE(0x0BFF5CB6),
    HN_ULONG_TO_UBASE(0xA637ED6B), HN_ULONG_TO_UBASE(0xF44C42E9),
    HN_ULONG_TO_UBASE(0x625E7EC6), HN_ULONG_TO_UBASE(0xE485B576),
    HN_ULONG_TO_UBASE(0x6D51C245), HN_ULONG_TO_UBASE(0x4FE1356D),
    HN_ULONG_TO_UBASE(0xF25F1437), HN_ULONG_TO_UBASE(0x302B0A6D),
    HN_ULONG_TO_UBASE(0xCD3A431B), HN_ULONG_TO_UBASE(0xEF9519B3),
    HN_ULONG_TO_UBASE(0x8E3404DD), HN_ULONG_TO_UBASE(0x514A0879),
    HN_ULONG_TO_UBASE(0x3B139B22), HN_ULONG_TO_UBASE(0x020BBEA6),
    HN_ULONG_TO_UBASE(0x8A67CC74), HN_ULONG_TO_UBASE(0x29024E08),
    HN_ULONG_TO_UBASE(0x80DC1CD1), HN_ULONG_TO_UBASE(0xC4C6628B),
    HN_ULONG_TO_UBASE(0x2168C234), HN_ULONG_TO_UBASE(0xC90FDAA2),
    HN_ULONG_TO_UBASE(0xFFFFFFFF), HN_ULONG_TO_UBASE(0xFFFFFFFF)
};


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_dh_setup                                 PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sets up a Diffie-Hellman context by generating a      */
/*    local.                                                              */
/*                                                                        */
/*    Note: The scratch buffer must be able to hold a number of *bytes*   */
/*          at least equal to NX_CRYPTO_DIFFIE_HELLMAN_SCRATCH_SIZE.      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    dh_ptr                                Diffie-Hellman context        */
/*    local_public_key                      Pointer to local public key   */
/*    local_public_key_len                  Local public key length       */
/*    dh_group_num                          Chosen DH group number        */
/*    scratch_buf_ptr                       Pointer to scratch buffer,    */
/*                                            which cannot be smaller     */
/*                                            than 6 times of the key     */
/*                                            size (in bytes). This       */
/*                                            scratch buffer can be       */
/*                                            reused after this function  */
/*                                            returns.                    */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_crypto_huge_number_adjust_size    Adjust the size of a huge     */
/*                                          number to remove leading      */
/*                                          zeroes                        */
/*    _nx_crypto_huge_number_mont_power_modulus                           */
/*                                          Raise a huge number for       */
/*                                            montgomery reduction        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP UINT _nx_crypto_dh_setup(NX_CRYPTO_DH  *dh_ptr,
                                        UCHAR  *local_public_key_ptr,
                                        UINT   *local_public_key_len_ptr,
                                        ULONG   dh_group_num,
                                        HN_UBASE *scratch_buf_ptr)
{

UINT i;


/* Actual huge numbers used in calculations */
NX_CRYPTO_HUGE_NUMBER modulus, public_key, private_key, generator;
HN_UBASE              generator_value;

    NX_CRYPTO_STATE_CHECK

    /* Assign the desired key size based on the chosen Diffie-Hellman group. */
    switch (dh_group_num)
    {
    case NX_CRYPTO_DH_GROUP_2:
        dh_ptr -> nx_crypto_dh_key_size = NX_CRYPTO_DIFFIE_HELLMAN_GROUP_2_KEY_SIZE;
        dh_ptr -> nx_crypto_dh_modulus = (HN_UBASE *)_nx_dh_group_2_modulus;

        NX_CRYPTO_HUGE_NUMBER_INITIALIZE_DIGIT(&generator, &generator_value,
                                               NX_CRYPTO_DH_GROUP_2_GENERATOR)

        /* Setup the modulus value. */
        modulus.nx_crypto_huge_number_data = dh_ptr -> nx_crypto_dh_modulus;
        modulus.nx_crypto_huge_number_size =  dh_ptr -> nx_crypto_dh_key_size >> HN_SIZE_SHIFT;
        modulus.nx_crypto_huge_buffer_size =  dh_ptr -> nx_crypto_dh_key_size;
        modulus.nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE;


        break;

    default:
        /* No supported group specified - error! */
        return(NX_CRYPTO_NOT_SUCCESSFUL);
    }

    /* Local pointer for pointer arithmetic.  The power-modulus operation does not require the scratch area
       to be "clean". Therefore no need to zero out the buffer.

       Public key buffer (and scratch). */
    NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&public_key, scratch_buf_ptr, dh_ptr -> nx_crypto_dh_key_size << 1);

    /* Private key buffer - note that no scratch is required for the private key. */
    private_key.nx_crypto_huge_number_data = dh_ptr -> nx_crypto_dh_private_key_buffer;
    private_key.nx_crypto_huge_number_size = dh_ptr -> nx_crypto_dh_key_size >> HN_SIZE_SHIFT;
    private_key.nx_crypto_huge_buffer_size = sizeof(dh_ptr -> nx_crypto_dh_private_key_buffer);
    private_key.nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE;


    /* Generate the private key. */
    for (i = 0; i < private_key.nx_crypto_huge_number_size; i++)
    {
        /* Grab a random value - this may be more than one byte and we want to use
           all the bytes in the value, so we do not simply copy the random_value
           into the buffers. */
        dh_ptr -> nx_crypto_dh_private_key_buffer[i] = (HN_UBASE)((HN_UBASE)(NX_CRYPTO_RAND()) & HN_MASK);
    }

    /* Finally, generate the public key from the private key, modulus, and the generator.
       The actual calculation is "public_key = (generator**private_key) % modulus"
       where the "**" denotes exponentiation. */
    _nx_crypto_huge_number_mont_power_modulus(&generator, &private_key,
                                              &modulus, &public_key, scratch_buf_ptr);

    /* Copy the public key into the return buffer. */
    _nx_crypto_huge_number_extract(&public_key, local_public_key_ptr,
                                   dh_ptr -> nx_crypto_dh_key_size, local_public_key_len_ptr);

    return(NX_CRYPTO_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_dh_compute_secret                        PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function computes the Diffie-Hellman shared secret using an    */
/*    existing Diffie-Hellman context and a public key received from a    */
/*    remote entity, usually as part of an IPSEC or SSL key exchange.     */
/*                                                                        */
/*    Note: The scratch buffer must be able to hold a number of *bytes*   */
/*          at least equal to NX_CRYPTO_DIFFIE_HELLMAN_SCRATCH_SIZE.      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    dh_ptr                                Diffie-Hellman context        */
/*    share_secret_key_ptr                  Shared secret buffer pointer  */
/*    share_secret_key_len_ptr              Length of shared secret       */
/*    remote_public_key                     Pointer to remote public key  */
/*    remote_public_key_len                 Remote public key length      */
/*    scratch_buf_ptr                       Pointer to scratch buffer,    */
/*                                            which cannot be smaller     */
/*                                            than 8 times of the key     */
/*                                            size (in bytes). This       */
/*                                            scratch buffer can be       */
/*                                            reused after this function  */
/*                                            returns.                    */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_crypto_huge_number_extract        Extract huge number           */
/*    _nx_crypto_huge_number_setup          Setup huge number             */
/*    _nx_crypto_huge_number_mont_power_modulus                           */
/*                                          Raise a huge number for       */
/*                                            montgomery reduction        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP UINT _nx_crypto_dh_compute_secret(NX_CRYPTO_DH  *dh_ptr,
                                                 UCHAR  *share_secret_key_ptr,
                                                 ULONG  *share_secret_key_len_ptr,
                                                 UCHAR  *remote_public_key,
                                                 ULONG   remote_public_key_len,
                                                 HN_UBASE *scratch_buf_ptr)
{

UINT key_size;

/* Actual huge numbers used in calculations */
NX_CRYPTO_HUGE_NUMBER modulus, public_key, private_key, shared_secret;

    NX_CRYPTO_STATE_CHECK

    /* Make sure the key size was assigned before we do anything else. Generally, this means
       _nx_crypto_dh_setup was not called to set up the NX_DH structure prior to this call.  */
    if (0 == dh_ptr -> nx_crypto_dh_key_size)
    {
        return(NX_CRYPTO_NOT_SUCCESSFUL);
    }

    /* Make sure the remote public key is small enough to fit into the huge number buffer. */
    if (remote_public_key_len > dh_ptr -> nx_crypto_dh_key_size)
    {
        return(NX_CRYPTO_NOT_SUCCESSFUL);
    }

    /* Figure out the sizes of our keys and buffers. We need 2X the key size for our buffer space. */
    key_size = dh_ptr -> nx_crypto_dh_key_size;

    NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&public_key, scratch_buf_ptr, key_size);
    NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&shared_secret, scratch_buf_ptr, key_size << 1);

    /* Copy the remote public key from the caller's buffer. */
    _nx_crypto_huge_number_setup(&public_key, remote_public_key, remote_public_key_len);



    /* Set up each of the buffers - point into the scratch buffer at increments of the DH buffer size. */
    modulus.nx_crypto_huge_number_data = (HN_UBASE *)dh_ptr -> nx_crypto_dh_modulus;
    modulus.nx_crypto_huge_number_size = key_size >> HN_SIZE_SHIFT;
    modulus.nx_crypto_huge_buffer_size = key_size;
    modulus.nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE;


    /* Private key buffer - note that no scratch is required for the private key, but we set it in case
       it is needed in the future. */
    private_key.nx_crypto_huge_number_data = (HN_UBASE *)dh_ptr -> nx_crypto_dh_private_key_buffer;
    private_key.nx_crypto_huge_number_size = key_size >> HN_SIZE_SHIFT;
    private_key.nx_crypto_huge_buffer_size = key_size;
    private_key.nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE;

    /* Finally, generate shared secret from the remote public key, our generated private key, and the modulus, modulus.
       The actual calculation is "shared_secret = (public_key**private_key) % modulus"
       where the "**" denotes exponentiation. */
    _nx_crypto_huge_number_mont_power_modulus(&public_key, &private_key, &modulus,
                                              &shared_secret, scratch_buf_ptr);

    /* Now we have a shared secret to return to the caller. Check to make sure the buffer is large enough to hold the public key. */
    if (*share_secret_key_len_ptr < key_size)
    {
        return(NX_CRYPTO_NOT_SUCCESSFUL);
    }

    /* The public key size is simply the key size for this group. */

    /* Copy the shared secret into the return buffer. */
    _nx_crypto_huge_number_extract(&shared_secret, share_secret_key_ptr,
                                   key_size, (UINT *)share_secret_key_len_ptr);

    return(NX_CRYPTO_SUCCESS);
}

#endif