xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/crypto/arcfour/arcfour_str2key.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * Copyright 2003 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 #include <k5-int.h>
9 #include <rsa-md4.h>
10 #include <arcfour.h>
11 
12 static void asctouni(unsigned char *unicode, unsigned char *ascii, size_t len)
13 {
14 	int counter;
15 	for (counter=0;counter<len;counter++) {
16 		unicode[2*counter]=ascii[counter];
17 		unicode[2*counter + 1]=0x00;
18 	}
19 }
20 
21 krb5_error_code
22 krb5int_arcfour_string_to_key(krb5_context context,
23 	const struct krb5_enc_provider *enc,
24 	const krb5_data *string, const krb5_data *salt,
25 	const krb5_data *params, krb5_keyblock *key)
26 {
27   size_t len,slen;
28   unsigned char *copystr = NULL;
29   krb5_MD4_CTX md4_context;
30 
31   if (params != NULL)
32       return KRB5_ERR_BAD_S2K_PARAMS;
33 
34   if (key->length != 16)
35     return (KRB5_BAD_MSIZE);
36 
37   /* We ignore salt per the Microsoft spec*/
38 
39   /* compute the space needed for the new string.
40      Since the password must be stored in unicode, we need to increase
41      that number by 2x.
42 
43      This should be re-evauated in the future, it makes the assumption that
44      thes user's password is in ascii.
45   */
46   slen = ((string->length)>128)?128:string->length;
47   len=(slen)*2;
48 
49   copystr = malloc(len);
50   if (copystr == NULL)
51     return ENOMEM;
52 
53   /* make the string.  start by creating the unicode version of the password*/
54   asctouni(copystr, (uchar_t *)string->data, slen );
55 
56   /* the actual MD4 hash of the data */
57   krb5_MD4Init(&md4_context);
58   krb5_MD4Update(&md4_context, (unsigned char *)copystr, len);
59   krb5_MD4Final(&md4_context);
60   memcpy(key->contents, md4_context.digest, 16);
61 
62 #if 0
63   /* test the string_to_key function */
64   printf("Hash=");
65   {
66     int counter;
67     for(counter=0;counter<16;counter++)
68       printf("%02x", md4_context.digest[counter]);
69     printf("\n");
70   }
71 #endif /* 0 */
72 
73   /* Zero out the data behind us */
74   memset (copystr, 0, len);
75   memset(&md4_context, 0, sizeof(md4_context));
76 
77   if (copystr)
78 	free(copystr);
79 
80   return 0;
81 }
82