summaryrefslogtreecommitdiffstats
path: root/src/lib/crypto/openssl/enc_provider/aes.c
blob: 76f81d41e1c5a64fdcd7daba3e7ce98fd56e6eeb (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
 * lib/crypto/openssl/enc_provider/aes.c
 *
 * Copyright (C) 2003, 2007, 2008, 2009 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 "enc_provider.h"
#include "rand2key.h"
#include "aes.h"
#include "aead.h"
#include "hash_provider/hash_provider.h"
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/modes.h>

/* proto's */
static krb5_error_code
cts_enc(krb5_key key, const krb5_data *ivec,
		    const krb5_data *input, krb5_data *output);
static krb5_error_code
cbc_enc(krb5_key key, const krb5_data *ivec,
		    const krb5_data *input, krb5_data *output);
static krb5_error_code
cts_decr(krb5_key key, const krb5_data *ivec,
		    const krb5_data *input, krb5_data *output);
static krb5_error_code
cbc_decr(krb5_key key, const krb5_data *ivec,
		    const krb5_data *input, krb5_data *output);
static krb5_error_code
cts_encr_iov(krb5_key key, const krb5_data *ivec,
                    krb5_crypto_iov *data, size_t num_data, size_t dlen);
static krb5_error_code
cts_decr_iov(krb5_key key, const krb5_data *ivec,
                    krb5_crypto_iov *data, size_t num_data, size_t dlen);

#define NUM_BITS 8
#define IV_CTS_BUF_SIZE 16 /* 16 - hardcoded in CRYPTO_cts128_en/decrypt */

static const EVP_CIPHER *
map_mode(unsigned int len)
{
    if (len==16)
        return EVP_aes_128_cbc();
    if (len==32)
        return EVP_aes_256_cbc();
    else
        return NULL;
}

static krb5_error_code
cbc_enc(krb5_key key, const krb5_data *ivec,
		    const krb5_data *input, krb5_data *output)
{
    int             ret = 0, tmp_len = 0;
    unsigned char  *key_buf = NULL;
    unsigned char  *tmp_buf = NULL;
    EVP_CIPHER_CTX  ciph_ctx;

    key_buf = OPENSSL_malloc(key->keyblock.length);
    if (!key_buf)
        return ENOMEM;

    tmp_len = input->length;
    tmp_buf = OPENSSL_malloc(input->length);
    if (!tmp_buf){
        OPENSSL_free(key_buf);
        return ENOMEM;
    }
    memcpy(key_buf, key->keyblock.contents, key->keyblock.length);

    EVP_CIPHER_CTX_init(&ciph_ctx);

    ret = EVP_EncryptInit_ex(&ciph_ctx, map_mode(key->keyblock.length),
                  NULL, key_buf, (ivec) ? (unsigned char*)ivec->data : NULL);

    if (ret == 1){
        EVP_CIPHER_CTX_set_padding(&ciph_ctx,0); 
        ret = EVP_EncryptUpdate(&ciph_ctx, tmp_buf, &tmp_len,
                           (unsigned char *)input->data, input->length);
        output->length = tmp_len;
        if(ret)
            ret = EVP_EncryptFinal_ex(&ciph_ctx,tmp_buf+tmp_len,&tmp_len);
    }

    EVP_CIPHER_CTX_cleanup(&ciph_ctx);

    if (ret == 1){
        memcpy(output->data, tmp_buf, output->length);
        ret = 0;
    } else {
        ret = KRB5_CRYPTO_INTERNAL;
    }

    memset(key_buf, 0, key->keyblock.length);
    memset(tmp_buf, 0, input->length);
    OPENSSL_free(key_buf);
    OPENSSL_free(tmp_buf);

    return ret;
}

static krb5_error_code
cbc_decr(krb5_key key, const krb5_data *ivec,
		    const krb5_data *input, krb5_data *output)
{
    int              ret = 0, tmp_len = 0;
    unsigned char   *key_buf = NULL;
    unsigned char   *tmp_buf = NULL;
    EVP_CIPHER_CTX   ciph_ctx;

    key_buf = OPENSSL_malloc(key->keyblock.length);
    if (!key_buf)
        return ENOMEM;

    tmp_len = input->length;
    tmp_buf = OPENSSL_malloc(input->length);
    if (!tmp_buf){
        OPENSSL_free(key_buf);
        return ENOMEM;
    }
    memcpy(key_buf, key->keyblock.contents, key->keyblock.length);

    EVP_CIPHER_CTX_init(&ciph_ctx);

    ret = EVP_DecryptInit_ex(&ciph_ctx, map_mode(key->keyblock.length),
                  NULL, key_buf, (ivec) ? (unsigned char*)ivec->data : NULL);
    if (ret == 1) {
        EVP_CIPHER_CTX_set_padding(&ciph_ctx,0); 
        ret = EVP_EncryptUpdate(&ciph_ctx, tmp_buf, &tmp_len,
                           (unsigned char *)input->data, input->length);
        output->length = tmp_len;
        if (ret == 1)
            ret = EVP_DecryptFinal_ex(&ciph_ctx,tmp_buf+tmp_len,&tmp_len);
    }

    EVP_CIPHER_CTX_cleanup(&ciph_ctx);

    if (ret == 1) {
        output->length += tmp_len;
        memcpy(output->data, tmp_buf, output->length);
        ret = 0;
    } else {
        ret = KRB5_CRYPTO_INTERNAL;
    }

    memset(key_buf, 0, key->keyblock.length);
    memset(tmp_buf, 0, input->length);
    OPENSSL_free(key_buf);
    OPENSSL_free(tmp_buf);

    return ret;
}

static krb5_error_code
cts_enc(krb5_key key, const krb5_data *ivec,
		    const krb5_data *input, krb5_data *output)
{
    int             ret = 0, tmp_len = 0;
    size_t          size = 0;
    unsigned char   iv_cts[IV_CTS_BUF_SIZE];
    unsigned char  *tmp_buf = NULL;
    AES_KEY         enck;

    memset(iv_cts,0,sizeof(iv_cts));
    if (ivec && ivec->data){
        if (ivec->length != sizeof(iv_cts))
            return KRB5_CRYPTO_INTERNAL;
        memcpy(iv_cts, ivec->data,ivec->length);
    }

    tmp_buf = OPENSSL_malloc(input->length);
    if (!tmp_buf)
        return ENOMEM;
    tmp_len = input->length;

    AES_set_encrypt_key(key->keyblock.contents,
			NUM_BITS * key->keyblock.length, &enck);

    size = CRYPTO_cts128_encrypt((unsigned char *)input->data, tmp_buf,
                                 input->length, &enck,
                                 iv_cts, (cbc128_f)AES_cbc_encrypt);
    if (size <= 0 || output->length < size) {
        ret = KRB5_CRYPTO_INTERNAL;
    } else {
        output->length = size;
        memcpy(output->data, tmp_buf, output->length);
        ret = 0;
    }

    if (!ret && ivec && ivec->data)
        memcpy(ivec->data, iv_cts, sizeof(iv_cts));

    memset(tmp_buf, 0, input->length);
    OPENSSL_free(tmp_buf);

    return ret;
}

static krb5_error_code
cts_decr(krb5_key key, const krb5_data *ivec,
		    const krb5_data *input, krb5_data *output)
{
    int    ret = 0, tmp_len = 0;
    size_t size = 0;
    unsigned char   iv_cts[IV_CTS_BUF_SIZE];
    unsigned char  *tmp_buf = NULL;
    AES_KEY         deck;

    memset(iv_cts,0,sizeof(iv_cts));
    if (ivec && ivec->data){
        if (ivec->length != sizeof(iv_cts))
            return KRB5_CRYPTO_INTERNAL;
        memcpy(iv_cts, ivec->data,ivec->length);
    }

    tmp_buf = OPENSSL_malloc(input->length);
    if (!tmp_buf)
        return ENOMEM;
    tmp_len = input->length;

    AES_set_decrypt_key(key->keyblock.contents,
			NUM_BITS * key->keyblock.length, &deck);

    size = CRYPTO_cts128_decrypt((unsigned char *)input->data, tmp_buf,
                                 input->length, &deck,
                                 iv_cts, (cbc128_f)AES_cbc_encrypt);
    if (size <= 0 || output->length < size) {
        ret = KRB5_CRYPTO_INTERNAL;
    } else {
        output->length = size + 16;
        memcpy(output->data, tmp_buf, output->length);
        ret = 0;
    }

    if (!ret && ivec && ivec->data)
        memcpy(ivec->data, iv_cts, sizeof(iv_cts));

    memset(tmp_buf, 0, input->length);
    OPENSSL_free(tmp_buf);

    return ret;
}

static krb5_error_code
cts_encr_iov(krb5_key key,
		        const krb5_data *ivec,
		        krb5_crypto_iov *data,
		        size_t num_data, size_t dlen)
{
    int                    ret = 0;
    int                    oblock_len = BLOCK_SIZE * num_data;
    size_t                 size = 0, tlen = 0;
    unsigned char         *oblock = NULL, *dbuf = NULL;
    unsigned char          iv_cts[IV_CTS_BUF_SIZE];
    unsigned char          iblock[BLOCK_SIZE];
    struct iov_block_state input_pos, output_pos;
    AES_KEY                enck;

    memset(iv_cts,0,sizeof(iv_cts));
    if (ivec && ivec->data){
        if (ivec->length != sizeof(iv_cts))
            return KRB5_CRYPTO_INTERNAL;
        memcpy(iv_cts, ivec->data,ivec->length);
    }

    oblock = OPENSSL_malloc(oblock_len);
    if (!oblock){
        return ENOMEM;
    }
    dbuf = OPENSSL_malloc(dlen);
    if (!dbuf){
        OPENSSL_free(oblock);
        return ENOMEM;
    }

    memset(oblock, 0, oblock_len);
    memset(dbuf, 0, dlen);

    IOV_BLOCK_STATE_INIT(&input_pos);
    IOV_BLOCK_STATE_INIT(&output_pos);

    tlen = 0;
    for (;;) {
        if (krb5int_c_iov_get_block(iblock, BLOCK_SIZE,
                                     data, num_data, &input_pos)){
            memcpy(dbuf+tlen,iblock, BLOCK_SIZE);

            tlen += BLOCK_SIZE;
       } else {
            memcpy(dbuf+tlen,iblock, dlen - tlen);
            break;
       }

        if (tlen > dlen) break;
    }

    AES_set_encrypt_key(key->keyblock.contents,
			NUM_BITS * key->keyblock.length, &enck);

    size = CRYPTO_cts128_encrypt((unsigned char *)dbuf, oblock, dlen, &enck,
                                 iv_cts, (cbc128_f)AES_cbc_encrypt);
    if (size <= 0) {
        ret = KRB5_CRYPTO_INTERNAL;
    } else {
        krb5int_c_iov_put_block(data, num_data,
                                oblock, dlen, &output_pos);
    }

    if (!ret && ivec && ivec->data)
        memcpy(ivec->data, iv_cts, sizeof(iv_cts));

    memset(oblock,0,oblock_len);
    memset(dbuf,0,dlen);
    OPENSSL_free(oblock);
    OPENSSL_free(dbuf);

    return ret;
}

static krb5_error_code
cts_decr_iov(krb5_key key,
		        const krb5_data *ivec,
		        krb5_crypto_iov *data,
		        size_t num_data, size_t dlen)
{
    int                    ret = 0;
    int                    oblock_len = BLOCK_SIZE*num_data;
    size_t                 size = 0, tlen = 0;
    unsigned char         *oblock = NULL;
    unsigned char         *dbuf = NULL;
    unsigned char          iblock[BLOCK_SIZE];
    unsigned char          iv_cts[IV_CTS_BUF_SIZE];
    struct iov_block_state input_pos, output_pos;
    AES_KEY                deck;

    memset(iv_cts,0,sizeof(iv_cts));
    if (ivec && ivec->data){
        if (ivec->length <= sizeof(iv_cts))
            return KRB5_CRYPTO_INTERNAL;
        memcpy(iv_cts, ivec->data,ivec->length);
    }

    IOV_BLOCK_STATE_INIT(&input_pos);
    IOV_BLOCK_STATE_INIT(&output_pos);

    oblock = OPENSSL_malloc(oblock_len);
    if (!oblock)
        return ENOMEM;
    dbuf = OPENSSL_malloc(dlen);
    if (!dbuf){
        OPENSSL_free(oblock);
        return ENOMEM;
    }

    memset(oblock, 0, oblock_len);
    memset(dbuf, 0, dlen);

    AES_set_decrypt_key(key->keyblock.contents,
			NUM_BITS * key->keyblock.length, &deck);

    tlen = 0;
    for (;;) {
        if (krb5int_c_iov_get_block(iblock, BLOCK_SIZE,
                                     data, num_data, &input_pos)){
            memcpy(dbuf+tlen,iblock, BLOCK_SIZE);

            tlen += BLOCK_SIZE;
       } else {
            memcpy(dbuf+tlen,iblock, dlen - tlen);
            break;
       }

        if (tlen > dlen) break;
    }

    size = CRYPTO_cts128_decrypt((unsigned char *)dbuf, oblock,
                                 dlen, &deck,
                                 iv_cts, (cbc128_f)AES_cbc_encrypt);
    if (size <= 0)
        ret = KRB5_CRYPTO_INTERNAL;
    else {
        krb5int_c_iov_put_block(data, num_data, oblock, dlen, &output_pos);
    }

    if (!ret && ivec && ivec->data)
        memcpy(ivec->data, iv_cts, sizeof(iv_cts));

    memset(oblock,0,oblock_len);
    memset(dbuf,0,dlen);
    OPENSSL_free(oblock);
    OPENSSL_free(dbuf);

    return ret;
}

krb5_error_code
krb5int_aes_encrypt(krb5_key key, const krb5_data *ivec,
		    const krb5_data *input, krb5_data *output)
{
    int  ret = 0;

    if (input->length <= BLOCK_SIZE){
        ret = cbc_enc(key, ivec, input, output);
    } else {
        ret = cts_enc(key, ivec, input, output);
    }

    return ret;
}

krb5_error_code
krb5int_aes_decrypt(krb5_key key, const krb5_data *ivec,
		    const krb5_data *input, krb5_data *output)
{
    int ret = 0;
    int nblocks = 0;

    if (input->length < BLOCK_SIZE)
        abort();

    if (input->length == BLOCK_SIZE){
        ret = cbc_decr(key, ivec, input, output);
    } else {
        ret = cts_decr(key, ivec, input, output);
    }

    return ret;
}

static krb5_error_code
krb5int_aes_encrypt_iov(krb5_key key,
		        const krb5_data *ivec,
		        krb5_crypto_iov *data,
		        size_t num_data)
{
    int    ret = 0;
    int    nblocks = 0;
    size_t input_length, i;

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

        if (ENCRYPT_IOV(iov))
            input_length += iov->data.length;
    }

    nblocks = (input_length + BLOCK_SIZE - 1) / BLOCK_SIZE;
    assert(nblocks > 1);

    ret = cts_encr_iov(key, ivec, data, num_data, input_length);

    return ret;
}

static krb5_error_code
krb5int_aes_decrypt_iov(krb5_key key,
		        const krb5_data *ivec,
		        krb5_crypto_iov *data,
		        size_t num_data)
{
    int    ret = 0;
    int    nblocks = 0;
    size_t input_length, i;

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

        if (ENCRYPT_IOV(iov))
            input_length += iov->data.length;
    }

    nblocks = (input_length + BLOCK_SIZE - 1) / BLOCK_SIZE;

    assert(nblocks > 1);

    ret = cts_decr_iov(key, ivec, data, num_data, input_length);

    return ret;
}

static krb5_error_code
krb5int_aes_init_state (const krb5_keyblock *key, krb5_keyusage usage,
			krb5_data *state)
{
    state->length = 16;
    state->data = (void *) malloc(16);
    if (state->data == NULL)
	return ENOMEM;
    memset(state->data, 0, state->length);
    return 0;
}
const struct krb5_enc_provider krb5int_enc_aes128 = {
    16,
    16, 16,
    krb5int_aes_encrypt,
    krb5int_aes_decrypt,
    krb5int_aes_make_key,
    krb5int_aes_init_state,
    krb5int_default_free_state,
    krb5int_aes_encrypt_iov,
    krb5int_aes_decrypt_iov
};

const struct krb5_enc_provider krb5int_enc_aes256 = {
    16,
    32, 32,
    krb5int_aes_encrypt,
    krb5int_aes_decrypt,
    krb5int_aes_make_key,
    krb5int_aes_init_state,
    krb5int_default_free_state,
    krb5int_aes_encrypt_iov,
    krb5int_aes_decrypt_iov
};