aboutsummaryrefslogtreecommitdiff
path: root/src/lib/crypto/builtin/enc_provider/camellia_ctr.c
blob: a71e03be99434081e2b1366f271e49887f46b8f2 (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
/*
 * lib/crypto/builtin/enc_provider/camellia_ctr.c
 *
 * Copyright (C) 2003, 2007-2010 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 "camellia.h"
#include <aead.h>
#include <rand2key.h>

static void
xorblock(unsigned char *out, const unsigned char *in)
{
    int z;
    for (z = 0; z < BLOCK_SIZE/4; z++) {
        unsigned char *outptr = &out[z*4];
        unsigned char *inptr = (unsigned char *)&in[z*4];
        /*
         * Use unaligned accesses.  On x86, this will probably still be faster
         * than multiple byte accesses for unaligned data, and for aligned data
         * should be far better.  (One test indicated about 2.4% faster
         * encryption for 1024-byte messages.)
         *
         * If some other CPU has really slow unaligned-word or byte accesses,
         * perhaps this function (or the load/store helpers?) should test for
         * alignment first.
         *
         * If byte accesses are faster than unaligned words, we may need to
         * conditionalize on CPU type, as that may be hard to determine
         * automatically.
         */
        store_32_n (load_32_n(outptr) ^ load_32_n(inptr), outptr);
    }
}

/* Get the current counter block number from the IV */
static inline void getctrblockno(krb5_ui_8 *pblockno,
				 const unsigned char ctr[BLOCK_SIZE])
{
    *pblockno = load_64_be(&ctr[BLOCK_SIZE - 8]);
}

/* Store the current counter block number in the IV */
static inline void putctrblockno(krb5_ui_8 blockno,
				 unsigned char ctr[BLOCK_SIZE])
{
    store_64_be(blockno, &ctr[BLOCK_SIZE - 8]);
}

/*
 * ivec must be a correctly formatted counter block per SP800-38C A.3
 */
static krb5_error_code
krb5int_camellia_encrypt_ctr(krb5_key key,
                             const krb5_data *ivec,
                             krb5_crypto_iov *data,
                             size_t num_data)
{
    camellia_ctx ctx;
    unsigned char ctr[BLOCK_SIZE];
    krb5_ui_8 blockno;
    struct iov_block_state input_pos, output_pos;

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

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

    /* Don't encrypt the header (B0), and use zero instead of IOV padding */
    input_pos.ignore_header = output_pos.ignore_header = 1;
    input_pos.pad_to_boundary = output_pos.pad_to_boundary = 1;

    if (ivec == NULL)
	return EINVAL;
    if (ivec->length != BLOCK_SIZE)
        return KRB5_BAD_MSIZE;

    memcpy(ctr, ivec->data, BLOCK_SIZE);

    getctrblockno(&blockno, ctr);

    for (;;) {
        unsigned char storage[BLOCK_SIZE], *block;
        unsigned char ectr[BLOCK_SIZE];

        if (!krb5int_c_iov_get_block_nocopy(storage, BLOCK_SIZE,
                                            data, num_data, &input_pos, &block))
            break;

        if (camellia_enc_blk(ctr, ectr, &ctx) != camellia_good)
            abort();

        xorblock(block, ectr);
        krb5int_c_iov_put_block_nocopy(data, num_data, storage, BLOCK_SIZE,
                                       &output_pos, block);
        putctrblockno(++blockno, ctr);
    }

    if (ivec != NULL)
        memcpy(ivec->data, ctr, sizeof(ctr));

    return 0;
}

krb5_error_code
krb5int_camellia_cbc_mac(krb5_key key,
                         const krb5_crypto_iov *data,
                         size_t num_data,
                         const krb5_data *iv,
                         krb5_data *output)
{
    camellia_ctx ctx;
    unsigned char blockY[BLOCK_SIZE];
    struct iov_block_state iov_state;

    if (output->length < BLOCK_SIZE)
        return KRB5_BAD_MSIZE;

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

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

    IOV_BLOCK_STATE_INIT(&iov_state);

    /*
     * The CCM header may not fit in a block, because it includes a variable
     * length encoding of the associated data length. This encoding plus the
     * associated data itself is padded to the block size.
     */
    iov_state.include_sign_only = 1;
    iov_state.pad_to_boundary = 1;

    for (;;) {
        unsigned char blockB[BLOCK_SIZE];

        if (!krb5int_c_iov_get_block(blockB, BLOCK_SIZE, data, num_data, &iov_state))
            break;

        xorblock(blockB, blockY);

        if (camellia_enc_blk(blockB, blockY, &ctx) != camellia_good)
            abort();
    }

    output->length = BLOCK_SIZE;
    memcpy(output->data, blockY, BLOCK_SIZE);

    return 0;
}

static krb5_error_code
krb5int_camellia_init_state_ctr (const krb5_keyblock *key, krb5_keyusage usage,
                                 krb5_data *state)
{
    return alloc_data(state, 16);
}

const struct krb5_enc_provider krb5int_enc_camellia128_ctr = {
    16,
    16, 16,
    krb5int_camellia_encrypt_ctr,
    krb5int_camellia_encrypt_ctr,
    krb5int_camellia_cbc_mac,
    krb5int_camellia_make_key,
    krb5int_camellia_init_state_ctr,
    krb5int_default_free_state,
    NULL
};

const struct krb5_enc_provider krb5int_enc_camellia256_ctr = {
    16,
    32, 32,
    krb5int_camellia_encrypt_ctr,
    krb5int_camellia_encrypt_ctr,
    krb5int_camellia_cbc_mac,
    krb5int_camellia_make_key,
    krb5int_camellia_init_state_ctr,
    krb5int_default_free_state,
    NULL
};