1/*
2 * COPYRIGHT (c) 2006
3 * The Regents of the University of Michigan
4 * ALL RIGHTS RESERVED
5 *
6 * Permission is granted to use, copy, create derivative works
7 * and redistribute this software and such derivative works
8 * for any purpose, so long as the name of The University of
9 * Michigan is not used in any advertising or publicity
10 * pertaining to the use of distribution of this software
11 * without specific, written prior authorization.  If the
12 * above copyright notice or any other identification of the
13 * University of Michigan is included in any copy of any
14 * portion of this software, then the disclaimer below must
15 * also be included.
16 *
17 * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
18 * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
19 * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
20 * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
21 * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
23 * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
24 * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
25 * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
26 * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
27 * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGES.
29 */
30
31
32/*
33 * Create a key given random data.  It is assumed that random_key has
34 * already been initialized and random_key->contents have been allocated
35 * with the correct length.
36 */
37#include "k5-int.h"
38#include "etypes.h"
39
40krb5_error_code KRB5_CALLCONV
41krb5_c_random_to_key(krb5_context context, krb5_enctype enctype,
42		     krb5_data *random_data, krb5_keyblock *random_key)
43{
44    int i;
45    krb5_error_code ret;
46    const struct krb5_enc_provider *enc;
47
48    if (random_data == NULL || random_key == NULL)
49	return(EINVAL);
50
51    if (random_key->contents == NULL)
52	return(EINVAL);
53
54    for (i=0; i<krb5_enctypes_length; i++) {
55	if (krb5_enctypes_list[i].etype == enctype)
56	    break;
57    }
58
59    if (i == krb5_enctypes_length)
60	return(KRB5_BAD_ENCTYPE);
61
62    enc = krb5_enctypes_list[i].enc;
63
64    if (random_key->length != enc->keylength)
65	return(KRB5_BAD_KEYSIZE);
66
67    /* Solaris Kerberos */
68    ret = ((*(enc->make_key))(context, random_data, random_key));
69
70    if (ret) {
71	memset(random_key->contents, 0, random_key->length);
72    }
73
74    return(ret);
75}
76