5c4af4b981a15b4ae0f2425df053e9ea7c7233e4
[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 typedef struct krb5_salt {
92     krb5_salttype salttype;
93     krb5_data saltvalue;
94 } krb5_salt;
95
96 typedef struct krb5_crypto_iov {
97     unsigned int flags;
98     /* ignored */
99 #define KRB5_CRYPTO_TYPE_EMPTY          0
100     /* OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_HEADER) */
101 #define KRB5_CRYPTO_TYPE_HEADER         1
102     /* IN and OUT */
103 #define KRB5_CRYPTO_TYPE_DATA           2
104     /* IN */
105 #define KRB5_CRYPTO_TYPE_SIGN_ONLY      3
106    /* (only for encryption) OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_TRAILER) */
107 #define KRB5_CRYPTO_TYPE_PADDING        4
108    /* OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_TRAILER) */
109 #define KRB5_CRYPTO_TYPE_TRAILER        5
110    /* OUT krb5_crypto_length(KRB5_CRYPTO_TYPE_CHECKSUM) */
111 #define KRB5_CRYPTO_TYPE_CHECKSUM       6
112     krb5_data data;
113 } krb5_crypto_iov;
114
115 #define ETYPE_NULL 0
116
117 #define KRB5_LIB_FUNCTION
118 #define KRB5_LIB_CALL
119
120 /* Error codes */
121 #define KRB5_BAD_MSIZE -1765328194
122 #define KRB5_BAD_KEYSIZE -1765328195
123 #define KRB5_PROG_SUMTYPE_NOSUPP -1765328231
124 #define KRB5_PROG_KEYTYPE_NOSUPP -1765328233
125 #define KRB5_PROG_ETYPE_NOSUPP -1765328234
126 #define HEIM_ERR_SALTTYPE_NOSUPP -1980176638
127 #define KRB5KRB_AP_ERR_BAD_INTEGRITY -1765328353
128
129 #define KRB5_CRYPTO_INTERNAL 1
130
131 /* Currently, we just disable localised error strings. We'll get the error
132  * numbers out, but no meaningful text */
133 #define N_(...) ""
134 #define krb5_set_error_message(...)
135 #define krb5_abortx(...)
136 #define krb5_clear_error_message(ctx)
137
138 /* Local prototypes. These are functions that we aren't admitting to in the
139  * public API */
140 krb5_error_code _krb5_n_fold(const void *str, size_t len, void *, size_t);
141 krb5_error_code krb5_derive_key(krb5_context context, const krb5_keyblock *key,
142                                 krb5_enctype etype, const void *constant,
143                                 size_t constant_len,
144                                 krb5_keyblock **derived_key);
145 krb5_error_code krb5_enctype_keysize(krb5_context context,
146                                      krb5_enctype type,
147                                      size_t *keysize);
148 krb5_ssize_t _krb5_put_int(void *buffer, unsigned long value, size_t size);
149 void krb5_data_zero(krb5_data *p);
150 krb5_error_code krb5_data_copy(krb5_data *p, const void *data, size_t len);
151 void krb5_free_data(krb5_context context, krb5_data *p);
152 krb5_error_code krb5_copy_keyblock(krb5_context,
153                                    const krb5_keyblock *,
154                                    krb5_keyblock **);
155 void krb5_free_keyblock(krb5_context, krb5_keyblock *);
156 int krb5_data_ct_cmp(const krb5_data *, const krb5_data *);
157 int der_copy_octet_string(const krb5_data *, krb5_data *);
158 int copy_EncryptionKey(const krb5_keyblock *, krb5_keyblock *);
159 int ct_memcmp(const void *p1, const void *p2, size_t len);
160
161 #include "crypto.h"
162
163 struct checksum_type * _krb5_find_checksum (krb5_cksumtype);
164 struct encryption_type * _krb5_find_enctype (krb5_enctype);
165 void _krb5_free_key_data (krb5_context, struct key_data *,
166                           struct encryption_type *);
167 void _krb5_evp_cleanup (krb5_context, struct key_data *);
168 krb5_error_code _krb5_evp_encrypt (krb5_context, struct key_data *, void *,
169                                    size_t, krb5_boolean, int, void *);
170 krb5_error_code _krb5_evp_encrypt_cts (krb5_context, struct key_data *,
171                                        void *,size_t, krb5_boolean,
172                                        int, void *);
173 void _krb5_evp_schedule (krb5_context, struct key_type *,struct key_data *);
174
175 krb5_error_code _krb5_SP_HMAC_SHA1_checksum (krb5_context,
176                                              struct key_data *,
177                                              const void *,
178                                              size_t, unsigned, Checksum *);
179
180 /* These are bodges - we don't implement these encryption types, but
181  * crypto.c contains hard coded references to them, and to these funcs.
182  *
183  * They will never actually be called ...
184  */
185 static_inline krb5_error_code
186 _krb5_usage2arcfour(krb5_context context, unsigned *usage) {
187    return -1;
188 }
189
190 static_inline void
191 _krb5_DES3_random_to_key (krb5_context context,
192                           krb5_keyblock *key,
193                           const void *rand,
194                           size_t size) {
195    return;
196 }
197
198 #define _krb5_AES_salt NULL