79bf02255a7e16e56d0192c9f0bad1559e9daf5f
[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
11 #include <roken.h>
12
13 #include <fcntl.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/param.h>
17 #include <inttypes.h>
18 #include <sys/types.h>
19 #include <sys/errno.h>
20 #include <pthread.h>
21
22 #endif
23
24 #include <hcrypto/evp.h>
25 #include <hcrypto/sha.h>
26
27 #include "rfc3961.h"
28
29 #ifndef KERNEL
30 # define HEIMDAL_MUTEX pthread_mutex_t
31 # define HEIMDAL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
32 # define HEIMDAL_MUTEX_init(m) pthread_mutex_init(m, NULL)
33 # define HEIMDAL_MUTEX_lock(m) pthread_mutex_lock(m)
34 # define HEIMDAL_MUTEX_unlock(m) pthread_mutex_unlock(m)
35 # define HEIMDAL_MUTEX_destroy(m) pthread_mutex_destroy(m)
36 #endif
37
38 #define HEIMDAL_SMALLER 1
39 #define HEIM_CRYPTO_NO_TRIPLE_DES
40 #define HEIM_CRYPTO_NO_ARCFOUR
41 #define HEIM_CRYPTO_NO_PK
42
43 #define ALLOC(X, N) (X) = calloc((N), sizeof(*(X)))
44
45 #ifndef max
46 #define max(a,b) (((a)>(b))?(a):(b))
47 #endif
48
49 #ifndef O_BINARY
50 #define O_BINARY 0
51 #endif
52
53 #ifndef O_CLOEXEC
54 #define O_CLOEXEC 0
55 #endif
56
57 typedef int krb5_boolean;
58 typedef ssize_t krb5_ssize_t;
59
60 #define KRB5_KU_AS_REP_ENC_PART 3
61 #define KRB5_KU_USAGE_SEAL 22
62 #define KRB5_KU_USAGE_SIGN 23
63 #define KRB5_KU_USAGE_SEQ 24
64
65 #define TRUE 1
66 #define FALSE 0
67
68 /* From the ASN.1 */
69
70 typedef struct EncryptedData {
71   int etype;
72   int *kvno;
73   heim_octet_string cipher;
74 } EncryptedData;
75
76 typedef enum krb5_salttype {
77     KRB5_PW_SALT = 3,
78     KRB5_AFS3_SALT = 10
79 } krb5_salttype;
80
81 typedef enum krb5_keytype {
82     KEYTYPE_NULL        = 0,
83     KEYTYPE_DES         = 1,
84     KEYTYPE_DES3        = 7,
85     KEYTYPE_AES128      = 17,
86     KEYTYPE_AES256      = 18,
87     KEYTYPE_ARCFOUR     = 23,
88     KEYTYPE_ARCFOUR_56  = 24
89 } krb5_keytype;
90
91 #define KRB5_ENCTYPE_NULL KEYTYPE_NULL
92 #define KRB5_ENCTYPE_OLD_DES3_CBC_SHA1 KEYTYPE_DES3
93 #define KRB5_ENCTYPE_AES128_CTS_HMAC_SHA1_96 KEYTYPE_AES128
94 #define KRB5_ENCTYPE_AES256_CTS_HMAC_SHA1_96 KEYTYPE_AES256
95
96 typedef struct krb5_salt {
97     krb5_salttype salttype;
98     krb5_data saltvalue;
99 } krb5_salt;
100
101 typedef struct krb5_crypto_iov {
102     unsigned int flags;
103     /* ignored */
104 #define KRB5_CRYPTO_TYPE_EMPTY          0
105     /* OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_HEADER) */
106 #define KRB5_CRYPTO_TYPE_HEADER         1
107     /* IN and OUT */
108 #define KRB5_CRYPTO_TYPE_DATA           2
109     /* IN */
110 #define KRB5_CRYPTO_TYPE_SIGN_ONLY      3
111    /* (only for encryption) OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_TRAILER) */
112 #define KRB5_CRYPTO_TYPE_PADDING        4
113    /* OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_TRAILER) */
114 #define KRB5_CRYPTO_TYPE_TRAILER        5
115    /* OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_CHECKSUM) */
116 #define KRB5_CRYPTO_TYPE_CHECKSUM       6
117     krb5_data data;
118 } krb5_crypto_iov;
119
120 #define ETYPE_NULL 0
121
122 #define KRB5_LIB_FUNCTION
123 #define KRB5_LIB_CALL
124
125 /* Error codes */
126 #define KRB5_BAD_MSIZE -1765328194
127 #define KRB5_BAD_KEYSIZE -1765328195
128 #define KRB5_PROG_SUMTYPE_NOSUPP -1765328231
129 #define KRB5_PROG_KEYTYPE_NOSUPP -1765328233
130 #define KRB5_PROG_ETYPE_NOSUPP -1765328234
131 #define HEIM_ERR_SALTTYPE_NOSUPP -1980176638
132 #define KRB5KRB_AP_ERR_BAD_INTEGRITY -1765328353
133
134 #define KRB5_CRYPTO_INTERNAL 1
135
136 /* Currently, we just disable localised error strings. We'll get the error
137  * numbers out, but no meaningful text */
138 #define N_(X, Y) X
139
140 /* These have to be real functions, because IRIX doesn't seem to support
141  * variadic macros */
142 void krb5_set_error_message(krb5_context, krb5_error_code, const char *, ...);
143 krb5_error_code krb5_abortx(krb5_context, const char *, ...);
144
145 #define krb5_clear_error_message(ctx)
146
147 static_inline krb5_error_code
148 krb5_enomem(krb5_context context)
149 {
150     return ENOMEM;
151 }
152
153
154 /* Local prototypes. These are functions that we aren't admitting to in the
155  * public API */
156 krb5_error_code _krb5_n_fold(const void *str, size_t len, void *, size_t);
157 krb5_error_code krb5_derive_key(krb5_context context, const krb5_keyblock *key,
158                                 krb5_enctype etype, const void *constant,
159                                 size_t constant_len,
160                                 krb5_keyblock **derived_key);
161 krb5_error_code krb5_enctype_keysize(krb5_context context,
162                                      krb5_enctype type,
163                                      size_t *keysize);
164 krb5_ssize_t _krb5_put_int(void *buffer, unsigned long value, size_t size);
165 void krb5_data_zero(krb5_data *p);
166 krb5_error_code krb5_data_copy(krb5_data *p, const void *data, size_t len);
167 void krb5_free_data(krb5_context context, krb5_data *p);
168 krb5_error_code krb5_copy_keyblock(krb5_context,
169                                    const krb5_keyblock *,
170                                    krb5_keyblock **);
171 void krb5_free_keyblock(krb5_context, krb5_keyblock *);
172 int krb5_data_ct_cmp(const krb5_data *, const krb5_data *);
173 int der_copy_octet_string(const krb5_data *, krb5_data *);
174 int copy_EncryptionKey(const krb5_keyblock *, krb5_keyblock *);
175 int ct_memcmp(const void *p1, const void *p2, size_t len);
176 krb5_error_code krb5_enctype_to_string(krb5_context context,
177                                        krb5_enctype etype,
178                                        char **string);
179
180
181 #include "crypto.h"
182
183 struct _krb5_checksum_type * _krb5_find_checksum (krb5_cksumtype);
184 struct _krb5_encryption_type * _krb5_find_enctype (krb5_enctype);
185 void _krb5_free_key_data (krb5_context, struct _krb5_key_data *,
186                           struct _krb5_encryption_type *);
187 void _krb5_evp_cleanup (krb5_context, struct _krb5_key_data *);
188
189 krb5_error_code _krb5_evp_encrypt (krb5_context, struct _krb5_key_data *,
190                                    void *, size_t, krb5_boolean, int,
191                                    void *);
192 krb5_error_code _krb5_evp_encrypt_cts (krb5_context, struct _krb5_key_data *,
193                                        void *,size_t, krb5_boolean,
194                                        int, void *);
195 void _krb5_evp_schedule (krb5_context, struct _krb5_key_type *,
196                          struct _krb5_key_data *);
197 krb5_error_code _krb5_SP_HMAC_SHA1_checksum (krb5_context,
198                                              struct _krb5_key_data *,
199                                              const void *,
200                                              size_t, unsigned, Checksum *);
201
202 /* These are bodges - we don't implement these encryption types, but
203  * crypto.c contains hard coded references to them, and to these funcs.
204  *
205  * They will never actually be called ...
206  */
207 static_inline krb5_error_code
208 _krb5_usage2arcfour(krb5_context context, unsigned *usage) {
209    return -1;
210 }
211
212 static_inline void
213 _krb5_DES3_random_to_key (krb5_context context,
214                           krb5_keyblock *key,
215                           const void *rand,
216                           size_t size) {
217    return;
218 }
219
220 #define _krb5_AES_salt NULL