root/source4/heimdal/lib/krb5/mit_glue.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. krb5_c_make_checksum
  2. krb5_c_verify_checksum
  3. krb5_c_get_checksum
  4. krb5_c_set_checksum
  5. krb5_free_checksum
  6. krb5_free_checksum_contents
  7. krb5_checksum_free
  8. krb5_c_valid_enctype
  9. krb5_c_valid_cksumtype
  10. krb5_c_is_coll_proof_cksum
  11. krb5_c_is_keyed_cksum
  12. krb5_copy_checksum
  13. krb5_c_checksum_length
  14. krb5_c_block_size
  15. krb5_c_decrypt
  16. krb5_c_encrypt
  17. krb5_c_encrypt_length
  18. krb5_c_enctype_compare
  19. krb5_c_make_random_key
  20. krb5_c_keylengths
  21. krb5_c_prf_length
  22. krb5_c_prf

   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$");
  36 
  37 #ifndef HEIMDAL_SMALLER
  38 
  39 /*
  40  * Glue for MIT API
  41  */
  42 
  43 krb5_error_code KRB5_LIB_FUNCTION
  44 krb5_c_make_checksum(krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
  45                      krb5_cksumtype cksumtype,
  46                      const krb5_keyblock *key,
  47                      krb5_keyusage usage,
  48                      const krb5_data *input,
  49                      krb5_checksum *cksum)
  50 {
  51     krb5_error_code ret;
  52     krb5_crypto crypto;
  53 
  54     ret = krb5_crypto_init(context, key, 0, &crypto);
  55     if (ret)
  56         return ret;
  57 
  58     ret = krb5_create_checksum(context, crypto,  usage, cksumtype,
  59                                input->data, input->length, cksum);
  60     krb5_crypto_destroy(context, crypto);
  61 
  62     return ret ;
  63 }
  64 
  65 krb5_error_code KRB5_LIB_FUNCTION
  66 krb5_c_verify_checksum(krb5_context context, const krb5_keyblock *key,
     /* [<][>][^][v][top][bottom][index][help] */
  67                        krb5_keyusage usage, const krb5_data *data,
  68                        const krb5_checksum *cksum, krb5_boolean *valid)
  69 {
  70     krb5_error_code ret;
  71     krb5_checksum data_cksum;
  72 
  73     *valid = 0;
  74 
  75     ret = krb5_c_make_checksum(context, cksum->cksumtype,
  76                                key, usage, data, &data_cksum);
  77     if (ret)
  78         return ret;
  79 
  80     if (data_cksum.cksumtype == cksum->cksumtype
  81         && data_cksum.checksum.length == cksum->checksum.length
  82         && memcmp(data_cksum.checksum.data, cksum->checksum.data, cksum->checksum.length) == 0)
  83         *valid = 1;
  84 
  85     krb5_free_checksum_contents(context, &data_cksum);
  86 
  87     return 0;
  88 }
  89 
  90 krb5_error_code KRB5_LIB_FUNCTION
  91 krb5_c_get_checksum(krb5_context context, const krb5_checksum *cksum,
     /* [<][>][^][v][top][bottom][index][help] */
  92                     krb5_cksumtype *type, krb5_data **data)
  93 {
  94     krb5_error_code ret;
  95 
  96     if (type)
  97         *type = cksum->cksumtype;
  98     if (data) {
  99         *data = malloc(sizeof(**data));
 100         if (*data == NULL)
 101             return ENOMEM;
 102 
 103         ret = der_copy_octet_string(&cksum->checksum, *data);
 104         if (ret) {
 105             free(*data);
 106             *data = NULL;
 107             return ret;
 108         }
 109     }
 110     return 0;
 111 }
 112 
 113 krb5_error_code KRB5_LIB_FUNCTION
 114 krb5_c_set_checksum(krb5_context context, krb5_checksum *cksum,
     /* [<][>][^][v][top][bottom][index][help] */
 115                     krb5_cksumtype type, const krb5_data *data)
 116 {
 117     cksum->cksumtype = type;
 118     return der_copy_octet_string(data, &cksum->checksum);
 119 }
 120 
 121 void KRB5_LIB_FUNCTION
 122 krb5_free_checksum (krb5_context context, krb5_checksum *cksum)
     /* [<][>][^][v][top][bottom][index][help] */
 123 {
 124     krb5_checksum_free(context, cksum);
 125     free(cksum);
 126 }
 127 
 128 void KRB5_LIB_FUNCTION
 129 krb5_free_checksum_contents(krb5_context context, krb5_checksum *cksum)
     /* [<][>][^][v][top][bottom][index][help] */
 130 {
 131     krb5_checksum_free(context, cksum);
 132     memset(cksum, 0, sizeof(*cksum));
 133 }
 134 
 135 void KRB5_LIB_FUNCTION
 136 krb5_checksum_free(krb5_context context, krb5_checksum *cksum)
     /* [<][>][^][v][top][bottom][index][help] */
 137 {
 138     free_Checksum(cksum);
 139 }
 140 
 141 krb5_boolean KRB5_LIB_FUNCTION
 142 krb5_c_valid_enctype (krb5_enctype etype)
     /* [<][>][^][v][top][bottom][index][help] */
 143 {
 144     return krb5_enctype_valid(NULL, etype);
 145 }
 146 
 147 krb5_boolean KRB5_LIB_FUNCTION
 148 krb5_c_valid_cksumtype(krb5_cksumtype ctype)
     /* [<][>][^][v][top][bottom][index][help] */
 149 {
 150     return krb5_cksumtype_valid(NULL, ctype);
 151 }
 152 
 153 krb5_boolean KRB5_LIB_FUNCTION
 154 krb5_c_is_coll_proof_cksum(krb5_cksumtype ctype)
     /* [<][>][^][v][top][bottom][index][help] */
 155 {
 156     return krb5_checksum_is_collision_proof(NULL, ctype);
 157 }
 158 
 159 krb5_boolean KRB5_LIB_FUNCTION
 160 krb5_c_is_keyed_cksum(krb5_cksumtype ctype)
     /* [<][>][^][v][top][bottom][index][help] */
 161 {
 162     return krb5_checksum_is_keyed(NULL, ctype);
 163 }
 164 
 165 krb5_error_code KRB5_LIB_FUNCTION
 166 krb5_copy_checksum (krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
 167                     const krb5_checksum *old,
 168                     krb5_checksum **new)
 169 {
 170     *new = malloc(sizeof(**new));
 171     if (*new == NULL)
 172         return ENOMEM;
 173     return copy_Checksum(old, *new);
 174 }
 175 
 176 krb5_error_code KRB5_LIB_FUNCTION
 177 krb5_c_checksum_length (krb5_context context, krb5_cksumtype cksumtype,
     /* [<][>][^][v][top][bottom][index][help] */
 178                         size_t *length)
 179 {
 180     return krb5_checksumsize(context, cksumtype, length);
 181 }
 182 
 183 krb5_error_code KRB5_LIB_FUNCTION
 184 krb5_c_block_size(krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
 185                   krb5_enctype enctype,
 186                   size_t *blocksize)
 187 {
 188     krb5_error_code ret;
 189     krb5_crypto crypto;
 190     krb5_keyblock key;
 191 
 192     ret = krb5_generate_random_keyblock(context, enctype, &key);
 193     if (ret)
 194         return ret;
 195 
 196     ret = krb5_crypto_init(context, &key, 0, &crypto);
 197     krb5_free_keyblock_contents(context, &key);
 198     if (ret)
 199         return ret;
 200     ret = krb5_crypto_getblocksize(context, crypto, blocksize);
 201     krb5_crypto_destroy(context, crypto);
 202 
 203     return ret;
 204 }
 205 
 206 krb5_error_code KRB5_LIB_FUNCTION
 207 krb5_c_decrypt(krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
 208                const krb5_keyblock key,
 209                krb5_keyusage usage,
 210                const krb5_data *ivec,
 211                krb5_enc_data *input,
 212                krb5_data *output)
 213 {
 214     krb5_error_code ret;
 215     krb5_crypto crypto;
 216 
 217     ret = krb5_crypto_init(context, &key, input->enctype, &crypto);
 218     if (ret)
 219         return ret;
 220 
 221     if (ivec) {
 222         size_t blocksize;
 223 
 224         ret = krb5_crypto_getblocksize(context, crypto, &blocksize);
 225         if (ret) {
 226         krb5_crypto_destroy(context, crypto);
 227         return ret;
 228         }
 229         
 230         if (blocksize > ivec->length) {
 231             krb5_crypto_destroy(context, crypto);
 232             return KRB5_BAD_MSIZE;
 233         }
 234     }
 235 
 236     ret = krb5_decrypt_ivec(context, crypto, usage,
 237                             input->ciphertext.data, input->ciphertext.length,
 238                             output,
 239                             ivec ? ivec->data : NULL);
 240 
 241     krb5_crypto_destroy(context, crypto);
 242 
 243     return ret ;
 244 }
 245 
 246 krb5_error_code KRB5_LIB_FUNCTION
 247 krb5_c_encrypt(krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
 248                const krb5_keyblock *key,
 249                krb5_keyusage usage,
 250                const krb5_data *ivec,
 251                const krb5_data *input,
 252                krb5_enc_data *output)
 253 {
 254     krb5_error_code ret;
 255     krb5_crypto crypto;
 256 
 257     ret = krb5_crypto_init(context, key, 0, &crypto);
 258     if (ret)
 259         return ret;
 260 
 261     if (ivec) {
 262         size_t blocksize;
 263 
 264         ret = krb5_crypto_getblocksize(context, crypto, &blocksize);
 265         if (ret) {
 266             krb5_crypto_destroy(context, crypto);
 267             return ret;
 268         }
 269 
 270         if (blocksize > ivec->length) {
 271             krb5_crypto_destroy(context, crypto);
 272             return KRB5_BAD_MSIZE;
 273         }
 274     }
 275 
 276     ret = krb5_encrypt_ivec(context, crypto, usage,
 277                             input->data, input->length,
 278                             &output->ciphertext,
 279                             ivec ? ivec->data : NULL);
 280     output->kvno = 0;
 281     krb5_crypto_getenctype(context, crypto, &output->enctype);
 282 
 283     krb5_crypto_destroy(context, crypto);
 284 
 285     return ret ;
 286 }
 287 
 288 krb5_error_code KRB5_LIB_FUNCTION
 289 krb5_c_encrypt_length(krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
 290                       krb5_enctype enctype,
 291                       size_t inputlen,
 292                       size_t *length)
 293 {
 294     krb5_error_code ret;
 295     krb5_crypto crypto;
 296     krb5_keyblock key;
 297 
 298     ret = krb5_generate_random_keyblock(context, enctype, &key);
 299     if (ret)
 300         return ret;
 301 
 302     ret = krb5_crypto_init(context, &key, 0, &crypto);
 303     krb5_free_keyblock_contents(context, &key);
 304     if (ret)
 305         return ret;
 306 
 307     *length = krb5_get_wrapped_length(context, crypto, inputlen);
 308     krb5_crypto_destroy(context, crypto);
 309 
 310     return 0;
 311 }
 312 
 313 krb5_error_code KRB5_LIB_FUNCTION
 314 krb5_c_enctype_compare(krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
 315                        krb5_enctype e1,
 316                        krb5_enctype e2,
 317                        krb5_boolean *similar)
 318 {
 319     *similar = krb5_enctypes_compatible_keys(context, e1, e2);
 320     return 0;
 321 }
 322 
 323 krb5_error_code KRB5_LIB_FUNCTION
 324 krb5_c_make_random_key(krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
 325                        krb5_enctype enctype,
 326                        krb5_keyblock *random_key)
 327 {
 328     return krb5_generate_random_keyblock(context, enctype, random_key);
 329 }
 330 
 331 krb5_error_code KRB5_LIB_FUNCTION
 332 krb5_c_keylengths(krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
 333                   krb5_enctype enctype,
 334                   size_t *ilen,
 335                   size_t *keylen)
 336 {
 337     krb5_error_code ret;
 338 
 339     ret = krb5_enctype_keybits(context, enctype, ilen);
 340     if (ret)
 341         return ret;
 342     *ilen = (*ilen + 7) / 8;
 343     return krb5_enctype_keysize(context, enctype, keylen);
 344 }
 345 
 346 krb5_error_code KRB5_LIB_FUNCTION
 347 krb5_c_prf_length(krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
 348                   krb5_enctype type,
 349                   size_t *length)
 350 {
 351     return krb5_crypto_prf_length(context, type, length);
 352 }
 353 
 354 krb5_error_code KRB5_LIB_FUNCTION
 355 krb5_c_prf(krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
 356            const krb5_keyblock *key,
 357            const krb5_data *input,
 358            krb5_data *output)
 359 {
 360     krb5_crypto crypto;
 361     krb5_error_code ret;
 362 
 363     ret = krb5_crypto_init(context, key, 0, &crypto);
 364     if (ret)
 365         return ret;
 366 
 367     ret = krb5_crypto_prf(context, crypto, input, output);
 368     krb5_crypto_destroy(context, crypto);
 369 
 370     return ret;
 371 }
 372 
 373 #endif /* HEIMDAL_SMALLER */

/* [<][>][^][v][top][bottom][index][help] */