xref: /freebsd/crypto/krb5/src/lib/crypto/krb/s2k_rc4.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #include "crypto_int.h"
3 #include "k5-utf8.h"
4 
5 krb5_error_code
krb5int_arcfour_string_to_key(const struct krb5_keytypes * ktp,const krb5_data * string,const krb5_data * salt,const krb5_data * params,krb5_keyblock * key)6 krb5int_arcfour_string_to_key(const struct krb5_keytypes *ktp,
7                               const krb5_data *string, const krb5_data *salt,
8                               const krb5_data *params, krb5_keyblock *key)
9 {
10     krb5_error_code err = 0;
11     krb5_crypto_iov iov;
12     krb5_data hash_out;
13     char *utf8;
14     unsigned char *copystr;
15     size_t copystrlen;
16 
17     if (params != NULL)
18         return KRB5_ERR_BAD_S2K_PARAMS;
19 
20     if (key->length != 16)
21         return (KRB5_BAD_MSIZE);
22 
23     /* We ignore salt per the Microsoft spec. */
24     utf8 = k5memdup0(string->data, string->length, &err);
25     if (utf8 == NULL)
26         return err;
27     err = k5_utf8_to_utf16le(utf8, &copystr, &copystrlen);
28     zapfree(utf8, string->length);
29     if (err)
30         return err;
31 
32     /* the actual MD4 hash of the data */
33     iov.flags = KRB5_CRYPTO_TYPE_DATA;
34     iov.data = make_data(copystr, copystrlen);
35     hash_out = make_data(key->contents, key->length);
36     err = krb5int_hash_md4.hash(&iov, 1, &hash_out);
37 
38     /* Zero out the data behind us */
39     zapfree(copystr, copystrlen);
40     return err;
41 }
42