Blame src/lib/crypto/krb/random_to_key.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/*
Packit fd8b60
 * COPYRIGHT (c) 2006
Packit fd8b60
 * The Regents of the University of Michigan
Packit fd8b60
 * ALL RIGHTS RESERVED
Packit fd8b60
 *
Packit fd8b60
 * Permission is granted to use, copy, create derivative works
Packit fd8b60
 * and redistribute this software and such derivative works
Packit fd8b60
 * for any purpose, so long as the name of The University of
Packit fd8b60
 * Michigan is not used in any advertising or publicity
Packit fd8b60
 * pertaining to the use of distribution of this software
Packit fd8b60
 * without specific, written prior authorization.  If the
Packit fd8b60
 * above copyright notice or any other identification of the
Packit fd8b60
 * University of Michigan is included in any copy of any
Packit fd8b60
 * portion of this software, then the disclaimer below must
Packit fd8b60
 * also be included.
Packit fd8b60
 *
Packit fd8b60
 * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
Packit fd8b60
 * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
Packit fd8b60
 * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
Packit fd8b60
 * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
Packit fd8b60
 * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
Packit fd8b60
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
Packit fd8b60
 * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
Packit fd8b60
 * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
Packit fd8b60
 * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
Packit fd8b60
 * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
Packit fd8b60
 * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
Packit fd8b60
 * SUCH DAMAGES.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Create a key given random data.  It is assumed that random_key has
Packit fd8b60
 * already been initialized and random_key->contents have been allocated
Packit fd8b60
 * with the correct length.
Packit fd8b60
 */
Packit fd8b60
#include "crypto_int.h"
Packit fd8b60
Packit fd8b60
krb5_error_code KRB5_CALLCONV
Packit fd8b60
krb5_c_random_to_key(krb5_context context, krb5_enctype enctype,
Packit fd8b60
                     krb5_data *random_data, krb5_keyblock *random_key)
Packit fd8b60
{
Packit fd8b60
    krb5_error_code ret;
Packit fd8b60
    const struct krb5_keytypes *ktp;
Packit fd8b60
Packit fd8b60
    if (random_data == NULL || random_key == NULL ||
Packit fd8b60
        random_key->contents == NULL)
Packit fd8b60
        return EINVAL;
Packit fd8b60
Packit fd8b60
    ktp = find_enctype(enctype);
Packit fd8b60
    if (ktp == NULL)
Packit fd8b60
        return KRB5_BAD_ENCTYPE;
Packit fd8b60
Packit fd8b60
    if (random_key->length != ktp->enc->keylength)
Packit fd8b60
        return KRB5_BAD_KEYSIZE;
Packit fd8b60
Packit fd8b60
    ret = ktp->rand2key(random_data, random_key);
Packit fd8b60
    if (ret)
Packit fd8b60
        zap(random_key->contents, random_key->length);
Packit fd8b60
Packit fd8b60
    return ret;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
krb5_error_code
Packit fd8b60
k5_rand2key_direct(const krb5_data *randombits, krb5_keyblock *keyblock)
Packit fd8b60
{
Packit fd8b60
    if (randombits->length != keyblock->length)
Packit fd8b60
        return KRB5_CRYPTO_INTERNAL;
Packit fd8b60
Packit fd8b60
    keyblock->magic = KV5M_KEYBLOCK;
Packit fd8b60
    memcpy(keyblock->contents, randombits->data, randombits->length);
Packit fd8b60
    return 0;
Packit fd8b60
}