aboutsummaryrefslogtreecommitdiff
path: root/src/lib/crypto/krb/arcfour/arcfour.c
blob: eb80124e4e544e1b57248ef0635195d8a667f81c (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
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*

  ARCFOUR cipher (based on a cipher posted on the Usenet in Spring-95).
  This cipher is widely believed and has been tested to be equivalent
  with the RC4 cipher from RSA Data Security, Inc.  (RC4 is a trademark
  of RSA Data Security)

*/
#include "k5-int.h"
#include "arcfour-int.h"
#include "hash_provider/hash_provider.h"

const char l40[] = "fortybits";

void
krb5int_arcfour_encrypt_length(const struct krb5_enc_provider *enc,
                               const struct krb5_hash_provider *hash,
                               size_t inputlen, size_t *length)
{
    /* checksum + (confounder + inputlen, in even blocksize) */
    *length = hash->hashsize + krb5_roundup(8 + inputlen, enc->block_size);
}

krb5_keyusage
krb5int_arcfour_translate_usage(krb5_keyusage usage)
{
    switch (usage) {
    case 1:  return 1;   /* AS-REQ PA-ENC-TIMESTAMP padata timestamp,  */
    case 2:  return 2;   /* ticket from kdc */
    case 3:  return 8;   /* as-rep encrypted part */
    case 4:  return 4;   /* tgs-req authz data */
    case 5:  return 5;   /* tgs-req authz data in subkey */
    case 6:  return 6;   /* tgs-req authenticator cksum */
    case 7:  return 7;   /* tgs-req authenticator */
    case 8:  return 8;
    case 9:  return 9;   /* tgs-rep encrypted with subkey */
    case 10: return 10;  /* ap-rep authentication cksum (never used by MS) */
    case 11: return 11;  /* app-req authenticator */
    case 12: return 12;  /* app-rep encrypted part */
    case 23: return 13;  /* sign wrap token*/
    default: return usage;
    }
}

/* Derive a usage key from a session key and krb5 usage constant. */
krb5_error_code
krb5int_arcfour_usage_key(const struct krb5_enc_provider *enc,
                          const struct krb5_hash_provider *hash,
                          const krb5_keyblock *session_keyblock,
                          krb5_keyusage usage,
                          krb5_keyblock *out)
{
    char salt_buf[14];
    krb5_data out_data = make_data(out->contents, out->length);
    krb5_data salt = make_data(salt_buf, sizeof(salt_buf));
    krb5_keyusage ms_usage;

    /* Generate the salt. */
    ms_usage = krb5int_arcfour_translate_usage(usage);
    if (session_keyblock->enctype == ENCTYPE_ARCFOUR_HMAC_EXP) {
        memcpy(salt_buf, l40, 10);
        store_32_le(ms_usage, salt_buf + 10);
    } else {
        salt.length=4;
        store_32_le(ms_usage, salt_buf);
    }

    /* Compute HMAC(key, salt) to produce the usage key. */
    return krb5int_hmac_keyblock(hash, session_keyblock, 1, &salt, &out_data);
}

/* Derive an encryption key from a usage key and (typically) checksum. */
krb5_error_code
krb5int_arcfour_enc_key(const struct krb5_enc_provider *enc,
                        const struct krb5_hash_provider *hash,
                        const krb5_keyblock *usage_keyblock,
                        const krb5_data *checksum, krb5_keyblock *out)
{
    krb5_keyblock *trunc_keyblock = NULL;
    krb5_data out_data = make_data(out->contents, out->length);
    krb5_error_code ret;

    /* Copy usage_keyblock to trunc_keyblock and truncate if exportable. */
    ret = krb5int_c_copy_keyblock(NULL, usage_keyblock, &trunc_keyblock);
    if (ret != 0)
        return ret;
    if (trunc_keyblock->enctype == ENCTYPE_ARCFOUR_HMAC_EXP)
        memset(trunc_keyblock->contents + 7, 0xab, 9);

    /* Compute HMAC(trunc_key, checksum) to produce the encryption key. */
    ret = krb5int_hmac_keyblock(hash, trunc_keyblock, 1, checksum, &out_data);
    krb5int_c_free_keyblock(NULL, trunc_keyblock);
    return ret;
}

krb5_error_code
krb5int_arcfour_encrypt(const struct krb5_enc_provider *enc,
                        const struct krb5_hash_provider *hash,
                        krb5_key key, krb5_keyusage usage,
                        const krb5_data *ivec, const krb5_data *input,
                        krb5_data *output)
{
    krb5_keyblock *usage_keyblock = NULL, *enc_keyblock = NULL;
    krb5_key enc_key;
    krb5_data plaintext = empty_data();
    krb5_data checksum, ciphertext, confounder;
    krb5_error_code ret;
    unsigned int plainlen;

    /* Allocate buffers. */
    plainlen = krb5_roundup(input->length + CONFOUNDERLENGTH, enc->block_size);
    ret = alloc_data(&plaintext, plainlen);
    if (ret != 0)
        goto cleanup;
    ret = krb5int_c_init_keyblock(NULL, key->keyblock.enctype, enc->keybytes,
                                  &usage_keyblock);
    if (ret != 0)
        goto cleanup;
    ret = krb5int_c_init_keyblock(NULL, key->keyblock.enctype, enc->keybytes,
                                  &enc_keyblock);
    if (ret != 0)
        goto cleanup;

    /* Set up subsets of output and plaintext. */
    checksum = make_data(output->data, hash->hashsize);
    ciphertext = make_data(output->data + hash->hashsize, plainlen);
    confounder = make_data(plaintext.data, CONFOUNDERLENGTH);

    /* Derive a usage key from the session key and usage. */
    ret = krb5int_arcfour_usage_key(enc, hash, &key->keyblock, usage,
                                    usage_keyblock);
    if (ret != 0)
        goto cleanup;

    /* Compose a confounder with the input data to form the plaintext. */
    ret = krb5_c_random_make_octets(NULL, &confounder);
    memcpy(plaintext.data + confounder.length, input->data, input->length);
    if (ret)
        goto cleanup;

    /* Compute HMAC(usage key, plaintext) to get the checksum. */
    ret = krb5int_hmac_keyblock(hash, usage_keyblock, 1, &plaintext,
                                &checksum);
    if (ret)
        goto cleanup;

    /* Derive the encryption key from the usage key and checksum. */
    ret = krb5int_arcfour_enc_key(enc, hash, usage_keyblock, &checksum,
                                  enc_keyblock);
    if (ret)
        goto cleanup;

    /* Encrypt the plaintext. */
    ret = krb5_k_create_key(NULL, enc_keyblock, &enc_key);
    if (ret)
        goto cleanup;
    ret = (*enc->encrypt)(enc_key, ivec, &plaintext, &ciphertext);
    krb5_k_free_key(NULL, enc_key);
    if (ret)
        goto cleanup;

    output->length = plainlen + hash->hashsize;

cleanup:
    krb5int_c_free_keyblock(NULL, usage_keyblock);
    krb5int_c_free_keyblock(NULL, enc_keyblock);
    zapfree(plaintext.data, plaintext.length);
    return ret;
}

krb5_error_code
krb5int_arcfour_decrypt(const struct krb5_enc_provider *enc,
                        const struct krb5_hash_provider *hash,
                        krb5_key key, krb5_keyusage usage,
                        const krb5_data *ivec, const krb5_data *input,
                        krb5_data *output)
{
    krb5_keyblock *usage_keyblock = NULL, *enc_keyblock = NULL;
    krb5_data plaintext = empty_data(), comp_checksum = empty_data();
    krb5_data checksum, ciphertext;
    krb5_key enc_key;
    krb5_error_code ret;

    /* Set up subsets of input. */
    checksum = make_data(input->data, hash->hashsize);
    ciphertext = make_data(input->data + hash->hashsize,
                           input->length - hash->hashsize);

    /* Allocate buffers. */
    ret = alloc_data(&plaintext, ciphertext.length);
    if (ret != 0)
        goto cleanup;
    ret = alloc_data(&comp_checksum, hash->hashsize);
    if (ret != 0)
        goto cleanup;
    ret = krb5int_c_init_keyblock(NULL, key->keyblock.enctype, enc->keybytes,
                                  &usage_keyblock);
    if (ret != 0)
        goto cleanup;
    ret = krb5int_c_init_keyblock(NULL, key->keyblock.enctype, enc->keybytes,
                                  &enc_keyblock);
    if (ret != 0)
        goto cleanup;

    /* We may have to try two usage values; see below. */
    do {
        /* Derive a usage key from the session key and usage. */
        ret = krb5int_arcfour_usage_key(enc, hash, &key->keyblock, usage,
                                        usage_keyblock);
        if (ret != 0)
            goto cleanup;

        /* Derive the encryption key from the usage key and checksum. */
        ret = krb5int_arcfour_enc_key(enc, hash, usage_keyblock, &checksum,
                                      enc_keyblock);
        if (ret)
            goto cleanup;

        /* Decrypt the ciphertext. */
        ret = krb5_k_create_key(NULL, enc_keyblock, &enc_key);
        if (ret)
            goto cleanup;
        ret = (*enc->decrypt)(enc_key, ivec, &ciphertext, &plaintext);
        krb5_k_free_key(NULL, enc_key);
        if (ret)
            goto cleanup;

        /* Compute HMAC(usage key, plaintext) to get the checksum. */
        ret = krb5int_hmac_keyblock(hash, usage_keyblock, 1, &plaintext,
                                    &comp_checksum);
        if (ret)
            goto cleanup;

        if (memcmp(checksum.data, comp_checksum.data, hash->hashsize) != 0) {
            if (usage == 9) {
                /*
                 * RFC 4757 specifies usage 8 for TGS-REP encrypted
                 * parts encrypted in a subkey, but the value used by MS
                 * is actually 9.  We now use 9 to start with, but fall
                 * back to 8 on failure in case we are communicating
                 * with a KDC using the value from the RFC.
                 */
                usage = 8;
                continue;
            }
            ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
            goto cleanup;
        }

        break;
    } while (1);

    /* Remove the confounder from the plaintext to get the output. */
    memcpy(output->data, plaintext.data + CONFOUNDERLENGTH,
           plaintext.length - CONFOUNDERLENGTH);
    output->length = plaintext.length - CONFOUNDERLENGTH;

cleanup:
    krb5int_c_free_keyblock(NULL, usage_keyblock);
    krb5int_c_free_keyblock(NULL, enc_keyblock);
    zapfree(plaintext.data, plaintext.length);
    zapfree(comp_checksum.data, comp_checksum.length);
    return ret;
}