summaryrefslogtreecommitdiffstats
path: root/src/lib/crypto/des/krb_glue.c
blob: 188e95dffbe2aa35d89333072073dc17c100a36a (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
/*
 * $Source$
 * $Author$
 *
 * Copyright 1985, 1986, 1987, 1988, 1990, 1991 by the Massachusetts Institute
 * of Technology.
 * All Rights Reserved.
 *
 * These routines perform encryption and decryption using the DES
 * private key algorithm, or else a subset of it -- fewer inner loops.
 * (AUTH_DES_ITER defaults to 16, may be less.)
 *
 * Under U.S. law, this software may not be exported outside the US
 * without license from the U.S. Commerce department.
 *
 * These routines form the library interface to the DES facilities.
 *
 * Originally written 8/85 by Steve Miller, MIT Project Athena.
 *
 * Export of this software from the United States of America is assumed
 *   to 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.  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.
 * 
 *
 */

/*
 * These routines were extracted out of enc_dec.c because they will
 * drag in the kerberos library, if someone references mit_des_cbc_encrypt,
 * even no kerberos routines are called
 */

#if !defined(lint) && !defined(SABER)
static char rcsid_enc_dec_c[] =
"$Id$";
#endif	/* !lint & !SABER */


#include <krb5/krb5.h>
#include <krb5/ext-proto.h>
#include <krb5/los-proto.h>
#include <krb5/crc-32.h>

#include "des_int.h"

#ifdef DEBUG
#include <stdio.h>

extern int mit_des_debug;
#endif

/*
	encrypts "size" bytes at "in", storing result in "out".
	"eblock" points to an encrypt block which has been initialized
	by process_key().

	"out" must be preallocated by the caller to contain sufficient
	storage to hold the output; the macro krb5_encrypt_size() can
	be used to compute this size.
	
	returns: errors
*/
static krb5_error_code
mit_des_encrypt_f(DECLARG(krb5_const_pointer, in),
		  DECLARG(krb5_pointer, out),
		  DECLARG(const size_t, size),
		  DECLARG(krb5_encrypt_block *, key),
		  DECLARG(krb5_pointer, ivec))
OLDDECLARG(krb5_const_pointer, in)
OLDDECLARG(krb5_pointer, out)
OLDDECLARG(const size_t, size)
OLDDECLARG(krb5_encrypt_block *, key)
OLDDECLARG(krb5_pointer, ivec)
{
    krb5_octet	*iv;
    
    if ( ivec == 0 )
	iv = key->key->contents;
    else
	iv = (krb5_octet *)ivec;

    return (mit_des_cbc_encrypt((krb5_octet *)in, 
			    (krb5_octet *)out,
			    size, 
			    (struct mit_des_ks_struct *)key->priv, 
			    iv,
			    MIT_DES_ENCRYPT));
}    


/*

	decrypts "size" bytes at "in", storing result in "out".
	"eblock" points to an encrypt block which has been initialized
	by process_key().

	"out" must be preallocated by the caller to contain sufficient
	storage to hold the output; this is guaranteed to be no more than
	the input size.

	returns: errors

 */
static krb5_error_code
mit_des_decrypt_f(DECLARG(krb5_const_pointer, in),
		  DECLARG(krb5_pointer, out),
		  DECLARG(const size_t, size),
		  DECLARG(krb5_encrypt_block *, key),
		  DECLARG(krb5_pointer, ivec))
OLDDECLARG(krb5_const_pointer, in)
OLDDECLARG(krb5_pointer, out)
OLDDECLARG(const size_t, size)
OLDDECLARG(krb5_encrypt_block *, key)
OLDDECLARG(krb5_pointer, ivec)
{
    krb5_octet	*iv;

    if ( ivec == 0 )
	iv = key->key->contents;
    else
	iv = (krb5_octet *)ivec;

    return (mit_des_cbc_encrypt ((krb5_octet *)in, 
			     (krb5_octet *)out, 
			     size, 
			     (struct mit_des_ks_struct *)key->priv, 
			     iv,
			     MIT_DES_DECRYPT));
}    

krb5_error_code mit_des_encrypt_func(DECLARG(krb5_const_pointer, in),
				     DECLARG(krb5_pointer, out),
				     DECLARG(const size_t, size),
				     DECLARG(krb5_encrypt_block *, key),
				     DECLARG(krb5_pointer, ivec))
OLDDECLARG(krb5_const_pointer, in)
OLDDECLARG(krb5_pointer, out)
OLDDECLARG(const size_t, size)
OLDDECLARG(krb5_encrypt_block *, key)
OLDDECLARG(krb5_pointer, ivec)
{
    krb5_checksum cksum;
    krb5_octet 	contents[CRC32_CKSUM_LENGTH];
    int sumsize;
    krb5_error_code retval;

/*    if ( size < sizeof(mit_des_cblock) )
	return KRB5_BAD_MSIZE; */

    /* caller passes data size, and saves room for the padding. */
    /* format of ciphertext, per RFC is:
      +-----------+----------+-------------+-----+
      |confounder |   check  |   msg-seq   | pad |
      +-----------+----------+-------------+-----+
      
      our confounder is 8 bytes (one cblock);
      our checksum is CRC32_CKSUM_LENGTH
     */
    sumsize =  krb5_roundup(size+CRC32_CKSUM_LENGTH+sizeof(mit_des_cblock),
			    sizeof(mit_des_cblock));

    /* assemble crypto input into the output area, then encrypt in place. */

    memset((char *)out, 0, sumsize);

    /* put in the confounder */
    if (retval = krb5_random_confounder(sizeof(mit_des_cblock), out))
	return retval;

    memcpy((char *)out+sizeof(mit_des_cblock)+CRC32_CKSUM_LENGTH, (char *)in,
	   size);

    cksum.contents = contents; 

    if (retval = krb5_calculate_checksum(CKSUMTYPE_CRC32,
					 (krb5_pointer) out,
					 sumsize,
					 (krb5_pointer)key->key->contents,
					 sizeof(mit_des_cblock),
					 &cksum)) 
	return retval;

    memcpy((char *)out+sizeof(mit_des_cblock), (char *)contents,
	   CRC32_CKSUM_LENGTH);

    /* We depend here on the ability of this DES implementation to
       encrypt plaintext to ciphertext in-place. */
    return (mit_des_encrypt_f(out, out, sumsize, key, ivec));
}

krb5_error_code mit_des_decrypt_func(DECLARG(krb5_const_pointer, in),
				     DECLARG(krb5_pointer, out),
				     DECLARG(const size_t, size),
				     DECLARG(krb5_encrypt_block *, key),
				     DECLARG(krb5_pointer, ivec))
OLDDECLARG(krb5_const_pointer, in)
OLDDECLARG(krb5_pointer, out)
OLDDECLARG(const size_t, size)
OLDDECLARG(krb5_encrypt_block *, key)
OLDDECLARG(krb5_pointer, ivec)
{
    krb5_checksum cksum;
    krb5_octet 	contents_prd[CRC32_CKSUM_LENGTH];
    krb5_octet  contents_get[CRC32_CKSUM_LENGTH];
    char 	*p;
    krb5_error_code   retval;

    if ( size < 2*sizeof(mit_des_cblock) )
	return KRB5_BAD_MSIZE;

    if (retval = mit_des_decrypt_f(in, out, size, key, ivec))
	return retval;

    cksum.contents = contents_prd;
    p = (char *)out + sizeof(mit_des_cblock);
    memcpy((char *)contents_get, p, CRC32_CKSUM_LENGTH);
    memset(p, 0, CRC32_CKSUM_LENGTH);

    if (retval = krb5_calculate_checksum(CKSUMTYPE_CRC32,
					 out,
					 size,
					 (krb5_pointer)key->key->contents,
					 sizeof(mit_des_cblock),
					 &cksum)) 
	return retval;

    if (memcmp((char *)contents_get, (char *)contents_prd, CRC32_CKSUM_LENGTH) )
        return KRB5KRB_AP_ERR_BAD_INTEGRITY;
    memcpy((char *)out, (char *)out +
	   sizeof(mit_des_cblock) + CRC32_CKSUM_LENGTH,
	   size - sizeof(mit_des_cblock) + CRC32_CKSUM_LENGTH);
    return 0;
}