Namespace-clean some more heimdal symbols
[openafs.git] / src / crypto / rfc3961 / krb5_locl.h
1 /* This is a shim header that's included by crypto.c, and turns it into
2  * something that we can actually build on its own.
3  */
4
5 #ifdef KERNEL
6
7 #include "config.h"
8
9 #else
10 #include <afsconfig.h>
11 #include <afs/stds.h>
12 #include <roken.h>
13
14 #include <fcntl.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #ifndef AFS_NT40_ENV
18 #include <sys/param.h>
19 #include <inttypes.h>
20 #include <sys/errno.h>
21 #endif
22 #include <sys/types.h>
23
24 #endif
25
26 #include <hcrypto/evp.h>
27 #include <hcrypto/des.h>
28 #include <hcrypto/rc4.h>
29 #include <hcrypto/sha.h>
30 #include <hcrypto/md5.h>
31
32 #include "rfc3961.h"
33
34 #ifndef KERNEL
35 #ifdef AFS_PTHREAD_ENV
36 #include <pthread.h>
37 # define HEIMDAL_MUTEX pthread_mutex_t
38 # define HEIMDAL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
39 # define HEIMDAL_MUTEX_init(m) pthread_mutex_init(m, NULL)
40 # define HEIMDAL_MUTEX_lock(m) pthread_mutex_lock(m)
41 # define HEIMDAL_MUTEX_unlock(m) pthread_mutex_unlock(m)
42 # define HEIMDAL_MUTEX_destroy(m) pthread_mutex_destroy(m)
43 #else
44 /* The one location in this library which uses mutexes is the PRNG
45  * code. As this code takes no locks, never yields, and does no
46  * I/O through the LWP IO Manager, it cannot be pre-empted, so
47  * it is safe to simply remove the locks in this case
48  */
49 #define HEIMDAL_MUTEX int
50 #define HEIMDAL_MUTEX_INITIALIZER 0
51 #define HEIMDAL_MUTEX_init(m) do { (void)(m); } while(0)
52 #define HEIMDAL_MUTEX_lock(m) do { (void)(m); } while(0)
53 #define HEIMDAL_MUTEX_unlock(m) do { (void)(m); } while(0)
54 #define HEIMDAL_MUTEX_destroy(m) do { (void)(m); } while(0)
55 #endif
56 #endif
57
58 #define HEIMDAL_SMALLER 1
59 #define HEIM_CRYPTO_NO_TRIPLE_DES
60 #define HEIM_CRYPTO_NO_ARCFOUR
61 #define HEIM_CRYPTO_NO_PK
62
63 #define NO_RAND_EGD_METHOD
64 #define NO_RANDFILE
65
66 #define ALLOC(X, N) (X) = calloc((N), sizeof(*(X)))
67
68 #ifndef max
69 #define max(a,b) (((a)>(b))?(a):(b))
70 #endif
71
72 #ifndef O_BINARY
73 #define O_BINARY 0
74 #endif
75
76 #ifndef O_CLOEXEC
77 #define O_CLOEXEC 0
78 #endif
79
80 typedef int krb5_boolean;
81 typedef ssize_t krb5_ssize_t;
82
83 #define KRB5_KU_AS_REP_ENC_PART 3
84 #define KRB5_KU_USAGE_SEAL 22
85 #define KRB5_KU_USAGE_SIGN 23
86 #define KRB5_KU_USAGE_SEQ 24
87
88 #define TRUE 1
89 #define FALSE 0
90
91 /* From the ASN.1 */
92
93 typedef struct EncryptedData {
94   int etype;
95   int *kvno;
96   afs_heim_octet_string cipher;
97 } EncryptedData;
98
99 typedef enum krb5_salttype {
100     KRB5_PW_SALT = 3,
101     KRB5_AFS3_SALT = 10
102 } krb5_salttype;
103
104 typedef enum krb5_keytype {
105     KEYTYPE_NULL        = 0,
106     KEYTYPE_DES         = 1,
107     KEYTYPE_DES3        = 7,
108     KEYTYPE_AES128      = 17,
109     KEYTYPE_AES256      = 18,
110     KEYTYPE_ARCFOUR     = 23,
111     KEYTYPE_ARCFOUR_56  = 24
112 } krb5_keytype;
113
114 #define KRB5_ENCTYPE_NULL ETYPE_NULL
115 #define KRB5_ENCTYPE_OLD_DES3_CBC_SHA1 ETYPE_OLD_DES3_CBC_SHA1
116 #define KRB5_ENCTYPE_AES128_CTS_HMAC_SHA1_96 ETYPE_AES128_CTS_HMAC_SHA1_96
117 #define KRB5_ENCTYPE_AES256_CTS_HMAC_SHA1_96 ETYPE_AES256_CTS_HMAC_SHA1_96
118 #define KRB5_ENCTYPE_ARCFOUR_HMAC_MD5 ETYPE_ARCFOUR_HMAC_MD5
119
120 typedef struct krb5_salt {
121     krb5_salttype salttype;
122     krb5_data saltvalue;
123 } krb5_salt;
124
125 typedef struct krb5_crypto_iov {
126     unsigned int flags;
127     /* ignored */
128 #define KRB5_CRYPTO_TYPE_EMPTY          0
129     /* OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_HEADER) */
130 #define KRB5_CRYPTO_TYPE_HEADER         1
131     /* IN and OUT */
132 #define KRB5_CRYPTO_TYPE_DATA           2
133     /* IN */
134 #define KRB5_CRYPTO_TYPE_SIGN_ONLY      3
135    /* (only for encryption) OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_TRAILER) */
136 #define KRB5_CRYPTO_TYPE_PADDING        4
137    /* OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_TRAILER) */
138 #define KRB5_CRYPTO_TYPE_TRAILER        5
139    /* OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_CHECKSUM) */
140 #define KRB5_CRYPTO_TYPE_CHECKSUM       6
141     krb5_data data;
142 } krb5_crypto_iov;
143
144 #define ETYPE_NULL 0
145
146 #define KRB5_LIB_FUNCTION
147 #define KRB5_LIB_CALL
148
149 /* Error codes */
150 #define KRB5_BAD_MSIZE -1765328194
151 #define KRB5_BAD_KEYSIZE -1765328195
152 #define KRB5_PROG_SUMTYPE_NOSUPP -1765328231
153 #define KRB5_PROG_KEYTYPE_NOSUPP -1765328233
154 #define KRB5_PROG_ETYPE_NOSUPP -1765328234
155 #define HEIM_ERR_SALTTYPE_NOSUPP -1980176638
156 #define KRB5KRB_AP_ERR_BAD_INTEGRITY -1765328353
157
158 #define KRB5_CRYPTO_INTERNAL 1
159
160 /* Currently, we just disable localised error strings. We'll get the error
161  * numbers out, but no meaningful text */
162 #define N_(X, Y) X
163
164 /* rename internal symbols, to reduce conflicts with external kerberos
165    libraries */
166 #define krb5_abortx _oafs_h_krb5_abortx
167 #define krb5_set_error_message _oafs_h_krb5_set_error_message
168 #define copy_EncryptionKey _oafs_h_copy_EncryptionKey
169 #define der_copy_octet_string _oafs_h_der_copy_octet_string
170 #define _krb5_HMAC_MD5_checksum _oafs_h__krb5_HMAC_MD5_checksum
171 #define _krb5_usage2arcfour _oafs_h__krb5_usage2arcfour
172 #define _krb5_SP_HMAC_SHA1_checksum _oafs_h__krb5_SP_HMAC_SHA1_checksum
173 #define _krb5_derive_key _oafs_h__krb5_derive_key
174 #define _krb5_find_checksum _oafs_h__krb5_find_checksum
175 #define _krb5_find_enctype _oafs_h__krb5_find_enctype
176 #define _krb5_free_key_data _oafs_h__krb5_free_key_data
177 #define _krb5_internal_hmac _oafs_h__krb5_internal_hmac
178 #define krb5_allow_weak_crypto _oafs_h_krb5_allow_weak_crypto
179 #define krb5_checksum_disable _oafs_h_krb5_checksum_disable
180 #define krb5_checksum_is_collision_proof _oafs_h_krb5_checksum_is_collision_proof
181 #define krb5_checksum_is_keyed _oafs_h_krb5_checksum_is_keyed
182 #define _krb5_checksum_hmac_md5 _oafs_h__krb5_checksum_hmac_md5
183 #define _krb5_checksum_hmac_sha1_des3 _oafs_h__krb5_checksum_hmac_sha1_des3
184 #define _krb5_checksum_rsa_md5 _oafs_h__krb5_checksum_rsa_md5
185 #define _krb5_checksum_sha1 _oafs_h__krb5_checksum_sha1
186 #define _krb5_checksum_sha1_des3 _oafs_h__krb5_checksum_sha1_des3
187 #define krb5_cksumtype_to_enctype _oafs_h_krb5_cksumtype_to_enctype
188 #define krb5_cksumtype_valid _oafs_h_krb5_cksumtype_valid
189 #define krb5_create_checksum_iov _oafs_h_krb5_create_checksum_iov
190 #define krb5_crypto_getblocksize _oafs_h_krb5_crypto_getblocksize
191 #define krb5_crypto_getconfoundersize _oafs_h_krb5_crypto_getconfoundersize
192 #define krb5_crypto_getenctype _oafs_h_krb5_crypto_getenctype
193 #define krb5_crypto_getpadsize _oafs_h_krb5_crypto_getpadsize
194 #define krb5_crypto_length _oafs_h_krb5_crypto_length
195 #define krb5_crypto_length_iov _oafs_h_krb5_crypto_length_iov
196 #define krb5_crypto_prf_length _oafs_h_krb5_crypto_prf_length
197 #define krb5_decrypt_EncryptedData _oafs_h_krb5_decrypt_EncryptedData
198 #define krb5_decrypt_iov_ivec _oafs_h_krb5_decrypt_iov_ivec
199 #define krb5_decrypt_ivec _oafs_h_krb5_decrypt_ivec
200 #define krb5_derive_key _oafs_h_krb5_derive_key
201 #define krb5_encrypt_EncryptedData _oafs_h_krb5_encrypt_EncryptedData
202 #define krb5_encrypt_iov_ivec _oafs_h_krb5_encrypt_iov_ivec
203 #define krb5_encrypt_ivec _oafs_h_krb5_encrypt_ivec
204 #define _krb5_enctype_des3_cbc_none _oafs_h__krb5_enctype_des3_cbc_none
205 #define _krb5_enctype_des3_cbc_sha1 _oafs_h__krb5_enctype_des3_cbc_sha1
206 #define _krb5_enctype_arcfour_hmac_md5 _oafs_h__krb5_enctype_arcfour_hmac_md5
207 #define krb5_enctype_disable _oafs_h_krb5_enctype_disable
208 #define krb5_enctype_enable _oafs_h_krb5_enctype_enable
209 #define krb5_enctype_keysize _oafs_h_krb5_enctype_keysize
210 #define krb5_enctype_to_keytype _oafs_h_krb5_enctype_to_keytype
211 #define krb5_enctype_to_string _oafs_h_krb5_enctype_to_string
212 #define krb5_generate_random_keyblock _oafs_h_krb5_generate_random_keyblock
213 #define krb5_get_wrapped_length _oafs_h_krb5_get_wrapped_length
214 #define krb5_hmac _oafs_h_krb5_hmac
215 #define krb5_is_enctype_weak _oafs_h_krb5_is_enctype_weak
216 #define krb5_string_to_enctype _oafs_h_krb5_string_to_enctype
217 #define krb5_verify_checksum_iov _oafs_h_krb5_verify_checksum_iov
218 #define _krb5_DES3_random_to_key _oafs_h__krb5_DES3_random_to_key
219 #define _krb5_xor _oafs_h__krb5_xor
220 #define _krb5_evp_cleanup _oafs_h__krb5_evp_cleanup
221 #define _krb5_evp_encrypt _oafs_h__krb5_evp_encrypt
222 #define _krb5_evp_encrypt_cts _oafs_h__krb5_evp_encrypt_cts
223 #define _krb5_evp_schedule _oafs_h__krb5_evp_schedule
224 #define krb5_copy_data _oafs_h_krb5_copy_data
225 #define krb5_data_cmp _oafs_h_krb5_data_cmp
226 #define krb5_data_copy _oafs_h_krb5_data_copy
227 #define krb5_data_ct_cmp _oafs_h_krb5_data_ct_cmp
228 #define krb5_data_realloc _oafs_h_krb5_data_realloc
229 #define krb5_data_zero _oafs_h_krb5_data_zero
230 #define krb5_free_data _oafs_h_krb5_free_data
231 #define _krb5_n_fold _oafs_h__krb5_n_fold
232 #define _krb5_get_int _oafs_h__krb5_get_int
233 #define _krb5_put_int _oafs_h__krb5_put_int
234
235
236 /* These have to be real functions, because IRIX doesn't seem to support
237  * variadic macros */
238 void krb5_set_error_message(krb5_context, krb5_error_code, const char *, ...);
239 krb5_error_code krb5_abortx(krb5_context, const char *, ...);
240
241 #define krb5_clear_error_message(ctx)
242
243 static_inline krb5_error_code
244 krb5_enomem(krb5_context context)
245 {
246     return ENOMEM;
247 }
248
249
250 /* Local prototypes. These are functions that we aren't admitting to in the
251  * public API */
252 krb5_error_code _krb5_n_fold(const void *str, size_t len, void *, size_t);
253 krb5_error_code krb5_derive_key(krb5_context context, const krb5_keyblock *key,
254                                 krb5_enctype etype, const void *constant,
255                                 size_t constant_len,
256                                 krb5_keyblock **derived_key);
257 krb5_error_code krb5_enctype_keysize(krb5_context context,
258                                      krb5_enctype type,
259                                      size_t *keysize);
260 krb5_ssize_t _krb5_put_int(void *buffer, unsigned long value, size_t size);
261 void krb5_data_zero(krb5_data *p);
262 krb5_error_code krb5_data_copy(krb5_data *p, const void *data, size_t len);
263 void krb5_free_data(krb5_context context, krb5_data *p);
264 krb5_error_code krb5_copy_keyblock(krb5_context,
265                                    const krb5_keyblock *,
266                                    krb5_keyblock **);
267 void krb5_free_keyblock(krb5_context, krb5_keyblock *);
268 int krb5_data_ct_cmp(const krb5_data *, const krb5_data *);
269 int der_copy_octet_string(const krb5_data *, krb5_data *);
270 int copy_EncryptionKey(const krb5_keyblock *, krb5_keyblock *);
271 krb5_error_code krb5_enctype_to_string(krb5_context context,
272                                        krb5_enctype etype,
273                                        char **string);
274 #ifdef KERNEL
275 /* Roken provides this in userspace, but we're on our own in the kernel. */
276 int ct_memcmp(const void *p1, const void *p2, size_t len);
277 #endif
278
279
280 #include "crypto.h"
281
282 struct _krb5_checksum_type * _krb5_find_checksum (krb5_cksumtype);
283 struct _krb5_encryption_type * _krb5_find_enctype (krb5_enctype);
284 void _krb5_free_key_data (krb5_context, struct _krb5_key_data *,
285                           struct _krb5_encryption_type *);
286 void _krb5_evp_cleanup (krb5_context, struct _krb5_key_data *);
287
288 krb5_error_code _krb5_evp_encrypt (krb5_context, struct _krb5_key_data *,
289                                    void *, size_t, krb5_boolean, int,
290                                    void *);
291 krb5_error_code _krb5_evp_encrypt_cts (krb5_context, struct _krb5_key_data *,
292                                        void *,size_t, krb5_boolean,
293                                        int, void *);
294 void _krb5_evp_schedule (krb5_context, struct _krb5_key_type *,
295                          struct _krb5_key_data *);
296 krb5_error_code _krb5_SP_HMAC_SHA1_checksum (krb5_context,
297                                              struct _krb5_key_data *,
298                                              const void *,
299                                              size_t, unsigned, Checksum *);
300
301 void _krb5_xor(DES_cblock *key, const unsigned char *b);
302
303 #ifdef KERNEL
304 /*
305  * Ew, gross!
306  * crypto.c contains hard-coded references to these, so even though we don't
307  * implement these enctypes in the kernel, we need to have stubs present in
308  * order to link a kernel module.  In userspace, we do implement these enctypes,
309  * and the real functions are provided by the heimdal source files.
310  */
311 static_inline krb5_error_code
312 _krb5_usage2arcfour(krb5_context context, unsigned *usage) {
313     return -1;
314 }
315
316 static_inline void
317 _krb5_DES3_random_to_key(krb5_context context, krb5_keyblock *key,
318                          const void *rand, size_t size) {
319     return;
320 }
321 #else   /* KERNEL */
322 void
323 _krb5_DES3_random_to_key (krb5_context context,
324                           krb5_keyblock *key,
325                           const void *rand,
326                           size_t size);
327
328 krb5_error_code _krb5_usage2arcfour(krb5_context context, unsigned *usage);
329 #endif  /* KERNEL */
330
331 #define _krb5_AES_salt NULL
332 #define _krb5_arcfour_salt NULL
333 #define _krb5_des3_salt NULL
334 #define _krb5_des3_salt_derived NULL
335 #define _krb5_des_salt NULL