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 #include "krb5_locl.h"
35 __RCSID("$Heimdal: mit_glue.c 20042 2007-01-23 20:37:43Z lha $"
42 krb5_error_code KRB5_LIB_FUNCTION
43 krb5_c_make_checksum(krb5_context context
,
44 krb5_cksumtype cksumtype
,
45 const krb5_keyblock
*key
,
47 const krb5_data
*input
,
53 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
57 ret
= krb5_create_checksum(context
, crypto
, usage
, cksumtype
,
58 input
->data
, input
->length
, cksum
);
59 krb5_crypto_destroy(context
, crypto
);
64 krb5_error_code KRB5_LIB_FUNCTION
65 krb5_c_verify_checksum(krb5_context context
, const krb5_keyblock
*key
,
66 krb5_keyusage usage
, const krb5_data
*data
,
67 const krb5_checksum
*cksum
, krb5_boolean
*valid
)
70 krb5_checksum data_cksum
;
74 ret
= krb5_c_make_checksum(context
, cksum
->cksumtype
,
75 key
, usage
, data
, &data_cksum
);
79 if (data_cksum
.cksumtype
== cksum
->cksumtype
80 && data_cksum
.checksum
.length
== cksum
->checksum
.length
81 && memcmp(data_cksum
.checksum
.data
, cksum
->checksum
.data
, cksum
->checksum
.length
) == 0)
84 krb5_free_checksum_contents(context
, &data_cksum
);
89 krb5_error_code KRB5_LIB_FUNCTION
90 krb5_c_get_checksum(krb5_context context
, const krb5_checksum
*cksum
,
91 krb5_cksumtype
*type
, krb5_data
**data
)
96 *type
= cksum
->cksumtype
;
98 *data
= malloc(sizeof(**data
));
102 ret
= der_copy_octet_string(&cksum
->checksum
, *data
);
112 krb5_error_code KRB5_LIB_FUNCTION
113 krb5_c_set_checksum(krb5_context context
, krb5_checksum
*cksum
,
114 krb5_cksumtype type
, const krb5_data
*data
)
116 cksum
->cksumtype
= type
;
117 return der_copy_octet_string(data
, &cksum
->checksum
);
120 void KRB5_LIB_FUNCTION
121 krb5_free_checksum (krb5_context context
, krb5_checksum
*cksum
)
123 krb5_checksum_free(context
, cksum
);
127 void KRB5_LIB_FUNCTION
128 krb5_free_checksum_contents(krb5_context context
, krb5_checksum
*cksum
)
130 krb5_checksum_free(context
, cksum
);
131 memset(cksum
, 0, sizeof(*cksum
));
134 void KRB5_LIB_FUNCTION
135 krb5_checksum_free(krb5_context context
, krb5_checksum
*cksum
)
137 free_Checksum(cksum
);
140 krb5_boolean KRB5_LIB_FUNCTION
141 krb5_c_valid_enctype (krb5_enctype etype
)
143 return krb5_enctype_valid(NULL
, etype
);
146 krb5_boolean KRB5_LIB_FUNCTION
147 krb5_c_valid_cksumtype(krb5_cksumtype ctype
)
149 return krb5_cksumtype_valid(NULL
, ctype
);
152 krb5_boolean KRB5_LIB_FUNCTION
153 krb5_c_is_coll_proof_cksum(krb5_cksumtype ctype
)
155 return krb5_checksum_is_collision_proof(NULL
, ctype
);
158 krb5_boolean KRB5_LIB_FUNCTION
159 krb5_c_is_keyed_cksum(krb5_cksumtype ctype
)
161 return krb5_checksum_is_keyed(NULL
, ctype
);
164 krb5_error_code KRB5_LIB_FUNCTION
165 krb5_copy_checksum (krb5_context context
,
166 const krb5_checksum
*old
,
169 *new = malloc(sizeof(**new));
172 return copy_Checksum(old
, *new);
175 krb5_error_code KRB5_LIB_FUNCTION
176 krb5_c_checksum_length (krb5_context context
, krb5_cksumtype cksumtype
,
179 return krb5_checksumsize(context
, cksumtype
, length
);
182 krb5_error_code KRB5_LIB_FUNCTION
183 krb5_c_block_size(krb5_context context
,
184 krb5_enctype enctype
,
191 ret
= krb5_generate_random_keyblock(context
, enctype
, &key
);
195 ret
= krb5_crypto_init(context
, &key
, 0, &crypto
);
196 krb5_free_keyblock_contents(context
, &key
);
199 ret
= krb5_crypto_getblocksize(context
, crypto
, blocksize
);
200 krb5_crypto_destroy(context
, crypto
);
205 krb5_error_code KRB5_LIB_FUNCTION
206 krb5_c_decrypt(krb5_context context
,
207 const krb5_keyblock key
,
209 const krb5_data
*ivec
,
210 krb5_enc_data
*input
,
216 ret
= krb5_crypto_init(context
, &key
, input
->enctype
, &crypto
);
223 ret
= krb5_crypto_getblocksize(context
, crypto
, &blocksize
);
225 krb5_crypto_destroy(context
, crypto
);
229 if (blocksize
> ivec
->length
) {
230 krb5_crypto_destroy(context
, crypto
);
231 return KRB5_BAD_MSIZE
;
235 ret
= krb5_decrypt_ivec(context
, crypto
, usage
,
236 input
->ciphertext
.data
, input
->ciphertext
.length
,
238 ivec
? ivec
->data
: NULL
);
240 krb5_crypto_destroy(context
, crypto
);
245 krb5_error_code KRB5_LIB_FUNCTION
246 krb5_c_encrypt(krb5_context context
,
247 const krb5_keyblock
*key
,
249 const krb5_data
*ivec
,
250 const krb5_data
*input
,
251 krb5_enc_data
*output
)
256 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
263 ret
= krb5_crypto_getblocksize(context
, crypto
, &blocksize
);
265 krb5_crypto_destroy(context
, crypto
);
269 if (blocksize
> ivec
->length
) {
270 krb5_crypto_destroy(context
, crypto
);
271 return KRB5_BAD_MSIZE
;
275 ret
= krb5_encrypt_ivec(context
, crypto
, usage
,
276 input
->data
, input
->length
,
278 ivec
? ivec
->data
: NULL
);
280 krb5_crypto_getenctype(context
, crypto
, &output
->enctype
);
282 krb5_crypto_destroy(context
, crypto
);
287 krb5_error_code KRB5_LIB_FUNCTION
288 krb5_c_encrypt_length(krb5_context context
,
289 krb5_enctype enctype
,
297 ret
= krb5_generate_random_keyblock(context
, enctype
, &key
);
301 ret
= krb5_crypto_init(context
, &key
, 0, &crypto
);
302 krb5_free_keyblock_contents(context
, &key
);
306 *length
= krb5_get_wrapped_length(context
, crypto
, inputlen
);
307 krb5_crypto_destroy(context
, crypto
);
312 krb5_error_code KRB5_LIB_FUNCTION
313 krb5_c_enctype_compare(krb5_context context
,
316 krb5_boolean
*similar
)
318 *similar
= krb5_enctypes_compatible_keys(context
, e1
, e2
);
322 krb5_error_code KRB5_LIB_FUNCTION
323 krb5_c_make_random_key(krb5_context context
,
324 krb5_enctype enctype
,
325 krb5_keyblock
*random_key
)
327 return krb5_generate_random_keyblock(context
, enctype
, random_key
);
330 krb5_error_code KRB5_LIB_FUNCTION
331 krb5_c_keylengths(krb5_context context
,
332 krb5_enctype enctype
,
338 ret
= krb5_enctype_keybits(context
, enctype
, ilen
);
341 *ilen
= (*ilen
+ 7) / 8;
342 return krb5_enctype_keysize(context
, enctype
, keylen
);
345 krb5_error_code KRB5_LIB_FUNCTION
346 krb5_c_prf_length(krb5_context context
,
350 return krb5_crypto_prf_length(context
, type
, length
);
353 krb5_error_code KRB5_LIB_FUNCTION
354 krb5_c_prf(krb5_context context
,
355 const krb5_keyblock
*key
,
356 const krb5_data
*input
,
362 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
366 ret
= krb5_crypto_prf(context
, crypto
, input
, output
);
367 krb5_crypto_destroy(context
, crypto
);