xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/crypto/dk/stringtokey.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * Copyright 2004 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(context, enc, string, salt, parms, 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     (*(enc->keysize))(&keybytes, &keylength);
57 
58     concatlen = string->length+(salt?salt->length:0);
59 
60     if ((concat = (unsigned char *) malloc(concatlen)) == NULL)
61 	return(ENOMEM);
62     if ((foldstring = (unsigned char *) malloc(keybytes)) == NULL) {
63 	free(concat);
64 	return(ENOMEM);
65     }
66     if ((foldkeydata = (unsigned char *) malloc(keylength)) == NULL) {
67 	free(foldstring);
68 	free(concat);
69 	return(ENOMEM);
70     }
71 
72     /* construct input string ( = string + salt), fold it, make_key it */
73 
74     (void) memcpy(concat, string->data, string->length);
75     if (salt)
76 	(void) memcpy(concat+string->length, salt->data, salt->length);
77 
78     krb5_nfold(concatlen*8, concat, keybytes*8, foldstring);
79 
80     indata.length = keybytes;
81     indata.data = (char *)foldstring;
82 
83     memset(&foldkey, 0, sizeof (krb5_keyblock));
84     foldkey.enctype = key->enctype;
85     foldkey.length = keylength;
86     foldkey.contents = foldkeydata;
87 
88     (*(enc->make_key))(context, &indata, &foldkey);
89 
90     /* now derive the key from this one */
91 
92     indata.length = kerberos_len;
93     indata.data = (char *)kerberos;
94 
95     if ((ret = krb5_derive_key(context, enc, &foldkey, key, &indata)))
96 	(void) memset(key->contents, 0, key->length);
97 
98     /* ret is set correctly by the prior call */
99 
100     (void) memset(concat, 0, concatlen);
101     (void) memset(foldstring, 0, keybytes);
102     (void) memset(foldkeydata, 0, keylength);
103 
104     free(foldkeydata);
105     free(foldstring);
106     free(concat);
107 
108     return(ret);
109 }
110