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"
41 krb5_error_code KRB5_LIB_FUNCTION
42 krb5_c_make_checksum(krb5_context context
,
43 krb5_cksumtype cksumtype
,
44 const krb5_keyblock
*key
,
46 const krb5_data
*input
,
52 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
56 ret
= krb5_create_checksum(context
, crypto
, usage
, cksumtype
,
57 input
->data
, input
->length
, cksum
);
58 krb5_crypto_destroy(context
, crypto
);
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
)
69 krb5_checksum data_cksum
;
73 ret
= krb5_c_make_checksum(context
, cksum
->cksumtype
,
74 key
, usage
, data
, &data_cksum
);
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)
83 krb5_free_checksum_contents(context
, &data_cksum
);
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
)
95 *type
= cksum
->cksumtype
;
97 *data
= malloc(sizeof(**data
));
101 ret
= der_copy_octet_string(&cksum
->checksum
, *data
);
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
)
115 cksum
->cksumtype
= type
;
116 return der_copy_octet_string(data
, &cksum
->checksum
);
119 void KRB5_LIB_FUNCTION
120 krb5_free_checksum (krb5_context context
, krb5_checksum
*cksum
)
122 krb5_checksum_free(context
, cksum
);
126 void KRB5_LIB_FUNCTION
127 krb5_free_checksum_contents(krb5_context context
, krb5_checksum
*cksum
)
129 krb5_checksum_free(context
, cksum
);
130 memset(cksum
, 0, sizeof(*cksum
));
133 void KRB5_LIB_FUNCTION
134 krb5_checksum_free(krb5_context context
, krb5_checksum
*cksum
)
136 free_Checksum(cksum
);
139 krb5_boolean KRB5_LIB_FUNCTION
140 krb5_c_valid_enctype (krb5_enctype etype
)
142 return krb5_enctype_valid(NULL
, etype
);
145 krb5_boolean KRB5_LIB_FUNCTION
146 krb5_c_valid_cksumtype(krb5_cksumtype ctype
)
148 return krb5_cksumtype_valid(NULL
, ctype
);
151 krb5_boolean KRB5_LIB_FUNCTION
152 krb5_c_is_coll_proof_cksum(krb5_cksumtype ctype
)
154 return krb5_checksum_is_collision_proof(NULL
, ctype
);
157 krb5_boolean KRB5_LIB_FUNCTION
158 krb5_c_is_keyed_cksum(krb5_cksumtype ctype
)
160 return krb5_checksum_is_keyed(NULL
, ctype
);
163 krb5_error_code KRB5_LIB_FUNCTION
164 krb5_copy_checksum (krb5_context context
,
165 const krb5_checksum
*old
,
168 *new = malloc(sizeof(**new));
171 return copy_Checksum(old
, *new);
174 krb5_error_code KRB5_LIB_FUNCTION
175 krb5_c_checksum_length (krb5_context context
, krb5_cksumtype cksumtype
,
178 return krb5_checksumsize(context
, cksumtype
, length
);
181 krb5_error_code KRB5_LIB_FUNCTION
182 krb5_c_block_size(krb5_context context
,
183 krb5_enctype enctype
,
190 ret
= krb5_generate_random_keyblock(context
, enctype
, &key
);
194 ret
= krb5_crypto_init(context
, &key
, 0, &crypto
);
195 krb5_free_keyblock_contents(context
, &key
);
198 ret
= krb5_crypto_getblocksize(context
, crypto
, blocksize
);
199 krb5_crypto_destroy(context
, crypto
);
204 krb5_error_code KRB5_LIB_FUNCTION
205 krb5_c_decrypt(krb5_context context
,
206 const krb5_keyblock key
,
208 const krb5_data
*ivec
,
209 krb5_enc_data
*input
,
215 ret
= krb5_crypto_init(context
, &key
, input
->enctype
, &crypto
);
222 ret
= krb5_crypto_getblocksize(context
, crypto
, &blocksize
);
224 krb5_crypto_destroy(context
, crypto
);
228 if (blocksize
> ivec
->length
) {
229 krb5_crypto_destroy(context
, crypto
);
230 return KRB5_BAD_MSIZE
;
234 ret
= krb5_decrypt_ivec(context
, crypto
, usage
,
235 input
->ciphertext
.data
, input
->ciphertext
.length
,
237 ivec
? ivec
->data
: NULL
);
239 krb5_crypto_destroy(context
, crypto
);
244 krb5_error_code KRB5_LIB_FUNCTION
245 krb5_c_encrypt(krb5_context context
,
246 const krb5_keyblock
*key
,
248 const krb5_data
*ivec
,
249 const krb5_data
*input
,
250 krb5_enc_data
*output
)
255 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
262 ret
= krb5_crypto_getblocksize(context
, crypto
, &blocksize
);
264 krb5_crypto_destroy(context
, crypto
);
268 if (blocksize
> ivec
->length
) {
269 krb5_crypto_destroy(context
, crypto
);
270 return KRB5_BAD_MSIZE
;
274 ret
= krb5_encrypt_ivec(context
, crypto
, usage
,
275 input
->data
, input
->length
,
277 ivec
? ivec
->data
: NULL
);
279 krb5_crypto_getenctype(context
, crypto
, &output
->enctype
);
281 krb5_crypto_destroy(context
, crypto
);
286 krb5_error_code KRB5_LIB_FUNCTION
287 krb5_c_encrypt_length(krb5_context context
,
288 krb5_enctype enctype
,
296 ret
= krb5_generate_random_keyblock(context
, enctype
, &key
);
300 ret
= krb5_crypto_init(context
, &key
, 0, &crypto
);
301 krb5_free_keyblock_contents(context
, &key
);
305 *length
= krb5_get_wrapped_length(context
, crypto
, inputlen
);
306 krb5_crypto_destroy(context
, crypto
);
311 krb5_error_code KRB5_LIB_FUNCTION
312 krb5_c_enctype_compare(krb5_context context
,
315 krb5_boolean
*similar
)
317 *similar
= krb5_enctypes_compatible_keys(context
, e1
, e2
);
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
)
326 return krb5_generate_random_keyblock(context
, enctype
, random_key
);
329 krb5_error_code KRB5_LIB_FUNCTION
330 krb5_c_keylengths(krb5_context context
,
331 krb5_enctype enctype
,
337 ret
= krb5_enctype_keybits(context
, enctype
, ilen
);
340 *ilen
= (*ilen
+ 7) / 8;
341 return krb5_enctype_keysize(context
, enctype
, keylen
);
344 krb5_error_code KRB5_LIB_FUNCTION
345 krb5_c_prf_length(krb5_context context
,
349 return krb5_crypto_prf_length(context
, type
, length
);
352 krb5_error_code KRB5_LIB_FUNCTION
353 krb5_c_prf(krb5_context context
,
354 const krb5_keyblock
*key
,
355 const krb5_data
*input
,
361 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
365 ret
= krb5_crypto_prf(context
, crypto
, input
, output
);
366 krb5_crypto_destroy(context
, crypto
);