summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/hkdf
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/hkdf')
-rw-r--r--src/lib/libcrypto/hkdf/hkdf.c135
-rw-r--r--src/lib/libcrypto/hkdf/hkdf.h65
2 files changed, 0 insertions, 200 deletions
diff --git a/src/lib/libcrypto/hkdf/hkdf.c b/src/lib/libcrypto/hkdf/hkdf.c
deleted file mode 100644
index 6104ef0cc7..0000000000
--- a/src/lib/libcrypto/hkdf/hkdf.c
+++ /dev/null
@@ -1,135 +0,0 @@
1/* $OpenBSD: hkdf.c,v 1.11 2024/03/25 13:09:13 jsing Exp $ */
2/*
3 * Copyright (c) 2014, Google Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <openssl/hkdf.h>
19
20#include <string.h>
21
22#include <openssl/err.h>
23#include <openssl/hmac.h>
24
25#include "bytestring.h"
26#include "evp_local.h"
27#include "hmac_local.h"
28
29/* https://tools.ietf.org/html/rfc5869#section-2 */
30int
31HKDF(uint8_t *out_key, size_t out_len, const EVP_MD *digest,
32 const uint8_t *secret, size_t secret_len, const uint8_t *salt,
33 size_t salt_len, const uint8_t *info, size_t info_len)
34{
35 uint8_t prk[EVP_MAX_MD_SIZE];
36 size_t prk_len;
37
38 if (!HKDF_extract(prk, &prk_len, digest, secret, secret_len, salt,
39 salt_len))
40 return 0;
41 if (!HKDF_expand(out_key, out_len, digest, prk, prk_len, info,
42 info_len))
43 return 0;
44
45 return 1;
46}
47LCRYPTO_ALIAS(HKDF);
48
49/* https://tools.ietf.org/html/rfc5869#section-2.2 */
50int
51HKDF_extract(uint8_t *out_key, size_t *out_len,
52 const EVP_MD *digest, const uint8_t *secret, size_t secret_len,
53 const uint8_t *salt, size_t salt_len)
54{
55 unsigned int len;
56
57 /*
58 * If salt is not given, HashLength zeros are used. However, HMAC does
59 * that internally already so we can ignore it.
60 */
61 if (HMAC(digest, salt, salt_len, secret, secret_len, out_key, &len) ==
62 NULL) {
63 CRYPTOerror(ERR_R_CRYPTO_LIB);
64 return 0;
65 }
66 *out_len = len;
67 return 1;
68}
69LCRYPTO_ALIAS(HKDF_extract);
70
71/* https://tools.ietf.org/html/rfc5869#section-2.3 */
72int
73HKDF_expand(uint8_t *out_key, size_t out_len,
74 const EVP_MD *digest, const uint8_t *prk, size_t prk_len,
75 const uint8_t *info, size_t info_len)
76{
77 const size_t digest_len = EVP_MD_size(digest);
78 uint8_t out_hmac[EVP_MAX_MD_SIZE];
79 size_t n, remaining;
80 uint8_t ctr;
81 HMAC_CTX *hmac = NULL;
82 CBB cbb;
83 int ret = 0;
84
85 if (!CBB_init_fixed(&cbb, out_key, out_len))
86 goto err;
87
88 if ((hmac = HMAC_CTX_new()) == NULL)
89 goto err;
90 if (!HMAC_Init_ex(hmac, prk, prk_len, digest, NULL))
91 goto err;
92
93 remaining = out_len;
94 ctr = 0;
95
96 /* Expand key material to desired length. */
97 while (remaining > 0) {
98 if (++ctr == 0) {
99 CRYPTOerror(EVP_R_TOO_LARGE);
100 goto err;
101 }
102
103 if (!HMAC_Update(hmac, info, info_len))
104 goto err;
105 if (!HMAC_Update(hmac, &ctr, 1))
106 goto err;
107 if (!HMAC_Final(hmac, out_hmac, NULL))
108 goto err;
109
110 if ((n = remaining) > digest_len)
111 n = digest_len;
112
113 if (!CBB_add_bytes(&cbb, out_hmac, n))
114 goto err;
115
116 remaining -= n;
117
118 if (remaining > 0) {
119 if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL))
120 goto err;
121 if (!HMAC_Update(hmac, out_hmac, digest_len))
122 goto err;
123 }
124 }
125
126 ret = 1;
127
128 err:
129 CBB_cleanup(&cbb);
130 HMAC_CTX_free(hmac);
131 explicit_bzero(out_hmac, sizeof(out_hmac));
132
133 return ret;
134}
135LCRYPTO_ALIAS(HKDF_expand);
diff --git a/src/lib/libcrypto/hkdf/hkdf.h b/src/lib/libcrypto/hkdf/hkdf.h
deleted file mode 100644
index 6cec526e3e..0000000000
--- a/src/lib/libcrypto/hkdf/hkdf.h
+++ /dev/null
@@ -1,65 +0,0 @@
1/* $OpenBSD: hkdf.h,v 1.3 2023/08/11 04:52:08 tb Exp $ */
2/* Copyright (c) 2014, Google Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
15
16#ifndef OPENSSL_HEADER_HKDF_H
17#define OPENSSL_HEADER_HKDF_H
18
19#include <openssl/evp.h>
20
21#if defined(__cplusplus)
22extern "C" {
23#endif
24
25/*
26 * HKDF computes HKDF (as specified by RFC 5869) of initial keying
27 * material |secret| with |salt| and |info| using |digest|, and
28 * outputs |out_len| bytes to |out_key|. It returns one on success and
29 * zero on error.
30 *
31 * HKDF is an Extract-and-Expand algorithm. It does not do any key
32 * stretching, and as such, is not suited to be used alone to generate
33 * a key from a password.
34 */
35
36int HKDF(uint8_t *out_key, size_t out_len, const EVP_MD *digest,
37 const uint8_t *secret, size_t secret_len, const uint8_t *salt,
38 size_t salt_len, const uint8_t *info, size_t info_len);
39
40/*
41 * HKDF_extract computes a HKDF PRK (as specified by RFC 5869) from
42 * initial keying material |secret| and salt |salt| using |digest|,
43 * and outputs |out_len| bytes to |out_key|. The maximum output size
44 * is |EVP_MAX_MD_SIZE|. It returns one on success and zero on error.
45 */
46int HKDF_extract(uint8_t *out_key, size_t *out_len, const EVP_MD *digest,
47 const uint8_t *secret, size_t secret_len,
48 const uint8_t *salt, size_t salt_len);
49
50/*
51 * HKDF_expand computes a HKDF OKM (as specified by RFC 5869) of
52 * length |out_len| from the PRK |prk| and info |info| using |digest|,
53 * and outputs the result to |out_key|. It returns one on success and
54 * zero on error.
55 */
56int HKDF_expand(uint8_t *out_key, size_t out_len,
57 const EVP_MD *digest, const uint8_t *prk, size_t prk_len,
58 const uint8_t *info, size_t info_len);
59
60
61#if defined(__cplusplus)
62} /* extern C */
63#endif
64
65#endif /* OPENSSL_HEADER_HKDF_H */