xref: /onnv-gate/usr/src/lib/gss_mechs/mech_krb5/crypto/dk/stringtokey.c (revision 7934:6aeeafc994de)
10Sstevel@tonic-gate /*
2*7934SMark.Phalan@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate 
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate  * Copyright (C) 1998 by the FundsXpress, INC.
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * All rights reserved.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * Export of this software from the United States of America may require
130Sstevel@tonic-gate  * a specific license from the United States Government.  It is the
140Sstevel@tonic-gate  * responsibility of any person or organization contemplating export to
150Sstevel@tonic-gate  * obtain such a license before exporting.
160Sstevel@tonic-gate  *
170Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
180Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
190Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
200Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
210Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
220Sstevel@tonic-gate  * the name of FundsXpress. not be used in advertising or publicity pertaining
230Sstevel@tonic-gate  * to distribution of the software without specific, written prior
240Sstevel@tonic-gate  * permission.  FundsXpress makes no representations about the suitability of
250Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
260Sstevel@tonic-gate  * or implied warranty.
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
290Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
300Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
33*7934SMark.Phalan@Sun.COM #include "dk.h"
340Sstevel@tonic-gate 
35*7934SMark.Phalan@Sun.COM static const unsigned char kerberos[] = "kerberos";
360Sstevel@tonic-gate #define kerberos_len (sizeof(kerberos)-1)
370Sstevel@tonic-gate 
380Sstevel@tonic-gate krb5_error_code
krb5int_dk_string_to_key(krb5_context context,const struct krb5_enc_provider * enc,const krb5_data * string,const krb5_data * salt,const krb5_data * parms,krb5_keyblock * key)39*7934SMark.Phalan@Sun.COM krb5int_dk_string_to_key(
40*7934SMark.Phalan@Sun.COM 			 krb5_context context,
41*7934SMark.Phalan@Sun.COM 			 const struct krb5_enc_provider *enc,
42*7934SMark.Phalan@Sun.COM 			 const krb5_data *string, const krb5_data *salt,
43*7934SMark.Phalan@Sun.COM 			 const krb5_data *parms, krb5_keyblock *key)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate     krb5_error_code ret;
460Sstevel@tonic-gate     size_t keybytes, keylength, concatlen;
470Sstevel@tonic-gate     unsigned char *concat, *foldstring, *foldkeydata;
480Sstevel@tonic-gate     krb5_data indata;
490Sstevel@tonic-gate     krb5_keyblock foldkey;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate     /* key->length is checked by krb5_derive_key */
520Sstevel@tonic-gate 
53781Sgtb     keybytes = enc->keybytes;
54781Sgtb     keylength = enc->keylength;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate     concatlen = string->length+(salt?salt->length:0);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate     if ((concat = (unsigned char *) malloc(concatlen)) == NULL)
590Sstevel@tonic-gate 	return(ENOMEM);
600Sstevel@tonic-gate     if ((foldstring = (unsigned char *) malloc(keybytes)) == NULL) {
610Sstevel@tonic-gate 	free(concat);
620Sstevel@tonic-gate 	return(ENOMEM);
630Sstevel@tonic-gate     }
640Sstevel@tonic-gate     if ((foldkeydata = (unsigned char *) malloc(keylength)) == NULL) {
650Sstevel@tonic-gate 	free(foldstring);
660Sstevel@tonic-gate 	free(concat);
670Sstevel@tonic-gate 	return(ENOMEM);
680Sstevel@tonic-gate     }
690Sstevel@tonic-gate 
700Sstevel@tonic-gate     /* construct input string ( = string + salt), fold it, make_key it */
710Sstevel@tonic-gate 
72*7934SMark.Phalan@Sun.COM     memcpy(concat, string->data, string->length);
730Sstevel@tonic-gate     if (salt)
74*7934SMark.Phalan@Sun.COM 	memcpy(concat+string->length, salt->data, salt->length);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate     krb5_nfold(concatlen*8, concat, keybytes*8, foldstring);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate     indata.length = keybytes;
79*7934SMark.Phalan@Sun.COM     indata.data = (char *) foldstring;
800Sstevel@tonic-gate 
81*7934SMark.Phalan@Sun.COM     /* Solaris Kerberos */
820Sstevel@tonic-gate     memset(&foldkey, 0, sizeof (krb5_keyblock));
830Sstevel@tonic-gate     foldkey.enctype = key->enctype;
840Sstevel@tonic-gate     foldkey.length = keylength;
850Sstevel@tonic-gate     foldkey.contents = foldkeydata;
860Sstevel@tonic-gate 
87*7934SMark.Phalan@Sun.COM     /* Solaris Kerberos */
880Sstevel@tonic-gate     (*(enc->make_key))(context, &indata, &foldkey);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate     /* now derive the key from this one */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate     indata.length = kerberos_len;
93*7934SMark.Phalan@Sun.COM     indata.data = (char *) kerberos;
94*7934SMark.Phalan@Sun.COM     /* Solaris Kerberos */
950Sstevel@tonic-gate     if ((ret = krb5_derive_key(context, enc, &foldkey, key, &indata)))
960Sstevel@tonic-gate 	(void) memset(key->contents, 0, key->length);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate     /* ret is set correctly by the prior call */
990Sstevel@tonic-gate 
100*7934SMark.Phalan@Sun.COM     memset(concat, 0, concatlen);
101*7934SMark.Phalan@Sun.COM     memset(foldstring, 0, keybytes);
102*7934SMark.Phalan@Sun.COM     memset(foldkeydata, 0, keylength);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate     free(foldkeydata);
1050Sstevel@tonic-gate     free(foldstring);
1060Sstevel@tonic-gate     free(concat);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate     return(ret);
1090Sstevel@tonic-gate }
110