aboutsummaryrefslogtreecommitdiff
path: root/src/lib/crypto/dk/dk_aead.c
blob: 8abf5af5f440fc38aaf9d116af2af97326d9d4c7 (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * lib/crypto/dk/dk_aead.c
 *
 * Copyright 2008 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */


#include "k5-int.h"
#include "dk.h"
#include "aead.h"

#define K5CLENGTH 5 /* 32 bit net byte order integer + one byte seed */

/* AEAD */

static krb5_error_code
krb5int_dk_crypto_length(const struct krb5_aead_provider *aead,
			 const struct krb5_enc_provider *enc,
			 const struct krb5_hash_provider *hash,
			 krb5_cryptotype type,
			 unsigned int *length)
{
    switch (type) {
    case KRB5_CRYPTO_TYPE_HEADER:
    case KRB5_CRYPTO_TYPE_PADDING:
	*length = enc->block_size;
	break;
    case KRB5_CRYPTO_TYPE_TRAILER:
    case KRB5_CRYPTO_TYPE_CHECKSUM:
	*length = hash->hashsize;
	break;
    default:
	assert(0 && "invalid cryptotype passed to krb5int_dk_crypto_length");
	break;
    }

    return 0;
}

static krb5_error_code
krb5int_dk_encrypt_iov(const struct krb5_aead_provider *aead,
		       const struct krb5_enc_provider *enc,
		       const struct krb5_hash_provider *hash,
		       const krb5_keyblock *key,
		       krb5_keyusage usage,
		       const krb5_data *ivec,
		       krb5_crypto_iov *data,
		       size_t num_data)
{
    krb5_error_code ret;
    unsigned char constantdata[K5CLENGTH];
    krb5_data d1, d2;
    krb5_crypto_iov *header, *trailer, *padding;
    krb5_keyblock ke, ki;
    size_t i;
    unsigned int blocksize = 0;
    unsigned int plainlen = 0;
    unsigned int hmacsize = 0;
    unsigned int padsize = 0;
    unsigned char *cksum = NULL;

    ke.contents = ki.contents = NULL;
    ke.length = ki.length = 0;

    /* E(Confounder | Plaintext | Pad) | Checksum */

    ret = aead->crypto_length(aead, enc, hash, KRB5_CRYPTO_TYPE_PADDING, &blocksize);
    if (ret != 0)
	return ret;

    ret = aead->crypto_length(aead, enc, hash, KRB5_CRYPTO_TYPE_TRAILER, &hmacsize);
    if (ret != 0)
	return ret;

    for (i = 0; i < num_data; i++) {
	krb5_crypto_iov *iov = &data[i];

	if (iov->flags == KRB5_CRYPTO_TYPE_DATA)
	    plainlen += iov->data.length;
    }

    /* Validate header and trailer lengths. */

    header = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_HEADER);
    if (header == NULL || header->data.length < enc->block_size)
	return KRB5_BAD_MSIZE;

    trailer = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_TRAILER);
    if (trailer == NULL || trailer->data.length < hmacsize)
	return KRB5_BAD_MSIZE;

    if (blocksize != 0) {
	/* Check that the input data is correctly padded */
	if (plainlen % blocksize)
	    padsize = blocksize - (plainlen % blocksize);
    }

    padding = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_PADDING);
    if (padsize && (padding == NULL || padding->data.length < padsize))
	return KRB5_BAD_MSIZE;

    if (padding != NULL) {
	memset(padding->data.data, 0, padsize);
	padding->data.length = padsize;
    }

    ke.length = enc->keylength;
    ke.contents = malloc(ke.length);
    if (ke.contents == NULL) {
	ret = ENOMEM;
	goto cleanup;
    }
    ki.length = enc->keylength;
    ki.contents = malloc(ki.length);
    if (ki.contents == NULL) {
	ret = ENOMEM;
	goto cleanup;
    }
    cksum = (unsigned char *)malloc(hash->hashsize);
    if (cksum == NULL) {
	ret = ENOMEM;
	goto cleanup;
    }

    /* derive the keys */

    d1.data = (char *)constantdata;
    d1.length = K5CLENGTH;

    d1.data[0] = (usage >> 24) & 0xFF;
    d1.data[1] = (usage >> 16) & 0xFF;
    d1.data[2] = (usage >> 8 ) & 0xFF;
    d1.data[3] = (usage      ) & 0xFF;

    d1.data[4] = 0xAA;

    ret = krb5_derive_key(enc, key, &ke, &d1);
    if (ret != 0)
	goto cleanup;

    d1.data[4] = 0x55;

    ret = krb5_derive_key(enc, key, &ki, &d1);
    if (ret != 0)
	goto cleanup;

    /* generate confounder */

    header->data.length = enc->block_size;

    ret = krb5_c_random_make_octets(/* XXX */ NULL, &header->data);
    if (ret != 0)
	goto cleanup;

    /* hash the plaintext */
    d2.length = hash->hashsize;
    d2.data = (char *)cksum;

    ret = krb5int_hmac_iov(hash, &ki, data, num_data, &d2);
    if (ret != 0)
	goto cleanup;

    /* encrypt the plaintext (header | data | padding) */
    assert(enc->encrypt_iov != NULL);

    ret = enc->encrypt_iov(&ke, ivec, data, num_data); /* will update ivec */
    if (ret != 0)
	goto cleanup;

    /* possibly truncate the hash */
    assert(hmacsize <= d2.length);

    memcpy(trailer->data.data, cksum, hmacsize);
    trailer->data.length = hmacsize;

cleanup:
    if (ke.contents != NULL) {
	memset(ke.contents, 0, ke.length);
	free(ke.contents);
    }
    if (ki.contents != NULL) {
	memset(ki.contents, 0, ki.length);
	free(ki.contents);
    }
    if (cksum != NULL) {
	free(cksum);
    }

    return ret;
}

static krb5_error_code
krb5int_dk_decrypt_iov(const struct krb5_aead_provider *aead,
		       const struct krb5_enc_provider *enc,
		       const struct krb5_hash_provider *hash,
		       const krb5_keyblock *key,
		       krb5_keyusage usage,
		       const krb5_data *ivec,
		       krb5_crypto_iov *data,
		       size_t num_data)
{
    krb5_error_code ret;
    unsigned char constantdata[K5CLENGTH];
    krb5_data d1;
    krb5_crypto_iov *header, *trailer;
    krb5_keyblock ke, ki;
    size_t i;
    unsigned int blocksize = 0; /* careful, this is enc block size not confounder len */
    unsigned int cipherlen = 0;
    unsigned int hmacsize = 0;
    unsigned char *cksum = NULL;

    if (krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_STREAM) != NULL) {
	return krb5int_c_iov_decrypt_stream(aead, enc, hash, key,
					    usage, ivec, data, num_data);
    }

    ke.contents = ki.contents = NULL;
    ke.length = ki.length = 0;

    /* E(Confounder | Plaintext | Pad) | Checksum */

    ret = aead->crypto_length(aead, enc, hash, KRB5_CRYPTO_TYPE_PADDING, &blocksize);
    if (ret != 0)
	return ret;

    ret = aead->crypto_length(aead, enc, hash, KRB5_CRYPTO_TYPE_TRAILER, &hmacsize);
    if (ret != 0)
	return ret;

    for (i = 0; i < num_data; i++) {
	const krb5_crypto_iov *iov = &data[i];

	if (ENCRYPT_DATA_IOV(iov))
	    cipherlen += iov->data.length;
    }

    if (blocksize == 0) {
	/* Check for correct input length in CTS mode */
	if (enc->block_size != 0 && cipherlen < enc->block_size)
	    return KRB5_BAD_MSIZE;
    } else {
	/* Check that the input data is correctly padded */
	if ((cipherlen % blocksize) != 0)
	    return KRB5_BAD_MSIZE;
    }

    /* Validate header and trailer lengths */

    header = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_HEADER);
    if (header == NULL || header->data.length != enc->block_size)
	return KRB5_BAD_MSIZE;

    trailer = krb5int_c_locate_iov(data, num_data, KRB5_CRYPTO_TYPE_TRAILER);
    if (trailer == NULL || trailer->data.length != hmacsize)
	return KRB5_BAD_MSIZE;

    ke.length = enc->keylength;
    ke.contents = malloc(ke.length);
    if (ke.contents == NULL) {
	ret = ENOMEM;
	goto cleanup;
    }
    ki.length = enc->keylength;
    ki.contents = malloc(ki.length);
    if (ki.contents == NULL) {
	ret = ENOMEM;
	goto cleanup;
    }
    cksum = (unsigned char *)malloc(hash->hashsize);
    if (cksum == NULL) {
	ret = ENOMEM;
	goto cleanup;
    }

    /* derive the keys */

    d1.data = (char *)constantdata;
    d1.length = K5CLENGTH;

    d1.data[0] = (usage >> 24) & 0xFF;
    d1.data[1] = (usage >> 16) & 0xFF;
    d1.data[2] = (usage >> 8 ) & 0xFF;
    d1.data[3] = (usage      ) & 0xFF;

    d1.data[4] = 0xAA;

    ret = krb5_derive_key(enc, key, &ke, &d1);
    if (ret != 0)
	goto cleanup;

    d1.data[4] = 0x55;

    ret = krb5_derive_key(enc, key, &ki, &d1);
    if (ret != 0)
	goto cleanup;

    /* decrypt the plaintext (header | data | padding) */
    assert(enc->decrypt_iov != NULL);

    ret = enc->decrypt_iov(&ke, ivec, data, num_data); /* will update ivec */
    if (ret != 0)
	goto cleanup;

    /* verify the hash */
    d1.length = hash->hashsize; /* non-truncated length */
    d1.data = (char *)cksum;

    ret = krb5int_hmac_iov(hash, &ki, data, num_data, &d1);
    if (ret != 0)
	goto cleanup;

    /* compare only the possibly truncated length */
    if (memcmp(cksum, trailer->data.data, hmacsize) != 0) {
	ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
	goto cleanup;
    }

cleanup:
    if (ke.contents != NULL) {
	memset(ke.contents, 0, ke.length);
	free(ke.contents);
    }
    if (ki.contents != NULL) {
	memset(ki.contents, 0, ki.length);
	free(ki.contents);
    }
    if (cksum != NULL) {
	free(cksum);
    }

    return ret;
}

const struct krb5_aead_provider krb5int_aead_dk = {
    krb5int_dk_crypto_length,
    krb5int_dk_encrypt_iov,
    krb5int_dk_decrypt_iov
};

static krb5_error_code
krb5int_aes_crypto_length(const struct krb5_aead_provider *aead,
			 const struct krb5_enc_provider *enc,
			 const struct krb5_hash_provider *hash,
			 krb5_cryptotype type,
			 unsigned int *length)
{
    switch (type) {
    case KRB5_CRYPTO_TYPE_HEADER:
	*length = enc->block_size;
	break;
    case KRB5_CRYPTO_TYPE_PADDING:
	*length = 0;
	break;
    case KRB5_CRYPTO_TYPE_TRAILER:
    case KRB5_CRYPTO_TYPE_CHECKSUM:
	*length = 96 / 8;
	break;
    default:
	assert(0 && "invalid cryptotype passed to krb5int_aes_crypto_length");
	break;
    }

    return 0;
}

const struct krb5_aead_provider krb5int_aead_aes = {
    krb5int_aes_crypto_length,
    krb5int_dk_encrypt_iov,
    krb5int_dk_decrypt_iov
};