summaryrefslogtreecommitdiffstats
path: root/xlators/encryption/crypt/src/keys.c
blob: e9da55960c8c61eceae5b5896089ec8f0a7c23c2 (plain)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
  Copyright (c) 2008-2013 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include "defaults.h"
#include "crypt-common.h"
#include "crypt.h"

/* Key hierarchy

                   +----------------+
                   | MASTER_VOL_KEY |
                   +-------+--------+
                           |
                           |
          +----------------+----------------+
          |                |                |
          |                |                |
  +-------+------+ +-------+-------+ +------+--------+
  | NMTD_VOL_KEY | | EMTD_FILE_KEY | | DATA_FILE_KEY |
  +-------+------+ +---------------+ +---------------+
          |
          |
  +-------+-------+
  | NMTD_LINK_KEY |
  +---------------+

 */

#if DEBUG_CRYPT
static void check_prf_iters(uint32_t num_iters)
{
	if (num_iters == 0)
		gf_log ("crypt", GF_LOG_DEBUG,
			"bad number of prf iterations : %d", num_iters);
}
#else
#define check_prf_iters(num_iters) noop
#endif /* DEBUG_CRYPT */

unsigned char crypt_fake_oid[16] =
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

/*
 * derive key in the counter mode using
 * sha256-based HMAC as PRF, see
 * NIST Special Publication 800-108, 5.1)
 */

#define PRF_OUTPUT_SIZE  SHA256_DIGEST_LENGTH

static int32_t kderive_init(struct kderive_context *ctx,
			    const unsigned char *pkey, /* parent key */
			    uint32_t pkey_size,  /* parent key size */
			    const unsigned char *idctx, /* id-context */
			    uint32_t idctx_size,
			    crypt_key_type type  /* type of child key */)
{
	unsigned char *pos;
	uint32_t llen = strlen(crypt_keys[type].label);
	/*
	 * Compoud the fixed input data for KDF:
	 * [i]_2 || Label || 0x00 || Id-Context || [L]_2),
	 * NIST SP 800-108, 5.1
	 */
	ctx->fid_len =
		sizeof(uint32_t) +
		llen +
		1 +
		idctx_size +
		sizeof(uint32_t);

	ctx->fid = GF_CALLOC(ctx->fid_len, 1, gf_crypt_mt_key);
	if (!ctx->fid)
		return ENOMEM;
	ctx->out_len = round_up(crypt_keys[type].len >> 3,
				PRF_OUTPUT_SIZE);
	ctx->out = GF_CALLOC(ctx->out_len, 1, gf_crypt_mt_key);
	if (!ctx->out) {
		GF_FREE(ctx->fid);
		return ENOMEM;
	}
	ctx->pkey = pkey;
	ctx->pkey_len = pkey_size;
	ctx->ckey_len = crypt_keys[type].len;

	pos = ctx->fid;

	/* counter will be set up in kderive_rfn() */
	pos += sizeof(uint32_t);

	memcpy(pos, crypt_keys[type].label, llen);
	pos += llen;

	/* set up zero octet */
	*pos = 0;
	pos += 1;

	memcpy(pos, idctx, idctx_size);
	pos += idctx_size;

	*((uint32_t *)pos) = htobe32(ctx->ckey_len);

	return 0;
}

static void kderive_update(struct kderive_context *ctx)
{
	uint32_t i;
#if (OPENSSL_VERSION_NUMBER < 0x1010002f)
	HMAC_CTX hctx;
#endif
        HMAC_CTX *phctx = NULL;
	unsigned char *pos = ctx->out;
	uint32_t *p_iter = (uint32_t *)ctx->fid;
	uint32_t num_iters = ctx->out_len / PRF_OUTPUT_SIZE;

	check_prf_iters(num_iters);

#if (OPENSSL_VERSION_NUMBER < 0x1010002f)
	HMAC_CTX_init(&hctx);
        phctx = &hctx;
#else
        phctx = HMAC_CTX_new();
        /* I guess we presume it was successful? */
#endif
	for (i = 0; i < num_iters; i++) {
		/*
		 * update the iteration number in the fid
		 */
		*p_iter = htobe32(i);
		HMAC_Init_ex(phctx,
			     ctx->pkey, ctx->pkey_len >> 3,
			     EVP_sha256(),
			     NULL);
		HMAC_Update(phctx, ctx->fid, ctx->fid_len);
		HMAC_Final(phctx, pos, NULL);

		pos += PRF_OUTPUT_SIZE;
	}
#if (OPENSSL_VERSION_NUMBER < 0x1010002f)
	HMAC_CTX_cleanup(phctx);
#else
        HMAC_CTX_free(phctx);
#endif
}

static void kderive_final(struct kderive_context *ctx, unsigned char *child)
{
	memcpy(child, ctx->out, ctx->ckey_len >> 3);
	GF_FREE(ctx->fid);
	GF_FREE(ctx->out);
	memset(ctx, 0, sizeof(*ctx));
}

/*
 * derive per-volume key for object ids aithentication
 */
int32_t get_nmtd_vol_key(struct master_cipher_info *master)
{
	int32_t ret;
	struct kderive_context ctx;

	ret = kderive_init(&ctx,
			   master->m_key,
			   master_key_size(),
			   crypt_fake_oid, sizeof(uuid_t), NMTD_VOL_KEY);
	if (ret)
		return ret;
	kderive_update(&ctx);
	kderive_final(&ctx, master->m_nmtd_key);
	return 0;
}

/*
 * derive per-link key for aithentication of non-encrypted
 * meta-data (nmtd)
 */
int32_t get_nmtd_link_key(loc_t *loc,
			  struct master_cipher_info *master,
			  unsigned char *result)
{
	int32_t ret;
	struct kderive_context ctx;

	ret = kderive_init(&ctx,
			   master->m_nmtd_key,
			   nmtd_vol_key_size(),
			   (const unsigned char *)loc->path,
			   strlen(loc->path), NMTD_LINK_KEY);
	if (ret)
		return ret;
	kderive_update(&ctx);
	kderive_final(&ctx, result);
	return 0;
}

/*
 * derive per-file key for encryption and authentication
 * of encrypted part of metadata (emtd)
 */
int32_t get_emtd_file_key(struct crypt_inode_info *info,
			  struct master_cipher_info *master,
			  unsigned char *result)
{
	int32_t ret;
	struct kderive_context ctx;

	ret = kderive_init(&ctx,
			   master->m_key,
			   master_key_size(),
			   info->oid, sizeof(uuid_t), EMTD_FILE_KEY);
	if (ret)
		return ret;
	kderive_update(&ctx);
	kderive_final(&ctx, result);
	return 0;
}

static int32_t data_key_type_by_size(uint32_t keysize, crypt_key_type *type)
{
	int32_t ret = 0;
	switch (keysize) {
	case 256:
		*type = DATA_FILE_KEY_256;
		break;
	case 512:
		*type = DATA_FILE_KEY_512;
		break;
	default:
		gf_log("crypt", GF_LOG_ERROR, "Unsupported data key size %d",
		       keysize);
		ret = ENOTSUP;
		break;
	}
	return ret;
}

/*
 * derive per-file key for data encryption
 */
int32_t get_data_file_key(struct crypt_inode_info *info,
			  struct master_cipher_info *master,
			  uint32_t keysize,
			  unsigned char *key)
{
	int32_t ret;
	struct kderive_context ctx;
	crypt_key_type type;

	ret = data_key_type_by_size(keysize, &type);
	if (ret)
		return ret;
	ret = kderive_init(&ctx,
			   master->m_key,
			   master_key_size(),
			   info->oid, sizeof(uuid_t), type);
	if (ret)
		return ret;
	kderive_update(&ctx);
	kderive_final(&ctx, key);
	return 0;
}

/*
 * NOTE: Don't change existing keys: it will break compatibility;
 */
struct crypt_key crypt_keys[LAST_KEY_TYPE] = {
	[MASTER_VOL_KEY] =
	{ .len = MASTER_VOL_KEY_SIZE << 3,
	  .label = "volume-master",
	},
	[NMTD_VOL_KEY] =
	{ .len = NMTD_VOL_KEY_SIZE << 3,
	  .label = "volume-nmtd-key-generation"
	},
	[NMTD_LINK_KEY] =
	{ .len = 128,
	  .label = "link-nmtd-authentication"
	},
	[EMTD_FILE_KEY] =
	{ .len = 128,
	  .label = "file-emtd-encryption-and-auth"
	},
	[DATA_FILE_KEY_256] =
	{ .len = 256,
	  .label = "file-data-encryption-256"
	},
	[DATA_FILE_KEY_512] =
	{ .len = 512,
	  .label = "file-data-encryption-512"
	}
};

/*
  Local variables:
  c-indentation-style: "K&R"
  mode-name: "LC"
  c-basic-offset: 8
  tab-width: 8
  fill-column: 80
  scroll-step: 1
  End:
*/