Debian Codegraph @

external/boringssl/crypto/hkdf/hkdf.c

raw
1 /* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <openssl/hkdf.h>
16
17 #include <assert.h>
18 #include <string.h>
19
20 #include <openssl/err.h>
21 #include <openssl/hmac.h>
22
23
24 int HKDF(uint8_t *out_key, size_t out_len, const EVP_MD *digest,
25 const uint8_t *secret, size_t secret_len, const uint8_t *salt,
26 size_t salt_len, const uint8_t *info, size_t info_len) {
27 /* https://tools.ietf.org/html/rfc5869#section-2 */
28 uint8_t prk[EVP_MAX_MD_SIZE];
29 size_t prk_len;
30
31 if (!HKDF_extract(prk, &prk_len, digest, secret, secret_len, salt,
32 salt_len) ||
33 !HKDF_expand(out_key, out_len, digest, prk, prk_len, info, info_len)) {
34 return 0;
35 }
36
37 return 1;
38 }
39
40 int HKDF_extract(uint8_t *out_key, size_t *out_len, const EVP_MD *digest,
41 const uint8_t *secret, size_t secret_len, const uint8_t *salt,
42 size_t salt_len) {
43 /* https://tools.ietf.org/html/rfc5869#section-2.2 */
44
45 /* If salt is not given, HashLength zeros are used. However, HMAC does that
46 * internally already so we can ignore it.*/
47 unsigned len;
48 if (HMAC(digest, salt, salt_len, secret, secret_len, out_key, &len) == NULL) {
49 OPENSSL_PUT_ERROR(HKDF, ERR_R_HMAC_LIB);
50 return 0;
51 }
52 *out_len = len;
53 assert(*out_len == EVP_MD_size(digest));
54 return 1;
55 }
56
57 int HKDF_expand(uint8_t *out_key, size_t out_len, const EVP_MD *digest,
58 uint8_t *prk, size_t prk_len, const uint8_t *info,
59 size_t info_len) {
60 /* https://tools.ietf.org/html/rfc5869#section-2.3 */
61 const size_t digest_len = EVP_MD_size(digest);
62 uint8_t previous[EVP_MAX_MD_SIZE];
63 size_t n, done = 0;
64 unsigned i;
65 int ret = 0;
66 HMAC_CTX hmac;
67
68 /* Expand key material to desired length. */
69 n = (out_len + digest_len - 1) / digest_len;
70 if (out_len + digest_len < out_len || n > 255) {
71 OPENSSL_PUT_ERROR(HKDF, HKDF_R_OUTPUT_TOO_LARGE);
72 return 0;
73 }
74
75 HMAC_CTX_init(&hmac);
76 if (!HMAC_Init_ex(&hmac, prk, prk_len, digest, NULL)) {
77 goto out;
78 }
79
80 for (i = 0; i < n; i++) {
81 uint8_t ctr = i + 1;
82 size_t todo;
83
84 if (i != 0 && (!HMAC_Init_ex(&hmac, NULL, 0, NULL, NULL) ||
85 !HMAC_Update(&hmac, previous, digest_len))) {
86 goto out;
87 }
88 if (!HMAC_Update(&hmac, info, info_len) ||
89 !HMAC_Update(&hmac, &ctr, 1) ||
90 !HMAC_Final(&hmac, previous, NULL)) {
91 goto out;
92 }
93
94 todo = digest_len;
95 if (done + todo > out_len) {
96 todo = out_len - done;
97 }
98 memcpy(out_key + done, previous, todo);
99 done += todo;
100 }
101
102 ret = 1;
103
104 out:
105 HMAC_CTX_cleanup(&hmac);
106 if (ret != 1) {
107 OPENSSL_PUT_ERROR(HKDF, ERR_R_HMAC_LIB);
108 }
109 return ret;
110 }