xref: /reactos/dll/3rdparty/mbedtls/hkdf.c (revision 9d33a205)
1 /*
2  *  HKDF implementation -- RFC 5869
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  *
7  *  This file is provided under the Apache License 2.0, or the
8  *  GNU General Public License v2.0 or later.
9  *
10  *  **********
11  *  Apache License 2.0:
12  *
13  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14  *  not use this file except in compliance with the License.
15  *  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  *  Unless required by applicable law or agreed to in writing, software
20  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the License for the specific language governing permissions and
23  *  limitations under the License.
24  *
25  *  **********
26  *
27  *  **********
28  *  GNU General Public License v2.0 or later:
29  *
30  *  This program is free software; you can redistribute it and/or modify
31  *  it under the terms of the GNU General Public License as published by
32  *  the Free Software Foundation; either version 2 of the License, or
33  *  (at your option) any later version.
34  *
35  *  This program is distributed in the hope that it will be useful,
36  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  *  GNU General Public License for more details.
39  *
40  *  You should have received a copy of the GNU General Public License along
41  *  with this program; if not, write to the Free Software Foundation, Inc.,
42  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43  *
44  *  **********
45  */
46 #if !defined(MBEDTLS_CONFIG_FILE)
47 #include "mbedtls/config.h"
48 #else
49 #include MBEDTLS_CONFIG_FILE
50 #endif
51 
52 #if defined(MBEDTLS_HKDF_C)
53 
54 #include <string.h>
55 #include "mbedtls/hkdf.h"
56 #include "mbedtls/platform_util.h"
57 
58 int mbedtls_hkdf( const mbedtls_md_info_t *md, const unsigned char *salt,
59                   size_t salt_len, const unsigned char *ikm, size_t ikm_len,
60                   const unsigned char *info, size_t info_len,
61                   unsigned char *okm, size_t okm_len )
62 {
63     int ret;
64     unsigned char prk[MBEDTLS_MD_MAX_SIZE];
65 
66     ret = mbedtls_hkdf_extract( md, salt, salt_len, ikm, ikm_len, prk );
67 
68     if( ret == 0 )
69     {
70         ret = mbedtls_hkdf_expand( md, prk, mbedtls_md_get_size( md ),
71                                    info, info_len, okm, okm_len );
72     }
73 
74     mbedtls_platform_zeroize( prk, sizeof( prk ) );
75 
76     return( ret );
77 }
78 
79 int mbedtls_hkdf_extract( const mbedtls_md_info_t *md,
80                           const unsigned char *salt, size_t salt_len,
81                           const unsigned char *ikm, size_t ikm_len,
82                           unsigned char *prk )
83 {
84     unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
85 
86     if( salt == NULL )
87     {
88         size_t hash_len;
89 
90         if( salt_len != 0 )
91         {
92             return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
93         }
94 
95         hash_len = mbedtls_md_get_size( md );
96 
97         if( hash_len == 0 )
98         {
99             return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
100         }
101 
102         salt = null_salt;
103         salt_len = hash_len;
104     }
105 
106     return( mbedtls_md_hmac( md, salt, salt_len, ikm, ikm_len, prk ) );
107 }
108 
109 int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk,
110                          size_t prk_len, const unsigned char *info,
111                          size_t info_len, unsigned char *okm, size_t okm_len )
112 {
113     size_t hash_len;
114     size_t where = 0;
115     size_t n;
116     size_t t_len = 0;
117     size_t i;
118     int ret = 0;
119     mbedtls_md_context_t ctx;
120     unsigned char t[MBEDTLS_MD_MAX_SIZE];
121 
122     if( okm == NULL )
123     {
124         return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
125     }
126 
127     hash_len = mbedtls_md_get_size( md );
128 
129     if( prk_len < hash_len || hash_len == 0 )
130     {
131         return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
132     }
133 
134     if( info == NULL )
135     {
136         info = (const unsigned char *) "";
137         info_len = 0;
138     }
139 
140     n = okm_len / hash_len;
141 
142     if( (okm_len % hash_len) != 0 )
143     {
144         n++;
145     }
146 
147     /*
148      * Per RFC 5869 Section 2.3, okm_len must not exceed
149      * 255 times the hash length
150      */
151     if( n > 255 )
152     {
153         return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
154     }
155 
156     mbedtls_md_init( &ctx );
157 
158     if( (ret = mbedtls_md_setup( &ctx, md, 1) ) != 0 )
159     {
160         goto exit;
161     }
162 
163     /*
164      * Compute T = T(1) | T(2) | T(3) | ... | T(N)
165      * Where T(N) is defined in RFC 5869 Section 2.3
166      */
167     for( i = 1; i <= n; i++ )
168     {
169         size_t num_to_copy;
170         unsigned char c = i & 0xff;
171 
172         ret = mbedtls_md_hmac_starts( &ctx, prk, prk_len );
173         if( ret != 0 )
174         {
175             goto exit;
176         }
177 
178         ret = mbedtls_md_hmac_update( &ctx, t, t_len );
179         if( ret != 0 )
180         {
181             goto exit;
182         }
183 
184         ret = mbedtls_md_hmac_update( &ctx, info, info_len );
185         if( ret != 0 )
186         {
187             goto exit;
188         }
189 
190         /* The constant concatenated to the end of each T(n) is a single octet.
191          * */
192         ret = mbedtls_md_hmac_update( &ctx, &c, 1 );
193         if( ret != 0 )
194         {
195             goto exit;
196         }
197 
198         ret = mbedtls_md_hmac_finish( &ctx, t );
199         if( ret != 0 )
200         {
201             goto exit;
202         }
203 
204         num_to_copy = i != n ? hash_len : okm_len - where;
205         memcpy( okm + where, t, num_to_copy );
206         where += hash_len;
207         t_len = hash_len;
208     }
209 
210 exit:
211     mbedtls_md_free( &ctx );
212     mbedtls_platform_zeroize( t, sizeof( t ) );
213 
214     return( ret );
215 }
216 
217 #endif /* MBEDTLS_HKDF_C */
218