forked from openenclave/openenclave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdf.c
194 lines (157 loc) · 5.12 KB
/
kdf.c
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
// Copyright (c) Open Enclave SDK contributors.
// Licensed under the MIT License.
#include <openenclave/bits/result.h>
#include <openenclave/bits/types.h>
#include <openenclave/internal/crypto/hmac.h>
#include <openenclave/internal/crypto/sha.h>
#include <openenclave/internal/kdf.h>
#include <openenclave/internal/raise.h>
#include <openenclave/internal/safecrt.h>
#include <openenclave/internal/safemath.h>
#include <openenclave/internal/utils.h>
#include "common.h"
static inline size_t _min(size_t x, size_t y)
{
return (x < y) ? x : y;
}
static inline uint32_t _to_big_endian(uint32_t x)
{
return ((x & 0xFF000000U) >> 24) | ((x & 0x00FF0000U) >> 8) |
((x & 0x0000FF00U) << 8) | ((x & 0x000000FFU) << 24);
}
static oe_result_t kdf_hmac_sha256_ctr(
const uint8_t* key,
size_t key_size,
const uint8_t* fixed_data,
size_t fixed_data_size,
uint8_t* derived_key,
size_t derived_key_size)
{
oe_result_t result = OE_UNEXPECTED;
size_t derived_key_size_rounded;
oe_hmac_sha256_context_t ctx;
OE_SHA256 sha256;
size_t iters;
uint32_t ctr;
if (!key || !derived_key)
OE_RAISE(OE_INVALID_PARAMETER);
/*
* Algorithm specified in NIST SP800-108. Essentially, for
* i = 1 to CEIL(derived_key_size / hash_length), calculate
* HMAC-SHA256(key, i || fixed_data) and concatenate the results.
*/
OE_CHECK(oe_safe_add_sizet(
derived_key_size, OE_SHA256_SIZE - 1, &derived_key_size_rounded));
iters = derived_key_size_rounded / OE_SHA256_SIZE;
/* NIST specifies that the counter must be at most 2^32 - 1. */
if (iters > OE_UINT32_MAX)
OE_RAISE(OE_CONSTRAINT_FAILED);
for (size_t i = 1; i <= iters; i++)
{
size_t bytes_to_copy = _min(derived_key_size, OE_SHA256_SIZE);
/* Counter must be in big endian. Assume we're little endian. */
ctr = _to_big_endian((uint32_t)i);
OE_CHECK(oe_hmac_sha256_init(&ctx, key, key_size));
OE_CHECK(oe_hmac_sha256_update(&ctx, (uint8_t*)&ctr, sizeof(ctr)));
if (fixed_data)
OE_CHECK(oe_hmac_sha256_update(&ctx, fixed_data, fixed_data_size));
OE_CHECK(oe_hmac_sha256_final(&ctx, &sha256));
OE_CHECK(oe_hmac_sha256_free(&ctx));
OE_CHECK(
oe_memcpy_s(derived_key, bytes_to_copy, sha256.buf, bytes_to_copy));
derived_key += bytes_to_copy;
derived_key_size -= bytes_to_copy;
}
result = OE_OK;
done:
oe_secure_zero_fill(sha256.buf, sizeof(sha256.buf));
return result;
}
oe_result_t oe_kdf_create_fixed_data(
const uint8_t* label,
size_t label_size,
const uint8_t* context,
size_t context_size,
size_t output_key_size,
uint8_t** fixed_data,
size_t* fixed_data_size)
{
oe_result_t result = OE_UNEXPECTED;
uint8_t* data = NULL;
size_t data_size = 0;
uint8_t* data_cur = NULL;
size_t data_size_cur = 0;
uint32_t key_bits;
if (!fixed_data || !fixed_data_size)
OE_RAISE(OE_INVALID_PARAMETER);
/* The output key length is at most 2^32 - 1 in bits. */
if (output_key_size > OE_UINT32_MAX / 8)
OE_RAISE(OE_CONSTRAINT_FAILED);
if (label == NULL)
label_size = 0;
if (context == NULL)
context_size = 0;
/* The fixed data is label || 0x00 || Context || key_bits */
OE_CHECK(oe_safe_add_sizet(label_size, context_size, &data_size));
/* The +5 is 1 byte for 0x00 and 4 bytes for key_bits. */
OE_CHECK(oe_safe_add_sizet(data_size, 5, &data_size));
data = (uint8_t*)oe_malloc(data_size);
if (data == NULL)
OE_RAISE(OE_OUT_OF_MEMORY);
data_cur = data;
data_size_cur = data_size;
/* Copy label if it exists. */
if (label)
OE_CHECK(oe_memcpy_s(data_cur, data_size_cur, label, label_size));
data_cur += label_size;
data_size_cur -= label_size;
/* Copy 0x00 byte. */
*data_cur++ = 0;
data_size_cur--;
/* Copy context if it exists. */
if (context)
OE_CHECK(oe_memcpy_s(data_cur, data_size_cur, context, context_size));
data_cur += context_size;
data_size_cur -= context_size;
/* Copy output_key_size_in_bits. */
key_bits = _to_big_endian((uint32_t)(8 * output_key_size));
OE_CHECK(oe_memcpy_s(data_cur, data_size_cur, &key_bits, 4));
*fixed_data = data;
*fixed_data_size = data_size;
data = NULL;
result = OE_OK;
done:
if (data != NULL)
oe_free(data);
return result;
}
oe_result_t oe_kdf_derive_key(
oe_kdf_mode_t mode,
const uint8_t* key,
size_t key_size,
const uint8_t* fixed_data,
size_t fixed_data_size,
uint8_t* derived_key,
size_t derived_key_size)
{
oe_result_t result = OE_UNEXPECTED;
if (!key || !derived_key)
OE_RAISE(OE_INVALID_PARAMETER);
switch (mode)
{
case OE_KDF_HMAC_SHA256_CTR:
OE_CHECK(kdf_hmac_sha256_ctr(
key,
key_size,
fixed_data,
fixed_data_size,
derived_key,
derived_key_size));
break;
default:
OE_RAISE(OE_INVALID_PARAMETER);
}
result = OE_OK;
done:
return result;
}