1 /*
2  *  HKDF implementation -- RFC 5869
3  *
4  *  Copyright (C) 2016-2018, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #include "hkdf.h"
23 
mbedtls_hkdf(const mbedtls_md_info_t * md,const uint8_t * salt,size_t salt_len,const uint8_t * ikm,size_t ikm_len,const uint8_t * info,size_t info_len,uint8_t * okm,size_t okm_len)24 int32_t mbedtls_hkdf(const mbedtls_md_info_t *md, const uint8_t *salt,
25                   size_t salt_len, const uint8_t *ikm, size_t ikm_len,
26                   const uint8_t *info, size_t info_len,
27                   uint8_t *okm, size_t okm_len)
28 {
29     int32_t ret;
30     uint8_t prk[MBEDTLS_MD_MAX_SIZE];
31 
32     ret = mbedtls_hkdf_extract(md, salt, salt_len, ikm, ikm_len, prk);
33 
34     if (ret == 0) {
35         ret = mbedtls_hkdf_expand(md, prk, (size_t)mbedtls_md_get_size(md),
36                                    info, info_len, okm, okm_len);
37     }
38 
39     (void)mbedtls_platform_zeroize(prk, sizeof(prk));
40 
41     return ret;
42 }
43 
mbedtls_hkdf_extract(const mbedtls_md_info_t * md,const uint8_t * salt,size_t salt_len,const uint8_t * ikm,size_t ikm_len,uint8_t * prk)44 int32_t mbedtls_hkdf_extract(const mbedtls_md_info_t *md,
45                           const uint8_t *salt, size_t salt_len,
46                           const uint8_t *ikm, size_t ikm_len,
47                           uint8_t *prk)
48 {
49     int32_t ret = 0;
50     size_t tmp_salt_len = salt_len;
51     const uint8_t *tmp_salt = salt;
52     uint8_t null_salt[MBEDTLS_MD_MAX_SIZE] = { 0U };
53 
54     if (tmp_salt == NULL) {
55         size_t hash_len;
56 
57         if (tmp_salt_len != 0U) {
58             ret =  MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
59         } else {
60 
61             hash_len = mbedtls_md_get_size(md);
62 
63             if (hash_len == 0U) {
64                 ret = MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
65             } else {
66                 tmp_salt = null_salt;
67                 tmp_salt_len = hash_len;
68             }
69         }
70     }
71 
72     if (ret == 0) {
73         ret = mbedtls_md_hmac(md, tmp_salt, tmp_salt_len, ikm, ikm_len, prk);
74     }
75 
76     return ret;
77 }
78 
mbedtls_hkdf_expand(const mbedtls_md_info_t * md,const uint8_t * prk,size_t prk_len,const uint8_t * info,size_t info_len,uint8_t * okm,size_t okm_len)79 int32_t mbedtls_hkdf_expand(const mbedtls_md_info_t *md, const uint8_t *prk,
80                          size_t prk_len, const uint8_t *info,
81                          size_t info_len, uint8_t *okm, size_t okm_len)
82 {
83     size_t hash_len;
84     size_t where = 0;
85     size_t n;
86     size_t t_len = 0;
87     size_t tmp_info_len = info_len;
88     const uint8_t *tmp_info = info;
89     size_t i;
90     int32_t ret = 0;
91     mbedtls_md_context_t ctx;
92     uint8_t t[MBEDTLS_MD_MAX_SIZE];
93 
94     hash_len = mbedtls_md_get_size(md);
95 
96     if ((okm == NULL) || (prk_len < hash_len) || (hash_len == 0U)) {
97         ret = MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
98     } else {
99 
100         if (tmp_info == NULL) {
101             tmp_info = (const uint8_t *) "";
102             tmp_info_len = 0U;
103         }
104 
105         n = okm_len / hash_len;
106 
107         if ((okm_len % hash_len) != 0U) {
108             n++;
109         }
110 
111         /*
112          * Per RFC 5869 Section 2.3, okm_len must not exceed
113          * 255 times the hash length
114          */
115         if (n > 255U) {
116             ret = MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
117         } else {
118             mbedtls_md_init(&ctx);
119 
120             ret = mbedtls_md_setup(&ctx, md);
121             if (ret == 0) {
122 
123                 /*
124                  * Compute T = T(1) | T(2) | T(3) | ... | T(N)
125                  * Where T(N) is defined in RFC 5869 Section 2.3
126                  */
127                 for (i = 1U; i <= n; i++) {
128                     size_t num_to_copy;
129                     uint8_t c = (uint8_t)(i & 0xffU);
130 
131                     ret = mbedtls_md_hmac_starts(&ctx, prk, prk_len);
132                     if (ret == 0) {
133                         ret = mbedtls_md_hmac_update(&ctx, t, t_len);
134                     }
135 
136                     if (ret == 0) {
137                         ret = mbedtls_md_hmac_update(&ctx, tmp_info, tmp_info_len);
138                     }
139 
140                     /* The constant concatenated to the end of each T(n) is a single octet.
141                      * */
142                     if (ret == 0) {
143                         ret = mbedtls_md_hmac_update(&ctx, &c, 1);
144                     }
145 
146                     if (ret == 0) {
147                         ret = mbedtls_md_hmac_finish(&ctx, t);
148                     }
149 
150                     if (ret != 0) {
151                         break;
152                     }
153 
154                     num_to_copy = (i != n) ? hash_len : (okm_len - where);
155                     (void)memcpy_s(okm + where, num_to_copy, t, num_to_copy);
156                     where += hash_len;
157                     t_len = hash_len;
158                 }
159             }
160 
161              mbedtls_md_free(&ctx);
162         }
163     }
164 
165     (void)mbedtls_platform_zeroize(t, sizeof(t));
166 
167     return ret;
168 }
169