xref: /freebsd/crypto/heimdal/lib/krb5/mit_glue.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 /*
2  * Copyright (c) 2003 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "krb5_locl.h"
35 RCSID("$Id: mit_glue.c 20042 2007-01-23 20:37:43Z lha $");
36 
37 /*
38  * Glue for MIT API
39  */
40 
41 krb5_error_code KRB5_LIB_FUNCTION
42 krb5_c_make_checksum(krb5_context context,
43 		     krb5_cksumtype cksumtype,
44 		     const krb5_keyblock *key,
45 		     krb5_keyusage usage,
46 		     const krb5_data *input,
47 		     krb5_checksum *cksum)
48 {
49     krb5_error_code ret;
50     krb5_crypto crypto;
51 
52     ret = krb5_crypto_init(context, key, 0, &crypto);
53     if (ret)
54 	return ret;
55 
56     ret = krb5_create_checksum(context, crypto,  usage, cksumtype,
57 			       input->data, input->length, cksum);
58     krb5_crypto_destroy(context, crypto);
59 
60     return ret ;
61 }
62 
63 krb5_error_code KRB5_LIB_FUNCTION
64 krb5_c_verify_checksum(krb5_context context, const krb5_keyblock *key,
65 		       krb5_keyusage usage, const krb5_data *data,
66 		       const krb5_checksum *cksum, krb5_boolean *valid)
67 {
68     krb5_error_code ret;
69     krb5_checksum data_cksum;
70 
71     *valid = 0;
72 
73     ret = krb5_c_make_checksum(context, cksum->cksumtype,
74 			       key, usage, data, &data_cksum);
75     if (ret)
76 	return ret;
77 
78     if (data_cksum.cksumtype == cksum->cksumtype
79 	&& data_cksum.checksum.length == cksum->checksum.length
80 	&& memcmp(data_cksum.checksum.data, cksum->checksum.data, cksum->checksum.length) == 0)
81 	*valid = 1;
82 
83     krb5_free_checksum_contents(context, &data_cksum);
84 
85     return 0;
86 }
87 
88 krb5_error_code KRB5_LIB_FUNCTION
89 krb5_c_get_checksum(krb5_context context, const krb5_checksum *cksum,
90 		    krb5_cksumtype *type, krb5_data **data)
91 {
92     krb5_error_code ret;
93 
94     if (type)
95 	*type = cksum->cksumtype;
96     if (data) {
97 	*data = malloc(sizeof(**data));
98 	if (*data == NULL)
99 	    return ENOMEM;
100 
101 	ret = der_copy_octet_string(&cksum->checksum, *data);
102 	if (ret) {
103 	    free(*data);
104 	    *data = NULL;
105 	    return ret;
106 	}
107     }
108     return 0;
109 }
110 
111 krb5_error_code KRB5_LIB_FUNCTION
112 krb5_c_set_checksum(krb5_context context, krb5_checksum *cksum,
113 		    krb5_cksumtype type, const krb5_data *data)
114 {
115     cksum->cksumtype = type;
116     return der_copy_octet_string(data, &cksum->checksum);
117 }
118 
119 void KRB5_LIB_FUNCTION
120 krb5_free_checksum (krb5_context context, krb5_checksum *cksum)
121 {
122     krb5_checksum_free(context, cksum);
123     free(cksum);
124 }
125 
126 void KRB5_LIB_FUNCTION
127 krb5_free_checksum_contents(krb5_context context, krb5_checksum *cksum)
128 {
129     krb5_checksum_free(context, cksum);
130     memset(cksum, 0, sizeof(*cksum));
131 }
132 
133 void KRB5_LIB_FUNCTION
134 krb5_checksum_free(krb5_context context, krb5_checksum *cksum)
135 {
136     free_Checksum(cksum);
137 }
138 
139 krb5_boolean KRB5_LIB_FUNCTION
140 krb5_c_valid_enctype (krb5_enctype etype)
141 {
142     return krb5_enctype_valid(NULL, etype);
143 }
144 
145 krb5_boolean KRB5_LIB_FUNCTION
146 krb5_c_valid_cksumtype(krb5_cksumtype ctype)
147 {
148     return krb5_cksumtype_valid(NULL, ctype);
149 }
150 
151 krb5_boolean KRB5_LIB_FUNCTION
152 krb5_c_is_coll_proof_cksum(krb5_cksumtype ctype)
153 {
154     return krb5_checksum_is_collision_proof(NULL, ctype);
155 }
156 
157 krb5_boolean KRB5_LIB_FUNCTION
158 krb5_c_is_keyed_cksum(krb5_cksumtype ctype)
159 {
160     return krb5_checksum_is_keyed(NULL, ctype);
161 }
162 
163 krb5_error_code KRB5_LIB_FUNCTION
164 krb5_copy_checksum (krb5_context context,
165 		    const krb5_checksum *old,
166 		    krb5_checksum **new)
167 {
168     *new = malloc(sizeof(**new));
169     if (*new == NULL)
170 	return ENOMEM;
171     return copy_Checksum(old, *new);
172 }
173 
174 krb5_error_code KRB5_LIB_FUNCTION
175 krb5_c_checksum_length (krb5_context context, krb5_cksumtype cksumtype,
176 			size_t *length)
177 {
178     return krb5_checksumsize(context, cksumtype, length);
179 }
180 
181 krb5_error_code KRB5_LIB_FUNCTION
182 krb5_c_block_size(krb5_context context,
183 		  krb5_enctype enctype,
184 		  size_t *blocksize)
185 {
186     krb5_error_code ret;
187     krb5_crypto crypto;
188     krb5_keyblock key;
189 
190     ret = krb5_generate_random_keyblock(context, enctype, &key);
191     if (ret)
192 	return ret;
193 
194     ret = krb5_crypto_init(context, &key, 0, &crypto);
195     krb5_free_keyblock_contents(context, &key);
196     if (ret)
197 	return ret;
198     ret = krb5_crypto_getblocksize(context, crypto, blocksize);
199     krb5_crypto_destroy(context, crypto);
200 
201     return ret;
202 }
203 
204 krb5_error_code KRB5_LIB_FUNCTION
205 krb5_c_decrypt(krb5_context context,
206 	       const krb5_keyblock key,
207 	       krb5_keyusage usage,
208 	       const krb5_data *ivec,
209 	       krb5_enc_data *input,
210 	       krb5_data *output)
211 {
212     krb5_error_code ret;
213     krb5_crypto crypto;
214 
215     ret = krb5_crypto_init(context, &key, input->enctype, &crypto);
216     if (ret)
217 	return ret;
218 
219     if (ivec) {
220 	size_t blocksize;
221 
222 	ret = krb5_crypto_getblocksize(context, crypto, &blocksize);
223 	if (ret) {
224 	krb5_crypto_destroy(context, crypto);
225 	return ret;
226 	}
227 
228 	if (blocksize > ivec->length) {
229 	    krb5_crypto_destroy(context, crypto);
230 	    return KRB5_BAD_MSIZE;
231 	}
232     }
233 
234     ret = krb5_decrypt_ivec(context, crypto, usage,
235 			    input->ciphertext.data, input->ciphertext.length,
236 			    output,
237 			    ivec ? ivec->data : NULL);
238 
239     krb5_crypto_destroy(context, crypto);
240 
241     return ret ;
242 }
243 
244 krb5_error_code KRB5_LIB_FUNCTION
245 krb5_c_encrypt(krb5_context context,
246 	       const krb5_keyblock *key,
247 	       krb5_keyusage usage,
248 	       const krb5_data *ivec,
249 	       const krb5_data *input,
250 	       krb5_enc_data *output)
251 {
252     krb5_error_code ret;
253     krb5_crypto crypto;
254 
255     ret = krb5_crypto_init(context, key, 0, &crypto);
256     if (ret)
257 	return ret;
258 
259     if (ivec) {
260 	size_t blocksize;
261 
262 	ret = krb5_crypto_getblocksize(context, crypto, &blocksize);
263 	if (ret) {
264 	    krb5_crypto_destroy(context, crypto);
265 	    return ret;
266 	}
267 
268 	if (blocksize > ivec->length) {
269 	    krb5_crypto_destroy(context, crypto);
270 	    return KRB5_BAD_MSIZE;
271 	}
272     }
273 
274     ret = krb5_encrypt_ivec(context, crypto, usage,
275 			    input->data, input->length,
276 			    &output->ciphertext,
277 			    ivec ? ivec->data : NULL);
278     output->kvno = 0;
279     krb5_crypto_getenctype(context, crypto, &output->enctype);
280 
281     krb5_crypto_destroy(context, crypto);
282 
283     return ret ;
284 }
285 
286 krb5_error_code KRB5_LIB_FUNCTION
287 krb5_c_encrypt_length(krb5_context context,
288 		      krb5_enctype enctype,
289 		      size_t inputlen,
290 		      size_t *length)
291 {
292     krb5_error_code ret;
293     krb5_crypto crypto;
294     krb5_keyblock key;
295 
296     ret = krb5_generate_random_keyblock(context, enctype, &key);
297     if (ret)
298 	return ret;
299 
300     ret = krb5_crypto_init(context, &key, 0, &crypto);
301     krb5_free_keyblock_contents(context, &key);
302     if (ret)
303 	return ret;
304 
305     *length = krb5_get_wrapped_length(context, crypto, inputlen);
306     krb5_crypto_destroy(context, crypto);
307 
308     return 0;
309 }
310 
311 krb5_error_code KRB5_LIB_FUNCTION
312 krb5_c_enctype_compare(krb5_context context,
313 		       krb5_enctype e1,
314 		       krb5_enctype e2,
315 		       krb5_boolean *similar)
316 {
317     *similar = krb5_enctypes_compatible_keys(context, e1, e2);
318     return 0;
319 }
320 
321 krb5_error_code KRB5_LIB_FUNCTION
322 krb5_c_make_random_key(krb5_context context,
323 		       krb5_enctype enctype,
324 		       krb5_keyblock *random_key)
325 {
326     return krb5_generate_random_keyblock(context, enctype, random_key);
327 }
328 
329 krb5_error_code KRB5_LIB_FUNCTION
330 krb5_c_keylengths(krb5_context context,
331 		  krb5_enctype enctype,
332 		  size_t *ilen,
333 		  size_t *keylen)
334 {
335     krb5_error_code ret;
336 
337     ret = krb5_enctype_keybits(context, enctype, ilen);
338     if (ret)
339 	return ret;
340     *ilen = (*ilen + 7) / 8;
341     return krb5_enctype_keysize(context, enctype, keylen);
342 }
343 
344 krb5_error_code KRB5_LIB_FUNCTION
345 krb5_c_prf_length(krb5_context context,
346 		  krb5_enctype type,
347 		  size_t *length)
348 {
349     return krb5_crypto_prf_length(context, type, length);
350 }
351 
352 krb5_error_code KRB5_LIB_FUNCTION
353 krb5_c_prf(krb5_context context,
354 	   const krb5_keyblock *key,
355 	   const krb5_data *input,
356 	   krb5_data *output)
357 {
358     krb5_crypto crypto;
359     krb5_error_code ret;
360 
361     ret = krb5_crypto_init(context, key, 0, &crypto);
362     if (ret)
363 	return ret;
364 
365     ret = krb5_crypto_prf(context, crypto, input, output);
366     krb5_crypto_destroy(context, crypto);
367 
368     return ret;
369 }
370