xref: /freebsd/crypto/krb5/src/lib/crypto/builtin/des/des_int.h (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/builtin/des/des_int.h */
3 /*
4  * Copyright 1987, 1988, 1990, 2002 by the Massachusetts Institute of
5  * Technology.  All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 /*
27  * Copyright (C) 1998 by the FundsXpress, INC.
28  *
29  * All rights reserved.
30  *
31  * Export of this software from the United States of America may require
32  * a specific license from the United States Government.  It is the
33  * responsibility of any person or organization contemplating export to
34  * obtain such a license before exporting.
35  *
36  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
37  * distribute this software and its documentation for any purpose and
38  * without fee is hereby granted, provided that the above copyright
39  * notice appear in all copies and that both that copyright notice and
40  * this permission notice appear in supporting documentation, and that
41  * the name of FundsXpress. not be used in advertising or publicity pertaining
42  * to distribution of the software without specific, written prior
43  * permission.  FundsXpress makes no representations about the suitability of
44  * this software for any purpose.  It is provided "as is" without express
45  * or implied warranty.
46  *
47  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
50  */
51 
52 /* Private include file for the Data Encryption Standard library. */
53 
54 /* only do the whole thing once  */
55 #ifndef DES_INTERNAL_DEFS
56 #define DES_INTERNAL_DEFS
57 
58 #include "k5-int.h"
59 /*
60  * Begin "mit-des.h"
61  */
62 #ifndef KRB5_MIT_DES__
63 #define KRB5_MIT_DES__
64 
65 #if defined(__MACH__) && defined(__APPLE__)
66 #include <TargetConditionals.h>
67 #include <AvailabilityMacros.h>
68 #if TARGET_RT_MAC_CFM
69 #error "Use KfM 4.0 SDK headers for CFM compilation."
70 #endif
71 #if defined(DEPRECATED_IN_MAC_OS_X_VERSION_10_5) && !defined(KRB5_SUPRESS_DEPRECATED_WARNINGS)
72 #define KRB5INT_DES_DEPRECATED DEPRECATED_IN_MAC_OS_X_VERSION_10_5
73 #endif
74 #endif /* defined(__MACH__) && defined(__APPLE__) */
75 
76 /* Macro to add deprecated attribute to DES types and functions */
77 /* Currently only defined on macOS 10.5 and later.              */
78 #ifndef KRB5INT_DES_DEPRECATED
79 #define KRB5INT_DES_DEPRECATED
80 #endif
81 
82 #include <limits.h>
83 
84 #if UINT_MAX >= 0xFFFFFFFFUL
85 #define DES_INT32 int
86 #define DES_UINT32 unsigned int
87 #else
88 #define DES_INT32 long
89 #define DES_UINT32 unsigned long
90 #endif
91 
92 typedef unsigned char des_cblock[8]     /* crypto-block size */
93 KRB5INT_DES_DEPRECATED;
94 
95 /*
96  * Key schedule.
97  *
98  * This used to be
99  *
100  * typedef struct des_ks_struct {
101  *     union { DES_INT32 pad; des_cblock _;} __;
102  * } des_key_schedule[16];
103  *
104  * but it would cause trouble if DES_INT32 were ever more than 4
105  * bytes.  The reason is that all the encryption functions cast it to
106  * (DES_INT32 *), and treat it as if it were DES_INT32[32].  If
107  * 2*sizeof(DES_INT32) is ever more than sizeof(des_cblock), the
108  * caller-allocated des_key_schedule will be overflowed by the key
109  * scheduling functions.  We can't assume that every platform will
110  * have an exact 32-bit int, and nothing should be looking inside a
111  * des_key_schedule anyway.
112  */
113 typedef struct des_ks_struct {  DES_INT32 _[2]; } des_key_schedule[16]
114 KRB5INT_DES_DEPRECATED;
115 
116 typedef des_cblock mit_des_cblock;
117 typedef des_key_schedule mit_des_key_schedule;
118 
119 /* Triple-DES structures */
120 typedef mit_des_cblock          mit_des3_cblock[3];
121 typedef mit_des_key_schedule    mit_des3_key_schedule[3];
122 
123 #define MIT_DES_ENCRYPT 1
124 #define MIT_DES_DECRYPT 0
125 
126 typedef struct mit_des_ran_key_seed {
127     krb5_encrypt_block eblock;
128     krb5_data sequence;
129 } mit_des_random_state;
130 
131 /* the first byte of the key is already in the keyblock */
132 
133 #define MIT_DES_BLOCK_LENGTH            (8*sizeof(krb5_octet))
134 /* This used to be 8*sizeof(krb5_octet) */
135 #define MIT_DES_KEYSIZE                 8
136 
137 #define MIT_DES_CBC_CKSUM_LENGTH        (4*sizeof(krb5_octet))
138 
139 #endif /* KRB5_MIT_DES__ */
140 /*
141  * End "mit-des.h"
142  */
143 
144 /* afsstring2key.c */
145 krb5_error_code mit_afs_string_to_key(krb5_keyblock *keyblock,
146                                       const krb5_data *data,
147                                       const krb5_data *salt);
148 char *mit_afs_crypt(const char *pw, const char *salt, char *iobuf);
149 
150 /* f_cksum.c */
151 unsigned long mit_des_cbc_cksum(const krb5_octet *, krb5_octet *,
152                                 unsigned long, const mit_des_key_schedule,
153                                 const krb5_octet *);
154 
155 /* f_cbc.c (used by test programs) */
156 int
157 mit_des_cbc_encrypt(const mit_des_cblock *in, mit_des_cblock *out,
158                     unsigned long length, const mit_des_key_schedule schedule,
159                     const mit_des_cblock ivec, int enc);
160 
161 #define mit_des_zeroblock krb5int_c_mit_des_zeroblock
162 extern const mit_des_cblock mit_des_zeroblock;
163 
164 /* fin_rndkey.c */
165 krb5_error_code mit_des_finish_random_key(const krb5_encrypt_block *,
166                                           krb5_pointer *);
167 
168 /* finish_key.c */
169 krb5_error_code mit_des_finish_key(krb5_encrypt_block *);
170 
171 /* init_rkey.c */
172 krb5_error_code mit_des_init_random_key(const krb5_encrypt_block *,
173                                         const krb5_keyblock *,
174                                         krb5_pointer *);
175 
176 /* key_parity.c */
177 void mit_des_fixup_key_parity(mit_des_cblock);
178 int mit_des_check_key_parity(mit_des_cblock);
179 
180 /* key_sched.c */
181 int mit_des_key_sched(mit_des_cblock, mit_des_key_schedule);
182 
183 /* process_ky.c */
184 krb5_error_code mit_des_process_key(krb5_encrypt_block *,
185                                     const krb5_keyblock *);
186 
187 /* random_key.c */
188 krb5_error_code mit_des_random_key(const krb5_encrypt_block *,
189                                    krb5_pointer, krb5_keyblock **);
190 
191 /* string2key.c */
192 krb5_error_code mit_des_string_to_key(const krb5_encrypt_block *,
193                                       krb5_keyblock *, const krb5_data *,
194                                       const krb5_data *);
195 krb5_error_code mit_des_string_to_key_int(krb5_keyblock *, const krb5_data *,
196                                           const krb5_data *);
197 
198 /* weak_key.c */
199 int mit_des_is_weak_key(mit_des_cblock);
200 
201 /* cmb_keys.c */
202 krb5_error_code mit_des_combine_subkeys(const krb5_keyblock *,
203                                         const krb5_keyblock *,
204                                         krb5_keyblock **);
205 
206 /* f_sched.c */
207 int mit_des_make_key_sched(mit_des_cblock, mit_des_key_schedule);
208 
209 
210 /* misc.c */
211 extern void swap_bits(char *);
212 extern unsigned long long_swap_bits(unsigned long);
213 extern unsigned long swap_six_bits_to_ansi(unsigned long);
214 extern unsigned long swap_four_bits_to_ansi(unsigned long);
215 extern unsigned long swap_bit_pos_1(unsigned long);
216 extern unsigned long swap_bit_pos_0(unsigned long);
217 extern unsigned long swap_bit_pos_0_to_ansi(unsigned long);
218 extern unsigned long rev_swap_bit_pos_0(unsigned long);
219 extern unsigned long swap_byte_bits(unsigned long);
220 extern unsigned long swap_long_bytes_bit_number(unsigned long);
221 #ifdef FILE
222 /* XXX depends on FILE being a #define! */
223 extern void test_set(FILE *, const char *, int, const char *, int);
224 #endif
225 
226 void
227 krb5int_des3_cbc_encrypt(krb5_crypto_iov *data, unsigned long num_data,
228                          const mit_des_key_schedule ks1,
229                          const mit_des_key_schedule ks2,
230                          const mit_des_key_schedule ks3,
231                          mit_des_cblock ivec);
232 
233 void
234 krb5int_des3_cbc_decrypt(krb5_crypto_iov *data, unsigned long num_data,
235                          const mit_des_key_schedule ks1,
236                          const mit_des_key_schedule ks2,
237                          const mit_des_key_schedule ks3,
238                          mit_des_cblock ivec);
239 
240 void
241 krb5int_des_cbc_encrypt(krb5_crypto_iov *data, unsigned long num_data,
242                         const mit_des_key_schedule schedule,
243                         mit_des_cblock ivec);
244 
245 void
246 krb5int_des_cbc_decrypt(krb5_crypto_iov *data, unsigned long num_data,
247                         const mit_des_key_schedule schedule,
248                         mit_des_cblock ivec);
249 
250 void
251 krb5int_des_cbc_mac(const krb5_crypto_iov *data, unsigned long num_data,
252                     const mit_des_key_schedule schedule, mit_des_cblock ivec,
253                     mit_des_cblock out);
254 
255 /* d3_procky.c */
256 krb5_error_code mit_des3_process_key(krb5_encrypt_block *eblock,
257                                      const krb5_keyblock *keyblock);
258 
259 /* d3_kysched.c */
260 int mit_des3_key_sched(mit_des3_cblock key, mit_des3_key_schedule schedule);
261 
262 /* d3_str2ky.c */
263 krb5_error_code mit_des3_string_to_key(const krb5_encrypt_block *eblock,
264                                        krb5_keyblock *keyblock,
265                                        const krb5_data *data,
266                                        const krb5_data *salt);
267 
268 /* u_nfold.c */
269 krb5_error_code mit_des_n_fold(const krb5_octet *input, const size_t in_len,
270                                krb5_octet *output, const size_t out_len);
271 
272 /* u_rn_key.c */
273 int mit_des_is_weak_keyblock(krb5_keyblock *keyblock);
274 
275 void mit_des_fixup_keyblock_parity(krb5_keyblock *keyblock);
276 
277 krb5_error_code mit_des_set_random_generator_seed(const krb5_data *seed,
278                                                   krb5_pointer random_state);
279 
280 krb5_error_code mit_des_set_random_sequence_number(const krb5_data *sequence,
281                                                    krb5_pointer random_state);
282 #endif  /*DES_INTERNAL_DEFS*/
283