2 * Copyright (c) 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 #define KRB5_DEPRECATED
36 #include "krb5_locl.h"
38 #ifndef HEIMDAL_SMALLER
44 krb5_error_code KRB5_LIB_FUNCTION
45 krb5_c_make_checksum(krb5_context context,
46 krb5_cksumtype cksumtype,
47 const krb5_keyblock *key,
49 const krb5_data *input,
55 ret = krb5_crypto_init(context, key, 0, &crypto);
59 ret = krb5_create_checksum(context, crypto, usage, cksumtype,
60 input->data, input->length, cksum);
61 krb5_crypto_destroy(context, crypto);
66 krb5_error_code KRB5_LIB_FUNCTION
67 krb5_c_verify_checksum(krb5_context context, const krb5_keyblock *key,
68 krb5_keyusage usage, const krb5_data *data,
69 const krb5_checksum *cksum, krb5_boolean *valid)
72 krb5_checksum data_cksum;
76 ret = krb5_c_make_checksum(context, cksum->cksumtype,
77 key, usage, data, &data_cksum);
81 if (data_cksum.cksumtype == cksum->cksumtype
82 && data_cksum.checksum.length == cksum->checksum.length
83 && memcmp(data_cksum.checksum.data, cksum->checksum.data, cksum->checksum.length) == 0)
86 krb5_free_checksum_contents(context, &data_cksum);
91 krb5_error_code KRB5_LIB_FUNCTION
92 krb5_c_get_checksum(krb5_context context, const krb5_checksum *cksum,
93 krb5_cksumtype *type, krb5_data **data)
98 *type = cksum->cksumtype;
100 *data = malloc(sizeof(**data));
104 ret = der_copy_octet_string(&cksum->checksum, *data);
114 krb5_error_code KRB5_LIB_FUNCTION
115 krb5_c_set_checksum(krb5_context context, krb5_checksum *cksum,
116 krb5_cksumtype type, const krb5_data *data)
118 cksum->cksumtype = type;
119 return der_copy_octet_string(data, &cksum->checksum);
122 void KRB5_LIB_FUNCTION
123 krb5_free_checksum (krb5_context context, krb5_checksum *cksum)
125 krb5_checksum_free(context, cksum);
129 void KRB5_LIB_FUNCTION
130 krb5_free_checksum_contents(krb5_context context, krb5_checksum *cksum)
132 krb5_checksum_free(context, cksum);
133 memset(cksum, 0, sizeof(*cksum));
136 void KRB5_LIB_FUNCTION
137 krb5_checksum_free(krb5_context context, krb5_checksum *cksum)
139 free_Checksum(cksum);
142 krb5_boolean KRB5_LIB_FUNCTION
143 krb5_c_valid_enctype (krb5_enctype etype)
145 return krb5_enctype_valid(NULL, etype);
148 krb5_boolean KRB5_LIB_FUNCTION
149 krb5_c_valid_cksumtype(krb5_cksumtype ctype)
151 return krb5_cksumtype_valid(NULL, ctype);
154 krb5_boolean KRB5_LIB_FUNCTION
155 krb5_c_is_coll_proof_cksum(krb5_cksumtype ctype)
157 return krb5_checksum_is_collision_proof(NULL, ctype);
160 krb5_boolean KRB5_LIB_FUNCTION
161 krb5_c_is_keyed_cksum(krb5_cksumtype ctype)
163 return krb5_checksum_is_keyed(NULL, ctype);
166 krb5_error_code KRB5_LIB_FUNCTION
167 krb5_copy_checksum (krb5_context context,
168 const krb5_checksum *old,
171 *new = malloc(sizeof(**new));
174 return copy_Checksum(old, *new);
177 krb5_error_code KRB5_LIB_FUNCTION
178 krb5_c_checksum_length (krb5_context context, krb5_cksumtype cksumtype,
181 return krb5_checksumsize(context, cksumtype, length);
184 krb5_error_code KRB5_LIB_FUNCTION
185 krb5_c_block_size(krb5_context context,
186 krb5_enctype enctype,
193 ret = krb5_generate_random_keyblock(context, enctype, &key);
197 ret = krb5_crypto_init(context, &key, 0, &crypto);
198 krb5_free_keyblock_contents(context, &key);
201 ret = krb5_crypto_getblocksize(context, crypto, blocksize);
202 krb5_crypto_destroy(context, crypto);
207 krb5_error_code KRB5_LIB_FUNCTION
208 krb5_c_decrypt(krb5_context context,
209 const krb5_keyblock key,
211 const krb5_data *ivec,
212 krb5_enc_data *input,
218 ret = krb5_crypto_init(context, &key, input->enctype, &crypto);
225 ret = krb5_crypto_getblocksize(context, crypto, &blocksize);
227 krb5_crypto_destroy(context, crypto);
231 if (blocksize > ivec->length) {
232 krb5_crypto_destroy(context, crypto);
233 return KRB5_BAD_MSIZE;
237 ret = krb5_decrypt_ivec(context, crypto, usage,
238 input->ciphertext.data, input->ciphertext.length,
240 ivec ? ivec->data : NULL);
242 krb5_crypto_destroy(context, crypto);
247 krb5_error_code KRB5_LIB_FUNCTION
248 krb5_c_encrypt(krb5_context context,
249 const krb5_keyblock *key,
251 const krb5_data *ivec,
252 const krb5_data *input,
253 krb5_enc_data *output)
258 ret = krb5_crypto_init(context, key, 0, &crypto);
265 ret = krb5_crypto_getblocksize(context, crypto, &blocksize);
267 krb5_crypto_destroy(context, crypto);
271 if (blocksize > ivec->length) {
272 krb5_crypto_destroy(context, crypto);
273 return KRB5_BAD_MSIZE;
277 ret = krb5_encrypt_ivec(context, crypto, usage,
278 input->data, input->length,
280 ivec ? ivec->data : NULL);
282 krb5_crypto_getenctype(context, crypto, &output->enctype);
284 krb5_crypto_destroy(context, crypto);
289 krb5_error_code KRB5_LIB_FUNCTION
290 krb5_c_encrypt_length(krb5_context context,
291 krb5_enctype enctype,
299 ret = krb5_generate_random_keyblock(context, enctype, &key);
303 ret = krb5_crypto_init(context, &key, 0, &crypto);
304 krb5_free_keyblock_contents(context, &key);
308 *length = krb5_get_wrapped_length(context, crypto, inputlen);
309 krb5_crypto_destroy(context, crypto);
314 krb5_error_code KRB5_LIB_FUNCTION
315 krb5_c_enctype_compare(krb5_context context,
318 krb5_boolean *similar)
321 *similar = (e1 == e2);
325 krb5_error_code KRB5_LIB_FUNCTION
326 krb5_c_make_random_key(krb5_context context,
327 krb5_enctype enctype,
328 krb5_keyblock *random_key)
330 return krb5_generate_random_keyblock(context, enctype, random_key);
333 krb5_error_code KRB5_LIB_FUNCTION
334 krb5_c_keylengths(krb5_context context,
335 krb5_enctype enctype,
341 ret = krb5_enctype_keybits(context, enctype, ilen);
344 *ilen = (*ilen + 7) / 8;
345 return krb5_enctype_keysize(context, enctype, keylen);
348 krb5_error_code KRB5_LIB_FUNCTION
349 krb5_c_prf_length(krb5_context context,
353 return krb5_crypto_prf_length(context, type, length);
356 krb5_error_code KRB5_LIB_FUNCTION
357 krb5_c_prf(krb5_context context,
358 const krb5_keyblock *key,
359 const krb5_data *input,
365 ret = krb5_crypto_init(context, key, 0, &crypto);
369 ret = krb5_crypto_prf(context, crypto, input, output);
370 krb5_crypto_destroy(context, crypto);
378 * @ingroup krb5_ccache
381 krb5_error_code KRB5_LIB_FUNCTION
382 krb5_cc_copy_creds(krb5_context context,
383 const krb5_ccache from,
386 return krb5_cc_copy_cache(context, from, to);
389 #endif /* HEIMDAL_SMALLER */