summaryrefslogtreecommitdiff
path: root/crypto_libraries/src/nx_crypto_phash.c
blob: b092b3c2f4fb7b0f268e30bd65bc0fd62ee530c7 (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
/***************************************************************************
 * 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                                                 */
/**                                                                       */
/**  Transport Layer Security (TLS) - PHASH function                      */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


#include "nx_crypto_phash.h"

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_phash                                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function implements phash for the crypto module.               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    phash                                 phash control block           */
/*    output                                Pointer to output buffer      */
/*    desired_length                        Number of bytes to return     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP UINT _nx_crypto_phash(NX_CRYPTO_PHASH *phash, UCHAR *output, UINT desired_length)
{
UINT    offset; /* point to the memory for the coming hmac data */
UINT    hash_size; /* the length of hmac output */
UINT    output_len; /* desired output length of hmac */
UINT    status;
/* A(i) */
UCHAR   *temp_A;
UINT    temp_A_size;
/* secret */
UCHAR   *secret;
UINT    secret_len;
/* seed */
UCHAR   *seed;
UINT    seed_len;
/* metadata */
UCHAR   *metadata;
UINT    metadata_size;
/* hmac output size */
UCHAR   *hmac_output;
UINT    hmac_output_size;
UINT    A_len; /* the length of current A(i) */
UINT    remaining_len, i; /* remaining length of data to be generated. */
VOID   *handler = NX_CRYPTO_NULL;
NX_CRYPTO_METHOD *hash_method = phash -> nx_crypto_hmac_method;

    NX_CRYPTO_STATE_CHECK

    /* Validate pointers. */
    if (hash_method == NX_CRYPTO_NULL
        || hash_method -> nx_crypto_operation == NX_CRYPTO_NULL
        || hash_method -> nx_crypto_cleanup == NX_CRYPTO_NULL
        || output == NX_CRYPTO_NULL)
    {
        return(NX_CRYPTO_INVALID_PARAMETER);
    }

    /* Initialize temporary variables. */
    secret = phash -> nx_crypto_phash_secret;
    secret_len = phash -> nx_crypto_phash_secret_length;
    seed = phash -> nx_crypto_phash_seed;
    seed_len = phash -> nx_crypto_phash_seed_length;
    temp_A = phash -> nx_crypto_phash_temp_A;
    temp_A_size = phash -> nx_crypto_phash_temp_A_size;
    hash_size = hash_method -> nx_crypto_ICV_size_in_bits >> 3;
    metadata = phash -> nx_crypto_hmac_metadata;
    metadata_size = phash -> nx_crypto_hmac_metadata_size;
    hmac_output = phash -> nx_crypto_hmac_output;
    hmac_output_size = phash -> nx_crypto_hmac_output_size;

    if (hash_size > hmac_output_size)
    {
        return(NX_CRYPTO_INVALID_PARAMETER);
    }

    if (seed_len > temp_A_size)
    {
        return(NX_CRYPTO_SIZE_ERROR);
    }

    /* Assign the seed as A(0). */
    NX_CRYPTO_MEMSET(temp_A, 0, temp_A_size);
    NX_CRYPTO_MEMCPY(temp_A, seed, seed_len); /* Use case of memcpy is verified. */
    A_len = phash -> nx_crypto_phash_seed_length;

    remaining_len = desired_length;
    for (offset = 0; offset < desired_length; offset += hash_size)
    {
        /* Calculate A(i) */
        if (hash_method -> nx_crypto_init)
        {
            status = hash_method -> nx_crypto_init(hash_method,
                                          secret,
                                          (NX_CRYPTO_KEY_SIZE)(secret_len << 3),
                                          &handler,
                                          metadata,
                                          metadata_size);

            if(status != NX_CRYPTO_SUCCESS)
            {
                return(status);
            }                                                     
        }

        status = hash_method -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE,
                                           handler,
                                           hash_method,
                                           secret,
                                           (NX_CRYPTO_KEY_SIZE)(secret_len << 3),
                                           temp_A,
                                           A_len,
                                           NX_CRYPTO_NULL,
                                           temp_A,
                                           temp_A_size,
                                           metadata,
                                           metadata_size,
                                           NX_CRYPTO_NULL,
                                           NX_CRYPTO_NULL);

        if(status != NX_CRYPTO_SUCCESS)
        {
            return(status);
        }                                                     
        
        /* Updated the length of A(i) */
        A_len = hash_size;

        if ((A_len + seed_len) > temp_A_size)
        {
            return(NX_CRYPTO_SIZE_ERROR);
        }

        /* Concatenate A[i] and seed to feed into digest. */
        NX_CRYPTO_MEMCPY(&temp_A[A_len], seed, seed_len); /* Use case of memcpy is verified. */

        /* Output block is the size of the digest unless the remaining
           desired length is smaller than the digest length. */
        if (remaining_len < hash_size)
        {
            output_len = remaining_len;
        }
        else
        {
            output_len = hash_size;
        }

        /* Calculate p-hash block, store in output. */
        status = hash_method -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE,
                                           handler,
                                           hash_method,
                                           secret,
                                           (NX_CRYPTO_KEY_SIZE)(secret_len << 3),
                                           temp_A,
                                           A_len + seed_len,
                                           NX_CRYPTO_NULL,
                                           hmac_output,
                                           output_len,
                                           metadata,
                                           metadata_size,
                                           NX_CRYPTO_NULL,
                                           NX_CRYPTO_NULL);

        if(status != NX_CRYPTO_SUCCESS)
        {
            return(status);
        }                                                     
        
        /* Append the hmac result to the output. */
        for (i = 0; i < output_len; i++)
        {
            output[offset + i] ^= hmac_output[i];
        }

        /* Adjust our remaining length by the number of bytes written. */
        remaining_len -= hash_size;

        status = hash_method -> nx_crypto_cleanup(metadata);

        if(status != NX_CRYPTO_SUCCESS)
        {
            return(status);
        }                                                     
    }

    return(NX_CRYPTO_SUCCESS);
}