aboutsummaryrefslogtreecommitdiff
path: root/src/lib/crypto/builtin/enc_provider/aes.c
blob: cde5bb551c4b3fd3047b3a90a71c2a9deff0af7d (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
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 * lib/crypto/enc_provider/aes.c
 *
 * Copyright (C) 2003, 2007, 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 "enc_provider.h"
#include "aes.h"
#include <aead.h>
#include <rand2key.h>

#if 0
aes_rval aes_blk_len(unsigned int blen, aes_ctx cx[1]);
aes_rval aes_enc_key(const unsigned char in_key[], unsigned int klen, aes_ctx cx[1]);
aes_rval aes_enc_blk(const unsigned char in_blk[], unsigned char out_blk[], const aes_ctx cx[1]);
aes_rval aes_dec_key(const unsigned char in_key[], unsigned int klen, aes_ctx cx[1]);
aes_rval aes_dec_blk(const unsigned char in_blk[], unsigned char out_blk[], const aes_ctx cx[1]);
#endif

#define CHECK_SIZES 0

#if 0
static void printd (const char *descr, krb5_data *d) {
    int i, j;
    const int r = 16;

    printf("%s:", descr);

    for (i = 0; i < d->length; i += r) {
        printf("\n  %04x: ", i);
        for (j = i; j < i + r && j < d->length; j++)
            printf(" %02x", 0xff & d->data[j]);
#ifdef SHOW_TEXT
        for (; j < i + r; j++)
            printf("   ");
        printf("   ");
        for (j = i; j < i + r && j < d->length; j++) {
            int c = 0xff & d->data[j];
            printf("%c", isprint(c) ? c : '.');
        }
#endif
    }
    printf("\n");
}
#endif

static inline void enc(char *out, const char *in, aes_ctx *ctx)
{
    if (aes_enc_blk((const unsigned char *)in, (unsigned char *)out, ctx)
        != aes_good)
        abort();
}
static inline void dec(char *out, const char *in, aes_ctx *ctx)
{
    if (aes_dec_blk((const unsigned char *)in, (unsigned char *)out, ctx)
        != aes_good)
        abort();
}

static void xorblock(char *out, const char *in)
{
    int z;
    for (z = 0; z < BLOCK_SIZE; z++)
        out[z] ^= in[z];
}

krb5_error_code
krb5int_aes_encrypt(krb5_key key, const krb5_data *ivec,
                    const krb5_data *input, krb5_data *output)
{
    aes_ctx ctx;
    char tmp[BLOCK_SIZE], tmp2[BLOCK_SIZE], tmp3[BLOCK_SIZE];
    int nblocks = 0, blockno;

/*    CHECK_SIZES; */

    if (aes_enc_key(key->keyblock.contents, key->keyblock.length,
                    &ctx) != aes_good)
        abort();

    if (ivec)
        memcpy(tmp, ivec->data, BLOCK_SIZE);
    else
        memset(tmp, 0, BLOCK_SIZE);

    nblocks = (input->length + BLOCK_SIZE - 1) / BLOCK_SIZE;

    if (nblocks == 1) {
        /* XXX Used for DK function.  */
        enc(output->data, input->data, &ctx);
    } else {
        unsigned int nleft;

        for (blockno = 0; blockno < nblocks - 2; blockno++) {
            xorblock(tmp, input->data + blockno * BLOCK_SIZE);
            enc(tmp2, tmp, &ctx);
            memcpy(output->data + blockno * BLOCK_SIZE, tmp2, BLOCK_SIZE);

            /* Set up for next block.  */
            memcpy(tmp, tmp2, BLOCK_SIZE);
        }
        /* Do final CTS step for last two blocks (the second of which
           may or may not be incomplete).  */
        xorblock(tmp, input->data + (nblocks - 2) * BLOCK_SIZE);
        enc(tmp2, tmp, &ctx);
        nleft = input->length - (nblocks - 1) * BLOCK_SIZE;
        memcpy(output->data + (nblocks - 1) * BLOCK_SIZE, tmp2, nleft);
        memcpy(tmp, tmp2, BLOCK_SIZE);

        memset(tmp3, 0, sizeof(tmp3));
        memcpy(tmp3, input->data + (nblocks - 1) * BLOCK_SIZE, nleft);
        xorblock(tmp, tmp3);
        enc(tmp2, tmp, &ctx);
        memcpy(output->data + (nblocks - 2) * BLOCK_SIZE, tmp2, BLOCK_SIZE);
        if (ivec)
            memcpy(ivec->data, tmp2, BLOCK_SIZE);
    }

    return 0;
}

krb5_error_code
krb5int_aes_decrypt(krb5_key key, const krb5_data *ivec,
                    const krb5_data *input, krb5_data *output)
{
    aes_ctx ctx;
    char tmp[BLOCK_SIZE], tmp2[BLOCK_SIZE], tmp3[BLOCK_SIZE];
    int nblocks = 0, blockno;

    CHECK_SIZES;

    if (aes_dec_key(key->keyblock.contents, key->keyblock.length,
                    &ctx) != aes_good)
        abort();

    if (ivec)
        memcpy(tmp, ivec->data, BLOCK_SIZE);
    else
        memset(tmp, 0, BLOCK_SIZE);

    nblocks = (input->length + BLOCK_SIZE - 1) / BLOCK_SIZE;

    if (nblocks == 1) {
        if (input->length < BLOCK_SIZE)
            abort();
        dec(output->data, input->data, &ctx);
    } else {

        for (blockno = 0; blockno < nblocks - 2; blockno++) {
            dec(tmp2, input->data + blockno * BLOCK_SIZE, &ctx);
            xorblock(tmp2, tmp);
            memcpy(output->data + blockno * BLOCK_SIZE, tmp2, BLOCK_SIZE);
            memcpy(tmp, input->data + blockno * BLOCK_SIZE, BLOCK_SIZE);
        }
        /* Do last two blocks, the second of which (next-to-last block
           of plaintext) may be incomplete.  */
        dec(tmp2, input->data + (nblocks - 2) * BLOCK_SIZE, &ctx);
        /* Set tmp3 to last ciphertext block, padded.  */
        memset(tmp3, 0, sizeof(tmp3));
        memcpy(tmp3, input->data + (nblocks - 1) * BLOCK_SIZE,
               input->length - (nblocks - 1) * BLOCK_SIZE);
        /* Set tmp2 to last (possibly partial) plaintext block, and
           save it.  */
        xorblock(tmp2, tmp3);
        memcpy(output->data + (nblocks - 1) * BLOCK_SIZE, tmp2,
               input->length - (nblocks - 1) * BLOCK_SIZE);
        /* Maybe keep the trailing part, and copy in the last
           ciphertext block.  */
        memcpy(tmp2, tmp3, input->length - (nblocks - 1) * BLOCK_SIZE);
        /* Decrypt, to get next to last plaintext block xor previous
           ciphertext.  */
        dec(tmp3, tmp2, &ctx);
        xorblock(tmp3, tmp);
        memcpy(output->data + (nblocks - 2) * BLOCK_SIZE, tmp3, BLOCK_SIZE);
        if (ivec)
            memcpy(ivec->data, input->data + (nblocks - 2) * BLOCK_SIZE,
                   BLOCK_SIZE);
    }

    return 0;
}

static krb5_error_code
krb5int_aes_encrypt_iov(krb5_key key,
                        const krb5_data *ivec,
                        krb5_crypto_iov *data,
                        size_t num_data)
{
    aes_ctx ctx;
    char tmp[BLOCK_SIZE], tmp2[BLOCK_SIZE];
    int nblocks = 0, blockno;
    size_t input_length, i;

    if (aes_enc_key(key->keyblock.contents, key->keyblock.length, &ctx)
        != aes_good)
        abort();

    if (ivec != NULL)
        memcpy(tmp, ivec->data, BLOCK_SIZE);
    else
        memset(tmp, 0, BLOCK_SIZE);

    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);

    {
        char blockN2[BLOCK_SIZE];   /* second last */
        char blockN1[BLOCK_SIZE];   /* last block */
        struct iov_block_state input_pos, output_pos;

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

        for (blockno = 0; blockno < nblocks - 2; blockno++) {
            char blockN[BLOCK_SIZE];

            krb5int_c_iov_get_block((unsigned char *)blockN, BLOCK_SIZE, data, num_data, &input_pos);
            xorblock(tmp, blockN);
            enc(tmp2, tmp, &ctx);
            krb5int_c_iov_put_block(data, num_data, (unsigned char *)tmp2, BLOCK_SIZE, &output_pos);

            /* Set up for next block.  */
            memcpy(tmp, tmp2, BLOCK_SIZE);
        }

        /* Do final CTS step for last two blocks (the second of which
           may or may not be incomplete).  */

        /* First, get the last two blocks */
        memset(blockN1, 0, sizeof(blockN1)); /* pad last block with zeros */
        krb5int_c_iov_get_block((unsigned char *)blockN2, BLOCK_SIZE, data, num_data, &input_pos);
        krb5int_c_iov_get_block((unsigned char *)blockN1, BLOCK_SIZE, data, num_data, &input_pos);

        /* Encrypt second last block */
        xorblock(tmp, blockN2);
        enc(tmp2, tmp, &ctx);
        memcpy(blockN2, tmp2, BLOCK_SIZE); /* blockN2 now contains first block */
        memcpy(tmp, tmp2, BLOCK_SIZE);

        /* Encrypt last block */
        xorblock(tmp, blockN1);
        enc(tmp2, tmp, &ctx);
        memcpy(blockN1, tmp2, BLOCK_SIZE);

        /* Put the last two blocks back into the iovec (reverse order) */
        krb5int_c_iov_put_block(data, num_data, (unsigned char *)blockN1, BLOCK_SIZE, &output_pos);
        krb5int_c_iov_put_block(data, num_data, (unsigned char *)blockN2, BLOCK_SIZE, &output_pos);

        if (ivec != NULL)
            memcpy(ivec->data, blockN1, BLOCK_SIZE);
    }

    return 0;
}

static krb5_error_code
krb5int_aes_decrypt_iov(krb5_key key,
                        const krb5_data *ivec,
                        krb5_crypto_iov *data,
                        size_t num_data)
{
    aes_ctx ctx;
    char tmp[BLOCK_SIZE], tmp2[BLOCK_SIZE], tmp3[BLOCK_SIZE];
    int nblocks = 0, blockno;
    unsigned int i;
    size_t input_length;

    CHECK_SIZES;

    if (aes_dec_key(key->keyblock.contents, key->keyblock.length,
                    &ctx) != aes_good)
        abort();

    if (ivec != NULL)
        memcpy(tmp, ivec->data, BLOCK_SIZE);
    else
        memset(tmp, 0, BLOCK_SIZE);

    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);

    {
        char blockN2[BLOCK_SIZE];   /* second last */
        char blockN1[BLOCK_SIZE];   /* last block */
        struct iov_block_state input_pos, output_pos;

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

        for (blockno = 0; blockno < nblocks - 2; blockno++) {
            char blockN[BLOCK_SIZE];

            krb5int_c_iov_get_block((unsigned char *)blockN, BLOCK_SIZE, data, num_data, &input_pos);
            dec(tmp2, blockN, &ctx);
            xorblock(tmp2, tmp);
            krb5int_c_iov_put_block(data, num_data, (unsigned char *)tmp2, BLOCK_SIZE, &output_pos);
            memcpy(tmp, blockN, BLOCK_SIZE);
        }

        /* Do last two blocks, the second of which (next-to-last block
           of plaintext) may be incomplete.  */

        /* First, get the last two encrypted blocks */
        memset(blockN1, 0, sizeof(blockN1)); /* pad last block with zeros */
        krb5int_c_iov_get_block((unsigned char *)blockN2, BLOCK_SIZE, data, num_data, &input_pos);
        krb5int_c_iov_get_block((unsigned char *)blockN1, BLOCK_SIZE, data, num_data, &input_pos);

        if (ivec != NULL)
            memcpy(ivec->data, blockN2, BLOCK_SIZE);

        /* Decrypt second last block */
        dec(tmp2, blockN2, &ctx);
        /* Set tmp2 to last (possibly partial) plaintext block, and
           save it.  */
        xorblock(tmp2, blockN1);
        memcpy(blockN2, tmp2, BLOCK_SIZE);

        /* Maybe keep the trailing part, and copy in the last
           ciphertext block.  */
        input_length %= BLOCK_SIZE;
        memcpy(tmp2, blockN1, input_length ? input_length : BLOCK_SIZE);
        dec(tmp3, tmp2, &ctx);
        xorblock(tmp3, tmp);
        memcpy(blockN1, tmp3, BLOCK_SIZE);

        /* Put the last two blocks back into the iovec */
        krb5int_c_iov_put_block(data, num_data, (unsigned char *)blockN1, BLOCK_SIZE, &output_pos);
        krb5int_c_iov_put_block(data, num_data, (unsigned char *)blockN2, BLOCK_SIZE, &output_pos);
    }

    return 0;
}

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
};