xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/crypto/dk/stringtokey.c (revision 505d05c73a6e56769f263d4803b22eddd168ee24)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * Copyright (C) 1998 by the FundsXpress, INC.
10  *
11  * All rights reserved.
12  *
13  * Export of this software from the United States of America may require
14  * a specific license from the United States Government.  It is the
15  * responsibility of any person or organization contemplating export to
16  * obtain such a license before exporting.
17  *
18  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
19  * distribute this software and its documentation for any purpose and
20  * without fee is hereby granted, provided that the above copyright
21  * notice appear in all copies and that both that copyright notice and
22  * this permission notice appear in supporting documentation, and that
23  * the name of FundsXpress. not be used in advertising or publicity pertaining
24  * to distribution of the software without specific, written prior
25  * permission.  FundsXpress makes no representations about the suitability of
26  * this software for any purpose.  It is provided "as is" without express
27  * or implied warranty.
28  *
29  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
30  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
31  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  */
33 
34 #include <dk.h>
35 
36 static unsigned char kerberos[] = "kerberos";
37 #define kerberos_len (sizeof(kerberos)-1)
38 
39 krb5_error_code
40 krb5_dk_string_to_key(
41 	krb5_context context,
42 	krb5_const struct krb5_enc_provider *enc,
43 	krb5_const krb5_data *string,
44 	krb5_const krb5_data *salt,
45 	krb5_const krb5_data *parms,
46 	krb5_keyblock *key)
47 {
48     krb5_error_code ret;
49     size_t keybytes, keylength, concatlen;
50     unsigned char *concat, *foldstring, *foldkeydata;
51     krb5_data indata;
52     krb5_keyblock foldkey;
53 
54     /* key->length is checked by krb5_derive_key */
55 
56     keybytes = enc->keybytes;
57     keylength = enc->keylength;
58 
59     concatlen = string->length+(salt?salt->length:0);
60 
61     if ((concat = (unsigned char *) malloc(concatlen)) == NULL)
62 	return(ENOMEM);
63     if ((foldstring = (unsigned char *) malloc(keybytes)) == NULL) {
64 	free(concat);
65 	return(ENOMEM);
66     }
67     if ((foldkeydata = (unsigned char *) malloc(keylength)) == NULL) {
68 	free(foldstring);
69 	free(concat);
70 	return(ENOMEM);
71     }
72 
73     /* construct input string ( = string + salt), fold it, make_key it */
74 
75     (void) memcpy(concat, string->data, string->length);
76     if (salt)
77 	(void) memcpy(concat+string->length, salt->data, salt->length);
78 
79     krb5_nfold(concatlen*8, concat, keybytes*8, foldstring);
80 
81     indata.length = keybytes;
82     indata.data = (char *)foldstring;
83 
84     memset(&foldkey, 0, sizeof (krb5_keyblock));
85     foldkey.enctype = key->enctype;
86     foldkey.length = keylength;
87     foldkey.contents = foldkeydata;
88 
89     (*(enc->make_key))(context, &indata, &foldkey);
90 
91     /* now derive the key from this one */
92 
93     indata.length = kerberos_len;
94     indata.data = (char *)kerberos;
95 
96     if ((ret = krb5_derive_key(context, enc, &foldkey, key, &indata)))
97 	(void) memset(key->contents, 0, key->length);
98 
99     /* ret is set correctly by the prior call */
100 
101     (void) memset(concat, 0, concatlen);
102     (void) memset(foldstring, 0, keybytes);
103     (void) memset(foldkeydata, 0, keylength);
104 
105     free(foldkeydata);
106     free(foldstring);
107     free(concat);
108 
109     return(ret);
110 }
111