17c478bdstevel@tonic-gate/*
2159d09aMark Phalan * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bdstevel@tonic-gate * Use is subject to license terms.
47c478bdstevel@tonic-gate */
57c478bdstevel@tonic-gate
67c478bdstevel@tonic-gate
77c478bdstevel@tonic-gate/*
8159d09aMark Phalan  * Copyright2001 by the Massachusetts Institute of Technology.
97c478bdstevel@tonic-gate * Copyright 1993 by OpenVision Technologies, Inc.
107c478bdstevel@tonic-gate *
117c478bdstevel@tonic-gate * Permission to use, copy, modify, distribute, and sell this software
127c478bdstevel@tonic-gate * and its documentation for any purpose is hereby granted without fee,
137c478bdstevel@tonic-gate * provided that the above copyright notice appears in all copies and
147c478bdstevel@tonic-gate * that both that copyright notice and this permission notice appear in
157c478bdstevel@tonic-gate * supporting documentation, and that the name of OpenVision not be used
167c478bdstevel@tonic-gate * in advertising or publicity pertaining to distribution of the software
177c478bdstevel@tonic-gate * without specific, written prior permission. OpenVision makes no
187c478bdstevel@tonic-gate * representations about the suitability of this software for any
197c478bdstevel@tonic-gate * purpose.  It is provided "as is" without express or implied warranty.
207c478bdstevel@tonic-gate *
217c478bdstevel@tonic-gate * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
227c478bdstevel@tonic-gate * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
237c478bdstevel@tonic-gate * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
247c478bdstevel@tonic-gate * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
257c478bdstevel@tonic-gate * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
267c478bdstevel@tonic-gate * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
277c478bdstevel@tonic-gate * PERFORMANCE OF THIS SOFTWARE.
287c478bdstevel@tonic-gate */
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate/*
317c478bdstevel@tonic-gate * Copyright (C) 1998 by the FundsXpress, INC.
327c478bdstevel@tonic-gate *
337c478bdstevel@tonic-gate * All rights reserved.
347c478bdstevel@tonic-gate *
357c478bdstevel@tonic-gate * Export of this software from the United States of America may require
367c478bdstevel@tonic-gate * a specific license from the United States Government.  It is the
377c478bdstevel@tonic-gate * responsibility of any person or organization contemplating export to
387c478bdstevel@tonic-gate * obtain such a license before exporting.
397c478bdstevel@tonic-gate *
407c478bdstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
417c478bdstevel@tonic-gate * distribute this software and its documentation for any purpose and
427c478bdstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
437c478bdstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
447c478bdstevel@tonic-gate * this permission notice appear in supporting documentation, and that
457c478bdstevel@tonic-gate * the name of FundsXpress. not be used in advertising or publicity pertaining
467c478bdstevel@tonic-gate * to distribution of the software without specific, written prior
477c478bdstevel@tonic-gate * permission.  FundsXpress makes no representations about the suitability of
487c478bdstevel@tonic-gate * this software for any purpose.  It is provided "as is" without express
497c478bdstevel@tonic-gate * or implied warranty.
507c478bdstevel@tonic-gate *
517c478bdstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
527c478bdstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
537c478bdstevel@tonic-gate * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
547c478bdstevel@tonic-gate */
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate/* Solaris Kerberos:  order is important here.  include gssapiP_krb5.h
577c478bdstevel@tonic-gate * before all others, otherwise we get a LINT error from MALLOC macro
587c478bdstevel@tonic-gate * being redefined in mechglueP.h */
597c478bdstevel@tonic-gate#include <gssapiP_krb5.h>
607c478bdstevel@tonic-gate#include <k5-int.h>
617c478bdstevel@tonic-gate
627c478bdstevel@tonic-gate/* Solaris Kerberos defines memory management macros in <krb5.h> */
637c478bdstevel@tonic-gate/* #include <memory.h> */
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate/*
667c478bdstevel@tonic-gate * $Id: util_crypt.c,v 1.11.6.3 2000/06/03 06:09:45 tlyu Exp $
677c478bdstevel@tonic-gate */
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gateint
707c478bdstevel@tonic-gatekg_confounder_size(context, key)
717c478bdstevel@tonic-gate     krb5_context context;
727c478bdstevel@tonic-gate     krb5_keyblock *key;
737c478bdstevel@tonic-gate{
74159d09aMark Phalan   krb5_error_code code;
757c478bdstevel@tonic-gate   size_t blocksize;
767c478bdstevel@tonic-gate   /* We special case rc4*/
777c478bdstevel@tonic-gate   if (key->enctype == ENCTYPE_ARCFOUR_HMAC)
78159d09aMark Phalan     return 8;
79159d09aMark Phalan   code = krb5_c_block_size(context, key->enctype, &blocksize);
80159d09aMark Phalan   if (code)
817c478bdstevel@tonic-gate      return(-1); /* XXX */
827c478bdstevel@tonic-gate
837c478bdstevel@tonic-gate   return(blocksize);
847c478bdstevel@tonic-gate}
857c478bdstevel@tonic-gate
867c478bdstevel@tonic-gatekrb5_error_code
877c478bdstevel@tonic-gatekg_make_confounder(context, key, buf)
887c478bdstevel@tonic-gate     krb5_context context;
897c478bdstevel@tonic-gate     krb5_keyblock *key;
907c478bdstevel@tonic-gate     unsigned char *buf;
917c478bdstevel@tonic-gate{
927c478bdstevel@tonic-gate   krb5_error_code code;
937c478bdstevel@tonic-gate   size_t blocksize;
94159d09aMark Phalan   krb5_data lrandom;
957c478bdstevel@tonic-gate
96159d09aMark Phalan   code = krb5_c_block_size(context, key->enctype, &blocksize);
97159d09aMark Phalan   if (code)
987c478bdstevel@tonic-gate       return(code);
997c478bdstevel@tonic-gate
100159d09aMark Phalan   lrandom.length = blocksize;
101159d09aMark Phalan   lrandom.data = (char *) buf;
1027c478bdstevel@tonic-gate
103159d09aMark Phalan   return(krb5_c_random_make_octets(context, &lrandom));
1047c478bdstevel@tonic-gate}
1057c478bdstevel@tonic-gate
1067c478bdstevel@tonic-gateint
1077c478bdstevel@tonic-gatekg_encrypt_size(context, key, n)
1087c478bdstevel@tonic-gate     krb5_context context;
1097c478bdstevel@tonic-gate     krb5_keyblock *key;
1107c478bdstevel@tonic-gate     int n;
1117c478bdstevel@tonic-gate{
1127c478bdstevel@tonic-gate   size_t enclen;
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gate   if (krb5_c_encrypt_length(context, key->enctype, n, &enclen) != 0)
1157c478bdstevel@tonic-gate      return(-1); /* XXX */
1167c478bdstevel@tonic-gate
1177c478bdstevel@tonic-gate   return(enclen);
1187c478bdstevel@tonic-gate}
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gatekrb5_error_code
1217c478bdstevel@tonic-gatekg_encrypt(context, key, usage, iv, in, out, length)
1227c478bdstevel@tonic-gate     krb5_context context;
1237c478bdstevel@tonic-gate     krb5_keyblock *key;
1247c478bdstevel@tonic-gate     int usage;
1257c478bdstevel@tonic-gate     krb5_pointer iv;
126159d09aMark Phalan     krb5_const_pointer in;
1277c478bdstevel@tonic-gate     krb5_pointer out;
128ab9b2e1gtb     unsigned int length;
1297c478bdstevel@tonic-gate{
1307c478bdstevel@tonic-gate   krb5_error_code code;
1317c478bdstevel@tonic-gate   size_t blocksize;
1327c478bdstevel@tonic-gate   krb5_data ivd, *pivd, inputd;
1337c478bdstevel@tonic-gate   krb5_enc_data outputd;
1347c478bdstevel@tonic-gate
1357c478bdstevel@tonic-gate   KRB5_LOG0(KRB5_INFO, "kg_encrypt() start.");
1367c478bdstevel@tonic-gate
1377c478bdstevel@tonic-gate   if (iv) {
138159d09aMark Phalan       code = krb5_c_block_size(context, key->enctype, &blocksize);
139159d09aMark Phalan       if (code)
1407c478bdstevel@tonic-gate	   return(code);
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gate       ivd.length = blocksize;
1437c478bdstevel@tonic-gate       ivd.data = MALLOC(ivd.length);
1447c478bdstevel@tonic-gate       if (ivd.data == NULL)
1457c478bdstevel@tonic-gate	   return ENOMEM;
1467c478bdstevel@tonic-gate       (void) memcpy(ivd.data, iv, ivd.length);
1477c478bdstevel@tonic-gate       pivd = &ivd;
1487c478bdstevel@tonic-gate   } else {
1497c478bdstevel@tonic-gate       pivd = NULL;
1507c478bdstevel@tonic-gate   }
1517c478bdstevel@tonic-gate
1527c478bdstevel@tonic-gate   inputd.length = length;
153159d09aMark Phalan   inputd.data = (char *)in; /* Solaris Kerberos */
1547c478bdstevel@tonic-gate
1557c478bdstevel@tonic-gate   outputd.ciphertext.length = length;
1567c478bdstevel@tonic-gate   outputd.ciphertext.data = out;
1577c478bdstevel@tonic-gate
1587c478bdstevel@tonic-gate   code = krb5_c_encrypt(context, key, usage, pivd, &inputd, &outputd);
1597c478bdstevel@tonic-gate   if (pivd != NULL)
1607c478bdstevel@tonic-gate       krb5_free_data_contents(context, pivd);
1617c478bdstevel@tonic-gate
1627c478bdstevel@tonic-gate   KRB5_LOG(KRB5_INFO, "kg_encrypt() end. code = %d", code);
1637c478bdstevel@tonic-gate   return code;
1647c478bdstevel@tonic-gate}
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gate/* length is the length of the cleartext. */
1677c478bdstevel@tonic-gate
1687c478bdstevel@tonic-gatekrb5_error_code
1697c478bdstevel@tonic-gatekg_decrypt(context, key, usage, iv, in, out, length)
1707c478bdstevel@tonic-gate     krb5_context context;
1717c478bdstevel@tonic-gate     krb5_keyblock *key;
1727c478bdstevel@tonic-gate     int usage;
1737c478bdstevel@tonic-gate     krb5_pointer iv;
174159d09aMark Phalan     krb5_const_pointer in;
1757c478bdstevel@tonic-gate     krb5_pointer out;
176ab9b2e1gtb     unsigned int length;
1777c478bdstevel@tonic-gate{
1787c478bdstevel@tonic-gate   krb5_error_code code;
1797c478bdstevel@tonic-gate   size_t blocksize;
1807c478bdstevel@tonic-gate   krb5_data ivd, *pivd, outputd;
1817c478bdstevel@tonic-gate   krb5_enc_data inputd;
1827c478bdstevel@tonic-gate   KRB5_LOG0(KRB5_INFO, "kg_decrypt() start.");
1837c478bdstevel@tonic-gate
1847c478bdstevel@tonic-gate   if (iv) {
185159d09aMark Phalan       code = krb5_c_block_size(context, key->enctype, &blocksize);
186159d09aMark Phalan       if (code)
1877c478bdstevel@tonic-gate	   return(code);
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate       ivd.length = blocksize;
1907c478bdstevel@tonic-gate       ivd.data = MALLOC(ivd.length);
1917c478bdstevel@tonic-gate       if (ivd.data == NULL)
1927c478bdstevel@tonic-gate	   return ENOMEM;
1937c478bdstevel@tonic-gate       (void) memcpy(ivd.data, iv, ivd.length);
1947c478bdstevel@tonic-gate       pivd = &ivd;
1957c478bdstevel@tonic-gate   } else {
1967c478bdstevel@tonic-gate       pivd = NULL;
1977c478bdstevel@tonic-gate   }
1987c478bdstevel@tonic-gate
1997c478bdstevel@tonic-gate   inputd.enctype = ENCTYPE_UNKNOWN;
2007c478bdstevel@tonic-gate   inputd.ciphertext.length = length;
201159d09aMark Phalan   inputd.ciphertext.data = (char *)in; /* Solaris Kerberos */
2027c478bdstevel@tonic-gate
2037c478bdstevel@tonic-gate   outputd.length = length;
2047c478bdstevel@tonic-gate   outputd.data = out;
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate   code = krb5_c_decrypt(context, key, usage, pivd, &inputd, &outputd);
2077c478bdstevel@tonic-gate   if (pivd != NULL)
2087c478bdstevel@tonic-gate       krb5_free_data_contents(context, pivd);
2097c478bdstevel@tonic-gate
2107c478bdstevel@tonic-gate   KRB5_LOG(KRB5_INFO, "kg_decrypt() end. code = %d", code);
2117c478bdstevel@tonic-gate   return code;
2127c478bdstevel@tonic-gate}
2137c478bdstevel@tonic-gate
2147c478bdstevel@tonic-gatekrb5_error_code
2157c478bdstevel@tonic-gatekg_arcfour_docrypt (krb5_context context,
2167c478bdstevel@tonic-gate		const krb5_keyblock *longterm_key , int ms_usage,
2177c478bdstevel@tonic-gate		const unsigned char *kd_data, size_t kd_data_len,
2187c478bdstevel@tonic-gate		const unsigned char *input_buf, size_t input_len,
2197c478bdstevel@tonic-gate		unsigned char *output_buf)
2207c478bdstevel@tonic-gate{
2217c478bdstevel@tonic-gate  krb5_error_code code;
2227c478bdstevel@tonic-gate  krb5_data input, output;
2237c478bdstevel@tonic-gate  krb5_keyblock seq_enc_key, usage_key;
2247c478bdstevel@tonic-gate  unsigned char t[4];
2257c478bdstevel@tonic-gate
2267c478bdstevel@tonic-gate  KRB5_LOG0(KRB5_INFO, "kg_arcfour_docrypt() start");
2277c478bdstevel@tonic-gate
2287c478bdstevel@tonic-gate  bzero(&usage_key, sizeof(krb5_keyblock));
2297c478bdstevel@tonic-gate  bzero(&seq_enc_key, sizeof(krb5_keyblock));
2307c478bdstevel@tonic-gate
2317c478bdstevel@tonic-gate  usage_key.length = longterm_key->length;
2327c478bdstevel@tonic-gate  usage_key.contents = MALLOC(usage_key.length);
2337c478bdstevel@tonic-gate  usage_key.enctype = longterm_key->enctype;
2347c478bdstevel@tonic-gate  usage_key.dk_list = NULL;
2357c478bdstevel@tonic-gate#ifdef _KERNEL
2367c478bdstevel@tonic-gate
2377c478bdstevel@tonic-gate  usage_key.kef_mt  = longterm_key->kef_mt;
2387c478bdstevel@tonic-gate  code = init_key_kef(longterm_key->kef_mt, &usage_key);
2397c478bdstevel@tonic-gate  if (code)
2407c478bdstevel@tonic-gate	return (code);
2417c478bdstevel@tonic-gate#endif /* _KERNEL */
2427c478bdstevel@tonic-gate  if (usage_key.contents == NULL)
2437c478bdstevel@tonic-gate    return (ENOMEM);
2447c478bdstevel@tonic-gate  seq_enc_key.length = longterm_key->length;
2457c478bdstevel@tonic-gate  seq_enc_key.contents = MALLOC(seq_enc_key.length);
2467c478bdstevel@tonic-gate  seq_enc_key.enctype = longterm_key->enctype;
2477c478bdstevel@tonic-gate  seq_enc_key.dk_list = NULL;
2487c478bdstevel@tonic-gate#ifdef _KERNEL
2497c478bdstevel@tonic-gate  seq_enc_key.kef_mt  = longterm_key->kef_mt;
2507c478bdstevel@tonic-gate  code = init_key_kef(longterm_key->kef_mt, &seq_enc_key);
2517c478bdstevel@tonic-gate  if (code)
2527c478bdstevel@tonic-gate	return (code);
2537c478bdstevel@tonic-gate#endif /* _KERNEL */
2547c478bdstevel@tonic-gate  if (seq_enc_key.contents == NULL) {
2557c478bdstevel@tonic-gate    FREE ((void *) usage_key.contents, usage_key.length);
2567c478bdstevel@tonic-gate    return (ENOMEM);
2577c478bdstevel@tonic-gate  }
2587c478bdstevel@tonic-gate
2597c478bdstevel@tonic-gate  t[0] = ms_usage &0xff;
2607c478bdstevel@tonic-gate  t[1] = (ms_usage>>8) & 0xff;
2617c478bdstevel@tonic-gate  t[2] = (ms_usage>>16) & 0xff;
2627c478bdstevel@tonic-gate  t[3] = (ms_usage>>24) & 0xff;
2637c478bdstevel@tonic-gate  input.data = (void *) &t;
2647c478bdstevel@tonic-gate  input.length = 4;
2657c478bdstevel@tonic-gate  output.data = (void *) usage_key.contents;
2667c478bdstevel@tonic-gate  output.length = usage_key.length;
2677c478bdstevel@tonic-gate#ifdef _KERNEL
2687c478bdstevel@tonic-gate  code = krb5_hmac(context, longterm_key, &input, &output);
2697c478bdstevel@tonic-gate#else
2707c478bdstevel@tonic-gate  code = krb5_hmac(context, &krb5int_hash_md5,
2717c478bdstevel@tonic-gate		longterm_key, 1, &input, &output);
2727c478bdstevel@tonic-gate#endif /* _KERNEL */
2737c478bdstevel@tonic-gate  if (code)
2747c478bdstevel@tonic-gate    goto cleanup_arcfour;
275159d09aMark Phalan
2767c478bdstevel@tonic-gate  input.data = ( void *) kd_data;
2777c478bdstevel@tonic-gate  input.length = kd_data_len;
2787c478bdstevel@tonic-gate  output.data = (void *) seq_enc_key.contents;
2797c478bdstevel@tonic-gate#ifdef _KERNEL
2807c478bdstevel@tonic-gate  code = krb5_hmac(context, &usage_key, &input, &output);
2817c478bdstevel@tonic-gate#else
2827c478bdstevel@tonic-gate  code = krb5_hmac(context, &krb5int_hash_md5,
2837c478bdstevel@tonic-gate		&usage_key, 1, &input, &output);
2847c478bdstevel@tonic-gate#endif /* _KERNEL */
2857c478bdstevel@tonic-gate
2867c478bdstevel@tonic-gate  if (code)
2877c478bdstevel@tonic-gate    goto cleanup_arcfour;
2887c478bdstevel@tonic-gate  input.data = ( void * ) input_buf;
2897c478bdstevel@tonic-gate  input.length = input_len;
290159d09aMark Phalan  output.data = (void * ) output_buf;
2917c478bdstevel@tonic-gate  output.length = input_len;
2927c478bdstevel@tonic-gate
2937c478bdstevel@tonic-gate  /*
2947c478bdstevel@tonic-gate   * Call the arcfour encryption method directly here, we cannot
2957c478bdstevel@tonic-gate   * use the standard "krb5_c_encrypt" interface because we just
2967c478bdstevel@tonic-gate   * want the arcfour algorithm applied and not the additional MD5-HMAC
2977c478bdstevel@tonic-gate   * which are applied when using the standard interface.
2987c478bdstevel@tonic-gate   */
2997c478bdstevel@tonic-gate  code = krb5int_enc_arcfour.encrypt(context, &seq_enc_key, 0, &input, &output);
3007c478bdstevel@tonic-gate
3017c478bdstevel@tonic-gate cleanup_arcfour:
3027c478bdstevel@tonic-gate  bzero ((void *) seq_enc_key.contents, seq_enc_key.length);
3037c478bdstevel@tonic-gate  bzero ((void *) usage_key.contents, usage_key.length);
3047c478bdstevel@tonic-gate  FREE ((void *) usage_key.contents, usage_key.length);
3057c478bdstevel@tonic-gate  FREE ((void *) seq_enc_key.contents, seq_enc_key.length);
3067c478bdstevel@tonic-gate
3077c478bdstevel@tonic-gate  KRB5_LOG(KRB5_INFO, "kg_arcfour_docrypt() end code = %d", code);
3087c478bdstevel@tonic-gate  return (code);
3097c478bdstevel@tonic-gate}
310159d09aMark Phalan
311