Debian Codegraph @

crt/aws-c-cal/source/shared/ref_hkdf.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/cal/cal.h>
6
7 #include <aws/cal/hkdf.h>
8 #include <aws/cal/hmac.h>
9
10 /*
11 * Note: mac only provides hkdf starting in cryptokit (swift only)
12 * and windows added convoluted api to generate hkdf starting with win 10+.
13 * in rfc5869.
14 */
15
16 enum { MAX_HMAC_SIZE = 64 };
17
18 static int s_hkdf_extract(
19 struct aws_allocator *allocator,
20 enum aws_hkdf_hmac_type hmac_type,
21 struct aws_byte_cursor ikm,
22 struct aws_byte_cursor salt,
23 struct aws_byte_buf *out_prk_buf) {
24 (void)hmac_type;
25 static size_t zero_salt[64] = {0};
26
27 if (salt.len == 0) {
28 salt = aws_byte_cursor_from_array(zero_salt, 64);
29 }
30
31 /* PRK = HMAC-Hash(salt, IKM) */
32 return aws_sha512_hmac_compute(allocator, &salt, &ikm, out_prk_buf, 0);
33 }
34
35 static int s_hkdf_expand(
36 struct aws_allocator *allocator,
37 enum aws_hkdf_hmac_type hmac_type,
38 struct aws_byte_cursor prk,
39 struct aws_byte_cursor info,
40 struct aws_byte_buf *out_okm_buf,
41 size_t length) {
42 (void)hmac_type;
43 static size_t hmac_length = 64; /* sha512 hmac */
44
45 /*
46 * Follows rfc implementation.
47 * N = ceil(L/HashLen)
48 * T = T(1) | T(2) | T(3) | ... | T(N) (i.e. concat of successive T(x))
49 * OKM = first L octets of T
50 * where:
51 * T(0) = empty string (zero length)
52 * T(1) = HMAC-Hash(PRK, T(0) | info | 0x01)
53 * T(2) = HMAC-Hash(PRK, T(1) | info | 0x02)
54 * T(3) = HMAC-Hash(PRK, T(2) | info | 0x03)
55 * ...
56 * Note: each T hmac has a single byte of counter appended to data, which increases on every iteration.
57 */
58
59 size_t num_iterations = (length + hmac_length - 1) / hmac_length; /* round up */
60
61 if (num_iterations > 255) {
62 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
63 }
64
65 struct aws_byte_buf ret_buf;
66 /* the approach is to append successive hmacs until we get over required length and then truncate.
67 * so overallocate here */
68 aws_byte_buf_init(&ret_buf, allocator, length + hmac_length);
69
70 struct aws_byte_cursor prev_cur = {0};
71
72 struct aws_hmac *hmac = NULL;
73
74 for (uint8_t counter = 1; counter <= num_iterations; ++counter) {
75
76 hmac = aws_sha512_hmac_new(allocator, &prk);
77 if (!hmac) {
78 goto on_error;
79 }
80
81 if (aws_hmac_update(hmac, &prev_cur)) {
82 goto on_error;
83 }
84
85 if (info.len > 0) {
86 if (aws_hmac_update(hmac, &info)) {
87 goto on_error;
88 }
89 }
90
91 struct aws_byte_cursor counter_cur = {
92 .ptr = &counter,
93 .len = 1,
94 };
95
96 if (aws_hmac_update(hmac, &counter_cur)) {
97 goto on_error;
98 }
99
100 if (aws_hmac_finalize(hmac, &ret_buf, 0)) {
101 goto on_error;
102 }
103
104 prev_cur = (struct aws_byte_cursor){
105 .ptr = ret_buf.buffer + (counter - 1) * hmac_length,
106 .len = hmac_length,
107 };
108 aws_hmac_destroy(hmac);
109 hmac = NULL;
110 }
111
112 struct aws_byte_cursor ret_cur = aws_byte_cursor_from_buf(&ret_buf);
113 ret_cur.len = length; /* truncate to required length */
114
115 if (aws_byte_buf_append(out_okm_buf, &ret_cur)) {
116 goto on_error;
117 }
118
119 aws_byte_buf_clean_up(&ret_buf);
120 return AWS_OP_SUCCESS;
121
122 on_error:
123 aws_byte_buf_clean_up(&ret_buf);
124 aws_hmac_destroy(hmac);
125 return AWS_OP_ERR;
126 }
127
128 int aws_hkdf_derive_impl(
129 struct aws_allocator *allocator,
130 enum aws_hkdf_hmac_type hmac_type,
131 struct aws_byte_cursor ikm,
132 struct aws_byte_cursor salt,
133 struct aws_byte_cursor info,
134 struct aws_byte_buf *out_buf,
135 size_t length) {
136
137 AWS_PRECONDITION(hmac_type == HKDF_HMAC_SHA512);
138
139 uint8_t prk[MAX_HMAC_SIZE] = {0};
140 struct aws_byte_buf prk_buf = aws_byte_buf_from_empty_array(prk, MAX_HMAC_SIZE);
141
142 if (s_hkdf_extract(allocator, hmac_type, ikm, salt, &prk_buf)) {
143 return AWS_OP_ERR;
144 }
145
146 struct aws_byte_cursor prk_cur = aws_byte_cursor_from_buf(&prk_buf);
147
148 return s_hkdf_expand(allocator, hmac_type, prk_cur, info, out_buf, length);
149 }