NCBI C++ ToolKit
hkdf.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /*
2  * HKDF implementation -- RFC 5869
3  *
4  * Copyright The Mbed TLS Contributors
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 #include "common.h"
20 
21 #if defined(MBEDTLS_HKDF_C)
22 
23 #include <string.h>
24 #include "mbedtls/hkdf.h"
25 #include "mbedtls/platform_util.h"
26 #include "mbedtls/error.h"
27 
28 int mbedtls_hkdf(const mbedtls_md_info_t *md, const unsigned char *salt,
29  size_t salt_len, const unsigned char *ikm, size_t ikm_len,
30  const unsigned char *info, size_t info_len,
31  unsigned char *okm, size_t okm_len)
32 {
34  unsigned char prk[MBEDTLS_MD_MAX_SIZE];
35 
36  ret = mbedtls_hkdf_extract(md, salt, salt_len, ikm, ikm_len, prk);
37 
38  if (ret == 0) {
40  info, info_len, okm, okm_len);
41  }
42 
43  mbedtls_platform_zeroize(prk, sizeof(prk));
44 
45  return ret;
46 }
47 
49  const unsigned char *salt, size_t salt_len,
50  const unsigned char *ikm, size_t ikm_len,
51  unsigned char *prk)
52 {
53  unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
54 
55  if (salt == NULL) {
56  size_t hash_len;
57 
58  if (salt_len != 0) {
60  }
61 
63 
64  if (hash_len == 0) {
66  }
67 
68  salt = null_salt;
69  salt_len = hash_len;
70  }
71 
72  return mbedtls_md_hmac(md, salt, salt_len, ikm, ikm_len, prk);
73 }
74 
75 int mbedtls_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk,
76  size_t prk_len, const unsigned char *info,
77  size_t info_len, unsigned char *okm, size_t okm_len)
78 {
79  size_t hash_len;
80  size_t where = 0;
81  size_t n;
82  size_t t_len = 0;
83  size_t i;
84  int ret = 0;
86  unsigned char t[MBEDTLS_MD_MAX_SIZE];
87 
88  if (okm == NULL) {
90  }
91 
93 
94  if (prk_len < hash_len || hash_len == 0) {
96  }
97 
98  if (info == NULL) {
99  info = (const unsigned char *) "";
100  info_len = 0;
101  }
102 
103  n = okm_len / hash_len;
104 
105  if (okm_len % hash_len != 0) {
106  n++;
107  }
108 
109  /*
110  * Per RFC 5869 Section 2.3, okm_len must not exceed
111  * 255 times the hash length
112  */
113  if (n > 255) {
115  }
116 
118 
119  if ((ret = mbedtls_md_setup(&ctx, md, 1)) != 0) {
120  goto exit;
121  }
122 
123  memset(t, 0, hash_len);
124 
125  /*
126  * Compute T = T(1) | T(2) | T(3) | ... | T(N)
127  * Where T(N) is defined in RFC 5869 Section 2.3
128  */
129  for (i = 1; i <= n; i++) {
130  size_t num_to_copy;
131  unsigned char c = i & 0xff;
132 
133  ret = mbedtls_md_hmac_starts(&ctx, prk, prk_len);
134  if (ret != 0) {
135  goto exit;
136  }
137 
138  ret = mbedtls_md_hmac_update(&ctx, t, t_len);
139  if (ret != 0) {
140  goto exit;
141  }
142 
143  ret = mbedtls_md_hmac_update(&ctx, info, info_len);
144  if (ret != 0) {
145  goto exit;
146  }
147 
148  /* The constant concatenated to the end of each T(n) is a single octet.
149  * */
150  ret = mbedtls_md_hmac_update(&ctx, &c, 1);
151  if (ret != 0) {
152  goto exit;
153  }
154 
155  ret = mbedtls_md_hmac_finish(&ctx, t);
156  if (ret != 0) {
157  goto exit;
158  }
159 
160  num_to_copy = i != n ? hash_len : okm_len - where;
161  memcpy(okm + where, t, num_to_copy);
162  where += hash_len;
163  t_len = hash_len;
164  }
165 
166 exit:
168  mbedtls_platform_zeroize(t, sizeof(t));
169 
170  return ret;
171 }
172 
173 #endif /* MBEDTLS_HKDF_C */
#define md
Definition: compat-1.3.h:2001
CS_CONTEXT * ctx
Definition: t0006.c:12
#define NULL
Definition: ncbistd.hpp:225
This file contains the HKDF interface.
#define MBEDTLS_ERR_HKDF_BAD_INPUT_DATA
Bad input parameters to function.
Definition: hkdf.h:41
int mbedtls_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk, size_t prk_len, const unsigned char *info, size_t info_len, unsigned char *okm, size_t okm_len)
Expand the supplied prk into several additional pseudorandom keys, which is the output of the HKDF.
int mbedtls_hkdf_extract(const mbedtls_md_info_t *md, const unsigned char *salt, size_t salt_len, const unsigned char *ikm, size_t ikm_len, unsigned char *prk)
Take the input keying material ikm and extract from it a fixed-length pseudorandom key prk.
int mbedtls_hkdf(const mbedtls_md_info_t *md, const unsigned char *salt, size_t salt_len, const unsigned char *ikm, size_t ikm_len, const unsigned char *info, size_t info_len, unsigned char *okm, size_t okm_len)
This is the HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
exit(2)
int i
yy_size_t n
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char *output)
This function calculates the full generic HMAC on the input buffer with the provided key.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output)
This function finishes the HMAC operation, and writes the result to the output buffer.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing HMAC computation.
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac)
This function selects the message digest algorithm to use, and allocates internal structures.
#define MBEDTLS_MD_MAX_SIZE
Definition: md.h:78
void mbedtls_md_init(mbedtls_md_context_t *ctx)
This function initializes a message-digest context without binding it to a particular message-digest ...
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
This function extracts the message-digest size from the message-digest information structure.
void mbedtls_md_free(mbedtls_md_context_t *ctx)
This function clears the internal structure of ctx and frees any embedded internal structure,...
MBEDTLS_CHECK_RETURN_TYPICAL int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen)
This function sets the HMAC key and prepares to authenticate a new message.
static MDB_envinfo info
Definition: mdb_load.c:37
EIPRangeType t
Definition: ncbi_localip.c:101
Common and shared functions used by multiple modules in the Mbed TLS library.
void mbedtls_platform_zeroize(void *buf, size_t len)
Securely zeroize a buffer.
Error to string translation.
#define MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
This is a bug in the library.
Definition: error.h:122
The generic message-digest context.
Definition: md.h:95
Message digest information.
Definition: md_internal.h:45
Modified on Sat Dec 02 09:22:26 2023 by modify_doxy.py rev. 669887