xref: /onnv-gate/usr/src/uts/common/gssapi/mechs/krb5/mech/util_crypt.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 /*
8*7934SMark.Phalan@Sun.COM   * Copyright2001 by the Massachusetts Institute of Technology.
90Sstevel@tonic-gate  * Copyright 1993 by OpenVision Technologies, Inc.
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  * Permission to use, copy, modify, distribute, and sell this software
120Sstevel@tonic-gate  * and its documentation for any purpose is hereby granted without fee,
130Sstevel@tonic-gate  * provided that the above copyright notice appears in all copies and
140Sstevel@tonic-gate  * that both that copyright notice and this permission notice appear in
150Sstevel@tonic-gate  * supporting documentation, and that the name of OpenVision not be used
160Sstevel@tonic-gate  * in advertising or publicity pertaining to distribution of the software
170Sstevel@tonic-gate  * without specific, written prior permission. OpenVision makes no
180Sstevel@tonic-gate  * representations about the suitability of this software for any
190Sstevel@tonic-gate  * purpose.  It is provided "as is" without express or implied warranty.
200Sstevel@tonic-gate  *
210Sstevel@tonic-gate  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
220Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
230Sstevel@tonic-gate  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
240Sstevel@tonic-gate  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
250Sstevel@tonic-gate  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
260Sstevel@tonic-gate  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
270Sstevel@tonic-gate  * PERFORMANCE OF THIS SOFTWARE.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Copyright (C) 1998 by the FundsXpress, INC.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * All rights reserved.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * Export of this software from the United States of America may require
360Sstevel@tonic-gate  * a specific license from the United States Government.  It is the
370Sstevel@tonic-gate  * responsibility of any person or organization contemplating export to
380Sstevel@tonic-gate  * obtain such a license before exporting.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
410Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
420Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
430Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
440Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
450Sstevel@tonic-gate  * the name of FundsXpress. not be used in advertising or publicity pertaining
460Sstevel@tonic-gate  * to distribution of the software without specific, written prior
470Sstevel@tonic-gate  * permission.  FundsXpress makes no representations about the suitability of
480Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
490Sstevel@tonic-gate  * or implied warranty.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
520Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
530Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /* Solaris Kerberos:  order is important here.  include gssapiP_krb5.h
570Sstevel@tonic-gate  * before all others, otherwise we get a LINT error from MALLOC macro
580Sstevel@tonic-gate  * being redefined in mechglueP.h */
590Sstevel@tonic-gate #include <gssapiP_krb5.h>
600Sstevel@tonic-gate #include <k5-int.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /* Solaris Kerberos defines memory management macros in <krb5.h> */
630Sstevel@tonic-gate /* #include <memory.h> */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * $Id: util_crypt.c,v 1.11.6.3 2000/06/03 06:09:45 tlyu Exp $
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate int
kg_confounder_size(context,key)700Sstevel@tonic-gate kg_confounder_size(context, key)
710Sstevel@tonic-gate      krb5_context context;
720Sstevel@tonic-gate      krb5_keyblock *key;
730Sstevel@tonic-gate {
74*7934SMark.Phalan@Sun.COM    krb5_error_code code;
750Sstevel@tonic-gate    size_t blocksize;
760Sstevel@tonic-gate    /* We special case rc4*/
770Sstevel@tonic-gate    if (key->enctype == ENCTYPE_ARCFOUR_HMAC)
78*7934SMark.Phalan@Sun.COM      return 8;
79*7934SMark.Phalan@Sun.COM    code = krb5_c_block_size(context, key->enctype, &blocksize);
80*7934SMark.Phalan@Sun.COM    if (code)
810Sstevel@tonic-gate       return(-1); /* XXX */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate    return(blocksize);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate krb5_error_code
kg_make_confounder(context,key,buf)870Sstevel@tonic-gate kg_make_confounder(context, key, buf)
880Sstevel@tonic-gate      krb5_context context;
890Sstevel@tonic-gate      krb5_keyblock *key;
900Sstevel@tonic-gate      unsigned char *buf;
910Sstevel@tonic-gate {
920Sstevel@tonic-gate    krb5_error_code code;
930Sstevel@tonic-gate    size_t blocksize;
94*7934SMark.Phalan@Sun.COM    krb5_data lrandom;
950Sstevel@tonic-gate 
96*7934SMark.Phalan@Sun.COM    code = krb5_c_block_size(context, key->enctype, &blocksize);
97*7934SMark.Phalan@Sun.COM    if (code)
980Sstevel@tonic-gate        return(code);
990Sstevel@tonic-gate 
100*7934SMark.Phalan@Sun.COM    lrandom.length = blocksize;
101*7934SMark.Phalan@Sun.COM    lrandom.data = (char *) buf;
1020Sstevel@tonic-gate 
103*7934SMark.Phalan@Sun.COM    return(krb5_c_random_make_octets(context, &lrandom));
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate int
kg_encrypt_size(context,key,n)1070Sstevel@tonic-gate kg_encrypt_size(context, key, n)
1080Sstevel@tonic-gate      krb5_context context;
1090Sstevel@tonic-gate      krb5_keyblock *key;
1100Sstevel@tonic-gate      int n;
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate    size_t enclen;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate    if (krb5_c_encrypt_length(context, key->enctype, n, &enclen) != 0)
1150Sstevel@tonic-gate       return(-1); /* XXX */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate    return(enclen);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate krb5_error_code
kg_encrypt(context,key,usage,iv,in,out,length)1210Sstevel@tonic-gate kg_encrypt(context, key, usage, iv, in, out, length)
1220Sstevel@tonic-gate      krb5_context context;
1230Sstevel@tonic-gate      krb5_keyblock *key;
1240Sstevel@tonic-gate      int usage;
1250Sstevel@tonic-gate      krb5_pointer iv;
126*7934SMark.Phalan@Sun.COM      krb5_const_pointer in;
1270Sstevel@tonic-gate      krb5_pointer out;
1285053Sgtb      unsigned int length;
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate    krb5_error_code code;
1310Sstevel@tonic-gate    size_t blocksize;
1320Sstevel@tonic-gate    krb5_data ivd, *pivd, inputd;
1330Sstevel@tonic-gate    krb5_enc_data outputd;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate    KRB5_LOG0(KRB5_INFO, "kg_encrypt() start.");
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate    if (iv) {
138*7934SMark.Phalan@Sun.COM        code = krb5_c_block_size(context, key->enctype, &blocksize);
139*7934SMark.Phalan@Sun.COM        if (code)
1400Sstevel@tonic-gate 	   return(code);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate        ivd.length = blocksize;
1430Sstevel@tonic-gate        ivd.data = MALLOC(ivd.length);
1440Sstevel@tonic-gate        if (ivd.data == NULL)
1450Sstevel@tonic-gate 	   return ENOMEM;
1460Sstevel@tonic-gate        (void) memcpy(ivd.data, iv, ivd.length);
1470Sstevel@tonic-gate        pivd = &ivd;
1480Sstevel@tonic-gate    } else {
1490Sstevel@tonic-gate        pivd = NULL;
1500Sstevel@tonic-gate    }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate    inputd.length = length;
153*7934SMark.Phalan@Sun.COM    inputd.data = (char *)in; /* Solaris Kerberos */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate    outputd.ciphertext.length = length;
1560Sstevel@tonic-gate    outputd.ciphertext.data = out;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate    code = krb5_c_encrypt(context, key, usage, pivd, &inputd, &outputd);
1590Sstevel@tonic-gate    if (pivd != NULL)
1600Sstevel@tonic-gate        krb5_free_data_contents(context, pivd);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate    KRB5_LOG(KRB5_INFO, "kg_encrypt() end. code = %d", code);
1630Sstevel@tonic-gate    return code;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /* length is the length of the cleartext. */
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate krb5_error_code
kg_decrypt(context,key,usage,iv,in,out,length)1690Sstevel@tonic-gate kg_decrypt(context, key, usage, iv, in, out, length)
1700Sstevel@tonic-gate      krb5_context context;
1710Sstevel@tonic-gate      krb5_keyblock *key;
1720Sstevel@tonic-gate      int usage;
1730Sstevel@tonic-gate      krb5_pointer iv;
174*7934SMark.Phalan@Sun.COM      krb5_const_pointer in;
1750Sstevel@tonic-gate      krb5_pointer out;
1765053Sgtb      unsigned int length;
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate    krb5_error_code code;
1790Sstevel@tonic-gate    size_t blocksize;
1800Sstevel@tonic-gate    krb5_data ivd, *pivd, outputd;
1810Sstevel@tonic-gate    krb5_enc_data inputd;
1820Sstevel@tonic-gate    KRB5_LOG0(KRB5_INFO, "kg_decrypt() start.");
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate    if (iv) {
185*7934SMark.Phalan@Sun.COM        code = krb5_c_block_size(context, key->enctype, &blocksize);
186*7934SMark.Phalan@Sun.COM        if (code)
1870Sstevel@tonic-gate 	   return(code);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate        ivd.length = blocksize;
1900Sstevel@tonic-gate        ivd.data = MALLOC(ivd.length);
1910Sstevel@tonic-gate        if (ivd.data == NULL)
1920Sstevel@tonic-gate 	   return ENOMEM;
1930Sstevel@tonic-gate        (void) memcpy(ivd.data, iv, ivd.length);
1940Sstevel@tonic-gate        pivd = &ivd;
1950Sstevel@tonic-gate    } else {
1960Sstevel@tonic-gate        pivd = NULL;
1970Sstevel@tonic-gate    }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate    inputd.enctype = ENCTYPE_UNKNOWN;
2000Sstevel@tonic-gate    inputd.ciphertext.length = length;
201*7934SMark.Phalan@Sun.COM    inputd.ciphertext.data = (char *)in; /* Solaris Kerberos */
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate    outputd.length = length;
2040Sstevel@tonic-gate    outputd.data = out;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate    code = krb5_c_decrypt(context, key, usage, pivd, &inputd, &outputd);
2070Sstevel@tonic-gate    if (pivd != NULL)
2080Sstevel@tonic-gate        krb5_free_data_contents(context, pivd);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate    KRB5_LOG(KRB5_INFO, "kg_decrypt() end. code = %d", code);
2110Sstevel@tonic-gate    return code;
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate krb5_error_code
kg_arcfour_docrypt(krb5_context context,const krb5_keyblock * longterm_key,int ms_usage,const unsigned char * kd_data,size_t kd_data_len,const unsigned char * input_buf,size_t input_len,unsigned char * output_buf)2150Sstevel@tonic-gate kg_arcfour_docrypt (krb5_context context,
2160Sstevel@tonic-gate 		const krb5_keyblock *longterm_key , int ms_usage,
2170Sstevel@tonic-gate 		const unsigned char *kd_data, size_t kd_data_len,
2180Sstevel@tonic-gate 		const unsigned char *input_buf, size_t input_len,
2190Sstevel@tonic-gate 		unsigned char *output_buf)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate   krb5_error_code code;
2220Sstevel@tonic-gate   krb5_data input, output;
2230Sstevel@tonic-gate   krb5_keyblock seq_enc_key, usage_key;
2240Sstevel@tonic-gate   unsigned char t[4];
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate   KRB5_LOG0(KRB5_INFO, "kg_arcfour_docrypt() start");
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate   bzero(&usage_key, sizeof(krb5_keyblock));
2290Sstevel@tonic-gate   bzero(&seq_enc_key, sizeof(krb5_keyblock));
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate   usage_key.length = longterm_key->length;
2320Sstevel@tonic-gate   usage_key.contents = MALLOC(usage_key.length);
2330Sstevel@tonic-gate   usage_key.enctype = longterm_key->enctype;
2340Sstevel@tonic-gate   usage_key.dk_list = NULL;
2350Sstevel@tonic-gate #ifdef _KERNEL
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate   usage_key.kef_mt  = longterm_key->kef_mt;
2380Sstevel@tonic-gate   code = init_key_kef(longterm_key->kef_mt, &usage_key);
2390Sstevel@tonic-gate   if (code)
2400Sstevel@tonic-gate 	return (code);
2410Sstevel@tonic-gate #endif /* _KERNEL */
2420Sstevel@tonic-gate   if (usage_key.contents == NULL)
2430Sstevel@tonic-gate     return (ENOMEM);
2440Sstevel@tonic-gate   seq_enc_key.length = longterm_key->length;
2450Sstevel@tonic-gate   seq_enc_key.contents = MALLOC(seq_enc_key.length);
2460Sstevel@tonic-gate   seq_enc_key.enctype = longterm_key->enctype;
2470Sstevel@tonic-gate   seq_enc_key.dk_list = NULL;
2480Sstevel@tonic-gate #ifdef _KERNEL
2490Sstevel@tonic-gate   seq_enc_key.kef_mt  = longterm_key->kef_mt;
2500Sstevel@tonic-gate   code = init_key_kef(longterm_key->kef_mt, &seq_enc_key);
2510Sstevel@tonic-gate   if (code)
2520Sstevel@tonic-gate 	return (code);
2530Sstevel@tonic-gate #endif /* _KERNEL */
2540Sstevel@tonic-gate   if (seq_enc_key.contents == NULL) {
2550Sstevel@tonic-gate     FREE ((void *) usage_key.contents, usage_key.length);
2560Sstevel@tonic-gate     return (ENOMEM);
2570Sstevel@tonic-gate   }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate   t[0] = ms_usage &0xff;
2600Sstevel@tonic-gate   t[1] = (ms_usage>>8) & 0xff;
2610Sstevel@tonic-gate   t[2] = (ms_usage>>16) & 0xff;
2620Sstevel@tonic-gate   t[3] = (ms_usage>>24) & 0xff;
2630Sstevel@tonic-gate   input.data = (void *) &t;
2640Sstevel@tonic-gate   input.length = 4;
2650Sstevel@tonic-gate   output.data = (void *) usage_key.contents;
2660Sstevel@tonic-gate   output.length = usage_key.length;
2670Sstevel@tonic-gate #ifdef _KERNEL
2680Sstevel@tonic-gate   code = krb5_hmac(context, longterm_key, &input, &output);
2690Sstevel@tonic-gate #else
2700Sstevel@tonic-gate   code = krb5_hmac(context, &krb5int_hash_md5,
2710Sstevel@tonic-gate 		longterm_key, 1, &input, &output);
2720Sstevel@tonic-gate #endif /* _KERNEL */
2730Sstevel@tonic-gate   if (code)
2740Sstevel@tonic-gate     goto cleanup_arcfour;
275*7934SMark.Phalan@Sun.COM 
2760Sstevel@tonic-gate   input.data = ( void *) kd_data;
2770Sstevel@tonic-gate   input.length = kd_data_len;
2780Sstevel@tonic-gate   output.data = (void *) seq_enc_key.contents;
2790Sstevel@tonic-gate #ifdef _KERNEL
2800Sstevel@tonic-gate   code = krb5_hmac(context, &usage_key, &input, &output);
2810Sstevel@tonic-gate #else
2820Sstevel@tonic-gate   code = krb5_hmac(context, &krb5int_hash_md5,
2830Sstevel@tonic-gate 		&usage_key, 1, &input, &output);
2840Sstevel@tonic-gate #endif /* _KERNEL */
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate   if (code)
2870Sstevel@tonic-gate     goto cleanup_arcfour;
2880Sstevel@tonic-gate   input.data = ( void * ) input_buf;
2890Sstevel@tonic-gate   input.length = input_len;
290*7934SMark.Phalan@Sun.COM   output.data = (void * ) output_buf;
2910Sstevel@tonic-gate   output.length = input_len;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate   /*
2940Sstevel@tonic-gate    * Call the arcfour encryption method directly here, we cannot
2950Sstevel@tonic-gate    * use the standard "krb5_c_encrypt" interface because we just
2960Sstevel@tonic-gate    * want the arcfour algorithm applied and not the additional MD5-HMAC
2970Sstevel@tonic-gate    * which are applied when using the standard interface.
2980Sstevel@tonic-gate    */
2990Sstevel@tonic-gate   code = krb5int_enc_arcfour.encrypt(context, &seq_enc_key, 0, &input, &output);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate  cleanup_arcfour:
3020Sstevel@tonic-gate   bzero ((void *) seq_enc_key.contents, seq_enc_key.length);
3030Sstevel@tonic-gate   bzero ((void *) usage_key.contents, usage_key.length);
3040Sstevel@tonic-gate   FREE ((void *) usage_key.contents, usage_key.length);
3050Sstevel@tonic-gate   FREE ((void *) seq_enc_key.contents, seq_enc_key.length);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate   KRB5_LOG(KRB5_INFO, "kg_arcfour_docrypt() end code = %d", code);
3080Sstevel@tonic-gate   return (code);
3090Sstevel@tonic-gate }
310*7934SMark.Phalan@Sun.COM 
311