| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/cal/cal.h> |
| 6 |
|
| 7 |
#include <aws/cal/hkdf.h> |
| 8 |
#include <aws/cal/hmac.h> |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 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; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
size_t num_iterations = (length + hmac_length - 1) / hmac_length; |
| 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 |
|
| 67 |
|
| 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; |
| 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 |
} |