1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
#include "hkdf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "axolotl_internal.h"
#define HASH_OUTPUT_SIZE 32
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
struct hkdf_context
{
axolotl_type_base base;
axolotl_context *global_context;
int iteration_start_offset;
};
int hkdf_create(hkdf_context **context, int message_version, axolotl_context *global_context)
{
assert(global_context);
*context = malloc(sizeof(hkdf_context));
if(!(*context)) {
return AX_ERR_NOMEM;
}
memset(*context, 0, sizeof(hkdf_context));
AXOLOTL_INIT(*context, hkdf_destroy);
(*context)->global_context = global_context;
if(message_version == 2) {
(*context)->iteration_start_offset = 0;
}
else if(message_version == 3) {
(*context)->iteration_start_offset = 1;
}
else {
free(*context);
return AX_ERR_INVAL;
}
return 0;
}
ssize_t hkdf_extract(hkdf_context *context,
uint8_t **output,
const uint8_t *salt, size_t salt_len,
const uint8_t *input_key_material, size_t input_key_material_len)
{
int result = 0;
axolotl_buffer *mac_buffer = 0;
uint8_t *mac = 0;
size_t mac_len = 0;
void *hmac_context;
assert(context);
result = axolotl_hmac_sha256_init(context->global_context,
&hmac_context, salt, salt_len);
if(result < 0) {
goto complete;
}
result = axolotl_hmac_sha256_update(context->global_context,
hmac_context, input_key_material, input_key_material_len);
if(result < 0) {
goto complete;
}
result = axolotl_hmac_sha256_final(context->global_context,
hmac_context, &mac_buffer);
if(result < 0) {
goto complete;
}
mac_len = axolotl_buffer_len(mac_buffer);
mac = malloc(mac_len);
if(!mac) {
result = AX_ERR_NOMEM;
goto complete;
}
memcpy(mac, axolotl_buffer_data(mac_buffer), mac_len);
complete:
axolotl_hmac_sha256_cleanup(context->global_context, hmac_context);
axolotl_buffer_free(mac_buffer);
if(result >= 0) {
*output = mac;
return (ssize_t)mac_len;
}
else {
return result;
}
}
ssize_t hkdf_expand(hkdf_context *context,
uint8_t **output,
const uint8_t *prk, size_t prk_len,
const uint8_t *info, size_t info_len,
size_t output_len)
{
int iterations = (int)ceil((double)output_len / (double)HASH_OUTPUT_SIZE);
size_t remaining_len = output_len;
axolotl_buffer *step_buffer = 0;
size_t step_size = 0;
uint8_t *result_buf = 0;
size_t result_buf_len = 0;
void *hmac_context = 0;
int result = 0;
uint8_t i;
assert(context);
for(i = context->iteration_start_offset; i < iterations + context->iteration_start_offset; i++) {
result = axolotl_hmac_sha256_init(context->global_context,
&hmac_context, prk, prk_len);
if(result < 0) {
goto complete;
}
if(step_buffer) {
result = axolotl_hmac_sha256_update(context->global_context,
hmac_context,
axolotl_buffer_data(step_buffer),
axolotl_buffer_len(step_buffer));
if(result < 0) {
goto complete;
}
axolotl_buffer_free(step_buffer);
step_buffer = 0;
}
if(info) {
result = axolotl_hmac_sha256_update(context->global_context,
hmac_context, info, info_len);
if(result < 0) {
goto complete;
}
}
result = axolotl_hmac_sha256_update(context->global_context,
hmac_context, &i, sizeof(uint8_t));
if(result < 0) {
goto complete;
}
result = axolotl_hmac_sha256_final(context->global_context,
hmac_context, &step_buffer);
if(result < 0) {
goto complete;
}
axolotl_hmac_sha256_cleanup(context->global_context, hmac_context);
hmac_context = 0;
step_size = MIN(remaining_len, axolotl_buffer_len(step_buffer));
if(!result_buf) {
result_buf = malloc(step_size);
if(!result_buf) {
result = AX_ERR_NOMEM;
goto complete;
}
memcpy(result_buf, axolotl_buffer_data(step_buffer), step_size);
result_buf_len = step_size;
}
else {
uint8_t *tmp_buf = realloc(result_buf, result_buf_len + step_size);
if(!tmp_buf) {
result = AX_ERR_NOMEM;
goto complete;
}
result_buf = tmp_buf;
memcpy(result_buf + result_buf_len, axolotl_buffer_data(step_buffer), step_size);
result_buf_len += step_size;
}
remaining_len -= step_size;
}
complete:
if(hmac_context) {
axolotl_hmac_sha256_cleanup(context->global_context, hmac_context);
}
axolotl_buffer_free(step_buffer);
if(result < 0) {
free(result_buf);
return result;
}
else {
*output = result_buf;
return (ssize_t)result_buf_len;
}
}
ssize_t hkdf_derive_secrets(hkdf_context *context,
uint8_t **output,
const uint8_t *input_key_material, size_t input_key_material_len,
const uint8_t *salt, size_t salt_len,
const uint8_t *info, size_t info_len,
size_t output_len)
{
ssize_t result = 0;
uint8_t *prk = 0;
ssize_t prk_len = 0;
assert(context);
prk_len = hkdf_extract(context, &prk, salt, salt_len, input_key_material, input_key_material_len);
if(prk_len < 0) {
axolotl_log(context->global_context, AX_LOG_ERROR, "hkdf_extract error: %d", prk_len);
return prk_len;
}
result = hkdf_expand(context, output, prk, (size_t)prk_len, info, info_len, output_len);
if(prk) {
free(prk);
}
return result;
}
int hkdf_compare(const hkdf_context *context1, const hkdf_context *context2)
{
if(context1 == context2) {
return 0;
}
else if(context1 == 0 && context2 != 0) {
return -1;
}
else if(context1 != 0 && context2 == 0) {
return 1;
}
else if(context1->iteration_start_offset < context2->iteration_start_offset) {
return -1;
}
else if(context1->iteration_start_offset > context2->iteration_start_offset) {
return 1;
}
else {
return 0;
}
}
void hkdf_destroy(axolotl_type_base *type)
{
hkdf_context *context = (hkdf_context *)type;
free(context);
}
|