aboutsummaryrefslogtreecommitdiff
path: root/src/lib/crypto/builtin/des/f_cbc.c
blob: c7e1f22d5bc85addeb646e4b1ce8ff8492e02c7e (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
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 * lib/crypto/des/f_cbc.c
 *
 * Copyright (C) 1990 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.
 *
 * DES implementation donated by Dennis Ferguson
 */

/*
 * des_cbc_encrypt.c - an implementation of the DES cipher function in cbc mode
 */
#include "des_int.h"
#include "f_tables.h"

/*
 * des_cbc_encrypt - {en,de}crypt a stream in CBC mode
 */

/*
 * This routine performs DES cipher-block-chaining operation, either
 * encrypting from cleartext to ciphertext, if encrypt != 0 or
 * decrypting from ciphertext to cleartext, if encrypt == 0.
 *
 * The key schedule is passed as an arg, as well as the cleartext or
 * ciphertext.  The cleartext and ciphertext should be in host order.
 *
 * NOTE-- the output is ALWAYS an multiple of 8 bytes long.  If not
 * enough space was provided, your program will get trashed.
 *
 * For encryption, the cleartext string is null padded, at the end, to
 * an integral multiple of eight bytes.
 *
 * For decryption, the ciphertext will be used in integral multiples
 * of 8 bytes, but only the first "length" bytes returned into the
 * cleartext.
 */

const mit_des_cblock mit_des_zeroblock /* = all zero */;

#undef mit_des_cbc_encrypt
int
mit_des_cbc_encrypt(const mit_des_cblock *in, mit_des_cblock *out,
                    unsigned long length, const mit_des_key_schedule schedule,
                    const mit_des_cblock ivec, int enc)
{
    /*
     * Deal with encryption and decryption separately.
     */
    if (enc)
        krb5int_des_cbc_encrypt(in, out, length, schedule, ivec);
    else
        krb5int_des_cbc_decrypt(in, out, length, schedule, ivec);
    return 0;
}

void
krb5int_des_cbc_encrypt(const mit_des_cblock *in,
                        mit_des_cblock *out,
                        unsigned long length,
                        const mit_des_key_schedule schedule,
                        const mit_des_cblock ivec)
{
    unsigned DES_INT32 left, right;
    const unsigned DES_INT32 *kp;
    const unsigned char *ip;
    unsigned char *op;

    /*
     * Get key pointer here.  This won't need to be reinitialized
     */
    kp = (const unsigned DES_INT32 *)schedule;

    /*
     * Initialize left and right with the contents of the initial
     * vector.
     */
    ip = ivec;
    GET_HALF_BLOCK(left, ip);
    GET_HALF_BLOCK(right, ip);

    /*
     * Suitably initialized, now work the length down 8 bytes
     * at a time.
     */
    ip = *in;
    op = *out;
    while (length > 0) {
        /*
         * Get more input, xor it in.  If the length is
         * greater than or equal to 8 this is straight
         * forward.  Otherwise we have to fart around.
         */
        if (length >= 8) {
            unsigned DES_INT32 temp;
            GET_HALF_BLOCK(temp, ip);
            left  ^= temp;
            GET_HALF_BLOCK(temp, ip);
            right ^= temp;
            length -= 8;
        } else {
            /*
             * Oh, shoot.  We need to pad the
             * end with zeroes.  Work backwards
             * to do this.
             */
            ip += (int) length;
            switch(length) {
            case 7:
                right ^= (*(--ip) & FF_UINT32) <<  8;
            case 6:
                right ^= (*(--ip) & FF_UINT32) << 16;
            case 5:
                right ^= (*(--ip) & FF_UINT32) << 24;
            case 4:
                left  ^=  *(--ip) & FF_UINT32;
            case 3:
                left  ^= (*(--ip) & FF_UINT32) <<  8;
            case 2:
                left  ^= (*(--ip) & FF_UINT32) << 16;
            case 1:
                left  ^= (*(--ip) & FF_UINT32) << 24;
                break;
            }
            length = 0;
        }

        /*
         * Encrypt what we have
         */
        DES_DO_ENCRYPT(left, right, kp);

        /*
         * Copy the results out
         */
        PUT_HALF_BLOCK(left, op);
        PUT_HALF_BLOCK(right, op);
    }
}

void
krb5int_des_cbc_decrypt(const mit_des_cblock *in,
                        mit_des_cblock *out,
                        unsigned long length,
                        const mit_des_key_schedule schedule,
                        const mit_des_cblock ivec)
{
    unsigned DES_INT32 left, right;
    const unsigned DES_INT32 *kp;
    const unsigned char *ip;
    unsigned char *op;
    unsigned DES_INT32 ocipherl, ocipherr;
    unsigned DES_INT32 cipherl, cipherr;

    /*
     * Get key pointer here.  This won't need to be reinitialized
     */
    kp = (const unsigned DES_INT32 *)schedule;

    /*
     * Decrypting is harder than encrypting because of
     * the necessity of remembering a lot more things.
     * Should think about this a little more...
     */

    if (length <= 0)
        return;

    /*
     * Prime the old cipher with ivec.
     */
    ip = ivec;
    GET_HALF_BLOCK(ocipherl, ip);
    GET_HALF_BLOCK(ocipherr, ip);

    /*
     * Now do this in earnest until we run out of length.
     */
    ip = *in;
    op = *out;
    for (;;) {              /* check done inside loop */
        /*
         * Read a block from the input into left and
         * right.  Save this cipher block for later.
         */
        GET_HALF_BLOCK(left, ip);
        GET_HALF_BLOCK(right, ip);
        cipherl = left;
        cipherr = right;

        /*
         * Decrypt this.
         */
        DES_DO_DECRYPT(left, right, kp);

        /*
         * Xor with the old cipher to get plain
         * text.  Output 8 or less bytes of this.
         */
        left ^= ocipherl;
        right ^= ocipherr;
        if (length > 8) {
            length -= 8;
            PUT_HALF_BLOCK(left, op);
            PUT_HALF_BLOCK(right, op);
            /*
             * Save current cipher block here
             */
            ocipherl = cipherl;
            ocipherr = cipherr;
        } else {
            /*
             * Trouble here.  Start at end of output,
             * work backwards.
             */
            op += (int) length;
            switch(length) {
            case 8:
                *(--op) = (unsigned char) (right & 0xff);
            case 7:
                *(--op) = (unsigned char) ((right >> 8) & 0xff);
            case 6:
                *(--op) = (unsigned char) ((right >> 16) & 0xff);
            case 5:
                *(--op) = (unsigned char) ((right >> 24) & 0xff);
            case 4:
                *(--op) = (unsigned char) (left & 0xff);
            case 3:
                *(--op) = (unsigned char) ((left >> 8) & 0xff);
            case 2:
                *(--op) = (unsigned char) ((left >> 16) & 0xff);
            case 1:
                *(--op) = (unsigned char) ((left >> 24) & 0xff);
                break;
            }
            break;          /* we're done */
        }
    }
}

#if defined(CONFIG_SMALL) && !defined(CONFIG_SMALL_NO_CRYPTO)
void krb5int_des_do_encrypt_2 (unsigned DES_INT32 *left,
                               unsigned DES_INT32 *right,
                               const unsigned DES_INT32 *kp)
{
    DES_DO_ENCRYPT_1 (*left, *right, kp);
}

void krb5int_des_do_decrypt_2 (unsigned DES_INT32 *left,
                               unsigned DES_INT32 *right,
                               const unsigned DES_INT32 *kp)
{
    DES_DO_DECRYPT_1 (*left, *right, kp);
}
#endif